Commit 82ef6701 authored by James Almer's avatar James Almer Committed by Michael Niedermayer

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/rfc4231Signed-off-by: 's avatarJames Almer <jamrial@gmail.com>
Signed-off-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parent b1d61eb7
...@@ -24,10 +24,11 @@ ...@@ -24,10 +24,11 @@
#include "hmac.h" #include "hmac.h"
#include "md5.h" #include "md5.h"
#include "sha.h" #include "sha.h"
#include "sha512.h"
#include "mem.h" #include "mem.h"
#define MAX_HASHLEN 20 #define MAX_HASHLEN 64
#define MAX_BLOCKLEN 64 #define MAX_BLOCKLEN 128
struct AVHMAC { struct AVHMAC {
void *hash; void *hash;
...@@ -39,11 +40,24 @@ struct AVHMAC { ...@@ -39,11 +40,24 @@ struct AVHMAC {
int keylen; int keylen;
}; };
static av_cold void sha1_init(void *ctx) #define DEFINE_SHA(bits) \
{ static av_cold void sha ## bits ##_init(void *ctx) \
av_sha_init(ctx, 160); { \
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 *av_hmac_alloc(enum AVHMACType type)
{ {
AVHMAC *c = av_mallocz(sizeof(*c)); AVHMAC *c = av_mallocz(sizeof(*c));
...@@ -61,11 +75,43 @@ AVHMAC *av_hmac_alloc(enum AVHMACType type) ...@@ -61,11 +75,43 @@ AVHMAC *av_hmac_alloc(enum AVHMACType type)
case AV_HMAC_SHA1: case AV_HMAC_SHA1:
c->blocklen = 64; c->blocklen = 64;
c->hashlen = 20; 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->update = (void*)av_sha_update;
c->final = (void*)av_sha_final; c->final = (void*)av_sha_final;
c->hash = av_sha_alloc(); c->hash = av_sha_alloc();
break; 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: default:
av_free(c); av_free(c);
return NULL; return NULL;
......
...@@ -32,6 +32,10 @@ ...@@ -32,6 +32,10 @@
enum AVHMACType { enum AVHMACType {
AV_HMAC_MD5, AV_HMAC_MD5,
AV_HMAC_SHA1, AV_HMAC_SHA1,
AV_HMAC_SHA224 = 10,
AV_HMAC_SHA256,
AV_HMAC_SHA384,
AV_HMAC_SHA512,
}; };
typedef struct AVHMAC AVHMAC; typedef struct AVHMAC AVHMAC;
......
...@@ -76,7 +76,7 @@ ...@@ -76,7 +76,7 @@
#define LIBAVUTIL_VERSION_MAJOR 52 #define LIBAVUTIL_VERSION_MAJOR 52
#define LIBAVUTIL_VERSION_MINOR 35 #define LIBAVUTIL_VERSION_MINOR 35
#define LIBAVUTIL_VERSION_MICRO 100 #define LIBAVUTIL_VERSION_MICRO 101
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \ #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
LIBAVUTIL_VERSION_MINOR, \ LIBAVUTIL_VERSION_MINOR, \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment