Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file block_cipher.c |
| 3 | * |
| 4 | * \brief Lightweight abstraction layer for block ciphers with 128 bit blocks, |
| 5 | * for use by the GCM and CCM modules. |
| 6 | */ |
| 7 | /* |
| 8 | * Copyright The Mbed TLS Contributors |
| 9 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 10 | */ |
| 11 | |
| 12 | #include "common.h" |
| 13 | |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 14 | #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA) |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 15 | #include "psa/crypto.h" |
Valerio Setti | 849a1ab | 2023-12-13 16:34:07 +0100 | [diff] [blame] | 16 | #include "psa_crypto_core.h" |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 17 | #include "psa_util_internal.h" |
| 18 | #endif |
| 19 | |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 20 | #include "block_cipher_internal.h" |
| 21 | |
| 22 | #if defined(MBEDTLS_BLOCK_CIPHER_C) |
| 23 | |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 24 | #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA) |
Valerio Setti | 4ff405c | 2023-12-15 16:10:52 +0100 | [diff] [blame] | 25 | static psa_key_type_t psa_key_type_from_block_cipher_id(mbedtls_block_cipher_id_t cipher_id) |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 26 | { |
| 27 | switch (cipher_id) { |
| 28 | #if defined(MBEDTLS_BLOCK_CIPHER_AES_VIA_PSA) |
Valerio Setti | 4ff405c | 2023-12-15 16:10:52 +0100 | [diff] [blame] | 29 | case MBEDTLS_BLOCK_CIPHER_ID_AES: |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 30 | return PSA_KEY_TYPE_AES; |
| 31 | #endif |
| 32 | #if defined(MBEDTLS_BLOCK_CIPHER_ARIA_VIA_PSA) |
Valerio Setti | 4ff405c | 2023-12-15 16:10:52 +0100 | [diff] [blame] | 33 | case MBEDTLS_BLOCK_CIPHER_ID_ARIA: |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 34 | return PSA_KEY_TYPE_ARIA; |
| 35 | #endif |
| 36 | #if defined(MBEDTLS_BLOCK_CIPHER_CAMELLIA_VIA_PSA) |
Valerio Setti | 4ff405c | 2023-12-15 16:10:52 +0100 | [diff] [blame] | 37 | case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA: |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 38 | return PSA_KEY_TYPE_CAMELLIA; |
| 39 | #endif |
| 40 | default: |
| 41 | return PSA_KEY_TYPE_NONE; |
| 42 | } |
| 43 | } |
| 44 | |
Valerio Setti | 1994e72 | 2023-12-28 14:01:22 +0100 | [diff] [blame^] | 45 | static int mbedtls_cipher_error_from_psa(psa_status_t status) |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 46 | { |
| 47 | return PSA_TO_MBEDTLS_ERR_LIST(status, psa_to_cipher_errors, |
| 48 | psa_generic_status_to_mbedtls); |
| 49 | } |
| 50 | #endif /* MBEDTLS_BLOCK_CIPHER_SOME_PSA */ |
| 51 | |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 52 | void mbedtls_block_cipher_free(mbedtls_block_cipher_context_t *ctx) |
| 53 | { |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 54 | #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA) |
| 55 | if (ctx->engine == MBEDTLS_BLOCK_CIPHER_ENGINE_PSA) { |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 56 | psa_destroy_key(ctx->psa_key_id); |
| 57 | return; |
| 58 | } |
| 59 | #endif |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 60 | switch (ctx->id) { |
| 61 | #if defined(MBEDTLS_AES_C) |
| 62 | case MBEDTLS_BLOCK_CIPHER_ID_AES: |
| 63 | mbedtls_aes_free(&ctx->ctx.aes); |
| 64 | break; |
| 65 | #endif |
| 66 | #if defined(MBEDTLS_ARIA_C) |
| 67 | case MBEDTLS_BLOCK_CIPHER_ID_ARIA: |
| 68 | mbedtls_aria_free(&ctx->ctx.aria); |
| 69 | break; |
| 70 | #endif |
| 71 | #if defined(MBEDTLS_CAMELLIA_C) |
| 72 | case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA: |
| 73 | mbedtls_camellia_free(&ctx->ctx.camellia); |
| 74 | break; |
| 75 | #endif |
| 76 | default: |
| 77 | break; |
| 78 | } |
| 79 | ctx->id = MBEDTLS_BLOCK_CIPHER_ID_NONE; |
| 80 | } |
| 81 | |
| 82 | int mbedtls_block_cipher_setup(mbedtls_block_cipher_context_t *ctx, |
| 83 | mbedtls_cipher_id_t cipher_id) |
| 84 | { |
Valerio Setti | 4ff405c | 2023-12-15 16:10:52 +0100 | [diff] [blame] | 85 | ctx->id = (cipher_id == MBEDTLS_CIPHER_ID_AES) ? MBEDTLS_BLOCK_CIPHER_ID_AES : |
| 86 | (cipher_id == MBEDTLS_CIPHER_ID_ARIA) ? MBEDTLS_BLOCK_CIPHER_ID_ARIA : |
| 87 | (cipher_id == MBEDTLS_CIPHER_ID_CAMELLIA) ? MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA : |
| 88 | MBEDTLS_BLOCK_CIPHER_ID_NONE; |
| 89 | |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 90 | #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA) |
Valerio Setti | 4ff405c | 2023-12-15 16:10:52 +0100 | [diff] [blame] | 91 | if (psa_can_do_cipher(cipher_id) && |
| 92 | (psa_key_type_from_block_cipher_id(ctx->id) != PSA_KEY_TYPE_NONE)) { |
| 93 | ctx->engine = MBEDTLS_BLOCK_CIPHER_ENGINE_PSA; |
| 94 | return 0; |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 95 | } |
| 96 | ctx->engine = MBEDTLS_BLOCK_CIPHER_ENGINE_LEGACY; |
| 97 | #endif |
| 98 | |
Valerio Setti | 4ff405c | 2023-12-15 16:10:52 +0100 | [diff] [blame] | 99 | switch (ctx->id) { |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 100 | #if defined(MBEDTLS_AES_C) |
Valerio Setti | 4ff405c | 2023-12-15 16:10:52 +0100 | [diff] [blame] | 101 | case MBEDTLS_BLOCK_CIPHER_ID_AES: |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 102 | mbedtls_aes_init(&ctx->ctx.aes); |
| 103 | return 0; |
| 104 | #endif |
| 105 | #if defined(MBEDTLS_ARIA_C) |
Valerio Setti | 4ff405c | 2023-12-15 16:10:52 +0100 | [diff] [blame] | 106 | case MBEDTLS_BLOCK_CIPHER_ID_ARIA: |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 107 | mbedtls_aria_init(&ctx->ctx.aria); |
| 108 | return 0; |
| 109 | #endif |
| 110 | #if defined(MBEDTLS_CAMELLIA_C) |
Valerio Setti | 4ff405c | 2023-12-15 16:10:52 +0100 | [diff] [blame] | 111 | case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA: |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 112 | mbedtls_camellia_init(&ctx->ctx.camellia); |
| 113 | return 0; |
| 114 | #endif |
| 115 | default: |
Valerio Setti | 4ff405c | 2023-12-15 16:10:52 +0100 | [diff] [blame] | 116 | ctx->id = MBEDTLS_BLOCK_CIPHER_ID_NONE; |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 117 | return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA; |
| 118 | } |
| 119 | } |
| 120 | |
Manuel Pégourié-Gonnard | 3e0884f | 2023-11-10 11:52:10 +0100 | [diff] [blame] | 121 | int mbedtls_block_cipher_setkey(mbedtls_block_cipher_context_t *ctx, |
| 122 | const unsigned char *key, |
| 123 | unsigned key_bitlen) |
| 124 | { |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 125 | #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA) |
| 126 | if (ctx->engine == MBEDTLS_BLOCK_CIPHER_ENGINE_PSA) { |
| 127 | psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
| 128 | psa_status_t status; |
| 129 | |
Valerio Setti | 4ff405c | 2023-12-15 16:10:52 +0100 | [diff] [blame] | 130 | psa_set_key_type(&key_attr, psa_key_type_from_block_cipher_id(ctx->id)); |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 131 | psa_set_key_bits(&key_attr, key_bitlen); |
| 132 | psa_set_key_algorithm(&key_attr, PSA_ALG_ECB_NO_PADDING); |
| 133 | psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_ENCRYPT); |
| 134 | |
Valerio Setti | 785ec17 | 2023-12-13 16:49:05 +0100 | [diff] [blame] | 135 | status = psa_import_key(&key_attr, key, PSA_BITS_TO_BYTES(key_bitlen), &ctx->psa_key_id); |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 136 | if (status != PSA_SUCCESS) { |
| 137 | return mbedtls_cipher_error_from_psa(status); |
| 138 | } |
| 139 | psa_reset_key_attributes(&key_attr); |
| 140 | |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | #endif /* MBEDTLS_BLOCK_CIPHER_SOME_PSA */ |
| 144 | |
Manuel Pégourié-Gonnard | 3e0884f | 2023-11-10 11:52:10 +0100 | [diff] [blame] | 145 | switch (ctx->id) { |
| 146 | #if defined(MBEDTLS_AES_C) |
| 147 | case MBEDTLS_BLOCK_CIPHER_ID_AES: |
| 148 | return mbedtls_aes_setkey_enc(&ctx->ctx.aes, key, key_bitlen); |
| 149 | #endif |
| 150 | #if defined(MBEDTLS_ARIA_C) |
| 151 | case MBEDTLS_BLOCK_CIPHER_ID_ARIA: |
| 152 | return mbedtls_aria_setkey_enc(&ctx->ctx.aria, key, key_bitlen); |
| 153 | #endif |
| 154 | #if defined(MBEDTLS_CAMELLIA_C) |
| 155 | case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA: |
| 156 | return mbedtls_camellia_setkey_enc(&ctx->ctx.camellia, key, key_bitlen); |
| 157 | #endif |
| 158 | default: |
| 159 | return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT; |
| 160 | } |
| 161 | } |
Manuel Pégourié-Gonnard | 76fa16c | 2023-11-10 12:02:53 +0100 | [diff] [blame] | 162 | |
| 163 | int mbedtls_block_cipher_encrypt(mbedtls_block_cipher_context_t *ctx, |
| 164 | const unsigned char input[16], |
| 165 | unsigned char output[16]) |
| 166 | { |
Valerio Setti | c1db99d | 2023-12-12 11:19:17 +0100 | [diff] [blame] | 167 | #if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA) |
| 168 | if (ctx->engine == MBEDTLS_BLOCK_CIPHER_ENGINE_PSA) { |
| 169 | psa_status_t status; |
| 170 | size_t olen; |
| 171 | |
| 172 | status = psa_cipher_encrypt(ctx->psa_key_id, PSA_ALG_ECB_NO_PADDING, |
| 173 | input, 16, output, 16, &olen); |
| 174 | if (status != PSA_SUCCESS) { |
| 175 | return mbedtls_cipher_error_from_psa(status); |
| 176 | } |
| 177 | return 0; |
| 178 | } |
| 179 | #endif /* MBEDTLS_BLOCK_CIPHER_SOME_PSA */ |
| 180 | |
Manuel Pégourié-Gonnard | 76fa16c | 2023-11-10 12:02:53 +0100 | [diff] [blame] | 181 | switch (ctx->id) { |
| 182 | #if defined(MBEDTLS_AES_C) |
| 183 | case MBEDTLS_BLOCK_CIPHER_ID_AES: |
| 184 | return mbedtls_aes_crypt_ecb(&ctx->ctx.aes, MBEDTLS_AES_ENCRYPT, |
| 185 | input, output); |
| 186 | #endif |
| 187 | #if defined(MBEDTLS_ARIA_C) |
| 188 | case MBEDTLS_BLOCK_CIPHER_ID_ARIA: |
| 189 | return mbedtls_aria_crypt_ecb(&ctx->ctx.aria, input, output); |
| 190 | #endif |
| 191 | #if defined(MBEDTLS_CAMELLIA_C) |
| 192 | case MBEDTLS_BLOCK_CIPHER_ID_CAMELLIA: |
| 193 | return mbedtls_camellia_crypt_ecb(&ctx->ctx.camellia, |
| 194 | MBEDTLS_CAMELLIA_ENCRYPT, |
| 195 | input, output); |
| 196 | #endif |
| 197 | default: |
| 198 | return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT; |
| 199 | } |
| 200 | } |
| 201 | |
Manuel Pégourié-Gonnard | 2171876 | 2023-11-10 11:21:17 +0100 | [diff] [blame] | 202 | #endif /* MBEDTLS_BLOCK_CIPHER_C */ |