00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "avutil.h"
00024 #include "common.h"
00025 #include "rc4.h"
00026
00027 typedef struct AVRC4 AVRC4;
00028
00029 int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt) {
00030 int i, j;
00031 uint8_t y;
00032 uint8_t *state = r->state;
00033 int keylen = key_bits >> 3;
00034 if (key_bits & 7)
00035 return -1;
00036 for (i = 0; i < 256; i++)
00037 state[i] = i;
00038 y = 0;
00039
00040 for (j = 0, i = 0; i < 256; i++, j++) {
00041 if (j == keylen) j = 0;
00042 y += state[i] + key[j];
00043 FFSWAP(uint8_t, state[i], state[y]);
00044 }
00045 r->x = 1;
00046 r->y = state[1];
00047 return 0;
00048 }
00049
00050 void av_rc4_crypt(AVRC4 *r, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
00051 uint8_t x = r->x, y = r->y;
00052 uint8_t *state = r->state;
00053 while (count-- > 0) {
00054 uint8_t sum = state[x] + state[y];
00055 FFSWAP(uint8_t, state[x], state[y]);
00056 *dst++ = src ? *src++ ^ state[sum] : state[sum];
00057 x++;
00058 y += state[x];
00059 }
00060 r->x = x; r->y = y;
00061 }