crypto_examples.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright The Mbed TLS Contributors
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "psa/crypto.h"
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #define ASSERT( predicate ) \
  22. do \
  23. { \
  24. if( ! ( predicate ) ) \
  25. { \
  26. printf( "\tassertion failed at %s:%d - '%s'\r\n", \
  27. __FILE__, __LINE__, #predicate); \
  28. goto exit; \
  29. } \
  30. } while ( 0 )
  31. #define ASSERT_STATUS( actual, expected ) \
  32. do \
  33. { \
  34. if( ( actual ) != ( expected ) ) \
  35. { \
  36. printf( "\tassertion failed at %s:%d - " \
  37. "actual:%d expected:%d\r\n", __FILE__, __LINE__, \
  38. (psa_status_t) actual, (psa_status_t) expected ); \
  39. goto exit; \
  40. } \
  41. } while ( 0 )
  42. #if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_AES_C) || \
  43. !defined(MBEDTLS_CIPHER_MODE_CBC) || !defined(MBEDTLS_CIPHER_MODE_CTR) || \
  44. !defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) || \
  45. defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
  46. int main( void )
  47. {
  48. printf( "MBEDTLS_PSA_CRYPTO_C and/or MBEDTLS_AES_C and/or "
  49. "MBEDTLS_CIPHER_MODE_CBC and/or MBEDTLS_CIPHER_MODE_CTR "
  50. "and/or MBEDTLS_CIPHER_MODE_WITH_PADDING "
  51. "not defined and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER"
  52. " defined.\r\n" );
  53. return( 0 );
  54. }
  55. #else
  56. static psa_status_t cipher_operation( psa_cipher_operation_t *operation,
  57. const uint8_t * input,
  58. size_t input_size,
  59. size_t part_size,
  60. uint8_t * output,
  61. size_t output_size,
  62. size_t *output_len )
  63. {
  64. psa_status_t status;
  65. size_t bytes_to_write = 0, bytes_written = 0, len = 0;
  66. *output_len = 0;
  67. while( bytes_written != input_size )
  68. {
  69. bytes_to_write = ( input_size - bytes_written > part_size ?
  70. part_size :
  71. input_size - bytes_written );
  72. status = psa_cipher_update( operation, input + bytes_written,
  73. bytes_to_write, output + *output_len,
  74. output_size - *output_len, &len );
  75. ASSERT_STATUS( status, PSA_SUCCESS );
  76. bytes_written += bytes_to_write;
  77. *output_len += len;
  78. }
  79. status = psa_cipher_finish( operation, output + *output_len,
  80. output_size - *output_len, &len );
  81. ASSERT_STATUS( status, PSA_SUCCESS );
  82. *output_len += len;
  83. exit:
  84. return( status );
  85. }
  86. static psa_status_t cipher_encrypt( psa_key_id_t key,
  87. psa_algorithm_t alg,
  88. uint8_t * iv,
  89. size_t iv_size,
  90. const uint8_t * input,
  91. size_t input_size,
  92. size_t part_size,
  93. uint8_t * output,
  94. size_t output_size,
  95. size_t *output_len )
  96. {
  97. psa_status_t status;
  98. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  99. size_t iv_len = 0;
  100. memset( &operation, 0, sizeof( operation ) );
  101. status = psa_cipher_encrypt_setup( &operation, key, alg );
  102. ASSERT_STATUS( status, PSA_SUCCESS );
  103. status = psa_cipher_generate_iv( &operation, iv, iv_size, &iv_len );
  104. ASSERT_STATUS( status, PSA_SUCCESS );
  105. status = cipher_operation( &operation, input, input_size, part_size,
  106. output, output_size, output_len );
  107. ASSERT_STATUS( status, PSA_SUCCESS );
  108. exit:
  109. psa_cipher_abort( &operation );
  110. return( status );
  111. }
  112. static psa_status_t cipher_decrypt( psa_key_id_t key,
  113. psa_algorithm_t alg,
  114. const uint8_t * iv,
  115. size_t iv_size,
  116. const uint8_t * input,
  117. size_t input_size,
  118. size_t part_size,
  119. uint8_t * output,
  120. size_t output_size,
  121. size_t *output_len )
  122. {
  123. psa_status_t status;
  124. psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
  125. memset( &operation, 0, sizeof( operation ) );
  126. status = psa_cipher_decrypt_setup( &operation, key, alg );
  127. ASSERT_STATUS( status, PSA_SUCCESS );
  128. status = psa_cipher_set_iv( &operation, iv, iv_size );
  129. ASSERT_STATUS( status, PSA_SUCCESS );
  130. status = cipher_operation( &operation, input, input_size, part_size,
  131. output, output_size, output_len );
  132. ASSERT_STATUS( status, PSA_SUCCESS );
  133. exit:
  134. psa_cipher_abort( &operation );
  135. return( status );
  136. }
  137. static psa_status_t
  138. cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block( void )
  139. {
  140. enum {
  141. block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( PSA_KEY_TYPE_AES ),
  142. key_bits = 256,
  143. part_size = block_size,
  144. };
  145. const psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING;
  146. psa_status_t status;
  147. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  148. psa_key_id_t key = 0;
  149. size_t output_len = 0;
  150. uint8_t iv[block_size];
  151. uint8_t input[block_size];
  152. uint8_t encrypt[block_size];
  153. uint8_t decrypt[block_size];
  154. status = psa_generate_random( input, sizeof( input ) );
  155. ASSERT_STATUS( status, PSA_SUCCESS );
  156. psa_set_key_usage_flags( &attributes,
  157. PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
  158. psa_set_key_algorithm( &attributes, alg );
  159. psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
  160. psa_set_key_bits( &attributes, key_bits );
  161. status = psa_generate_key( &attributes, &key );
  162. ASSERT_STATUS( status, PSA_SUCCESS );
  163. status = cipher_encrypt( key, alg, iv, sizeof( iv ),
  164. input, sizeof( input ), part_size,
  165. encrypt, sizeof( encrypt ), &output_len );
  166. ASSERT_STATUS( status, PSA_SUCCESS );
  167. status = cipher_decrypt( key, alg, iv, sizeof( iv ),
  168. encrypt, output_len, part_size,
  169. decrypt, sizeof( decrypt ), &output_len );
  170. ASSERT_STATUS( status, PSA_SUCCESS );
  171. status = memcmp( input, decrypt, sizeof( input ) );
  172. ASSERT_STATUS( status, PSA_SUCCESS );
  173. exit:
  174. psa_destroy_key( key );
  175. return( status );
  176. }
  177. static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi( void )
  178. {
  179. enum {
  180. block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( PSA_KEY_TYPE_AES ),
  181. key_bits = 256,
  182. input_size = 100,
  183. part_size = 10,
  184. };
  185. const psa_algorithm_t alg = PSA_ALG_CBC_PKCS7;
  186. psa_status_t status;
  187. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  188. psa_key_id_t key = 0;
  189. size_t output_len = 0;
  190. uint8_t iv[block_size], input[input_size],
  191. encrypt[input_size + block_size], decrypt[input_size + block_size];
  192. status = psa_generate_random( input, sizeof( input ) );
  193. ASSERT_STATUS( status, PSA_SUCCESS );
  194. psa_set_key_usage_flags( &attributes,
  195. PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
  196. psa_set_key_algorithm( &attributes, alg );
  197. psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
  198. psa_set_key_bits( &attributes, key_bits );
  199. status = psa_generate_key( &attributes, &key );
  200. ASSERT_STATUS( status, PSA_SUCCESS );
  201. status = cipher_encrypt( key, alg, iv, sizeof( iv ),
  202. input, sizeof( input ), part_size,
  203. encrypt, sizeof( encrypt ), &output_len );
  204. ASSERT_STATUS( status, PSA_SUCCESS );
  205. status = cipher_decrypt( key, alg, iv, sizeof( iv ),
  206. encrypt, output_len, part_size,
  207. decrypt, sizeof( decrypt ), &output_len );
  208. ASSERT_STATUS( status, PSA_SUCCESS );
  209. status = memcmp( input, decrypt, sizeof( input ) );
  210. ASSERT_STATUS( status, PSA_SUCCESS );
  211. exit:
  212. psa_destroy_key( key );
  213. return( status );
  214. }
  215. static psa_status_t cipher_example_encrypt_decrypt_aes_ctr_multi( void )
  216. {
  217. enum {
  218. block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( PSA_KEY_TYPE_AES ),
  219. key_bits = 256,
  220. input_size = 100,
  221. part_size = 10,
  222. };
  223. const psa_algorithm_t alg = PSA_ALG_CTR;
  224. psa_status_t status;
  225. psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
  226. psa_key_id_t key = 0;
  227. size_t output_len = 0;
  228. uint8_t iv[block_size], input[input_size], encrypt[input_size],
  229. decrypt[input_size];
  230. status = psa_generate_random( input, sizeof( input ) );
  231. ASSERT_STATUS( status, PSA_SUCCESS );
  232. psa_set_key_usage_flags( &attributes,
  233. PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
  234. psa_set_key_algorithm( &attributes, alg );
  235. psa_set_key_type( &attributes, PSA_KEY_TYPE_AES );
  236. psa_set_key_bits( &attributes, key_bits );
  237. status = psa_generate_key( &attributes, &key );
  238. ASSERT_STATUS( status, PSA_SUCCESS );
  239. status = cipher_encrypt( key, alg, iv, sizeof( iv ),
  240. input, sizeof( input ), part_size,
  241. encrypt, sizeof( encrypt ), &output_len );
  242. ASSERT_STATUS( status, PSA_SUCCESS );
  243. status = cipher_decrypt( key, alg, iv, sizeof( iv ),
  244. encrypt, output_len, part_size,
  245. decrypt, sizeof( decrypt ), &output_len );
  246. ASSERT_STATUS( status, PSA_SUCCESS );
  247. status = memcmp( input, decrypt, sizeof( input ) );
  248. ASSERT_STATUS( status, PSA_SUCCESS );
  249. exit:
  250. psa_destroy_key( key );
  251. return( status );
  252. }
  253. static void cipher_examples( void )
  254. {
  255. psa_status_t status;
  256. printf( "cipher encrypt/decrypt AES CBC no padding:\r\n" );
  257. status = cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block( );
  258. if( status == PSA_SUCCESS )
  259. printf( "\tsuccess!\r\n" );
  260. printf( "cipher encrypt/decrypt AES CBC PKCS7 multipart:\r\n" );
  261. status = cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi( );
  262. if( status == PSA_SUCCESS )
  263. printf( "\tsuccess!\r\n" );
  264. printf( "cipher encrypt/decrypt AES CTR multipart:\r\n" );
  265. status = cipher_example_encrypt_decrypt_aes_ctr_multi( );
  266. if( status == PSA_SUCCESS )
  267. printf( "\tsuccess!\r\n" );
  268. }
  269. int main( void )
  270. {
  271. ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
  272. cipher_examples( );
  273. exit:
  274. mbedtls_psa_crypto_free( );
  275. return( 0 );
  276. }
  277. #endif /* MBEDTLS_PSA_CRYPTO_C && MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CBC &&
  278. MBEDTLS_CIPHER_MODE_CTR && MBEDTLS_CIPHER_MODE_WITH_PADDING */