1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (C) 2018, ARM Limited 4 * Copyright (C) 2019, Linaro Limited 5 */ 6 7 #include <assert.h> 8 #include <compiler.h> 9 #include <crypto/crypto.h> 10 #include <crypto/crypto_impl.h> 11 #include <kernel/panic.h> 12 #include <mbedtls/md.h> 13 #include <stdlib.h> 14 #include <string.h> 15 #include <tee_api_types.h> 16 #include <utee_defines.h> 17 #include <util.h> 18 19 struct mbed_hmac_ctx { 20 struct crypto_mac_ctx mac_ctx; 21 mbedtls_md_context_t md_ctx; 22 }; 23 24 static const struct crypto_mac_ops mbed_hmac_ops; 25 26 static struct mbed_hmac_ctx *to_hmac_ctx(struct crypto_mac_ctx *ctx) 27 { 28 assert(ctx && ctx->ops == &mbed_hmac_ops); 29 30 return container_of(ctx, struct mbed_hmac_ctx, mac_ctx); 31 } 32 33 static TEE_Result mbed_hmac_init(struct crypto_mac_ctx *ctx, 34 const uint8_t *key, size_t len) 35 { 36 if (mbedtls_md_hmac_starts(&to_hmac_ctx(ctx)->md_ctx, key, len)) 37 return TEE_ERROR_BAD_STATE; 38 39 return TEE_SUCCESS; 40 } 41 42 static TEE_Result mbed_hmac_update(struct crypto_mac_ctx *ctx, 43 const uint8_t *data, size_t len) 44 { 45 if (mbedtls_md_hmac_update(&to_hmac_ctx(ctx)->md_ctx, data, len)) 46 return TEE_ERROR_BAD_STATE; 47 48 return TEE_SUCCESS; 49 } 50 51 static TEE_Result mbed_hmac_final(struct crypto_mac_ctx *ctx, uint8_t *digest, 52 size_t len) 53 { 54 struct mbed_hmac_ctx *c = to_hmac_ctx(ctx); 55 size_t hmac_size = mbedtls_md_get_size(c->md_ctx.md_info); 56 uint8_t block_digest[TEE_MAX_HASH_SIZE] = { 0 }; 57 uint8_t *tmp_digest = NULL; 58 59 if (len == 0) 60 return TEE_ERROR_BAD_PARAMETERS; 61 62 if (hmac_size > len) { 63 if (hmac_size > sizeof(block_digest)) 64 return TEE_ERROR_BAD_STATE; 65 tmp_digest = block_digest; /* use a tempory buffer */ 66 } else { 67 tmp_digest = digest; 68 } 69 70 if (mbedtls_md_hmac_finish(&c->md_ctx, tmp_digest)) 71 return TEE_ERROR_BAD_STATE; 72 73 if (hmac_size > len) 74 memcpy(digest, tmp_digest, len); 75 76 return TEE_SUCCESS; 77 } 78 79 static void mbed_hmac_free_ctx(struct crypto_mac_ctx *ctx) 80 { 81 struct mbed_hmac_ctx *c = to_hmac_ctx(ctx); 82 83 mbedtls_md_free(&c->md_ctx); 84 free(c); 85 } 86 87 static void mbed_hmac_copy_state(struct crypto_mac_ctx *dst_ctx, 88 struct crypto_mac_ctx *src_ctx) 89 { 90 struct mbed_hmac_ctx *src = to_hmac_ctx(src_ctx); 91 struct mbed_hmac_ctx *dst = to_hmac_ctx(dst_ctx); 92 93 if (mbedtls_md_clone(&dst->md_ctx, &src->md_ctx)) 94 panic(); 95 } 96 97 static const struct crypto_mac_ops mbed_hmac_ops = { 98 .init = mbed_hmac_init, 99 .update = mbed_hmac_update, 100 .final = mbed_hmac_final, 101 .free_ctx = mbed_hmac_free_ctx, 102 .copy_state = mbed_hmac_copy_state, 103 }; 104 105 static TEE_Result mbed_hmac_alloc_ctx(struct crypto_mac_ctx **ctx_ret, 106 mbedtls_md_type_t md_type) 107 { 108 int mbed_res = 0; 109 struct mbed_hmac_ctx *c = NULL; 110 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_type); 111 112 if (!md_info) 113 return TEE_ERROR_NOT_SUPPORTED; 114 115 c = calloc(1, sizeof(*c)); 116 if (!c) 117 return TEE_ERROR_OUT_OF_MEMORY; 118 119 c->mac_ctx.ops = &mbed_hmac_ops; 120 mbed_res = mbedtls_md_setup(&c->md_ctx, md_info, 1); 121 if (mbed_res) { 122 free(c); 123 if (mbed_res == MBEDTLS_ERR_MD_ALLOC_FAILED) 124 return TEE_ERROR_OUT_OF_MEMORY; 125 return TEE_ERROR_NOT_SUPPORTED; 126 } 127 128 *ctx_ret = &c->mac_ctx; 129 130 return TEE_SUCCESS; 131 } 132 133 #if defined(CFG_CRYPTO_MD5) 134 TEE_Result crypto_hmac_md5_alloc_ctx(struct crypto_mac_ctx **ctx) 135 { 136 return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_MD5); 137 } 138 #endif 139 140 #if defined(CFG_CRYPTO_SHA1) 141 TEE_Result crypto_hmac_sha1_alloc_ctx(struct crypto_mac_ctx **ctx) 142 { 143 return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA1); 144 } 145 #endif 146 147 #if defined(CFG_CRYPTO_SHA224) 148 TEE_Result crypto_hmac_sha224_alloc_ctx(struct crypto_mac_ctx **ctx) 149 { 150 return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA224); 151 } 152 #endif 153 154 #if defined(CFG_CRYPTO_SHA256) 155 TEE_Result crypto_hmac_sha256_alloc_ctx(struct crypto_mac_ctx **ctx) 156 { 157 return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA256); 158 } 159 #endif 160 161 #if defined(CFG_CRYPTO_SHA384) 162 TEE_Result crypto_hmac_sha384_alloc_ctx(struct crypto_mac_ctx **ctx) 163 { 164 return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA384); 165 } 166 #endif 167 168 #if defined(CFG_CRYPTO_SHA512) 169 TEE_Result crypto_hmac_sha512_alloc_ctx(struct crypto_mac_ctx **ctx) 170 { 171 return mbed_hmac_alloc_ctx(ctx, MBEDTLS_MD_SHA512); 172 } 173 #endif 174