blob: 93817948cbbd1ddd2e1440e90ee50e5531a36736 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/* BEGIN_HEADER */
2#include "psa/crypto.h"
Gilles Peskine8c9def32018-02-08 10:02:12 +01003
4#include "mbedtls/md.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +01005/* END_HEADER */
6
7/* BEGIN_DEPENDENCIES
8 * depends_on:MBEDTLS_PSA_CRYPTO_C
9 * END_DEPENDENCIES
10 */
11
12/* BEGIN_CASE */
13void init_deinit()
14{
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010015 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +010016 int i;
17 for( i = 0; i <= 1; i++ )
18 {
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010019 status = psa_crypto_init( );
20 TEST_ASSERT( status == PSA_SUCCESS );
21 status = psa_crypto_init( );
22 TEST_ASSERT( status == PSA_SUCCESS );
Gilles Peskinee59236f2018-01-27 23:32:46 +010023 mbedtls_psa_crypto_free( );
24 }
25}
26/* END_CASE */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010027
28/* BEGIN_CASE */
29void import( char *hex, int type, int expected_status )
30{
31 int slot = 1;
32 psa_status_t status;
33 unsigned char *data = NULL;
34 size_t data_size;
35
Gilles Peskine40f68b92018-03-07 16:43:36 +010036 data = unhexify_alloc( hex, &data_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010037 TEST_ASSERT( data != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010038 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
Gilles Peskine40f68b92018-03-07 16:43:36 +010072 data = unhexify_alloc( hex, &data_size );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010073 TEST_ASSERT( data != NULL );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010074 export_size = (ssize_t) data_size + export_size_delta;
75 exported = mbedtls_calloc( 1, export_size );
76 TEST_ASSERT( exported != NULL );
77 if( ! canonical_input )
78 {
79 reexported = mbedtls_calloc( 1, export_size );
80 TEST_ASSERT( reexported != NULL );
81 }
82 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
83
84 /* Import the key */
85 TEST_ASSERT( psa_import_key( slot, type,
86 data, data_size ) == PSA_SUCCESS );
87
88 /* Test the key information */
89 TEST_ASSERT( psa_get_key_information( slot,
90 &got_type, &got_bits ) ==
91 PSA_SUCCESS );
92 TEST_ASSERT( got_type == type );
93 TEST_ASSERT( got_bits == (size_t) expected_bits );
94
95 /* Export the key */
96 status = psa_export_key( slot,
97 exported, export_size,
98 &exported_length );
99 TEST_ASSERT( status == (psa_status_t) expected_export_status );
100 if( status != PSA_SUCCESS )
101 goto destroy;
102
103 if( canonical_input )
104 {
105 TEST_ASSERT( exported_length == data_size );
106 TEST_ASSERT( memcmp( exported, data, data_size ) == 0 );
107 }
108 else
109 {
110 TEST_ASSERT( psa_import_key( slot2, type,
111 exported, export_size ) ==
112 PSA_SUCCESS );
113 TEST_ASSERT( psa_export_key( slot2,
114 reexported, export_size,
115 &reexported_length ) ==
116 PSA_SUCCESS );
117 TEST_ASSERT( reexported_length == exported_length );
118 TEST_ASSERT( memcmp( reexported, exported,
119 exported_length ) == 0 );
120 }
121
122destroy:
123 /* Destroy the key */
124 TEST_ASSERT( psa_destroy_key( slot ) == PSA_SUCCESS );
125 TEST_ASSERT( psa_get_key_information(
126 slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT );
127
128exit:
129 mbedtls_free( data );
130 mbedtls_psa_crypto_free( );
131}
132/* END_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100133
134/* BEGIN_CASE */
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100135void hash_finish( int alg_arg, char *input_hex, char *hash_hex )
136{
137 psa_algorithm_t alg = alg_arg;
138 unsigned char *input = NULL;
139 size_t input_size;
140 unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE];
141 size_t expected_hash_length;
142 unsigned char actual_hash[MBEDTLS_MD_MAX_SIZE];
143 size_t actual_hash_length;
144 psa_hash_operation_t operation;
145
Gilles Peskine40f68b92018-03-07 16:43:36 +0100146 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100147 TEST_ASSERT( input != NULL );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100148 expected_hash_length = unhexify( expected_hash, hash_hex );
149
150 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
151
152 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
153 TEST_ASSERT( psa_hash_update( &operation,
154 input, input_size ) == PSA_SUCCESS );
155 TEST_ASSERT( psa_hash_finish( &operation,
156 actual_hash, sizeof( actual_hash ),
157 &actual_hash_length ) == PSA_SUCCESS );
158 TEST_ASSERT( actual_hash_length == expected_hash_length );
159 TEST_ASSERT( memcmp( expected_hash, actual_hash,
160 expected_hash_length ) == 0 );
161
162exit:
163 mbedtls_free( input );
164 mbedtls_psa_crypto_free( );
165}
166/* END_CASE */
167
168/* BEGIN_CASE */
169void hash_verify( int alg_arg, char *input_hex, char *hash_hex )
170{
171 psa_algorithm_t alg = alg_arg;
172 unsigned char *input = NULL;
173 size_t input_size;
174 unsigned char expected_hash[MBEDTLS_MD_MAX_SIZE];
175 size_t expected_hash_length;
176 psa_hash_operation_t operation;
177
Gilles Peskine40f68b92018-03-07 16:43:36 +0100178 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100179 TEST_ASSERT( input != NULL );
Gilles Peskine9ef733f2018-02-07 21:05:37 +0100180 expected_hash_length = unhexify( expected_hash, hash_hex );
181
182 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
183
184 TEST_ASSERT( psa_hash_start( &operation, alg ) == PSA_SUCCESS );
185 TEST_ASSERT( psa_hash_update( &operation,
186 input, input_size ) == PSA_SUCCESS );
187 TEST_ASSERT( psa_hash_verify( &operation,
188 expected_hash,
189 expected_hash_length ) == PSA_SUCCESS );
190
191exit:
192 mbedtls_free( input );
193 mbedtls_psa_crypto_free( );
194}
195/* END_CASE */
196
197/* BEGIN_CASE */
Gilles Peskine8c9def32018-02-08 10:02:12 +0100198void mac_verify( int key_type_arg, char *key_hex,
199 int alg_arg, char *iv_hex,
200 char *input_hex, char *mac_hex )
201{
202 int key_slot = 1;
203 psa_key_type_t key_type = key_type_arg;
204 psa_algorithm_t alg = alg_arg;
205 unsigned char *key = NULL;
206 size_t key_size;
207 unsigned char *iv = NULL;
208 size_t iv_size;
209 unsigned char *input = NULL;
210 size_t input_size;
211 unsigned char *expected_mac = NULL;
212 size_t expected_mac_size;
213 psa_mac_operation_t operation;
214
Gilles Peskine40f68b92018-03-07 16:43:36 +0100215 key = unhexify_alloc( key_hex, &key_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100216 TEST_ASSERT( key != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100217 if( iv_hex[0] != 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +0100218 {
Gilles Peskine40f68b92018-03-07 16:43:36 +0100219 iv = unhexify_alloc( iv_hex, &iv_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100220 TEST_ASSERT( iv != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100221 }
Gilles Peskine40f68b92018-03-07 16:43:36 +0100222 input = unhexify_alloc( input_hex, &input_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100223 TEST_ASSERT( input != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100224 expected_mac = unhexify_alloc( mac_hex, &expected_mac_size );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100225 TEST_ASSERT( expected_mac != NULL );
Gilles Peskine8c9def32018-02-08 10:02:12 +0100226
227 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
228
229 TEST_ASSERT( psa_import_key( key_slot, key_type,
230 key, key_size ) == PSA_SUCCESS );
231 // TODO: support IV
232 TEST_ASSERT( psa_mac_start( &operation, key_slot, alg ) == PSA_SUCCESS );
233 TEST_ASSERT( psa_destroy_key( key_slot ) == PSA_SUCCESS );
234 TEST_ASSERT( psa_mac_update( &operation,
235 input, input_size ) == PSA_SUCCESS );
236 TEST_ASSERT( psa_mac_verify( &operation,
237 expected_mac,
238 expected_mac_size ) == PSA_SUCCESS );
239
240exit:
241 mbedtls_free( key );
242 mbedtls_free( iv );
243 mbedtls_free( input );
244 mbedtls_free( expected_mac );
245 psa_destroy_key( key_slot );
246 mbedtls_psa_crypto_free( );
247}
248/* END_CASE */
249
250/* BEGIN_CASE */
Gilles Peskine0189e752018-02-03 23:57:22 +0100251void signature_size( int type_arg, int bits, int alg_arg, int expected_size_arg )
252{
253 psa_key_type_t type = type_arg;
254 psa_algorithm_t alg = alg_arg;
255 size_t actual_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE(type, bits, alg);
256 TEST_ASSERT( actual_size == (size_t) expected_size_arg );
257exit:
258 ;
259}
260/* END_CASE */
261
262/* BEGIN_CASE */
Gilles Peskine20035e32018-02-03 22:44:14 +0100263void sign_deterministic( int key_type_arg, char *key_hex,
264 int alg_arg, char *input_hex, char *output_hex )
265{
266 int slot = 1;
267 psa_key_type_t key_type = key_type_arg;
268 psa_algorithm_t alg = alg_arg;
269 unsigned char *key_data = NULL;
270 size_t key_size;
Gilles Peskine0189e752018-02-03 23:57:22 +0100271 size_t key_bits;
Gilles Peskine20035e32018-02-03 22:44:14 +0100272 unsigned char *input_data = NULL;
273 size_t input_size;
274 unsigned char *output_data = NULL;
275 size_t output_size;
Gilles Peskine0189e752018-02-03 23:57:22 +0100276 unsigned char *signature = NULL;
277 size_t signature_size;
Gilles Peskine93aa0332018-02-03 23:58:03 +0100278 size_t signature_length = 0xdeadbeef;
Gilles Peskine20035e32018-02-03 22:44:14 +0100279
Gilles Peskine40f68b92018-03-07 16:43:36 +0100280 key_data = unhexify_alloc( key_hex, &key_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100281 TEST_ASSERT( key_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100282 input_data = unhexify_alloc( input_hex, &input_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100283 TEST_ASSERT( input_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100284 output_data = unhexify_alloc( output_hex, &output_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100285 TEST_ASSERT( output_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100286
287 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
288
289 TEST_ASSERT( psa_import_key( slot, key_type,
290 key_data, key_size ) == PSA_SUCCESS );
Gilles Peskine0189e752018-02-03 23:57:22 +0100291 TEST_ASSERT( psa_get_key_information( slot,
292 NULL,
293 &key_bits ) == PSA_SUCCESS );
294
295 signature_size = PSA_ASYMMETRIC_SIGN_OUTPUT_SIZE( key_type, alg, key_bits );
296 TEST_ASSERT( signature_size != 0 );
297 signature = mbedtls_calloc( 1, signature_size );
298 TEST_ASSERT( signature != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100299
300 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
301 input_data, input_size,
302 NULL, 0,
Gilles Peskine0189e752018-02-03 23:57:22 +0100303 signature, signature_size,
Gilles Peskine20035e32018-02-03 22:44:14 +0100304 &signature_length ) == PSA_SUCCESS );
305 TEST_ASSERT( signature_length == output_size );
306 TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
307
308exit:
309 psa_destroy_key( slot );
310 mbedtls_free( key_data );
311 mbedtls_free( input_data );
312 mbedtls_free( output_data );
Gilles Peskine0189e752018-02-03 23:57:22 +0100313 mbedtls_free( signature );
Gilles Peskine20035e32018-02-03 22:44:14 +0100314 mbedtls_psa_crypto_free( );
315}
316/* END_CASE */
317
318/* BEGIN_CASE */
319void sign_fail( int key_type_arg, char *key_hex,
320 int alg_arg, char *input_hex,
321 int signature_size, int expected_status_arg )
322{
323 int slot = 1;
324 psa_key_type_t key_type = key_type_arg;
325 psa_algorithm_t alg = alg_arg;
326 unsigned char *key_data = NULL;
327 size_t key_size;
328 unsigned char *input_data = NULL;
329 size_t input_size;
330 psa_status_t actual_status;
331 psa_status_t expected_status = expected_status_arg;
Gilles Peskine40f68b92018-03-07 16:43:36 +0100332 unsigned char *signature = NULL;
Gilles Peskine93aa0332018-02-03 23:58:03 +0100333 size_t signature_length = 0xdeadbeef;
Gilles Peskine20035e32018-02-03 22:44:14 +0100334
Gilles Peskine40f68b92018-03-07 16:43:36 +0100335 key_data = unhexify_alloc( key_hex, &key_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100336 TEST_ASSERT( key_data != NULL );
Gilles Peskine40f68b92018-03-07 16:43:36 +0100337 input_data = unhexify_alloc( input_hex, &input_size );
Gilles Peskine20035e32018-02-03 22:44:14 +0100338 TEST_ASSERT( input_data != NULL );
Gilles Peskine20035e32018-02-03 22:44:14 +0100339 signature = mbedtls_calloc( 1, signature_size );
340 TEST_ASSERT( signature != NULL );
341
342 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
343
344 TEST_ASSERT( psa_import_key( slot, key_type,
345 key_data, key_size ) == PSA_SUCCESS );
346
347 actual_status = psa_asymmetric_sign( slot, alg,
348 input_data, input_size,
349 NULL, 0,
350 signature, signature_size,
351 &signature_length );
352 TEST_ASSERT( actual_status == expected_status );
Gilles Peskine93aa0332018-02-03 23:58:03 +0100353 TEST_ASSERT( signature_length == 0 );
Gilles Peskine20035e32018-02-03 22:44:14 +0100354
355exit:
356 psa_destroy_key( slot );
357 mbedtls_free( key_data );
358 mbedtls_free( input_data );
359 mbedtls_free( signature );
360 mbedtls_psa_crypto_free( );
361}
362/* END_CASE */