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 : if (length != 16 && length != 32)
253 : : return -EINVAL;
254 : 0 : read_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_AES_GCM;
255 [ # # ]: 0 : if (length == 16)
256 : 0 : read_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_128;
257 : : else
258 : 0 : read_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_256;
259 [ # # ]: 0 : } else if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_CHACHA20_POLY1305) {
260 [ # # ]: 0 : if (length != 32)
261 : : return -EINVAL;
262 : 0 : read_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_CHACHA_POLY;
263 : 0 : read_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_256;
264 : : } else {
265 : : return -EINVAL;
266 : : }
267 : :
268 : 0 : key = crypto_xfrm->aead.key.data;
269 [ # # ]: 0 : if (key == NULL)
270 : : return -EINVAL;
271 [ # # ]: 0 : memcpy(cipher_key, key, length);
272 : :
273 [ # # ]: 0 : if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2)
274 : 0 : memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_2.imp_nonce, 4);
275 [ # # ]: 0 : else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)
276 : 0 : memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->dtls_1_2.imp_nonce, 4);
277 [ # # ]: 0 : else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3)
278 : 0 : memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_3.imp_nonce, 12);
279 : :
280 : 0 : goto key_swap;
281 : : }
282 : :
283 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
284 : : auth_xfrm = crypto_xfrm;
285 : 0 : cipher_xfrm = crypto_xfrm->next;
286 : : } else {
287 : : cipher_xfrm = crypto_xfrm;
288 : 0 : auth_xfrm = crypto_xfrm->next;
289 : : }
290 : :
291 [ # # ]: 0 : if (cipher_xfrm != NULL) {
292 [ # # ]: 0 : if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC) {
293 : 0 : read_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_3DES;
294 : 0 : length = cipher_xfrm->cipher.key.length;
295 [ # # ]: 0 : if (length != 24)
296 : : return -EINVAL;
297 [ # # ]: 0 : } else if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
298 : 0 : read_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_AES_CBC;
299 : 0 : length = cipher_xfrm->cipher.key.length;
300 [ # # ]: 0 : if (length == 16)
301 : 0 : read_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_128;
302 [ # # ]: 0 : else if (length == 32)
303 : 0 : read_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_256;
304 : : else
305 : : return -EINVAL;
306 : : } else {
307 : : return -EINVAL;
308 : : }
309 : :
310 : 0 : key = cipher_xfrm->cipher.key.data;
311 [ # # ]: 0 : if (key == NULL || length == 0)
312 : : return -EINVAL;
313 : 0 : memcpy(cipher_key, key, length);
314 : : }
315 : :
316 [ # # # # : 0 : switch (auth_xfrm->auth.algo) {
# ]
317 : 0 : case RTE_CRYPTO_AUTH_MD5_HMAC:
318 : 0 : read_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_MD5;
319 : 0 : tls_opt->mac_len = 0;
320 : 0 : break;
321 : 0 : case RTE_CRYPTO_AUTH_SHA1_HMAC:
322 : 0 : read_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_SHA1;
323 : 0 : tls_opt->mac_len = 20;
324 : 0 : break;
325 : 0 : case RTE_CRYPTO_AUTH_SHA256_HMAC:
326 : 0 : read_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_SHA2_256;
327 : 0 : tls_opt->mac_len = 32;
328 : 0 : break;
329 : 0 : case RTE_CRYPTO_AUTH_SHA384_HMAC:
330 : 0 : read_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_SHA2_384;
331 : 0 : tls_opt->mac_len = 48;
332 : 0 : break;
333 : : default:
334 : : return -EINVAL;
335 : : }
336 : :
337 : 0 : roc_se_hmac_opad_ipad_gen(read_sa->w2.s.mac_select, auth_xfrm->auth.key.data,
338 : 0 : auth_xfrm->auth.key.length, read_sa->tls_12.opad_ipad,
339 : : ROC_SE_TLS);
340 : :
341 : : tmp = (uint64_t *)read_sa->tls_12.opad_ipad;
342 [ # # ]: 0 : for (i = 0; i < (int)(ROC_CTX_MAX_OPAD_IPAD_LEN / sizeof(uint64_t)); i++)
343 [ # # ]: 0 : tmp[i] = rte_be_to_cpu_64(tmp[i]);
344 : :
345 : 0 : key_swap:
346 : : tmp_key = (uint64_t *)cipher_key;
347 [ # # ]: 0 : for (i = 0; i < (int)(ROC_IE_OW_TLS_CTX_MAX_KEY_IV_LEN / sizeof(uint64_t)); i++)
348 [ # # ]: 0 : tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
349 : :
350 [ # # ]: 0 : if (tls_xfrm->ver == RTE_SECURITY_VERSION_DTLS_1_2) {
351 : : /* Only support power-of-two window sizes supported */
352 : 0 : replay_win_sz = tls_xfrm->dtls_1_2.ar_win_sz;
353 [ # # ]: 0 : if (replay_win_sz) {
354 [ # # ]: 0 : if (!rte_is_power_of_2(replay_win_sz) ||
355 : : replay_win_sz > ROC_IE_OW_TLS_AR_WIN_SIZE_MAX)
356 : : return -ENOTSUP;
357 : :
358 : 0 : read_sa->w0.s.ar_win = rte_log2_u32(replay_win_sz) - 5;
359 : : }
360 : : }
361 : :
362 : 0 : read_sa->w0.s.ctx_hdr_size = ROC_IE_OW_TLS_CTX_HDR_SIZE;
363 : 0 : read_sa->w0.s.aop_valid = 1;
364 : :
365 : : offset = offsetof(struct roc_ie_ow_tls_read_sa, tls_12.ctx);
366 [ # # ]: 0 : if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3)
367 : : offset = offsetof(struct roc_ie_ow_tls_read_sa, tls_13.ctx);
368 : :
369 : : /* Entire context size in 128B units */
370 : 0 : read_sa->w0.s.ctx_size =
371 : 0 : (PLT_ALIGN_CEIL(tls_read_ctx_size(read_sa, tls_ver), ROC_CTX_UNIT_128B) /
372 : 0 : ROC_CTX_UNIT_128B) -
373 : : 1;
374 : :
375 : : /* Word offset for HW managed CTX field */
376 : 0 : read_sa->w0.s.hw_ctx_off = offset / 8;
377 : 0 : read_sa->w0.s.ctx_push_size = read_sa->w0.s.hw_ctx_off;
378 : :
379 : : rte_wmb();
380 : :
381 : 0 : return 0;
382 : : }
383 : :
384 : : static int
385 : 0 : cn20k_tls_read_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
386 : : struct rte_security_tls_record_xform *tls_xfrm,
387 : : struct rte_crypto_sym_xform *crypto_xfrm,
388 : : struct cn20k_sec_session *sec_sess)
389 : : {
390 : : struct roc_ie_ow_tls_read_sa *sa_dptr;
391 : 0 : uint8_t tls_ver = tls_xfrm->ver;
392 : : struct cn20k_tls_record *tls;
393 : : union cpt_inst_w4 inst_w4;
394 : : void *read_sa;
395 : : int ret = 0;
396 : :
397 : : tls = &sec_sess->tls_rec;
398 : :
399 : 0 : read_sa = rte_zmalloc("cn20k_tls", sizeof(struct roc_ie_ow_tls_read_sa), ROC_CPTR_ALIGN);
400 [ # # ]: 0 : if (read_sa == NULL) {
401 : 0 : plt_err("Couldn't allocate memory for READ SA");
402 : 0 : return -ENOMEM;
403 : : }
404 : 0 : tls->read_sa = read_sa;
405 : :
406 : : /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
407 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ow_tls_read_sa), 8);
408 [ # # ]: 0 : if (sa_dptr == NULL) {
409 : 0 : plt_err("Could not allocate memory for SA dptr");
410 : : ret = -ENOMEM;
411 : 0 : goto sa_cptr_free;
412 : : }
413 : :
414 : : /* Translate security parameters to SA */
415 : 0 : ret = tls_read_sa_fill(sa_dptr, tls_xfrm, crypto_xfrm, &sec_sess->tls_opt);
416 [ # # ]: 0 : if (ret) {
417 : 0 : plt_err("Could not fill read session parameters");
418 : 0 : goto sa_dptr_free;
419 : : }
420 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
421 : 0 : sec_sess->iv_offset = crypto_xfrm->aead.iv.offset;
422 : 0 : sec_sess->iv_length = crypto_xfrm->aead.iv.length;
423 [ # # ]: 0 : } else if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
424 : 0 : sec_sess->iv_offset = crypto_xfrm->cipher.iv.offset;
425 : 0 : sec_sess->iv_length = crypto_xfrm->cipher.iv.length;
426 : : } else {
427 : 0 : sec_sess->iv_offset = crypto_xfrm->auth.iv.offset;
428 : 0 : sec_sess->iv_length = crypto_xfrm->auth.iv.length;
429 : : }
430 : :
431 : 0 : sec_sess->proto = RTE_SECURITY_PROTOCOL_TLS_RECORD;
432 : :
433 : : /* pre-populate CPT INST word 4 */
434 : 0 : inst_w4.u64 = 0;
435 : 0 : if ((tls_ver == RTE_SECURITY_VERSION_TLS_1_2) ||
436 [ # # ]: 0 : (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)) {
437 : 0 : inst_w4.s.opcode_major = ROC_IE_OW_TLS_MAJOR_OP_RECORD_DEC | ROC_IE_OW_INPLACE_BIT;
438 : 0 : sec_sess->tls_opt.tail_fetch_len = 0;
439 [ # # ]: 0 : if (sa_dptr->w2.s.cipher_select == ROC_IE_OW_TLS_CIPHER_3DES)
440 : 0 : sec_sess->tls_opt.tail_fetch_len = 1;
441 [ # # ]: 0 : else if (sa_dptr->w2.s.cipher_select == ROC_IE_OW_TLS_CIPHER_AES_CBC)
442 : 0 : sec_sess->tls_opt.tail_fetch_len = 2;
443 [ # # ]: 0 : } else if (tls_xfrm->ver == RTE_SECURITY_VERSION_TLS_1_3) {
444 : 0 : inst_w4.s.opcode_major =
445 : : ROC_IE_OW_TLS13_MAJOR_OP_RECORD_DEC | ROC_IE_OW_INPLACE_BIT;
446 : : }
447 : :
448 : 0 : sec_sess->tls_opt.tls_ver = tls_ver;
449 [ # # ]: 0 : sec_sess->inst.w4 = inst_w4.u64;
450 : 0 : sec_sess->inst.w7 = cnxk_cpt_sec_inst_w7_get(roc_cpt, read_sa);
451 : :
452 : : memset(read_sa, 0, sizeof(struct roc_ie_ow_tls_read_sa));
453 : :
454 : : /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
455 : : memcpy(read_sa, sa_dptr, 8);
456 : :
457 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
458 : :
459 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, read_sa, sizeof(struct roc_ie_ow_tls_read_sa));
460 [ # # ]: 0 : if (ret) {
461 : 0 : plt_err("Could not write read session to hardware");
462 : 0 : goto sa_dptr_free;
463 : : }
464 : :
465 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
466 : :
467 : 0 : sa_dptr_free:
468 : 0 : plt_free(sa_dptr);
469 : : sa_cptr_free:
470 [ # # ]: 0 : if (ret != 0) {
471 : 0 : rte_free(read_sa);
472 : : read_sa = NULL;
473 : : }
474 : :
475 : : return ret;
476 : : }
477 : :
478 : : static int
479 : 0 : tls_write_rlens_get(struct rte_security_tls_record_xform *tls_xfrm,
480 : : struct rte_crypto_sym_xform *crypto_xfrm)
481 : : {
482 : : enum rte_crypto_cipher_algorithm c_algo = RTE_CRYPTO_CIPHER_NULL;
483 : : enum rte_crypto_auth_algorithm a_algo = RTE_CRYPTO_AUTH_NULL;
484 : : uint8_t roundup_byte, tls_hdr_len;
485 : : uint8_t mac_len, iv_len;
486 : :
487 [ # # # ]: 0 : switch (tls_xfrm->ver) {
488 : : case RTE_SECURITY_VERSION_TLS_1_2:
489 : : case RTE_SECURITY_VERSION_TLS_1_3:
490 : : tls_hdr_len = 5;
491 : : break;
492 : 0 : case RTE_SECURITY_VERSION_DTLS_1_2:
493 : : tls_hdr_len = 13;
494 : 0 : break;
495 : 0 : default:
496 : : tls_hdr_len = 0;
497 : 0 : break;
498 : : }
499 : :
500 : : /* Get Cipher and Auth algo */
501 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD)
502 : 0 : return tls_hdr_len + ROC_CPT_AES_GCM_IV_LEN + ROC_CPT_AES_GCM_MAC_LEN;
503 : :
504 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
505 : 0 : c_algo = crypto_xfrm->cipher.algo;
506 [ # # ]: 0 : if (crypto_xfrm->next)
507 : 0 : a_algo = crypto_xfrm->next->auth.algo;
508 : : } else {
509 : 0 : a_algo = crypto_xfrm->auth.algo;
510 [ # # ]: 0 : if (crypto_xfrm->next)
511 : 0 : c_algo = crypto_xfrm->next->cipher.algo;
512 : : }
513 : :
514 [ # # # # ]: 0 : switch (c_algo) {
515 : : case RTE_CRYPTO_CIPHER_NULL:
516 : : roundup_byte = 4;
517 : : iv_len = 0;
518 : : break;
519 : 0 : case RTE_CRYPTO_CIPHER_3DES_CBC:
520 : : roundup_byte = ROC_CPT_DES_BLOCK_LENGTH;
521 : : iv_len = ROC_CPT_DES_IV_LEN;
522 : 0 : break;
523 : 0 : case RTE_CRYPTO_CIPHER_AES_CBC:
524 : : roundup_byte = ROC_CPT_AES_BLOCK_LENGTH;
525 : : iv_len = ROC_CPT_AES_CBC_IV_LEN;
526 : 0 : break;
527 : 0 : default:
528 : : roundup_byte = 0;
529 : : iv_len = 0;
530 : 0 : break;
531 : : }
532 : :
533 : : switch (a_algo) {
534 : : case RTE_CRYPTO_AUTH_NULL:
535 : : mac_len = 0;
536 : : break;
537 : : case RTE_CRYPTO_AUTH_MD5_HMAC:
538 : : mac_len = 16;
539 : : break;
540 : : case RTE_CRYPTO_AUTH_SHA1_HMAC:
541 : : mac_len = 20;
542 : : break;
543 : : case RTE_CRYPTO_AUTH_SHA256_HMAC:
544 : : mac_len = 32;
545 : : break;
546 : : case RTE_CRYPTO_AUTH_SHA384_HMAC:
547 : : mac_len = 32;
548 : : break;
549 : : default:
550 : : mac_len = 0;
551 : : break;
552 : : }
553 : :
554 : 0 : return tls_hdr_len + iv_len + mac_len + roundup_byte;
555 : : }
556 : :
557 : : static int
558 : 0 : tls_write_sa_fill(struct roc_ie_ow_tls_write_sa *write_sa,
559 : : struct rte_security_tls_record_xform *tls_xfrm,
560 : : struct rte_crypto_sym_xform *crypto_xfrm)
561 : : {
562 : 0 : enum rte_security_tls_version tls_ver = tls_xfrm->ver;
563 : : struct rte_crypto_sym_xform *auth_xfrm, *cipher_xfrm;
564 : : const uint8_t *key = NULL;
565 : : uint8_t *cipher_key;
566 : : uint64_t *tmp_key;
567 : : int i, length = 0;
568 : : size_t offset;
569 : :
570 [ # # ]: 0 : if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2) {
571 : 0 : write_sa->w2.s.version_select = ROC_IE_OW_TLS_VERSION_TLS_12;
572 : 0 : write_sa->tls_12.seq_num = tls_xfrm->tls_1_2.seq_no - 1;
573 [ # # ]: 0 : } else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2) {
574 : 0 : write_sa->w2.s.version_select = ROC_IE_OW_TLS_VERSION_DTLS_12;
575 : 0 : write_sa->tls_12.seq_num = ((uint64_t)tls_xfrm->dtls_1_2.epoch << 48) |
576 : 0 : (tls_xfrm->dtls_1_2.seq_no & 0x0000ffffffffffff);
577 : 0 : write_sa->tls_12.seq_num -= 1;
578 [ # # ]: 0 : } else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
579 : 0 : write_sa->w2.s.version_select = ROC_IE_OW_TLS_VERSION_TLS_13;
580 : 0 : write_sa->tls_13.seq_num = tls_xfrm->tls_1_3.seq_no - 1;
581 : : }
582 : :
583 : 0 : cipher_key = write_sa->cipher_key;
584 : :
585 : : /* Set encryption algorithm */
586 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
587 : 0 : length = crypto_xfrm->aead.key.length;
588 [ # # ]: 0 : if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_AES_GCM) {
589 [ # # ]: 0 : if (length != 16 && length != 32)
590 : : return -EINVAL;
591 : 0 : write_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_AES_GCM;
592 [ # # ]: 0 : if (length == 16)
593 : 0 : write_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_128;
594 : : else
595 : 0 : write_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_256;
596 [ # # ]: 0 : } else if (crypto_xfrm->aead.algo == RTE_CRYPTO_AEAD_CHACHA20_POLY1305) {
597 [ # # ]: 0 : if (length != 32)
598 : : return -EINVAL;
599 : 0 : write_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_CHACHA_POLY;
600 : 0 : write_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_256;
601 : : } else {
602 : : return -EINVAL;
603 : : }
604 : :
605 : 0 : key = crypto_xfrm->aead.key.data;
606 [ # # ]: 0 : if (key == NULL)
607 : : return -EINVAL;
608 [ # # ]: 0 : memcpy(cipher_key, key, length);
609 : :
610 [ # # ]: 0 : if (tls_ver == RTE_SECURITY_VERSION_TLS_1_2)
611 : 0 : memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_2.imp_nonce, 4);
612 [ # # ]: 0 : else if (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)
613 : 0 : memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->dtls_1_2.imp_nonce, 4);
614 [ # # ]: 0 : else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3)
615 : 0 : memcpy(((uint8_t *)cipher_key + 32), &tls_xfrm->tls_1_3.imp_nonce, 12);
616 : :
617 : 0 : goto key_swap;
618 : : }
619 : :
620 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
621 : : auth_xfrm = crypto_xfrm;
622 : 0 : cipher_xfrm = crypto_xfrm->next;
623 : : } else {
624 : : cipher_xfrm = crypto_xfrm;
625 : 0 : auth_xfrm = crypto_xfrm->next;
626 : : }
627 : :
628 [ # # ]: 0 : if (cipher_xfrm != NULL) {
629 [ # # ]: 0 : if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_3DES_CBC) {
630 : 0 : write_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_3DES;
631 : 0 : length = cipher_xfrm->cipher.key.length;
632 [ # # ]: 0 : if (length != 24)
633 : : return -EINVAL;
634 [ # # ]: 0 : } else if (cipher_xfrm->cipher.algo == RTE_CRYPTO_CIPHER_AES_CBC) {
635 : 0 : write_sa->w2.s.cipher_select = ROC_IE_OW_TLS_CIPHER_AES_CBC;
636 : 0 : length = cipher_xfrm->cipher.key.length;
637 [ # # ]: 0 : if (length == 16)
638 : 0 : write_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_128;
639 [ # # ]: 0 : else if (length == 32)
640 : 0 : write_sa->w2.s.aes_key_len = ROC_IE_OW_TLS_AES_KEY_LEN_256;
641 : : else
642 : : return -EINVAL;
643 : : } else {
644 : : return -EINVAL;
645 : : }
646 : :
647 : 0 : key = cipher_xfrm->cipher.key.data;
648 [ # # ]: 0 : if (key == NULL)
649 : : return -EINVAL;
650 : 0 : memcpy(cipher_key, key, length);
651 : : }
652 : :
653 [ # # ]: 0 : if (auth_xfrm != NULL) {
654 [ # # ]: 0 : if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_MD5_HMAC)
655 : 0 : write_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_MD5;
656 [ # # ]: 0 : else if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_SHA1_HMAC)
657 : 0 : write_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_SHA1;
658 [ # # ]: 0 : else if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_SHA256_HMAC)
659 : 0 : write_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_SHA2_256;
660 [ # # ]: 0 : else if (auth_xfrm->auth.algo == RTE_CRYPTO_AUTH_SHA384_HMAC)
661 : 0 : write_sa->w2.s.mac_select = ROC_IE_OW_TLS_MAC_SHA2_384;
662 : : else
663 : : return -EINVAL;
664 : :
665 : 0 : roc_se_hmac_opad_ipad_gen(write_sa->w2.s.mac_select, auth_xfrm->auth.key.data,
666 : 0 : auth_xfrm->auth.key.length, write_sa->tls_12.opad_ipad,
667 : : ROC_SE_TLS);
668 : : }
669 : :
670 : 0 : tmp_key = (uint64_t *)write_sa->tls_12.opad_ipad;
671 [ # # ]: 0 : for (i = 0; i < (int)(ROC_CTX_MAX_OPAD_IPAD_LEN / sizeof(uint64_t)); i++)
672 [ # # ]: 0 : tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
673 : :
674 : 0 : key_swap:
675 : : tmp_key = (uint64_t *)cipher_key;
676 [ # # ]: 0 : for (i = 0; i < (int)(ROC_IE_OW_TLS_CTX_MAX_KEY_IV_LEN / sizeof(uint64_t)); i++)
677 [ # # ]: 0 : tmp_key[i] = rte_be_to_cpu_64(tmp_key[i]);
678 : :
679 : 0 : write_sa->w0.s.ctx_hdr_size = ROC_IE_OW_TLS_CTX_HDR_SIZE;
680 : : /* Entire context size in 128B units */
681 : 0 : write_sa->w0.s.ctx_size =
682 : : (PLT_ALIGN_CEIL(sizeof(struct roc_ie_ow_tls_write_sa), ROC_CTX_UNIT_128B) /
683 : : ROC_CTX_UNIT_128B) -
684 : : 1;
685 : : offset = offsetof(struct roc_ie_ow_tls_write_sa, tls_12.w26_rsvd7);
686 : :
687 [ # # ]: 0 : if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
688 : : offset = offsetof(struct roc_ie_ow_tls_write_sa, tls_13.w10_rsvd7);
689 : 0 : write_sa->w0.s.ctx_size -= 1;
690 : : }
691 : :
692 : : /* Word offset for HW managed CTX field */
693 : 0 : write_sa->w0.s.hw_ctx_off = offset / 8;
694 : 0 : write_sa->w0.s.ctx_push_size = write_sa->w0.s.hw_ctx_off;
695 : :
696 : 0 : write_sa->w0.s.aop_valid = 1;
697 : :
698 : 0 : write_sa->w2.s.iv_at_cptr = ROC_IE_OW_TLS_IV_SRC_DEFAULT;
699 : :
700 [ # # ]: 0 : if (write_sa->w2.s.version_select != ROC_IE_OW_TLS_VERSION_TLS_13) {
701 : : #ifdef LA_IPSEC_DEBUG
702 : : if (tls_xfrm->options.iv_gen_disable == 1)
703 : : write_sa->w2.s.iv_at_cptr = ROC_IE_OW_TLS_IV_SRC_FROM_SA;
704 : : #else
705 [ # # ]: 0 : if (tls_xfrm->options.iv_gen_disable) {
706 : 0 : plt_err("Application provided IV is not supported");
707 : 0 : return -ENOTSUP;
708 : : }
709 : : #endif
710 : : }
711 : :
712 : : rte_wmb();
713 : :
714 : 0 : return 0;
715 : : }
716 : :
717 : : static int
718 : 0 : cn20k_tls_write_sa_create(struct roc_cpt *roc_cpt, struct roc_cpt_lf *lf,
719 : : struct rte_security_tls_record_xform *tls_xfrm,
720 : : struct rte_crypto_sym_xform *crypto_xfrm,
721 : : struct cn20k_sec_session *sec_sess)
722 : : {
723 : : struct roc_ie_ow_tls_write_sa *sa_dptr;
724 : 0 : uint8_t tls_ver = tls_xfrm->ver;
725 : : struct cn20k_tls_record *tls;
726 : : union cpt_inst_w4 inst_w4;
727 : : void *write_sa;
728 : : int ret = 0;
729 : :
730 : : tls = &sec_sess->tls_rec;
731 : :
732 : 0 : write_sa = rte_zmalloc("cn20k_tls", sizeof(struct roc_ie_ow_tls_write_sa), ROC_CPTR_ALIGN);
733 [ # # ]: 0 : if (write_sa == NULL) {
734 : 0 : plt_err("Couldn't allocate memory for WRITE SA");
735 : 0 : return -ENOMEM;
736 : : }
737 : :
738 : : /* Allocate memory to be used as dptr for CPT ucode WRITE_SA op */
739 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ow_tls_write_sa), 8);
740 [ # # ]: 0 : if (sa_dptr == NULL) {
741 : 0 : plt_err("Could not allocate memory for SA dptr");
742 : : ret = -ENOMEM;
743 : 0 : goto sa_cptr_free;
744 : : }
745 : :
746 : : /* Translate security parameters to SA */
747 : 0 : ret = tls_write_sa_fill(sa_dptr, tls_xfrm, crypto_xfrm);
748 [ # # ]: 0 : if (ret) {
749 : 0 : plt_err("Could not fill write session parameters");
750 : 0 : goto sa_dptr_free;
751 : : }
752 : :
753 [ # # ]: 0 : if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_AEAD) {
754 : 0 : sec_sess->iv_offset = crypto_xfrm->aead.iv.offset;
755 : 0 : sec_sess->iv_length = crypto_xfrm->aead.iv.length;
756 [ # # ]: 0 : } else if (crypto_xfrm->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
757 : 0 : sec_sess->iv_offset = crypto_xfrm->cipher.iv.offset;
758 : 0 : sec_sess->iv_length = crypto_xfrm->cipher.iv.length;
759 : : } else {
760 : 0 : sec_sess->iv_offset = crypto_xfrm->next->cipher.iv.offset;
761 : 0 : sec_sess->iv_length = crypto_xfrm->next->cipher.iv.length;
762 : : }
763 : :
764 : 0 : sec_sess->tls_opt.is_write = 1;
765 : 0 : sec_sess->tls_opt.pad_shift = 0;
766 : 0 : sec_sess->tls_opt.tls_ver = tls_ver;
767 : 0 : sec_sess->tls_opt.enable_padding = tls_xfrm->options.extra_padding_enable;
768 : 0 : sec_sess->max_extended_len = tls_write_rlens_get(tls_xfrm, crypto_xfrm);
769 : 0 : sec_sess->proto = RTE_SECURITY_PROTOCOL_TLS_RECORD;
770 : :
771 : : /* pre-populate CPT INST word 4 */
772 : 0 : inst_w4.u64 = 0;
773 : 0 : if ((tls_ver == RTE_SECURITY_VERSION_TLS_1_2) ||
774 [ # # ]: 0 : (tls_ver == RTE_SECURITY_VERSION_DTLS_1_2)) {
775 : 0 : inst_w4.s.opcode_major = ROC_IE_OW_TLS_MAJOR_OP_RECORD_ENC | ROC_IE_OW_INPLACE_BIT;
776 [ # # ]: 0 : if (sa_dptr->w2.s.cipher_select == ROC_IE_OW_TLS_CIPHER_3DES)
777 : 0 : sec_sess->tls_opt.pad_shift = 3;
778 : : else
779 : 0 : sec_sess->tls_opt.pad_shift = 4;
780 [ # # ]: 0 : } else if (tls_ver == RTE_SECURITY_VERSION_TLS_1_3) {
781 : 0 : inst_w4.s.opcode_major =
782 : : ROC_IE_OW_TLS13_MAJOR_OP_RECORD_ENC | ROC_IE_OW_INPLACE_BIT;
783 : : }
784 [ # # ]: 0 : sec_sess->inst.w4 = inst_w4.u64;
785 : 0 : sec_sess->inst.w7 = cnxk_cpt_sec_inst_w7_get(roc_cpt, write_sa);
786 : :
787 : : memset(write_sa, 0, sizeof(struct roc_ie_ow_tls_write_sa));
788 : :
789 : : /* Copy word0 from sa_dptr to populate ctx_push_sz ctx_size fields */
790 : : memcpy(write_sa, sa_dptr, 8);
791 : :
792 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
793 : :
794 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, write_sa, sizeof(struct roc_ie_ow_tls_write_sa));
795 [ # # ]: 0 : if (ret) {
796 : 0 : plt_err("Could not write tls write session to hardware");
797 : 0 : goto sa_dptr_free;
798 : : }
799 : :
800 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
801 : :
802 [ # # ]: 0 : if (tls->write_sa != NULL)
803 : 0 : rte_free(tls->write_sa);
804 : 0 : tls->write_sa = write_sa;
805 : 0 : sa_dptr_free:
806 : 0 : plt_free(sa_dptr);
807 : : sa_cptr_free:
808 [ # # ]: 0 : if (ret != 0) {
809 : 0 : rte_free(write_sa);
810 : : write_sa = NULL;
811 : : }
812 : :
813 : : return ret;
814 : : }
815 : :
816 : : static void
817 : : tls_write_sa_init(struct roc_ie_ow_tls_write_sa *sa)
818 : : {
819 : : size_t offset;
820 : :
821 : : memset(sa, 0, sizeof(struct roc_ie_ow_tls_write_sa));
822 : :
823 : : offset = offsetof(struct roc_ie_ow_tls_write_sa, tls_12.w26_rsvd7);
824 : 0 : sa->w0.s.hw_ctx_off = offset / ROC_CTX_UNIT_8B;
825 : 0 : sa->w0.s.ctx_push_size = sa->w0.s.hw_ctx_off;
826 : 0 : sa->w0.s.ctx_size = ROC_IE_OW_TLS_CTX_ILEN;
827 : 0 : sa->w0.s.ctx_hdr_size = ROC_IE_OW_TLS_CTX_HDR_SIZE;
828 : 0 : sa->w0.s.aop_valid = 1;
829 : : }
830 : :
831 : : static void
832 : : tls_read_sa_init(struct roc_ie_ow_tls_read_sa *sa)
833 : : {
834 : : size_t offset;
835 : :
836 : : memset(sa, 0, sizeof(struct roc_ie_ow_tls_read_sa));
837 : :
838 : : offset = offsetof(struct roc_ie_ow_tls_read_sa, tls_12.ctx);
839 : 0 : sa->w0.s.hw_ctx_off = offset / ROC_CTX_UNIT_8B;
840 : 0 : sa->w0.s.ctx_push_size = sa->w0.s.hw_ctx_off;
841 : 0 : sa->w0.s.ctx_size = ROC_IE_OW_TLS_CTX_ILEN;
842 : 0 : sa->w0.s.ctx_hdr_size = ROC_IE_OW_TLS_CTX_HDR_SIZE;
843 : 0 : sa->w0.s.aop_valid = 1;
844 : : }
845 : :
846 : : int
847 : 0 : cn20k_tls_record_session_update(struct cnxk_cpt_vf *vf, struct cnxk_cpt_qp *qp,
848 : : struct cn20k_sec_session *sess,
849 : : struct rte_security_session_conf *conf)
850 : : {
851 : : struct roc_cpt *roc_cpt;
852 : : int ret;
853 : :
854 [ # # ]: 0 : if (conf->tls_record.type == RTE_SECURITY_TLS_SESS_TYPE_READ)
855 : : return -ENOTSUP;
856 : :
857 : 0 : ret = cnxk_tls_xform_verify(&conf->tls_record, conf->crypto_xform);
858 [ # # ]: 0 : if (ret)
859 : : return ret;
860 : :
861 : 0 : roc_cpt = &vf->cpt;
862 : 0 : ret = cn20k_tls_write_sa_create(roc_cpt, &qp->lf, &conf->tls_record, conf->crypto_xform,
863 : : (struct cn20k_sec_session *)sess);
864 : :
865 : 0 : return ret;
866 : : }
867 : :
868 : : int
869 : 0 : cn20k_tls_record_session_create(struct cnxk_cpt_vf *vf, struct cnxk_cpt_qp *qp,
870 : : struct rte_security_tls_record_xform *tls_xfrm,
871 : : struct rte_crypto_sym_xform *crypto_xfrm,
872 : : struct rte_security_session *sess)
873 : : {
874 : : struct roc_cpt *roc_cpt;
875 : : int ret;
876 : :
877 : 0 : ret = cnxk_tls_xform_verify(tls_xfrm, crypto_xfrm);
878 [ # # ]: 0 : if (ret)
879 : : return ret;
880 : :
881 : 0 : roc_cpt = &vf->cpt;
882 : :
883 [ # # ]: 0 : if (tls_xfrm->type == RTE_SECURITY_TLS_SESS_TYPE_READ)
884 : 0 : return cn20k_tls_read_sa_create(roc_cpt, &qp->lf, tls_xfrm, crypto_xfrm,
885 : : (struct cn20k_sec_session *)sess);
886 : : else
887 : 0 : return cn20k_tls_write_sa_create(roc_cpt, &qp->lf, tls_xfrm, crypto_xfrm,
888 : : (struct cn20k_sec_session *)sess);
889 : : }
890 : :
891 : : int
892 : 0 : cn20k_sec_tls_session_destroy(struct cnxk_cpt_qp *qp, struct cn20k_sec_session *sess)
893 : : {
894 : : struct cn20k_tls_record *tls;
895 : : struct roc_cpt_lf *lf;
896 : : void *sa_dptr = NULL;
897 : : int ret = -ENOMEM;
898 : :
899 : 0 : lf = &qp->lf;
900 : :
901 : : tls = &sess->tls_rec;
902 : :
903 [ # # ]: 0 : if (tls->sa_ptr == NULL)
904 : : return -EINVAL;
905 : :
906 [ # # ]: 0 : if (sess->tls_opt.is_write) {
907 : 0 : sa_dptr = plt_zmalloc(sizeof(struct roc_ie_ow_tls_write_sa), 8);
908 [ # # ]: 0 : if (sa_dptr != NULL) {
909 : : tls_write_sa_init(sa_dptr);
910 : :
911 : 0 : ret = roc_cpt_ctx_write(lf, sa_dptr, tls->write_sa,
912 : : sizeof(struct roc_ie_ow_tls_write_sa));
913 : 0 : plt_free(sa_dptr);
914 : : }
915 [ # # ]: 0 : if (ret) {
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 : : rte_atomic_thread_fence(rte_memory_order_seq_cst);
932 : :
933 : : /* Trigger CTX reload to fetch new data from DRAM */
934 : 0 : roc_cpt_lf_ctx_reload(lf, tls->read_sa);
935 : : }
936 : : }
937 : :
938 : 0 : rte_free(tls->sa_ptr);
939 : :
940 : 0 : return 0;
941 : : }
|