iPXE
arc4.c
Go to the documentation of this file.
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
22FILE_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 */
42static 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 */
78static 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
99/**
100 * Perform ARC4 encryption or decryption, skipping initial keystream bytes
101 *
102 * @v key ARC4 encryption key
103 * @v keylen Key length
104 * @v skip Number of bytes of keystream to skip
105 * @v src Message to encrypt or decrypt
106 * @v msglen Length of message
107 * @ret dst Encrypted or decrypted message
108 */
109void arc4_skip ( const void *key, size_t keylen, size_t skip,
110 const void *src, void *dst, size_t msglen )
111{
112 struct arc4_ctx ctx;
113 arc4_setkey ( &ctx, key, keylen );
114 arc4_xor ( &ctx, NULL, NULL, skip );
115 arc4_xor ( &ctx, src, dst, msglen );
116}
117
119 .name = "ARC4",
120 .ctxsize = ARC4_CTX_SIZE,
121 .blocksize = 1,
122 .alignsize = 1,
123 .authsize = 0,
124 .setkey = arc4_setkey,
125 .setiv = cipher_null_setiv,
126 .encrypt = arc4_xor,
127 .decrypt = arc4_xor,
128 .auth = cipher_null_auth,
129};
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
union @162305117151260234136356364136041353210355154177 key
Sense key.
Definition scsi.h:3
struct golan_eq_context ctx
Definition CIB_PRM.h:0
static void arc4_xor(void *ctxv, const void *srcv, void *dstv, size_t len)
Perform ARC4 encryption or decryption.
Definition arc4.c:78
void arc4_skip(const void *key, size_t keylen, size_t skip, const void *src, void *dst, size_t msglen)
Perform ARC4 encryption or decryption, skipping initial keystream bytes.
Definition arc4.c:109
#define SWAP(ary, i, j)
Definition arc4.c:27
struct cipher_algorithm arc4_algorithm
Definition arc4.c:118
static int arc4_setkey(void *ctxv, const void *keyv, size_t keylen)
Set ARC4 key.
Definition arc4.c:42
#define ARC4_CTX_SIZE
Definition arc4.h:15
static const void * src
Definition string.h:48
void cipher_null_setiv(void *ctx __unused, const void *iv __unused, size_t ivlen __unused)
Definition crypto_null.c:65
void cipher_null_auth(void *ctx __unused, void *auth __unused)
Definition crypto_null.c:80
ring len
Length.
Definition dwmac.h:226
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define u8
Definition igbvf_osdep.h:40
Cryptographic API.
int i
Definition arc4.h:11
int j
Definition arc4.h:11
A cipher algorithm.
Definition crypto.h:51
static u16 S(u16 v)
Perform S-box mapping on a 16-bit value.
Definition wpa_tkip.c:138