source: UsbWattMeter/trunk/wolfssl-3.7.0/wolfcrypt/src/hash.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 8 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 4.7 KB
Line 
1/* hash.c
2 *
3 * Copyright (C) 2006-2015 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL. (formerly known as CyaSSL)
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-1301, USA
20 */
21
22#ifdef HAVE_CONFIG_H
23 #include <config.h>
24#endif
25
26#include <wolfssl/wolfcrypt/settings.h>
27#include <wolfssl/wolfcrypt/logging.h>
28#include <wolfssl/wolfcrypt/error-crypt.h>
29
30#if !defined(WOLFSSL_TI_HASH)
31
32#include <wolfssl/wolfcrypt/hash.h>
33
34#if !defined(NO_MD5)
35void wc_Md5GetHash(Md5* md5, byte* hash)
36{
37 Md5 save = *md5 ;
38 wc_Md5Final(md5, hash) ;
39 *md5 = save ;
40}
41
42WOLFSSL_API void wc_Md5RestorePos(Md5* m1, Md5* m2) {
43 *m1 = *m2 ;
44}
45
46#endif
47
48#if !defined(NO_SHA)
49int wc_ShaGetHash(Sha* sha, byte* hash)
50{
51 int ret ;
52 Sha save = *sha ;
53 ret = wc_ShaFinal(sha, hash) ;
54 *sha = save ;
55 return ret ;
56}
57
58WOLFSSL_API void wc_ShaRestorePos(Sha* s1, Sha* s2) {
59 *s1 = *s2 ;
60}
61
62int wc_ShaHash(const byte* data, word32 len, byte* hash)
63{
64 int ret = 0;
65#ifdef WOLFSSL_SMALL_STACK
66 Sha* sha;
67#else
68 Sha sha[1];
69#endif
70
71#ifdef WOLFSSL_SMALL_STACK
72 sha = (Sha*)XMALLOC(sizeof(Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
73 if (sha == NULL)
74 return MEMORY_E;
75#endif
76
77 if ((ret = wc_InitSha(sha)) != 0) {
78 WOLFSSL_MSG("wc_InitSha failed");
79 }
80 else {
81 wc_ShaUpdate(sha, data, len);
82 wc_ShaFinal(sha, hash);
83 }
84
85#ifdef WOLFSSL_SMALL_STACK
86 XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
87#endif
88
89 return ret;
90
91}
92
93#endif /* !defined(NO_SHA) */
94
95#if !defined(NO_SHA256)
96int wc_Sha256GetHash(Sha256* sha256, byte* hash)
97{
98 int ret ;
99 Sha256 save = *sha256 ;
100 ret = wc_Sha256Final(sha256, hash) ;
101 *sha256 = save ;
102 return ret ;
103}
104
105WOLFSSL_API void wc_Sha256RestorePos(Sha256* s1, Sha256* s2) {
106 *s1 = *s2 ;
107}
108
109int wc_Sha256Hash(const byte* data, word32 len, byte* hash)
110{
111 int ret = 0;
112#ifdef WOLFSSL_SMALL_STACK
113 Sha256* sha256;
114#else
115 Sha256 sha256[1];
116#endif
117
118#ifdef WOLFSSL_SMALL_STACK
119 sha256 = (Sha256*)XMALLOC(sizeof(Sha256), NULL, DYNAMIC_TYPE_TMP_BUFFER);
120 if (sha256 == NULL)
121 return MEMORY_E;
122#endif
123
124 if ((ret = wc_InitSha256(sha256)) != 0) {
125 WOLFSSL_MSG("InitSha256 failed");
126 }
127 else if ((ret = wc_Sha256Update(sha256, data, len)) != 0) {
128 WOLFSSL_MSG("Sha256Update failed");
129 }
130 else if ((ret = wc_Sha256Final(sha256, hash)) != 0) {
131 WOLFSSL_MSG("Sha256Final failed");
132 }
133
134#ifdef WOLFSSL_SMALL_STACK
135 XFREE(sha256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
136#endif
137
138 return ret;
139}
140
141#endif /* !defined(NO_SHA256) */
142
143#endif /* !defined(WOLFSSL_TI_HASH) */
144
145#if defined(WOLFSSL_SHA512)
146int wc_Sha512Hash(const byte* data, word32 len, byte* hash)
147{
148 int ret = 0;
149#ifdef WOLFSSL_SMALL_STACK
150 Sha512* sha512;
151#else
152 Sha512 sha512[1];
153#endif
154
155#ifdef WOLFSSL_SMALL_STACK
156 sha512 = (Sha512*)XMALLOC(sizeof(Sha512), NULL, DYNAMIC_TYPE_TMP_BUFFER);
157 if (sha512 == NULL)
158 return MEMORY_E;
159#endif
160
161 if ((ret = wc_InitSha512(sha512)) != 0) {
162 WOLFSSL_MSG("InitSha512 failed");
163 }
164 else if ((ret = wc_Sha512Update(sha512, data, len)) != 0) {
165 WOLFSSL_MSG("Sha512Update failed");
166 }
167 else if ((ret = wc_Sha512Final(sha512, hash)) != 0) {
168 WOLFSSL_MSG("Sha512Final failed");
169 }
170
171#ifdef WOLFSSL_SMALL_STACK
172 XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
173#endif
174
175 return ret;
176}
177
178#if defined(WOLFSSL_SHA384)
179int wc_Sha384Hash(const byte* data, word32 len, byte* hash)
180{
181 int ret = 0;
182#ifdef WOLFSSL_SMALL_STACK
183 Sha384* sha384;
184#else
185 Sha384 sha384[1];
186#endif
187
188#ifdef WOLFSSL_SMALL_STACK
189 sha384 = (Sha384*)XMALLOC(sizeof(Sha384), NULL, DYNAMIC_TYPE_TMP_BUFFER);
190 if (sha384 == NULL)
191 return MEMORY_E;
192#endif
193
194 if ((ret = wc_InitSha384(sha384)) != 0) {
195 WOLFSSL_MSG("InitSha384 failed");
196 }
197 else if ((ret = wc_Sha384Update(sha384, data, len)) != 0) {
198 WOLFSSL_MSG("Sha384Update failed");
199 }
200 else if ((ret = wc_Sha384Final(sha384, hash)) != 0) {
201 WOLFSSL_MSG("Sha384Final failed");
202 }
203
204#ifdef WOLFSSL_SMALL_STACK
205 XFREE(sha384, NULL, DYNAMIC_TYPE_TMP_BUFFER);
206#endif
207
208 return ret;
209}
210
211#endif /* defined(WOLFSSL_SHA384) */
212#endif /* defined(WOLFSSL_SHA512) */
Note: See TracBrowser for help on using the repository browser.