Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 1 | /* |
| 2 | * PSA cipher driver entry points |
| 3 | */ |
| 4 | /* |
| 5 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "common.h" |
| 10 | |
| 11 | #if defined(MBEDTLS_PSA_CRYPTO_C) |
| 12 | |
Martin Man | 43dedd8 | 2022-08-02 12:44:35 +0200 | [diff] [blame] | 13 | #include "psa_crypto_cipher.h" |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 14 | #include "psa_crypto_core.h" |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 15 | #include "psa_crypto_random_impl.h" |
| 16 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 17 | #include "mbedtls/cipher.h" |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 18 | #include "mbedtls/error.h" |
Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 19 | |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 20 | #include <string.h> |
| 21 | |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 22 | const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa( |
| 23 | psa_algorithm_t alg, |
| 24 | psa_key_type_t key_type, |
| 25 | size_t key_bits, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 26 | mbedtls_cipher_id_t *cipher_id) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 27 | { |
| 28 | mbedtls_cipher_mode_t mode; |
| 29 | mbedtls_cipher_id_t cipher_id_tmp; |
| 30 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 31 | if (PSA_ALG_IS_AEAD(alg)) { |
| 32 | alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG(alg, 0); |
| 33 | } |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 34 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 35 | if (PSA_ALG_IS_CIPHER(alg) || PSA_ALG_IS_AEAD(alg)) { |
| 36 | switch (alg) { |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 37 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_STREAM_CIPHER) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 38 | case PSA_ALG_STREAM_CIPHER: |
| 39 | mode = MBEDTLS_MODE_STREAM; |
| 40 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 41 | #endif |
| 42 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CTR) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 43 | case PSA_ALG_CTR: |
| 44 | mode = MBEDTLS_MODE_CTR; |
| 45 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 46 | #endif |
| 47 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CFB) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 48 | case PSA_ALG_CFB: |
| 49 | mode = MBEDTLS_MODE_CFB; |
| 50 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 51 | #endif |
| 52 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_OFB) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 53 | case PSA_ALG_OFB: |
| 54 | mode = MBEDTLS_MODE_OFB; |
| 55 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 56 | #endif |
| 57 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 58 | case PSA_ALG_ECB_NO_PADDING: |
| 59 | mode = MBEDTLS_MODE_ECB; |
| 60 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 61 | #endif |
| 62 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 63 | case PSA_ALG_CBC_NO_PADDING: |
| 64 | mode = MBEDTLS_MODE_CBC; |
| 65 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 66 | #endif |
| 67 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 68 | case PSA_ALG_CBC_PKCS7: |
| 69 | mode = MBEDTLS_MODE_CBC; |
| 70 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 71 | #endif |
| 72 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CCM) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 73 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 0): |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 74 | mode = MBEDTLS_MODE_CCM; |
| 75 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 76 | #endif |
| 77 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_GCM) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 78 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, 0): |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 79 | mode = MBEDTLS_MODE_GCM; |
| 80 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 81 | #endif |
| 82 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CHACHA20_POLY1305) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 83 | case PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CHACHA20_POLY1305, 0): |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 84 | mode = MBEDTLS_MODE_CHACHAPOLY; |
| 85 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 86 | #endif |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 87 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 88 | return NULL; |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 89 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 90 | } else if (alg == PSA_ALG_CMAC) { |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 91 | mode = MBEDTLS_MODE_ECB; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 92 | } else { |
| 93 | return NULL; |
| 94 | } |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 95 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 96 | switch (key_type) { |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 97 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 98 | case PSA_KEY_TYPE_AES: |
| 99 | cipher_id_tmp = MBEDTLS_CIPHER_ID_AES; |
| 100 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 101 | #endif |
| 102 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARIA) |
Gilles Peskine | 8890f64 | 2021-09-21 11:59:39 +0200 | [diff] [blame] | 103 | case PSA_KEY_TYPE_ARIA: |
| 104 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARIA; |
| 105 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 106 | #endif |
| 107 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 108 | case PSA_KEY_TYPE_DES: |
| 109 | /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES, |
| 110 | * and 192 for three-key Triple-DES. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 111 | if (key_bits == 64) { |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 112 | cipher_id_tmp = MBEDTLS_CIPHER_ID_DES; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | } else { |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 114 | cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | } |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 116 | /* mbedtls doesn't recognize two-key Triple-DES as an algorithm, |
| 117 | * but two-key Triple-DES is functionally three-key Triple-DES |
| 118 | * with K1=K3, so that's how we present it to mbedtls. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 119 | if (key_bits == 128) { |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 120 | key_bits = 192; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | } |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 122 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 123 | #endif |
| 124 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 125 | case PSA_KEY_TYPE_CAMELLIA: |
| 126 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA; |
| 127 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 128 | #endif |
| 129 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARC4) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 130 | case PSA_KEY_TYPE_ARC4: |
| 131 | cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4; |
| 132 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 133 | #endif |
| 134 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20) |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 135 | case PSA_KEY_TYPE_CHACHA20: |
| 136 | cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20; |
| 137 | break; |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 138 | #endif |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 139 | default: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 140 | return NULL; |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 141 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 142 | if (cipher_id != NULL) { |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 143 | *cipher_id = cipher_id_tmp; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | } |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 145 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 146 | return mbedtls_cipher_info_from_values(cipher_id_tmp, |
| 147 | (int) key_bits, mode); |
Ronald Cron | 75e6ae2 | 2021-03-17 14:46:05 +0100 | [diff] [blame] | 148 | } |
| 149 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 150 | #if defined(MBEDTLS_PSA_BUILTIN_CIPHER) |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 151 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 152 | static psa_status_t psa_cipher_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 153 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 154 | const psa_key_attributes_t *attributes, |
| 155 | const uint8_t *key_buffer, size_t key_buffer_size, |
| 156 | psa_algorithm_t alg, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 157 | mbedtls_operation_t cipher_operation) |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 158 | { |
| 159 | int ret = 0; |
| 160 | size_t key_bits; |
| 161 | const mbedtls_cipher_info_t *cipher_info = NULL; |
| 162 | psa_key_type_t key_type = attributes->core.type; |
| 163 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 164 | (void) key_buffer_size; |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 165 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 166 | mbedtls_cipher_init(&operation->ctx.cipher); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 167 | |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 168 | operation->alg = alg; |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 169 | key_bits = attributes->core.bits; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 170 | cipher_info = mbedtls_cipher_info_from_psa(alg, key_type, |
| 171 | key_bits, NULL); |
| 172 | if (cipher_info == NULL) { |
| 173 | return PSA_ERROR_NOT_SUPPORTED; |
| 174 | } |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 175 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 176 | ret = mbedtls_cipher_setup(&operation->ctx.cipher, cipher_info); |
| 177 | if (ret != 0) { |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 178 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 179 | } |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 180 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 181 | #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 182 | if (key_type == PSA_KEY_TYPE_DES && key_bits == 128) { |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 183 | /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */ |
| 184 | uint8_t keys[24]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 185 | memcpy(keys, key_buffer, 16); |
| 186 | memcpy(keys + 16, key_buffer, 8); |
| 187 | ret = mbedtls_cipher_setkey(&operation->ctx.cipher, |
| 188 | keys, |
| 189 | 192, cipher_operation); |
| 190 | } else |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 191 | #endif |
| 192 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 193 | ret = mbedtls_cipher_setkey(&operation->ctx.cipher, key_buffer, |
| 194 | (int) key_bits, cipher_operation); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 195 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | if (ret != 0) { |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 197 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 198 | } |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 199 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 200 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \ |
| 201 | defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 202 | switch (alg) { |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 203 | case PSA_ALG_CBC_NO_PADDING: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 204 | ret = mbedtls_cipher_set_padding_mode(&operation->ctx.cipher, |
| 205 | MBEDTLS_PADDING_NONE); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 206 | break; |
| 207 | case PSA_ALG_CBC_PKCS7: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 208 | ret = mbedtls_cipher_set_padding_mode(&operation->ctx.cipher, |
| 209 | MBEDTLS_PADDING_PKCS7); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 210 | break; |
| 211 | default: |
| 212 | /* The algorithm doesn't involve padding. */ |
| 213 | ret = 0; |
| 214 | break; |
| 215 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 216 | if (ret != 0) { |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 217 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 218 | } |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 219 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || |
| 220 | MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */ |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 221 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 222 | operation->block_length = (PSA_ALG_IS_STREAM_CIPHER(alg) ? 1 : |
| 223 | PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type)); |
| 224 | operation->iv_length = PSA_CIPHER_IV_LENGTH(key_type, alg); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 225 | |
| 226 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 227 | return mbedtls_to_psa_error(ret); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 228 | } |
| 229 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 230 | psa_status_t mbedtls_psa_cipher_encrypt_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 231 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 232 | const psa_key_attributes_t *attributes, |
| 233 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 234 | psa_algorithm_t alg) |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 235 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 236 | return psa_cipher_setup(operation, attributes, |
| 237 | key_buffer, key_buffer_size, |
| 238 | alg, MBEDTLS_ENCRYPT); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 239 | } |
| 240 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 241 | psa_status_t mbedtls_psa_cipher_decrypt_setup( |
Ronald Cron | 6e412a7 | 2021-03-10 09:58:47 +0100 | [diff] [blame] | 242 | mbedtls_psa_cipher_operation_t *operation, |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 243 | const psa_key_attributes_t *attributes, |
| 244 | const uint8_t *key_buffer, size_t key_buffer_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 245 | psa_algorithm_t alg) |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 246 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 247 | return psa_cipher_setup(operation, attributes, |
| 248 | key_buffer, key_buffer_size, |
| 249 | alg, MBEDTLS_DECRYPT); |
Ronald Cron | d6d2888 | 2020-12-14 14:56:02 +0100 | [diff] [blame] | 250 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 251 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 252 | psa_status_t mbedtls_psa_cipher_set_iv( |
| 253 | mbedtls_psa_cipher_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 254 | const uint8_t *iv, size_t iv_length) |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 255 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 256 | if (iv_length != operation->iv_length) { |
| 257 | return PSA_ERROR_INVALID_ARGUMENT; |
| 258 | } |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 259 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 260 | return mbedtls_to_psa_error( |
| 261 | mbedtls_cipher_set_iv(&operation->ctx.cipher, |
| 262 | iv, iv_length)); |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 263 | } |
| 264 | |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 265 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING) |
Gilles Peskine | 7b1c916 | 2021-09-13 09:33:28 +0200 | [diff] [blame] | 266 | /** Process input for which the algorithm is set to ECB mode. |
| 267 | * |
| 268 | * This requires manual processing, since the PSA API is defined as being |
| 269 | * able to process arbitrary-length calls to psa_cipher_update() with ECB mode, |
| 270 | * but the underlying mbedtls_cipher_update only takes full blocks. |
| 271 | * |
| 272 | * \param ctx The mbedtls cipher context to use. It must have been |
| 273 | * set up for ECB. |
| 274 | * \param[in] input The input plaintext or ciphertext to process. |
| 275 | * \param input_length The number of bytes to process from \p input. |
| 276 | * This does not need to be aligned to a block boundary. |
| 277 | * If there is a partial block at the end of the input, |
| 278 | * it is stored in \p ctx for future processing. |
Gilles Peskine | 0390016 | 2021-09-13 12:20:51 +0200 | [diff] [blame] | 279 | * \param output The buffer where the output is written. It must be |
| 280 | * at least `BS * floor((p + input_length) / BS)` bytes |
| 281 | * long, where `p` is the number of bytes in the |
| 282 | * unprocessed partial block in \p ctx (with |
| 283 | * `0 <= p <= BS - 1`) and `BS` is the block size. |
Gilles Peskine | 7b1c916 | 2021-09-13 09:33:28 +0200 | [diff] [blame] | 284 | * \param output_length On success, the number of bytes written to \p output. |
| 285 | * \c 0 on error. |
| 286 | * |
| 287 | * \return #PSA_SUCCESS or an error from a hardware accelerator |
| 288 | */ |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 289 | static psa_status_t psa_cipher_update_ecb( |
| 290 | mbedtls_cipher_context_t *ctx, |
| 291 | const uint8_t *input, |
| 292 | size_t input_length, |
| 293 | uint8_t *output, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 294 | size_t *output_length) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 295 | { |
| 296 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 297 | size_t block_size = ctx->cipher_info->block_size; |
| 298 | size_t internal_output_length = 0; |
| 299 | *output_length = 0; |
| 300 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 301 | if (input_length == 0) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 302 | status = PSA_SUCCESS; |
| 303 | goto exit; |
| 304 | } |
| 305 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 306 | if (ctx->unprocessed_len > 0) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 307 | /* Fill up to block size, and run the block if there's a full one. */ |
| 308 | size_t bytes_to_copy = block_size - ctx->unprocessed_len; |
| 309 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 310 | if (input_length < bytes_to_copy) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 311 | bytes_to_copy = input_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 312 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 313 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 314 | memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), |
| 315 | input, bytes_to_copy); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 316 | input_length -= bytes_to_copy; |
| 317 | input += bytes_to_copy; |
| 318 | ctx->unprocessed_len += bytes_to_copy; |
| 319 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 320 | if (ctx->unprocessed_len == block_size) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 321 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 322 | mbedtls_cipher_update(ctx, |
| 323 | ctx->unprocessed_data, |
| 324 | block_size, |
| 325 | output, &internal_output_length)); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 326 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 327 | if (status != PSA_SUCCESS) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 328 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 329 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 330 | |
| 331 | output += internal_output_length; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 332 | *output_length += internal_output_length; |
| 333 | ctx->unprocessed_len = 0; |
| 334 | } |
| 335 | } |
| 336 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 337 | while (input_length >= block_size) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 338 | /* Run all full blocks we have, one by one */ |
| 339 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 340 | mbedtls_cipher_update(ctx, input, |
| 341 | block_size, |
| 342 | output, &internal_output_length)); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 343 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 344 | if (status != PSA_SUCCESS) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 345 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 346 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 347 | |
| 348 | input_length -= block_size; |
| 349 | input += block_size; |
| 350 | |
| 351 | output += internal_output_length; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 352 | *output_length += internal_output_length; |
| 353 | } |
| 354 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 355 | if (input_length > 0) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 356 | /* Save unprocessed bytes for later processing */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 357 | memcpy(&(ctx->unprocessed_data[ctx->unprocessed_len]), |
| 358 | input, input_length); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 359 | ctx->unprocessed_len += input_length; |
| 360 | } |
| 361 | |
| 362 | status = PSA_SUCCESS; |
| 363 | |
| 364 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 365 | return status; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 366 | } |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 367 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING */ |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 368 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 369 | psa_status_t mbedtls_psa_cipher_update( |
| 370 | mbedtls_psa_cipher_operation_t *operation, |
| 371 | const uint8_t *input, size_t input_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 372 | uint8_t *output, size_t output_size, size_t *output_length) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 373 | { |
| 374 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 375 | size_t expected_output_size; |
| 376 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 377 | if (!PSA_ALG_IS_STREAM_CIPHER(operation->alg)) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 378 | /* Take the unprocessed partial block left over from previous |
| 379 | * update calls, if any, plus the input to this call. Remove |
| 380 | * the last partial block, if any. You get the data that will be |
| 381 | * output in this call. */ |
| 382 | expected_output_size = |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 383 | (operation->ctx.cipher.unprocessed_len + input_length) |
Ronald Cron | 6ad554c | 2021-03-26 09:29:09 +0100 | [diff] [blame] | 384 | / operation->block_length * operation->block_length; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 385 | } else { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 386 | expected_output_size = input_length; |
| 387 | } |
| 388 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 389 | if (output_size < expected_output_size) { |
| 390 | return PSA_ERROR_BUFFER_TOO_SMALL; |
| 391 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 392 | |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 393 | #if defined(MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 394 | if (operation->alg == PSA_ALG_ECB_NO_PADDING) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 395 | /* mbedtls_cipher_update has an API inconsistency: it will only |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 396 | * process a single block at a time in ECB mode. Abstract away that |
| 397 | * inconsistency here to match the PSA API behaviour. */ |
| 398 | status = psa_cipher_update_ecb(&operation->ctx.cipher, |
| 399 | input, |
| 400 | input_length, |
| 401 | output, |
| 402 | output_length); |
| 403 | } else |
Gilles Peskine | 449e02e | 2022-03-16 12:25:17 +0100 | [diff] [blame] | 404 | #endif /* MBEDTLS_PSA_BUILTIN_ALG_ECB_NO_PADDING */ |
Gabor Mezei | 92905be | 2024-01-29 13:33:58 +0100 | [diff] [blame] | 405 | if (input_length == 0) { |
| 406 | /* There is no input, nothing to be done */ |
| 407 | *output_length = 0; |
| 408 | status = PSA_SUCCESS; |
| 409 | } else { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 410 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 411 | mbedtls_cipher_update(&operation->ctx.cipher, input, |
| 412 | input_length, output, output_length)); |
gabor-mezei-arm | 42373bd | 2021-06-29 16:41:25 +0200 | [diff] [blame] | 413 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 414 | if (*output_length > output_size) { |
| 415 | return PSA_ERROR_CORRUPTION_DETECTED; |
| 416 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 419 | return status; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 422 | psa_status_t mbedtls_psa_cipher_finish( |
| 423 | mbedtls_psa_cipher_operation_t *operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 424 | uint8_t *output, size_t output_size, size_t *output_length) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 425 | { |
| 426 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 427 | uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH]; |
| 428 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 429 | if (operation->ctx.cipher.unprocessed_len != 0) { |
| 430 | if (operation->alg == PSA_ALG_ECB_NO_PADDING || |
| 431 | operation->alg == PSA_ALG_CBC_NO_PADDING) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 432 | status = PSA_ERROR_INVALID_ARGUMENT; |
| 433 | goto exit; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | status = mbedtls_to_psa_error( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 438 | mbedtls_cipher_finish(&operation->ctx.cipher, |
| 439 | temp_output_buffer, |
| 440 | output_length)); |
| 441 | if (status != PSA_SUCCESS) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 442 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 443 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 444 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 445 | if (*output_length == 0) { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 446 | ; /* Nothing to copy. Note that output may be NULL in this case. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 447 | } else if (output_size >= *output_length) { |
| 448 | memcpy(output, temp_output_buffer, *output_length); |
| 449 | } else { |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 450 | status = PSA_ERROR_BUFFER_TOO_SMALL; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 451 | } |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 452 | |
| 453 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 454 | mbedtls_platform_zeroize(temp_output_buffer, |
| 455 | sizeof(temp_output_buffer)); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 456 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 457 | return status; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 458 | } |
| 459 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 460 | psa_status_t mbedtls_psa_cipher_abort( |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 461 | mbedtls_psa_cipher_operation_t *operation) |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 462 | { |
Ronald Cron | 937dfee | 2021-03-10 09:17:32 +0100 | [diff] [blame] | 463 | /* Sanity check (shouldn't happen: operation->alg should |
| 464 | * always have been initialized to a valid value). */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 465 | if (!PSA_ALG_IS_CIPHER(operation->alg)) { |
| 466 | return PSA_ERROR_BAD_STATE; |
| 467 | } |
Ronald Cron | 937dfee | 2021-03-10 09:17:32 +0100 | [diff] [blame] | 468 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 469 | mbedtls_cipher_free(&operation->ctx.cipher); |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 470 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 471 | return PSA_SUCCESS; |
Ronald Cron | 6d05173 | 2020-10-01 14:10:20 +0200 | [diff] [blame] | 472 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 473 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 474 | psa_status_t mbedtls_psa_cipher_encrypt(const psa_key_attributes_t *attributes, |
| 475 | const uint8_t *key_buffer, |
| 476 | size_t key_buffer_size, |
| 477 | psa_algorithm_t alg, |
| 478 | const uint8_t *iv, |
| 479 | size_t iv_length, |
| 480 | const uint8_t *input, |
| 481 | size_t input_length, |
| 482 | uint8_t *output, |
| 483 | size_t output_size, |
| 484 | size_t *output_length) |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 485 | { |
| 486 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 487 | mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT; |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 488 | size_t update_output_length, finish_output_length; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 489 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 490 | status = mbedtls_psa_cipher_encrypt_setup(&operation, attributes, |
| 491 | key_buffer, key_buffer_size, |
| 492 | alg); |
| 493 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 494 | goto exit; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 497 | if (iv_length > 0) { |
| 498 | status = mbedtls_psa_cipher_set_iv(&operation, iv, iv_length); |
| 499 | if (status != PSA_SUCCESS) { |
| 500 | goto exit; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | status = mbedtls_psa_cipher_update(&operation, input, input_length, |
| 505 | output, output_size, &update_output_length); |
| 506 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 507 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 508 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 509 | |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 510 | status = mbedtls_psa_cipher_finish( |
| 511 | &operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 512 | mbedtls_buffer_offset(output, update_output_length), |
| 513 | output_size - update_output_length, &finish_output_length); |
| 514 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 515 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 516 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 517 | |
Ronald Cron | a833169 | 2021-07-09 09:19:35 +0200 | [diff] [blame] | 518 | *output_length = update_output_length + finish_output_length; |
gabor-mezei-arm | 7fbea09 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 519 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 520 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 521 | if (status == PSA_SUCCESS) { |
| 522 | status = mbedtls_psa_cipher_abort(&operation); |
| 523 | } else { |
| 524 | mbedtls_psa_cipher_abort(&operation); |
| 525 | } |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 526 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 527 | return status; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 528 | } |
| 529 | |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 530 | psa_status_t mbedtls_psa_cipher_decrypt( |
| 531 | const psa_key_attributes_t *attributes, |
| 532 | const uint8_t *key_buffer, |
| 533 | size_t key_buffer_size, |
| 534 | psa_algorithm_t alg, |
| 535 | const uint8_t *input, |
| 536 | size_t input_length, |
| 537 | uint8_t *output, |
| 538 | size_t output_size, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 539 | size_t *output_length) |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 540 | { |
| 541 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 542 | mbedtls_psa_cipher_operation_t operation = MBEDTLS_PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | 7fbea09 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 543 | size_t olength, accumulated_length; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 544 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 545 | status = mbedtls_psa_cipher_decrypt_setup(&operation, attributes, |
| 546 | key_buffer, key_buffer_size, |
| 547 | alg); |
| 548 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 549 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 550 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 551 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 552 | if (operation.iv_length > 0) { |
| 553 | status = mbedtls_psa_cipher_set_iv(&operation, |
| 554 | input, operation.iv_length); |
| 555 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 556 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 557 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 558 | } |
| 559 | |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 560 | status = mbedtls_psa_cipher_update( |
| 561 | &operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 562 | mbedtls_buffer_offset_const(input, operation.iv_length), |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 563 | input_length - operation.iv_length, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 564 | output, output_size, &olength); |
| 565 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 566 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 567 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 568 | |
gabor-mezei-arm | 809634d | 2021-06-29 16:42:13 +0200 | [diff] [blame] | 569 | accumulated_length = olength; |
gabor-mezei-arm | 3fd792d | 2021-06-25 15:25:38 +0200 | [diff] [blame] | 570 | |
Gilles Peskine | 01bf631 | 2022-11-23 14:15:57 +0100 | [diff] [blame] | 571 | status = mbedtls_psa_cipher_finish( |
| 572 | &operation, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 573 | mbedtls_buffer_offset(output, accumulated_length), |
| 574 | output_size - accumulated_length, &olength); |
| 575 | if (status != PSA_SUCCESS) { |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 576 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 577 | } |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 578 | |
gabor-mezei-arm | 2523045 | 2021-06-29 19:06:30 +0200 | [diff] [blame] | 579 | *output_length = accumulated_length + olength; |
gabor-mezei-arm | 7fbea09 | 2021-06-25 15:23:05 +0200 | [diff] [blame] | 580 | |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 581 | exit: |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 582 | if (status == PSA_SUCCESS) { |
| 583 | status = mbedtls_psa_cipher_abort(&operation); |
| 584 | } else { |
| 585 | mbedtls_psa_cipher_abort(&operation); |
| 586 | } |
Ronald Cron | cfc3c7b | 2021-03-13 18:50:11 +0100 | [diff] [blame] | 587 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 588 | return status; |
gabor-mezei-arm | fa990b5 | 2021-03-25 11:17:10 +0100 | [diff] [blame] | 589 | } |
Ronald Cron | 5d9b00d | 2021-03-10 14:43:20 +0100 | [diff] [blame] | 590 | #endif /* MBEDTLS_PSA_BUILTIN_CIPHER */ |
Ronald Cron | 8287e6b | 2021-03-12 10:35:18 +0100 | [diff] [blame] | 591 | |
Ronald Cron | 0ff5795 | 2021-03-08 16:46:35 +0100 | [diff] [blame] | 592 | #endif /* MBEDTLS_PSA_CRYPTO_C */ |