-
Notifications
You must be signed in to change notification settings - Fork 128
/
qat_hw_gcm.c
2031 lines (1836 loc) · 73 KB
/
qat_hw_gcm.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) 2020-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_gcm.c
*
* This file contains the engine implementations for GCM cipher operations
*
*****************************************************************************/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <string.h>
#include <pthread.h>
#include <signal.h>
#include "e_qat.h"
#include "qat_utils.h"
#include "qat_hw_callback.h"
#include "qat_hw_polling.h"
#include "qat_events.h"
#include "cpa.h"
#include "cpa_types.h"
#include "cpa_cy_sym.h"
#include "qat_evp.h"
#include "qat_hw_gcm.h"
#include "qat_hw_ciphers.h"
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/err.h>
#include <openssl/sha.h>
#include <openssl/tls1.h>
#include <openssl/ssl.h>
#ifdef ENABLE_QAT_FIPS
# include "qat_prov_cmvp.h"
#endif
#ifdef ENABLE_QAT_HW_GCM
# ifdef ENABLE_QAT_FIPS
extern int qat_fips_key_zeroize;
# endif
/******************************************************************************
* function:
* qat_session_data_init(EVP_CIPHER_CTX *ctx,
* qat_aes_gcm_ctx *qctx,
* const unsigned char* key,
* int keylen,
* const unsigned char* iv,
* int ivlen,
* int enc)
*
* @param ctx [IN] - pointer to the evp context
* @param qctx [IN] - pointer to the qat context
* @param key [IN] - pointer to the cipher key
* @param iv [IN] - pointer to the iv this maybe NULL.
* @param enc [IN] - whether we are doing encryption (1) or decryption (0).
*
* description:
* This function is to create QAT specific session data.
* It is called from the function qat_aes_gcm_init().
*
* It will return 1 if successful and 0 on failure.
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
static int qat_session_data_init(QAT_GCM_CTX *ctx,
const unsigned char* key,
int keylen,
const unsigned char* iv,
int ivlen,
int enc)
#else
static int qat_session_data_init(EVP_CIPHER_CTX *ctx,
qat_gcm_ctx *qctx,
const unsigned char* key,
const unsigned char* iv,
int enc)
#endif
{
#ifdef QAT_OPENSSL_PROVIDER
QAT_GCM_CTX *qctx = (QAT_GCM_CTX *)ctx;
#endif
DEBUG("QAT HW GCM Started\n");
if (NULL == qctx || NULL == ctx) {
WARN("qctx or ctx is NULL\n");
QATerr(QAT_F_QAT_SESSION_DATA_INIT, QAT_R_QCTX_CTX_NULL);
return 0;
}
if (key != NULL) {
if (qctx->qat_svm) {
if (qctx->cipher_key){
OPENSSL_free(qctx->cipher_key);
qctx->cipher_key = NULL;
}
#ifdef QAT_OPENSSL_PROVIDER
qctx->cipher_key = OPENSSL_zalloc(keylen);
#else
qctx->cipher_key = OPENSSL_zalloc(EVP_CIPHER_CTX_key_length(ctx));
#endif
} else {
if (qctx->cipher_key) {
qaeCryptoMemFreeNonZero(qctx->cipher_key);
qctx->cipher_key = NULL;
}
#ifdef QAT_OPENSSL_PROVIDER
qctx->cipher_key = qaeCryptoMemAlloc(keylen, __FILE__, __LINE__);
#else
qctx->cipher_key = qaeCryptoMemAlloc(EVP_CIPHER_CTX_key_length(ctx), __FILE__, __LINE__);
#endif
}
if (qctx->cipher_key == NULL) {
WARN("Unable to allocate memory for qctx->cipher_key.\n");
QATerr(QAT_F_QAT_SESSION_DATA_INIT, QAT_R_KEY_MALLOC_FAILURE);
return 0;
}
#ifdef QAT_OPENSSL_PROVIDER
memcpy(qctx->cipher_key, key, keylen);
#else
memcpy(qctx->cipher_key, key, EVP_CIPHER_CTX_key_length(ctx));
#endif
qctx->key_set = 1;
}
if (iv != NULL && qctx->iv_set) {
qctx->OpData.pIv = qctx->iv;
qctx->OpData.ivLenInBytes = qctx->iv_len;
}
if (NULL == qctx->session_data) {
qctx->session_data = OPENSSL_zalloc(sizeof(CpaCySymSessionSetupData));
if (NULL == qctx->session_data) {
WARN("session setup data zalloc failure\n");
QATerr(QAT_F_QAT_SESSION_DATA_INIT, QAT_R_SSD_MALLOC_FAILURE);
return 0;
}
}
/* Set priority and operation of this session */
qctx->session_data->sessionPriority = CPA_CY_PRIORITY_HIGH;
qctx->session_data->symOperation = CPA_CY_SYM_OP_ALGORITHM_CHAINING;
/* --- Cipher configuration --- */
/* Cipher algorithm and mode */
qctx->session_data->cipherSetupData.cipherAlgorithm =
CPA_CY_SYM_CIPHER_AES_GCM;
/* Cipher key length */
#ifdef QAT_OPENSSL_PROVIDER
qctx->session_data->cipherSetupData.cipherKeyLenInBytes = (Cpa32U)keylen;
#else
qctx->session_data->cipherSetupData.cipherKeyLenInBytes = (Cpa32U)EVP_CIPHER_CTX_key_length(ctx);
#endif
if (qctx->key_set){
qctx->session_data->cipherSetupData.pCipherKey = (Cpa8U *)qctx->cipher_key;
#ifdef QAT_OPENSSL_PROVIDER
qctx->session_data->cipherSetupData.cipherKeyLenInBytes = (Cpa32U)qctx->keylen;
#endif
}
/* Operation to perform */
if (enc) {
qctx->session_data->cipherSetupData.cipherDirection =
CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT;
qctx->session_data->algChainOrder =
CPA_CY_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH;
} else {
qctx->session_data->cipherSetupData.cipherDirection =
CPA_CY_SYM_CIPHER_DIRECTION_DECRYPT;
qctx->session_data->algChainOrder =
CPA_CY_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER;
}
/* --- Hash Configuration --- */
/* Set the hash mode and the length of the digest */
qctx->session_data->hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_AES_GCM;
qctx->session_data->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_AUTH;
qctx->session_data->hashSetupData.digestResultLenInBytes = EVP_GCM_TLS_TAG_LEN;
/* For GCM authKey and authKeyLen are not required.
* This information is provided by the cipherKey in cipherSetupData */
qctx->session_data->hashSetupData.authModeSetupData.authKey = NULL;
qctx->session_data->hashSetupData.authModeSetupData.authKeyLenInBytes = 0;
/* Set the length of the AAD to the default value */
qctx->session_data->hashSetupData.authModeSetupData.aadLenInBytes = 0;
/* Tag follows immediately after the region to hash */
qctx->session_data->digestIsAppended = CPA_FALSE;
/* digestVerify is not required to be set. For GCM authenticated
* encryption this value is understood to be CPA_FALSE during encryption and
* CPA_TRUE during decryption */
qctx->init_params_set = 1;
return 1;
}
/******************************************************************************
* function:
* qat_aes_gcm_init(EVP_CIPHER_CTX *ctx, const unsigned char *inkey,
* int keylen, const unsigned char *iv, int ivlen,
* int enc)
*
* @param ctx [IN] - pointer to existing context
* @param inKey [IN] - input cipher key
* @param iv [IN] - initialisation vector
* @param enc [IN] - 1 encrypt 0 decrypt
*
* @retval 1 function succeeded
* @retval 0 function failed
*
* description:
* This function initialises the cipher and hash algorithm parameters for this
* EVP context.
*
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
int qat_aes_gcm_init(QAT_GCM_CTX *ctx, const unsigned char *inkey,
int keylen, const unsigned char *iv, int ivlen,
int enc)
#else
int qat_aes_gcm_init(EVP_CIPHER_CTX *ctx,
const unsigned char *inkey,
const unsigned char *iv,
int enc)
#endif
{
#ifdef QAT_OPENSSL_PROVIDER
QAT_GCM_CTX* qctx = NULL;
int nid = 0;
QAT_EVP_CIPHER sw_aes_gcm_cipher;
#else
qat_gcm_ctx* qctx = NULL;
#endif
int ret = 0;
if (NULL == ctx) {
WARN("ctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_INIT, QAT_R_CTX_NULL);
return 0;
}
DEBUG("CTX = %p, key = %p, iv = %p, enc = %d\n",
(void*)ctx, (void*)inkey, (void*)iv, enc);
#ifdef QAT_OPENSSL_PROVIDER
qctx = (QAT_GCM_CTX *)ctx;
qctx->iv = (Cpa8U *)qctx->iv;
qctx->next_iv = (Cpa8U *)qctx->next_iv;
qctx->enc = enc;
nid = QAT_AES_GCM_CTX_get_nid((QAT_AES_GCM_CTX *)ctx);
#else
qctx = QAT_GCM_GET_CTX(ctx);
#endif
/* Initialise a QAT session and set the cipher keys*/
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_INIT, QAT_R_QCTX_NULL);
return 0;
}
if (qat_get_sw_fallback_enabled()) {
#ifndef QAT_OPENSSL_PROVIDER
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx->sw_ctx_cipher_data);
/* Run the software init function */
ret =
EVP_CIPHER_meth_get_init(GET_SW_AES_GCM_CIPHER(ctx)) (ctx, inkey, iv,
enc);
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx);
#else
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
sw_aes_gcm_cipher = get_default_cipher_aes_gcm(nid);
if (enc) {
if (!qctx->sw_ctx)
qctx->sw_ctx = sw_aes_gcm_cipher.newctx(ctx);
ret =
sw_aes_gcm_cipher.einit(qctx->sw_ctx, inkey, keylen, iv, ivlen,
params);
} else {
if (!qctx->sw_ctx)
qctx->sw_ctx = sw_aes_gcm_cipher.newctx(ctx);
unsigned int pad = 0;
params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pad);
ret =
sw_aes_gcm_cipher.dinit(qctx->sw_ctx, inkey, keylen, iv, ivlen,
params);
}
#endif
if (ret != 1)
goto err;
}
if (!inkey && !iv) {
DEBUG("key and IV not set\n");
return 1;
}
qctx->inst_num = get_instance(QAT_INSTANCE_SYM, QAT_INSTANCE_ANY);
if (qctx->inst_num == QAT_INVALID_INSTANCE) {
WARN("Failed to get a QAT instance.\n");
goto err;
}
qctx->qat_svm = !qat_instance_details[qctx->inst_num].qat_instance_info.requiresPhysicallyContiguousMemory;
if (NULL == qctx->iv) {
/* The length of the IV in the TLS case is fixed = 12 Bytes */
qctx->iv_len = QAT_GCM_TLS_TOTAL_IV_LEN;
#ifdef QAT_OPENSSL_PROVIDER
qctx->iv = qat_mem_alloc(GCM_IV_MAX_SIZE, qctx->qat_svm, __FILE__, __LINE__);
#else
qctx->iv = qat_mem_alloc(EVP_CIPHER_CTX_iv_length(ctx), qctx->qat_svm, __FILE__, __LINE__);
#endif
if (qctx->iv == NULL) {
WARN("iv is NULL.\n");
QATerr(QAT_F_QAT_AES_GCM_INIT, QAT_R_IV_MALLOC_FAILURE);
goto err;
}
}
#ifdef QAT_OPENSSL_PROVIDER
if (!qctx->next_iv)
qctx->next_iv = OPENSSL_zalloc(EVP_MAX_IV_LENGTH);
#endif
if (iv) {
/* Set the value of the IV */
memcpy(qctx->iv, iv, qctx->iv_len);
memcpy(qctx->next_iv, iv, qctx->iv_len);
qctx->iv_set = 1;
DUMPL("iv", iv, qctx->iv_len);
}
qctx->tls_aad_len = -1;
qctx->tag_len = -1;
qctx->iv_gen = 0;
qctx->tag_set = 0;
/* Initialize QAT session */
#ifdef QAT_OPENSSL_PROVIDER
if (0 == qat_session_data_init(ctx, inkey, keylen, iv, ivlen, enc)) {
#else
if (0 == qat_session_data_init(ctx, qctx, inkey, iv, enc)) {
#endif
WARN("qat_session_data_init failed.\n");
goto err;
}
return 1;
err:
if (NULL != qctx->iv) {
#ifndef QAT_OPENSSL_PROVIDER
if (qctx->iv != EVP_CIPHER_CTX_iv_noconst(ctx)) {
#else
if (qctx->iv != ctx->iv) {
#endif
QAT_MEM_FREE_NONZERO_BUFF(qctx->iv, qctx->qat_svm);
qctx->iv = NULL;
}
}
QAT_MEM_FREE_BUFF(qctx->cipher_key, qctx->qat_svm);
return ret;
}
/******************************************************************************
* * function:
* aes_gcm_increment_counter(unsigned char *ifc)
*
* @param ifc [IN,OUT] - pointer to invocation field counter
*
* description:
* Increment provided invocation field counter (64-bit int) by 1
*
*******************************************************************************/
static inline void qat_aes_gcm_inc_ctr(unsigned char* ifc)
{
int inv_field_size = 8;
unsigned char byte;
/* Loop over ifc starting with the least significant byte
* and work towards the most significant byte of ifc*/
do {
--inv_field_size;
byte = ifc[inv_field_size];
/* Increment by one and copy back to invocation field */
++byte;
ifc[inv_field_size] = byte;
if (byte)
return;
} while (inv_field_size);
}
/******************************************************************************
* function:
* qat_aes_gcm_ctrl(EVP_CIPHER_CTX *ctx,
* int type, int arg, void *ptr)
*
* @param ctx [IN] - pointer to existing context
* @param type [IN] - type of request either
* EVP_CTRL_AEAD_SET_MAC_KEY or EVP_CTRL_AEAD_TLS1_AAD
* @param arg [IN] - size of the pointed to by ptr
* @param ptr [IN] - input buffer contain the necessary parameters
*
* @retval x The return value is dependent on the type of request being made
* EVP_CTRL_AEAD_SET_MAC_KEY return of 1 is success
* EVP_CTRL_AEAD_TLS1_AAD return value indicates the amount
* for padding to be applied to the SSL/TLS record
* @retval -1 function failed
*
* description:
* This function is a generic control interface provided by the EVP API. For
* chained requests this interface is used for setting the hmac key value for
* authentication of the SSL/TLS record.
* The second type is used to specify the TLS virtual header which is used
* in the authentication calculation and to identify record payload size.
*
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
int qat_aes_gcm_ctrl(void *ctx, int type, int arg, void *ptr)
#else
int qat_aes_gcm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
#endif
{
#ifdef QAT_OPENSSL_PROVIDER
QAT_GCM_CTX* qctx = NULL;
#else
qat_gcm_ctx *qctx = NULL;
int ret_sw = 0;
#endif
unsigned int plen = 0;
int enc = 0;
int ret = 0;
if (NULL == ctx) {
WARN("ctx is NULL.\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_CTX_NULL);
return 0;
}
#ifdef QAT_OPENSSL_PROVIDER
qctx = (QAT_GCM_CTX *)ctx;
qctx->iv = (Cpa8U *)qctx->iv;
qctx->next_iv = (Cpa8U *)qctx->next_iv;
#else
qctx = QAT_GCM_GET_CTX(ctx);
#endif
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_QCTX_NULL);
return 0;
}
#ifdef QAT_OPENSSL_PROVIDER
enc = QAT_GCM_GET_ENC(qctx);
#else
enc = EVP_CIPHER_CTX_encrypting(ctx);
#endif
switch (type) {
case EVP_CTRL_INIT:
DEBUG("EVP_CTRL_INIT, ctx = %p, type = %d, "
"arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
qctx->key_set = 0;
qctx->iv_len = 0;
qctx->iv_set = 0;
qctx->iv_gen = 0;
qctx->tls_aad_len = -1;
qctx->tag_len = -1;
qctx->tag_set = 0;
#ifndef QAT_OPENSSL_PROVIDER
if (qctx->sw_ctx_cipher_data == NULL && qat_get_sw_fallback_enabled()) {
unsigned int sw_size = 0;
sw_size = EVP_CIPHER_impl_ctx_size(GET_SW_AES_GCM_CIPHER(ctx));
qctx->sw_ctx_cipher_data = OPENSSL_zalloc(sw_size);
if (qctx->sw_ctx_cipher_data == NULL) {
WARN("Unable to allocate memory for sw_ctx_cipher_data\n");
return -1;
}
goto sw_ctrl;
}
#endif
return 1;
case EVP_CTRL_GET_IVLEN:
DEBUG("EVP_CTRL_GCM_GET_IVLEN, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
*(int*)ptr = qctx->iv_len;
ret = 1;
goto sw_ctrl;
case EVP_CTRL_GCM_SET_IVLEN:
DEBUG("EVP_CTRL_GCM_SET_IVLEN, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
/* At the moment we support only IV with length of 12 Bytes.
* This is the length of the IV in TLS.
*
* If the user wants to use an IV with different length we should
* implement the function J0 as described in GCM standard
*/
if (QAT_GCM_TLS_TOTAL_IV_LEN != arg) {
WARN("Unsupported IV length %d\n", arg);
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_IV_LEN_NOT_SUPPORTED);
return 0;
}
ret = 1;
goto sw_ctrl;
case EVP_CTRL_GCM_SET_TAG:
DEBUG("EVP_CTRL_GCM_SET_TAG, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
if (arg <= QAT_GCM_TAG_MIN_LEN || arg > QAT_GCM_TAG_MAX_LEN || enc) {
WARN("TAG length invalid or invalid operation enc\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_SET_TAG_INVALID_OP);
return 0;
}
if (EVP_GCM_TLS_TAG_LEN != arg) {
WARN("TAG length %d not supported. Accepted value = 16\n", arg);
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_IV_LEN_NOT_SUPPORTED);
return 0;
}
if (NULL == EVP_CIPHER_CTX_buf_noconst(ctx) || NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_INVALID_PTR);
return 0;
}
/* ctx->buf is a static buffer of size
* EVP_MAX_BLOCK_LENGTH = 32
*/
if (ptr) {
memcpy(EVP_CIPHER_CTX_buf_noconst(ctx), ptr, arg);
qctx->tag_set = 1;
}
qctx->tag_len = arg;
ret = 1;
goto sw_ctrl;
case EVP_CTRL_GCM_GET_TAG:
DEBUG("EVP_CTRL_GCM_GET_TAG, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
if (arg <= QAT_GCM_TAG_MIN_LEN || arg > QAT_GCM_TAG_MAX_LEN ||
!enc) {
WARN("TAG length invalid or invalid operation (!enc)\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_SET_TAG_INVALID_OP);
return 0;
}
if (NULL == EVP_CIPHER_CTX_buf_noconst(ctx) || NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_INVALID_PTR);
return 0;
}
memcpy(ptr, EVP_CIPHER_CTX_buf_noconst(ctx), arg);
return 1;
case EVP_CTRL_GCM_SET_IV_FIXED:
DEBUG("EVP_CTRL_GCM_SET_IV_FIXED, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
if (NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_INVALID_PTR);
return 0;
}
/* Special case: -1 length restores whole IV */
if (arg == -1) {
memcpy(qctx->next_iv, ptr, qctx->iv_len);
qctx->iv_gen = 1;
ret = 1;
goto sw_ctrl;
}
/* Fixed field must be at least 4 bytes (EVP_GCM_TLS_FIXED_IV_LEN)
* and invocation field at least 8 (EVP_GCM_TLS_EXPLICIT_IV_LEN)
*/
if ((arg < EVP_GCM_TLS_FIXED_IV_LEN) ||
(qctx->iv_len - arg) < EVP_GCM_TLS_EXPLICIT_IV_LEN) {
WARN("IV length invalid\n");
return 0;
}
if (arg != EVP_GCM_TLS_FIXED_IV_LEN) {
WARN("IV length not supported\n");
return 0;
}
if (arg) {
#ifdef QAT_OPENSSL_PROVIDER
if (qctx->next_iv != NULL)
#endif
memcpy(qctx->next_iv, ptr, arg);
}
/* Generate the explicit part of the IV for encryption */
#ifdef QAT_OPENSSL_PROVIDER
if (qctx->next_iv != NULL)
#endif
if (enc && RAND_bytes(qctx->next_iv + arg, qctx->iv_len - arg) <= 0) {
WARN("Error while generating random explicit IV\n");
if (!qat_get_sw_fallback_enabled())
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_RAND_FAILURE);
ret = 0;
goto sw_ctrl;
}
qctx->iv_gen = 1;
ret = 1;
goto sw_ctrl;
case EVP_CTRL_GCM_IV_GEN:
/* Called in TLS case before encryption */
DEBUG("EVP_CTRL_GCM_IV_GEN, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
if (qctx->sw_tls_ctrl != TLS_CIPHER_SW_CTRL)
goto sw_ctrl;
if (NULL == qctx->iv || NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_IV_NULL_PTR_INVALID);
return 0;
}
if (0 == qctx->iv_gen) {
WARN("Operation is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_IV_GEN_INVALID);
return 0;
}
/* Set the IV that will be used in the current operation */
memcpy(qctx->iv, qctx->next_iv, qctx->iv_len);
if (arg <= 0 || arg > qctx->iv_len) {
arg = qctx->iv_len;
}
/* Copy the explicit IV in the output buffer */
memcpy(ptr, qctx->next_iv + qctx->iv_len - arg, arg);
/* Invocation field will be at least 8 bytes in size and
* so no need to check wrap around or increment more than
* last 8 bytes.
*/
qat_aes_gcm_inc_ctr(qctx->next_iv + qctx->iv_len - 8);
qctx->iv_set = 1;
return 1;
case EVP_CTRL_GCM_SET_IV_INV:
/* Called in TLS case before decryption */
DEBUG("EVP_CTRL_GCM_SET_IV_INV, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
if (qctx->sw_tls_ctrl != TLS_CIPHER_SW_CTRL)
goto sw_ctrl;
if (0 == qctx->iv_gen || enc) {
WARN("Operation is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_IV_NVALID);
return 0;
}
if (NULL == qctx->iv || NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_IV_INVALID);
return 0;
}
/* Retrieve the explicit IV from the message buffer */
memcpy(qctx->next_iv + qctx->iv_len - arg, ptr, arg);
/* Set the IV that will be used in the current operation */
memcpy(qctx->iv, qctx->next_iv, qctx->iv_len);
qctx->iv_set = 1;
return 1;
case EVP_CTRL_AEAD_TLS1_AAD:
DEBUG("EVP_CTRL_AEAD_TLS1_AAD, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void*)ctx, type, arg, ptr);
if (TLS_VIRT_HDR_SIZE != arg) {
WARN("AAD length is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_AAD_LEN_INVALID);
return 0;
}
/* Allocate the memory only the first time */
if (qctx->tls_aad_len < 0) {
int aad_buffer_len = TLS_VIRT_HDR_SIZE;
DEBUG("Allocating memory for AAD in TLS sync\n");
/* For QAT the length of the buffer for AAD must be multiple
* of block size */
if (aad_buffer_len % AES_BLOCK_SIZE) {
aad_buffer_len += AES_BLOCK_SIZE - (aad_buffer_len % AES_BLOCK_SIZE);
DEBUG("Adjusting AAD buffer length = %d\n", aad_buffer_len);
}
qctx->aad = qat_mem_alloc(aad_buffer_len, qctx->qat_svm, __FILE__, __LINE__);
if (NULL == qctx->aad) {
WARN("Unable to allocate memory for TLS header\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_AAD_MALLOC_FAILURE);
return 0;
}
/* Set the flag to mark the TLS case */
qctx->tls_aad_len = TLS_VIRT_HDR_SIZE;
/* Set the length of the AAD in the session
* The session hasn't been initialized yet here and this value
* should never change in the TLS case */
if (qctx->session_data != NULL)
qctx->session_data->hashSetupData.authModeSetupData.aadLenInBytes = TLS_VIRT_HDR_SIZE;
}
if (NULL == qctx->aad || NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_AAD_INVALID_PTR);
return 0;
}
/* Copy the header from p into the buffer */
memcpy(qctx->aad, ptr, TLS_VIRT_HDR_SIZE);
/* Extract the length of the payload from the TLS header */
plen = qctx->aad[arg - QAT_GCM_TLS_PAYLOADLENGTH_MSB_OFFSET]
<< QAT_BYTE_SHIFT |
qctx->aad[arg - QAT_GCM_TLS_PAYLOADLENGTH_LSB_OFFSET];
DEBUG("IN plen = %d\n", plen);
DUMPL("IN qctx->aad", qctx->aad, TLS_VIRT_HDR_SIZE);
/* The payload contains the explicit IV -> correct the length */
plen -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
/* If decrypting correct for tag too */
if (!enc) {
plen -= EVP_GCM_TLS_TAG_LEN;
}
/* Fix the length like in the SW version of GCM */
qctx->aad[TLS_VIRT_HDR_SIZE - QAT_GCM_TLS_PAYLOADLENGTH_MSB_OFFSET]
= plen >> QAT_BYTE_SHIFT;
qctx->aad[TLS_VIRT_HDR_SIZE - QAT_GCM_TLS_PAYLOADLENGTH_LSB_OFFSET]
= plen;
DEBUG("OUT plen = %d\n", plen);
DUMPL("OUT qctx->aad", qctx->aad, TLS_VIRT_HDR_SIZE);
/* Return the length of the TAG */
ret = EVP_GCM_TLS_TAG_LEN;
goto sw_ctrl;
default:
WARN("Invalid type %d\n", type);
QATerr(QAT_F_QAT_AES_GCM_CTRL, QAT_R_INVALID_CTRL_TYPE);
return -1;
}
sw_ctrl:
#ifndef QAT_OPENSSL_PROVIDER
if (qat_get_sw_fallback_enabled()) {
if (type != EVP_CTRL_GCM_SET_IV_INV && type != EVP_CTRL_GCM_IV_GEN)
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx->sw_ctx_cipher_data);
ret_sw =
EVP_CIPHER_meth_get_ctrl(GET_SW_AES_GCM_CIPHER(ctx)) (ctx, type, arg,
ptr);
if (type != EVP_CTRL_GCM_SET_IV_INV && type != EVP_CTRL_GCM_IV_GEN)
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx);
if (ret_sw <= 0) {
WARN("s/w chained ciphers ctrl function failed.\n");
return ret_sw;
}
return ret_sw;
}
#endif
return ret;
}
/******************************************************************************
* function:
* qat_aes_gcm_cleanup(EVP_CIPHER_CTX *ctx)
*
* @param ctx [IN] - pointer to existing context
*
* @retval 1 function succeeded
* @retval 0 function failed
*
* description:
* This function will cleanup all allocated resources required to perform the
* cryptographic transform.
*
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
int qat_aes_gcm_cleanup(void *ctx)
#else
int qat_aes_gcm_cleanup(EVP_CIPHER_CTX *ctx)
#endif
{
#ifdef QAT_OPENSSL_PROVIDER
# ifdef ENABLE_QAT_FIPS
qat_fips_key_zeroize = 0;
# endif
QAT_GCM_CTX *qctx = NULL;
#else
qat_gcm_ctx *qctx = NULL;
#endif
CpaStatus sts = 0;
CpaCySymSessionSetupData* session_data = NULL;
CpaBoolean sessionInUse = CPA_FALSE;
int ret_val = 1;
DEBUG("- Entering\n");
if (NULL == ctx) {
WARN("ctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_CLEANUP, QAT_R_CTX_NULL);
return 0;
}
#ifdef QAT_OPENSSL_PROVIDER
qctx = (QAT_GCM_CTX *)ctx;
qctx->iv = (Cpa8U *)qctx->iv;
#else
qctx = QAT_GCM_GET_CTX(ctx);
#endif
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_GCM_CLEANUP, QAT_R_QCTX_NULL);
return 0;
}
/* Wait for in-flight requests before removing session */
if (qctx->qat_ctx != NULL) {
do {
cpaCySymSessionInUse(qctx->qat_ctx, &sessionInUse);
} while (sessionInUse);
}
session_data = qctx->session_data;
if (session_data) {
/* Remove the session */
if (qctx->qat_ctx) {
if (is_instance_available(qctx->inst_num)) {
sts = cpaCySymRemoveSession(qat_instance_handles[qctx->inst_num],
qctx->qat_ctx);
if (sts != CPA_STATUS_SUCCESS) {
WARN("cpaCySymRemoveSession FAILED, sts = %d.!\n", sts);
ret_val = 0;
/* Lets not return yet and instead make a best effort to
* cleanup the rest to avoid memory leaks
*/
}
}
/* Cleanup the memory */
QAT_MEM_FREE_NONZERO_BUFF(qctx->qat_ctx, qctx->qat_svm);
}
if (!qctx->qat_svm)
QAT_MEM_FREE_NONZERO_BUFF(qctx->aad, qctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(qctx->srcBufferList.pPrivateMetaData, qctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(qctx->dstBufferList.pPrivateMetaData, qctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(qctx->iv, qctx->qat_svm);
QAT_MEM_FREE_BUFF(qctx->cipher_key, qctx->qat_svm);
QAT_MEM_FREE_BUFF(qctx->OpData.pDigestResult, qctx->qat_svm);
#ifdef QAT_OPENSSL_PROVIDER
if (qctx->next_iv) {
OPENSSL_free(qctx->next_iv);
qctx->next_iv = NULL;
}
#endif
session_data->cipherSetupData.pCipherKey = NULL;
OPENSSL_clear_free(session_data, sizeof(CpaCySymSessionSetupData));
}
qctx->is_session_init = 0;
#ifdef ENABLE_QAT_FIPS
qat_fips_key_zeroize = 1;
qat_fips_get_key_zeroize_status();
#endif
#ifndef QAT_OPENSSL_PROVIDER
if (qctx->sw_ctx_cipher_data) {
OPENSSL_free(qctx->sw_ctx_cipher_data);
qctx->sw_ctx_cipher_data = NULL;
}
#else
if (qctx->sw_ctx) {
OPENSSL_free(qctx->sw_ctx);
qctx->sw_ctx = NULL;
}
#endif
return ret_val;
}
/******************************************************************************
* * function:
*
* static void qat_gcm_cb(void *pCallbackTag, CpaStatus status,
* const CpaCySymOp operationType,
* void *pOpData, CpaBufferList *pDstBuffer,
* CpaBoolean verifyResult)
*
* @param pCallbackTag [IN] - Opaque value provided by user while making
* individual function call. Cast to op_done_pipe_t.
* @param status [IN] - Status of the operation.
* @param operationType [IN] - Identifies the operation type requested.
* @param pOpData [IN] - Pointer to structure with input parameters.
* @param pDstBuffer [IN] - Destination buffer to hold the data output.
* @param verifyResult [IN] - Used to verify digest result.
*
* description:
Callback to indicate the completion of crypto operation
******************************************************************************/
static void qat_gcm_cb(void *pCallbackTag, CpaStatus status,
const CpaCySymOp operationType,
void *pOpData, CpaBufferList *pDstBuffer,
CpaBoolean verifyResult)
{
if (enable_heuristic_polling) {
QAT_ATOMIC_DEC(num_cipher_pipeline_requests_in_flight);
}
DEBUG("status is = %d | verifyResult is = %d | tag function called %p \n", status, verifyResult, (struct COMPLETION_STRUCT *)pCallbackTag);
qat_crypto_callbackFn(pCallbackTag, status, CPA_CY_SYM_OP_CIPHER, pOpData,
NULL, verifyResult);
}
/******************************************************************************
* function:
* qat_aes_gcm_session_init(EVP_CIPHER_CTX *ctx)
*
* @param ctx [IN] - pointer to context
*
* @retval 1 function succeeded
* @retval 0 function failed
*
* description:
* This function synchronises the initialisation of the QAT session and
* pre-allocates the necessary buffers for the session.
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
static int qat_aes_gcm_session_init(void *ctx, int *fallback)
#else
static int qat_aes_gcm_session_init(EVP_CIPHER_CTX *ctx, int *fallback)
#endif
{
#ifdef QAT_OPENSSL_PROVIDER
QAT_GCM_CTX* qctx = NULL;
#else
qat_gcm_ctx* qctx = NULL;
#endif
CpaCySymSessionSetupData *sessionSetupData = NULL;
Cpa32U sessionCtxSize = 0;
CpaCySymSessionCtx pSessionCtx = NULL;
int numBuffers = 1, enc = 0;
int sts = -1;
DEBUG("- Entering\n");
if (NULL == ctx) {
WARN("parameter ctx is NULL\n");