1 /*
2 * ASF decryption
3 * Copyright (c) 2007 Reimar Doeffinger
4 * This is a rewrite of code contained in freeme/freeme2
5 *
6 * This file is part of FFmpeg.
7 *
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.
12 *
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.
17 *
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
21 */
22
29
30 /**
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)
34 */
36 {
37 // v ^ 3 gives the inverse (mod 16), could also be implemented
38 // as table etc. (only lowest 4 bits matter!)
40 // uses a fixpoint-iteration that doubles the number
41 // of correct lowest bits each time
46 }
47
48 /**
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
52 * native endianness
53 */
55 {
56 int i;
57 for (i = 0; i < 12; i++)
58 keys[i] =
AV_RL32(keybuf + (i << 2)) | 1;
59 }
60
61 /**
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
65 */
67 {
68 int i;
69 for (i = 0; i < 5; i++)
71 for (i = 6; i < 11; i++)
73 }
74
76 {
77 int i;
78 v *= keys[0];
79 for (i = 1; i < 5; i++) {
80 v = (v >> 16) | (v << 16);
81 v *= keys[i];
82 }
83 v += keys[5];
85 }
86
88 {
89 int i;
90 v -= keys[5];
91 for (i = 4; i > 0; i--) {
92 v *= keys[i];
93 v = (v >> 16) | (v << 16);
94 }
95 v *= keys[0];
97 }
98
99 /**
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
106 */
108 uint64_t key, uint64_t
data)
109 {
111 uint32_t
b = data >> 32;
113 uint32_t tmp;
114 a += key;
116 b += tmp;
117 c = (key >> 32) + tmp;
119 c += tmp;
120 return ((uint64_t)c << 32) | tmp;
121 }
122
123 /**
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
130 */
132 uint64_t key, uint64_t
data)
133 {
136 uint32_t
c = data >> 32;
138 c -= tmp;
140 tmp = c - (key >> 32);
141 b -= tmp;
143 a -= key;
144 return ((uint64_t)b << 32) |
a;
145 }
146
148 {
151 int num_qwords = len >> 3;
153 uint64_t rc4buff[8] = { 0 };
154 uint64_t packetkey;
155 uint32_t ms_keys[12];
156 uint64_t ms_state;
157 int i;
158 if (len < 16) {
159 for (i = 0; i <
len; i++)
160 data[i] ^= key[i];
161 return;
162 }
163
167
168 packetkey =
AV_RN64(&qwords[num_qwords * 8 - 8]);
169 packetkey ^= rc4buff[7];
172 packetkey ^= rc4buff[6];
173
176
177 ms_state = 0;
178 for (i = 0; i < num_qwords - 1; i++, qwords += 8)
181 packetkey = (packetkey << 32) | (packetkey >> 32);
185 }