Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(C) 2025 Marvell.
3 : : */
4 : :
5 : : #include <rte_crypto_sym.h>
6 : : #include <rte_cryptodev.h>
7 : : #include <rte_security.h>
8 : :
9 : : #include <cryptodev_pmd.h>
10 : :
11 : : #include "roc_cpt.h"
12 : : #include "roc_se.h"
13 : :
14 : : #include "cn20k_cryptodev_sec.h"
15 : : #include "cn20k_tls.h"
16 : : #include "cnxk_cryptodev.h"
17 : : #include "cnxk_cryptodev_ops.h"
18 : : #include "cnxk_ipsec.h"
19 : : #include "cnxk_security.h"
20 : :
21 : : static int
22 : 0 : tls_xform_cipher_auth_verify(struct rte_crypto_sym_xform *cipher_xform,
23 : : struct rte_crypto_sym_xform *auth_xform)
24 : : {
25 : 0 : enum rte_crypto_cipher_algorithm c_algo = cipher_xform->cipher.algo;
26 : 0 : enum rte_crypto_auth_algorithm a_algo = auth_xform->auth.algo;
27 : : int ret = -ENOTSUP;
28 : :
29 [ # # # # ]: 0 : switch (c_algo) {
30 : 0 : case RTE_CRYPTO_CIPHER_NULL:
31 [ # # ]: 0 : if ((a_algo == RTE_CRYPTO_AUTH_MD5_HMAC) || (a_algo == RTE_CRYPTO_AUTH_SHA1_HMAC) ||
32 : 0 : (a_algo == RTE_CRYPTO_AUTH_SHA256_HMAC) ||
33 [ # # ]: 0 : (a_algo == RTE_CRYPTO_AUTH_SHA384_HMAC))
34 : : ret = 0;
35 : : break;
36 : 0 : case RTE_CRYPTO_CIPHER_3DES_CBC:
37 [ # # ]: 0 : if (a_algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
38 : : ret = 0;
39 : : break;
40 : 0 : case RTE_CRYPTO_CIPHER_AES_CBC:
41 : 0 : if ((a_algo == RTE_CRYPTO_AUTH_SHA1_HMAC) ||
42 [ # # # # ]: 0 : (a_algo == RTE_CRYPTO_AUTH_SHA256_HMAC) ||
43 : : (a_algo == RTE_CRYPTO_AUTH_SHA384_HMAC))
44 : : ret = 0;
45 : : break;
46 : : default:
47 : : break;
48 : : }
49 : :
50 : 0 : return ret;
51 : : }
52 : :
53 : : static int
54 : 0 : tls_xform_cipher_verify(struct rte_crypto_sym_xform *crypto_xform)
55 : : {
56 : 0 : enum rte_crypto_cipher_algorithm c_algo = crypto_xform->cipher.algo;
57 : 0 : uint16_t keylen = crypto_xform->cipher.key.length;
58 : :
59 [ # # ]: 0 : if (((c_algo == RTE_CRYPTO_CIPHER_NULL) && (keylen == 0)) ||
60 [ # # # # ]: 0 : ((c_algo == RTE_CRYPTO_CIPHER_3DES_CBC) && (keylen == 24)) ||
61 [ # # ]: 0 : ((c_algo == RTE_CRYPTO_CIPHER_AES_CBC) && ((keylen == 16) || (keylen == 32))))
62 : 0 : return 0;
63 : :
64 : : return -EINVAL;
65 : : }
66 : :
67 : : static int
68 : 0 : tls_xform_auth_verify(struct rte_crypto_sym_xform *crypto_xform)
69 : : {
70 : 0 : enum rte_crypto_auth_algorithm a_algo = crypto_xform->auth.algo;
71 : 0 : uint16_t keylen = crypto_xform->auth.key.length;
72 : :
73 [ # # ]: 0 : if (((a_algo == RTE_CRYPTO_AUTH_MD5_HMAC) && (keylen == 16)) ||
74 [ # # ]: 0 : ((a_algo == RTE_CRYPTO_AUTH_SHA1_HMAC) && (keylen == 20)) ||
75 [ # # ]: 0 : ((a_algo == RTE_CRYPTO_AUTH_SHA256_HMAC) && (keylen == 32)) ||
76 [ # # ]: 0 : ((a_algo == RTE_CRYPTO_AUTH_SHA384_HMAC) && (keylen == 48)))
77 : 0 : return 0;
78 : :
79 : : return -EINVAL;
80 : : }
81 : :
82 : : static int
83 : 0 : tls_xform_aead_verify(struct rte_security_tls_record_xform *tls_xform,
84 : : struct rte_crypto_sym_xform *crypto_xform)
85 : : {
86 : 0 : uint16_t keylen = crypto_xform->aead.key.length;
87 : :
88 [ # # ]: 0 : if (tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_WRITE &&
89 [ # # ]: 0 : crypto_xform->aead.op != RTE_CRYPTO_AEAD_OP_ENCRYPT)
90 : : return -EINVAL;
91 : :
92 [ # # ]: 0 : if (tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_READ &&
93 [ # # ]: 0 : crypto_xform->aead.op != RTE_CRYPTO_AEAD_OP_DECRYPT)
94 : : return -EINVAL;
95 : :
96 [ # # ]: 0 : if (crypto_xform->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
97 [ # # ]: 0 : if ((keylen == 16) || (keylen == 32))
98 : : return 0;
99 : : }
100 : :
101 [ # # # # ]: 0 : if ((crypto_xform->aead.algo == RTE_CRYPTO_AEAD_CHACHA20_POLY1305) && (keylen == 32))
102 : 0 : return 0;
103 : :
104 : : return -EINVAL;
105 : : }
106 : :
107 : : static int
108 : 0 : cnxk_tls_xform_verify(struct rte_security_tls_record_xform *tls_xform,
109 : : struct rte_crypto_sym_xform *crypto_xform)
110 : : {
111 : : struct rte_crypto_sym_xform *auth_xform, *cipher_xform = NULL;
112 : : int ret = 0;
113 : :
114 [ # # ]: 0 : if ((tls_xform->ver != RTE_SECURITY_VERSION_TLS_1_2) &&
115 [ # # ]: 0 : (tls_xform->ver != RTE_SECURITY_VERSION_DTLS_1_2) &&
116 : : (tls_xform->ver != RTE_SECURITY_VERSION_TLS_1_3))
117 : : return -EINVAL;
118 : :
119 [ # # ]: 0 : if ((tls_xform->type != RTE_SECURITY_TLS_SESS_TYPE_READ) &&
120 : : (tls_xform->type != RTE_SECURITY_TLS_SESS_TYPE_WRITE))
121 : : return -EINVAL;
122 : :
123 [ # # ]: 0 : if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
124 : : /* optional padding is not allowed in TLS-1.2 for AEAD */
125 [ # # # # ]: 0 : if ((tls_xform->options.extra_padding_enable == 1) &&
126 : : (tls_xform->ver != RTE_SECURITY_VERSION_TLS_1_3))
127 : : return -EINVAL;
128 : :
129 : 0 : return tls_xform_aead_verify(tls_xform, crypto_xform);
130 : : }
131 : :
132 : : /* TLS-1.3 only support AEAD.
133 : : * Control should not reach here for TLS-1.3
134 : : */
135 [ # # ]: 0 : if (tls_xform->ver == RTE_SECURITY_VERSION_TLS_1_3)
136 : : return -EINVAL;
137 : :
138 [ # # ]: 0 : if (tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_WRITE) {
139 : : /* Egress */
140 : :
141 : : /* First should be for auth in Egress */
142 [ # # ]: 0 : if (crypto_xform->type != RTE_CRYPTO_SYM_XFORM_AUTH)
143 : : return -EINVAL;
144 : :
145 : : /* Next if present, should be for cipher in Egress */
146 [ # # ]: 0 : if ((crypto_xform->next != NULL) &&
147 [ # # ]: 0 : (crypto_xform->next->type != RTE_CRYPTO_SYM_XFORM_CIPHER))
148 : : return -EINVAL;
149 : :
150 : : auth_xform = crypto_xform;
151 : : cipher_xform = crypto_xform->next;
152 : : } else {
153 : : /* Ingress */
154 : :
155 : : /* First can be for auth only when next is NULL in Ingress. */
156 [ # # ]: 0 : if ((crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) &&
157 [ # # ]: 0 : (crypto_xform->next != NULL))
158 : : return -EINVAL;
159 [ # # ]: 0 : else if ((crypto_xform->type != RTE_CRYPTO_SYM_XFORM_CIPHER) ||
160 [ # # ]: 0 : (crypto_xform->next->type != RTE_CRYPTO_SYM_XFORM_AUTH))
161 : : return -EINVAL;
162 : :
163 : : cipher_xform = crypto_xform;
164 : : auth_xform = crypto_xform->next;
165 : : }
166 : :
167 [ # # ]: 0 : if (cipher_xform) {
168 [ # # ]: 0 : if ((tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_WRITE) &&
169 [ # # ]: 0 : !(cipher_xform->cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT &&
170 [ # # ]: 0 : auth_xform->auth.op == RTE_CRYPTO_AUTH_OP_GENERATE))
171 : : return -EINVAL;
172 : :
173 [ # # ]: 0 : if ((tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_READ) &&
174 [ # # ]: 0 : !(cipher_xform->cipher.op == RTE_CRYPTO_CIPHER_OP_DECRYPT &&
175 [ # # ]: 0 : auth_xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY))
176 : : return -EINVAL;
177 : : } else {
178 [ # # ]: 0 : if ((tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_WRITE) &&
179 [ # # ]: 0 : (auth_xform->auth.op != RTE_CRYPTO_AUTH_OP_GENERATE))
180 : : return -EINVAL;
181 : :
182 [ # # ]: 0 : if ((tls_xform->type == RTE_SECURITY_TLS_SESS_TYPE_READ) &&
183 [ # # ]: 0 : (auth_xform->auth.op == RTE_CRYPTO_AUTH_OP_VERIFY))
184 : : return -EINVAL;
185 : : }
186 : :
187 [ # # ]: 0 : if (cipher_xform)
188 : 0 : ret = tls_xform_cipher_verify(cipher_xform);
189 : :
190 [ # # ]: 0 : if (!ret)
191 : 0 : ret = tls_xform_auth_verify(auth_xform);
192 : :
193 [ # # ]: 0 : if (cipher_xform && !ret)
194 : 0 : return tls_xform_cipher_auth_verify(cipher_xform, auth_xform);
195 : :
196 : : return ret;
197 : : }
198 : :
199 : : static size_t
200 : : tls_read_ctx_size(struct roc_ie_ow_tls_read_sa *sa, enum rte_security_tls_version tls_ver)
201 : : {
202 : : size_t size;
203 : :
204 : : /* Variable based on Anti-replay Window */
205 [ # # ]: 0 : if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
206 : : size = offsetof(struct roc_ie_ow_tls_read_sa, tls_13.ctx) +
207 : : sizeof(struct roc_ie_ow_tls_1_3_read_ctx_update_reg);
208 : : } else {
209 : : size = offsetof(struct roc_ie_ow_tls_read_sa, tls_12.ctx) +
210 : : offsetof(struct roc_ie_ow_tls_read_ctx_update_reg, ar_winbits);
211 : : }
212 : :
213 [ # # ]: 0 : if (sa->w0.s.ar_win)
214 : 0 : size += (1 << (sa->w0.s.ar_win - 1)) * sizeof(uint64_t);
215 : :
216 : : return size;
217 : : }
218 : :
219 : : static int
220 : 0 : tls_read_sa_fill(struct roc_ie_ow_tls_read_sa *read_sa,
221 : : struct rte_security_tls_record_xform *tls_xfrm,
222 : : struct rte_crypto_sym_xform *crypto_xfrm, struct cn20k_tls_opt *tls_opt)
223 : : {
224 [ # # ]: 0 : enum rte_security_tls_version tls_ver = tls_xfrm->ver;
225 : : struct rte_crypto_sym_xform *auth_xfrm, *cipher_xfrm;
226 : : const uint8_t *key = NULL;
227 : : uint64_t *tmp, *tmp_key;
228 : : uint32_t replay_win_sz;
229 : : uint8_t *cipher_key;
230 : : int i, length = 0;
231 : : size_t offset;
232 : :
233 : : /* Initialize the SA */
234 : : memset(read_sa, 0, sizeof(struct roc_ie_ow_tls_read_sa));
235 : :
236 [ # # ]: 0 : if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2) {
237 : 0 : read_sa->w2.s.version_select = ROC_IE_OW_TLS_VERSION_TLS_12;
238 : 0 : read_sa->tls_12.ctx.ar_valid_mask = tls_xfrm->tls_1_2.seq_no - 1;
239 [ # # ]: 0 : } else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2) {
240 : 0 : read_sa->w2.s.version_select = ROC_IE_OW_TLS_VERSION_DTLS_12;
241 [ # # ]: 0 : } else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
242 : 0 : read_sa->w2.s.version_select = ROC_IE_OW_TLS_VERSION_TLS_13;
243 : 0 : read_sa->tls_13.ctx.ar_valid_mask = tls_xfrm->tls_1_3.seq_no - 1;
244 : : }
245 : :
246 : 0 : cipher_key = read_sa->cipher_key;
247 : :
248 : : /* Set encryption algorithm */
249 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
250 : 0 : length = crypto_xfrm->aead.key.length;
251 [ # # ]: 0 : if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
252 : 0 : read_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_AES_GCM;
253 [ # # ]: 0 : if (length == 16)
254 : 0 : read_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_128;
255 : : else
256 : 0 : read_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_256;
257 : : }
258 : :
259 [ # # ]: 0 : if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_CHACHA20_POLY1305) {
260 : 0 : read_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_CHACHA_POLY;
261 : 0 : read_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_256;
262 : : }
263 : :
264 : 0 : key = crypto_xfrm->aead.key.data;
265 [ # # ]: 0 : memcpy(cipher_key, key, length);
266 : :
267 [ # # ]: 0 : if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2)
268 : 0 : memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_2.imp_nonce, 4);
269 [ # # ]: 0 : else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)
270 : 0 : memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->dtls_1_2.imp_nonce, 4);
271 [ # # ]: 0 : else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3)
272 : 0 : memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_3.imp_nonce, 12);
273 : :
274 : 0 : goto key_swap;
275 : : }
276 : :
277 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
278 : : auth_xfrm = crypto_xfrm;
279 : 0 : cipher_xfrm = crypto_xfrm->next;
280 : : } else {
281 : : cipher_xfrm = crypto_xfrm;
282 : 0 : auth_xfrm = crypto_xfrm->next;
283 : : }
284 : :
285 [ # # ]: 0 : if (cipher_xfrm != NULL) {
286 [ # # ]: 0 : if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC) {
287 : 0 : read_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_3DES;
288 : 0 : length = cipher_xfrm->cipher.key.length;
289 [ # # ]: 0 : } else if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
290 : 0 : read_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_AES_CBC;
291 : 0 : length = cipher_xfrm->cipher.key.length;
292 [ # # ]: 0 : if (length == 16)
293 : 0 : read_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_128;
294 [ # # ]: 0 : else if (length == 32)
295 : 0 : read_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_256;
296 : : else
297 : : return -EINVAL;
298 : : } else {
299 : : return -EINVAL;
300 : : }
301 : :
302 : 0 : key = cipher_xfrm->cipher.key.data;
303 : 0 : memcpy(cipher_key, key, length);
304 : : }
305 : :
306 [ # # # # : 0 : switch (auth_xfrm->auth.algo) {
# ]
307 : 0 : case RTE_CRYPTO_AUTH_MD5_HMAC:
308 : 0 : read_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_MD5;
309 : 0 : tls_opt->mac_len = 0;
310 : 0 : break;
311 : 0 : case RTE_CRYPTO_AUTH_SHA1_HMAC:
312 : 0 : read_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_SHA1;
313 : 0 : tls_opt->mac_len = 20;
314 : 0 : break;
315 : 0 : case RTE_CRYPTO_AUTH_SHA256_HMAC:
316 : 0 : read_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_SHA2_256;
317 : 0 : tls_opt->mac_len = 32;
318 : 0 : break;
319 : 0 : case RTE_CRYPTO_AUTH_SHA384_HMAC:
320 : 0 : read_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_SHA2_384;
321 : 0 : tls_opt->mac_len = 48;
322 : 0 : break;
323 : : default:
324 : : return -EINVAL;
325 : : }
326 : :
327 : 0 : roc_se_hmac_opad_ipad_gen(read_sa->w2.s.mac_select, auth_xfrm->auth.key.data,
328 : 0 : auth_xfrm->auth.key.length, read_sa->tls_12.opad_ipad,
329 : : ROC_SE_TLS);
330 : :
331 : : tmp = (uint64_t *)read_sa->tls_12.opad_ipad;
332 [ # # ]: 0 : for (i = 0; i < (int)(ROC_CTX_MAX_OPAD_IPAD_LEN / sizeof(uint64_t)); i++)
333 [ # # ]: 0 : tmp[i] = rte_be_to_cpu_64(tmp[i]);
334 : :
335 : 0 : key_swap:
336 : : tmp_key = (uint64_t *)cipher_key;
337 [ # # ]: 0 : for (i = 0; i < (int)(ROC_IE_OW_TLS_CTX_MAX_KEY_IV_LEN / sizeof(uint64_t)); i++)
338 [ # # ]: 0 : tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
339 : :
340 [ # # ]: 0 : if (tls_xfrm->ver == RTE_SECURITY_VERSION_DTLS_1_2) {
341 : : /* Only support power-of-two window sizes supported */
342 : 0 : replay_win_sz = tls_xfrm->dtls_1_2.ar_win_sz;
343 [ # # ]: 0 : if (replay_win_sz) {
344 [ # # ]: 0 : if (!rte_is_power_of_2(replay_win_sz) ||
345 : : replay_win_sz > ROC_IE_OW_TLS_AR_WIN_SIZE_MAX)
346 : : return -ENOTSUP;
347 : :
348 : 0 : read_sa->w0.s.ar_win = rte_log2_u32(replay_win_sz) - 5;
349 : : }
350 : : }
351 : :
352 : 0 : read_sa->w0.s.ctx_hdr_size = ROC_IE_OW_TLS_CTX_HDR_SIZE;
353 : 0 : read_sa->w0.s.aop_valid = 1;
354 : :
355 : : offset = offsetof(struct roc_ie_ow_tls_read_sa, tls_12.ctx);
356 [ # # ]: 0 : if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3)
357 : : offset = offsetof(struct roc_ie_ow_tls_read_sa, tls_13.ctx);
358 : :
359 : : /* Entire context size in 128B units */
360 : 0 : read_sa->w0.s.ctx_size =
361 : 0 : (PLT_ALIGN_CEIL(tls_read_ctx_size(read_sa, tls_ver), ROC_CTX_UNIT_128B) /
362 : 0 : ROC_CTX_UNIT_128B) -
363 : : 1;
364 : :
365 : : /* Word offset for HW managed CTX field */
366 : 0 : read_sa->w0.s.hw_ctx_off = offset / 8;
367 : 0 : read_sa->w0.s.ctx_push_size = read_sa->w0.s.hw_ctx_off;
368 : :
369 : : rte_wmb();
370 : :
371 : 0 : return 0;
372 : : }
373 : :
374 : : static int
375 : 0 : cn20k_tls_read_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
376 : : struct rte_security_tls_record_xform *tls_xfrm,
377 : : struct rte_crypto_sym_xform *crypto_xfrm,
378 : : struct cn20k_sec_session *sec_sess)
379 : : {
380 : : struct roc_ie_ow_tls_read_sa *sa_dptr;
381 : 0 : uint8_t tls_ver = tls_xfrm->ver;
382 : : struct cn20k_tls_record *tls;
383 : : union cpt_inst_w4 inst_w4;
384 : : void *read_sa;
385 : : int ret = 0;
386 : :
387 : : tls = &sec_sess->tls_rec;
388 : :
389 : 0 : read_sa = rte_zmalloc("cn20k_tls", sizeof(struct roc_ie_ow_tls_read_sa), ROC_CPTR_ALIGN);
390 [ # # ]: 0 : if (read_sa == NULL) {
391 : 0 : plt_err("Couldn't allocate memory for READ SA");
392 : 0 : return -ENOMEM;
393 : : }
394 : 0 : tls->read_sa = read_sa;
395 : :
396 : : /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
397 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ow_tls_read_sa), 8);
398 [ # # ]: 0 : if (sa_dptr == NULL) {
399 : 0 : plt_err("Could not allocate memory for SA dptr");
400 : : ret = -ENOMEM;
401 : 0 : goto sa_cptr_free;
402 : : }
403 : :
404 : : /* Translate security parameters to SA */
405 : 0 : ret = tls_read_sa_fill(sa_dptr, tls_xfrm, crypto_xfrm, &sec_sess->tls_opt);
406 [ # # ]: 0 : if (ret) {
407 : 0 : plt_err("Could not fill read session parameters");
408 : 0 : goto sa_dptr_free;
409 : : }
410 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
411 : 0 : sec_sess->iv_offset = crypto_xfrm->aead.iv.offset;
412 : 0 : sec_sess->iv_length = crypto_xfrm->aead.iv.length;
413 [ # # ]: 0 : } else if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
414 : 0 : sec_sess->iv_offset = crypto_xfrm->cipher.iv.offset;
415 : 0 : sec_sess->iv_length = crypto_xfrm->cipher.iv.length;
416 : : } else {
417 : 0 : sec_sess->iv_offset = crypto_xfrm->auth.iv.offset;
418 : 0 : sec_sess->iv_length = crypto_xfrm->auth.iv.length;
419 : : }
420 : :
421 : 0 : sec_sess->proto = RTE_SECURITY_PROTOCOL_TLS_RECORD;
422 : :
423 : : /* pre-populate CPT INST word 4 */
424 : 0 : inst_w4.u64 = 0;
425 : 0 : if ((tls_ver == RTE_SECURITY_VERSION_TLS_1_2) ||
426 [ # # ]: 0 : (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)) {
427 : 0 : inst_w4.s.opcode_major = ROC_IE_OW_TLS_MAJOR_OP_RECORD_DEC | ROC_IE_OW_INPLACE_BIT;
428 : 0 : sec_sess->tls_opt.tail_fetch_len = 0;
429 [ # # ]: 0 : if (sa_dptr->w2.s.cipher_select == ROC_IE_OW_TLS_CIPHER_3DES)
430 : 0 : sec_sess->tls_opt.tail_fetch_len = 1;
431 [ # # ]: 0 : else if (sa_dptr->w2.s.cipher_select == ROC_IE_OW_TLS_CIPHER_AES_CBC)
432 : 0 : sec_sess->tls_opt.tail_fetch_len = 2;
433 [ # # ]: 0 : } else if (tls_xfrm->ver == RTE_SECURITY_VERSION_TLS_1_3) {
434 : 0 : inst_w4.s.opcode_major =
435 : : ROC_IE_OW_TLS13_MAJOR_OP_RECORD_DEC | ROC_IE_OW_INPLACE_BIT;
436 : : }
437 : :
438 : 0 : sec_sess->tls_opt.tls_ver = tls_ver;
439 [ # # ]: 0 : sec_sess->inst.w4 = inst_w4.u64;
440 : 0 : sec_sess->inst.w7 = cnxk_cpt_sec_inst_w7_get(roc_cpt, read_sa);
441 : :
442 : : memset(read_sa, 0, sizeof(struct roc_ie_ow_tls_read_sa));
443 : :
444 : : /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
445 : : memcpy(read_sa, sa_dptr, 8);
446 : :
447 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
448 : :
449 : : /* Write session using microcode opcode */
450 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, read_sa, sizeof(struct roc_ie_ow_tls_read_sa));
451 [ # # ]: 0 : if (ret) {
452 : 0 : plt_err("Could not write read session to hardware");
453 : 0 : goto sa_dptr_free;
454 : : }
455 : :
456 : : /* Trigger CTX flush so that data is written back to DRAM */
457 : 0 : ret = roc_cpt_lf_ctx_flush(lf, read_sa, true);
458 [ # # ]: 0 : if (ret == -EFAULT) {
459 : 0 : plt_err("Could not flush TLS read session to hardware");
460 : 0 : goto sa_dptr_free;
461 : : }
462 : :
463 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
464 : :
465 : 0 : sa_dptr_free:
466 : 0 : plt_free(sa_dptr);
467 : : sa_cptr_free:
468 [ # # ]: 0 : if (ret != 0) {
469 : 0 : rte_free(read_sa);
470 : : read_sa = NULL;
471 : : }
472 : :
473 : : return ret;
474 : : }
475 : :
476 : : static int
477 : 0 : tls_write_rlens_get(struct rte_security_tls_record_xform *tls_xfrm,
478 : : struct rte_crypto_sym_xform *crypto_xfrm)
479 : : {
480 : : enum rte_crypto_cipher_algorithm c_algo = RTE_CRYPTO_CIPHER_NULL;
481 : : enum rte_crypto_auth_algorithm a_algo = RTE_CRYPTO_AUTH_NULL;
482 : : uint8_t roundup_byte, tls_hdr_len;
483 : : uint8_t mac_len, iv_len;
484 : :
485 [ # # # ]: 0 : switch (tls_xfrm->ver) {
486 : : case RTE_SECURITY_VERSION_TLS_1_2:
487 : : case RTE_SECURITY_VERSION_TLS_1_3:
488 : : tls_hdr_len = 5;
489 : : break;
490 : 0 : case RTE_SECURITY_VERSION_DTLS_1_2:
491 : : tls_hdr_len = 13;
492 : 0 : break;
493 : 0 : default:
494 : : tls_hdr_len = 0;
495 : 0 : break;
496 : : }
497 : :
498 : : /* Get Cipher and Auth algo */
499 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD)
500 : 0 : return tls_hdr_len + ROC_CPT_AES_GCM_IV_LEN + ROC_CPT_AES_GCM_MAC_LEN;
501 : :
502 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
503 : 0 : c_algo = crypto_xfrm->cipher.algo;
504 [ # # ]: 0 : if (crypto_xfrm->next)
505 : 0 : a_algo = crypto_xfrm->next->auth.algo;
506 : : } else {
507 : 0 : a_algo = crypto_xfrm->auth.algo;
508 [ # # ]: 0 : if (crypto_xfrm->next)
509 : 0 : c_algo = crypto_xfrm->next->cipher.algo;
510 : : }
511 : :
512 [ # # # # ]: 0 : switch (c_algo) {
513 : : case RTE_CRYPTO_CIPHER_NULL:
514 : : roundup_byte = 4;
515 : : iv_len = 0;
516 : : break;
517 : 0 : case RTE_CRYPTO_CIPHER_3DES_CBC:
518 : : roundup_byte = ROC_CPT_DES_BLOCK_LENGTH;
519 : : iv_len = ROC_CPT_DES_IV_LEN;
520 : 0 : break;
521 : 0 : case RTE_CRYPTO_CIPHER_AES_CBC:
522 : : roundup_byte = ROC_CPT_AES_BLOCK_LENGTH;
523 : : iv_len = ROC_CPT_AES_CBC_IV_LEN;
524 : 0 : break;
525 : 0 : default:
526 : : roundup_byte = 0;
527 : : iv_len = 0;
528 : 0 : break;
529 : : }
530 : :
531 : : switch (a_algo) {
532 : : case RTE_CRYPTO_AUTH_NULL:
533 : : mac_len = 0;
534 : : break;
535 : : case RTE_CRYPTO_AUTH_MD5_HMAC:
536 : : mac_len = 16;
537 : : break;
538 : : case RTE_CRYPTO_AUTH_SHA1_HMAC:
539 : : mac_len = 20;
540 : : break;
541 : : case RTE_CRYPTO_AUTH_SHA256_HMAC:
542 : : mac_len = 32;
543 : : break;
544 : : case RTE_CRYPTO_AUTH_SHA384_HMAC:
545 : : mac_len = 32;
546 : : break;
547 : : default:
548 : : mac_len = 0;
549 : : break;
550 : : }
551 : :
552 : 0 : return tls_hdr_len + iv_len + mac_len + roundup_byte;
553 : : }
554 : :
555 : : static int
556 : 0 : tls_write_sa_fill(struct roc_ie_ow_tls_write_sa *write_sa,
557 : : struct rte_security_tls_record_xform *tls_xfrm,
558 : : struct rte_crypto_sym_xform *crypto_xfrm)
559 : : {
560 : 0 : enum rte_security_tls_version tls_ver = tls_xfrm->ver;
561 : : struct rte_crypto_sym_xform *auth_xfrm, *cipher_xfrm;
562 : : const uint8_t *key = NULL;
563 : : uint8_t *cipher_key;
564 : : uint64_t *tmp_key;
565 : : int i, length = 0;
566 : : size_t offset;
567 : :
568 [ # # ]: 0 : if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2) {
569 : 0 : write_sa->w2.s.version_select = ROC_IE_OW_TLS_VERSION_TLS_12;
570 : 0 : write_sa->tls_12.seq_num = tls_xfrm->tls_1_2.seq_no - 1;
571 [ # # ]: 0 : } else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2) {
572 : 0 : write_sa->w2.s.version_select = ROC_IE_OW_TLS_VERSION_DTLS_12;
573 : 0 : write_sa->tls_12.seq_num = ((uint64_t)tls_xfrm->dtls_1_2.epoch << 48) |
574 : 0 : (tls_xfrm->dtls_1_2.seq_no & 0x0000ffffffffffff);
575 : 0 : write_sa->tls_12.seq_num -= 1;
576 [ # # ]: 0 : } else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
577 : 0 : write_sa->w2.s.version_select = ROC_IE_OW_TLS_VERSION_TLS_13;
578 : 0 : write_sa->tls_13.seq_num = tls_xfrm->tls_1_3.seq_no - 1;
579 : : }
580 : :
581 : 0 : cipher_key = write_sa->cipher_key;
582 : :
583 : : /* Set encryption algorithm */
584 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
585 : 0 : length = crypto_xfrm->aead.key.length;
586 [ # # ]: 0 : if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
587 : 0 : write_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_AES_GCM;
588 [ # # ]: 0 : if (length == 16)
589 : 0 : write_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_128;
590 : : else
591 : 0 : write_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_256;
592 : : }
593 [ # # ]: 0 : if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_CHACHA20_POLY1305) {
594 : 0 : write_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_CHACHA_POLY;
595 : 0 : write_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_256;
596 : : }
597 : :
598 : 0 : key = crypto_xfrm->aead.key.data;
599 [ # # ]: 0 : memcpy(cipher_key, key, length);
600 : :
601 [ # # ]: 0 : if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2)
602 : 0 : memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_2.imp_nonce, 4);
603 [ # # ]: 0 : else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)
604 : 0 : memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->dtls_1_2.imp_nonce, 4);
605 [ # # ]: 0 : else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3)
606 : 0 : memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_3.imp_nonce, 12);
607 : :
608 : 0 : goto key_swap;
609 : : }
610 : :
611 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
612 : : auth_xfrm = crypto_xfrm;
613 : 0 : cipher_xfrm = crypto_xfrm->next;
614 : : } else {
615 : : cipher_xfrm = crypto_xfrm;
616 : 0 : auth_xfrm = crypto_xfrm->next;
617 : : }
618 : :
619 [ # # ]: 0 : if (cipher_xfrm != NULL) {
620 [ # # ]: 0 : if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC) {
621 : 0 : write_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_3DES;
622 : 0 : length = cipher_xfrm->cipher.key.length;
623 [ # # ]: 0 : } else if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
624 : 0 : write_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_AES_CBC;
625 : 0 : length = cipher_xfrm->cipher.key.length;
626 [ # # ]: 0 : if (length == 16)
627 : 0 : write_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_128;
628 [ # # ]: 0 : else if (length == 32)
629 : 0 : write_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_256;
630 : : else
631 : : return -EINVAL;
632 : : } else {
633 : : return -EINVAL;
634 : : }
635 : :
636 : 0 : key = cipher_xfrm->cipher.key.data;
637 [ # # ]: 0 : if (key != NULL && length != 0) {
638 : : /* Copy encryption key */
639 : 0 : memcpy(cipher_key, key, length);
640 : : }
641 : : }
642 : :
643 [ # # ]: 0 : if (auth_xfrm != NULL) {
644 [ # # ]: 0 : if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_MD5_HMAC)
645 : 0 : write_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_MD5;
646 [ # # ]: 0 : else if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
647 : 0 : write_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_SHA1;
648 [ # # ]: 0 : else if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_SHA256_HMAC)
649 : 0 : write_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_SHA2_256;
650 [ # # ]: 0 : else if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_SHA384_HMAC)
651 : 0 : write_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_SHA2_384;
652 : : else
653 : : return -EINVAL;
654 : :
655 : 0 : roc_se_hmac_opad_ipad_gen(write_sa->w2.s.mac_select, auth_xfrm->auth.key.data,
656 : 0 : auth_xfrm->auth.key.length, write_sa->tls_12.opad_ipad,
657 : : ROC_SE_TLS);
658 : : }
659 : :
660 : 0 : tmp_key = (uint64_t *)write_sa->tls_12.opad_ipad;
661 [ # # ]: 0 : for (i = 0; i < (int)(ROC_CTX_MAX_OPAD_IPAD_LEN / sizeof(uint64_t)); i++)
662 [ # # ]: 0 : tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
663 : :
664 : 0 : key_swap:
665 : : tmp_key = (uint64_t *)cipher_key;
666 [ # # ]: 0 : for (i = 0; i < (int)(ROC_IE_OW_TLS_CTX_MAX_KEY_IV_LEN / sizeof(uint64_t)); i++)
667 [ # # ]: 0 : tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
668 : :
669 : 0 : write_sa->w0.s.ctx_hdr_size = ROC_IE_OW_TLS_CTX_HDR_SIZE;
670 : : /* Entire context size in 128B units */
671 : 0 : write_sa->w0.s.ctx_size =
672 : : (PLT_ALIGN_CEIL(sizeof(struct roc_ie_ow_tls_write_sa), ROC_CTX_UNIT_128B) /
673 : : ROC_CTX_UNIT_128B) -
674 : : 1;
675 : : offset = offsetof(struct roc_ie_ow_tls_write_sa, tls_12.w26_rsvd7);
676 : :
677 [ # # ]: 0 : if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
678 : : offset = offsetof(struct roc_ie_ow_tls_write_sa, tls_13.w10_rsvd7);
679 : 0 : write_sa->w0.s.ctx_size -= 1;
680 : : }
681 : :
682 : : /* Word offset for HW managed CTX field */
683 : 0 : write_sa->w0.s.hw_ctx_off = offset / 8;
684 : 0 : write_sa->w0.s.ctx_push_size = write_sa->w0.s.hw_ctx_off;
685 : :
686 : 0 : write_sa->w0.s.aop_valid = 1;
687 : :
688 : 0 : write_sa->w2.s.iv_at_cptr = ROC_IE_OW_TLS_IV_SRC_DEFAULT;
689 : :
690 [ # # ]: 0 : if (write_sa->w2.s.version_select != ROC_IE_OW_TLS_VERSION_TLS_13) {
691 : : #ifdef LA_IPSEC_DEBUG
692 : : if (tls_xfrm->options.iv_gen_disable == 1)
693 : : write_sa->w2.s.iv_at_cptr = ROC_IE_OW_TLS_IV_SRC_FROM_SA;
694 : : #else
695 [ # # ]: 0 : if (tls_xfrm->options.iv_gen_disable) {
696 : 0 : plt_err("Application provided IV is not supported");
697 : 0 : return -ENOTSUP;
698 : : }
699 : : #endif
700 : : }
701 : :
702 : : rte_wmb();
703 : :
704 : 0 : return 0;
705 : : }
706 : :
707 : : static int
708 : 0 : cn20k_tls_write_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
709 : : struct rte_security_tls_record_xform *tls_xfrm,
710 : : struct rte_crypto_sym_xform *crypto_xfrm,
711 : : struct cn20k_sec_session *sec_sess)
712 : : {
713 : : struct roc_ie_ow_tls_write_sa *sa_dptr;
714 : 0 : uint8_t tls_ver = tls_xfrm->ver;
715 : : struct cn20k_tls_record *tls;
716 : : union cpt_inst_w4 inst_w4;
717 : : void *write_sa;
718 : : int ret = 0;
719 : :
720 : : tls = &sec_sess->tls_rec;
721 : :
722 : 0 : write_sa = rte_zmalloc("cn20k_tls", sizeof(struct roc_ie_ow_tls_write_sa), ROC_CPTR_ALIGN);
723 [ # # ]: 0 : if (write_sa == NULL) {
724 : 0 : plt_err("Couldn't allocate memory for WRITE SA");
725 : 0 : return -ENOMEM;
726 : : }
727 : 0 : tls->write_sa = write_sa;
728 : :
729 : : /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
730 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ow_tls_write_sa), 8);
731 [ # # ]: 0 : if (sa_dptr == NULL) {
732 : 0 : plt_err("Could not allocate memory for SA dptr");
733 : : ret = -ENOMEM;
734 : 0 : goto sa_cptr_free;
735 : : }
736 : :
737 : : /* Translate security parameters to SA */
738 : 0 : ret = tls_write_sa_fill(sa_dptr, tls_xfrm, crypto_xfrm);
739 [ # # ]: 0 : if (ret) {
740 : 0 : plt_err("Could not fill write session parameters");
741 : 0 : goto sa_dptr_free;
742 : : }
743 : :
744 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
745 : 0 : sec_sess->iv_offset = crypto_xfrm->aead.iv.offset;
746 : 0 : sec_sess->iv_length = crypto_xfrm->aead.iv.length;
747 [ # # ]: 0 : } else if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
748 : 0 : sec_sess->iv_offset = crypto_xfrm->cipher.iv.offset;
749 : 0 : sec_sess->iv_length = crypto_xfrm->cipher.iv.length;
750 : : } else {
751 : 0 : sec_sess->iv_offset = crypto_xfrm->next->cipher.iv.offset;
752 : 0 : sec_sess->iv_length = crypto_xfrm->next->cipher.iv.length;
753 : : }
754 : :
755 : 0 : sec_sess->tls_opt.is_write = 1;
756 : 0 : sec_sess->tls_opt.pad_shift = 0;
757 : 0 : sec_sess->tls_opt.tls_ver = tls_ver;
758 : 0 : sec_sess->tls_opt.enable_padding = tls_xfrm->options.extra_padding_enable;
759 : 0 : sec_sess->max_extended_len = tls_write_rlens_get(tls_xfrm, crypto_xfrm);
760 : 0 : sec_sess->proto = RTE_SECURITY_PROTOCOL_TLS_RECORD;
761 : :
762 : : /* pre-populate CPT INST word 4 */
763 : 0 : inst_w4.u64 = 0;
764 : 0 : if ((tls_ver == RTE_SECURITY_VERSION_TLS_1_2) ||
765 [ # # ]: 0 : (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)) {
766 : 0 : inst_w4.s.opcode_major = ROC_IE_OW_TLS_MAJOR_OP_RECORD_ENC | ROC_IE_OW_INPLACE_BIT;
767 [ # # ]: 0 : if (sa_dptr->w2.s.cipher_select == ROC_IE_OW_TLS_CIPHER_3DES)
768 : 0 : sec_sess->tls_opt.pad_shift = 3;
769 : : else
770 : 0 : sec_sess->tls_opt.pad_shift = 4;
771 [ # # ]: 0 : } else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
772 : 0 : inst_w4.s.opcode_major =
773 : : ROC_IE_OW_TLS13_MAJOR_OP_RECORD_ENC | ROC_IE_OW_INPLACE_BIT;
774 : : }
775 [ # # ]: 0 : sec_sess->inst.w4 = inst_w4.u64;
776 : 0 : sec_sess->inst.w7 = cnxk_cpt_sec_inst_w7_get(roc_cpt, write_sa);
777 : :
778 : : memset(write_sa, 0, sizeof(struct roc_ie_ow_tls_write_sa));
779 : :
780 : : /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
781 : : memcpy(write_sa, sa_dptr, 8);
782 : :
783 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
784 : :
785 : : /* Write session using microcode opcode */
786 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, write_sa, sizeof(struct roc_ie_ow_tls_write_sa));
787 [ # # ]: 0 : if (ret) {
788 : 0 : plt_err("Could not write tls write session to hardware");
789 : 0 : goto sa_dptr_free;
790 : : }
791 : :
792 : : /* Trigger CTX flush so that data is written back to DRAM */
793 : 0 : ret = roc_cpt_lf_ctx_flush(lf, write_sa, false);
794 [ # # ]: 0 : if (ret == -EFAULT) {
795 : 0 : plt_err("Could not flush TLS write session to hardware");
796 : 0 : goto sa_dptr_free;
797 : : }
798 : :
799 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
800 : :
801 : 0 : sa_dptr_free:
802 : 0 : plt_free(sa_dptr);
803 : : sa_cptr_free:
804 [ # # ]: 0 : if (ret != 0) {
805 : 0 : rte_free(write_sa);
806 : : write_sa = NULL;
807 : : }
808 : :
809 : : return ret;
810 : : }
811 : :
812 : : static void
813 : : tls_write_sa_init(struct roc_ie_ow_tls_write_sa *sa)
814 : : {
815 : : size_t offset;
816 : :
817 : : memset(sa, 0, sizeof(struct roc_ie_ow_tls_write_sa));
818 : :
819 : : offset = offsetof(struct roc_ie_ow_tls_write_sa, tls_12.w26_rsvd7);
820 : 0 : sa->w0.s.hw_ctx_off = offset / ROC_CTX_UNIT_8B;
821 : 0 : sa->w0.s.ctx_push_size = sa->w0.s.hw_ctx_off;
822 : 0 : sa->w0.s.ctx_size = ROC_IE_OW_TLS_CTX_ILEN;
823 : 0 : sa->w0.s.ctx_hdr_size = ROC_IE_OW_TLS_CTX_HDR_SIZE;
824 : 0 : sa->w0.s.aop_valid = 1;
825 : : }
826 : :
827 : : static void
828 : : tls_read_sa_init(struct roc_ie_ow_tls_read_sa *sa)
829 : : {
830 : : size_t offset;
831 : :
832 : : memset(sa, 0, sizeof(struct roc_ie_ow_tls_read_sa));
833 : :
834 : : offset = offsetof(struct roc_ie_ow_tls_read_sa, tls_12.ctx);
835 : 0 : sa->w0.s.hw_ctx_off = offset / ROC_CTX_UNIT_8B;
836 : 0 : sa->w0.s.ctx_push_size = sa->w0.s.hw_ctx_off;
837 : 0 : sa->w0.s.ctx_size = ROC_IE_OW_TLS_CTX_ILEN;
838 : 0 : sa->w0.s.ctx_hdr_size = ROC_IE_OW_TLS_CTX_HDR_SIZE;
839 : 0 : sa->w0.s.aop_valid = 1;
840 : : }
841 : :
842 : : int
843 : 0 : cn20k_tls_record_session_update(struct cnxk_cpt_vf *vf, struct cnxk_cpt_qp *qp,
844 : : struct cn20k_sec_session *sess,
845 : : struct rte_security_session_conf *conf)
846 : : {
847 : : struct roc_cpt *roc_cpt;
848 : : int ret;
849 : :
850 [ # # ]: 0 : if (conf->tls_record.type == RTE_SECURITY_TLS_SESS_TYPE_READ)
851 : : return -ENOTSUP;
852 : :
853 : 0 : roc_cpt = &vf->cpt;
854 : 0 : ret = cn20k_tls_write_sa_create(roc_cpt, &qp->lf, &conf->tls_record, conf->crypto_xform,
855 : : (struct cn20k_sec_session *)sess);
856 : :
857 : 0 : return ret;
858 : : }
859 : :
860 : : int
861 : 0 : cn20k_tls_record_session_create(struct cnxk_cpt_vf *vf, struct cnxk_cpt_qp *qp,
862 : : struct rte_security_tls_record_xform *tls_xfrm,
863 : : struct rte_crypto_sym_xform *crypto_xfrm,
864 : : struct rte_security_session *sess)
865 : : {
866 : : struct roc_cpt *roc_cpt;
867 : : int ret;
868 : :
869 : 0 : ret = cnxk_tls_xform_verify(tls_xfrm, crypto_xfrm);
870 [ # # ]: 0 : if (ret)
871 : : return ret;
872 : :
873 : 0 : roc_cpt = &vf->cpt;
874 : :
875 [ # # ]: 0 : if (tls_xfrm->type == RTE_SECURITY_TLS_SESS_TYPE_READ)
876 : 0 : return cn20k_tls_read_sa_create(roc_cpt, &qp->lf, tls_xfrm, crypto_xfrm,
877 : : (struct cn20k_sec_session *)sess);
878 : : else
879 : 0 : return cn20k_tls_write_sa_create(roc_cpt, &qp->lf, tls_xfrm, crypto_xfrm,
880 : : (struct cn20k_sec_session *)sess);
881 : : }
882 : :
883 : : int
884 : 0 : cn20k_sec_tls_session_destroy(struct cnxk_cpt_qp *qp, struct cn20k_sec_session *sess)
885 : : {
886 : : struct cn20k_tls_record *tls;
887 : : struct roc_cpt_lf *lf;
888 : : void *sa_dptr = NULL;
889 : : int ret = -ENOMEM;
890 : :
891 : 0 : lf = &qp->lf;
892 : :
893 : : tls = &sess->tls_rec;
894 : :
895 [ # # ]: 0 : if (tls->sa_ptr == NULL)
896 : : return -EINVAL;
897 : :
898 : : /* Trigger CTX flush to write dirty data back to DRAM */
899 : 0 : roc_cpt_lf_ctx_flush(lf, tls->read_sa, false);
900 : :
901 [ # # ]: 0 : if (sess->tls_opt.is_write) {
902 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ow_tls_write_sa), 8);
903 [ # # ]: 0 : if (sa_dptr != NULL) {
904 : : tls_write_sa_init(sa_dptr);
905 : :
906 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, tls->write_sa,
907 : : sizeof(struct roc_ie_ow_tls_write_sa));
908 : 0 : plt_free(sa_dptr);
909 : : }
910 [ # # ]: 0 : if (ret) {
911 : : /* MC write_ctx failed. Attempt reload of CTX */
912 : :
913 : : /* Wait for 1 ms so that flush is complete */
914 : : rte_delay_ms(1);
915 : :
916 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
917 : :
918 : : /* Trigger CTX reload to fetch new data from DRAM */
919 : 0 : roc_cpt_lf_ctx_reload(lf, tls->write_sa);
920 : : }
921 : : } else {
922 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ow_tls_read_sa), 8);
923 [ # # ]: 0 : if (sa_dptr != NULL) {
924 : : tls_read_sa_init(sa_dptr);
925 : :
926 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, tls->read_sa,
927 : : sizeof(struct roc_ie_ow_tls_read_sa));
928 : 0 : plt_free(sa_dptr);
929 : : }
930 [ # # ]: 0 : if (ret) {
931 : : /* MC write_ctx failed. Attempt reload of CTX */
932 : :
933 : : /* Wait for 1 ms so that flush is complete */
934 : : rte_delay_ms(1);
935 : :
936 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
937 : :
938 : : /* Trigger CTX reload to fetch new data from DRAM */
939 : 0 : roc_cpt_lf_ctx_reload(lf, tls->read_sa);
940 : : }
941 : : }
942 : :
943 : 0 : rte_free(tls->sa_ptr);
944 : :
945 : 0 : return 0;
946 : : }
|