1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (C) 2019, Linaro Limited 4 */ 5 6 #include <assert.h> 7 #include <compiler.h> 8 #include <crypto/crypto_accel.h> 9 #include <crypto/crypto.h> 10 #include <crypto/crypto_impl.h> 11 #include <mbedtls/aes.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #include <tee_api_types.h> 15 #include <utee_defines.h> 16 #include <util.h> 17 18 struct mbed_aes_cbc_ctx { 19 struct crypto_cipher_ctx ctx; 20 int mbed_mode; 21 mbedtls_aes_context aes_ctx; 22 unsigned char iv[TEE_AES_BLOCK_SIZE]; 23 }; 24 25 static const struct crypto_cipher_ops mbed_aes_cbc_ops; 26 27 static struct mbed_aes_cbc_ctx *to_aes_cbc_ctx(struct crypto_cipher_ctx *ctx) 28 { 29 assert(ctx && ctx->ops == &mbed_aes_cbc_ops); 30 31 return container_of(ctx, struct mbed_aes_cbc_ctx, ctx); 32 } 33 34 static TEE_Result mbed_aes_cbc_init(struct crypto_cipher_ctx *ctx, 35 TEE_OperationMode mode, const uint8_t *key1, 36 size_t key1_len, 37 const uint8_t *key2 __unused, 38 size_t key2_len __unused, 39 const uint8_t *iv, size_t iv_len) 40 { 41 struct mbed_aes_cbc_ctx *c = to_aes_cbc_ctx(ctx); 42 int mbed_res = 0; 43 44 if (iv_len != sizeof(c->iv)) 45 return TEE_ERROR_BAD_PARAMETERS; 46 memcpy(c->iv, iv, sizeof(c->iv)); 47 48 mbedtls_aes_init(&c->aes_ctx); 49 50 if (mode == TEE_MODE_ENCRYPT) { 51 c->mbed_mode = MBEDTLS_AES_ENCRYPT; 52 mbed_res = mbedtls_aes_setkey_enc(&c->aes_ctx, key1, 53 key1_len * 8); 54 } else { 55 c->mbed_mode = MBEDTLS_AES_DECRYPT; 56 mbed_res = mbedtls_aes_setkey_dec(&c->aes_ctx, key1, 57 key1_len * 8); 58 } 59 60 if (mbed_res) 61 return TEE_ERROR_BAD_STATE; 62 63 return TEE_SUCCESS; 64 } 65 66 static TEE_Result mbed_aes_cbc_update(struct crypto_cipher_ctx *ctx, 67 bool last_block __unused, 68 const uint8_t *data, size_t len, 69 uint8_t *dst) 70 { 71 struct mbed_aes_cbc_ctx *c = to_aes_cbc_ctx(ctx); 72 73 if (mbedtls_aes_crypt_cbc(&c->aes_ctx, c->mbed_mode, len, c->iv, 74 data, dst)) 75 return TEE_ERROR_BAD_STATE; 76 77 return TEE_SUCCESS; 78 } 79 80 static void mbed_aes_cbc_final(struct crypto_cipher_ctx *ctx) 81 { 82 mbedtls_aes_free(&to_aes_cbc_ctx(ctx)->aes_ctx); 83 } 84 85 static void mbed_aes_cbc_free_ctx(struct crypto_cipher_ctx *ctx) 86 { 87 free(to_aes_cbc_ctx(ctx)); 88 } 89 90 static void mbed_aes_cbc_copy_state(struct crypto_cipher_ctx *dst_ctx, 91 struct crypto_cipher_ctx *src_ctx) 92 { 93 struct mbed_aes_cbc_ctx *src = to_aes_cbc_ctx(src_ctx); 94 struct mbed_aes_cbc_ctx *dst = to_aes_cbc_ctx(dst_ctx); 95 96 memcpy(dst->iv, src->iv, sizeof(dst->iv)); 97 dst->mbed_mode = src->mbed_mode; 98 dst->aes_ctx = src->aes_ctx; 99 } 100 101 static const struct crypto_cipher_ops mbed_aes_cbc_ops = { 102 .init = mbed_aes_cbc_init, 103 .update = mbed_aes_cbc_update, 104 .final = mbed_aes_cbc_final, 105 .free_ctx = mbed_aes_cbc_free_ctx, 106 .copy_state = mbed_aes_cbc_copy_state, 107 }; 108 109 TEE_Result crypto_aes_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx_ret) 110 { 111 struct mbed_aes_cbc_ctx *c = NULL; 112 113 c = calloc(1, sizeof(*c)); 114 if (!c) 115 return TEE_ERROR_OUT_OF_MEMORY; 116 117 c->ctx.ops = &mbed_aes_cbc_ops; 118 *ctx_ret = &c->ctx; 119 120 return TEE_SUCCESS; 121 } 122 123 #if defined(MBEDTLS_AES_ALT) 124 int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx, int mode, size_t length, 125 unsigned char iv[16], const unsigned char *input, 126 unsigned char *output) 127 { 128 if (length % 16) 129 return MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH; 130 131 if (mode == MBEDTLS_AES_ENCRYPT) 132 crypto_accel_aes_cbc_enc(output, input, ctx->key, 133 ctx->round_count, length / 16, iv); 134 else 135 crypto_accel_aes_cbc_dec(output, input, ctx->key, 136 ctx->round_count, length / 16, iv); 137 138 return 0; 139 } 140 #endif /*MBEDTLS_AES_ALT*/ 141