blob: 21802d147776576b7dd330292ecefe15083749e2 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
2#include "psa/crypto.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_PSA_CRYPTO_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void init_deinit()
12{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010013 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +010014 int i;
15 for( i = 0; i <= 1; i++ )
16 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010017 status = psa_crypto_init( );
18 TEST_ASSERT( status == PSA_SUCCESS );
19 status = psa_crypto_init( );
20 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +010021 mbedtls_psa_crypto_free( );
22 }
23}
24/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010025
26/* BEGIN_CASE */
27void import( char *hex, int type, int expected_status )
28{
29 int slot = 1;
30 psa_status_t status;
31 unsigned char *data = NULL;
32 size_t data_size;
33
34 data_size = strlen( hex ) / 2;
35 data = mbedtls_calloc( 1, data_size );
36 TEST_ASSERT( data != NULL );
37 data_size = unhexify( data, hex );
38 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
39
40 status = psa_import_key( slot, type, data, data_size );
41 TEST_ASSERT( status == (psa_status_t) expected_status );
42 if( status == PSA_SUCCESS )
43 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
44
45exit:
46 mbedtls_free( data );
47 mbedtls_psa_crypto_free( );
48}
49/* END_CASE */
50
51/* BEGIN_CASE */
52void import_export( char *hex, int type_arg,
53 int expected_bits,
54 int export_size_delta,
55 int expected_export_status,
56 int canonical_input )
57{
58 int slot = 1;
59 int slot2 = slot + 1;
60 psa_key_type_t type = type_arg;
61 psa_status_t status;
62 unsigned char *data = NULL;
63 unsigned char *exported = NULL;
64 unsigned char *reexported = NULL;
65 size_t data_size;
66 size_t export_size;
67 size_t exported_length;
68 size_t reexported_length;
69 psa_key_type_t got_type;
70 size_t got_bits;
71
72 data_size = strlen( hex ) / 2;
73 data = mbedtls_calloc( 1, data_size );
74 TEST_ASSERT( data != NULL );
75 data_size = unhexify( data, hex );
76 export_size = (ssize_t) data_size + export_size_delta;
77 exported = mbedtls_calloc( 1, export_size );
78 TEST_ASSERT( exported != NULL );
79 if( ! canonical_input )
80 {
81 reexported = mbedtls_calloc( 1, export_size );
82 TEST_ASSERT( reexported != NULL );
83 }
84 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
85
86 /* Import the key */
87 TEST_ASSERT( psa_import_key( slot, type,
88 data, data_size ) == PSA_SUCCESS );
89
90 /* Test the key information */
91 TEST_ASSERT( psa_get_key_information( slot,
92 &got_type, &got_bits ) ==
93 PSA_SUCCESS );
94 TEST_ASSERT( got_type == type );
95 TEST_ASSERT( got_bits == (size_t) expected_bits );
96
97 /* Export the key */
98 status = psa_export_key( slot,
99 exported, export_size,
100 &exported_length );
101 TEST_ASSERT( status == (psa_status_t) expected_export_status );
102 if( status != PSA_SUCCESS )
103 goto destroy;
104
105 if( canonical_input )
106 {
107 TEST_ASSERT( exported_length == data_size );
108 TEST_ASSERT( memcmp( exported, data, data_size ) == 0 );
109 }
110 else
111 {
112 TEST_ASSERT( psa_import_key( slot2, type,
113 exported, export_size ) ==
114 PSA_SUCCESS );
115 TEST_ASSERT( psa_export_key( slot2,
116 reexported, export_size,
117 &reexported_length ) ==
118 PSA_SUCCESS );
119 TEST_ASSERT( reexported_length == exported_length );
120 TEST_ASSERT( memcmp( reexported, exported,
121 exported_length ) == 0 );
122 }
123
124destroy:
125 /* Destroy the key */
126 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
127 TEST_ASSERT( psa_get_key_information(
128 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
129
130exit:
131 mbedtls_free( data );
132 mbedtls_psa_crypto_free( );
133}
134/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100135
136/* BEGIN_CASE */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100137void hash_finish( int alg_arg, char *input_hex, char *hash_hex )
138{
139 psa_algorithm_t alg = alg_arg;
140 unsigned char *input = NULL;
141 size_t input_size;
142 unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE];
143 size_t expected_hash_length;
144 unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE];
145 size_t actual_hash_length;
146 psa_hash_operation_t operation;
147
148 input_size = strlen( input_hex ) / 2;
149 input = mbedtls_calloc( 1, input_size );
150 TEST_ASSERT( input != NULL );
151 input_size = unhexify( input, input_hex );
152 expected_hash_length = unhexify( expected_hash, hash_hex );
153
154 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
155
156 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
157 TEST_ASSERT( psa_hash_update( &operation,
158 input, input_size ) == PSA_SUCCESS );
159 TEST_ASSERT( psa_hash_finish( &operation,
160 actual_hash, sizeof( actual_hash ),
161 &actual_hash_length ) == PSA_SUCCESS );
162 TEST_ASSERT( actual_hash_length == expected_hash_length );
163 TEST_ASSERT( memcmp( expected_hash, actual_hash,
164 expected_hash_length ) == 0 );
165
166exit:
167 mbedtls_free( input );
168 mbedtls_psa_crypto_free( );
169}
170/* END_CASE */
171
172/* BEGIN_CASE */
173void hash_verify( int alg_arg, char *input_hex, char *hash_hex )
174{
175 psa_algorithm_t alg = alg_arg;
176 unsigned char *input = NULL;
177 size_t input_size;
178 unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE];
179 size_t expected_hash_length;
180 psa_hash_operation_t operation;
181
182 input_size = strlen( input_hex ) / 2;
183 input = mbedtls_calloc( 1, input_size );
184 TEST_ASSERT( input != NULL );
185 input_size = unhexify( input, input_hex );
186 expected_hash_length = unhexify( expected_hash, hash_hex );
187
188 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
189
190 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
191 TEST_ASSERT( psa_hash_update( &operation,
192 input, input_size ) == PSA_SUCCESS );
193 TEST_ASSERT( psa_hash_verify( &operation,
194 expected_hash,
195 expected_hash_length ) == PSA_SUCCESS );
196
197exit:
198 mbedtls_free( input );
199 mbedtls_psa_crypto_free( );
200}
201/* END_CASE */
202
203/* BEGIN_CASE */
Gilles Peskine0189e752018-02-03 23:57:22 +0100204void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg )
205{
206 psa_key_type_t type = type_arg;
207 psa_algorithm_t alg = alg_arg;
208 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg);
209 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
210exit:
211 ;
212}
213/* END_CASE */
214
215/* BEGIN_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100216void sign_deterministic( int key_type_arg, char *key_hex,
217 int alg_arg, char *input_hex, char *output_hex )
218{
219 int slot = 1;
220 psa_key_type_t key_type = key_type_arg;
221 psa_algorithm_t alg = alg_arg;
222 unsigned char *key_data = NULL;
223 size_t key_size;
Gilles Peskine0189e752018-02-03 23:57:22 +0100224 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +0100225 unsigned char *input_data = NULL;
226 size_t input_size;
227 unsigned char *output_data = NULL;
228 size_t output_size;
Gilles Peskine0189e752018-02-03 23:57:22 +0100229 unsigned char *signature = NULL;
230 size_t signature_size;
Gilles Peskine93aa0332018-02-03 23:58:03 +0100231 size_t signature_length = 0xdeadbeef;
Gilles Peskine20035e32018-02-03 22:44:14 +0100232
233 key_data = mbedtls_calloc( 1, strlen( key_hex ) / 2 );
234 TEST_ASSERT( key_data != NULL );
235 key_size = unhexify( key_data, key_hex );
236 input_data = mbedtls_calloc( 1, strlen( input_hex ) / 2 );
237 TEST_ASSERT( input_data != NULL );
238 input_size = unhexify( input_data, input_hex );
239 output_data = mbedtls_calloc( 1, strlen( output_hex ) / 2 );
240 TEST_ASSERT( output_data != NULL );
241 output_size = unhexify( output_data, output_hex );
242
243 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
244
245 TEST_ASSERT( psa_import_key( slot, key_type,
246 key_data, key_size ) == PSA_SUCCESS );
Gilles Peskine0189e752018-02-03 23:57:22 +0100247 TEST_ASSERT( psa_get_key_information( slot,
248 NULL,
249 &key_bits ) == PSA_SUCCESS );
250
251 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits );
252 TEST_ASSERT( signature_size != 0 );
253 signature = mbedtls_calloc( 1, signature_size );
254 TEST_ASSERT( signature != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100255
256 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
257 input_data, input_size,
258 NULL, 0,
Gilles Peskine0189e752018-02-03 23:57:22 +0100259 signature, signature_size,
Gilles Peskine20035e32018-02-03 22:44:14 +0100260 &signature_length ) == PSA_SUCCESS );
261 TEST_ASSERT( signature_length == output_size );
262 TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
263
264exit:
265 psa_destroy_key( slot );
266 mbedtls_free( key_data );
267 mbedtls_free( input_data );
268 mbedtls_free( output_data );
Gilles Peskine0189e752018-02-03 23:57:22 +0100269 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +0100270 mbedtls_psa_crypto_free( );
271}
272/* END_CASE */
273
274/* BEGIN_CASE */
275void sign_fail( int key_type_arg, char *key_hex,
276 int alg_arg, char *input_hex,
277 int signature_size, int expected_status_arg )
278{
279 int slot = 1;
280 psa_key_type_t key_type = key_type_arg;
281 psa_algorithm_t alg = alg_arg;
282 unsigned char *key_data = NULL;
283 size_t key_size;
284 unsigned char *input_data = NULL;
285 size_t input_size;
286 psa_status_t actual_status;
287 psa_status_t expected_status = expected_status_arg;
288 unsigned char *signature;
Gilles Peskine93aa0332018-02-03 23:58:03 +0100289 size_t signature_length = 0xdeadbeef;
Gilles Peskine20035e32018-02-03 22:44:14 +0100290
291 key_data = mbedtls_calloc( 1, strlen( key_hex ) / 2 );
292 TEST_ASSERT( key_data != NULL );
293 key_size = unhexify( key_data, key_hex );
294 input_data = mbedtls_calloc( 1, strlen( input_hex ) / 2 );
295 TEST_ASSERT( input_data != NULL );
296 input_size = unhexify( input_data, input_hex );
297 signature = mbedtls_calloc( 1, signature_size );
298 TEST_ASSERT( signature != NULL );
299
300 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
301
302 TEST_ASSERT( psa_import_key( slot, key_type,
303 key_data, key_size ) == PSA_SUCCESS );
304
305 actual_status = psa_asymmetric_sign( slot, alg,
306 input_data, input_size,
307 NULL, 0,
308 signature, signature_size,
309 &signature_length );
310 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +0100311 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +0100312
313exit:
314 psa_destroy_key( slot );
315 mbedtls_free( key_data );
316 mbedtls_free( input_data );
317 mbedtls_free( signature );
318 mbedtls_psa_crypto_free( );
319}
320/* END_CASE */