Libav
rc4.c
Go to the documentation of this file.
1 /*
2  * RC4 encryption/decryption/pseudo-random number generator
3  * Copyright (c) 2007 Reimar Doeffinger
4  *
5  * loosely based on LibTomCrypt by Tom St Denis
6  *
7  * This file is part of Libav.
8  *
9  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 #include "avutil.h"
24 #include "common.h"
25 #include "mem.h"
26 #include "rc4.h"
27 
28 #if !FF_API_CRYPTO_CONTEXT
29 struct AVRC4 {
30  uint8_t state[256];
31  int x, y;
32 };
33 #endif
34 
36 {
37  return av_mallocz(sizeof(struct AVRC4));
38 }
39 
40 int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt) {
41  int i, j;
42  uint8_t y;
43  uint8_t *state = r->state;
44  int keylen = key_bits >> 3;
45  if (key_bits & 7)
46  return -1;
47  for (i = 0; i < 256; i++)
48  state[i] = i;
49  y = 0;
50  // j is i % keylen
51  for (j = 0, i = 0; i < 256; i++, j++) {
52  if (j == keylen) j = 0;
53  y += state[i] + key[j];
54  FFSWAP(uint8_t, state[i], state[y]);
55  }
56  r->x = 1;
57  r->y = state[1];
58  return 0;
59 }
60 
61 void av_rc4_crypt(AVRC4 *r, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
62  uint8_t x = r->x, y = r->y;
63  uint8_t *state = r->state;
64  while (count-- > 0) {
65  uint8_t sum = state[x] + state[y];
66  FFSWAP(uint8_t, state[x], state[y]);
67  *dst++ = src ? *src++ ^ state[sum] : state[sum];
68  x++;
69  y += state[x];
70  }
71  r->x = x; r->y = y;
72 }
memory handling functions
external API header
int x
Definition: rc4.h:36
uint8_t
int y
Definition: rc4.h:36
Definition: rc4.h:34
void av_rc4_crypt(AVRC4 *r, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt)
Encrypts / decrypts using the RC4 algorithm.
Definition: rc4.c:61
#define r
Definition: input.c:51
#define src
Definition: vp8dsp.c:254
AVRC4 * av_rc4_alloc(void)
Allocate an AVRC4 context.
Definition: rc4.c:35
common internal and external API header
int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt)
Initializes an AVRC4 context.
Definition: rc4.c:40
uint8_t state[256]
Definition: rc4.h:35
#define FFSWAP(type, a, b)
Definition: common.h:69
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211