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...
 
static void arc4_setiv (void *ctx __unused, const void *iv __unused)
 
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
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
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
int j
Definition: arc4.h:11
uint8_t u8
Definition: stdint.h:19
union @375 key
Sense key.
Definition: scsi.h:18

References ctx, arc4_ctx::i, arc4_ctx::j, key, 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
int i
Definition: arc4.h:11
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
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
static __always_inline void off_t userptr_t src
Definition: efi_uaccess.h:66
int j
Definition: arc4.h:11
uint32_t len
Length.
Definition: ena.h:14
uint8_t u8
Definition: stdint.h:19

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

Referenced by arc4_skip().

◆ arc4_setiv()

static void arc4_setiv ( void *ctx  __unused,
const void *iv  __unused 
)
static

Definition at line 99 of file arc4.c.

100 {
101  /* ARC4 does not use a fixed-length IV */
102 }

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

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 }
Definition: arc4.h:10
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
static void arc4_xor(void *ctxv, const void *srcv, void *dstv, size_t len)
Perform ARC4 encryption or decryption.
Definition: arc4.c:78
static __always_inline void off_t userptr_t src
Definition: efi_uaccess.h:66
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:362
union @375 key
Sense key.
Definition: scsi.h:18

References 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,
.setkey = arc4_setkey,
.setiv = arc4_setiv,
.encrypt = arc4_xor,
.decrypt = arc4_xor,
}
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
static void arc4_setiv(void *ctx __unused, const void *iv __unused)
Definition: arc4.c:99
#define ARC4_CTX_SIZE
Definition: arc4.h:15

Definition at line 124 of file arc4.c.

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