blob: 6fa10dd96b3adbe54a55eb1b3c98e6f6396b25e9 [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 */