summaryrefslogtreecommitdiffstats
path: root/libavutil/hmac.c
diff options
context:
space:
mode:
Diffstat (limited to 'libavutil/hmac.c')
-rw-r--r--libavutil/hmac.c128
1 files changed, 100 insertions, 28 deletions
diff --git a/libavutil/hmac.c b/libavutil/hmac.c
index f87728e..02bdc18 100644
--- a/libavutil/hmac.c
+++ b/libavutil/hmac.c
@@ -1,20 +1,20 @@
/*
* Copyright (C) 2012 Martin Storsjo
*
- * This file is part of Libav.
+ * This file is part of FFmpeg.
*
- * Libav is free software; you can redistribute it and/or
+ * FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
- * Libav is distributed in the hope that it will be useful,
+ * FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with Libav; if not, write to the Free Software
+ * License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@@ -24,10 +24,11 @@
#include "hmac.h"
#include "md5.h"
#include "sha.h"
+#include "sha512.h"
#include "mem.h"
-#define MAX_HASHLEN 20
-#define MAX_BLOCKLEN 64
+#define MAX_HASHLEN 64
+#define MAX_BLOCKLEN 128
struct AVHMAC {
void *hash;
@@ -39,11 +40,24 @@ struct AVHMAC {
int keylen;
};
-static av_cold void sha1_init(void *ctx)
-{
- av_sha_init(ctx, 160);
+#define DEFINE_SHA(bits) \
+static av_cold void sha ## bits ##_init(void *ctx) \
+{ \
+ av_sha_init(ctx, bits); \
+}
+
+#define DEFINE_SHA512(bits) \
+static av_cold void sha ## bits ##_init(void *ctx) \
+{ \
+ av_sha512_init(ctx, bits); \
}
+DEFINE_SHA(160)
+DEFINE_SHA(224)
+DEFINE_SHA(256)
+DEFINE_SHA512(384)
+DEFINE_SHA512(512)
+
AVHMAC *av_hmac_alloc(enum AVHMACType type)
{
AVHMAC *c = av_mallocz(sizeof(*c));
@@ -53,19 +67,51 @@ AVHMAC *av_hmac_alloc(enum AVHMACType type)
case AV_HMAC_MD5:
c->blocklen = 64;
c->hashlen = 16;
- c->init = av_md5_init;
- c->update = av_md5_update;
- c->final = av_md5_final;
+ c->init = (void*)av_md5_init;
+ c->update = (void*)av_md5_update;
+ c->final = (void*)av_md5_final;
c->hash = av_md5_alloc();
break;
case AV_HMAC_SHA1:
c->blocklen = 64;
c->hashlen = 20;
- c->init = sha1_init;
- c->update = av_sha_update;
- c->final = av_sha_final;
+ c->init = sha160_init;
+ c->update = (void*)av_sha_update;
+ c->final = (void*)av_sha_final;
+ c->hash = av_sha_alloc();
+ break;
+ case AV_HMAC_SHA224:
+ c->blocklen = 64;
+ c->hashlen = 28;
+ c->init = sha224_init;
+ c->update = (void*)av_sha_update;
+ c->final = (void*)av_sha_final;
+ c->hash = av_sha_alloc();
+ break;
+ case AV_HMAC_SHA256:
+ c->blocklen = 64;
+ c->hashlen = 32;
+ c->init = sha256_init;
+ c->update = (void*)av_sha_update;
+ c->final = (void*)av_sha_final;
c->hash = av_sha_alloc();
break;
+ case AV_HMAC_SHA384:
+ c->blocklen = 128;
+ c->hashlen = 48;
+ c->init = sha384_init;
+ c->update = (void*)av_sha512_update;
+ c->final = (void*)av_sha512_final;
+ c->hash = av_sha512_alloc();
+ break;
+ case AV_HMAC_SHA512:
+ c->blocklen = 128;
+ c->hashlen = 64;
+ c->init = sha512_init;
+ c->update = (void*)av_sha512_update;
+ c->final = (void*)av_sha512_final;
+ c->hash = av_sha512_alloc();
+ break;
default:
av_free(c);
return NULL;
@@ -160,28 +206,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];
- const uint8_t key2[] = "Jefe";
- const uint8_t data1[] = "Hi There";
- const uint8_t data2[] = "what do ya want for nothing?";
+ uint8_t key1[20], key3[131], data3[50];
+ enum AVHMACType i = AV_HMAC_SHA224;
+ static const uint8_t key2[] = "Jefe";
+ static const uint8_t data1[] = "Hi There";
+ static const uint8_t data2[] = "what do ya want for nothing?";
+ static const uint8_t data4[] = "Test Using Larger Than Block-Size Key - Hash Key First";
+ static const uint8_t data5[] = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data";
+ static 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_SHA512) {
+ 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 */
OpenPOWER on IntegriCloud