2 * The ARC4 stream cipher.
4 * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 FILE_LICENCE ( GPL2_OR_LATER
);
23 #include <gpxe/crypto.h>
24 #include <gpxe/arc4.h>
26 #define SWAP( ary, i, j ) \
27 ({ u8 temp = ary[i]; ary[i] = ary[j]; ary[j] = temp; })
32 * @v ctxv ARC4 encryption context
34 * @v keylen Length of key
36 * If an initialisation vector is to be used, it should be prepended
37 * to the key; ARC4 does not implement the @c setiv function because
38 * there is no standard length for an initialisation vector in the
41 static int arc4_setkey ( void *ctxv
, const void *keyv
, size_t keylen
)
43 struct arc4_ctx
*ctx
= ctxv
;
48 for ( i
= 0; i
< 256; i
++ ) {
52 for ( i
= j
= 0; i
< 256; i
++ ) {
53 j
= ( j
+ S
[i
] + key
[i
% keylen
] ) & 0xff;
62 * Perform ARC4 encryption or decryption
64 * @v ctxv ARC4 encryption context
65 * @v srcv Data to encrypt or decrypt
66 * @v dstv Location to store encrypted or decrypted data
67 * @v len Length of data to operate on
69 * ARC4 is a stream cipher that works by generating a stream of PRNG
70 * data based on the key, and XOR'ing it with the data to be
71 * encrypted. Since XOR is symmetric, encryption and decryption in
72 * ARC4 are the same operation.
74 * If you pass a @c NULL source or destination pointer, @a len
75 * keystream bytes will be consumed without encrypting any data.
77 static void arc4_xor ( void *ctxv
, const void *srcv
, void *dstv
,
80 struct arc4_ctx
*ctx
= ctxv
;
84 int i
= ctx
->i
, j
= ctx
->j
;
88 j
= ( j
+ S
[i
] ) & 0xff;
91 *dst
++ = *src
++ ^ S
[(S
[i
] + S
[j
]) & 0xff];
98 static void arc4_setiv ( void *ctx __unused
, const void *iv __unused
)
100 /* ARC4 does not use a fixed-length IV */
105 * Perform ARC4 encryption or decryption, skipping initial keystream bytes
107 * @v key ARC4 encryption key
108 * @v keylen Key length
109 * @v skip Number of bytes of keystream to skip
110 * @v src Message to encrypt or decrypt
111 * @v msglen Length of message
112 * @ret dst Encrypted or decrypted message
114 void arc4_skip ( const void *key
, size_t keylen
, size_t skip
,
115 const void *src
, void *dst
, size_t msglen
)
118 arc4_setkey ( &ctx
, key
, keylen
);
119 arc4_xor ( &ctx
, NULL
, NULL
, skip
);
120 arc4_xor ( &ctx
, src
, dst
, msglen
);
123 struct cipher_algorithm arc4_algorithm
= {
125 .ctxsize
= ARC4_CTX_SIZE
,
127 .setkey
= arc4_setkey
,