1
/* This file was automatically imported with
2
import_gcry.py. Please don't modify it */
3
/* arcfour.c - The arcfour stream cipher
4
* Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
6
* This file is part of Libgcrypt.
8
* Libgcrypt is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU Lesser general Public License as
10
* published by the Free Software Foundation; either version 2.1 of
11
* the License, or (at your option) any later version.
13
* Libgcrypt is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
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.
39
do_encrypt_stream( ARCFOUR_context *ctx,
40
byte *outbuf, const byte *inbuf, unsigned int length )
42
register int i = ctx->idx_i;
43
register int j = ctx->idx_j;
44
register byte *sbox = ctx->sbox;
50
i = i & 255; /* The and-op seems to be faster than the mod-op. */
53
t = sbox[i]; sbox[i] = sbox[j]; sbox[j] = t;
54
*outbuf++ = *inbuf++ ^ sbox[(sbox[i] + sbox[j]) & 255];
62
encrypt_stream (void *context,
63
byte *outbuf, const byte *inbuf, unsigned int length)
65
ARCFOUR_context *ctx = (ARCFOUR_context *) context;
66
do_encrypt_stream (ctx, outbuf, inbuf, length );
67
_gcry_burn_stack (64);
71
static gcry_err_code_t
72
do_arcfour_setkey (void *context, const byte *key, unsigned int keylen)
74
static int initialized;
75
static const char* selftest_failed;
78
ARCFOUR_context *ctx = (ARCFOUR_context *) context;
83
selftest_failed = selftest();
85
log_error ("ARCFOUR selftest failed (%s)\n", selftest_failed );
88
return GPG_ERR_SELFTEST_FAILED;
90
if( keylen < 40/8 ) /* we want at least 40 bits */
91
return GPG_ERR_INV_KEYLEN;
93
ctx->idx_i = ctx->idx_j = 0;
94
for (i=0; i < 256; i++ )
96
for (i=0; i < 256; i++ )
97
karr[i] = key[i%keylen];
98
for (i=j=0; i < 256; i++ )
101
j = (j + ctx->sbox[i] + karr[i]) % 256;
103
ctx->sbox[i] = ctx->sbox[j];
106
memset( karr, 0, 256 );
108
return GPG_ERR_NO_ERROR;
111
static gcry_err_code_t
112
arcfour_setkey ( void *context, const byte *key, unsigned int keylen )
114
ARCFOUR_context *ctx = (ARCFOUR_context *) context;
115
gcry_err_code_t rc = do_arcfour_setkey (ctx, key, keylen );
116
_gcry_burn_stack (300);
123
gcry_cipher_spec_t _gcry_cipher_spec_arcfour =
125
"ARCFOUR", NULL, NULL, 1, 128, sizeof (ARCFOUR_context),
126
arcfour_setkey, NULL, NULL, encrypt_stream, encrypt_stream,
131
GRUB_MOD_INIT(gcry_arcfour)
133
grub_cipher_register (&_gcry_cipher_spec_arcfour);
136
GRUB_MOD_FINI(gcry_arcfour)
138
grub_cipher_unregister (&_gcry_cipher_spec_arcfour);