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 */
19 	struct cipher_algorithm *cipher;
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 	/** Plaintext */
29 	const void *plaintext;
30 	/** Ciphertext */
31 	const void *ciphertext;
32 	/** Length of text */
33 	size_t len;
34 };
35 
36 /** Define inline key */
37 #define KEY(...) { __VA_ARGS__ }
38 
39 /** Define inline initialisation vector */
40 #define IV(...) { __VA_ARGS__ }
41 
42 /** Define inline plaintext data */
43 #define PLAINTEXT(...) { __VA_ARGS__ }
44 
45 /** Define inline ciphertext data */
46 #define CIPHERTEXT(...) { __VA_ARGS__ }
47 
48 /**
49  * Define a cipher test
50  *
51  * @v name		Test name
52  * @v CIPHER		Cipher algorithm
53  * @v KEY		Key
54  * @v IV		Initialisation vector
55  * @v PLAINTEXT		Plaintext
56  * @v CIPHERTEXT	Ciphertext
57  * @ret test		Cipher test
58  */
59 #define CIPHER_TEST( name, CIPHER, KEY, IV, PLAINTEXT, CIPHERTEXT )	\
60 	static const uint8_t name ## _key [] = KEY;			\
61 	static const uint8_t name ## _iv [] = IV;			\
62 	static const uint8_t name ## _plaintext [] = PLAINTEXT;		\
63 	static const uint8_t name ## _ciphertext			\
64 		[ sizeof ( name ## _plaintext ) ] = CIPHERTEXT;		\
65 	static struct cipher_test name = {				\
66 		.cipher = CIPHER,					\
67 		.key = name ## _key,					\
68 		.key_len = sizeof ( name ## _key ),			\
69 		.iv = name ## _iv,					\
70 		.iv_len = sizeof ( name ## _iv ),			\
71 		.plaintext = name ## _plaintext,			\
72 		.ciphertext = name ## _ciphertext,			\
73 		.len = sizeof ( name ## _plaintext ),			\
74 	}
75 
76 extern void cipher_encrypt_okx ( struct cipher_test *test, const char *file,
77 				 unsigned int line );
78 extern void cipher_decrypt_okx ( struct cipher_test *test, const char *file,
79 				 unsigned int line );
80 extern void cipher_okx ( struct cipher_test *test, const char *file,
81 			 unsigned int line );
82 extern unsigned long cipher_cost_encrypt ( struct cipher_algorithm *cipher,
83 					   size_t key_len );
84 extern unsigned long cipher_cost_decrypt ( struct cipher_algorithm *cipher,
85 					   size_t key_len );
86 
87 /**
88  * Report a cipher encryption test result
89  *
90  * @v test		Cipher test
91  */
92 #define cipher_encrypt_ok( test ) \
93 	cipher_encrypt_okx ( test, __FILE__, __LINE__ )
94 
95 /**
96  * Report a cipher decryption test result
97  *
98  * @v test		Cipher test
99  */
100 #define cipher_decrypt_ok( test ) \
101 	cipher_decrypt_okx ( test, __FILE__, __LINE__ )
102 
103 /**
104  * Report a cipher encryption and decryption test result
105  *
106  * @v test		Cipher test
107  */
108 #define cipher_ok( test ) \
109 	cipher_okx ( test, __FILE__, __LINE__ )
110 
111 #endif /* _CIPHER_TEST_H */
112