Skip to content

Commit ec27f8e

Browse files
authored
Merge pull request #69 from SparkiDev/aes_gcm_perf_fix
AES-GCM: performance fix
2 parents 1162045 + f8bd3f0 commit ec27f8e

File tree

2 files changed

+72
-79
lines changed

2 files changed

+72
-79
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ Add `--enable-pwdbased` to the configure command above if PKCS#12 is used in Ope
8686

8787
Add to CPPFLAGS `-DHAVE_FFDHE_6144 -DHAVE_FFDHE_8192 -DFP_MAX_BITS=16384` to enable predefined 6144-bit and 8192-bit DH parameters.
8888

89+
Add to `--enable-hmac-copy` if performing HMAC repeatedly with the same key to improve performance. (Available with wolfSSL 5.7.8+.)
90+
8991
Add `--enable-sp=yes,asm' '--enable-sp-math-all'` to use SP Integer maths. Replace `-DFP_MAX_BITS=16384` with -DSP_INT_BITS=8192` when used.
9092

9193
Remove `-DWOLFSSL_PSS_LONG_SALT -DWOLFSSL_PSS_SALT_LEN_DISCOVER` and add `--enable-fips=v2` to the configure command above if building from a FIPS v2 bundle and not the git repository. Change `--enable-fips=v2` to `--enable-fips=ready` if using a FIPS Ready bundle.

src/wp_aes_aead.c

Lines changed: 70 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -441,26 +441,22 @@ static int wp_aead_set_param_tag(wp_AeadCtx* ctx,
441441
const OSSL_PARAM params[])
442442
{
443443
int ok = 1;
444-
const OSSL_PARAM* p;
444+
const OSSL_PARAM* p = params;
445445
size_t sz;
446+
void* vp = ctx->buf;
446447

447-
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG);
448-
if (p != NULL) {
449-
void* vp = ctx->buf;
450-
if (p->data != NULL) {
451-
if (!OSSL_PARAM_get_octet_string(p, &vp, EVP_GCM_TLS_TAG_LEN,
452-
&sz)) {
453-
ok = 0;
454-
}
455-
}
456-
else {
457-
sz = p->data_size;
458-
}
459-
if (ok && ((sz == 0) || ((p->data != NULL) && ctx->enc))) {
448+
if (p->data != NULL) {
449+
if (!OSSL_PARAM_get_octet_string(p, &vp, EVP_GCM_TLS_TAG_LEN, &sz)) {
460450
ok = 0;
461451
}
462-
ctx->tagLen = sz;
463452
}
453+
else {
454+
sz = p->data_size;
455+
}
456+
if (ok && ((sz == 0) || ((p->data != NULL) && ctx->enc))) {
457+
ok = 0;
458+
}
459+
ctx->tagLen = sz;
464460

465461
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
466462
return ok;
@@ -478,20 +474,17 @@ static int wp_aead_set_param_iv_len(wp_AeadCtx* ctx,
478474
const OSSL_PARAM params[])
479475
{
480476
int ok = 1;
481-
const OSSL_PARAM* p;
477+
const OSSL_PARAM* p = params;
482478
size_t sz;
483479

484-
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_IVLEN);
485-
if (p != NULL) {
486-
if (!OSSL_PARAM_get_size_t(p, &sz)) {
487-
ok = 0;
488-
}
489-
if (ok & ((sz == 0) || (sz > sizeof(ctx->aes.reg)))) {
490-
ok = 0;
491-
}
492-
if (ok) {
493-
ctx->ivLen = sz;
494-
}
480+
if (!OSSL_PARAM_get_size_t(p, &sz)) {
481+
ok = 0;
482+
}
483+
if (ok & ((sz == 0) || (sz > sizeof(ctx->aes.reg)))) {
484+
ok = 0;
485+
}
486+
if (ok) {
487+
ctx->ivLen = sz;
495488
}
496489

497490
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
@@ -510,21 +503,18 @@ static int wp_aead_set_param_tls1_aad(wp_AeadCtx* ctx,
510503
const OSSL_PARAM params[])
511504
{
512505
int ok = 1;
513-
const OSSL_PARAM* p;
506+
const OSSL_PARAM* p = params;
514507
size_t sz;
515508

516-
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD);
517-
if (p != NULL) {
518-
if (p->data_type != OSSL_PARAM_OCTET_STRING) {
509+
if (p->data_type != OSSL_PARAM_OCTET_STRING) {
510+
ok = 0;
511+
}
512+
else {
513+
sz = wp_aead_tls_init(ctx, p->data, p->data_size);
514+
if (sz == 0) {
519515
ok = 0;
520516
}
521-
else {
522-
sz = wp_aead_tls_init(ctx, p->data, p->data_size);
523-
if (sz == 0) {
524-
ok = 0;
525-
}
526-
ctx->tlsAadPadSz = sz;
527-
}
517+
ctx->tlsAadPadSz = sz;
528518
}
529519

530520
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
@@ -543,28 +533,25 @@ static int wp_aead_set_param_tls1_iv_fixed(wp_AeadCtx* ctx,
543533
const OSSL_PARAM params[])
544534
{
545535
int ok = 1;
546-
const OSSL_PARAM* p;
536+
const OSSL_PARAM* p = params;
547537

548-
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED);
549-
if (p != NULL) {
550-
if (p->data_type != OSSL_PARAM_OCTET_STRING) {
538+
if (p->data_type != OSSL_PARAM_OCTET_STRING) {
539+
ok = 0;
540+
}
541+
#ifdef WP_HAVE_AESGCM
542+
else if (ctx->mode == EVP_CIPH_GCM_MODE) {
543+
if (wp_aesgcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
551544
ok = 0;
552545
}
553-
#ifdef WP_HAVE_AESGCM
554-
else if (ctx->mode == EVP_CIPH_GCM_MODE) {
555-
if (wp_aesgcm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
556-
ok = 0;
557-
}
558-
}
559-
#endif
560-
#ifdef WP_HAVE_AESCCM
561-
else {
562-
if (wp_aesccm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
563-
ok = 0;
564-
}
546+
}
547+
#endif
548+
#ifdef WP_HAVE_AESCCM
549+
else {
550+
if (wp_aesccm_tls_iv_set_fixed(ctx, p->data, p->data_size) == 0) {
551+
ok = 0;
565552
}
566-
#endif
567553
}
554+
#endif
568555

569556
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
570557
return ok;
@@ -583,19 +570,16 @@ static int wp_aead_set_param_tls1_iv_rand(wp_AeadCtx* ctx,
583570
{
584571
#ifdef WP_HAVE_AESGCM
585572
int ok = 1;
586-
const OSSL_PARAM* p;
573+
const OSSL_PARAM* p = params;
587574

588-
p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV);
589-
if (p != NULL) {
590-
if (p->data == NULL) {
591-
ok = 0;
592-
}
593-
if (ok && (p->data_type != OSSL_PARAM_OCTET_STRING)) {
594-
ok = 0;
595-
}
596-
if (ok && (!wp_aesgcm_set_rand_iv(ctx, p->data, p->data_size))) {
597-
ok = 0;
598-
}
575+
if (p->data == NULL) {
576+
ok = 0;
577+
}
578+
if (ok && (p->data_type != OSSL_PARAM_OCTET_STRING)) {
579+
ok = 0;
580+
}
581+
if (ok && (!wp_aesgcm_set_rand_iv(ctx, p->data, p->data_size))) {
582+
ok = 0;
599583
}
600584

601585
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);
@@ -620,23 +604,30 @@ static int wp_aead_set_ctx_params(wp_AeadCtx* ctx, const OSSL_PARAM params[])
620604
{
621605
int ok = 1;
622606

623-
if (params != NULL) {
624-
if ((!wp_aead_set_param_tag(ctx, params))) {
625-
ok = 0;
607+
while ((params != NULL) && (params->key != NULL)) {
608+
if (XMEMCMP(params->key, OSSL_CIPHER_PARAM_AEAD_TAG,
609+
sizeof(OSSL_CIPHER_PARAM_AEAD_TAG)) == 0) {
610+
ok = wp_aead_set_param_tag(ctx, params);
626611
}
627-
if (ok && (!wp_aead_set_param_iv_len(ctx, params))) {
628-
ok = 0;
612+
else if (XMEMCMP(params->key, OSSL_CIPHER_PARAM_AEAD_IVLEN,
613+
sizeof(OSSL_CIPHER_PARAM_AEAD_IVLEN)) == 0) {
614+
ok = wp_aead_set_param_iv_len(ctx, params);
629615
}
630-
if (ok && (!wp_aead_set_param_tls1_aad(ctx, params))) {
631-
ok = 0;
616+
else if (XMEMCMP(params->key, OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
617+
sizeof(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD)) == 0) {
618+
ok = wp_aead_set_param_tls1_aad(ctx, params);
632619
}
633-
if (ok && (!wp_aead_set_param_tls1_iv_fixed(ctx, params))) {
634-
ok = 0;
620+
else if (XMEMCMP(params->key, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED,
621+
sizeof(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED)) == 0) {
622+
ok = wp_aead_set_param_tls1_iv_fixed(ctx, params);
635623
}
636-
if (ok && (ctx->mode == EVP_CIPH_GCM_MODE) &&
637-
(!wp_aead_set_param_tls1_iv_rand(ctx, params))) {
638-
ok = 0;
624+
else if (ok && (ctx->mode == EVP_CIPH_GCM_MODE) &&
625+
(XMEMCMP(params->key, OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED,
626+
sizeof(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED)) == 0)) {
627+
ok = wp_aead_set_param_tls1_iv_rand(ctx, params);
639628
}
629+
630+
params++;
640631
}
641632

642633
WOLFPROV_LEAVE(WP_LOG_CIPHER, __FILE__ ":" WOLFPROV_STRINGIZE(__LINE__), ok);

0 commit comments

Comments
 (0)