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 */
6969static 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 */
8282static 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 */
637658IMPLEMENT_AES_STREAM (ctr , CTR , 256 , 128 )
638- /** wp_aes192cbc_functions */
659+ /** wp_aes192ctr_functions */
639660IMPLEMENT_AES_STREAM (ctr , CTR , 192 , 128 )
640- /** wp_aes128cbc_functions */
661+ /** wp_aes128ctr_functions */
641662IMPLEMENT_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+
0 commit comments