iPXE
cipher_test.c File Reference

Cipher self-tests. More...

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ipxe/crypto.h>
#include <ipxe/profile.h>
#include <ipxe/test.h>
#include "cipher_test.h"

Go to the source code of this file.

Macros

#define PROFILE_COUNT   16
 Number of sample iterations for profiling.

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.
void cipher_key_fail_okx (struct cipher_test *test, const char *file, unsigned int line)
 Report a cipher key failure test result.
void cipher_iv_fail_okx (struct cipher_test *test, const char *file, unsigned int line)
 Report a cipher IV failure test result.
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.
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.c.

Macro Definition Documentation

◆ PROFILE_COUNT

#define PROFILE_COUNT   16

Number of sample iterations for profiling.

Definition at line 45 of file cipher_test.c.

Referenced by cipher_cost(), digest_cost(), librm_test_exec(), memcpy_test_speed(), and tcpip_random_okx().

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 )

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 okx ( cipher_setiv ( cipher, ctx, test->iv, test->iv_len ) == 0,
66 file, line );
67
68 /* Process additional data, if applicable */
69 if ( test->additional_len ) {
70 okx ( is_auth_cipher ( cipher ), file, line );
71 cipher_encrypt ( cipher, ctx, test->additional, NULL,
72 test->additional_len );
73 }
74
75 /* Perform encryption */
76 cipher_encrypt ( cipher, ctx, test->plaintext, ciphertext, len );
77
78 /* Compare against expected ciphertext */
79 okx ( memcmp ( ciphertext, test->ciphertext, len ) == 0, file, line );
80
81 /* Check authentication tag */
82 okx ( cipher->authsize == test->auth_len, file, line );
83 cipher_auth ( cipher, ctx, auth );
84 okx ( memcmp ( auth, test->auth, test->auth_len ) == 0, file, line );
85
86 /* Reset initialisation vector */
87 okx ( cipher_setiv ( cipher, ctx, test->iv, test->iv_len ) == 0,
88 file, line );
89
90 /* Process additional data, if applicable */
91 if ( test->additional_len ) {
92 cipher_encrypt ( cipher, ctx, test->additional, NULL,
93 test->additional_len );
94 }
95
96 /* Perform in-place encryption */
97 memcpy ( ciphertext, test->plaintext, len );
98 cipher_encrypt ( cipher, ctx, ciphertext, ciphertext, len );
99
100 /* Compare against expected ciphertext */
101 okx ( memcmp ( ciphertext, test->ciphertext, len ) == 0, file, line );
102
103 /* Check authentication tag */
104 cipher_auth ( cipher, ctx, auth );
105 okx ( memcmp ( auth, test->auth, test->auth_len ) == 0, file, line );
106}
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
struct golan_eq_context ctx
Definition CIB_PRM.h:0
unsigned char uint8_t
Definition stdint.h:10
ring len
Length.
Definition dwmac.h:226
static int test
Definition epic100.c:73
static int cipher_setkey(struct cipher_algorithm *cipher, void *ctx, const void *key, size_t keylen)
Definition crypto.h:310
static int cipher_setiv(struct cipher_algorithm *cipher, void *ctx, const void *iv, size_t ivlen)
Definition crypto.h:316
static int is_auth_cipher(struct cipher_algorithm *cipher)
Definition crypto.h:357
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition crypto.h:326
static void cipher_auth(struct cipher_algorithm *cipher, void *ctx, void *auth)
Definition crypto.h:342
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:58
size_t ctxsize
Context size.
Definition crypto.h:62
size_t authsize
Authentication tag size.
Definition crypto.h:82
void(* auth)(struct cipher_algorithm *cipher, void *ctx, void *auth)
Generate authentication tag.
Definition crypto.h:135
#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 )

Report a cipher decryption test result.

Parameters
testCipher test
fileTest code file
lineTest code line

Definition at line 115 of file cipher_test.c.

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

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 )

Report a cipher encryption and decryption test result.

Parameters
testCipher test
fileTest code file
lineTest code line

Definition at line 176 of file cipher_test.c.

177 {
178 struct cipher_algorithm *cipher = test->cipher;
179 size_t len = test->len;
180
181 /* Sanity checks */
182 okx ( cipher->blocksize != 0, file, line );
183 okx ( ( len % cipher->blocksize ) == 0, file, line );
184 okx ( ( cipher->alignsize % cipher->blocksize ) == 0, file, line );
185 okx ( cipher->confidential, file, line );
186
187 /* Report encryption test result */
188 cipher_encrypt_okx ( test, file, line );
189
190 /* Report decryption test result */
191 cipher_decrypt_okx ( test, file, line );
192}
void cipher_decrypt_okx(struct cipher_test *test, const char *file, unsigned int line)
Report a cipher decryption test result.
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
size_t blocksize
Block size.
Definition crypto.h:68
int confidential
Cipher is capable of providing confidentiality.
Definition crypto.h:84
size_t alignsize
Alignment size.
Definition crypto.h:80

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

◆ cipher_key_fail_okx()

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

Report a cipher key failure test result.

Parameters
testCipher test
fileTest code file
lineTest code line

Definition at line 201 of file cipher_test.c.

202 {
203 struct cipher_algorithm *cipher = test->cipher;
204 uint8_t ctx[cipher->ctxsize];
205
206 /* Initialise cipher */
207 okx ( cipher_setkey ( cipher, ctx, test->key, test->key_len ) != 0,
208 file, line );
209}

References cipher_setkey(), ctx, cipher_algorithm::ctxsize, okx, and test.

◆ cipher_iv_fail_okx()

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

Report a cipher IV failure test result.

Parameters
testCipher test
fileTest code file
lineTest code line

Definition at line 218 of file cipher_test.c.

219 {
220 struct cipher_algorithm *cipher = test->cipher;
221 uint8_t ctx[cipher->ctxsize];
222
223 /* Initialise cipher */
224 okx ( cipher_setkey ( cipher, ctx, test->key, test->key_len ) == 0,
225 file, line );
226 okx ( cipher_setiv ( cipher, ctx, test->iv, test->iv_len ) != 0,
227 file, line );
228}

References cipher_setiv(), cipher_setkey(), ctx, cipher_algorithm::ctxsize, okx, and test.

◆ cipher_cost()

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) )
static

Calculate cipher encryption or decryption cost.

Parameters
cipherCipher algorithm
key_lenLength of key
opEncryption or decryption operation
Return values
costCost (in cycles per byte)

Definition at line 239 of file cipher_test.c.

241 {
242 static uint8_t random[8192]; /* Too large for stack */
243 uint8_t key[key_len];
244 uint8_t iv[cipher->blocksize];
245 uint8_t ctx[cipher->ctxsize];
246 struct profiler profiler;
247 unsigned long cost;
248 unsigned int i;
249 int rc;
250
251 /* Fill buffer with pseudo-random data */
252 srand ( 0x1234568 );
253 for ( i = 0 ; i < sizeof ( random ) ; i++ )
254 random[i] = rand();
255 for ( i = 0 ; i < sizeof ( key ) ; i++ )
256 key[i] = rand();
257 for ( i = 0 ; i < sizeof ( iv ) ; i++ )
258 iv[i] = rand();
259
260 /* Initialise cipher */
261 rc = cipher_setkey ( cipher, ctx, key, key_len );
262 assert ( rc == 0 );
263 rc = cipher_setiv ( cipher, ctx, iv, sizeof ( iv ) );
264 assert ( rc == 0 );
265
266 /* Profile cipher operation */
267 memset ( &profiler, 0, sizeof ( profiler ) );
268 for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
270 op ( cipher, ctx, random, random, sizeof ( random ) );
272 }
273
274 /* Round to nearest whole number of cycles per byte */
275 cost = ( ( profile_mean ( &profiler ) + ( sizeof ( random ) / 2 ) ) /
276 sizeof ( random ) );
277
278 return cost;
279}
union @162305117151260234136356364136041353210355154177 key
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:61
#define PROFILE_COUNT
Number of sample iterations for profiling.
Definition cipher_test.c:45
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition profile.h:174
static void profile_start(struct profiler *profiler)
Start profiling.
Definition profile.h:161
void * memset(void *dest, int character, size_t len) __nonnull
static uint16_t struct vmbus_xfer_pages_operations * op
Definition netvsc.h:327
unsigned long profile_mean(struct profiler *profiler)
Get mean sample value.
Definition profile.c:242
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition random.c:32
static void srand(unsigned int seed)
Definition stdlib.h:65
static int rand(void)
Definition stdlib.h:61
uint32_t cost
Root path cost.
Definition stp.h:17
A data structure for storing profiling information.
Definition profile.h:27
u8 iv[16]
Initialization vector.
Definition wpa.h:33

References assert, cipher_algorithm::blocksize, cipher_setiv(), cipher_setkey(), cost, ctx, cipher_algorithm::ctxsize, iv, key, len, memset(), op, PROFILE_COUNT, profile_mean(), profile_start(), profile_stop(), rand(), random(), rc, srand(), and src.

Referenced by cipher_cost_decrypt(), and cipher_cost_encrypt().

◆ cipher_cost_encrypt()

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

Calculate cipher encryption cost.

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

Definition at line 288 of file cipher_test.c.

289 {
290 return cipher_cost ( cipher, key_len, cipher_encrypt );
291}
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 )

Calculate cipher decryption cost.

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

Definition at line 300 of file cipher_test.c.

301 {
302 return cipher_cost ( cipher, key_len, cipher_decrypt );
303}

References cipher_cost(), and cipher_decrypt.

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