~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavformat/asfcrypt.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc, Andrew Starr-Bochicchio, Lionel Le Folgoc
  • Date: 2008-12-26 00:10:06 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081226001006-2040ls9680bd1blt
Tags: 1.1.7-0.2ubuntu1
[ Andrew Starr-Bochicchio ]
* Merge from debian-multimedia (LP: #298547), Ubuntu Changes:
 - For ffmpeg-related build-deps, fix versionized dependencies
   as the ubuntu versioning is different than debian-multimedia's.

[ Lionel Le Folgoc ]
* LP: #311412 is fixed since the 1.1.7~rc1-0.1 revision.
* debian/patches/03_ffmpeg.diff: updated to fix FTBFS due to libswscale API
  change (cherry-pick from Gentoo #234383).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include "common.h"
 
23
#include "intreadwrite.h"
 
24
#include "bswap.h"
 
25
#include "des.h"
 
26
#include "rc4.h"
 
27
#include "asfcrypt.h"
 
28
 
 
29
/**
 
30
 * \brief find multiplicative inverse modulo 2 ^ 32
 
31
 * \param v number to invert, must be odd!
 
32
 * \return number so that result * v = 1 (mod 2^32)
 
33
 */
 
34
static uint32_t inverse(uint32_t v) {
 
35
    // v ^ 3 gives the inverse (mod 16), could also be implemented
 
36
    // as table etc. (only lowest 4 bits matter!)
 
37
    uint32_t inverse = v * v * v;
 
38
    // uses a fixpoint-iteration that doubles the number
 
39
    // of correct lowest bits each time
 
40
    inverse *= 2 - v * inverse;
 
41
    inverse *= 2 - v * inverse;
 
42
    inverse *= 2 - v * inverse;
 
43
    return inverse;
 
44
}
 
45
 
 
46
/**
 
47
 * \brief read keys from keybuf into keys
 
48
 * \param keybuf buffer containing the keys
 
49
 * \param keys output key array containing the keys for encryption in
 
50
 *             native endianness
 
51
 */
 
52
static void multiswap_init(const uint8_t keybuf[48], uint32_t keys[12]) {
 
53
    int i;
 
54
    for (i = 0; i < 12; i++)
 
55
        keys[i] = AV_RL32(keybuf + (i << 2)) | 1;
 
56
}
 
57
 
 
58
/**
 
59
 * \brief invert the keys so that encryption become decryption keys and
 
60
 *        the other way round.
 
61
 * \param keys key array of ints to invert
 
62
 */
 
63
static void multiswap_invert_keys(uint32_t keys[12]) {
 
64
    int i;
 
65
    for (i = 0; i < 5; i++)
 
66
        keys[i] = inverse(keys[i]);
 
67
    for (i = 6; i < 11; i++)
 
68
        keys[i] = inverse(keys[i]);
 
69
}
 
70
 
 
71
static uint32_t multiswap_step(const uint32_t keys[12], uint32_t v) {
 
72
    int i;
 
73
    v *= keys[0];
 
74
    for (i = 1; i < 5; i++) {
 
75
        v = (v >> 16) | (v << 16);
 
76
        v *= keys[i];
 
77
    }
 
78
    v += keys[5];
 
79
    return v;
 
80
}
 
81
 
 
82
static uint32_t multiswap_inv_step(const uint32_t keys[12], uint32_t v) {
 
83
    int i;
 
84
    v -= keys[5];
 
85
    for (i = 4; i > 0; i--) {
 
86
        v *= keys[i];
 
87
        v = (v >> 16) | (v << 16);
 
88
    }
 
89
    v *= keys[0];
 
90
    return v;
 
91
}
 
92
 
 
93
/**
 
94
 * \brief "MultiSwap" encryption
 
95
 * \param keys 32 bit numbers in machine endianness,
 
96
 *             0-4 and 6-10 must be inverted from decryption
 
97
 * \param key another key, this one must be the same for the decryption
 
98
 * \param data data to encrypt
 
99
 * \return encrypted data
 
100
 */
 
101
static uint64_t multiswap_enc(const uint32_t keys[12], uint64_t key, uint64_t data) {
 
102
    uint32_t a = data;
 
103
    uint32_t b = data >> 32;
 
104
    uint32_t c;
 
105
    uint32_t tmp;
 
106
    a += key;
 
107
    tmp = multiswap_step(keys    , a);
 
108
    b += tmp;
 
109
    c = (key >> 32) + tmp;
 
110
    tmp = multiswap_step(keys + 6, b);
 
111
    c += tmp;
 
112
    return ((uint64_t)c << 32) | tmp;
 
113
}
 
114
 
 
115
/**
 
116
 * \brief "MultiSwap" decryption
 
117
 * \param keys 32 bit numbers in machine endianness,
 
118
 *             0-4 and 6-10 must be inverted from encryption
 
119
 * \param key another key, this one must be the same as for the encryption
 
120
 * \param data data to decrypt
 
121
 * \return decrypted data
 
122
 */
 
123
static uint64_t multiswap_dec(const uint32_t keys[12], uint64_t key, uint64_t data) {
 
124
    uint32_t a;
 
125
    uint32_t b;
 
126
    uint32_t c = data >> 32;
 
127
    uint32_t tmp = data;
 
128
    c -= tmp;
 
129
    b = multiswap_inv_step(keys + 6, tmp);
 
130
    tmp = c - (key >> 32);
 
131
    b -= tmp;
 
132
    a = multiswap_inv_step(keys    , tmp);
 
133
    a -= key;
 
134
    return ((uint64_t)b << 32) | a;
 
135
}
 
136
 
 
137
void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len) {
 
138
    int num_qwords = len >> 3;
 
139
    uint64_t *qwords = (uint64_t *)data;
 
140
    uint64_t rc4buff[8];
 
141
    uint64_t packetkey;
 
142
    uint32_t ms_keys[12];
 
143
    uint64_t ms_state;
 
144
    int i;
 
145
    if (len < 16) {
 
146
        for (i = 0; i < len; i++)
 
147
            data[i] ^= key[i];
 
148
        return;
 
149
    }
 
150
 
 
151
    memset(rc4buff, 0, sizeof(rc4buff));
 
152
    ff_rc4_enc(key, 12, (uint8_t *)rc4buff, sizeof(rc4buff));
 
153
    multiswap_init((uint8_t *)rc4buff, ms_keys);
 
154
 
 
155
    packetkey = qwords[num_qwords - 1];
 
156
    packetkey ^= rc4buff[7];
 
157
    packetkey = be2me_64(packetkey);
 
158
    packetkey = ff_des_encdec(packetkey, AV_RB64(key + 12), 1);
 
159
    packetkey = be2me_64(packetkey);
 
160
    packetkey ^= rc4buff[6];
 
161
 
 
162
    ff_rc4_enc((uint8_t *)&packetkey, 8, data, len);
 
163
 
 
164
    ms_state = 0;
 
165
    for (i = 0; i < num_qwords - 1; i++, qwords++)
 
166
        ms_state = multiswap_enc(ms_keys, ms_state, AV_RL64(qwords));
 
167
    multiswap_invert_keys(ms_keys);
 
168
    packetkey = (packetkey << 32) | (packetkey >> 32);
 
169
    packetkey = le2me_64(packetkey);
 
170
    packetkey = multiswap_dec(ms_keys, ms_state, packetkey);
 
171
    AV_WL64(qwords, packetkey);
 
172
}