annotate ffmpeg/libavutil/rc4.c @ 13:844d341cf643 tip

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