blob: 35515706d486f2ef90281c1d8a217be124b7cd31 [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 */
137void sign_deterministic( int key_type_arg, char *key_hex,
138 int alg_arg, char *input_hex, char *output_hex )
139{
140 int slot = 1;
141 psa_key_type_t key_type = key_type_arg;
142 psa_algorithm_t alg = alg_arg;
143 unsigned char *key_data = NULL;
144 size_t key_size;
145 unsigned char *input_data = NULL;
146 size_t input_size;
147 unsigned char *output_data = NULL;
148 size_t output_size;
149 unsigned char signature[512];
150 size_t signature_length;
151
152 key_data = mbedtls_calloc( 1, strlen( key_hex ) / 2 );
153 TEST_ASSERT( key_data != NULL );
154 key_size = unhexify( key_data, key_hex );
155 input_data = mbedtls_calloc( 1, strlen( input_hex ) / 2 );
156 TEST_ASSERT( input_data != NULL );
157 input_size = unhexify( input_data, input_hex );
158 output_data = mbedtls_calloc( 1, strlen( output_hex ) / 2 );
159 TEST_ASSERT( output_data != NULL );
160 output_size = unhexify( output_data, output_hex );
161
162 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
163
164 TEST_ASSERT( psa_import_key( slot, key_type,
165 key_data, key_size ) == PSA_SUCCESS );
166
167 TEST_ASSERT( psa_asymmetric_sign( slot, alg,
168 input_data, input_size,
169 NULL, 0,
170 signature, sizeof( signature ),
171 &signature_length ) == PSA_SUCCESS );
172 TEST_ASSERT( signature_length == output_size );
173 TEST_ASSERT( memcmp( signature, output_data, output_size ) == 0 );
174
175exit:
176 psa_destroy_key( slot );
177 mbedtls_free( key_data );
178 mbedtls_free( input_data );
179 mbedtls_free( output_data );
180 mbedtls_psa_crypto_free( );
181}
182/* END_CASE */
183
184/* BEGIN_CASE */
185void sign_fail( int key_type_arg, char *key_hex,
186 int alg_arg, char *input_hex,
187 int signature_size, int expected_status_arg )
188{
189 int slot = 1;
190 psa_key_type_t key_type = key_type_arg;
191 psa_algorithm_t alg = alg_arg;
192 unsigned char *key_data = NULL;
193 size_t key_size;
194 unsigned char *input_data = NULL;
195 size_t input_size;
196 psa_status_t actual_status;
197 psa_status_t expected_status = expected_status_arg;
198 unsigned char *signature;
199 size_t signature_length;
200
201 key_data = mbedtls_calloc( 1, strlen( key_hex ) / 2 );
202 TEST_ASSERT( key_data != NULL );
203 key_size = unhexify( key_data, key_hex );
204 input_data = mbedtls_calloc( 1, strlen( input_hex ) / 2 );
205 TEST_ASSERT( input_data != NULL );
206 input_size = unhexify( input_data, input_hex );
207 signature = mbedtls_calloc( 1, signature_size );
208 TEST_ASSERT( signature != NULL );
209
210 TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
211
212 TEST_ASSERT( psa_import_key( slot, key_type,
213 key_data, key_size ) == PSA_SUCCESS );
214
215 actual_status = psa_asymmetric_sign( slot, alg,
216 input_data, input_size,
217 NULL, 0,
218 signature, signature_size,
219 &signature_length );
220 TEST_ASSERT( actual_status == expected_status );
221
222exit:
223 psa_destroy_key( slot );
224 mbedtls_free( key_data );
225 mbedtls_free( input_data );
226 mbedtls_free( signature );
227 mbedtls_psa_crypto_free( );
228}
229/* END_CASE */