2007-10-20 16:25:02 +02:00
|
|
|
/*
|
|
|
|
* ASF decryption
|
|
|
|
* Copyright (c) 2007 Reimar Doeffinger
|
|
|
|
* This is a rewrite of code contained in freeme/freeme2
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2008-05-09 13:56:36 +02:00
|
|
|
|
|
|
|
#include "libavutil/bswap.h"
|
2013-02-06 00:44:00 +01:00
|
|
|
#include "libavutil/common.h"
|
2008-05-09 13:56:36 +02:00
|
|
|
#include "libavutil/des.h"
|
2013-02-06 00:44:00 +01:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2008-05-09 13:56:36 +02:00
|
|
|
#include "libavutil/rc4.h"
|
2007-10-20 16:25:02 +02:00
|
|
|
#include "asfcrypt.h"
|
|
|
|
|
|
|
|
/**
|
2011-06-23 22:41:54 +02:00
|
|
|
* @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)
|
2007-10-20 16:25:02 +02:00
|
|
|
*/
|
2013-02-06 00:44:00 +01:00
|
|
|
static uint32_t inverse(uint32_t v)
|
|
|
|
{
|
2007-10-20 16:25:02 +02:00
|
|
|
// v ^ 3 gives the inverse (mod 16), could also be implemented
|
|
|
|
// as table etc. (only lowest 4 bits matter!)
|
|
|
|
uint32_t inverse = v * v * v;
|
|
|
|
// uses a fixpoint-iteration that doubles the number
|
|
|
|
// of correct lowest bits each time
|
|
|
|
inverse *= 2 - v * inverse;
|
|
|
|
inverse *= 2 - v * inverse;
|
|
|
|
inverse *= 2 - v * inverse;
|
|
|
|
return inverse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-06-23 22:41:54 +02:00
|
|
|
* @brief read keys from keybuf into keys
|
|
|
|
* @param keybuf buffer containing the keys
|
|
|
|
* @param keys output key array containing the keys for encryption in
|
2007-10-20 16:25:02 +02:00
|
|
|
* native endianness
|
|
|
|
*/
|
2013-02-06 00:44:00 +01:00
|
|
|
static void multiswap_init(const uint8_t keybuf[48], uint32_t keys[12])
|
|
|
|
{
|
2007-10-20 16:25:02 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 12; i++)
|
|
|
|
keys[i] = AV_RL32(keybuf + (i << 2)) | 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-06-23 22:41:54 +02:00
|
|
|
* @brief invert the keys so that encryption become decryption keys and
|
2007-10-20 16:25:02 +02:00
|
|
|
* the other way round.
|
2011-06-23 22:41:54 +02:00
|
|
|
* @param keys key array of ints to invert
|
2007-10-20 16:25:02 +02:00
|
|
|
*/
|
2013-02-06 00:44:00 +01:00
|
|
|
static void multiswap_invert_keys(uint32_t keys[12])
|
|
|
|
{
|
2007-10-20 16:25:02 +02:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
keys[i] = inverse(keys[i]);
|
|
|
|
for (i = 6; i < 11; i++)
|
|
|
|
keys[i] = inverse(keys[i]);
|
|
|
|
}
|
|
|
|
|
2013-02-06 00:44:00 +01:00
|
|
|
static uint32_t multiswap_step(const uint32_t keys[12], uint32_t v)
|
|
|
|
{
|
2007-10-20 16:25:02 +02:00
|
|
|
int i;
|
|
|
|
v *= keys[0];
|
|
|
|
for (i = 1; i < 5; i++) {
|
2013-02-06 00:44:00 +01:00
|
|
|
v = (v >> 16) | (v << 16);
|
2007-10-20 16:25:02 +02:00
|
|
|
v *= keys[i];
|
|
|
|
}
|
|
|
|
v += keys[5];
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2013-02-06 00:44:00 +01:00
|
|
|
static uint32_t multiswap_inv_step(const uint32_t keys[12], uint32_t v)
|
|
|
|
{
|
2007-10-20 16:25:02 +02:00
|
|
|
int i;
|
|
|
|
v -= keys[5];
|
|
|
|
for (i = 4; i > 0; i--) {
|
|
|
|
v *= keys[i];
|
2013-02-06 00:44:00 +01:00
|
|
|
v = (v >> 16) | (v << 16);
|
2007-10-20 16:25:02 +02:00
|
|
|
}
|
|
|
|
v *= keys[0];
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-06-23 22:41:54 +02:00
|
|
|
* @brief "MultiSwap" encryption
|
|
|
|
* @param keys 32 bit numbers in machine endianness,
|
2007-10-20 16:25:02 +02:00
|
|
|
* 0-4 and 6-10 must be inverted from decryption
|
2011-06-23 22:41:54 +02:00
|
|
|
* @param key another key, this one must be the same for the decryption
|
|
|
|
* @param data data to encrypt
|
|
|
|
* @return encrypted data
|
2007-10-20 16:25:02 +02:00
|
|
|
*/
|
2013-02-06 00:44:00 +01:00
|
|
|
static uint64_t multiswap_enc(const uint32_t keys[12],
|
|
|
|
uint64_t key, uint64_t data)
|
|
|
|
{
|
2007-10-20 16:25:02 +02:00
|
|
|
uint32_t a = data;
|
|
|
|
uint32_t b = data >> 32;
|
|
|
|
uint32_t c;
|
|
|
|
uint32_t tmp;
|
2013-02-06 00:44:00 +01:00
|
|
|
a += key;
|
|
|
|
tmp = multiswap_step(keys, a);
|
|
|
|
b += tmp;
|
|
|
|
c = (key >> 32) + tmp;
|
2007-10-20 16:25:02 +02:00
|
|
|
tmp = multiswap_step(keys + 6, b);
|
2013-02-06 00:44:00 +01:00
|
|
|
c += tmp;
|
2007-10-20 16:25:02 +02:00
|
|
|
return ((uint64_t)c << 32) | tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-06-23 22:41:54 +02:00
|
|
|
* @brief "MultiSwap" decryption
|
|
|
|
* @param keys 32 bit numbers in machine endianness,
|
2007-10-20 16:25:02 +02:00
|
|
|
* 0-4 and 6-10 must be inverted from encryption
|
2011-06-23 22:41:54 +02:00
|
|
|
* @param key another key, this one must be the same as for the encryption
|
|
|
|
* @param data data to decrypt
|
|
|
|
* @return decrypted data
|
2007-10-20 16:25:02 +02:00
|
|
|
*/
|
2013-02-06 00:44:00 +01:00
|
|
|
static uint64_t multiswap_dec(const uint32_t keys[12],
|
|
|
|
uint64_t key, uint64_t data)
|
|
|
|
{
|
2007-10-20 16:25:02 +02:00
|
|
|
uint32_t a;
|
|
|
|
uint32_t b;
|
2013-02-06 00:44:00 +01:00
|
|
|
uint32_t c = data >> 32;
|
2007-10-20 16:25:02 +02:00
|
|
|
uint32_t tmp = data;
|
2013-02-06 00:44:00 +01:00
|
|
|
c -= tmp;
|
|
|
|
b = multiswap_inv_step(keys + 6, tmp);
|
2007-10-20 16:25:02 +02:00
|
|
|
tmp = c - (key >> 32);
|
2013-02-06 00:44:00 +01:00
|
|
|
b -= tmp;
|
|
|
|
a = multiswap_inv_step(keys, tmp);
|
|
|
|
a -= key;
|
2007-10-20 16:25:02 +02:00
|
|
|
return ((uint64_t)b << 32) | a;
|
|
|
|
}
|
|
|
|
|
2013-02-06 00:44:00 +01:00
|
|
|
void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len)
|
|
|
|
{
|
2009-02-03 15:20:55 +01:00
|
|
|
struct AVDES des;
|
|
|
|
struct AVRC4 rc4;
|
2013-02-06 00:44:00 +01:00
|
|
|
int num_qwords = len >> 3;
|
|
|
|
uint8_t *qwords = data;
|
2011-06-06 14:13:02 +02:00
|
|
|
uint64_t rc4buff[8] = { 0 };
|
2007-10-20 16:25:02 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-02-03 15:20:55 +01:00
|
|
|
av_rc4_init(&rc4, key, 12 * 8, 1);
|
|
|
|
av_rc4_crypt(&rc4, (uint8_t *)rc4buff, NULL, sizeof(rc4buff), NULL, 1);
|
2007-10-20 16:25:02 +02:00
|
|
|
multiswap_init((uint8_t *)rc4buff, ms_keys);
|
|
|
|
|
2013-02-06 00:44:00 +01:00
|
|
|
packetkey = AV_RN64(&qwords[num_qwords * 8 - 8]);
|
2007-10-20 16:25:02 +02:00
|
|
|
packetkey ^= rc4buff[7];
|
2009-02-03 15:20:55 +01:00
|
|
|
av_des_init(&des, key + 12, 64, 1);
|
|
|
|
av_des_crypt(&des, (uint8_t *)&packetkey, (uint8_t *)&packetkey, 1, NULL, 1);
|
2007-10-20 16:25:02 +02:00
|
|
|
packetkey ^= rc4buff[6];
|
|
|
|
|
2009-02-03 15:20:55 +01:00
|
|
|
av_rc4_init(&rc4, (uint8_t *)&packetkey, 64, 1);
|
|
|
|
av_rc4_crypt(&rc4, data, data, len, NULL, 1);
|
2007-10-20 16:25:02 +02:00
|
|
|
|
|
|
|
ms_state = 0;
|
2010-08-24 15:42:28 +02:00
|
|
|
for (i = 0; i < num_qwords - 1; i++, qwords += 8)
|
2007-10-20 16:25:02 +02:00
|
|
|
ms_state = multiswap_enc(ms_keys, ms_state, AV_RL64(qwords));
|
|
|
|
multiswap_invert_keys(ms_keys);
|
|
|
|
packetkey = (packetkey << 32) | (packetkey >> 32);
|
2010-07-11 00:12:30 +02:00
|
|
|
packetkey = av_le2ne64(packetkey);
|
2007-10-20 16:25:02 +02:00
|
|
|
packetkey = multiswap_dec(ms_keys, ms_state, packetkey);
|
|
|
|
AV_WL64(qwords, packetkey);
|
|
|
|
}
|