1 /* arcfour.c --- The arcfour stream cipher
2 * Copyright (C) 2000-2003, 2005-2006, 2009-2017 Free Software Foundation, Inc.
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2, or (at your
7 * option) any later version.
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this file; if not, see <http://www.gnu.org/licenses/>.
19 /* Code from Libgcrypt adapted for gnulib by Simon Josefsson. */
22 * For a description of the algorithm, see:
23 * Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
24 * ISBN 0-471-11709-9. Pages 397 ff.
32 arcfour_stream (arcfour_context
* context
, const char *inbuf
, char *outbuf
,
35 uint8_t i
= context
->idx_i
;
36 uint8_t j
= context
->idx_j
;
37 char *sbox
= context
->sbox
;
39 for (; length
> 0; length
--)
49 ^ sbox
[(0U + sbox
[i
] + sbox
[j
]) % ARCFOUR_SBOX_SIZE
]);
57 arcfour_setkey (arcfour_context
* context
, const char *key
, size_t keylen
)
60 char *sbox
= context
->sbox
;
62 context
->idx_i
= context
->idx_j
= 0;
63 for (i
= 0; i
< ARCFOUR_SBOX_SIZE
; i
++)
65 for (i
= j
= k
= 0; i
< ARCFOUR_SBOX_SIZE
; i
++)
68 j
= (j
+ sbox
[i
] + key
[k
]) % ARCFOUR_SBOX_SIZE
;