Originally committed as revision 10821 to svn://svn.ffmpeg.org/ffmpeg/trunk
| ... | ... |
@@ -21,7 +21,7 @@ OBJS-$(CONFIG_AMR_DEMUXER) += amr.o |
| 21 | 21 |
OBJS-$(CONFIG_AMR_MUXER) += amr.o |
| 22 | 22 |
OBJS-$(CONFIG_APC_DEMUXER) += apc.o |
| 23 | 23 |
OBJS-$(CONFIG_APE_DEMUXER) += ape.o |
| 24 |
-OBJS-$(CONFIG_ASF_DEMUXER) += asf.o riff.o |
|
| 24 |
+OBJS-$(CONFIG_ASF_DEMUXER) += asf.o asfcrypt.o riff.o |
|
| 25 | 25 |
OBJS-$(CONFIG_ASF_MUXER) += asf-enc.o riff.o |
| 26 | 26 |
OBJS-$(CONFIG_ASF_STREAM_MUXER) += asf-enc.o riff.o |
| 27 | 27 |
OBJS-$(CONFIG_AU_DEMUXER) += au.o raw.o |
| ... | ... |
@@ -23,6 +23,7 @@ |
| 23 | 23 |
#include "mpegaudio.h" |
| 24 | 24 |
#include "asf.h" |
| 25 | 25 |
#include "common.h" |
| 26 |
+#include "asfcrypt.h" |
|
| 26 | 27 |
|
| 27 | 28 |
#undef NDEBUG |
| 28 | 29 |
#include <assert.h> |
| ... | ... |
@@ -823,6 +824,9 @@ static int asf_read_packet(AVFormatContext *s, AVPacket *pkt) |
| 823 | 823 |
|
| 824 | 824 |
get_buffer(pb, asf_st->pkt.data + asf->packet_frag_offset, |
| 825 | 825 |
asf->packet_frag_size); |
| 826 |
+ if (s->key && s->keylen == 20) |
|
| 827 |
+ ff_asfcrypt_dec(s->key, asf_st->pkt.data + asf->packet_frag_offset, |
|
| 828 |
+ asf->packet_frag_size); |
|
| 826 | 829 |
asf_st->frag_offset += asf->packet_frag_size; |
| 827 | 830 |
/* test if whole packet is read */ |
| 828 | 831 |
if (asf_st->frag_offset == asf_st->pkt.size) {
|
| 829 | 832 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,172 @@ |
| 0 |
+/* |
|
| 1 |
+ * ASF decryption |
|
| 2 |
+ * Copyright (c) 2007 Reimar Doeffinger |
|
| 3 |
+ * This is a rewrite of code contained in freeme/freeme2 |
|
| 4 |
+ * |
|
| 5 |
+ * This file is part of FFmpeg. |
|
| 6 |
+ * |
|
| 7 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
| 8 |
+ * modify it under the terms of the GNU Lesser General Public |
|
| 9 |
+ * License as published by the Free Software Foundation; either |
|
| 10 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
| 11 |
+ * |
|
| 12 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
| 13 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 14 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
| 15 |
+ * Lesser General Public License for more details. |
|
| 16 |
+ * |
|
| 17 |
+ * You should have received a copy of the GNU Lesser General Public |
|
| 18 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
| 19 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
| 20 |
+ */ |
|
| 21 |
+#include "common.h" |
|
| 22 |
+#include "intreadwrite.h" |
|
| 23 |
+#include "bswap.h" |
|
| 24 |
+#include "des.h" |
|
| 25 |
+#include "rc4.h" |
|
| 26 |
+#include "asfcrypt.h" |
|
| 27 |
+ |
|
| 28 |
+/** |
|
| 29 |
+ * \brief find multiplicative inverse modulo 2 ^ 32 |
|
| 30 |
+ * \param v number to invert, must be odd! |
|
| 31 |
+ * \return number so that result * v = 1 (mod 2^32) |
|
| 32 |
+ */ |
|
| 33 |
+static uint32_t inverse(uint32_t v) {
|
|
| 34 |
+ // v ^ 3 gives the inverse (mod 16), could also be implemented |
|
| 35 |
+ // as table etc. (only lowest 4 bits matter!) |
|
| 36 |
+ uint32_t inverse = v * v * v; |
|
| 37 |
+ // uses a fixpoint-iteration that doubles the number |
|
| 38 |
+ // of correct lowest bits each time |
|
| 39 |
+ inverse *= 2 - v * inverse; |
|
| 40 |
+ inverse *= 2 - v * inverse; |
|
| 41 |
+ inverse *= 2 - v * inverse; |
|
| 42 |
+ return inverse; |
|
| 43 |
+} |
|
| 44 |
+ |
|
| 45 |
+/** |
|
| 46 |
+ * \brief read keys from keybuf into keys |
|
| 47 |
+ * \param keybuf buffer containing the keys |
|
| 48 |
+ * \param keys output key array containing the keys for encryption in |
|
| 49 |
+ * native endianness |
|
| 50 |
+ */ |
|
| 51 |
+static void multiswap_init(const uint8_t keybuf[48], uint32_t keys[12]) {
|
|
| 52 |
+ int i; |
|
| 53 |
+ for (i = 0; i < 12; i++) |
|
| 54 |
+ keys[i] = AV_RL32(keybuf + (i << 2)) | 1; |
|
| 55 |
+} |
|
| 56 |
+ |
|
| 57 |
+/** |
|
| 58 |
+ * \brief invert the keys so that encryption become decryption keys and |
|
| 59 |
+ * the other way round. |
|
| 60 |
+ * \param keys key array of ints to invert |
|
| 61 |
+ */ |
|
| 62 |
+static void multiswap_invert_keys(uint32_t keys[12]) {
|
|
| 63 |
+ int i; |
|
| 64 |
+ for (i = 0; i < 5; i++) |
|
| 65 |
+ keys[i] = inverse(keys[i]); |
|
| 66 |
+ for (i = 6; i < 11; i++) |
|
| 67 |
+ keys[i] = inverse(keys[i]); |
|
| 68 |
+} |
|
| 69 |
+ |
|
| 70 |
+static uint32_t multiswap_step(const uint32_t keys[12], uint32_t v) {
|
|
| 71 |
+ int i; |
|
| 72 |
+ v *= keys[0]; |
|
| 73 |
+ for (i = 1; i < 5; i++) {
|
|
| 74 |
+ v = (v >> 16) | (v << 16); |
|
| 75 |
+ v *= keys[i]; |
|
| 76 |
+ } |
|
| 77 |
+ v += keys[5]; |
|
| 78 |
+ return v; |
|
| 79 |
+} |
|
| 80 |
+ |
|
| 81 |
+static uint32_t multiswap_inv_step(const uint32_t keys[12], uint32_t v) {
|
|
| 82 |
+ int i; |
|
| 83 |
+ v -= keys[5]; |
|
| 84 |
+ for (i = 4; i > 0; i--) {
|
|
| 85 |
+ v *= keys[i]; |
|
| 86 |
+ v = (v >> 16) | (v << 16); |
|
| 87 |
+ } |
|
| 88 |
+ v *= keys[0]; |
|
| 89 |
+ return v; |
|
| 90 |
+} |
|
| 91 |
+ |
|
| 92 |
+/** |
|
| 93 |
+ * \brief "MultiSwap" encryption |
|
| 94 |
+ * \param keys 32 bit numbers in machine endianness, |
|
| 95 |
+ * 0-4 and 6-10 must be inverted from decryption |
|
| 96 |
+ * \param key another key, this one must be the same for the decryption |
|
| 97 |
+ * \param data data to encrypt |
|
| 98 |
+ * \return encrypted data |
|
| 99 |
+ */ |
|
| 100 |
+static uint64_t multiswap_enc(const uint32_t keys[12], uint64_t key, uint64_t data) {
|
|
| 101 |
+ uint32_t a = data; |
|
| 102 |
+ uint32_t b = data >> 32; |
|
| 103 |
+ uint32_t c; |
|
| 104 |
+ uint32_t tmp; |
|
| 105 |
+ a += key; |
|
| 106 |
+ tmp = multiswap_step(keys , a); |
|
| 107 |
+ b += tmp; |
|
| 108 |
+ c = (key >> 32) + tmp; |
|
| 109 |
+ tmp = multiswap_step(keys + 6, b); |
|
| 110 |
+ c += tmp; |
|
| 111 |
+ return ((uint64_t)c << 32) | tmp; |
|
| 112 |
+} |
|
| 113 |
+ |
|
| 114 |
+/** |
|
| 115 |
+ * \brief "MultiSwap" decryption |
|
| 116 |
+ * \param keys 32 bit numbers in machine endianness, |
|
| 117 |
+ * 0-4 and 6-10 must be inverted from encryption |
|
| 118 |
+ * \param key another key, this one must be the same as for the encryption |
|
| 119 |
+ * \param data data to decrypt |
|
| 120 |
+ * \return decrypted data |
|
| 121 |
+ */ |
|
| 122 |
+static uint64_t multiswap_dec(const uint32_t keys[12], uint64_t key, uint64_t data) {
|
|
| 123 |
+ uint32_t a; |
|
| 124 |
+ uint32_t b; |
|
| 125 |
+ uint32_t c = data >> 32; |
|
| 126 |
+ uint32_t tmp = data; |
|
| 127 |
+ c -= tmp; |
|
| 128 |
+ b = multiswap_inv_step(keys + 6, tmp); |
|
| 129 |
+ tmp = c - (key >> 32); |
|
| 130 |
+ b -= tmp; |
|
| 131 |
+ a = multiswap_inv_step(keys , tmp); |
|
| 132 |
+ a -= key; |
|
| 133 |
+ return ((uint64_t)b << 32) | a; |
|
| 134 |
+} |
|
| 135 |
+ |
|
| 136 |
+void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len) {
|
|
| 137 |
+ int num_qwords = len >> 3; |
|
| 138 |
+ uint64_t *qwords = (uint64_t *)data; |
|
| 139 |
+ uint64_t rc4buff[8]; |
|
| 140 |
+ uint64_t packetkey; |
|
| 141 |
+ uint32_t ms_keys[12]; |
|
| 142 |
+ uint64_t ms_state; |
|
| 143 |
+ int i; |
|
| 144 |
+ if (len < 16) {
|
|
| 145 |
+ for (i = 0; i < len; i++) |
|
| 146 |
+ data[i] ^= key[i]; |
|
| 147 |
+ return; |
|
| 148 |
+ } |
|
| 149 |
+ |
|
| 150 |
+ memset(rc4buff, 0, sizeof(rc4buff)); |
|
| 151 |
+ ff_rc4_enc(key, 12, (uint8_t *)rc4buff, sizeof(rc4buff)); |
|
| 152 |
+ multiswap_init((uint8_t *)rc4buff, ms_keys); |
|
| 153 |
+ |
|
| 154 |
+ packetkey = qwords[num_qwords - 1]; |
|
| 155 |
+ packetkey ^= rc4buff[7]; |
|
| 156 |
+ packetkey = be2me_64(packetkey); |
|
| 157 |
+ packetkey = ff_des_encdec(packetkey, AV_RB64(key + 12), 1); |
|
| 158 |
+ packetkey = be2me_64(packetkey); |
|
| 159 |
+ packetkey ^= rc4buff[6]; |
|
| 160 |
+ |
|
| 161 |
+ ff_rc4_enc((uint8_t *)&packetkey, 8, data, len); |
|
| 162 |
+ |
|
| 163 |
+ ms_state = 0; |
|
| 164 |
+ for (i = 0; i < num_qwords - 1; i++, qwords++) |
|
| 165 |
+ ms_state = multiswap_enc(ms_keys, ms_state, AV_RL64(qwords)); |
|
| 166 |
+ multiswap_invert_keys(ms_keys); |
|
| 167 |
+ packetkey = (packetkey << 32) | (packetkey >> 32); |
|
| 168 |
+ packetkey = le2me_64(packetkey); |
|
| 169 |
+ packetkey = multiswap_dec(ms_keys, ms_state, packetkey); |
|
| 170 |
+ AV_WL64(qwords, packetkey); |
|
| 171 |
+} |
| 0 | 172 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,27 @@ |
| 0 |
+/* |
|
| 1 |
+ * ASF decryption |
|
| 2 |
+ * Copyright (c) 2007 Reimar Doeffinger |
|
| 3 |
+ * |
|
| 4 |
+ * This file is part of FFmpeg. |
|
| 5 |
+ * |
|
| 6 |
+ * FFmpeg is free software; you can redistribute it and/or |
|
| 7 |
+ * modify it under the terms of the GNU Lesser General Public |
|
| 8 |
+ * License as published by the Free Software Foundation; either |
|
| 9 |
+ * version 2.1 of the License, or (at your option) any later version. |
|
| 10 |
+ * |
|
| 11 |
+ * FFmpeg is distributed in the hope that it will be useful, |
|
| 12 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 13 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
| 14 |
+ * Lesser General Public License for more details. |
|
| 15 |
+ * |
|
| 16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
| 17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
| 18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
| 19 |
+ */ |
|
| 20 |
+ |
|
| 21 |
+#ifndef FFMPEG_ASFCRYPT_H |
|
| 22 |
+#define FFMPEG_ASFCRYPT_H |
|
| 23 |
+ |
|
| 24 |
+void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len); |
|
| 25 |
+ |
|
| 26 |
+#endif |