1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "log.h"
17 #include "utils.h"
18 #include "fillp_function.h"
19 #include "spunge.h"
20 #include "hmac.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 #define CSE_DATA_ONE_PAR 17
26 #define CSE_DATA_TWO_PAR 63
27 /*
28 Description: Internal data for cleansing the data
29 Value Range: None
30 Access: Used to store internal data for cleansing the data
31 */
32 /*****************************************************************************
33 Function :void FillpCleanSedata(void *ptr, size_t len)
34
35 Description : Clean and fill the memory with random bytes
36
37 Input : ptr - Pointer to buffer which needs to be cleansed.
38 len - Length of the buffer.
39
40 Output : NA
41 Return : NA
42
43 ******************************************************************************/
FillpCleanSedata(void * ptr,size_t len,struct SpungeInstance * pcbInst)44 static void FillpCleanSedata(void *ptr, size_t len, struct SpungeInstance *pcbInst)
45 {
46 if (ptr == FILLP_NULL_PTR) {
47 return;
48 }
49
50 FILLP_UINT8 *pptr = ptr;
51 size_t loop = len;
52 size_t ctr = pcbInst->cleanseDataCtr;
53
54 while (loop > 0) {
55 *(pptr++) = (FILLP_UINT8)ctr;
56 ctr += (CSE_DATA_ONE_PAR + ((size_t)(uintptr_t)pptr & 0xF));
57 loop--;
58 }
59
60 pptr = FILLP_MEMCHR(ptr, (FILLP_UINT8)ctr, len);
61 if (pptr != FILLP_NULL_PTR) {
62 ctr += (CSE_DATA_TWO_PAR + (size_t)(uintptr_t)pptr);
63 }
64
65 pcbInst->cleanseDataCtr = (FILLP_UINT8)ctr;
66 }
67
FillpHmacSha256Init(OUT FillpHmacSha256 ctx[1],IN FILLP_UINT8 * key,FILLP_UINT32 klen,struct SpungeInstance * pcbInst)68 void FillpHmacSha256Init(OUT FillpHmacSha256 ctx[1], IN FILLP_UINT8 *key, FILLP_UINT32 klen,
69 struct SpungeInstance *pcbInst)
70 {
71 FillpHmacSha256Ctx *tempCtx = (FillpHmacSha256Ctx *)ctx;
72
73 /* inner padding - key XORed with ipad */
74 FILLP_UINT8 keyIpad[FILLP_SHA256_BLOCK_SIZE];
75
76 /* outer padding - key XORed with opad */
77 FILLP_UINT8 keyOpad[FILLP_SHA256_BLOCK_SIZE];
78 FILLP_UINT8 tk[FILLP_SHA256_DIGEST_SIZE];
79 FILLP_UINT i;
80
81 /* Null context */
82 if (tempCtx == FILLP_NULL_PTR) {
83 FILLP_LOGERR("FillpHmacSha256Init - Null Context ");
84 return;
85 }
86 if ((key == FILLP_NULL_PTR) || (klen == 0)) {
87 FILLP_LOGERR("FillpHmacSha256Init - Invalid Parameters passed ");
88 return;
89 }
90 if (klen > FILLP_SHA256_BLOCK_SIZE) {
91 FillpSha256Ctx tctx;
92 FillpSha256Set(&tctx);
93 FillpSha256Upd(&tctx, key, klen);
94 FillpSha256Fin(&tctx, tk, FILLP_SHA256_DIGEST_SIZE);
95 key = (FILLP_UINT8 *)tk;
96 klen = FILLP_SHA256_DIGEST_SIZE;
97 FILLP_UNUSED_PARA(tctx);
98 }
99 (void)memset_s(keyIpad, FILLP_SHA256_BLOCK_SIZE, 0, FILLP_SHA256_BLOCK_SIZE);
100 (void)memset_s(keyOpad, FILLP_SHA256_BLOCK_SIZE, 0, FILLP_SHA256_BLOCK_SIZE);
101 FillpErrorType err = memcpy_s(keyIpad, klen, key, klen);
102 if (err != EOK) {
103 FILLP_LOGERR("FillpHmacSha256Init memcpy_s keyIpad failed: %d ", err);
104 return;
105 }
106 err = memcpy_s(keyOpad, klen, key, klen);
107 if (err != EOK) {
108 FILLP_LOGERR("FillpHmacSha256Init memcpy_s keyOpad failed: %d ", err);
109 return;
110 }
111 for (i = 0; i < FILLP_SHA256_BLOCK_SIZE; i++) {
112 keyIpad[i] = keyIpad[i] ^ FILLP_HMAC_IPAD;
113 keyOpad[i] = keyOpad[i] ^ FILLP_HMAC_OPAD;
114 }
115
116 FillpSha256Set(tempCtx->hashki);
117 FillpSha256Upd(tempCtx->hashki, keyIpad, FILLP_SHA256_BLOCK_SIZE);
118 FillpSha256Set(tempCtx->hashko);
119 FillpSha256Upd(tempCtx->hashko, keyOpad, FILLP_SHA256_BLOCK_SIZE);
120
121 FillpCleanSedata((void *)tk, sizeof(tk), pcbInst);
122 FillpCleanSedata((void *)keyIpad, sizeof(keyIpad), pcbInst);
123 FillpCleanSedata((void *)keyOpad, sizeof(keyOpad), pcbInst);
124 }
125
126
FillpHmacSha256Update(IO FillpHmacSha256 ctx[1],FILLP_CONST FILLP_UINT8 * data,FILLP_UINT32 dlen)127 void FillpHmacSha256Update(IO FillpHmacSha256 ctx[1], FILLP_CONST FILLP_UINT8 *data, FILLP_UINT32 dlen)
128 {
129 FillpHmacSha256Ctx *tempCtx = (FillpHmacSha256Ctx *)ctx;
130
131 /* Null context */
132 if (tempCtx == FILLP_NULL_PTR) {
133 FILLP_LOGERR("FillpHmacSha256Update - Null Context ");
134 return;
135 }
136
137 if ((dlen == 0) && (data == FILLP_NULL_PTR)) {
138 FILLP_UINT8 x = 0;
139 FillpSha256Upd(tempCtx->hashki, &x, 0);
140 FILLP_UNUSED_PARA(x);
141 } else if (data == FILLP_NULL_PTR) {
142 FILLP_LOGERR("FillpHmacSha256Update - Null data ");
143 return;
144 } else {
145 FillpSha256Upd(tempCtx->hashki, data, dlen);
146 }
147 }
148
FillpHmacSha256Final(IO FillpHmacSha256 ctx[1],OUT FILLP_UINT8 digest[FILLP_SHA256_DIGEST_SIZE],FILLP_UINT32 size)149 void FillpHmacSha256Final(IO FillpHmacSha256 ctx[1],
150 OUT FILLP_UINT8 digest[FILLP_SHA256_DIGEST_SIZE], FILLP_UINT32 size)
151 {
152 FillpHmacSha256Ctx *tempCtx = (FillpHmacSha256Ctx *)ctx;
153 if (tempCtx == FILLP_NULL_PTR) {
154 FILLP_LOGERR("FillpHmacSha256Final - Null Context ");
155 return;
156 }
157
158 if (digest == FILLP_NULL_PTR) {
159 FILLP_LOGERR("FillpHmacSha256Final - invalid argument ");
160 return;
161 }
162
163 FillpSha256Fin(tempCtx->hashki, digest, size);
164 FillpSha256Upd(tempCtx->hashko, digest, size);
165 FillpSha256Fin(tempCtx->hashko, digest, size);
166 }
167
168
169 #ifdef __cplusplus
170 }
171 #endif
172
173