forked from intel/QAT_Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qat_hw_chachapoly.c
1721 lines (1566 loc) · 65 KB
/
qat_hw_chachapoly.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) 2021-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_chachapoly.c
*
* This file provides an implementation of CHACHAPOLY operations for OpenSSL
* engine
*
*****************************************************************************/
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <pthread.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include "openssl/ossl_typ.h"
#include <openssl/evp.h>
#include "e_qat.h"
#include "qat_hw_callback.h"
#include "qat_hw_polling.h"
#include "qat_events.h"
#include "qat_utils.h"
#include "qat_hw_ciphers.h"
#include "qat_evp.h"
#include "qat_hw_chachapoly.h"
#ifdef QAT_HW_INTREE
# define ENABLE_QAT_HW_CHACHAPOLY
#endif
# define GET_SW_CHACHA_CTX EVP_chacha20_poly1305()
#ifdef ENABLE_QAT_HW_CHACHAPOLY
# ifdef QAT_OPENSSL_PROVIDER
static int qat_chacha20_poly1305_init_key_iv(qat_chachapoly_ctx *cp_ctx,
const unsigned char *key,
const unsigned char *iv);
static int qat_chacha20_poly1305_mac_keygen(qat_chachapoly_ctx *cp_ctx);
static void qat_chacha20_core(chacha_buf *output,
const unsigned int input[16]);
static void qat_chacha20_ctr32(unsigned char *out, const unsigned char *inp,
size_t len, const unsigned int key[8],
const unsigned int counter[4]);
# else
static int qat_chacha20_poly1305_init(EVP_CIPHER_CTX *ctx,
const unsigned char *user_key,
const unsigned char *iv, int enc);
static int qat_chacha20_poly1305_do_cipher(EVP_CIPHER_CTX * ctx,
unsigned char *out,
const unsigned char *in,
size_t len);
static int qat_chacha20_poly1305_cleanup(EVP_CIPHER_CTX *ctx);
static int qat_chacha20_poly1305_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg,
void *ptr);
static int qat_chacha20_poly1305_init_key_iv(qat_chachapoly_ctx *cp_ctx,
const unsigned char *key,
const unsigned char *iv);
static int qat_chacha20_poly1305_mac_keygen(qat_chachapoly_ctx *cp_ctx);
static void qat_chacha20_core(chacha_buf *output,
const unsigned int input[16]);
static void qat_chacha20_ctr32(unsigned char *out, const unsigned char *inp,
size_t len, const unsigned int key[8],
const unsigned int counter[4]);
# endif
/******************************************************************************
* function:
* chachapoly_cipher_meth(int nid, int keylen)
*
* @param nid [IN] - Cipher NID to be created
* @param keylen [IN] - Key length of cipher
* @retval - EVP_CIPHER * to created cipher
* @retval - NULL if failure
*
* description:
* create a new EVP_CIPHER based on requested nid
******************************************************************************/
const EVP_CIPHER *chachapoly_cipher_meth(int nid, int keylen)
{
EVP_CIPHER *c = NULL;
# ifndef QAT_OPENSSL_PROVIDER
int res = 1;
if (qat_hw_offload &&
(qat_hw_algo_enable_mask & ALGO_ENABLE_MASK_CHACHA_POLY)) {
/* block size is 1 and key size is 32 bytes. */
if ((c = EVP_CIPHER_meth_new(nid, 1, keylen)) == NULL) {
WARN("Failed to allocate cipher methods for nid %d\n", nid);
return NULL;
}
/* IV size is 12 bytes for TLS protocol */
res &= EVP_CIPHER_meth_set_iv_length(c, QAT_CHACHA20_POLY1305_MAX_IVLEN);
res &= EVP_CIPHER_meth_set_flags(c, EVP_CIPH_FLAG_AEAD_CIPHER |
EVP_CIPH_CUSTOM_IV |
EVP_CIPH_ALWAYS_CALL_INIT |
EVP_CIPH_CTRL_INIT | EVP_CIPH_CUSTOM_COPY |
EVP_CIPH_FLAG_CUSTOM_CIPHER |
EVP_CIPH_CUSTOM_IV_LENGTH);
res &= EVP_CIPHER_meth_set_init(c, qat_chacha20_poly1305_init);
res &= EVP_CIPHER_meth_set_do_cipher(c, qat_chacha20_poly1305_do_cipher);
res &= EVP_CIPHER_meth_set_cleanup(c, qat_chacha20_poly1305_cleanup);
res &= EVP_CIPHER_meth_set_impl_ctx_size(c, 0);
res &= EVP_CIPHER_meth_set_set_asn1_params(c, NULL);
res &= EVP_CIPHER_meth_set_get_asn1_params(c, NULL);
res &= EVP_CIPHER_meth_set_ctrl(c, qat_chacha20_poly1305_ctrl);
if (res == 0) {
WARN("Failed to set cipher methods for nid %d\n", nid);
EVP_CIPHER_meth_free(c);
c = NULL;
}
qat_hw_chacha_poly_offload = 1;
DEBUG("QAT HW CHACHA POLY registration succeeded\n");
return c;
} else {
qat_hw_chacha_poly_offload = 0;
DEBUG("QAT HW CHACHA POLY is disabled, using OpenSSL SW\n");
EVP_CIPHER_meth_free(c);
return EVP_chacha20_poly1305();
}
# else
return c;
# endif
}
/******************************************************************************
* function:
* qat_chachapoly_cb(void *callbackTag, CpaStatus status,
* const CpaCySymOp operationType, void *pOpData,
* CpaBufferList * pDstBuffer, CpaBoolean verifyResult)
*
* @param pCallbackTag [IN] - Opaque value provided by user while making
* individual function call.
* @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 function used by chachapoly.
******************************************************************************/
static void qat_chachapoly_cb(void *callbackTag, CpaStatus status,
const CpaCySymOp operationType,
void *pOpData, CpaBufferList *pDstBuffer,
CpaBoolean verifyResult)
{
if (enable_heuristic_polling) {
QAT_ATOMIC_DEC(num_cipher_pipeline_requests_in_flight);
}
qat_crypto_callbackFn(callbackTag, status, CPA_CY_SYM_OP_CIPHER, pOpData,
NULL, CPA_TRUE);
}
static const CpaCySymOpData template_opData = {
.sessionCtx = NULL,
.packetType = CPA_CY_SYM_PACKET_TYPE_FULL,
.pIv = NULL,
.ivLenInBytes = 0,
.cryptoStartSrcOffsetInBytes = 0,
.messageLenToCipherInBytes = 0,
.hashStartSrcOffsetInBytes = 0,
.messageLenToHashInBytes = 0,
.pDigestResult = NULL,
.pAdditionalAuthData = NULL
};
/******************************************************************************
* function:
* qat_chacha20_core(chacha_buf *output, const unsigned int input[16])
*
* @param output [OUT] - output buffer to hold key bytes
* @param input [IN] - input keystream
*
* description:
* chacha_core performs 20 rounds of ChaCha on the input words in
* "input" and writes the 64 output bytes to "output".
******************************************************************************/
static void qat_chacha20_core(chacha_buf *output, const unsigned int input[16])
{
unsigned x[16];
int i;
memcpy(x, input, sizeof(x));
for (i = 20; i > 0; i -= 2) {
QUARTERROUND(0, 4, 8, 12);
QUARTERROUND(1, 5, 9, 13);
QUARTERROUND(2, 6, 10, 14);
QUARTERROUND(3, 7, 11, 15);
QUARTERROUND(0, 5, 10, 15);
QUARTERROUND(1, 6, 11, 12);
QUARTERROUND(2, 7, 8, 13);
QUARTERROUND(3, 4, 9, 14);
}
for (i = 0; i < 16; ++i)
output->u[i] = x[i] + input[i];
}
/******************************************************************************
* function:
* qat_chacha20_ctr32(unsigned char *out, const unsigned char *inp,
* size_t len, const unsigned int key[8],
* const unsigned int counter[4])
*
* @param out [OUT] - output buffer to hold key bytes
* @param inp [IN] - input keystream
* @param len [IN] - length of input key buffer
* @param key [IN] - chacha key collected into 8 64-bit elements
* @param counter [IN] - nonce and counter values concatenated into 4 32-bit elements
*
* description:
* ChaCha20_ctr32 encrypts "len" bytes from "inp" with the given key and
* nonce and writes the result to "out", which may be equal to "inp".
******************************************************************************/
static void qat_chacha20_ctr32(unsigned char *out, const unsigned char *inp,
size_t len, const unsigned int key[8],
const unsigned int counter[4])
{
static const char sigma[16] = "expand 32-byte k";
const char *constants;
unsigned int input[16];
chacha_buf buf;
size_t todo, i;
constants = sigma;
input[0] = U8TO32_LITTLE(constants + 0);
input[1] = U8TO32_LITTLE(constants + 4);
input[2] = U8TO32_LITTLE(constants + 8);
input[3] = U8TO32_LITTLE(constants + 12);
input[4] = key[0];
input[5] = key[1];
input[6] = key[2];
input[7] = key[3];
input[8] = key[4];
input[9] = key[5];
input[10] = key[6];
input[11] = key[7];
input[12] = counter[0];
input[13] = counter[1];
input[14] = counter[2];
input[15] = counter[3];
while (len > 0) {
todo = sizeof(buf);
if (len < todo)
todo = len;
qat_chacha20_core(&buf, input);
for (i = 0; i < todo; i++)
out[i] = inp[i] ^ buf.c[i];
out += todo;
inp += todo;
len -= todo;
input[12]++;
}
}
/******************************************************************************
* function:
* qat_chacha20_poly1305_init_key_iv(qat_chachapoly_ctx *ctx,
* const unsigned char *key,
* const unsigned char *iv)
*
* @param ctx [IN] - pointer to existing cipher ctx
* @param key [IN] - 256 bit cipher key
* @param iv [IN] - 96 bit IV
*
* @retval 1 function succeeded
* @retval 0 function failed
*
* description:
* Initialise cipher key and IV in order to generate Poly1305(mac) key.
******************************************************************************/
static int qat_chacha20_poly1305_init_key_iv(qat_chachapoly_ctx *cp_ctx,
const unsigned char *key,
const unsigned char *iv)
{
int i;
if (cp_ctx == NULL) {
WARN("chachapoly cipher data is NULL.\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_INIT_KEY_IV,
QAT_R_CHACHAPOLY_CTX_NULL);
return 0;
}
if (iv != NULL) {
unsigned char temp[QAT_CHACHA_CTR_SIZE] = { 0 };
if (cp_ctx->nonce_len <= QAT_CHACHA_CTR_SIZE)
memcpy(temp + QAT_CHACHA_CTR_SIZE - cp_ctx->nonce_len, iv,
cp_ctx->nonce_len);
for (i = 0; i < QAT_CHACHA_CTR_SIZE; i+=4)
cp_ctx->counter[i/4] = CHACHA_U8TOU32(temp+i);
cp_ctx->iv[0] = cp_ctx->counter[1];
cp_ctx->iv[1] = cp_ctx->counter[2];
cp_ctx->iv[2] = cp_ctx->counter[3];
}
if (key != NULL) {
for (i = 0; i < QAT_CHACHA_KEY_SIZE; i+=4)
cp_ctx->chacha_key[i/4] = CHACHA_U8TOU32(key+i);
}
return 1;
}
/******************************************************************************
* function:
* qat_chacha20_poly1305_mac_keygen(qat_chachapoly_ctx *cp_ctx)
*
* @param ctx [IN] - pointer to existing cipher ctx
*
* @retval 1 function succeeded
* @retval 0 function failed
*
* description:
* Generate One-Time Poly1305 key for the hash operation
* using cipher key and IV.
******************************************************************************/
static int qat_chacha20_poly1305_mac_keygen(qat_chachapoly_ctx *cp_ctx)
{
const unsigned char in_buf[2 * QAT_CHACHA_BLK_SIZE] = { 0 };
if (cp_ctx == NULL) {
WARN("chachapoly cipher data is NULL.\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_MAC_KEYGEN,
QAT_R_CHACHAPOLY_CTX_NULL);
return 0;
}
/* Initialise counter as 0. */
cp_ctx->counter[0] = 0;
/* qat_chacha20_ctr32 encrypts QAT_CHACHA_BLK_SIZE bytes from in_buf
* with the given key and nonce and writes the result
* to cp_ctx->mac_key */
qat_chacha20_ctr32(cp_ctx->mac_key, in_buf, QAT_CHACHA_BLK_SIZE,
cp_ctx->chacha_key, cp_ctx->counter);
cp_ctx->mac_key_set = 1;
return 1;
}
/******************************************************************************
* function:
* qat_chachapoly_session_data_init(qat_chachapoly_ctx *cp_ctx,
* const unsigned char *inkey,
* const unsigned char *iv,
* int enc)
*
* @param ctx [IN] - pointer to existing cipher ctx
* @param inKey [IN] - 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 is to create QAT specific session data.
* It is called from the function qat_chacha20_poly1305_init().
******************************************************************************/
static int qat_chachapoly_session_data_init(qat_chachapoly_ctx *cp_ctx,
const unsigned char* key,
const unsigned char* iv,
int enc)
{
if (cp_ctx == NULL) {
WARN("chachapoly cipher data is NULL.\n");
QATerr(QAT_F_QAT_CHACHAPOLY_SESSION_DATA_INIT,
QAT_R_CHACHAPOLY_CTX_NULL);
return 0;
}
if (cp_ctx->session_data == NULL) {
cp_ctx->session_data = OPENSSL_zalloc(sizeof(CpaCySymSessionSetupData));
if (cp_ctx->session_data == NULL) {
WARN("Failed to allocate session setup data\n");
QATerr(QAT_F_QAT_CHACHAPOLY_SESSION_DATA_INIT,
ERR_R_MALLOC_FAILURE);
return 0;
}
}
cp_ctx->inst_num = get_instance(QAT_INSTANCE_SYM, QAT_INSTANCE_ANY);
if (cp_ctx->inst_num == QAT_INVALID_INSTANCE) {
WARN("Failed to get a QAT instance.\n");
QATerr(QAT_F_QAT_CHACHAPOLY_SESSION_DATA_INIT, ERR_R_INTERNAL_ERROR);
return 0;
}
cp_ctx->qat_svm = !qat_instance_details[cp_ctx->inst_num].qat_instance_info.requiresPhysicallyContiguousMemory;
DEBUG("inst_num = %d inst mem type %d \n", cp_ctx->inst_num, cp_ctx->qat_svm);
if (cp_ctx->mac_key == NULL) {
cp_ctx->mac_key = qat_mem_alloc(QAT_CHACHA_BLK_SIZE, cp_ctx->qat_svm, __FILE__, __LINE__);
if (cp_ctx->mac_key == NULL) {
WARN("Failure in mac_key buffer allocation.\n");
QATerr(QAT_F_QAT_CHACHAPOLY_SESSION_DATA_INIT,
ERR_R_MALLOC_FAILURE);
return -1;
}
}
if (key != NULL) {
memcpy(cp_ctx->cipher_key, key, QAT_CHACHA_KEY_SIZE);
cp_ctx->key_set = 1;
DUMPL("key", key, QAT_CHACHA_KEY_SIZE);
}
if (iv != NULL) {
memcpy(cp_ctx->nonce, iv, QAT_CHACHA20_POLY1305_MAX_IVLEN);
cp_ctx->iv_set = 1;
DUMPL("IV", iv, QAT_CHACHA20_POLY1305_MAX_IVLEN);
}
cp_ctx->session_data->sessionPriority = CPA_CY_PRIORITY_HIGH;
cp_ctx->session_data->symOperation = CPA_CY_SYM_OP_ALGORITHM_CHAINING;
cp_ctx->session_data->cipherSetupData.cipherAlgorithm = CPA_CY_SYM_CIPHER_CHACHA;
cp_ctx->session_data->cipherSetupData.cipherDirection = CPA_CY_SYM_CIPHER_DIRECTION_ENCRYPT;
cp_ctx->session_data->cipherSetupData.cipherKeyLenInBytes = QAT_CHACHA_KEY_SIZE;
/* Cipher key set NULL here as it will be set later. */
cp_ctx->session_data->cipherSetupData.pCipherKey = NULL;
cp_ctx->session_data->hashSetupData.hashAlgorithm = CPA_CY_SYM_HASH_POLY;
cp_ctx->session_data->hashSetupData.hashMode = CPA_CY_SYM_HASH_MODE_AUTH;
cp_ctx->session_data->hashSetupData.digestResultLenInBytes = QAT_POLY1305_DIGEST_SIZE;
cp_ctx->session_data->hashSetupData.authModeSetupData.authKey = NULL;
cp_ctx->session_data->hashSetupData.authModeSetupData.authKeyLenInBytes = 0;
cp_ctx->session_data->hashSetupData.authModeSetupData.aadLenInBytes = 0;
/* ChachaPoly performs the chacha(cipher) operation first and then
* the Poly MAC operation (hash) during encryption. */
cp_ctx->session_data->algChainOrder = CPA_CY_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH;
cp_ctx->session_data->digestIsAppended = CPA_FALSE;
cp_ctx->session_data->verifyDigest = CPA_FALSE;
if (!enc) {
cp_ctx->session_data->cipherSetupData.cipherDirection =
CPA_CY_SYM_CIPHER_DIRECTION_DECRYPT;
cp_ctx->session_data->algChainOrder = CPA_CY_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER;
}
/* Partial requests not supported for CHACHA-POLY in the driver. */
cp_ctx->session_data->partialsNotRequired = CPA_TRUE;
if (cp_ctx->opd == NULL) {
cp_ctx->opd = OPENSSL_zalloc(sizeof(template_opData));
if (cp_ctx->opd == NULL) {
WARN("memory allocation failed for symopData struct.\n");
QATerr(QAT_F_QAT_CHACHAPOLY_SESSION_DATA_INIT, ERR_R_MALLOC_FAILURE);
return 0;
}
/* Set cipher operation data template */
memcpy(cp_ctx->opd, &template_opData, sizeof(template_opData));
}
/* Set cipher key and IV. */
if (cp_ctx->key_set && cp_ctx->iv_set) {
if (qat_chacha20_poly1305_init_key_iv(cp_ctx, cp_ctx->cipher_key, cp_ctx->nonce)) {
/* Generate Poly1305 mac key */
if (!qat_chacha20_poly1305_mac_keygen(cp_ctx)) {
WARN("MAC keygen operation failed.\n");
QATerr(QAT_F_QAT_CHACHAPOLY_SESSION_DATA_INIT,
ERR_R_INTERNAL_ERROR);
return 0;
}
DEBUG("Poly1305 key generated\n");
}
cp_ctx->session_data->cipherSetupData.pCipherKey = cp_ctx->cipher_key;
if (cp_ctx->opd->pIv == NULL) {
cp_ctx->opd->pIv = qat_mem_alloc(QAT_CHACHA20_POLY1305_MAX_IVLEN, cp_ctx->qat_svm,
__FILE__, __LINE__);
if (cp_ctx->opd->pIv == NULL) {
WARN("Malloc Failure for opd->pIv.\n");
QATerr(QAT_F_QAT_CHACHAPOLY_SESSION_DATA_INIT, ERR_R_MALLOC_FAILURE);
return 0;
}
}
memcpy(cp_ctx->opd->pIv, cp_ctx->nonce, QAT_CHACHA20_POLY1305_MAX_IVLEN);
cp_ctx->opd->ivLenInBytes = QAT_CHACHA20_POLY1305_MAX_IVLEN;
}
/* Mark session context init as set. */
cp_ctx->context_params_set = 1;
DEBUG("Chachapoly context init set.\n");
return 1;
}
/******************************************************************************
* function:
* qat_chacha20_poly1305_init(EVP_CIPHER_CTX *ctx,
* const unsigned char *inkey,
* const unsigned char *iv,
* int enc)
*
* @param ctx [IN] - pointer to existing cipher ctx
* @param inKey [IN] - 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 parameters for this EVP context.
* This function can and will be **called multiple times** with some args
* being NULL.
*
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
int qat_chacha20_poly1305_init(QAT_PROV_CIPHER_CTX *ctx,
const unsigned char *user_key,
const unsigned char *iv, int enc)
#else
static int qat_chacha20_poly1305_init(EVP_CIPHER_CTX *ctx,
const unsigned char *user_key,
const unsigned char *iv, int enc)
#endif
{
int ret = 1;
if (unlikely(ctx == NULL)) {
WARN("cipher context is NULL.\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_INIT, QAT_R_CTX_NULL);
return 0;
}
#ifdef QAT_OPENSSL_PROVIDER
qat_chachapoly_ctx *cp_ctx = ctx->qat_cpctx;
#else
qat_chachapoly_ctx *cp_ctx = qat_chachapoly_data(ctx);
#endif
if (cp_ctx == NULL) {
WARN("chachapoly cipher data is NULL.\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_INIT, QAT_R_CHACHAPOLY_CTX_NULL);
return 0;
}
#ifndef ENABLE_QAT_SMALL_PKT_OFFLOAD
# ifndef QAT_OPENSSL_PROVIDER
EVP_CIPHER_CTX_set_cipher_data(ctx, cp_ctx->sw_ctx_cipher_data);
/* Run the software init function */
ret = EVP_CIPHER_meth_get_init(GET_SW_CHACHA_CTX)(ctx, user_key, iv, enc);
EVP_CIPHER_CTX_set_cipher_data(ctx, cp_ctx);
if (ret != 1)
goto init_err;
# endif
#endif
/* Return 1 since it's not an error if key and IV not set. */
if (!user_key && !iv) {
DEBUG("key and IV not set.\n");
return 1;
}
/* Initialize QAT session */
ret = qat_chachapoly_session_data_init(cp_ctx, user_key, iv, enc);
if (ret == 0) {
WARN("qat_chachapoly_session_data_init failed.\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_INIT, ERR_R_INTERNAL_ERROR);
goto init_err;
}
DEBUG("Init complete. ctx %p, cp_ctx %p, Session data %p\n", ctx, cp_ctx,
cp_ctx->session_data);
return ret;
init_err:
if (cp_ctx->opd) {
QAT_MEM_FREE_NONZERO_BUFF(cp_ctx->opd->pIv, cp_ctx->qat_svm);
OPENSSL_clear_free(cp_ctx->opd, sizeof(template_opData));
}
if (cp_ctx->session_data != NULL) {
OPENSSL_free(cp_ctx->session_data);
cp_ctx->session_data = NULL;
}
return ret;
}
/******************************************************************************
* function:
* qat_chachapoly_setup_op_params(qat_chachapoly_ctx *cp_ctx)
*
* @param ctx [IN] - pointer to existing ctx
*
* @retval 1 function succeeded
* @retval 0 function failed
*
* description:
* This function initialises the crypto session and flatbuffer lists to be
* passed to the driver.
*
******************************************************************************/
static int qat_chachapoly_setup_op_params(qat_chachapoly_ctx *cp_ctx)
{
int numBuffers = 1; /* Set numBuffers to 1. (For cipher and hash operations.) */
Cpa32U bufferMetaSize = 0;
Cpa32U sctx_size = 0;
CpaStatus status;
if (cp_ctx == NULL) {
WARN("chachapoly context cipher data is NULL.\n");
QATerr(QAT_F_QAT_CHACHAPOLY_SETUP_OP_PARAMS, QAT_R_CHACHAPOLY_CTX_NULL);
return 0;
}
status = cpaCySymSessionCtxGetSize(qat_instance_handles[cp_ctx->inst_num],
cp_ctx->session_data, &sctx_size);
if (status != CPA_STATUS_SUCCESS) {
WARN("Failed to get SessionCtx size.\n");
QATerr(QAT_F_QAT_CHACHAPOLY_SETUP_OP_PARAMS, ERR_R_INTERNAL_ERROR);
return 0;
}
DEBUG("Size of session ctx = %d\n", sctx_size);
cp_ctx->session_ctx = (CpaCySymSessionCtx) qat_mem_alloc(sctx_size, cp_ctx->qat_svm,
__FILE__, __LINE__);
if (cp_ctx->session_ctx == NULL) {
WARN("Memory alloc failed for session ctx\n");
QATerr(QAT_F_QAT_CHACHAPOLY_SETUP_OP_PARAMS, ERR_R_MALLOC_FAILURE);
return 0;
}
DUMP_SESSION_SETUP_DATA(cp_ctx->session_data);
/* Initialise Session data */
status = cpaCySymInitSession(qat_instance_handles[cp_ctx->inst_num],
qat_chachapoly_cb,
cp_ctx->session_data, cp_ctx->session_ctx);
if (status != CPA_STATUS_SUCCESS) {
WARN("cpaCySymInitSession failed! Status = %d\n", status);
if (((status == CPA_STATUS_RESTARTING) || (status == CPA_STATUS_FAIL))) {
CRYPTO_QAT_LOG("Failed to submit request to qat inst_num %d device_id %d\n",
cp_ctx->inst_num,
qat_instance_details[cp_ctx->inst_num].qat_instance_info.physInstId.packageId);
}
QATerr(QAT_F_QAT_CHACHAPOLY_SETUP_OP_PARAMS, ERR_R_INTERNAL_ERROR);
QAT_MEM_FREE_NONZERO_BUFF(cp_ctx->session_ctx, cp_ctx->qat_svm);
return 0;
}
/* Get buffer metasize */
status = cpaCyBufferListGetMetaSize(qat_instance_handles[cp_ctx->inst_num],
numBuffers, &bufferMetaSize);
if (status != CPA_STATUS_SUCCESS) {
WARN("cpaCyBufferListGetMetaSize failed for the instance id %d\n",
cp_ctx->inst_num);
QATerr(QAT_F_QAT_CHACHAPOLY_SETUP_OP_PARAMS, ERR_R_INTERNAL_ERROR);
QAT_MEM_FREE_NONZERO_BUFF(cp_ctx->session_ctx, cp_ctx->qat_svm);
return 0;
}
DEBUG("Buffer MetaSize : %d\n", bufferMetaSize);
cp_ctx->pSrcBufferList.numBuffers = numBuffers;
cp_ctx->pDstBufferList.numBuffers = numBuffers;
if (bufferMetaSize) {
cp_ctx->pSrcBufferList.pPrivateMetaData =
qat_mem_alloc(bufferMetaSize, cp_ctx->qat_svm, __FILE__, __LINE__);
cp_ctx->pDstBufferList.pPrivateMetaData =
qat_mem_alloc(bufferMetaSize, cp_ctx->qat_svm,__FILE__, __LINE__);
if (cp_ctx->pSrcBufferList.pPrivateMetaData == NULL ||
cp_ctx->pDstBufferList.pPrivateMetaData == NULL) {
WARN("QMEM alloc failed for PrivateData\n");
QATerr(QAT_F_QAT_CHACHAPOLY_SETUP_OP_PARAMS, ERR_R_MALLOC_FAILURE);
QAT_MEM_FREE_NONZERO_BUFF(cp_ctx->session_ctx, cp_ctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(cp_ctx->pSrcBufferList.pPrivateMetaData, cp_ctx->qat_svm);
QAT_MEM_FREE_NONZERO_BUFF(cp_ctx->pDstBufferList.pPrivateMetaData, cp_ctx->qat_svm);
return 0;
}
} else {
cp_ctx->pSrcBufferList.pPrivateMetaData = NULL;
cp_ctx->pDstBufferList.pPrivateMetaData = NULL;
}
cp_ctx->pDstBufferList.pUserData = NULL;
cp_ctx->pSrcBufferList.pUserData = NULL;
cp_ctx->pSrcBufferList.pBuffers = &cp_ctx->src_buffer;
cp_ctx->pDstBufferList.pBuffers = &cp_ctx->dst_buffer;
/* Mark session init as set.*/
cp_ctx->session_init = 1;
return 1;
}
/******************************************************************************
* function:
* qat_chacha20_poly1305_tls_cipher(EVP_CIPHER_CTX *evp_ctx, unsigned char *out,
* const unsigned char *in, size_t len)
*
* @param evp_ctx [IN] - pointer to existing context
* @param out [OUT] - output buffer for transform result
* @param in [IN] - input buffer
* @param len [IN] - length of input buffer
*
* @retval 0 failure
* @retval 1 success
*
* description:
* This function performs the cryptographic transform according to the
* parameters setup during initialisation.
*
* This is the function used in the TLS case.
*
******************************************************************************/
#ifdef QAT_OPENSSL_PROVIDER
static int qat_chacha20_poly1305_tls_cipher(QAT_PROV_CIPHER_CTX * ctx, unsigned char *out,
size_t *outl, const unsigned char *in, size_t len)
#else
static int qat_chacha20_poly1305_tls_cipher(EVP_CIPHER_CTX * ctx, unsigned char *out,
const unsigned char *in, size_t len)
#endif
{
int outlen = 0;
int job_ret = 0;
int cipher_len = 0;
int plen;
int enc;
CpaStatus status;
op_done_t op_done;
thread_local_variables_t *tlv = NULL;
qat_chachapoly_ctx *cp_ctx = NULL;
#if !defined(QAT_OPENSSL_PROVIDER) && !defined(ENABLE_QAT_SMALL_PKT_OFFLOAD)
int retVal = 0;
# endif
if (unlikely(ctx == NULL)) {
WARN("ctx parameter is NULL.\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_TLS_CIPHER, QAT_R_CTX_NULL);
return -1;
}
#ifdef QAT_OPENSSL_PROVIDER
cp_ctx = ctx->qat_cpctx;
#else
cp_ctx = qat_chachapoly_data(ctx);
#endif
if (cp_ctx == NULL) {
WARN("chachapoly context cipher data is NULL.\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_TLS_CIPHER,
QAT_R_CHACHAPOLY_CTX_NULL);
return -1;
}
plen = cp_ctx->tls_payload_length;
if (len != plen + QAT_POLY1305_BLOCK_SIZE) {
WARN("Invalid tls payload length\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_TLS_CIPHER,
QAT_R_INVALID_INPUT_LENGTH);
return -1;
}
#ifdef QAT_OPENSSL_PROVIDER
enc = QAT_PROV_GET_ENC(ctx);
#else
enc = EVP_CIPHER_CTX_encrypting(ctx);
#endif
DEBUG("ctx %p, cp_ctx %p, len %zu plen %d enc: %d\n",
ctx, cp_ctx, len, plen, enc);
#ifndef ENABLE_QAT_SMALL_PKT_OFFLOAD
cp_ctx->packet_size = len;
# ifdef QAT_OPENSSL_PROVIDER
if ( (len - QAT_POLY1305_BLOCK_SIZE) <=
qat_pkt_threshold_table_get_threshold(ctx->nid)) {
int sw_final_len = 0;
DEBUG("Using OpenSSL SW for Packetsize %zu\n", len);
if (!EVP_CipherUpdate(ctx->sw_ctx, out, &outlen, in, len))
goto cleanup;
if (!EVP_CipherFinal_ex(ctx->sw_ctx, out + outlen, &sw_final_len))
goto cleanup;
outlen = len + sw_final_len;
goto cleanup;
}
# else
if ( (len - QAT_POLY1305_BLOCK_SIZE) <=
qat_pkt_threshold_table_get_threshold(EVP_CIPHER_CTX_nid(ctx))) {
DEBUG("Using OpenSSL SW for Packetsize %zu\n", len);
EVP_CIPHER_CTX_set_cipher_data(ctx, cp_ctx->sw_ctx_cipher_data);
retVal = EVP_CIPHER_meth_get_do_cipher(GET_SW_CHACHA_CTX)
(ctx, out, in, len);
EVP_CIPHER_CTX_set_cipher_data(ctx, cp_ctx);
if (retVal)
outlen = len;
goto cleanup;
}
# endif
#endif
/* Set chachapoly opdata params and initialise session. */
if (cp_ctx->context_params_set && !cp_ctx->session_init) {
if (!qat_chachapoly_setup_op_params(cp_ctx)) {
WARN("chachapoly operational params setup failed.\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_TLS_CIPHER,
ERR_R_INTERNAL_ERROR);
goto tls_cipher_err;
}
}
/* Actual message length is the input length minus the tag length. */
cipher_len = len - QAT_POLY1305_DIGEST_SIZE;
DEBUG("InputLen %zu, CipherLen %d\n", len, cipher_len);
/* Allocate buffer for HASH and CIPHER operation. */
if (!cp_ctx->qat_svm)
cp_ctx->src_buffer.pData = qaeCryptoMemAlloc(len, __FILE__, __LINE__);
else
cp_ctx->src_buffer.pData = (Cpa8U *)in;
if (cp_ctx->src_buffer.pData == NULL) {
WARN("Unable to allocate memory for TLS header\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_TLS_CIPHER, ERR_R_MALLOC_FAILURE);
goto tls_cipher_err;
}
if (!cp_ctx->qat_svm) {
cp_ctx->dst_buffer.pData = cp_ctx->src_buffer.pData;
memcpy(cp_ctx->src_buffer.pData, in, cipher_len);
} else {
cp_ctx->dst_buffer.pData = (Cpa8U *)out;
}
tlv = qat_check_create_local_variables();
if (NULL == tlv) {
WARN("could not create local variables\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_TLS_CIPHER, ERR_R_INTERNAL_ERROR);
goto tls_cipher_err;
}
qat_init_op_done(&op_done);
if (op_done.job != NULL) {
if (qat_setup_async_event_notification(op_done.job) == 0) {
WARN("Failed to setup async event notification\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_TLS_CIPHER,
ERR_R_INTERNAL_ERROR);
qat_cleanup_op_done(&op_done);
goto tls_cipher_err;
}
}
cp_ctx->src_buffer.dataLenInBytes = cipher_len;
cp_ctx->pSrcBufferList.pUserData = NULL;
cp_ctx->dst_buffer.dataLenInBytes = cipher_len;
cp_ctx->pDstBufferList.pUserData = NULL;
cp_ctx->opd->sessionCtx = cp_ctx->session_ctx;
/* Set messageLenToCipherInBytes to the cipher buffer length. */
cp_ctx->opd->messageLenToCipherInBytes = cipher_len;
/* Set the offset where the tag need to be written in the destination buffer. */
cp_ctx->opd->hashStartSrcOffsetInBytes = cipher_len;
/* Set AAD */
cp_ctx->opd->pAdditionalAuthData = cp_ctx->tls_aad;
cp_ctx->opd->pDigestResult = cp_ctx->mac_key;
DUMPL("pDigestResult", cp_ctx->opd->pDigestResult, QAT_CHACHA_KEY_SIZE);
DUMPL("AAD Buffer", cp_ctx->opd->pAdditionalAuthData,
cp_ctx->session_data->hashSetupData.authModeSetupData.aadLenInBytes);
OPENSSL_cleanse(cp_ctx->opd->pIv, QAT_CHACHA20_POLY1305_MAX_IVLEN);
memcpy(cp_ctx->opd->pIv, cp_ctx->derived_iv, QAT_CHACHA20_POLY1305_MAX_IVLEN);
if (!is_instance_available(cp_ctx->inst_num)) {
WARN("QAT instance %d not available.\n", cp_ctx->inst_num);
QATerr(QAT_F_QAT_CHACHA20_POLY1305_TLS_CIPHER, ERR_R_INTERNAL_ERROR);
if (op_done.job != NULL)
qat_clear_async_event_notification(op_done.job);
qat_cleanup_op_done(&op_done);
goto tls_cipher_err;
}
DUMP_CP_PERFORM_OP(qat_instance_handles[cp_ctx->inst_num], cp_ctx->opd,
cp_ctx->pSrcBufferList, cp_ctx->pDstBufferList);
status = qat_sym_perform_op(cp_ctx->inst_num, &op_done, cp_ctx->opd,
&(cp_ctx->pSrcBufferList),
&(cp_ctx->pDstBufferList),
&(cp_ctx->session_data->verifyDigest));
if (status != CPA_STATUS_SUCCESS) {
if (((status == CPA_STATUS_RESTARTING) || (status == CPA_STATUS_FAIL))) {
CRYPTO_QAT_LOG("Failed to submit request to qat inst_num %d device_id %d - %s\n",
cp_ctx->inst_num,
qat_instance_details[cp_ctx->inst_num].qat_instance_info.physInstId.packageId, __func__);
QATerr(QAT_F_QAT_CHACHA20_POLY1305_TLS_CIPHER, ERR_R_INTERNAL_ERROR);
} else if (status == CPA_STATUS_UNSUPPORTED) {
WARN("Algorithm type unsupported in QAT_HW\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_TLS_CIPHER, QAT_R_ALGO_TYPE_UNSUPPORTED);
}
if (op_done.job != NULL)
qat_clear_async_event_notification(op_done.job);
qat_cleanup_op_done(&op_done);
goto tls_cipher_err;
}
QAT_INC_IN_FLIGHT_REQS(num_requests_in_flight, tlv);
if (qat_use_signals()) {
if (tlv->localOpsInFlight == 1) {
if (sem_post(&hw_polling_thread_sem) != 0) {
WARN("hw sem_post failed!, hw_polling_thread_sem address: %p.\n",
&hw_polling_thread_sem);
QATerr(QAT_F_QAT_CHACHA20_POLY1305_TLS_CIPHER,
ERR_R_INTERNAL_ERROR);
QAT_DEC_IN_FLIGHT_REQS(num_requests_in_flight, tlv);
goto tls_cipher_err;
}
}
}
if (enable_heuristic_polling) {
QAT_ATOMIC_INC(num_cipher_pipeline_requests_in_flight);
}
do {
if (op_done.job != NULL) {
/* If we get a failure on qat_pause_job then we will
not flag an error here and quit because we have
an asynchronous request in flight.
We don't want to start cleaning up data
structures that are still being used. If
qat_pause_job fails we will just yield and
loop around and try again until the request
completes and we can continue. */
if ((job_ret = qat_pause_job(op_done.job, ASYNC_STATUS_OK)) == 0)
sched_yield();
} else {
sched_yield();
}
} while (!op_done.flag ||
QAT_CHK_JOB_RESUMED_UNEXPECTEDLY(job_ret));
DUMP_CP_PERFORM_OP_OUTPUT(&(cp_ctx->session_data->verifyDigest),
cp_ctx->pDstBufferList);
QAT_DEC_IN_FLIGHT_REQS(num_requests_in_flight, tlv);
if (enc) {
outlen = len;
DEBUG("Encryption succeeded.\n");
} else {
if (op_done.verifyResult != CPA_TRUE) {
WARN("Verification of result failed\n");
QATerr(QAT_F_QAT_CHACHA20_POLY1305_TLS_CIPHER, ERR_R_INTERNAL_ERROR);
if (op_done.status == CPA_STATUS_FAIL) {
CRYPTO_QAT_LOG("Verification of result failed for qat inst_num %d device_id %d - %s\n",
cp_ctx->inst_num,
qat_instance_details[cp_ctx->inst_num].qat_instance_info.physInstId.packageId, __func__);
qat_cleanup_op_done(&op_done);
goto tls_cipher_err;
}
}
outlen = cipher_len;