FFmpeg
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 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  */
23 
24 #include "error.h"
25 #include "macros.h"
26 #include "mem.h"
27 #include "rc4.h"
28 
30 {
31  return av_mallocz(sizeof(struct AVRC4));
32 }
33 
34 int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt) {
35  int i, j;
36  uint8_t y;
37  uint8_t *state = r->state;
38  int keylen = key_bits >> 3;
39  if (key_bits & 7)
40  return AVERROR(EINVAL);
41  for (i = 0; i < 256; i++)
42  state[i] = i;
43  y = 0;
44  // j is i % keylen
45  for (j = 0, i = 0; i < 256; i++, j++) {
46  if (j == keylen) j = 0;
47  y += state[i] + key[j];
48  FFSWAP(uint8_t, state[i], state[y]);
49  }
50  r->x = 1;
51  r->y = state[1];
52  return 0;
53 }
54 
55 void av_rc4_crypt(AVRC4 *r, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) {
56  uint8_t x = r->x, y = r->y;
57  uint8_t *state = r->state;
58  while (count-- > 0) {
59  uint8_t sum = state[x] + state[y];
60  FFSWAP(uint8_t, state[x], state[y]);
61  *dst++ = src ? *src++ ^ state[sum] : state[sum];
62  x++;
63  y += state[x];
64  }
65  r->x = x; r->y = y;
66 }
r
const char * r
Definition: vf_curves.c:127
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVRC4
Definition: rc4.h:32
state
static struct @464 state
macros.h
key
const char * key
Definition: hwcontext_opencl.c:189
error.h
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:83
av_rc4_alloc
AVRC4 * av_rc4_alloc(void)
Allocate an AVRC4 context.
Definition: rc4.c:29
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_rc4_crypt
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:55
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
av_rc4_init
int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt)
Initializes an AVRC4 context.
Definition: rc4.c:34
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
mem.h
rc4.h
src
#define src
Definition: vp8dsp.c:248