iPXE
cipher_test.h File Reference

Cipher self-tests. More...

#include <stdint.h>
#include <ipxe/crypto.h>
#include <ipxe/test.h>

Go to the source code of this file.

Data Structures

struct  cipher_test
 A cipher test. More...

Macros

#define KEY(...)
 Define inline key.
#define IV(...)
 Define inline initialisation vector.
#define ADDITIONAL(...)
 Define inline additional data.
#define PLAINTEXT(...)
 Define inline plaintext data.
#define CIPHERTEXT(...)
 Define inline ciphertext data.
#define AUTH(...)
 Define inline authentication tag.
#define CIPHER_TEST(name, CIPHER, KEY, IV, ADDITIONAL, PLAINTEXT, CIPHERTEXT, AUTH)
 Define a cipher test.
#define cipher_encrypt_ok(test)
 Report a cipher encryption test result.
#define cipher_decrypt_ok(test)
 Report a cipher decryption test result.
#define cipher_ok(test)
 Report a cipher encryption and decryption test result.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
void cipher_encrypt_okx (struct cipher_test *test, const char *file, unsigned int line)
 Report a cipher encryption test result.
void cipher_decrypt_okx (struct cipher_test *test, const char *file, unsigned int line)
 Report a cipher decryption test result.
void cipher_okx (struct cipher_test *test, const char *file, unsigned int line)
 Report a cipher encryption and decryption test result.
unsigned long cipher_cost_encrypt (struct cipher_algorithm *cipher, size_t key_len)
 Calculate cipher encryption cost.
unsigned long cipher_cost_decrypt (struct cipher_algorithm *cipher, size_t key_len)
 Calculate cipher decryption cost.

Detailed Description

Cipher self-tests.

Definition in file cipher_test.h.

Macro Definition Documentation

◆ KEY

#define KEY ( ...)
Value:
{ __VA_ARGS__ }

Define inline key.

Definition at line 45 of file cipher_test.h.

Referenced by CIPHER_TEST(), CIPHER_TEST(), HMAC_TEST(), HMAC_TEST(), HMAC_TEST(), HMAC_TEST(), and HMAC_TEST().

◆ IV

#define IV ( ...)
Value:
{ __VA_ARGS__ }

Define inline initialisation vector.

Definition at line 48 of file cipher_test.h.

Referenced by CIPHER_TEST().

◆ ADDITIONAL

#define ADDITIONAL ( ...)
Value:
{ __VA_ARGS__ }

Define inline additional data.

Definition at line 51 of file cipher_test.h.

◆ PLAINTEXT

#define PLAINTEXT ( ...)

◆ CIPHERTEXT

◆ AUTH

◆ CIPHER_TEST

#define CIPHER_TEST ( name,
CIPHER,
KEY,
IV,
ADDITIONAL,
PLAINTEXT,
CIPHERTEXT,
AUTH )
Value:
static const uint8_t name ## _key [] = KEY; \
static const uint8_t name ## _iv [] = IV; \
static const uint8_t name ## _additional [] = ADDITIONAL; \
static const uint8_t name ## _plaintext [] = PLAINTEXT; \
static const uint8_t name ## _ciphertext \
[ sizeof ( name ## _plaintext ) ] = CIPHERTEXT; \
static const uint8_t name ## _auth [] = AUTH; \
static struct cipher_test name = { \
.cipher = CIPHER, \
.key = name ## _key, \
.key_len = sizeof ( name ## _key ), \
.iv = name ## _iv, \
.iv_len = sizeof ( name ## _iv ), \
.additional = name ## _additional, \
.additional_len = sizeof ( name ## _additional ), \
.plaintext = name ## _plaintext, \
.ciphertext = name ## _ciphertext, \
.len = sizeof ( name ## _plaintext ), \
.auth = name ## _auth, \
.auth_len = sizeof ( name ## _auth ), \
}
unsigned char uint8_t
Definition stdint.h:10
const char * name
Definition ath9k_hw.c:1986
#define IV(...)
Define inline initialisation vector.
Definition cipher_test.h:48
#define AUTH(...)
Define inline authentication tag.
Definition cipher_test.h:60
#define KEY(...)
Define inline key.
Definition cipher_test.h:45
#define ADDITIONAL(...)
Define inline additional data.
Definition cipher_test.h:51
#define CIPHERTEXT(...)
Define inline ciphertext data.
Definition cipher_test.h:57
#define PLAINTEXT(...)
Define inline plaintext data.
Definition cipher_test.h:54
ring len
Length.
Definition dwmac.h:226
A cipher test.
Definition cipher_test.h:17
u8 iv[16]
Initialization vector.
Definition wpa.h:33

Define a cipher test.

Parameters
nameTest name
CIPHERCipher algorithm
KEYKey
IVInitialisation vector
ADDITIONALAdditional data
PLAINTEXTPlaintext
CIPHERTEXTCiphertext
AUTHAuthentication tag
Return values
testCipher test

Definition at line 75 of file cipher_test.h.

◆ cipher_encrypt_ok

#define cipher_encrypt_ok ( test)
Value:
cipher_encrypt_okx ( test, __FILE__, __LINE__ )
void cipher_encrypt_okx(struct cipher_test *test, const char *file, unsigned int line)
Report a cipher encryption test result.
Definition cipher_test.c:54
static int test
Definition epic100.c:73

Report a cipher encryption test result.

Parameters
testCipher test

Definition at line 115 of file cipher_test.h.

115#define cipher_encrypt_ok( test ) \
116 cipher_encrypt_okx ( test, __FILE__, __LINE__ )

◆ cipher_decrypt_ok

#define cipher_decrypt_ok ( test)
Value:
cipher_decrypt_okx ( test, __FILE__, __LINE__ )
void cipher_decrypt_okx(struct cipher_test *test, const char *file, unsigned int line)
Report a cipher decryption test result.

Report a cipher decryption test result.

Parameters
testCipher test

Definition at line 123 of file cipher_test.h.

123#define cipher_decrypt_ok( test ) \
124 cipher_decrypt_okx ( test, __FILE__, __LINE__ )

◆ cipher_ok

#define cipher_ok ( test)
Value:
cipher_okx ( test, __FILE__, __LINE__ )
void cipher_okx(struct cipher_test *test, const char *file, unsigned int line)
Report a cipher encryption and decryption test result.

Report a cipher encryption and decryption test result.

Parameters
testCipher test

Definition at line 131 of file cipher_test.h.

131#define cipher_ok( test ) \
132 cipher_okx ( test, __FILE__, __LINE__ )

Referenced by aes_test_exec(), des_test_exec(), and gcm_test_exec().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ cipher_encrypt_okx()

void cipher_encrypt_okx ( struct cipher_test * test,
const char * file,
unsigned int line )
extern

Report a cipher encryption test result.

Parameters
testCipher test
fileTest code file
lineTest code line

Definition at line 54 of file cipher_test.c.

55 {
56 struct cipher_algorithm *cipher = test->cipher;
57 size_t len = test->len;
58 uint8_t ctx[cipher->ctxsize];
59 uint8_t ciphertext[len];
60 uint8_t auth[cipher->authsize];
61
62 /* Initialise cipher */
63 okx ( cipher_setkey ( cipher, ctx, test->key, test->key_len ) == 0,
64 file, line );
65 cipher_setiv ( cipher, ctx, test->iv, test->iv_len );
66
67 /* Process additional data, if applicable */
68 if ( test->additional_len ) {
69 okx ( is_auth_cipher ( cipher ), file, line );
70 cipher_encrypt ( cipher, ctx, test->additional, NULL,
71 test->additional_len );
72 }
73
74 /* Perform encryption */
75 cipher_encrypt ( cipher, ctx, test->plaintext, ciphertext, len );
76
77 /* Compare against expected ciphertext */
78 okx ( memcmp ( ciphertext, test->ciphertext, len ) == 0, file, line );
79
80 /* Check authentication tag */
81 okx ( cipher->authsize == test->auth_len, file, line );
82 cipher_auth ( cipher, ctx, auth );
83 okx ( memcmp ( auth, test->auth, test->auth_len ) == 0, file, line );
84
85 /* Reset initialisation vector */
86 cipher_setiv ( cipher, ctx, test->iv, test->iv_len );
87
88 /* Process additional data, if applicable */
89 if ( test->additional_len ) {
90 cipher_encrypt ( cipher, ctx, test->additional, NULL,
91 test->additional_len );
92 }
93
94 /* Perform in-place encryption */
95 memcpy ( ciphertext, test->plaintext, len );
96 cipher_encrypt ( cipher, ctx, ciphertext, ciphertext, len );
97
98 /* Compare against expected ciphertext */
99 okx ( memcmp ( ciphertext, test->ciphertext, len ) == 0, file, line );
100
101 /* Check authentication tag */
102 cipher_auth ( cipher, ctx, auth );
103 okx ( memcmp ( auth, test->auth, test->auth_len ) == 0, file, line );
104}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct golan_eq_context ctx
Definition CIB_PRM.h:0
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition crypto.h:235
static void cipher_setiv(struct cipher_algorithm *cipher, void *ctx, const void *iv, size_t ivlen)
Definition crypto.h:241
static int is_auth_cipher(struct cipher_algorithm *cipher)
Definition crypto.h:282
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition crypto.h:251
static void cipher_auth(struct cipher_algorithm *cipher, void *ctx, void *auth)
Definition crypto.h:267
void * memcpy(void *dest, const void *src, size_t len) __nonnull
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
A cipher algorithm.
Definition crypto.h:51
void(* auth)(void *ctx, void *auth)
Generate authentication tag.
Definition crypto.h:118
size_t ctxsize
Context size.
Definition crypto.h:55
size_t authsize
Authentication tag size.
Definition crypto.h:75
#define okx(success, file, line)
Report test result.
Definition test.h:44

References cipher_algorithm::auth, cipher_algorithm::authsize, cipher_auth(), cipher_encrypt, cipher_setiv(), cipher_setkey(), ctx, cipher_algorithm::ctxsize, is_auth_cipher(), len, memcmp(), memcpy(), NULL, okx, and test.

Referenced by cipher_okx().

◆ cipher_decrypt_okx()

void cipher_decrypt_okx ( struct cipher_test * test,
const char * file,
unsigned int line )
extern

Report a cipher decryption test result.

Parameters
testCipher test
fileTest code file
lineTest code line

Definition at line 113 of file cipher_test.c.

114 {
115 struct cipher_algorithm *cipher = test->cipher;
116 size_t len = test->len;
117 uint8_t ctx[cipher->ctxsize];
118 uint8_t plaintext[len];
119 uint8_t auth[cipher->authsize];
120
121 /* Initialise cipher */
122 okx ( cipher_setkey ( cipher, ctx, test->key, test->key_len ) == 0,
123 file, line );
124 cipher_setiv ( cipher, ctx, test->iv, test->iv_len );
125
126 /* Process additional data, if applicable */
127 if ( test->additional_len ) {
128 okx ( is_auth_cipher ( cipher ), file, line );
129 cipher_decrypt ( cipher, ctx, test->additional, NULL,
130 test->additional_len );
131 }
132
133 /* Perform decryption */
134 cipher_decrypt ( cipher, ctx, test->ciphertext, plaintext, len );
135
136 /* Compare against expected plaintext */
137 okx ( memcmp ( plaintext, test->plaintext, len ) == 0, file, line );
138
139 /* Check authentication tag */
140 okx ( cipher->authsize == test->auth_len, file, line );
141 cipher_auth ( cipher, ctx, auth );
142 okx ( memcmp ( auth, test->auth, test->auth_len ) == 0, file, line );
143
144 /* Reset initialisation vector */
145 cipher_setiv ( cipher, ctx, test->iv, test->iv_len );
146
147 /* Process additional data, if applicable */
148 if ( test->additional_len ) {
149 cipher_decrypt ( cipher, ctx, test->additional, NULL,
150 test->additional_len );
151 }
152
153 /* Perform in-place decryption */
154 memcpy ( plaintext, test->ciphertext, len );
155 cipher_decrypt ( cipher, ctx, plaintext, plaintext, len );
156
157 /* Compare against expected plaintext */
158 okx ( memcmp ( plaintext, test->plaintext, len ) == 0, file, line );
159
160 /* Check authentication tag */
161 cipher_auth ( cipher, ctx, auth );
162 okx ( memcmp ( auth, test->auth, test->auth_len ) == 0, file, line );
163}
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition crypto.h:261

References cipher_algorithm::auth, cipher_algorithm::authsize, cipher_auth(), cipher_decrypt, cipher_setiv(), cipher_setkey(), ctx, cipher_algorithm::ctxsize, is_auth_cipher(), len, memcmp(), memcpy(), NULL, okx, and test.

Referenced by cipher_okx().

◆ cipher_okx()

void cipher_okx ( struct cipher_test * test,
const char * file,
unsigned int line )
extern

Report a cipher encryption and decryption test result.

Parameters
testCipher test
fileTest code file
lineTest code line

Definition at line 172 of file cipher_test.c.

173 {
174 struct cipher_algorithm *cipher = test->cipher;
175 size_t len = test->len;
176
177 /* Sanity checks */
178 okx ( cipher->blocksize != 0, file, line );
179 okx ( ( len % cipher->blocksize ) == 0, file, line );
180 okx ( ( cipher->alignsize % cipher->blocksize ) == 0, file, line );
181
182 /* Report encryption test result */
183 cipher_encrypt_okx ( test, file, line );
184
185 /* Report decryption test result */
186 cipher_decrypt_okx ( test, file, line );
187}
size_t blocksize
Block size.
Definition crypto.h:61
size_t alignsize
Alignment size.
Definition crypto.h:73

References cipher_algorithm::alignsize, cipher_algorithm::blocksize, cipher_decrypt_okx(), cipher_encrypt_okx(), len, okx, and test.

◆ cipher_cost_encrypt()

unsigned long cipher_cost_encrypt ( struct cipher_algorithm * cipher,
size_t key_len )
extern

Calculate cipher encryption cost.

Parameters
cipherCipher algorithm
key_lenLength of key
Return values
costCost (in cycles per byte)

Definition at line 246 of file cipher_test.c.

247 {
248 return cipher_cost ( cipher, key_len, cipher_encrypt );
249}
static unsigned long cipher_cost(struct cipher_algorithm *cipher, size_t key_len, void(*op)(struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len))
Calculate cipher encryption or decryption cost.

References cipher_cost(), and cipher_encrypt.

Referenced by aes_test_exec(), des_test_exec(), and gcm_test_exec().

◆ cipher_cost_decrypt()

unsigned long cipher_cost_decrypt ( struct cipher_algorithm * cipher,
size_t key_len )
extern

Calculate cipher decryption cost.

Parameters
cipherCipher algorithm
key_lenLength of key
Return values
costCost (in cycles per byte)

Definition at line 258 of file cipher_test.c.

259 {
260 return cipher_cost ( cipher, key_len, cipher_decrypt );
261}

References cipher_cost(), and cipher_decrypt.

Referenced by aes_test_exec(), des_test_exec(), and gcm_test_exec().