@@ -10491,6 +10491,161 @@ int test_wc_AesKeyWrap_Pad(void)
1049110491 WC_NO_ERR_TRACE(BAD_KEYWRAP_IV_E));
1049210492 }
1049310493
10494+ #if defined(WOLFSSL_AES_128) || defined(WOLFSSL_AES_192) || \
10495+ defined(WOLFSSL_AES_256)
10496+ /* --- Extended coverage for every enabled key size (128/192/256):
10497+ * boundary-size known-answer vectors independently generated with the
10498+ * pyca/cryptography library (which reproduces the RFC 5649 Section 6
10499+ * published vectors), an exhaustive round-trip over all input sizes
10500+ * 1..64, and in-place (in==out) operation. Plaintext is 0xA0,0xA1,...
10501+ * The boundary sizes pin the single->multi-block transition (8 vs 9
10502+ * octets) and the exact-8-multiple zero-padding cases that the 7- and
10503+ * 20-octet vectors never reach. --- */
10504+ {
10505+ #ifdef WOLFSSL_AES_128
10506+ static const byte k128_1[] = {
10507+ 0xdc, 0x0c, 0xed, 0x32, 0x50, 0xa4, 0x92, 0x77, 0x59, 0xb4, 0xe9, 0x28,
10508+ 0x73, 0x2e, 0x16, 0x8a };
10509+ static const byte k128_8[] = {
10510+ 0x23, 0xe1, 0xcd, 0x73, 0x92, 0xf0, 0xcc, 0x69, 0xdc, 0x20, 0xdf, 0x56,
10511+ 0x48, 0x9f, 0xfd, 0xd7 };
10512+ static const byte k128_9[] = {
10513+ 0xe6, 0xe7, 0x6a, 0xfc, 0xf1, 0xf7, 0x1f, 0x43, 0x63, 0x4b, 0x96, 0x91,
10514+ 0x9c, 0x1e, 0x36, 0xa9, 0x5b, 0xf7, 0xb0, 0x26, 0x0f, 0x51, 0x9b, 0x4b };
10515+ static const byte k128_16[] = {
10516+ 0xa7, 0x41, 0x46, 0x28, 0x3b, 0x00, 0x85, 0x06, 0x23, 0xd3, 0x02, 0xf4,
10517+ 0x57, 0xb1, 0x8c, 0x96, 0xac, 0xe5, 0xb5, 0xd3, 0x64, 0x7c, 0xc2, 0xf1 };
10518+ static const byte k128_17[] = {
10519+ 0x8d, 0x69, 0xe5, 0xc5, 0x01, 0x98, 0x70, 0xd3, 0x50, 0x37, 0x3a, 0x00,
10520+ 0xa8, 0xe3, 0xa5, 0x32, 0xdf, 0xce, 0x76, 0x8a, 0x6b, 0x79, 0xef, 0x2c,
10521+ 0x34, 0xcf, 0xed, 0x5c, 0xb4, 0x09, 0xff, 0xf4 };
10522+ static const byte k128_31[] = {
10523+ 0x50, 0x3e, 0xc4, 0xff, 0x2e, 0xd3, 0x01, 0x14, 0xfa, 0x5a, 0x02, 0x47,
10524+ 0x9f, 0x20, 0x4c, 0xb1, 0xd8, 0xcb, 0xa2, 0xa3, 0xa3, 0x7d, 0x7b, 0xa5,
10525+ 0x60, 0x77, 0x01, 0x46, 0xd6, 0x03, 0x93, 0xe0, 0xf0, 0x01, 0xf7, 0x88,
10526+ 0xb0, 0x4b, 0xc6, 0xb2 };
10527+ #endif
10528+ #ifdef WOLFSSL_AES_192
10529+ static const byte k192_1[] = {
10530+ 0x95, 0xf7, 0xba, 0x0a, 0x72, 0x6e, 0xed, 0x9a, 0x90, 0xa9, 0x90, 0x00,
10531+ 0x94, 0xc5, 0xd9, 0x2d };
10532+ static const byte k192_8[] = {
10533+ 0xaa, 0x6f, 0x7d, 0x3b, 0xab, 0x34, 0x91, 0xcc, 0xd9, 0x52, 0xc9, 0x86,
10534+ 0x64, 0x42, 0x8c, 0x40 };
10535+ static const byte k192_9[] = {
10536+ 0x52, 0xa0, 0xf3, 0xda, 0x5a, 0x48, 0xaf, 0xe9, 0xac, 0x1e, 0x8f, 0x96,
10537+ 0x84, 0x25, 0x93, 0x8e, 0xd5, 0x35, 0xaa, 0xe9, 0xbc, 0xe1, 0x0b, 0x52 };
10538+ static const byte k192_16[] = {
10539+ 0xe8, 0xba, 0xab, 0xb4, 0xa1, 0xf3, 0x57, 0x6e, 0x72, 0xe4, 0x71, 0xca,
10540+ 0x51, 0x2b, 0x5b, 0x64, 0xfb, 0x25, 0x25, 0x97, 0xfc, 0x80, 0x75, 0xe3 };
10541+ static const byte k192_17[] = {
10542+ 0xaf, 0x1e, 0xc0, 0xdf, 0x04, 0xef, 0xde, 0xb6, 0x0d, 0xa4, 0xdf, 0xf5,
10543+ 0x89, 0x84, 0x14, 0x91, 0x11, 0xdf, 0xda, 0x2d, 0xef, 0xc1, 0x30, 0x6e,
10544+ 0x54, 0x46, 0x2e, 0xc3, 0xac, 0x57, 0xf7, 0x8a };
10545+ static const byte k192_31[] = {
10546+ 0x1d, 0x59, 0x4c, 0x1a, 0x06, 0x03, 0x33, 0x60, 0x03, 0x12, 0x1e, 0x69,
10547+ 0x81, 0xd8, 0xbe, 0xc6, 0x0a, 0xef, 0x71, 0x7f, 0x62, 0x1e, 0x95, 0xb1,
10548+ 0xfb, 0x29, 0x96, 0x61, 0x39, 0x78, 0xbb, 0x5f, 0x52, 0xee, 0xc6, 0xda,
10549+ 0xed, 0xd8, 0x48, 0x97 };
10550+ #endif
10551+ #ifdef WOLFSSL_AES_256
10552+ static const byte k256_1[] = {
10553+ 0xcc, 0xc4, 0x9f, 0xbf, 0x20, 0xf2, 0xac, 0xe7, 0xeb, 0x31, 0xa8, 0xdd,
10554+ 0xe2, 0x26, 0x50, 0x6a };
10555+ static const byte k256_8[] = {
10556+ 0xea, 0x91, 0xdd, 0x60, 0xe5, 0x9b, 0xd6, 0x8b, 0xad, 0x0d, 0x6e, 0x25,
10557+ 0x4b, 0x5e, 0x1c, 0x39 };
10558+ static const byte k256_9[] = {
10559+ 0xc9, 0x72, 0x2a, 0x95, 0x51, 0xdf, 0xa2, 0x83, 0x2a, 0xa1, 0xca, 0xe4,
10560+ 0x87, 0x82, 0x1e, 0x06, 0x99, 0x12, 0x94, 0xae, 0xbc, 0xe0, 0x98, 0x48 };
10561+ static const byte k256_16[] = {
10562+ 0xb3, 0x33, 0x58, 0x13, 0x95, 0xce, 0xdf, 0x83, 0x56, 0xdc, 0x35, 0x6c,
10563+ 0x1c, 0xc7, 0x9e, 0x9a, 0x88, 0x5c, 0xb4, 0x98, 0x8e, 0xd6, 0x29, 0xb8 };
10564+ static const byte k256_17[] = {
10565+ 0x18, 0x58, 0x36, 0x63, 0x5d, 0xea, 0xaf, 0x4d, 0xa8, 0x27, 0x0c, 0x04,
10566+ 0x89, 0x09, 0xae, 0xbf, 0xe9, 0x21, 0x11, 0x66, 0xca, 0x2f, 0xdb, 0x34,
10567+ 0xa0, 0x93, 0x69, 0xfe, 0x9b, 0xb8, 0x6b, 0x04 };
10568+ static const byte k256_31[] = {
10569+ 0x16, 0x44, 0xf1, 0x8b, 0x57, 0xc7, 0xb3, 0xf1, 0x85, 0x51, 0xbe, 0x73,
10570+ 0xff, 0xd0, 0x9c, 0xa7, 0x42, 0xf7, 0xf1, 0x56, 0x26, 0x1d, 0x58, 0x95,
10571+ 0xaa, 0xc0, 0x97, 0xc4, 0xb6, 0x4e, 0x02, 0x80, 0xfc, 0x80, 0xbd, 0xac,
10572+ 0x45, 0x2c, 0x90, 0x10 };
10573+ #endif
10574+ static const word32 bsz[] = { 1, 8, 9, 16, 17, 31 };
10575+ static const word32 bes[] = { 16, 16, 24, 24, 32, 40 };
10576+ const struct { const byte* kek; word32 kekSz; const byte* exp[6]; } ek[] = {
10577+ #ifdef WOLFSSL_AES_128
10578+ { kek128, (word32)sizeof(kek128),
10579+ { k128_1, k128_8, k128_9, k128_16, k128_17, k128_31 } },
10580+ #endif
10581+ #ifdef WOLFSSL_AES_192
10582+ { kek192, (word32)sizeof(kek192),
10583+ { k192_1, k192_8, k192_9, k192_16, k192_17, k192_31 } },
10584+ #endif
10585+ #ifdef WOLFSSL_AES_256
10586+ { kek256, (word32)sizeof(kek256),
10587+ { k256_1, k256_8, k256_9, k256_16, k256_17, k256_31 } },
10588+ #endif
10589+ };
10590+ byte ewrap[80];
10591+ byte eback[80];
10592+ byte rpt[64];
10593+ word32 ki, bi, s, t;
10594+
10595+ for (t = 0; t < (word32)sizeof(rpt); t++)
10596+ rpt[t] = (byte)(0xA0 + t);
10597+
10598+ for (ki = 0; ki < (word32)(sizeof(ek) / sizeof(ek[0])); ki++) {
10599+ /* boundary-size known-answer + round-trip */
10600+ for (bi = 0; bi < (word32)(sizeof(bsz) / sizeof(bsz[0])); bi++) {
10601+ XMEMSET(ewrap, 0, sizeof(ewrap));
10602+ XMEMSET(eback, 0, sizeof(eback));
10603+ ExpectIntEQ(wc_AesKeyWrap_Pad(ek[ki].kek, ek[ki].kekSz,
10604+ rpt, bsz[bi], ewrap, sizeof(ewrap), NULL),
10605+ (int)bes[bi]);
10606+ ExpectBufEQ(ewrap, ek[ki].exp[bi], bes[bi]);
10607+ ExpectIntEQ(wc_AesKeyUnWrap_Pad(ek[ki].kek, ek[ki].kekSz,
10608+ ewrap, bes[bi], eback, sizeof(eback), NULL),
10609+ (int)bsz[bi]);
10610+ ExpectBufEQ(eback, rpt, bsz[bi]);
10611+ }
10612+
10613+ /* exhaustive round-trip for every input size 1..64 */
10614+ for (s = 1; s <= (word32)sizeof(rpt); s++) {
10615+ int w;
10616+ word32 expW = ((s + 7u) / 8u) * 8u + 8u;
10617+
10618+ XMEMSET(ewrap, 0, sizeof(ewrap));
10619+ XMEMSET(eback, 0, sizeof(eback));
10620+ w = wc_AesKeyWrap_Pad(ek[ki].kek, ek[ki].kekSz,
10621+ rpt, s, ewrap, sizeof(ewrap), NULL);
10622+ ExpectIntEQ(w, (int)expW);
10623+ ExpectIntEQ(wc_AesKeyUnWrap_Pad(ek[ki].kek, ek[ki].kekSz,
10624+ ewrap, (word32)w, eback, sizeof(eback), NULL),
10625+ (int)s);
10626+ ExpectBufEQ(eback, rpt, s);
10627+ }
10628+
10629+ /* in-place (in == out aliasing must be supported) */
10630+ {
10631+ byte buf[80];
10632+ int w;
10633+
10634+ XMEMSET(buf, 0, sizeof(buf));
10635+ XMEMCPY(buf, data20, sizeof(data20));
10636+ w = wc_AesKeyWrap_Pad(ek[ki].kek, ek[ki].kekSz, buf,
10637+ (word32)sizeof(data20), buf,
10638+ sizeof(buf), NULL);
10639+ ExpectIntGE(w, 0);
10640+ ExpectIntEQ(wc_AesKeyUnWrap_Pad(ek[ki].kek, ek[ki].kekSz, buf,
10641+ (word32)w, buf, sizeof(buf), NULL),
10642+ (int)sizeof(data20));
10643+ ExpectBufEQ(buf, data20, sizeof(data20));
10644+ }
10645+ }
10646+ }
10647+ #endif /* WOLFSSL_AES_128 || WOLFSSL_AES_192 || WOLFSSL_AES_256 */
10648+
1049410649 return EXPECT_RESULT();
1049510650} /* END test_wc_AesKeyWrap_Pad */
1049610651
0 commit comments