| 1 | /* arcfour.c --- The arcfour stream cipher |
| 2 | * Copyright (C) 2000, 2001, 2002, 2003, 2005, 2006 Free Software |
| 3 | * Foundation, Inc. |
| 4 | * |
| 5 | * This file is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published |
| 7 | * by the Free Software Foundation; either version 2, or (at your |
| 8 | * option) any later version. |
| 9 | * |
| 10 | * This file is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this file; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 18 | * 02110-1301, USA. |
| 19 | * |
| 20 | */ |
| 21 | /** @file lib/arcfour.c |
| 22 | * @brief Arcfour (RC4-compatible) stream cipher implementation |
| 23 | * |
| 24 | * Code from Libgcrypt adapted for gnulib by Simon Josefsson. |
| 25 | * |
| 26 | * For a description of the algorithm, see: |
| 27 | * |
| 28 | * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996. |
| 29 | * ISBN 0-471-11709-9. Pages 397 ff. |
| 30 | */ |
| 31 | |
| 32 | #include "arcfour.h" |
| 33 | |
| 34 | /** @brief Encrypt using Arcfour stream cipher |
| 35 | * @param context Context structure |
| 36 | * @param inbuf Input buffer |
| 37 | * @param outbuf Output buffer |
| 38 | * @param length Number of bytes in @p inbuf |
| 39 | * |
| 40 | * Copies from @p inbuf to @p outbuf, encrypting (or decrypting) using |
| 41 | * the stream controlled by @p context. |
| 42 | */ |
| 43 | void |
| 44 | arcfour_stream (arcfour_context * context, const char *inbuf, char *outbuf, |
| 45 | size_t length) |
| 46 | { |
| 47 | uint8_t i = context->idx_i; |
| 48 | uint8_t j = context->idx_j; |
| 49 | char *sbox = context->sbox; |
| 50 | |
| 51 | for (; length > 0; length--) |
| 52 | { |
| 53 | char t; |
| 54 | |
| 55 | i++; |
| 56 | j += sbox[i]; |
| 57 | t = sbox[i]; |
| 58 | sbox[i] = sbox[j]; |
| 59 | sbox[j] = t; |
| 60 | *outbuf++ = (*inbuf++ |
| 61 | ^ sbox[(0U + sbox[i] + sbox[j]) % ARCFOUR_SBOX_SIZE]); |
| 62 | } |
| 63 | |
| 64 | context->idx_i = i; |
| 65 | context->idx_j = j; |
| 66 | } |
| 67 | |
| 68 | /** @brief Initialize an @ref arcfour_context |
| 69 | * @param context Context structure |
| 70 | * @param key Key data |
| 71 | * @param keylen Length of key |
| 72 | * |
| 73 | * Initializes @p context using @p key. |
| 74 | */ |
| 75 | void |
| 76 | arcfour_setkey (arcfour_context * context, const char *key, size_t keylen) |
| 77 | { |
| 78 | size_t i, j, k; |
| 79 | char *sbox = context->sbox; |
| 80 | |
| 81 | context->idx_i = context->idx_j = 0; |
| 82 | for (i = 0; i < ARCFOUR_SBOX_SIZE; i++) |
| 83 | sbox[i] = i; |
| 84 | for (i = j = k = 0; i < ARCFOUR_SBOX_SIZE; i++) |
| 85 | { |
| 86 | char t; |
| 87 | j = (j + sbox[i] + key[k]) % ARCFOUR_SBOX_SIZE; |
| 88 | t = sbox[i]; |
| 89 | sbox[i] = sbox[j]; |
| 90 | sbox[j] = t; |
| 91 | if (++k == keylen) |
| 92 | k = 0; |
| 93 | } |
| 94 | } |