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.h> 9 #include <crypto/crypto_impl.h> 10 #include <mbedtls/aes.h> 11 #include <stdlib.h> 12 #include <string.h> 13 #include <tee_api_types.h> 14 #include <utee_defines.h> 15 #include <util.h> 16 17 struct mbed_aes_ctr_ctx { 18 struct crypto_cipher_ctx ctx; 19 mbedtls_aes_context aes_ctx; 20 size_t nc_off; 21 unsigned char counter[TEE_AES_BLOCK_SIZE]; 22 unsigned char block[TEE_AES_BLOCK_SIZE]; 23 }; 24 25 static const struct crypto_cipher_ops mbed_aes_ctr_ops; 26 27 static struct mbed_aes_ctr_ctx *to_aes_ctr_ctx(struct crypto_cipher_ctx *ctx) 28 { 29 assert(ctx && ctx->ops == &mbed_aes_ctr_ops); 30 31 return container_of(ctx, struct mbed_aes_ctr_ctx, ctx); 32 } 33 34 static TEE_Result mbed_aes_ctr_init(struct crypto_cipher_ctx *ctx, 35 TEE_OperationMode mode __unused, 36 const uint8_t *key1, 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_ctr_ctx *c = to_aes_ctr_ctx(ctx); 42 43 if (iv_len != sizeof(c->counter)) 44 return TEE_ERROR_BAD_PARAMETERS; 45 memcpy(c->counter, iv, sizeof(c->counter)); 46 47 mbedtls_aes_init(&c->aes_ctx); 48 c->nc_off = 0; 49 50 if (mbedtls_aes_setkey_enc(&c->aes_ctx, key1, key1_len * 8)) 51 return TEE_ERROR_BAD_STATE; 52 53 return TEE_SUCCESS; 54 } 55 56 static TEE_Result mbed_aes_ctr_update(struct crypto_cipher_ctx *ctx, 57 bool last_block __unused, 58 const uint8_t *data, size_t len, 59 uint8_t *dst) 60 { 61 struct mbed_aes_ctr_ctx *c = to_aes_ctr_ctx(ctx); 62 63 if (mbedtls_aes_crypt_ctr(&c->aes_ctx, len, &c->nc_off, c->counter, 64 c->block, data, dst)) 65 return TEE_ERROR_BAD_STATE; 66 67 return TEE_SUCCESS; 68 } 69 70 static void mbed_aes_ctr_final(struct crypto_cipher_ctx *ctx) 71 { 72 struct mbed_aes_ctr_ctx *c = to_aes_ctr_ctx(ctx); 73 74 mbedtls_aes_free(&c->aes_ctx); 75 memset(c->block, 0, sizeof(c->block)); 76 } 77 78 static void mbed_aes_ctr_free_ctx(struct crypto_cipher_ctx *ctx) 79 { 80 free(to_aes_ctr_ctx(ctx)); 81 } 82 83 static void mbed_aes_ctr_copy_state(struct crypto_cipher_ctx *dst_ctx, 84 struct crypto_cipher_ctx *src_ctx) 85 { 86 struct mbed_aes_ctr_ctx *src = to_aes_ctr_ctx(src_ctx); 87 struct mbed_aes_ctr_ctx *dst = to_aes_ctr_ctx(dst_ctx); 88 89 memcpy(dst->counter, src->counter, sizeof(dst->counter)); 90 memcpy(dst->block, src->block, sizeof(dst->block)); 91 dst->nc_off = src->nc_off; 92 dst->aes_ctx = src->aes_ctx; 93 } 94 95 static const struct crypto_cipher_ops mbed_aes_ctr_ops = { 96 .init = mbed_aes_ctr_init, 97 .update = mbed_aes_ctr_update, 98 .final = mbed_aes_ctr_final, 99 .free_ctx = mbed_aes_ctr_free_ctx, 100 .copy_state = mbed_aes_ctr_copy_state, 101 }; 102 103 TEE_Result crypto_aes_ctr_alloc_ctx(struct crypto_cipher_ctx **ctx_ret) 104 { 105 struct mbed_aes_ctr_ctx *c = NULL; 106 107 c = calloc(1, sizeof(*c)); 108 if (!c) 109 return TEE_ERROR_OUT_OF_MEMORY; 110 111 c->ctx.ops = &mbed_aes_ctr_ops; 112 *ctx_ret = &c->ctx; 113 114 return TEE_SUCCESS; 115 } 116