iPXE
Data Structures | Macros | Functions
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(...)   { __VA_ARGS__ }
 Define inline key. More...
 
#define IV(...)   { __VA_ARGS__ }
 Define inline initialisation vector. More...
 
#define PLAINTEXT(...)   { __VA_ARGS__ }
 Define inline plaintext data. More...
 
#define CIPHERTEXT(...)   { __VA_ARGS__ }
 Define inline ciphertext data. More...
 
#define CIPHER_TEST(name, CIPHER, KEY, IV, PLAINTEXT, CIPHERTEXT)
 Define a cipher test. More...
 
#define cipher_encrypt_ok(test)   cipher_encrypt_okx ( test, __FILE__, __LINE__ )
 Report a cipher encryption test result. More...
 
#define cipher_decrypt_ok(test)   cipher_decrypt_okx ( test, __FILE__, __LINE__ )
 Report a cipher decryption test result. More...
 
#define cipher_ok(test)   cipher_okx ( test, __FILE__, __LINE__ )
 Report a cipher encryption and decryption test result. 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...
 
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.h.

Macro Definition Documentation

◆ KEY

#define KEY (   ...)    { __VA_ARGS__ }

Define inline key.

Definition at line 37 of file cipher_test.h.

◆ IV

#define IV (   ...)    { __VA_ARGS__ }

Define inline initialisation vector.

Definition at line 40 of file cipher_test.h.

◆ PLAINTEXT

#define PLAINTEXT (   ...)    { __VA_ARGS__ }

Define inline plaintext data.

Definition at line 43 of file cipher_test.h.

◆ CIPHERTEXT

#define CIPHERTEXT (   ...)    { __VA_ARGS__ }

Define inline ciphertext data.

Definition at line 46 of file cipher_test.h.

◆ CIPHER_TEST

#define CIPHER_TEST (   name,
  CIPHER,
  KEY,
  IV,
  PLAINTEXT,
  CIPHERTEXT 
)
Value:
static const uint8_t name ## _key [] = KEY; \
static const uint8_t name ## _iv [] = IV; \
static const uint8_t name ## _plaintext [] = PLAINTEXT; \
static const uint8_t name ## _ciphertext \
[ sizeof ( name ## _plaintext ) ] = CIPHERTEXT; \
static struct cipher_test name = { \
.cipher = CIPHER, \
.key = name ## _key, \
.key_len = sizeof ( name ## _key ), \
.iv = name ## _iv, \
.iv_len = sizeof ( name ## _iv ), \
.plaintext = name ## _plaintext, \
.ciphertext = name ## _ciphertext, \
.len = sizeof ( name ## _plaintext ), \
}
#define IV(...)
Define inline initialisation vector.
Definition: cipher_test.h:40
const char * name
Definition: ath9k_hw.c:1984
#define CIPHERTEXT(...)
Define inline ciphertext data.
Definition: cipher_test.h:46
#define PLAINTEXT(...)
Define inline plaintext data.
Definition: cipher_test.h:43
u8 iv[16]
Initialization vector.
Definition: wpa.h:60
#define KEY(...)
Define inline key.
Definition: cipher_test.h:37
A cipher test.
Definition: cipher_test.h:17
unsigned char uint8_t
Definition: stdint.h:10
size_t iv_len
Length of initialisation vector.
Definition: cipher_test.h:27

Define a cipher test.

Parameters
nameTest name
CIPHERCipher algorithm
KEYKey
IVInitialisation vector
PLAINTEXTPlaintext
CIPHERTEXTCiphertext
Return values
testCipher test

Definition at line 59 of file cipher_test.h.

◆ cipher_encrypt_ok

#define cipher_encrypt_ok (   test)    cipher_encrypt_okx ( test, __FILE__, __LINE__ )

Report a cipher encryption test result.

Parameters
testCipher test

Definition at line 92 of file cipher_test.h.

◆ cipher_decrypt_ok

#define cipher_decrypt_ok (   test)    cipher_decrypt_okx ( test, __FILE__, __LINE__ )

Report a cipher decryption test result.

Parameters
testCipher test

Definition at line 100 of file cipher_test.h.

◆ cipher_ok

#define cipher_ok (   test)    cipher_okx ( test, __FILE__, __LINE__ )

Report a cipher encryption and decryption test result.

Parameters
testCipher test

Definition at line 108 of file cipher_test.h.

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 
61  /* Initialise cipher */
62  okx ( cipher_setkey ( cipher, ctx, test->key, test->key_len ) == 0,
63  file, line );
64  cipher_setiv ( cipher, ctx, test->iv );
65 
66  /* Perform encryption */
67  cipher_encrypt ( cipher, ctx, test->plaintext, ciphertext, len );
68 
69  /* Compare against expected ciphertext */
70  okx ( memcmp ( ciphertext, test->ciphertext, len ) == 0, file, line );
71 }
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:202
#define okx(success, file, line)
Report test result.
Definition: test.h:44
unsigned char uint8_t
Definition: stdint.h:10
size_t ctxsize
Context size.
Definition: crypto.h:52
uint32_t len
Length.
Definition: ena.h:14
A cipher algorithm.
Definition: crypto.h:48
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:113
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:187
static void cipher_setiv(struct cipher_algorithm *cipher, void *ctx, const void *iv)
Definition: crypto.h:192

References cipher_encrypt, cipher_setiv(), cipher_setkey(), ctx, cipher_algorithm::ctxsize, len, memcmp(), 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 80 of file cipher_test.c.

81  {
82  struct cipher_algorithm *cipher = test->cipher;
83  size_t len = test->len;
84  uint8_t ctx[cipher->ctxsize];
85  uint8_t plaintext[len];
86 
87  /* Initialise cipher */
88  okx ( cipher_setkey ( cipher, ctx, test->key, test->key_len ) == 0,
89  file, line );
90  cipher_setiv ( cipher, ctx, test->iv );
91 
92  /* Perform encryption */
93  cipher_decrypt ( cipher, ctx, test->ciphertext, plaintext, len );
94 
95  /* Compare against expected plaintext */
96  okx ( memcmp ( plaintext, test->plaintext, len ) == 0, file, line );
97 }
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
#define okx(success, file, line)
Report test result.
Definition: test.h:44
unsigned char uint8_t
Definition: stdint.h:10
size_t ctxsize
Context size.
Definition: crypto.h:52
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:212
uint32_t len
Length.
Definition: ena.h:14
A cipher algorithm.
Definition: crypto.h:48
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:113
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:187
static void cipher_setiv(struct cipher_algorithm *cipher, void *ctx, const void *iv)
Definition: crypto.h:192

References cipher_decrypt, cipher_setiv(), cipher_setkey(), ctx, cipher_algorithm::ctxsize, len, memcmp(), 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 106 of file cipher_test.c.

107  {
108 
109  cipher_encrypt_okx ( test, file, line );
110  cipher_decrypt_okx ( test, file, line );
111 }
void cipher_decrypt_okx(struct cipher_test *test, const char *file, unsigned int line)
Report a cipher decryption test result.
Definition: cipher_test.c:80
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_decrypt_okx(), cipher_encrypt_okx(), and test.

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

171  {
172  return cipher_cost ( cipher, key_len, cipher_encrypt );
173 }
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:122
#define cipher_encrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:202

References cipher_cost(), and cipher_encrypt.

Referenced by aes_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 182 of file cipher_test.c.

183  {
184  return cipher_cost ( cipher, key_len, cipher_decrypt );
185 }
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:122
#define cipher_decrypt(cipher, ctx, src, dst, len)
Definition: crypto.h:212

References cipher_cost(), and cipher_decrypt.

Referenced by aes_test_exec().