forked from intel/QAT_Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qat_rsa.c
1181 lines (1044 loc) · 40 KB
/
qat_rsa.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 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_rsa.c
*
* This file contains the engine implementations for RSA operations
*
*****************************************************************************/
/* macros defined to allow use of the cpu get and set affinity functions */
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#define __USE_GNU
#include <pthread.h>
#include <openssl/rsa.h>
#include <openssl/async.h>
#include <openssl/err.h>
#include <string.h>
#include <unistd.h>
#ifdef USE_QAT_CONTIG_MEM
# include "qae_mem_utils.h"
#endif
#ifdef USE_QAE_MEM
# include "cmn_mem_drv_inf.h"
#endif
#include "qat_utils.h"
#include "e_qat.h"
#include "cpa.h"
#include "cpa_types.h"
#include "cpa_cy_rsa.h"
#include "qat_rsa.h"
#include "qat_asym_common.h"
#include "e_qat_err.h"
#ifdef OPENSSL_ENABLE_QAT_RSA
# ifdef OPENSSL_DISABLE_QAT_RSA
# undef OPENSSL_DISABLE_QAT_RSA
# endif
#endif
/* To specify the RSA op sizes supported by QAT engine */
#define RSA_QAT_RANGE_MIN 512
#define RSA_QAT_RANGE_MAX 4096
/* Qat engine RSA methods declaration */
static int qat_rsa_priv_enc(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding);
static int qat_rsa_priv_dec(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding);
static int qat_rsa_pub_enc(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding);
static int qat_rsa_pub_dec(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding);
static int qat_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
static int qat_bn_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
/* Callback to indicate QAT completion of RSA. */
void qat_rsaCallbackFn(void *pCallbackTag,
CpaStatus status, void *pOpData, CpaFlatBuffer * pOut);
static RSA_METHOD *qat_rsa_method = NULL;
RSA_METHOD *qat_get_RSA_methods(void)
{
if (qat_rsa_method != NULL)
return qat_rsa_method;
#ifndef OPENSSL_DISABLE_QAT_RSA
if ((qat_rsa_method = RSA_meth_new("QAT RSA method", 0)) == NULL
|| RSA_meth_set_pub_enc(qat_rsa_method, qat_rsa_pub_enc) == 0
|| RSA_meth_set_pub_dec(qat_rsa_method, qat_rsa_pub_dec) == 0
|| RSA_meth_set_priv_enc(qat_rsa_method, qat_rsa_priv_enc) == 0
|| RSA_meth_set_priv_dec(qat_rsa_method, qat_rsa_priv_dec) == 0
|| RSA_meth_set_mod_exp(qat_rsa_method, qat_rsa_mod_exp) == 0
|| RSA_meth_set_bn_mod_exp(qat_rsa_method, qat_bn_mod_exp) == 0) {
QATerr(QAT_F_QAT_GET_RSA_METHODS, ERR_R_INTERNAL_ERROR);
return NULL;
}
#else
qat_rsa_method = (RSA_METHOD *)RSA_get_default_method();
#endif
return qat_rsa_method;
}
void qat_free_RSA_methods(void)
{
#ifndef OPENSSL_DISABLE_QAT_RSA
if (qat_rsa_method != NULL) {
RSA_meth_free(qat_rsa_method);
qat_rsa_method = NULL;
} else {
QATerr(QAT_F_QAT_FREE_RSA_METHODS, ERR_R_INTERNAL_ERROR);
}
#endif
}
/*
* The RSA range check is performed so that if the op sizes are not in the
* range supported by QAT engine then fall back to software
*/
static inline int qat_rsa_range_check(int plen)
{
return ((plen >= RSA_QAT_RANGE_MIN) && (plen <= RSA_QAT_RANGE_MAX));
}
/******************************************************************************
* function:
* qat_alloc_pad(unsigned char *in,
* int len,
* int rLen,
* int sign)
*
* @param in [IN] - pointer to Flat Buffer
* @param len [IN] - length of input data (hash)
* @param rLen [IN] - length of RSA
* @param sign [IN] - 1 for sign operation and 0 for decryption
*
* description:
* This function is used to add PKCS#1 padding into input data buffer
* before it pass to cpaCyRsaDecrypt() function.
* The function returns a pointer to unsigned char buffer
******************************************************************************/
static unsigned char *qat_alloc_pad(unsigned char *in, int len,
int rLen, int sign)
{
int i = 0;
/* out data buffer should have fix length */
unsigned char *out = qaeCryptoMemAlloc(rLen, __FILE__, __LINE__);
if (NULL == out) {
WARN("[%s] --- out buffer malloc failed.\n", __func__);
return NULL;
}
/* First two char are (0x00, 0x01) or (0x00, 0x02) */
out[0] = 0x00;
if (sign) {
out[1] = 0x01;
} else {
out[1] = 0x02;
}
/*
* Fill 0xff and end up with 0x00 in out buffer until the length of
* actual data space left
*/
for (i = 2; i < (rLen - len - 1); i++) {
out[i] = 0xff;
}
/*
* i has been incremented on beyond the last padding byte to exit for
* loop
*/
out[i] = 0x00;
/* shift actual data to the end of out buffer */
memcpy((out + rLen - len), in, len);
return out;
}
/******************************************************************************
* function:
* qat_data_len(unsigned char *in
* int rLen, int sign)
*
* @param in [IN] - pointer to Flat Buffer
* @param rLen [IN] - length of RSA
* @param sign [IN] - 1 for sign operation and 0 for decryption
*
* description:
* This function is used to calculate the length of actual data
* and padding size inside of outputBuffer returned from cpaCyRsaEncrypt() function.
* The function counts the padding length (i) and return the length
* of actual data (dLen) contained in the outputBuffer
******************************************************************************/
static int qat_data_len(unsigned char *in, int rLen, int sign)
{
/* first two bytes are 0x00, 0x01 */
int i = 0;
int dLen = 0;
int pLen = 0;
/* First two char of padding should be 0x00, 0x01 */
if (sign) {
/* First two char of padding should be 0x00, 0x01 */
if (in[0] != 0x00 || in[1] != 0x01) {
WARN("[%s] --- [%d] Padding format unknown!\n", __func__, sign);
return 0;
}
} else {
/* First two char of padding should be 0x00, 0x02 for decryption */
if (in[0] != 0x00 || in[1] != 0x02) {
WARN("[%s] --- [%d] Padding format unknown!\n", __func__, sign);
return 0;
}
}
/*
* while loop is design to reach the 0x00 value and count all the 0xFF
* value where filled by PKCS#1 padding
*/
while (in[i + 2] != 0x00 && i < rLen)
i++;
/* padding length = 2 + length of 0xFF + 0x00 */
pLen = 2 + i + 1;
dLen = rLen - pLen;
if (dLen < 0) dLen = 0;
return dLen;
}
/******************************************************************************
* function:
* qat_remove_pad(unsigned char *out
* unsigned char *in,
* int r_len,
* int out_len,
* int sign,
* int padding)
*
* @param out [OUT] - pointer to a Flat Buffer
* @param in [IN] - pointer to a Flat Buffer
* @param r_len [IN] - length of the RSA data
* @param out_len [OUT] - length of the output data after the padding has
been removed
* @param sign [IN] - 1 for sign operation and 0 for decryption
* @param padding [IN] - type of padding
*
* description:
* This function is used to remove PKCS#1 padding from outputBuffer
* after cpaCyRsaEncrypt() function during RSA verify if that is the type of
* padding passed in. However, if the type of padding is RSA_NO_PADDING, then
* all the data in the 'in' buffer is copied to the 'out' buffer.
******************************************************************************/
static int qat_remove_pad(unsigned char *out, unsigned char *in,
int r_len, int *out_len, int sign, int padding)
{
int p_len = 0;
int d_len = 0;
if (padding == RSA_NO_PADDING) {
memcpy(out, in, r_len);
*out_len = r_len;
}
else { /* should be RSA_PKCS1_PADDING */
if (0 == (d_len = qat_data_len(in, r_len, sign)))
return 0;
p_len = r_len - d_len;
/* shift actual data to the beginning of out buffer */
memcpy(out, in + p_len, d_len);
*out_len = d_len;
}
return 1;
}
/******************************************************************************
* function:
* qat_rsaCallbackFn(void *pCallbackTag, CpaStatus status,
* void *pOpData, CpaFlatBuffer * pOut)
*
* @param pCallbackTag [IN] - Opaque User Data for this specific call. Will
* be returned unchanged in the callback.
* @param status [IN] - Status result of the RSA operation.
* @param pOpData [IN] - Structure containing all the data needed to
* perform the RSA encryption operation.
* @param pOut [IN] - Pointer to buffer into which the result of
* the RSA encryption is written.
* description:
* Callback function used by RSA operations to indicate completion.
* Calls back to qat_crypto_callbackFn() as functionally it does the same.
*
******************************************************************************/
void qat_rsaCallbackFn(void *pCallbackTag, CpaStatus status, void *pOpData,
CpaFlatBuffer * pOut)
{
qat_crypto_callbackFn(pCallbackTag, status, CPA_CY_SYM_OP_CIPHER, pOpData,
NULL, CPA_TRUE);
}
typedef struct rsa_op_data {
CpaCyRsaPublicKey *cpa_pub_key;
CpaCyRsaDecryptOpData *dec_op_data;
CpaCyRsaEncryptOpData *enc_op_data;
CpaFlatBuffer *output_buffer;
int rsa_len;
int padding;
unsigned char *cb_output;
int (*cb_func) (unsigned char *res, size_t reslen,
void *cb_data, int status);
void *cb_data;
} rsa_op_data_t;
#define NO_PADDING 0
#define PADDING 1
static void
rsa_decrypt_op_buf_free(CpaCyRsaDecryptOpData * dec_op_data,
CpaFlatBuffer * out_buf, int padding)
{
CpaCyRsaPrivateKeyRep2 *key = NULL;
if (dec_op_data) {
if (dec_op_data->inputData.pData)
qaeCryptoMemFree(dec_op_data->inputData.pData);
if (dec_op_data->pRecipientPrivateKey) {
key = &dec_op_data->pRecipientPrivateKey->privateKeyRep2;
QAT_CHK_CLNSE_QMFREE_FLATBUFF(key->prime1P);
QAT_CHK_CLNSE_QMFREE_FLATBUFF(key->prime2Q);
QAT_CHK_CLNSE_QMFREE_FLATBUFF(key->exponent1Dp);
QAT_CHK_CLNSE_QMFREE_FLATBUFF(key->exponent2Dq);
QAT_CHK_CLNSE_QMFREE_FLATBUFF(key->coefficientQInv);
OPENSSL_free(dec_op_data->pRecipientPrivateKey);
}
OPENSSL_free(dec_op_data);
}
if (out_buf) {
if (out_buf->pData)
qaeCryptoMemFree(out_buf->pData);
OPENSSL_free(out_buf);
}
}
int
qat_rsa_decrypt(CpaCyRsaDecryptOpData * dec_op_data,
CpaFlatBuffer * output_buf)
{
/* Used for RSA Decrypt and RSA Sign */
struct op_done op_done;
CpaStatus sts = CPA_STATUS_FAIL;
int qatPerformOpRetries = 0;
CpaInstanceHandle instanceHandle = NULL;
int iMsgRetry = getQatMsgRetryCount();
useconds_t ulPollInterval = getQatPollInterval();
initOpDone(&op_done);
if (op_done.job) {
if (qat_setup_async_event_notification(0) == 0) {
QATerr(QAT_F_QAT_RSA_DECRYPT, ERR_R_INTERNAL_ERROR);
cleanupOpDone(&op_done);
return 0;
}
}
/*
* cpaCyRsaDecrypt() is the function called for RSA verify in API, the
* DecOpData [IN] contains both private key value and input file (hash)
* value, the outputBuffer [OUT] stores the signature as the output
* message, the sts value return 0 if successful
*/
CRYPTO_QAT_LOG("RSA - %s\n", __func__);
do {
if (NULL == (instanceHandle = get_next_inst())) {
WARN("instanceHandle is NULL\n");
QATerr(QAT_F_QAT_RSA_DECRYPT, ERR_R_INTERNAL_ERROR);
cleanupOpDone(&op_done);
return 0;
}
sts = cpaCyRsaDecrypt(instanceHandle, qat_rsaCallbackFn, &op_done,
dec_op_data, output_buf);
if (sts == CPA_STATUS_RETRY) {
if (op_done.job == NULL) {
usleep(ulPollInterval +
(qatPerformOpRetries % QAT_RETRY_BACKOFF_MODULO_DIVISOR));
qatPerformOpRetries++;
if (iMsgRetry != QAT_INFINITE_MAX_NUM_RETRIES) {
if (qatPerformOpRetries >= iMsgRetry) {
break;
}
}
} else {
if ((qat_wake_job(op_done.job, 0) == 0) ||
(qat_pause_job(op_done.job, 0) == 0)) {
sts = CPA_STATUS_FAIL;
break;
}
}
}
}
while (sts == CPA_STATUS_RETRY);
if (sts != CPA_STATUS_SUCCESS) {
fprintf(stderr,"[%s] --- cpaCyRsaDecrypt failed, sts=%d.\n", __func__, sts);
QATerr(QAT_F_QAT_RSA_DECRYPT, ERR_R_INTERNAL_ERROR);
cleanupOpDone(&op_done);
return 0;
}
do {
if(op_done.job) {
/* 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 (qat_pause_job(op_done.job, 0) == 0)
pthread_yield();
} else {
pthread_yield();
}
}
while (!op_done.flag);
cleanupOpDone(&op_done);
if (op_done.verifyResult != CPA_TRUE) {
fprintf(stderr,"[%s] --- cpaCyRsaDecrypt failed, verifyResult=%d.\n",
__func__, op_done.verifyResult);
QATerr(QAT_F_QAT_RSA_DECRYPT, ERR_R_INTERNAL_ERROR);
return 0;
}
return 1;
}
static int
build_decrypt_op_buf(int flen, const unsigned char *from, unsigned char *to,
RSA *rsa, int padding,
CpaCyRsaDecryptOpData ** dec_op_data,
CpaFlatBuffer ** output_buffer, int alloc_pad)
{
int rsa_len = 0;
CpaCyRsaPrivateKey *cpa_prv_key = NULL;
const BIGNUM *p = NULL;
const BIGNUM *q = NULL;
const BIGNUM *dmp1 = NULL;
const BIGNUM *dmq1 = NULL;
const BIGNUM *iqmp = NULL;
RSA_get0_factors((const RSA*)rsa, &p, &q);
RSA_get0_crt_params((const RSA*)rsa, &dmp1, &dmq1, &iqmp);
if (p == NULL || q == NULL || dmp1 == NULL || dmq1 == NULL || iqmp == NULL) {
WARN("[%s] --- RSA factors or crt params are NULL!\n", __func__);
QATerr(QAT_F_BUILD_DECRYPT_OP_BUF, ERR_R_INTERNAL_ERROR);
return 0;
}
/* Padding check */
if ((padding != RSA_NO_PADDING) && (padding != RSA_PKCS1_PADDING)) {
DEBUG("[%s] --- Unknown Padding!\n", __func__);
QATerr(QAT_F_BUILD_DECRYPT_OP_BUF, ERR_R_INTERNAL_ERROR);
return 0;
}
cpa_prv_key =
(CpaCyRsaPrivateKey *) OPENSSL_zalloc(sizeof(CpaCyRsaPrivateKey));
if (NULL == cpa_prv_key) {
WARN("[%s] --- Private Key zalloc failed!\n", __func__);
QATerr(QAT_F_BUILD_DECRYPT_OP_BUF, ERR_R_MALLOC_FAILURE);
return 0;
}
DEBUG("[%s] --- flen =%d, padding = %d \n", __func__, flen, padding);
/* output signature should have same length as RSA(128) */
rsa_len = RSA_size(rsa);
/* output and input data MUST allocate memory for sign process */
/* memory allocation for DecOpdata[IN] */
*dec_op_data = OPENSSL_zalloc(sizeof(CpaCyRsaDecryptOpData));
if (NULL == *dec_op_data) {
WARN("[%s] --- OpData zalloc failed!\n", __func__);
QATerr(QAT_F_BUILD_DECRYPT_OP_BUF, ERR_R_MALLOC_FAILURE);
OPENSSL_free(cpa_prv_key);
return 0;
}
/* Setup the DecOpData structure */
(*dec_op_data)->pRecipientPrivateKey = cpa_prv_key;
cpa_prv_key->version = CPA_CY_RSA_VERSION_TWO_PRIME;
/* Setup the private key rep type 2 structure */
cpa_prv_key->privateKeyRepType = CPA_CY_RSA_PRIVATE_KEY_REP_TYPE_2;
if (qat_BN_to_FB(&cpa_prv_key->privateKeyRep2.prime1P, p) != 1 ||
qat_BN_to_FB(&cpa_prv_key->privateKeyRep2.prime2Q, q) != 1 ||
qat_BN_to_FB(&cpa_prv_key->privateKeyRep2.exponent1Dp, dmp1) != 1
|| qat_BN_to_FB(&cpa_prv_key->privateKeyRep2.exponent2Dq,
dmq1) != 1
|| qat_BN_to_FB(&cpa_prv_key->privateKeyRep2.coefficientQInv,
iqmp) != 1) {
WARN("[%s] --- qat_BN_to_FB failed for privateKeyRep2 elements\n",
__func__);
QATerr(QAT_F_BUILD_DECRYPT_OP_BUF, ERR_R_INTERNAL_ERROR);
return 0;
}
if (alloc_pad) {
(*dec_op_data)->inputData.pData =
qat_alloc_pad((Cpa8U *) from, flen, rsa_len, 1);
} else {
(*dec_op_data)->inputData.pData =
(Cpa8U *) copyAllocPinnedMemory((void *)from, flen, __FILE__,
__LINE__);
}
if (NULL == (*dec_op_data)->inputData.pData) {
WARN("[%s] --- InputData malloc failed!\n", __func__);
QATerr(QAT_F_BUILD_DECRYPT_OP_BUF, ERR_R_MALLOC_FAILURE);
return 0;
}
if (alloc_pad)
(*dec_op_data)->inputData.dataLenInBytes = rsa_len;
else
(*dec_op_data)->inputData.dataLenInBytes = flen;
*output_buffer = OPENSSL_malloc(sizeof(CpaFlatBuffer));
if (NULL == *output_buffer) {
WARN("[%s] --- OutputBuffer malloc failed!\n", __func__);
QATerr(QAT_F_BUILD_DECRYPT_OP_BUF, ERR_R_MALLOC_FAILURE);
return 0;
}
/*
* Memory allocation for DecOpdata[IN] the size of outputBuffer
* should big enough to contain RSA_size
*/
(*output_buffer)->pData =
(Cpa8U *) qaeCryptoMemAlloc(rsa_len, __FILE__, __LINE__);
if (NULL == (*output_buffer)->pData) {
WARN("[%s] --- OutputBuffer Data malloc failed!\n", __func__);
QATerr(QAT_F_BUILD_DECRYPT_OP_BUF, ERR_R_MALLOC_FAILURE);
return 0;
}
(*output_buffer)->dataLenInBytes = rsa_len;
return 1;
}
static void
rsa_encrypt_op_buf_free(CpaCyRsaEncryptOpData * enc_op_data,
CpaFlatBuffer * out_buf, int padding)
{
if (enc_op_data) {
if (enc_op_data->pPublicKey) {
if (enc_op_data->pPublicKey->modulusN.pData)
qaeCryptoMemFree(enc_op_data->pPublicKey->modulusN.pData);
if (enc_op_data->pPublicKey->publicExponentE.pData)
qaeCryptoMemFree(enc_op_data->pPublicKey->
publicExponentE.pData);
OPENSSL_free(enc_op_data->pPublicKey);
}
if (enc_op_data->inputData.pData)
qaeCryptoMemFree(enc_op_data->inputData.pData);
OPENSSL_free(enc_op_data);
}
if (out_buf) {
if (out_buf->pData)
qaeCryptoMemFree(out_buf->pData);
OPENSSL_free(out_buf);
}
}
int
qat_rsa_encrypt(CpaCyRsaEncryptOpData * enc_op_data,
CpaFlatBuffer * output_buf)
{
/* Used for RSA Encrypt and RSA Verify */
struct op_done op_done;
CpaStatus sts = CPA_STATUS_FAIL;
int qatPerformOpRetries = 0;
CpaInstanceHandle instanceHandle = NULL;
int iMsgRetry = getQatMsgRetryCount();
useconds_t ulPollInterval = getQatPollInterval();
initOpDone(&op_done);
if (op_done.job) {
if (qat_setup_async_event_notification(0) == 0) {
QATerr(QAT_F_QAT_RSA_ENCRYPT, ERR_R_INTERNAL_ERROR);
cleanupOpDone(&op_done);
return 0;
}
}
/*
* cpaCyRsaEncrypt() is the function called for RSA verify in API, the
* DecOpData [IN] contains both private key value and input file (hash)
* value, the outputBuffer [OUT] stores the signature as the output
* message, the sts value return 0 if successful
*/
CRYPTO_QAT_LOG("RSA - %s\n", __func__);
do {
if (NULL == (instanceHandle = get_next_inst())) {
WARN("instanceHandle is NULL\n");
QATerr(QAT_F_QAT_RSA_ENCRYPT, ERR_R_INTERNAL_ERROR);
cleanupOpDone(&op_done);
return 0;
}
sts = cpaCyRsaEncrypt(instanceHandle, qat_rsaCallbackFn, &op_done,
enc_op_data, output_buf);
if (sts == CPA_STATUS_RETRY) {
if (op_done.job == NULL) {
usleep(ulPollInterval +
(qatPerformOpRetries % QAT_RETRY_BACKOFF_MODULO_DIVISOR));
qatPerformOpRetries++;
if (iMsgRetry != QAT_INFINITE_MAX_NUM_RETRIES) {
if (qatPerformOpRetries >= iMsgRetry) {
break;
}
}
} else {
if ((qat_wake_job(op_done.job, 0) == 0) ||
(qat_pause_job(op_done.job, 0) == 0)) {
sts = CPA_STATUS_FAIL;
break;
}
}
}
}
while (sts == CPA_STATUS_RETRY );
if (sts != CPA_STATUS_SUCCESS) {
WARN("[%s] --- cpaCyRsaEncrypt failed, sts=%d.\n", __func__, sts);
QATerr(QAT_F_QAT_RSA_ENCRYPT, ERR_R_INTERNAL_ERROR);
cleanupOpDone(&op_done);
return 0;
}
do {
if(op_done.job) {
/* 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 (qat_pause_job(op_done.job, 0) == 0)
pthread_yield();
} else {
pthread_yield();
}
} while (!op_done.flag);
cleanupOpDone(&op_done);
if (op_done.verifyResult != CPA_TRUE) {
WARN("[%s] --- cpaCyRsaEncrypt failed, verifyResult=%d.\n",
__func__, op_done.verifyResult);
QATerr(QAT_F_QAT_RSA_ENCRYPT, ERR_R_INTERNAL_ERROR);
return 0;
}
return 1;
}
static int
build_encrypt_op(int flen, const unsigned char *from, unsigned char *to,
RSA *rsa, int padding,
CpaCyRsaEncryptOpData ** enc_op_data,
CpaFlatBuffer ** output_buffer, int alloc_pad)
{
CpaCyRsaPublicKey *cpa_pub_key = NULL;
int rsa_len = 0;
const BIGNUM *n = NULL;
const BIGNUM *e = NULL;
const BIGNUM *d = NULL;
RSA_get0_key((const RSA*)rsa, &n, &e, &d);
/* Note: not checking 'd' as it is not used */
if (n == NULL || e == NULL) {
WARN("[%s] --- RSA key values are NULL!\n", __func__);
QATerr(QAT_F_BUILD_ENCRYPT_OP, ERR_R_INTERNAL_ERROR);
return 0;
}
if (padding != RSA_PKCS1_PADDING) {
WARN("[%s] --- Unknown Padding!\n", __func__);
QATerr(QAT_F_BUILD_ENCRYPT_OP, ERR_R_INTERNAL_ERROR);
return 0;
}
cpa_pub_key = OPENSSL_zalloc(sizeof(CpaCyRsaPublicKey));
if (NULL == cpa_pub_key) {
WARN("[%s] --- Public Key zalloc failed!\n", __func__);
QATerr(QAT_F_BUILD_ENCRYPT_OP, ERR_R_MALLOC_FAILURE);
return 0;
}
rsa_len = RSA_size(rsa);
/* Output and input data MUST allocate memory for RSA verify process */
/* Memory allocation for EncOpData[IN] */
*enc_op_data = OPENSSL_zalloc(sizeof(CpaCyRsaEncryptOpData));
if (NULL == *enc_op_data) {
WARN("[%s] --- OpData zalloc failed!\n", __func__);
QATerr(QAT_F_BUILD_ENCRYPT_OP, ERR_R_MALLOC_FAILURE);
OPENSSL_free(cpa_pub_key);
return 0;
}
/* Setup the Encrypt operation Data structure */
(*enc_op_data)->pPublicKey = cpa_pub_key;
DEBUG("[%s] --- flen=%d padding=%d\n", __func__, flen, padding);
/* Passing Public key from big number format to big endian order binary */
if (qat_BN_to_FB(&cpa_pub_key->modulusN, n) != 1 ||
qat_BN_to_FB(&cpa_pub_key->publicExponentE, e) != 1) {
WARN("[%s] --- qat_BN_to_FB failed for cpa_pub_key elements\n",
__func__);
QATerr(QAT_F_BUILD_ENCRYPT_OP, ERR_R_INTERNAL_ERROR);
return 0;
}
if (alloc_pad) {
(*enc_op_data)->inputData.pData =
qat_alloc_pad((Cpa8U *) from, flen, rsa_len, 0);
} else {
(*enc_op_data)->inputData.pData =
(Cpa8U *) copyAllocPinnedMemory((void *)from, flen, __FILE__,
__LINE__);
}
if (NULL == (*enc_op_data)->inputData.pData) {
WARN("[%s] --- Input buffer assignment failed!\n", __func__);
QATerr(QAT_F_BUILD_ENCRYPT_OP, ERR_R_MALLOC_FAILURE);
return 0;
}
if (alloc_pad)
(*enc_op_data)->inputData.dataLenInBytes = rsa_len;
else
(*enc_op_data)->inputData.dataLenInBytes = flen;
/*
* Memory allocation for outputBuffer[OUT] OutputBuffer size initialize
* as the size of rsa size
*/
(*output_buffer) =
(CpaFlatBuffer *) OPENSSL_malloc(sizeof(CpaFlatBuffer));
if (NULL == (*output_buffer)) {
WARN("[%s] --- OutputBuffer malloc failed!\n", __func__);
QATerr(QAT_F_BUILD_ENCRYPT_OP, ERR_R_MALLOC_FAILURE);
return 0;
}
/*
* outputBuffer size should large enough to hold the Hash value but
* smaller than (RSA_size(rsa)-11)
*/
(*output_buffer)->dataLenInBytes = rsa_len;
(*output_buffer)->pData = qaeCryptoMemAlloc(rsa_len, __FILE__, __LINE__);
if (NULL == (*output_buffer)->pData) {
WARN("[%s] --- OutputBuffer pData malloc failed!\n", __func__);
QATerr(QAT_F_BUILD_ENCRYPT_OP, ERR_R_MALLOC_FAILURE);
return 0;;
}
return 1;
}
/******************************************************************************
* function:
* qat_rsa_priv_enc (int flen,
* const unsigned char *from,
* unsigned char *to,
* RSA *rsa,
* int padding)
*
* @param flen [IN] - length in bytes of input file
* @param from [IN] - pointer to the input file
* @param to [OUT] - pointer to output signature
* @param rsa [IN] - pointer to private key structure
* @param padding [IN] - Padding scheme
*
* description: Perform an RSA private encrypt (RSA Sign)
* We use the decrypt implementation to achieve this.
******************************************************************************/
int
qat_rsa_priv_enc(int flen, const unsigned char *from, unsigned char *to,
RSA *rsa, int padding)
{
int rsa_len = 0;
CpaCyRsaDecryptOpData *dec_op_data = NULL;
CpaFlatBuffer *output_buffer = NULL;
int sts = 1;
DEBUG("[%s] --- called.\n", __func__);
/* Parameter Checking */
/*
* The input message length should less than RSA size and also have
* minimum space of PKCS1 padding(4 bytes)
*/
if (rsa == NULL || from == NULL || to == NULL ||
(flen > ((rsa_len = RSA_size(rsa)) - 4))
|| flen == 0) {
DEBUG("[%s] --- Invalid Parameter\n", __func__);
QATerr(QAT_F_QAT_RSA_PRIV_ENC, ERR_R_PASSED_NULL_PARAMETER);
goto exit;
}
/*
* If the op sizes are not in the range supported by QAT engine then fall
* back to software
*/
if (!qat_rsa_range_check(RSA_bits((const RSA*)rsa)))
return RSA_meth_get_priv_enc(RSA_PKCS1_OpenSSL())
(flen, from, to, rsa, padding);
if (1 != build_decrypt_op_buf(flen, from, to, rsa, padding,
&dec_op_data, &output_buffer, PADDING)) {
sts = 0;
goto exit;
}
if (1 != qat_rsa_decrypt(dec_op_data, output_buffer)) {
/* set output all 0xff if failed */
DEBUG("[%s] --- cpaCyRsaDecrypt failed! \n", __func__);
sts = 0;
goto exit;
}
memcpy(to, output_buffer->pData, rsa_len);
DEBUG("[%s] --- cpaCyRsaDecrypt finished! \n", __func__);
rsa_decrypt_op_buf_free(dec_op_data, output_buffer, PADDING);
return rsa_len;
exit:
/* Free all the memory allocated in this function */
rsa_decrypt_op_buf_free(dec_op_data, output_buffer, PADDING);
if (!sts)
memset(to, 0xff, rsa_len);
/* Return an error */
return 0;
}
/******************************************************************************
* function:
* qat_rsa_priv_dec(int flen,
* const unsigned char *from,
* unsigned char *to,
* RSA * rsa,
* int padding)
*
* @param flen [IN] - length in bytes of input
* @param from [IN] - pointer to the input
* @param to [OUT] - pointer to output
* @param rsa [IN] - pointer to the private key structure
* @param padding [IN] - Padding scheme
*
* description:
* description: Perform an RSA private decrypt. (RSA Decrypt)
******************************************************************************/
int qat_rsa_priv_dec(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding)
{
int rsa_len = 0;
int output_len = 0;
int sts = 1;
CpaCyRsaDecryptOpData *dec_op_data = NULL;
CpaFlatBuffer *output_buffer = NULL;
DEBUG("[%s] --- called.\n", __func__);
/* parameter checks */
if (rsa == NULL || from == NULL || to == NULL ||
(flen != (rsa_len = RSA_size(rsa)))) {
WARN("[%s] --- Invalid Parameter\n", __func__);
QATerr(QAT_F_QAT_RSA_PRIV_DEC, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
/*
* If the op sizes are not in the range supported by QAT engine then fall
* back to software
*/
if (!qat_rsa_range_check(RSA_bits((const RSA*)rsa)))
return RSA_meth_get_priv_dec(RSA_PKCS1_OpenSSL())
(flen, from, to, rsa, padding);
if (1 != build_decrypt_op_buf(flen, from, to, rsa, padding,
&dec_op_data, &output_buffer, NO_PADDING)) {
sts = 0;
goto exit;
}
if (1 != qat_rsa_decrypt(dec_op_data, output_buffer)) {
WARN("[%s] --- RsaDecrypt failed.\n", __func__);
sts = 0;
goto exit;
}
/* Copy output to output buffer */
if (qat_remove_pad(to, output_buffer->pData, rsa_len, &output_len,
0, padding) != 1) {
/* set output all 0xff if failed */
WARN("[%s] --- pData remove padding detected an error!\n",
__func__);
DEBUG("[%s] --- cpaCyRsaDecrypt failed! \n", __func__);
QATerr(QAT_F_QAT_RSA_PRIV_DEC, ERR_R_INTERNAL_ERROR);
sts = 0;
goto exit;
}
rsa_decrypt_op_buf_free(dec_op_data, output_buffer, NO_PADDING);
DEBUG("[%s] --- cpaCyRsaDecrypt finished! \n", __func__);
return output_len;
exit:
/* Free all the memory allocated in this function */
rsa_decrypt_op_buf_free(dec_op_data, output_buffer, NO_PADDING);
if (!sts && to)
memset(to, 0xff, rsa_len);
return 0;
}
/******************************************************************************
* function:
* qat_rsa_pub_enc(int flen,
* const unsigned char *from,
* unsigned char *to,
* RSA *rsa,
* int padding)
*
* @param flen [IN] - size in bytes of input
* @param from [IN] - pointer to the input
* @param to [OUT] - pointer to output data
* @param rsa [IN] - pointer to public key structure
* @param padding [IN] - Padding scheme
*
*
* description:
* This function is rewrite of OpenSSL RSA_pub_enc() function. (RSA Encrypt)
* All the inputs are pass form the above OpenSSL layer to the corresponding
* API RSA sign function cpaCyRsaEncrypt().
******************************************************************************/
int qat_rsa_pub_enc(int flen, const unsigned char *from,
unsigned char *to, RSA *rsa, int padding)
{
int rsa_len = 0;
CpaCyRsaEncryptOpData *enc_op_data = NULL;
CpaFlatBuffer *output_buffer = NULL;
int sts = 1;
DEBUG("[%s] --- called.\n", __func__);