1
/* arcfour.c - The arcfour stream cipher
2
* Copyright (C) 2000 Free Software Foundation, Inc.
4
* For a description of the algorithm, see:
5
* Bruce Schneier: Applied Cryptography. John Wiley & Sons, 1996.
6
* ISBN 0-471-11709-9. Pages 397 ff.
8
* This file is part of GnuPG.
10
* GnuPG is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2 of the License, or
13
* (at your option) any later version.
15
* GnuPG is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
34
static const char *selftest(void);
44
encrypt_stream( ARCFOUR_context *ctx,
45
byte *outbuf, const byte *inbuf, unsigned int length )
50
byte *sbox = ctx->sbox;
54
j = (j + sbox[i]) % 256;
55
t = sbox[i]; sbox[i] = sbox[j]; sbox[j] = t;
56
*outbuf++ = *inbuf++ ^ sbox[(sbox[i] + sbox[j]) % 256];
65
arcfour_setkey( ARCFOUR_context *ctx, const byte *key, unsigned int keylen )
67
static int initialized;
68
static const char* selftest_failed;
74
selftest_failed = selftest();
76
fprintf(stderr,"ARCFOUR selftest failed (%s)\n", selftest_failed );
79
return GCRYERR_SELFTEST;
82
return GCRYERR_INV_KEYLEN;
84
ctx->idx_i = ctx->idx_j = 0;
85
for (i=0; i < 256; i++ )
87
for (i=0; i < 256; i++ )
88
karr[i] = key[i%keylen];
89
for (i=j=0; i < 256; i++ ) {
91
j = (j + ctx->sbox[i] + karr[i]) % 256;
93
ctx->sbox[i] = ctx->sbox[j];
96
memset( karr, 0, 256 );
108
/* Test vector from Cryptlib labeled there:
109
* "from the State/Commerce Department" */
110
static const byte key_1[] =
111
{ 0x61, 0x8A, 0x63, 0xD2, 0xFB };
112
static const byte plaintext_1[] =
113
{ 0xDC, 0xEE, 0x4C, 0xF9, 0x2C };
114
static const byte ciphertext_1[] =
115
{ 0xF1, 0x38, 0x29, 0xC9, 0xDE };
117
arcfour_setkey( &ctx, key_1, sizeof(key_1));
118
encrypt_stream( &ctx, scratch, plaintext_1, sizeof(plaintext_1));
119
if (memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
120
return "Arcfour encryption test 1 failed.";
121
encrypt_stream(&ctx, scratch, scratch, sizeof(plaintext_1)); /* decrypt */
122
if ( memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
123
return "Arcfour decryption test 1 failed.";
129
* Return some information about the algorithm. We need algo here to
130
* distinguish different flavors of the algorithm.
131
* Returns: A pointer to string describing the algorithm or NULL if
132
* the ALGO is invalid.
133
* NOTE: This is a special get_info function which is different from all
134
* others because arcfour is a stream cipher. We use this hack until
135
* we have redesign the interface.
138
arcfour_get_info( int algo, size_t *keylen, size_t *blocksize,
140
int (**r_setkey)( void *c, byte *key, unsigned keylen ),
141
void (**r_stencrypt)( void *c, byte *outbuf,
142
byte *inbuf, unsigned int nbytes ),
143
void (**r_stdecrypt)( void *c, byte *outbuf,
144
byte *inbuf, unsigned int nbytes )
147
*keylen = 128; /* arbitrary value */
149
*contextsize = sizeof(ARCFOUR_context);
150
*(int (**)(ARCFOUR_context*, byte*, unsigned))r_setkey
152
*(void (**)(ARCFOUR_context*, byte*, const byte*, unsigned))r_stencrypt
154
*(void (**)(ARCFOUR_context*, byte*, const byte*, unsigned))r_stdecrypt