diff options
author | James Almer <jamrial@gmail.com> | 2013-06-10 17:24:47 -0300 |
---|---|---|
committer | Michael Niedermayer <michaelni@gmx.at> | 2013-06-12 01:47:42 +0200 |
commit | 82ef67016ef731268ab7b8e91139bd5f16edc991 (patch) | |
tree | edc2a2620aa7ca5b543826d01aabf81d6c7b4401 /libavutil/hmac.c | |
parent | b1d61eb7aaaef84391130b6f5e83942cc829a8c8 (diff) | |
download | ffmpeg-82ef67016ef731268ab7b8e91139bd5f16edc991.tar.gz |
lavu/hmac: Add support for SHA-2
Includes HMAC-SHA-224, HMAC-SHA-256, HMAC-SHA-384, and HMAC-SHA-512.
Tested using test vectors from https://tools.ietf.org/html/rfc4231
Signed-off-by: James Almer <jamrial@gmail.com>
Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavutil/hmac.c')
-rw-r--r-- | libavutil/hmac.c | 58 |
1 files changed, 52 insertions, 6 deletions
diff --git a/libavutil/hmac.c b/libavutil/hmac.c index f1cf7a4326..4f7f3f4769 100644 --- a/libavutil/hmac.c +++ b/libavutil/hmac.c @@ -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)); @@ -61,11 +75,43 @@ AVHMAC *av_hmac_alloc(enum AVHMACType type) case AV_HMAC_SHA1: c->blocklen = 64; c->hashlen = 20; - c->init = sha1_init; + 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; |