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