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