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 cipher Cipher algorithm
34 * @v ctxv ARC4 encryption context
35 * @v keyv Key to set
36 * @v keylen Length of key
37 *
38 * If an initialisation vector is to be used, it should be prepended
39 * to the key; ARC4 does not implement the @c setiv function because
40 * there is no standard length for an initialisation vector in the
41 * cipher.
42 */
43static int arc4_setkey ( struct cipher_algorithm *cipher __unused, void *ctxv,
44 const void *keyv, size_t keylen )
45{
46 struct arc4_ctx *ctx = ctxv;
47 const u8 *key = keyv;
48 u8 *S = ctx->state;
49 int i, j;
50
51 for ( i = 0; i < 256; i++ ) {
52 S[i] = i;
53 }
54
55 for ( i = j = 0; i < 256; i++ ) {
56 j = ( j + S[i] + key[i % keylen] ) & 0xff;
57 SWAP ( S, i, j );
58 }
59
60 ctx->i = ctx->j = 0;
61 return 0;
62}
63
64/**
65 * Perform ARC4 encryption or decryption
66 *
67 * @v cipher Cipher algorithm
68 * @v ctxv ARC4 encryption context
69 * @v srcv Data to encrypt or decrypt
70 * @v dstv Location to store encrypted or decrypted data
71 * @v len Length of data to operate on
72 *
73 * ARC4 is a stream cipher that works by generating a stream of PRNG
74 * data based on the key, and XOR'ing it with the data to be
75 * encrypted. Since XOR is symmetric, encryption and decryption in
76 * ARC4 are the same operation.
77 *
78 * If you pass a @c NULL source or destination pointer, @a len
79 * keystream bytes will be consumed without encrypting any data.
80 */
81static void arc4_xor ( struct cipher_algorithm *cipher __unused, void *ctxv,
82 const void *srcv, void *dstv, size_t len )
83{
84 struct arc4_ctx *ctx = ctxv;
85 const u8 *src = srcv;
86 u8 *dst = dstv;
87 u8 *S = ctx->state;
88 int i = ctx->i, j = ctx->j;
89
90 while ( len-- ) {
91 i = ( i + 1 ) & 0xff;
92 j = ( j + S[i] ) & 0xff;
93 SWAP ( S, i, j );
94 if ( srcv && dstv )
95 *dst++ = *src++ ^ S[(S[i] + S[j]) & 0xff];
96 }
97
98 ctx->i = i;
99 ctx->j = j;
100}
101
102/**
103 * Perform ARC4 encryption or decryption, skipping initial keystream bytes
104 *
105 * @v key ARC4 encryption key
106 * @v keylen Key length
107 * @v skip Number of bytes of keystream to skip
108 * @v src Message to encrypt or decrypt
109 * @v msglen Length of message
110 * @ret dst Encrypted or decrypted message
111 */
112void arc4_skip ( const void *key, size_t keylen, size_t skip,
113 const void *src, void *dst, size_t msglen )
114{
115 struct cipher_algorithm *cipher = &arc4_algorithm;
116 struct arc4_ctx ctx;
117
118 arc4_setkey ( cipher, &ctx, key, keylen );
119 arc4_xor ( cipher, &ctx, NULL, NULL, skip );
120 arc4_xor ( cipher, &ctx, src, dst, msglen );
121}
122
124 .name = "ARC4",
125 .ctxsize = ARC4_CTX_SIZE,
126 .blocksize = 1,
127 .alignsize = 1,
128 .authsize = 0,
129 .confidential = 1,
130 .setkey = arc4_setkey,
131 .setiv = cipher_null_setiv,
132 .encrypt = arc4_xor,
133 .decrypt = arc4_xor,
134 .auth = cipher_null_auth,
135};
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
struct golan_eq_context ctx
Definition CIB_PRM.h:0
union @162305117151260234136356364136041353210355154177 key
static int arc4_setkey(struct cipher_algorithm *cipher __unused, void *ctxv, const void *keyv, size_t keylen)
Set ARC4 key.
Definition arc4.c:43
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:112
#define SWAP(ary, i, j)
Definition arc4.c:27
struct cipher_algorithm arc4_algorithm
Definition arc4.c:123
static void arc4_xor(struct cipher_algorithm *cipher __unused, void *ctxv, const void *srcv, void *dstv, size_t len)
Perform ARC4 encryption or decryption.
Definition arc4.c:81
#define ARC4_CTX_SIZE
Definition arc4.h:15
static const void * src
Definition string.h:48
int cipher_null_setiv(struct cipher_algorithm *cipher __unused, void *ctx __unused, const void *iv __unused, size_t ivlen __unused)
Definition crypto_null.c:70
void cipher_null_auth(struct cipher_algorithm *cipher __unused, void *ctx __unused, void *auth __unused)
Definition crypto_null.c:89
ring len
Length.
Definition dwmac.h:226
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:598
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#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:58
static u16 S(u16 v)
Perform S-box mapping on a 16-bit value.
Definition wpa_tkip.c:138