[FFmpeg-devel] [PATCH] support encrypted asf

Reimar Döffinger Reimar.Doeffinger
Tue Oct 9 19:53:04 CEST 2007


Hello,
except for the missing license headers attached is the "final" (pending
comments/objections) version.
Please comment.

Greetings,
Reimar D?ffinger
-------------- next part --------------
Index: libavformat/asfcrypt.c
===================================================================
--- libavformat/asfcrypt.c	(revision 0)
+++ libavformat/asfcrypt.c	(revision 0)
@@ -0,0 +1,152 @@
+// this is a rewrite of code contained in freeme/freeme2
+#include "common.h"
+#include "intreadwrite.h"
+#include "bswap.h"
+#include "des.h"
+#include "rc4.h"
+#include "asfcrypt.h"
+
+/**
+ * \brief find multiplicative inverse modulo 2 ^ 32
+ * \param v number to invert, must be odd!
+ * \return number so that result * v = 1 (mod 2^32)
+ */
+static uint32_t inverse(uint32_t v) {
+    // this initialization reduces the number of iterations
+    // to 3 compared to 4 for = 2 - v and 5 for = 1
+    uint32_t inverse = v * v * v;
+    // uses a fixpoint-iteration to find the inverse very quickly
+    int i = 3;
+    do {
+        inverse *= 2 - v * inverse;
+    } while (--i);
+    return inverse;
+}
+
+/**
+ * \brief read keys from keybuf into keys
+ * \param keybuf buffer of 48 bytes containing the keys
+ * \param keys output key array of size 12 ints containing the keys
+ *             in native endianness for encryption
+ */
+static void multiswap_init(const uint8_t *keybuf, uint32_t *keys) {
+    int i;
+    for (i = 0; i < 12; i++)
+        keys[i] = AV_RL32(keybuf + (i << 2)) | 1;
+}
+
+/**
+ * \brief invert the keys so that encryption become decryption keys and
+ *        the other way round.
+ * \param keys key array of 12 ints to invert
+ */
+static void multiswap_invert_keys(uint32_t *keys) {
+    int i;
+    for (i = 0; i < 5; i++)
+        keys[i] = inverse(keys[i]);
+    for (i = 6; i < 11; i++)
+        keys[i] = inverse(keys[i]);
+}
+
+static uint32_t multiswap_step(const uint32_t *keys, uint32_t v) {
+    int i;
+    v *= keys[0];
+    for (i = 1; i < 5; i++) {
+        v = (v >> 16) | (v << 16);
+        v *= keys[i];
+    }
+    v += keys[5];
+    return v;
+}
+
+static uint32_t multiswap_inv_step(const uint32_t *keys, uint32_t v) {
+    int i;
+    v -= keys[5];
+    for (i = 4; i > 0; i--) {
+        v *= keys[i];
+        v = (v >> 16) | (v << 16);
+    }
+    v *= keys[0];
+    return v;
+}
+
+/**
+ * \brief "MultiSwap" encryption
+ * \param keys pointer to an array 12 32 bit numbers in machine endianness
+ *             0-4 and 6-10 must be inverted for the decryption
+ * \param key another key, this one must be the same for the decryption
+ * \param data data to encrypt
+ * \return encrypted data
+ */
+static uint64_t multiswap_enc(const uint32_t *keys, uint64_t key, uint64_t data) {
+    uint32_t a = data;
+    uint32_t b = data >> 32;
+    uint32_t c;
+    uint32_t tmp;
+    a += key;
+    tmp = multiswap_step(keys    , a);
+    b += tmp;
+    c = (key >> 32) + tmp;
+    tmp = multiswap_step(keys + 6, b);
+    c += tmp;
+    return ((uint64_t)c << 32) | tmp;
+}
+
+/**
+ * \brief "MultiSwap" decryption
+ * \param keys pointer to an array 12 32 bit numbers in machine endianness
+ *             0-4 and 6-10 must be inverted from encryption
+ * \param key another key, this one must be the same as for the encryption
+ * \param data data to decrypt
+ * \return decrypted data
+ */
+static uint64_t multiswap_dec(const uint32_t *keys, uint64_t key, uint64_t data) {
+    uint32_t a;
+    uint32_t b;
+    uint32_t c = data >> 32;
+    uint32_t tmp = data;
+    c -= tmp;
+    b = multiswap_inv_step(keys + 6, tmp);
+    tmp = c - (key >> 32);
+    b -= tmp;
+    a = multiswap_inv_step(keys    , tmp);
+    a -= key;
+    return ((uint64_t)b << 32) | a;
+}
+
+void ff_asfcrypt_dec(const uint8_t *key, uint8_t *data, int len) {
+    int num_qwords = len >> 3;
+    uint64_t *qwords = (uint64_t *)data;
+    uint64_t rc4buff[8];
+    uint64_t packetkey;
+    uint32_t ms_keys[12];
+    uint64_t ms_state;
+    int i;
+    if (len < 16) {
+        for (i = 0; i < len; i++)
+            data[i] ^= key[i];
+        return;
+    }
+
+    memset(rc4buff, 0, sizeof(rc4buff));
+    ff_rc4_enc(key, 12, (uint8_t *)rc4buff, sizeof(rc4buff));
+    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);
+    packetkey ^= rc4buff[6];
+
+    ff_rc4_enc((uint8_t *)&packetkey, 8, data, len);
+
+    ms_state = 0;
+    for (i = 0; i < num_qwords - 1; i++, qwords++)
+        ms_state = multiswap_enc(ms_keys, ms_state, AV_RL64(qwords));
+    multiswap_invert_keys(ms_keys);
+    packetkey = (packetkey << 32) | (packetkey >> 32);
+    packetkey = le2me_64(packetkey);
+    packetkey = multiswap_dec(ms_keys, ms_state, packetkey);
+    AV_WL64(qwords, packetkey);
+}
Index: libavformat/asfcrypt.h
===================================================================
--- libavformat/asfcrypt.h	(revision 0)
+++ libavformat/asfcrypt.h	(revision 0)
@@ -0,0 +1 @@
+void ff_asfcrypt_dec(const uint8_t *key, uint8_t *data, int len);
Index: libavformat/des.c
===================================================================
--- libavformat/des.c	(revision 0)
+++ libavformat/des.c	(revision 0)
@@ -0,0 +1,218 @@
+#include <inttypes.h>
+#include "des.h"
+
+#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),
+    T(60, 52, 44, 36, 28, 20, 12, 4),
+    T(62, 54, 46, 38, 30, 22, 14, 6),
+    T(64, 56, 48, 40, 32, 24, 16, 8),
+    T(57, 49, 41, 33, 25, 17,  9, 1),
+    T(59, 51, 43, 35, 27, 19, 11, 3),
+    T(61, 53, 45, 37, 29, 21, 13, 5),
+    T(63, 55, 47, 39, 31, 23, 15, 7)
+};
+#undef T
+
+#define T(a, b, c, d, e, f) 32-a,32-b,32-c,32-d,32-e,32-f
+static const uint8_t E_shuffle[] = {
+    T(32,  1,  2,  3,  4,  5),
+    T( 4,  5,  6,  7,  8,  9),
+    T( 8,  9, 10, 11, 12, 13),
+    T(12, 13, 14, 15, 16, 17),
+    T(16, 17, 18, 19, 20, 21),
+    T(20, 21, 22, 23, 24, 25),
+    T(24, 25, 26, 27, 28, 29),
+    T(28, 29, 30, 31, 32,  1)
+};
+#undef T
+
+#define T(a, b, c, d) 32-a,32-b,32-c,32-d
+static const uint8_t P_shuffle[] = {
+    T(16,  7, 20, 21),
+    T(29, 12, 28, 17),
+    T( 1, 15, 23, 26),
+    T( 5, 18, 31, 10),
+    T( 2,  8, 24, 14),
+    T(32, 27,  3,  9),
+    T(19, 13, 30,  6),
+    T(22, 11,  4, 25)
+};
+#undef T
+
+#define T(a, b, c, d, e, f, g) 64-a,64-b,64-c,64-d,64-e,64-f,64-g
+static const uint8_t PC1_shuffle[] = {
+    T(57, 49, 41, 33, 25, 17,  9),
+    T( 1, 58, 50, 42, 34, 26, 18),
+    T(10,  2, 59, 51, 43, 35, 27),
+    T(19, 11,  3, 60, 52, 44, 36),
+    T(63, 55, 47, 39, 31, 23, 15),
+    T( 7, 62, 54, 46, 38, 30, 22),
+    T(14,  6, 61, 53, 45, 37, 29),
+    T(21, 13,  5, 28, 20, 12,  4)
+};
+#undef T
+
+#define T(a, b, c, d, e, f) 56-a,56-b,56-c,56-d,56-e,56-f
+static const uint8_t PC2_shuffle[] = {
+    T(14, 17, 11, 24,  1,  5),
+    T( 3, 28, 15,  6, 21, 10),
+    T(23, 19, 12,  4, 26,  8),
+    T(16,  7, 27, 20, 13,  2),
+    T(41, 52, 31, 37, 47, 55),
+    T(30, 40, 51, 45, 33, 48),
+    T(44, 49, 39, 56, 34, 53),
+    T(46, 42, 50, 36, 29, 32)
+};
+#undef T
+
+static const uint8_t S_boxes[8][32] = {
+    {
+    0x0e, 0xf4, 0x7d, 0x41, 0xe2, 0x2f, 0xdb, 0x18, 0xa3, 0x6a, 0xc6, 0xbc, 0x95, 0x59, 0x30, 0x87,
+    0xf4, 0xc1, 0x8e, 0x28, 0x4d, 0x96, 0x12, 0x7b, 0x5f, 0xbc, 0x39, 0xe7, 0xa3, 0x0a, 0x65, 0xd0,
+    }, {
+    0x3f, 0xd1, 0x48, 0x7e, 0xf6, 0x2b, 0x83, 0xe4, 0xc9, 0x07, 0x12, 0xad, 0x6c, 0x90, 0xb5, 0x5a,
+    0xd0, 0x8e, 0xa7, 0x1b, 0x3a, 0xf4, 0x4d, 0x21, 0xb5, 0x68, 0x7c, 0xc6, 0x09, 0x53, 0xe2, 0x9f,
+    }, {
+    0xda, 0x70, 0x09, 0x9e, 0x36, 0x43, 0x6f, 0xa5, 0x21, 0x8d, 0x5c, 0xe7, 0xcb, 0xb4, 0xf2, 0x18,
+    0x1d, 0xa6, 0xd4, 0x09, 0x68, 0x9f, 0x83, 0x70, 0x4b, 0xf1, 0xe2, 0x3c, 0xb5, 0x5a, 0x2e, 0xc7,
+    }, {
+    0xd7, 0x8d, 0xbe, 0x53, 0x60, 0xf6, 0x09, 0x3a, 0x41, 0x72, 0x28, 0xc5, 0x1b, 0xac, 0xe4, 0x9f,
+    0x3a, 0xf6, 0x09, 0x60, 0xac, 0x1b, 0xd7, 0x8d, 0x9f, 0x41, 0x53, 0xbe, 0xc5, 0x72, 0x28, 0xe4,
+    }, {
+    0xe2, 0xbc, 0x24, 0xc1, 0x47, 0x7a, 0xdb, 0x16, 0x58, 0x05, 0xf3, 0xaf, 0x3d, 0x90, 0x8e, 0x69,
+    0xb4, 0x82, 0xc1, 0x7b, 0x1a, 0xed, 0x27, 0xd8, 0x6f, 0xf9, 0x0c, 0x95, 0xa6, 0x43, 0x50, 0x3e,
+    }, {
+    0xac, 0xf1, 0x4a, 0x2f, 0x79, 0xc2, 0x96, 0x58, 0x60, 0x1d, 0xd3, 0xe4, 0x0e, 0xb7, 0x35, 0x8b,
+    0x49, 0x3e, 0x2f, 0xc5, 0x92, 0x58, 0xfc, 0xa3, 0xb7, 0xe0, 0x14, 0x7a, 0x61, 0x0d, 0x8b, 0xd6,
+    }, {
+    0xd4, 0x0b, 0xb2, 0x7e, 0x4f, 0x90, 0x18, 0xad, 0xe3, 0x3c, 0x59, 0xc7, 0x25, 0xfa, 0x86, 0x61,
+    0x61, 0xb4, 0xdb, 0x8d, 0x1c, 0x43, 0xa7, 0x7e, 0x9a, 0x5f, 0x06, 0xf8, 0xe0, 0x25, 0x39, 0xc2,
+    }, {
+    0x1d, 0xf2, 0xd8, 0x84, 0xa6, 0x3f, 0x7b, 0x41, 0xca, 0x59, 0x63, 0xbe, 0x05, 0xe0, 0x9c, 0x27,
+    0x27, 0x1b, 0xe4, 0x71, 0x49, 0xac, 0x8e, 0xd2, 0xf0, 0xc6, 0x9a, 0x0d, 0x3f, 0x53, 0x65, 0xb8,
+    }
+};
+
+static uint64_t shuffle(uint64_t in, const uint8_t *shuffle, int shuffle_len) {
+    int i;
+    uint64_t res = 0;
+    for (i = 0; i < shuffle_len; i++)
+        res = (res << 1) | ((in >> *shuffle++) & 1);
+    return res;
+}
+
+static uint64_t shuffle_inv(uint64_t in, const uint8_t *shuffle, int shuffle_len) {
+    int i;
+    uint64_t res = 0;
+    shuffle += shuffle_len - 1;
+    for (i = 0; i < shuffle_len; i++) {
+        res |= (in & 1) << *shuffle--;
+        in >>= 1;
+    }
+    return res;
+}
+
+static uint32_t f_func(uint32_t r, uint64_t k) {
+    int i;
+    uint32_t out = 0;
+    // expand 32 bit data to 8 * 6 bit blocks
+    uint64_t tmp = shuffle(r, E_shuffle, sizeof(E_shuffle));
+    tmp ^= k;
+    // apply S-boxes, those compress the data again from 8 * 6 to 8 * 4 bits
+    for (i = 0; i < 8; i++) {
+        uint32_t v = S_boxes[i][(tmp >> 43) & 0x1f];
+        if (tmp & ((uint64_t)1 << 42)) v >>= 4;
+        else v &= 0x0f;
+        tmp <<= 6;
+        out = (out << 4) | v;
+    }
+    out = shuffle(out, P_shuffle, sizeof(P_shuffle));
+    return out;
+}
+
+/**
+ * \brief rotate the two halves of the expanded 56 bit key each 1 bit left
+ *
+ * Note: the specification calls this "shift", so I kept it although
+ * it is confusing.
+ */
+static uint64_t key_shift_left(uint64_t CDn) {
+    uint64_t carries = (CDn >> 27) & 0x10000001;
+    CDn <<= 1;
+    CDn &= ~0x10000001;
+    CDn |= carries;
+    return CDn;
+}
+
+/**
+ * \brief rotate the two halves of the expanded 56 bit key each 1 bit right
+ *
+ * Note: the specification calls this "shift", so I kept it although
+ * it is confusing.
+ */
+static uint64_t key_shift_right(uint64_t CDn) {
+    uint64_t carries = (CDn & 0x10000001) << 27;
+    CDn >>= 1;
+    CDn &= ~(0x10000001ULL << 27);
+    CDn |= carries;
+    return CDn;
+}
+
+uint64_t ff_des_encdec(uint64_t in, uint64_t key, int decrypt) {
+    int i;
+    // discard parity bits from key and shuffle it into C and D parts
+    uint64_t CDn = shuffle(key, PC1_shuffle, sizeof(PC1_shuffle));
+    // shuffle irrelevant to security but to ease hardware implementations
+    in = shuffle(in, IP_shuffle, sizeof(IP_shuffle));
+    for (i = 0; i < 16; i++) {
+        uint64_t Kn;
+        uint32_t f_res;
+        if (!decrypt) {
+            CDn = key_shift_left(CDn);
+            if (i > 1 && i != 8 && i != 15)
+                CDn = key_shift_left(CDn);
+        }
+        Kn = shuffle(CDn, PC2_shuffle, sizeof(PC2_shuffle));
+        f_res = f_func(in, Kn);
+        in = (in << 32) | (in >> 32);
+        in ^= f_res;
+        if (decrypt) {
+            CDn = key_shift_right(CDn);
+            if (i != 0 && i != 7 && i < 14)
+                CDn = key_shift_right(CDn);
+        }
+    }
+    in = (in << 32) | (in >> 32);
+    // reverse shuffle used to ease hardware implementations
+    in = shuffle_inv(in, IP_shuffle, sizeof(IP_shuffle));
+    return in;
+}
+
+#ifdef TEST
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/time.h>
+static uint64_t rand64(void) {
+    uint64_t r = rand();
+    r = (r << 32) | rand();
+    return r;
+}
+
+int main(void) {
+    struct timeval tv;
+    uint64_t key;
+    uint64_t data;
+    uint64_t ct;
+    gettimeofday(&tv, NULL);
+    srand(tv.tv_sec * 1000 * 1000 + tv.tv_usec);
+    key = rand64();
+    data = rand64();
+    ct = des_encdec(data, key, 0);
+    if (des_encdec(ct, key, 1) != data) {
+        printf("Test failed\n");
+        return 1;
+    }
+    return 0;
+}
+#endif
Index: libavformat/des.h
===================================================================
--- libavformat/des.h	(revision 0)
+++ libavformat/des.h	(revision 0)
@@ -0,0 +1 @@
+uint64_t ff_des_encdec(uint64_t in, uint64_t key, int decrypt);
Index: libavformat/rc4.c
===================================================================
--- libavformat/rc4.c	(revision 0)
+++ libavformat/rc4.c	(revision 0)
@@ -0,0 +1,27 @@
+// loosely based on LibTomCrypt by Tom St Denis
+#include "common.h"
+#include "rc4.h"
+
+void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen) {
+    int i, j;
+    uint8_t x, y;
+    uint8_t state[256];
+    for (i = 0; i < 256; i++)
+        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]);
+    }
+    // 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];
+        x++;
+        y += state[x];
+    }
+}
Index: libavformat/rc4.h
===================================================================
--- libavformat/rc4.h	(revision 0)
+++ libavformat/rc4.h	(revision 0)
@@ -0,0 +1 @@
+void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen);
Index: libavformat/Makefile
===================================================================
--- libavformat/Makefile	(revision 10695)
+++ libavformat/Makefile	(working copy)
@@ -21,7 +21,7 @@
 OBJS-$(CONFIG_AMR_MUXER)                 += amr.o
 OBJS-$(CONFIG_APC_DEMUXER)               += apc.o
 OBJS-$(CONFIG_APE_DEMUXER)               += ape.o
-OBJS-$(CONFIG_ASF_DEMUXER)               += asf.o riff.o
+OBJS-$(CONFIG_ASF_DEMUXER)               += asf.o riff.o asfcrypt.o des.o rc4.o
 OBJS-$(CONFIG_ASF_MUXER)                 += asf-enc.o riff.o
 OBJS-$(CONFIG_ASF_STREAM_MUXER)          += asf-enc.o riff.o
 OBJS-$(CONFIG_AU_DEMUXER)                += au.o raw.o
Index: libavformat/asf.c
===================================================================
--- libavformat/asf.c	(revision 10695)
+++ libavformat/asf.c	(working copy)
@@ -23,6 +23,7 @@
 #include "mpegaudio.h"
 #include "asf.h"
 #include "common.h"
+#include "asfcrypt.h"
 
 #undef NDEBUG
 #include <assert.h>
@@ -823,6 +830,9 @@
 
         get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset,
                    asf->packet_frag_size);
+        if (s->key && s->keylen == 20)
+            ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset,
+                            asf->packet_frag_size);
         asf_st->frag_offset += asf->packet_frag_size;
         /* test if whole packet is read */
         if (asf_st->frag_offset == asf_st->pkt.size) {



More information about the ffmpeg-devel mailing list