summaryrefslogtreecommitdiffstats
path: root/src/tests/cipher_test.h
blob: 519d12e8df000d9426b883918bf02e315b2c4346 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef _CIPHER_TEST_H
#define _CIPHER_TEST_H

/** @file
 *
 * Cipher self-tests
 *
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stdint.h>
#include <ipxe/crypto.h>
#include <ipxe/test.h>

/** A cipher test */
struct cipher_test {
	/** Cipher algorithm */
	struct cipher_algorithm *cipher;
	/** Key */
	const void *key;
	/** Length of key */
	size_t key_len;
	/** Initialisation vector */
	const void *iv;
	/** Length of initialisation vector */
	size_t iv_len;
	/** Additional data */
	const void *additional;
	/** Length of additional data */
	size_t additional_len;
	/** Plaintext */
	const void *plaintext;
	/** Ciphertext */
	const void *ciphertext;
	/** Length of text */
	size_t len;
	/** Authentication tag */
	const void *auth;
	/** Length of authentication tag */
	size_t auth_len;
};

/** Define inline key */
#define KEY(...) { __VA_ARGS__ }

/** Define inline initialisation vector */
#define IV(...) { __VA_ARGS__ }

/** Define inline additional data */
#define ADDITIONAL(...) { __VA_ARGS__ }

/** Define inline plaintext data */
#define PLAINTEXT(...) { __VA_ARGS__ }

/** Define inline ciphertext data */
#define CIPHERTEXT(...) { __VA_ARGS__ }

/** Define inline authentication tag */
#define AUTH(...) { __VA_ARGS__ }

/**
 * Define a cipher test
 *
 * @v name		Test name
 * @v CIPHER		Cipher algorithm
 * @v KEY		Key
 * @v IV		Initialisation vector
 * @v ADDITIONAL	Additional data
 * @v PLAINTEXT		Plaintext
 * @v CIPHERTEXT	Ciphertext
 * @v AUTH		Authentication tag
 * @ret test		Cipher test
 */
#define CIPHER_TEST( name, CIPHER, KEY, IV, ADDITIONAL, PLAINTEXT,	\
		     CIPHERTEXT, AUTH )					\
	static const uint8_t name ## _key [] = KEY;			\
	static const uint8_t name ## _iv [] = IV;			\
	static const uint8_t name ## _additional [] = ADDITIONAL;	\
	static const uint8_t name ## _plaintext [] = PLAINTEXT;		\
	static const uint8_t name ## _ciphertext			\
		[ sizeof ( name ## _plaintext ) ] = CIPHERTEXT;		\
	static const uint8_t name ## _auth [] = AUTH;			\
	static struct cipher_test name = {				\
		.cipher = CIPHER,					\
		.key = name ## _key,					\
		.key_len = sizeof ( name ## _key ),			\
		.iv = name ## _iv,					\
		.iv_len = sizeof ( name ## _iv ),			\
		.additional = name ## _additional,			\
		.additional_len = sizeof ( name ## _additional ),	\
		.plaintext = name ## _plaintext,			\
		.ciphertext = name ## _ciphertext,			\
		.len = sizeof ( name ## _plaintext ),			\
		.auth = name ## _auth,					\
		.auth_len = sizeof ( name ## _auth ),			\
	}

extern void cipher_encrypt_okx ( struct cipher_test *test, const char *file,
				 unsigned int line );
extern void cipher_decrypt_okx ( struct cipher_test *test, const char *file,
				 unsigned int line );
extern void cipher_okx ( struct cipher_test *test, const char *file,
			 unsigned int line );
extern unsigned long cipher_cost_encrypt ( struct cipher_algorithm *cipher,
					   size_t key_len );
extern unsigned long cipher_cost_decrypt ( struct cipher_algorithm *cipher,
					   size_t key_len );

/**
 * Report a cipher encryption test result
 *
 * @v test		Cipher test
 */
#define cipher_encrypt_ok( test ) \
	cipher_encrypt_okx ( test, __FILE__, __LINE__ )

/**
 * Report a cipher decryption test result
 *
 * @v test		Cipher test
 */
#define cipher_decrypt_ok( test ) \
	cipher_decrypt_okx ( test, __FILE__, __LINE__ )

/**
 * Report a cipher encryption and decryption test result
 *
 * @v test		Cipher test
 */
#define cipher_ok( test ) \
	cipher_okx ( test, __FILE__, __LINE__ )

#endif /* _CIPHER_TEST_H */