changeset 647:67fb0b442dd2 libavutil

Add and use a public API for RC4 and DES, analogous to the AES API.
author reimar
date Tue, 03 Feb 2009 14:20:55 +0000
parents 8e5654be3500
children ed614f42a5d6
files des.c des.h rc4.c rc4.h
diffstat 4 files changed, 117 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/des.c	Mon Feb 02 23:13:18 2009 +0000
+++ b/des.c	Tue Feb 03 14:20:55 2009 +0000
@@ -19,9 +19,13 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include <inttypes.h>
+#include "avutil.h"
 #include "common.h"
+#include "intreadwrite.h"
 #include "des.h"
 
+typedef struct AVDES 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,9 +253,8 @@
     return CDn;
 }
 
-uint64_t ff_des_encdec(uint64_t in, uint64_t key, int decrypt) {
+static void gen_roundkeys(uint64_t K[16], uint64_t key) {
     int i;
-    uint64_t K[16];
     // discard parity bits from key and shuffle it into C and D parts
     uint64_t CDn = shuffle(key, PC1_shuffle, sizeof(PC1_shuffle));
     // generate round keys
@@ -261,6 +264,10 @@
             CDn = key_shift_left(CDn);
         K[i] = shuffle(CDn, PC2_shuffle, sizeof(PC2_shuffle));
     }
+}
+
+static uint64_t des_encdec(uint64_t in, uint64_t K[16], int decrypt) {
+    int i;
     // used to apply round keys in reverse order for decryption
     decrypt = decrypt ? 15 : 0;
     // shuffle irrelevant to security but to ease hardware implementations
@@ -277,6 +284,42 @@
     return in;
 }
 
+#if LIBAVUTIL_VERSION_MAJOR < 50
+uint64_t ff_des_encdec(uint64_t in, uint64_t key, int decrypt) {
+    uint64_t K[16];
+    gen_roundkeys(K, key);
+    return des_encdec(in, K, decrypt);
+}
+#endif
+
+int av_des_init(AVDES *d, const uint8_t *key, int key_bits, int decrypt) {
+    if (key_bits != 64)
+        return -1;
+    d->triple_des = 0;
+    gen_roundkeys(d->round_keys[0], AV_RB64(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 = des_encdec(src_val, d->round_keys[0], 1) ^ iv_val;
+            iv_val = iv ? src_val : 0;
+        } else {
+            dst_val = des_encdec(src_val ^ iv_val, d->round_keys[0], 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
--- a/des.h	Mon Feb 02 23:13:18 2009 +0000
+++ b/des.h	Tue Feb 03 14:20:55 2009 +0000
@@ -23,18 +23,30 @@
 #define AVUTIL_DES_H
 
 #include <stdint.h>
-#include "common.h"
+
+struct AVDES {
+    uint64_t round_keys[3][16];
+    int triple_des;
+};
 
 /**
- * \brief Encrypt/decrypt a 64-bit block of data with DES.
- * \param in data to process
- * \param key key to use for encryption/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 */
--- a/rc4.c	Mon Feb 02 23:13:18 2009 +0000
+++ b/rc4.c	Tue Feb 03 14:20:55 2009 +0000
@@ -20,13 +20,19 @@
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
+#include "avutil.h"
 #include "common.h"
 #include "rc4.h"
 
-void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen) {
+typedef struct AVRC4 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];
+    uint8_t y;
+    uint8_t *state = r->state;
+    int keylen = key_bits >> 3;
+    if (key_bits & 7)
+        return -1;
     for (i = 0; i < 256; i++)
         state[i] = i;
     y = 0;
@@ -36,13 +42,28 @@
         y += state[i] + key[j];
         FFSWAP(uint8_t, state[i], state[y]);
     }
-    // state initialized, now do the real encryption
-    x = 1; y = state[1];
-    while (datalen-- > 0) {
+    r->x = 1;
+    r->y = 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;
+    uint8_t *state = r->state;
+    while (count-- > 0) {
         uint8_t sum = state[x] + state[y];
         FFSWAP(uint8_t, state[x], state[y]);
-        *data++ ^= state[sum];
+        *dst++ = src ? *src++ ^ state[sum] : state[sum];
         x++;
         y += state[x];
     }
+    r->x = x; r->y = y;
 }
+
+#if LIBAVUTIL_VERSION_MAJOR < 50
+void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen) {
+    AVRC4 r;
+    av_rc4_init(&r, key, keylen * 8, 0);
+    av_rc4_crypt(&r, data, data, datalen, NULL, 0);
+}
+#endif
--- a/rc4.h	Mon Feb 02 23:13:18 2009 +0000
+++ b/rc4.h	Tue Feb 03 14:20:55 2009 +0000
@@ -23,6 +23,28 @@
 
 #include <stdint.h>
 
-void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen);
+struct AVRC4 {
+    uint8_t state[256];
+    int x, y;
+};
+
+/**
+ * \brief Initializes an AVRC4 context.
+ *
+ * \param key_bits must be a 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 */