annotate rc4.c @ 452:4b2cd7e98d58 libavutil

prettyprinting cosmetics
author diego
date Fri, 15 Feb 2008 14:58:18 +0000
parents e6ec944b7ff0
children 67fb0b442dd2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
384
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
1 /*
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
2 * RC4 encryption/decryption/pseudo-random number generator
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
3 * Copyright (c) 2007 Reimar Doeffinger
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
4 *
386
e6ec944b7ff0 Consistently place comments in file header.
diego
parents: 384
diff changeset
5 * loosely based on LibTomCrypt by Tom St Denis
e6ec944b7ff0 Consistently place comments in file header.
diego
parents: 384
diff changeset
6 *
384
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
7 * This file is part of FFmpeg.
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
8 *
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
9 * FFmpeg is free software; you can redistribute it and/or
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
10 * modify it under the terms of the GNU Lesser General Public
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
11 * License as published by the Free Software Foundation; either
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
12 * version 2.1 of the License, or (at your option) any later version.
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
13 *
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
14 * FFmpeg is distributed in the hope that it will be useful,
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
17 * Lesser General Public License for more details.
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
18 *
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
19 * You should have received a copy of the GNU Lesser General Public
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
20 * License along with FFmpeg; if not, write to the Free Software
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
22 */
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
23 #include "common.h"
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
24 #include "rc4.h"
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
25
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
26 void ff_rc4_enc(const uint8_t *key, int keylen, uint8_t *data, int datalen) {
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
27 int i, j;
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
28 uint8_t x, y;
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
29 uint8_t state[256];
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
30 for (i = 0; i < 256; i++)
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
31 state[i] = i;
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
32 y = 0;
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
33 // j is i % keylen
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
34 for (j = 0, i = 0; i < 256; i++, j++) {
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
35 if (j == keylen) j = 0;
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
36 y += state[i] + key[j];
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
37 FFSWAP(uint8_t, state[i], state[y]);
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
38 }
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
39 // state initialized, now do the real encryption
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
40 x = 1; y = state[1];
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
41 while (datalen-- > 0) {
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
42 uint8_t sum = state[x] + state[y];
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
43 FFSWAP(uint8_t, state[x], state[y]);
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
44 *data++ ^= state[sum];
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
45 x++;
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
46 y += state[x];
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
47 }
8d29c19c22c4 Add RC4 encryption/decryption function
reimar
parents:
diff changeset
48 }