1 /***************************************************************************//**
2 * \file cy_crypto_core_sha_v2.c
3 * \version 2.120
4 *
5 * \brief
6 * This file provides the source code to the API for the SHA method
7 * in the Crypto block driver.
8 *
9 ********************************************************************************
10 * \copyright
11 * Copyright (c) (2020-2022), Cypress Semiconductor Corporation (an Infineon company) or
12 * an affiliate of Cypress Semiconductor Corporation.
13 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License");
16 * you may not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 *******************************************************************************/
27
28 #include "cy_device.h"
29
30 #if defined(CY_IP_MXCRYPTO)
31
32 #include "cy_crypto_core_sha_v2.h"
33
34 #if defined(CY_CRYPTO_CFG_HW_V2_ENABLE)
35
36 #if defined(__cplusplus)
37 extern "C" {
38 #endif
39
40 #if (CPUSS_CRYPTO_SHA == 1) && defined(CY_CRYPTO_CFG_SHA_C)
41
42 #include "cy_crypto_core_hw_v2.h"
43 #include "cy_crypto_core_mem_v2.h"
44 #include "cy_syslib.h"
45
46 #if (CPUSS_CRYPTO_SHA3 == 1) && defined(CY_CRYPTO_CFG_SHA3_ENABLED)
47 /* Functions prototypes */
48 static cy_en_crypto_status_t Cy_Crypto_Sha3_Finish(CRYPTO_Type *base,
49 cy_stc_crypto_sha_state_t *hashState,
50 uint8_t *digest);
51
52 static cy_en_crypto_status_t Cy_Crypto_Sha3_Update(CRYPTO_Type *base, cy_stc_crypto_sha_state_t *hashState,
53 uint8_t const *message,
54 uint32_t messageSize);
55 #endif
56
57 static cy_en_crypto_status_t Cy_Crypto_Sha1_Sha2_Update(CRYPTO_Type *base, cy_stc_crypto_sha_state_t *hashState,
58 uint8_t const *message,
59 uint32_t messageSize);
60
61 static cy_en_crypto_status_t Cy_Crypto_Sha1_Sha2_Finish(CRYPTO_Type *base,
62 cy_stc_crypto_sha_state_t *hashState,
63 uint8_t *digest);
64
65
66 /*******************************************************************************
67 * Function Name: Cy_Crypto_Core_V2_Sha_Init
68 ****************************************************************************//**
69 *
70 * The function to initialize SHA operation.
71 *
72 * \param base
73 * The pointer to the CRYPTO instance.
74 *
75 * \param hashState
76 * The pointer to a Hash State.
77 *
78 * \param mode
79 * One of these: CY_CRYPTO_SHA256, CY_CRYPTO_SHA1, CY_CRYPTO_SHA256_224,
80 * CY_CRYPTO_SHA512, CY_CRYPTO_SHA384, CY_CRYPTO_SHA512_224, CY_CRYPTO_SHA512_256
81 *
82 * \param shaBuffers
83 * The pointer to the memory buffers storage
84 *
85 * \return
86 * \ref cy_en_crypto_status_t
87 *
88 *******************************************************************************/
Cy_Crypto_Core_V2_Sha_Init(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState,cy_en_crypto_sha_mode_t mode,void * shaBuffers)89 cy_en_crypto_status_t Cy_Crypto_Core_V2_Sha_Init(CRYPTO_Type *base,
90 cy_stc_crypto_sha_state_t *hashState,
91 cy_en_crypto_sha_mode_t mode,
92 void *shaBuffers)
93 {
94 cy_en_crypto_status_t tmpResult = CY_CRYPTO_SUCCESS;
95
96 (void)base; /* Suppress warning */
97
98 /* Initialization vectors for different modes of the SHA algorithm */
99 #if (CPUSS_CRYPTO_SHA1 == 1) && defined(CY_CRYPTO_CFG_SHA1_ENABLED)
100 static const uint8_t sha1InitHash[] =
101 {
102 0x67u, 0x45u, 0x23u, 0x01u,
103 0xefu, 0xcdu, 0xabu, 0x89u,
104 0x98u, 0xbau, 0xdcu, 0xfeu,
105 0x10u, 0x32u, 0x54u, 0x76u,
106 0xc3u, 0xd2u, 0xe1u, 0xf0u
107 };
108 #endif /* (CPUSS_CRYPTO_SHA1 == 1) && defined(CY_CRYPTO_CFG_SHA1_ENABLED) */
109
110 #if (CPUSS_CRYPTO_SHA256 == 1) && defined(CY_CRYPTO_CFG_SHA2_256_ENABLED)
111 static const uint8_t sha224InitHash[] =
112 {
113 0xc1u, 0x05u, 0x9eu, 0xd8u,
114 0x36u, 0x7cu, 0xd5u, 0x07u,
115 0x30u, 0x70u, 0xddu, 0x17u,
116 0xf7u, 0x0eu, 0x59u, 0x39u,
117 0xffu, 0xc0u, 0x0bu, 0x31u,
118 0x68u, 0x58u, 0x15u, 0x11u,
119 0x64u, 0xf9u, 0x8fu, 0xa7u,
120 0xbeu, 0xfau, 0x4fu, 0xa4u
121 };
122
123 static const uint8_t sha256InitHash[] =
124 {
125 0x6au, 0x09u, 0xe6u, 0x67u,
126 0xbbu, 0x67u, 0xaeu, 0x85u,
127 0x3cu, 0x6eu, 0xf3u, 0x72u,
128 0xa5u, 0x4fu, 0xf5u, 0x3au,
129 0x51u, 0x0eu, 0x52u, 0x7fu,
130 0x9bu, 0x05u, 0x68u, 0x8cu,
131 0x1fu, 0x83u, 0xd9u, 0xabu,
132 0x5bu, 0xe0u, 0xcdu, 0x19u
133 };
134 #endif /* (CPUSS_CRYPTO_SHA256 == 1) && defined(CY_CRYPTO_CFG_SHA2_256_ENABLED) */
135
136 #if (CPUSS_CRYPTO_SHA512 == 1) && defined(CY_CRYPTO_CFG_SHA2_512_ENABLED)
137 static const uint8_t sha512_224InitHash[] = {
138 0x8cu, 0x3du, 0x37u, 0xc8u, 0x19u, 0x54u, 0x4du, 0xa2u,
139 0x73u, 0xe1u, 0x99u, 0x66u, 0x89u, 0xdcu, 0xd4u, 0xd6u,
140 0x1du, 0xfau, 0xb7u, 0xaeu, 0x32u, 0xffu, 0x9cu, 0x82u,
141 0x67u, 0x9du, 0xd5u, 0x14u, 0x58u, 0x2fu, 0x9fu, 0xcfu,
142 0x0fu, 0x6du, 0x2bu, 0x69u, 0x7bu, 0xd4u, 0x4du, 0xa8u,
143 0x77u, 0xe3u, 0x6fu, 0x73u, 0x04u, 0xc4u, 0x89u, 0x42u,
144 0x3fu, 0x9du, 0x85u, 0xa8u, 0x6au, 0x1du, 0x36u, 0xc8u,
145 0x11u, 0x12u, 0xe6u, 0xadu, 0x91u, 0xd6u, 0x92u, 0xa1u
146 };
147
148 static const uint8_t sha512_256InitHash[] = {
149 0x22u, 0x31u, 0x21u, 0x94u, 0xfcu, 0x2bu, 0xf7u, 0x2cu,
150 0x9fu, 0x55u, 0x5fu, 0xa3u, 0xc8u, 0x4cu, 0x64u, 0xc2u,
151 0x23u, 0x93u, 0xb8u, 0x6bu, 0x6fu, 0x53u, 0xb1u, 0x51u,
152 0x96u, 0x38u, 0x77u, 0x19u, 0x59u, 0x40u, 0xeau, 0xbdu,
153 0x96u, 0x28u, 0x3eu, 0xe2u, 0xa8u, 0x8eu, 0xffu, 0xe3u,
154 0xbeu, 0x5eu, 0x1eu, 0x25u, 0x53u, 0x86u, 0x39u, 0x92u,
155 0x2bu, 0x01u, 0x99u, 0xfcu, 0x2cu, 0x85u, 0xb8u, 0xaau,
156 0x0eu, 0xb7u, 0x2du, 0xdcu, 0x81u, 0xc5u, 0x2cu, 0xa2u
157 };
158
159 static const uint8_t sha384InitHash[] = {
160 0xcbu, 0xbbu, 0x9du, 0x5du, 0xc1u, 0x05u, 0x9eu, 0xd8u,
161 0x62u, 0x9au, 0x29u, 0x2au, 0x36u, 0x7cu, 0xd5u, 0x07u,
162 0x91u, 0x59u, 0x01u, 0x5au, 0x30u, 0x70u, 0xddu, 0x17u,
163 0x15u, 0x2fu, 0xecu, 0xd8u, 0xf7u, 0x0eu, 0x59u, 0x39u,
164 0x67u, 0x33u, 0x26u, 0x67u, 0xffu, 0xc0u, 0x0bu, 0x31u,
165 0x8eu, 0xb4u, 0x4au, 0x87u, 0x68u, 0x58u, 0x15u, 0x11u,
166 0xdbu, 0x0cu, 0x2eu, 0x0du, 0x64u, 0xf9u, 0x8fu, 0xa7u,
167 0x47u, 0xb5u, 0x48u, 0x1du, 0xbeu, 0xfau, 0x4fu, 0xa4u
168 };
169
170 static const uint8_t sha512InitHash[] = {
171 0x6au, 0x09u, 0xe6u, 0x67u, 0xf3u, 0xbcu, 0xc9u, 0x08u,
172 0xbbu, 0x67u, 0xaeu, 0x85u, 0x84u, 0xcau, 0xa7u, 0x3bu,
173 0x3cu, 0x6eu, 0xf3u, 0x72u, 0xfeu, 0x94u, 0xf8u, 0x2bu,
174 0xa5u, 0x4fu, 0xf5u, 0x3au, 0x5fu, 0x1du, 0x36u, 0xf1u,
175 0x51u, 0x0eu, 0x52u, 0x7fu, 0xadu, 0xe6u, 0x82u, 0xd1u,
176 0x9bu, 0x05u, 0x68u, 0x8cu, 0x2bu, 0x3eu, 0x6cu, 0x1fu,
177 0x1fu, 0x83u, 0xd9u, 0xabu, 0xfbu, 0x41u, 0xbdu, 0x6bu,
178 0x5bu, 0xe0u, 0xcdu, 0x19u, 0x13u, 0x7eu, 0x21u, 0x79u
179 };
180 #endif /* (CPUSS_CRYPTO_SHA512 == 1) && defined(CY_CRYPTO_CFG_SHA2_512_ENABLED) */
181
182 CY_ASSERT_L1((shaBuffers != NULL) && (hashState != NULL));
183
184 switch (mode)
185 {
186 #if (CPUSS_CRYPTO_SHA1 == 1) && defined(CY_CRYPTO_CFG_SHA1_ENABLED)
187 case CY_CRYPTO_MODE_SHA1:
188 hashState->block = (uint8_t*)((cy_stc_crypto_v2_sha1_buffers_t*)shaBuffers)->block;
189 hashState->hash = (uint8_t*)((cy_stc_crypto_v2_sha1_buffers_t*)shaBuffers)->hash;
190
191 hashState->mode = (uint32_t)mode;
192 hashState->modeHw = (uint32_t)CY_CRYPTO_V2_SHA1_OPC;
193 hashState->initialHash = (const uint8_t*)(sha1InitHash);
194 hashState->blockSize = CY_CRYPTO_SHA1_BLOCK_SIZE;
195 hashState->hashSize = CY_CRYPTO_SHA1_HASH_SIZE;
196 hashState->digestSize = CY_CRYPTO_SHA1_DIGEST_SIZE;
197 break;
198 #endif /* (CPUSS_CRYPTO_SHA1 == 1) && defined(CY_CRYPTO_CFG_SHA1_ENABLED) */
199
200 #if (CPUSS_CRYPTO_SHA256 == 1) && defined(CY_CRYPTO_CFG_SHA2_256_ENABLED)
201 case CY_CRYPTO_MODE_SHA256:
202 hashState->block = (uint8_t*)((cy_stc_crypto_v2_sha256_buffers_t*)shaBuffers)->block;
203 hashState->hash = (uint8_t*)((cy_stc_crypto_v2_sha256_buffers_t*)shaBuffers)->hash;
204
205 hashState->mode = (uint32_t)mode;
206 hashState->modeHw = (uint32_t)CY_CRYPTO_V2_SHA2_256_OPC;
207 hashState->initialHash = (const uint8_t*)(sha256InitHash);
208 hashState->blockSize = CY_CRYPTO_SHA256_BLOCK_SIZE;
209 hashState->hashSize = CY_CRYPTO_SHA256_HASH_SIZE;
210 hashState->digestSize = CY_CRYPTO_SHA256_DIGEST_SIZE;
211 break;
212 case CY_CRYPTO_MODE_SHA224:
213 hashState->block = (uint8_t*)((cy_stc_crypto_v2_sha256_buffers_t*)shaBuffers)->block;
214 hashState->hash = (uint8_t*)((cy_stc_crypto_v2_sha256_buffers_t*)shaBuffers)->hash;
215
216 hashState->mode = (uint32_t)mode;
217 hashState->modeHw = (uint32_t)CY_CRYPTO_V2_SHA2_256_OPC;
218 hashState->initialHash = (const uint8_t*)(sha224InitHash);
219 hashState->blockSize = CY_CRYPTO_SHA256_BLOCK_SIZE;
220 hashState->hashSize = CY_CRYPTO_SHA256_HASH_SIZE;
221 hashState->digestSize = CY_CRYPTO_SHA224_DIGEST_SIZE;
222 break;
223 #endif /* (CPUSS_CRYPTO_SHA256 == 1) && defined(CY_CRYPTO_CFG_SHA2_256_ENABLED) */
224
225 #if (CPUSS_CRYPTO_SHA512 == 1) && defined(CY_CRYPTO_CFG_SHA2_512_ENABLED)
226 case CY_CRYPTO_MODE_SHA512:
227 hashState->block = (uint8_t*)((cy_stc_crypto_v2_sha512_buffers_t*)shaBuffers)->block;
228 hashState->hash = (uint8_t*)((cy_stc_crypto_v2_sha512_buffers_t*)shaBuffers)->hash;
229
230 hashState->mode = (uint32_t)mode;
231 hashState->modeHw = (uint32_t)CY_CRYPTO_V2_SHA2_512_OPC;
232 hashState->initialHash = (const uint8_t*)(sha512InitHash);
233 hashState->blockSize = CY_CRYPTO_SHA512_BLOCK_SIZE;
234 hashState->hashSize = CY_CRYPTO_SHA512_HASH_SIZE;
235 hashState->digestSize = CY_CRYPTO_SHA512_DIGEST_SIZE;
236 break;
237 case CY_CRYPTO_MODE_SHA384:
238 hashState->block = (uint8_t*)((cy_stc_crypto_v2_sha512_buffers_t*)shaBuffers)->block;
239 hashState->hash = (uint8_t*)((cy_stc_crypto_v2_sha512_buffers_t*)shaBuffers)->hash;
240
241 hashState->mode = (uint32_t)mode;
242 hashState->modeHw = (uint32_t)CY_CRYPTO_V2_SHA2_512_OPC;
243 hashState->initialHash = (const uint8_t*)(sha384InitHash);
244 hashState->blockSize = CY_CRYPTO_SHA512_BLOCK_SIZE;
245 hashState->hashSize = CY_CRYPTO_SHA512_HASH_SIZE;
246 hashState->digestSize = CY_CRYPTO_SHA384_DIGEST_SIZE;
247 break;
248 case CY_CRYPTO_MODE_SHA512_256:
249 hashState->block = (uint8_t*)((cy_stc_crypto_v2_sha512_buffers_t*)shaBuffers)->block;
250 hashState->hash = (uint8_t*)((cy_stc_crypto_v2_sha512_buffers_t*)shaBuffers)->hash;
251
252 hashState->mode = (uint32_t)mode;
253 hashState->modeHw = (uint32_t)CY_CRYPTO_V2_SHA2_512_OPC;
254 hashState->initialHash = (const uint8_t*)(sha512_256InitHash);
255 hashState->blockSize = CY_CRYPTO_SHA512_BLOCK_SIZE;
256 hashState->hashSize = CY_CRYPTO_SHA512_HASH_SIZE;
257 hashState->digestSize = CY_CRYPTO_SHA512_256_DIGEST_SIZE;
258 break;
259 case CY_CRYPTO_MODE_SHA512_224:
260 hashState->block = (uint8_t*)((cy_stc_crypto_v2_sha512_buffers_t*)shaBuffers)->block;
261 hashState->hash = (uint8_t*)((cy_stc_crypto_v2_sha512_buffers_t*)shaBuffers)->hash;
262
263 hashState->mode = (uint32_t)mode;
264 hashState->modeHw = (uint32_t)CY_CRYPTO_V2_SHA2_512_OPC;
265 hashState->initialHash = (const uint8_t*)(sha512_224InitHash);
266 hashState->blockSize = CY_CRYPTO_SHA512_BLOCK_SIZE;
267 hashState->hashSize = CY_CRYPTO_SHA512_HASH_SIZE;
268 hashState->digestSize = CY_CRYPTO_SHA512_224_DIGEST_SIZE;
269 break;
270 #endif /* (CPUSS_CRYPTO_SHA512 == 1) && defined(CY_CRYPTO_CFG_SHA2_512_ENABLED) */
271
272 #if (CPUSS_CRYPTO_SHA3 == 1) && defined(CY_CRYPTO_CFG_SHA3_ENABLED)
273 case CY_CRYPTO_MODE_SHA3_224:
274 hashState->hash = (uint8_t*)((cy_stc_crypto_v2_sha3_buffers_t*)shaBuffers)->hash;
275 hashState->mode = (uint32_t)mode;
276 hashState->modeHw = (uint32_t)CY_CRYPTO_V2_SHA3_OPC;
277 hashState->blockSize = CY_CRYPTO_SHA3_224_BLOCK_SIZE;
278 hashState->digestSize = CY_CRYPTO_SHA224_DIGEST_SIZE;
279 hashState->hashSize = 0u;
280 break;
281 case CY_CRYPTO_MODE_SHA3_256:
282 hashState->hash = (uint8_t*)((cy_stc_crypto_v2_sha3_buffers_t*)shaBuffers)->hash;
283 hashState->mode = (uint32_t)mode;
284 hashState->modeHw = (uint32_t)CY_CRYPTO_V2_SHA3_OPC;
285 hashState->blockSize = CY_CRYPTO_SHA3_256_BLOCK_SIZE;
286 hashState->digestSize = CY_CRYPTO_SHA256_DIGEST_SIZE;
287 hashState->hashSize = 0u;
288 break;
289 case CY_CRYPTO_MODE_SHA3_384:
290 hashState->hash = (uint8_t*)((cy_stc_crypto_v2_sha3_buffers_t*)shaBuffers)->hash;
291 hashState->mode = (uint32_t)mode;
292 hashState->modeHw = (uint32_t)CY_CRYPTO_V2_SHA3_OPC;
293 hashState->blockSize = CY_CRYPTO_SHA3_384_BLOCK_SIZE;
294 hashState->digestSize = CY_CRYPTO_SHA384_DIGEST_SIZE;
295 hashState->hashSize = 0u;
296 break;
297 case CY_CRYPTO_MODE_SHA3_512:
298 hashState->hash = (uint8_t*)((cy_stc_crypto_v2_sha3_buffers_t*)shaBuffers)->hash;
299 hashState->mode = (uint32_t)mode;
300 hashState->modeHw = (uint32_t)CY_CRYPTO_V2_SHA3_OPC;
301 hashState->blockSize = CY_CRYPTO_SHA3_512_BLOCK_SIZE;
302 hashState->digestSize = CY_CRYPTO_SHA512_DIGEST_SIZE;
303 hashState->hashSize = 0u;
304 break;
305 #endif/* (CPUSS_CRYPTO_SHA3 == 1) && defined(CY_CRYPTO_CFG_SHA3_ENABLED) */
306 default:
307 tmpResult = CY_CRYPTO_BAD_PARAMS;
308 break;
309 }
310
311 return (tmpResult);
312 }
313
314
315 /*******************************************************************************
316 * Function Name: Cy_Crypto_Core_V2_Sha_Start
317 ****************************************************************************//**
318 *
319 * Initializes the initial hash vector.
320 *
321 * \param base
322 * The pointer to the CRYPTO instance.
323 *
324 * \param hashState
325 * The pointer to the SHA context.
326 *
327 * \return
328 * \ref cy_en_crypto_status_t
329 *
330 *******************************************************************************/
Cy_Crypto_Core_V2_Sha_Start(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState)331 cy_en_crypto_status_t Cy_Crypto_Core_V2_Sha_Start(CRYPTO_Type *base, cy_stc_crypto_sha_state_t *hashState)
332 {
333 cy_en_crypto_status_t tmpResult = CY_CRYPTO_BAD_PARAMS;
334 uint8_t *hashRemap;
335 uint8_t *initialHashRemap;
336
337 if (hashState != NULL)
338 {
339
340 hashRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(hashState->hash);
341 initialHashRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(hashState->initialHash);
342
343 hashState->blockIdx = 0U;
344 hashState->messageSize = 0U;
345
346 #if (CPUSS_CRYPTO_SHA3 == 1) && defined(CY_CRYPTO_CFG_SHA3_ENABLED)
347 switch ((cy_en_crypto_sha_mode_t)hashState->mode)
348 {
349 case CY_CRYPTO_MODE_SHA3_224:
350 case CY_CRYPTO_MODE_SHA3_256:
351 case CY_CRYPTO_MODE_SHA3_384:
352 case CY_CRYPTO_MODE_SHA3_512:
353 Cy_Crypto_Core_V2_MemSet(base, hashRemap, (uint8_t) 0, (uint16_t) CY_CRYPTO_SHA3_STATE_SIZE);
354 tmpResult = CY_CRYPTO_SUCCESS;
355 break;
356 default:
357 tmpResult = CY_CRYPTO_BAD_PARAMS;
358 break;
359 }
360 #endif
361
362 if (hashState->hashSize != 0U)
363 {
364 Cy_Crypto_Core_V2_MemCpy(base, (void*)hashRemap, (void *)initialHashRemap, (uint16_t)hashState->hashSize);
365 tmpResult = CY_CRYPTO_SUCCESS;
366 }
367
368 }
369
370 return (tmpResult);
371 }
372
373 #if (CPUSS_CRYPTO_SHA3 == 1) && defined(CY_CRYPTO_CFG_SHA3_ENABLED)
374
375 /*******************************************************************************
376 * Function Name: Cy_Crypto_Sha3_Update
377 ****************************************************************************//**
378 *
379 * Performs the SHA3 update calculation on message.
380 *
381 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameter message must align and end in 32 byte boundary.
382 *
383 * \param base
384 * The pointer to the CRYPTO instance.
385 *
386 * \param hashState
387 * The pointer to the SHA context.
388 *
389 * \param message
390 * The pointer to the message whose Hash is being computed.
391 *
392 * \param messageSize
393 * The size of the message whose Hash is being computed.
394 *
395 * \return
396 * \ref cy_en_crypto_status_t
397 *
398 *
399 *******************************************************************************/
Cy_Crypto_Sha3_Update(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState,uint8_t const * message,uint32_t messageSize)400 static cy_en_crypto_status_t Cy_Crypto_Sha3_Update(CRYPTO_Type *base, cy_stc_crypto_sha_state_t *hashState,
401 uint8_t const *message,
402 uint32_t messageSize)
403 {
404
405 uint32_t hashBlockIdx = hashState->blockIdx;
406 uint32_t hashBlockSize = hashState->blockSize;
407
408 uint8_t *hashRemap;
409 uint8_t *messageRemap;
410
411 hashRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(hashState->hash);
412 messageRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(message);
413
414 /* Load the calculated hash from the context buffer */
415 Cy_Crypto_Core_V2_RBClear(base);
416 Cy_Crypto_Core_V2_Sync(base);
417
418 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_LOAD0, hashRemap, CY_CRYPTO_SHA3_STATE_SIZE);
419 Cy_Crypto_Core_V2_RBXor(base, 0U, 128U);
420 Cy_Crypto_Core_V2_RBSwap(base);
421 Cy_Crypto_Core_V2_RBXor(base, 0U, 72U);
422 Cy_Crypto_Core_V2_RBSwap(base);
423
424 /* Start the message loading */
425 Cy_Crypto_Core_V2_FFContinue(base, CY_CRYPTO_V2_RB_FF_LOAD0, messageRemap, messageSize);
426
427 /* Processing the fully filled blocks with remaining buffer data */
428 while ((hashBlockIdx + messageSize) >= hashBlockSize)
429 {
430 uint32_t tempBlockSize = hashBlockSize - hashBlockIdx;
431
432 if(hashBlockSize>128U) // This condition is for the SHA3_224 & SHA3_256 where the rate size is more that 1024 bits
433 {
434 if(hashBlockIdx<=128U)
435 {
436 Cy_Crypto_Core_V2_RBXor(base, hashBlockIdx, 128U-hashBlockIdx);
437 Cy_Crypto_Core_V2_RBSwap(base);
438 Cy_Crypto_Core_V2_RBXor(base, 0U, hashBlockSize-128U);
439 Cy_Crypto_Core_V2_RBSwap(base);
440 }
441 else
442 {
443 Cy_Crypto_Core_V2_RBSwap(base);
444 Cy_Crypto_Core_V2_RBXor(base, hashBlockIdx - 128U, hashBlockSize-hashBlockIdx);
445 Cy_Crypto_Core_V2_RBSwap(base);
446 }
447 }
448 else
449 {
450 Cy_Crypto_Core_V2_RBXor(base, hashBlockIdx, tempBlockSize);
451 }
452
453 Cy_Crypto_Core_V2_Run(base, hashState->modeHw);
454
455 messageSize -= tempBlockSize;
456 hashBlockIdx = 0U;
457 }
458
459 hashState->blockIdx = hashBlockIdx + messageSize;
460
461 /* Load the end of the message (tail that less then block size) to the register buffer */
462 if (messageSize != 0U)
463 {
464 if(hashBlockSize>128U)
465 {
466 if(messageSize + hashBlockIdx <= 128U)
467 {
468 Cy_Crypto_Core_V2_RBXor(base, hashBlockIdx, messageSize);
469 Cy_Crypto_Core_V2_Sync(base);
470 }
471
472 else
473 {
474 if(hashBlockIdx<128U)
475 {
476 Cy_Crypto_Core_V2_RBXor(base, hashBlockIdx, 128U-hashBlockIdx);
477 Cy_Crypto_Core_V2_RBSwap(base);
478 Cy_Crypto_Core_V2_RBXor(base, 0U, messageSize - 128U);
479 Cy_Crypto_Core_V2_RBSwap(base);
480 }
481 else
482 {
483 Cy_Crypto_Core_V2_RBSwap(base);
484 Cy_Crypto_Core_V2_RBXor(base, hashBlockIdx % 128U , messageSize);
485 Cy_Crypto_Core_V2_RBSwap(base);
486 }
487 }
488 }
489 else
490 {
491 Cy_Crypto_Core_V2_RBXor(base, hashBlockIdx, messageSize);
492 Cy_Crypto_Core_V2_Sync(base);
493 }
494 }
495
496 /* Store the hash state to the context buffer */
497 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_STORE, hashRemap, 128);
498 Cy_Crypto_Core_V2_RBStore(base, 0U, 128U);
499 Cy_Crypto_Core_V2_FFStoreSync(base);
500
501 Cy_Crypto_Core_V2_RBSwap(base);
502 Cy_Crypto_Core_V2_Sync(base);
503
504 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_STORE, &hashRemap[128u], 72u);
505 Cy_Crypto_Core_V2_RBStore(base, 0U, 72U);
506 Cy_Crypto_Core_V2_FFStoreSync(base);
507
508 return CY_CRYPTO_SUCCESS;
509 }
510
511
512 /*******************************************************************************
513 * Function Name: Cy_Crypto_Sha3_Finish
514 ****************************************************************************//**
515 *
516 * Completes SHA3 calculation.
517 *
518 * \param base
519 * The pointer to the CRYPTO instance.
520 *
521 * \param hashState
522 * The pointer to the SHA context.
523 *
524 * \param digest
525 * The pointer to the calculated hash digest.
526 *
527 * \return
528 * \ref cy_en_crypto_status_t
529 *
530 *******************************************************************************/
Cy_Crypto_Sha3_Finish(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState,uint8_t * digest)531 static cy_en_crypto_status_t Cy_Crypto_Sha3_Finish(CRYPTO_Type *base,
532 cy_stc_crypto_sha_state_t *hashState,
533 uint8_t *digest)
534 {
535 cy_en_crypto_status_t tmpResult = CY_CRYPTO_BAD_PARAMS;
536 uint8_t *hashRemap;
537
538 if ((hashState != NULL) && (digest != NULL))
539 {
540
541 #if (((CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)) || CY_CPU_CORTEX_M55)
542 /* Flush the cache */
543 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to int32_t.');
544 SCB_InvalidateDCache_by_Addr((volatile void *)hashState->hash,(int32_t)(CY_CRYPTO_SHA_MAX_HASH_SIZE));
545 #endif
546
547 uint32_t hashBlockSize = hashState->blockSize;
548 uint32_t hashBlockIdx = hashState->blockIdx;
549 hashRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(hashState->hash);
550
551 hashState->hash[hashBlockIdx] = hashState->hash[hashBlockIdx] ^ 0x06U;
552 hashState->hash[hashBlockSize-1U] = hashState->hash[hashBlockSize-1U] ^ 0x80U;
553
554 #if (((CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)) || CY_CPU_CORTEX_M55)
555 /* Flush the cache */
556 CY_MISRA_DEVIATE_LINE('MISRA C-2012 Rule 10.8','Intentional typecast to int32_t.');
557 SCB_CleanDCache_by_Addr((volatile void *)hashState->hash,(int32_t)(CY_CRYPTO_SHA_MAX_HASH_SIZE));
558 #endif
559 /* Load the calculated hash state from the context buffer */
560 Cy_Crypto_Core_V2_RBClear(base);
561 Cy_Crypto_Core_V2_Sync(base);
562
563 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_LOAD0, hashRemap, CY_CRYPTO_SHA3_STATE_SIZE);
564 Cy_Crypto_Core_V2_RBXor(base, 0U, 128U);
565 Cy_Crypto_Core_V2_RBSwap(base);
566
567 Cy_Crypto_Core_V2_RBXor(base, 0U, 72U);
568 Cy_Crypto_Core_V2_RBSwap(base);
569 Cy_Crypto_Core_V2_Sync(base);
570
571 Cy_Crypto_Core_V2_Run(base, hashState->modeHw);
572 Cy_Crypto_Core_V2_Sync(base);
573
574 /* Write digest. */
575 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_STORE, digest, hashState->digestSize);
576 Cy_Crypto_Core_V2_RBStore(base, 0U, hashState->digestSize);
577 Cy_Crypto_Core_V2_FFStoreSync(base);
578
579 tmpResult = CY_CRYPTO_SUCCESS;
580
581 }
582
583 return (tmpResult);
584 }
585 #endif
586
587 /*******************************************************************************
588 * Function Name: Cy_Crypto_Sha1_Sha2_Update
589 ****************************************************************************//**
590 *
591 * Performs the SHA1/SHA2 update calculation on message.
592 *
593 * \param base
594 * The pointer to the CRYPTO instance.
595 *
596 * \param hashState
597 * The pointer to the SHA context.
598 *
599 * \param message
600 * The pointer to the message whose Hash is being computed.
601 *
602 * \param messageSize
603 * The size of the message whose Hash is being computed.
604 *
605 * \return
606 * \ref cy_en_crypto_status_t
607 *
608 *
609 *******************************************************************************/
Cy_Crypto_Sha1_Sha2_Update(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState,uint8_t const * message,uint32_t messageSize)610 static cy_en_crypto_status_t Cy_Crypto_Sha1_Sha2_Update(CRYPTO_Type *base, cy_stc_crypto_sha_state_t *hashState,
611 uint8_t const *message,
612 uint32_t messageSize)
613 {
614
615 hashState->messageSize += messageSize;
616 uint8_t *hashRemap;
617 uint8_t *blockRemap;
618 uint8_t *messageRemap;
619
620 hashRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(hashState->hash);
621 blockRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(hashState->block);
622 messageRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(message);
623
624 uint32_t hashBlockIdx = hashState->blockIdx;
625 uint32_t hashBlockSize = hashState->blockSize;
626
627 /* Load the calculated hash from the context buffer */
628 Cy_Crypto_Core_V2_RBClear(base);
629 Cy_Crypto_Core_V2_Sync(base);
630
631 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_LOAD0, hashRemap, hashState->hashSize);
632 Cy_Crypto_Core_V2_RBXor(base, 0U, hashState->hashSize);
633 Cy_Crypto_Core_V2_Sync(base);
634 Cy_Crypto_Core_V2_RBSwap(base);
635
636 /* Load the remaining block from the context buffer */
637 if (hashBlockIdx != 0U)
638 {
639 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_LOAD0, blockRemap, hashBlockIdx);
640 Cy_Crypto_Core_V2_RBXor(base, 0U, hashBlockIdx);
641 Cy_Crypto_Core_V2_Sync(base);
642 }
643
644 /* Start the hash calculating */
645 Cy_Crypto_Core_V2_FFContinue(base, CY_CRYPTO_V2_RB_FF_LOAD0, messageRemap, messageSize);
646
647 /* Processing the fully filled blocks with remaining buffer data */
648 while ((hashBlockIdx + messageSize) >= hashBlockSize)
649 {
650 uint32_t tempBlockSize = hashBlockSize - hashBlockIdx;
651
652 Cy_Crypto_Core_V2_RBXor(base, hashBlockIdx, tempBlockSize);
653
654 Cy_Crypto_Core_V2_Run(base, hashState->modeHw);
655
656 messageSize -= tempBlockSize;
657
658 hashBlockIdx = 0U;
659 }
660
661 /* The remaining block will be calculated in the Finish function */
662 hashState->blockIdx = hashBlockIdx + messageSize;
663
664 /* Load the end of the message (tail that less then block size) to the register buffer */
665 if (messageSize != 0U)
666 {
667 Cy_Crypto_Core_V2_RBXor(base, hashBlockIdx, messageSize);
668 Cy_Crypto_Core_V2_Sync(base);
669 }
670
671 /* Save the remaining data to the context buffer */
672 if (hashState->blockIdx != 0U)
673 {
674 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_STORE, blockRemap, hashState->blockIdx);
675 Cy_Crypto_Core_V2_RBStore(base, 0U, hashState->blockIdx);
676 Cy_Crypto_Core_V2_Sync(base);
677 }
678
679 /* Store the calculated hash to the context buffer */
680 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_STORE, hashRemap, hashState->hashSize);
681 Cy_Crypto_Core_V2_RBSwap(base);
682 Cy_Crypto_Core_V2_RBStore(base, 0U, hashState->hashSize);
683 Cy_Crypto_Core_V2_Sync(base);
684 Cy_Crypto_Core_V2_RBSwap(base);
685
686 return CY_CRYPTO_SUCCESS;
687 }
688
689
690 /*******************************************************************************
691 * Function Name: Cy_Crypto_Sha1_Sha2_Finish
692 ****************************************************************************//**
693 *
694 * Completes SHA1/SHA2 calculation.
695 *
696 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameter digest must align and end in 32 byte boundary.
697 *
698 * \param base
699 * The pointer to the CRYPTO instance.
700 *
701 * \param hashState
702 * The pointer to the SHA context.
703 *
704 * \param digest
705 * The pointer to the calculated hash digest.
706 *
707 * \return
708 * \ref cy_en_crypto_status_t
709 *
710 *******************************************************************************/
Cy_Crypto_Sha1_Sha2_Finish(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState,uint8_t * digest)711 static cy_en_crypto_status_t Cy_Crypto_Sha1_Sha2_Finish(CRYPTO_Type *base,
712 cy_stc_crypto_sha_state_t *hashState,
713 uint8_t *digest)
714 {
715 uint32_t hashBlockSize = hashState->blockSize;
716 uint32_t hashBlockIdx = hashState->blockIdx;
717
718 uint64_t finalMessageSizeInBits = hashState->messageSize * 8U;
719 uint32_t padSize;
720 uint8_t *hashRemap;
721 uint8_t *blockRemap;
722
723 hashRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(hashState->hash);
724 blockRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(hashState->block);
725
726 if (CY_CRYPTO_SHA512_BLOCK_SIZE == hashBlockSize)
727 {
728 padSize = CY_CRYPTO_SHA512_PAD_SIZE; /* Pad size = 112 */
729 }
730 else
731 {
732 padSize = CY_CRYPTO_SHA256_PAD_SIZE; /* Pad size = 56 */
733 }
734
735 /* Load the calculated hash from the context buffer */
736 Cy_Crypto_Core_V2_RBClear(base);
737 Cy_Crypto_Core_V2_Sync(base);
738
739 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_LOAD0, hashRemap, hashState->hashSize);
740 Cy_Crypto_Core_V2_RBXor(base, 0U, hashState->hashSize);
741 Cy_Crypto_Core_V2_Sync(base);
742 Cy_Crypto_Core_V2_RBSwap(base);
743
744 /* Load the end of the message (tail that less then block size) to the register buffer */
745 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_LOAD0, blockRemap, hashBlockIdx);
746 Cy_Crypto_Core_V2_RBXor(base, 0U, hashBlockIdx);
747
748 /* Sync until XOR operation is completed */
749 Cy_Crypto_Core_V2_Sync(base);
750
751 /* Append 1 bit to the end of the message */
752 Cy_Crypto_Core_V2_RBSetByte(base, hashBlockIdx, 0x80U);
753
754 if (hashBlockIdx >= padSize)
755 {
756 Cy_Crypto_Core_V2_Run(base, hashState->modeHw);
757 Cy_Crypto_Core_V2_Sync(base);
758 }
759
760 /* Append message size into last of the block */
761
762 /* In case of u64SizeInByte * 8 > u64Max */
763
764 Cy_Crypto_Core_V2_RBSetByte(base, hashBlockSize - 8U, (uint8_t)(finalMessageSizeInBits >> 56U));
765 Cy_Crypto_Core_V2_RBSetByte(base, hashBlockSize - 7U, (uint8_t)(finalMessageSizeInBits >> 48U));
766 Cy_Crypto_Core_V2_RBSetByte(base, hashBlockSize - 6U, (uint8_t)(finalMessageSizeInBits >> 40U));
767 Cy_Crypto_Core_V2_RBSetByte(base, hashBlockSize - 5U, (uint8_t)(finalMessageSizeInBits >> 32U));
768 Cy_Crypto_Core_V2_RBSetByte(base, hashBlockSize - 4U, (uint8_t)(finalMessageSizeInBits >> 24U));
769 Cy_Crypto_Core_V2_RBSetByte(base, hashBlockSize - 3U, (uint8_t)(finalMessageSizeInBits >> 16U));
770 Cy_Crypto_Core_V2_RBSetByte(base, hashBlockSize - 2U, (uint8_t)(finalMessageSizeInBits >> 8U));
771 Cy_Crypto_Core_V2_RBSetByte(base, hashBlockSize - 1U, (uint8_t)(finalMessageSizeInBits));
772
773 Cy_Crypto_Core_V2_Run(base, hashState->modeHw);
774 Cy_Crypto_Core_V2_Sync(base);
775
776 /* Write digest. */
777 Cy_Crypto_Core_V2_FFStart(base, CY_CRYPTO_V2_RB_FF_STORE, digest, hashState->digestSize);
778 Cy_Crypto_Core_V2_RBSwap(base);
779 Cy_Crypto_Core_V2_RBStore(base, 0U, hashState->digestSize);
780
781 Cy_Crypto_Core_V2_FFStoreSync(base);
782
783 return CY_CRYPTO_SUCCESS;
784 }
785
786 /*******************************************************************************
787 * Function Name: Cy_Crypto_Core_V2_Sha_Update
788 ****************************************************************************//**
789 *
790 * Performs the SHA calculation on one message.
791 *
792 * \param base
793 * The pointer to the CRYPTO instance.
794 *
795 * \param hashState
796 * The pointer to the SHA context.
797 *
798 * \param message
799 * The pointer to the message whose Hash is being computed.
800 *
801 * \param messageSize
802 * The size of the message whose Hash is being computed.
803 *
804 * \return
805 * \ref cy_en_crypto_status_t
806 *
807 * \note
808 * This function can be called several times only with message lengths dividable
809 * by the block size. Only the last call to the function can process a message with
810 * a not-dividable size.
811 *
812 *******************************************************************************/
Cy_Crypto_Core_V2_Sha_Update(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState,uint8_t const * message,uint32_t messageSize)813 cy_en_crypto_status_t Cy_Crypto_Core_V2_Sha_Update(CRYPTO_Type *base,
814 cy_stc_crypto_sha_state_t *hashState,
815 uint8_t const *message,
816 uint32_t messageSize)
817 {
818 cy_en_crypto_status_t tmpResult = CY_CRYPTO_BAD_PARAMS;
819 uint8_t *messageRemap;
820
821 if(messageSize == 0UL)
822 {
823 return CY_CRYPTO_SUCCESS;
824 }
825
826 if ((hashState != NULL) && (message != NULL))
827 {
828
829 #if (((CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)) || CY_CPU_CORTEX_M55)
830 /* Flush the cache */
831 SCB_CleanDCache_by_Addr((volatile void *)message,(int32_t)messageSize);
832 #endif
833
834 messageRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(message);
835
836 if (hashState->blockSize != 0U)
837 {
838
839 switch ((cy_en_crypto_sha_mode_t)hashState->mode)
840 {
841
842 #if (CPUSS_CRYPTO_SHA1 ==1) && defined(CY_CRYPTO_CFG_SHA1_ENABLED)
843 case CY_CRYPTO_MODE_SHA1:
844 tmpResult = Cy_Crypto_Sha1_Sha2_Update(base, hashState, messageRemap, messageSize);
845 break;
846 #endif
847
848 #if (CPUSS_CRYPTO_SHA256 ==1) && defined(CY_CRYPTO_CFG_SHA2_256_ENABLED)
849 case CY_CRYPTO_MODE_SHA224:
850 case CY_CRYPTO_MODE_SHA256:
851 tmpResult = Cy_Crypto_Sha1_Sha2_Update(base, hashState, messageRemap, messageSize);
852 break;
853 #endif
854
855 #if (CPUSS_CRYPTO_SHA512 ==1) && defined(CY_CRYPTO_CFG_SHA2_512_ENABLED)
856 case CY_CRYPTO_MODE_SHA384:
857 case CY_CRYPTO_MODE_SHA512:
858 case CY_CRYPTO_MODE_SHA512_224:
859 case CY_CRYPTO_MODE_SHA512_256:
860 tmpResult = Cy_Crypto_Sha1_Sha2_Update(base, hashState, messageRemap, messageSize);
861 break;
862 #endif
863
864 #if (CPUSS_CRYPTO_SHA3 == 1) && defined(CY_CRYPTO_CFG_SHA3_ENABLED)
865 case CY_CRYPTO_MODE_SHA3_224:
866 case CY_CRYPTO_MODE_SHA3_256:
867 case CY_CRYPTO_MODE_SHA3_384:
868 case CY_CRYPTO_MODE_SHA3_512:
869 tmpResult = Cy_Crypto_Sha3_Update(base, hashState, messageRemap, messageSize);
870 break;
871 #endif
872
873 default:
874 tmpResult = CY_CRYPTO_BAD_PARAMS;
875 break;
876 }
877
878 }
879 }
880
881 return (tmpResult);
882 }
883
884 /*******************************************************************************
885 * Function Name: Cy_Crypto_Core_V2_Sha_Finish
886 ****************************************************************************//**
887 *
888 * Completes SHA calculation.
889 *
890 * \param base
891 * The pointer to the CRYPTO instance.
892 *
893 * \param hashState
894 * The pointer to the SHA context.
895 *
896 * \param digest
897 * The pointer to the calculated hash digest.
898 *
899 * \return
900 * \ref cy_en_crypto_status_t
901 *
902 *******************************************************************************/
Cy_Crypto_Core_V2_Sha_Finish(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState,uint8_t * digest)903 cy_en_crypto_status_t Cy_Crypto_Core_V2_Sha_Finish(CRYPTO_Type *base,
904 cy_stc_crypto_sha_state_t *hashState,
905 uint8_t *digest)
906 {
907 cy_en_crypto_status_t tmpResult = CY_CRYPTO_BAD_PARAMS;
908 uint8_t *digestRemap;
909
910 if ((hashState != NULL) && (digest != NULL))
911 {
912 digestRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(digest);
913
914 switch ((cy_en_crypto_sha_mode_t)hashState->mode)
915 {
916
917 #if (CPUSS_CRYPTO_SHA1 ==1) && defined(CY_CRYPTO_CFG_SHA1_ENABLED)
918 case CY_CRYPTO_MODE_SHA1:
919 tmpResult = Cy_Crypto_Sha1_Sha2_Finish(base, hashState, digestRemap);
920 break;
921 #endif
922
923 #if (CPUSS_CRYPTO_SHA256 ==1) && defined(CY_CRYPTO_CFG_SHA2_256_ENABLED)
924 case CY_CRYPTO_MODE_SHA224:
925 case CY_CRYPTO_MODE_SHA256:
926 tmpResult = Cy_Crypto_Sha1_Sha2_Finish(base, hashState, digestRemap);
927 break;
928 #endif
929
930 #if (CPUSS_CRYPTO_SHA512 ==1) && defined(CY_CRYPTO_CFG_SHA2_512_ENABLED)
931 case CY_CRYPTO_MODE_SHA384:
932 case CY_CRYPTO_MODE_SHA512:
933 case CY_CRYPTO_MODE_SHA512_224:
934 case CY_CRYPTO_MODE_SHA512_256:
935 tmpResult = Cy_Crypto_Sha1_Sha2_Finish(base, hashState, digestRemap);
936 break;
937 #endif
938
939 #if (CPUSS_CRYPTO_SHA3 == 1) && defined(CY_CRYPTO_CFG_SHA3_ENABLED)
940 case CY_CRYPTO_MODE_SHA3_224:
941 case CY_CRYPTO_MODE_SHA3_256:
942 case CY_CRYPTO_MODE_SHA3_384:
943 case CY_CRYPTO_MODE_SHA3_512:
944 tmpResult = Cy_Crypto_Sha3_Finish(base, hashState, digestRemap);
945 break;
946 #endif
947
948 default:
949 tmpResult = CY_CRYPTO_BAD_PARAMS;
950 break;
951
952 }
953 }
954
955 #if (((CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)) || CY_CPU_CORTEX_M55)
956 if(tmpResult == CY_CRYPTO_SUCCESS)
957 {
958 SCB_InvalidateDCache_by_Addr(digest, (int32_t)hashState->digestSize);
959 }
960 #endif
961
962 return (tmpResult);
963 }
964
965 /*******************************************************************************
966 * Function Name: Cy_Crypto_Core_V2_Sha_Free
967 ****************************************************************************//**
968 *
969 * Clears the used memory buffers.
970 *
971 * For CAT1C & CAT1D(CM55) devices when D-Cache is enabled parameter hashState must align and end in 32 byte boundary.
972 *
973 * \param base
974 * The pointer to the CRYPTO instance.
975 *
976 * \param hashState
977 * The pointer to the SHA context.
978 *
979 * \return
980 * \ref cy_en_crypto_status_t
981 *
982 * *******************************************************************************/
Cy_Crypto_Core_V2_Sha_Free(CRYPTO_Type * base,cy_stc_crypto_sha_state_t * hashState)983 cy_en_crypto_status_t Cy_Crypto_Core_V2_Sha_Free(CRYPTO_Type *base, cy_stc_crypto_sha_state_t *hashState)
984 {
985 cy_en_crypto_status_t tmpResult = CY_CRYPTO_BAD_PARAMS;
986 uint8_t *hashRemap;
987 uint8_t *blockRemap;
988 cy_stc_crypto_sha_state_t *stateRemap;
989
990 if ((NULL != base) && (hashState != NULL))
991 {
992 hashRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(hashState->hash);
993 blockRemap = (uint8_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(hashState->block);
994 stateRemap = (cy_stc_crypto_sha_state_t *)CY_REMAP_ADDRESS_FOR_CRYPTO(hashState);
995
996
997 /* Clears the context buffers */
998 tmpResult = CY_CRYPTO_SUCCESS;
999
1000 switch ((cy_en_crypto_sha_mode_t)hashState->mode)
1001 {
1002
1003 #if (CPUSS_CRYPTO_SHA1 ==1) && defined(CY_CRYPTO_CFG_SHA1_ENABLED)
1004 case CY_CRYPTO_MODE_SHA1:
1005 if(NULL != blockRemap)
1006 {
1007 Cy_Crypto_Core_V2_MemSet(base, blockRemap, 0x00U, (uint16_t)hashState->blockSize);
1008 }
1009 if(NULL != hashRemap)
1010 {
1011 Cy_Crypto_Core_V2_MemSet(base, hashRemap, 0x00U, (uint16_t)hashState->hashSize);
1012 }
1013 break;
1014 #endif
1015
1016 #if (CPUSS_CRYPTO_SHA256 ==1) && defined(CY_CRYPTO_CFG_SHA2_256_ENABLED)
1017 case CY_CRYPTO_MODE_SHA224:
1018 case CY_CRYPTO_MODE_SHA256:
1019 if(NULL != blockRemap)
1020 {
1021 Cy_Crypto_Core_V2_MemSet(base, blockRemap, 0x00U, (uint16_t)hashState->blockSize);
1022 }
1023 if(NULL != hashRemap)
1024 {
1025 Cy_Crypto_Core_V2_MemSet(base, hashRemap, 0x00U, (uint16_t)hashState->hashSize);
1026 }
1027 break;
1028 #endif
1029
1030 #if (CPUSS_CRYPTO_SHA512 ==1) && defined(CY_CRYPTO_CFG_SHA2_512_ENABLED)
1031 case CY_CRYPTO_MODE_SHA384:
1032 case CY_CRYPTO_MODE_SHA512:
1033 case CY_CRYPTO_MODE_SHA512_224:
1034 case CY_CRYPTO_MODE_SHA512_256:
1035 if(NULL != blockRemap)
1036 {
1037 Cy_Crypto_Core_V2_MemSet(base, blockRemap, 0x00U, (uint16_t)hashState->blockSize);
1038 }
1039 if(NULL != hashRemap)
1040 {
1041 Cy_Crypto_Core_V2_MemSet(base, hashRemap, 0x00U, (uint16_t)hashState->hashSize);
1042 }
1043 break;
1044 #endif
1045
1046 #if (CPUSS_CRYPTO_SHA3 == 1) && defined(CY_CRYPTO_CFG_SHA3_ENABLED)
1047 case CY_CRYPTO_MODE_SHA3_224:
1048 case CY_CRYPTO_MODE_SHA3_256:
1049 case CY_CRYPTO_MODE_SHA3_384:
1050 case CY_CRYPTO_MODE_SHA3_512:
1051 if(NULL != hashRemap)
1052 {
1053 Cy_Crypto_Core_V2_MemSet(base, hashRemap, 0x00U, (uint16_t)hashState->hashSize);
1054 } break;
1055 #endif
1056
1057 default:
1058 tmpResult = CY_CRYPTO_NOT_SUPPORTED;
1059 break;
1060
1061 }
1062
1063 if(CY_CRYPTO_SUCCESS == tmpResult)
1064 {
1065 /* Clears the hash state */
1066 Cy_Crypto_Core_V2_MemSet(base, (void*)stateRemap, 0x00U, (uint16_t)sizeof(cy_stc_crypto_sha_state_t));
1067
1068 /* Clears the memory buffer. */
1069 Cy_Crypto_Core_V2_RBClear(base);
1070 Cy_Crypto_Core_V2_Sync(base);
1071 }
1072 }
1073
1074 return (tmpResult);
1075 }
1076
1077
1078
1079
1080 /*******************************************************************************
1081 * Function Name: Cy_Crypto_Core_V2_Sha
1082 ****************************************************************************//**
1083 *
1084 * Performs the SHA Hash function.
1085 *
1086 * \param base
1087 * The pointer to the CRYPTO instance.
1088 *
1089 * \param mode
1090 * \ref cy_en_crypto_sha_mode_t
1091 *
1092 * \param message
1093 * The pointer to a message whose hash value is being computed.
1094 *
1095 * \param messageSize
1096 * The size of a message.
1097 *
1098 * \param digest
1099 * The pointer to the hash digest.
1100 *
1101 * \return
1102 * \ref cy_en_crypto_status_t
1103 *
1104 *******************************************************************************/
Cy_Crypto_Core_V2_Sha(CRYPTO_Type * base,uint8_t const * message,uint32_t messageSize,uint8_t * digest,cy_en_crypto_sha_mode_t mode)1105 cy_en_crypto_status_t Cy_Crypto_Core_V2_Sha(CRYPTO_Type *base,
1106 uint8_t const *message,
1107 uint32_t messageSize,
1108 uint8_t *digest,
1109 cy_en_crypto_sha_mode_t mode)
1110 {
1111 cy_en_crypto_status_t tmpResult = CY_CRYPTO_BAD_PARAMS;
1112
1113 #if (((CY_CPU_CORTEX_M7) && defined (ENABLE_CM7_DATA_CACHE)) || CY_CPU_CORTEX_M55)
1114 /* Allocate maximal space for the structure which stores the SHA buffers */
1115
1116 CY_ALIGN(__SCB_DCACHE_LINE_SIZE) static cy_stc_crypto_v2_sha_buffers_t shaBuffers;
1117 /* Allocate space for the structure which stores the SHA context */
1118 CY_ALIGN(__SCB_DCACHE_LINE_SIZE) static cy_stc_crypto_sha_state_t hashState;
1119
1120 #else
1121 /* Allocate maximal space for the structure which stores the SHA buffers */
1122 cy_stc_crypto_v2_sha_buffers_t shaBuffers = {{0,},{0,}};
1123 /* Allocate space for the structure which stores the SHA context */
1124 cy_stc_crypto_sha_state_t hashState = { 0 };
1125 #endif
1126
1127 Cy_Crypto_Core_V2_MemSet(base, (void*)&shaBuffers, 0x00U, (uint16_t)sizeof(cy_stc_crypto_v2_sha_buffers_t));
1128 Cy_Crypto_Core_V2_MemSet(base, (void*)&hashState, 0x00U, (uint16_t)sizeof(cy_stc_crypto_sha_state_t));
1129
1130 /* No buffers are needed for the Crypto_ver2 IP block. */
1131 tmpResult = Cy_Crypto_Core_V2_Sha_Init (base, &hashState, mode, &shaBuffers);
1132
1133 if (CY_CRYPTO_SUCCESS == tmpResult)
1134 {
1135 tmpResult = Cy_Crypto_Core_V2_Sha_Start (base, &hashState);
1136 }
1137 if (CY_CRYPTO_SUCCESS == tmpResult)
1138 {
1139 tmpResult = Cy_Crypto_Core_V2_Sha_Update (base, &hashState, message, messageSize);
1140 }
1141
1142 if (CY_CRYPTO_SUCCESS == tmpResult)
1143 {
1144 tmpResult = Cy_Crypto_Core_V2_Sha_Finish (base, &hashState, digest);
1145 }
1146
1147 if (CY_CRYPTO_SUCCESS == tmpResult)
1148 {
1149 tmpResult = Cy_Crypto_Core_V2_Sha_Free (base, &hashState);
1150 }
1151
1152 return (tmpResult);
1153 }
1154
1155
1156 #endif /* (CPUSS_CRYPTO_SHA == 1) && defined(CY_CRYPTO_CFG_SHA_C) */
1157
1158 #if defined(__cplusplus)
1159 }
1160 #endif
1161
1162 #endif /* defined(CY_CRYPTO_CFG_HW_V2_ENABLE) */
1163
1164 #endif /* defined(CY_IP_MXCRYPTO) */
1165
1166
1167 /* [] END OF FILE */
1168