Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
sPymbed
Date:
Tue Nov 19 14:32:16 2019 +0000
Revision:
16:048e5e270a58
Parent:
15:117db924cf7c
working ssl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* hash.c
wolfSSL 15:117db924cf7c 2 *
wolfSSL 15:117db924cf7c 3 * Copyright (C) 2006-2017 wolfSSL Inc.
wolfSSL 15:117db924cf7c 4 *
wolfSSL 15:117db924cf7c 5 * This file is part of wolfSSL.
wolfSSL 15:117db924cf7c 6 *
wolfSSL 15:117db924cf7c 7 * wolfSSL is free software; you can redistribute it and/or modify
wolfSSL 15:117db924cf7c 8 * it under the terms of the GNU General Public License as published by
wolfSSL 15:117db924cf7c 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 15:117db924cf7c 10 * (at your option) any later version.
wolfSSL 15:117db924cf7c 11 *
wolfSSL 15:117db924cf7c 12 * wolfSSL is distributed in the hope that it will be useful,
wolfSSL 15:117db924cf7c 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 15:117db924cf7c 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 15:117db924cf7c 15 * GNU General Public License for more details.
wolfSSL 15:117db924cf7c 16 *
wolfSSL 15:117db924cf7c 17 * You should have received a copy of the GNU General Public License
wolfSSL 15:117db924cf7c 18 * along with this program; if not, write to the Free Software
wolfSSL 15:117db924cf7c 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
wolfSSL 15:117db924cf7c 20 */
wolfSSL 15:117db924cf7c 21
wolfSSL 15:117db924cf7c 22
wolfSSL 15:117db924cf7c 23 #ifdef HAVE_CONFIG_H
wolfSSL 15:117db924cf7c 24 #include <config.h>
wolfSSL 15:117db924cf7c 25 #endif
wolfSSL 15:117db924cf7c 26
wolfSSL 15:117db924cf7c 27 #include <wolfssl/wolfcrypt/settings.h>
wolfSSL 15:117db924cf7c 28 #include <wolfssl/wolfcrypt/logging.h>
wolfSSL 15:117db924cf7c 29 #include <wolfssl/wolfcrypt/error-crypt.h>
wolfSSL 15:117db924cf7c 30 #ifndef NO_ASN
wolfSSL 15:117db924cf7c 31 #include <wolfssl/wolfcrypt/asn.h>
wolfSSL 15:117db924cf7c 32 #endif
wolfSSL 15:117db924cf7c 33
wolfSSL 15:117db924cf7c 34 #include <wolfssl/wolfcrypt/hash.h>
wolfSSL 15:117db924cf7c 35
wolfSSL 15:117db924cf7c 36
wolfSSL 15:117db924cf7c 37 #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
wolfSSL 15:117db924cf7c 38
wolfSSL 15:117db924cf7c 39 #ifdef NO_ASN
wolfSSL 15:117db924cf7c 40 enum Hash_Sum {
wolfSSL 15:117db924cf7c 41 MD2h = 646,
wolfSSL 15:117db924cf7c 42 MD5h = 649,
wolfSSL 15:117db924cf7c 43 SHAh = 88,
wolfSSL 15:117db924cf7c 44 SHA224h = 417,
wolfSSL 15:117db924cf7c 45 SHA256h = 414,
wolfSSL 15:117db924cf7c 46 SHA384h = 415,
wolfSSL 15:117db924cf7c 47 SHA512h = 416
wolfSSL 15:117db924cf7c 48 };
wolfSSL 15:117db924cf7c 49 #endif /* !NO_ASN */
wolfSSL 15:117db924cf7c 50
wolfSSL 15:117db924cf7c 51 #ifdef HAVE_SELFTEST
wolfSSL 15:117db924cf7c 52 enum {
wolfSSL 15:117db924cf7c 53 /* CAVP selftest includes these in hmac.h instead of sha3.h,
wolfSSL 15:117db924cf7c 54 copied here for that build */
wolfSSL 15:117db924cf7c 55 WC_SHA3_224_BLOCK_SIZE = 144,
wolfSSL 15:117db924cf7c 56 WC_SHA3_256_BLOCK_SIZE = 136,
wolfSSL 15:117db924cf7c 57 WC_SHA3_384_BLOCK_SIZE = 104,
wolfSSL 15:117db924cf7c 58 WC_SHA3_512_BLOCK_SIZE = 72,
wolfSSL 15:117db924cf7c 59 };
wolfSSL 15:117db924cf7c 60 #endif
wolfSSL 15:117db924cf7c 61
wolfSSL 15:117db924cf7c 62
wolfSSL 15:117db924cf7c 63 /* function converts int hash type to enum */
wolfSSL 15:117db924cf7c 64 enum wc_HashType wc_HashTypeConvert(int hashType)
wolfSSL 15:117db924cf7c 65 {
wolfSSL 15:117db924cf7c 66 /* Default to hash type none as error */
wolfSSL 15:117db924cf7c 67 enum wc_HashType eHashType = WC_HASH_TYPE_NONE;
wolfSSL 15:117db924cf7c 68 #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
wolfSSL 15:117db924cf7c 69 /* original FIPSv1 and CAVP selftest require a mapping for unique hash
wolfSSL 15:117db924cf7c 70 type to wc_HashType */
wolfSSL 15:117db924cf7c 71 switch (hashType) {
wolfSSL 15:117db924cf7c 72 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 73 case WC_MD5:
wolfSSL 15:117db924cf7c 74 eHashType = WC_HASH_TYPE_MD5;
wolfSSL 15:117db924cf7c 75 break;
wolfSSL 15:117db924cf7c 76 #endif /* !NO_MD5 */
wolfSSL 15:117db924cf7c 77 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 78 case WC_SHA:
wolfSSL 15:117db924cf7c 79 eHashType = WC_HASH_TYPE_SHA;
wolfSSL 15:117db924cf7c 80 break;
wolfSSL 15:117db924cf7c 81 #endif /* !NO_SHA */
wolfSSL 15:117db924cf7c 82
wolfSSL 15:117db924cf7c 83 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 84 case WC_SHA224:
wolfSSL 15:117db924cf7c 85 eHashType = WC_HASH_TYPE_SHA224;
wolfSSL 15:117db924cf7c 86 break;
wolfSSL 15:117db924cf7c 87 #endif /* WOLFSSL_SHA224 */
wolfSSL 15:117db924cf7c 88
wolfSSL 15:117db924cf7c 89 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 90 case WC_SHA256:
wolfSSL 15:117db924cf7c 91 eHashType = WC_HASH_TYPE_SHA256;
wolfSSL 15:117db924cf7c 92 break;
wolfSSL 15:117db924cf7c 93 #endif /* !NO_SHA256 */
wolfSSL 15:117db924cf7c 94
wolfSSL 15:117db924cf7c 95 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 96 case WC_SHA384:
wolfSSL 15:117db924cf7c 97 eHashType = WC_HASH_TYPE_SHA384;
wolfSSL 15:117db924cf7c 98 break;
wolfSSL 15:117db924cf7c 99 #endif /* WOLFSSL_SHA384 */
wolfSSL 15:117db924cf7c 100 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 101 case WC_SHA512:
wolfSSL 15:117db924cf7c 102 eHashType = WC_HASH_TYPE_SHA512;
wolfSSL 15:117db924cf7c 103 break;
wolfSSL 15:117db924cf7c 104 #endif /* WOLFSSL_SHA512 */
wolfSSL 15:117db924cf7c 105 default:
wolfSSL 15:117db924cf7c 106 eHashType = WC_HASH_TYPE_NONE;
wolfSSL 15:117db924cf7c 107 break;
wolfSSL 15:117db924cf7c 108 }
wolfSSL 15:117db924cf7c 109 #else
wolfSSL 15:117db924cf7c 110 /* current master uses same unique types as wc_HashType */
wolfSSL 15:117db924cf7c 111 if (hashType > 0 && hashType <= WC_HASH_TYPE_MAX) {
wolfSSL 15:117db924cf7c 112 eHashType = (enum wc_HashType)hashType;
wolfSSL 15:117db924cf7c 113 }
wolfSSL 15:117db924cf7c 114 #endif
wolfSSL 15:117db924cf7c 115 return eHashType;
wolfSSL 15:117db924cf7c 116 }
wolfSSL 15:117db924cf7c 117
wolfSSL 15:117db924cf7c 118
wolfSSL 15:117db924cf7c 119 int wc_HashGetOID(enum wc_HashType hash_type)
wolfSSL 15:117db924cf7c 120 {
wolfSSL 15:117db924cf7c 121 int oid = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 122 switch(hash_type)
wolfSSL 15:117db924cf7c 123 {
wolfSSL 15:117db924cf7c 124 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 125 #ifdef WOLFSSL_MD2
wolfSSL 15:117db924cf7c 126 oid = MD2h;
wolfSSL 15:117db924cf7c 127 #endif
wolfSSL 15:117db924cf7c 128 break;
wolfSSL 15:117db924cf7c 129 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 15:117db924cf7c 130 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 131 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 132 oid = MD5h;
wolfSSL 15:117db924cf7c 133 #endif
wolfSSL 15:117db924cf7c 134 break;
wolfSSL 15:117db924cf7c 135 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 136 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 137 oid = SHAh;
wolfSSL 15:117db924cf7c 138 #endif
wolfSSL 15:117db924cf7c 139 break;
wolfSSL 15:117db924cf7c 140 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 141 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 142 oid = SHA224h;
wolfSSL 15:117db924cf7c 143 #endif
wolfSSL 15:117db924cf7c 144 break;
wolfSSL 15:117db924cf7c 145 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 146 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 147 oid = SHA256h;
wolfSSL 15:117db924cf7c 148 #endif
wolfSSL 15:117db924cf7c 149 break;
wolfSSL 15:117db924cf7c 150 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 151 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 152 oid = SHA384h;
wolfSSL 15:117db924cf7c 153 #endif
wolfSSL 15:117db924cf7c 154 break;
wolfSSL 15:117db924cf7c 155 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 156 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 157 oid = SHA512h;
wolfSSL 15:117db924cf7c 158 #endif
wolfSSL 15:117db924cf7c 159 break;
wolfSSL 15:117db924cf7c 160
wolfSSL 15:117db924cf7c 161 /* Not Supported */
wolfSSL 15:117db924cf7c 162 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 163 case WC_HASH_TYPE_SHA3_224:
wolfSSL 15:117db924cf7c 164 case WC_HASH_TYPE_SHA3_256:
wolfSSL 15:117db924cf7c 165 case WC_HASH_TYPE_SHA3_384:
wolfSSL 15:117db924cf7c 166 case WC_HASH_TYPE_SHA3_512:
wolfSSL 15:117db924cf7c 167 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 15:117db924cf7c 168 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 169 default:
wolfSSL 15:117db924cf7c 170 oid = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 171 break;
wolfSSL 15:117db924cf7c 172 }
wolfSSL 15:117db924cf7c 173 return oid;
wolfSSL 15:117db924cf7c 174 }
wolfSSL 15:117db924cf7c 175
wolfSSL 15:117db924cf7c 176 enum wc_HashType wc_OidGetHash(int oid)
wolfSSL 15:117db924cf7c 177 {
wolfSSL 15:117db924cf7c 178 enum wc_HashType hash_type = WC_HASH_TYPE_NONE;
wolfSSL 15:117db924cf7c 179 switch (oid)
wolfSSL 15:117db924cf7c 180 {
wolfSSL 15:117db924cf7c 181 case MD2h:
wolfSSL 15:117db924cf7c 182 #ifdef WOLFSSL_MD2
wolfSSL 15:117db924cf7c 183 hash_type = WC_HASH_TYPE_MD2;
wolfSSL 15:117db924cf7c 184 #endif
wolfSSL 15:117db924cf7c 185 break;
wolfSSL 15:117db924cf7c 186 case MD5h:
wolfSSL 15:117db924cf7c 187 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 188 hash_type = WC_HASH_TYPE_MD5;
wolfSSL 15:117db924cf7c 189 #endif
wolfSSL 15:117db924cf7c 190 break;
wolfSSL 15:117db924cf7c 191 case SHAh:
wolfSSL 15:117db924cf7c 192 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 193 hash_type = WC_HASH_TYPE_SHA;
wolfSSL 15:117db924cf7c 194 #endif
wolfSSL 15:117db924cf7c 195 break;
wolfSSL 15:117db924cf7c 196 case SHA224h:
wolfSSL 15:117db924cf7c 197 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 198 hash_type = WC_HASH_TYPE_SHA224;
wolfSSL 15:117db924cf7c 199 #endif
wolfSSL 15:117db924cf7c 200 break;
wolfSSL 15:117db924cf7c 201 case SHA256h:
wolfSSL 15:117db924cf7c 202 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 203 hash_type = WC_HASH_TYPE_SHA256;
wolfSSL 15:117db924cf7c 204 #endif
wolfSSL 15:117db924cf7c 205 break;
wolfSSL 15:117db924cf7c 206 case SHA384h:
wolfSSL 15:117db924cf7c 207 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 208 hash_type = WC_HASH_TYPE_SHA384;
wolfSSL 15:117db924cf7c 209 #endif
wolfSSL 15:117db924cf7c 210 break;
wolfSSL 15:117db924cf7c 211 case SHA512h:
wolfSSL 15:117db924cf7c 212 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 213 hash_type = WC_HASH_TYPE_SHA512;
wolfSSL 15:117db924cf7c 214 #endif
wolfSSL 15:117db924cf7c 215 break;
wolfSSL 15:117db924cf7c 216 default:
wolfSSL 15:117db924cf7c 217 break;
wolfSSL 15:117db924cf7c 218 }
wolfSSL 15:117db924cf7c 219 return hash_type;
wolfSSL 15:117db924cf7c 220 }
wolfSSL 15:117db924cf7c 221 #endif /* !NO_ASN || !NO_DH || HAVE_ECC */
wolfSSL 15:117db924cf7c 222
wolfSSL 15:117db924cf7c 223
wolfSSL 15:117db924cf7c 224
wolfSSL 15:117db924cf7c 225 /* Get Hash digest size */
wolfSSL 15:117db924cf7c 226 int wc_HashGetDigestSize(enum wc_HashType hash_type)
wolfSSL 15:117db924cf7c 227 {
wolfSSL 15:117db924cf7c 228 int dig_size = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 229 switch(hash_type)
wolfSSL 15:117db924cf7c 230 {
wolfSSL 15:117db924cf7c 231 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 232 #ifdef WOLFSSL_MD2
wolfSSL 15:117db924cf7c 233 dig_size = MD2_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 234 #endif
wolfSSL 15:117db924cf7c 235 break;
wolfSSL 15:117db924cf7c 236 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 237 #ifndef NO_MD4
wolfSSL 15:117db924cf7c 238 dig_size = MD4_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 239 #endif
wolfSSL 15:117db924cf7c 240 break;
wolfSSL 15:117db924cf7c 241 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 242 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 243 dig_size = WC_MD5_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 244 #endif
wolfSSL 15:117db924cf7c 245 break;
wolfSSL 15:117db924cf7c 246 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 247 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 248 dig_size = WC_SHA_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 249 #endif
wolfSSL 15:117db924cf7c 250 break;
wolfSSL 15:117db924cf7c 251 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 252 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 253 dig_size = WC_SHA224_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 254 #endif
wolfSSL 15:117db924cf7c 255 break;
wolfSSL 15:117db924cf7c 256 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 257 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 258 dig_size = WC_SHA256_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 259 #endif
wolfSSL 15:117db924cf7c 260 break;
wolfSSL 15:117db924cf7c 261 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 262 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 263 dig_size = WC_SHA384_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 264 #endif
wolfSSL 15:117db924cf7c 265 break;
wolfSSL 15:117db924cf7c 266 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 267 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 268 dig_size = WC_SHA512_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 269 #endif
wolfSSL 15:117db924cf7c 270 break;
wolfSSL 15:117db924cf7c 271 case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */
wolfSSL 15:117db924cf7c 272 #if !defined(NO_MD5) && !defined(NO_SHA)
wolfSSL 15:117db924cf7c 273 dig_size = (int)WC_MD5_DIGEST_SIZE + (int)WC_SHA_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 274 #endif
wolfSSL 15:117db924cf7c 275 break;
wolfSSL 15:117db924cf7c 276
wolfSSL 15:117db924cf7c 277 case WC_HASH_TYPE_SHA3_224:
wolfSSL 15:117db924cf7c 278 #ifdef WOLFSSL_SHA3
wolfSSL 15:117db924cf7c 279 dig_size = WC_SHA3_224_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 280 #endif
wolfSSL 15:117db924cf7c 281 break;
wolfSSL 15:117db924cf7c 282 case WC_HASH_TYPE_SHA3_256:
wolfSSL 15:117db924cf7c 283 #ifdef WOLFSSL_SHA3
wolfSSL 15:117db924cf7c 284 dig_size = WC_SHA3_256_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 285 #endif
wolfSSL 15:117db924cf7c 286 break;
wolfSSL 15:117db924cf7c 287 case WC_HASH_TYPE_SHA3_384:
wolfSSL 15:117db924cf7c 288 #ifdef WOLFSSL_SHA3
wolfSSL 15:117db924cf7c 289 dig_size = WC_SHA3_384_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 290 #endif
wolfSSL 15:117db924cf7c 291 break;
wolfSSL 15:117db924cf7c 292 case WC_HASH_TYPE_SHA3_512:
wolfSSL 15:117db924cf7c 293 #ifdef WOLFSSL_SHA3
wolfSSL 15:117db924cf7c 294 dig_size = WC_SHA3_512_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 295 #endif
wolfSSL 15:117db924cf7c 296 break;
wolfSSL 15:117db924cf7c 297
wolfSSL 15:117db924cf7c 298 /* Not Supported */
wolfSSL 15:117db924cf7c 299 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 15:117db924cf7c 300 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 301 default:
wolfSSL 15:117db924cf7c 302 dig_size = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 303 break;
wolfSSL 15:117db924cf7c 304 }
wolfSSL 15:117db924cf7c 305 return dig_size;
wolfSSL 15:117db924cf7c 306 }
wolfSSL 15:117db924cf7c 307
wolfSSL 15:117db924cf7c 308
wolfSSL 15:117db924cf7c 309 /* Get Hash block size */
wolfSSL 15:117db924cf7c 310 int wc_HashGetBlockSize(enum wc_HashType hash_type)
wolfSSL 15:117db924cf7c 311 {
wolfSSL 15:117db924cf7c 312 int block_size = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 313 switch (hash_type)
wolfSSL 15:117db924cf7c 314 {
wolfSSL 15:117db924cf7c 315 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 316 #ifdef WOLFSSL_MD2
wolfSSL 15:117db924cf7c 317 block_size = MD2_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 318 #endif
wolfSSL 15:117db924cf7c 319 break;
wolfSSL 15:117db924cf7c 320 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 321 #ifndef NO_MD4
wolfSSL 15:117db924cf7c 322 block_size = MD4_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 323 #endif
wolfSSL 15:117db924cf7c 324 break;
wolfSSL 15:117db924cf7c 325 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 326 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 327 block_size = WC_MD5_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 328 #endif
wolfSSL 15:117db924cf7c 329 break;
wolfSSL 15:117db924cf7c 330 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 331 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 332 block_size = WC_SHA_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 333 #endif
wolfSSL 15:117db924cf7c 334 break;
wolfSSL 15:117db924cf7c 335 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 336 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 337 block_size = WC_SHA224_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 338 #endif
wolfSSL 15:117db924cf7c 339 break;
wolfSSL 15:117db924cf7c 340 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 341 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 342 block_size = WC_SHA256_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 343 #endif
wolfSSL 15:117db924cf7c 344 break;
wolfSSL 15:117db924cf7c 345 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 346 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 347 block_size = WC_SHA384_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 348 #endif
wolfSSL 15:117db924cf7c 349 break;
wolfSSL 15:117db924cf7c 350 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 351 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 352 block_size = WC_SHA512_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 353 #endif
wolfSSL 15:117db924cf7c 354 break;
wolfSSL 15:117db924cf7c 355 case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */
wolfSSL 15:117db924cf7c 356 #if !defined(NO_MD5) && !defined(NO_SHA)
wolfSSL 15:117db924cf7c 357 block_size = (int)WC_MD5_BLOCK_SIZE + (int)WC_SHA_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 358 #endif
wolfSSL 15:117db924cf7c 359 break;
wolfSSL 15:117db924cf7c 360
wolfSSL 15:117db924cf7c 361 case WC_HASH_TYPE_SHA3_224:
wolfSSL 15:117db924cf7c 362 #ifdef WOLFSSL_SHA3
wolfSSL 15:117db924cf7c 363 block_size = WC_SHA3_224_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 364 #endif
wolfSSL 15:117db924cf7c 365 break;
wolfSSL 15:117db924cf7c 366 case WC_HASH_TYPE_SHA3_256:
wolfSSL 15:117db924cf7c 367 #ifdef WOLFSSL_SHA3
wolfSSL 15:117db924cf7c 368 block_size = WC_SHA3_256_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 369 #endif
wolfSSL 15:117db924cf7c 370 break;
wolfSSL 15:117db924cf7c 371 case WC_HASH_TYPE_SHA3_384:
wolfSSL 15:117db924cf7c 372 #ifdef WOLFSSL_SHA3
wolfSSL 15:117db924cf7c 373 block_size = WC_SHA3_384_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 374 #endif
wolfSSL 15:117db924cf7c 375 break;
wolfSSL 15:117db924cf7c 376 case WC_HASH_TYPE_SHA3_512:
wolfSSL 15:117db924cf7c 377 #ifdef WOLFSSL_SHA3
wolfSSL 15:117db924cf7c 378 block_size = WC_SHA3_512_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 379 #endif
wolfSSL 15:117db924cf7c 380 break;
wolfSSL 15:117db924cf7c 381
wolfSSL 15:117db924cf7c 382 /* Not Supported */
wolfSSL 15:117db924cf7c 383 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 15:117db924cf7c 384 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 385 default:
wolfSSL 15:117db924cf7c 386 block_size = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 387 break;
wolfSSL 15:117db924cf7c 388 }
wolfSSL 15:117db924cf7c 389 return block_size;
wolfSSL 15:117db924cf7c 390 }
wolfSSL 15:117db924cf7c 391
wolfSSL 15:117db924cf7c 392 /* Generic Hashing Wrapper */
wolfSSL 15:117db924cf7c 393 int wc_Hash(enum wc_HashType hash_type, const byte* data,
wolfSSL 15:117db924cf7c 394 word32 data_len, byte* hash, word32 hash_len)
wolfSSL 15:117db924cf7c 395 {
wolfSSL 15:117db924cf7c 396 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 397 word32 dig_size;
wolfSSL 15:117db924cf7c 398
wolfSSL 15:117db924cf7c 399 /* Validate hash buffer size */
wolfSSL 15:117db924cf7c 400 dig_size = wc_HashGetDigestSize(hash_type);
wolfSSL 15:117db924cf7c 401 if (hash_len < dig_size) {
wolfSSL 15:117db924cf7c 402 return BUFFER_E;
wolfSSL 15:117db924cf7c 403 }
wolfSSL 15:117db924cf7c 404
wolfSSL 15:117db924cf7c 405 /* Suppress possible unused arg if all hashing is disabled */
wolfSSL 15:117db924cf7c 406 (void)data;
wolfSSL 15:117db924cf7c 407 (void)data_len;
wolfSSL 15:117db924cf7c 408 (void)hash;
wolfSSL 15:117db924cf7c 409 (void)hash_len;
wolfSSL 15:117db924cf7c 410
wolfSSL 15:117db924cf7c 411 switch(hash_type)
wolfSSL 15:117db924cf7c 412 {
wolfSSL 15:117db924cf7c 413 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 414 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 415 ret = wc_Md5Hash(data, data_len, hash);
wolfSSL 15:117db924cf7c 416 #endif
wolfSSL 15:117db924cf7c 417 break;
wolfSSL 15:117db924cf7c 418 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 419 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 420 ret = wc_ShaHash(data, data_len, hash);
wolfSSL 15:117db924cf7c 421 #endif
wolfSSL 15:117db924cf7c 422 break;
wolfSSL 15:117db924cf7c 423 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 424 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 425 ret = wc_Sha224Hash(data, data_len, hash);
wolfSSL 15:117db924cf7c 426 #endif
wolfSSL 15:117db924cf7c 427 break;
wolfSSL 15:117db924cf7c 428 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 429 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 430 ret = wc_Sha256Hash(data, data_len, hash);
wolfSSL 15:117db924cf7c 431 #endif
wolfSSL 15:117db924cf7c 432 break;
wolfSSL 15:117db924cf7c 433 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 434 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 435 ret = wc_Sha384Hash(data, data_len, hash);
wolfSSL 15:117db924cf7c 436 #endif
wolfSSL 15:117db924cf7c 437 break;
wolfSSL 15:117db924cf7c 438 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 439 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 440 ret = wc_Sha512Hash(data, data_len, hash);
wolfSSL 15:117db924cf7c 441 #endif
wolfSSL 15:117db924cf7c 442 break;
wolfSSL 15:117db924cf7c 443 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 15:117db924cf7c 444 #if !defined(NO_MD5) && !defined(NO_SHA)
wolfSSL 15:117db924cf7c 445 ret = wc_Md5Hash(data, data_len, hash);
wolfSSL 15:117db924cf7c 446 if (ret == 0) {
wolfSSL 15:117db924cf7c 447 ret = wc_ShaHash(data, data_len, &hash[WC_MD5_DIGEST_SIZE]);
wolfSSL 15:117db924cf7c 448 }
wolfSSL 15:117db924cf7c 449 #endif
wolfSSL 15:117db924cf7c 450 break;
wolfSSL 15:117db924cf7c 451
wolfSSL 15:117db924cf7c 452 /* Not Supported */
wolfSSL 15:117db924cf7c 453 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 454 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 455 case WC_HASH_TYPE_SHA3_224:
wolfSSL 15:117db924cf7c 456 case WC_HASH_TYPE_SHA3_256:
wolfSSL 15:117db924cf7c 457 case WC_HASH_TYPE_SHA3_384:
wolfSSL 15:117db924cf7c 458 case WC_HASH_TYPE_SHA3_512:
wolfSSL 15:117db924cf7c 459 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 15:117db924cf7c 460 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 461 default:
wolfSSL 15:117db924cf7c 462 ret = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 463 break;
wolfSSL 15:117db924cf7c 464 }
wolfSSL 15:117db924cf7c 465 return ret;
wolfSSL 15:117db924cf7c 466 }
wolfSSL 15:117db924cf7c 467
wolfSSL 15:117db924cf7c 468 int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type)
wolfSSL 15:117db924cf7c 469 {
wolfSSL 15:117db924cf7c 470 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 471
wolfSSL 15:117db924cf7c 472 if (hash == NULL)
wolfSSL 15:117db924cf7c 473 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 474
wolfSSL 15:117db924cf7c 475 switch (type) {
wolfSSL 15:117db924cf7c 476 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 477 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 478 wc_InitMd5(&hash->md5);
wolfSSL 15:117db924cf7c 479 ret = 0;
wolfSSL 15:117db924cf7c 480 #endif
wolfSSL 15:117db924cf7c 481 break;
wolfSSL 15:117db924cf7c 482 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 483 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 484 ret = wc_InitSha(&hash->sha);
wolfSSL 15:117db924cf7c 485 #endif
wolfSSL 15:117db924cf7c 486 break;
wolfSSL 15:117db924cf7c 487 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 488 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 489 ret = wc_InitSha224(&hash->sha224);
wolfSSL 15:117db924cf7c 490 #endif
wolfSSL 15:117db924cf7c 491 break;
wolfSSL 15:117db924cf7c 492 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 493 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 494 ret = wc_InitSha256(&hash->sha256);
wolfSSL 15:117db924cf7c 495 #endif
wolfSSL 15:117db924cf7c 496 break;
wolfSSL 15:117db924cf7c 497 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 498 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 499 ret = wc_InitSha384(&hash->sha384);
wolfSSL 15:117db924cf7c 500 #endif
wolfSSL 15:117db924cf7c 501 break;
wolfSSL 15:117db924cf7c 502 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 503 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 504 ret = wc_InitSha512(&hash->sha512);
wolfSSL 15:117db924cf7c 505 #endif
wolfSSL 15:117db924cf7c 506 break;
wolfSSL 15:117db924cf7c 507
wolfSSL 15:117db924cf7c 508 /* not supported */
wolfSSL 15:117db924cf7c 509 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 15:117db924cf7c 510 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 511 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 512 case WC_HASH_TYPE_SHA3_224:
wolfSSL 15:117db924cf7c 513 case WC_HASH_TYPE_SHA3_256:
wolfSSL 15:117db924cf7c 514 case WC_HASH_TYPE_SHA3_384:
wolfSSL 15:117db924cf7c 515 case WC_HASH_TYPE_SHA3_512:
wolfSSL 15:117db924cf7c 516 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 15:117db924cf7c 517 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 518 default:
wolfSSL 15:117db924cf7c 519 ret = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 520 };
wolfSSL 15:117db924cf7c 521
wolfSSL 15:117db924cf7c 522 return ret;
wolfSSL 15:117db924cf7c 523 }
wolfSSL 15:117db924cf7c 524
wolfSSL 15:117db924cf7c 525 int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, const byte* data,
wolfSSL 15:117db924cf7c 526 word32 dataSz)
wolfSSL 15:117db924cf7c 527 {
wolfSSL 15:117db924cf7c 528 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 529
wolfSSL 15:117db924cf7c 530 if (hash == NULL || data == NULL)
wolfSSL 15:117db924cf7c 531 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 532
wolfSSL 15:117db924cf7c 533 switch (type) {
wolfSSL 15:117db924cf7c 534 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 535 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 536 wc_Md5Update(&hash->md5, data, dataSz);
wolfSSL 15:117db924cf7c 537 ret = 0;
wolfSSL 15:117db924cf7c 538 #endif
wolfSSL 15:117db924cf7c 539 break;
wolfSSL 15:117db924cf7c 540 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 541 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 542 ret = wc_ShaUpdate(&hash->sha, data, dataSz);
wolfSSL 15:117db924cf7c 543 if (ret != 0)
wolfSSL 15:117db924cf7c 544 return ret;
wolfSSL 15:117db924cf7c 545 #endif
wolfSSL 15:117db924cf7c 546 break;
wolfSSL 15:117db924cf7c 547 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 548 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 549 ret = wc_Sha224Update(&hash->sha224, data, dataSz);
wolfSSL 15:117db924cf7c 550 #endif
wolfSSL 15:117db924cf7c 551 break;
wolfSSL 15:117db924cf7c 552 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 553 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 554 ret = wc_Sha256Update(&hash->sha256, data, dataSz);
wolfSSL 15:117db924cf7c 555 #endif
wolfSSL 15:117db924cf7c 556 break;
wolfSSL 15:117db924cf7c 557 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 558 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 559 ret = wc_Sha384Update(&hash->sha384, data, dataSz);
wolfSSL 15:117db924cf7c 560 #endif
wolfSSL 15:117db924cf7c 561 break;
wolfSSL 15:117db924cf7c 562 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 563 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 564 ret = wc_Sha512Update(&hash->sha512, data, dataSz);
wolfSSL 15:117db924cf7c 565 #endif
wolfSSL 15:117db924cf7c 566 break;
wolfSSL 15:117db924cf7c 567
wolfSSL 15:117db924cf7c 568 /* not supported */
wolfSSL 15:117db924cf7c 569 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 15:117db924cf7c 570 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 571 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 572 case WC_HASH_TYPE_SHA3_224:
wolfSSL 15:117db924cf7c 573 case WC_HASH_TYPE_SHA3_256:
wolfSSL 15:117db924cf7c 574 case WC_HASH_TYPE_SHA3_384:
wolfSSL 15:117db924cf7c 575 case WC_HASH_TYPE_SHA3_512:
wolfSSL 15:117db924cf7c 576 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 15:117db924cf7c 577 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 578 default:
wolfSSL 15:117db924cf7c 579 ret = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 580 };
wolfSSL 15:117db924cf7c 581
wolfSSL 15:117db924cf7c 582 return ret;
wolfSSL 15:117db924cf7c 583 }
wolfSSL 15:117db924cf7c 584
wolfSSL 15:117db924cf7c 585 int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out)
wolfSSL 15:117db924cf7c 586 {
wolfSSL 15:117db924cf7c 587 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 588
wolfSSL 15:117db924cf7c 589 if (hash == NULL || out == NULL)
wolfSSL 15:117db924cf7c 590 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 591
wolfSSL 15:117db924cf7c 592 switch (type) {
wolfSSL 15:117db924cf7c 593 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 594 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 595 wc_Md5Final(&hash->md5, out);
wolfSSL 15:117db924cf7c 596 ret = 0;
wolfSSL 15:117db924cf7c 597 #endif
wolfSSL 15:117db924cf7c 598 break;
wolfSSL 15:117db924cf7c 599 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 600 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 601 ret = wc_ShaFinal(&hash->sha, out);
wolfSSL 15:117db924cf7c 602 #endif
wolfSSL 15:117db924cf7c 603 break;
wolfSSL 15:117db924cf7c 604 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 605 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 606 ret = wc_Sha224Final(&hash->sha224, out);
wolfSSL 15:117db924cf7c 607 #endif
wolfSSL 15:117db924cf7c 608 break;
wolfSSL 15:117db924cf7c 609 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 610 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 611 ret = wc_Sha256Final(&hash->sha256, out);
wolfSSL 15:117db924cf7c 612 #endif
wolfSSL 15:117db924cf7c 613 break;
wolfSSL 15:117db924cf7c 614 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 615 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 616 ret = wc_Sha384Final(&hash->sha384, out);
wolfSSL 15:117db924cf7c 617 #endif
wolfSSL 15:117db924cf7c 618 break;
wolfSSL 15:117db924cf7c 619 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 620 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 621 ret = wc_Sha512Final(&hash->sha512, out);
wolfSSL 15:117db924cf7c 622 #endif
wolfSSL 15:117db924cf7c 623 break;
wolfSSL 15:117db924cf7c 624
wolfSSL 15:117db924cf7c 625 /* not supported */
wolfSSL 15:117db924cf7c 626 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 15:117db924cf7c 627 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 628 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 629 case WC_HASH_TYPE_SHA3_224:
wolfSSL 15:117db924cf7c 630 case WC_HASH_TYPE_SHA3_256:
wolfSSL 15:117db924cf7c 631 case WC_HASH_TYPE_SHA3_384:
wolfSSL 15:117db924cf7c 632 case WC_HASH_TYPE_SHA3_512:
wolfSSL 15:117db924cf7c 633 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 15:117db924cf7c 634 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 635 default:
wolfSSL 15:117db924cf7c 636 ret = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 637 };
wolfSSL 15:117db924cf7c 638
wolfSSL 15:117db924cf7c 639 return ret;
wolfSSL 15:117db924cf7c 640 }
wolfSSL 15:117db924cf7c 641
wolfSSL 15:117db924cf7c 642
wolfSSL 15:117db924cf7c 643 #if !defined(WOLFSSL_TI_HASH)
wolfSSL 15:117db924cf7c 644
wolfSSL 15:117db924cf7c 645 #if !defined(NO_MD5)
wolfSSL 15:117db924cf7c 646 int wc_Md5Hash(const byte* data, word32 len, byte* hash)
wolfSSL 15:117db924cf7c 647 {
wolfSSL 15:117db924cf7c 648 int ret;
wolfSSL 15:117db924cf7c 649 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 650 wc_Md5* md5;
wolfSSL 15:117db924cf7c 651 #else
wolfSSL 15:117db924cf7c 652 wc_Md5 md5[1];
wolfSSL 15:117db924cf7c 653 #endif
wolfSSL 15:117db924cf7c 654
wolfSSL 15:117db924cf7c 655 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 656 md5 = (wc_Md5*)XMALLOC(sizeof(wc_Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 657 if (md5 == NULL)
wolfSSL 15:117db924cf7c 658 return MEMORY_E;
wolfSSL 15:117db924cf7c 659 #endif
wolfSSL 15:117db924cf7c 660
wolfSSL 15:117db924cf7c 661 ret = wc_InitMd5(md5);
wolfSSL 15:117db924cf7c 662 if (ret == 0) {
wolfSSL 15:117db924cf7c 663 ret = wc_Md5Update(md5, data, len);
wolfSSL 15:117db924cf7c 664 if (ret == 0) {
wolfSSL 15:117db924cf7c 665 ret = wc_Md5Final(md5, hash);
wolfSSL 15:117db924cf7c 666 }
wolfSSL 15:117db924cf7c 667 }
wolfSSL 15:117db924cf7c 668
wolfSSL 15:117db924cf7c 669 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 670 XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 671 #endif
wolfSSL 15:117db924cf7c 672
wolfSSL 15:117db924cf7c 673 return ret;
wolfSSL 15:117db924cf7c 674 }
wolfSSL 15:117db924cf7c 675 #endif /* !NO_MD5 */
wolfSSL 15:117db924cf7c 676
wolfSSL 15:117db924cf7c 677 #if !defined(NO_SHA)
wolfSSL 15:117db924cf7c 678 int wc_ShaHash(const byte* data, word32 len, byte* hash)
wolfSSL 15:117db924cf7c 679 {
wolfSSL 15:117db924cf7c 680 int ret = 0;
wolfSSL 15:117db924cf7c 681 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 682 wc_Sha* sha;
wolfSSL 15:117db924cf7c 683 #else
wolfSSL 15:117db924cf7c 684 wc_Sha sha[1];
wolfSSL 15:117db924cf7c 685 #endif
wolfSSL 15:117db924cf7c 686
wolfSSL 15:117db924cf7c 687 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 688 sha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 689 if (sha == NULL)
wolfSSL 15:117db924cf7c 690 return MEMORY_E;
wolfSSL 15:117db924cf7c 691 #endif
wolfSSL 15:117db924cf7c 692
wolfSSL 15:117db924cf7c 693 if ((ret = wc_InitSha(sha)) != 0) {
wolfSSL 15:117db924cf7c 694 WOLFSSL_MSG("wc_InitSha failed");
wolfSSL 15:117db924cf7c 695 }
wolfSSL 15:117db924cf7c 696 else {
wolfSSL 15:117db924cf7c 697 wc_ShaUpdate(sha, data, len);
wolfSSL 15:117db924cf7c 698 wc_ShaFinal(sha, hash);
wolfSSL 15:117db924cf7c 699 }
wolfSSL 15:117db924cf7c 700
wolfSSL 15:117db924cf7c 701 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 702 XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 703 #endif
wolfSSL 15:117db924cf7c 704
wolfSSL 15:117db924cf7c 705 return ret;
wolfSSL 15:117db924cf7c 706 }
wolfSSL 15:117db924cf7c 707 #endif /* !NO_SHA */
wolfSSL 15:117db924cf7c 708
wolfSSL 15:117db924cf7c 709 #if defined(WOLFSSL_SHA224)
wolfSSL 15:117db924cf7c 710 int wc_Sha224Hash(const byte* data, word32 len, byte* hash)
wolfSSL 15:117db924cf7c 711 {
wolfSSL 15:117db924cf7c 712 int ret = 0;
wolfSSL 15:117db924cf7c 713 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 714 wc_Sha224* sha224;
wolfSSL 15:117db924cf7c 715 #else
wolfSSL 15:117db924cf7c 716 wc_Sha224 sha224[1];
wolfSSL 15:117db924cf7c 717 #endif
wolfSSL 15:117db924cf7c 718
wolfSSL 15:117db924cf7c 719 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 720 sha224 = (wc_Sha224*)XMALLOC(sizeof(wc_Sha224), NULL,
wolfSSL 15:117db924cf7c 721 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 722 if (sha224 == NULL)
wolfSSL 15:117db924cf7c 723 return MEMORY_E;
wolfSSL 15:117db924cf7c 724 #endif
wolfSSL 15:117db924cf7c 725
wolfSSL 15:117db924cf7c 726 if ((ret = wc_InitSha224(sha224)) != 0) {
wolfSSL 15:117db924cf7c 727 WOLFSSL_MSG("InitSha224 failed");
wolfSSL 15:117db924cf7c 728 }
wolfSSL 15:117db924cf7c 729 else {
wolfSSL 15:117db924cf7c 730 if ((ret = wc_Sha224Update(sha224, data, len)) != 0) {
wolfSSL 15:117db924cf7c 731 WOLFSSL_MSG("Sha224Update failed");
wolfSSL 15:117db924cf7c 732 }
wolfSSL 15:117db924cf7c 733 else if ((ret = wc_Sha224Final(sha224, hash)) != 0) {
wolfSSL 15:117db924cf7c 734 WOLFSSL_MSG("Sha224Final failed");
wolfSSL 15:117db924cf7c 735 }
wolfSSL 15:117db924cf7c 736 wc_Sha224Free(sha224);
wolfSSL 15:117db924cf7c 737 }
wolfSSL 15:117db924cf7c 738
wolfSSL 15:117db924cf7c 739 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 740 XFREE(sha224, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 741 #endif
wolfSSL 15:117db924cf7c 742
wolfSSL 15:117db924cf7c 743 return ret;
wolfSSL 15:117db924cf7c 744 }
wolfSSL 15:117db924cf7c 745 #endif /* WOLFSSL_SHA224 */
wolfSSL 15:117db924cf7c 746
wolfSSL 15:117db924cf7c 747 #if !defined(NO_SHA256)
wolfSSL 15:117db924cf7c 748 int wc_Sha256Hash(const byte* data, word32 len, byte* hash)
wolfSSL 15:117db924cf7c 749 {
wolfSSL 15:117db924cf7c 750 int ret = 0;
wolfSSL 15:117db924cf7c 751 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 752 wc_Sha256* sha256;
wolfSSL 15:117db924cf7c 753 #else
wolfSSL 15:117db924cf7c 754 wc_Sha256 sha256[1];
wolfSSL 15:117db924cf7c 755 #endif
wolfSSL 15:117db924cf7c 756
wolfSSL 15:117db924cf7c 757 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 758 sha256 = (wc_Sha256*)XMALLOC(sizeof(wc_Sha256), NULL,
wolfSSL 15:117db924cf7c 759 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 760 if (sha256 == NULL)
wolfSSL 15:117db924cf7c 761 return MEMORY_E;
wolfSSL 15:117db924cf7c 762 #endif
wolfSSL 15:117db924cf7c 763
wolfSSL 15:117db924cf7c 764 if ((ret = wc_InitSha256(sha256)) != 0) {
wolfSSL 15:117db924cf7c 765 WOLFSSL_MSG("InitSha256 failed");
wolfSSL 15:117db924cf7c 766 }
wolfSSL 15:117db924cf7c 767 else {
wolfSSL 15:117db924cf7c 768 if ((ret = wc_Sha256Update(sha256, data, len)) != 0) {
wolfSSL 15:117db924cf7c 769 WOLFSSL_MSG("Sha256Update failed");
wolfSSL 15:117db924cf7c 770 }
wolfSSL 15:117db924cf7c 771 else if ((ret = wc_Sha256Final(sha256, hash)) != 0) {
wolfSSL 15:117db924cf7c 772 WOLFSSL_MSG("Sha256Final failed");
wolfSSL 15:117db924cf7c 773 }
wolfSSL 15:117db924cf7c 774 wc_Sha256Free(sha256);
wolfSSL 15:117db924cf7c 775 }
wolfSSL 15:117db924cf7c 776
wolfSSL 15:117db924cf7c 777
wolfSSL 15:117db924cf7c 778 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 779 XFREE(sha256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 780 #endif
wolfSSL 15:117db924cf7c 781
wolfSSL 15:117db924cf7c 782 return ret;
wolfSSL 15:117db924cf7c 783 }
wolfSSL 15:117db924cf7c 784 #endif /* !NO_SHA256 */
wolfSSL 15:117db924cf7c 785
wolfSSL 15:117db924cf7c 786 #endif /* !defined(WOLFSSL_TI_HASH) */
wolfSSL 15:117db924cf7c 787
wolfSSL 15:117db924cf7c 788
wolfSSL 15:117db924cf7c 789 #if defined(WOLFSSL_SHA512)
wolfSSL 15:117db924cf7c 790 int wc_Sha512Hash(const byte* data, word32 len, byte* hash)
wolfSSL 15:117db924cf7c 791 {
wolfSSL 15:117db924cf7c 792 int ret = 0;
wolfSSL 15:117db924cf7c 793 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 794 wc_Sha512* sha512;
wolfSSL 15:117db924cf7c 795 #else
wolfSSL 15:117db924cf7c 796 wc_Sha512 sha512[1];
wolfSSL 15:117db924cf7c 797 #endif
wolfSSL 15:117db924cf7c 798
wolfSSL 15:117db924cf7c 799 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 800 sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL,
wolfSSL 15:117db924cf7c 801 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 802 if (sha512 == NULL)
wolfSSL 15:117db924cf7c 803 return MEMORY_E;
wolfSSL 15:117db924cf7c 804 #endif
wolfSSL 15:117db924cf7c 805
wolfSSL 15:117db924cf7c 806 if ((ret = wc_InitSha512(sha512)) != 0) {
wolfSSL 15:117db924cf7c 807 WOLFSSL_MSG("InitSha512 failed");
wolfSSL 15:117db924cf7c 808 }
wolfSSL 15:117db924cf7c 809 else {
wolfSSL 15:117db924cf7c 810 if ((ret = wc_Sha512Update(sha512, data, len)) != 0) {
wolfSSL 15:117db924cf7c 811 WOLFSSL_MSG("Sha512Update failed");
wolfSSL 15:117db924cf7c 812 }
wolfSSL 15:117db924cf7c 813 else if ((ret = wc_Sha512Final(sha512, hash)) != 0) {
wolfSSL 15:117db924cf7c 814 WOLFSSL_MSG("Sha512Final failed");
wolfSSL 15:117db924cf7c 815 }
wolfSSL 15:117db924cf7c 816 wc_Sha512Free(sha512);
wolfSSL 15:117db924cf7c 817 }
wolfSSL 15:117db924cf7c 818
wolfSSL 15:117db924cf7c 819 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 820 XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 821 #endif
wolfSSL 15:117db924cf7c 822
wolfSSL 15:117db924cf7c 823 return ret;
wolfSSL 15:117db924cf7c 824 }
wolfSSL 15:117db924cf7c 825 #endif /* WOLFSSL_SHA512 */
wolfSSL 15:117db924cf7c 826
wolfSSL 15:117db924cf7c 827 #if defined(WOLFSSL_SHA384)
wolfSSL 15:117db924cf7c 828 int wc_Sha384Hash(const byte* data, word32 len, byte* hash)
wolfSSL 15:117db924cf7c 829 {
wolfSSL 15:117db924cf7c 830 int ret = 0;
wolfSSL 15:117db924cf7c 831 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 832 wc_Sha384* sha384;
wolfSSL 15:117db924cf7c 833 #else
wolfSSL 15:117db924cf7c 834 wc_Sha384 sha384[1];
wolfSSL 15:117db924cf7c 835 #endif
wolfSSL 15:117db924cf7c 836
wolfSSL 15:117db924cf7c 837 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 838 sha384 = (wc_Sha384*)XMALLOC(sizeof(wc_Sha384), NULL,
wolfSSL 15:117db924cf7c 839 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 840 if (sha384 == NULL)
wolfSSL 15:117db924cf7c 841 return MEMORY_E;
wolfSSL 15:117db924cf7c 842 #endif
wolfSSL 15:117db924cf7c 843
wolfSSL 15:117db924cf7c 844 if ((ret = wc_InitSha384(sha384)) != 0) {
wolfSSL 15:117db924cf7c 845 WOLFSSL_MSG("InitSha384 failed");
wolfSSL 15:117db924cf7c 846 }
wolfSSL 15:117db924cf7c 847 else {
wolfSSL 15:117db924cf7c 848 if ((ret = wc_Sha384Update(sha384, data, len)) != 0) {
wolfSSL 15:117db924cf7c 849 WOLFSSL_MSG("Sha384Update failed");
wolfSSL 15:117db924cf7c 850 }
wolfSSL 15:117db924cf7c 851 else if ((ret = wc_Sha384Final(sha384, hash)) != 0) {
wolfSSL 15:117db924cf7c 852 WOLFSSL_MSG("Sha384Final failed");
wolfSSL 15:117db924cf7c 853 }
wolfSSL 15:117db924cf7c 854 wc_Sha384Free(sha384);
wolfSSL 15:117db924cf7c 855 }
wolfSSL 15:117db924cf7c 856
wolfSSL 15:117db924cf7c 857 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 858 XFREE(sha384, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 859 #endif
wolfSSL 15:117db924cf7c 860
wolfSSL 15:117db924cf7c 861 return ret;
wolfSSL 15:117db924cf7c 862 }
wolfSSL 15:117db924cf7c 863 #endif /* WOLFSSL_SHA384 */
wolfSSL 15:117db924cf7c 864