3 * Copyright (c) 2007 Reimar Doeffinger
4 * This is a rewrite of code contained in freeme/freeme2
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/bswap.h"
24 #include "libavutil/common.h"
25 #include "libavutil/des.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/rc4.h"
31 * @brief find multiplicative inverse modulo 2 ^ 32
32 * @param v number to invert, must be odd!
33 * @return number so that result * v = 1 (mod 2^32)
35 static uint32_t inverse(uint32_t v)
37 // v ^ 3 gives the inverse (mod 16), could also be implemented
38 // as table etc. (only lowest 4 bits matter!)
39 uint32_t inverse = v * v * v;
40 // uses a fixpoint-iteration that doubles the number
41 // of correct lowest bits each time
42 inverse *= 2 - v * inverse;
43 inverse *= 2 - v * inverse;
44 inverse *= 2 - v * inverse;
49 * @brief read keys from keybuf into keys
50 * @param keybuf buffer containing the keys
51 * @param keys output key array containing the keys for encryption in
54 static void multiswap_init(const uint8_t keybuf[48], uint32_t keys[12])
57 for (i = 0; i < 12; i++)
58 keys[i] = AV_RL32(keybuf + (i << 2)) | 1;
62 * @brief invert the keys so that encryption become decryption keys and
63 * the other way round.
64 * @param keys key array of ints to invert
66 static void multiswap_invert_keys(uint32_t keys[12])
69 for (i = 0; i < 5; i++)
70 keys[i] = inverse(keys[i]);
71 for (i = 6; i < 11; i++)
72 keys[i] = inverse(keys[i]);
75 static uint32_t multiswap_step(const uint32_t keys[12], uint32_t v)
79 for (i = 1; i < 5; i++) {
80 v = (v >> 16) | (v << 16);
87 static uint32_t multiswap_inv_step(const uint32_t keys[12], uint32_t v)
91 for (i = 4; i > 0; i--) {
93 v = (v >> 16) | (v << 16);
100 * @brief "MultiSwap" encryption
101 * @param keys 32 bit numbers in machine endianness,
102 * 0-4 and 6-10 must be inverted from decryption
103 * @param key another key, this one must be the same for the decryption
104 * @param data data to encrypt
105 * @return encrypted data
107 static uint64_t multiswap_enc(const uint32_t keys[12],
108 uint64_t key, uint64_t data)
111 uint32_t b = data >> 32;
115 tmp = multiswap_step(keys, a);
117 c = (key >> 32) + tmp;
118 tmp = multiswap_step(keys + 6, b);
120 return ((uint64_t)c << 32) | tmp;
124 * @brief "MultiSwap" decryption
125 * @param keys 32 bit numbers in machine endianness,
126 * 0-4 and 6-10 must be inverted from encryption
127 * @param key another key, this one must be the same as for the encryption
128 * @param data data to decrypt
129 * @return decrypted data
131 static uint64_t multiswap_dec(const uint32_t keys[12],
132 uint64_t key, uint64_t data)
136 uint32_t c = data >> 32;
139 b = multiswap_inv_step(keys + 6, tmp);
140 tmp = c - (key >> 32);
142 a = multiswap_inv_step(keys, tmp);
144 return ((uint64_t)b << 32) | a;
147 void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len)
151 int num_qwords = len >> 3;
152 uint8_t *qwords = data;
153 uint64_t rc4buff[8] = { 0 };
155 uint32_t ms_keys[12];
159 for (i = 0; i < len; i++)
164 av_rc4_init(&rc4, key, 12 * 8, 1);
165 av_rc4_crypt(&rc4, (uint8_t *)rc4buff, NULL, sizeof(rc4buff), NULL, 1);
166 multiswap_init((uint8_t *)rc4buff, ms_keys);
168 packetkey = AV_RN64(&qwords[num_qwords * 8 - 8]);
169 packetkey ^= rc4buff[7];
170 av_des_init(&des, key + 12, 64, 1);
171 av_des_crypt(&des, (uint8_t *)&packetkey, (uint8_t *)&packetkey, 1, NULL, 1);
172 packetkey ^= rc4buff[6];
174 av_rc4_init(&rc4, (uint8_t *)&packetkey, 64, 1);
175 av_rc4_crypt(&rc4, data, data, len, NULL, 1);
178 for (i = 0; i < num_qwords - 1; i++, qwords += 8)
179 ms_state = multiswap_enc(ms_keys, ms_state, AV_RL64(qwords));
180 multiswap_invert_keys(ms_keys);
181 packetkey = (packetkey << 32) | (packetkey >> 32);
182 packetkey = av_le2ne64(packetkey);
183 packetkey = multiswap_dec(ms_keys, ms_state, packetkey);
184 AV_WL64(qwords, packetkey);