1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2014-2019, Linaro Limited 4 */ 5 6 #include <assert.h> 7 #include <crypto/crypto.h> 8 #include <crypto/crypto_impl.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <tee_api_types.h> 12 #include <tomcrypt_private.h> 13 #include <utee_defines.h> 14 #include <util.h> 15 16 struct ltc_hmac_ctx { 17 struct crypto_mac_ctx ctx; 18 int hash_idx; 19 hmac_state state; 20 }; 21 22 static const struct crypto_mac_ops ltc_hmac_ops; 23 24 static struct ltc_hmac_ctx *to_hmac_ctx(struct crypto_mac_ctx *ctx) 25 { 26 assert(ctx && ctx->ops == <c_hmac_ops); 27 28 return container_of(ctx, struct ltc_hmac_ctx, ctx); 29 } 30 31 static TEE_Result ltc_hmac_init(struct crypto_mac_ctx *ctx, const uint8_t *key, 32 size_t len) 33 { 34 struct ltc_hmac_ctx *hc = to_hmac_ctx(ctx); 35 36 if (hmac_init(&hc->state, hc->hash_idx, key, len) == CRYPT_OK) 37 return TEE_SUCCESS; 38 else 39 return TEE_ERROR_BAD_STATE; 40 } 41 42 static TEE_Result ltc_hmac_update(struct crypto_mac_ctx *ctx, 43 const uint8_t *data, size_t len) 44 { 45 if (hmac_process(&to_hmac_ctx(ctx)->state, data, len) == CRYPT_OK) 46 return TEE_SUCCESS; 47 else 48 return TEE_ERROR_BAD_STATE; 49 } 50 51 static TEE_Result ltc_hmac_final(struct crypto_mac_ctx *ctx, uint8_t *digest, 52 size_t len) 53 { 54 unsigned long l = len; 55 56 if (hmac_done(&to_hmac_ctx(ctx)->state, digest, &l) == CRYPT_OK) 57 return TEE_SUCCESS; 58 else 59 return TEE_ERROR_BAD_STATE; 60 } 61 62 static void ltc_hmac_free_ctx(struct crypto_mac_ctx *ctx) 63 { 64 free(to_hmac_ctx(ctx)); 65 } 66 67 static void ltc_hmac_copy_state(struct crypto_mac_ctx *dst_ctx, 68 struct crypto_mac_ctx *src_ctx) 69 { 70 struct ltc_hmac_ctx *src = to_hmac_ctx(src_ctx); 71 struct ltc_hmac_ctx *dst = to_hmac_ctx(dst_ctx); 72 73 assert(src->hash_idx == dst->hash_idx); 74 dst->state = src->state; 75 } 76 77 static const struct crypto_mac_ops ltc_hmac_ops = { 78 .init = ltc_hmac_init, 79 .update = ltc_hmac_update, 80 .final = ltc_hmac_final, 81 .free_ctx = ltc_hmac_free_ctx, 82 .copy_state = ltc_hmac_copy_state, 83 }; 84 85 static TEE_Result ltc_hmac_alloc_ctx(struct crypto_mac_ctx **ctx_ret, 86 int hash_idx) 87 { 88 struct ltc_hmac_ctx *ctx = NULL; 89 90 if (hash_idx < 0) 91 return TEE_ERROR_NOT_SUPPORTED; 92 93 ctx = calloc(1, sizeof(*ctx)); 94 if (!ctx) 95 return TEE_ERROR_OUT_OF_MEMORY; 96 97 ctx->ctx.ops = <c_hmac_ops; 98 ctx->hash_idx = hash_idx; 99 *ctx_ret = &ctx->ctx; 100 101 return TEE_SUCCESS; 102 } 103 104 #if defined(_CFG_CORE_LTC_MD5) 105 TEE_Result crypto_hmac_md5_alloc_ctx(struct crypto_mac_ctx **ctx) 106 { 107 return ltc_hmac_alloc_ctx(ctx, find_hash("md5")); 108 } 109 #endif 110 111 #if defined(_CFG_CORE_LTC_SHA1) 112 TEE_Result crypto_hmac_sha1_alloc_ctx(struct crypto_mac_ctx **ctx) 113 { 114 return ltc_hmac_alloc_ctx(ctx, find_hash("sha1")); 115 } 116 #endif 117 118 #if defined(_CFG_CORE_LTC_SHA224) 119 TEE_Result crypto_hmac_sha224_alloc_ctx(struct crypto_mac_ctx **ctx) 120 { 121 return ltc_hmac_alloc_ctx(ctx, find_hash("sha224")); 122 } 123 #endif 124 125 #if defined(_CFG_CORE_LTC_SHA256) 126 TEE_Result crypto_hmac_sha256_alloc_ctx(struct crypto_mac_ctx **ctx) 127 { 128 return ltc_hmac_alloc_ctx(ctx, find_hash("sha256")); 129 } 130 #endif 131 132 #if defined(_CFG_CORE_LTC_SHA384) 133 TEE_Result crypto_hmac_sha384_alloc_ctx(struct crypto_mac_ctx **ctx) 134 { 135 return ltc_hmac_alloc_ctx(ctx, find_hash("sha384")); 136 } 137 #endif 138 139 #if defined(_CFG_CORE_LTC_SHA512) 140 TEE_Result crypto_hmac_sha512_alloc_ctx(struct crypto_mac_ctx **ctx) 141 { 142 return ltc_hmac_alloc_ctx(ctx, find_hash("sha512")); 143 } 144 #endif 145 146 #if defined(_CFG_CORE_LTC_SHA3_224) 147 TEE_Result crypto_hmac_sha3_224_alloc_ctx(struct crypto_mac_ctx **ctx) 148 { 149 return ltc_hmac_alloc_ctx(ctx, find_hash("sha3-224")); 150 } 151 #endif 152 153 #if defined(_CFG_CORE_LTC_SHA3_256) 154 TEE_Result crypto_hmac_sha3_256_alloc_ctx(struct crypto_mac_ctx **ctx) 155 { 156 return ltc_hmac_alloc_ctx(ctx, find_hash("sha3-256")); 157 } 158 #endif 159 160 #if defined(_CFG_CORE_LTC_SHA3_384) 161 TEE_Result crypto_hmac_sha3_384_alloc_ctx(struct crypto_mac_ctx **ctx) 162 { 163 return ltc_hmac_alloc_ctx(ctx, find_hash("sha3-384")); 164 } 165 #endif 166 167 #if defined(_CFG_CORE_LTC_SHA3_512) 168 TEE_Result crypto_hmac_sha3_512_alloc_ctx(struct crypto_mac_ctx **ctx) 169 { 170 return ltc_hmac_alloc_ctx(ctx, find_hash("sha3-512")); 171 } 172 #endif 173