iPXE
Defines | Functions | Variables
arc4.c File Reference
#include <ipxe/crypto.h>
#include <ipxe/arc4.h>

Go to the source code of this file.

Defines

#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.
static void arc4_xor (void *ctxv, const void *srcv, void *dstv, size_t len)
 Perform ARC4 encryption or decryption.
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.

Variables

struct cipher_algorithm arc4_algorithm

Define Documentation

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

Definition at line 27 of file arc4.c.

Referenced by arc4_setkey(), and arc4_xor().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER  )
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.

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

Referenced by arc4_skip().

{
        struct arc4_ctx *ctx = ctxv;
        const u8 *key = keyv;
        u8 *S = ctx->state;
        int i, j;

        for ( i = 0; i < 256; i++ ) {
                S[i] = i;
        }

        for ( i = j = 0; i < 256; i++ ) {
                j = ( j + S[i] + key[i % keylen] ) & 0xff;
                SWAP ( S, i, j );
        }

        ctx->i = ctx->j = 0;
        return 0;
}
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.

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

Referenced by arc4_skip().

{
        struct arc4_ctx *ctx = ctxv;
        const u8 *src = srcv;
        u8 *dst = dstv;
        u8 *S = ctx->state;
        int i = ctx->i, j = ctx->j;

        while ( len-- ) {
                i = ( i + 1 ) & 0xff;
                j = ( j + S[i] ) & 0xff;
                SWAP ( S, i, j );
                if ( srcv && dstv )
                        *dst++ = *src++ ^ S[(S[i] + S[j]) & 0xff];
        }

        ctx->i = i;
        ctx->j = j;
}
static void arc4_setiv ( void *ctx  __unused,
const void *iv  __unused 
) [static]

Definition at line 99 of file arc4.c.

{
        /* ARC4 does not use a fixed-length IV */
}
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.

References arc4_setkey(), arc4_xor(), and NULL.

Referenced by tkip_kie_decrypt().

{
        struct arc4_ctx ctx;
        arc4_setkey ( &ctx, key, keylen );
        arc4_xor ( &ctx, NULL, NULL, skip );
        arc4_xor ( &ctx, src, dst, msglen );
}

Variable Documentation

Initial value:
 {
        .name = "ARC4",
        .ctxsize = ARC4_CTX_SIZE,
        .blocksize = 1,
        .setkey = arc4_setkey,
        .setiv = arc4_setiv,
        .encrypt = arc4_xor,
        .decrypt = arc4_xor,
}

Definition at line 124 of file arc4.c.

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