blob: 88e34d1fd73e26416f0ecb49f7548b6917482fe8 [file] [log] [blame]
Bence Szépkúti700ee442020-05-26 00:33:31 +02001/*
2 * Copyright (C) 2018-2019, ARM Limited, All Rights Reserved
3 *
4 * This file is part of mbed TLS (https://tls.mbed.org)
5 */
6
itayzafrir10366702018-07-11 13:44:41 +03007#include "psa/crypto.h"
8#include <string.h>
itayzafrir10366702018-07-11 13:44:41 +03009#include <stdio.h>
Jaeden Amerodb29ab52019-02-12 16:40:27 +000010#include <stdlib.h>
itayzafrir10366702018-07-11 13:44:41 +030011
12#define ASSERT( predicate ) \
13 do \
14 { \
15 if( ! ( predicate ) ) \
16 { \
Jaeden Amerofa30c332018-12-21 18:42:18 +000017 printf( "\tassertion failed at %s:%d - '%s'\r\n", \
18 __FILE__, __LINE__, #predicate); \
itayzafrir10366702018-07-11 13:44:41 +030019 goto exit; \
20 } \
21 } while ( 0 )
22
23#define ASSERT_STATUS( actual, expected ) \
24 do \
25 { \
26 if( ( actual ) != ( expected ) ) \
27 { \
Jaeden Amerofa30c332018-12-21 18:42:18 +000028 printf( "\tassertion failed at %s:%d - " \
29 "actual:%d expected:%d\r\n", __FILE__, __LINE__, \
itayzafrir10366702018-07-11 13:44:41 +030030 (psa_status_t) actual, (psa_status_t) expected ); \
31 goto exit; \
32 } \
33 } while ( 0 )
34
itayzafrir18ac3312018-07-17 09:28:11 +030035#if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_AES_C) || \
36 !defined(MBEDTLS_CIPHER_MODE_CBC) || !defined(MBEDTLS_CIPHER_MODE_CTR) || \
37 !defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
38int main( void )
39{
Jaeden Amerofa30c332018-12-21 18:42:18 +000040 printf( "MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_AES_C and/or "
41 "MBEDTLS_CIPHER_MODE_CBC and/or MBEDTLS_CIPHER_MODE_CTR "
42 "and/or MBEDTLS_CIPHER_MODE_WITH_PADDING "
43 "not defined.\r\n" );
itayzafrir18ac3312018-07-17 09:28:11 +030044 return( 0 );
45}
46#else
47
itayzafrir10366702018-07-11 13:44:41 +030048static psa_status_t cipher_operation( psa_cipher_operation_t *operation,
49 const uint8_t * input,
50 size_t input_size,
51 size_t part_size,
52 uint8_t * output,
53 size_t output_size,
54 size_t *output_len )
55{
56 psa_status_t status;
57 size_t bytes_to_write = 0, bytes_written = 0, len = 0;
58
59 *output_len = 0;
60 while( bytes_written != input_size )
61 {
62 bytes_to_write = ( input_size - bytes_written > part_size ?
63 part_size :
64 input_size - bytes_written );
65
66 status = psa_cipher_update( operation, input + bytes_written,
67 bytes_to_write, output + *output_len,
68 output_size - *output_len, &len );
69 ASSERT_STATUS( status, PSA_SUCCESS );
70
71 bytes_written += bytes_to_write;
72 *output_len += len;
73 }
74
75 status = psa_cipher_finish( operation, output + *output_len,
76 output_size - *output_len, &len );
77 ASSERT_STATUS( status, PSA_SUCCESS );
78 *output_len += len;
79
80exit:
81 return( status );
82}
83
Gilles Peskineb0edfb52018-12-03 16:24:51 +010084static psa_status_t cipher_encrypt( psa_key_handle_t key_handle,
itayzafrir10366702018-07-11 13:44:41 +030085 psa_algorithm_t alg,
86 uint8_t * iv,
87 size_t iv_size,
88 const uint8_t * input,
89 size_t input_size,
90 size_t part_size,
91 uint8_t * output,
92 size_t output_size,
93 size_t *output_len )
94{
95 psa_status_t status;
Jaeden Amerob281f742019-02-20 10:40:20 +000096 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
itayzafrir10366702018-07-11 13:44:41 +030097 size_t iv_len = 0;
98
99 memset( &operation, 0, sizeof( operation ) );
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100100 status = psa_cipher_encrypt_setup( &operation, key_handle, alg );
itayzafrir10366702018-07-11 13:44:41 +0300101 ASSERT_STATUS( status, PSA_SUCCESS );
102
103 status = psa_cipher_generate_iv( &operation, iv, iv_size, &iv_len );
104 ASSERT_STATUS( status, PSA_SUCCESS );
105
106 status = cipher_operation( &operation, input, input_size, part_size,
107 output, output_size, output_len );
108 ASSERT_STATUS( status, PSA_SUCCESS );
109
110exit:
111 psa_cipher_abort( &operation );
112 return( status );
113}
114
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100115static psa_status_t cipher_decrypt( psa_key_handle_t key_handle,
itayzafrir10366702018-07-11 13:44:41 +0300116 psa_algorithm_t alg,
117 const uint8_t * iv,
118 size_t iv_size,
119 const uint8_t * input,
120 size_t input_size,
121 size_t part_size,
122 uint8_t * output,
123 size_t output_size,
124 size_t *output_len )
125{
126 psa_status_t status;
Jaeden Amerob281f742019-02-20 10:40:20 +0000127 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
itayzafrir10366702018-07-11 13:44:41 +0300128
129 memset( &operation, 0, sizeof( operation ) );
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100130 status = psa_cipher_decrypt_setup( &operation, key_handle, alg );
itayzafrir10366702018-07-11 13:44:41 +0300131 ASSERT_STATUS( status, PSA_SUCCESS );
132
133 status = psa_cipher_set_iv( &operation, iv, iv_size );
134 ASSERT_STATUS( status, PSA_SUCCESS );
135
136 status = cipher_operation( &operation, input, input_size, part_size,
137 output, output_size, output_len );
138 ASSERT_STATUS( status, PSA_SUCCESS );
139
140exit:
141 psa_cipher_abort( &operation );
142 return( status );
143}
144
145static psa_status_t
146cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block( void )
147{
148 enum {
149 block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( PSA_KEY_TYPE_AES ),
150 key_bits = 256,
151 part_size = block_size,
152 };
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200153 const psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING;
itayzafrir10366702018-07-11 13:44:41 +0300154
155 psa_status_t status;
Gilles Peskinedfea0a252019-04-18 13:39:40 +0200156 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100157 psa_key_handle_t key_handle = 0;
itayzafrir10366702018-07-11 13:44:41 +0300158 size_t output_len = 0;
159 uint8_t iv[block_size];
160 uint8_t input[block_size];
161 uint8_t encrypt[block_size];
162 uint8_t decrypt[block_size];
163
164 status = psa_generate_random( input, sizeof( input ) );
165 ASSERT_STATUS( status, PSA_SUCCESS );
166
Gilles Peskinedfea0a252019-04-18 13:39:40 +0200167 psa_set_key_usage_flags( &attributes,
168 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
169 psa_set_key_algorithm( &attributes, alg );
170 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200171 psa_set_key_bits( &attributes, key_bits );
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100172
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200173 status = psa_generate_key( &attributes, &key_handle );
itayzafrir10366702018-07-11 13:44:41 +0300174 ASSERT_STATUS( status, PSA_SUCCESS );
175
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100176 status = cipher_encrypt( key_handle, alg, iv, sizeof( iv ),
itayzafrir10366702018-07-11 13:44:41 +0300177 input, sizeof( input ), part_size,
178 encrypt, sizeof( encrypt ), &output_len );
179 ASSERT_STATUS( status, PSA_SUCCESS );
180
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100181 status = cipher_decrypt( key_handle, alg, iv, sizeof( iv ),
itayzafrir10366702018-07-11 13:44:41 +0300182 encrypt, output_len, part_size,
183 decrypt, sizeof( decrypt ), &output_len );
184 ASSERT_STATUS( status, PSA_SUCCESS );
185
186 status = memcmp( input, decrypt, sizeof( input ) );
187 ASSERT_STATUS( status, PSA_SUCCESS );
188
189exit:
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100190 psa_destroy_key( key_handle );
itayzafrir10366702018-07-11 13:44:41 +0300191 return( status );
192}
193
itayzafrira2d08042018-07-12 10:27:58 +0300194static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi( void )
195{
196 enum {
197 block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( PSA_KEY_TYPE_AES ),
198 key_bits = 256,
199 input_size = 100,
200 part_size = 10,
201 };
202
Gilles Peskinedaea26f2018-08-21 14:02:45 +0200203 const psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
itayzafrira2d08042018-07-12 10:27:58 +0300204
205 psa_status_t status;
Gilles Peskinedfea0a252019-04-18 13:39:40 +0200206 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100207 psa_key_handle_t key_handle = 0;
itayzafrira2d08042018-07-12 10:27:58 +0300208 size_t output_len = 0;
209 uint8_t iv[block_size], input[input_size],
210 encrypt[input_size + block_size], decrypt[input_size + block_size];
211
212 status = psa_generate_random( input, sizeof( input ) );
213 ASSERT_STATUS( status, PSA_SUCCESS );
214
Gilles Peskinedfea0a252019-04-18 13:39:40 +0200215 psa_set_key_usage_flags( &attributes,
216 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
217 psa_set_key_algorithm( &attributes, alg );
218 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200219 psa_set_key_bits( &attributes, key_bits );
itayzafrira2d08042018-07-12 10:27:58 +0300220
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200221 status = psa_generate_key( &attributes, &key_handle );
itayzafrira2d08042018-07-12 10:27:58 +0300222 ASSERT_STATUS( status, PSA_SUCCESS );
223
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100224 status = cipher_encrypt( key_handle, alg, iv, sizeof( iv ),
itayzafrira2d08042018-07-12 10:27:58 +0300225 input, sizeof( input ), part_size,
226 encrypt, sizeof( encrypt ), &output_len );
227 ASSERT_STATUS( status, PSA_SUCCESS );
228
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100229 status = cipher_decrypt( key_handle, alg, iv, sizeof( iv ),
itayzafrira2d08042018-07-12 10:27:58 +0300230 encrypt, output_len, part_size,
231 decrypt, sizeof( decrypt ), &output_len );
232 ASSERT_STATUS( status, PSA_SUCCESS );
233
234 status = memcmp( input, decrypt, sizeof( input ) );
235 ASSERT_STATUS( status, PSA_SUCCESS );
236
237exit:
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100238 psa_destroy_key( key_handle );
itayzafrira2d08042018-07-12 10:27:58 +0300239 return( status );
240}
241
itayzafrir44b09d22018-07-12 13:06:41 +0300242static psa_status_t cipher_example_encrypt_decrypt_aes_ctr_multi( void )
243{
244 enum {
245 block_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( PSA_KEY_TYPE_AES ),
246 key_bits = 256,
247 input_size = 100,
248 part_size = 10,
249 };
250 const psa_algorithm_t alg = PSA_ALG_CTR;
251
252 psa_status_t status;
Gilles Peskinedfea0a252019-04-18 13:39:40 +0200253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100254 psa_key_handle_t key_handle = 0;
itayzafrir44b09d22018-07-12 13:06:41 +0300255 size_t output_len = 0;
256 uint8_t iv[block_size], input[input_size], encrypt[input_size],
257 decrypt[input_size];
258
259 status = psa_generate_random( input, sizeof( input ) );
260 ASSERT_STATUS( status, PSA_SUCCESS );
261
Gilles Peskinedfea0a252019-04-18 13:39:40 +0200262 psa_set_key_usage_flags( &attributes,
263 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
264 psa_set_key_algorithm( &attributes, alg );
265 psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
Gilles Peskine3a4f1f82019-04-26 13:49:28 +0200266 psa_set_key_bits( &attributes, key_bits );
itayzafrir44b09d22018-07-12 13:06:41 +0300267
Gilles Peskine35ef36b2019-05-16 19:42:05 +0200268 status = psa_generate_key( &attributes, &key_handle );
itayzafrir44b09d22018-07-12 13:06:41 +0300269 ASSERT_STATUS( status, PSA_SUCCESS );
270
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100271 status = cipher_encrypt( key_handle, alg, iv, sizeof( iv ),
itayzafrir44b09d22018-07-12 13:06:41 +0300272 input, sizeof( input ), part_size,
273 encrypt, sizeof( encrypt ), &output_len );
274 ASSERT_STATUS( status, PSA_SUCCESS );
275
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100276 status = cipher_decrypt( key_handle, alg, iv, sizeof( iv ),
itayzafrir44b09d22018-07-12 13:06:41 +0300277 encrypt, output_len, part_size,
278 decrypt, sizeof( decrypt ), &output_len );
279 ASSERT_STATUS( status, PSA_SUCCESS );
280
281 status = memcmp( input, decrypt, sizeof( input ) );
282 ASSERT_STATUS( status, PSA_SUCCESS );
283
284exit:
Gilles Peskineb0edfb52018-12-03 16:24:51 +0100285 psa_destroy_key( key_handle );
itayzafrir44b09d22018-07-12 13:06:41 +0300286 return( status );
287}
288
itayzafrir10366702018-07-11 13:44:41 +0300289static void cipher_examples( void )
290{
291 psa_status_t status;
292
Jaeden Amerofa30c332018-12-21 18:42:18 +0000293 printf( "cipher encrypt/decrypt AES CBC no padding:\r\n" );
itayzafrir10366702018-07-11 13:44:41 +0300294 status = cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block( );
295 if( status == PSA_SUCCESS )
Jaeden Amerofa30c332018-12-21 18:42:18 +0000296 printf( "\tsuccess!\r\n" );
itayzafrira2d08042018-07-12 10:27:58 +0300297
Jaeden Amerofa30c332018-12-21 18:42:18 +0000298 printf( "cipher encrypt/decrypt AES CBC PKCS7 multipart:\r\n" );
itayzafrira2d08042018-07-12 10:27:58 +0300299 status = cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi( );
300 if( status == PSA_SUCCESS )
Jaeden Amerofa30c332018-12-21 18:42:18 +0000301 printf( "\tsuccess!\r\n" );
itayzafrir44b09d22018-07-12 13:06:41 +0300302
Jaeden Amerofa30c332018-12-21 18:42:18 +0000303 printf( "cipher encrypt/decrypt AES CTR multipart:\r\n" );
itayzafrir44b09d22018-07-12 13:06:41 +0300304 status = cipher_example_encrypt_decrypt_aes_ctr_multi( );
305 if( status == PSA_SUCCESS )
Jaeden Amerofa30c332018-12-21 18:42:18 +0000306 printf( "\tsuccess!\r\n" );
itayzafrir10366702018-07-11 13:44:41 +0300307}
308
Jaeden Amero44a59ab2019-02-11 13:24:47 +0000309#if defined(MBEDTLS_CHECK_PARAMS)
310#include "mbedtls/platform_util.h"
311void mbedtls_param_failed( const char *failure_condition,
312 const char *file,
313 int line )
314{
Jaeden Amerofa30c332018-12-21 18:42:18 +0000315 printf( "%s:%i: Input param failed - %s\n",
Jaeden Amero44a59ab2019-02-11 13:24:47 +0000316 file, line, failure_condition );
Jaeden Amerofa30c332018-12-21 18:42:18 +0000317 exit( EXIT_FAILURE );
Jaeden Amero44a59ab2019-02-11 13:24:47 +0000318}
319#endif
320
itayzafrira3ff8a62018-07-10 10:10:21 +0300321int main( void )
322{
itayzafrir10366702018-07-11 13:44:41 +0300323 ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
324 cipher_examples( );
325exit:
326 mbedtls_psa_crypto_free( );
itayzafrira3ff8a62018-07-10 10:10:21 +0300327 return( 0 );
328}
itayzafrir18ac3312018-07-17 09:28:11 +0300329#endif /* MBEDTLS_PSA_CRYPTO_C && MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CBC &&
330 MBEDTLS_CIPHER_MODE_CTR && MBEDTLS_CIPHER_MODE_WITH_PADDING */