forked from wolfSSL/wolfProvider
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp_aes_stream.c
More file actions
687 lines (623 loc) · 22.1 KB
/
wp_aes_stream.c
File metadata and controls
687 lines (623 loc) · 22.1 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
/* wp_aes_stream.c
*
* Copyright (C) 2006-2024 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 <openssl/prov_ssl.h>
#include <wolfprovider/settings.h>
#include <wolfprovider/alg_funcs.h>
#if defined(WP_HAVE_AESCTR) || defined(WP_HAVE_AESCFB)
/**
* Data structure for AES ciphers that are streaming.
*/
typedef struct wp_AesStreamCtx {
/** wolfSSL AES object. */
Aes aes;
/** Cipher mode - CTR. */
int mode;
/** Length of key in bytes. */
size_t keyLen;
/** Length of IV in bytes */
size_t ivLen;
/** Operation being performed is encryption. */
unsigned int enc:1;
/** Current IV. */
unsigned char iv[AES_BLOCK_SIZE];
/** Original IV. */
unsigned char oiv[AES_BLOCK_SIZE];
} wp_AesStreamCtx;
/* Prototype for initialization to call. */
static int wp_aes_stream_set_ctx_params(wp_AesStreamCtx *ctx,
const OSSL_PARAM params[]);
/**
* Free the AES stream context object.
*
* @param [in, out] ctx AES stream context object.
*/
static void wp_aes_stream_freectx(wp_AesStreamCtx *ctx)
{
wc_AesFree(&ctx->aes);
OPENSSL_clear_free(ctx, sizeof(*ctx));
}
/**
* Duplicate the AES stream context object.
*
* @param [in] src AES stream context object to copy.
* @return NULL on failure.
* @return AES stream context object.
*/
static void *wp_aes_stream_dupctx(wp_AesStreamCtx *src)
{
wp_AesStreamCtx *dst = NULL;
if (wolfssl_prov_is_running()) {
dst = OPENSSL_malloc(sizeof(*dst));
}
if (dst != NULL) {
/* TODO: copying Aes may not work if it has pointers in it. */
XMEMCPY(dst, src, sizeof(*src));
}
return dst;
}
/**
* Parameters able to be retrieved for a cipher.
*/
static const OSSL_PARAM cipher_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_CUSTOM_IV, NULL),
OSSL_PARAM_int(OSSL_CIPHER_PARAM_HAS_RAND_KEY, NULL),
OSSL_PARAM_END
};
/**
* Returns the parameters that can be retrieved.
*
* @param [in] provCtx wolfProvider context object. Unused.
* @return Array of parameters.
*/
static const OSSL_PARAM *wp_cipher_gettable_params(
WOLFPROV_CTX *provCtx)
{
(void)provCtx;
return cipher_supported_gettable_params;
}
/**
* Get the values from the AES stream context for the parameters.
*
* @param [in, out] params Array of parameters to retrieve.
* @param [in] mode AES cipher mode.
* @param [in] kBits Number of bits in key.
* @param [in] ivBits Number of bits in IV.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aes_stream_get_params(OSSL_PARAM params[], unsigned int mode,
size_t kBits, size_t ivBits)
{
int ok = 1;
OSSL_PARAM *p;
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_MODE);
if ((p != NULL) && (!OSSL_PARAM_set_uint(p, mode))) {
ok = 0;
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_CUSTOM_IV);
if ((p != NULL) && (!OSSL_PARAM_set_int(p, 0))) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_HAS_RAND_KEY);
if ((p != NULL) && (!OSSL_PARAM_set_int(p, 0))) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_KEYLEN);
if ((p != NULL) && (!OSSL_PARAM_set_size_t(p, kBits / 8))) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_BLOCK_SIZE);
if ((p != NULL) && (!OSSL_PARAM_set_size_t(p, AES_BLOCK_SIZE))) {
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_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Returns the parameters of a cipher context that can be retrieved.
*
* @param [in] ctx AES stream context object. Unused.
* @param [in] provCtx wolfProvider context object. Unused.
* @return Array of parameters.
*/
static const OSSL_PARAM* wp_cipher_gettable_ctx_params(wp_AesStreamCtx* ctx,
WOLFPROV_CTX* provCtx)
{
/**
* Parameters able to be retrieved for a cipher context.
*/
static const OSSL_PARAM wp_cipher_supported_gettable_ctx_params[] = {
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL),
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL),
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_IV, NULL, 0),
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_UPDATED_IV, NULL, 0),
OSSL_PARAM_END
};
(void)ctx;
(void)provCtx;
return wp_cipher_supported_gettable_ctx_params;
}
/**
* Returns the parameters of a cipher context that can be set.
*
* @param [in] ctx AES stream context object. Unused.
* @param [in] provCtx wolfProvider context object. Unused.
* @return Array of parameters.
*/
static const OSSL_PARAM* wp_cipher_settable_ctx_params(wp_AesStreamCtx* ctx,
WOLFPROV_CTX *provCtx)
{
/**
* Parameters able to be set into a cipher context.
*/
static const OSSL_PARAM wp_cipher_supported_settable_ctx_params[] = {
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_NUM, NULL),
OSSL_PARAM_uint(OSSL_CIPHER_PARAM_USE_BITS, NULL),
OSSL_PARAM_END
};
(void)ctx;
(void)provCtx;
return wp_cipher_supported_settable_ctx_params;
}
/**
* Set the IV against the AES stream context object.
*
* @param [in, out] ctx AES stream context object.
* @param [in] iv IV data.
* @param [in] ivlen Length of IV data in bytes.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aes_init_iv(wp_AesStreamCtx *ctx, const unsigned char *iv,
size_t ivLen)
{
int ok = 1;
if (ivLen != ctx->ivLen) {
ok = 0;
}
if (ok) {
XMEMCPY(ctx->iv, iv, ivLen);
XMEMCPY(ctx->oiv, iv, ivLen);
}
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Initialization of an AES stream cipher.
*
* Internal. Handles both encrypt and ddecrypt.
*
* @param [in, out] ctx AES stream context object.
* @param [in] key Private key data. May be NULL.
* @param [in] keyLen Length of private key in bytes.
* @param [in] iv IV data. May be NULL.
* @param [in] ivLen Length of IV in bytes.
* @param [in] params Parameters to set against AES stream context object.
* @param [in] enc Initializing for encryption.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aes_stream_init(wp_AesStreamCtx *ctx, const unsigned char *key,
size_t keyLen, const unsigned char *iv, size_t ivLen,
const OSSL_PARAM params[], int enc)
{
int ok = 1;
ctx->enc = enc;
if (!wolfssl_prov_is_running()) {
ok = 0;
}
if (ok && (iv != NULL) && (!wp_aes_init_iv(ctx, iv, ivLen))) {
ok = 0;
}
if (ok && (key != NULL)) {
if (keyLen != ctx->keyLen) {
ok = 0;
}
if (ok) {
/* Decryption is the same as encryption with CTR mode. */
int rc = wc_AesSetKey(&ctx->aes, key, (word32)ctx->keyLen, iv,
AES_ENCRYPTION);
if (rc != 0) {
ok = 0;
}
}
}
else if (ok) {
/* TODO: don't reach in under the covers.
* Setting the key will reset this.
*/
ctx->aes.left = 0;
}
if (ok) {
ok = wp_aes_stream_set_ctx_params(ctx, params);
}
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Initialization of an AES stream cipher for encryption.
*
* @param [in, out] ctx AES stream context object.
* @param [in] key Private key data. May be NULL.
* @param [in] keyLen Length of private key in bytes.
* @param [in] iv IV data. May be NULL.
* @param [in] ivLen Length of IV in bytes.
* @param [in] params Parameters to set against AES stream context object.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aes_stream_einit(wp_AesStreamCtx *ctx, const unsigned char *key,
size_t keyLen, const unsigned char *iv, size_t ivLen,
const OSSL_PARAM params[])
{
return wp_aes_stream_init(ctx, key, keyLen, iv, ivLen, params, 1);
}
/**
* Initialization of an AES stream cipher for decryption.
*
* @param [in, out] ctx AES stream context object.
* @param [in] key Private key data. May be NULL.
* @param [in] keyLen Length of private key in bytes.
* @param [in] iv IV data. May be NULL.
* @param [in] ivLen Length of IV in bytes.
* @param [in] params Parameters to set against AES stream context object.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aes_stream_dinit(wp_AesStreamCtx *ctx, const unsigned char *key,
size_t keyLen, const unsigned char *iv, size_t ivLen,
const OSSL_PARAM params[])
{
return wp_aes_stream_init(ctx, key, keyLen, iv, ivLen, params, 0);
}
/**
* Encrypt/decrypt using AES-CTR or AES-CFB with wolfSSL.
*
* Assumes out has inLen bytes available.
* Assumes whole blocks only.
*
* @param [in] ctx AES stream context object.
* @param [out] out Buffer to hold encrypted/decrypted result.
* @param [in] in Data to encrypt/decrypt.
* @param [in] inLen Length of data to encrypt/decrypt in bytes.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aes_stream_doit(wp_AesStreamCtx *ctx, unsigned char *out,
const unsigned char *in, size_t inLen)
{
int ok = 0;
#ifdef WP_HAVE_AESCTR
if (ctx->mode == EVP_CIPH_CTR_MODE) {
int rc;
XMEMCPY(&ctx->aes.reg, ctx->iv, ctx->ivLen);
rc = wc_AesCtrEncrypt(&ctx->aes, out, in, (word32)inLen);
if (rc == 0) {
XMEMCPY(ctx->iv, ctx->aes.reg, ctx->ivLen);
ok = 1;
}
}
else
#endif
#ifdef WP_HAVE_AESCFB
if (ctx->mode == EVP_CIPH_CFB_MODE) {
int rc;
XMEMCPY(&ctx->aes.reg, ctx->iv, ctx->ivLen);
if (ctx->enc) {
rc = wc_AesCfbEncrypt(&ctx->aes, out, in, (word32)inLen);
}else {
rc = wc_AesCfbDecrypt(&ctx->aes, out, in, (word32)inLen);
}
if (rc == 0) {
XMEMCPY(ctx->iv, ctx->aes.reg, ctx->ivLen);
ok = 1;
}
}
else
#endif
{}
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Update encryption/decryption with more data.
*
* @param [in] ctx AES stream context object.
* @param [out] out Buffer to hold encrypted/decrypted result.
* @param [out] outLen Length of encrypted/decrypted data in bytes.
* @param [in] outSize Size of output buffer in bytes.
* @param [in] in Data to encrypt/decrypt.
* @param [in] inLen Length of data in bytes.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aes_stream_update(wp_AesStreamCtx *ctx, unsigned char *out,
size_t *outLen, size_t outSize, const unsigned char *in, size_t inLen)
{
int ok = 1;
if (outSize < inLen) {
ok = 0;
}
if (ok) {
ok = wp_aes_stream_doit(ctx, out, in, inLen);
}
if (ok) {
*outLen = inLen;
}
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Finalize AES stream encryption/decryption.
*
* @param [in] ctx AES stream context object.
* @param [out] out Buffer to hold encrypted/decrypted data.
* @param [out] outLen Length of data encrypted/decrypted in bytes.
* @param [in] outSize Size of buffer.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aes_stream_final(wp_AesStreamCtx* ctx, unsigned char *out,
size_t *outLen, size_t outSize)
{
/* Nothing to do as all the data has been processed in update. */
(void)ctx;
(void)out;
(void)outSize;
*outLen = 0;
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), 1);
return 1;
}
/**
* One-shot encryption/decryption operation.
*
* @param [in] ctx AES stream context object.
* @param [out] out Buffer to hold encrypted/decrypted result.
* @param [out] outLen Length of encrypted/decrypted data in bytes.
* @param [in] outSize Size of output buffer in bytes.
* @param [in] in Data to encrypt/decrypt.
* @param [in] inLen Length of data in bytes.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aes_stream_cipher(wp_AesStreamCtx* ctx, unsigned char* out,
size_t* outLen, size_t outSize, const unsigned char* in, size_t inLen)
{
int ok = 1;
if (!wolfssl_prov_is_running()) {
ok = 0;
}
if (ok && (outSize < inLen)) {
ok = 0;
}
if (ok && (!wp_aes_stream_doit(ctx, out, in, inLen))) {
ok = 0;
}
*outLen = inLen;
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Put values from the AES stream context object into parameters objects.
*
* @param [in] ctx AES stream context object.
* @param [in, out] params Array of parameters objects.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aes_stream_get_ctx_params(wp_AesStreamCtx* ctx,
OSSL_PARAM params[])
{
int ok = 1;
OSSL_PARAM* p;
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_IV);
if ((p != NULL) &&
(!OSSL_PARAM_set_octet_ptr(p, &ctx->oiv, ctx->ivLen)) &&
(!OSSL_PARAM_set_octet_string(p, &ctx->oiv, ctx->ivLen))) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_UPDATED_IV);
if ((p != NULL) &&
(!OSSL_PARAM_set_octet_ptr(p, &ctx->iv, ctx->ivLen)) &&
(!OSSL_PARAM_set_octet_string(p, &ctx->iv, ctx->ivLen))) {
ok = 0;
}
}
if (ok) {
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_NUM);
if ((p != NULL) && (!OSSL_PARAM_set_uint(p, 0))) {
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;
}
}
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Sets the parameters to use into AES stream context object.
*
* @param [in, out] ctx AES stream context object.
* @param [in] params Array of parameter objects.
* @return 1 on success.
* @return 0 on failure.
*/
static int wp_aes_stream_set_ctx_params(wp_AesStreamCtx *ctx,
const OSSL_PARAM params[])
{
int ok = 1;
(void)ctx;
if (params != NULL) {
unsigned int val;
/* TODO: can these be left out? */
if (ok && (!wp_params_get_uint(params, OSSL_CIPHER_PARAM_USE_BITS,
&val, NULL))) {
ok = 0;
}
(void)val;
if (ok && (!wp_params_get_uint(params, OSSL_CIPHER_PARAM_NUM,
&val, NULL))) {
ok = 0;
}
(void)val;
}
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
return ok;
}
/**
* Initialize the AES stream context object.
*
* @param [in, out] ctx AES stream context object.
* @param [in] kBits Number of bits in a valid key.
* @param [in] ivBits Number of bits in a valid IV. 0 indicates no IV.
* @param [in] mode AES stream mode: CTR.
* @return 1 on success.
* @return 0 on failure.
*/
static void wp_aes_stream_init_ctx(wp_AesStreamCtx* ctx, size_t kBits,
size_t ivBits, unsigned int mode)
{
ctx->keyLen = ((kBits) / 8);
ctx->ivLen = ((ivBits) / 8);
ctx->mode = mode;
}
/** Implements the get parameters API for a stream cipher. */
#define IMPLEMENT_AES_STREAM_GET_PARAMS(lcmode, UCMODE, kBits, ivBits) \
/** \
* Get the values from the AES stream context for the parameters. \
* \
* @param [in, out] params Array of parameters to retrieve. \
* @return 1 on success. \
* @return 0 on failure. \
*/ \
static int wp_aes_##kBits##_##lcmode##_get_params(OSSL_PARAM params[]) \
{ \
return wp_aes_stream_get_params(params, EVP_CIPH_##UCMODE##_MODE, kBits, \
ivBits); \
}
/** Implements the new context API for a stream cipher. */
#define IMPLEMENT_AES_STREAM_NEWCTX(lcmode, UCMODE, kBits, ivBits) \
/** \
* Create a new stream cipher context object. \
* \
* @param [in] provCtx Provider context object. \
* @return NULL on failure. \
* @return AEAD context object on success. \
*/ \
static wp_AesStreamCtx* wp_aes_stream_##kBits##_##lcmode##_newctx( \
WOLFPROV_CTX *provCtx) \
{ \
wp_AesStreamCtx *ctx = NULL; \
(void)provCtx; \
if (wolfssl_prov_is_running()) { \
ctx = OPENSSL_zalloc(sizeof(*ctx)); \
} \
if (ctx != NULL) { \
wp_aes_stream_init_ctx(ctx, kBits, ivBits, EVP_CIPH_##UCMODE##_MODE); \
} \
return ctx; \
}
/** Implements dispatch table for a stream cipher. */
#define IMPLEMENT_AES_STREAM_DISPATCH(mode, kBits, ivBits) \
const OSSL_DISPATCH wp_aes##kBits##mode##_functions[] = { \
{ OSSL_FUNC_CIPHER_NEWCTX, \
(DFUNC)wp_aes_stream_##kBits##_##mode##_newctx }, \
{ OSSL_FUNC_CIPHER_FREECTX, (DFUNC)wp_aes_stream_freectx }, \
{ OSSL_FUNC_CIPHER_DUPCTX, (DFUNC)wp_aes_stream_dupctx }, \
{ OSSL_FUNC_CIPHER_ENCRYPT_INIT, (DFUNC)wp_aes_stream_einit }, \
{ OSSL_FUNC_CIPHER_DECRYPT_INIT, (DFUNC)wp_aes_stream_dinit }, \
{ OSSL_FUNC_CIPHER_UPDATE, (DFUNC)wp_aes_stream_update }, \
{ OSSL_FUNC_CIPHER_FINAL, (DFUNC)wp_aes_stream_final }, \
{ OSSL_FUNC_CIPHER_CIPHER, (DFUNC)wp_aes_stream_cipher }, \
{ OSSL_FUNC_CIPHER_GET_PARAMS, \
(DFUNC)wp_aes_##kBits##_##mode##_get_params }, \
{ OSSL_FUNC_CIPHER_GET_CTX_PARAMS, (DFUNC)wp_aes_stream_get_ctx_params }, \
{ OSSL_FUNC_CIPHER_SET_CTX_PARAMS, (DFUNC)wp_aes_stream_set_ctx_params }, \
{ OSSL_FUNC_CIPHER_GETTABLE_PARAMS, (DFUNC)wp_cipher_gettable_params }, \
{ OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
(DFUNC)wp_cipher_gettable_ctx_params }, \
{ OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
(DFUNC)wp_cipher_settable_ctx_params }, \
{ 0, NULL } \
};
/** Implements the functions calling base functions for a stream cipher. */
#define IMPLEMENT_AES_STREAM(lcmode, UCMODE, kBits, ivBits) \
IMPLEMENT_AES_STREAM_GET_PARAMS(lcmode, UCMODE, kBits, ivBits) \
IMPLEMENT_AES_STREAM_NEWCTX(lcmode, UCMODE, kBits, ivBits) \
IMPLEMENT_AES_STREAM_DISPATCH(lcmode, kBits, ivBits)
/*
* AES CTR
*/
#ifdef WP_HAVE_AESCTR
/** wp_aes256ctr_functions */
IMPLEMENT_AES_STREAM(ctr, CTR, 256, 128)
/** wp_aes192ctr_functions */
IMPLEMENT_AES_STREAM(ctr, CTR, 192, 128)
/** wp_aes128ctr_functions */
IMPLEMENT_AES_STREAM(ctr, CTR, 128, 128)
#endif /* WP_HAVE_AESCTR */
/*
* AES CFB
*/
#ifdef WP_HAVE_AESCFB
/** wp_aes256cfb_functions */
IMPLEMENT_AES_STREAM(cfb, CFB, 256, 128)
/** wp_aes192cfb_functions */
IMPLEMENT_AES_STREAM(cfb, CFB, 192, 128)
/** wp_aes128cfb_functions */
IMPLEMENT_AES_STREAM(cfb, CFB, 128, 128)
#endif /* WP_HAVE_AESCFB */
#endif /* WP_HAVE_AESCTR || WP_HAVE_AESCFB */