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 <stdlib.h>
13 #include <string.h>
14 #include <string_ext.h>
15 #include <tee_api_types.h>
16 #include <utee_defines.h>
17 #include <util.h>
18
19 #include "sm3.h"
20
21 struct sm3_hmac_ctx {
22 struct crypto_mac_ctx mac_ctx;
23 struct sm3_context sm3_ctx;
24 };
25
26 static const struct crypto_mac_ops sm3_hmac_ops;
27
to_hmac_ctx(struct crypto_mac_ctx * ctx)28 static struct sm3_hmac_ctx *to_hmac_ctx(struct crypto_mac_ctx *ctx)
29 {
30 assert(ctx && ctx->ops == &sm3_hmac_ops);
31
32 return container_of(ctx, struct sm3_hmac_ctx, mac_ctx);
33 }
34
op_sm3_hmac_init(struct crypto_mac_ctx * ctx,const uint8_t * key,size_t len)35 static TEE_Result op_sm3_hmac_init(struct crypto_mac_ctx *ctx,
36 const uint8_t *key, size_t len)
37 {
38 sm3_hmac_init(&to_hmac_ctx(ctx)->sm3_ctx, key, len);
39
40 return TEE_SUCCESS;
41 }
42
op_sm3_hmac_update(struct crypto_mac_ctx * ctx,const uint8_t * data,size_t len)43 static TEE_Result op_sm3_hmac_update(struct crypto_mac_ctx *ctx,
44 const uint8_t *data, size_t len)
45 {
46 sm3_hmac_update(&to_hmac_ctx(ctx)->sm3_ctx, data, len);
47
48 return TEE_SUCCESS;
49 }
50
op_sm3_hmac_final(struct crypto_mac_ctx * ctx,uint8_t * digest,size_t len)51 static TEE_Result op_sm3_hmac_final(struct crypto_mac_ctx *ctx, uint8_t *digest,
52 size_t len)
53 {
54 struct sm3_hmac_ctx *c = to_hmac_ctx(ctx);
55 size_t hmac_size = TEE_SM3_HASH_SIZE;
56 uint8_t block_digest[TEE_SM3_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 tmp_digest = block_digest; /* use a tempory buffer */
64 else
65 tmp_digest = digest;
66
67 sm3_hmac_final(&c->sm3_ctx, tmp_digest);
68
69 if (hmac_size > len)
70 memcpy(digest, tmp_digest, len);
71
72 return TEE_SUCCESS;
73 }
74
op_sm3_hmac_free_ctx(struct crypto_mac_ctx * ctx)75 static void op_sm3_hmac_free_ctx(struct crypto_mac_ctx *ctx)
76 {
77 struct sm3_hmac_ctx *c = to_hmac_ctx(ctx);
78
79 memzero_explicit(&c->sm3_ctx, sizeof(c->sm3_ctx));
80 free(c);
81 }
82
op_sm3_hmac_copy_state(struct crypto_mac_ctx * dst_ctx,struct crypto_mac_ctx * src_ctx)83 static void op_sm3_hmac_copy_state(struct crypto_mac_ctx *dst_ctx,
84 struct crypto_mac_ctx *src_ctx)
85 {
86 struct sm3_hmac_ctx *src = to_hmac_ctx(src_ctx);
87 struct sm3_hmac_ctx *dst = to_hmac_ctx(dst_ctx);
88
89 dst->sm3_ctx = src->sm3_ctx;
90 }
91
92 static const struct crypto_mac_ops sm3_hmac_ops = {
93 .init = op_sm3_hmac_init,
94 .update = op_sm3_hmac_update,
95 .final = op_sm3_hmac_final,
96 .free_ctx = op_sm3_hmac_free_ctx,
97 .copy_state = op_sm3_hmac_copy_state,
98 };
99
crypto_hmac_sm3_alloc_ctx(struct crypto_mac_ctx ** ctx)100 TEE_Result crypto_hmac_sm3_alloc_ctx(struct crypto_mac_ctx **ctx)
101 {
102 struct sm3_hmac_ctx *c = NULL;
103
104 c = calloc(1, sizeof(*c));
105 if (!c)
106 return TEE_ERROR_OUT_OF_MEMORY;
107
108 c->mac_ctx.ops = &sm3_hmac_ops;
109
110 *ctx = &c->mac_ctx;
111
112 return TEE_SUCCESS;
113 }
114