-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathqat_hw_ec.c
2384 lines (2153 loc) · 83.3 KB
/
qat_hw_ec.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) 2016-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_hw_ec.c
*
* This file provides support for ECDH & ECDSA
*
*****************************************************************************/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <pthread.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <signal.h>
#include <openssl/ecdh.h>
#ifdef QAT_BORINGSSL
#include <openssl/ecdsa.h>
#include <openssl/bytestring.h>
#endif /* QAT_BORINGSSL */
#include <openssl/err.h>
#include <openssl/ec.h>
#include <openssl/obj_mac.h>
#include <openssl/bn.h>
#include <openssl/rand.h>
#include "cpa.h"
#include "cpa_types.h"
#ifdef QAT_BORINGSSL
#include "icp_sal_poll.h"
#endif
#include "cpa_cy_ec.h"
#include "cpa_cy_ecdsa.h"
#ifdef ENABLE_QAT_HW_SM2
# include "cpa_cy_ecsm2.h"
#endif
#include "e_qat.h"
#include "qat_hw_callback.h"
#include "qat_hw_polling.h"
#include "qat_events.h"
#include "qat_hw_asym_common.h"
#include "qat_utils.h"
#include "qat_hw_ec.h"
#include "qat_evp.h"
#if defined(ENABLE_QAT_SW_ECDH) || defined(ENABLE_QAT_SW_ECDSA)
# include "qat_sw_ec.h"
#endif
# define QAT_EC_MIN_RANGE 256
#ifdef ENABLE_QAT_FIPS
# include "qat_prov_cmvp.h"
# ifdef ENABLE_QAT_HW_ECDSA
extern int qat_fips_kat_test;
static const unsigned char kvalue[] = {
0x23, 0xAF, 0x40, 0x74, 0xC9, 0x0A, 0x02, 0xB3,
0xE6, 0x1D, 0x28, 0x6D, 0x5C, 0x87, 0xF4, 0x25,
0xE6, 0xBD, 0xD8, 0x1B
};
# endif
#endif
CpaCyEcFieldType qat_get_field_type(const EC_GROUP *group)
{
/* For BoringSSL,EC_METHOD_get_field_type only support NID_X9_62_prime_field
* and EC_METHOD_get_field_type return NID_X9_62_prime_field directly
*/
if (EC_METHOD_get_field_type(EC_GROUP_method_of(group))
== NID_X9_62_prime_field)
return CPA_CY_EC_FIELD_TYPE_PRIME;
else
return CPA_CY_EC_FIELD_TYPE_BINARY;
}
#if defined(QAT20_OOT) || defined(QAT_HW_INTREE)
int qat_get_curve(CpaCyEcFieldType fieldType)
{
if (fieldType == CPA_CY_EC_FIELD_TYPE_PRIME)
return CPA_CY_EC_CURVE_TYPE_WEIERSTRASS_PRIME;
else if (fieldType == CPA_CY_EC_FIELD_TYPE_BINARY)
return CPA_CY_EC_CURVE_TYPE_WEIERSTRASS_BINARY;
else
return CPA_CY_EC_CURVE_TYPE_WEIERSTRASS_KOBLITZ_BINARY;
}
#endif
#ifdef ENABLE_QAT_HW_ECDH
/* Callback to indicate QAT completion of EC point multiply */
static void qat_ecCallbackFn(void *pCallbackTag, CpaStatus status, void *pOpData,
CpaBoolean multiplyStatus, CpaFlatBuffer * pXk,
CpaFlatBuffer * pYk)
{
if (enable_heuristic_polling) {
QAT_ATOMIC_DEC(num_asym_requests_in_flight);
}
qat_crypto_callbackFn(pCallbackTag, status, CPA_CY_SYM_OP_CIPHER, pOpData,
NULL, multiplyStatus);
}
int qat_ecdh_compute_key(unsigned char **outX, size_t *outlenX,
unsigned char **outY, size_t *outlenY,
const EC_POINT *pub_key, const EC_KEY *ecdh,
int *fallback)
{
BN_CTX *ctx = NULL;
BIGNUM *p = NULL, *a = NULL, *b = NULL, *h = NULL;
const BIGNUM *priv_key = NULL;
const EC_GROUP *group = NULL;
int ret = 0, job_ret = 0;
size_t buflen = 0;
int inst_num = QAT_INVALID_INSTANCE;
BIGNUM *xP = NULL, *yP = NULL;
CpaBoolean bEcStatus = 0;
CpaFlatBuffer *pResultX = NULL;
CpaFlatBuffer *pResultY = NULL;
int qatPerformOpRetries = 0;
useconds_t ulPollInterval = getQatPollInterval();
int iMsgRetry = getQatMsgRetryCount();
CpaStatus status = CPA_STATUS_FAIL;
op_done_t op_done;
thread_local_variables_t *tlv = NULL;
int curve_name;
int sm2_flag = 0;
ret = sm2_flag;
int qat_svm = QAT_INSTANCE_ANY;
DEBUG("QAT HW ECDH Started\n");
#ifdef ENABLE_QAT_FIPS
qat_fips_get_approved_status();
#endif
START_RDTSC(&qat_hw_ecdh_derive_req_prepare);
if (unlikely(ecdh == NULL ||
((priv_key = EC_KEY_get0_private_key(ecdh)) == NULL)
|| pub_key == NULL)) {
WARN("Either ecdh or priv_key or pub_key is NULL\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_ECDH_PRIV_KEY_PUB_KEY_NULL);
return ret;
}
if (fallback == NULL) {
WARN("NULL fallback pointer passed in.\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_FALLBACK_POINTER_NULL);
return ret;
}
if ((outX != NULL && outlenX == NULL) ||
(outY != NULL && outlenY == NULL)) {
WARN("Either outX, outY, outlenX or outlenY are NULL\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_OUTX_OUTY_LEN_NULL);
return ret;
}
if ((group = EC_KEY_get0_group(ecdh)) == NULL) {
WARN("group is NULL\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_GET_GROUP_FAILURE);
return ret;
}
if ((inst_num = get_instance(QAT_INSTANCE_ASYM, QAT_INSTANCE_ANY))
== QAT_INVALID_INSTANCE) {
WARN("Failed to get an instance\n");
if (qat_get_sw_fallback_enabled()) {
CRYPTO_QAT_LOG("Failed to get an instance - fallback to SW - %s\n", __func__);
*fallback = 1;
return ret;
} else {
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
return ret;
}
}
qat_svm = !qat_instance_details[inst_num].qat_instance_info.requiresPhysicallyContiguousMemory;
curve_name = EC_GROUP_get_curve_name(group);
# if defined(QAT20_OOT) || defined(QAT_HW_INTREE)
# ifdef ENABLE_QAT_HW_SM2
CpaCyEcsm2PointMultiplyOpData *psm2OpData = NULL;
# endif
CpaCyEcGenericPointMultiplyOpData *pOpData = NULL;
# else
CpaCyEcPointMultiplyOpData *opData = NULL;
# endif
# if defined(QAT20_OOT) || defined(QAT_HW_INTREE)
# ifdef ENABLE_QAT_HW_SM2
if (curve_name == NID_sm2 || curve_name == NID_sm3) {
psm2OpData = (CpaCyEcsm2PointMultiplyOpData *)
OPENSSL_zalloc(sizeof(CpaCyEcsm2PointMultiplyOpData));
if (psm2OpData == NULL) {
WARN("Failure to allocate psm2OpData\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_POPDATA_MALLOC_FAILURE);
return ret;
}
sm2_flag = 1;
psm2OpData->k.pData = NULL;
psm2OpData->x.pData = NULL;
psm2OpData->y.pData = NULL;
}
# endif
if (!sm2_flag) {
pOpData = (CpaCyEcGenericPointMultiplyOpData *)
OPENSSL_zalloc(sizeof(CpaCyEcGenericPointMultiplyOpData));
if (pOpData == NULL) {
WARN("Failure to allocate pOpData\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_POPDATA_MALLOC_FAILURE);
return ret;
}
pOpData->pCurve = (CpaCyEcCurve *) qat_mem_alloc(sizeof(CpaCyEcCurve),
qat_svm, __FILE__, __LINE__);
if (pOpData->pCurve == NULL) {
WARN("Failure to allocate pOpData->pCurve\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_POPDATA_PCURVE_MALLOC_FAILURE);
OPENSSL_free(pOpData);
return ret;
}
if (outY != NULL)
pOpData->generator = CPA_TRUE;
else
pOpData->generator = CPA_FALSE;
pOpData->k.pData = NULL;
pOpData->xP.pData = NULL;
pOpData->yP.pData = NULL;
pOpData->pCurve->parameters.weierstrassParameters.a.pData = NULL;
pOpData->pCurve->parameters.weierstrassParameters.b.pData = NULL;
pOpData->pCurve->parameters.weierstrassParameters.p.pData = NULL;
pOpData->pCurve->parameters.weierstrassParameters.h.pData = NULL;
pOpData->pCurve->parameters.weierstrassParameters.h.dataLenInBytes = 0;
}
# else
opData = (CpaCyEcPointMultiplyOpData *)
OPENSSL_zalloc(sizeof(CpaCyEcPointMultiplyOpData));
if (opData == NULL) {
WARN("Failure to allocate opData\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_OPDATA_MALLOC_FAILURE);
return ret;
}
opData->k.pData = NULL;
opData->xg.pData = NULL;
opData->yg.pData = NULL;
opData->a.pData = NULL;
opData->b.pData = NULL;
opData->q.pData = NULL;
opData->h.pData = NULL;
opData->h.dataLenInBytes = 0;
# endif
/* Populate the parameters required for EC point multiply */
if ((ctx = BN_CTX_new()) == NULL) {
WARN("Failure to allocate ctx\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_CTX_MALLOC_FAILURE);
goto err;
}
BN_CTX_start(ctx);
p = BN_CTX_get(ctx);
a = BN_CTX_get(ctx);
b = BN_CTX_get(ctx);
h = BN_CTX_get(ctx);
xP = BN_CTX_get(ctx);
yP = BN_CTX_get(ctx);
if (yP == NULL) {
WARN("Failed to allocate p, a, b, xP or yP\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_P_A_B_XP_YP_MALLOC_FAILURE);
goto err;
}
buflen = (EC_GROUP_get_degree(group) + 7) / 8;
pResultX = (CpaFlatBuffer *) OPENSSL_zalloc(sizeof(CpaFlatBuffer));
if (pResultX == NULL) {
WARN("Failure to allocate pResultX\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_PRESULTX_MALLOC_FAILURE);
goto err;
}
if (qat_svm && outX != NULL) {
*outlenX = (Cpa32U) buflen;
*outX = OPENSSL_zalloc(*outlenX);
pResultX->pData = (Cpa8U*) *outX;
} else {
pResultX->pData = qat_mem_alloc(buflen, qat_svm, __FILE__, __LINE__);
}
if (pResultX->pData == NULL) {
WARN("Failure to allocate pResultX->pData\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_PRESULTX_PDATA_MALLOC_FAILURE);
goto err;
}
pResultX->dataLenInBytes = (Cpa32U) buflen;
pResultY = (CpaFlatBuffer *) OPENSSL_zalloc(sizeof(CpaFlatBuffer));
if (!pResultY) {
WARN("Failure to allocate pResultY\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_PRESULTY_MALLOC_FAILURE);
goto err;
}
if (qat_svm && outY != NULL) {
*outY = OPENSSL_zalloc(*outlenY);
pResultY->pData = (Cpa8U*) *outY;
} else {
pResultY->pData = qat_mem_alloc(buflen, qat_svm, __FILE__, __LINE__);
}
if (pResultY->pData == NULL) {
WARN("Failure to allocate pResultY->pData\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_PRESULTY_PDATA_MALLOC_FAILURE);
goto err;
}
pResultY->dataLenInBytes = (Cpa32U) buflen;
if (!EC_GROUP_get_cofactor(group, h, ctx)) {
WARN("Failure in get cofactor\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_GET_COFACTOR_FAILURE);
goto err;
}
if (!EC_GROUP_get_curve(group, p, a, b, ctx)) {
WARN("Failure to get the curve\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
goto err;
}
if (!EC_POINT_get_affine_coordinates(group, pub_key, xP, yP, ctx)) {
WARN("Failure to get the affine coordinates\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
goto err;
}
# if defined(QAT20_OOT) || defined(QAT_HW_INTREE)
# ifdef ENABLE_QAT_HW_SM2
if (curve_name == NID_sm2 || curve_name == NID_sm3) {
psm2OpData->fieldType = qat_get_field_type(group);
if ((qat_BN_to_FB(&(psm2OpData->k), (BIGNUM *)priv_key, qat_svm) != 1) ||
(qat_BN_to_FB(&(psm2OpData->x), xP, qat_svm) != 1) ||
(qat_BN_to_FB(&(psm2OpData->y), yP, qat_svm) != 1)) {
WARN("Failure to convert priv_key, x, y to flatbuffer\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_PRIV_KEY_XG_YG_A_B_P_CONVERT_TO_FB_FAILURE);
goto err;
}
}
# endif
if (!sm2_flag) {
pOpData->pCurve->parameters.weierstrassParameters.fieldType = qat_get_field_type(group);
pOpData->pCurve->curveType = qat_get_curve(pOpData->pCurve->parameters.weierstrassParameters.fieldType);
if ((qat_BN_to_FB(&(pOpData->k), (BIGNUM *)priv_key, qat_svm) != 1) ||
(qat_BN_to_FB(&(pOpData->xP), xP, qat_svm) != 1) ||
(qat_BN_to_FB(&(pOpData->yP), yP, qat_svm) != 1) ||
(qat_BN_to_FB(&(pOpData->pCurve->parameters.weierstrassParameters.a), a, qat_svm) != 1) ||
(qat_BN_to_FB(&(pOpData->pCurve->parameters.weierstrassParameters.b), b, qat_svm) != 1) ||
(qat_BN_to_FB(&(pOpData->pCurve->parameters.weierstrassParameters.p), p, qat_svm) != 1)) {
WARN("Failure to convert priv_key, xP, yP, a, b or p to flatbuffer\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_PRIV_KEY_XP_YP_A_B_P_CONVERT_TO_FB_FAILURE);
goto err;
}
/* Pass Co-factor to Opdata */
if (pOpData->pCurve->parameters.weierstrassParameters.fieldType
== CPA_CY_EC_FIELD_TYPE_PRIME) {
if (qat_BN_to_FB(&(pOpData->pCurve->parameters.weierstrassParameters.h), h, qat_svm) != 1) {
WARN("Failure to convert h to flatbuffer\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_H_CONVERT_TO_FB_FAILURE);
goto err;
}
}
/*
* This is a special handling required for curves with 'a' co-efficient
* of 0. The translation to a flatbuffer results in a zero sized field
* but the Quickassist API expects a flatbuffer of size 1 with a value
* of zero. As a special case we will create that manually.
*/
if (pOpData->pCurve->parameters.weierstrassParameters.a.pData == NULL &&
pOpData->pCurve->parameters.weierstrassParameters.a.dataLenInBytes == 0) {
pOpData->pCurve->parameters.weierstrassParameters.a.pData =
qat_mem_alloc(1, qat_svm, __FILE__, __LINE__);
if (pOpData->pCurve->parameters.weierstrassParameters.a.pData == NULL) {
WARN("Failure to allocate pOpData->pCurve->parameters.weierstrassParameters.a.pData\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_POPDATA_A_PDATA_MALLOC_FAILURE);
goto err;
}
pOpData->pCurve->parameters.weierstrassParameters.a.dataLenInBytes = 1;
pOpData->pCurve->parameters.weierstrassParameters.a.pData[0] = 0;
}
}
# else
opData->fieldType = qat_get_field_type(group);
if ((qat_BN_to_FB(&(opData->k), (BIGNUM *)priv_key, qat_svm) != 1) ||
(qat_BN_to_FB(&(opData->xg), xP, qat_svm) != 1) ||
(qat_BN_to_FB(&(opData->yg), yP, qat_svm) != 1) ||
(qat_BN_to_FB(&(opData->a), a, qat_svm) != 1) ||
(qat_BN_to_FB(&(opData->b), b, qat_svm) != 1) ||
(qat_BN_to_FB(&(opData->q), p, qat_svm) != 1)) {
WARN("Failure to convert priv_key, xg, yg, a, b or p to flatbuffer\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_PRIV_KEY_XG_YG_A_B_P_CONVERT_TO_FB_FAILURE);
goto err;
}
/* Pass Co-factor to Opdata */
if (opData->fieldType == CPA_CY_EC_FIELD_TYPE_PRIME) {
if (qat_BN_to_FB(&(opData->h), h, qat_svm) != 1) {
WARN("Failure to convert h to flatbuffer\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_H_CONVERT_TO_FB_FAILURE);
goto err;
}
}
/*
* This is a special handling required for curves with 'a' co-efficient
* of 0. The translation to a flatbuffer results in a zero sized field
* but the Quickassist API expects a flatbuffer of size 1 with a value
* of zero. As a special case we will create that manually.
*/
if (opData->a.pData == NULL && opData->a.dataLenInBytes == 0) {
opData->a.pData = qat_mem_alloc(1, qat_svm,__FILE__, __LINE__);
if (opData->a.pData == NULL) {
WARN("Failure to allocate opData->a.pData\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_OPDATA_A_PDATA_MALLOC_FAILURE);
goto err;
}
opData->a.dataLenInBytes = 1;
opData->a.pData[0] = 0;
}
# endif
tlv = qat_check_create_local_variables();
if (NULL == tlv) {
WARN("could not create local variables\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
goto err;
}
#ifdef QAT_BORINGSSL
qat_init_op_done(&op_done, qat_svm);
#else
qat_init_op_done(&op_done);
#endif
if (op_done.job != NULL) {
if (qat_setup_async_event_notification(op_done.job) == 0) {
WARN("Failed to setup async event notification\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
qat_cleanup_op_done(&op_done);
goto err;
}
}
CRYPTO_QAT_LOG("KX - %s\n", __func__);
STOP_RDTSC(&qat_hw_ecdh_derive_req_prepare, 1, "[QAT HW ECDH: prepare]");
/* Invoke the crypto engine API for EC Point Multiply */
do {
START_RDTSC(&qat_hw_ecdh_derive_req_submit);
if (status == CPA_STATUS_RETRY &&
(inst_num = get_instance(QAT_INSTANCE_ASYM, qat_svm)) == QAT_INVALID_INSTANCE) {
WARN("Failed to get an instance\n");
if (qat_get_sw_fallback_enabled()) {
CRYPTO_QAT_LOG("Failed to get an instance - fallback to SW - %s\n", __func__);
*fallback = 1;
} else {
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
}
if (op_done.job != NULL)
qat_clear_async_event_notification(op_done.job);
qat_cleanup_op_done(&op_done);
goto err;
}
CRYPTO_QAT_LOG("KX - %s\n", __func__);
# if defined(QAT20_OOT) || defined(QAT_HW_INTREE)
# ifdef ENABLE_QAT_HW_SM2
if (curve_name == NID_sm2 || curve_name == NID_sm3) {
DUMP_EC_SM2_POINT_MULTIPLY(qat_instance_handles[inst_num], psm2OpData, pResultX, pResultY);
status = cpaCyEcsm2PointMultiply(qat_instance_handles[inst_num],
qat_ecCallbackFn,
&op_done,
psm2OpData,
&bEcStatus, pResultX, pResultY);
}
# endif
if (!sm2_flag) {
DUMP_EC_GENERIC_POINT_MULTIPLY(qat_instance_handles[inst_num], pOpData, pResultX, pResultY);
status = cpaCyEcGenericPointMultiply(qat_instance_handles[inst_num],
qat_ecCallbackFn,
&op_done,
pOpData,
&bEcStatus, pResultX, pResultY);
}
# else
DUMP_EC_POINT_MULTIPLY(qat_instance_handles[inst_num], opData, pResultX, pResultY);
status = cpaCyEcPointMultiply(qat_instance_handles[inst_num],
qat_ecCallbackFn,
&op_done,
opData,
&bEcStatus, pResultX, pResultY);
# endif
STOP_RDTSC(&qat_hw_ecdh_derive_req_submit, 1, "[QAT HW ECDH: submit]");
if (status == CPA_STATUS_RETRY) {
if (qat_ecdh_coexist &&
((curve_name == NID_X9_62_prime256v1) ||
(curve_name == NID_secp384r1))) {
START_RDTSC(&qat_hw_ecdh_derive_req_retry);
if (op_done.job) {
DEBUG("cpaCyEcPointMultiply Retry \n");
if (outY) { /* key generation */
++num_ecdh_keygen_retry;
qat_sw_ecdh_keygen_req += QAT_SW_SWITCH_MB8;
} else { /* compute key */
++num_ecdh_derive_retry;
qat_sw_ecdh_derive_req += QAT_SW_SWITCH_MB8;
}
*fallback = 1;
qat_cleanup_op_done(&op_done);
STOP_RDTSC(&qat_hw_ecdh_derive_req_retry, 1, "[QAT HW ECDH: retry]");
goto err;
}
} else {
if (op_done.job == NULL) {
usleep(ulPollInterval +
(qatPerformOpRetries %
QAT_RETRY_BACKOFF_MODULO_DIVISOR));
qatPerformOpRetries++;
if (iMsgRetry != QAT_INFINITE_MAX_NUM_RETRIES) {
if (qatPerformOpRetries >= iMsgRetry) {
WARN("No. of retries exceeded max retry : %d\n", iMsgRetry);
break;
}
}
} else {
if ((qat_wake_job(op_done.job, ASYNC_STATUS_EAGAIN) == 0) ||
(qat_pause_job(op_done.job, ASYNC_STATUS_EAGAIN) == 0)) {
WARN("qat_wake_job or qat_pause_job failed\n");
break;
}
}
}
}
}
while (status == CPA_STATUS_RETRY );
if (status != CPA_STATUS_SUCCESS) {
WARN("Failed to submit request to qat - status = %d\n", status);
if (qat_get_sw_fallback_enabled() &&
(status == CPA_STATUS_RESTARTING || status == CPA_STATUS_FAIL)) {
CRYPTO_QAT_LOG("Failed to submit request to qat inst_num %d device_id %d - fallback to SW - %s\n",
inst_num,
qat_instance_details[inst_num].qat_instance_info.physInstId.packageId,
__func__);
*fallback = 1;
} else if (status == CPA_STATUS_UNSUPPORTED) {
WARN("Algorithm Unsupported in QAT_HW! Using OpenSSL SW\n");
*fallback = 1;
} else {
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
}
if (op_done.job != NULL) {
qat_clear_async_event_notification(op_done.job);
}
qat_cleanup_op_done(&op_done);
goto err;
}
QAT_INC_IN_FLIGHT_REQS(num_requests_in_flight, tlv);
if (qat_use_signals()) {
if (tlv->localOpsInFlight == 1) {
if (sem_post(&hw_polling_thread_sem) != 0) {
WARN("hw sem_post failed!, hw_polling_thread_sem address: %p.\n",
&hw_polling_thread_sem);
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
QAT_DEC_IN_FLIGHT_REQS(num_requests_in_flight, tlv);
goto err;
}
}
}
if (qat_get_sw_fallback_enabled()) {
CRYPTO_QAT_LOG("Submit success qat inst_num %d device_id %d - %s\n",
inst_num,
qat_instance_details[inst_num].qat_instance_info.physInstId.packageId,
__func__);
}
if (enable_heuristic_polling) {
QAT_ATOMIC_INC(num_asym_requests_in_flight);
}
if (qat_ecdh_coexist) {
if (outY)
++num_ecdh_hw_keygen_reqs;
else
++num_ecdh_hw_derive_reqs;
}
do {
if(op_done.job != NULL) {
/* If we get a failure on qat_pause_job then we will
not flag an error here and quit because we have
an asynchronous request in flight.
We don't want to start cleaning up data
structures that are still being used. If
qat_pause_job fails we will just yield and
loop around and try again until the request
completes and we can continue. */
if ((job_ret = qat_pause_job(op_done.job, ASYNC_STATUS_OK)) == 0)
sched_yield();
} else {
sched_yield();
}
}
while (!op_done.flag ||
QAT_CHK_JOB_RESUMED_UNEXPECTEDLY(job_ret));
DUMP_EC_POINT_MULTIPLY_OUTPUT(bEcStatus, pResultX, pResultY);
QAT_DEC_IN_FLIGHT_REQS(num_requests_in_flight, tlv);
if (op_done.verifyResult != CPA_TRUE) {
WARN("Verification of request failed\n");
if (qat_get_sw_fallback_enabled() && op_done.status == CPA_STATUS_FAIL) {
CRYPTO_QAT_LOG("Verification of result failed for qat inst_num %d device_id %d - fallback to SW - %s\n",
inst_num,
qat_instance_details[inst_num].qat_instance_info.physInstId.packageId,
__func__);
*fallback = 1;
} else {
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
}
qat_cleanup_op_done(&op_done);
goto err;
}
qat_cleanup_op_done(&op_done);
/* KDF, is done in the caller now just copy out bytes */
if (outX != NULL && !qat_svm) {
*outlenX = pResultX->dataLenInBytes;
*outX = OPENSSL_zalloc(*outlenX);
if (*outX == NULL) {
WARN("Failure to allocate outX\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_OUTX_MALLOC_FAILURE);
goto err;
}
if (unlikely(pResultX->pData == NULL)) {
WARN("pResultX->pData is NULL\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
goto err;
}
memcpy(*outX, pResultX->pData, *outlenX);
}
if (outY != NULL && !qat_svm) {
if (*outlenY != pResultY->dataLenInBytes) {
WARN("Failed length check of pResultY->dataLenInBytes\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_PRESULTY_LENGTH_CHECK_FAILURE);
goto err;
}
*outY = OPENSSL_zalloc(*outlenY);
if (*outY == NULL) {
WARN("Failure to allocate outY\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, QAT_R_OUTY_MALLOC_FAILURE);
goto err;
}
if (unlikely(pResultY->pData == NULL)) {
WARN("pResultY->pData is NULL\n");
QATerr(QAT_F_QAT_ECDH_COMPUTE_KEY, ERR_R_INTERNAL_ERROR);
goto err;
}
memcpy(*outY, pResultY->pData, pResultY->dataLenInBytes);
}
ret = *outlenX;
err:
START_RDTSC(&qat_hw_ecdh_derive_req_cleanup);
if (pResultX) {
if (!qat_svm)
qaeCryptoMemFree(pResultX->pData);
OPENSSL_free(pResultX);
}
if (pResultY) {
if (!qat_svm)
qaeCryptoMemFree(pResultY->pData);
OPENSSL_free(pResultY);
}
# if defined(QAT20_OOT) || defined(QAT_HW_INTREE)
# ifdef ENABLE_QAT_HW_SM2
if (curve_name == NID_sm2 || curve_name == NID_sm3) {
if (psm2OpData) {
QAT_CLEANSE_MEMFREE_NONZERO_FLATBUFF(psm2OpData->k, qat_svm);
QAT_MEM_FREE_FLATBUFF(psm2OpData->x, qat_svm);
QAT_MEM_FREE_FLATBUFF(psm2OpData->y, qat_svm);
OPENSSL_free(psm2OpData);
}
}
# endif
if (!sm2_flag) {
if (pOpData) {
QAT_CLEANSE_MEMFREE_NONZERO_FLATBUFF(pOpData->k, qat_svm);
QAT_MEM_FREE_FLATBUFF(pOpData->xP, qat_svm);
QAT_MEM_FREE_FLATBUFF(pOpData->yP, qat_svm);
QAT_MEM_FREE_FLATBUFF(pOpData->pCurve->parameters.weierstrassParameters.a, qat_svm);
QAT_MEM_FREE_FLATBUFF(pOpData->pCurve->parameters.weierstrassParameters.b, qat_svm);
QAT_MEM_FREE_FLATBUFF(pOpData->pCurve->parameters.weierstrassParameters.p, qat_svm);
QAT_MEM_FREE_FLATBUFF(pOpData->pCurve->parameters.weierstrassParameters.h, qat_svm);
QAT_MEM_FREE_BUFF(pOpData->pCurve, qat_svm);
OPENSSL_free(pOpData);
}
}
# else
if (opData) {
QAT_CLEANSE_MEMFREE_NONZERO_FLATBUFF(opData->k, qat_svm);
QAT_MEM_FREE_FLATBUFF(opData->xg, qat_svm);
QAT_MEM_FREE_FLATBUFF(opData->yg, qat_svm);
QAT_MEM_FREE_FLATBUFF(opData->a, qat_svm);
QAT_MEM_FREE_FLATBUFF(opData->b, qat_svm);
QAT_MEM_FREE_FLATBUFF(opData->q, qat_svm);
QAT_MEM_FREE_FLATBUFF(opData->h, qat_svm);
OPENSSL_free(opData);
}
# endif
if (ctx) {
BN_CTX_end(ctx);
BN_CTX_free(ctx);
}
STOP_RDTSC(&qat_hw_ecdh_derive_req_cleanup, 1, "[QAT HW ECDH: cleanup]");
DEBUG("- Finished\n");
return ret;
}
int qat_engine_ecdh_compute_key(unsigned char **out,
size_t *outlen,
const EC_POINT *pub_key,
const EC_KEY *ecdh)
{
int fallback = 0, ret = 0;
PFUNC_COMP_KEY comp_key_pfunc = NULL;
const EC_GROUP *group = NULL;
#ifndef QAT_INSECURE_ALGO
int bitlen = 0;
#endif
DEBUG("QAT HW ECDH Started\n");
EC_KEY_METHOD_get_compute_key((EC_KEY_METHOD *)EC_KEY_OpenSSL(), &comp_key_pfunc);
if (comp_key_pfunc == NULL) {
WARN("comp_key_pfunc is NULL\n");
QATerr(QAT_F_QAT_ENGINE_ECDH_COMPUTE_KEY, QAT_R_SW_GET_COMPUTE_KEY_PFUNC_NULL);
return ret;
}
if (ecdh == NULL || (EC_KEY_get0_private_key(ecdh) == NULL)) {
WARN("Either ecdh or priv_key is NULL\n");
QATerr(QAT_F_QAT_ENGINE_ECDH_COMPUTE_KEY, QAT_R_ECDH_PRIVATE_KEY_NULL);
return ret;
}
if ((group = EC_KEY_get0_group(ecdh)) == NULL) {
WARN("group is NULL\n");
QATerr(QAT_F_QAT_ENGINE_ECDH_COMPUTE_KEY, QAT_R_GET_GROUP_FAILURE);
return ret;
}
if (qat_sw_ecdh_derive_req > 0 || qat_get_qat_offload_disabled()) {
fallback = 1;
goto exit;
}
#ifndef QAT_INSECURE_ALGO
/* Bits < 256 is not supported in QAT_HW */
bitlen = EC_GROUP_order_bits(group);
if (bitlen < QAT_EC_MIN_RANGE) {
DEBUG("Curve-Bitlen %d not supported! Using OPENSSL_SW\n", bitlen);
fallback = 1;
goto exit;
}
#endif
ret = qat_ecdh_compute_key(out, outlen, NULL, NULL, pub_key, ecdh, &fallback);
exit:
if (fallback == 1) {
CRYPTO_QAT_LOG("Resubmitting request to SW - %s\n", __func__);
#ifdef ENABLE_QAT_SW_ECDH
int curve_name = EC_GROUP_get_curve_name(group);
if (qat_ecdh_coexist &&
((curve_name == NID_X9_62_prime256v1) ||
(curve_name == NID_secp384r1))) {
DEBUG("- Switched to QAT_SW mode\n");
if (qat_sw_ecdh_derive_req > 0)
--qat_sw_ecdh_derive_req;
return mb_ecdh_compute_key(out, outlen, pub_key, ecdh);
}
#endif
WARN("- Fallback to software mode.\n");
return (*comp_key_pfunc)(out, outlen, pub_key, ecdh);
}
DEBUG("- Finished\n");
return ret;
}
int qat_ecdh_generate_key(EC_KEY *ecdh)
{
int ok = 0, alloc_priv = 0, alloc_pub = 0;
int field_size = 0, fallback = 0;
BN_CTX *ctx = NULL;
BIGNUM *priv_key = NULL, *order = NULL, *x_bn = NULL,
*y_bn = NULL, *tx_bn = NULL, *ty_bn = NULL;
EC_POINT *pub_key = NULL;
const EC_POINT *gen;
const EC_GROUP *group = NULL;
unsigned char *temp_xbuf = NULL;
unsigned char *temp_ybuf = NULL;
size_t temp_xfield_size = 0;
size_t temp_yfield_size = 0;
PFUNC_GEN_KEY gen_key_pfunc = NULL;
#ifndef QAT_INSECURE_ALGO
int bitlen = 0;
#endif
DEBUG("QAT HW ECDH Started\n");
EC_KEY_METHOD_get_keygen((EC_KEY_METHOD *) EC_KEY_OpenSSL(), &gen_key_pfunc);
if (gen_key_pfunc == NULL) {
WARN("get keygen failed\n");
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_SW_GET_KEYGEN_PFUNC_NULL);
return 0;
}
if (unlikely(ecdh == NULL || ((group = EC_KEY_get0_group(ecdh)) == NULL))) {
WARN("Either ecdh or group are NULL\n");
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_ECDH_GROUP_NULL);
return 0;
}
if (qat_sw_ecdh_keygen_req > 0 || qat_get_qat_offload_disabled()) {
fallback = 1;
goto exit;
}
#ifndef QAT_INSECURE_ALGO
/* Bits < 256 is not supported in QAT_HW */
bitlen = EC_GROUP_order_bits(group);
if (bitlen < QAT_EC_MIN_RANGE) {
DEBUG("Curve-Bitlen %d not supported! Using OPENSSL_SW\n", bitlen);
fallback = 1;
goto exit;
}
#endif
if ((ctx = BN_CTX_new()) == NULL) {
WARN("Failure to allocate ctx\n");
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_CTX_MALLOC_FAILURE);
goto err;
}
BN_CTX_start(ctx);
if ((order = BN_CTX_get(ctx)) == NULL) {
WARN("Failure to allocate order\n");
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_ORDER_MALLOC_FAILURE);
goto err;
}
if ((priv_key = (BIGNUM *)EC_KEY_get0_private_key(ecdh)) == NULL) {
priv_key = BN_new();
if (priv_key == NULL) {
WARN("Failure to get priv_key\n");
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_GET_PRIV_KEY_FAILURE);
goto err;
}
alloc_priv = 1;
}
if (!EC_GROUP_get_order(group, order, ctx)) {
WARN("Failure to retrieve order\n");
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_RETRIEVE_ORDER_FAILURE);
goto err;
}
do
if (!BN_rand_range(priv_key, order)) {
WARN("Failure to generate random value\n");
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_PRIV_KEY_RAND_GENERATE_FAILURE);
goto err;
}
while (BN_is_zero(priv_key)) ;
if (alloc_priv) {
if (!EC_KEY_set_private_key(ecdh, priv_key)) {
WARN("Failure to set private key\n");
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_SET_PRIV_KEY_FAILURE);
goto err;
}
}
if ((pub_key = (EC_POINT *)EC_KEY_get0_public_key(ecdh)) == NULL) {
pub_key = EC_POINT_new(group);
if (pub_key == NULL) {
WARN("Failure to allocate pub_key\n");
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_PUB_KEY_MALLOC_FAILURE);
goto err;
}
alloc_pub = 1;
}
field_size = EC_GROUP_get_degree(group);
if (field_size <= 0) {
WARN("invalid field_size: %d\n", field_size);
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_FIELD_SIZE_INVALID);
goto err;
}
gen = EC_GROUP_get0_generator(group);
temp_xfield_size = temp_yfield_size = (field_size + 7) / 8;
if ((qat_ecdh_compute_key(&temp_xbuf,
&temp_xfield_size,
&temp_ybuf,
&temp_yfield_size,
gen,
ecdh,
&fallback) <= 0)
|| (fallback == 1)) {
/*
* No QATerr is raised here because errors are already handled in
* qat_ecdh_compute_key()
*/
goto err;
}
x_bn = BN_CTX_get(ctx);
y_bn = BN_CTX_get(ctx);
tx_bn = BN_CTX_get(ctx);
ty_bn = BN_CTX_get(ctx);
if (ty_bn == NULL) {
WARN("Failure to allocate ctx x_bn, y_bn, tx_bn or ty_bn\n");
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_X_Y_TX_TY_BN_MALLOC_FAILURE);
goto err;
}
x_bn = BN_bin2bn(temp_xbuf, temp_xfield_size, x_bn);
y_bn = BN_bin2bn(temp_ybuf, temp_yfield_size, y_bn);
if (!EC_POINT_set_affine_coordinates(group, pub_key, x_bn, y_bn, ctx)) {
WARN("Failure to get the affine coordinates\n");
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_ECDH_SET_AFFINE_COORD_FAILED);
goto err;
}
if (!EC_POINT_get_affine_coordinates(group, pub_key, tx_bn, ty_bn, ctx)) {
WARN("Failure to get the affine coordinates\n");
QATerr(QAT_F_QAT_ECDH_GENERATE_KEY, QAT_R_ECDH_GET_AFFINE_COORD_FAILED);
goto err;
}