Skip to content

Commit 0a2e3f9

Browse files
authored
Merge pull request #34 from adrianjarc/addAESCFBEncryptionDecryption
Add AES CFB encryption/decryption
2 parents eee6d8b + d5940a2 commit 0a2e3f9

File tree

5 files changed

+137
-39
lines changed

5 files changed

+137
-39
lines changed

include/wolfprovider/alg_funcs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ typedef void (*DFUNC)(void);
109109
#define WP_NAMES_AES_192_CTR "AES-192-CTR"
110110
#define WP_NAMES_AES_128_CTR "AES-128-CTR"
111111

112+
#define WP_NAMES_AES_256_CFB "AES-256-CFB:2.16.840.1.101.3.4.1.44"
113+
#define WP_NAMES_AES_192_CFB "AES-192-CFB:2.16.840.1.101.3.4.1.24"
114+
#define WP_NAMES_AES_128_CFB "AES-128-CFB:2.16.840.1.101.3.4.1.4"
115+
112116
#define WP_NAMES_AES_256_WRAP \
113117
"AES-256-WRAP:id-aes256-wrap:AES256-WRAP:2.16.840.1.101.3.4.1.45"
114118
#define WP_NAMES_AES_192_WRAP \
@@ -265,6 +269,10 @@ extern const OSSL_DISPATCH wp_aes256ctr_functions[];
265269
extern const OSSL_DISPATCH wp_aes192ctr_functions[];
266270
extern const OSSL_DISPATCH wp_aes128ctr_functions[];
267271

272+
extern const OSSL_DISPATCH wp_aes256cfb_functions[];
273+
extern const OSSL_DISPATCH wp_aes192cfb_functions[];
274+
extern const OSSL_DISPATCH wp_aes128cfb_functions[];
275+
268276
extern const OSSL_DISPATCH wp_aes256wrap_functions[];
269277
extern const OSSL_DISPATCH wp_aes192wrap_functions[];
270278
extern const OSSL_DISPATCH wp_aes128wrap_functions[];

include/wolfprovider/settings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@
9191
#ifdef HAVE_AESCCM
9292
#define WP_HAVE_AESCCM
9393
#endif
94+
#ifdef WOLFSSL_AES_CFB
95+
#define WP_HAVE_AESCFB
96+
#endif
9497

9598
#ifndef WC_NO_RNG
9699
#define WP_HAVE_RANDOM

src/wp_aes_stream.c

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <wolfprovider/settings.h>
3030
#include <wolfprovider/alg_funcs.h>
3131

32-
#ifdef WP_HAVE_AESCTR
32+
#if defined(WP_HAVE_AESCTR) || defined(WP_HAVE_AESCFB)
3333

3434
/**
3535
* Data structure for AES ciphers that are streaming.
@@ -62,9 +62,9 @@ static int wp_aes_stream_set_ctx_params(wp_AesStreamCtx *ctx,
6262

6363

6464
/**
65-
* Free the AES block context object.
65+
* Free the AES stream context object.
6666
*
67-
* @param [in, out] ctx AES block context object.
67+
* @param [in, out] ctx AES stream context object.
6868
*/
6969
static void wp_aes_stream_freectx(wp_AesStreamCtx *ctx)
7070
{
@@ -73,11 +73,11 @@ static void wp_aes_stream_freectx(wp_AesStreamCtx *ctx)
7373
}
7474

7575
/**
76-
* Duplicate the AES block context object.
76+
* Duplicate the AES stream context object.
7777
*
78-
* @param [in] src AES block context object to copy.
78+
* @param [in] src AES stream context object to copy.
7979
* @return NULL on failure.
80-
* @return AES block context object.
80+
* @return AES stream context object.
8181
*/
8282
static void *wp_aes_stream_dupctx(wp_AesStreamCtx *src)
8383
{
@@ -121,7 +121,7 @@ static const OSSL_PARAM *wp_cipher_gettable_params(
121121
}
122122

123123
/**
124-
* Get the values from the AES block context for the parameters.
124+
* Get the values from the AES stream context for the parameters.
125125
*
126126
* @param [in, out] params Array of parameters to retrieve.
127127
* @param [in] mode AES cipher mode.
@@ -177,7 +177,7 @@ static int wp_aes_stream_get_params(OSSL_PARAM params[], unsigned int mode,
177177
/**
178178
* Returns the parameters of a cipher context that can be retrieved.
179179
*
180-
* @param [in] ctx AES block context object. Unused.
180+
* @param [in] ctx AES stream context object. Unused.
181181
* @param [in] provCtx wolfProvider context object. Unused.
182182
* @return Array of parameters.
183183
*/
@@ -203,7 +203,7 @@ static const OSSL_PARAM* wp_cipher_gettable_ctx_params(wp_AesStreamCtx* ctx,
203203
/**
204204
* Returns the parameters of a cipher context that can be set.
205205
*
206-
* @param [in] ctx AES block context object. Unused.
206+
* @param [in] ctx AES stream context object. Unused.
207207
* @param [in] provCtx wolfProvider context object. Unused.
208208
* @return Array of parameters.
209209
*/
@@ -224,9 +224,9 @@ static const OSSL_PARAM* wp_cipher_settable_ctx_params(wp_AesStreamCtx* ctx,
224224
}
225225

226226
/**
227-
* Set the IV against the AES block context object.
227+
* Set the IV against the AES stream context object.
228228
*
229-
* @param [in, out] ctx AES block context object.
229+
* @param [in, out] ctx AES stream context object.
230230
* @param [in] iv IV data.
231231
* @param [in] ivlen Length of IV data in bytes.
232232
* @return 1 on success.
@@ -249,16 +249,16 @@ static int wp_aes_init_iv(wp_AesStreamCtx *ctx, const unsigned char *iv,
249249
}
250250

251251
/**
252-
* Initialization of an AES block cipher.
252+
* Initialization of an AES stream cipher.
253253
*
254254
* Internal. Handles both encrypt and ddecrypt.
255255
*
256-
* @param [in, out] ctx AES block context object.
256+
* @param [in, out] ctx AES stream context object.
257257
* @param [in] key Private key data. May be NULL.
258258
* @param [in] keyLen Length of private key in bytes.
259259
* @param [in] iv IV data. May be NULL.
260260
* @param [in] ivLen Length of IV in bytes.
261-
* @param [in] params Parameters to set against AES block context object.
261+
* @param [in] params Parameters to set against AES stream context object.
262262
* @param [in] enc Initializing for encryption.
263263
* @return 1 on success.
264264
* @return 0 on failure.
@@ -307,14 +307,14 @@ static int wp_aes_stream_init(wp_AesStreamCtx *ctx, const unsigned char *key,
307307
}
308308

309309
/**
310-
* Initialization of an AES block cipher for encryption.
310+
* Initialization of an AES stream cipher for encryption.
311311
*
312-
* @param [in, out] ctx AES block context object.
312+
* @param [in, out] ctx AES stream context object.
313313
* @param [in] key Private key data. May be NULL.
314314
* @param [in] keyLen Length of private key in bytes.
315315
* @param [in] iv IV data. May be NULL.
316316
* @param [in] ivLen Length of IV in bytes.
317-
* @param [in] params Parameters to set against AES block context object.
317+
* @param [in] params Parameters to set against AES stream context object.
318318
* @return 1 on success.
319319
* @return 0 on failure.
320320
*/
@@ -326,14 +326,14 @@ static int wp_aes_stream_einit(wp_AesStreamCtx *ctx, const unsigned char *key,
326326
}
327327

328328
/**
329-
* Initialization of an AES block cipher for decryption.
329+
* Initialization of an AES stream cipher for decryption.
330330
*
331-
* @param [in, out] ctx AES block context object.
331+
* @param [in, out] ctx AES stream context object.
332332
* @param [in] key Private key data. May be NULL.
333333
* @param [in] keyLen Length of private key in bytes.
334334
* @param [in] iv IV data. May be NULL.
335335
* @param [in] ivLen Length of IV in bytes.
336-
* @param [in] params Parameters to set against AES block context object.
336+
* @param [in] params Parameters to set against AES stream context object.
337337
* @return 1 on success.
338338
* @return 0 on failure.
339339
*/
@@ -345,12 +345,12 @@ static int wp_aes_stream_dinit(wp_AesStreamCtx *ctx, const unsigned char *key,
345345
}
346346

347347
/**
348-
* Encrypt/decrypt using AES-ECB or AES-CBC with wolfSSL.
348+
* Encrypt/decrypt using AES-CTR or AES-CFB with wolfSSL.
349349
*
350350
* Assumes out has inLen bytes available.
351351
* Assumes whole blocks only.
352352
*
353-
* @param [in] ctx AES block context object.
353+
* @param [in] ctx AES stream context object.
354354
* @param [out] out Buffer to hold encrypted/decrypted result.
355355
* @param [in] in Data to encrypt/decrypt.
356356
* @param [in] inLen Length of data to encrypt/decrypt in bytes.
@@ -362,6 +362,7 @@ static int wp_aes_stream_doit(wp_AesStreamCtx *ctx, unsigned char *out,
362362
{
363363
int ok = 0;
364364

365+
#ifdef WP_HAVE_AESCTR
365366
if (ctx->mode == EVP_CIPH_CTR_MODE) {
366367
int rc;
367368

@@ -372,14 +373,34 @@ static int wp_aes_stream_doit(wp_AesStreamCtx *ctx, unsigned char *out,
372373
ok = 1;
373374
}
374375
}
376+
else
377+
#endif
378+
#ifdef WP_HAVE_AESCFB
379+
if (ctx->mode == EVP_CIPH_CFB_MODE) {
380+
int rc;
381+
382+
XMEMCPY(&ctx->aes.reg, ctx->iv, ctx->ivLen);
383+
if (ctx->enc) {
384+
rc = wc_AesCfbEncrypt(&ctx->aes, out, in, (word32)inLen);
385+
}else {
386+
rc = wc_AesCfbDecrypt(&ctx->aes, out, in, (word32)inLen);
387+
}
388+
if (rc == 0) {
389+
XMEMCPY(ctx->iv, ctx->aes.reg, ctx->ivLen);
390+
ok = 1;
391+
}
392+
}
393+
else
394+
#endif
395+
{}
375396

376397
return ok;
377398
}
378399

379400
/**
380401
* Update encryption/decryption with more data.
381402
*
382-
* @param [in] ctx AES block context object.
403+
* @param [in] ctx AES stream context object.
383404
* @param [out] out Buffer to hold encrypted/decrypted result.
384405
* @param [out] outLen Length of encrypted/decrypted data in bytes.
385406
* @param [in] outSize Size of output buffer in bytes.
@@ -407,9 +428,9 @@ static int wp_aes_stream_update(wp_AesStreamCtx *ctx, unsigned char *out,
407428
}
408429

409430
/**
410-
* Finalize AES block encryption/decryption.
431+
* Finalize AES stream encryption/decryption.
411432
*
412-
* @param [in] ctx AES block context object.
433+
* @param [in] ctx AES stream context object.
413434
* @param [out] out Buffer to hold encrypted/decrypted data.
414435
* @param [out] outLen Length of data encrypted/decrypted in bytes.
415436
* @param [in] outSize Size of buffer.
@@ -430,7 +451,7 @@ static int wp_aes_stream_final(wp_AesStreamCtx* ctx, unsigned char *out,
430451
/**
431452
* One-shot encryption/decryption operation.
432453
*
433-
* @param [in] ctx AES block context object.
454+
* @param [in] ctx AES stream context object.
434455
* @param [out] out Buffer to hold encrypted/decrypted result.
435456
* @param [out] outLen Length of encrypted/decrypted data in bytes.
436457
* @param [in] outSize Size of output buffer in bytes.
@@ -459,9 +480,9 @@ static int wp_aes_stream_cipher(wp_AesStreamCtx* ctx, unsigned char* out,
459480
}
460481

461482
/**
462-
* Put values from the AES block context object into parameters objects.
483+
* Put values from the AES stream context object into parameters objects.
463484
*
464-
* @param [in] ctx AES block context object.
485+
* @param [in] ctx AES stream context object.
465486
* @param [in, out] params Array of parameters objects.
466487
* @return 1 on success.
467488
* @return 0 on failure.
@@ -509,9 +530,9 @@ static int wp_aes_stream_get_ctx_params(wp_AesStreamCtx* ctx,
509530
}
510531

511532
/**
512-
* Sets the parameters to use into AES block context object.
533+
* Sets the parameters to use into AES stream context object.
513534
*
514-
* @param [in, out] ctx AES block context object.
535+
* @param [in, out] ctx AES stream context object.
515536
* @param [in] params Array of parameter objects.
516537
* @return 1 on success.
517538
* @return 0 on failure.
@@ -543,7 +564,7 @@ static int wp_aes_stream_set_ctx_params(wp_AesStreamCtx *ctx,
543564
}
544565

545566
/**
546-
* Initialize the AES block context object.
567+
* Initialize the AES stream context object.
547568
*
548569
* @param [in, out] ctx AES stream context object.
549570
* @param [in] kBits Number of bits in a valid key.
@@ -564,7 +585,7 @@ static void wp_aes_stream_init_ctx(wp_AesStreamCtx* ctx, size_t kBits,
564585
/** Implements the get parameters API for a stream cipher. */
565586
#define IMPLEMENT_AES_STREAM_GET_PARAMS(lcmode, UCMODE, kBits, ivBits) \
566587
/** \
567-
* Get the values from the AES block context for the parameters. \
588+
* Get the values from the AES stream context for the parameters. \
568589
* \
569590
* @param [in, out] params Array of parameters to retrieve. \
570591
* @return 1 on success. \
@@ -632,13 +653,26 @@ IMPLEMENT_AES_STREAM_DISPATCH(lcmode, kBits, ivBits)
632653
/*
633654
* AES CTR
634655
*/
635-
636-
/** wp_aes256cbc_functions */
656+
#ifdef WP_HAVE_AESCTR
657+
/** wp_aes256ctr_functions */
637658
IMPLEMENT_AES_STREAM(ctr, CTR, 256, 128)
638-
/** wp_aes192cbc_functions */
659+
/** wp_aes192ctr_functions */
639660
IMPLEMENT_AES_STREAM(ctr, CTR, 192, 128)
640-
/** wp_aes128cbc_functions */
661+
/** wp_aes128ctr_functions */
641662
IMPLEMENT_AES_STREAM(ctr, CTR, 128, 128)
642-
643663
#endif /* WP_HAVE_AESCTR */
644664

665+
/*
666+
* AES CFB
667+
*/
668+
#ifdef WP_HAVE_AESCFB
669+
/** wp_aes256cfb_functions */
670+
IMPLEMENT_AES_STREAM(cfb, CFB, 256, 128)
671+
/** wp_aes192cfb_functions */
672+
IMPLEMENT_AES_STREAM(cfb, CFB, 192, 128)
673+
/** wp_aes128cfb_functions */
674+
IMPLEMENT_AES_STREAM(cfb, CFB, 128, 128)
675+
#endif /* WP_HAVE_AESCFB */
676+
677+
#endif /* WP_HAVE_AESCTR || WP_HAVE_AESCFB */
678+

src/wp_wolfprov.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,16 @@ static const OSSL_ALGORITHM wolfprov_ciphers[] = {
331331
"" },
332332
#endif
333333

334+
#ifdef WP_HAVE_AESCFB
335+
/* AES-CFB */
336+
{ WP_NAMES_AES_256_CFB, WOLFPROV_PROPERTIES, wp_aes256cfb_functions,
337+
"" },
338+
{ WP_NAMES_AES_192_CFB, WOLFPROV_PROPERTIES, wp_aes192cfb_functions,
339+
"" },
340+
{ WP_NAMES_AES_128_CFB, WOLFPROV_PROPERTIES, wp_aes128cfb_functions,
341+
"" },
342+
#endif
343+
334344
#ifdef HAVE_AES_KEYWRAP
335345
/* AES Kwy Wrap - unpadded */
336346
{ WP_NAMES_AES_256_WRAP, WOLFPROV_PROPERTIES, wp_aes256wrap_functions,

test/test_cipher.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
#include "unit.h"
2222

2323
#if defined(WP_HAVE_DES3CBC) || defined(WP_HAVE_AESCBC) || \
24-
defined(WP_HAVE_AESECB) || defined(WP_HAVE_AESCTR)
24+
defined(WP_HAVE_AESECB) || defined(WP_HAVE_AESCTR) || \
25+
defined(WP_HAVE_AESCFB)
2526

2627
static int test_cipher_enc(const EVP_CIPHER *cipher,
2728
unsigned char *key, unsigned char *iv,
@@ -179,7 +180,8 @@ static int test_cipher_enc_dec(void *data, const char *cipher, int keyLen,
179180
#endif
180181

181182
#if defined(WP_HAVE_DES3CBC) || defined(WP_HAVE_AESCBC) || \
182-
defined(WP_HAVE_AESECB) || defined(WP_HAVE_AESCTR)
183+
defined(WP_HAVE_AESECB) || defined(WP_HAVE_AESCTR) || \
184+
defined(WP_HAVE_AESCFB)
183185

184186

185187
/******************************************************************************/
@@ -542,3 +544,44 @@ int test_aes256_ctr_stream(void *data)
542544

543545
#endif /* WP_HAVE_AESCTR */
544546

547+
#ifdef WP_HAVE_AESCFB
548+
549+
int test_aes128_cfb_stream(void *data)
550+
{
551+
int err;
552+
553+
err = test_stream_enc_dec(data, "AES-128-CFB", 16, 16, 16, 0);
554+
if (err == 0)
555+
err = test_stream_enc_dec(data, "AES-128-CFB", 16, 16, 1, 0);
556+
557+
return err;
558+
}
559+
560+
/******************************************************************************/
561+
562+
int test_aes192_cfb_stream(void *data)
563+
{
564+
int err;
565+
566+
err = test_stream_enc_dec(data, "AES-192-CFB", 24, 16, 15, 0);
567+
if (err == 0)
568+
err = test_stream_enc_dec(data, "AES-192-CFB", 24, 16, 2, 0);
569+
570+
return err;
571+
}
572+
573+
/******************************************************************************/
574+
575+
int test_aes256_cfb_stream(void *data)
576+
{
577+
int err;
578+
579+
err = test_stream_enc_dec(data, "AES-256-CFB", 32, 16, 14, 0);
580+
if (err == 0)
581+
err = test_stream_enc_dec(data, "AES-256-CFB", 32, 16, 3, 0);
582+
583+
return err;
584+
}
585+
586+
#endif /* WP_HAVE_AESCFB */
587+

0 commit comments

Comments
 (0)