1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (C) 2018, ARM Limited
4 * Copyright (C) 2019, Linaro Limited
5 * Copyright (C) 2019 Huawei Technologies Co., Ltd
6 */
7
8 #include <assert.h>
9 #include <compiler.h>
10 #include <crypto/crypto.h>
11 #include <crypto/crypto_impl.h>
12 #include <stdlib.h>
13 #include <string_ext.h>
14 #include <string.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_hash_ctx {
22 struct crypto_hash_ctx hash_ctx;
23 struct sm3_context sm3_ctx;
24 };
25
26 static const struct crypto_hash_ops sm3_hash_ops;
27
to_hash_ctx(struct crypto_hash_ctx * ctx)28 static struct sm3_hash_ctx *to_hash_ctx(struct crypto_hash_ctx *ctx)
29 {
30 assert(ctx && ctx->ops == &sm3_hash_ops);
31
32 return container_of(ctx, struct sm3_hash_ctx, hash_ctx);
33 }
34
op_sm3_hash_init(struct crypto_hash_ctx * ctx)35 static TEE_Result op_sm3_hash_init(struct crypto_hash_ctx *ctx)
36 {
37 sm3_init(&to_hash_ctx(ctx)->sm3_ctx);
38
39 return TEE_SUCCESS;
40 }
41
op_sm3_hash_update(struct crypto_hash_ctx * ctx,const uint8_t * data,size_t len)42 static TEE_Result op_sm3_hash_update(struct crypto_hash_ctx *ctx,
43 const uint8_t *data, size_t len)
44 {
45 sm3_update(&to_hash_ctx(ctx)->sm3_ctx, data, len);
46
47 return TEE_SUCCESS;
48 }
49
op_sm3_hash_final(struct crypto_hash_ctx * ctx,uint8_t * digest,size_t len)50 static TEE_Result op_sm3_hash_final(struct crypto_hash_ctx *ctx,
51 uint8_t *digest,
52 size_t len)
53 {
54 struct sm3_hash_ctx *hc = to_hash_ctx(ctx);
55 size_t hash_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 (hash_size > len)
63 tmp_digest = block_digest; /* use a tempory buffer */
64 else
65 tmp_digest = digest;
66
67 sm3_final(&hc->sm3_ctx, tmp_digest);
68
69 if (hash_size > len)
70 memcpy(digest, tmp_digest, len);
71
72 return TEE_SUCCESS;
73 }
74
op_sm3_hash_free_ctx(struct crypto_hash_ctx * ctx)75 static void op_sm3_hash_free_ctx(struct crypto_hash_ctx *ctx)
76 {
77 struct sm3_hash_ctx *hc = to_hash_ctx(ctx);
78
79 memzero_explicit(&hc->sm3_ctx, sizeof(hc->sm3_ctx));
80 free(hc);
81 }
82
op_sm3_hash_copy_state(struct crypto_hash_ctx * dst_ctx,struct crypto_hash_ctx * src_ctx)83 static void op_sm3_hash_copy_state(struct crypto_hash_ctx *dst_ctx,
84 struct crypto_hash_ctx *src_ctx)
85 {
86 struct sm3_hash_ctx *src = to_hash_ctx(src_ctx);
87 struct sm3_hash_ctx *dst = to_hash_ctx(dst_ctx);
88
89 dst->sm3_ctx = src->sm3_ctx;
90 }
91
92 static const struct crypto_hash_ops sm3_hash_ops = {
93 .init = op_sm3_hash_init,
94 .update = op_sm3_hash_update,
95 .final = op_sm3_hash_final,
96 .free_ctx = op_sm3_hash_free_ctx,
97 .copy_state = op_sm3_hash_copy_state,
98 };
99
crypto_sm3_alloc_ctx(struct crypto_hash_ctx ** ctx)100 TEE_Result crypto_sm3_alloc_ctx(struct crypto_hash_ctx **ctx)
101 {
102 struct sm3_hash_ctx *hc = NULL;
103
104 hc = calloc(1, sizeof(*hc));
105 if (!hc)
106 return TEE_ERROR_OUT_OF_MEMORY;
107
108 hc->hash_ctx.ops = &sm3_hash_ops;
109
110 *ctx = &hc->hash_ctx;
111
112 return TEE_SUCCESS;
113 }
114