[FFmpeg-devel] [PATCH] avutil/hmac: fix API/ABI compatibility with the fork

James Almer jamrial at gmail.com
Thu Jul 30 02:03:06 CEST 2015


The test code is updated with some cosmetics to avoid the loop
using undefined AVHMACType values.
The old enum values will be removed in the next major bump, effectively
making both projects synced.

Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavutil/hmac.c | 59 +++++++++++++++++++++++++++++++++-----------------------
 libavutil/hmac.h | 12 ++++++------
 2 files changed, 41 insertions(+), 30 deletions(-)

diff --git a/libavutil/hmac.c b/libavutil/hmac.c
index b63d1b2..0cf4cd6 100644
--- a/libavutil/hmac.c
+++ b/libavutil/hmac.c
@@ -81,6 +81,9 @@ AVHMAC *av_hmac_alloc(enum AVHMACType type)
         c->hash     = av_sha_alloc();
         break;
     case AV_HMAC_SHA224:
+#if FF_API_HMAC
+    case AV_HMAC_SHA224_DEPRECATED:
+#endif
         c->blocklen = 64;
         c->hashlen  = 28;
         c->init     = sha224_init;
@@ -89,6 +92,9 @@ AVHMAC *av_hmac_alloc(enum AVHMACType type)
         c->hash     = av_sha_alloc();
         break;
     case AV_HMAC_SHA256:
+#if FF_API_HMAC
+    case AV_HMAC_SHA256_DEPRECATED:
+#endif
         c->blocklen = 64;
         c->hashlen  = 32;
         c->init     = sha256_init;
@@ -207,7 +213,8 @@ static void test(AVHMAC *hmac, const uint8_t *key, int keylen,
 int main(void)
 {
     uint8_t key1[20], key3[131], data3[50];
-    enum AVHMACType i = AV_HMAC_SHA224;
+    AVHMAC *hmac;
+    enum AVHMACType i;
     static const uint8_t key2[]  = "Jefe";
     static const uint8_t data1[] = "Hi There";
     static const uint8_t data2[] = "what do ya want for nothing?";
@@ -216,34 +223,39 @@ int main(void)
     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(data3, 0xdd, sizeof(data3));
-    // 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, 20, data3, sizeof(data3));
-    test(hmac, key3, 80, data4, sizeof(data4));
-    test(hmac, key3, 80, data5, sizeof(data5));
-    av_hmac_free(hmac);
+    /* MD5, SHA-1 */
+    for (i = AV_HMAC_MD5; i <= AV_HMAC_SHA1; i++) {
+        hmac = av_hmac_alloc(i);
+        if (!hmac)
+            return 1;
+        // RFC 2202 test vectors
+        test(hmac, key1, hmac->hashlen, data1, sizeof(data1));
+        test(hmac, key2, sizeof(key2),  data2, sizeof(data2));
+        test(hmac, key3, hmac->hashlen, 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) {
+    for (i = AV_HMAC_SHA224; i <= AV_HMAC_SHA256; i++) {
+        hmac = av_hmac_alloc(i);
+        if (!hmac)
+            return 1;
+        // 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);
+    }
+
+    for (i = AV_HMAC_SHA384; i <= AV_HMAC_SHA512; i++) {
         hmac = av_hmac_alloc(i);
         if (!hmac)
             return 1;
@@ -254,7 +266,6 @@ int main(void)
         test(hmac, key3, sizeof(key3), data4, sizeof(data4));
         test(hmac, key3, sizeof(key3), data6, sizeof(data6));
         av_hmac_free(hmac);
-        i++;
     }
     return 0;
 }
diff --git a/libavutil/hmac.h b/libavutil/hmac.h
index 4045c08..892c02e 100644
--- a/libavutil/hmac.h
+++ b/libavutil/hmac.h
@@ -23,7 +23,7 @@
 
 #include <stdint.h>
 
-#include "libavutil/avconfig.h"
+#include "version.h"
 /**
  * @defgroup lavu_hmac HMAC
  * @ingroup lavu_crypto
@@ -33,13 +33,13 @@
 enum AVHMACType {
     AV_HMAC_MD5,
     AV_HMAC_SHA1,
-#if AV_HAVE_INCOMPATIBLE_LIBAV_ABI
     AV_HMAC_SHA224,
-#else
-    AV_HMAC_SHA224 = 10,
-#endif
     AV_HMAC_SHA256,
-    AV_HMAC_SHA384,
+#if FF_API_HMAC
+    AV_HMAC_SHA224_DEPRECATED = 10,
+    AV_HMAC_SHA256_DEPRECATED,
+#endif
+    AV_HMAC_SHA384 = 12,
     AV_HMAC_SHA512,
 };
 
-- 
2.5.0



More information about the ffmpeg-devel mailing list