| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 29 | 544 | AVRC4 *av_rc4_alloc(void) | |
| 30 | { | ||
| 31 | 544 | return av_mallocz(sizeof(struct AVRC4)); | |
| 32 | } | ||
| 33 | |||
| 34 | 1095 | int av_rc4_init(AVRC4 *r, const uint8_t *key, int key_bits, int decrypt) { | |
| 35 | int i, j; | ||
| 36 | uint8_t y; | ||
| 37 | 1095 | uint8_t *state = r->state; | |
| 38 | 1095 | int keylen = key_bits >> 3; | |
| 39 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1094 times.
|
1095 | if (key_bits & 7) |
| 40 | 1 | return AVERROR(EINVAL); | |
| 41 |
2/2✓ Branch 0 taken 280064 times.
✓ Branch 1 taken 1094 times.
|
281158 | for (i = 0; i < 256; i++) |
| 42 | 280064 | state[i] = i; | |
| 43 | 1094 | y = 0; | |
| 44 | // j is i % keylen | ||
| 45 |
2/2✓ Branch 0 taken 280064 times.
✓ Branch 1 taken 1094 times.
|
281158 | for (j = 0, i = 0; i < 256; i++, j++) { |
| 46 |
2/2✓ Branch 0 taken 28533 times.
✓ Branch 1 taken 251531 times.
|
280064 | if (j == keylen) j = 0; |
| 47 | 280064 | y += state[i] + key[j]; | |
| 48 | 280064 | FFSWAP(uint8_t, state[i], state[y]); | |
| 49 | } | ||
| 50 | 1094 | r->x = 1; | |
| 51 | 1094 | r->y = state[1]; | |
| 52 | 1094 | return 0; | |
| 53 | } | ||
| 54 | |||
| 55 | 1094 | void av_rc4_crypt(AVRC4 *r, uint8_t *dst, const uint8_t *src, int count, uint8_t *iv, int decrypt) { | |
| 56 | 1094 | uint8_t x = r->x, y = r->y; | |
| 57 | 1094 | uint8_t *state = r->state; | |
| 58 |
2/2✓ Branch 0 taken 402280 times.
✓ Branch 1 taken 1094 times.
|
403374 | while (count-- > 0) { |
| 59 | 402280 | uint8_t sum = state[x] + state[y]; | |
| 60 | 402280 | FFSWAP(uint8_t, state[x], state[y]); | |
| 61 |
2/2✓ Branch 0 taken 367496 times.
✓ Branch 1 taken 34784 times.
|
402280 | *dst++ = src ? *src++ ^ state[sum] : state[sum]; |
| 62 | 402280 | x++; | |
| 63 | 402280 | y += state[x]; | |
| 64 | } | ||
| 65 | 1094 | r->x = x; r->y = y; | |
| 66 | 1094 | } | |
| 67 |