iPXE
cipher_test.h
Go to the documentation of this file.
1 #ifndef _CIPHER_TEST_H
2 #define _CIPHER_TEST_H
3 
4 /** @file
5  *
6  * Cipher self-tests
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stdint.h>
13 #include <ipxe/crypto.h>
14 #include <ipxe/test.h>
15 
16 /** A cipher test */
17 struct cipher_test {
18  /** Cipher algorithm */
20  /** Key */
21  const void *key;
22  /** Length of key */
23  size_t key_len;
24  /** Initialisation vector */
25  const void *iv;
26  /** Length of initialisation vector */
27  size_t iv_len;
28  /** Additional data */
29  const void *additional;
30  /** Length of additional data */
32  /** Plaintext */
33  const void *plaintext;
34  /** Ciphertext */
35  const void *ciphertext;
36  /** Length of text */
37  size_t len;
38  /** Authentication tag */
39  const void *auth;
40  /** Length of authentication tag */
41  size_t auth_len;
42 };
43 
44 /** Define inline key */
45 #define KEY(...) { __VA_ARGS__ }
46 
47 /** Define inline initialisation vector */
48 #define IV(...) { __VA_ARGS__ }
49 
50 /** Define inline additional data */
51 #define ADDITIONAL(...) { __VA_ARGS__ }
52 
53 /** Define inline plaintext data */
54 #define PLAINTEXT(...) { __VA_ARGS__ }
55 
56 /** Define inline ciphertext data */
57 #define CIPHERTEXT(...) { __VA_ARGS__ }
58 
59 /** Define inline authentication tag */
60 #define AUTH(...) { __VA_ARGS__ }
61 
62 /**
63  * Define a cipher test
64  *
65  * @v name Test name
66  * @v CIPHER Cipher algorithm
67  * @v KEY Key
68  * @v IV Initialisation vector
69  * @v ADDITIONAL Additional data
70  * @v PLAINTEXT Plaintext
71  * @v CIPHERTEXT Ciphertext
72  * @v AUTH Authentication tag
73  * @ret test Cipher test
74  */
75 #define CIPHER_TEST( name, CIPHER, KEY, IV, ADDITIONAL, PLAINTEXT, \
76  CIPHERTEXT, AUTH ) \
77  static const uint8_t name ## _key [] = KEY; \
78  static const uint8_t name ## _iv [] = IV; \
79  static const uint8_t name ## _additional [] = ADDITIONAL; \
80  static const uint8_t name ## _plaintext [] = PLAINTEXT; \
81  static const uint8_t name ## _ciphertext \
82  [ sizeof ( name ## _plaintext ) ] = CIPHERTEXT; \
83  static const uint8_t name ## _auth [] = AUTH; \
84  static struct cipher_test name = { \
85  .cipher = CIPHER, \
86  .key = name ## _key, \
87  .key_len = sizeof ( name ## _key ), \
88  .iv = name ## _iv, \
89  .iv_len = sizeof ( name ## _iv ), \
90  .additional = name ## _additional, \
91  .additional_len = sizeof ( name ## _additional ), \
92  .plaintext = name ## _plaintext, \
93  .ciphertext = name ## _ciphertext, \
94  .len = sizeof ( name ## _plaintext ), \
95  .auth = name ## _auth, \
96  .auth_len = sizeof ( name ## _auth ), \
97  }
98 
99 extern void cipher_encrypt_okx ( struct cipher_test *test, const char *file,
100  unsigned int line );
101 extern void cipher_decrypt_okx ( struct cipher_test *test, const char *file,
102  unsigned int line );
103 extern void cipher_okx ( struct cipher_test *test, const char *file,
104  unsigned int line );
105 extern unsigned long cipher_cost_encrypt ( struct cipher_algorithm *cipher,
106  size_t key_len );
107 extern unsigned long cipher_cost_decrypt ( struct cipher_algorithm *cipher,
108  size_t key_len );
109 
110 /**
111  * Report a cipher encryption test result
112  *
113  * @v test Cipher test
114  */
115 #define cipher_encrypt_ok( test ) \
116  cipher_encrypt_okx ( test, __FILE__, __LINE__ )
117 
118 /**
119  * Report a cipher decryption test result
120  *
121  * @v test Cipher test
122  */
123 #define cipher_decrypt_ok( test ) \
124  cipher_decrypt_okx ( test, __FILE__, __LINE__ )
125 
126 /**
127  * Report a cipher encryption and decryption test result
128  *
129  * @v test Cipher test
130  */
131 #define cipher_ok( test ) \
132  cipher_okx ( test, __FILE__, __LINE__ )
133 
134 #endif /* _CIPHER_TEST_H */
unsigned long cipher_cost_decrypt(struct cipher_algorithm *cipher, size_t key_len)
Calculate cipher decryption cost.
Definition: cipher_test.c:258
const void * additional
Additional data.
Definition: cipher_test.h:29
Self-test infrastructure.
Cryptographic API.
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
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
size_t key_len
Length of key.
Definition: cipher_test.h:23
static void const void size_t key_len
Definition: crypto.h:285
struct cipher_algorithm * cipher
Cipher algorithm.
Definition: cipher_test.h:19
A cipher test.
Definition: cipher_test.h:17
size_t additional_len
Length of additional data.
Definition: cipher_test.h:31
const void * auth
Authentication tag.
Definition: cipher_test.h:39
const void * ciphertext
Ciphertext.
Definition: cipher_test.h:35
size_t auth_len
Length of authentication tag.
Definition: cipher_test.h:41
const void * key
Key.
Definition: cipher_test.h:21
const void * iv
Initialisation vector.
Definition: cipher_test.h:25
void cipher_okx(struct cipher_test *test, const char *file, unsigned int line)
Report a cipher encryption and decryption test result.
Definition: cipher_test.c:172
size_t len
Length of text.
Definition: cipher_test.h:37
const void * plaintext
Plaintext.
Definition: cipher_test.h:33
size_t iv_len
Length of initialisation vector.
Definition: cipher_test.h:27
unsigned long cipher_cost_encrypt(struct cipher_algorithm *cipher, size_t key_len)
Calculate cipher encryption cost.
Definition: cipher_test.c:246
A cipher algorithm.
Definition: crypto.h:49
static int test
Definition: epic100.c:73
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)