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., 51 Franklin Street, Fifth Floor, Boston, MA
22 FILE_LICENCE ( GPL2_OR_LATER );
24 #include <ipxe/crypto.h>
25 #include <ipxe/arc4.h>
27 #define SWAP( ary, i, j ) \
28 ({ u8 temp = ary[i]; ary[i] = ary[j]; ary[j] = temp; })
33 * @v ctxv ARC4 encryption context
35 * @v keylen Length of key
37 * If an initialisation vector is to be used, it should be prepended
38 * to the key; ARC4 does not implement the @c setiv function because
39 * there is no standard length for an initialisation vector in the
42 static int arc4_setkey ( void *ctxv, const void *keyv, size_t keylen )
44 struct arc4_ctx *ctx = ctxv;
49 for ( i = 0; i < 256; i++ ) {
53 for ( i = j = 0; i < 256; i++ ) {
54 j = ( j + S[i] + key[i % keylen] ) & 0xff;
63 * Perform ARC4 encryption or decryption
65 * @v ctxv ARC4 encryption context
66 * @v srcv Data to encrypt or decrypt
67 * @v dstv Location to store encrypted or decrypted data
68 * @v len Length of data to operate on
70 * ARC4 is a stream cipher that works by generating a stream of PRNG
71 * data based on the key, and XOR'ing it with the data to be
72 * encrypted. Since XOR is symmetric, encryption and decryption in
73 * ARC4 are the same operation.
75 * If you pass a @c NULL source or destination pointer, @a len
76 * keystream bytes will be consumed without encrypting any data.
78 static void arc4_xor ( void *ctxv, const void *srcv, void *dstv,
81 struct arc4_ctx *ctx = ctxv;
85 int i = ctx->i, j = ctx->j;
89 j = ( j + S[i] ) & 0xff;
92 *dst++ = *src++ ^ S[(S[i] + S[j]) & 0xff];
99 static void arc4_setiv ( void *ctx __unused, const void *iv __unused )
101 /* ARC4 does not use a fixed-length IV */
106 * Perform ARC4 encryption or decryption, skipping initial keystream bytes
108 * @v key ARC4 encryption key
109 * @v keylen Key length
110 * @v skip Number of bytes of keystream to skip
111 * @v src Message to encrypt or decrypt
112 * @v msglen Length of message
113 * @ret dst Encrypted or decrypted message
115 void arc4_skip ( const void *key, size_t keylen, size_t skip,
116 const void *src, void *dst, size_t msglen )
119 arc4_setkey ( &ctx, key, keylen );
120 arc4_xor ( &ctx, NULL, NULL, skip );
121 arc4_xor ( &ctx, src, dst, msglen );
124 struct cipher_algorithm arc4_algorithm = {
126 .ctxsize = ARC4_CTX_SIZE,
128 .setkey = arc4_setkey,