iPXE
arc4.c File Reference
#include <ipxe/crypto.h>
#include <ipxe/arc4.h>

Go to the source code of this file.

Macros

#define SWAP(ary, i, j)

Functions

 FILE_LICENCE (GPL2_OR_LATER)
static int arc4_setkey (struct cipher_algorithm *cipher __unused, void *ctxv, const void *keyv, size_t keylen)
 Set ARC4 key.
static void arc4_xor (struct cipher_algorithm *cipher __unused, void *ctxv, const void *srcv, void *dstv, size_t len)
 Perform ARC4 encryption or decryption.
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.

Variables

struct cipher_algorithm arc4_algorithm

Macro Definition Documentation

◆ SWAP

#define SWAP ( ary,
i,
j )
Value:
({ u8 temp = ary[i]; ary[i] = ary[j]; ary[j] = temp; })
#define u8
Definition igbvf_osdep.h:40

Definition at line 27 of file arc4.c.

27#define SWAP( ary, i, j ) \
28 ({ u8 temp = ary[i]; ary[i] = ary[j]; ary[j] = temp; })

Referenced by arc4_setkey(), and arc4_xor().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER )

◆ arc4_setkey()

int arc4_setkey ( struct cipher_algorithm *cipher __unused,
void * ctxv,
const void * keyv,
size_t keylen )
static

Set ARC4 key.

Parameters
cipherCipher algorithm
ctxvARC4 encryption context
keyvKey to set
keylenLength of key

If an initialisation vector is to be used, it should be prepended to the key; ARC4 does not implement the setiv function because there is no standard length for an initialisation vector in the cipher.

Definition at line 43 of file arc4.c.

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}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
union @162305117151260234136356364136041353210355154177 key
#define SWAP(ary, i, j)
Definition arc4.c:27
int i
Definition arc4.h:11
int j
Definition arc4.h:11
static u16 S(u16 v)
Perform S-box mapping on a 16-bit value.
Definition wpa_tkip.c:138

References __unused, ctx, arc4_ctx::i, arc4_ctx::j, key, S(), SWAP, and u8.

Referenced by arc4_skip().

◆ arc4_xor()

void arc4_xor ( struct cipher_algorithm *cipher __unused,
void * ctxv,
const void * srcv,
void * dstv,
size_t len )
static

Perform ARC4 encryption or decryption.

Parameters
cipherCipher algorithm
ctxvARC4 encryption context
srcvData to encrypt or decrypt
dstvLocation to store encrypted or decrypted data
lenLength of data to operate on

ARC4 is a stream cipher that works by generating a stream of PRNG data based on the key, and XOR'ing it with the data to be encrypted. Since XOR is symmetric, encryption and decryption in ARC4 are the same operation.

If you pass a NULL source or destination pointer, len keystream bytes will be consumed without encrypting any data.

Definition at line 81 of file arc4.c.

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}
static const void * src
Definition string.h:48
ring len
Length.
Definition dwmac.h:226

References __unused, ctx, arc4_ctx::i, arc4_ctx::j, len, S(), src, SWAP, and u8.

Referenced by arc4_skip().

◆ arc4_skip()

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.

Parameters
keyARC4 encryption key
keylenKey length
skipNumber of bytes of keystream to skip
srcMessage to encrypt or decrypt
msglenLength of message
Return values
dstEncrypted or decrypted message

Definition at line 112 of file arc4.c.

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}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
static int arc4_setkey(struct cipher_algorithm *cipher __unused, void *ctxv, const void *keyv, size_t keylen)
Set ARC4 key.
Definition arc4.c:43
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
A cipher algorithm.
Definition crypto.h:58

References arc4_algorithm, arc4_setkey(), arc4_xor(), ctx, key, NULL, and src.

Referenced by tkip_kie_decrypt().

Variable Documentation

◆ arc4_algorithm

struct cipher_algorithm arc4_algorithm
Initial value:
= {
.name = "ARC4",
.ctxsize = ARC4_CTX_SIZE,
.blocksize = 1,
.alignsize = 1,
.authsize = 0,
.confidential = 1,
.setkey = arc4_setkey,
.encrypt = arc4_xor,
.decrypt = arc4_xor,
}
#define ARC4_CTX_SIZE
Definition arc4.h:15
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

Definition at line 123 of file arc4.c.

123 {
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};

Referenced by arc4_skip(), tkip_decrypt(), tkip_encrypt(), wep_decrypt(), and wep_encrypt().