1 /* cryptoCellHash.c
2 *
3 * Copyright (C) 2006-2021 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL.
6 *
7 * wolfSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * wolfSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <wolfssl/wolfcrypt/settings.h>
27
28 /* This source is included in wc_port.c */
29 /* WOLFSSL_CRYPTOCELL_HASH_C is defined by wc_port.c in case compile tries
30 to include this .c directly */
31 #ifdef WOLFSSL_CRYPTOCELL_HASH_C
32 #if !defined(NO_SHA256) && defined(WOLFSSL_CRYPTOCELL)
33
34 #include <wolfssl/wolfcrypt/error-crypt.h>
35 #include <wolfssl/wolfcrypt/logging.h>
36 #include <wolfssl/wolfcrypt/sha256.h>
37 #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h>
38
39 #ifdef NO_INLINE
40 #include <wolfssl/wolfcrypt/misc.h>
41 #else
42 #define WOLFSSL_MISC_INCLUDED
43 #include <wolfcrypt/src/misc.c>
44 #endif
45
wc_InitSha256_ex(wc_Sha256 * sha256,void * heap,int devId)46 int wc_InitSha256_ex(wc_Sha256* sha256, void* heap, int devId)
47 {
48 CRYSError_t ret = 0;
49
50 (void)heap;
51 (void)devId;
52
53 if (sha256 == NULL)
54 return BAD_FUNC_ARG;
55
56 XMEMSET(sha256->digest, 0, sizeof(sha256->digest));
57
58 /* initializes the HASH context and machine to the supported mode.*/
59 ret = CRYS_HASH_Init(&sha256->ctx, CRYS_HASH_SHA256_mode);
60
61 if (ret != SA_SILIB_RET_OK){
62 WOLFSSL_MSG("Error CRYS_HASH_Init failed");
63 }
64
65 return ret;
66 }
67
wc_InitSha256(Sha256 * sha256)68 int wc_InitSha256(Sha256* sha256)
69 {
70 return wc_InitSha256_ex(sha256, NULL, INVALID_DEVID);
71 }
72
wc_Sha256Update(wc_Sha256 * sha256,const byte * data,word32 len)73 int wc_Sha256Update(wc_Sha256* sha256, const byte* data, word32 len)
74 {
75 CRYSError_t ret = 0;
76 size_t length;
77 size_t remaining = len;
78 byte const * p_cur = data;
79
80 if (sha256 == NULL || (data == NULL && len > 0)) {
81 return BAD_FUNC_ARG;
82 }
83
84 if (data == NULL && len == 0) {
85 /* valid, but do nothing */
86 return 0;
87 }
88
89 /* If the input is larger than CC310_MAX_LENGTH_DMA, split into smaller */
90 do {
91 length = (remaining > CC310_MAX_LENGTH_DMA) ?
92 CC310_MAX_LENGTH_DMA : remaining;
93
94 ret = CRYS_HASH_Update(&sha256->ctx, (uint8_t *)p_cur, length);
95
96 remaining -= length;
97 p_cur += length;
98
99 } while (ret == CRYS_OK && remaining > 0);
100
101 return ret;
102 }
103
wc_Sha256Final(wc_Sha256 * sha256,byte * hash)104 int wc_Sha256Final(wc_Sha256* sha256, byte* hash)
105 {
106 CRYSError_t ret = 0;
107 CRYS_HASH_Result_t hashResult;
108
109 if (sha256 == NULL || hash == NULL) {
110 return BAD_FUNC_ARG;
111 }
112
113 ret = CRYS_HASH_Finish(&sha256->ctx, hashResult);
114
115 if (ret != SA_SILIB_RET_OK){
116 WOLFSSL_MSG("Error CRYS_HASH_Finish failed");
117 return ret;
118 }
119 XMEMCPY(sha256->digest, hashResult, WC_SHA256_DIGEST_SIZE);
120
121 XMEMCPY(hash, sha256->digest, WC_SHA256_DIGEST_SIZE);
122
123 /* reset state */
124 return wc_InitSha256_ex(sha256, NULL, INVALID_DEVID);
125 }
126
wc_Sha256Free(wc_Sha256 * sha256)127 void wc_Sha256Free(wc_Sha256* sha256)
128 {
129 if (sha256 == NULL)
130 return;
131 }
132
133 #endif /* !NO_SHA256 && WOLFSSL_CRYPTOCELL */
134 #endif /* WOLFSSL_CRYPTOCELL_HASH_C */
135