• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavformat/asfcrypt.c

Go to the documentation of this file.
00001 /*
00002  * ASF decryption
00003  * Copyright (c) 2007 Reimar Doeffinger
00004  * This is a rewrite of code contained in freeme/freeme2
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include "libavutil/common.h"
00024 #include "libavutil/intreadwrite.h"
00025 #include "libavutil/bswap.h"
00026 #include "libavutil/des.h"
00027 #include "libavutil/rc4.h"
00028 #include "asfcrypt.h"
00029 
00035 static uint32_t inverse(uint32_t v) {
00036     // v ^ 3 gives the inverse (mod 16), could also be implemented
00037     // as table etc. (only lowest 4 bits matter!)
00038     uint32_t inverse = v * v * v;
00039     // uses a fixpoint-iteration that doubles the number
00040     // of correct lowest bits each time
00041     inverse *= 2 - v * inverse;
00042     inverse *= 2 - v * inverse;
00043     inverse *= 2 - v * inverse;
00044     return inverse;
00045 }
00046 
00053 static void multiswap_init(const uint8_t keybuf[48], uint32_t keys[12]) {
00054     int i;
00055     for (i = 0; i < 12; i++)
00056         keys[i] = AV_RL32(keybuf + (i << 2)) | 1;
00057 }
00058 
00064 static void multiswap_invert_keys(uint32_t keys[12]) {
00065     int i;
00066     for (i = 0; i < 5; i++)
00067         keys[i] = inverse(keys[i]);
00068     for (i = 6; i < 11; i++)
00069         keys[i] = inverse(keys[i]);
00070 }
00071 
00072 static uint32_t multiswap_step(const uint32_t keys[12], uint32_t v) {
00073     int i;
00074     v *= keys[0];
00075     for (i = 1; i < 5; i++) {
00076         v = (v >> 16) | (v << 16);
00077         v *= keys[i];
00078     }
00079     v += keys[5];
00080     return v;
00081 }
00082 
00083 static uint32_t multiswap_inv_step(const uint32_t keys[12], uint32_t v) {
00084     int i;
00085     v -= keys[5];
00086     for (i = 4; i > 0; i--) {
00087         v *= keys[i];
00088         v = (v >> 16) | (v << 16);
00089     }
00090     v *= keys[0];
00091     return v;
00092 }
00093 
00102 static uint64_t multiswap_enc(const uint32_t keys[12], uint64_t key, uint64_t data) {
00103     uint32_t a = data;
00104     uint32_t b = data >> 32;
00105     uint32_t c;
00106     uint32_t tmp;
00107     a += key;
00108     tmp = multiswap_step(keys    , a);
00109     b += tmp;
00110     c = (key >> 32) + tmp;
00111     tmp = multiswap_step(keys + 6, b);
00112     c += tmp;
00113     return ((uint64_t)c << 32) | tmp;
00114 }
00115 
00124 static uint64_t multiswap_dec(const uint32_t keys[12], uint64_t key, uint64_t data) {
00125     uint32_t a;
00126     uint32_t b;
00127     uint32_t c = data >> 32;
00128     uint32_t tmp = data;
00129     c -= tmp;
00130     b = multiswap_inv_step(keys + 6, tmp);
00131     tmp = c - (key >> 32);
00132     b -= tmp;
00133     a = multiswap_inv_step(keys    , tmp);
00134     a -= key;
00135     return ((uint64_t)b << 32) | a;
00136 }
00137 
00138 void ff_asfcrypt_dec(const uint8_t key[20], uint8_t *data, int len) {
00139     struct AVDES des;
00140     struct AVRC4 rc4;
00141     int num_qwords = len >> 3;
00142     uint64_t *qwords = (uint64_t *)data;
00143     uint64_t rc4buff[8];
00144     uint64_t packetkey;
00145     uint32_t ms_keys[12];
00146     uint64_t ms_state;
00147     int i;
00148     if (len < 16) {
00149         for (i = 0; i < len; i++)
00150             data[i] ^= key[i];
00151         return;
00152     }
00153 
00154     memset(rc4buff, 0, sizeof(rc4buff));
00155     av_rc4_init(&rc4, key, 12 * 8, 1);
00156     av_rc4_crypt(&rc4, (uint8_t *)rc4buff, NULL, sizeof(rc4buff), NULL, 1);
00157     multiswap_init((uint8_t *)rc4buff, ms_keys);
00158 
00159     packetkey = qwords[num_qwords - 1];
00160     packetkey ^= rc4buff[7];
00161     av_des_init(&des, key + 12, 64, 1);
00162     av_des_crypt(&des, (uint8_t *)&packetkey, (uint8_t *)&packetkey, 1, NULL, 1);
00163     packetkey ^= rc4buff[6];
00164 
00165     av_rc4_init(&rc4, (uint8_t *)&packetkey, 64, 1);
00166     av_rc4_crypt(&rc4, data, data, len, NULL, 1);
00167 
00168     ms_state = 0;
00169     for (i = 0; i < num_qwords - 1; i++, qwords++)
00170         ms_state = multiswap_enc(ms_keys, ms_state, AV_RL64(qwords));
00171     multiswap_invert_keys(ms_keys);
00172     packetkey = (packetkey << 32) | (packetkey >> 32);
00173     packetkey = le2me_64(packetkey);
00174     packetkey = multiswap_dec(ms_keys, ms_state, packetkey);
00175     AV_WL64(qwords, packetkey);
00176 }

Generated on Fri Sep 16 2011 17:17:47 for FFmpeg by  doxygen 1.7.1