[FFmpeg-devel] [PATCH] lavu/hmac: Add support for SHA-2

James Almer jamrial at gmail.com
Sun Jun 9 21:28:18 CEST 2013


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 at gmail.com>
---
 libavutil/hmac.c    | 58 +++++++++++++++++++++++++++++++++++++++++++++++------
 libavutil/hmac.h    |  4 ++++
 libavutil/version.h |  2 +-
 3 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/libavutil/hmac.c b/libavutil/hmac.c
index f1cf7a4..4f7f3f4 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;
diff --git a/libavutil/hmac.h b/libavutil/hmac.h
index aef84c6..ca4da6a 100644
--- a/libavutil/hmac.h
+++ b/libavutil/hmac.h
@@ -32,6 +32,10 @@
 enum AVHMACType {
     AV_HMAC_MD5,
     AV_HMAC_SHA1,
+    AV_HMAC_SHA224,
+    AV_HMAC_SHA256,
+    AV_HMAC_SHA384,
+    AV_HMAC_SHA512,
 };
 
 typedef struct AVHMAC AVHMAC;
diff --git a/libavutil/version.h b/libavutil/version.h
index 9941229..ba5f14e 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -76,7 +76,7 @@
 
 #define LIBAVUTIL_VERSION_MAJOR  52
 #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, \
                                                LIBAVUTIL_VERSION_MINOR, \
-- 
1.8.1.5



More information about the ffmpeg-devel mailing list