forked from wolfSSL/wolfProvider
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp_aes_aead.c
More file actions
2168 lines (1952 loc) · 65 KB
/
wp_aes_aead.c
File metadata and controls
2168 lines (1952 loc) · 65 KB
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
/* wp_aes_aead.c
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfProvider.
*
* wolfProvider is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfProvider is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wolfProvider. If not, see <http://www.gnu.org/licenses/>.
*/
#include <openssl/err.h>
#include <openssl/proverr.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/params.h>
#include <openssl/evp.h>
#include <wolfprovider/settings.h>
#include <wolfprovider/alg_funcs.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#if defined(WP_HAVE_AESGCM) || defined(WP_HAVE_AESCCM)
/* For non-streaming AES-GCM, we have to spool all previous updates.
* This is the number of extra bytes we add when allocating the internal
* buffer used to spool the stream input. This way we if there is space
* we can use the existing buffer, reducing the number of full realloc + copy
* operations we need to do. Increase this number for better performance and
* more memory usage, decrease for worse performance but less overhead */
#ifndef WP_AES_GCM_EXTRA_BUF_LEN
#define WP_AES_GCM_EXTRA_BUF_LEN 128
#endif
/**
* Authenticated Encryption with Associated Data structure.
*/
typedef struct wp_AeadCtx {
/** wolfSSL AES encryption/decryption object. */
Aes aes;
/** wolfSSL HMAC object. */
Hmac hmac;
/** HMAC digest type. */
int hashType;
/** Provider context that we are constructed from. */
WOLFPROV_CTX* provCtx;
/** Cipher mode: GCM or CCM */
int mode;
/** Length of key. */
size_t keyLen;
/** Length of iv/nonce. */
size_t ivLen;
/** Authentication tag length. */
size_t tagLen;
/** TLS pad size. */
size_t tlsAadPadSz;
/** TLS additional authentication data size. */
size_t tlsAadLen;
/** Number of TLS records encrypted - GCM check for too many. */
uint64_t tlsEncRecords;
/** TLS version - CBC-HMAC-SHA does different things based on version. */
unsigned int tlsVersion;
/** Current state of IV/nonce. */
int ivState;
/** Initialized for encryption or decryption. */
unsigned int enc:1;
/** IV/nonce has been generated. */
unsigned int ivGen:1;
/** CCM needs to set IV specially. */
unsigned int ivSet:1;
/** GCM needs to know if key has been set. */
unsigned int keySet:1;
/** Cache of authentication status. */
unsigned int authErr:1;
/** AAD set with call to update. */
unsigned int aadSet:1;
/** CCM tag available and must be retrieved. */
unsigned int tagAvail:1;
/** Buffer to hold TLS AAD or tag. */
unsigned char buf[AES_BLOCK_SIZE];
/** IV/nonce data. */
unsigned char iv[AES_BLOCK_SIZE];
/** Original IV/nonce data. */
unsigned char oiv[AES_BLOCK_SIZE];
/** Length of AAD data cached. */
size_t aadLen;
/** CCM is not streaming and needs to cache AAD data. */
unsigned char* aad;
#if defined(WP_HAVE_AESGCM) && !defined(WOLFSSL_AESGCM_STREAM)
/** Length of data cached. */
size_t inLen;
/** CCM is not streaming and needs to cache AAD data. */
unsigned char* in;
/* Total buffer size */
size_t bufSize;
/* Original IV */
unsigned char origIv[AES_BLOCK_SIZE];
#endif
} wp_AeadCtx;
/* Both GCM and CCM have the same explicit IV length. */
#define EVP_AEAD_TLS_EXPLICIT_IV_LEN 8
/** IV has not be set. */
#define IV_STATE_UNINITIALISED 0 /* initial state is not initialized */
/** IV has been copied from a source. */
#define IV_STATE_COPIED 1
/** Fixed part of IV has been buffered. */
#define IV_STATE_BUFFERED 2
/** IV has been used and can't be reuse. */
#define IV_STATE_FINISHED 3
/** Uninitialized value for a field of type size_t. */
#define UNINITIALISED_SIZET ((size_t)-1)
/** AEAD cipher flags. */
#define AEAD_FLAGS (WP_CIPHER_FLAG_AEAD | WP_CIPHER_FLAG_CUSTOM_IV)
/** Implements the get_params function for an AEAD cipher. */
#define IMPLEMENT_AES_AEAD_GET_PARAMS(lc, UCMODE, flags, kbits, blkbits, \
ivbits) \
/** \
* Get the AEAD cipher parameters. \
* \
* @param [in, out] params Array of parameters and values. \
* @return 1 on success. \
* @return 0 on failure. \
*/ \
static int wp_aes_##kbits##_##lc##_get_params(OSSL_PARAM params[]) \
{ \
return wp_aead_get_params(params, EVP_CIPH_##UCMODE##_MODE, flags, kbits, \
blkbits, ivbits); \
}
/** Implements the newctx function for an AEAD cipher. */
#define IMPLEMENT_AES_AEAD_NEWCTX(lc, kbits) \
/** \
* Create a new AEAD context object. \
* \
* @param [in] provCtx Provider context object. \
* @return NULL on failure. \
* @return AEAD context object on success. \
*/ \
static void * wp_aes_##lc##_##kbits##lc##_newctx(WOLFPROV_CTX* provCtx) \
{ \
return wp_aes_##lc##_newctx(provCtx, kbits); \
}
/** Implements the dispatch table for AES AEAD ciphers. */
#define IMPLEMENT_AES_AEAD_DISPATCH(lc, kbits) \
const OSSL_DISPATCH wp_aes##kbits##lc##_functions[] = { \
{ OSSL_FUNC_CIPHER_NEWCTX, \
(DFUNC)wp_aes_##lc##_##kbits##lc##_newctx }, \
{ OSSL_FUNC_CIPHER_FREECTX, (DFUNC)wp_aes_##lc##_freectx }, \
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (DFUNC)wp_aes##lc##_einit }, \
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (DFUNC)wp_aes##lc##_dinit }, \
{ OSSL_FUNC_CIPHER_UPDATE, (DFUNC)wp_aes##lc##_stream_update }, \
{ OSSL_FUNC_CIPHER_FINAL, (DFUNC)wp_aes##lc##_stream_final }, \
{ OSSL_FUNC_CIPHER_CIPHER, (DFUNC)wp_aes##lc##_cipher }, \
{ OSSL_FUNC_CIPHER_GET_PARAMS, \
(DFUNC)wp_aes_##kbits##_##lc##_get_params }, \
{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (DFUNC)wp_aead_get_ctx_params }, \
{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (DFUNC)wp_aead_set_ctx_params }, \
{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS, (DFUNC)wp_aead_gettable_params }, \
{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
(DFUNC)wp_aead_gettable_ctx_params }, \
{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
(DFUNC)wp_aead_settable_ctx_params }, \
{ 0, NULL } \
}
/** Implements the functions calling base functions and the disapatch table. */
#define IMPLEMENT_AES_AEAD(lc, UCMODE, flags, kbits, blkbits, ivbits) \
IMPLEMENT_AES_AEAD_GET_PARAMS(lc, UCMODE, flags, kbits, blkbits, ivbits) \
IMPLEMENT_AES_AEAD_NEWCTX(lc, kbits) \
IMPLEMENT_AES_AEAD_DISPATCH(lc, kbits)
#ifdef WP_HAVE_AESGCM
/* Prototypes for get/set params API. */
static int wp_aesgcm_get_rand_iv(wp_AeadCtx* ctx, unsigned char* out,
size_t olen, int inc);
static int wp_aesgcm_set_rand_iv(wp_AeadCtx *ctx, unsigned char *in,
size_t inLen);
static int wp_aesgcm_tls_iv_set_fixed(wp_AeadCtx* ctx, unsigned char* iv,
size_t len);
#endif
#ifdef WP_HAVE_AESCCM
static int wp_aesccm_tls_iv_set_fixed(wp_AeadCtx* ctx, unsigned char* iv,
size_t len);
#endif
/**
* Initialize AEAD cipher for use with TLS. Return extra padding (tag length).
*
* @param [in, out] ctx AEAD context object.
* @param [in] aad Additional authentication data.
* @param [in] aadLen Length of AAD in bytes.
* @return Length of extra padding in bytes on success.
* @return 0 on failure.
*/
static int wp_aead_tls_init(wp_AeadCtx* ctx, unsigned char* aad, size_t aadLen)
{
int ok = 1;
unsigned char *buf = ctx->buf;
size_t len;
/* CCM will have a tag length set. */
size_t tagLen = (ctx->tagLen != UNINITIALISED_SIZET) ? ctx->tagLen :
EVP_GCM_TLS_TAG_LEN;
if (!wolfssl_prov_is_running()) {
ok = 0;
}
if (aadLen != EVP_AEAD_TLS1_AAD_LEN) {
ok = 0;
}
if (ok) {
/* Cache AAD. */
XMEMCPY(buf, aad, aadLen);
ctx->tlsAadLen = aadLen;
ctx->tlsEncRecords = 0;
len = (buf[aadLen - 2] << 8) | buf[aadLen - 1];
if (len >= EVP_AEAD_TLS_EXPLICIT_IV_LEN) {
len -= EVP_AEAD_TLS_EXPLICIT_IV_LEN;
}
else {
ok = 0;
}
}
/* If decrypting, correct for tag too. */
if (ok && (!ctx->enc)) {
if (len < tagLen) {
ok = 0;
}
if (ok) {
len -= tagLen;
}
}
if (ok) {
buf[aadLen - 2] = (unsigned char)(len >> 8);
buf[aadLen - 1] = (unsigned char)(len & 0xff);
}
if (!ok) {
tagLen = 0;
}
/* Extra padding: tag appended to record. */
return (int)tagLen;
}
#if defined(WP_HAVE_AESGCM) && !defined(WOLFSSL_AESGCM_STREAM) || \
defined(WP_HAVE_AESCCM)
/**
* Cache more Additional Authentication Data in AEAD context object.
*
* @param [in, out] ctx AEAD context object.
* @param [in] in More AAD data.
* @param [in] inLen Length of new AAD data.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aead_cache_aad(wp_AeadCtx *ctx, const unsigned char *in,
size_t inLen)
{
int ok = 1;
unsigned char *p;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aead_cache_aad");
if (inLen > 0) {
p = (unsigned char*)OPENSSL_realloc(ctx->aad, ctx->aadLen + inLen);
if (p == NULL) {
ok = 0;
}
if (ok) {
ctx->aad = p;
XMEMCPY(ctx->aad + ctx->aadLen, in, inLen);
ctx->aadLen += inLen;
}
}
if (ok) {
ctx->aadSet = 1;
}
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
#endif
#if defined(WP_HAVE_AESGCM) && !defined(WOLFSSL_AESGCM_STREAM)
/**
* Cache more input data in AEAD context object.
*
* @param [in, out] ctx AEAD context object.
* @param [in] in More AAD data.
* @param [in] inLen Length of new AAD data.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aead_cache_in(wp_AeadCtx *ctx, const unsigned char *in,
size_t inLen)
{
int ok = 1;
unsigned char *p;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aead_cache_in");
if (inLen > 0) {
if (inLen < (ctx->bufSize - ctx->inLen)) {
/* We can fit this new data into the extra space, dont realloc */
XMEMCPY(ctx->in + ctx->inLen, in, inLen);
ctx->inLen += inLen;
}
else {
p = (unsigned char*)OPENSSL_realloc(ctx->in,
ctx->inLen + inLen + WP_AES_GCM_EXTRA_BUF_LEN);
if (p == NULL) {
ok = 0;
}
if (ok) {
ctx->bufSize = ctx->inLen + inLen + WP_AES_GCM_EXTRA_BUF_LEN;
ctx->in = p;
XMEMCPY(ctx->in + ctx->inLen, in, inLen);
ctx->inLen += inLen;
}
}
}
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
#endif
/**
* Get the AEAD context parameters.
*
* @param [in] ctx AEAD context object.
* @param [in, out] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aead_get_ctx_params(wp_AeadCtx* ctx, OSSL_PARAM params[])
{
int ok = 1;
OSSL_PARAM* p;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aead_get_ctx_params");
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->ivLen)) {
ok = 0;
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->keyLen)) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAGLEN);
if (p != NULL) {
size_t tagLen = (ctx->tagLen != UNINITIALISED_SIZET) ? ctx->tagLen :
EVP_GCM_TLS_TAG_LEN;
if (!OSSL_PARAM_set_size_t(p, tagLen)) {
ok = 0;
}
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IV);
if (p != NULL) {
if (ctx->ivState == IV_STATE_UNINITIALISED) {
ok = 0;
}
if (ok && (ctx->ivLen > p->data_size)) {
ok = 0;
}
if (ok &&
(!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivLen)) &&
(!OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivLen))) {
ok = 0;
}
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_UPDATED_IV);
if (p != NULL) {
if (ctx->ivState == IV_STATE_UNINITIALISED) {
ok = 0;
}
if (ok && (ctx->ivLen > p->data_size)) {
ok = 0;
}
if (ok &&
(!OSSL_PARAM_set_octet_string(p, ctx->iv, ctx->ivLen)) &&
(!OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivLen))) {
ok = 0;
}
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD);
if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->tlsAadPadSz)) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TAG);
if (p != NULL) {
size_t sz = p->data_size;
if ((!ctx->enc) || (ctx->tagLen == UNINITIALISED_SIZET) ||
(sz == 0) || (sz > ctx->tagLen)) {
ok = 0;
}
if (ok && (!OSSL_PARAM_set_octet_string(p, ctx->buf, sz))) {
ok = 0;
}
ctx->tagAvail = 0;
}
}
#ifdef WP_HAVE_AESGCM
if (ok && (ctx->mode == EVP_CIPH_GCM_MODE)) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN);
if (p != NULL) {
if ((p->data == NULL) ||
(p->data_type != OSSL_PARAM_OCTET_STRING) ||
(!wp_aesgcm_get_rand_iv(ctx, p->data, p->data_size, 1))) {
ok = 0;
}
}
}
#endif
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set the AEAD tag from the parameters.
*
* @param [in, out] ctx AEAD context object.
* @param [in] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aead_set_param_tag(wp_AeadCtx* ctx,
const OSSL_PARAM params[])
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aead_set_param_tag");
const OSSL_PARAM* p = params;
size_t sz;
void* vp = ctx->buf;
if (p->data != NULL) {
if (!OSSL_PARAM_get_octet_string(p, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) {
ok = 0;
}
}
else {
sz = p->data_size;
}
if (ok && ((sz == 0) || ((p->data != NULL) && ctx->enc))) {
ok = 0;
}
ctx->tagLen = sz;
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set the IV length from the parameters.
*
* @param [in, out] ctx AEAD context object.
* @param [in] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aead_set_param_iv_len(wp_AeadCtx* ctx,
const OSSL_PARAM params[])
{
int ok = 1;
const OSSL_PARAM* p = params;
size_t sz;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aead_set_param_iv_len");
if (!OSSL_PARAM_get_size_t(p, &sz)) {
ok = 0;
}
if (ok & ((sz == 0) || (sz > sizeof(ctx->aes.reg)))) {
ok = 0;
}
if (ok) {
ctx->ivLen = sz;
}
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set the TLS1 AAD from the parameters.
*
* @param [in, out] ctx AEAD context object.
* @param [in] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aead_set_param_tls1_aad(wp_AeadCtx* ctx,
const OSSL_PARAM params[])
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aead_set_param_tls1_aad");
const OSSL_PARAM* p = params;
size_t sz;
if (p->data_type != OSSL_PARAM_OCTET_STRING) {
ok = 0;
}
else {
sz = wp_aead_tls_init(ctx, p->data, p->data_size);
if (sz == 0) {
ok = 0;
}
ctx->tlsAadPadSz = sz;
}
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set the TLS1 fixed IV from the parameters.
*
* @param [in, out] ctx AEAD context object.
* @param [in] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aead_set_param_tls1_iv_fixed(wp_AeadCtx* ctx,
const OSSL_PARAM params[])
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aead_set_param_tls1_iv_fixed");
const OSSL_PARAM* p = params;
if (p->data_type != OSSL_PARAM_OCTET_STRING) {
ok = 0;
}
#ifdef WP_HAVE_AESGCM
else if (ctx->mode == EVP_CIPH_GCM_MODE) {
if (wp_aesgcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
ok = 0;
}
}
#endif
#ifdef WP_HAVE_AESCCM
else {
if (wp_aesccm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
ok = 0;
}
}
#endif
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set a random IV with fixed part from the parameters.
*
* @param [in, out] ctx AEAD context object.
* @param [in] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aead_set_param_tls1_iv_rand(wp_AeadCtx* ctx,
const OSSL_PARAM params[])
{
#ifdef WP_HAVE_AESGCM
int ok = 1;
const OSSL_PARAM* p = params;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aead_set_param_tls1_iv_rand");
if (p->data == NULL) {
ok = 0;
}
if (ok && (p->data_type != OSSL_PARAM_OCTET_STRING)) {
ok = 0;
}
if (ok && (!wp_aesgcm_set_rand_iv(ctx, p->data, p->data_size))) {
ok = 0;
}
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
#else
(void)ctx;
(void)params;
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 1);
return 1;
#endif
}
/**
* Set the AEAD context parameters.
*
* @param [in, out] ctx AEAD context object.
* @param [in] params Array of parameters and values.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aead_set_ctx_params(wp_AeadCtx* ctx, const OSSL_PARAM params[])
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aead_set_ctx_params");
while ((params != NULL) && (params->key != NULL)) {
if (XMEMCMP(params->key, OSSL_CIPHER_PARAM_AEAD_TAG,
sizeof(OSSL_CIPHER_PARAM_AEAD_TAG)) == 0) {
ok = wp_aead_set_param_tag(ctx, params);
}
else if (XMEMCMP(params->key, OSSL_CIPHER_PARAM_AEAD_IVLEN,
sizeof(OSSL_CIPHER_PARAM_AEAD_IVLEN)) == 0) {
ok = wp_aead_set_param_iv_len(ctx, params);
}
else if (XMEMCMP(params->key, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
sizeof(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD)) == 0) {
ok = wp_aead_set_param_tls1_aad(ctx, params);
}
else if (XMEMCMP(params->key, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED,
sizeof(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED)) == 0) {
ok = wp_aead_set_param_tls1_iv_fixed(ctx, params);
}
else if (ok && (ctx->mode == EVP_CIPH_GCM_MODE) &&
(XMEMCMP(params->key, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED,
sizeof(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED)) == 0)) {
ok = wp_aead_set_param_tls1_iv_rand(ctx, params);
}
params++;
}
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Return an array of supported gettable parameters for the AEAD cipher.
*
* @param [in] provCtx Provider context object. Unused.
* @return Array of parameters with data type.
*/
static const OSSL_PARAM *wp_aead_gettable_params(WOLFPROV_CTX* provCtx)
{
/**
* Supported gettable parameters for AEAD cipher.
*/
static const OSSL_PARAM wp_aead_supported_gettable_params[] = {
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_MODE, NULL),
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_BLOCK_SIZE, NULL),
OSSL_PARAM_int(OSSL_CIPHER_PARAM_AEAD, NULL),
OSSL_PARAM_int(OSSL_CIPHER_PARAM_CUSTOM_IV, NULL),
OSSL_PARAM_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY, NULL),
OSSL_PARAM_END
};
(void)provCtx;
return wp_aead_supported_gettable_params;
}
/**
* Get the AEAD cipher parameters.
*
* @param [in, out] params Array of parameters and values.
* @param [in] md Message digest id.
* @param [in] flags Flags of cipher.
* @param [in] keyBits Size of key in bits.
* @param [in] blkBits Size of block in bits.
* @param [in] ivBits Size of IV/nonce in bits.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aead_get_params(OSSL_PARAM params[], unsigned int md,
uint64_t flags, size_t keyBits, size_t blkBits, size_t ivBits)
{
int ok = 1;
OSSL_PARAM* p;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aead_get_params");
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_MODE);
if ((p != NULL) && (!OSSL_PARAM_set_uint(p, md))) {
ok = 0;
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_AEAD);
if ((p != NULL) &&
(!OSSL_PARAM_set_int(p, (flags & WP_CIPHER_FLAG_AEAD) != 0))) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_CUSTOM_IV);
if ((p != NULL) &&
(!OSSL_PARAM_set_int(p, (flags & WP_CIPHER_FLAG_CUSTOM_IV) != 0))) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_HAS_RAND_KEY);
if ((p != NULL) &&
(!OSSL_PARAM_set_int(p, (flags & WP_CIPHER_FLAG_RAND_KEY) != 0))) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
if ((p != NULL) && (!OSSL_PARAM_set_size_t(p, keyBits / 8))) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_BLOCK_SIZE);
if ((p != NULL) && (!OSSL_PARAM_set_size_t(p, blkBits / 8))) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_IVLEN);
if ((p != NULL) && (!OSSL_PARAM_set_size_t(p, ivBits / 8))) {
ok = 0;
}
}
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Return an array of supported gettable parameters for the AEAD context.
*
* @param [in] ctx AEAD context object. Unused.
* @param [in] provCtx Provider context object. Unused.
* @return Array of parameters with data type.
*/
static const OSSL_PARAM *wp_aead_gettable_ctx_params(wp_AeadCtx* ctx,
WOLFPROV_CTX* provCtx)
{
/**
* Supported gettable parameters for AEAD context.
*/
static const OSSL_PARAM wp_aead_supported_gettable_ctx_params[] = {
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TAGLEN, NULL),
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0),
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0),
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL),
#ifdef WP_HAVE_AESGCM
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, NULL,
0),
#endif
OSSL_PARAM_END
};
(void)ctx;
(void)provCtx;
return wp_aead_supported_gettable_ctx_params;
}
/**
* Return an array of supported settable parameters for the AEAD context.
*
* @param [in] ctx AEAD context object. Unused.
* @param [in] provCtx Provider context object. Unused.
* @return Array of parameters with data type.
*/
static const OSSL_PARAM *wp_aead_settable_ctx_params(wp_AeadCtx* ctx,
WOLFPROV_CTX* provCtx)
{
/**
* Supported settable parameters for AEAD context.
*/
static const OSSL_PARAM wp_aead_supported_settable_ctx_params[] = {
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_IVLEN, NULL),
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, NULL, 0),
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0),
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, NULL, 0),
#ifdef WP_HAVE_AESGCM
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, NULL,
0),
#endif
OSSL_PARAM_END
};
(void)ctx;
(void)provCtx;
return wp_aead_supported_settable_ctx_params;
}
#ifdef WP_HAVE_AESGCM
/*
* AES-GCM
*/
/**
* Get the random part of the IV/nonce.
*
* FIPS 140 requires the encryptor to generate a random part for the IV.
* This has to be sent to the other side.
*
* @param [in, out] ctx AEAD context object.
* @param [out] out Buffer to hold random part of IV.
* @param [in] olen Length of random in bytes.
* @param [in] inc Whether to increment IV after copy.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aesgcm_get_rand_iv(wp_AeadCtx* ctx, unsigned char* out,
size_t olen, int inc)
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aesgcm_get_rand_iv");
/* Ensure that an IV/nonce has not been generated or a key set. */
if ((!ctx->ivGen) || (!ctx->keySet)) {
ok = 0;
}
if (ok) {
#ifdef WOLFSSL_AESGCM_STREAM
int rc;
rc = wc_AesGcmInit(&ctx->aes, NULL, 0, ctx->iv, (word32)ctx->ivLen);
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmInit", rc);
ok = 0;
}
#endif
}
if (ok) {
/* Use all the IV/nonce length if none specified or too much. */
if ((olen == 0) || (olen > ctx->ivLen)) {
olen = ctx->ivLen;
}
XMEMCPY(out, ctx->iv + ctx->ivLen - olen, olen);
#ifndef WOLFSSL_AESGCM_STREAM
XMEMCPY(ctx->origIv, ctx->iv, ctx->ivLen);
#endif
if (inc) {
int i;
unsigned char* p = ctx->iv + ctx->ivLen - 8;
for (i = 7; i >= 0 && (++p[i]) == 0; i--) {
/* Nothing to do. */
}
}
ctx->ivState = IV_STATE_COPIED;
}
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set the random part of the IV/nonce.
*
* FIPS 140 requires the encryptor to generate a random part for the IV.
*
* @param [in, out] ctx AEAD context object.
* @param [in] in Random part of the IV/nonce to set.
* @param [in] inLen Length of random.
*/
static int wp_aesgcm_set_rand_iv(wp_AeadCtx *ctx, unsigned char *in,
size_t inLen)
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aesgcm_set_rand_iv");
/* Ensure that an IV/nonce has not been generated or a key set and this
* is the decrypt side.
*/
if ((!ctx->ivGen) || (!ctx->keySet) || (ctx->enc)) {
ok = 0;
}
else {
#ifndef WOLFSSL_AESGCM_STREAM
XMEMCPY(ctx->origIv, ctx->iv, ctx->ivLen);
#endif
XMEMCPY(ctx->iv + ctx->ivLen - inLen, in, inLen);
ctx->ivState = IV_STATE_COPIED;
}
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Set the fixed part of the nonce for GCM cipher and generate random for rest.
*
* @param [in, out] ctx AEAD context object.
* @param [in] iv Fixed part of IV/nonce.
* @param [in] len Length of fixed part or -1 to restore IV/nonce.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aesgcm_tls_iv_set_fixed(wp_AeadCtx* ctx, unsigned char* iv,
size_t len)
{
int ok = 1;
WOLFPROV_ENTER(WP_LOG_COMP_AES, "wp_aesgcm_tls_iv_set_fixed");
/* Special case: -1 length restores whole IV */
if (len == (size_t)-1) {
XMEMCPY(ctx->iv, iv, ctx->ivLen);
ctx->ivGen = 1;
ctx->ivState = IV_STATE_BUFFERED;
}
else {
/* Fixed field must be at least 4 bytes and invocation field at least 8
*/
if ((len < EVP_GCM_TLS_FIXED_IV_LEN) ||
(ctx->ivLen - (int)len) < EVP_GCM_TLS_EXPLICIT_IV_LEN) {
return 0;
}
if (ctx->enc) {
int rc;
#ifndef WP_SINGLE_THREADED
if (!wp_provctx_lock_rng(ctx->provCtx)) {
ok = 0;
}
#endif
if (ok) {
rc = wc_AesGcmSetIV(&ctx->aes, (word32)ctx->ivLen, iv,
(word32)len, wp_provctx_get_rng(ctx->provCtx));
if (rc != 0) {
WOLFPROV_MSG_DEBUG_RETCODE(WP_LOG_LEVEL_DEBUG, "wc_AesGcmSetIV", rc);
ok = 0;
}
}
#ifndef WP_SINGLE_THREADED
wp_provctx_unlock_rng(ctx->provCtx);
#endif
if (ok && len > 0) {
XMEMCPY(ctx->iv, ctx->aes.reg, ctx->ivLen);
ctx->ivSet = 1;
}
}
else {
XMEMCPY(ctx->iv, iv, len);
}
ctx->ivGen = 1;
ctx->ivState = IV_STATE_BUFFERED;
}
WOLFPROV_LEAVE(WP_LOG_COMP_AES, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Initialize AES GCM cipher for encryption.