forked from intel/QAT_Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qat_evp.c
1957 lines (1785 loc) · 63.4 KB
/
qat_evp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ====================================================================
*
*
* BSD LICENSE
*
* Copyright(c) 2019-2024 Intel Corporation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* ====================================================================
*/
/*****************************************************************************
* @file qat_evp.c
*
* This file provides an initialisation of the various operations at the EVP
* layer for an OpenSSL engine.
*
*****************************************************************************/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <stddef.h>
#include <stdarg.h>
#include "openssl/ossl_typ.h"
#ifndef QAT_BORINGSSL
#include "openssl/kdf.h"
#endif
#include "openssl/evp.h"
#include "e_qat.h"
#include "qat_evp.h"
#include "qat_utils.h"
#ifdef QAT_HW
# include "qat_hw_rsa.h"
# include "qat_hw_sm4_cbc.h"
# ifndef QAT_BORINGSSL
# include "qat_hw_ciphers.h"
# endif /* QAT_BORINGSSL */
# include "qat_hw_ec.h"
# ifndef QAT_BORINGSSL
# include "qat_hw_gcm.h"
# include "qat_hw_ccm.h"
# include "qat_hw_sha3.h"
# include "qat_hw_chachapoly.h"
# include "qat_hw_sm3.h"
# include "qat_hw_sm2.h"
# endif /* QAT_BORINGSSL */
#endif
#ifdef ENABLE_QAT_SW_GCM
# ifndef QAT_BORINGSSL
# include "qat_sw_gcm.h"
# endif /* QAT_BORINGSSL */
#endif
#ifdef QAT_SW
# ifndef QAT_BORINGSSL
# include "qat_sw_ecx.h"
# include "qat_sw_sm3.h"
# endif /* QAT_BORINGSSL */
# include "qat_sw_ec.h"
# include "qat_sw_rsa.h"
# include "qat_sw_sm3.h"
# include "qat_sw_sm2.h"
# ifndef QAT_BORINGSSL
# include "qat_sw_sm4_cbc.h"
# endif /* QAT_BORINGSSL */
# include "crypto_mb/cpu_features.h"
# ifndef QAT_BORINGSSL
# ifdef ENABLE_QAT_SW_SM4_GCM
# include "qat_sw_sm4_gcm.h"
# include "crypto_mb/sm4_gcm.h"
# endif
# ifdef ENABLE_QAT_SW_SM4_CCM
# include "qat_sw_sm4_ccm.h"
# include "crypto_mb/sm4_ccm.h"
# endif
# endif /* QAT_BORINGSSL */
#endif
#ifdef QAT_HW_INTREE
# define ENABLE_QAT_HW_SHA3
# define ENABLE_QAT_HW_CHACHAPOLY
#endif
#ifndef SM4_BLOCK_SIZE
# define SM4_BLOCK_SIZE 16
#endif
# ifndef QAT_BORINGSSL
typedef struct _chained_info {
const int nid;
EVP_CIPHER *cipher;
const int keylen;
} chained_info;
static chained_info info[] = {
#ifdef ENABLE_QAT_HW_CIPHERS
# ifdef QAT_INSECURE_ALGO
{NID_aes_128_cbc_hmac_sha1, NULL, AES_KEY_SIZE_128},
{NID_aes_256_cbc_hmac_sha1, NULL, AES_KEY_SIZE_256},
# endif
{NID_aes_128_cbc_hmac_sha256, NULL, AES_KEY_SIZE_128},
{NID_aes_256_cbc_hmac_sha256, NULL, AES_KEY_SIZE_256},
#endif
#ifdef ENABLE_QAT_HW_CHACHAPOLY
{NID_chacha20_poly1305, NULL, CHACHA_KEY_SIZE},
#endif
#if defined(ENABLE_QAT_HW_GCM) || defined(ENABLE_QAT_SW_GCM)
{NID_aes_128_gcm, NULL, AES_KEY_SIZE_128},
{NID_aes_192_gcm, NULL, AES_KEY_SIZE_192},
{NID_aes_256_gcm, NULL, AES_KEY_SIZE_256},
#endif
#if defined(ENABLE_QAT_HW_SM4_CBC) || defined(ENABLE_QAT_SW_SM4_CBC)
/* sm4-cbc key size is fixed to 128 bits (16 bytes) */
{NID_sm4_cbc, NULL, SM4_KEY_SIZE},
#endif
#ifdef ENABLE_QAT_SW_SM4_GCM
{NID_sm4_gcm, NULL, SM4_KEY_SIZE},
#endif
#ifdef ENABLE_QAT_SW_SM4_CCM
{NID_sm4_ccm, NULL, SM4_KEY_SIZE},
#endif
#ifdef ENABLE_QAT_HW_CCM
{NID_aes_128_ccm, NULL, AES_KEY_SIZE_128},
{NID_aes_192_ccm, NULL, AES_KEY_SIZE_192},
{NID_aes_256_ccm, NULL, AES_KEY_SIZE_256},
#endif
};
static const unsigned int num_cc = sizeof(info) / sizeof(chained_info);
/* Qat Symmetric cipher function register */
int qat_cipher_nids[] = {
#ifdef ENABLE_QAT_HW_CIPHERS
# ifdef QAT_INSECURE_ALGO
NID_aes_128_cbc_hmac_sha1,
NID_aes_256_cbc_hmac_sha1,
# endif
NID_aes_128_cbc_hmac_sha256,
NID_aes_256_cbc_hmac_sha256,
#endif
#ifdef ENABLE_QAT_HW_CHACHAPOLY
NID_chacha20_poly1305,
#endif
#if defined(ENABLE_QAT_HW_GCM) || defined(ENABLE_QAT_SW_GCM)
NID_aes_128_gcm,
NID_aes_192_gcm,
NID_aes_256_gcm,
#endif
#if defined(ENABLE_QAT_HW_SM4_CBC) || defined(ENABLE_QAT_SW_SM4_CBC)
NID_sm4_cbc,
#endif
#ifdef ENABLE_QAT_SW_SM4_GCM
NID_sm4_gcm,
#endif
#ifdef ENABLE_QAT_SW_SM4_CCM
NID_sm4_ccm,
#endif
#ifdef ENABLE_QAT_HW_CCM
NID_aes_128_ccm,
NID_aes_192_ccm,
NID_aes_256_ccm,
#endif
};
/* Supported EVP nids */
int qat_evp_nids[] = {
# ifdef ENABLE_QAT_HW_PRF
EVP_PKEY_TLS1_PRF,
# endif
# ifdef ENABLE_QAT_HW_HKDF
EVP_PKEY_HKDF,
# endif
# if defined(ENABLE_QAT_HW_ECX) || defined(ENABLE_QAT_SW_ECX)
EVP_PKEY_X25519,
# endif
# ifdef ENABLE_QAT_HW_ECX
EVP_PKEY_X448,
# endif
# if defined(ENABLE_QAT_SW_SM2) || defined(ENABLE_QAT_HW_SM2)
EVP_PKEY_SM2
# endif
};
const int num_evp_nids = sizeof(qat_evp_nids) / sizeof(qat_evp_nids[0]);
typedef struct _digest_data {
const int m_type;
EVP_MD *md;
const int pkey_type;
} digest_data;
static digest_data digest_info[] = {
#ifdef ENABLE_QAT_HW_SHA3
# ifdef QAT_INSECURE_ALGO
{ NID_sha3_224, NULL, NID_RSA_SHA3_224},
# endif
{ NID_sha3_256, NULL, NID_RSA_SHA3_256},
{ NID_sha3_384, NULL, NID_RSA_SHA3_384},
{ NID_sha3_512, NULL, NID_RSA_SHA3_512},
#endif
#if defined(ENABLE_QAT_SW_SM3) || defined(ENABLE_QAT_HW_SM3)
{ NID_sm3, NULL, NID_sm3WithRSAEncryption},
#endif
};
/* QAT Hash Algorithm register */
int qat_digest_nids[] = {
#ifdef ENABLE_QAT_HW_SHA3
# ifdef QAT_INSECURE_ALGO
NID_sha3_224,
# endif
NID_sha3_256,
NID_sha3_384,
NID_sha3_512,
# endif
#if defined(ENABLE_QAT_SW_SM3) || defined(ENABLE_QAT_HW_SM3)
NID_sm3,
#endif
};
const int num_digest_nids = sizeof(qat_digest_nids) / sizeof(qat_digest_nids[0]);
# ifndef ENABLE_QAT_SMALL_PKT_OFFLOAD
typedef struct cipher_threshold_table_s {
int nid;
int threshold;
} PKT_THRESHOLD;
static PKT_THRESHOLD qat_pkt_threshold_table[] = {
# ifdef ENABLE_QAT_HW_CIPHERS
# ifdef QAT_INSECURE_ALGO
{NID_aes_128_cbc_hmac_sha1, CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT},
{NID_aes_256_cbc_hmac_sha1, CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT},
# endif
{NID_aes_128_cbc_hmac_sha256,
CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT},
{NID_aes_256_cbc_hmac_sha256,
CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT},
# endif
# ifdef ENABLE_QAT_HW_SHA3
# ifdef QAT_INSECURE_ALGO
{NID_sha3_224, CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT},
# endif
{NID_sha3_256, CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT},
{NID_sha3_384, CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT},
{NID_sha3_512, CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT},
# endif
# ifdef ENABLE_QAT_HW_CHACHAPOLY
{NID_chacha20_poly1305, CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT},
# endif
# if defined(ENABLE_QAT_HW_SM4_CBC) || defined(ENABLE_QAT_SW_SM4_CBC)
{NID_sm4_cbc, CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_SM4_CBC},
# endif
# ifdef ENABLE_QAT_HW_SM3
{NID_sm3, CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_HW_SM3},
# endif
# ifdef ENABLE_QAT_HW_CCM
{NID_aes_128_ccm, CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT},
{NID_aes_192_ccm, CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT},
{NID_aes_256_ccm, CRYPTO_SMALL_PACKET_OFFLOAD_THRESHOLD_DEFAULT},
# endif
};
static int pkt_threshold_table_size =
(sizeof(qat_pkt_threshold_table) / sizeof(qat_pkt_threshold_table[0]));
# endif
#endif /* QAT_BORINGSSL */
static EC_KEY_METHOD *qat_ec_method = NULL;
static RSA_METHOD *qat_rsa_method = NULL;
#ifdef QAT_BORINGSSL
static RSA_METHOD null_rsa_method = {.common={.is_static = 1}};
static EC_KEY_METHOD null_ecdsa_method = {.common={.is_static = 1}};
#endif /* QAT_BORINGSSL */
#ifndef QAT_BORINGSSL
static EVP_PKEY_METHOD *_hidden_x25519_pmeth = NULL;
/* Have a store of the s/w EVP_PKEY_METHOD for software fallback purposes. */
const EVP_PKEY_METHOD *sw_x25519_pmeth = NULL;
static EVP_PKEY_METHOD *_hidden_x448_pmeth = NULL;
/* Have a store of the s/w EVP_PKEY_METHOD for software fallback purposes. */
const EVP_PKEY_METHOD *sw_x448_pmeth = NULL;
#if defined(ENABLE_QAT_HW_SM2) || defined(ENABLE_QAT_SW_SM2)
static EVP_PKEY_METHOD *_hidden_sm2_pmeth = NULL;
const EVP_PKEY_METHOD *sw_sm2_pmeth = NULL;
#endif
#if defined(ENABLE_QAT_SW_SM3)
int qat_sw_sm3_md_methods(EVP_MD *c)
{
int res = 1;
res &= EVP_MD_meth_set_result_size(c, 32);
res &= EVP_MD_meth_set_input_blocksize(c, SM3_MSG_BLOCK_SIZE);
res &= EVP_MD_meth_set_app_datasize(c, sizeof(EVP_MD *) + sizeof(QAT_SM3_CTX_mb));
res &= EVP_MD_meth_set_flags(c, EVP_MD_CTX_FLAG_REUSE);
#ifndef QAT_OPENSSL_PROVIDER
res &= EVP_MD_meth_set_init(c, qat_sw_sm3_init);
res &= EVP_MD_meth_set_update(c, qat_sw_sm3_update);
res &= EVP_MD_meth_set_final(c, qat_sw_sm3_final);
#endif
return res;
}
#endif
const EVP_MD *qat_sw_create_sm3_meth(int nid , int key_type)
{
#ifdef ENABLE_QAT_SW_SM3
int res = 1;
EVP_MD *qat_sw_sm3_meth = NULL;
if ((qat_sw_sm3_meth = EVP_MD_meth_new(nid, key_type)) == NULL) {
WARN("Failed to allocate digest methods for nid %d\n", nid);
return NULL;
}
if (qat_sw_offload &&
(qat_sw_algo_enable_mask & ALGO_ENABLE_MASK_SM3)) {
/* For now check using MBX_ALGO_X25519 as algo info for sm3 is not implemented */
if (mbx_get_algo_info(MBX_ALGO_X25519)) {
res = qat_sw_sm3_md_methods(qat_sw_sm3_meth);
}
if (0 == res) {
WARN("Failed to set MD methods for nid %d\n", nid);
EVP_MD_meth_free(qat_sw_sm3_meth);
return NULL;
}
qat_sw_sm3_offload = 1;
DEBUG("QAT SW SM3 Registration succeeded\n");
return qat_sw_sm3_meth;
} else {
qat_sw_sm3_offload = 0;
DEBUG("QAT SW SM3 is disabled, using OpenSSL SW\n");
# if defined(QAT_OPENSSL_3) && !defined(QAT_OPENSSL_PROVIDER)
qat_openssl3_sm3_fallback = 1;
res = qat_sw_sm3_md_methods(qat_sw_sm3_meth);
if (0 == res) {
WARN("Failed to set MD methods for nid %d\n", nid);
EVP_MD_meth_free(qat_sw_sm3_meth);
return NULL;
}
return qat_sw_sm3_meth;
# else
return (EVP_MD *)EVP_sm3();
# endif
}
#else
qat_sw_sm3_offload = 0;
DEBUG("QAT SW SM3 is disabled, using OpenSSL SW\n");
# ifdef OPENSSL_NO_SM2_SM3
return NULL;
# else
return (EVP_MD *)EVP_sm3();
# endif
#endif
}
/******************************************************************************
* function:
* qat_create_digest_meth(void)
*
* description:
* Creates qat EVP MD methods.
******************************************************************************/
void qat_create_digest_meth(void)
{
int i;
/* free the old method while algorithm reload */
if (qat_reload_algo)
qat_free_digest_meth();
for (i = 0; i < num_digest_nids; i++) {
if (digest_info[i].md == NULL) {
switch (digest_info[i].m_type) {
#ifdef ENABLE_QAT_HW_SHA3
# ifdef QAT_INSECURE_ALGO
case NID_sha3_224:
# endif
case NID_sha3_256:
case NID_sha3_384:
case NID_sha3_512:
digest_info[i].md = (EVP_MD *)
qat_create_sha3_meth(digest_info[i].m_type , digest_info[i].pkey_type);
break;
#endif
#ifdef ENABLE_QAT_SW_SM3
case NID_sm3:
digest_info[i].md = (EVP_MD *)
qat_sw_create_sm3_meth(digest_info[i].m_type , digest_info[i].pkey_type);
break;
#endif
#ifdef ENABLE_QAT_HW_SM3
case NID_sm3:
digest_info[i].md = (EVP_MD *)
qat_hw_create_sm3_meth(digest_info[i].m_type , digest_info[i].pkey_type);
break;
#endif
default:
break;
}
}
}
}
void qat_free_digest_meth(void)
{
int i;
for (i = 0; i < num_digest_nids; i++) {
if (digest_info[i].md != NULL) {
switch (digest_info[i].m_type) {
#ifdef ENABLE_QAT_HW_SHA3
# ifdef QAT_INSECURE_ALGO
case NID_sha3_224:
# endif
case NID_sha3_256:
case NID_sha3_384:
case NID_sha3_512:
if (qat_hw_sha_offload)
EVP_MD_meth_free(digest_info[i].md);
break;
#endif
#ifdef ENABLE_QAT_SW_SM3
case NID_sm3:
if (qat_sw_sm3_offload)
EVP_MD_meth_free(digest_info[i].md);
break;
#endif
#ifdef ENABLE_QAT_HW_SM3
case NID_sm3:
if (qat_hw_sm3_offload)
EVP_MD_meth_free(digest_info[i].md);
break;
#endif
}
digest_info[i].md = NULL;
}
}
qat_hw_sha_offload = 0;
qat_sw_sm3_offload = 0;
qat_hw_sm3_offload = 0;
}
/******************************************************************************
* function:
* qat_digest_methods(ENGINE *e,
* const EVP_MD **md,
* const int **nids,
* int nid)
*
* @param e [IN] - OpenSSL engine pointer
* @param pmeth [IN] - EVP methods structure pointer
* @param nids [IN] - EVP function nids
* @param nid [IN] - EVP operation id
*
* description:
* QAT engine digest operations register.
******************************************************************************/
int qat_digest_methods(ENGINE *e, const EVP_MD **md,
const int **nids, int nid)
{
int i;
if (md == NULL) {
if (unlikely(nids == NULL)) {
WARN("Invalid input params.\n");
return 0;
}
*nids = qat_digest_nids;
return num_digest_nids;
}
for (i = 0; i < num_digest_nids; i++) {
if (nid == qat_digest_nids[i]) {
if (digest_info[i].md == NULL)
qat_create_digest_meth();
*md = digest_info[i].md;
return 1;
}
}
WARN("NID %d not supported\n", nid);
*md = NULL;
return 0;
}
#ifdef QAT_OPENSSL_3
/* The following 3 functions are only used for
* TLSv1.3 with OpenSSL 3 Engine API.
*/
int qat_ecx_paramgen_init(EVP_PKEY_CTX *ctx)
{
return 1;
}
int qat_ecx25519_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
{
if (pkey == NULL) {
pkey = EVP_PKEY_new();
if (pkey == NULL) {
WARN("Couldn't allocate pkey.\n");
return -1;
}
}
return EVP_PKEY_set_type(pkey, NID_X25519);
}
int qat_ecx448_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
{
if (pkey == NULL) {
pkey = EVP_PKEY_new();
if (pkey == NULL) {
WARN("Couldn't allocate pkey.\n");
return -1;
}
}
return EVP_PKEY_set_type(pkey, NID_X448);
}
#endif
#ifdef ENABLE_QAT_SW_ECX
static void qat_ecx25519_pkey_methods(void)
{
EVP_PKEY_meth_set_keygen(_hidden_x25519_pmeth, NULL, multibuff_x25519_keygen);
EVP_PKEY_meth_set_derive(_hidden_x25519_pmeth, NULL, multibuff_x25519_derive);
# ifdef QAT_OPENSSL_3
EVP_PKEY_meth_set_paramgen(_hidden_x25519_pmeth, qat_ecx_paramgen_init,
qat_ecx25519_paramgen);
# endif /* QAT_OPENSSL_3 */
# ifndef QAT_OPENSSL_PROVIDER
EVP_PKEY_meth_set_ctrl(_hidden_x25519_pmeth, multibuff_x25519_ctrl, NULL);
# endif
}
#endif
EVP_PKEY_METHOD *qat_x25519_pmeth(void)
{
if (_hidden_x25519_pmeth) {
if (!qat_reload_algo)
return _hidden_x25519_pmeth;
EVP_PKEY_meth_free(_hidden_x25519_pmeth);
_hidden_x25519_pmeth = NULL;
}
if ((_hidden_x25519_pmeth =
EVP_PKEY_meth_new(EVP_PKEY_X25519, 0)) == NULL) {
QATerr(QAT_F_QAT_X25519_PMETH, QAT_R_ALLOC_QAT_X25519_METH_FAILURE);
return NULL;
}
/* Now save the current (non-offloaded) x25519 pmeth to sw_x25519_pmeth */
/* for software fallback purposes */
if ((sw_x25519_pmeth = EVP_PKEY_meth_find(EVP_PKEY_X25519)) == NULL) {
QATerr(QAT_F_QAT_X25519_PMETH, ERR_R_INTERNAL_ERROR);
return NULL;
}
#ifdef ENABLE_QAT_HW_ECX
if (qat_hw_offload && (qat_hw_algo_enable_mask & ALGO_ENABLE_MASK_ECX25519)) {
EVP_PKEY_meth_set_keygen(_hidden_x25519_pmeth, NULL, qat_pkey_ecx25519_keygen);
EVP_PKEY_meth_set_derive(_hidden_x25519_pmeth, NULL, qat_pkey_ecx_derive25519);
# ifdef QAT_OPENSSL_3
EVP_PKEY_meth_set_paramgen(_hidden_x25519_pmeth, qat_ecx_paramgen_init,
qat_ecx25519_paramgen);
# endif /* QAT_OPENSSL_3 */
# ifndef QAT_OPENSSL_PROVIDER
EVP_PKEY_meth_set_ctrl(_hidden_x25519_pmeth, qat_pkey_ecx_ctrl, NULL);
# endif
qat_hw_ecx_offload = 1;
DEBUG("QAT HW X25519 registration succeeded\n");
# ifdef ENABLE_QAT_SW_ECX
if (qat_sw_offload &&
(qat_sw_algo_enable_mask & ALGO_ENABLE_MASK_ECX25519) &&
mbx_get_algo_info(MBX_ALGO_X25519)) {
qat_ecx_coexist = 1;
DEBUG("QAT ECX25519 HW&SW Coexistence is enabled \n");
}
# endif
} else {
qat_hw_ecx_offload = 0;
DEBUG("QAT HW X25519 is disabled\n");
}
#endif
#ifdef ENABLE_QAT_SW_ECX
if (qat_sw_offload && !qat_hw_ecx_offload &&
(qat_sw_algo_enable_mask & ALGO_ENABLE_MASK_ECX25519) &&
mbx_get_algo_info(MBX_ALGO_X25519)) {
qat_ecx25519_pkey_methods();
qat_sw_ecx_offload = 1;
DEBUG("QAT SW X25519 registration succeeded\n");
} else {
qat_sw_ecx_offload = 0;
DEBUG("QAT SW X25519 disabled\n");
}
# if defined(QAT_OPENSSL_3) && !defined(QAT_OPENSSL_PROVIDER)
if (!qat_sw_offload) {
fallback_to_openssl = 1;
qat_ecx25519_pkey_methods();
return _hidden_x25519_pmeth;
}
# endif
#endif
if (qat_hw_ecx_offload == 0 && qat_sw_ecx_offload == 0)
EVP_PKEY_meth_copy(_hidden_x25519_pmeth, sw_x25519_pmeth);
return _hidden_x25519_pmeth;
}
EVP_PKEY_METHOD *qat_x448_pmeth(void)
{
if (_hidden_x448_pmeth) {
if (!qat_reload_algo)
return _hidden_x448_pmeth;
EVP_PKEY_meth_free(_hidden_x448_pmeth);
_hidden_x448_pmeth = NULL;
}
if ((_hidden_x448_pmeth =
EVP_PKEY_meth_new(EVP_PKEY_X448, 0)) == NULL) {
QATerr(QAT_F_QAT_X448_PMETH, QAT_R_ALLOC_QAT_X448_METH_FAILURE);
return NULL;
}
/* Now save the current (non-offloaded) x448 pmeth to sw_x448_pmeth */
/* for software fallback purposes */
if ((sw_x448_pmeth = EVP_PKEY_meth_find(EVP_PKEY_X448)) == NULL) {
QATerr(QAT_F_QAT_X448_PMETH, ERR_R_INTERNAL_ERROR);
return NULL;
}
#ifdef ENABLE_QAT_HW_ECX
if (qat_hw_offload && (qat_hw_algo_enable_mask & ALGO_ENABLE_MASK_ECX448)) {
EVP_PKEY_meth_set_keygen(_hidden_x448_pmeth, NULL, qat_pkey_ecx448_keygen);
EVP_PKEY_meth_set_derive(_hidden_x448_pmeth, NULL, qat_pkey_ecx_derive448);
# ifdef QAT_OPENSSL_3
EVP_PKEY_meth_set_paramgen(_hidden_x448_pmeth, qat_ecx_paramgen_init, qat_ecx448_paramgen);
# endif /* QAT_OPENSSL_3 */
# ifndef QAT_OPENSSL_PROVIDER
EVP_PKEY_meth_set_ctrl(_hidden_x448_pmeth, qat_pkey_ecx_ctrl, NULL);
# endif
qat_hw_ecx_offload = 1;
DEBUG("QAT HW ECDH X448 Registration succeeded\n");
} else {
qat_hw_ecx_offload = 0;
}
#else
qat_hw_ecx_offload = 0;
#endif
if (!qat_hw_ecx_offload) {
EVP_PKEY_meth_copy(_hidden_x448_pmeth, sw_x448_pmeth);
DEBUG("QAT HW ECDH X448 is disabled, using OpenSSL SW\n");
}
return _hidden_x448_pmeth;
}
/******************************************************************************
* function:
* qat_create_pkey_meth(int nid)
*
* @param nid [IN] - EVP operation id
*
* description:
* Creates qat EVP Pkey methods for the nid
******************************************************************************/
static EVP_PKEY_METHOD *qat_create_pkey_meth(int nid)
{
switch (nid) {
#ifdef ENABLE_QAT_HW_PRF
case EVP_PKEY_TLS1_PRF:
return qat_prf_pmeth();
#endif
#ifdef ENABLE_QAT_HW_HKDF
case EVP_PKEY_HKDF:
return qat_hkdf_pmeth();
#endif
#if defined(ENABLE_QAT_HW_ECX) || defined(ENABLE_QAT_SW_ECX)
case EVP_PKEY_X25519:
return qat_x25519_pmeth();
#endif
#ifdef ENABLE_QAT_HW_ECX
case EVP_PKEY_X448:
return qat_x448_pmeth();
#endif
#if defined(ENABLE_QAT_HW_SM2) || defined(ENABLE_QAT_SW_SM2)
case EVP_PKEY_SM2:
return qat_create_sm2_pmeth();
#endif
default:
WARN("Invalid nid %d\n", nid);
return NULL;
}
}
/******************************************************************************
* function:
* qat_pkey_methods(ENGINE *e,
* const EVP_PKEY_METHOD **pmeth,
* const int **nids,
* int nid)
*
* @param e [IN] - OpenSSL engine pointer
* @param pmeth [IN] - EVP methods structure pointer
* @param nids [IN] - EVP functions nids
* @param nid [IN] - EVP operation id
*
* description:
* QAT engine digest operations registrar
******************************************************************************/
int qat_pkey_methods(ENGINE *e, EVP_PKEY_METHOD **pmeth,
const int **nids, int nid)
{
int i = 0;
if (pmeth == NULL) {
if (unlikely(nids == NULL)) {
WARN("Invalid input params.\n");
return 0;
}
*nids = qat_evp_nids;
return num_evp_nids;
}
for (i = 0; i < num_evp_nids; i++) {
if (nid == qat_evp_nids[i]) {
*pmeth = qat_create_pkey_meth(nid);
return 1;
}
}
WARN("NID %d not supported\n", nid);
*pmeth = NULL;
return 0;
}
static inline const EVP_CIPHER *qat_gcm_cipher_sw_impl(int nid)
{
switch (nid) {
case NID_aes_128_gcm:
return EVP_aes_128_gcm();
case NID_aes_192_gcm:
return EVP_aes_192_gcm();
case NID_aes_256_gcm:
return EVP_aes_256_gcm();
default:
WARN("Invalid nid %d\n", nid);
return NULL;
}
}
#ifdef ENABLE_QAT_HW_CCM
const EVP_CIPHER *qat_ccm_cipher_sw_impl(int nid)
{
switch (nid) {
case NID_aes_128_ccm:
return EVP_aes_128_ccm();
case NID_aes_192_ccm:
return EVP_aes_192_ccm();
case NID_aes_256_ccm:
return EVP_aes_256_ccm();
default:
WARN("Invalid nid %d\n", nid);
return NULL;
}
}
#endif
/******************************************************************************
* function:
* qat_create_gcm_cipher_meth(int nid, int keylen)
*
* @param nid [IN] - Cipher NID to be created
* @param keylen [IN] - Key length of cipher [128|192|256]
* @retval - EVP_CIPHER * to created cipher
* @retval - NULL if failure
*
* description:
* Create a new EVP_CIPHER based on requested nid for qat_hw or qat_sw
******************************************************************************/
const EVP_CIPHER *qat_create_gcm_cipher_meth(int nid, int keylen)
{
EVP_CIPHER *c = NULL;
#if defined(ENABLE_QAT_SW_GCM) || defined(ENABLE_QAT_HW_GCM)
int res = 1;
#endif
if ((c = EVP_CIPHER_meth_new(nid, AES_GCM_BLOCK_SIZE, keylen)) == NULL) {
WARN("Failed to allocate cipher methods for nid %d\n", nid);
return NULL;
}
#ifdef ENABLE_QAT_SW_GCM
if (qat_sw_offload && (qat_sw_algo_enable_mask & ALGO_ENABLE_MASK_AES_GCM)) {
res &= EVP_CIPHER_meth_set_iv_length(c, IMB_GCM_IV_DATA_LEN);
res &= EVP_CIPHER_meth_set_flags(c, VAESGCM_FLAG);
#ifndef QAT_OPENSSL_PROVIDER
res &= EVP_CIPHER_meth_set_init(c, vaesgcm_ciphers_init);
res &= EVP_CIPHER_meth_set_do_cipher(c, vaesgcm_ciphers_do_cipher);
res &= EVP_CIPHER_meth_set_cleanup(c, vaesgcm_ciphers_cleanup);
#endif
res &= EVP_CIPHER_meth_set_impl_ctx_size(c, sizeof(vaesgcm_ctx));
res &= EVP_CIPHER_meth_set_set_asn1_params(c, NULL);
res &= EVP_CIPHER_meth_set_get_asn1_params(c, NULL);
#ifndef QAT_OPENSSL_PROVIDER
res &= EVP_CIPHER_meth_set_ctrl(c, vaesgcm_ciphers_ctrl);
#endif
if (0 == res) {
WARN("Failed to set cipher methods for nid %d\n", nid);
EVP_CIPHER_meth_free(c);
return NULL;
}
qat_sw_gcm_offload = 1;
DEBUG("QAT SW AES_GCM_%d registration succeeded\n", keylen*8);
} else {
qat_sw_gcm_offload = 0;
DEBUG("QAT SW AES_GCM_%d is disabled\n", keylen*8);
}
#endif
#ifdef ENABLE_QAT_HW_GCM
if (!qat_sw_gcm_offload && qat_hw_offload &&
(qat_hw_algo_enable_mask & ALGO_ENABLE_MASK_AES_GCM)) {
if (nid == NID_aes_192_gcm) {
EVP_CIPHER_meth_free(c);
DEBUG("OpenSSL SW AES_GCM_%d registration succeeded\n", keylen*8);
return qat_gcm_cipher_sw_impl(nid);
}
res &= EVP_CIPHER_meth_set_iv_length(c, AES_GCM_IV_LEN);
res &= EVP_CIPHER_meth_set_flags(c, QAT_GCM_FLAGS);
#ifndef QAT_OPENSSL_PROVIDER
res &= EVP_CIPHER_meth_set_init(c, qat_aes_gcm_init);
res &= EVP_CIPHER_meth_set_do_cipher(c, qat_aes_gcm_cipher);
res &= EVP_CIPHER_meth_set_cleanup(c, qat_aes_gcm_cleanup);
#endif
res &= EVP_CIPHER_meth_set_impl_ctx_size(c, sizeof(qat_gcm_ctx));
res &= EVP_CIPHER_meth_set_set_asn1_params(c, EVP_CIPH_FLAG_DEFAULT_ASN1 ?
NULL : EVP_CIPHER_set_asn1_iv);
res &= EVP_CIPHER_meth_set_get_asn1_params(c, EVP_CIPH_FLAG_DEFAULT_ASN1 ?
NULL : EVP_CIPHER_get_asn1_iv);
#ifndef QAT_OPENSSL_PROVIDER
res &= EVP_CIPHER_meth_set_ctrl(c, qat_aes_gcm_ctrl);
#endif
if (0 == res) {
WARN("Failed to set cipher methods for nid %d\n", nid);
EVP_CIPHER_meth_free(c);
return NULL;
}
qat_hw_gcm_offload = 1;
DEBUG("QAT HW AES_GCM_%d registration succeeded\n", keylen*8);
} else {
qat_hw_gcm_offload = 0;
DEBUG("QAT HW AES_GCM_%d is disabled\n", keylen*8);
}
#endif
if (!qat_sw_gcm_offload && !qat_hw_gcm_offload) {
DEBUG("OpenSSL SW AES_GCM_%d registration succeeded\n", keylen*8);
EVP_CIPHER_meth_free(c);
return qat_gcm_cipher_sw_impl(nid);
}
return c;
}
/******************************************************************************
* function:
* qat_create_ccm_cipher_meth(int nid, int keylen)
*
* @param nid [IN] - Cipher NID to be created
* @param keylen [IN] - Key length of cipher [128|192|256]
* @retval - EVP_CIPHER * to created cipher
* @retval - NULL if failure
*
* description:
* Create a new EVP_CIPHER based on requested nid for qat_hw
******************************************************************************/
#ifdef ENABLE_QAT_HW_CCM
const EVP_CIPHER *qat_create_ccm_cipher_meth(int nid, int keylen)
{
EVP_CIPHER *c = NULL;
int res = 1;
if ((c = EVP_CIPHER_meth_new(nid, AES_CCM_BLOCK_SIZE, keylen)) == NULL) {
WARN("Failed to allocate cipher methods for nid %d\n", nid);
return NULL;
}
if (qat_hw_offload &&
(qat_hw_algo_enable_mask & ALGO_ENABLE_MASK_AES_CCM)) {
#if !defined(QAT20_OOT) && !defined(QAT_HW_INTREE)
if (nid == NID_aes_192_ccm || nid == NID_aes_256_ccm) {
EVP_CIPHER_meth_free(c);
DEBUG("OpenSSL SW AES_CCM_%d registration succeeded\n", keylen*8);
return qat_ccm_cipher_sw_impl(nid);
}
#endif
res &= EVP_CIPHER_meth_set_iv_length(c, AES_CCM_IV_LEN);
res &= EVP_CIPHER_meth_set_flags(c, QAT_CCM_FLAGS);
#ifndef QAT_OPENSSL_PROVIDER
res &= EVP_CIPHER_meth_set_init(c, qat_aes_ccm_init);
res &= EVP_CIPHER_meth_set_do_cipher(c, qat_aes_ccm_cipher);
res &= EVP_CIPHER_meth_set_cleanup(c, qat_aes_ccm_cleanup);
#endif
res &= EVP_CIPHER_meth_set_impl_ctx_size(c, sizeof(qat_ccm_ctx));
res &= EVP_CIPHER_meth_set_set_asn1_params(c, EVP_CIPH_FLAG_DEFAULT_ASN1 ?
NULL : EVP_CIPHER_set_asn1_iv);
res &= EVP_CIPHER_meth_set_get_asn1_params(c, EVP_CIPH_FLAG_DEFAULT_ASN1 ?
NULL : EVP_CIPHER_get_asn1_iv);
#ifndef QAT_OPENSSL_PROVIDER
res &= EVP_CIPHER_meth_set_ctrl(c, qat_aes_ccm_ctrl);
#endif
if (0 == res) {
WARN("Failed to set cipher methods for nid %d\n", nid);
EVP_CIPHER_meth_free(c);
return NULL;
}
qat_hw_aes_ccm_offload = 1;
DEBUG("QAT HW AES_CCM_%d registration succeeded\n", keylen*8);
} else {
qat_hw_aes_ccm_offload = 0;
DEBUG("QAT HW AES_CCM_%d is disabled\n", keylen*8);
}
if (!qat_hw_aes_ccm_offload) {
DEBUG("OpenSSL SW AES_CCM_%d registration succeeded\n", keylen*8);
EVP_CIPHER_meth_free(c);
return qat_ccm_cipher_sw_impl(nid);
}
return c;
}
#endif
/******************************************************************************
* function:
* qat_create_sm4_cbc_cipher_meth(void)
*
* @param nid [IN] - Cipher NID to be created
* @param keylen [IN] - Key length of cipher [128|192|256]
* @retval - EVP_CIPHER * to created cipher
* @retval - EVP_CIPHER * EVP_sm4_cbc
* @retval - NULL if failure
*
* description:
* Create a new EVP_CIPHER based on requested nid for qat_hw or qat_sw
******************************************************************************/
const EVP_CIPHER *qat_create_sm4_cbc_cipher_meth(int nid, int keylen)
{
EVP_CIPHER *c = NULL;
#if defined(ENABLE_QAT_SW_SM4_CBC) || defined(ENABLE_QAT_HW_SM4_CBC)
int res = 1;
#endif