wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
Parent:
11:cee25a834751
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 11:cee25a834751 1 /* hash.c
wolfSSL 11:cee25a834751 2 *
wolfSSL 11:cee25a834751 3 * Copyright (C) 2006-2016 wolfSSL Inc.
wolfSSL 11:cee25a834751 4 *
wolfSSL 11:cee25a834751 5 * This file is part of wolfSSL.
wolfSSL 11:cee25a834751 6 *
wolfSSL 11:cee25a834751 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 11:cee25a834751 8 * it under the terms of the GNU General Public License as published by
wolfSSL 11:cee25a834751 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 11:cee25a834751 10 * (at your option) any later version.
wolfSSL 11:cee25a834751 11 *
wolfSSL 11:cee25a834751 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 11:cee25a834751 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 11:cee25a834751 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 11:cee25a834751 15 * GNU General Public License for more details.
wolfSSL 11:cee25a834751 16 *
wolfSSL 11:cee25a834751 17 * You should have received a copy of the GNU General Public License
wolfSSL 11:cee25a834751 18 * along with this program; if not, write to the Free Software
wolfSSL 11:cee25a834751 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 11:cee25a834751 20 */
wolfSSL 11:cee25a834751 21
wolfSSL 11:cee25a834751 22
wolfSSL 11:cee25a834751 23 #ifdef HAVE_CONFIG_H
wolfSSL 11:cee25a834751 24 #include <config.h>
wolfSSL 11:cee25a834751 25 #endif
wolfSSL 11:cee25a834751 26
wolfSSL 11:cee25a834751 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 11:cee25a834751 28 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 11:cee25a834751 29 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 11:cee25a834751 30 #ifndef NO_ASN
wolfSSL 11:cee25a834751 31 #include <wolfssl/wolfcrypt/asn.h>
wolfSSL 11:cee25a834751 32 #endif
wolfSSL 11:cee25a834751 33
wolfSSL 11:cee25a834751 34 #include <wolfssl/wolfcrypt/hash.h>
wolfSSL 11:cee25a834751 35
wolfSSL 11:cee25a834751 36
wolfSSL 11:cee25a834751 37 #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
wolfSSL 11:cee25a834751 38
wolfSSL 11:cee25a834751 39 #ifdef NO_ASN
wolfSSL 11:cee25a834751 40 enum Hash_Sum {
wolfSSL 11:cee25a834751 41 MD2h = 646,
wolfSSL 11:cee25a834751 42 MD5h = 649,
wolfSSL 11:cee25a834751 43 SHAh = 88,
wolfSSL 11:cee25a834751 44 SHA224h = 417,
wolfSSL 11:cee25a834751 45 SHA256h = 414,
wolfSSL 11:cee25a834751 46 SHA384h = 415,
wolfSSL 11:cee25a834751 47 SHA512h = 416
wolfSSL 11:cee25a834751 48 };
wolfSSL 11:cee25a834751 49 #endif
wolfSSL 11:cee25a834751 50
wolfSSL 11:cee25a834751 51 int wc_HashGetOID(enum wc_HashType hash_type)
wolfSSL 11:cee25a834751 52 {
wolfSSL 11:cee25a834751 53 int oid = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 11:cee25a834751 54 switch(hash_type)
wolfSSL 11:cee25a834751 55 {
wolfSSL 11:cee25a834751 56 case WC_HASH_TYPE_MD2:
wolfSSL 11:cee25a834751 57 #ifdef WOLFSSL_MD2
wolfSSL 11:cee25a834751 58 oid = MD2h;
wolfSSL 11:cee25a834751 59 #endif
wolfSSL 11:cee25a834751 60 break;
wolfSSL 11:cee25a834751 61 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 11:cee25a834751 62 case WC_HASH_TYPE_MD5:
wolfSSL 11:cee25a834751 63 #ifndef NO_MD5
wolfSSL 11:cee25a834751 64 oid = MD5h;
wolfSSL 11:cee25a834751 65 #endif
wolfSSL 11:cee25a834751 66 break;
wolfSSL 11:cee25a834751 67 case WC_HASH_TYPE_SHA:
wolfSSL 11:cee25a834751 68 #ifndef NO_SHA
wolfSSL 11:cee25a834751 69 oid = SHAh;
wolfSSL 11:cee25a834751 70 #endif
wolfSSL 11:cee25a834751 71 break;
wolfSSL 11:cee25a834751 72 case WC_HASH_TYPE_SHA224:
wolfSSL 11:cee25a834751 73 #if defined(WOLFSSL_SHA224)
wolfSSL 11:cee25a834751 74 oid = SHA224h;
wolfSSL 11:cee25a834751 75 #endif
wolfSSL 11:cee25a834751 76 break;
wolfSSL 11:cee25a834751 77 case WC_HASH_TYPE_SHA256:
wolfSSL 11:cee25a834751 78 #ifndef NO_SHA256
wolfSSL 11:cee25a834751 79 oid = SHA256h;
wolfSSL 11:cee25a834751 80 #endif
wolfSSL 11:cee25a834751 81 break;
wolfSSL 11:cee25a834751 82 case WC_HASH_TYPE_SHA384:
wolfSSL 11:cee25a834751 83 #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384)
wolfSSL 11:cee25a834751 84 oid = SHA384h;
wolfSSL 11:cee25a834751 85 #endif
wolfSSL 11:cee25a834751 86 break;
wolfSSL 11:cee25a834751 87 case WC_HASH_TYPE_SHA512:
wolfSSL 11:cee25a834751 88 #ifdef WOLFSSL_SHA512
wolfSSL 11:cee25a834751 89 oid = SHA512h;
wolfSSL 11:cee25a834751 90 #endif
wolfSSL 11:cee25a834751 91 break;
wolfSSL 11:cee25a834751 92
wolfSSL 11:cee25a834751 93 /* Not Supported */
wolfSSL 11:cee25a834751 94 case WC_HASH_TYPE_MD4:
wolfSSL 11:cee25a834751 95 case WC_HASH_TYPE_NONE:
wolfSSL 11:cee25a834751 96 default:
wolfSSL 11:cee25a834751 97 oid = BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 98 break;
wolfSSL 11:cee25a834751 99 }
wolfSSL 11:cee25a834751 100 return oid;
wolfSSL 11:cee25a834751 101 }
wolfSSL 11:cee25a834751 102 #endif
wolfSSL 11:cee25a834751 103
wolfSSL 11:cee25a834751 104 /* Get Hash digest size */
wolfSSL 11:cee25a834751 105 int wc_HashGetDigestSize(enum wc_HashType hash_type)
wolfSSL 11:cee25a834751 106 {
wolfSSL 11:cee25a834751 107 int dig_size = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 11:cee25a834751 108 switch(hash_type)
wolfSSL 11:cee25a834751 109 {
wolfSSL 11:cee25a834751 110 case WC_HASH_TYPE_MD5:
wolfSSL 11:cee25a834751 111 #ifndef NO_MD5
wolfSSL 11:cee25a834751 112 dig_size = MD5_DIGEST_SIZE;
wolfSSL 11:cee25a834751 113 #endif
wolfSSL 11:cee25a834751 114 break;
wolfSSL 11:cee25a834751 115 case WC_HASH_TYPE_SHA:
wolfSSL 11:cee25a834751 116 #ifndef NO_SHA
wolfSSL 11:cee25a834751 117 dig_size = SHA_DIGEST_SIZE;
wolfSSL 11:cee25a834751 118 #endif
wolfSSL 11:cee25a834751 119 break;
wolfSSL 11:cee25a834751 120 case WC_HASH_TYPE_SHA224:
wolfSSL 11:cee25a834751 121 #ifdef WOLFSSL_SHA224
wolfSSL 11:cee25a834751 122 dig_size = SHA224_DIGEST_SIZE;
wolfSSL 11:cee25a834751 123 #endif
wolfSSL 11:cee25a834751 124 break;
wolfSSL 11:cee25a834751 125 case WC_HASH_TYPE_SHA256:
wolfSSL 11:cee25a834751 126 #ifndef NO_SHA256
wolfSSL 11:cee25a834751 127 dig_size = SHA256_DIGEST_SIZE;
wolfSSL 11:cee25a834751 128 #endif
wolfSSL 11:cee25a834751 129 break;
wolfSSL 11:cee25a834751 130 case WC_HASH_TYPE_SHA384:
wolfSSL 11:cee25a834751 131 #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384)
wolfSSL 11:cee25a834751 132 dig_size = SHA384_DIGEST_SIZE;
wolfSSL 11:cee25a834751 133 #endif
wolfSSL 11:cee25a834751 134 break;
wolfSSL 11:cee25a834751 135 case WC_HASH_TYPE_SHA512:
wolfSSL 11:cee25a834751 136 #ifdef WOLFSSL_SHA512
wolfSSL 11:cee25a834751 137 dig_size = SHA512_DIGEST_SIZE;
wolfSSL 11:cee25a834751 138 #endif
wolfSSL 11:cee25a834751 139 break;
wolfSSL 11:cee25a834751 140 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 11:cee25a834751 141 #if !defined(NO_MD5) && !defined(NO_SHA)
wolfSSL 11:cee25a834751 142 dig_size = MD5_DIGEST_SIZE + SHA_DIGEST_SIZE;
wolfSSL 11:cee25a834751 143 #endif
wolfSSL 11:cee25a834751 144 break;
wolfSSL 11:cee25a834751 145
wolfSSL 11:cee25a834751 146 /* Not Supported */
wolfSSL 11:cee25a834751 147 case WC_HASH_TYPE_MD2:
wolfSSL 11:cee25a834751 148 case WC_HASH_TYPE_MD4:
wolfSSL 11:cee25a834751 149 case WC_HASH_TYPE_NONE:
wolfSSL 11:cee25a834751 150 default:
wolfSSL 11:cee25a834751 151 dig_size = BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 152 break;
wolfSSL 11:cee25a834751 153 }
wolfSSL 11:cee25a834751 154 return dig_size;
wolfSSL 11:cee25a834751 155 }
wolfSSL 11:cee25a834751 156
wolfSSL 11:cee25a834751 157 /* Generic Hashing Wrapper */
wolfSSL 11:cee25a834751 158 int wc_Hash(enum wc_HashType hash_type, const byte* data,
wolfSSL 11:cee25a834751 159 word32 data_len, byte* hash, word32 hash_len)
wolfSSL 11:cee25a834751 160 {
wolfSSL 11:cee25a834751 161 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 11:cee25a834751 162 word32 dig_size;
wolfSSL 11:cee25a834751 163
wolfSSL 11:cee25a834751 164 /* Validate hash buffer size */
wolfSSL 11:cee25a834751 165 dig_size = wc_HashGetDigestSize(hash_type);
wolfSSL 11:cee25a834751 166 if (hash_len < dig_size) {
wolfSSL 11:cee25a834751 167 return BUFFER_E;
wolfSSL 11:cee25a834751 168 }
wolfSSL 11:cee25a834751 169
wolfSSL 11:cee25a834751 170 /* Suppress possible unused arg if all hashing is disabled */
wolfSSL 11:cee25a834751 171 (void)data;
wolfSSL 11:cee25a834751 172 (void)data_len;
wolfSSL 11:cee25a834751 173 (void)hash;
wolfSSL 11:cee25a834751 174 (void)hash_len;
wolfSSL 11:cee25a834751 175
wolfSSL 11:cee25a834751 176 switch(hash_type)
wolfSSL 11:cee25a834751 177 {
wolfSSL 11:cee25a834751 178 case WC_HASH_TYPE_MD5:
wolfSSL 11:cee25a834751 179 #ifndef NO_MD5
wolfSSL 11:cee25a834751 180 ret = wc_Md5Hash(data, data_len, hash);
wolfSSL 11:cee25a834751 181 #endif
wolfSSL 11:cee25a834751 182 break;
wolfSSL 11:cee25a834751 183 case WC_HASH_TYPE_SHA:
wolfSSL 11:cee25a834751 184 #ifndef NO_SHA
wolfSSL 11:cee25a834751 185 ret = wc_ShaHash(data, data_len, hash);
wolfSSL 11:cee25a834751 186 #endif
wolfSSL 11:cee25a834751 187 break;
wolfSSL 11:cee25a834751 188 case WC_HASH_TYPE_SHA224:
wolfSSL 11:cee25a834751 189 #ifdef WOLFSSL_SHA224
wolfSSL 11:cee25a834751 190 ret = wc_Sha224Hash(data, data_len, hash);
wolfSSL 11:cee25a834751 191 #endif
wolfSSL 11:cee25a834751 192 break;
wolfSSL 11:cee25a834751 193 case WC_HASH_TYPE_SHA256:
wolfSSL 11:cee25a834751 194 #ifndef NO_SHA256
wolfSSL 11:cee25a834751 195 ret = wc_Sha256Hash(data, data_len, hash);
wolfSSL 11:cee25a834751 196 #endif
wolfSSL 11:cee25a834751 197 break;
wolfSSL 11:cee25a834751 198 case WC_HASH_TYPE_SHA384:
wolfSSL 11:cee25a834751 199 #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384)
wolfSSL 11:cee25a834751 200 ret = wc_Sha384Hash(data, data_len, hash);
wolfSSL 11:cee25a834751 201 #endif
wolfSSL 11:cee25a834751 202 break;
wolfSSL 11:cee25a834751 203 case WC_HASH_TYPE_SHA512:
wolfSSL 11:cee25a834751 204 #ifdef WOLFSSL_SHA512
wolfSSL 11:cee25a834751 205 ret = wc_Sha512Hash(data, data_len, hash);
wolfSSL 11:cee25a834751 206 #endif
wolfSSL 11:cee25a834751 207 break;
wolfSSL 11:cee25a834751 208 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 11:cee25a834751 209 #if !defined(NO_MD5) && !defined(NO_SHA)
wolfSSL 11:cee25a834751 210 ret = wc_Md5Hash(data, data_len, hash);
wolfSSL 11:cee25a834751 211 if (ret == 0) {
wolfSSL 11:cee25a834751 212 ret = wc_ShaHash(data, data_len, &hash[MD5_DIGEST_SIZE]);
wolfSSL 11:cee25a834751 213 }
wolfSSL 11:cee25a834751 214 #endif
wolfSSL 11:cee25a834751 215 break;
wolfSSL 11:cee25a834751 216
wolfSSL 11:cee25a834751 217 /* Not Supported */
wolfSSL 11:cee25a834751 218 case WC_HASH_TYPE_MD2:
wolfSSL 11:cee25a834751 219 case WC_HASH_TYPE_MD4:
wolfSSL 11:cee25a834751 220 case WC_HASH_TYPE_NONE:
wolfSSL 11:cee25a834751 221 default:
wolfSSL 11:cee25a834751 222 ret = BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 223 break;
wolfSSL 11:cee25a834751 224 }
wolfSSL 11:cee25a834751 225 return ret;
wolfSSL 11:cee25a834751 226 }
wolfSSL 11:cee25a834751 227
wolfSSL 11:cee25a834751 228 int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type)
wolfSSL 11:cee25a834751 229 {
wolfSSL 11:cee25a834751 230 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 11:cee25a834751 231
wolfSSL 11:cee25a834751 232 if (hash == NULL)
wolfSSL 11:cee25a834751 233 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 234
wolfSSL 11:cee25a834751 235 switch (type) {
wolfSSL 11:cee25a834751 236 case WC_HASH_TYPE_MD5:
wolfSSL 11:cee25a834751 237 #ifndef NO_MD5
wolfSSL 11:cee25a834751 238 wc_InitMd5(&hash->md5);
wolfSSL 11:cee25a834751 239 ret = 0;
wolfSSL 11:cee25a834751 240 #endif
wolfSSL 11:cee25a834751 241 break;
wolfSSL 11:cee25a834751 242 case WC_HASH_TYPE_SHA:
wolfSSL 11:cee25a834751 243 #ifndef NO_SHA
wolfSSL 11:cee25a834751 244 ret = wc_InitSha(&hash->sha);
wolfSSL 11:cee25a834751 245 #endif
wolfSSL 11:cee25a834751 246 break;
wolfSSL 11:cee25a834751 247 case WC_HASH_TYPE_SHA224:
wolfSSL 11:cee25a834751 248 #ifdef WOLFSSL_SHA224
wolfSSL 11:cee25a834751 249 ret = wc_InitSha224(&hash->sha224);
wolfSSL 11:cee25a834751 250 #endif
wolfSSL 11:cee25a834751 251 break;
wolfSSL 11:cee25a834751 252 case WC_HASH_TYPE_SHA256:
wolfSSL 11:cee25a834751 253 #ifndef NO_SHA256
wolfSSL 11:cee25a834751 254 ret = wc_InitSha256(&hash->sha256);
wolfSSL 11:cee25a834751 255 #endif
wolfSSL 11:cee25a834751 256 break;
wolfSSL 11:cee25a834751 257 case WC_HASH_TYPE_SHA384:
wolfSSL 11:cee25a834751 258 #ifdef WOLFSSL_SHA384
wolfSSL 11:cee25a834751 259 ret = wc_InitSha384(&hash->sha384);
wolfSSL 11:cee25a834751 260 #endif
wolfSSL 11:cee25a834751 261 break;
wolfSSL 11:cee25a834751 262 case WC_HASH_TYPE_SHA512:
wolfSSL 11:cee25a834751 263 #ifdef WOLFSSL_SHA512
wolfSSL 11:cee25a834751 264 ret = wc_InitSha512(&hash->sha512);
wolfSSL 11:cee25a834751 265 #endif
wolfSSL 11:cee25a834751 266 break;
wolfSSL 11:cee25a834751 267
wolfSSL 11:cee25a834751 268 /* not supported */
wolfSSL 11:cee25a834751 269 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 11:cee25a834751 270 case WC_HASH_TYPE_MD2:
wolfSSL 11:cee25a834751 271 case WC_HASH_TYPE_MD4:
wolfSSL 11:cee25a834751 272 case WC_HASH_TYPE_NONE:
wolfSSL 11:cee25a834751 273 default:
wolfSSL 11:cee25a834751 274 ret = BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 275 };
wolfSSL 11:cee25a834751 276
wolfSSL 11:cee25a834751 277 return ret;
wolfSSL 11:cee25a834751 278 }
wolfSSL 11:cee25a834751 279
wolfSSL 11:cee25a834751 280 int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, const byte* data,
wolfSSL 11:cee25a834751 281 word32 dataSz)
wolfSSL 11:cee25a834751 282 {
wolfSSL 11:cee25a834751 283 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 11:cee25a834751 284
wolfSSL 11:cee25a834751 285 if (hash == NULL || data == NULL)
wolfSSL 11:cee25a834751 286 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 287
wolfSSL 11:cee25a834751 288 switch (type) {
wolfSSL 11:cee25a834751 289 case WC_HASH_TYPE_MD5:
wolfSSL 11:cee25a834751 290 #ifndef NO_MD5
wolfSSL 11:cee25a834751 291 wc_Md5Update(&hash->md5, data, dataSz);
wolfSSL 11:cee25a834751 292 ret = 0;
wolfSSL 11:cee25a834751 293 #endif
wolfSSL 11:cee25a834751 294 break;
wolfSSL 11:cee25a834751 295 case WC_HASH_TYPE_SHA:
wolfSSL 11:cee25a834751 296 #ifndef NO_SHA
wolfSSL 11:cee25a834751 297 ret = wc_ShaUpdate(&hash->sha, data, dataSz);
wolfSSL 11:cee25a834751 298 if (ret != 0)
wolfSSL 11:cee25a834751 299 return ret;
wolfSSL 11:cee25a834751 300 #endif
wolfSSL 11:cee25a834751 301 break;
wolfSSL 11:cee25a834751 302 case WC_HASH_TYPE_SHA224:
wolfSSL 11:cee25a834751 303 #ifdef WOLFSSL_SHA224
wolfSSL 11:cee25a834751 304 ret = wc_Sha224Update(&hash->sha224, data, dataSz);
wolfSSL 11:cee25a834751 305 #endif
wolfSSL 11:cee25a834751 306 break;
wolfSSL 11:cee25a834751 307 case WC_HASH_TYPE_SHA256:
wolfSSL 11:cee25a834751 308 #ifndef NO_SHA256
wolfSSL 11:cee25a834751 309 ret = wc_Sha256Update(&hash->sha256, data, dataSz);
wolfSSL 11:cee25a834751 310 #endif
wolfSSL 11:cee25a834751 311 break;
wolfSSL 11:cee25a834751 312 case WC_HASH_TYPE_SHA384:
wolfSSL 11:cee25a834751 313 #ifdef WOLFSSL_SHA384
wolfSSL 11:cee25a834751 314 ret = wc_Sha384Update(&hash->sha384, data, dataSz);
wolfSSL 11:cee25a834751 315 #endif
wolfSSL 11:cee25a834751 316 break;
wolfSSL 11:cee25a834751 317 case WC_HASH_TYPE_SHA512:
wolfSSL 11:cee25a834751 318 #ifdef WOLFSSL_SHA512
wolfSSL 11:cee25a834751 319 ret = wc_Sha512Update(&hash->sha512, data, dataSz);
wolfSSL 11:cee25a834751 320 #endif
wolfSSL 11:cee25a834751 321 break;
wolfSSL 11:cee25a834751 322
wolfSSL 11:cee25a834751 323 /* not supported */
wolfSSL 11:cee25a834751 324 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 11:cee25a834751 325 case WC_HASH_TYPE_MD2:
wolfSSL 11:cee25a834751 326 case WC_HASH_TYPE_MD4:
wolfSSL 11:cee25a834751 327 case WC_HASH_TYPE_NONE:
wolfSSL 11:cee25a834751 328 default:
wolfSSL 11:cee25a834751 329 ret = BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 330 };
wolfSSL 11:cee25a834751 331
wolfSSL 11:cee25a834751 332 return ret;
wolfSSL 11:cee25a834751 333 }
wolfSSL 11:cee25a834751 334
wolfSSL 11:cee25a834751 335 int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out)
wolfSSL 11:cee25a834751 336 {
wolfSSL 11:cee25a834751 337 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 11:cee25a834751 338
wolfSSL 11:cee25a834751 339 if (hash == NULL || out == NULL)
wolfSSL 11:cee25a834751 340 return BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 341
wolfSSL 11:cee25a834751 342 switch (type) {
wolfSSL 11:cee25a834751 343 case WC_HASH_TYPE_MD5:
wolfSSL 11:cee25a834751 344 #ifndef NO_MD5
wolfSSL 11:cee25a834751 345 wc_Md5Final(&hash->md5, out);
wolfSSL 11:cee25a834751 346 ret = 0;
wolfSSL 11:cee25a834751 347 #endif
wolfSSL 11:cee25a834751 348 break;
wolfSSL 11:cee25a834751 349 case WC_HASH_TYPE_SHA:
wolfSSL 11:cee25a834751 350 #ifndef NO_SHA
wolfSSL 11:cee25a834751 351 ret = wc_ShaFinal(&hash->sha, out);
wolfSSL 11:cee25a834751 352 #endif
wolfSSL 11:cee25a834751 353 break;
wolfSSL 11:cee25a834751 354 case WC_HASH_TYPE_SHA224:
wolfSSL 11:cee25a834751 355 #ifdef WOLFSSL_SHA224
wolfSSL 11:cee25a834751 356 ret = wc_Sha224Final(&hash->sha224, out);
wolfSSL 11:cee25a834751 357 #endif
wolfSSL 11:cee25a834751 358 break;
wolfSSL 11:cee25a834751 359 case WC_HASH_TYPE_SHA256:
wolfSSL 11:cee25a834751 360 #ifndef NO_SHA256
wolfSSL 11:cee25a834751 361 ret = wc_Sha256Final(&hash->sha256, out);
wolfSSL 11:cee25a834751 362 #endif
wolfSSL 11:cee25a834751 363 break;
wolfSSL 11:cee25a834751 364 case WC_HASH_TYPE_SHA384:
wolfSSL 11:cee25a834751 365 #ifdef WOLFSSL_SHA384
wolfSSL 11:cee25a834751 366 ret = wc_Sha384Final(&hash->sha384, out);
wolfSSL 11:cee25a834751 367 #endif
wolfSSL 11:cee25a834751 368 break;
wolfSSL 11:cee25a834751 369 case WC_HASH_TYPE_SHA512:
wolfSSL 11:cee25a834751 370 #ifdef WOLFSSL_SHA512
wolfSSL 11:cee25a834751 371 ret = wc_Sha512Final(&hash->sha512, out);
wolfSSL 11:cee25a834751 372 #endif
wolfSSL 11:cee25a834751 373 break;
wolfSSL 11:cee25a834751 374
wolfSSL 11:cee25a834751 375 /* not supported */
wolfSSL 11:cee25a834751 376 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 11:cee25a834751 377 case WC_HASH_TYPE_MD2:
wolfSSL 11:cee25a834751 378 case WC_HASH_TYPE_MD4:
wolfSSL 11:cee25a834751 379 case WC_HASH_TYPE_NONE:
wolfSSL 11:cee25a834751 380 default:
wolfSSL 11:cee25a834751 381 ret = BAD_FUNC_ARG;
wolfSSL 11:cee25a834751 382 };
wolfSSL 11:cee25a834751 383
wolfSSL 11:cee25a834751 384 return ret;
wolfSSL 11:cee25a834751 385 }
wolfSSL 11:cee25a834751 386
wolfSSL 11:cee25a834751 387
wolfSSL 11:cee25a834751 388 #if !defined(WOLFSSL_TI_HASH)
wolfSSL 11:cee25a834751 389
wolfSSL 11:cee25a834751 390 #if !defined(NO_MD5)
wolfSSL 11:cee25a834751 391 int wc_Md5Hash(const byte* data, word32 len, byte* hash)
wolfSSL 11:cee25a834751 392 {
wolfSSL 11:cee25a834751 393 int ret;
wolfSSL 11:cee25a834751 394 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 395 Md5* md5;
wolfSSL 11:cee25a834751 396 #else
wolfSSL 11:cee25a834751 397 Md5 md5[1];
wolfSSL 11:cee25a834751 398 #endif
wolfSSL 11:cee25a834751 399
wolfSSL 11:cee25a834751 400 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 401 md5 = (Md5*)XMALLOC(sizeof(Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 402 if (md5 == NULL)
wolfSSL 11:cee25a834751 403 return MEMORY_E;
wolfSSL 11:cee25a834751 404 #endif
wolfSSL 11:cee25a834751 405
wolfSSL 11:cee25a834751 406 ret = wc_InitMd5(md5);
wolfSSL 11:cee25a834751 407 if (ret == 0) {
wolfSSL 11:cee25a834751 408 ret = wc_Md5Update(md5, data, len);
wolfSSL 11:cee25a834751 409 if (ret == 0) {
wolfSSL 11:cee25a834751 410 ret = wc_Md5Final(md5, hash);
wolfSSL 11:cee25a834751 411 }
wolfSSL 11:cee25a834751 412 }
wolfSSL 11:cee25a834751 413
wolfSSL 11:cee25a834751 414 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 415 XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 416 #endif
wolfSSL 11:cee25a834751 417
wolfSSL 11:cee25a834751 418 return ret;
wolfSSL 11:cee25a834751 419 }
wolfSSL 11:cee25a834751 420 #endif /* !NO_MD5 */
wolfSSL 11:cee25a834751 421
wolfSSL 11:cee25a834751 422 #if !defined(NO_SHA)
wolfSSL 11:cee25a834751 423 int wc_ShaHash(const byte* data, word32 len, byte* hash)
wolfSSL 11:cee25a834751 424 {
wolfSSL 11:cee25a834751 425 int ret = 0;
wolfSSL 11:cee25a834751 426 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 427 Sha* sha;
wolfSSL 11:cee25a834751 428 #else
wolfSSL 11:cee25a834751 429 Sha sha[1];
wolfSSL 11:cee25a834751 430 #endif
wolfSSL 11:cee25a834751 431
wolfSSL 11:cee25a834751 432 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 433 sha = (Sha*)XMALLOC(sizeof(Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 434 if (sha == NULL)
wolfSSL 11:cee25a834751 435 return MEMORY_E;
wolfSSL 11:cee25a834751 436 #endif
wolfSSL 11:cee25a834751 437
wolfSSL 11:cee25a834751 438 if ((ret = wc_InitSha(sha)) != 0) {
wolfSSL 11:cee25a834751 439 WOLFSSL_MSG("wc_InitSha failed");
wolfSSL 11:cee25a834751 440 }
wolfSSL 11:cee25a834751 441 else {
wolfSSL 11:cee25a834751 442 wc_ShaUpdate(sha, data, len);
wolfSSL 11:cee25a834751 443 wc_ShaFinal(sha, hash);
wolfSSL 11:cee25a834751 444 }
wolfSSL 11:cee25a834751 445
wolfSSL 11:cee25a834751 446 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 447 XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 448 #endif
wolfSSL 11:cee25a834751 449
wolfSSL 11:cee25a834751 450 return ret;
wolfSSL 11:cee25a834751 451 }
wolfSSL 11:cee25a834751 452 #endif /* !NO_SHA */
wolfSSL 11:cee25a834751 453
wolfSSL 11:cee25a834751 454 #if defined(WOLFSSL_SHA224)
wolfSSL 11:cee25a834751 455 int wc_Sha224Hash(const byte* data, word32 len, byte* hash)
wolfSSL 11:cee25a834751 456 {
wolfSSL 11:cee25a834751 457 int ret = 0;
wolfSSL 11:cee25a834751 458 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 459 Sha224* sha224;
wolfSSL 11:cee25a834751 460 #else
wolfSSL 11:cee25a834751 461 Sha224 sha224[1];
wolfSSL 11:cee25a834751 462 #endif
wolfSSL 11:cee25a834751 463
wolfSSL 11:cee25a834751 464 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 465 sha224 = (Sha224*)XMALLOC(sizeof(Sha224), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 466 if (sha224 == NULL)
wolfSSL 11:cee25a834751 467 return MEMORY_E;
wolfSSL 11:cee25a834751 468 #endif
wolfSSL 11:cee25a834751 469
wolfSSL 11:cee25a834751 470 if ((ret = wc_InitSha224(sha224)) != 0) {
wolfSSL 11:cee25a834751 471 WOLFSSL_MSG("InitSha224 failed");
wolfSSL 11:cee25a834751 472 }
wolfSSL 11:cee25a834751 473 else if ((ret = wc_Sha224Update(sha224, data, len)) != 0) {
wolfSSL 11:cee25a834751 474 WOLFSSL_MSG("Sha224Update failed");
wolfSSL 11:cee25a834751 475 }
wolfSSL 11:cee25a834751 476 else if ((ret = wc_Sha224Final(sha224, hash)) != 0) {
wolfSSL 11:cee25a834751 477 WOLFSSL_MSG("Sha224Final failed");
wolfSSL 11:cee25a834751 478 }
wolfSSL 11:cee25a834751 479
wolfSSL 11:cee25a834751 480 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 481 XFREE(sha224, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 482 #endif
wolfSSL 11:cee25a834751 483
wolfSSL 11:cee25a834751 484 return ret;
wolfSSL 11:cee25a834751 485 }
wolfSSL 11:cee25a834751 486 #endif /* WOLFSSL_SHA224 */
wolfSSL 11:cee25a834751 487
wolfSSL 11:cee25a834751 488 #if !defined(NO_SHA256)
wolfSSL 11:cee25a834751 489 int wc_Sha256Hash(const byte* data, word32 len, byte* hash)
wolfSSL 11:cee25a834751 490 {
wolfSSL 11:cee25a834751 491 int ret = 0;
wolfSSL 11:cee25a834751 492 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 493 Sha256* sha256;
wolfSSL 11:cee25a834751 494 #else
wolfSSL 11:cee25a834751 495 Sha256 sha256[1];
wolfSSL 11:cee25a834751 496 #endif
wolfSSL 11:cee25a834751 497
wolfSSL 11:cee25a834751 498 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 499 sha256 = (Sha256*)XMALLOC(sizeof(Sha256), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 500 if (sha256 == NULL)
wolfSSL 11:cee25a834751 501 return MEMORY_E;
wolfSSL 11:cee25a834751 502 #endif
wolfSSL 11:cee25a834751 503
wolfSSL 11:cee25a834751 504 if ((ret = wc_InitSha256(sha256)) != 0) {
wolfSSL 11:cee25a834751 505 WOLFSSL_MSG("InitSha256 failed");
wolfSSL 11:cee25a834751 506 }
wolfSSL 11:cee25a834751 507 else if ((ret = wc_Sha256Update(sha256, data, len)) != 0) {
wolfSSL 11:cee25a834751 508 WOLFSSL_MSG("Sha256Update failed");
wolfSSL 11:cee25a834751 509 }
wolfSSL 11:cee25a834751 510 else if ((ret = wc_Sha256Final(sha256, hash)) != 0) {
wolfSSL 11:cee25a834751 511 WOLFSSL_MSG("Sha256Final failed");
wolfSSL 11:cee25a834751 512 }
wolfSSL 11:cee25a834751 513
wolfSSL 11:cee25a834751 514 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 515 XFREE(sha256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 516 #endif
wolfSSL 11:cee25a834751 517
wolfSSL 11:cee25a834751 518 return ret;
wolfSSL 11:cee25a834751 519 }
wolfSSL 11:cee25a834751 520 #endif /* !NO_SHA256 */
wolfSSL 11:cee25a834751 521
wolfSSL 11:cee25a834751 522 #endif /* !defined(WOLFSSL_TI_HASH) */
wolfSSL 11:cee25a834751 523
wolfSSL 11:cee25a834751 524
wolfSSL 11:cee25a834751 525 #if defined(WOLFSSL_SHA512)
wolfSSL 11:cee25a834751 526 int wc_Sha512Hash(const byte* data, word32 len, byte* hash)
wolfSSL 11:cee25a834751 527 {
wolfSSL 11:cee25a834751 528 int ret = 0;
wolfSSL 11:cee25a834751 529 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 530 Sha512* sha512;
wolfSSL 11:cee25a834751 531 #else
wolfSSL 11:cee25a834751 532 Sha512 sha512[1];
wolfSSL 11:cee25a834751 533 #endif
wolfSSL 11:cee25a834751 534
wolfSSL 11:cee25a834751 535 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 536 sha512 = (Sha512*)XMALLOC(sizeof(Sha512), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 537 if (sha512 == NULL)
wolfSSL 11:cee25a834751 538 return MEMORY_E;
wolfSSL 11:cee25a834751 539 #endif
wolfSSL 11:cee25a834751 540
wolfSSL 11:cee25a834751 541 if ((ret = wc_InitSha512(sha512)) != 0) {
wolfSSL 11:cee25a834751 542 WOLFSSL_MSG("InitSha512 failed");
wolfSSL 11:cee25a834751 543 }
wolfSSL 11:cee25a834751 544 else if ((ret = wc_Sha512Update(sha512, data, len)) != 0) {
wolfSSL 11:cee25a834751 545 WOLFSSL_MSG("Sha512Update failed");
wolfSSL 11:cee25a834751 546 }
wolfSSL 11:cee25a834751 547 else if ((ret = wc_Sha512Final(sha512, hash)) != 0) {
wolfSSL 11:cee25a834751 548 WOLFSSL_MSG("Sha512Final failed");
wolfSSL 11:cee25a834751 549 }
wolfSSL 11:cee25a834751 550
wolfSSL 11:cee25a834751 551 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 552 XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 553 #endif
wolfSSL 11:cee25a834751 554
wolfSSL 11:cee25a834751 555 return ret;
wolfSSL 11:cee25a834751 556 }
wolfSSL 11:cee25a834751 557
wolfSSL 11:cee25a834751 558 #if defined(WOLFSSL_SHA384)
wolfSSL 11:cee25a834751 559 int wc_Sha384Hash(const byte* data, word32 len, byte* hash)
wolfSSL 11:cee25a834751 560 {
wolfSSL 11:cee25a834751 561 int ret = 0;
wolfSSL 11:cee25a834751 562 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 563 Sha384* sha384;
wolfSSL 11:cee25a834751 564 #else
wolfSSL 11:cee25a834751 565 Sha384 sha384[1];
wolfSSL 11:cee25a834751 566 #endif
wolfSSL 11:cee25a834751 567
wolfSSL 11:cee25a834751 568 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 569 sha384 = (Sha384*)XMALLOC(sizeof(Sha384), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 570 if (sha384 == NULL)
wolfSSL 11:cee25a834751 571 return MEMORY_E;
wolfSSL 11:cee25a834751 572 #endif
wolfSSL 11:cee25a834751 573
wolfSSL 11:cee25a834751 574 if ((ret = wc_InitSha384(sha384)) != 0) {
wolfSSL 11:cee25a834751 575 WOLFSSL_MSG("InitSha384 failed");
wolfSSL 11:cee25a834751 576 }
wolfSSL 11:cee25a834751 577 else if ((ret = wc_Sha384Update(sha384, data, len)) != 0) {
wolfSSL 11:cee25a834751 578 WOLFSSL_MSG("Sha384Update failed");
wolfSSL 11:cee25a834751 579 }
wolfSSL 11:cee25a834751 580 else if ((ret = wc_Sha384Final(sha384, hash)) != 0) {
wolfSSL 11:cee25a834751 581 WOLFSSL_MSG("Sha384Final failed");
wolfSSL 11:cee25a834751 582 }
wolfSSL 11:cee25a834751 583
wolfSSL 11:cee25a834751 584 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 11:cee25a834751 585 XFREE(sha384, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 11:cee25a834751 586 #endif
wolfSSL 11:cee25a834751 587
wolfSSL 11:cee25a834751 588 return ret;
wolfSSL 11:cee25a834751 589 }
wolfSSL 11:cee25a834751 590 #endif /* WOLFSSL_SHA384 */
wolfSSL 11:cee25a834751 591 #endif /* WOLFSSL_SHA512 */
wolfSSL 11:cee25a834751 592