iPXE
aes.h File Reference

AES algorithm. More...

#include <stdint.h>
#include <ipxe/crypto.h>
#include <bits/aes.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.
#define AES_MAX_ROUNDS   15
 Maximum number of AES rounds.
#define AES_CTX_SIZE   ( sizeof ( struct aes_context ) + AES_BLOCKSIZE - 1 )
 AES context size (including alignment padding).

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static struct aes_context * aes_context (void *ctx)
 Align AES context.
void aes_accelerate (void)
 Enable hardware acceleration (if supported).
void aes_decelerate (void)
 Disable hardware acceleration (for testing).
int aes_is_accelerated (void)
 Check if hardware acceleration is currently enabled (for testing).
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).
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).

Variables

struct cipher_algorithm aes_algorithm
 Basic AES algorithm.
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

◆ AES_MAX_ROUNDS

#define AES_MAX_ROUNDS   15

Maximum number of AES rounds.

Definition at line 20 of file aes.h.

Referenced by aes_rounds().

◆ AES_CTX_SIZE

#define AES_CTX_SIZE   ( sizeof ( struct aes_context ) + AES_BLOCKSIZE - 1 )

AES context size (including alignment padding).

Definition at line 47 of file aes.h.

Referenced by aes_unwrap(), and aes_wrap().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ aes_context()

struct aes_context * aes_context ( void * ctx)
inlinestatic

Align AES context.

Parameters
ctxContext
Return values
aesAES context

Definition at line 55 of file aes.h.

55 {
56
57 return ( ( struct aes_context * )
58 ( ( ( ( intptr_t ) ctx ) + AES_BLOCKSIZE - 1 ) &
59 ~( AES_BLOCKSIZE - 1 ) ) );
60}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
unsigned long intptr_t
Definition stdint.h:21
#define AES_BLOCKSIZE
AES blocksize.
Definition aes.h:17
AES context.
Definition aes.h:37

References AES_BLOCKSIZE, and ctx.

Referenced by aes_decrypt(), aes_encrypt(), aes_setkey(), aesni_decrypt(), and aesni_encrypt().

◆ aes_accelerate()

void aes_accelerate ( void )

Enable hardware acceleration (if supported).

Definition at line 211 of file aesni.c.

211 {
212 struct x86_features features;
213
214 /* Detect AES-NI support */
216 if ( features.intel.ecx & CPUID_FEATURES_INTEL_ECX_AESNI ) {
217 DBGC ( &aes_algorithm, "AES enabled AES-NI acceleration\n" );
220 }
221}
struct cipher_algorithm aes_algorithm
Basic AES algorithm.
Definition aes.c:847
static void aesni_decrypt(struct cipher_algorithm *cipher __unused, void *ctx, const void *src, void *dst, size_t len)
Decrypt data.
Definition aesni.c:150
static void aesni_encrypt(struct cipher_algorithm *cipher __unused, void *ctx, const void *src, void *dst, size_t len)
Encrypt data.
Definition aesni.c:84
void x86_features(struct x86_features *features)
Get x86 CPU features.
Definition cpuid.c:164
#define CPUID_FEATURES_INTEL_ECX_AESNI
AES-NI instructions are supported.
Definition cpuid.h:47
uint32_t features
Supported features.
Definition ena.h:5
#define DBGC(...)
Definition compiler.h:530
x86 CPU features
Definition cpuid.h:24

References aes_algorithm, aesni_decrypt(), aesni_encrypt(), CPUID_FEATURES_INTEL_ECX_AESNI, DBGC, features, and x86_features().

Referenced by aes_hw_test_exec(), aes_setkey(), and aes_sw_test_exec().

◆ aes_decelerate()

void aes_decelerate ( void )
extern

Disable hardware acceleration (for testing).

Definition at line 824 of file aes.c.

824 {
825
826 /* Restore original algorithm pointers */
827 aes_algorithm.encrypt = aes_encrypt;
828 aes_algorithm.decrypt = aes_decrypt;
829 DBGC ( &aes_algorithm, "AES disabled hardware acceleration\n" );
830
831 /* Mark hardware acceleration mode as selected */
832 aes_selected = 1;
833}
static void aes_encrypt(struct cipher_algorithm *cipher __unused, void *ctx, const void *src, void *dst, size_t len)
Encrypt data.
Definition aes.c:427
static void aes_decrypt(struct cipher_algorithm *cipher __unused, void *ctx, const void *src, void *dst, size_t len)
Decrypt data.
Definition aes.c:464
static int aes_selected
AES hardware acceleration mode has been selected.
Definition aes.c:153

References aes_algorithm, aes_decrypt(), aes_encrypt(), aes_selected, and DBGC.

Referenced by aes_sw_test_exec().

◆ aes_is_accelerated()

int aes_is_accelerated ( void )
extern

Check if hardware acceleration is currently enabled (for testing).

Return values
is_acceleratedAES is using hardware acceleration

Definition at line 840 of file aes.c.

840 {
841
842 /* Check if hardware acceleration is enabled */
843 return ( aes_algorithm.encrypt != aes_encrypt );
844}

References aes_algorithm, and aes_encrypt().

Referenced by aes_hw_test_exec().

◆ 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 zfree ( aes_ctx );
70 return 0;
71}
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" retur dest)
Definition string.h:151
static const void * src
Definition string.h:48
#define u8
Definition igbvf_osdep.h:40
#define AES_CTX_SIZE
AES context size (including alignment padding).
Definition aes.h:47
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition crypto.h:310
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition crypto.h:326
void * memcpy(void *dest, const void *src, size_t len) __nonnull
void * memset(void *dest, int character, size_t len) __nonnull
void * memmove(void *dest, const void *src, size_t len) __nonnull
void * malloc(size_t size)
Allocate memory.
Definition malloc.c:677
void zfree(void *ptr)
Clear and free memory.
Definition malloc.c:738
u8 kek[WPA_KEK_LEN]
EAPOL-Key Key Encryption Key (KEK).
Definition wpa.h:4

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

◆ 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 zfree ( 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}
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition crypto.h:336

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

Referenced by ccmp_kie_decrypt().

Variable Documentation

◆ aes_algorithm

struct cipher_algorithm aes_algorithm
extern

Basic AES algorithm.

Definition at line 847 of file aes.c.

847 {
848 .name = "aes",
849 .ctxsize = sizeof ( struct aes_context ),
850 .blocksize = AES_BLOCKSIZE,
851 .alignsize = 0,
852 .authsize = 0,
853 .confidential = 1,
854 .setkey = aes_setkey,
855 .setiv = cipher_null_setiv,
856 .encrypt = aes_encrypt,
857 .decrypt = aes_decrypt,
858 .auth = cipher_null_auth,
859};
static int aes_setkey(struct cipher_algorithm *cipher __unused, void *ctx, const void *key, size_t keylen)
Set key.
Definition aes.c:712
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

Referenced by aes_accelerate(), aes_decelerate(), aes_is_accelerated(), aes_unwrap(), aes_wrap(), CBC_CIPHER(), ccmp_cbc_mac(), ccmp_ctr_xor(), ccmp_feed_cbc_mac(), ccmp_init(), ECB_CIPHER(), and GCM_CIPHER().

◆ aes_ecb_algorithm

struct cipher_algorithm aes_ecb_algorithm
extern

◆ aes_cbc_algorithm

◆ aes_gcm_algorithm

struct cipher_algorithm aes_gcm_algorithm
extern

Referenced by __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), __tls_cipher_suite(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), CIPHER_TEST(), GCM_CIPHER(), and gcm_test_exec().