iPXE
Macros | Functions | Variables
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)   ({ u8 temp = ary[i]; ary[i] = ary[j]; ary[j] = temp; })
 

Functions

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

Variables

struct cipher_algorithm arc4_algorithm
 

Macro Definition Documentation

◆ SWAP

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

Definition at line 27 of file arc4.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER  )

◆ arc4_setkey()

static int arc4_setkey ( void *  ctxv,
const void *  keyv,
size_t  keylen 
)
static

Set ARC4 key.

Parameters
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 42 of file arc4.c.

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 }
Definition: arc4.h:10
int i
Definition: arc4.h:11
static u16 S(u16 v)
Perform S-box mapping on a 16-bit value.
Definition: wpa_tkip.c:137
static void const void size_t keylen
Definition: crypto.h:233
#define SWAP(ary, i, j)
Definition: arc4.c:27
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
int j
Definition: arc4.h:11
uint8_t u8
Definition: stdint.h:19
union @382 key
Sense key.
Definition: crypto.h:284

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

Referenced by arc4_skip().

◆ arc4_xor()

static void arc4_xor ( void *  ctxv,
const void *  srcv,
void *  dstv,
size_t  len 
)
static

Perform ARC4 encryption or decryption.

Parameters
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 78 of file arc4.c.

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 }
Definition: arc4.h:10
static void const void void * dst
Definition: crypto.h:244
static void const void * src
Definition: crypto.h:244
int i
Definition: arc4.h:11
static u16 S(u16 v)
Perform S-box mapping on a 16-bit value.
Definition: wpa_tkip.c:137
#define SWAP(ary, i, j)
Definition: arc4.c:27
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
int j
Definition: arc4.h:11
uint32_t len
Length.
Definition: ena.h:14
uint8_t u8
Definition: stdint.h:19

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

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 109 of file arc4.c.

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 }
Definition: arc4.h:10
static void const void void * dst
Definition: crypto.h:244
static void const void * src
Definition: crypto.h:244
static void const void size_t keylen
Definition: crypto.h:233
static void arc4_xor(void *ctxv, const void *srcv, void *dstv, size_t len)
Perform ARC4 encryption or decryption.
Definition: arc4.c:78
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
static int arc4_setkey(void *ctxv, const void *keyv, size_t keylen)
Set ARC4 key.
Definition: arc4.c:42
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
union @382 key
Sense key.
Definition: crypto.h:284

References arc4_setkey(), arc4_xor(), ctx, dst, key, keylen, 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,
.setkey = arc4_setkey,
.encrypt = arc4_xor,
.decrypt = arc4_xor,
}
void cipher_null_setiv(void *ctx __unused, const void *iv __unused, size_t ivlen __unused)
Definition: crypto_null.c:64
static void arc4_xor(void *ctxv, const void *srcv, void *dstv, size_t len)
Perform ARC4 encryption or decryption.
Definition: arc4.c:78
static int arc4_setkey(void *ctxv, const void *keyv, size_t keylen)
Set ARC4 key.
Definition: arc4.c:42
void cipher_null_auth(void *ctx __unused, void *auth __unused)
Definition: crypto_null.c:79
#define ARC4_CTX_SIZE
Definition: arc4.h:15

Definition at line 118 of file arc4.c.

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