mirror of
https://github.com/xenia-project/FFmpeg.git
synced 2024-11-23 19:49:56 +00:00
Add and use a public API for RC4 and DES, analogous to the AES API.
Originally committed as revision 16970 to svn://svn.ffmpeg.org/ffmpeg/trunk
This commit is contained in:
parent
88297e80aa
commit
1a534c7f1e
@ -136,6 +136,8 @@ static uint64_t multiswap_dec(const uint32_t keys[12], uint64_t key, uint64_t da
|
||||
}
|
||||
|
||||
void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len) {
|
||||
struct AVDES des;
|
||||
struct AVRC4 rc4;
|
||||
int num_qwords = len >> 3;
|
||||
uint64_t *qwords = (uint64_t *)data;
|
||||
uint64_t rc4buff[8];
|
||||
@ -150,17 +152,18 @@ void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len) {
|
||||
}
|
||||
|
||||
memset(rc4buff, 0, sizeof(rc4buff));
|
||||
ff_rc4_enc(key, 12, (uint8_t *)rc4buff, sizeof(rc4buff));
|
||||
av_rc4_init(&rc4, key, 12 * 8, 1);
|
||||
av_rc4_crypt(&rc4, (uint8_t *)rc4buff, NULL, sizeof(rc4buff), NULL, 1);
|
||||
multiswap_init((uint8_t *)rc4buff, ms_keys);
|
||||
|
||||
packetkey = qwords[num_qwords - 1];
|
||||
packetkey ^= rc4buff[7];
|
||||
packetkey = be2me_64(packetkey);
|
||||
packetkey = ff_des_encdec(packetkey, AV_RB64(key + 12), 1);
|
||||
packetkey = be2me_64(packetkey);
|
||||
av_des_init(&des, key + 12, 64, 1);
|
||||
av_des_crypt(&des, (uint8_t *)&packetkey, (uint8_t *)&packetkey, 1, NULL, 1);
|
||||
packetkey ^= rc4buff[6];
|
||||
|
||||
ff_rc4_enc((uint8_t *)&packetkey, 8, data, len);
|
||||
av_rc4_init(&rc4, (uint8_t *)&packetkey, 64, 1);
|
||||
av_rc4_crypt(&rc4, data, data, len, NULL, 1);
|
||||
|
||||
ms_state = 0;
|
||||
for (i = 0; i < num_qwords - 1; i++, qwords++)
|
||||
|
@ -19,9 +19,13 @@
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#include <inttypes.h>
|
||||
#include "avutil.h"
|
||||
#include "common.h"
|
||||
#include "intreadwrite.h"
|
||||
#include "des.h"
|
||||
|
||||
typedef struct AVDES AVDES;
|
||||
|
||||
#define T(a, b, c, d, e, f, g, h) 64-a,64-b,64-c,64-d,64-e,64-f,64-g,64-h
|
||||
static const uint8_t IP_shuffle[] = {
|
||||
T(58, 50, 42, 34, 26, 18, 10, 2),
|
||||
@ -249,9 +253,8 @@ static uint64_t key_shift_left(uint64_t CDn) {
|
||||
return CDn;
|
||||
}
|
||||
|
||||
uint64_t ff_des_encdec(uint64_t in, uint64_t key, int decrypt) {
|
||||
static void gen_roundkeys(uint64_t K[16], uint64_t key) {
|
||||
int i;
|
||||
uint64_t K[16];
|
||||
// discard parity bits from key and shuffle it into C and D parts
|
||||
uint64_t CDn = shuffle(key, PC1_shuffle, sizeof(PC1_shuffle));
|
||||
// generate round keys
|
||||
@ -261,6 +264,10 @@ uint64_t ff_des_encdec(uint64_t in, uint64_t key, int decrypt) {
|
||||
CDn = key_shift_left(CDn);
|
||||
K[i] = shuffle(CDn, PC2_shuffle, sizeof(PC2_shuffle));
|
||||
}
|
||||
}
|
||||
|
||||
static uint64_t des_encdec(uint64_t in, uint64_t K[16], int decrypt) {
|
||||
int i;
|
||||
// used to apply round keys in reverse order for decryption
|
||||
decrypt = decrypt ? 15 : 0;
|
||||
// shuffle irrelevant to security but to ease hardware implementations
|
||||
@ -277,6 +284,42 @@ uint64_t ff_des_encdec(uint64_t in, uint64_t key, int decrypt) {
|
||||
return in;
|
||||
}
|
||||
|
||||
#if LIBAVUTIL_VERSION_MAJOR < 50
|
||||
uint64_t ff_des_encdec(uint64_t in, uint64_t key, int decrypt) {
|
||||
uint64_t K[16];
|
||||
gen_roundkeys(K, key);
|
||||
return des_encdec(in, K, decrypt);
|
||||
}
|
||||
#endif
|
||||
|
||||
int av_des_init(AVDES *d, const uint8_t *key, int key_bits, int decrypt) {
|
||||
if (key_bits != 64)
|
||||
return -1;
|
||||
d->triple_des = 0;
|
||||
gen_roundkeys(d->round_keys[0], AV_RB64(key));
|
||||
return 0;
|
||||
}
|
||||
|
||||
void av_des_crypt(AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
|
||||
uint64_t iv_val = iv ? be2me_64(*(uint64_t *)iv) : 0;
|
||||
while (count-- > 0) {
|
||||
uint64_t dst_val;
|
||||
uint64_t src_val = src ? be2me_64(*(const uint64_t *)src) : 0;
|
||||
if (decrypt) {
|
||||
dst_val = des_encdec(src_val, d->round_keys[0], 1) ^ iv_val;
|
||||
iv_val = iv ? src_val : 0;
|
||||
} else {
|
||||
dst_val = des_encdec(src_val ^ iv_val, d->round_keys[0], 0);
|
||||
iv_val = iv ? dst_val : 0;
|
||||
}
|
||||
*(uint64_t *)dst = be2me_64(dst_val);
|
||||
src += 8;
|
||||
dst += 8;
|
||||
}
|
||||
if (iv)
|
||||
*(uint64_t *)iv = be2me_64(iv_val);
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
#undef printf
|
||||
#undef rand
|
||||
|
@ -23,18 +23,30 @@
|
||||
#define AVUTIL_DES_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "common.h"
|
||||
|
||||
struct AVDES {
|
||||
uint64_t round_keys[3][16];
|
||||
int triple_des;
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Encrypt/decrypt a 64-bit block of data with DES.
|
||||
* \param in data to process
|
||||
* \param key key to use for encryption/decryption
|
||||
* \param decrypt if 0 encrypt, else decrypt
|
||||
* \return processed data
|
||||
* \brief Initializes an AVDES context.
|
||||
*
|
||||
* If your input data is in 8-bit blocks, treat it as big-endian
|
||||
* (use e.g. AV_RB64 and AV_WB64).
|
||||
* \param key_bits must be 64
|
||||
* \param decrypt 0 for encryption, 1 for decryption
|
||||
*/
|
||||
uint64_t ff_des_encdec(uint64_t in, uint64_t key, int decrypt) av_const;
|
||||
int av_des_init(struct AVDES *d, const uint8_t *key, int key_bits, int decrypt);
|
||||
|
||||
/**
|
||||
* \brief Encrypts / decrypts using the DES algorithm.
|
||||
*
|
||||
* \param count number of 8 byte blocks
|
||||
* \param dst destination array, can be equal to src, must be 8-byte aligned
|
||||
* \param src source array, can be equal to dst, must be 8-byte aligned, may be NULL
|
||||
* \param iv initialization vector for CBC mode, if NULL then ECB will be used,
|
||||
* must be 8-byte aligned
|
||||
* \param decrypt 0 for encryption, 1 for decryption
|
||||
*/
|
||||
void av_des_crypt(struct AVDES *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt);
|
||||
|
||||
#endif /* AVUTIL_DES_H */
|
||||
|
@ -20,13 +20,19 @@
|
||||
* License along with FFmpeg; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#include "avutil.h"
|
||||
#include "common.h"
|
||||
#include "rc4.h"
|
||||
|
||||
void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen) {
|
||||
typedef struct AVRC4 AVRC4;
|
||||
|
||||
int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt) {
|
||||
int i, j;
|
||||
uint8_t x, y;
|
||||
uint8_t state[256];
|
||||
uint8_t y;
|
||||
uint8_t *state = r->state;
|
||||
int keylen = key_bits >> 3;
|
||||
if (key_bits & 7)
|
||||
return -1;
|
||||
for (i = 0; i < 256; i++)
|
||||
state[i] = i;
|
||||
y = 0;
|
||||
@ -36,13 +42,28 @@ void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen) {
|
||||
y += state[i] + key[j];
|
||||
FFSWAP(uint8_t, state[i], state[y]);
|
||||
}
|
||||
// state initialized, now do the real encryption
|
||||
x = 1; y = state[1];
|
||||
while (datalen-- > 0) {
|
||||
r->x = 1;
|
||||
r->y = state[1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
void av_rc4_crypt(AVRC4 *r, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
|
||||
uint8_t x = r->x, y = r->y;
|
||||
uint8_t *state = r->state;
|
||||
while (count-- > 0) {
|
||||
uint8_t sum = state[x] + state[y];
|
||||
FFSWAP(uint8_t, state[x], state[y]);
|
||||
*data++ ^= state[sum];
|
||||
*dst++ = src ? *src++ ^ state[sum] : state[sum];
|
||||
x++;
|
||||
y += state[x];
|
||||
}
|
||||
r->x = x; r->y = y;
|
||||
}
|
||||
|
||||
#if LIBAVUTIL_VERSION_MAJOR < 50
|
||||
void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen) {
|
||||
AVRC4 r;
|
||||
av_rc4_init(&r, key, keylen * 8, 0);
|
||||
av_rc4_crypt(&r, data, data, datalen, NULL, 0);
|
||||
}
|
||||
#endif
|
||||
|
@ -23,6 +23,28 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen);
|
||||
struct AVRC4 {
|
||||
uint8_t state[256];
|
||||
int x, y;
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Initializes an AVRC4 context.
|
||||
*
|
||||
* \param key_bits must be a multiple of 8
|
||||
* \param decrypt 0 for encryption, 1 for decryption, currently has no effect
|
||||
*/
|
||||
int av_rc4_init(struct AVRC4 *d, const uint8_t *key, int key_bits, int decrypt);
|
||||
|
||||
/**
|
||||
* \brief Encrypts / decrypts using the RC4 algorithm.
|
||||
*
|
||||
* \param count number of bytes
|
||||
* \param dst destination array, can be equal to src
|
||||
* \param src source array, can be equal to dst, may be NULL
|
||||
* \param iv not (yet) used for RC4, should be NULL
|
||||
* \param decrypt 0 for encryption, 1 for decryption, not (yet) used
|
||||
*/
|
||||
void av_rc4_crypt(struct AVRC4 *d, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt);
|
||||
|
||||
#endif /* AVUTIL_RC4_H */
|
||||
|
Loading…
Reference in New Issue
Block a user