| Bence Szépkúti | 700ee44 | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 1 | /* | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 2 | *  Copyright The Mbed TLS Contributors | 
| Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 3 | *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later | 
| Bence Szépkúti | 700ee44 | 2020-05-26 00:33:31 +0200 | [diff] [blame] | 4 | */ | 
|  | 5 |  | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 6 | #include "psa/crypto.h" | 
|  | 7 | #include <string.h> | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 8 | #include <stdio.h> | 
| Jaeden Amero | db29ab5 | 2019-02-12 16:40:27 +0000 | [diff] [blame] | 9 | #include <stdlib.h> | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 10 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 11 | #define ASSERT(predicate)                                                   \ | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 12 | do                                                                        \ | 
|  | 13 | {                                                                         \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 14 | if (!(predicate))                                                 \ | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 15 | {                                                                     \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 16 | printf("\tassertion failed at %s:%d - '%s'\r\n",         \ | 
|  | 17 | __FILE__, __LINE__, #predicate);                  \ | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 18 | goto exit;                                                        \ | 
|  | 19 | }                                                                     \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 20 | } while (0) | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 21 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 22 | #define ASSERT_STATUS(actual, expected)                                     \ | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 23 | do                                                                        \ | 
|  | 24 | {                                                                         \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 25 | if ((actual) != (expected))                                      \ | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 26 | {                                                                     \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 27 | printf("\tassertion failed at %s:%d - "                  \ | 
|  | 28 | "actual:%d expected:%d\r\n", __FILE__, __LINE__,  \ | 
|  | 29 | (psa_status_t) actual, (psa_status_t) expected); \ | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 30 | goto exit;                                                        \ | 
|  | 31 | }                                                                     \ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 32 | } while (0) | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 33 |  | 
| itayzafrir | 18ac331 | 2018-07-17 09:28:11 +0300 | [diff] [blame] | 34 | #if !defined(MBEDTLS_PSA_CRYPTO_C) || !defined(MBEDTLS_AES_C) || \ | 
|  | 35 | !defined(MBEDTLS_CIPHER_MODE_CBC) || !defined(MBEDTLS_CIPHER_MODE_CTR) || \ | 
| Ronald Cron | adc2ff2 | 2020-09-16 16:49:27 +0200 | [diff] [blame] | 36 | !defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) || \ | 
|  | 37 | defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | int main(void) | 
| itayzafrir | 18ac331 | 2018-07-17 09:28:11 +0300 | [diff] [blame] | 39 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 40 | 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 and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER" | 
|  | 44 | " defined.\r\n"); | 
|  | 45 | return 0; | 
| itayzafrir | 18ac331 | 2018-07-17 09:28:11 +0300 | [diff] [blame] | 46 | } | 
|  | 47 | #else | 
|  | 48 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | static psa_status_t cipher_operation(psa_cipher_operation_t *operation, | 
|  | 50 | const uint8_t *input, | 
|  | 51 | size_t input_size, | 
|  | 52 | size_t part_size, | 
|  | 53 | uint8_t *output, | 
|  | 54 | size_t output_size, | 
|  | 55 | size_t *output_len) | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 56 | { | 
|  | 57 | psa_status_t status; | 
|  | 58 | size_t bytes_to_write = 0, bytes_written = 0, len = 0; | 
|  | 59 |  | 
|  | 60 | *output_len = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | while (bytes_written != input_size) { | 
|  | 62 | bytes_to_write = (input_size - bytes_written > part_size ? | 
|  | 63 | part_size : | 
|  | 64 | input_size - bytes_written); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 65 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 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); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 70 |  | 
|  | 71 | bytes_written += bytes_to_write; | 
|  | 72 | *output_len += len; | 
|  | 73 | } | 
|  | 74 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 75 | status = psa_cipher_finish(operation, output + *output_len, | 
|  | 76 | output_size - *output_len, &len); | 
|  | 77 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 78 | *output_len += len; | 
|  | 79 |  | 
|  | 80 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | return status; | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 84 | static psa_status_t cipher_encrypt(psa_key_id_t key, | 
|  | 85 | 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) | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 94 | { | 
|  | 95 | psa_status_t status; | 
| Jaeden Amero | b281f74 | 2019-02-20 10:40:20 +0000 | [diff] [blame] | 96 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 97 | size_t iv_len = 0; | 
|  | 98 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | memset(&operation, 0, sizeof(operation)); | 
|  | 100 | status = psa_cipher_encrypt_setup(&operation, key, alg); | 
|  | 101 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 102 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | status = psa_cipher_generate_iv(&operation, iv, iv_size, &iv_len); | 
|  | 104 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 105 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | status = cipher_operation(&operation, input, input_size, part_size, | 
|  | 107 | output, output_size, output_len); | 
|  | 108 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 109 |  | 
|  | 110 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 111 | psa_cipher_abort(&operation); | 
|  | 112 | return status; | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 113 | } | 
|  | 114 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | static psa_status_t cipher_decrypt(psa_key_id_t key, | 
|  | 116 | 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) | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 125 | { | 
|  | 126 | psa_status_t status; | 
| Jaeden Amero | b281f74 | 2019-02-20 10:40:20 +0000 | [diff] [blame] | 127 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 128 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | memset(&operation, 0, sizeof(operation)); | 
|  | 130 | status = psa_cipher_decrypt_setup(&operation, key, alg); | 
|  | 131 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 132 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | status = psa_cipher_set_iv(&operation, iv, iv_size); | 
|  | 134 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 135 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 136 | status = cipher_operation(&operation, input, input_size, part_size, | 
|  | 137 | output, output_size, output_len); | 
|  | 138 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 139 |  | 
|  | 140 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 141 | psa_cipher_abort(&operation); | 
|  | 142 | return status; | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
|  | 145 | static psa_status_t | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 146 | cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block(void) | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 147 | { | 
|  | 148 | enum { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 149 | block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES), | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 150 | key_bits = 256, | 
|  | 151 | part_size = block_size, | 
|  | 152 | }; | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 153 | const psa_algorithm_t alg = PSA_ALG_CBC_NO_PADDING; | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 154 |  | 
|  | 155 | psa_status_t status; | 
| Gilles Peskine | dfea0a25 | 2019-04-18 13:39:40 +0200 | [diff] [blame] | 156 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Ronald Cron | adc2ff2 | 2020-09-16 16:49:27 +0200 | [diff] [blame] | 157 | psa_key_id_t key = 0; | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 158 | 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 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | status = psa_generate_random(input, sizeof(input)); | 
|  | 165 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 166 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | 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); | 
|  | 171 | psa_set_key_bits(&attributes, key_bits); | 
| Gilles Peskine | b0edfb5 | 2018-12-03 16:24:51 +0100 | [diff] [blame] | 172 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | status = psa_generate_key(&attributes, &key); | 
|  | 174 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 175 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 176 | status = cipher_encrypt(key, alg, iv, sizeof(iv), | 
|  | 177 | input, sizeof(input), part_size, | 
|  | 178 | encrypt, sizeof(encrypt), &output_len); | 
|  | 179 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 180 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 181 | status = cipher_decrypt(key, alg, iv, sizeof(iv), | 
|  | 182 | encrypt, output_len, part_size, | 
|  | 183 | decrypt, sizeof(decrypt), &output_len); | 
|  | 184 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 185 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | status = memcmp(input, decrypt, sizeof(input)); | 
|  | 187 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 188 |  | 
|  | 189 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | psa_destroy_key(key); | 
|  | 191 | return status; | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 194 | static psa_status_t cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi(void) | 
| itayzafrir | a2d0804 | 2018-07-12 10:27:58 +0300 | [diff] [blame] | 195 | { | 
|  | 196 | enum { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 197 | block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES), | 
| itayzafrir | a2d0804 | 2018-07-12 10:27:58 +0300 | [diff] [blame] | 198 | key_bits = 256, | 
|  | 199 | input_size = 100, | 
|  | 200 | part_size = 10, | 
|  | 201 | }; | 
|  | 202 |  | 
| Gilles Peskine | daea26f | 2018-08-21 14:02:45 +0200 | [diff] [blame] | 203 | const psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; | 
| itayzafrir | a2d0804 | 2018-07-12 10:27:58 +0300 | [diff] [blame] | 204 |  | 
|  | 205 | psa_status_t status; | 
| Gilles Peskine | dfea0a25 | 2019-04-18 13:39:40 +0200 | [diff] [blame] | 206 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Ronald Cron | adc2ff2 | 2020-09-16 16:49:27 +0200 | [diff] [blame] | 207 | psa_key_id_t key = 0; | 
| itayzafrir | a2d0804 | 2018-07-12 10:27:58 +0300 | [diff] [blame] | 208 | 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 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 212 | status = psa_generate_random(input, sizeof(input)); | 
|  | 213 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | a2d0804 | 2018-07-12 10:27:58 +0300 | [diff] [blame] | 214 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 215 | 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); | 
|  | 219 | psa_set_key_bits(&attributes, key_bits); | 
| itayzafrir | a2d0804 | 2018-07-12 10:27:58 +0300 | [diff] [blame] | 220 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | status = psa_generate_key(&attributes, &key); | 
|  | 222 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | a2d0804 | 2018-07-12 10:27:58 +0300 | [diff] [blame] | 223 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 224 | status = cipher_encrypt(key, alg, iv, sizeof(iv), | 
|  | 225 | input, sizeof(input), part_size, | 
|  | 226 | encrypt, sizeof(encrypt), &output_len); | 
|  | 227 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | a2d0804 | 2018-07-12 10:27:58 +0300 | [diff] [blame] | 228 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | status = cipher_decrypt(key, alg, iv, sizeof(iv), | 
|  | 230 | encrypt, output_len, part_size, | 
|  | 231 | decrypt, sizeof(decrypt), &output_len); | 
|  | 232 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | a2d0804 | 2018-07-12 10:27:58 +0300 | [diff] [blame] | 233 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 234 | status = memcmp(input, decrypt, sizeof(input)); | 
|  | 235 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | a2d0804 | 2018-07-12 10:27:58 +0300 | [diff] [blame] | 236 |  | 
|  | 237 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 238 | psa_destroy_key(key); | 
|  | 239 | return status; | 
| itayzafrir | a2d0804 | 2018-07-12 10:27:58 +0300 | [diff] [blame] | 240 | } | 
|  | 241 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | static psa_status_t cipher_example_encrypt_decrypt_aes_ctr_multi(void) | 
| itayzafrir | 44b09d2 | 2018-07-12 13:06:41 +0300 | [diff] [blame] | 243 | { | 
|  | 244 | enum { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 245 | block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES), | 
| itayzafrir | 44b09d2 | 2018-07-12 13:06:41 +0300 | [diff] [blame] | 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 Peskine | dfea0a25 | 2019-04-18 13:39:40 +0200 | [diff] [blame] | 253 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Ronald Cron | adc2ff2 | 2020-09-16 16:49:27 +0200 | [diff] [blame] | 254 | psa_key_id_t key = 0; | 
| itayzafrir | 44b09d2 | 2018-07-12 13:06:41 +0300 | [diff] [blame] | 255 | size_t output_len = 0; | 
|  | 256 | uint8_t iv[block_size], input[input_size], encrypt[input_size], | 
|  | 257 | decrypt[input_size]; | 
|  | 258 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 259 | status = psa_generate_random(input, sizeof(input)); | 
|  | 260 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 44b09d2 | 2018-07-12 13:06:41 +0300 | [diff] [blame] | 261 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | 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); | 
|  | 266 | psa_set_key_bits(&attributes, key_bits); | 
| itayzafrir | 44b09d2 | 2018-07-12 13:06:41 +0300 | [diff] [blame] | 267 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | status = psa_generate_key(&attributes, &key); | 
|  | 269 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 44b09d2 | 2018-07-12 13:06:41 +0300 | [diff] [blame] | 270 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 271 | status = cipher_encrypt(key, alg, iv, sizeof(iv), | 
|  | 272 | input, sizeof(input), part_size, | 
|  | 273 | encrypt, sizeof(encrypt), &output_len); | 
|  | 274 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 44b09d2 | 2018-07-12 13:06:41 +0300 | [diff] [blame] | 275 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 276 | status = cipher_decrypt(key, alg, iv, sizeof(iv), | 
|  | 277 | encrypt, output_len, part_size, | 
|  | 278 | decrypt, sizeof(decrypt), &output_len); | 
|  | 279 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 44b09d2 | 2018-07-12 13:06:41 +0300 | [diff] [blame] | 280 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | status = memcmp(input, decrypt, sizeof(input)); | 
|  | 282 | ASSERT_STATUS(status, PSA_SUCCESS); | 
| itayzafrir | 44b09d2 | 2018-07-12 13:06:41 +0300 | [diff] [blame] | 283 |  | 
|  | 284 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 285 | psa_destroy_key(key); | 
|  | 286 | return status; | 
| itayzafrir | 44b09d2 | 2018-07-12 13:06:41 +0300 | [diff] [blame] | 287 | } | 
|  | 288 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | static void cipher_examples(void) | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 290 | { | 
|  | 291 | psa_status_t status; | 
|  | 292 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | printf("cipher encrypt/decrypt AES CBC no padding:\r\n"); | 
|  | 294 | status = cipher_example_encrypt_decrypt_aes_cbc_nopad_1_block(); | 
|  | 295 | if (status == PSA_SUCCESS) { | 
|  | 296 | printf("\tsuccess!\r\n"); | 
|  | 297 | } | 
| itayzafrir | a2d0804 | 2018-07-12 10:27:58 +0300 | [diff] [blame] | 298 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | printf("cipher encrypt/decrypt AES CBC PKCS7 multipart:\r\n"); | 
|  | 300 | status = cipher_example_encrypt_decrypt_aes_cbc_pkcs7_multi(); | 
|  | 301 | if (status == PSA_SUCCESS) { | 
|  | 302 | printf("\tsuccess!\r\n"); | 
|  | 303 | } | 
| itayzafrir | 44b09d2 | 2018-07-12 13:06:41 +0300 | [diff] [blame] | 304 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 305 | printf("cipher encrypt/decrypt AES CTR multipart:\r\n"); | 
|  | 306 | status = cipher_example_encrypt_decrypt_aes_ctr_multi(); | 
|  | 307 | if (status == PSA_SUCCESS) { | 
|  | 308 | printf("\tsuccess!\r\n"); | 
|  | 309 | } | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 310 | } | 
|  | 311 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | int main(void) | 
| itayzafrir | a3ff8a6 | 2018-07-10 10:10:21 +0300 | [diff] [blame] | 313 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | ASSERT(psa_crypto_init() == PSA_SUCCESS); | 
|  | 315 | cipher_examples(); | 
| itayzafrir | 1036670 | 2018-07-11 13:44:41 +0300 | [diff] [blame] | 316 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | mbedtls_psa_crypto_free(); | 
|  | 318 | return 0; | 
| itayzafrir | a3ff8a6 | 2018-07-10 10:10:21 +0300 | [diff] [blame] | 319 | } | 
| itayzafrir | 18ac331 | 2018-07-17 09:28:11 +0300 | [diff] [blame] | 320 | #endif /* MBEDTLS_PSA_CRYPTO_C && MBEDTLS_AES_C && MBEDTLS_CIPHER_MODE_CBC && | 
|  | 321 | MBEDTLS_CIPHER_MODE_CTR && MBEDTLS_CIPHER_MODE_WITH_PADDING */ |