Mercurial > gnulib
annotate lib/arcfour.c @ 40057:b06060465f09
maint: Run 'make update-copyright'
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Tue, 01 Jan 2019 00:25:11 +0100 |
parents | 10eb9086bea0 |
children |
rev | line source |
---|---|
6377 | 1 /* arcfour.c --- The arcfour stream cipher |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
2 * Copyright (C) 2000-2003, 2005-2006, 2009-2019 Free Software Foundation, Inc. |
6377 | 3 * |
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. | |
8 * | |
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. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
19190 | 15 * along with this file; if not, see <https://www.gnu.org/licenses/>. |
6377 | 16 * |
17 */ | |
18 | |
19 /* Code from Libgcrypt adapted for gnulib by Simon Josefsson. */ | |
20 | |
21 /* | |
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. | |
25 */ | |
26 | |
7584
a88f85e4728f
* lib/arcfour.c: Assume config.h.
Eric Blake <ebb9@byu.net>
parents:
6423
diff
changeset
|
27 #include <config.h> |
6377 | 28 |
29 #include "arcfour.h" | |
30 | |
31 void | |
32 arcfour_stream (arcfour_context * context, const char *inbuf, char *outbuf, | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
33 size_t length) |
6377 | 34 { |
6423 | 35 uint8_t i = context->idx_i; |
36 uint8_t j = context->idx_j; | |
6377 | 37 char *sbox = context->sbox; |
38 | |
39 for (; length > 0; length--) | |
40 { | |
41 char t; | |
42 | |
6423 | 43 i++; |
44 j += sbox[i]; | |
6377 | 45 t = sbox[i]; |
46 sbox[i] = sbox[j]; | |
47 sbox[j] = t; | |
48 *outbuf++ = (*inbuf++ | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
49 ^ sbox[(0U + sbox[i] + sbox[j]) % ARCFOUR_SBOX_SIZE]); |
6377 | 50 } |
51 | |
52 context->idx_i = i; | |
53 context->idx_j = j; | |
54 } | |
55 | |
56 void | |
57 arcfour_setkey (arcfour_context * context, const char *key, size_t keylen) | |
58 { | |
59 size_t i, j, k; | |
60 char *sbox = context->sbox; | |
61 | |
62 context->idx_i = context->idx_j = 0; | |
63 for (i = 0; i < ARCFOUR_SBOX_SIZE; i++) | |
64 sbox[i] = i; | |
65 for (i = j = k = 0; i < ARCFOUR_SBOX_SIZE; i++) | |
66 { | |
67 char t; | |
68 j = (j + sbox[i] + key[k]) % ARCFOUR_SBOX_SIZE; | |
69 t = sbox[i]; | |
70 sbox[i] = sbox[j]; | |
71 sbox[j] = t; | |
72 if (++k == keylen) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
7584
diff
changeset
|
73 k = 0; |
6377 | 74 } |
75 } |