diff --git a/libavutil/hmac.c b/libavutil/hmac.c index 318174ec31..abbee368b0 100644 --- a/libavutil/hmac.c +++ b/libavutil/hmac.c @@ -181,28 +181,54 @@ static void test(AVHMAC *hmac, const uint8_t *key, int keylen, int main(void) { - uint8_t key1[16], key3[16], data3[50], key4[63], key5[64], key6[65]; + uint8_t key1[20], key3[131], data3[50]; + enum AVHMACType i = AV_HMAC_SHA224; const uint8_t key2[] = "Jefe"; const uint8_t data1[] = "Hi There"; const uint8_t data2[] = "what do ya want for nothing?"; + const uint8_t data4[] = "Test Using Larger Than Block-Size Key - Hash Key First"; + const uint8_t data5[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"; + const uint8_t data6[] = "This is a test using a larger than block-size key and a larger " + "than block-size data. The key needs to be hashed before being used" + " by the HMAC algorithm."; AVHMAC *hmac = av_hmac_alloc(AV_HMAC_MD5); if (!hmac) return 1; memset(key1, 0x0b, sizeof(key1)); memset(key3, 0xaa, sizeof(key3)); - memset(key4, 0x44, sizeof(key4)); - memset(key5, 0x55, sizeof(key5)); - memset(key6, 0x66, sizeof(key6)); memset(data3, 0xdd, sizeof(data3)); - // RFC 2104 test vectors + // RFC 2202 test vectors + test(hmac, key1, 16, data1, sizeof(data1)); + test(hmac, key2, sizeof(key2), data2, sizeof(data2)); + test(hmac, key3, 16, data3, sizeof(data3)); + test(hmac, key3, 80, data4, sizeof(data4)); + test(hmac, key3, 80, data5, sizeof(data5)); + av_hmac_free(hmac); + + /* SHA-1 */ + hmac = av_hmac_alloc(AV_HMAC_SHA1); + if (!hmac) + return 1; + // RFC 2202 test vectors test(hmac, key1, sizeof(key1), data1, sizeof(data1)); test(hmac, key2, sizeof(key2), data2, sizeof(data2)); - test(hmac, key3, sizeof(key3), data3, sizeof(data3)); - // Additional tests, to test cases where the key is too long - test(hmac, key4, sizeof(key4), data1, sizeof(data1)); - test(hmac, key5, sizeof(key5), data2, sizeof(data2)); - test(hmac, key6, sizeof(key6), data3, sizeof(data3)); + test(hmac, key3, 20, data3, sizeof(data3)); + test(hmac, key3, 80, data4, sizeof(data4)); + test(hmac, key3, 80, data5, sizeof(data5)); av_hmac_free(hmac); + + /* SHA-2 */ + while (i <= AV_HMAC_SHA256) { + hmac = av_hmac_alloc(i); + // RFC 4231 test vectors + test(hmac, key1, sizeof(key1), data1, sizeof(data1)); + test(hmac, key2, sizeof(key2), data2, sizeof(data2)); + test(hmac, key3, 20, data3, sizeof(data3)); + test(hmac, key3, sizeof(key3), data4, sizeof(data4)); + test(hmac, key3, sizeof(key3), data6, sizeof(data6)); + av_hmac_free(hmac); + i++; + } return 0; } #endif /* TEST */ diff --git a/tests/ref/fate/hmac b/tests/ref/fate/hmac index 7d2a437c69..7964305eb4 100644 --- a/tests/ref/fate/hmac +++ b/tests/ref/fate/hmac @@ -1,6 +1,20 @@ 9294727a3638bb1c13f48ef8158bfc9d 750c783e6ab0b503eaa86e310a5db738 56be34521d144c88dbb8c733f0e8b3f6 -467cb2560355d7fa3ab2d6b939e6e47c -5a6ffd741d3e23b12f78b1baee9e609a -8b4b9d11c9e186c58f2a53b08ddfa436 +6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd +6f630fad67cda0ee1fb1f562db3aa53e +b617318655057264e28bc0b6fb378c8ef146be00 +effcdf6ae5eb2fa2d27416d5f184df9c259a7c79 +125d7342b9ac11cd91a39af48aa17b4f63f175d3 +aa4ae5e15272d00e95705637ce8a3b55ed402112 +e8e99d0f45237d786d6bbaa7965c7808bbff1a91 +896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22 +a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44 +7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea +95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e +3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1 +b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7 +5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843 +773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe +60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54 +9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2