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/des.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_des_cbc_ctx { 18 struct crypto_cipher_ctx ctx; 19 int mbed_mode; 20 mbedtls_des_context des_ctx; 21 unsigned char iv[TEE_DES_BLOCK_SIZE]; 22 }; 23 24 static const struct crypto_cipher_ops mbed_des_cbc_ops; 25 26 static struct mbed_des_cbc_ctx *to_des_cbc_ctx(struct crypto_cipher_ctx *ctx) 27 { 28 assert(ctx && ctx->ops == &mbed_des_cbc_ops); 29 30 return container_of(ctx, struct mbed_des_cbc_ctx, ctx); 31 } 32 33 static TEE_Result mbed_des_cbc_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_des_cbc_ctx *c = to_des_cbc_ctx(ctx); 42 int mbed_res = 0; 43 44 if (key1_len != MBEDTLS_DES_KEY_SIZE) 45 return TEE_ERROR_BAD_PARAMETERS; 46 if (iv_len != sizeof(c->iv)) 47 return TEE_ERROR_BAD_PARAMETERS; 48 memcpy(c->iv, iv, sizeof(c->iv)); 49 50 mbedtls_des_init(&c->des_ctx); 51 52 if (mode == TEE_MODE_ENCRYPT) { 53 c->mbed_mode = MBEDTLS_DES_ENCRYPT; 54 mbed_res = mbedtls_des_setkey_enc(&c->des_ctx, key1); 55 } else { 56 c->mbed_mode = MBEDTLS_DES_DECRYPT; 57 mbed_res = mbedtls_des_setkey_dec(&c->des_ctx, key1); 58 } 59 60 if (mbed_res) 61 return TEE_ERROR_BAD_STATE; 62 63 return TEE_SUCCESS; 64 } 65 66 static TEE_Result mbed_des_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_des_cbc_ctx *c = to_des_cbc_ctx(ctx); 72 73 if (mbedtls_des_crypt_cbc(&c->des_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_des_cbc_final(struct crypto_cipher_ctx *ctx) 81 { 82 mbedtls_des_free(&to_des_cbc_ctx(ctx)->des_ctx); 83 } 84 85 static void mbed_des_cbc_free_ctx(struct crypto_cipher_ctx *ctx) 86 { 87 free(to_des_cbc_ctx(ctx)); 88 } 89 90 static void mbed_des_cbc_copy_state(struct crypto_cipher_ctx *dst_ctx, 91 struct crypto_cipher_ctx *src_ctx) 92 { 93 struct mbed_des_cbc_ctx *src = to_des_cbc_ctx(src_ctx); 94 struct mbed_des_cbc_ctx *dst = to_des_cbc_ctx(dst_ctx); 95 96 memcpy(dst->iv, src->iv, sizeof(dst->iv)); 97 dst->mbed_mode = src->mbed_mode; 98 dst->des_ctx = src->des_ctx; 99 } 100 101 static const struct crypto_cipher_ops mbed_des_cbc_ops = { 102 .init = mbed_des_cbc_init, 103 .update = mbed_des_cbc_update, 104 .final = mbed_des_cbc_final, 105 .free_ctx = mbed_des_cbc_free_ctx, 106 .copy_state = mbed_des_cbc_copy_state, 107 }; 108 109 TEE_Result crypto_des_cbc_alloc_ctx(struct crypto_cipher_ctx **ctx_ret) 110 { 111 struct mbed_des_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_des_cbc_ops; 118 *ctx_ret = &c->ctx; 119 120 return TEE_SUCCESS; 121 } 122