-
Notifications
You must be signed in to change notification settings - Fork 128
/
qat_hw_ccm.c
2020 lines (1825 loc) · 71.3 KB
/
qat_hw_ccm.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) 2023-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_ccm.c
*
* This file contains the engine implementations for CCM 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_ccm.h"
#include "qat_hw_ciphers.h"
#ifdef QAT_OPENSSL_PROVIDER
# include "qat_prov_aes_ccm.h"
#endif
#include <openssl/evp.h>
#include <openssl/aes.h>
#include <openssl/err.h>
#include <openssl/sha.h>
#include <openssl/tls1.h>
#include <openssl/ssl.h>
#define GET_SW_AES_CCM_CIPHER(ctx) \
qat_ccm_cipher_sw_impl(EVP_CIPHER_CTX_type((ctx)) )
#ifdef ENABLE_QAT_HW_CCM
/******************************************************************************
* function:
* qat_session_data_init(EVP_CIPHER_CTX *ctx,
* qat_aes_ccm_ctx *qctx,
* const unsigned char* key,
* const unsigned char* iv,
* 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_ccm_init().
*
* It will return 1 if successful and 0 on failure.
******************************************************************************/
# ifdef QAT_OPENSSL_PROVIDER
static int qat_session_data_init(EVP_CIPHER_CTX *ctx,
void *vctx,
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_ccm_ctx * qctx,
const unsigned char *key,
const unsigned char *iv, int enc)
# endif
{
# ifdef QAT_OPENSSL_PROVIDER
QAT_PROV_CCM_CTX *qctx = (QAT_PROV_CCM_CTX *) vctx;
# endif
DEBUG("QAT HW CCM 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.ivLenInBytes = QAT_AES_CCM_OP_VALUE - qctx->L;
}
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_CCM;
/* Cipher key length */
if (key != NULL && qctx->key_set) {
# ifdef QAT_OPENSSL_PROVIDER
qctx->session_data->cipherSetupData.cipherKeyLenInBytes = keylen;
# else
qctx->session_data->cipherSetupData.cipherKeyLenInBytes =
EVP_CIPHER_CTX_key_length(ctx);
# endif
}
if (qctx->key_set) {
qctx->session_data->cipherSetupData.pCipherKey =
(Cpa8U *) qctx->cipher_key;
}
/* 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_HASH_THEN_CIPHER;
/* Tag follows immediately after the region to hash */
} else {
qctx->session_data->cipherSetupData.cipherDirection =
CPA_CY_SYM_CIPHER_DIRECTION_DECRYPT;
qctx->session_data->algChainOrder =
CPA_CY_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH;
}
/* --- Hash Configuration --- */
/* Set the hash mode and the length of the digest */
qctx->session_data->hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_AES_CCM;
qctx->session_data->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_AUTH;
qctx->session_data->hashSetupData.digestResultLenInBytes = qctx->M;
/* For CCM 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_TRUE;
/* digestVerify is not required to be set. For CCM 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_ccm_init(EVP_CIPHER_CTX *ctx,
* const unsigned char *inkey,
* const unsigned char *iv,
* 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_ccm_init(void *ctx, const unsigned char *inkey,
int keylen, const unsigned char *iv, int ivlen, int enc)
{
# else
int qat_aes_ccm_init(EVP_CIPHER_CTX *ctx,
const unsigned char *inkey,
const unsigned char *iv, int enc)
{
# endif
# ifdef QAT_OPENSSL_PROVIDER
QAT_PROV_CCM_CTX *qctx = (QAT_PROV_CCM_CTX *) ctx;
# else
qat_ccm_ctx *qctx = NULL;
# endif
int ret = 1;
int fallback = 0;
# ifdef QAT_OPENSSL_PROVIDER
QAT_EVP_CIPHER sw_aes_ccm_cipher;
# endif
if (NULL == ctx) {
WARN("ctx is NULL\n");
QATerr(QAT_F_QAT_AES_CCM_INIT, QAT_R_CTX_NULL);
return 0;
}
DEBUG("CTX = %p, key = %p, iv = %p, enc = %d\n",
(void *)ctx, (void *)inkey, (void *)iv, enc);
# ifndef QAT_OPENSSL_PROVIDER
qctx = QAT_CCM_GET_CTX(ctx);
# else
qctx->enc = enc;
# endif
/* Initialise a QAT session and set the cipher keys */
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_CCM_INIT, QAT_R_QCTX_NULL);
return 0;
}
if (qat_get_sw_fallback_enabled())
fallback = 1;
# ifndef ENABLE_QAT_SMALL_PKT_OFFLOAD
fallback = 1;
# endif
if (!inkey && !iv) {
DEBUG("key and IV not set\n");
ret = 1;
goto end;
}
if (iv) {
/* Set the value of the IV */
memcpy(qctx->next_iv, iv, QAT_AES_CCM_OP_VALUE - qctx->L);
qctx->iv_len = QAT_AES_CCM_OP_VALUE - qctx->L;
qctx->iv_set = 1;
}
qctx->tls_aad_len = -1;
qctx->tag_len = -1;
qctx->tag_set = 0;
qctx->len_set = 0;
if ((qctx->inst_num = get_instance(QAT_INSTANCE_SYM, QAT_INSTANCE_ANY))
== QAT_INVALID_INSTANCE) {
WARN("Failed to get QAT Instance Handle\n");
ret = 0;
goto end;
}
qctx->qat_svm =
!qat_instance_details[qctx->inst_num].qat_instance_info.
requiresPhysicallyContiguousMemory;
/* Initialize QAT session */
# ifdef QAT_OPENSSL_PROVIDER
if (0 == qat_session_data_init(ctx, qctx, 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;
}
goto end;
err:
QAT_MEM_FREE_BUFF(qctx->cipher_key, qctx->qat_svm);
end:
if (fallback) {
# 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_CCM_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_ccm_cipher = get_default_cipher_aes_ccm(qctx->nid);
if (enc) {
if (!qctx->sw_ctx)
qctx->sw_ctx = sw_aes_ccm_cipher.newctx(ctx);
ret =
sw_aes_ccm_cipher.einit(qctx->sw_ctx, inkey, keylen, iv, ivlen,
params);
} else {
if (!qctx->sw_ctx)
qctx->sw_ctx = sw_aes_ccm_cipher.newctx(ctx);
unsigned int pad = 0;
params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pad);
ret =
sw_aes_ccm_cipher.dinit(qctx->sw_ctx, inkey, keylen, iv, ivlen,
params);
}
# endif
}
return ret;
}
/******************************************************************************
* function:
* qat_aes_ccm_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_ccm_ctrl(void *ctx, int type, int arg, void *ptr)
# else
int qat_aes_ccm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
# endif
{
# ifdef QAT_OPENSSL_PROVIDER
QAT_PROV_CCM_CTX *qctx = (QAT_PROV_CCM_CTX *) ctx;
# else
qat_ccm_ctx *qctx = NULL;
int ret_sw = 0;
int fallback = 0;
# ifndef ENABLE_QAT_SMALL_PKT_OFFLOAD
int nid = EVP_CIPHER_CTX_nid(ctx);
# endif
# endif
unsigned int plen = 0;
int enc = 0;
int ret = 0;
int l_value = 0;
if (NULL == ctx) {
WARN("ctx is NULL.\n");
QATerr(QAT_F_QAT_AES_CCM_CTRL, QAT_R_CTX_NULL);
return 0;
}
# ifdef QAT_OPENSSL_PROVIDER
enc = qctx->enc;
# else
enc = EVP_CIPHER_CTX_encrypting(ctx);
qctx = QAT_CCM_GET_CTX(ctx);
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_CCM_CTRL, QAT_R_QCTX_NULL);
return 0;
}
# 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 = QAT_CCM_TLS_TOTAL_IV_LEN;
qctx->iv_set = 0;
qctx->tag_set = 0;
qctx->L = 8;
qctx->M = 12;
qctx->tls_aad_len = -1;
qctx->tag_len = -1;
qctx->len_set = 0;
ret = 1;
goto end;
case EVP_CTRL_GET_IVLEN:
DEBUG("EVP_CTRL_CCM_GET_IVLEN, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void *)ctx, type, arg, ptr);
*(int *)ptr = QAT_AES_CCM_OP_VALUE - qctx->L;
ret = 1;
goto end;
case EVP_CTRL_AEAD_SET_TAG:
DEBUG("EVP_CTRL_AEAD_SET_TAG, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void *)ctx, type, arg, ptr);
if (arg < QAT_CCM_TAG_MIN_LEN || arg > QAT_CCM_TAG_MAX_LEN) {
WARN("TAG length invalid or invalid operation enc\n");
QATerr(QAT_F_QAT_AES_CCM_CTRL, QAT_R_SET_TAG_INVALID_OP);
return 0;
}
if (NULL == EVP_CIPHER_CTX_buf_noconst(ctx)) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_CCM_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;
qctx->M = arg;
ret = 1;
goto end;
case EVP_CTRL_AEAD_GET_TAG:
DEBUG("EVP_CTRL_CCM_GET_TAG, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void *)ctx, type, arg, ptr);
if (arg <= QAT_CCM_TAG_MIN_LEN || arg > QAT_CCM_TAG_MAX_LEN || !enc) {
WARN("TAG length invalid or invalid operation (!enc)\n");
QATerr(QAT_F_QAT_AES_CCM_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_CCM_CTRL, QAT_R_INVALID_PTR);
return 0;
}
qctx->iv_set = 0;
qctx->len_set = 0;
if (!qctx->tag_set) {
qctx->tag_set = 0;
goto end;
}
memcpy(ptr, EVP_CIPHER_CTX_buf_noconst(ctx), arg);
qctx->tag_set = 0;
return 1;
case EVP_CTRL_CCM_SET_IV_FIXED:
DEBUG("EVP_CTRL_CCM_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_CCM_CTRL, QAT_R_INVALID_PTR);
return 0;
}
/* Special case: -1 length restores whole IV */
if (arg == -1) {
memcpy(qctx->next_iv, ptr, QAT_CCM_TLS_TOTAL_IV_LEN);
ret = 1;
goto end;
}
/* Fixed field must be at least 4 bytes (EVP_CCM_TLS_FIXED_IV_LEN)
* and invocation field at least 8 (EVP_CCM_TLS_EXPLICIT_IV_LEN)
*/
if ((arg < EVP_CCM_TLS_FIXED_IV_LEN) ||
(qctx->iv_len - arg) < EVP_CCM_TLS_EXPLICIT_IV_LEN) {
WARN("IV length invalid\n");
ret = 0;
goto end;
}
if (arg != EVP_CCM_TLS_FIXED_IV_LEN) {
WARN("IV length not supported\n");
return 0;
}
if (arg) {
memcpy(qctx->next_iv, ptr, arg);
}
ret = 1;
goto end;
case EVP_CTRL_AEAD_SET_IVLEN:
DEBUG("EVP_CTRL_CCM_SET_IVLEN, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void *)ctx, type, arg, ptr);
l_value = QAT_AES_CCM_OP_VALUE - arg;
/* fall thru */
case EVP_CTRL_CCM_SET_L:
DEBUG("EVP_CTRL_CCM_SET_L, ctx = %p, type = %d,"
" arg = %d, ptr = %p\n", (void *)ctx, type, arg, ptr);
if (l_value < 2 || l_value > 8) {
if (arg < 2 || arg > 8) {
return 0;
}
else {
l_value = arg;
}
}
qctx->L = l_value;
ret = 1;
goto end;
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_CCM_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_CCM_CTRL, QAT_R_AAD_MALLOC_FAILURE);
ret = 0;
goto end;
}
/* Set the flag to mark the TLS case */
qctx->tls_aad_len = arg;
/* 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 =
arg;
}
if (NULL == qctx->aad || NULL == ptr) {
WARN("Memory pointer is not valid\n");
QATerr(QAT_F_QAT_AES_CCM_CTRL, QAT_R_AAD_INVALID_PTR);
ret = 0;
goto end;
}
/* Copy the header from p into the buffer */
memcpy(qctx->aad, ptr, arg);
/* Extract the length of the payload from the TLS header */
plen = qctx->aad[arg - QAT_CCM_TLS_PAYLOADLENGTH_MSB_OFFSET]
<< QAT_BYTE_SHIFT |
qctx->aad[arg - QAT_CCM_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_CCM_TLS_EXPLICIT_IV_LEN;
/* If decrypting correct for tag too */
if (!enc) {
plen -= qctx->M;
}
/* Fix the length like in the SW version of CCM */
qctx->aad[arg - QAT_CCM_TLS_PAYLOADLENGTH_MSB_OFFSET]
= plen >> QAT_BYTE_SHIFT;
qctx->aad[arg - QAT_CCM_TLS_PAYLOADLENGTH_LSB_OFFSET]
= plen & 0xff;
DEBUG("OUT plen = %d\n", plen);
DUMPL("OUT qctx->aad", qctx->aad, TLS_VIRT_HDR_SIZE);
/* Return the length of the TAG */
ret = qctx->M;
goto end;
case EVP_CTRL_COPY:
ret = 1;
goto end;
default:
WARN("Invalid type %d\n", type);
QATerr(QAT_F_QAT_AES_CCM_CTRL, QAT_R_INVALID_CTRL_TYPE);
return -1;
}
end:
#ifndef QAT_OPENSSL_PROVIDER
# ifndef ENABLE_QAT_SMALL_PKT_OFFLOAD
if (type == EVP_CTRL_INIT
|| qctx->packet_size <= qat_pkt_threshold_table_get_threshold(nid))
fallback = 1;
# endif
if (qat_get_sw_fallback_enabled())
fallback = 1;
if (fallback) {
if (type == EVP_CTRL_INIT && qctx->sw_ctx_cipher_data == NULL) {
unsigned int sw_size = 0;
sw_size = EVP_CIPHER_impl_ctx_size(GET_SW_AES_CCM_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;
}
}
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx->sw_ctx_cipher_data);
ret_sw =
EVP_CIPHER_meth_get_ctrl(GET_SW_AES_CCM_CIPHER(ctx)) (ctx, type, arg,
ptr);
EVP_CIPHER_CTX_set_cipher_data(ctx, qctx);
if (ret_sw < 0) {
WARN("SW aes-ccm ctrl function failed.\n");
return -1;
}
return ret_sw;
}
# endif
return ret;
}
/******************************************************************************
* function:
* qat_aes_ccm_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_ccm_cleanup(void *ctx)
# else
int qat_aes_ccm_cleanup(EVP_CIPHER_CTX *ctx)
# endif
{
# ifdef QAT_OPENSSL_PROVIDER
QAT_PROV_CCM_CTX *qctx = (QAT_PROV_CCM_CTX *) ctx;
# else
qat_ccm_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_CCM_CLEANUP, QAT_R_CTX_NULL);
return 0;
}
# ifndef QAT_OPENSSL_PROVIDER
qctx = QAT_CCM_GET_CTX(ctx);
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_CCM_CLEANUP, QAT_R_QCTX_NULL);
return 0;
}
# endif
/* 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);
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_BUFF(qctx->cipher_key, qctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(qctx->aad, qctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(qctx->OpData.pAdditionalAuthData,
qctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(qctx->OpData.pIv, qctx->qat_svm);
qctx->qat_ctx = NULL;
qctx->srcBufferList.pPrivateMetaData = NULL;
qctx->dstBufferList.pPrivateMetaData = NULL;
qctx->OpData.pAdditionalAuthData = NULL;
qctx->OpData.pIv = NULL;
qctx->cipher_key = NULL;
qctx->aad = NULL;
}
session_data->cipherSetupData.pCipherKey = NULL;
OPENSSL_clear_free(session_data, sizeof(CpaCySymSessionSetupData));
}
qctx->is_session_init = 0;
# ifdef QAT_OPENSSL_PROVIDER
if (qctx->sw_ctx) {
OPENSSL_free(qctx->sw_ctx);
qctx->sw_ctx = NULL;
}
# else
qctx->packet_size = 0;
if (qctx->sw_ctx_cipher_data) {
OPENSSL_free(qctx->sw_ctx_cipher_data);
qctx->sw_ctx_cipher_data = NULL;
}
# endif
return ret_val;
}
/******************************************************************************
* * function:
*
* static void qat_ccm_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_ccm_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_ccm_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_ccm_session_init(void *ctx, int *fallback)
# else
static int qat_aes_ccm_session_init(EVP_CIPHER_CTX *ctx, int *fallback)
# endif
{
# ifdef QAT_OPENSSL_PROVIDER
QAT_PROV_CCM_CTX *qctx = NULL;
# else
qat_ccm_ctx *qctx = NULL;
# endif
CpaCySymSessionSetupData *sessionSetupData = NULL;
Cpa32U sessionCtxSize = 0;
CpaCySymSessionCtx pSessionCtx = NULL;
int numBuffers = 1, enc = 0;
CpaStatus status;
DEBUG("- Entering\n");
if (NULL == ctx) {
WARN("parameter ctx is NULL\n");
QATerr(QAT_F_QAT_AES_CCM_SESSION_INIT, QAT_R_CTX_NULL);
return 0;
}
# ifdef QAT_OPENSSL_PROVIDER
qctx = (QAT_PROV_CCM_CTX *) ctx;
enc = QAT_CCM_GET_ENC(qctx);;
# else
qctx = QAT_CCM_GET_CTX(ctx);
enc = EVP_CIPHER_CTX_encrypting(ctx);
# endif
if (NULL == qctx) {
WARN("qctx is NULL\n");
QATerr(QAT_F_QAT_AES_CCM_SESSION_INIT, QAT_R_QCTX_NULL);
return 0;
}
/* All parameters have not been set yet or we have already been
* initialised. */
if ((1 != qctx->init_params_set) || (1 == qctx->is_session_init)) {
WARN("Parameters not set or session already initialised\n");
if (qat_get_sw_fallback_enabled())
*fallback = 1;
return 0;
}
sessionSetupData = qctx->session_data;
if (NULL == sessionSetupData) {
WARN("sessionSetupData is NULL\n");
QATerr(QAT_F_QAT_AES_CCM_SESSION_INIT, QAT_R_SSD_NULL);
return 0;
}
/* Update digestResultLenInBytes with qctx->tag_len if both lengths
are mismatch for decryption */
if (!enc) {
DEBUG("digestResultLenInBytes = %d, tag len = %d\n",
sessionSetupData->hashSetupData.digestResultLenInBytes,
(int)qctx->M);
if (!(qctx->tag_len < 0)
&& sessionSetupData->hashSetupData.digestResultLenInBytes !=
qctx->M) {
sessionSetupData->hashSetupData.digestResultLenInBytes = qctx->M;
DEBUG("Taglen updated\n");
}
}
if ((qctx->inst_num = get_instance(QAT_INSTANCE_SYM, QAT_INSTANCE_ANY))
== QAT_INVALID_INSTANCE) {
WARN("Failed to get QAT Instance Handle\n");
if (qat_get_sw_fallback_enabled()) {
CRYPTO_QAT_LOG("Failed to get an instance - fallback to SW - %s\n", __func__);
*fallback = 1;
}
return 0;
}
status = cpaCySymSessionCtxGetSize(qat_instance_handles[qctx->inst_num],
sessionSetupData,
&sessionCtxSize);
if (status != CPA_STATUS_SUCCESS) {
WARN("Failed to get SessionCtx size.\n");
if (qat_get_sw_fallback_enabled())
*fallback = 1;
return 0;
}
pSessionCtx =
(CpaCySymSessionCtx) qat_mem_alloc(sessionCtxSize, qctx->qat_svm,
__FILE__, __LINE__);
if (NULL == pSessionCtx) {
WARN("pSessionCtx malloc failed\n");
QATerr(QAT_F_QAT_AES_CCM_SESSION_INIT, ERR_R_INTERNAL_ERROR);
return 0;
}
DUMP_SESSION_SETUP_DATA(sessionSetupData);
status = cpaCySymInitSession(qat_instance_handles[qctx->inst_num],
qat_ccm_cb,
sessionSetupData,
pSessionCtx);
if (status == CPA_STATUS_SUCCESS) {
if (qat_get_sw_fallback_enabled()) {
CRYPTO_QAT_LOG("Submit success qat inst_num %d device_id %d - %s\n",
qctx->inst_num,
qat_instance_details[qctx->inst_num].qat_instance_info.physInstId.packageId,
__func__);
}
} else {
QAT_MEM_FREE_BUFF(pSessionCtx, qctx->qat_svm);
WARN("cpaCySymInitSession failed! 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",
qctx->inst_num,
qat_instance_details[qctx->inst_num].qat_instance_info.physInstId.packageId,
__func__);
*fallback = 1;
return 0;
}
else {
WARN("- No QAT instance available and s/w fallback not enabled.\n");
return 0;
}
}
qctx->qat_ctx = pSessionCtx;
/* Setup meta data for buffer lists */