1 /*
2 * The ARC4 stream cipher.
3 *
4 * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
5 *
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.
10 *
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.
15 *
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
19 * 02110-1301, USA.
20 */
21
22 FILE_LICENCE ( GPL2_OR_LATER );
23
24 #include <ipxe/crypto.h>
25 #include <ipxe/arc4.h>
26
27 #define SWAP( ary, i, j ) \
28 ({ u8 temp = ary[i]; ary[i] = ary[j]; ary[j] = temp; })
29
30 /**
31 * Set ARC4 key
32 *
33 * @v ctxv ARC4 encryption context
34 * @v keyv Key to set
35 * @v keylen Length of key
36 *
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
40 * cipher.
41 */
arc4_setkey(void * ctxv,const void * keyv,size_t keylen)42 static int arc4_setkey ( void *ctxv, const void *keyv, size_t keylen )
43 {
44 struct arc4_ctx *ctx = ctxv;
45 const u8 *key = keyv;
46 u8 *S = ctx->state;
47 int i, j;
48
49 for ( i = 0; i < 256; i++ ) {
50 S[i] = i;
51 }
52
53 for ( i = j = 0; i < 256; i++ ) {
54 j = ( j + S[i] + key[i % keylen] ) & 0xff;
55 SWAP ( S, i, j );
56 }
57
58 ctx->i = ctx->j = 0;
59 return 0;
60 }
61
62 /**
63 * Perform ARC4 encryption or decryption
64 *
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
69 *
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.
74 *
75 * If you pass a @c NULL source or destination pointer, @a len
76 * keystream bytes will be consumed without encrypting any data.
77 */
arc4_xor(void * ctxv,const void * srcv,void * dstv,size_t len)78 static void arc4_xor ( void *ctxv, const void *srcv, void *dstv,
79 size_t len )
80 {
81 struct arc4_ctx *ctx = ctxv;
82 const u8 *src = srcv;
83 u8 *dst = dstv;
84 u8 *S = ctx->state;
85 int i = ctx->i, j = ctx->j;
86
87 while ( len-- ) {
88 i = ( i + 1 ) & 0xff;
89 j = ( j + S[i] ) & 0xff;
90 SWAP ( S, i, j );
91 if ( srcv && dstv )
92 *dst++ = *src++ ^ S[(S[i] + S[j]) & 0xff];
93 }
94
95 ctx->i = i;
96 ctx->j = j;
97 }
98
arc4_setiv(void * ctx __unused,const void * iv __unused)99 static void arc4_setiv ( void *ctx __unused, const void *iv __unused )
100 {
101 /* ARC4 does not use a fixed-length IV */
102 }
103
104
105 /**
106 * Perform ARC4 encryption or decryption, skipping initial keystream bytes
107 *
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
114 */
arc4_skip(const void * key,size_t keylen,size_t skip,const void * src,void * dst,size_t msglen)115 void arc4_skip ( const void *key, size_t keylen, size_t skip,
116 const void *src, void *dst, size_t msglen )
117 {
118 struct arc4_ctx ctx;
119 arc4_setkey ( &ctx, key, keylen );
120 arc4_xor ( &ctx, NULL, NULL, skip );
121 arc4_xor ( &ctx, src, dst, msglen );
122 }
123
124 struct cipher_algorithm arc4_algorithm = {
125 .name = "ARC4",
126 .ctxsize = ARC4_CTX_SIZE,
127 .blocksize = 1,
128 .setkey = arc4_setkey,
129 .setiv = arc4_setiv,
130 .encrypt = arc4_xor,
131 .decrypt = arc4_xor,
132 };
133