• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     FILLP_UINT8 *pptr = ptr;
47     size_t loop = len;
48     size_t ctr = pcbInst->cleanseDataCtr;
49 
50     if (ptr == FILLP_NULL_PTR) {
51         return;
52     }
53 
54     while (loop--) {
55         *(pptr++) = (FILLP_UINT8)ctr;
56         ctr += (CSE_DATA_ONE_PAR + ((size_t)(uintptr_t)pptr & 0xF));
57     }
58 
59     pptr = FILLP_MEMCHR(ptr, (FILLP_UINT8)ctr, len);
60     if (pptr != FILLP_NULL_PTR) {
61         ctr += (CSE_DATA_TWO_PAR + (size_t)(uintptr_t)pptr);
62     }
63 
64     pcbInst->cleanseDataCtr = (FILLP_UINT8)ctr;
65 
66     return;
67 }
68 
FillpHmacSha256Init(OUT FillpHmacSha256 ctx[1],IN FILLP_UINT8 * key,FILLP_UINT32 klen,struct SpungeInstance * pcbInst)69 void FillpHmacSha256Init(OUT FillpHmacSha256 ctx[1], IN FILLP_UINT8 *key, FILLP_UINT32 klen,
70     struct SpungeInstance *pcbInst)
71 {
72     FillpHmacSha256Ctx *tempCtx = (FillpHmacSha256Ctx *)ctx;
73 
74     /* inner padding - key XORed with ipad */
75     FILLP_UINT8 keyIpad[FILLP_SHA256_BLOCK_SIZE];
76 
77     /* outer padding - key XORed with opad */
78     FILLP_UINT8 keyOpad[FILLP_SHA256_BLOCK_SIZE];
79     FILLP_UINT8 tk[FILLP_SHA256_DIGEST_SIZE];
80     FILLP_UINT i;
81 
82     /* Null context */
83     if (tempCtx == FILLP_NULL_PTR) {
84         FILLP_LOGERR("FillpHmacSha256Init - Null Context ");
85         return;
86     }
87     if ((key == FILLP_NULL_PTR) || (klen == 0)) {
88         FILLP_LOGERR("FillpHmacSha256Init - Invalid Parameters passed ");
89         return;
90     }
91     if (klen > FILLP_SHA256_BLOCK_SIZE) {
92         FillpSha256Ctx tctx;
93         FillpSha256Set(&tctx);
94         FillpSha256Upd(&tctx, key, klen);
95         FillpSha256Fin(&tctx, tk, FILLP_SHA256_DIGEST_SIZE);
96         key = (FILLP_UINT8 *)tk;
97         klen = FILLP_SHA256_DIGEST_SIZE;
98         FILLP_UNUSED_PARA(tctx);
99     }
100     (void)memset_s(keyIpad, FILLP_SHA256_BLOCK_SIZE, 0, FILLP_SHA256_BLOCK_SIZE);
101     (void)memset_s(keyOpad, FILLP_SHA256_BLOCK_SIZE, 0, FILLP_SHA256_BLOCK_SIZE);
102     FillpErrorType err = memcpy_s(keyIpad, klen, key, klen);
103     if (err != EOK) {
104         FILLP_LOGERR("FillpHmacSha256Init memcpy_s keyIpad failed: %d ", err);
105         return;
106     }
107     err = memcpy_s(keyOpad, klen, key, klen);
108     if (err != EOK) {
109         FILLP_LOGERR("FillpHmacSha256Init memcpy_s keyOpad failed: %d ", err);
110         return;
111     }
112     for (i = 0; i < FILLP_SHA256_BLOCK_SIZE; i++) {
113         keyIpad[i] = keyIpad[i] ^ FILLP_HMAC_IPAD;
114         keyOpad[i] = keyOpad[i] ^ FILLP_HMAC_OPAD;
115     }
116 
117     FillpSha256Set(tempCtx->hashki);
118     FillpSha256Upd(tempCtx->hashki, keyIpad, FILLP_SHA256_BLOCK_SIZE);
119     FillpSha256Set(tempCtx->hashko);
120     FillpSha256Upd(tempCtx->hashko, keyOpad, FILLP_SHA256_BLOCK_SIZE);
121 
122     FillpCleanSedata((void *)tk, sizeof(tk), pcbInst);
123     FillpCleanSedata((void *)keyIpad, sizeof(keyIpad), pcbInst);
124     FillpCleanSedata((void *)keyOpad, sizeof(keyOpad), pcbInst);
125 
126     return;
127 }
128 
129 
FillpHmacSha256Update(IO FillpHmacSha256 ctx[1],FILLP_CONST FILLP_UINT8 * data,FILLP_UINT32 dlen)130 void FillpHmacSha256Update(IO FillpHmacSha256 ctx[1], FILLP_CONST FILLP_UINT8 *data, FILLP_UINT32 dlen)
131 {
132     FillpHmacSha256Ctx *tempCtx = (FillpHmacSha256Ctx *)ctx;
133 
134     /* Null context */
135     if (tempCtx == FILLP_NULL_PTR) {
136         FILLP_LOGERR("FillpHmacSha256Update - Null Context ");
137         return;
138     }
139 
140     if ((dlen == 0) && (data == FILLP_NULL_PTR)) {
141         FILLP_UINT8 x = 0;
142         FillpSha256Upd(tempCtx->hashki, &x, 0);
143         FILLP_UNUSED_PARA(x);
144     } else if (data == FILLP_NULL_PTR) {
145         FILLP_LOGERR("FillpHmacSha256Update - Null data ");
146         return;
147     } else {
148         FillpSha256Upd(tempCtx->hashki, data, dlen);
149     }
150 
151     return;
152 }
153 
FillpHmacSha256Final(IO FillpHmacSha256 ctx[1],OUT FILLP_UINT8 digest[FILLP_SHA256_DIGEST_SIZE],FILLP_UINT32 size)154 void FillpHmacSha256Final(IO FillpHmacSha256 ctx[1],
155     OUT FILLP_UINT8 digest[FILLP_SHA256_DIGEST_SIZE], FILLP_UINT32 size)
156 {
157     FillpHmacSha256Ctx *tempCtx = (FillpHmacSha256Ctx *)ctx;
158     if (tempCtx == FILLP_NULL_PTR) {
159         FILLP_LOGERR("FillpHmacSha256Final - Null Context ");
160         return;
161     }
162 
163     if (digest == FILLP_NULL_PTR) {
164         FILLP_LOGERR("FillpHmacSha256Final - invalid argument ");
165         return;
166     }
167 
168     FillpSha256Fin(tempCtx->hashki, digest, size);
169     FillpSha256Upd(tempCtx->hashko, digest, size);
170     FillpSha256Fin(tempCtx->hashko, digest, size);
171 
172     return;
173 }
174 
175 
176 #ifdef __cplusplus
177 }
178 #endif
179 
180