[FFmpeg-devel] [RFC] Public API for RC4 and DES

Reimar Döffinger Reimar.Doeffinger
Wed Jan 28 15:17:27 CET 2009


Hello,
attached patch makes them use an API similar to AES.
Differences:
- src is const
- alignment requirements of src and dst documented
- src == NULL is allowed for easier use as PRNG
- RC4 does not implement CBC (what would be the IV length for RC4?
  Would a one-byte IV even be useful?)
- block size, relative to which count is given, is 16 bytes for AES,
  8 bytes for DES and 1 byte for RC4

The disadvantage for the only user asfcrypt is that it adds two
malloc/frees per ASF block, at least with the current design.

Greetings,
Reimar D?ffinger
-------------- next part --------------
Index: libavformat/asfcrypt.c
===================================================================
--- libavformat/asfcrypt.c	(revision 16844)
+++ libavformat/asfcrypt.c	(working copy)
@@ -136,6 +136,8 @@
 }
 
 void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len) {
+    struct AVDES *des;
+    struct AVRC4 *rc4;
     int num_qwords = len >> 3;
     uint64_t *qwords = (uint64_t *)data;
     uint64_t rc4buff[8];
@@ -150,17 +152,22 @@
     }
 
     memset(rc4buff, 0, sizeof(rc4buff));
-    ff_rc4_enc(key, 12, (uint8_t *)rc4buff, sizeof(rc4buff));
+    rc4 = av_malloc(av_rc4_size);
+    av_rc4_init(rc4, key, 12 * 8, 1);
+    av_rc4_crypt(rc4, (uint8_t *)rc4buff, NULL, sizeof(rc4buff), NULL, 1);
     multiswap_init((uint8_t *)rc4buff, ms_keys);
 
     packetkey = qwords[num_qwords - 1];
     packetkey ^= rc4buff[7];
-    packetkey = be2me_64(packetkey);
-    packetkey = ff_des_encdec(packetkey, AV_RB64(key + 12), 1);
-    packetkey = be2me_64(packetkey);
+    des = av_malloc(av_des_size);
+    av_des_init(des, key + 12, 64, 1);
+    av_des_crypt(des, (uint8_t *)&packetkey, (uint8_t *)&packetkey, 1, NULL, 1);
+    av_free(des);
     packetkey ^= rc4buff[6];
 
-    ff_rc4_enc((uint8_t *)&packetkey, 8, data, len);
+    av_rc4_init(rc4, (uint8_t *)&packetkey, 64, 1);
+    av_rc4_crypt(rc4, data, data, len, NULL, 1);
+    av_free(rc4);
 
     ms_state = 0;
     for (i = 0; i < num_qwords - 1; i++, qwords++)
Index: libavutil/des.c
===================================================================
--- libavutil/des.c	(revision 16844)
+++ libavutil/des.c	(working copy)
@@ -20,8 +20,15 @@
  */
 #include <inttypes.h>
 #include "common.h"
+#include "intreadwrite.h"
 #include "des.h"
 
+typedef struct AVDES {
+    uint64_t key;
+} AVDES;
+
+const int av_des_size = sizeof(AVDES);
+
 #define T(a, b, c, d, e, f, g, h) 64-a,64-b,64-c,64-d,64-e,64-f,64-g,64-h
 static const uint8_t IP_shuffle[] = {
     T(58, 50, 42, 34, 26, 18, 10, 2),
@@ -249,7 +256,7 @@
     return CDn;
 }
 
-uint64_t ff_des_encdec(uint64_t in, uint64_t key, int decrypt) {
+static uint64_t ff_des_encdec(uint64_t in, uint64_t key, int decrypt) {
     int i;
     uint64_t K[16];
     // discard parity bits from key and shuffle it into C and D parts
@@ -277,6 +284,33 @@
     return in;
 }
 
+int av_des_init(AVDES *d, const uint8_t *key, int key_bits, int decrypt) {
+    if (key_bits != 64)
+        return -1;
+    d->key = be2me_64(*(const uint64_t *)key);
+    return 0;
+}
+
+void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
+    uint64_t iv_val = iv ? be2me_64(*(uint64_t *)iv) : 0;
+    while (count-- > 0) {
+        uint64_t dst_val;
+        uint64_t src_val = src ? be2me_64(*(const uint64_t *)src) : 0;
+        if (decrypt) {
+            dst_val = ff_des_encdec(src_val, d->key, 1) ^ iv_val;
+            iv_val = iv ? src_val : 0;
+        } else {
+            dst_val = ff_des_encdec(src_val ^ iv_val, d->key, 0);
+            iv_val = iv ? dst_val : 0;
+        }
+        *(uint64_t *)dst = be2me_64(dst_val);
+        src += 8;
+        dst += 8;
+    }
+    if (iv)
+        *(uint64_t *)iv = be2me_64(iv_val);
+}
+
 #ifdef TEST
 #undef printf
 #undef rand
Index: libavutil/des.h
===================================================================
--- libavutil/des.h	(revision 16844)
+++ libavutil/des.h	(working copy)
@@ -23,18 +23,29 @@
 #define AVUTIL_DES_H
 
 #include <stdint.h>
-#include "common.h"
 
+extern const int av_des_size;
+
+struct AVDES;
+
 /**
- * \brief en- or decrypt an 64-bit block of data with DES
- * \param in data to process.
- * \param key key to use for en-/decryption.
- * \param decrypt if 0 encrypt, else decrypt.
- * \return processed data
+ * \brief Initializes an AVDES context.
  *
- * If your input data is in 8-bit blocks treat it as big-endian
- * (use e.g. AV_RB64 and AV_WB64).
+ * \param key_bits must be 64
+ * \param decrypt 0 for encryption, 1 for decryption
  */
-uint64_t ff_des_encdec(uint64_t in, uint64_t key, int decrypt) av_const;
+int av_des_init(struct AVDES *d, const uint8_t *key, int key_bits, int decrypt);
 
+/**
+ * \brief Encrypts / decrypts using the DES algorithm.
+ *
+ * \param count number of 8 byte blocks
+ * \param dst destination array, can be equal to src, must be 8-byte aligned
+ * \param src source array, can be equal to dst, must be 8-byte aligned, may be NULL
+ * \param iv initialization vector for CBC mode, if NULL then ECB will be used,
+ *           must be 8-byte aligned
+ * \param decrypt 0 for encryption, 1 for decryption
+ */
+void av_des_crypt(struct AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt);
+
 #endif /* AVUTIL_DES_H */
Index: libavutil/rc4.c
===================================================================
--- libavutil/rc4.c	(revision 16844)
+++ libavutil/rc4.c	(working copy)
@@ -23,26 +23,39 @@
 #include "common.h"
 #include "rc4.h"
 
-void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen) {
+typedef struct AVRC4 {
+    uint8_t state[256];
+    int x, y;
+} AVRC4;
+
+const int av_rc4_size = sizeof(AVRC4);
+
+int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt) {
     int i, j;
-    uint8_t x, y;
-    uint8_t state[256];
+    int keylen = key_bits >> 3;
+    uint8_t y;
     for (i = 0; i < 256; i++)
-        state[i] = i;
+        r->state[i] = i;
     y = 0;
     // j is i % keylen
     for (j = 0, i = 0; i < 256; i++, j++) {
         if (j == keylen) j = 0;
-        y += state[i] + key[j];
-        FFSWAP(uint8_t, state[i], state[y]);
+        y += r->state[i] + key[j];
+        FFSWAP(uint8_t, r->state[i], r->state[y]);
     }
-    // state initialized, now do the real encryption
-    x = 1; y = state[1];
-    while (datalen-- > 0) {
-        uint8_t sum = state[x] + state[y];
-        FFSWAP(uint8_t, state[x], state[y]);
-        *data++ ^= state[sum];
+    r->x = 1;
+    r->y = r->state[1];
+    return 0;
+}
+
+void av_rc4_crypt(AVRC4 *r, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
+    uint8_t x = r->x, y = r->y;
+    while (count-- > 0) {
+        uint8_t sum = r->state[x] + r->state[y];
+        FFSWAP(uint8_t, r->state[x], r->state[y]);
+        *dst++ = src ? *src++ ^ r->state[sum] : r->state[sum];
         x++;
-        y += state[x];
+        y += r->state[x];
     }
+    r->x = x; r->y = y;
 }
Index: libavutil/rc4.h
===================================================================
--- libavutil/rc4.h	(revision 16844)
+++ libavutil/rc4.h	(working copy)
@@ -23,6 +23,27 @@
 
 #include <stdint.h>
 
-void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen);
+extern const int av_rc4_size;
 
+struct AVRC4;
+
+/**
+ * \brief Initializes an AVRC4 context.
+ *
+ * \param key_bits will be rounded down to the next multiple of 8 
+ * \param decrypt 0 for encryption, 1 for decryption, currently has no effect
+ */
+int av_rc4_init(struct AVRC4 *d, const uint8_t *key, int key_bits, int decrypt);
+
+/**
+ * \brief Encrypts / decrypts using the RC4 algorithm.
+ *
+ * \param count number of bytes
+ * \param dst destination array, can be equal to src
+ * \param src source array, can be equal to dst, may be NULL
+ * \param iv not (yet) used for RC4 should be NULL
+ * \param decrypt 0 for encryption, 1 for decryption, not (yet) used
+ */
+void av_rc4_crypt(struct AVRC4 *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt);
+
 #endif /* AVUTIL_RC4_H */



More information about the ffmpeg-devel mailing list