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 
to_hmac_ctx(struct crypto_mac_ctx * ctx)24 static struct ltc_hmac_ctx *to_hmac_ctx(struct crypto_mac_ctx *ctx)
25 {
26 	assert(ctx && ctx->ops == &ltc_hmac_ops);
27 
28 	return container_of(ctx, struct ltc_hmac_ctx, ctx);
29 }
30 
ltc_hmac_init(struct crypto_mac_ctx * ctx,const uint8_t * key,size_t len)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 
ltc_hmac_update(struct crypto_mac_ctx * ctx,const uint8_t * data,size_t len)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 
ltc_hmac_final(struct crypto_mac_ctx * ctx,uint8_t * digest,size_t len)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 
ltc_hmac_free_ctx(struct crypto_mac_ctx * ctx)62 static void ltc_hmac_free_ctx(struct crypto_mac_ctx *ctx)
63 {
64 	free(to_hmac_ctx(ctx));
65 }
66 
ltc_hmac_copy_state(struct crypto_mac_ctx * dst_ctx,struct crypto_mac_ctx * src_ctx)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 
ltc_hmac_alloc_ctx(struct crypto_mac_ctx ** ctx_ret,int hash_idx)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 = &ltc_hmac_ops;
98 	ctx->hash_idx = hash_idx;
99 	*ctx_ret = &ctx->ctx;
100 
101 	return TEE_SUCCESS;
102 }
103 
crypto_hmac_md5_alloc_ctx(struct crypto_mac_ctx ** ctx)104 TEE_Result crypto_hmac_md5_alloc_ctx(struct crypto_mac_ctx **ctx)
105 {
106 	return ltc_hmac_alloc_ctx(ctx, find_hash("md5"));
107 }
108 
crypto_hmac_sha1_alloc_ctx(struct crypto_mac_ctx ** ctx)109 TEE_Result crypto_hmac_sha1_alloc_ctx(struct crypto_mac_ctx **ctx)
110 {
111 	return ltc_hmac_alloc_ctx(ctx, find_hash("sha1"));
112 }
113 
crypto_hmac_sha224_alloc_ctx(struct crypto_mac_ctx ** ctx)114 TEE_Result crypto_hmac_sha224_alloc_ctx(struct crypto_mac_ctx **ctx)
115 {
116 	return ltc_hmac_alloc_ctx(ctx, find_hash("sha224"));
117 }
118 
crypto_hmac_sha256_alloc_ctx(struct crypto_mac_ctx ** ctx)119 TEE_Result crypto_hmac_sha256_alloc_ctx(struct crypto_mac_ctx **ctx)
120 {
121 	return ltc_hmac_alloc_ctx(ctx, find_hash("sha256"));
122 }
123 
crypto_hmac_sha384_alloc_ctx(struct crypto_mac_ctx ** ctx)124 TEE_Result crypto_hmac_sha384_alloc_ctx(struct crypto_mac_ctx **ctx)
125 {
126 	return ltc_hmac_alloc_ctx(ctx, find_hash("sha384"));
127 }
128 
crypto_hmac_sha512_alloc_ctx(struct crypto_mac_ctx ** ctx)129 TEE_Result crypto_hmac_sha512_alloc_ctx(struct crypto_mac_ctx **ctx)
130 {
131 	return ltc_hmac_alloc_ctx(ctx, find_hash("sha512"));
132 }
133