This supports HMAC-MD5 and HMAC-SHA1 for now, other hashes are
simple to add.
---
 doc/APIchanges      |    3 ++
 libavutil/Makefile  |    2 +
 libavutil/hmac.c    |  135 +++++++++++++++++++++++++++++++++++++++++++++++++++
 libavutil/hmac.h    |   44 +++++++++++++++++
 libavutil/version.h |    2 +-
 5 files changed, 185 insertions(+), 1 deletion(-)
 create mode 100644 libavutil/hmac.c
 create mode 100644 libavutil/hmac.h

diff --git a/doc/APIchanges b/doc/APIchanges
index 4b60253..7d6fdaf 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -13,6 +13,9 @@ libavutil:     2012-10-22
 
 API changes, most recent first:
 
+2013-01-xx - xxxxxxx - lavu 52.5.0 - hmac.h
+  Add AVHMAC
+
 2013-01-13 - xxxxxxx - lavc 54.36.0 - vdpau.h
   Add AVVDPAUContext struct for VDPAU hardware-accelerated decoding.
 
diff --git a/libavutil/Makefile b/libavutil/Makefile
index 48a0e16..79e67b5 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -19,6 +19,7 @@ HEADERS = adler32.h                                           
          \
           eval.h                                                        \
           fifo.h                                                        \
           file.h                                                        \
+          hmac.h                                                        \
           imgutils.h                                                    \
           intfloat.h                                                    \
           intfloat_readwrite.h                                          \
@@ -66,6 +67,7 @@ OBJS = adler32.o                                              
          \
        fifo.o                                                           \
        file.o                                                           \
        float_dsp.o                                                      \
+       hmac.o                                                           \
        imgutils.o                                                       \
        intfloat_readwrite.o                                             \
        intmath.o                                                        \
diff --git a/libavutil/hmac.c b/libavutil/hmac.c
new file mode 100644
index 0000000..8ebd868
--- /dev/null
+++ b/libavutil/hmac.c
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2012 Martin Storsjo
+ *
+ * This file is part of Libav.
+ *
+ * Libav 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,
+ * 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
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <string.h>
+#include "hmac.h"
+#include "md5.h"
+#include "sha.h"
+#include "mem.h"
+
+#define MAX_HASHLEN 20
+#define MAX_BLOCKLEN 64
+
+struct AVHMAC {
+    void *hash;
+    int blocklen, hashlen;
+    void (*final)(void*, uint8_t*);
+    void (*update)(void*, const uint8_t*, int len);
+    void (*init)(void*);
+    uint8_t key[MAX_BLOCKLEN];
+    int keylen;
+};
+
+static void sha1_init(void *ctx)
+{
+    av_sha_init(ctx, 160);
+}
+
+struct AVHMAC *av_hmac_alloc(enum AVHMACType type)
+{
+    struct AVHMAC *c = av_mallocz(sizeof(*c));
+    if (!c)
+        return NULL;
+    switch (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->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->hash = av_sha_alloc();
+        break;
+    default:
+        av_free(c);
+        return NULL;
+    }
+    if (!c->hash) {
+        av_free(c);
+        return NULL;
+    }
+    return c;
+}
+
+void av_hmac_free(struct AVHMAC *c)
+{
+    av_free(c->hash);
+    av_free(c);
+}
+
+void av_hmac_init(struct AVHMAC *c, const uint8_t *key, unsigned int keylen)
+{
+    int i;
+    uint8_t block[MAX_BLOCKLEN];
+    if (keylen > c->blocklen) {
+        c->init(c->hash);
+        c->update(c->hash, key, keylen);
+        c->final(c->hash, c->key);
+        c->keylen = c->hashlen;
+    } else {
+        memcpy(c->key, key, keylen);
+        c->keylen = keylen;
+    }
+    c->init(c->hash);
+    for (i = 0; i < c->keylen; i++)
+        block[i] = c->key[i] ^ 0x36;
+    for (i = c->keylen; i < c->blocklen; i++)
+        block[i] = 0x36;
+    c->update(c->hash, block, c->blocklen);
+}
+
+void av_hmac_update(struct AVHMAC *c, const uint8_t *data, unsigned int len)
+{
+    c->update(c->hash, data, len);
+}
+
+int av_hmac_final(struct AVHMAC *c, uint8_t *out, unsigned int outlen)
+{
+    uint8_t block[MAX_BLOCKLEN];
+    int i;
+    if (outlen < c->hashlen)
+        return -1;
+    c->final(c->hash, out);
+    c->init(c->hash);
+    for (i = 0; i < c->keylen; i++)
+        block[i] = c->key[i] ^ 0x5C;
+    for (i = c->keylen; i < c->blocklen; i++)
+        block[i] = 0x5C;
+    c->update(c->hash, block, c->blocklen);
+    c->update(c->hash, out, c->hashlen);
+    c->final(c->hash, out);
+    return c->hashlen;
+}
+
+int av_hmac_calc(struct AVHMAC *c, const uint8_t *data, unsigned int len,
+                 const uint8_t *key, unsigned int keylen,
+                 uint8_t *out, unsigned int outlen)
+{
+    av_hmac_init(c, key, keylen);
+    av_hmac_update(c, data, len);
+    return av_hmac_final(c, out, outlen);
+}
diff --git a/libavutil/hmac.h b/libavutil/hmac.h
new file mode 100644
index 0000000..206742f
--- /dev/null
+++ b/libavutil/hmac.h
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2012 Martin Storsjo
+ *
+ * This file is part of Libav.
+ *
+ * Libav 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,
+ * 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
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_HMAC_H
+#define AVUTIL_HMAC_H
+
+#include <stdint.h>
+
+enum AVHMACType {
+    AV_HMAC_MD5,
+    AV_HMAC_SHA1,
+};
+
+struct AVHMAC;
+
+struct AVHMAC *av_hmac_alloc(enum AVHMACType type);
+void av_hmac_free(struct AVHMAC *ctx);
+
+void av_hmac_init(struct AVHMAC *ctx, const uint8_t *key, unsigned int keylen);
+void av_hmac_update(struct AVHMAC *ctx, const uint8_t *data, unsigned int len);
+int av_hmac_final(struct AVHMAC *ctx, uint8_t *out, unsigned int outlen);
+
+int av_hmac_calc(struct AVHMAC *ctx, const uint8_t *data, unsigned int len,
+                 const uint8_t *key, unsigned int keylen,
+                 uint8_t *out, unsigned int outlen);
+
+#endif
diff --git a/libavutil/version.h b/libavutil/version.h
index 83a1f81..68f5752 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -37,7 +37,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR 52
-#define LIBAVUTIL_VERSION_MINOR  4
+#define LIBAVUTIL_VERSION_MINOR  5
 #define LIBAVUTIL_VERSION_MICRO  0
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
-- 
1.7.9.4

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to