iPXE
Data Structures | Macros | Functions | Variables
aes.h File Reference

AES algorithm. More...

#include <ipxe/crypto.h>

Go to the source code of this file.

Data Structures

union  aes_matrix
 AES matrix. More...
 
struct  aes_round_keys
 AES round keys. More...
 
struct  aes_context
 AES context. More...
 

Macros

#define AES_BLOCKSIZE   16
 AES blocksize. More...
 
#define AES_MAX_ROUNDS   15
 Maximum number of AES rounds. More...
 
#define AES_CTX_SIZE   sizeof ( struct aes_context )
 AES context size. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
int aes_wrap (const void *kek, const void *src, void *dest, int nblk)
 Wrap a key or other data using AES Key Wrap (RFC 3394) More...
 
int aes_unwrap (const void *kek, const void *src, void *dest, int nblk)
 Unwrap a key or other data using AES Key Wrap (RFC 3394) More...
 

Variables

struct cipher_algorithm aes_algorithm
 Basic AES algorithm. More...
 
struct cipher_algorithm aes_ecb_algorithm
 
struct cipher_algorithm aes_cbc_algorithm
 
struct cipher_algorithm aes_gcm_algorithm
 

Detailed Description

AES algorithm.

Definition in file aes.h.

Macro Definition Documentation

◆ AES_BLOCKSIZE

#define AES_BLOCKSIZE   16

AES blocksize.

Definition at line 15 of file aes.h.

◆ AES_MAX_ROUNDS

#define AES_MAX_ROUNDS   15

Maximum number of AES rounds.

Definition at line 18 of file aes.h.

◆ AES_CTX_SIZE

#define AES_CTX_SIZE   sizeof ( struct aes_context )

AES context size.

Definition at line 45 of file aes.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ aes_wrap()

int aes_wrap ( const void *  kek,
const void *  src,
void *  dest,
int  nblk 
)

Wrap a key or other data using AES Key Wrap (RFC 3394)

Parameters
kekKey Encryption Key, 16 bytes
srcData to encrypt
nblkNumber of 8-byte blocks in data
Return values
destEncrypted data (8 bytes longer than input)

The algorithm is implemented such that src and dest may point to the same buffer.

Definition at line 38 of file aes_wrap.c.

39 {
40  u8 *A = dest;
41  u8 B[16];
42  u8 *R;
43  int i, j;
44  void *aes_ctx = malloc ( AES_CTX_SIZE );
45 
46  if ( ! aes_ctx )
47  return -1;
48 
49  cipher_setkey ( &aes_algorithm, aes_ctx, kek, 16 );
50 
51  /* Set up */
52  memset ( A, 0xA6, 8 );
53  memmove ( dest + 8, src, nblk * 8 );
54 
55  /* Wrap */
56  for ( j = 0; j < 6; j++ ) {
57  R = dest + 8;
58  for ( i = 1; i <= nblk; i++ ) {
59  memcpy ( B, A, 8 );
60  memcpy ( B + 8, R, 8 );
61  cipher_encrypt ( &aes_algorithm, aes_ctx, B, B, 16 );
62  memcpy ( A, B, 8 );
63  A[7] ^= ( nblk * j ) + i;
64  memcpy ( R, B + 8, 8 );
65  R += 8;
66  }
67  }
68 
69  free ( aes_ctx );
70  return 0;
71 }
static const void * src
Definition: string.h:47
#define AES_CTX_SIZE
AES context size.
Definition: aes.h:45
struct cipher_algorithm aes_algorithm
Basic AES algorithm.
Definition: aes.c:783
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:233
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
u8 kek[WPA_KEK_LEN]
EAPOL-Key Key Encryption Key (KEK)
Definition: wpa.h:31
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
void * memmove(void *dest, const void *src, size_t len) __nonnull
if(len >=6 *4) __asm__ __volatile__("movsl" if(len >=5 *4) __asm__ __volatile__("movsl" if(len >=4 *4) __asm__ __volatile__("movsl" if(len >=3 *4) __asm__ __volatile__("movsl" if(len >=2 *4) __asm__ __volatile__("movsl" if(len >=1 *4) __asm__ __volatile__("movsl" if((len % 4) >=2) __asm__ __volatile__("movsw" if((len % 2) >=1) __asm__ __volatile__("movsb" return dest
Definition: string.h:150
uint8_t u8
Definition: stdint.h:19
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition: crypto.h:217
void * memset(void *dest, int character, size_t len) __nonnull

References aes_algorithm, AES_CTX_SIZE, cipher_encrypt, cipher_setkey(), dest, free, kek, malloc(), memcpy(), memmove(), memset(), and src.

◆ aes_unwrap()

int aes_unwrap ( const void *  kek,
const void *  src,
void *  dest,
int  nblk 
)

Unwrap a key or other data using AES Key Wrap (RFC 3394)

Parameters
kekKey Encryption Key, 16 bytes
srcData to decrypt
nblkNumber of 8-byte blocks in plaintext key
Return values
destDecrypted data (8 bytes shorter than input)
rcZero on success, nonzero on IV mismatch

The algorithm is implemented such that src and dest may point to the same buffer.

Definition at line 85 of file aes_wrap.c.

86 {
87  u8 A[8], B[16];
88  u8 *R;
89  int i, j;
90  void *aes_ctx = malloc ( AES_CTX_SIZE );
91 
92  if ( ! aes_ctx )
93  return -1;
94 
95  cipher_setkey ( &aes_algorithm, aes_ctx, kek, 16 );
96 
97  /* Set up */
98  memcpy ( A, src, 8 );
99  memmove ( dest, src + 8, nblk * 8 );
100 
101  /* Unwrap */
102  for ( j = 5; j >= 0; j-- ) {
103  R = dest + ( nblk - 1 ) * 8;
104  for ( i = nblk; i >= 1; i-- ) {
105  memcpy ( B, A, 8 );
106  memcpy ( B + 8, R, 8 );
107  B[7] ^= ( nblk * j ) + i;
108  cipher_decrypt ( &aes_algorithm, aes_ctx, B, B, 16 );
109  memcpy ( A, B, 8 );
110  memcpy ( R, B + 8, 8 );
111  R -= 8;
112  }
113  }
114 
115  free ( aes_ctx );
116 
117  /* Check IV */
118  for ( i = 0; i < 8; i++ ) {
119  if ( A[i] != 0xA6 )
120  return -1;
121  }
122 
123  return 0;
124 }
static const void * src
Definition: string.h:47
#define AES_CTX_SIZE
AES context size.
Definition: aes.h:45
struct cipher_algorithm aes_algorithm
Basic AES algorithm.
Definition: aes.c:783
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
u8 kek[WPA_KEK_LEN]
EAPOL-Key Key Encryption Key (KEK)
Definition: wpa.h:31
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:243
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
void * memmove(void *dest, const void *src, size_t len) __nonnull
if(len >=6 *4) __asm__ __volatile__("movsl" if(len >=5 *4) __asm__ __volatile__("movsl" if(len >=4 *4) __asm__ __volatile__("movsl" if(len >=3 *4) __asm__ __volatile__("movsl" if(len >=2 *4) __asm__ __volatile__("movsl" if(len >=1 *4) __asm__ __volatile__("movsl" if((len % 4) >=2) __asm__ __volatile__("movsw" if((len % 2) >=1) __asm__ __volatile__("movsb" return dest
Definition: string.h:150
uint8_t u8
Definition: stdint.h:19
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition: crypto.h:217

References aes_algorithm, AES_CTX_SIZE, cipher_decrypt, cipher_setkey(), dest, free, kek, malloc(), memcpy(), memmove(), and src.

Referenced by ccmp_kie_decrypt().

Variable Documentation

◆ aes_algorithm

struct cipher_algorithm aes_algorithm

Basic AES algorithm.

Definition at line 783 of file aes.c.

Referenced by aes_unwrap(), aes_wrap(), ccmp_cbc_mac(), ccmp_ctr_xor(), ccmp_feed_cbc_mac(), and ccmp_init().

◆ aes_ecb_algorithm

struct cipher_algorithm aes_ecb_algorithm

Referenced by aes_test_exec().

◆ aes_cbc_algorithm

struct cipher_algorithm aes_cbc_algorithm

◆ aes_gcm_algorithm

struct cipher_algorithm aes_gcm_algorithm

Referenced by gcm_test_exec().