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_ecb_ctx { 19 struct crypto_cipher_ctx ctx; 20 int mbed_mode; 21 mbedtls_aes_context aes_ctx; 22 }; 23 24 static const struct crypto_cipher_ops mbed_aes_ecb_ops; 25 26 static struct mbed_aes_ecb_ctx *to_aes_ecb_ctx(struct crypto_cipher_ctx *ctx) 27 { 28 assert(ctx && ctx->ops == &mbed_aes_ecb_ops); 29 30 return container_of(ctx, struct mbed_aes_ecb_ctx, ctx); 31 } 32 33 static TEE_Result mbed_aes_ecb_init(struct crypto_cipher_ctx *ctx, 34 TEE_OperationMode mode, const uint8_t *key1, 35 size_t key1_len, 36 const uint8_t *key2 __unused, 37 size_t key2_len __unused, 38 const uint8_t *iv __unused, 39 size_t iv_len __unused) 40 { 41 struct mbed_aes_ecb_ctx *c = to_aes_ecb_ctx(ctx); 42 int mbed_res = 0; 43 44 mbedtls_aes_init(&c->aes_ctx); 45 46 if (mode == TEE_MODE_ENCRYPT) { 47 c->mbed_mode = MBEDTLS_AES_ENCRYPT; 48 mbed_res = mbedtls_aes_setkey_enc(&c->aes_ctx, key1, 49 key1_len * 8); 50 } else { 51 c->mbed_mode = MBEDTLS_AES_DECRYPT; 52 mbed_res = mbedtls_aes_setkey_dec(&c->aes_ctx, key1, 53 key1_len * 8); 54 } 55 56 if (mbed_res) 57 return TEE_ERROR_BAD_STATE; 58 59 return TEE_SUCCESS; 60 } 61 62 static TEE_Result mbed_aes_ecb_update(struct crypto_cipher_ctx *ctx, 63 bool last_block __unused, 64 const uint8_t *data, size_t len, 65 uint8_t *dst) 66 { 67 struct mbed_aes_ecb_ctx *c = to_aes_ecb_ctx(ctx); 68 size_t block_size = TEE_AES_BLOCK_SIZE; 69 size_t offs = 0; 70 71 if (len % block_size) 72 return TEE_ERROR_BAD_PARAMETERS; 73 74 for (offs = 0; offs < len; offs += block_size) { 75 if (mbedtls_aes_crypt_ecb(&c->aes_ctx, c->mbed_mode, 76 data + offs, dst + offs)) 77 return TEE_ERROR_BAD_STATE; 78 } 79 80 return TEE_SUCCESS; 81 } 82 83 static void mbed_aes_ecb_final(struct crypto_cipher_ctx *ctx) 84 { 85 mbedtls_aes_free(&to_aes_ecb_ctx(ctx)->aes_ctx); 86 } 87 88 static void mbed_aes_ecb_free_ctx(struct crypto_cipher_ctx *ctx) 89 { 90 free(to_aes_ecb_ctx(ctx)); 91 } 92 93 static void mbed_aes_ecb_copy_state(struct crypto_cipher_ctx *dst_ctx, 94 struct crypto_cipher_ctx *src_ctx) 95 { 96 struct mbed_aes_ecb_ctx *src = to_aes_ecb_ctx(src_ctx); 97 struct mbed_aes_ecb_ctx *dst = to_aes_ecb_ctx(dst_ctx); 98 99 dst->mbed_mode = src->mbed_mode; 100 dst->aes_ctx = src->aes_ctx; 101 } 102 103 static const struct crypto_cipher_ops mbed_aes_ecb_ops = { 104 .init = mbed_aes_ecb_init, 105 .update = mbed_aes_ecb_update, 106 .final = mbed_aes_ecb_final, 107 .free_ctx = mbed_aes_ecb_free_ctx, 108 .copy_state = mbed_aes_ecb_copy_state, 109 }; 110 111 TEE_Result crypto_aes_ecb_alloc_ctx(struct crypto_cipher_ctx **ctx_ret) 112 { 113 struct mbed_aes_ecb_ctx *c = NULL; 114 115 c = calloc(1, sizeof(*c)); 116 if (!c) 117 return TEE_ERROR_OUT_OF_MEMORY; 118 119 c->ctx.ops = &mbed_aes_ecb_ops; 120 *ctx_ret = &c->ctx; 121 122 return TEE_SUCCESS; 123 } 124 125 #if defined(MBEDTLS_AES_ALT) 126 int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx, int mode, 127 const unsigned char input[16], 128 unsigned char output[16]) 129 130 { 131 if (mode == MBEDTLS_AES_ENCRYPT) 132 crypto_accel_aes_ecb_enc(output, input, ctx->key, 133 ctx->round_count, 1); 134 else 135 crypto_accel_aes_ecb_dec(output, input, ctx->key, 136 ctx->round_count, 1); 137 138 return 0; 139 } 140 #endif /*MBEDTLS_AES_ALT*/ 141