1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 Intel Corporation
3 */
4
5 #include <rte_common.h>
6 #include <rte_hexdump.h>
7 #include <rte_cryptodev.h>
8 #include <cryptodev_pmd.h>
9 #include <rte_bus_vdev.h>
10 #include <rte_malloc.h>
11 #include <rte_cpuflags.h>
12
13 #include <openssl/hmac.h>
14 #include <openssl/evp.h>
15
16 #include "openssl_pmd_private.h"
17 #include "compat.h"
18
19 #define DES_BLOCK_SIZE 8
20
21 static uint8_t cryptodev_driver_id;
22
23 #if (OPENSSL_VERSION_NUMBER < 0x10100000L)
HMAC_CTX_new(void)24 static HMAC_CTX *HMAC_CTX_new(void)
25 {
26 HMAC_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
27
28 if (ctx != NULL)
29 HMAC_CTX_init(ctx);
30 return ctx;
31 }
32
HMAC_CTX_free(HMAC_CTX * ctx)33 static void HMAC_CTX_free(HMAC_CTX *ctx)
34 {
35 if (ctx != NULL) {
36 HMAC_CTX_cleanup(ctx);
37 OPENSSL_free(ctx);
38 }
39 }
40 #endif
41
42 static int cryptodev_openssl_remove(struct rte_vdev_device *vdev);
43
44 /*----------------------------------------------------------------------------*/
45
46 /**
47 * Increment counter by 1
48 * Counter is 64 bit array, big-endian
49 */
50 static void
ctr_inc(uint8_t * ctr)51 ctr_inc(uint8_t *ctr)
52 {
53 uint64_t *ctr64 = (uint64_t *)ctr;
54
55 *ctr64 = __builtin_bswap64(*ctr64);
56 (*ctr64)++;
57 *ctr64 = __builtin_bswap64(*ctr64);
58 }
59
60 /*
61 *------------------------------------------------------------------------------
62 * Session Prepare
63 *------------------------------------------------------------------------------
64 */
65
66 /** Get xform chain order */
67 static enum openssl_chain_order
openssl_get_chain_order(const struct rte_crypto_sym_xform * xform)68 openssl_get_chain_order(const struct rte_crypto_sym_xform *xform)
69 {
70 enum openssl_chain_order res = OPENSSL_CHAIN_NOT_SUPPORTED;
71
72 if (xform != NULL) {
73 if (xform->type == RTE_CRYPTO_SYM_XFORM_AUTH) {
74 if (xform->next == NULL)
75 res = OPENSSL_CHAIN_ONLY_AUTH;
76 else if (xform->next->type ==
77 RTE_CRYPTO_SYM_XFORM_CIPHER)
78 res = OPENSSL_CHAIN_AUTH_CIPHER;
79 }
80 if (xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
81 if (xform->next == NULL)
82 res = OPENSSL_CHAIN_ONLY_CIPHER;
83 else if (xform->next->type == RTE_CRYPTO_SYM_XFORM_AUTH)
84 res = OPENSSL_CHAIN_CIPHER_AUTH;
85 }
86 if (xform->type == RTE_CRYPTO_SYM_XFORM_AEAD)
87 res = OPENSSL_CHAIN_COMBINED;
88 }
89
90 return res;
91 }
92
93 /** Get session cipher key from input cipher key */
94 static void
get_cipher_key(const uint8_t * input_key,int keylen,uint8_t * session_key)95 get_cipher_key(const uint8_t *input_key, int keylen, uint8_t *session_key)
96 {
97 memcpy(session_key, input_key, keylen);
98 }
99
100 /** Get key ede 24 bytes standard from input key */
101 static int
get_cipher_key_ede(const uint8_t * key,int keylen,uint8_t * key_ede)102 get_cipher_key_ede(const uint8_t *key, int keylen, uint8_t *key_ede)
103 {
104 int res = 0;
105
106 /* Initialize keys - 24 bytes: [key1-key2-key3] */
107 switch (keylen) {
108 case 24:
109 memcpy(key_ede, key, 24);
110 break;
111 case 16:
112 /* K3 = K1 */
113 memcpy(key_ede, key, 16);
114 memcpy(key_ede + 16, key, 8);
115 break;
116 case 8:
117 /* K1 = K2 = K3 (DES compatibility) */
118 memcpy(key_ede, key, 8);
119 memcpy(key_ede + 8, key, 8);
120 memcpy(key_ede + 16, key, 8);
121 break;
122 default:
123 OPENSSL_LOG(ERR, "Unsupported key size");
124 res = -EINVAL;
125 }
126
127 return res;
128 }
129
130 /** Get adequate openssl function for input cipher algorithm */
131 static uint8_t
get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo,size_t keylen,const EVP_CIPHER ** algo)132 get_cipher_algo(enum rte_crypto_cipher_algorithm sess_algo, size_t keylen,
133 const EVP_CIPHER **algo)
134 {
135 int res = 0;
136
137 if (algo != NULL) {
138 switch (sess_algo) {
139 case RTE_CRYPTO_CIPHER_3DES_CBC:
140 switch (keylen) {
141 case 8:
142 *algo = EVP_des_cbc();
143 break;
144 case 16:
145 *algo = EVP_des_ede_cbc();
146 break;
147 case 24:
148 *algo = EVP_des_ede3_cbc();
149 break;
150 default:
151 res = -EINVAL;
152 }
153 break;
154 case RTE_CRYPTO_CIPHER_3DES_CTR:
155 break;
156 case RTE_CRYPTO_CIPHER_AES_CBC:
157 switch (keylen) {
158 case 16:
159 *algo = EVP_aes_128_cbc();
160 break;
161 case 24:
162 *algo = EVP_aes_192_cbc();
163 break;
164 case 32:
165 *algo = EVP_aes_256_cbc();
166 break;
167 default:
168 res = -EINVAL;
169 }
170 break;
171 case RTE_CRYPTO_CIPHER_AES_CTR:
172 switch (keylen) {
173 case 16:
174 *algo = EVP_aes_128_ctr();
175 break;
176 case 24:
177 *algo = EVP_aes_192_ctr();
178 break;
179 case 32:
180 *algo = EVP_aes_256_ctr();
181 break;
182 default:
183 res = -EINVAL;
184 }
185 break;
186 default:
187 res = -EINVAL;
188 break;
189 }
190 } else {
191 res = -EINVAL;
192 }
193
194 return res;
195 }
196
197 /** Get adequate openssl function for input auth algorithm */
198 static uint8_t
get_auth_algo(enum rte_crypto_auth_algorithm sessalgo,const EVP_MD ** algo)199 get_auth_algo(enum rte_crypto_auth_algorithm sessalgo,
200 const EVP_MD **algo)
201 {
202 int res = 0;
203
204 if (algo != NULL) {
205 switch (sessalgo) {
206 case RTE_CRYPTO_AUTH_MD5:
207 case RTE_CRYPTO_AUTH_MD5_HMAC:
208 *algo = EVP_md5();
209 break;
210 case RTE_CRYPTO_AUTH_SHA1:
211 case RTE_CRYPTO_AUTH_SHA1_HMAC:
212 *algo = EVP_sha1();
213 break;
214 case RTE_CRYPTO_AUTH_SHA224:
215 case RTE_CRYPTO_AUTH_SHA224_HMAC:
216 *algo = EVP_sha224();
217 break;
218 case RTE_CRYPTO_AUTH_SHA256:
219 case RTE_CRYPTO_AUTH_SHA256_HMAC:
220 *algo = EVP_sha256();
221 break;
222 case RTE_CRYPTO_AUTH_SHA384:
223 case RTE_CRYPTO_AUTH_SHA384_HMAC:
224 *algo = EVP_sha384();
225 break;
226 case RTE_CRYPTO_AUTH_SHA512:
227 case RTE_CRYPTO_AUTH_SHA512_HMAC:
228 *algo = EVP_sha512();
229 break;
230 default:
231 res = -EINVAL;
232 break;
233 }
234 } else {
235 res = -EINVAL;
236 }
237
238 return res;
239 }
240
241 /** Get adequate openssl function for input cipher algorithm */
242 static uint8_t
get_aead_algo(enum rte_crypto_aead_algorithm sess_algo,size_t keylen,const EVP_CIPHER ** algo)243 get_aead_algo(enum rte_crypto_aead_algorithm sess_algo, size_t keylen,
244 const EVP_CIPHER **algo)
245 {
246 int res = 0;
247
248 if (algo != NULL) {
249 switch (sess_algo) {
250 case RTE_CRYPTO_AEAD_AES_GCM:
251 switch (keylen) {
252 case 16:
253 *algo = EVP_aes_128_gcm();
254 break;
255 case 24:
256 *algo = EVP_aes_192_gcm();
257 break;
258 case 32:
259 *algo = EVP_aes_256_gcm();
260 break;
261 default:
262 res = -EINVAL;
263 }
264 break;
265 case RTE_CRYPTO_AEAD_AES_CCM:
266 switch (keylen) {
267 case 16:
268 *algo = EVP_aes_128_ccm();
269 break;
270 case 24:
271 *algo = EVP_aes_192_ccm();
272 break;
273 case 32:
274 *algo = EVP_aes_256_ccm();
275 break;
276 default:
277 res = -EINVAL;
278 }
279 break;
280 default:
281 res = -EINVAL;
282 break;
283 }
284 } else {
285 res = -EINVAL;
286 }
287
288 return res;
289 }
290
291 /* Set session AEAD encryption parameters */
292 static int
openssl_set_sess_aead_enc_param(struct openssl_session * sess,enum rte_crypto_aead_algorithm algo,uint8_t tag_len,const uint8_t * key)293 openssl_set_sess_aead_enc_param(struct openssl_session *sess,
294 enum rte_crypto_aead_algorithm algo,
295 uint8_t tag_len, const uint8_t *key)
296 {
297 int iv_type = 0;
298 unsigned int do_ccm;
299
300 sess->cipher.direction = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
301 sess->auth.operation = RTE_CRYPTO_AUTH_OP_GENERATE;
302
303 /* Select AEAD algo */
304 switch (algo) {
305 case RTE_CRYPTO_AEAD_AES_GCM:
306 iv_type = EVP_CTRL_GCM_SET_IVLEN;
307 if (tag_len != 16)
308 return -EINVAL;
309 do_ccm = 0;
310 break;
311 case RTE_CRYPTO_AEAD_AES_CCM:
312 iv_type = EVP_CTRL_CCM_SET_IVLEN;
313 /* Digest size can be 4, 6, 8, 10, 12, 14 or 16 bytes */
314 if (tag_len < 4 || tag_len > 16 || (tag_len & 1) == 1)
315 return -EINVAL;
316 do_ccm = 1;
317 break;
318 default:
319 return -ENOTSUP;
320 }
321
322 sess->cipher.mode = OPENSSL_CIPHER_LIB;
323 sess->cipher.ctx = EVP_CIPHER_CTX_new();
324
325 if (get_aead_algo(algo, sess->cipher.key.length,
326 &sess->cipher.evp_algo) != 0)
327 return -EINVAL;
328
329 get_cipher_key(key, sess->cipher.key.length, sess->cipher.key.data);
330
331 sess->chain_order = OPENSSL_CHAIN_COMBINED;
332
333 if (EVP_EncryptInit_ex(sess->cipher.ctx, sess->cipher.evp_algo,
334 NULL, NULL, NULL) <= 0)
335 return -EINVAL;
336
337 if (EVP_CIPHER_CTX_ctrl(sess->cipher.ctx, iv_type, sess->iv.length,
338 NULL) <= 0)
339 return -EINVAL;
340
341 if (do_ccm)
342 EVP_CIPHER_CTX_ctrl(sess->cipher.ctx, EVP_CTRL_CCM_SET_TAG,
343 tag_len, NULL);
344
345 if (EVP_EncryptInit_ex(sess->cipher.ctx, NULL, NULL, key, NULL) <= 0)
346 return -EINVAL;
347
348 return 0;
349 }
350
351 /* Set session AEAD decryption parameters */
352 static int
openssl_set_sess_aead_dec_param(struct openssl_session * sess,enum rte_crypto_aead_algorithm algo,uint8_t tag_len,const uint8_t * key)353 openssl_set_sess_aead_dec_param(struct openssl_session *sess,
354 enum rte_crypto_aead_algorithm algo,
355 uint8_t tag_len, const uint8_t *key)
356 {
357 int iv_type = 0;
358 unsigned int do_ccm = 0;
359
360 sess->cipher.direction = RTE_CRYPTO_CIPHER_OP_DECRYPT;
361 sess->auth.operation = RTE_CRYPTO_AUTH_OP_VERIFY;
362
363 /* Select AEAD algo */
364 switch (algo) {
365 case RTE_CRYPTO_AEAD_AES_GCM:
366 iv_type = EVP_CTRL_GCM_SET_IVLEN;
367 if (tag_len != 16)
368 return -EINVAL;
369 break;
370 case RTE_CRYPTO_AEAD_AES_CCM:
371 iv_type = EVP_CTRL_CCM_SET_IVLEN;
372 /* Digest size can be 4, 6, 8, 10, 12, 14 or 16 bytes */
373 if (tag_len < 4 || tag_len > 16 || (tag_len & 1) == 1)
374 return -EINVAL;
375 do_ccm = 1;
376 break;
377 default:
378 return -ENOTSUP;
379 }
380
381 sess->cipher.mode = OPENSSL_CIPHER_LIB;
382 sess->cipher.ctx = EVP_CIPHER_CTX_new();
383
384 if (get_aead_algo(algo, sess->cipher.key.length,
385 &sess->cipher.evp_algo) != 0)
386 return -EINVAL;
387
388 get_cipher_key(key, sess->cipher.key.length, sess->cipher.key.data);
389
390 sess->chain_order = OPENSSL_CHAIN_COMBINED;
391
392 if (EVP_DecryptInit_ex(sess->cipher.ctx, sess->cipher.evp_algo,
393 NULL, NULL, NULL) <= 0)
394 return -EINVAL;
395
396 if (EVP_CIPHER_CTX_ctrl(sess->cipher.ctx, iv_type,
397 sess->iv.length, NULL) <= 0)
398 return -EINVAL;
399
400 if (do_ccm)
401 EVP_CIPHER_CTX_ctrl(sess->cipher.ctx, EVP_CTRL_CCM_SET_TAG,
402 tag_len, NULL);
403
404 if (EVP_DecryptInit_ex(sess->cipher.ctx, NULL, NULL, key, NULL) <= 0)
405 return -EINVAL;
406
407 return 0;
408 }
409
410 /** Set session cipher parameters */
411 static int
openssl_set_session_cipher_parameters(struct openssl_session * sess,const struct rte_crypto_sym_xform * xform)412 openssl_set_session_cipher_parameters(struct openssl_session *sess,
413 const struct rte_crypto_sym_xform *xform)
414 {
415 /* Select cipher direction */
416 sess->cipher.direction = xform->cipher.op;
417 /* Select cipher key */
418 sess->cipher.key.length = xform->cipher.key.length;
419
420 /* Set IV parameters */
421 sess->iv.offset = xform->cipher.iv.offset;
422 sess->iv.length = xform->cipher.iv.length;
423
424 /* Select cipher algo */
425 switch (xform->cipher.algo) {
426 case RTE_CRYPTO_CIPHER_3DES_CBC:
427 case RTE_CRYPTO_CIPHER_AES_CBC:
428 case RTE_CRYPTO_CIPHER_AES_CTR:
429 sess->cipher.mode = OPENSSL_CIPHER_LIB;
430 sess->cipher.algo = xform->cipher.algo;
431 sess->cipher.ctx = EVP_CIPHER_CTX_new();
432
433 if (get_cipher_algo(sess->cipher.algo, sess->cipher.key.length,
434 &sess->cipher.evp_algo) != 0)
435 return -EINVAL;
436
437 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
438 sess->cipher.key.data);
439 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
440 if (EVP_EncryptInit_ex(sess->cipher.ctx,
441 sess->cipher.evp_algo,
442 NULL, xform->cipher.key.data,
443 NULL) != 1) {
444 return -EINVAL;
445 }
446 } else if (sess->cipher.direction ==
447 RTE_CRYPTO_CIPHER_OP_DECRYPT) {
448 if (EVP_DecryptInit_ex(sess->cipher.ctx,
449 sess->cipher.evp_algo,
450 NULL, xform->cipher.key.data,
451 NULL) != 1) {
452 return -EINVAL;
453 }
454 }
455
456 break;
457
458 case RTE_CRYPTO_CIPHER_3DES_CTR:
459 sess->cipher.mode = OPENSSL_CIPHER_DES3CTR;
460 sess->cipher.ctx = EVP_CIPHER_CTX_new();
461
462 if (get_cipher_key_ede(xform->cipher.key.data,
463 sess->cipher.key.length,
464 sess->cipher.key.data) != 0)
465 return -EINVAL;
466 break;
467
468 case RTE_CRYPTO_CIPHER_DES_CBC:
469 sess->cipher.algo = xform->cipher.algo;
470 sess->cipher.ctx = EVP_CIPHER_CTX_new();
471 sess->cipher.evp_algo = EVP_des_cbc();
472
473 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
474 sess->cipher.key.data);
475 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
476 if (EVP_EncryptInit_ex(sess->cipher.ctx,
477 sess->cipher.evp_algo,
478 NULL, xform->cipher.key.data,
479 NULL) != 1) {
480 return -EINVAL;
481 }
482 } else if (sess->cipher.direction ==
483 RTE_CRYPTO_CIPHER_OP_DECRYPT) {
484 if (EVP_DecryptInit_ex(sess->cipher.ctx,
485 sess->cipher.evp_algo,
486 NULL, xform->cipher.key.data,
487 NULL) != 1) {
488 return -EINVAL;
489 }
490 }
491
492 break;
493
494 case RTE_CRYPTO_CIPHER_DES_DOCSISBPI:
495 sess->cipher.algo = xform->cipher.algo;
496 sess->chain_order = OPENSSL_CHAIN_CIPHER_BPI;
497 sess->cipher.ctx = EVP_CIPHER_CTX_new();
498 sess->cipher.evp_algo = EVP_des_cbc();
499
500 sess->cipher.bpi_ctx = EVP_CIPHER_CTX_new();
501 /* IV will be ECB encrypted whether direction is encrypt or decrypt */
502 if (EVP_EncryptInit_ex(sess->cipher.bpi_ctx, EVP_des_ecb(),
503 NULL, xform->cipher.key.data, 0) != 1)
504 return -EINVAL;
505
506 get_cipher_key(xform->cipher.key.data, sess->cipher.key.length,
507 sess->cipher.key.data);
508 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
509 if (EVP_EncryptInit_ex(sess->cipher.ctx,
510 sess->cipher.evp_algo,
511 NULL, xform->cipher.key.data,
512 NULL) != 1) {
513 return -EINVAL;
514 }
515 } else if (sess->cipher.direction ==
516 RTE_CRYPTO_CIPHER_OP_DECRYPT) {
517 if (EVP_DecryptInit_ex(sess->cipher.ctx,
518 sess->cipher.evp_algo,
519 NULL, xform->cipher.key.data,
520 NULL) != 1) {
521 return -EINVAL;
522 }
523 }
524
525 break;
526 default:
527 sess->cipher.algo = RTE_CRYPTO_CIPHER_NULL;
528 return -ENOTSUP;
529 }
530
531 return 0;
532 }
533
534 /* Set session auth parameters */
535 static int
openssl_set_session_auth_parameters(struct openssl_session * sess,const struct rte_crypto_sym_xform * xform)536 openssl_set_session_auth_parameters(struct openssl_session *sess,
537 const struct rte_crypto_sym_xform *xform)
538 {
539 /* Select auth generate/verify */
540 sess->auth.operation = xform->auth.op;
541 sess->auth.algo = xform->auth.algo;
542
543 sess->auth.digest_length = xform->auth.digest_length;
544
545 /* Select auth algo */
546 switch (xform->auth.algo) {
547 case RTE_CRYPTO_AUTH_AES_GMAC:
548 /*
549 * OpenSSL requires GMAC to be a GCM operation
550 * with no cipher data length
551 */
552 sess->cipher.key.length = xform->auth.key.length;
553
554 /* Set IV parameters */
555 sess->iv.offset = xform->auth.iv.offset;
556 sess->iv.length = xform->auth.iv.length;
557
558 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_GENERATE)
559 return openssl_set_sess_aead_enc_param(sess,
560 RTE_CRYPTO_AEAD_AES_GCM,
561 xform->auth.digest_length,
562 xform->auth.key.data);
563 else
564 return openssl_set_sess_aead_dec_param(sess,
565 RTE_CRYPTO_AEAD_AES_GCM,
566 xform->auth.digest_length,
567 xform->auth.key.data);
568 break;
569
570 case RTE_CRYPTO_AUTH_MD5:
571 case RTE_CRYPTO_AUTH_SHA1:
572 case RTE_CRYPTO_AUTH_SHA224:
573 case RTE_CRYPTO_AUTH_SHA256:
574 case RTE_CRYPTO_AUTH_SHA384:
575 case RTE_CRYPTO_AUTH_SHA512:
576 sess->auth.mode = OPENSSL_AUTH_AS_AUTH;
577 if (get_auth_algo(xform->auth.algo,
578 &sess->auth.auth.evp_algo) != 0)
579 return -EINVAL;
580 sess->auth.auth.ctx = EVP_MD_CTX_create();
581 break;
582
583 case RTE_CRYPTO_AUTH_MD5_HMAC:
584 case RTE_CRYPTO_AUTH_SHA1_HMAC:
585 case RTE_CRYPTO_AUTH_SHA224_HMAC:
586 case RTE_CRYPTO_AUTH_SHA256_HMAC:
587 case RTE_CRYPTO_AUTH_SHA384_HMAC:
588 case RTE_CRYPTO_AUTH_SHA512_HMAC:
589 sess->auth.mode = OPENSSL_AUTH_AS_HMAC;
590 sess->auth.hmac.ctx = HMAC_CTX_new();
591 if (get_auth_algo(xform->auth.algo,
592 &sess->auth.hmac.evp_algo) != 0)
593 return -EINVAL;
594
595 if (HMAC_Init_ex(sess->auth.hmac.ctx,
596 xform->auth.key.data,
597 xform->auth.key.length,
598 sess->auth.hmac.evp_algo, NULL) != 1)
599 return -EINVAL;
600 break;
601
602 default:
603 return -ENOTSUP;
604 }
605
606 return 0;
607 }
608
609 /* Set session AEAD parameters */
610 static int
openssl_set_session_aead_parameters(struct openssl_session * sess,const struct rte_crypto_sym_xform * xform)611 openssl_set_session_aead_parameters(struct openssl_session *sess,
612 const struct rte_crypto_sym_xform *xform)
613 {
614 /* Select cipher key */
615 sess->cipher.key.length = xform->aead.key.length;
616
617 /* Set IV parameters */
618 if (xform->aead.algo == RTE_CRYPTO_AEAD_AES_CCM)
619 /*
620 * For AES-CCM, the actual IV is placed
621 * one byte after the start of the IV field,
622 * according to the API.
623 */
624 sess->iv.offset = xform->aead.iv.offset + 1;
625 else
626 sess->iv.offset = xform->aead.iv.offset;
627
628 sess->iv.length = xform->aead.iv.length;
629
630 sess->auth.aad_length = xform->aead.aad_length;
631 sess->auth.digest_length = xform->aead.digest_length;
632
633 sess->aead_algo = xform->aead.algo;
634 /* Select cipher direction */
635 if (xform->aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
636 return openssl_set_sess_aead_enc_param(sess, xform->aead.algo,
637 xform->aead.digest_length, xform->aead.key.data);
638 else
639 return openssl_set_sess_aead_dec_param(sess, xform->aead.algo,
640 xform->aead.digest_length, xform->aead.key.data);
641 }
642
643 /** Parse crypto xform chain and set private session parameters */
644 int
openssl_set_session_parameters(struct openssl_session * sess,const struct rte_crypto_sym_xform * xform)645 openssl_set_session_parameters(struct openssl_session *sess,
646 const struct rte_crypto_sym_xform *xform)
647 {
648 const struct rte_crypto_sym_xform *cipher_xform = NULL;
649 const struct rte_crypto_sym_xform *auth_xform = NULL;
650 const struct rte_crypto_sym_xform *aead_xform = NULL;
651 int ret;
652
653 sess->chain_order = openssl_get_chain_order(xform);
654 switch (sess->chain_order) {
655 case OPENSSL_CHAIN_ONLY_CIPHER:
656 cipher_xform = xform;
657 break;
658 case OPENSSL_CHAIN_ONLY_AUTH:
659 auth_xform = xform;
660 break;
661 case OPENSSL_CHAIN_CIPHER_AUTH:
662 cipher_xform = xform;
663 auth_xform = xform->next;
664 break;
665 case OPENSSL_CHAIN_AUTH_CIPHER:
666 auth_xform = xform;
667 cipher_xform = xform->next;
668 break;
669 case OPENSSL_CHAIN_COMBINED:
670 aead_xform = xform;
671 break;
672 default:
673 return -EINVAL;
674 }
675
676 /* Default IV length = 0 */
677 sess->iv.length = 0;
678
679 /* cipher_xform must be check before auth_xform */
680 if (cipher_xform) {
681 ret = openssl_set_session_cipher_parameters(
682 sess, cipher_xform);
683 if (ret != 0) {
684 OPENSSL_LOG(ERR,
685 "Invalid/unsupported cipher parameters");
686 return ret;
687 }
688 }
689
690 if (auth_xform) {
691 ret = openssl_set_session_auth_parameters(sess, auth_xform);
692 if (ret != 0) {
693 OPENSSL_LOG(ERR,
694 "Invalid/unsupported auth parameters");
695 return ret;
696 }
697 }
698
699 if (aead_xform) {
700 ret = openssl_set_session_aead_parameters(sess, aead_xform);
701 if (ret != 0) {
702 OPENSSL_LOG(ERR,
703 "Invalid/unsupported AEAD parameters");
704 return ret;
705 }
706 }
707
708 return 0;
709 }
710
711 /** Reset private session parameters */
712 void
openssl_reset_session(struct openssl_session * sess)713 openssl_reset_session(struct openssl_session *sess)
714 {
715 EVP_CIPHER_CTX_free(sess->cipher.ctx);
716
717 if (sess->chain_order == OPENSSL_CHAIN_CIPHER_BPI)
718 EVP_CIPHER_CTX_free(sess->cipher.bpi_ctx);
719
720 switch (sess->auth.mode) {
721 case OPENSSL_AUTH_AS_AUTH:
722 EVP_MD_CTX_destroy(sess->auth.auth.ctx);
723 break;
724 case OPENSSL_AUTH_AS_HMAC:
725 EVP_PKEY_free(sess->auth.hmac.pkey);
726 HMAC_CTX_free(sess->auth.hmac.ctx);
727 break;
728 default:
729 break;
730 }
731 }
732
733 /** Provide session for operation */
734 static void *
get_session(struct openssl_qp * qp,struct rte_crypto_op * op)735 get_session(struct openssl_qp *qp, struct rte_crypto_op *op)
736 {
737 struct openssl_session *sess = NULL;
738 struct openssl_asym_session *asym_sess = NULL;
739
740 if (op->sess_type == RTE_CRYPTO_OP_WITH_SESSION) {
741 if (op->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC) {
742 /* get existing session */
743 if (likely(op->sym->session != NULL))
744 sess = (struct openssl_session *)
745 get_sym_session_private_data(
746 op->sym->session,
747 cryptodev_driver_id);
748 } else {
749 if (likely(op->asym->session != NULL))
750 asym_sess = (struct openssl_asym_session *)
751 op->asym->session->sess_private_data;
752 if (asym_sess == NULL)
753 op->status =
754 RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
755 return asym_sess;
756 }
757 } else {
758 /* sessionless asymmetric not supported */
759 if (op->type == RTE_CRYPTO_OP_TYPE_ASYMMETRIC)
760 return NULL;
761
762 /* provide internal session */
763 void *_sess = rte_cryptodev_sym_session_create(qp->sess_mp);
764 void *_sess_private_data = NULL;
765
766 if (_sess == NULL)
767 return NULL;
768
769 if (rte_mempool_get(qp->sess_mp_priv,
770 (void **)&_sess_private_data))
771 return NULL;
772
773 sess = (struct openssl_session *)_sess_private_data;
774
775 if (unlikely(openssl_set_session_parameters(sess,
776 op->sym->xform) != 0)) {
777 rte_mempool_put(qp->sess_mp, _sess);
778 rte_mempool_put(qp->sess_mp_priv, _sess_private_data);
779 sess = NULL;
780 }
781 op->sym->session = (struct rte_cryptodev_sym_session *)_sess;
782 set_sym_session_private_data(op->sym->session,
783 cryptodev_driver_id, _sess_private_data);
784 }
785
786 if (sess == NULL)
787 op->status = RTE_CRYPTO_OP_STATUS_INVALID_SESSION;
788
789 return sess;
790 }
791
792 /*
793 *------------------------------------------------------------------------------
794 * Process Operations
795 *------------------------------------------------------------------------------
796 */
797 static inline int
process_openssl_encryption_update(struct rte_mbuf * mbuf_src,int offset,uint8_t ** dst,int srclen,EVP_CIPHER_CTX * ctx,uint8_t inplace)798 process_openssl_encryption_update(struct rte_mbuf *mbuf_src, int offset,
799 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx, uint8_t inplace)
800 {
801 struct rte_mbuf *m;
802 int dstlen;
803 int l, n = srclen;
804 uint8_t *src, temp[EVP_CIPHER_CTX_block_size(ctx)];
805
806 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
807 m = m->next)
808 offset -= rte_pktmbuf_data_len(m);
809
810 if (m == 0)
811 return -1;
812
813 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
814 if (inplace)
815 *dst = src;
816
817 l = rte_pktmbuf_data_len(m) - offset;
818 if (srclen <= l) {
819 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
820 return -1;
821 *dst += l;
822 return 0;
823 }
824
825 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
826 return -1;
827
828 *dst += dstlen;
829 n -= l;
830
831 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
832 uint8_t diff = l - dstlen, rem;
833
834 src = rte_pktmbuf_mtod(m, uint8_t *);
835 l = RTE_MIN(rte_pktmbuf_data_len(m), n);
836 if (diff && inplace) {
837 rem = RTE_MIN(l,
838 (EVP_CIPHER_CTX_block_size(ctx) - diff));
839 if (EVP_EncryptUpdate(ctx, temp,
840 &dstlen, src, rem) <= 0)
841 return -1;
842 n -= rem;
843 rte_memcpy(*dst, temp, diff);
844 rte_memcpy(src, temp + diff, rem);
845 src += rem;
846 l -= rem;
847 }
848 if (inplace)
849 *dst = src;
850 if (EVP_EncryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
851 return -1;
852 *dst += dstlen;
853 n -= l;
854 }
855
856 return 0;
857 }
858
859 static inline int
process_openssl_decryption_update(struct rte_mbuf * mbuf_src,int offset,uint8_t ** dst,int srclen,EVP_CIPHER_CTX * ctx,uint8_t inplace)860 process_openssl_decryption_update(struct rte_mbuf *mbuf_src, int offset,
861 uint8_t **dst, int srclen, EVP_CIPHER_CTX *ctx, uint8_t inplace)
862 {
863 struct rte_mbuf *m;
864 int dstlen;
865 int l, n = srclen;
866 uint8_t *src, temp[EVP_CIPHER_CTX_block_size(ctx)];
867
868 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
869 m = m->next)
870 offset -= rte_pktmbuf_data_len(m);
871
872 if (m == 0)
873 return -1;
874
875 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
876 if (inplace)
877 *dst = src;
878
879 l = rte_pktmbuf_data_len(m) - offset;
880 if (srclen <= l) {
881 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, srclen) <= 0)
882 return -1;
883 *dst += l;
884 return 0;
885 }
886
887 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
888 return -1;
889
890 *dst += dstlen;
891 n -= l;
892
893 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
894 uint8_t diff = l - dstlen, rem;
895
896 src = rte_pktmbuf_mtod(m, uint8_t *);
897 l = RTE_MIN(rte_pktmbuf_data_len(m), n);
898 if (diff && inplace) {
899 rem = RTE_MIN(l,
900 (EVP_CIPHER_CTX_block_size(ctx) - diff));
901 if (EVP_DecryptUpdate(ctx, temp,
902 &dstlen, src, rem) <= 0)
903 return -1;
904 n -= rem;
905 rte_memcpy(*dst, temp, diff);
906 rte_memcpy(src, temp + diff, rem);
907 src += rem;
908 l -= rem;
909 }
910 if (inplace)
911 *dst = src;
912 if (EVP_DecryptUpdate(ctx, *dst, &dstlen, src, l) <= 0)
913 return -1;
914 *dst += dstlen;
915 n -= l;
916 }
917
918 return 0;
919 }
920
921 /** Process standard openssl cipher encryption */
922 static int
process_openssl_cipher_encrypt(struct rte_mbuf * mbuf_src,uint8_t * dst,int offset,uint8_t * iv,int srclen,EVP_CIPHER_CTX * ctx,uint8_t inplace)923 process_openssl_cipher_encrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
924 int offset, uint8_t *iv, int srclen, EVP_CIPHER_CTX *ctx,
925 uint8_t inplace)
926 {
927 int totlen;
928
929 if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
930 goto process_cipher_encrypt_err;
931
932 EVP_CIPHER_CTX_set_padding(ctx, 0);
933
934 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
935 srclen, ctx, inplace))
936 goto process_cipher_encrypt_err;
937
938 if (EVP_EncryptFinal_ex(ctx, dst, &totlen) <= 0)
939 goto process_cipher_encrypt_err;
940
941 return 0;
942
943 process_cipher_encrypt_err:
944 OPENSSL_LOG(ERR, "Process openssl cipher encrypt failed");
945 return -EINVAL;
946 }
947
948 /** Process standard openssl cipher encryption */
949 static int
process_openssl_cipher_bpi_encrypt(uint8_t * src,uint8_t * dst,uint8_t * iv,int srclen,EVP_CIPHER_CTX * ctx)950 process_openssl_cipher_bpi_encrypt(uint8_t *src, uint8_t *dst,
951 uint8_t *iv, int srclen,
952 EVP_CIPHER_CTX *ctx)
953 {
954 uint8_t i;
955 uint8_t encrypted_iv[DES_BLOCK_SIZE];
956 int encrypted_ivlen;
957
958 if (EVP_EncryptUpdate(ctx, encrypted_iv, &encrypted_ivlen,
959 iv, DES_BLOCK_SIZE) <= 0)
960 goto process_cipher_encrypt_err;
961
962 for (i = 0; i < srclen; i++)
963 *(dst + i) = *(src + i) ^ (encrypted_iv[i]);
964
965 return 0;
966
967 process_cipher_encrypt_err:
968 OPENSSL_LOG(ERR, "Process openssl cipher bpi encrypt failed");
969 return -EINVAL;
970 }
971 /** Process standard openssl cipher decryption */
972 static int
process_openssl_cipher_decrypt(struct rte_mbuf * mbuf_src,uint8_t * dst,int offset,uint8_t * iv,int srclen,EVP_CIPHER_CTX * ctx,uint8_t inplace)973 process_openssl_cipher_decrypt(struct rte_mbuf *mbuf_src, uint8_t *dst,
974 int offset, uint8_t *iv, int srclen, EVP_CIPHER_CTX *ctx,
975 uint8_t inplace)
976 {
977 int totlen;
978
979 if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
980 goto process_cipher_decrypt_err;
981
982 EVP_CIPHER_CTX_set_padding(ctx, 0);
983
984 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
985 srclen, ctx, inplace))
986 goto process_cipher_decrypt_err;
987
988 if (EVP_DecryptFinal_ex(ctx, dst, &totlen) <= 0)
989 goto process_cipher_decrypt_err;
990 return 0;
991
992 process_cipher_decrypt_err:
993 OPENSSL_LOG(ERR, "Process openssl cipher decrypt failed");
994 return -EINVAL;
995 }
996
997 /** Process cipher des 3 ctr encryption, decryption algorithm */
998 static int
process_openssl_cipher_des3ctr(struct rte_mbuf * mbuf_src,uint8_t * dst,int offset,uint8_t * iv,uint8_t * key,int srclen,EVP_CIPHER_CTX * ctx)999 process_openssl_cipher_des3ctr(struct rte_mbuf *mbuf_src, uint8_t *dst,
1000 int offset, uint8_t *iv, uint8_t *key, int srclen,
1001 EVP_CIPHER_CTX *ctx)
1002 {
1003 uint8_t ebuf[8], ctr[8];
1004 int unused, n;
1005 struct rte_mbuf *m;
1006 uint8_t *src;
1007 int l;
1008
1009 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1010 m = m->next)
1011 offset -= rte_pktmbuf_data_len(m);
1012
1013 if (m == 0)
1014 goto process_cipher_des3ctr_err;
1015
1016 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1017 l = rte_pktmbuf_data_len(m) - offset;
1018
1019 /* We use 3DES encryption also for decryption.
1020 * IV is not important for 3DES ecb
1021 */
1022 if (EVP_EncryptInit_ex(ctx, EVP_des_ede3_ecb(), NULL, key, NULL) <= 0)
1023 goto process_cipher_des3ctr_err;
1024
1025 memcpy(ctr, iv, 8);
1026
1027 for (n = 0; n < srclen; n++) {
1028 if (n % 8 == 0) {
1029 if (EVP_EncryptUpdate(ctx,
1030 (unsigned char *)&ebuf, &unused,
1031 (const unsigned char *)&ctr, 8) <= 0)
1032 goto process_cipher_des3ctr_err;
1033 ctr_inc(ctr);
1034 }
1035 dst[n] = *(src++) ^ ebuf[n % 8];
1036
1037 l--;
1038 if (!l) {
1039 m = m->next;
1040 if (m) {
1041 src = rte_pktmbuf_mtod(m, uint8_t *);
1042 l = rte_pktmbuf_data_len(m);
1043 }
1044 }
1045 }
1046
1047 return 0;
1048
1049 process_cipher_des3ctr_err:
1050 OPENSSL_LOG(ERR, "Process openssl cipher des 3 ede ctr failed");
1051 return -EINVAL;
1052 }
1053
1054 /** Process AES-GCM encrypt algorithm */
1055 static int
process_openssl_auth_encryption_gcm(struct rte_mbuf * mbuf_src,int offset,int srclen,uint8_t * aad,int aadlen,uint8_t * iv,uint8_t * dst,uint8_t * tag,EVP_CIPHER_CTX * ctx)1056 process_openssl_auth_encryption_gcm(struct rte_mbuf *mbuf_src, int offset,
1057 int srclen, uint8_t *aad, int aadlen, uint8_t *iv,
1058 uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx)
1059 {
1060 int len = 0, unused = 0;
1061 uint8_t empty[] = {};
1062
1063 if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1064 goto process_auth_encryption_gcm_err;
1065
1066 if (aadlen > 0)
1067 if (EVP_EncryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
1068 goto process_auth_encryption_gcm_err;
1069
1070 if (srclen > 0)
1071 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
1072 srclen, ctx, 0))
1073 goto process_auth_encryption_gcm_err;
1074
1075 /* Workaround open ssl bug in version less then 1.0.1f */
1076 if (EVP_EncryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
1077 goto process_auth_encryption_gcm_err;
1078
1079 if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
1080 goto process_auth_encryption_gcm_err;
1081
1082 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, 16, tag) <= 0)
1083 goto process_auth_encryption_gcm_err;
1084
1085 return 0;
1086
1087 process_auth_encryption_gcm_err:
1088 OPENSSL_LOG(ERR, "Process openssl auth encryption gcm failed");
1089 return -EINVAL;
1090 }
1091
1092 /** Process AES-CCM encrypt algorithm */
1093 static int
process_openssl_auth_encryption_ccm(struct rte_mbuf * mbuf_src,int offset,int srclen,uint8_t * aad,int aadlen,uint8_t * iv,uint8_t * dst,uint8_t * tag,uint8_t taglen,EVP_CIPHER_CTX * ctx)1094 process_openssl_auth_encryption_ccm(struct rte_mbuf *mbuf_src, int offset,
1095 int srclen, uint8_t *aad, int aadlen, uint8_t *iv,
1096 uint8_t *dst, uint8_t *tag, uint8_t taglen, EVP_CIPHER_CTX *ctx)
1097 {
1098 int len = 0;
1099
1100 if (EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1101 goto process_auth_encryption_ccm_err;
1102
1103 if (EVP_EncryptUpdate(ctx, NULL, &len, NULL, srclen) <= 0)
1104 goto process_auth_encryption_ccm_err;
1105
1106 if (aadlen > 0)
1107 /*
1108 * For AES-CCM, the actual AAD is placed
1109 * 18 bytes after the start of the AAD field,
1110 * according to the API.
1111 */
1112 if (EVP_EncryptUpdate(ctx, NULL, &len, aad + 18, aadlen) <= 0)
1113 goto process_auth_encryption_ccm_err;
1114
1115 if (srclen >= 0)
1116 if (process_openssl_encryption_update(mbuf_src, offset, &dst,
1117 srclen, ctx, 0))
1118 goto process_auth_encryption_ccm_err;
1119
1120 if (EVP_EncryptFinal_ex(ctx, dst, &len) <= 0)
1121 goto process_auth_encryption_ccm_err;
1122
1123 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_GET_TAG, taglen, tag) <= 0)
1124 goto process_auth_encryption_ccm_err;
1125
1126 return 0;
1127
1128 process_auth_encryption_ccm_err:
1129 OPENSSL_LOG(ERR, "Process openssl auth encryption ccm failed");
1130 return -EINVAL;
1131 }
1132
1133 /** Process AES-GCM decrypt algorithm */
1134 static int
process_openssl_auth_decryption_gcm(struct rte_mbuf * mbuf_src,int offset,int srclen,uint8_t * aad,int aadlen,uint8_t * iv,uint8_t * dst,uint8_t * tag,EVP_CIPHER_CTX * ctx)1135 process_openssl_auth_decryption_gcm(struct rte_mbuf *mbuf_src, int offset,
1136 int srclen, uint8_t *aad, int aadlen, uint8_t *iv,
1137 uint8_t *dst, uint8_t *tag, EVP_CIPHER_CTX *ctx)
1138 {
1139 int len = 0, unused = 0;
1140 uint8_t empty[] = {};
1141
1142 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag) <= 0)
1143 goto process_auth_decryption_gcm_err;
1144
1145 if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1146 goto process_auth_decryption_gcm_err;
1147
1148 if (aadlen > 0)
1149 if (EVP_DecryptUpdate(ctx, NULL, &len, aad, aadlen) <= 0)
1150 goto process_auth_decryption_gcm_err;
1151
1152 if (srclen > 0)
1153 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
1154 srclen, ctx, 0))
1155 goto process_auth_decryption_gcm_err;
1156
1157 /* Workaround open ssl bug in version less then 1.0.1f */
1158 if (EVP_DecryptUpdate(ctx, empty, &unused, empty, 0) <= 0)
1159 goto process_auth_decryption_gcm_err;
1160
1161 if (EVP_DecryptFinal_ex(ctx, dst, &len) <= 0)
1162 return -EFAULT;
1163
1164 return 0;
1165
1166 process_auth_decryption_gcm_err:
1167 OPENSSL_LOG(ERR, "Process openssl auth decryption gcm failed");
1168 return -EINVAL;
1169 }
1170
1171 /** Process AES-CCM decrypt algorithm */
1172 static int
process_openssl_auth_decryption_ccm(struct rte_mbuf * mbuf_src,int offset,int srclen,uint8_t * aad,int aadlen,uint8_t * iv,uint8_t * dst,uint8_t * tag,uint8_t tag_len,EVP_CIPHER_CTX * ctx)1173 process_openssl_auth_decryption_ccm(struct rte_mbuf *mbuf_src, int offset,
1174 int srclen, uint8_t *aad, int aadlen, uint8_t *iv,
1175 uint8_t *dst, uint8_t *tag, uint8_t tag_len,
1176 EVP_CIPHER_CTX *ctx)
1177 {
1178 int len = 0;
1179
1180 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_len, tag) <= 0)
1181 goto process_auth_decryption_ccm_err;
1182
1183 if (EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, iv) <= 0)
1184 goto process_auth_decryption_ccm_err;
1185
1186 if (EVP_DecryptUpdate(ctx, NULL, &len, NULL, srclen) <= 0)
1187 goto process_auth_decryption_ccm_err;
1188
1189 if (aadlen > 0)
1190 /*
1191 * For AES-CCM, the actual AAD is placed
1192 * 18 bytes after the start of the AAD field,
1193 * according to the API.
1194 */
1195 if (EVP_DecryptUpdate(ctx, NULL, &len, aad + 18, aadlen) <= 0)
1196 goto process_auth_decryption_ccm_err;
1197
1198 if (srclen >= 0)
1199 if (process_openssl_decryption_update(mbuf_src, offset, &dst,
1200 srclen, ctx, 0))
1201 return -EFAULT;
1202
1203 return 0;
1204
1205 process_auth_decryption_ccm_err:
1206 OPENSSL_LOG(ERR, "Process openssl auth decryption ccm failed");
1207 return -EINVAL;
1208 }
1209
1210 /** Process standard openssl auth algorithms */
1211 static int
process_openssl_auth(struct rte_mbuf * mbuf_src,uint8_t * dst,int offset,__rte_unused uint8_t * iv,__rte_unused EVP_PKEY * pkey,int srclen,EVP_MD_CTX * ctx,const EVP_MD * algo)1212 process_openssl_auth(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
1213 __rte_unused uint8_t *iv, __rte_unused EVP_PKEY * pkey,
1214 int srclen, EVP_MD_CTX *ctx, const EVP_MD *algo)
1215 {
1216 size_t dstlen;
1217 struct rte_mbuf *m;
1218 int l, n = srclen;
1219 uint8_t *src;
1220
1221 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1222 m = m->next)
1223 offset -= rte_pktmbuf_data_len(m);
1224
1225 if (m == 0)
1226 goto process_auth_err;
1227
1228 if (EVP_DigestInit_ex(ctx, algo, NULL) <= 0)
1229 goto process_auth_err;
1230
1231 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1232
1233 l = rte_pktmbuf_data_len(m) - offset;
1234 if (srclen <= l) {
1235 if (EVP_DigestUpdate(ctx, (char *)src, srclen) <= 0)
1236 goto process_auth_err;
1237 goto process_auth_final;
1238 }
1239
1240 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
1241 goto process_auth_err;
1242
1243 n -= l;
1244
1245 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1246 src = rte_pktmbuf_mtod(m, uint8_t *);
1247 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1248 if (EVP_DigestUpdate(ctx, (char *)src, l) <= 0)
1249 goto process_auth_err;
1250 n -= l;
1251 }
1252
1253 process_auth_final:
1254 if (EVP_DigestFinal_ex(ctx, dst, (unsigned int *)&dstlen) <= 0)
1255 goto process_auth_err;
1256 return 0;
1257
1258 process_auth_err:
1259 OPENSSL_LOG(ERR, "Process openssl auth failed");
1260 return -EINVAL;
1261 }
1262
1263 /** Process standard openssl auth algorithms with hmac */
1264 static int
process_openssl_auth_hmac(struct rte_mbuf * mbuf_src,uint8_t * dst,int offset,int srclen,HMAC_CTX * ctx)1265 process_openssl_auth_hmac(struct rte_mbuf *mbuf_src, uint8_t *dst, int offset,
1266 int srclen, HMAC_CTX *ctx)
1267 {
1268 unsigned int dstlen;
1269 struct rte_mbuf *m;
1270 int l, n = srclen;
1271 uint8_t *src;
1272
1273 for (m = mbuf_src; m != NULL && offset > rte_pktmbuf_data_len(m);
1274 m = m->next)
1275 offset -= rte_pktmbuf_data_len(m);
1276
1277 if (m == 0)
1278 goto process_auth_err;
1279
1280 src = rte_pktmbuf_mtod_offset(m, uint8_t *, offset);
1281
1282 l = rte_pktmbuf_data_len(m) - offset;
1283 if (srclen <= l) {
1284 if (HMAC_Update(ctx, (unsigned char *)src, srclen) != 1)
1285 goto process_auth_err;
1286 goto process_auth_final;
1287 }
1288
1289 if (HMAC_Update(ctx, (unsigned char *)src, l) != 1)
1290 goto process_auth_err;
1291
1292 n -= l;
1293
1294 for (m = m->next; (m != NULL) && (n > 0); m = m->next) {
1295 src = rte_pktmbuf_mtod(m, uint8_t *);
1296 l = rte_pktmbuf_data_len(m) < n ? rte_pktmbuf_data_len(m) : n;
1297 if (HMAC_Update(ctx, (unsigned char *)src, l) != 1)
1298 goto process_auth_err;
1299 n -= l;
1300 }
1301
1302 process_auth_final:
1303 if (HMAC_Final(ctx, dst, &dstlen) != 1)
1304 goto process_auth_err;
1305
1306 if (unlikely(HMAC_Init_ex(ctx, NULL, 0, NULL, NULL) != 1))
1307 goto process_auth_err;
1308
1309 return 0;
1310
1311 process_auth_err:
1312 OPENSSL_LOG(ERR, "Process openssl auth failed");
1313 return -EINVAL;
1314 }
1315
1316 /*----------------------------------------------------------------------------*/
1317
1318 /** Process auth/cipher combined operation */
1319 static void
process_openssl_combined_op(struct rte_crypto_op * op,struct openssl_session * sess,struct rte_mbuf * mbuf_src,struct rte_mbuf * mbuf_dst)1320 process_openssl_combined_op
1321 (struct rte_crypto_op *op, struct openssl_session *sess,
1322 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1323 {
1324 /* cipher */
1325 uint8_t *dst = NULL, *iv, *tag, *aad;
1326 int srclen, aadlen, status = -1;
1327 uint32_t offset;
1328 uint8_t taglen;
1329 EVP_CIPHER_CTX *ctx_copy;
1330
1331 /*
1332 * Segmented destination buffer is not supported for
1333 * encryption/decryption
1334 */
1335 if (!rte_pktmbuf_is_contiguous(mbuf_dst)) {
1336 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1337 return;
1338 }
1339
1340 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1341 sess->iv.offset);
1342 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC) {
1343 srclen = 0;
1344 offset = op->sym->auth.data.offset;
1345 aadlen = op->sym->auth.data.length;
1346 aad = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1347 op->sym->auth.data.offset);
1348 tag = op->sym->auth.digest.data;
1349 if (tag == NULL)
1350 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1351 offset + aadlen);
1352 } else {
1353 srclen = op->sym->aead.data.length;
1354 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1355 op->sym->aead.data.offset);
1356 offset = op->sym->aead.data.offset;
1357 aad = op->sym->aead.aad.data;
1358 aadlen = sess->auth.aad_length;
1359 tag = op->sym->aead.digest.data;
1360 if (tag == NULL)
1361 tag = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1362 offset + srclen);
1363 }
1364
1365 taglen = sess->auth.digest_length;
1366 ctx_copy = EVP_CIPHER_CTX_new();
1367 EVP_CIPHER_CTX_copy(ctx_copy, sess->cipher.ctx);
1368
1369 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1370 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC ||
1371 sess->aead_algo == RTE_CRYPTO_AEAD_AES_GCM)
1372 status = process_openssl_auth_encryption_gcm(
1373 mbuf_src, offset, srclen,
1374 aad, aadlen, iv,
1375 dst, tag, ctx_copy);
1376 else
1377 status = process_openssl_auth_encryption_ccm(
1378 mbuf_src, offset, srclen,
1379 aad, aadlen, iv,
1380 dst, tag, taglen, ctx_copy);
1381
1382 } else {
1383 if (sess->auth.algo == RTE_CRYPTO_AUTH_AES_GMAC ||
1384 sess->aead_algo == RTE_CRYPTO_AEAD_AES_GCM)
1385 status = process_openssl_auth_decryption_gcm(
1386 mbuf_src, offset, srclen,
1387 aad, aadlen, iv,
1388 dst, tag, ctx_copy);
1389 else
1390 status = process_openssl_auth_decryption_ccm(
1391 mbuf_src, offset, srclen,
1392 aad, aadlen, iv,
1393 dst, tag, taglen, ctx_copy);
1394 }
1395
1396 EVP_CIPHER_CTX_free(ctx_copy);
1397 if (status != 0) {
1398 if (status == (-EFAULT) &&
1399 sess->auth.operation ==
1400 RTE_CRYPTO_AUTH_OP_VERIFY)
1401 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1402 else
1403 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1404 }
1405 }
1406
1407 /** Process cipher operation */
1408 static void
process_openssl_cipher_op(struct rte_crypto_op * op,struct openssl_session * sess,struct rte_mbuf * mbuf_src,struct rte_mbuf * mbuf_dst)1409 process_openssl_cipher_op
1410 (struct rte_crypto_op *op, struct openssl_session *sess,
1411 struct rte_mbuf *mbuf_src, struct rte_mbuf *mbuf_dst)
1412 {
1413 uint8_t *dst, *iv;
1414 int srclen, status;
1415 uint8_t inplace = (mbuf_src == mbuf_dst) ? 1 : 0;
1416 EVP_CIPHER_CTX *ctx_copy;
1417
1418 /*
1419 * Segmented OOP destination buffer is not supported for encryption/
1420 * decryption. In case of des3ctr, even inplace segmented buffers are
1421 * not supported.
1422 */
1423 if (!rte_pktmbuf_is_contiguous(mbuf_dst) &&
1424 (!inplace || sess->cipher.mode != OPENSSL_CIPHER_LIB)) {
1425 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1426 return;
1427 }
1428
1429 srclen = op->sym->cipher.data.length;
1430 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1431 op->sym->cipher.data.offset);
1432
1433 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1434 sess->iv.offset);
1435 ctx_copy = EVP_CIPHER_CTX_new();
1436 EVP_CIPHER_CTX_copy(ctx_copy, sess->cipher.ctx);
1437
1438 if (sess->cipher.mode == OPENSSL_CIPHER_LIB)
1439 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1440 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1441 op->sym->cipher.data.offset, iv,
1442 srclen, ctx_copy, inplace);
1443 else
1444 status = process_openssl_cipher_decrypt(mbuf_src, dst,
1445 op->sym->cipher.data.offset, iv,
1446 srclen, ctx_copy, inplace);
1447 else
1448 status = process_openssl_cipher_des3ctr(mbuf_src, dst,
1449 op->sym->cipher.data.offset, iv,
1450 sess->cipher.key.data, srclen,
1451 ctx_copy);
1452
1453 EVP_CIPHER_CTX_free(ctx_copy);
1454 if (status != 0)
1455 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1456 }
1457
1458 /** Process cipher operation */
1459 static void
process_openssl_docsis_bpi_op(struct rte_crypto_op * op,struct openssl_session * sess,struct rte_mbuf * mbuf_src,struct rte_mbuf * mbuf_dst)1460 process_openssl_docsis_bpi_op(struct rte_crypto_op *op,
1461 struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1462 struct rte_mbuf *mbuf_dst)
1463 {
1464 uint8_t *src, *dst, *iv;
1465 uint8_t block_size, last_block_len;
1466 int srclen, status = 0;
1467
1468 srclen = op->sym->cipher.data.length;
1469 src = rte_pktmbuf_mtod_offset(mbuf_src, uint8_t *,
1470 op->sym->cipher.data.offset);
1471 dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1472 op->sym->cipher.data.offset);
1473
1474 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1475 sess->iv.offset);
1476
1477 block_size = DES_BLOCK_SIZE;
1478
1479 last_block_len = srclen % block_size;
1480 if (sess->cipher.direction == RTE_CRYPTO_CIPHER_OP_ENCRYPT) {
1481 /* Encrypt only with ECB mode XOR IV */
1482 if (srclen < block_size) {
1483 status = process_openssl_cipher_bpi_encrypt(src, dst,
1484 iv, srclen,
1485 sess->cipher.bpi_ctx);
1486 } else {
1487 srclen -= last_block_len;
1488 /* Encrypt with the block aligned stream with CBC mode */
1489 status = process_openssl_cipher_encrypt(mbuf_src, dst,
1490 op->sym->cipher.data.offset, iv,
1491 srclen, sess->cipher.ctx, 0);
1492 if (last_block_len) {
1493 /* Point at last block */
1494 dst += srclen;
1495 /*
1496 * IV is the last encrypted block from
1497 * the previous operation
1498 */
1499 iv = dst - block_size;
1500 src += srclen;
1501 srclen = last_block_len;
1502 /* Encrypt the last frame with ECB mode */
1503 status |= process_openssl_cipher_bpi_encrypt(src,
1504 dst, iv,
1505 srclen, sess->cipher.bpi_ctx);
1506 }
1507 }
1508 } else {
1509 /* Decrypt only with ECB mode (encrypt, as it is same operation) */
1510 if (srclen < block_size) {
1511 status = process_openssl_cipher_bpi_encrypt(src, dst,
1512 iv,
1513 srclen,
1514 sess->cipher.bpi_ctx);
1515 } else {
1516 if (last_block_len) {
1517 /* Point at last block */
1518 dst += srclen - last_block_len;
1519 src += srclen - last_block_len;
1520 /*
1521 * IV is the last full block
1522 */
1523 iv = src - block_size;
1524 /*
1525 * Decrypt the last frame with ECB mode
1526 * (encrypt, as it is the same operation)
1527 */
1528 status = process_openssl_cipher_bpi_encrypt(src,
1529 dst, iv,
1530 last_block_len, sess->cipher.bpi_ctx);
1531 /* Prepare parameters for CBC mode op */
1532 iv = rte_crypto_op_ctod_offset(op, uint8_t *,
1533 sess->iv.offset);
1534 dst += last_block_len - srclen;
1535 srclen -= last_block_len;
1536 }
1537
1538 /* Decrypt with CBC mode */
1539 status |= process_openssl_cipher_decrypt(mbuf_src, dst,
1540 op->sym->cipher.data.offset, iv,
1541 srclen, sess->cipher.ctx, 0);
1542 }
1543 }
1544
1545 if (status != 0)
1546 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1547 }
1548
1549 /** Process auth operation */
1550 static void
process_openssl_auth_op(struct openssl_qp * qp,struct rte_crypto_op * op,struct openssl_session * sess,struct rte_mbuf * mbuf_src,struct rte_mbuf * mbuf_dst)1551 process_openssl_auth_op(struct openssl_qp *qp, struct rte_crypto_op *op,
1552 struct openssl_session *sess, struct rte_mbuf *mbuf_src,
1553 struct rte_mbuf *mbuf_dst)
1554 {
1555 uint8_t *dst;
1556 int srclen, status;
1557 EVP_MD_CTX *ctx_a;
1558 HMAC_CTX *ctx_h;
1559
1560 srclen = op->sym->auth.data.length;
1561
1562 dst = qp->temp_digest;
1563
1564 switch (sess->auth.mode) {
1565 case OPENSSL_AUTH_AS_AUTH:
1566 ctx_a = EVP_MD_CTX_create();
1567 EVP_MD_CTX_copy_ex(ctx_a, sess->auth.auth.ctx);
1568 status = process_openssl_auth(mbuf_src, dst,
1569 op->sym->auth.data.offset, NULL, NULL, srclen,
1570 ctx_a, sess->auth.auth.evp_algo);
1571 EVP_MD_CTX_destroy(ctx_a);
1572 break;
1573 case OPENSSL_AUTH_AS_HMAC:
1574 ctx_h = HMAC_CTX_new();
1575 HMAC_CTX_copy(ctx_h, sess->auth.hmac.ctx);
1576 status = process_openssl_auth_hmac(mbuf_src, dst,
1577 op->sym->auth.data.offset, srclen,
1578 ctx_h);
1579 HMAC_CTX_free(ctx_h);
1580 break;
1581 default:
1582 status = -1;
1583 break;
1584 }
1585
1586 if (sess->auth.operation == RTE_CRYPTO_AUTH_OP_VERIFY) {
1587 if (CRYPTO_memcmp(dst, op->sym->auth.digest.data,
1588 sess->auth.digest_length) != 0) {
1589 op->status = RTE_CRYPTO_OP_STATUS_AUTH_FAILED;
1590 }
1591 } else {
1592 uint8_t *auth_dst;
1593
1594 auth_dst = op->sym->auth.digest.data;
1595 if (auth_dst == NULL)
1596 auth_dst = rte_pktmbuf_mtod_offset(mbuf_dst, uint8_t *,
1597 op->sym->auth.data.offset +
1598 op->sym->auth.data.length);
1599 memcpy(auth_dst, dst, sess->auth.digest_length);
1600 }
1601
1602 if (status != 0)
1603 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
1604 }
1605
1606 /* process dsa sign operation */
1607 static int
process_openssl_dsa_sign_op(struct rte_crypto_op * cop,struct openssl_asym_session * sess)1608 process_openssl_dsa_sign_op(struct rte_crypto_op *cop,
1609 struct openssl_asym_session *sess)
1610 {
1611 struct rte_crypto_dsa_op_param *op = &cop->asym->dsa;
1612 DSA *dsa = sess->u.s.dsa;
1613 DSA_SIG *sign = NULL;
1614
1615 sign = DSA_do_sign(op->message.data,
1616 op->message.length,
1617 dsa);
1618
1619 if (sign == NULL) {
1620 OPENSSL_LOG(ERR, "%s:%d\n", __func__, __LINE__);
1621 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1622 } else {
1623 const BIGNUM *r = NULL, *s = NULL;
1624 get_dsa_sign(sign, &r, &s);
1625
1626 op->r.length = BN_bn2bin(r, op->r.data);
1627 op->s.length = BN_bn2bin(s, op->s.data);
1628 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1629 }
1630
1631 DSA_SIG_free(sign);
1632
1633 return 0;
1634 }
1635
1636 /* process dsa verify operation */
1637 static int
process_openssl_dsa_verify_op(struct rte_crypto_op * cop,struct openssl_asym_session * sess)1638 process_openssl_dsa_verify_op(struct rte_crypto_op *cop,
1639 struct openssl_asym_session *sess)
1640 {
1641 struct rte_crypto_dsa_op_param *op = &cop->asym->dsa;
1642 DSA *dsa = sess->u.s.dsa;
1643 int ret;
1644 DSA_SIG *sign = DSA_SIG_new();
1645 BIGNUM *r = NULL, *s = NULL;
1646 BIGNUM *pub_key = NULL;
1647
1648 if (sign == NULL) {
1649 OPENSSL_LOG(ERR, " %s:%d\n", __func__, __LINE__);
1650 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1651 return -1;
1652 }
1653
1654 r = BN_bin2bn(op->r.data,
1655 op->r.length,
1656 r);
1657 s = BN_bin2bn(op->s.data,
1658 op->s.length,
1659 s);
1660 pub_key = BN_bin2bn(op->y.data,
1661 op->y.length,
1662 pub_key);
1663 if (!r || !s || !pub_key) {
1664 BN_free(r);
1665 BN_free(s);
1666 BN_free(pub_key);
1667
1668 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1669 return -1;
1670 }
1671 set_dsa_sign(sign, r, s);
1672 set_dsa_pub_key(dsa, pub_key);
1673
1674 ret = DSA_do_verify(op->message.data,
1675 op->message.length,
1676 sign,
1677 dsa);
1678
1679 if (ret != 1)
1680 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1681 else
1682 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1683
1684 DSA_SIG_free(sign);
1685
1686 return 0;
1687 }
1688
1689 /* process dh operation */
1690 static int
process_openssl_dh_op(struct rte_crypto_op * cop,struct openssl_asym_session * sess)1691 process_openssl_dh_op(struct rte_crypto_op *cop,
1692 struct openssl_asym_session *sess)
1693 {
1694 struct rte_crypto_dh_op_param *op = &cop->asym->dh;
1695 DH *dh_key = sess->u.dh.dh_key;
1696 BIGNUM *priv_key = NULL;
1697 int ret = 0;
1698
1699 if (sess->u.dh.key_op &
1700 (1 << RTE_CRYPTO_ASYM_OP_SHARED_SECRET_COMPUTE)) {
1701 /* compute shared secret using peer public key
1702 * and current private key
1703 * shared secret = peer_key ^ priv_key mod p
1704 */
1705 BIGNUM *peer_key = NULL;
1706
1707 /* copy private key and peer key and compute shared secret */
1708 peer_key = BN_bin2bn(op->pub_key.data,
1709 op->pub_key.length,
1710 peer_key);
1711 if (peer_key == NULL) {
1712 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1713 return -1;
1714 }
1715 priv_key = BN_bin2bn(op->priv_key.data,
1716 op->priv_key.length,
1717 priv_key);
1718 if (priv_key == NULL) {
1719 BN_free(peer_key);
1720 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1721 return -1;
1722 }
1723 ret = set_dh_priv_key(dh_key, priv_key);
1724 if (ret) {
1725 OPENSSL_LOG(ERR, "Failed to set private key\n");
1726 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1727 BN_free(peer_key);
1728 BN_free(priv_key);
1729 return 0;
1730 }
1731
1732 ret = DH_compute_key(
1733 op->shared_secret.data,
1734 peer_key, dh_key);
1735 if (ret < 0) {
1736 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1737 BN_free(peer_key);
1738 /* priv key is already loaded into dh,
1739 * let's not free that directly here.
1740 * DH_free() will auto free it later.
1741 */
1742 return 0;
1743 }
1744 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1745 op->shared_secret.length = ret;
1746 BN_free(peer_key);
1747 return 0;
1748 }
1749
1750 /*
1751 * other options are public and private key generations.
1752 *
1753 * if user provides private key,
1754 * then first set DH with user provided private key
1755 */
1756 if ((sess->u.dh.key_op &
1757 (1 << RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE)) &&
1758 !(sess->u.dh.key_op &
1759 (1 << RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE))) {
1760 /* generate public key using user-provided private key
1761 * pub_key = g ^ priv_key mod p
1762 */
1763
1764 /* load private key into DH */
1765 priv_key = BN_bin2bn(op->priv_key.data,
1766 op->priv_key.length,
1767 priv_key);
1768 if (priv_key == NULL) {
1769 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1770 return -1;
1771 }
1772 ret = set_dh_priv_key(dh_key, priv_key);
1773 if (ret) {
1774 OPENSSL_LOG(ERR, "Failed to set private key\n");
1775 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1776 BN_free(priv_key);
1777 return 0;
1778 }
1779 }
1780
1781 /* generate public and private key pair.
1782 *
1783 * if private key already set, generates only public key.
1784 *
1785 * if private key is not already set, then set it to random value
1786 * and update internal private key.
1787 */
1788 if (!DH_generate_key(dh_key)) {
1789 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1790 return 0;
1791 }
1792
1793 if (sess->u.dh.key_op & (1 << RTE_CRYPTO_ASYM_OP_PUBLIC_KEY_GENERATE)) {
1794 const BIGNUM *pub_key = NULL;
1795
1796 OPENSSL_LOG(DEBUG, "%s:%d update public key\n",
1797 __func__, __LINE__);
1798
1799 /* get the generated keys */
1800 get_dh_pub_key(dh_key, &pub_key);
1801
1802 /* output public key */
1803 op->pub_key.length = BN_bn2bin(pub_key,
1804 op->pub_key.data);
1805 }
1806
1807 if (sess->u.dh.key_op &
1808 (1 << RTE_CRYPTO_ASYM_OP_PRIVATE_KEY_GENERATE)) {
1809 const BIGNUM *priv_key = NULL;
1810
1811 OPENSSL_LOG(DEBUG, "%s:%d updated priv key\n",
1812 __func__, __LINE__);
1813
1814 /* get the generated keys */
1815 get_dh_priv_key(dh_key, &priv_key);
1816
1817 /* provide generated private key back to user */
1818 op->priv_key.length = BN_bn2bin(priv_key,
1819 op->priv_key.data);
1820 }
1821
1822 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1823
1824 return 0;
1825 }
1826
1827 /* process modinv operation */
1828 static int
process_openssl_modinv_op(struct rte_crypto_op * cop,struct openssl_asym_session * sess)1829 process_openssl_modinv_op(struct rte_crypto_op *cop,
1830 struct openssl_asym_session *sess)
1831 {
1832 struct rte_crypto_asym_op *op = cop->asym;
1833 BIGNUM *base = BN_CTX_get(sess->u.m.ctx);
1834 BIGNUM *res = BN_CTX_get(sess->u.m.ctx);
1835
1836 if (unlikely(base == NULL || res == NULL)) {
1837 BN_free(base);
1838 BN_free(res);
1839 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1840 return -1;
1841 }
1842
1843 base = BN_bin2bn((const unsigned char *)op->modinv.base.data,
1844 op->modinv.base.length, base);
1845
1846 if (BN_mod_inverse(res, base, sess->u.m.modulus, sess->u.m.ctx)) {
1847 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1848 op->modinv.result.length = BN_bn2bin(res, op->modinv.result.data);
1849 } else {
1850 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1851 }
1852
1853 BN_clear(res);
1854 BN_clear(base);
1855
1856 return 0;
1857 }
1858
1859 /* process modexp operation */
1860 static int
process_openssl_modexp_op(struct rte_crypto_op * cop,struct openssl_asym_session * sess)1861 process_openssl_modexp_op(struct rte_crypto_op *cop,
1862 struct openssl_asym_session *sess)
1863 {
1864 struct rte_crypto_asym_op *op = cop->asym;
1865 BIGNUM *base = BN_CTX_get(sess->u.e.ctx);
1866 BIGNUM *res = BN_CTX_get(sess->u.e.ctx);
1867
1868 if (unlikely(base == NULL || res == NULL)) {
1869 BN_free(base);
1870 BN_free(res);
1871 cop->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1872 return -1;
1873 }
1874
1875 base = BN_bin2bn((const unsigned char *)op->modex.base.data,
1876 op->modex.base.length, base);
1877
1878 if (BN_mod_exp(res, base, sess->u.e.exp,
1879 sess->u.e.mod, sess->u.e.ctx)) {
1880 op->modex.result.length = BN_bn2bin(res, op->modex.result.data);
1881 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1882 } else {
1883 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1884 }
1885
1886 BN_clear(res);
1887 BN_clear(base);
1888
1889 return 0;
1890 }
1891
1892 /* process rsa operations */
1893 static int
process_openssl_rsa_op(struct rte_crypto_op * cop,struct openssl_asym_session * sess)1894 process_openssl_rsa_op(struct rte_crypto_op *cop,
1895 struct openssl_asym_session *sess)
1896 {
1897 int ret = 0;
1898 struct rte_crypto_asym_op *op = cop->asym;
1899 RSA *rsa = sess->u.r.rsa;
1900 uint32_t pad = (op->rsa.pad);
1901 uint8_t *tmp;
1902
1903 cop->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
1904
1905 switch (pad) {
1906 case RTE_CRYPTO_RSA_PADDING_PKCS1_5:
1907 pad = RSA_PKCS1_PADDING;
1908 break;
1909 case RTE_CRYPTO_RSA_PADDING_NONE:
1910 pad = RSA_NO_PADDING;
1911 break;
1912 default:
1913 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
1914 OPENSSL_LOG(ERR,
1915 "rsa pad type not supported %d\n", pad);
1916 return 0;
1917 }
1918
1919 switch (op->rsa.op_type) {
1920 case RTE_CRYPTO_ASYM_OP_ENCRYPT:
1921 ret = RSA_public_encrypt(op->rsa.message.length,
1922 op->rsa.message.data,
1923 op->rsa.cipher.data,
1924 rsa,
1925 pad);
1926
1927 if (ret > 0)
1928 op->rsa.cipher.length = ret;
1929 OPENSSL_LOG(DEBUG,
1930 "length of encrypted text %d\n", ret);
1931 break;
1932
1933 case RTE_CRYPTO_ASYM_OP_DECRYPT:
1934 ret = RSA_private_decrypt(op->rsa.cipher.length,
1935 op->rsa.cipher.data,
1936 op->rsa.message.data,
1937 rsa,
1938 pad);
1939 if (ret > 0)
1940 op->rsa.message.length = ret;
1941 break;
1942
1943 case RTE_CRYPTO_ASYM_OP_SIGN:
1944 ret = RSA_private_encrypt(op->rsa.message.length,
1945 op->rsa.message.data,
1946 op->rsa.sign.data,
1947 rsa,
1948 pad);
1949 if (ret > 0)
1950 op->rsa.sign.length = ret;
1951 break;
1952
1953 case RTE_CRYPTO_ASYM_OP_VERIFY:
1954 tmp = rte_malloc(NULL, op->rsa.sign.length, 0);
1955 if (tmp == NULL) {
1956 OPENSSL_LOG(ERR, "Memory allocation failed");
1957 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1958 break;
1959 }
1960 ret = RSA_public_decrypt(op->rsa.sign.length,
1961 op->rsa.sign.data,
1962 tmp,
1963 rsa,
1964 pad);
1965
1966 OPENSSL_LOG(DEBUG,
1967 "Length of public_decrypt %d "
1968 "length of message %zd\n",
1969 ret, op->rsa.message.length);
1970 if ((ret <= 0) || (CRYPTO_memcmp(tmp, op->rsa.message.data,
1971 op->rsa.message.length))) {
1972 OPENSSL_LOG(ERR, "RSA sign Verification failed");
1973 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1974 }
1975 rte_free(tmp);
1976 break;
1977
1978 default:
1979 /* allow ops with invalid args to be pushed to
1980 * completion queue
1981 */
1982 cop->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
1983 break;
1984 }
1985
1986 if (ret < 0)
1987 cop->status = RTE_CRYPTO_OP_STATUS_ERROR;
1988
1989 return 0;
1990 }
1991
1992 static int
process_asym_op(struct openssl_qp * qp,struct rte_crypto_op * op,struct openssl_asym_session * sess)1993 process_asym_op(struct openssl_qp *qp, struct rte_crypto_op *op,
1994 struct openssl_asym_session *sess)
1995 {
1996 int retval = 0;
1997
1998 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
1999
2000 switch (sess->xfrm_type) {
2001 case RTE_CRYPTO_ASYM_XFORM_RSA:
2002 retval = process_openssl_rsa_op(op, sess);
2003 break;
2004 case RTE_CRYPTO_ASYM_XFORM_MODEX:
2005 retval = process_openssl_modexp_op(op, sess);
2006 break;
2007 case RTE_CRYPTO_ASYM_XFORM_MODINV:
2008 retval = process_openssl_modinv_op(op, sess);
2009 break;
2010 case RTE_CRYPTO_ASYM_XFORM_DH:
2011 retval = process_openssl_dh_op(op, sess);
2012 break;
2013 case RTE_CRYPTO_ASYM_XFORM_DSA:
2014 if (op->asym->dsa.op_type == RTE_CRYPTO_ASYM_OP_SIGN)
2015 retval = process_openssl_dsa_sign_op(op, sess);
2016 else if (op->asym->dsa.op_type ==
2017 RTE_CRYPTO_ASYM_OP_VERIFY)
2018 retval =
2019 process_openssl_dsa_verify_op(op, sess);
2020 else
2021 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
2022 break;
2023 default:
2024 op->status = RTE_CRYPTO_OP_STATUS_INVALID_ARGS;
2025 break;
2026 }
2027 if (!retval) {
2028 /* op processed so push to completion queue as processed */
2029 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
2030 if (retval)
2031 /* return error if failed to put in completion queue */
2032 retval = -1;
2033 }
2034
2035 return retval;
2036 }
2037
2038 static void
copy_plaintext(struct rte_mbuf * m_src,struct rte_mbuf * m_dst,struct rte_crypto_op * op)2039 copy_plaintext(struct rte_mbuf *m_src, struct rte_mbuf *m_dst,
2040 struct rte_crypto_op *op)
2041 {
2042 uint8_t *p_src, *p_dst;
2043
2044 p_src = rte_pktmbuf_mtod(m_src, uint8_t *);
2045 p_dst = rte_pktmbuf_mtod(m_dst, uint8_t *);
2046
2047 /**
2048 * Copy the content between cipher offset and auth offset
2049 * for generating correct digest.
2050 */
2051 if (op->sym->cipher.data.offset > op->sym->auth.data.offset)
2052 memcpy(p_dst + op->sym->auth.data.offset,
2053 p_src + op->sym->auth.data.offset,
2054 op->sym->cipher.data.offset -
2055 op->sym->auth.data.offset);
2056 }
2057
2058 /** Process crypto operation for mbuf */
2059 static int
process_op(struct openssl_qp * qp,struct rte_crypto_op * op,struct openssl_session * sess)2060 process_op(struct openssl_qp *qp, struct rte_crypto_op *op,
2061 struct openssl_session *sess)
2062 {
2063 struct rte_mbuf *msrc, *mdst;
2064 int retval;
2065
2066 msrc = op->sym->m_src;
2067 mdst = op->sym->m_dst ? op->sym->m_dst : op->sym->m_src;
2068
2069 op->status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
2070
2071 switch (sess->chain_order) {
2072 case OPENSSL_CHAIN_ONLY_CIPHER:
2073 process_openssl_cipher_op(op, sess, msrc, mdst);
2074 break;
2075 case OPENSSL_CHAIN_ONLY_AUTH:
2076 process_openssl_auth_op(qp, op, sess, msrc, mdst);
2077 break;
2078 case OPENSSL_CHAIN_CIPHER_AUTH:
2079 process_openssl_cipher_op(op, sess, msrc, mdst);
2080 /* OOP */
2081 if (msrc != mdst)
2082 copy_plaintext(msrc, mdst, op);
2083 process_openssl_auth_op(qp, op, sess, mdst, mdst);
2084 break;
2085 case OPENSSL_CHAIN_AUTH_CIPHER:
2086 process_openssl_auth_op(qp, op, sess, msrc, mdst);
2087 process_openssl_cipher_op(op, sess, msrc, mdst);
2088 break;
2089 case OPENSSL_CHAIN_COMBINED:
2090 process_openssl_combined_op(op, sess, msrc, mdst);
2091 break;
2092 case OPENSSL_CHAIN_CIPHER_BPI:
2093 process_openssl_docsis_bpi_op(op, sess, msrc, mdst);
2094 break;
2095 default:
2096 op->status = RTE_CRYPTO_OP_STATUS_ERROR;
2097 break;
2098 }
2099
2100 /* Free session if a session-less crypto op */
2101 if (op->sess_type == RTE_CRYPTO_OP_SESSIONLESS) {
2102 openssl_reset_session(sess);
2103 memset(sess, 0, sizeof(struct openssl_session));
2104 memset(op->sym->session, 0,
2105 rte_cryptodev_sym_get_existing_header_session_size(
2106 op->sym->session));
2107 rte_mempool_put(qp->sess_mp_priv, sess);
2108 rte_mempool_put(qp->sess_mp, op->sym->session);
2109 op->sym->session = NULL;
2110 }
2111
2112 if (op->status == RTE_CRYPTO_OP_STATUS_NOT_PROCESSED)
2113 op->status = RTE_CRYPTO_OP_STATUS_SUCCESS;
2114
2115 if (op->status != RTE_CRYPTO_OP_STATUS_ERROR)
2116 retval = rte_ring_enqueue(qp->processed_ops, (void *)op);
2117 else
2118 retval = -1;
2119
2120 return retval;
2121 }
2122
2123 /*
2124 *------------------------------------------------------------------------------
2125 * PMD Framework
2126 *------------------------------------------------------------------------------
2127 */
2128
2129 /** Enqueue burst */
2130 static uint16_t
openssl_pmd_enqueue_burst(void * queue_pair,struct rte_crypto_op ** ops,uint16_t nb_ops)2131 openssl_pmd_enqueue_burst(void *queue_pair, struct rte_crypto_op **ops,
2132 uint16_t nb_ops)
2133 {
2134 void *sess;
2135 struct openssl_qp *qp = queue_pair;
2136 int i, retval;
2137
2138 for (i = 0; i < nb_ops; i++) {
2139 sess = get_session(qp, ops[i]);
2140 if (unlikely(sess == NULL))
2141 goto enqueue_err;
2142
2143 if (ops[i]->type == RTE_CRYPTO_OP_TYPE_SYMMETRIC)
2144 retval = process_op(qp, ops[i],
2145 (struct openssl_session *) sess);
2146 else
2147 retval = process_asym_op(qp, ops[i],
2148 (struct openssl_asym_session *) sess);
2149 if (unlikely(retval < 0))
2150 goto enqueue_err;
2151 }
2152
2153 qp->stats.enqueued_count += i;
2154 return i;
2155
2156 enqueue_err:
2157 qp->stats.enqueue_err_count++;
2158 return i;
2159 }
2160
2161 /** Dequeue burst */
2162 static uint16_t
openssl_pmd_dequeue_burst(void * queue_pair,struct rte_crypto_op ** ops,uint16_t nb_ops)2163 openssl_pmd_dequeue_burst(void *queue_pair, struct rte_crypto_op **ops,
2164 uint16_t nb_ops)
2165 {
2166 struct openssl_qp *qp = queue_pair;
2167
2168 unsigned int nb_dequeued = 0;
2169
2170 nb_dequeued = rte_ring_dequeue_burst(qp->processed_ops,
2171 (void **)ops, nb_ops, NULL);
2172 qp->stats.dequeued_count += nb_dequeued;
2173
2174 return nb_dequeued;
2175 }
2176
2177 /** Create OPENSSL crypto device */
2178 static int
cryptodev_openssl_create(const char * name,struct rte_vdev_device * vdev,struct rte_cryptodev_pmd_init_params * init_params)2179 cryptodev_openssl_create(const char *name,
2180 struct rte_vdev_device *vdev,
2181 struct rte_cryptodev_pmd_init_params *init_params)
2182 {
2183 struct rte_cryptodev *dev;
2184 struct openssl_private *internals;
2185
2186 dev = rte_cryptodev_pmd_create(name, &vdev->device, init_params);
2187 if (dev == NULL) {
2188 OPENSSL_LOG(ERR, "failed to create cryptodev vdev");
2189 goto init_error;
2190 }
2191
2192 dev->driver_id = cryptodev_driver_id;
2193 dev->dev_ops = rte_openssl_pmd_ops;
2194
2195 /* register rx/tx burst functions for data path */
2196 dev->dequeue_burst = openssl_pmd_dequeue_burst;
2197 dev->enqueue_burst = openssl_pmd_enqueue_burst;
2198
2199 dev->feature_flags = RTE_CRYPTODEV_FF_SYMMETRIC_CRYPTO |
2200 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING |
2201 RTE_CRYPTODEV_FF_CPU_AESNI |
2202 RTE_CRYPTODEV_FF_IN_PLACE_SGL |
2203 RTE_CRYPTODEV_FF_OOP_SGL_IN_LB_OUT |
2204 RTE_CRYPTODEV_FF_OOP_LB_IN_LB_OUT |
2205 RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO |
2206 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_EXP |
2207 RTE_CRYPTODEV_FF_RSA_PRIV_OP_KEY_QT |
2208 RTE_CRYPTODEV_FF_SYM_SESSIONLESS;
2209
2210 internals = dev->data->dev_private;
2211
2212 internals->max_nb_qpairs = init_params->max_nb_queue_pairs;
2213
2214 rte_cryptodev_pmd_probing_finish(dev);
2215
2216 return 0;
2217
2218 init_error:
2219 OPENSSL_LOG(ERR, "driver %s: create failed",
2220 init_params->name);
2221
2222 cryptodev_openssl_remove(vdev);
2223 return -EFAULT;
2224 }
2225
2226 /** Initialise OPENSSL crypto device */
2227 static int
cryptodev_openssl_probe(struct rte_vdev_device * vdev)2228 cryptodev_openssl_probe(struct rte_vdev_device *vdev)
2229 {
2230 struct rte_cryptodev_pmd_init_params init_params = {
2231 "",
2232 sizeof(struct openssl_private),
2233 rte_socket_id(),
2234 RTE_CRYPTODEV_PMD_DEFAULT_MAX_NB_QUEUE_PAIRS
2235 };
2236 const char *name;
2237 const char *input_args;
2238
2239 name = rte_vdev_device_name(vdev);
2240 if (name == NULL)
2241 return -EINVAL;
2242 input_args = rte_vdev_device_args(vdev);
2243
2244 rte_cryptodev_pmd_parse_input_args(&init_params, input_args);
2245
2246 return cryptodev_openssl_create(name, vdev, &init_params);
2247 }
2248
2249 /** Uninitialise OPENSSL crypto device */
2250 static int
cryptodev_openssl_remove(struct rte_vdev_device * vdev)2251 cryptodev_openssl_remove(struct rte_vdev_device *vdev)
2252 {
2253 struct rte_cryptodev *cryptodev;
2254 const char *name;
2255
2256 name = rte_vdev_device_name(vdev);
2257 if (name == NULL)
2258 return -EINVAL;
2259
2260 cryptodev = rte_cryptodev_pmd_get_named_dev(name);
2261 if (cryptodev == NULL)
2262 return -ENODEV;
2263
2264 return rte_cryptodev_pmd_destroy(cryptodev);
2265 }
2266
2267 static struct rte_vdev_driver cryptodev_openssl_pmd_drv = {
2268 .probe = cryptodev_openssl_probe,
2269 .remove = cryptodev_openssl_remove
2270 };
2271
2272 static struct cryptodev_driver openssl_crypto_drv;
2273
2274 RTE_PMD_REGISTER_VDEV(CRYPTODEV_NAME_OPENSSL_PMD,
2275 cryptodev_openssl_pmd_drv);
2276 RTE_PMD_REGISTER_PARAM_STRING(CRYPTODEV_NAME_OPENSSL_PMD,
2277 "max_nb_queue_pairs=<int> "
2278 "socket_id=<int>");
2279 RTE_PMD_REGISTER_CRYPTO_DRIVER(openssl_crypto_drv,
2280 cryptodev_openssl_pmd_drv.driver, cryptodev_driver_id);
2281 RTE_LOG_REGISTER_DEFAULT(openssl_logtype_driver, INFO);
2282