iPXE
Macros | Functions
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. More...
 

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. More...
 
void cipher_decrypt_okx (struct cipher_test *test, const char *file, unsigned int line)
 Report a cipher decryption test result. More...
 
void cipher_okx (struct cipher_test *test, const char *file, unsigned int line)
 Report a cipher encryption and decryption test result. More...
 
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. More...
 
unsigned long cipher_cost_encrypt (struct cipher_algorithm *cipher, size_t key_len)
 Calculate cipher encryption cost. More...
 
unsigned long cipher_cost_decrypt (struct cipher_algorithm *cipher, size_t key_len)
 Calculate cipher decryption cost. More...
 

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.

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  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 }
static void void * auth
Definition: crypto.h:264
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:248
size_t authsize
Authentication tag size.
Definition: crypto.h:73
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define okx(success, file, line)
Report test result.
Definition: test.h:44
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
unsigned char uint8_t
Definition: stdint.h:10
size_t ctxsize
Context size.
Definition: crypto.h:53
uint32_t len
Length.
Definition: ena.h:14
A cipher algorithm.
Definition: crypto.h:49
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static int test
Definition: epic100.c:73

References auth, cipher_algorithm::authsize, cipher_encrypt, ctx, cipher_algorithm::ctxsize, 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 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 }
static void void * auth
Definition: crypto.h:264
size_t authsize
Authentication tag size.
Definition: crypto.h:73
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define okx(success, file, line)
Report test result.
Definition: test.h:44
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
unsigned char uint8_t
Definition: stdint.h:10
size_t ctxsize
Context size.
Definition: crypto.h:53
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:258
uint32_t len
Length.
Definition: ena.h:14
A cipher algorithm.
Definition: crypto.h:49
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:114
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
static int test
Definition: epic100.c:73

References auth, cipher_algorithm::authsize, cipher_decrypt, ctx, cipher_algorithm::ctxsize, 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 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:59
size_t alignsize
Alignment size.
Definition: crypto.h:71
void cipher_decrypt_okx(struct cipher_test *test, const char *file, unsigned int line)
Report a cipher decryption test result.
Definition: cipher_test.c:113
#define okx(success, file, line)
Report test result.
Definition: test.h:44
uint32_t len
Length.
Definition: ena.h:14
A cipher algorithm.
Definition: crypto.h:49
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

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

◆ cipher_cost()

static unsigned long cipher_cost ( struct cipher_algorithm cipher,
size_t  key_len,
void(*)(struct cipher_algorithm *cipher, void *ctx, const void *src, void *dst, size_t len op 
)
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 198 of file cipher_test.c.

200  {
201  static uint8_t random[8192]; /* Too large for stack */
203  uint8_t iv[cipher->blocksize];
204  uint8_t ctx[cipher->ctxsize];
205  struct profiler profiler;
206  unsigned long cost;
207  unsigned int i;
208  int rc;
209 
210  /* Fill buffer with pseudo-random data */
211  srand ( 0x1234568 );
212  for ( i = 0 ; i < sizeof ( random ) ; i++ )
213  random[i] = rand();
214  for ( i = 0 ; i < sizeof ( key ) ; i++ )
215  key[i] = rand();
216  for ( i = 0 ; i < sizeof ( iv ) ; i++ )
217  iv[i] = rand();
218 
219  /* Initialise cipher */
220  rc = cipher_setkey ( cipher, ctx, key, key_len );
221  assert ( rc == 0 );
222  cipher_setiv ( cipher, ctx, iv, sizeof ( iv ) );
223 
224  /* Profile cipher operation */
225  memset ( &profiler, 0, sizeof ( profiler ) );
226  for ( i = 0 ; i < PROFILE_COUNT ; i++ ) {
227  profile_start ( &profiler );
228  op ( cipher, ctx, random, random, sizeof ( random ) );
229  profile_stop ( &profiler );
230  }
231 
232  /* Round to nearest whole number of cycles per byte */
233  cost = ( ( profile_mean ( &profiler ) + ( sizeof ( random ) / 2 ) ) /
234  sizeof ( random ) );
235 
236  return cost;
237 }
unsigned long profile_mean(struct profiler *profiler)
Get mean sample value.
Definition: profile.c:241
size_t blocksize
Block size.
Definition: crypto.h:59
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A data structure for storing profiling information.
Definition: profile.h:26
static void profile_stop(struct profiler *profiler)
Stop profiling.
Definition: profile.h:171
static void const void size_t key_len
Definition: crypto.h:285
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static int rand(void)
Definition: stdlib.h:59
static void srand(unsigned int seed)
Definition: stdlib.h:63
static void profile_start(struct profiler *profiler)
Start profiling.
Definition: profile.h:158
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
long int random(void)
Generate a pseudo-random number between 0 and 2147483647L or 2147483562?
Definition: random.c:31
unsigned char uint8_t
Definition: stdint.h:10
size_t ctxsize
Context size.
Definition: crypto.h:53
uint32_t cost
Root path cost.
Definition: stp.h:28
#define PROFILE_COUNT
Number of sample iterations for profiling.
Definition: cipher_test.c:45
static uint16_t struct vmbus_xfer_pages_operations * op
Definition: netvsc.h:327
static void const void * iv
Definition: crypto.h:238
union @382 key
Sense key.
Definition: crypto.h:284
void * memset(void *dest, int character, size_t len) __nonnull

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

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 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.
Definition: cipher_test.c:198
static void const void size_t key_len
Definition: crypto.h:285
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:248

References cipher_cost(), cipher_encrypt, and key_len.

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 258 of file cipher_test.c.

259  {
260  return cipher_cost ( cipher, key_len, cipher_decrypt );
261 }
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.
Definition: cipher_test.c:198
static void const void size_t key_len
Definition: crypto.h:285
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:258

References cipher_cost(), cipher_decrypt, and key_len.

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