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
23 #include <stddef.h>
30
31 /**
32 * @brief find multiplicative inverse modulo 2 ^ 32
33 * @param v number to invert, must be odd!
34 * @return number so that result * v = 1 (mod 2^32)
35 */
37 {
38 // v ^ 3 gives the inverse (mod 16), could also be implemented
39 // as table etc. (only lowest 4 bits matter!)
41 // uses a fixpoint-iteration that doubles the number
42 // of correct lowest bits each time
47 }
48
49 /**
50 * @brief read keys from keybuf into keys
51 * @param keybuf buffer containing the keys
52 * @param keys output key array containing the keys for encryption in
53 * native endianness
54 */
56 {
58 for (
i = 0;
i < 12;
i++)
60 }
61
62 /**
63 * @brief invert the keys so that encryption become decryption keys and
64 * the other way round.
65 * @param keys key array of ints to invert
66 */
68 {
70 for (
i = 0;
i < 5;
i++)
72 for (
i = 6;
i < 11;
i++)
74 }
75
77 {
79 v *= keys[0];
80 for (
i = 1;
i < 5;
i++) {
81 v = (v >> 16) | (v << 16);
83 }
84 v += keys[5];
85 return v;
86 }
87
89 {
91 v -= keys[5];
92 for (
i = 4;
i > 0;
i--) {
94 v = (v >> 16) | (v << 16);
95 }
96 v *= keys[0];
97 return v;
98 }
99
100 /**
101 * @brief "MultiSwap" encryption
102 * @param keys 32 bit numbers in machine endianness,
103 * 0-4 and 6-10 must be inverted from decryption
104 * @param key another key, this one must be the same for the decryption
105 * @param data data to encrypt
106 * @return encrypted data
107 */
110 {
112 uint32_t
b =
data >> 32;
121 return ((uint64_t)
c << 32) |
tmp;
122 }
123
124 /**
125 * @brief "MultiSwap" decryption
126 * @param keys 32 bit numbers in machine endianness,
127 * 0-4 and 6-10 must be inverted from encryption
128 * @param key another key, this one must be the same as for the encryption
129 * @param data data to decrypt
130 * @return decrypted data
131 */
134 {
137 uint32_t
c =
data >> 32;
145 return ((uint64_t)
b << 32) |
a;
146 }
147
149 {
152 int num_qwords =
len >> 3;
153 uint8_t *qwords =
data;
154 uint64_t rc4buff[8] = { 0 };
155 uint64_t packetkey;
156 uint32_t ms_keys[12];
157 uint64_t ms_state;
162 return;
163 }
166 if (!des || !rc4) {
169 return;
170 }
171
175
176 packetkey =
AV_RN64(&qwords[num_qwords * 8 - 8]);
177 packetkey ^= rc4buff[7];
179 av_des_crypt(des, (uint8_t *)&packetkey, (uint8_t *)&packetkey, 1,
NULL, 1);
180 packetkey ^= rc4buff[6];
181
184
185 ms_state = 0;
186 for (
i = 0;
i < num_qwords - 1;
i++, qwords += 8)
189 packetkey = (packetkey << 32) | (packetkey >> 32);
193
196 }