Skip to content

Commit e16f3e5

Browse files
committed
Add tests for key wrap and key wrap with padding for callback only path
1 parent 41dcbab commit e16f3e5

2 files changed

Lines changed: 64 additions & 4 deletions

File tree

.github/workflows/cryptocb-only.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ jobs:
162162
"--enable-tls13",
163163
"CPPFLAGS=-DWOLF_CRYPTO_CB_ONLY_SHA512 -DWOLFSSL_SWDEV_SHA512_GENERAL_ONLY"]},
164164
{"name": "aes", "minutes": 2,
165-
"comment": "WOLF_CRYPTO_CB_ONLY_AES: strips software AES; swdev provides the software path via cryptocb.",
165+
"comment": "WOLF_CRYPTO_CB_ONLY_AES: strips software AES; swdev provides the software path via cryptocb. aeskeywrap=padding covers RFC 3394 + RFC 5649 key wrap via swdev_aes_keywrap.",
166166
"configure": ["--enable-swdev", "--enable-cryptocb", "--enable-ecc",
167167
"--enable-rsa", "--enable-dh", "--enable-aesgcm",
168168
"--enable-aesccm", "--enable-aesctr", "--enable-aescfb",
169-
"--enable-aeskeywrap", "--enable-aessiv", "--enable-aesofb",
169+
"--enable-aeskeywrap=padding", "--enable-aessiv", "--enable-aesofb",
170170
"--enable-aesxts", "--enable-camellia", "--enable-chacha",
171171
"--enable-poly1305", "--enable-sha", "--enable-sha3",
172172
"--enable-shake128", "--enable-shake256", "--enable-blake2",
@@ -181,11 +181,11 @@ jobs:
181181
"--enable-ocspstapling2", "--enable-dtls", "--enable-dtls13",
182182
"--enable-tls13", "CPPFLAGS=-DWOLF_CRYPTO_CB_ONLY_AES"]},
183183
{"name": "aes-gcm-via-ecb", "minutes": 2,
184-
"comment": "Same as aes but tells swdev to refuse AES-GCM (SWDEV_AES_ONLYECB). That forces the parent's CB_ONLY_AES host-side GCM software path: GHASH runs on the host while AES-CTR blocks dispatch back through cryptocb ECB. The aes entry instead has swdev handle GCM end-to-end, so the host-side GCM path is otherwise uncovered.",
184+
"comment": "Same as aes but tells swdev to refuse AES-GCM (SWDEV_AES_ONLYECB). That forces the parent's CB_ONLY_AES host-side GCM software path: GHASH runs on the host while AES-CTR blocks dispatch back through cryptocb ECB. The aes entry instead has swdev handle GCM end-to-end, so the host-side GCM path is otherwise uncovered. swdev also refuses key wrap here, so RFC 3394 + RFC 5649 key wrap composes from cryptocb ECB on the host.",
185185
"configure": ["--enable-swdev", "--enable-cryptocb", "--enable-ecc",
186186
"--enable-rsa", "--enable-dh", "--enable-aesgcm",
187187
"--enable-aesccm", "--enable-aesctr", "--enable-aescfb",
188-
"--enable-aeskeywrap", "--enable-aessiv", "--enable-aesofb",
188+
"--enable-aeskeywrap=padding", "--enable-aessiv", "--enable-aesofb",
189189
"--enable-aesxts", "--enable-camellia", "--enable-chacha",
190190
"--enable-poly1305", "--enable-sha", "--enable-sha3",
191191
"--enable-shake128", "--enable-shake256", "--enable-blake2",

tests/swdev/swdev.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,62 @@ static int swdev_aes_ecb(wc_CryptoInfo* info)
566566
}
567567
#endif /* HAVE_AES_ECB || WOLFSSL_AES_DIRECT */
568568

569+
#if defined(HAVE_AES_KEYWRAP) && !defined(SWDEV_AES_ONLYECB)
570+
/* AES Key Wrap (RFC 3394) and, when built, Key Wrap with Padding (RFC 5649).
571+
* enc selects wrap (forward cipher) vs unwrap (inverse cipher); pad selects the
572+
* RFC 5649 variant. The wrap/unwrap helpers return the produced byte count,
573+
* which the cryptocb contract reports via outResSz with a 0 return. Gated like
574+
* swdev_aes_gcm so SWDEV_AES_ONLYECB instead forces the parent's CB_ONLY_AES
575+
* host-side key wrap (block ops dispatch back through cryptocb ECB). */
576+
static int swdev_aes_keywrap(wc_CryptoInfo* info)
577+
{
578+
Aes* aes = info->cipher.aeskeywrap.aes;
579+
const byte* in = info->cipher.aeskeywrap.in;
580+
word32 inSz = info->cipher.aeskeywrap.inSz;
581+
byte* out = info->cipher.aeskeywrap.out;
582+
word32 outSz = info->cipher.aeskeywrap.outSz;
583+
const byte* iv = info->cipher.aeskeywrap.iv;
584+
Aes shadow;
585+
int ret;
586+
int dir;
587+
588+
if (info->cipher.enc)
589+
dir = AES_ENCRYPTION;
590+
else
591+
dir = AES_DECRYPTION;
592+
593+
ret = swdev_aes_shadow_init(&shadow, aes, dir);
594+
if (ret != 0)
595+
return ret;
596+
597+
if (info->cipher.enc) {
598+
#ifdef WOLFSSL_AES_KEYWRAP_PADDING
599+
if (info->cipher.aeskeywrap.pad)
600+
ret = wc_AesKeyWrap_Pad_ex(&shadow, in, inSz, out, outSz, iv);
601+
else
602+
#endif
603+
ret = wc_AesKeyWrap_ex(&shadow, in, inSz, out, outSz, iv);
604+
}
605+
else {
606+
#ifdef WOLFSSL_AES_KEYWRAP_PADDING
607+
if (info->cipher.aeskeywrap.pad)
608+
ret = wc_AesKeyUnWrap_Pad_ex(&shadow, in, inSz, out, outSz, iv);
609+
else
610+
#endif
611+
ret = wc_AesKeyUnWrap_ex(&shadow, in, inSz, out, outSz, iv);
612+
}
613+
614+
wc_AesFree(&shadow);
615+
616+
if (ret < 0)
617+
return ret;
618+
619+
/* success: report produced length; cryptocb returns outResSz on ret==0 */
620+
info->cipher.aeskeywrap.outResSz = (word32)ret;
621+
return 0;
622+
}
623+
#endif /* HAVE_AES_KEYWRAP && !SWDEV_AES_ONLYECB */
624+
569625
/* SWDEV_AES_ONLYECB: when defined, swdev's AES backend returns
570626
* CRYPTOCB_UNAVAILABLE for AES-GCM so the parent's CB_ONLY_AES host-side
571627
* GCM software path runs (GHASH on the host; AES-CTR blocks dispatch back
@@ -773,6 +829,10 @@ WC_SWDEV_EXPORT int wc_SwDev_Callback(int devId, wc_CryptoInfo* info,
773829
#ifdef HAVE_AESCCM
774830
case WC_CIPHER_AES_CCM:
775831
return swdev_aes_ccm(info);
832+
#endif
833+
#if defined(HAVE_AES_KEYWRAP) && !defined(SWDEV_AES_ONLYECB)
834+
case WC_CIPHER_AES_KEYWRAP:
835+
return swdev_aes_keywrap(info);
776836
#endif
777837
default:
778838
return CRYPTOCB_UNAVAILABLE;

0 commit comments

Comments
 (0)