Mercurial > libavutil.hg
annotate rc4.c @ 693:ddffe864f8b2 libavutil
Fix ff_random_get_seed() prototype
author | mru |
---|---|
date | Sat, 14 Mar 2009 23:00:57 +0000 |
parents | b084f8cd043f |
children |
rev | line source |
---|---|
384 | 1 /* |
2 * RC4 encryption/decryption/pseudo-random number generator | |
3 * Copyright (c) 2007 Reimar Doeffinger | |
4 * | |
386 | 5 * loosely based on LibTomCrypt by Tom St Denis |
6 * | |
384 | 7 * This file is part of FFmpeg. |
8 * | |
9 * FFmpeg is free software; you can redistribute it and/or | |
10 * modify it under the terms of the GNU Lesser General Public | |
11 * License as published by the Free Software Foundation; either | |
12 * version 2.1 of the License, or (at your option) any later version. | |
13 * | |
14 * FFmpeg is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 * Lesser General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU Lesser General Public | |
20 * License along with FFmpeg; if not, write to the Free Software | |
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 */ | |
647
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
23 #include "avutil.h" |
384 | 24 #include "common.h" |
25 #include "rc4.h" | |
26 | |
647
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
27 typedef struct AVRC4 AVRC4; |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
28 |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
29 int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt) { |
384 | 30 int i, j; |
647
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
31 uint8_t y; |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
32 uint8_t *state = r->state; |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
33 int keylen = key_bits >> 3; |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
34 if (key_bits & 7) |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
35 return -1; |
384 | 36 for (i = 0; i < 256; i++) |
37 state[i] = i; | |
38 y = 0; | |
39 // j is i % keylen | |
40 for (j = 0, i = 0; i < 256; i++, j++) { | |
41 if (j == keylen) j = 0; | |
42 y += state[i] + key[j]; | |
43 FFSWAP(uint8_t, state[i], state[y]); | |
44 } | |
647
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
45 r->x = 1; |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
46 r->y = state[1]; |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
47 return 0; |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
48 } |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
49 |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
50 void av_rc4_crypt(AVRC4 *r, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) { |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
51 uint8_t x = r->x, y = r->y; |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
52 uint8_t *state = r->state; |
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
53 while (count-- > 0) { |
384 | 54 uint8_t sum = state[x] + state[y]; |
55 FFSWAP(uint8_t, state[x], state[y]); | |
647
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
56 *dst++ = src ? *src++ ^ state[sum] : state[sum]; |
384 | 57 x++; |
58 y += state[x]; | |
59 } | |
647
67fb0b442dd2
Add and use a public API for RC4 and DES, analogous to the AES API.
reimar
parents:
386
diff
changeset
|
60 r->x = x; r->y = y; |
384 | 61 } |