Index: openssl-1.1.1d/crypto/fips/fips_hmac_selftest.c =================================================================== --- openssl-1.1.1d.orig/crypto/fips/fips_hmac_selftest.c +++ openssl-1.1.1d/crypto/fips/fips_hmac_selftest.c @@ -55,12 +55,23 @@ #include #ifdef OPENSSL_FIPS + +#define MAX_SHA3_KEY_LENGTH 200 + typedef struct { const EVP_MD *(*alg) (void); const char *key, *iv; unsigned char kaval[EVP_MAX_MD_SIZE]; } HMAC_KAT; +typedef struct { + const EVP_MD *(*alg) (void); + unsigned char key[MAX_SHA3_KEY_LENGTH]; + size_t keylen; /* strlen() doesn't work with NIST test vector keys */ + const char *iv; + unsigned char kaval[EVP_MAX_MD_SIZE]; +} HMAC_KAT_SHA3; + static const HMAC_KAT vector[] = { {EVP_sha1, /* from http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf */ @@ -111,6 +122,289 @@ static const HMAC_KAT vector[] = { }, }; +static const HMAC_KAT_SHA3 vector_SHA3[] = { + /* using SHA3-224 from https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/HMAC_SHA3-224.pdf */ + {EVP_sha3_224, + {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b}, + 0x1c, + "Sample message for keylenblocklen", + {0x07, 0x86, 0x95, 0xee, 0xcc, 0x22, 0x7c, 0x63, + 0x6a, 0xd3, 0x1d, 0x06, 0x3a, 0x15, 0xdd, 0x05, + 0xa7, 0xe8, 0x19, 0xa6, 0x6e, 0xc6, 0xd8, 0xde, + 0x1e, 0x19, 0x3e, 0x59}, + }, + /* using SHA3-256 from https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/HMAC_SHA3-256.pdf */ + {EVP_sha3_256, + {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f}, + 0x30, + "Sample message for keylenblocklen", + {0x9b, 0xcf, 0x2c, 0x23, 0x8e, 0x23, 0x5c, 0x3c, + 0xe8, 0x84, 0x04, 0xe8, 0x13, 0xbd, 0x2f, 0x3a, + 0x97, 0x18, 0x5a, 0xc6, 0xf2, 0x38, 0xc6, 0x3d, + 0x62, 0x29, 0xa0, 0x0b, 0x07, 0x97, 0x42, 0x58}, + }, + /* using SHA3-384 from https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/HMAC_SHA3-384.pdf */ + {EVP_sha3_384, + {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, + 0x30, + "Sample message for keylenblocklen", + {0xe5, 0xae, 0x4c, 0x73, 0x9f, 0x45, 0x52, 0x79, + 0x36, 0x8e, 0xbf, 0x36, 0xd4, 0xf5, 0x35, 0x4c, + 0x95, 0xaa, 0x18, 0x4c, 0x89, 0x9d, 0x38, 0x70, + 0xe4, 0x60, 0xeb, 0xc2, 0x88, 0xef, 0x1f, 0x94, + 0x70, 0x05, 0x3f, 0x73, 0xf7, 0xc6, 0xda, 0x2a, + 0x71, 0xbc, 0xae, 0xc3, 0x8c, 0xe7, 0xd6, 0xac}, + }, + + + + /* using SHA3-512 from https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/HMAC_SHA3-512.pdf */ + {EVP_sha3_512, + {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f}, + 0x40, + "Sample message for keylenblocklen", + {0x5f, 0x46, 0x4f, 0x5e, 0x5b, 0x78, 0x48, 0xe3, + 0x88, 0x5e, 0x49, 0xb2, 0xc3, 0x85, 0xf0, 0x69, + 0x49, 0x85, 0xd0, 0xe3, 0x89, 0x66, 0x24, 0x2d, + 0xc4, 0xa5, 0xfe, 0x3f, 0xea, 0x4b, 0x37, 0xd4, + 0x6b, 0x65, 0xce, 0xce, 0xd5, 0xdc, 0xf5, 0x94, + 0x38, 0xdd, 0x84, 0x0b, 0xab, 0x22, 0x26, 0x9f, + 0x0b, 0xa7, 0xfe, 0xbd, 0xb9, 0xfc, 0xf7, 0x46, + 0x02, 0xa3, 0x56, 0x66, 0xb2, 0xa3, 0x29, 0x15}, + }, +}; + + int FIPS_selftest_hmac() { int n; @@ -118,7 +412,9 @@ int FIPS_selftest_hmac() unsigned char out[EVP_MAX_MD_SIZE]; const EVP_MD *md; const HMAC_KAT *t; + const HMAC_KAT_SHA3 *t3; + /* SHA1 and SHA2 */ for (n = 0, t = vector; n < sizeof(vector) / sizeof(vector[0]); n++, t++) { md = (*t->alg) (); HMAC(md, t->key, strlen(t->key), @@ -128,6 +424,18 @@ int FIPS_selftest_hmac() FIPSerr(FIPS_F_FIPS_SELFTEST_HMAC, FIPS_R_SELFTEST_FAILED); return 0; } + } + + /* SHA3 */ + for (n = 0, t3 = vector_SHA3; n < sizeof(vector_SHA3) / sizeof(vector_SHA3[0]); n++, t3++) { + md = (*t3->alg) (); + HMAC(md, t3->key, t3->keylen, + (const unsigned char *)t3->iv, strlen(t3->iv), out, &outlen); + + if (memcmp(out, t3->kaval, outlen)) { + FIPSerr(FIPS_F_FIPS_SELFTEST_HMAC, FIPS_R_SELFTEST_FAILED); + return 0; + } } return 1; }