wolf SSL / wolfSSL

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

Committer:
wolfSSL
Date:
Thu Jun 04 23:57:22 2020 +0000
Revision:
16:8e0d178b1d1e
Parent:
15:117db924cf7c
wolfSSL 4.4.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 15:117db924cf7c 1 /* hash.c
wolfSSL 15:117db924cf7c 2 *
wolfSSL 16:8e0d178b1d1e 3 * Copyright (C) 2006-2020 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 16:8e0d178b1d1e 35 #include <wolfssl/wolfcrypt/hmac.h>
wolfSSL 15:117db924cf7c 36
wolfSSL 16:8e0d178b1d1e 37 #ifdef NO_INLINE
wolfSSL 16:8e0d178b1d1e 38 #include <wolfssl/wolfcrypt/misc.h>
wolfSSL 16:8e0d178b1d1e 39 #else
wolfSSL 16:8e0d178b1d1e 40 #define WOLFSSL_MISC_INCLUDED
wolfSSL 16:8e0d178b1d1e 41 #include <wolfcrypt/src/misc.c>
wolfSSL 16:8e0d178b1d1e 42 #endif
wolfSSL 16:8e0d178b1d1e 43
wolfSSL 15:117db924cf7c 44
wolfSSL 15:117db924cf7c 45 #ifdef NO_ASN
wolfSSL 15:117db924cf7c 46 enum Hash_Sum {
wolfSSL 16:8e0d178b1d1e 47 MD2h = 646,
wolfSSL 16:8e0d178b1d1e 48 MD5h = 649,
wolfSSL 16:8e0d178b1d1e 49 SHAh = 88,
wolfSSL 16:8e0d178b1d1e 50 SHA224h = 417,
wolfSSL 16:8e0d178b1d1e 51 SHA256h = 414,
wolfSSL 16:8e0d178b1d1e 52 SHA384h = 415,
wolfSSL 16:8e0d178b1d1e 53 SHA512h = 416,
wolfSSL 16:8e0d178b1d1e 54 SHA3_224h = 420,
wolfSSL 16:8e0d178b1d1e 55 SHA3_256h = 421,
wolfSSL 16:8e0d178b1d1e 56 SHA3_384h = 422,
wolfSSL 16:8e0d178b1d1e 57 SHA3_512h = 423
wolfSSL 15:117db924cf7c 58 };
wolfSSL 15:117db924cf7c 59 #endif /* !NO_ASN */
wolfSSL 15:117db924cf7c 60
wolfSSL 16:8e0d178b1d1e 61 #if !defined(NO_PWDBASED) || !defined(NO_ASN)
wolfSSL 15:117db924cf7c 62 /* function converts int hash type to enum */
wolfSSL 15:117db924cf7c 63 enum wc_HashType wc_HashTypeConvert(int hashType)
wolfSSL 15:117db924cf7c 64 {
wolfSSL 15:117db924cf7c 65 /* Default to hash type none as error */
wolfSSL 15:117db924cf7c 66 enum wc_HashType eHashType = WC_HASH_TYPE_NONE;
wolfSSL 15:117db924cf7c 67 #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
wolfSSL 15:117db924cf7c 68 /* original FIPSv1 and CAVP selftest require a mapping for unique hash
wolfSSL 15:117db924cf7c 69 type to wc_HashType */
wolfSSL 15:117db924cf7c 70 switch (hashType) {
wolfSSL 15:117db924cf7c 71 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 72 case WC_MD5:
wolfSSL 15:117db924cf7c 73 eHashType = WC_HASH_TYPE_MD5;
wolfSSL 15:117db924cf7c 74 break;
wolfSSL 15:117db924cf7c 75 #endif /* !NO_MD5 */
wolfSSL 15:117db924cf7c 76 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 77 case WC_SHA:
wolfSSL 15:117db924cf7c 78 eHashType = WC_HASH_TYPE_SHA;
wolfSSL 15:117db924cf7c 79 break;
wolfSSL 15:117db924cf7c 80 #endif /* !NO_SHA */
wolfSSL 15:117db924cf7c 81
wolfSSL 15:117db924cf7c 82 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 83 case WC_SHA224:
wolfSSL 15:117db924cf7c 84 eHashType = WC_HASH_TYPE_SHA224;
wolfSSL 15:117db924cf7c 85 break;
wolfSSL 15:117db924cf7c 86 #endif /* WOLFSSL_SHA224 */
wolfSSL 15:117db924cf7c 87
wolfSSL 15:117db924cf7c 88 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 89 case WC_SHA256:
wolfSSL 15:117db924cf7c 90 eHashType = WC_HASH_TYPE_SHA256;
wolfSSL 15:117db924cf7c 91 break;
wolfSSL 15:117db924cf7c 92 #endif /* !NO_SHA256 */
wolfSSL 15:117db924cf7c 93
wolfSSL 15:117db924cf7c 94 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 95 case WC_SHA384:
wolfSSL 15:117db924cf7c 96 eHashType = WC_HASH_TYPE_SHA384;
wolfSSL 15:117db924cf7c 97 break;
wolfSSL 15:117db924cf7c 98 #endif /* WOLFSSL_SHA384 */
wolfSSL 15:117db924cf7c 99 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 100 case WC_SHA512:
wolfSSL 15:117db924cf7c 101 eHashType = WC_HASH_TYPE_SHA512;
wolfSSL 15:117db924cf7c 102 break;
wolfSSL 15:117db924cf7c 103 #endif /* WOLFSSL_SHA512 */
wolfSSL 16:8e0d178b1d1e 104 #ifdef WOLFSSL_SHA3
wolfSSL 16:8e0d178b1d1e 105 case WC_SHA3_224:
wolfSSL 16:8e0d178b1d1e 106 eHashType = WC_HASH_TYPE_SHA3_224;
wolfSSL 16:8e0d178b1d1e 107 break;
wolfSSL 16:8e0d178b1d1e 108 case WC_SHA3_256:
wolfSSL 16:8e0d178b1d1e 109 eHashType = WC_HASH_TYPE_SHA3_256;
wolfSSL 16:8e0d178b1d1e 110 break;
wolfSSL 16:8e0d178b1d1e 111 case WC_SHA3_384:
wolfSSL 16:8e0d178b1d1e 112 eHashType = WC_HASH_TYPE_SHA3_384;
wolfSSL 16:8e0d178b1d1e 113 break;
wolfSSL 16:8e0d178b1d1e 114 case WC_SHA3_512:
wolfSSL 16:8e0d178b1d1e 115 eHashType = WC_HASH_TYPE_SHA3_512;
wolfSSL 16:8e0d178b1d1e 116 break;
wolfSSL 16:8e0d178b1d1e 117 #endif /* WOLFSSL_SHA3 */
wolfSSL 15:117db924cf7c 118 default:
wolfSSL 15:117db924cf7c 119 eHashType = WC_HASH_TYPE_NONE;
wolfSSL 15:117db924cf7c 120 break;
wolfSSL 15:117db924cf7c 121 }
wolfSSL 15:117db924cf7c 122 #else
wolfSSL 15:117db924cf7c 123 /* current master uses same unique types as wc_HashType */
wolfSSL 15:117db924cf7c 124 if (hashType > 0 && hashType <= WC_HASH_TYPE_MAX) {
wolfSSL 15:117db924cf7c 125 eHashType = (enum wc_HashType)hashType;
wolfSSL 15:117db924cf7c 126 }
wolfSSL 15:117db924cf7c 127 #endif
wolfSSL 15:117db924cf7c 128 return eHashType;
wolfSSL 15:117db924cf7c 129 }
wolfSSL 16:8e0d178b1d1e 130 #endif /* !NO_PWDBASED || !NO_ASN */
wolfSSL 15:117db924cf7c 131
wolfSSL 16:8e0d178b1d1e 132 #if !defined(NO_ASN) || !defined(NO_DH) || defined(HAVE_ECC)
wolfSSL 15:117db924cf7c 133
wolfSSL 15:117db924cf7c 134 int wc_HashGetOID(enum wc_HashType hash_type)
wolfSSL 15:117db924cf7c 135 {
wolfSSL 15:117db924cf7c 136 int oid = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 137 switch(hash_type)
wolfSSL 15:117db924cf7c 138 {
wolfSSL 15:117db924cf7c 139 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 140 #ifdef WOLFSSL_MD2
wolfSSL 15:117db924cf7c 141 oid = MD2h;
wolfSSL 15:117db924cf7c 142 #endif
wolfSSL 15:117db924cf7c 143 break;
wolfSSL 15:117db924cf7c 144 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 15:117db924cf7c 145 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 146 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 147 oid = MD5h;
wolfSSL 15:117db924cf7c 148 #endif
wolfSSL 15:117db924cf7c 149 break;
wolfSSL 15:117db924cf7c 150 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 151 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 152 oid = SHAh;
wolfSSL 15:117db924cf7c 153 #endif
wolfSSL 15:117db924cf7c 154 break;
wolfSSL 15:117db924cf7c 155 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 156 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 157 oid = SHA224h;
wolfSSL 15:117db924cf7c 158 #endif
wolfSSL 15:117db924cf7c 159 break;
wolfSSL 15:117db924cf7c 160 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 161 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 162 oid = SHA256h;
wolfSSL 15:117db924cf7c 163 #endif
wolfSSL 15:117db924cf7c 164 break;
wolfSSL 15:117db924cf7c 165 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 166 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 167 oid = SHA384h;
wolfSSL 15:117db924cf7c 168 #endif
wolfSSL 15:117db924cf7c 169 break;
wolfSSL 15:117db924cf7c 170 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 171 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 172 oid = SHA512h;
wolfSSL 15:117db924cf7c 173 #endif
wolfSSL 15:117db924cf7c 174 break;
wolfSSL 16:8e0d178b1d1e 175 case WC_HASH_TYPE_SHA3_224:
wolfSSL 16:8e0d178b1d1e 176 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
wolfSSL 16:8e0d178b1d1e 177 oid = SHA3_224h;
wolfSSL 16:8e0d178b1d1e 178 #endif
wolfSSL 16:8e0d178b1d1e 179 break;
wolfSSL 16:8e0d178b1d1e 180 case WC_HASH_TYPE_SHA3_256:
wolfSSL 16:8e0d178b1d1e 181 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
wolfSSL 16:8e0d178b1d1e 182 oid = SHA3_256h;
wolfSSL 16:8e0d178b1d1e 183 #endif
wolfSSL 16:8e0d178b1d1e 184 break;
wolfSSL 16:8e0d178b1d1e 185 case WC_HASH_TYPE_SHA3_384:
wolfSSL 16:8e0d178b1d1e 186 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
wolfSSL 16:8e0d178b1d1e 187 oid = SHA3_384h;
wolfSSL 16:8e0d178b1d1e 188 #endif
wolfSSL 16:8e0d178b1d1e 189 break;
wolfSSL 16:8e0d178b1d1e 190 case WC_HASH_TYPE_SHA3_512:
wolfSSL 16:8e0d178b1d1e 191 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
wolfSSL 16:8e0d178b1d1e 192 oid = SHA3_512h;
wolfSSL 16:8e0d178b1d1e 193 #endif
wolfSSL 16:8e0d178b1d1e 194 break;
wolfSSL 15:117db924cf7c 195
wolfSSL 15:117db924cf7c 196 /* Not Supported */
wolfSSL 15:117db924cf7c 197 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 198 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 16:8e0d178b1d1e 199 case WC_HASH_TYPE_BLAKE2S:
wolfSSL 15:117db924cf7c 200 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 201 default:
wolfSSL 15:117db924cf7c 202 oid = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 203 break;
wolfSSL 15:117db924cf7c 204 }
wolfSSL 15:117db924cf7c 205 return oid;
wolfSSL 15:117db924cf7c 206 }
wolfSSL 15:117db924cf7c 207
wolfSSL 15:117db924cf7c 208 enum wc_HashType wc_OidGetHash(int oid)
wolfSSL 15:117db924cf7c 209 {
wolfSSL 15:117db924cf7c 210 enum wc_HashType hash_type = WC_HASH_TYPE_NONE;
wolfSSL 15:117db924cf7c 211 switch (oid)
wolfSSL 15:117db924cf7c 212 {
wolfSSL 16:8e0d178b1d1e 213 #ifdef WOLFSSL_MD2
wolfSSL 15:117db924cf7c 214 case MD2h:
wolfSSL 15:117db924cf7c 215 hash_type = WC_HASH_TYPE_MD2;
wolfSSL 15:117db924cf7c 216 break;
wolfSSL 16:8e0d178b1d1e 217 #endif
wolfSSL 15:117db924cf7c 218 case MD5h:
wolfSSL 15:117db924cf7c 219 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 220 hash_type = WC_HASH_TYPE_MD5;
wolfSSL 15:117db924cf7c 221 #endif
wolfSSL 15:117db924cf7c 222 break;
wolfSSL 15:117db924cf7c 223 case SHAh:
wolfSSL 15:117db924cf7c 224 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 225 hash_type = WC_HASH_TYPE_SHA;
wolfSSL 15:117db924cf7c 226 #endif
wolfSSL 15:117db924cf7c 227 break;
wolfSSL 15:117db924cf7c 228 case SHA224h:
wolfSSL 15:117db924cf7c 229 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 230 hash_type = WC_HASH_TYPE_SHA224;
wolfSSL 15:117db924cf7c 231 #endif
wolfSSL 15:117db924cf7c 232 break;
wolfSSL 15:117db924cf7c 233 case SHA256h:
wolfSSL 15:117db924cf7c 234 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 235 hash_type = WC_HASH_TYPE_SHA256;
wolfSSL 15:117db924cf7c 236 #endif
wolfSSL 15:117db924cf7c 237 break;
wolfSSL 15:117db924cf7c 238 case SHA384h:
wolfSSL 15:117db924cf7c 239 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 240 hash_type = WC_HASH_TYPE_SHA384;
wolfSSL 15:117db924cf7c 241 #endif
wolfSSL 15:117db924cf7c 242 break;
wolfSSL 15:117db924cf7c 243 case SHA512h:
wolfSSL 15:117db924cf7c 244 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 245 hash_type = WC_HASH_TYPE_SHA512;
wolfSSL 15:117db924cf7c 246 #endif
wolfSSL 15:117db924cf7c 247 break;
wolfSSL 16:8e0d178b1d1e 248 #ifdef WOLFSSL_SHA3
wolfSSL 16:8e0d178b1d1e 249 case SHA3_224h:
wolfSSL 16:8e0d178b1d1e 250 hash_type = WC_HASH_TYPE_SHA3_224;
wolfSSL 16:8e0d178b1d1e 251 break;
wolfSSL 16:8e0d178b1d1e 252 case SHA3_256h:
wolfSSL 16:8e0d178b1d1e 253 hash_type = WC_HASH_TYPE_SHA3_256;
wolfSSL 16:8e0d178b1d1e 254 break;
wolfSSL 16:8e0d178b1d1e 255 case SHA3_384h:
wolfSSL 16:8e0d178b1d1e 256 hash_type = WC_HASH_TYPE_SHA3_384;
wolfSSL 16:8e0d178b1d1e 257 break;
wolfSSL 16:8e0d178b1d1e 258 case SHA3_512h:
wolfSSL 16:8e0d178b1d1e 259 hash_type = WC_HASH_TYPE_SHA3_512;
wolfSSL 16:8e0d178b1d1e 260 break;
wolfSSL 16:8e0d178b1d1e 261 #endif /* WOLFSSL_SHA3 */
wolfSSL 15:117db924cf7c 262 default:
wolfSSL 15:117db924cf7c 263 break;
wolfSSL 15:117db924cf7c 264 }
wolfSSL 15:117db924cf7c 265 return hash_type;
wolfSSL 15:117db924cf7c 266 }
wolfSSL 15:117db924cf7c 267 #endif /* !NO_ASN || !NO_DH || HAVE_ECC */
wolfSSL 15:117db924cf7c 268
wolfSSL 16:8e0d178b1d1e 269 #ifndef NO_HASH_WRAPPER
wolfSSL 15:117db924cf7c 270
wolfSSL 15:117db924cf7c 271 /* Get Hash digest size */
wolfSSL 15:117db924cf7c 272 int wc_HashGetDigestSize(enum wc_HashType hash_type)
wolfSSL 15:117db924cf7c 273 {
wolfSSL 15:117db924cf7c 274 int dig_size = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 275 switch(hash_type)
wolfSSL 15:117db924cf7c 276 {
wolfSSL 15:117db924cf7c 277 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 278 #ifdef WOLFSSL_MD2
wolfSSL 15:117db924cf7c 279 dig_size = MD2_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 280 #endif
wolfSSL 15:117db924cf7c 281 break;
wolfSSL 15:117db924cf7c 282 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 283 #ifndef NO_MD4
wolfSSL 15:117db924cf7c 284 dig_size = MD4_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 285 #endif
wolfSSL 15:117db924cf7c 286 break;
wolfSSL 15:117db924cf7c 287 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 288 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 289 dig_size = WC_MD5_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 290 #endif
wolfSSL 15:117db924cf7c 291 break;
wolfSSL 15:117db924cf7c 292 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 293 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 294 dig_size = WC_SHA_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 295 #endif
wolfSSL 15:117db924cf7c 296 break;
wolfSSL 15:117db924cf7c 297 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 298 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 299 dig_size = WC_SHA224_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 300 #endif
wolfSSL 15:117db924cf7c 301 break;
wolfSSL 15:117db924cf7c 302 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 303 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 304 dig_size = WC_SHA256_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 305 #endif
wolfSSL 15:117db924cf7c 306 break;
wolfSSL 15:117db924cf7c 307 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 308 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 309 dig_size = WC_SHA384_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 310 #endif
wolfSSL 15:117db924cf7c 311 break;
wolfSSL 15:117db924cf7c 312 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 313 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 314 dig_size = WC_SHA512_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 315 #endif
wolfSSL 15:117db924cf7c 316 break;
wolfSSL 15:117db924cf7c 317 case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */
wolfSSL 15:117db924cf7c 318 #if !defined(NO_MD5) && !defined(NO_SHA)
wolfSSL 15:117db924cf7c 319 dig_size = (int)WC_MD5_DIGEST_SIZE + (int)WC_SHA_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 320 #endif
wolfSSL 15:117db924cf7c 321 break;
wolfSSL 15:117db924cf7c 322
wolfSSL 15:117db924cf7c 323 case WC_HASH_TYPE_SHA3_224:
wolfSSL 16:8e0d178b1d1e 324 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
wolfSSL 15:117db924cf7c 325 dig_size = WC_SHA3_224_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 326 #endif
wolfSSL 15:117db924cf7c 327 break;
wolfSSL 15:117db924cf7c 328 case WC_HASH_TYPE_SHA3_256:
wolfSSL 16:8e0d178b1d1e 329 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
wolfSSL 15:117db924cf7c 330 dig_size = WC_SHA3_256_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 331 #endif
wolfSSL 15:117db924cf7c 332 break;
wolfSSL 15:117db924cf7c 333 case WC_HASH_TYPE_SHA3_384:
wolfSSL 16:8e0d178b1d1e 334 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
wolfSSL 15:117db924cf7c 335 dig_size = WC_SHA3_384_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 336 #endif
wolfSSL 15:117db924cf7c 337 break;
wolfSSL 15:117db924cf7c 338 case WC_HASH_TYPE_SHA3_512:
wolfSSL 16:8e0d178b1d1e 339 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
wolfSSL 15:117db924cf7c 340 dig_size = WC_SHA3_512_DIGEST_SIZE;
wolfSSL 15:117db924cf7c 341 #endif
wolfSSL 15:117db924cf7c 342 break;
wolfSSL 16:8e0d178b1d1e 343 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 16:8e0d178b1d1e 344 case WC_HASH_TYPE_BLAKE2S:
wolfSSL 16:8e0d178b1d1e 345 #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
wolfSSL 16:8e0d178b1d1e 346 dig_size = BLAKE2S_OUTBYTES;
wolfSSL 16:8e0d178b1d1e 347 #endif
wolfSSL 16:8e0d178b1d1e 348 break;
wolfSSL 15:117db924cf7c 349
wolfSSL 15:117db924cf7c 350 /* Not Supported */
wolfSSL 15:117db924cf7c 351 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 352 default:
wolfSSL 15:117db924cf7c 353 dig_size = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 354 break;
wolfSSL 15:117db924cf7c 355 }
wolfSSL 15:117db924cf7c 356 return dig_size;
wolfSSL 15:117db924cf7c 357 }
wolfSSL 15:117db924cf7c 358
wolfSSL 15:117db924cf7c 359
wolfSSL 15:117db924cf7c 360 /* Get Hash block size */
wolfSSL 15:117db924cf7c 361 int wc_HashGetBlockSize(enum wc_HashType hash_type)
wolfSSL 15:117db924cf7c 362 {
wolfSSL 15:117db924cf7c 363 int block_size = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 364 switch (hash_type)
wolfSSL 15:117db924cf7c 365 {
wolfSSL 15:117db924cf7c 366 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 367 #ifdef WOLFSSL_MD2
wolfSSL 15:117db924cf7c 368 block_size = MD2_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 369 #endif
wolfSSL 15:117db924cf7c 370 break;
wolfSSL 15:117db924cf7c 371 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 372 #ifndef NO_MD4
wolfSSL 15:117db924cf7c 373 block_size = MD4_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 374 #endif
wolfSSL 15:117db924cf7c 375 break;
wolfSSL 15:117db924cf7c 376 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 377 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 378 block_size = WC_MD5_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 379 #endif
wolfSSL 15:117db924cf7c 380 break;
wolfSSL 15:117db924cf7c 381 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 382 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 383 block_size = WC_SHA_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 384 #endif
wolfSSL 15:117db924cf7c 385 break;
wolfSSL 15:117db924cf7c 386 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 387 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 388 block_size = WC_SHA224_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 389 #endif
wolfSSL 15:117db924cf7c 390 break;
wolfSSL 15:117db924cf7c 391 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 392 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 393 block_size = WC_SHA256_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 394 #endif
wolfSSL 15:117db924cf7c 395 break;
wolfSSL 15:117db924cf7c 396 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 397 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 398 block_size = WC_SHA384_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 399 #endif
wolfSSL 15:117db924cf7c 400 break;
wolfSSL 15:117db924cf7c 401 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 402 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 403 block_size = WC_SHA512_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 404 #endif
wolfSSL 15:117db924cf7c 405 break;
wolfSSL 15:117db924cf7c 406 case WC_HASH_TYPE_MD5_SHA: /* Old TLS Specific */
wolfSSL 15:117db924cf7c 407 #if !defined(NO_MD5) && !defined(NO_SHA)
wolfSSL 15:117db924cf7c 408 block_size = (int)WC_MD5_BLOCK_SIZE + (int)WC_SHA_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 409 #endif
wolfSSL 15:117db924cf7c 410 break;
wolfSSL 15:117db924cf7c 411
wolfSSL 15:117db924cf7c 412 case WC_HASH_TYPE_SHA3_224:
wolfSSL 16:8e0d178b1d1e 413 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
wolfSSL 15:117db924cf7c 414 block_size = WC_SHA3_224_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 415 #endif
wolfSSL 15:117db924cf7c 416 break;
wolfSSL 15:117db924cf7c 417 case WC_HASH_TYPE_SHA3_256:
wolfSSL 16:8e0d178b1d1e 418 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
wolfSSL 15:117db924cf7c 419 block_size = WC_SHA3_256_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 420 #endif
wolfSSL 15:117db924cf7c 421 break;
wolfSSL 15:117db924cf7c 422 case WC_HASH_TYPE_SHA3_384:
wolfSSL 16:8e0d178b1d1e 423 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
wolfSSL 15:117db924cf7c 424 block_size = WC_SHA3_384_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 425 #endif
wolfSSL 15:117db924cf7c 426 break;
wolfSSL 15:117db924cf7c 427 case WC_HASH_TYPE_SHA3_512:
wolfSSL 16:8e0d178b1d1e 428 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
wolfSSL 15:117db924cf7c 429 block_size = WC_SHA3_512_BLOCK_SIZE;
wolfSSL 15:117db924cf7c 430 #endif
wolfSSL 15:117db924cf7c 431 break;
wolfSSL 16:8e0d178b1d1e 432 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 16:8e0d178b1d1e 433 case WC_HASH_TYPE_BLAKE2S:
wolfSSL 16:8e0d178b1d1e 434 #if defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S)
wolfSSL 16:8e0d178b1d1e 435 block_size = BLAKE2S_BLOCKBYTES;
wolfSSL 16:8e0d178b1d1e 436 #endif
wolfSSL 16:8e0d178b1d1e 437 break;
wolfSSL 15:117db924cf7c 438
wolfSSL 15:117db924cf7c 439 /* Not Supported */
wolfSSL 15:117db924cf7c 440 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 441 default:
wolfSSL 15:117db924cf7c 442 block_size = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 443 break;
wolfSSL 15:117db924cf7c 444 }
wolfSSL 15:117db924cf7c 445 return block_size;
wolfSSL 15:117db924cf7c 446 }
wolfSSL 15:117db924cf7c 447
wolfSSL 15:117db924cf7c 448 /* Generic Hashing Wrapper */
wolfSSL 15:117db924cf7c 449 int wc_Hash(enum wc_HashType hash_type, const byte* data,
wolfSSL 15:117db924cf7c 450 word32 data_len, byte* hash, word32 hash_len)
wolfSSL 15:117db924cf7c 451 {
wolfSSL 15:117db924cf7c 452 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 453 word32 dig_size;
wolfSSL 15:117db924cf7c 454
wolfSSL 15:117db924cf7c 455 /* Validate hash buffer size */
wolfSSL 15:117db924cf7c 456 dig_size = wc_HashGetDigestSize(hash_type);
wolfSSL 15:117db924cf7c 457 if (hash_len < dig_size) {
wolfSSL 15:117db924cf7c 458 return BUFFER_E;
wolfSSL 15:117db924cf7c 459 }
wolfSSL 15:117db924cf7c 460
wolfSSL 15:117db924cf7c 461 /* Suppress possible unused arg if all hashing is disabled */
wolfSSL 15:117db924cf7c 462 (void)data;
wolfSSL 15:117db924cf7c 463 (void)data_len;
wolfSSL 15:117db924cf7c 464 (void)hash;
wolfSSL 15:117db924cf7c 465 (void)hash_len;
wolfSSL 15:117db924cf7c 466
wolfSSL 15:117db924cf7c 467 switch(hash_type)
wolfSSL 15:117db924cf7c 468 {
wolfSSL 15:117db924cf7c 469 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 470 #ifndef NO_MD5
wolfSSL 15:117db924cf7c 471 ret = wc_Md5Hash(data, data_len, hash);
wolfSSL 15:117db924cf7c 472 #endif
wolfSSL 15:117db924cf7c 473 break;
wolfSSL 15:117db924cf7c 474 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 475 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 476 ret = wc_ShaHash(data, data_len, hash);
wolfSSL 15:117db924cf7c 477 #endif
wolfSSL 15:117db924cf7c 478 break;
wolfSSL 15:117db924cf7c 479 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 480 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 481 ret = wc_Sha224Hash(data, data_len, hash);
wolfSSL 15:117db924cf7c 482 #endif
wolfSSL 15:117db924cf7c 483 break;
wolfSSL 15:117db924cf7c 484 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 485 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 486 ret = wc_Sha256Hash(data, data_len, hash);
wolfSSL 15:117db924cf7c 487 #endif
wolfSSL 15:117db924cf7c 488 break;
wolfSSL 15:117db924cf7c 489 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 490 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 491 ret = wc_Sha384Hash(data, data_len, hash);
wolfSSL 15:117db924cf7c 492 #endif
wolfSSL 15:117db924cf7c 493 break;
wolfSSL 15:117db924cf7c 494 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 495 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 496 ret = wc_Sha512Hash(data, data_len, hash);
wolfSSL 15:117db924cf7c 497 #endif
wolfSSL 15:117db924cf7c 498 break;
wolfSSL 15:117db924cf7c 499 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 15:117db924cf7c 500 #if !defined(NO_MD5) && !defined(NO_SHA)
wolfSSL 15:117db924cf7c 501 ret = wc_Md5Hash(data, data_len, hash);
wolfSSL 15:117db924cf7c 502 if (ret == 0) {
wolfSSL 15:117db924cf7c 503 ret = wc_ShaHash(data, data_len, &hash[WC_MD5_DIGEST_SIZE]);
wolfSSL 15:117db924cf7c 504 }
wolfSSL 15:117db924cf7c 505 #endif
wolfSSL 15:117db924cf7c 506 break;
wolfSSL 15:117db924cf7c 507
wolfSSL 16:8e0d178b1d1e 508 case WC_HASH_TYPE_SHA3_224:
wolfSSL 16:8e0d178b1d1e 509 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
wolfSSL 16:8e0d178b1d1e 510 ret = wc_Sha3_224Hash(data, data_len, hash);
wolfSSL 16:8e0d178b1d1e 511 #endif
wolfSSL 16:8e0d178b1d1e 512 break;
wolfSSL 16:8e0d178b1d1e 513 case WC_HASH_TYPE_SHA3_256:
wolfSSL 16:8e0d178b1d1e 514 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
wolfSSL 16:8e0d178b1d1e 515 ret = wc_Sha3_256Hash(data, data_len, hash);
wolfSSL 16:8e0d178b1d1e 516 #endif
wolfSSL 16:8e0d178b1d1e 517 break;
wolfSSL 16:8e0d178b1d1e 518 case WC_HASH_TYPE_SHA3_384:
wolfSSL 16:8e0d178b1d1e 519 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
wolfSSL 16:8e0d178b1d1e 520 ret = wc_Sha3_384Hash(data, data_len, hash);
wolfSSL 16:8e0d178b1d1e 521 #endif
wolfSSL 16:8e0d178b1d1e 522 break;
wolfSSL 16:8e0d178b1d1e 523 case WC_HASH_TYPE_SHA3_512:
wolfSSL 16:8e0d178b1d1e 524 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
wolfSSL 16:8e0d178b1d1e 525 ret = wc_Sha3_512Hash(data, data_len, hash);
wolfSSL 16:8e0d178b1d1e 526 #endif
wolfSSL 16:8e0d178b1d1e 527 break;
wolfSSL 16:8e0d178b1d1e 528
wolfSSL 15:117db924cf7c 529 /* Not Supported */
wolfSSL 15:117db924cf7c 530 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 531 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 532 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 16:8e0d178b1d1e 533 case WC_HASH_TYPE_BLAKE2S:
wolfSSL 15:117db924cf7c 534 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 535 default:
wolfSSL 15:117db924cf7c 536 ret = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 537 break;
wolfSSL 15:117db924cf7c 538 }
wolfSSL 15:117db924cf7c 539 return ret;
wolfSSL 15:117db924cf7c 540 }
wolfSSL 15:117db924cf7c 541
wolfSSL 16:8e0d178b1d1e 542 int wc_HashInit_ex(wc_HashAlg* hash, enum wc_HashType type, void* heap,
wolfSSL 16:8e0d178b1d1e 543 int devId)
wolfSSL 15:117db924cf7c 544 {
wolfSSL 15:117db924cf7c 545 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 546
wolfSSL 15:117db924cf7c 547 if (hash == NULL)
wolfSSL 15:117db924cf7c 548 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 549
wolfSSL 15:117db924cf7c 550 switch (type) {
wolfSSL 15:117db924cf7c 551 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 552 #ifndef NO_MD5
wolfSSL 16:8e0d178b1d1e 553 ret = wc_InitMd5_ex(&hash->md5, heap, devId);
wolfSSL 15:117db924cf7c 554 #endif
wolfSSL 15:117db924cf7c 555 break;
wolfSSL 15:117db924cf7c 556 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 557 #ifndef NO_SHA
wolfSSL 16:8e0d178b1d1e 558 ret = wc_InitSha_ex(&hash->sha, heap, devId);
wolfSSL 15:117db924cf7c 559 #endif
wolfSSL 15:117db924cf7c 560 break;
wolfSSL 15:117db924cf7c 561 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 562 #ifdef WOLFSSL_SHA224
wolfSSL 16:8e0d178b1d1e 563 ret = wc_InitSha224_ex(&hash->sha224, heap, devId);
wolfSSL 15:117db924cf7c 564 #endif
wolfSSL 15:117db924cf7c 565 break;
wolfSSL 15:117db924cf7c 566 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 567 #ifndef NO_SHA256
wolfSSL 16:8e0d178b1d1e 568 ret = wc_InitSha256_ex(&hash->sha256, heap, devId);
wolfSSL 15:117db924cf7c 569 #endif
wolfSSL 15:117db924cf7c 570 break;
wolfSSL 15:117db924cf7c 571 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 572 #ifdef WOLFSSL_SHA384
wolfSSL 16:8e0d178b1d1e 573 ret = wc_InitSha384_ex(&hash->sha384, heap, devId);
wolfSSL 15:117db924cf7c 574 #endif
wolfSSL 15:117db924cf7c 575 break;
wolfSSL 15:117db924cf7c 576 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 577 #ifdef WOLFSSL_SHA512
wolfSSL 16:8e0d178b1d1e 578 ret = wc_InitSha512_ex(&hash->sha512, heap, devId);
wolfSSL 16:8e0d178b1d1e 579 #endif
wolfSSL 16:8e0d178b1d1e 580 break;
wolfSSL 16:8e0d178b1d1e 581
wolfSSL 16:8e0d178b1d1e 582 case WC_HASH_TYPE_SHA3_224:
wolfSSL 16:8e0d178b1d1e 583 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
wolfSSL 16:8e0d178b1d1e 584 ret = wc_InitSha3_224(&hash->sha3, heap, devId);
wolfSSL 16:8e0d178b1d1e 585 #endif
wolfSSL 16:8e0d178b1d1e 586 break;
wolfSSL 16:8e0d178b1d1e 587 case WC_HASH_TYPE_SHA3_256:
wolfSSL 16:8e0d178b1d1e 588 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
wolfSSL 16:8e0d178b1d1e 589 ret = wc_InitSha3_256(&hash->sha3, heap, devId);
wolfSSL 16:8e0d178b1d1e 590 #endif
wolfSSL 16:8e0d178b1d1e 591 break;
wolfSSL 16:8e0d178b1d1e 592 case WC_HASH_TYPE_SHA3_384:
wolfSSL 16:8e0d178b1d1e 593 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
wolfSSL 16:8e0d178b1d1e 594 ret = wc_InitSha3_384(&hash->sha3, heap, devId);
wolfSSL 16:8e0d178b1d1e 595 #endif
wolfSSL 16:8e0d178b1d1e 596 break;
wolfSSL 16:8e0d178b1d1e 597 case WC_HASH_TYPE_SHA3_512:
wolfSSL 16:8e0d178b1d1e 598 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
wolfSSL 16:8e0d178b1d1e 599 ret = wc_InitSha3_512(&hash->sha3, heap, devId);
wolfSSL 15:117db924cf7c 600 #endif
wolfSSL 15:117db924cf7c 601 break;
wolfSSL 15:117db924cf7c 602
wolfSSL 15:117db924cf7c 603 /* not supported */
wolfSSL 15:117db924cf7c 604 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 15:117db924cf7c 605 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 606 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 607 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 16:8e0d178b1d1e 608 case WC_HASH_TYPE_BLAKE2S:
wolfSSL 15:117db924cf7c 609 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 610 default:
wolfSSL 15:117db924cf7c 611 ret = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 612 };
wolfSSL 15:117db924cf7c 613
wolfSSL 15:117db924cf7c 614 return ret;
wolfSSL 15:117db924cf7c 615 }
wolfSSL 15:117db924cf7c 616
wolfSSL 16:8e0d178b1d1e 617 int wc_HashInit(wc_HashAlg* hash, enum wc_HashType type)
wolfSSL 16:8e0d178b1d1e 618 {
wolfSSL 16:8e0d178b1d1e 619 return wc_HashInit_ex(hash, type, NULL, INVALID_DEVID);
wolfSSL 16:8e0d178b1d1e 620 }
wolfSSL 16:8e0d178b1d1e 621
wolfSSL 15:117db924cf7c 622 int wc_HashUpdate(wc_HashAlg* hash, enum wc_HashType type, const byte* data,
wolfSSL 15:117db924cf7c 623 word32 dataSz)
wolfSSL 15:117db924cf7c 624 {
wolfSSL 15:117db924cf7c 625 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 626
wolfSSL 15:117db924cf7c 627 if (hash == NULL || data == NULL)
wolfSSL 15:117db924cf7c 628 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 629
wolfSSL 15:117db924cf7c 630 switch (type) {
wolfSSL 15:117db924cf7c 631 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 632 #ifndef NO_MD5
wolfSSL 16:8e0d178b1d1e 633 ret = wc_Md5Update(&hash->md5, data, dataSz);
wolfSSL 15:117db924cf7c 634 #endif
wolfSSL 15:117db924cf7c 635 break;
wolfSSL 15:117db924cf7c 636 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 637 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 638 ret = wc_ShaUpdate(&hash->sha, data, dataSz);
wolfSSL 15:117db924cf7c 639 #endif
wolfSSL 15:117db924cf7c 640 break;
wolfSSL 15:117db924cf7c 641 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 642 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 643 ret = wc_Sha224Update(&hash->sha224, data, dataSz);
wolfSSL 15:117db924cf7c 644 #endif
wolfSSL 15:117db924cf7c 645 break;
wolfSSL 15:117db924cf7c 646 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 647 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 648 ret = wc_Sha256Update(&hash->sha256, data, dataSz);
wolfSSL 15:117db924cf7c 649 #endif
wolfSSL 15:117db924cf7c 650 break;
wolfSSL 15:117db924cf7c 651 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 652 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 653 ret = wc_Sha384Update(&hash->sha384, data, dataSz);
wolfSSL 15:117db924cf7c 654 #endif
wolfSSL 15:117db924cf7c 655 break;
wolfSSL 15:117db924cf7c 656 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 657 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 658 ret = wc_Sha512Update(&hash->sha512, data, dataSz);
wolfSSL 15:117db924cf7c 659 #endif
wolfSSL 15:117db924cf7c 660 break;
wolfSSL 15:117db924cf7c 661
wolfSSL 16:8e0d178b1d1e 662 case WC_HASH_TYPE_SHA3_224:
wolfSSL 16:8e0d178b1d1e 663 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
wolfSSL 16:8e0d178b1d1e 664 ret = wc_Sha3_224_Update(&hash->sha3, data, dataSz);
wolfSSL 16:8e0d178b1d1e 665 #endif
wolfSSL 16:8e0d178b1d1e 666 break;
wolfSSL 16:8e0d178b1d1e 667 case WC_HASH_TYPE_SHA3_256:
wolfSSL 16:8e0d178b1d1e 668 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
wolfSSL 16:8e0d178b1d1e 669 ret = wc_Sha3_256_Update(&hash->sha3, data, dataSz);
wolfSSL 16:8e0d178b1d1e 670 #endif
wolfSSL 16:8e0d178b1d1e 671 break;
wolfSSL 16:8e0d178b1d1e 672 case WC_HASH_TYPE_SHA3_384:
wolfSSL 16:8e0d178b1d1e 673 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
wolfSSL 16:8e0d178b1d1e 674 ret = wc_Sha3_384_Update(&hash->sha3, data, dataSz);
wolfSSL 16:8e0d178b1d1e 675 #endif
wolfSSL 16:8e0d178b1d1e 676 break;
wolfSSL 16:8e0d178b1d1e 677 case WC_HASH_TYPE_SHA3_512:
wolfSSL 16:8e0d178b1d1e 678 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
wolfSSL 16:8e0d178b1d1e 679 ret = wc_Sha3_512_Update(&hash->sha3, data, dataSz);
wolfSSL 16:8e0d178b1d1e 680 #endif
wolfSSL 16:8e0d178b1d1e 681 break;
wolfSSL 16:8e0d178b1d1e 682
wolfSSL 15:117db924cf7c 683 /* not supported */
wolfSSL 15:117db924cf7c 684 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 15:117db924cf7c 685 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 686 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 687 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 16:8e0d178b1d1e 688 case WC_HASH_TYPE_BLAKE2S:
wolfSSL 15:117db924cf7c 689 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 690 default:
wolfSSL 15:117db924cf7c 691 ret = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 692 };
wolfSSL 15:117db924cf7c 693
wolfSSL 15:117db924cf7c 694 return ret;
wolfSSL 15:117db924cf7c 695 }
wolfSSL 15:117db924cf7c 696
wolfSSL 15:117db924cf7c 697 int wc_HashFinal(wc_HashAlg* hash, enum wc_HashType type, byte* out)
wolfSSL 15:117db924cf7c 698 {
wolfSSL 15:117db924cf7c 699 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 15:117db924cf7c 700
wolfSSL 15:117db924cf7c 701 if (hash == NULL || out == NULL)
wolfSSL 15:117db924cf7c 702 return BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 703
wolfSSL 15:117db924cf7c 704 switch (type) {
wolfSSL 15:117db924cf7c 705 case WC_HASH_TYPE_MD5:
wolfSSL 15:117db924cf7c 706 #ifndef NO_MD5
wolfSSL 16:8e0d178b1d1e 707 ret = wc_Md5Final(&hash->md5, out);
wolfSSL 15:117db924cf7c 708 #endif
wolfSSL 15:117db924cf7c 709 break;
wolfSSL 15:117db924cf7c 710 case WC_HASH_TYPE_SHA:
wolfSSL 15:117db924cf7c 711 #ifndef NO_SHA
wolfSSL 15:117db924cf7c 712 ret = wc_ShaFinal(&hash->sha, out);
wolfSSL 15:117db924cf7c 713 #endif
wolfSSL 15:117db924cf7c 714 break;
wolfSSL 15:117db924cf7c 715 case WC_HASH_TYPE_SHA224:
wolfSSL 15:117db924cf7c 716 #ifdef WOLFSSL_SHA224
wolfSSL 15:117db924cf7c 717 ret = wc_Sha224Final(&hash->sha224, out);
wolfSSL 15:117db924cf7c 718 #endif
wolfSSL 15:117db924cf7c 719 break;
wolfSSL 15:117db924cf7c 720 case WC_HASH_TYPE_SHA256:
wolfSSL 15:117db924cf7c 721 #ifndef NO_SHA256
wolfSSL 15:117db924cf7c 722 ret = wc_Sha256Final(&hash->sha256, out);
wolfSSL 15:117db924cf7c 723 #endif
wolfSSL 15:117db924cf7c 724 break;
wolfSSL 15:117db924cf7c 725 case WC_HASH_TYPE_SHA384:
wolfSSL 15:117db924cf7c 726 #ifdef WOLFSSL_SHA384
wolfSSL 15:117db924cf7c 727 ret = wc_Sha384Final(&hash->sha384, out);
wolfSSL 15:117db924cf7c 728 #endif
wolfSSL 15:117db924cf7c 729 break;
wolfSSL 15:117db924cf7c 730 case WC_HASH_TYPE_SHA512:
wolfSSL 15:117db924cf7c 731 #ifdef WOLFSSL_SHA512
wolfSSL 15:117db924cf7c 732 ret = wc_Sha512Final(&hash->sha512, out);
wolfSSL 15:117db924cf7c 733 #endif
wolfSSL 15:117db924cf7c 734 break;
wolfSSL 15:117db924cf7c 735
wolfSSL 16:8e0d178b1d1e 736 case WC_HASH_TYPE_SHA3_224:
wolfSSL 16:8e0d178b1d1e 737 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
wolfSSL 16:8e0d178b1d1e 738 ret = wc_Sha3_224_Final(&hash->sha3, out);
wolfSSL 16:8e0d178b1d1e 739 #endif
wolfSSL 16:8e0d178b1d1e 740 break;
wolfSSL 16:8e0d178b1d1e 741 case WC_HASH_TYPE_SHA3_256:
wolfSSL 16:8e0d178b1d1e 742 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
wolfSSL 16:8e0d178b1d1e 743 ret = wc_Sha3_256_Final(&hash->sha3, out);
wolfSSL 16:8e0d178b1d1e 744 #endif
wolfSSL 16:8e0d178b1d1e 745 break;
wolfSSL 16:8e0d178b1d1e 746 case WC_HASH_TYPE_SHA3_384:
wolfSSL 16:8e0d178b1d1e 747 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
wolfSSL 16:8e0d178b1d1e 748 ret = wc_Sha3_384_Final(&hash->sha3, out);
wolfSSL 16:8e0d178b1d1e 749 #endif
wolfSSL 16:8e0d178b1d1e 750 break;
wolfSSL 16:8e0d178b1d1e 751 case WC_HASH_TYPE_SHA3_512:
wolfSSL 16:8e0d178b1d1e 752 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
wolfSSL 16:8e0d178b1d1e 753 ret = wc_Sha3_512_Final(&hash->sha3, out);
wolfSSL 16:8e0d178b1d1e 754 #endif
wolfSSL 16:8e0d178b1d1e 755 break;
wolfSSL 16:8e0d178b1d1e 756
wolfSSL 15:117db924cf7c 757 /* not supported */
wolfSSL 15:117db924cf7c 758 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 15:117db924cf7c 759 case WC_HASH_TYPE_MD2:
wolfSSL 15:117db924cf7c 760 case WC_HASH_TYPE_MD4:
wolfSSL 16:8e0d178b1d1e 761 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 16:8e0d178b1d1e 762 case WC_HASH_TYPE_BLAKE2S:
wolfSSL 16:8e0d178b1d1e 763 case WC_HASH_TYPE_NONE:
wolfSSL 16:8e0d178b1d1e 764 default:
wolfSSL 16:8e0d178b1d1e 765 ret = BAD_FUNC_ARG;
wolfSSL 16:8e0d178b1d1e 766 };
wolfSSL 16:8e0d178b1d1e 767
wolfSSL 16:8e0d178b1d1e 768 return ret;
wolfSSL 16:8e0d178b1d1e 769 }
wolfSSL 16:8e0d178b1d1e 770
wolfSSL 16:8e0d178b1d1e 771 int wc_HashFree(wc_HashAlg* hash, enum wc_HashType type)
wolfSSL 16:8e0d178b1d1e 772 {
wolfSSL 16:8e0d178b1d1e 773 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 16:8e0d178b1d1e 774
wolfSSL 16:8e0d178b1d1e 775 if (hash == NULL)
wolfSSL 16:8e0d178b1d1e 776 return BAD_FUNC_ARG;
wolfSSL 16:8e0d178b1d1e 777
wolfSSL 16:8e0d178b1d1e 778 switch (type) {
wolfSSL 16:8e0d178b1d1e 779 case WC_HASH_TYPE_MD5:
wolfSSL 16:8e0d178b1d1e 780 #ifndef NO_MD5
wolfSSL 16:8e0d178b1d1e 781 wc_Md5Free(&hash->md5);
wolfSSL 16:8e0d178b1d1e 782 ret = 0;
wolfSSL 16:8e0d178b1d1e 783 #endif
wolfSSL 16:8e0d178b1d1e 784 break;
wolfSSL 16:8e0d178b1d1e 785 case WC_HASH_TYPE_SHA:
wolfSSL 16:8e0d178b1d1e 786 #ifndef NO_SHA
wolfSSL 16:8e0d178b1d1e 787 wc_ShaFree(&hash->sha);
wolfSSL 16:8e0d178b1d1e 788 ret = 0;
wolfSSL 16:8e0d178b1d1e 789 #endif
wolfSSL 16:8e0d178b1d1e 790 break;
wolfSSL 16:8e0d178b1d1e 791 case WC_HASH_TYPE_SHA224:
wolfSSL 16:8e0d178b1d1e 792 #ifdef WOLFSSL_SHA224
wolfSSL 16:8e0d178b1d1e 793 wc_Sha224Free(&hash->sha224);
wolfSSL 16:8e0d178b1d1e 794 ret = 0;
wolfSSL 16:8e0d178b1d1e 795 #endif
wolfSSL 16:8e0d178b1d1e 796 break;
wolfSSL 16:8e0d178b1d1e 797 case WC_HASH_TYPE_SHA256:
wolfSSL 16:8e0d178b1d1e 798 #ifndef NO_SHA256
wolfSSL 16:8e0d178b1d1e 799 wc_Sha256Free(&hash->sha256);
wolfSSL 16:8e0d178b1d1e 800 ret = 0;
wolfSSL 16:8e0d178b1d1e 801 #endif
wolfSSL 16:8e0d178b1d1e 802 break;
wolfSSL 16:8e0d178b1d1e 803 case WC_HASH_TYPE_SHA384:
wolfSSL 16:8e0d178b1d1e 804 #ifdef WOLFSSL_SHA384
wolfSSL 16:8e0d178b1d1e 805 wc_Sha384Free(&hash->sha384);
wolfSSL 16:8e0d178b1d1e 806 ret = 0;
wolfSSL 16:8e0d178b1d1e 807 #endif
wolfSSL 16:8e0d178b1d1e 808 break;
wolfSSL 16:8e0d178b1d1e 809 case WC_HASH_TYPE_SHA512:
wolfSSL 16:8e0d178b1d1e 810 #ifdef WOLFSSL_SHA512
wolfSSL 16:8e0d178b1d1e 811 wc_Sha512Free(&hash->sha512);
wolfSSL 16:8e0d178b1d1e 812 ret = 0;
wolfSSL 16:8e0d178b1d1e 813 #endif
wolfSSL 16:8e0d178b1d1e 814 break;
wolfSSL 16:8e0d178b1d1e 815
wolfSSL 15:117db924cf7c 816 case WC_HASH_TYPE_SHA3_224:
wolfSSL 16:8e0d178b1d1e 817 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
wolfSSL 16:8e0d178b1d1e 818 wc_Sha3_224_Free(&hash->sha3);
wolfSSL 16:8e0d178b1d1e 819 ret = 0;
wolfSSL 16:8e0d178b1d1e 820 #endif
wolfSSL 16:8e0d178b1d1e 821 break;
wolfSSL 15:117db924cf7c 822 case WC_HASH_TYPE_SHA3_256:
wolfSSL 16:8e0d178b1d1e 823 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
wolfSSL 16:8e0d178b1d1e 824 wc_Sha3_256_Free(&hash->sha3);
wolfSSL 16:8e0d178b1d1e 825 ret = 0;
wolfSSL 16:8e0d178b1d1e 826 #endif
wolfSSL 16:8e0d178b1d1e 827 break;
wolfSSL 15:117db924cf7c 828 case WC_HASH_TYPE_SHA3_384:
wolfSSL 16:8e0d178b1d1e 829 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
wolfSSL 16:8e0d178b1d1e 830 wc_Sha3_384_Free(&hash->sha3);
wolfSSL 16:8e0d178b1d1e 831 ret = 0;
wolfSSL 16:8e0d178b1d1e 832 #endif
wolfSSL 16:8e0d178b1d1e 833 break;
wolfSSL 15:117db924cf7c 834 case WC_HASH_TYPE_SHA3_512:
wolfSSL 16:8e0d178b1d1e 835 #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
wolfSSL 16:8e0d178b1d1e 836 wc_Sha3_512_Free(&hash->sha3);
wolfSSL 16:8e0d178b1d1e 837 ret = 0;
wolfSSL 16:8e0d178b1d1e 838 #endif
wolfSSL 16:8e0d178b1d1e 839 break;
wolfSSL 16:8e0d178b1d1e 840
wolfSSL 16:8e0d178b1d1e 841 /* not supported */
wolfSSL 16:8e0d178b1d1e 842 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 16:8e0d178b1d1e 843 case WC_HASH_TYPE_MD2:
wolfSSL 16:8e0d178b1d1e 844 case WC_HASH_TYPE_MD4:
wolfSSL 15:117db924cf7c 845 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 16:8e0d178b1d1e 846 case WC_HASH_TYPE_BLAKE2S:
wolfSSL 15:117db924cf7c 847 case WC_HASH_TYPE_NONE:
wolfSSL 15:117db924cf7c 848 default:
wolfSSL 15:117db924cf7c 849 ret = BAD_FUNC_ARG;
wolfSSL 15:117db924cf7c 850 };
wolfSSL 15:117db924cf7c 851
wolfSSL 15:117db924cf7c 852 return ret;
wolfSSL 15:117db924cf7c 853 }
wolfSSL 15:117db924cf7c 854
wolfSSL 16:8e0d178b1d1e 855 #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
wolfSSL 16:8e0d178b1d1e 856 int wc_HashSetFlags(wc_HashAlg* hash, enum wc_HashType type, word32 flags)
wolfSSL 16:8e0d178b1d1e 857 {
wolfSSL 16:8e0d178b1d1e 858 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 16:8e0d178b1d1e 859
wolfSSL 16:8e0d178b1d1e 860 if (hash == NULL)
wolfSSL 16:8e0d178b1d1e 861 return BAD_FUNC_ARG;
wolfSSL 16:8e0d178b1d1e 862
wolfSSL 16:8e0d178b1d1e 863 switch (type) {
wolfSSL 16:8e0d178b1d1e 864 case WC_HASH_TYPE_MD5:
wolfSSL 16:8e0d178b1d1e 865 #ifndef NO_MD5
wolfSSL 16:8e0d178b1d1e 866 ret = wc_Md5SetFlags(&hash->md5, flags);
wolfSSL 16:8e0d178b1d1e 867 #endif
wolfSSL 16:8e0d178b1d1e 868 break;
wolfSSL 16:8e0d178b1d1e 869 case WC_HASH_TYPE_SHA:
wolfSSL 16:8e0d178b1d1e 870 #ifndef NO_SHA
wolfSSL 16:8e0d178b1d1e 871 ret = wc_ShaSetFlags(&hash->sha, flags);
wolfSSL 16:8e0d178b1d1e 872 #endif
wolfSSL 16:8e0d178b1d1e 873 break;
wolfSSL 16:8e0d178b1d1e 874 case WC_HASH_TYPE_SHA224:
wolfSSL 16:8e0d178b1d1e 875 #ifdef WOLFSSL_SHA224
wolfSSL 16:8e0d178b1d1e 876 ret = wc_Sha224SetFlags(&hash->sha224, flags);
wolfSSL 16:8e0d178b1d1e 877 #endif
wolfSSL 16:8e0d178b1d1e 878 break;
wolfSSL 16:8e0d178b1d1e 879 case WC_HASH_TYPE_SHA256:
wolfSSL 16:8e0d178b1d1e 880 #ifndef NO_SHA256
wolfSSL 16:8e0d178b1d1e 881 ret = wc_Sha256SetFlags(&hash->sha256, flags);
wolfSSL 16:8e0d178b1d1e 882 #endif
wolfSSL 16:8e0d178b1d1e 883 break;
wolfSSL 16:8e0d178b1d1e 884 case WC_HASH_TYPE_SHA384:
wolfSSL 16:8e0d178b1d1e 885 #ifdef WOLFSSL_SHA384
wolfSSL 16:8e0d178b1d1e 886 ret = wc_Sha384SetFlags(&hash->sha384, flags);
wolfSSL 16:8e0d178b1d1e 887 #endif
wolfSSL 16:8e0d178b1d1e 888 break;
wolfSSL 16:8e0d178b1d1e 889 case WC_HASH_TYPE_SHA512:
wolfSSL 16:8e0d178b1d1e 890 #ifdef WOLFSSL_SHA512
wolfSSL 16:8e0d178b1d1e 891 ret = wc_Sha512SetFlags(&hash->sha512, flags);
wolfSSL 16:8e0d178b1d1e 892 #endif
wolfSSL 16:8e0d178b1d1e 893 break;
wolfSSL 16:8e0d178b1d1e 894
wolfSSL 16:8e0d178b1d1e 895 case WC_HASH_TYPE_SHA3_224:
wolfSSL 16:8e0d178b1d1e 896 case WC_HASH_TYPE_SHA3_256:
wolfSSL 16:8e0d178b1d1e 897 case WC_HASH_TYPE_SHA3_384:
wolfSSL 16:8e0d178b1d1e 898 case WC_HASH_TYPE_SHA3_512:
wolfSSL 16:8e0d178b1d1e 899 #ifdef WOLFSSL_SHA3
wolfSSL 16:8e0d178b1d1e 900 ret = wc_Sha3_SetFlags(&hash->sha3, flags);
wolfSSL 16:8e0d178b1d1e 901 #endif
wolfSSL 16:8e0d178b1d1e 902 break;
wolfSSL 16:8e0d178b1d1e 903
wolfSSL 16:8e0d178b1d1e 904 /* not supported */
wolfSSL 16:8e0d178b1d1e 905 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 16:8e0d178b1d1e 906 case WC_HASH_TYPE_MD2:
wolfSSL 16:8e0d178b1d1e 907 case WC_HASH_TYPE_MD4:
wolfSSL 16:8e0d178b1d1e 908 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 16:8e0d178b1d1e 909 case WC_HASH_TYPE_BLAKE2S:
wolfSSL 16:8e0d178b1d1e 910 case WC_HASH_TYPE_NONE:
wolfSSL 16:8e0d178b1d1e 911 default:
wolfSSL 16:8e0d178b1d1e 912 ret = BAD_FUNC_ARG;
wolfSSL 16:8e0d178b1d1e 913 };
wolfSSL 16:8e0d178b1d1e 914
wolfSSL 16:8e0d178b1d1e 915 return ret;
wolfSSL 16:8e0d178b1d1e 916 }
wolfSSL 16:8e0d178b1d1e 917 int wc_HashGetFlags(wc_HashAlg* hash, enum wc_HashType type, word32* flags)
wolfSSL 16:8e0d178b1d1e 918 {
wolfSSL 16:8e0d178b1d1e 919 int ret = HASH_TYPE_E; /* Default to hash type error */
wolfSSL 16:8e0d178b1d1e 920
wolfSSL 16:8e0d178b1d1e 921 if (hash == NULL)
wolfSSL 16:8e0d178b1d1e 922 return BAD_FUNC_ARG;
wolfSSL 16:8e0d178b1d1e 923
wolfSSL 16:8e0d178b1d1e 924 switch (type) {
wolfSSL 16:8e0d178b1d1e 925 case WC_HASH_TYPE_MD5:
wolfSSL 16:8e0d178b1d1e 926 #ifndef NO_MD5
wolfSSL 16:8e0d178b1d1e 927 ret = wc_Md5GetFlags(&hash->md5, flags);
wolfSSL 16:8e0d178b1d1e 928 #endif
wolfSSL 16:8e0d178b1d1e 929 break;
wolfSSL 16:8e0d178b1d1e 930 case WC_HASH_TYPE_SHA:
wolfSSL 16:8e0d178b1d1e 931 #ifndef NO_SHA
wolfSSL 16:8e0d178b1d1e 932 ret = wc_ShaGetFlags(&hash->sha, flags);
wolfSSL 16:8e0d178b1d1e 933 #endif
wolfSSL 16:8e0d178b1d1e 934 break;
wolfSSL 16:8e0d178b1d1e 935 case WC_HASH_TYPE_SHA224:
wolfSSL 16:8e0d178b1d1e 936 #ifdef WOLFSSL_SHA224
wolfSSL 16:8e0d178b1d1e 937 ret = wc_Sha224GetFlags(&hash->sha224, flags);
wolfSSL 16:8e0d178b1d1e 938 #endif
wolfSSL 16:8e0d178b1d1e 939 break;
wolfSSL 16:8e0d178b1d1e 940 case WC_HASH_TYPE_SHA256:
wolfSSL 16:8e0d178b1d1e 941 #ifndef NO_SHA256
wolfSSL 16:8e0d178b1d1e 942 ret = wc_Sha256GetFlags(&hash->sha256, flags);
wolfSSL 16:8e0d178b1d1e 943 #endif
wolfSSL 16:8e0d178b1d1e 944 break;
wolfSSL 16:8e0d178b1d1e 945 case WC_HASH_TYPE_SHA384:
wolfSSL 16:8e0d178b1d1e 946 #ifdef WOLFSSL_SHA384
wolfSSL 16:8e0d178b1d1e 947 ret = wc_Sha384GetFlags(&hash->sha384, flags);
wolfSSL 16:8e0d178b1d1e 948 #endif
wolfSSL 16:8e0d178b1d1e 949 break;
wolfSSL 16:8e0d178b1d1e 950 case WC_HASH_TYPE_SHA512:
wolfSSL 16:8e0d178b1d1e 951 #ifdef WOLFSSL_SHA512
wolfSSL 16:8e0d178b1d1e 952 ret = wc_Sha512GetFlags(&hash->sha512, flags);
wolfSSL 16:8e0d178b1d1e 953 #endif
wolfSSL 16:8e0d178b1d1e 954 break;
wolfSSL 16:8e0d178b1d1e 955
wolfSSL 16:8e0d178b1d1e 956 case WC_HASH_TYPE_SHA3_224:
wolfSSL 16:8e0d178b1d1e 957 case WC_HASH_TYPE_SHA3_256:
wolfSSL 16:8e0d178b1d1e 958 case WC_HASH_TYPE_SHA3_384:
wolfSSL 16:8e0d178b1d1e 959 case WC_HASH_TYPE_SHA3_512:
wolfSSL 16:8e0d178b1d1e 960 #ifdef WOLFSSL_SHA3
wolfSSL 16:8e0d178b1d1e 961 ret = wc_Sha3_GetFlags(&hash->sha3, flags);
wolfSSL 16:8e0d178b1d1e 962 #endif
wolfSSL 16:8e0d178b1d1e 963 break;
wolfSSL 16:8e0d178b1d1e 964
wolfSSL 16:8e0d178b1d1e 965 /* not supported */
wolfSSL 16:8e0d178b1d1e 966 case WC_HASH_TYPE_MD5_SHA:
wolfSSL 16:8e0d178b1d1e 967 case WC_HASH_TYPE_MD2:
wolfSSL 16:8e0d178b1d1e 968 case WC_HASH_TYPE_MD4:
wolfSSL 16:8e0d178b1d1e 969 case WC_HASH_TYPE_BLAKE2B:
wolfSSL 16:8e0d178b1d1e 970 case WC_HASH_TYPE_BLAKE2S:
wolfSSL 16:8e0d178b1d1e 971 case WC_HASH_TYPE_NONE:
wolfSSL 16:8e0d178b1d1e 972 default:
wolfSSL 16:8e0d178b1d1e 973 ret = BAD_FUNC_ARG;
wolfSSL 16:8e0d178b1d1e 974 };
wolfSSL 16:8e0d178b1d1e 975
wolfSSL 16:8e0d178b1d1e 976 return ret;
wolfSSL 16:8e0d178b1d1e 977 }
wolfSSL 16:8e0d178b1d1e 978 #endif
wolfSSL 16:8e0d178b1d1e 979
wolfSSL 15:117db924cf7c 980
wolfSSL 15:117db924cf7c 981 #if !defined(WOLFSSL_TI_HASH)
wolfSSL 15:117db924cf7c 982
wolfSSL 15:117db924cf7c 983 #if !defined(NO_MD5)
wolfSSL 15:117db924cf7c 984 int wc_Md5Hash(const byte* data, word32 len, byte* hash)
wolfSSL 15:117db924cf7c 985 {
wolfSSL 15:117db924cf7c 986 int ret;
wolfSSL 15:117db924cf7c 987 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 988 wc_Md5* md5;
wolfSSL 15:117db924cf7c 989 #else
wolfSSL 15:117db924cf7c 990 wc_Md5 md5[1];
wolfSSL 15:117db924cf7c 991 #endif
wolfSSL 15:117db924cf7c 992
wolfSSL 15:117db924cf7c 993 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 994 md5 = (wc_Md5*)XMALLOC(sizeof(wc_Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 995 if (md5 == NULL)
wolfSSL 15:117db924cf7c 996 return MEMORY_E;
wolfSSL 15:117db924cf7c 997 #endif
wolfSSL 15:117db924cf7c 998
wolfSSL 16:8e0d178b1d1e 999 if ((ret = wc_InitMd5(md5)) != 0) {
wolfSSL 16:8e0d178b1d1e 1000 WOLFSSL_MSG("InitMd5 failed");
wolfSSL 16:8e0d178b1d1e 1001 }
wolfSSL 16:8e0d178b1d1e 1002 else {
wolfSSL 16:8e0d178b1d1e 1003 if ((ret = wc_Md5Update(md5, data, len)) != 0) {
wolfSSL 16:8e0d178b1d1e 1004 WOLFSSL_MSG("Md5Update failed");
wolfSSL 15:117db924cf7c 1005 }
wolfSSL 16:8e0d178b1d1e 1006 else if ((ret = wc_Md5Final(md5, hash)) != 0) {
wolfSSL 16:8e0d178b1d1e 1007 WOLFSSL_MSG("Md5Final failed");
wolfSSL 16:8e0d178b1d1e 1008 }
wolfSSL 16:8e0d178b1d1e 1009 wc_Md5Free(md5);
wolfSSL 15:117db924cf7c 1010 }
wolfSSL 15:117db924cf7c 1011
wolfSSL 15:117db924cf7c 1012 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1013 XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1014 #endif
wolfSSL 15:117db924cf7c 1015
wolfSSL 15:117db924cf7c 1016 return ret;
wolfSSL 15:117db924cf7c 1017 }
wolfSSL 15:117db924cf7c 1018 #endif /* !NO_MD5 */
wolfSSL 15:117db924cf7c 1019
wolfSSL 15:117db924cf7c 1020 #if !defined(NO_SHA)
wolfSSL 15:117db924cf7c 1021 int wc_ShaHash(const byte* data, word32 len, byte* hash)
wolfSSL 15:117db924cf7c 1022 {
wolfSSL 15:117db924cf7c 1023 int ret = 0;
wolfSSL 15:117db924cf7c 1024 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1025 wc_Sha* sha;
wolfSSL 15:117db924cf7c 1026 #else
wolfSSL 15:117db924cf7c 1027 wc_Sha sha[1];
wolfSSL 15:117db924cf7c 1028 #endif
wolfSSL 15:117db924cf7c 1029
wolfSSL 15:117db924cf7c 1030 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1031 sha = (wc_Sha*)XMALLOC(sizeof(wc_Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1032 if (sha == NULL)
wolfSSL 15:117db924cf7c 1033 return MEMORY_E;
wolfSSL 15:117db924cf7c 1034 #endif
wolfSSL 15:117db924cf7c 1035
wolfSSL 15:117db924cf7c 1036 if ((ret = wc_InitSha(sha)) != 0) {
wolfSSL 16:8e0d178b1d1e 1037 WOLFSSL_MSG("InitSha failed");
wolfSSL 15:117db924cf7c 1038 }
wolfSSL 15:117db924cf7c 1039 else {
wolfSSL 16:8e0d178b1d1e 1040 if ((ret = wc_ShaUpdate(sha, data, len)) != 0) {
wolfSSL 16:8e0d178b1d1e 1041 WOLFSSL_MSG("ShaUpdate failed");
wolfSSL 16:8e0d178b1d1e 1042 }
wolfSSL 16:8e0d178b1d1e 1043 else if ((ret = wc_ShaFinal(sha, hash)) != 0) {
wolfSSL 16:8e0d178b1d1e 1044 WOLFSSL_MSG("ShaFinal failed");
wolfSSL 16:8e0d178b1d1e 1045 }
wolfSSL 16:8e0d178b1d1e 1046 wc_ShaFree(sha);
wolfSSL 15:117db924cf7c 1047 }
wolfSSL 15:117db924cf7c 1048
wolfSSL 15:117db924cf7c 1049 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1050 XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1051 #endif
wolfSSL 15:117db924cf7c 1052
wolfSSL 15:117db924cf7c 1053 return ret;
wolfSSL 15:117db924cf7c 1054 }
wolfSSL 15:117db924cf7c 1055 #endif /* !NO_SHA */
wolfSSL 15:117db924cf7c 1056
wolfSSL 15:117db924cf7c 1057 #if defined(WOLFSSL_SHA224)
wolfSSL 15:117db924cf7c 1058 int wc_Sha224Hash(const byte* data, word32 len, byte* hash)
wolfSSL 15:117db924cf7c 1059 {
wolfSSL 15:117db924cf7c 1060 int ret = 0;
wolfSSL 15:117db924cf7c 1061 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1062 wc_Sha224* sha224;
wolfSSL 15:117db924cf7c 1063 #else
wolfSSL 15:117db924cf7c 1064 wc_Sha224 sha224[1];
wolfSSL 15:117db924cf7c 1065 #endif
wolfSSL 15:117db924cf7c 1066
wolfSSL 15:117db924cf7c 1067 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1068 sha224 = (wc_Sha224*)XMALLOC(sizeof(wc_Sha224), NULL,
wolfSSL 15:117db924cf7c 1069 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1070 if (sha224 == NULL)
wolfSSL 15:117db924cf7c 1071 return MEMORY_E;
wolfSSL 15:117db924cf7c 1072 #endif
wolfSSL 15:117db924cf7c 1073
wolfSSL 15:117db924cf7c 1074 if ((ret = wc_InitSha224(sha224)) != 0) {
wolfSSL 15:117db924cf7c 1075 WOLFSSL_MSG("InitSha224 failed");
wolfSSL 15:117db924cf7c 1076 }
wolfSSL 15:117db924cf7c 1077 else {
wolfSSL 15:117db924cf7c 1078 if ((ret = wc_Sha224Update(sha224, data, len)) != 0) {
wolfSSL 15:117db924cf7c 1079 WOLFSSL_MSG("Sha224Update failed");
wolfSSL 15:117db924cf7c 1080 }
wolfSSL 15:117db924cf7c 1081 else if ((ret = wc_Sha224Final(sha224, hash)) != 0) {
wolfSSL 15:117db924cf7c 1082 WOLFSSL_MSG("Sha224Final failed");
wolfSSL 15:117db924cf7c 1083 }
wolfSSL 15:117db924cf7c 1084 wc_Sha224Free(sha224);
wolfSSL 15:117db924cf7c 1085 }
wolfSSL 15:117db924cf7c 1086
wolfSSL 15:117db924cf7c 1087 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1088 XFREE(sha224, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1089 #endif
wolfSSL 15:117db924cf7c 1090
wolfSSL 15:117db924cf7c 1091 return ret;
wolfSSL 15:117db924cf7c 1092 }
wolfSSL 15:117db924cf7c 1093 #endif /* WOLFSSL_SHA224 */
wolfSSL 15:117db924cf7c 1094
wolfSSL 15:117db924cf7c 1095 #if !defined(NO_SHA256)
wolfSSL 15:117db924cf7c 1096 int wc_Sha256Hash(const byte* data, word32 len, byte* hash)
wolfSSL 15:117db924cf7c 1097 {
wolfSSL 15:117db924cf7c 1098 int ret = 0;
wolfSSL 15:117db924cf7c 1099 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1100 wc_Sha256* sha256;
wolfSSL 15:117db924cf7c 1101 #else
wolfSSL 15:117db924cf7c 1102 wc_Sha256 sha256[1];
wolfSSL 15:117db924cf7c 1103 #endif
wolfSSL 15:117db924cf7c 1104
wolfSSL 15:117db924cf7c 1105 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1106 sha256 = (wc_Sha256*)XMALLOC(sizeof(wc_Sha256), NULL,
wolfSSL 15:117db924cf7c 1107 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1108 if (sha256 == NULL)
wolfSSL 15:117db924cf7c 1109 return MEMORY_E;
wolfSSL 15:117db924cf7c 1110 #endif
wolfSSL 15:117db924cf7c 1111
wolfSSL 15:117db924cf7c 1112 if ((ret = wc_InitSha256(sha256)) != 0) {
wolfSSL 15:117db924cf7c 1113 WOLFSSL_MSG("InitSha256 failed");
wolfSSL 15:117db924cf7c 1114 }
wolfSSL 15:117db924cf7c 1115 else {
wolfSSL 15:117db924cf7c 1116 if ((ret = wc_Sha256Update(sha256, data, len)) != 0) {
wolfSSL 15:117db924cf7c 1117 WOLFSSL_MSG("Sha256Update failed");
wolfSSL 15:117db924cf7c 1118 }
wolfSSL 15:117db924cf7c 1119 else if ((ret = wc_Sha256Final(sha256, hash)) != 0) {
wolfSSL 15:117db924cf7c 1120 WOLFSSL_MSG("Sha256Final failed");
wolfSSL 15:117db924cf7c 1121 }
wolfSSL 15:117db924cf7c 1122 wc_Sha256Free(sha256);
wolfSSL 15:117db924cf7c 1123 }
wolfSSL 15:117db924cf7c 1124
wolfSSL 15:117db924cf7c 1125
wolfSSL 15:117db924cf7c 1126 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1127 XFREE(sha256, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1128 #endif
wolfSSL 15:117db924cf7c 1129
wolfSSL 15:117db924cf7c 1130 return ret;
wolfSSL 15:117db924cf7c 1131 }
wolfSSL 15:117db924cf7c 1132 #endif /* !NO_SHA256 */
wolfSSL 15:117db924cf7c 1133
wolfSSL 15:117db924cf7c 1134 #endif /* !defined(WOLFSSL_TI_HASH) */
wolfSSL 15:117db924cf7c 1135
wolfSSL 15:117db924cf7c 1136
wolfSSL 15:117db924cf7c 1137 #if defined(WOLFSSL_SHA512)
wolfSSL 15:117db924cf7c 1138 int wc_Sha512Hash(const byte* data, word32 len, byte* hash)
wolfSSL 15:117db924cf7c 1139 {
wolfSSL 15:117db924cf7c 1140 int ret = 0;
wolfSSL 15:117db924cf7c 1141 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1142 wc_Sha512* sha512;
wolfSSL 15:117db924cf7c 1143 #else
wolfSSL 15:117db924cf7c 1144 wc_Sha512 sha512[1];
wolfSSL 15:117db924cf7c 1145 #endif
wolfSSL 15:117db924cf7c 1146
wolfSSL 15:117db924cf7c 1147 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1148 sha512 = (wc_Sha512*)XMALLOC(sizeof(wc_Sha512), NULL,
wolfSSL 15:117db924cf7c 1149 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1150 if (sha512 == NULL)
wolfSSL 15:117db924cf7c 1151 return MEMORY_E;
wolfSSL 15:117db924cf7c 1152 #endif
wolfSSL 15:117db924cf7c 1153
wolfSSL 15:117db924cf7c 1154 if ((ret = wc_InitSha512(sha512)) != 0) {
wolfSSL 15:117db924cf7c 1155 WOLFSSL_MSG("InitSha512 failed");
wolfSSL 15:117db924cf7c 1156 }
wolfSSL 15:117db924cf7c 1157 else {
wolfSSL 15:117db924cf7c 1158 if ((ret = wc_Sha512Update(sha512, data, len)) != 0) {
wolfSSL 15:117db924cf7c 1159 WOLFSSL_MSG("Sha512Update failed");
wolfSSL 15:117db924cf7c 1160 }
wolfSSL 15:117db924cf7c 1161 else if ((ret = wc_Sha512Final(sha512, hash)) != 0) {
wolfSSL 15:117db924cf7c 1162 WOLFSSL_MSG("Sha512Final failed");
wolfSSL 15:117db924cf7c 1163 }
wolfSSL 15:117db924cf7c 1164 wc_Sha512Free(sha512);
wolfSSL 15:117db924cf7c 1165 }
wolfSSL 15:117db924cf7c 1166
wolfSSL 15:117db924cf7c 1167 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1168 XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1169 #endif
wolfSSL 15:117db924cf7c 1170
wolfSSL 15:117db924cf7c 1171 return ret;
wolfSSL 15:117db924cf7c 1172 }
wolfSSL 15:117db924cf7c 1173 #endif /* WOLFSSL_SHA512 */
wolfSSL 15:117db924cf7c 1174
wolfSSL 15:117db924cf7c 1175 #if defined(WOLFSSL_SHA384)
wolfSSL 15:117db924cf7c 1176 int wc_Sha384Hash(const byte* data, word32 len, byte* hash)
wolfSSL 15:117db924cf7c 1177 {
wolfSSL 15:117db924cf7c 1178 int ret = 0;
wolfSSL 15:117db924cf7c 1179 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1180 wc_Sha384* sha384;
wolfSSL 15:117db924cf7c 1181 #else
wolfSSL 15:117db924cf7c 1182 wc_Sha384 sha384[1];
wolfSSL 15:117db924cf7c 1183 #endif
wolfSSL 15:117db924cf7c 1184
wolfSSL 15:117db924cf7c 1185 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1186 sha384 = (wc_Sha384*)XMALLOC(sizeof(wc_Sha384), NULL,
wolfSSL 15:117db924cf7c 1187 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1188 if (sha384 == NULL)
wolfSSL 15:117db924cf7c 1189 return MEMORY_E;
wolfSSL 15:117db924cf7c 1190 #endif
wolfSSL 15:117db924cf7c 1191
wolfSSL 15:117db924cf7c 1192 if ((ret = wc_InitSha384(sha384)) != 0) {
wolfSSL 15:117db924cf7c 1193 WOLFSSL_MSG("InitSha384 failed");
wolfSSL 15:117db924cf7c 1194 }
wolfSSL 15:117db924cf7c 1195 else {
wolfSSL 15:117db924cf7c 1196 if ((ret = wc_Sha384Update(sha384, data, len)) != 0) {
wolfSSL 15:117db924cf7c 1197 WOLFSSL_MSG("Sha384Update failed");
wolfSSL 15:117db924cf7c 1198 }
wolfSSL 15:117db924cf7c 1199 else if ((ret = wc_Sha384Final(sha384, hash)) != 0) {
wolfSSL 15:117db924cf7c 1200 WOLFSSL_MSG("Sha384Final failed");
wolfSSL 15:117db924cf7c 1201 }
wolfSSL 15:117db924cf7c 1202 wc_Sha384Free(sha384);
wolfSSL 15:117db924cf7c 1203 }
wolfSSL 15:117db924cf7c 1204
wolfSSL 15:117db924cf7c 1205 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 15:117db924cf7c 1206 XFREE(sha384, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 15:117db924cf7c 1207 #endif
wolfSSL 15:117db924cf7c 1208
wolfSSL 15:117db924cf7c 1209 return ret;
wolfSSL 15:117db924cf7c 1210 }
wolfSSL 15:117db924cf7c 1211 #endif /* WOLFSSL_SHA384 */
wolfSSL 16:8e0d178b1d1e 1212
wolfSSL 16:8e0d178b1d1e 1213 #if defined(WOLFSSL_SHA3)
wolfSSL 16:8e0d178b1d1e 1214 #if !defined(WOLFSSL_NOSHA3_224)
wolfSSL 16:8e0d178b1d1e 1215 int wc_Sha3_224Hash(const byte* data, word32 len, byte* hash)
wolfSSL 16:8e0d178b1d1e 1216 {
wolfSSL 16:8e0d178b1d1e 1217 int ret = 0;
wolfSSL 16:8e0d178b1d1e 1218 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1219 wc_Sha3* sha3;
wolfSSL 16:8e0d178b1d1e 1220 #else
wolfSSL 16:8e0d178b1d1e 1221 wc_Sha3 sha3[1];
wolfSSL 16:8e0d178b1d1e 1222 #endif
wolfSSL 16:8e0d178b1d1e 1223
wolfSSL 16:8e0d178b1d1e 1224 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1225 sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL,
wolfSSL 16:8e0d178b1d1e 1226 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 16:8e0d178b1d1e 1227 if (sha3 == NULL)
wolfSSL 16:8e0d178b1d1e 1228 return MEMORY_E;
wolfSSL 16:8e0d178b1d1e 1229 #endif
wolfSSL 16:8e0d178b1d1e 1230
wolfSSL 16:8e0d178b1d1e 1231 if ((ret = wc_InitSha3_224(sha3, NULL, INVALID_DEVID)) != 0) {
wolfSSL 16:8e0d178b1d1e 1232 WOLFSSL_MSG("InitSha3_224 failed");
wolfSSL 16:8e0d178b1d1e 1233 }
wolfSSL 16:8e0d178b1d1e 1234 else {
wolfSSL 16:8e0d178b1d1e 1235 if ((ret = wc_Sha3_224_Update(sha3, data, len)) != 0) {
wolfSSL 16:8e0d178b1d1e 1236 WOLFSSL_MSG("Sha3_224_Update failed");
wolfSSL 16:8e0d178b1d1e 1237 }
wolfSSL 16:8e0d178b1d1e 1238 else if ((ret = wc_Sha3_224_Final(sha3, hash)) != 0) {
wolfSSL 16:8e0d178b1d1e 1239 WOLFSSL_MSG("Sha3_224_Final failed");
wolfSSL 16:8e0d178b1d1e 1240 }
wolfSSL 16:8e0d178b1d1e 1241 wc_Sha3_224_Free(sha3);
wolfSSL 16:8e0d178b1d1e 1242 }
wolfSSL 16:8e0d178b1d1e 1243
wolfSSL 16:8e0d178b1d1e 1244 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1245 XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 16:8e0d178b1d1e 1246 #endif
wolfSSL 16:8e0d178b1d1e 1247
wolfSSL 16:8e0d178b1d1e 1248 return ret;
wolfSSL 16:8e0d178b1d1e 1249 }
wolfSSL 16:8e0d178b1d1e 1250 #endif /* !WOLFSSL_NOSHA3_224 */
wolfSSL 16:8e0d178b1d1e 1251
wolfSSL 16:8e0d178b1d1e 1252 #if !defined(WOLFSSL_NOSHA3_256)
wolfSSL 16:8e0d178b1d1e 1253 int wc_Sha3_256Hash(const byte* data, word32 len, byte* hash)
wolfSSL 16:8e0d178b1d1e 1254 {
wolfSSL 16:8e0d178b1d1e 1255 int ret = 0;
wolfSSL 16:8e0d178b1d1e 1256 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1257 wc_Sha3* sha3;
wolfSSL 16:8e0d178b1d1e 1258 #else
wolfSSL 16:8e0d178b1d1e 1259 wc_Sha3 sha3[1];
wolfSSL 16:8e0d178b1d1e 1260 #endif
wolfSSL 16:8e0d178b1d1e 1261
wolfSSL 16:8e0d178b1d1e 1262 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1263 sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL,
wolfSSL 16:8e0d178b1d1e 1264 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 16:8e0d178b1d1e 1265 if (sha3 == NULL)
wolfSSL 16:8e0d178b1d1e 1266 return MEMORY_E;
wolfSSL 16:8e0d178b1d1e 1267 #endif
wolfSSL 16:8e0d178b1d1e 1268
wolfSSL 16:8e0d178b1d1e 1269 if ((ret = wc_InitSha3_256(sha3, NULL, INVALID_DEVID)) != 0) {
wolfSSL 16:8e0d178b1d1e 1270 WOLFSSL_MSG("InitSha3_256 failed");
wolfSSL 16:8e0d178b1d1e 1271 }
wolfSSL 16:8e0d178b1d1e 1272 else {
wolfSSL 16:8e0d178b1d1e 1273 if ((ret = wc_Sha3_256_Update(sha3, data, len)) != 0) {
wolfSSL 16:8e0d178b1d1e 1274 WOLFSSL_MSG("Sha3_256_Update failed");
wolfSSL 16:8e0d178b1d1e 1275 }
wolfSSL 16:8e0d178b1d1e 1276 else if ((ret = wc_Sha3_256_Final(sha3, hash)) != 0) {
wolfSSL 16:8e0d178b1d1e 1277 WOLFSSL_MSG("Sha3_256_Final failed");
wolfSSL 16:8e0d178b1d1e 1278 }
wolfSSL 16:8e0d178b1d1e 1279 wc_Sha3_256_Free(sha3);
wolfSSL 16:8e0d178b1d1e 1280 }
wolfSSL 16:8e0d178b1d1e 1281
wolfSSL 16:8e0d178b1d1e 1282 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1283 XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 16:8e0d178b1d1e 1284 #endif
wolfSSL 16:8e0d178b1d1e 1285
wolfSSL 16:8e0d178b1d1e 1286 return ret;
wolfSSL 16:8e0d178b1d1e 1287 }
wolfSSL 16:8e0d178b1d1e 1288 #endif /* !WOLFSSL_NOSHA3_256 */
wolfSSL 16:8e0d178b1d1e 1289
wolfSSL 16:8e0d178b1d1e 1290 #if !defined(WOLFSSL_NOSHA3_384)
wolfSSL 16:8e0d178b1d1e 1291 int wc_Sha3_384Hash(const byte* data, word32 len, byte* hash)
wolfSSL 16:8e0d178b1d1e 1292 {
wolfSSL 16:8e0d178b1d1e 1293 int ret = 0;
wolfSSL 16:8e0d178b1d1e 1294 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1295 wc_Sha3* sha3;
wolfSSL 16:8e0d178b1d1e 1296 #else
wolfSSL 16:8e0d178b1d1e 1297 wc_Sha3 sha3[1];
wolfSSL 16:8e0d178b1d1e 1298 #endif
wolfSSL 16:8e0d178b1d1e 1299
wolfSSL 16:8e0d178b1d1e 1300 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1301 sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL,
wolfSSL 16:8e0d178b1d1e 1302 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 16:8e0d178b1d1e 1303 if (sha3 == NULL)
wolfSSL 16:8e0d178b1d1e 1304 return MEMORY_E;
wolfSSL 16:8e0d178b1d1e 1305 #endif
wolfSSL 16:8e0d178b1d1e 1306
wolfSSL 16:8e0d178b1d1e 1307 if ((ret = wc_InitSha3_384(sha3, NULL, INVALID_DEVID)) != 0) {
wolfSSL 16:8e0d178b1d1e 1308 WOLFSSL_MSG("InitSha3_384 failed");
wolfSSL 16:8e0d178b1d1e 1309 }
wolfSSL 16:8e0d178b1d1e 1310 else {
wolfSSL 16:8e0d178b1d1e 1311 if ((ret = wc_Sha3_384_Update(sha3, data, len)) != 0) {
wolfSSL 16:8e0d178b1d1e 1312 WOLFSSL_MSG("Sha3_384_Update failed");
wolfSSL 16:8e0d178b1d1e 1313 }
wolfSSL 16:8e0d178b1d1e 1314 else if ((ret = wc_Sha3_384_Final(sha3, hash)) != 0) {
wolfSSL 16:8e0d178b1d1e 1315 WOLFSSL_MSG("Sha3_384_Final failed");
wolfSSL 16:8e0d178b1d1e 1316 }
wolfSSL 16:8e0d178b1d1e 1317 wc_Sha3_384_Free(sha3);
wolfSSL 16:8e0d178b1d1e 1318 }
wolfSSL 16:8e0d178b1d1e 1319
wolfSSL 16:8e0d178b1d1e 1320 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1321 XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 16:8e0d178b1d1e 1322 #endif
wolfSSL 16:8e0d178b1d1e 1323
wolfSSL 16:8e0d178b1d1e 1324 return ret;
wolfSSL 16:8e0d178b1d1e 1325 }
wolfSSL 16:8e0d178b1d1e 1326 #endif /* !WOLFSSL_NOSHA3_384 */
wolfSSL 16:8e0d178b1d1e 1327
wolfSSL 16:8e0d178b1d1e 1328 #if !defined(WOLFSSL_NOSHA3_512)
wolfSSL 16:8e0d178b1d1e 1329 int wc_Sha3_512Hash(const byte* data, word32 len, byte* hash)
wolfSSL 16:8e0d178b1d1e 1330 {
wolfSSL 16:8e0d178b1d1e 1331 int ret = 0;
wolfSSL 16:8e0d178b1d1e 1332 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1333 wc_Sha3* sha3;
wolfSSL 16:8e0d178b1d1e 1334 #else
wolfSSL 16:8e0d178b1d1e 1335 wc_Sha3 sha3[1];
wolfSSL 16:8e0d178b1d1e 1336 #endif
wolfSSL 16:8e0d178b1d1e 1337
wolfSSL 16:8e0d178b1d1e 1338 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1339 sha3 = (wc_Sha3*)XMALLOC(sizeof(wc_Sha3), NULL,
wolfSSL 16:8e0d178b1d1e 1340 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 16:8e0d178b1d1e 1341 if (sha3 == NULL)
wolfSSL 16:8e0d178b1d1e 1342 return MEMORY_E;
wolfSSL 16:8e0d178b1d1e 1343 #endif
wolfSSL 16:8e0d178b1d1e 1344
wolfSSL 16:8e0d178b1d1e 1345 if ((ret = wc_InitSha3_512(sha3, NULL, INVALID_DEVID)) != 0) {
wolfSSL 16:8e0d178b1d1e 1346 WOLFSSL_MSG("InitSha3_512 failed");
wolfSSL 16:8e0d178b1d1e 1347 }
wolfSSL 16:8e0d178b1d1e 1348 else {
wolfSSL 16:8e0d178b1d1e 1349 if ((ret = wc_Sha3_512_Update(sha3, data, len)) != 0) {
wolfSSL 16:8e0d178b1d1e 1350 WOLFSSL_MSG("Sha3_512_Update failed");
wolfSSL 16:8e0d178b1d1e 1351 }
wolfSSL 16:8e0d178b1d1e 1352 else if ((ret = wc_Sha3_512_Final(sha3, hash)) != 0) {
wolfSSL 16:8e0d178b1d1e 1353 WOLFSSL_MSG("Sha3_512_Final failed");
wolfSSL 16:8e0d178b1d1e 1354 }
wolfSSL 16:8e0d178b1d1e 1355 wc_Sha3_512_Free(sha3);
wolfSSL 16:8e0d178b1d1e 1356 }
wolfSSL 16:8e0d178b1d1e 1357
wolfSSL 16:8e0d178b1d1e 1358 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1359 XFREE(sha3, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 16:8e0d178b1d1e 1360 #endif
wolfSSL 16:8e0d178b1d1e 1361
wolfSSL 16:8e0d178b1d1e 1362 return ret;
wolfSSL 16:8e0d178b1d1e 1363 }
wolfSSL 16:8e0d178b1d1e 1364 #endif /* !WOLFSSL_NOSHA3_512 */
wolfSSL 16:8e0d178b1d1e 1365
wolfSSL 16:8e0d178b1d1e 1366 #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
wolfSSL 16:8e0d178b1d1e 1367 int wc_Shake256Hash(const byte* data, word32 len, byte* hash,
wolfSSL 16:8e0d178b1d1e 1368 word32 hashLen)
wolfSSL 16:8e0d178b1d1e 1369 {
wolfSSL 16:8e0d178b1d1e 1370 int ret = 0;
wolfSSL 16:8e0d178b1d1e 1371 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1372 wc_Shake* shake;
wolfSSL 16:8e0d178b1d1e 1373 #else
wolfSSL 16:8e0d178b1d1e 1374 wc_Shake shake[1];
wolfSSL 16:8e0d178b1d1e 1375 #endif
wolfSSL 16:8e0d178b1d1e 1376
wolfSSL 16:8e0d178b1d1e 1377 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1378 shake = (wc_Shake*)XMALLOC(sizeof(wc_Shake), NULL,
wolfSSL 16:8e0d178b1d1e 1379 DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 16:8e0d178b1d1e 1380 if (shake == NULL)
wolfSSL 16:8e0d178b1d1e 1381 return MEMORY_E;
wolfSSL 16:8e0d178b1d1e 1382 #endif
wolfSSL 16:8e0d178b1d1e 1383
wolfSSL 16:8e0d178b1d1e 1384 if ((ret = wc_InitShake256(shake, NULL, INVALID_DEVID)) != 0) {
wolfSSL 16:8e0d178b1d1e 1385 WOLFSSL_MSG("InitShake256 failed");
wolfSSL 16:8e0d178b1d1e 1386 }
wolfSSL 16:8e0d178b1d1e 1387 else {
wolfSSL 16:8e0d178b1d1e 1388 if ((ret = wc_Shake256_Update(shake, data, len)) != 0) {
wolfSSL 16:8e0d178b1d1e 1389 WOLFSSL_MSG("Shake256_Update failed");
wolfSSL 16:8e0d178b1d1e 1390 }
wolfSSL 16:8e0d178b1d1e 1391 else if ((ret = wc_Shake256_Final(shake, hash, hashLen)) != 0) {
wolfSSL 16:8e0d178b1d1e 1392 WOLFSSL_MSG("Shake256_Final failed");
wolfSSL 16:8e0d178b1d1e 1393 }
wolfSSL 16:8e0d178b1d1e 1394 wc_Shake256_Free(shake);
wolfSSL 16:8e0d178b1d1e 1395 }
wolfSSL 16:8e0d178b1d1e 1396
wolfSSL 16:8e0d178b1d1e 1397 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1398 XFREE(shake, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 16:8e0d178b1d1e 1399 #endif
wolfSSL 16:8e0d178b1d1e 1400
wolfSSL 16:8e0d178b1d1e 1401 return ret;
wolfSSL 16:8e0d178b1d1e 1402 }
wolfSSL 16:8e0d178b1d1e 1403 #endif /* WOLFSSL_SHAKE_256 && !WOLFSSL_NO_SHAKE256 */
wolfSSL 16:8e0d178b1d1e 1404 #endif /* WOLFSSL_SHA3 */
wolfSSL 16:8e0d178b1d1e 1405
wolfSSL 16:8e0d178b1d1e 1406 #endif /* !NO_HASH_WRAPPER */
wolfSSL 16:8e0d178b1d1e 1407
wolfSSL 16:8e0d178b1d1e 1408 #ifdef WOLFSSL_HAVE_PRF
wolfSSL 16:8e0d178b1d1e 1409
wolfSSL 16:8e0d178b1d1e 1410 #ifdef WOLFSSL_SHA384
wolfSSL 16:8e0d178b1d1e 1411 #define P_HASH_MAX_SIZE WC_SHA384_DIGEST_SIZE
wolfSSL 16:8e0d178b1d1e 1412 #else
wolfSSL 16:8e0d178b1d1e 1413 #define P_HASH_MAX_SIZE WC_SHA256_DIGEST_SIZE
wolfSSL 16:8e0d178b1d1e 1414 #endif
wolfSSL 16:8e0d178b1d1e 1415
wolfSSL 16:8e0d178b1d1e 1416 /* Pseudo Random Function for MD5, SHA-1, SHA-256, or SHA-384 */
wolfSSL 16:8e0d178b1d1e 1417 int wc_PRF(byte* result, word32 resLen, const byte* secret,
wolfSSL 16:8e0d178b1d1e 1418 word32 secLen, const byte* seed, word32 seedLen, int hash,
wolfSSL 16:8e0d178b1d1e 1419 void* heap, int devId)
wolfSSL 16:8e0d178b1d1e 1420 {
wolfSSL 16:8e0d178b1d1e 1421 word32 len = P_HASH_MAX_SIZE;
wolfSSL 16:8e0d178b1d1e 1422 word32 times;
wolfSSL 16:8e0d178b1d1e 1423 word32 lastLen;
wolfSSL 16:8e0d178b1d1e 1424 word32 lastTime;
wolfSSL 16:8e0d178b1d1e 1425 word32 i;
wolfSSL 16:8e0d178b1d1e 1426 word32 idx = 0;
wolfSSL 16:8e0d178b1d1e 1427 int ret = 0;
wolfSSL 16:8e0d178b1d1e 1428 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1429 byte* previous;
wolfSSL 16:8e0d178b1d1e 1430 byte* current;
wolfSSL 16:8e0d178b1d1e 1431 Hmac* hmac;
wolfSSL 16:8e0d178b1d1e 1432 #else
wolfSSL 16:8e0d178b1d1e 1433 byte previous[P_HASH_MAX_SIZE]; /* max size */
wolfSSL 16:8e0d178b1d1e 1434 byte current[P_HASH_MAX_SIZE]; /* max size */
wolfSSL 16:8e0d178b1d1e 1435 Hmac hmac[1];
wolfSSL 16:8e0d178b1d1e 1436 #endif
wolfSSL 16:8e0d178b1d1e 1437
wolfSSL 16:8e0d178b1d1e 1438 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1439 previous = (byte*)XMALLOC(P_HASH_MAX_SIZE, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1440 current = (byte*)XMALLOC(P_HASH_MAX_SIZE, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1441 hmac = (Hmac*)XMALLOC(sizeof(Hmac), heap, DYNAMIC_TYPE_HMAC);
wolfSSL 16:8e0d178b1d1e 1442
wolfSSL 16:8e0d178b1d1e 1443 if (previous == NULL || current == NULL || hmac == NULL) {
wolfSSL 16:8e0d178b1d1e 1444 if (previous) XFREE(previous, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1445 if (current) XFREE(current, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1446 if (hmac) XFREE(hmac, heap, DYNAMIC_TYPE_HMAC);
wolfSSL 16:8e0d178b1d1e 1447
wolfSSL 16:8e0d178b1d1e 1448 return MEMORY_E;
wolfSSL 16:8e0d178b1d1e 1449 }
wolfSSL 16:8e0d178b1d1e 1450 #endif
wolfSSL 16:8e0d178b1d1e 1451
wolfSSL 16:8e0d178b1d1e 1452 switch (hash) {
wolfSSL 16:8e0d178b1d1e 1453 #ifndef NO_MD5
wolfSSL 16:8e0d178b1d1e 1454 case md5_mac:
wolfSSL 16:8e0d178b1d1e 1455 hash = WC_MD5;
wolfSSL 16:8e0d178b1d1e 1456 len = WC_MD5_DIGEST_SIZE;
wolfSSL 16:8e0d178b1d1e 1457 break;
wolfSSL 16:8e0d178b1d1e 1458 #endif
wolfSSL 16:8e0d178b1d1e 1459
wolfSSL 16:8e0d178b1d1e 1460 #ifndef NO_SHA256
wolfSSL 16:8e0d178b1d1e 1461 case sha256_mac:
wolfSSL 16:8e0d178b1d1e 1462 hash = WC_SHA256;
wolfSSL 16:8e0d178b1d1e 1463 len = WC_SHA256_DIGEST_SIZE;
wolfSSL 16:8e0d178b1d1e 1464 break;
wolfSSL 16:8e0d178b1d1e 1465 #endif
wolfSSL 16:8e0d178b1d1e 1466
wolfSSL 16:8e0d178b1d1e 1467 #ifdef WOLFSSL_SHA384
wolfSSL 16:8e0d178b1d1e 1468 case sha384_mac:
wolfSSL 16:8e0d178b1d1e 1469 hash = WC_SHA384;
wolfSSL 16:8e0d178b1d1e 1470 len = WC_SHA384_DIGEST_SIZE;
wolfSSL 16:8e0d178b1d1e 1471 break;
wolfSSL 16:8e0d178b1d1e 1472 #endif
wolfSSL 16:8e0d178b1d1e 1473
wolfSSL 16:8e0d178b1d1e 1474 #ifndef NO_SHA
wolfSSL 16:8e0d178b1d1e 1475 case sha_mac:
wolfSSL 16:8e0d178b1d1e 1476 default:
wolfSSL 16:8e0d178b1d1e 1477 hash = WC_SHA;
wolfSSL 16:8e0d178b1d1e 1478 len = WC_SHA_DIGEST_SIZE;
wolfSSL 16:8e0d178b1d1e 1479 break;
wolfSSL 16:8e0d178b1d1e 1480 #endif
wolfSSL 16:8e0d178b1d1e 1481 }
wolfSSL 16:8e0d178b1d1e 1482
wolfSSL 16:8e0d178b1d1e 1483 times = resLen / len;
wolfSSL 16:8e0d178b1d1e 1484 lastLen = resLen % len;
wolfSSL 16:8e0d178b1d1e 1485
wolfSSL 16:8e0d178b1d1e 1486 if (lastLen)
wolfSSL 16:8e0d178b1d1e 1487 times += 1;
wolfSSL 16:8e0d178b1d1e 1488
wolfSSL 16:8e0d178b1d1e 1489 lastTime = times - 1;
wolfSSL 16:8e0d178b1d1e 1490
wolfSSL 16:8e0d178b1d1e 1491 ret = wc_HmacInit(hmac, heap, devId);
wolfSSL 16:8e0d178b1d1e 1492 if (ret == 0) {
wolfSSL 16:8e0d178b1d1e 1493 ret = wc_HmacSetKey(hmac, hash, secret, secLen);
wolfSSL 16:8e0d178b1d1e 1494 if (ret == 0)
wolfSSL 16:8e0d178b1d1e 1495 ret = wc_HmacUpdate(hmac, seed, seedLen); /* A0 = seed */
wolfSSL 16:8e0d178b1d1e 1496 if (ret == 0)
wolfSSL 16:8e0d178b1d1e 1497 ret = wc_HmacFinal(hmac, previous); /* A1 */
wolfSSL 16:8e0d178b1d1e 1498 if (ret == 0) {
wolfSSL 16:8e0d178b1d1e 1499 for (i = 0; i < times; i++) {
wolfSSL 16:8e0d178b1d1e 1500 ret = wc_HmacUpdate(hmac, previous, len);
wolfSSL 16:8e0d178b1d1e 1501 if (ret != 0)
wolfSSL 16:8e0d178b1d1e 1502 break;
wolfSSL 16:8e0d178b1d1e 1503 ret = wc_HmacUpdate(hmac, seed, seedLen);
wolfSSL 16:8e0d178b1d1e 1504 if (ret != 0)
wolfSSL 16:8e0d178b1d1e 1505 break;
wolfSSL 16:8e0d178b1d1e 1506 ret = wc_HmacFinal(hmac, current);
wolfSSL 16:8e0d178b1d1e 1507 if (ret != 0)
wolfSSL 16:8e0d178b1d1e 1508 break;
wolfSSL 16:8e0d178b1d1e 1509
wolfSSL 16:8e0d178b1d1e 1510 if ((i == lastTime) && lastLen)
wolfSSL 16:8e0d178b1d1e 1511 XMEMCPY(&result[idx], current,
wolfSSL 16:8e0d178b1d1e 1512 min(lastLen, P_HASH_MAX_SIZE));
wolfSSL 16:8e0d178b1d1e 1513 else {
wolfSSL 16:8e0d178b1d1e 1514 XMEMCPY(&result[idx], current, len);
wolfSSL 16:8e0d178b1d1e 1515 idx += len;
wolfSSL 16:8e0d178b1d1e 1516 ret = wc_HmacUpdate(hmac, previous, len);
wolfSSL 16:8e0d178b1d1e 1517 if (ret != 0)
wolfSSL 16:8e0d178b1d1e 1518 break;
wolfSSL 16:8e0d178b1d1e 1519 ret = wc_HmacFinal(hmac, previous);
wolfSSL 16:8e0d178b1d1e 1520 if (ret != 0)
wolfSSL 16:8e0d178b1d1e 1521 break;
wolfSSL 16:8e0d178b1d1e 1522 }
wolfSSL 16:8e0d178b1d1e 1523 }
wolfSSL 16:8e0d178b1d1e 1524 }
wolfSSL 16:8e0d178b1d1e 1525 wc_HmacFree(hmac);
wolfSSL 16:8e0d178b1d1e 1526 }
wolfSSL 16:8e0d178b1d1e 1527
wolfSSL 16:8e0d178b1d1e 1528 ForceZero(previous, P_HASH_MAX_SIZE);
wolfSSL 16:8e0d178b1d1e 1529 ForceZero(current, P_HASH_MAX_SIZE);
wolfSSL 16:8e0d178b1d1e 1530 ForceZero(hmac, sizeof(Hmac));
wolfSSL 16:8e0d178b1d1e 1531
wolfSSL 16:8e0d178b1d1e 1532 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1533 XFREE(previous, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1534 XFREE(current, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1535 XFREE(hmac, heap, DYNAMIC_TYPE_HMAC);
wolfSSL 16:8e0d178b1d1e 1536 #endif
wolfSSL 16:8e0d178b1d1e 1537
wolfSSL 16:8e0d178b1d1e 1538 return ret;
wolfSSL 16:8e0d178b1d1e 1539 }
wolfSSL 16:8e0d178b1d1e 1540 #undef P_HASH_MAX_SIZE
wolfSSL 16:8e0d178b1d1e 1541
wolfSSL 16:8e0d178b1d1e 1542 /* compute PRF (pseudo random function) using SHA1 and MD5 for TLSv1 */
wolfSSL 16:8e0d178b1d1e 1543 int wc_PRF_TLSv1(byte* digest, word32 digLen, const byte* secret,
wolfSSL 16:8e0d178b1d1e 1544 word32 secLen, const byte* label, word32 labLen,
wolfSSL 16:8e0d178b1d1e 1545 const byte* seed, word32 seedLen, void* heap, int devId)
wolfSSL 16:8e0d178b1d1e 1546 {
wolfSSL 16:8e0d178b1d1e 1547 int ret = 0;
wolfSSL 16:8e0d178b1d1e 1548 word32 half = (secLen + 1) / 2;
wolfSSL 16:8e0d178b1d1e 1549
wolfSSL 16:8e0d178b1d1e 1550 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1551 byte* md5_half;
wolfSSL 16:8e0d178b1d1e 1552 byte* sha_half;
wolfSSL 16:8e0d178b1d1e 1553 byte* md5_result;
wolfSSL 16:8e0d178b1d1e 1554 byte* sha_result;
wolfSSL 16:8e0d178b1d1e 1555 #else
wolfSSL 16:8e0d178b1d1e 1556 byte md5_half[MAX_PRF_HALF]; /* half is real size */
wolfSSL 16:8e0d178b1d1e 1557 byte sha_half[MAX_PRF_HALF]; /* half is real size */
wolfSSL 16:8e0d178b1d1e 1558 byte md5_result[MAX_PRF_DIG]; /* digLen is real size */
wolfSSL 16:8e0d178b1d1e 1559 byte sha_result[MAX_PRF_DIG]; /* digLen is real size */
wolfSSL 16:8e0d178b1d1e 1560 #endif
wolfSSL 16:8e0d178b1d1e 1561 #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
wolfSSL 16:8e0d178b1d1e 1562 DECLARE_VAR(labelSeed, byte, MAX_PRF_LABSEED, heap);
wolfSSL 16:8e0d178b1d1e 1563 if (labelSeed == NULL)
wolfSSL 16:8e0d178b1d1e 1564 return MEMORY_E;
wolfSSL 16:8e0d178b1d1e 1565 #else
wolfSSL 16:8e0d178b1d1e 1566 byte labelSeed[MAX_PRF_LABSEED];
wolfSSL 16:8e0d178b1d1e 1567 #endif
wolfSSL 16:8e0d178b1d1e 1568
wolfSSL 16:8e0d178b1d1e 1569 if (half > MAX_PRF_HALF ||
wolfSSL 16:8e0d178b1d1e 1570 labLen + seedLen > MAX_PRF_LABSEED ||
wolfSSL 16:8e0d178b1d1e 1571 digLen > MAX_PRF_DIG)
wolfSSL 16:8e0d178b1d1e 1572 {
wolfSSL 16:8e0d178b1d1e 1573 #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
wolfSSL 16:8e0d178b1d1e 1574 FREE_VAR(labelSeed, heap);
wolfSSL 16:8e0d178b1d1e 1575 #endif
wolfSSL 16:8e0d178b1d1e 1576 return BUFFER_E;
wolfSSL 16:8e0d178b1d1e 1577 }
wolfSSL 16:8e0d178b1d1e 1578
wolfSSL 16:8e0d178b1d1e 1579 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1580 md5_half = (byte*)XMALLOC(MAX_PRF_HALF, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1581 sha_half = (byte*)XMALLOC(MAX_PRF_HALF, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1582 md5_result = (byte*)XMALLOC(MAX_PRF_DIG, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1583 sha_result = (byte*)XMALLOC(MAX_PRF_DIG, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1584
wolfSSL 16:8e0d178b1d1e 1585 if (md5_half == NULL || sha_half == NULL || md5_result == NULL ||
wolfSSL 16:8e0d178b1d1e 1586 sha_result == NULL) {
wolfSSL 16:8e0d178b1d1e 1587 if (md5_half) XFREE(md5_half, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1588 if (sha_half) XFREE(sha_half, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1589 if (md5_result) XFREE(md5_result, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1590 if (sha_result) XFREE(sha_result, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1591 #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
wolfSSL 16:8e0d178b1d1e 1592 FREE_VAR(labelSeed, heap);
wolfSSL 16:8e0d178b1d1e 1593 #endif
wolfSSL 16:8e0d178b1d1e 1594
wolfSSL 16:8e0d178b1d1e 1595 return MEMORY_E;
wolfSSL 16:8e0d178b1d1e 1596 }
wolfSSL 16:8e0d178b1d1e 1597 #endif
wolfSSL 16:8e0d178b1d1e 1598
wolfSSL 16:8e0d178b1d1e 1599 XMEMSET(md5_result, 0, digLen);
wolfSSL 16:8e0d178b1d1e 1600 XMEMSET(sha_result, 0, digLen);
wolfSSL 16:8e0d178b1d1e 1601
wolfSSL 16:8e0d178b1d1e 1602 XMEMCPY(md5_half, secret, half);
wolfSSL 16:8e0d178b1d1e 1603 XMEMCPY(sha_half, secret + half - secLen % 2, half);
wolfSSL 16:8e0d178b1d1e 1604
wolfSSL 16:8e0d178b1d1e 1605 XMEMCPY(labelSeed, label, labLen);
wolfSSL 16:8e0d178b1d1e 1606 XMEMCPY(labelSeed + labLen, seed, seedLen);
wolfSSL 16:8e0d178b1d1e 1607
wolfSSL 16:8e0d178b1d1e 1608 if ((ret = wc_PRF(md5_result, digLen, md5_half, half, labelSeed,
wolfSSL 16:8e0d178b1d1e 1609 labLen + seedLen, md5_mac, heap, devId)) == 0) {
wolfSSL 16:8e0d178b1d1e 1610 if ((ret = wc_PRF(sha_result, digLen, sha_half, half, labelSeed,
wolfSSL 16:8e0d178b1d1e 1611 labLen + seedLen, sha_mac, heap, devId)) == 0) {
wolfSSL 16:8e0d178b1d1e 1612 /* calculate XOR for TLSv1 PRF */
wolfSSL 16:8e0d178b1d1e 1613 XMEMCPY(digest, md5_result, digLen);
wolfSSL 16:8e0d178b1d1e 1614 xorbuf(digest, sha_result, digLen);
wolfSSL 16:8e0d178b1d1e 1615 }
wolfSSL 16:8e0d178b1d1e 1616 }
wolfSSL 16:8e0d178b1d1e 1617
wolfSSL 16:8e0d178b1d1e 1618 #ifdef WOLFSSL_SMALL_STACK
wolfSSL 16:8e0d178b1d1e 1619 XFREE(md5_half, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1620 XFREE(sha_half, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1621 XFREE(md5_result, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1622 XFREE(sha_result, heap, DYNAMIC_TYPE_DIGEST);
wolfSSL 16:8e0d178b1d1e 1623 #endif
wolfSSL 16:8e0d178b1d1e 1624
wolfSSL 16:8e0d178b1d1e 1625 #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
wolfSSL 16:8e0d178b1d1e 1626 FREE_VAR(labelSeed, heap);
wolfSSL 16:8e0d178b1d1e 1627 #endif
wolfSSL 16:8e0d178b1d1e 1628
wolfSSL 16:8e0d178b1d1e 1629 return ret;
wolfSSL 16:8e0d178b1d1e 1630 }
wolfSSL 16:8e0d178b1d1e 1631
wolfSSL 16:8e0d178b1d1e 1632 /* Wrapper for TLS 1.2 and TLSv1 cases to calculate PRF */
wolfSSL 16:8e0d178b1d1e 1633 /* In TLS 1.2 case call straight thru to wc_PRF */
wolfSSL 16:8e0d178b1d1e 1634 int wc_PRF_TLS(byte* digest, word32 digLen, const byte* secret, word32 secLen,
wolfSSL 16:8e0d178b1d1e 1635 const byte* label, word32 labLen, const byte* seed, word32 seedLen,
wolfSSL 16:8e0d178b1d1e 1636 int useAtLeastSha256, int hash_type, void* heap, int devId)
wolfSSL 16:8e0d178b1d1e 1637 {
wolfSSL 16:8e0d178b1d1e 1638 int ret = 0;
wolfSSL 16:8e0d178b1d1e 1639
wolfSSL 16:8e0d178b1d1e 1640 if (useAtLeastSha256) {
wolfSSL 16:8e0d178b1d1e 1641 #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
wolfSSL 16:8e0d178b1d1e 1642 DECLARE_VAR(labelSeed, byte, MAX_PRF_LABSEED, heap);
wolfSSL 16:8e0d178b1d1e 1643 if (labelSeed == NULL)
wolfSSL 16:8e0d178b1d1e 1644 return MEMORY_E;
wolfSSL 16:8e0d178b1d1e 1645 #else
wolfSSL 16:8e0d178b1d1e 1646 byte labelSeed[MAX_PRF_LABSEED];
wolfSSL 16:8e0d178b1d1e 1647 #endif
wolfSSL 16:8e0d178b1d1e 1648
wolfSSL 16:8e0d178b1d1e 1649 if (labLen + seedLen > MAX_PRF_LABSEED)
wolfSSL 16:8e0d178b1d1e 1650 return BUFFER_E;
wolfSSL 16:8e0d178b1d1e 1651
wolfSSL 16:8e0d178b1d1e 1652 XMEMCPY(labelSeed, label, labLen);
wolfSSL 16:8e0d178b1d1e 1653 XMEMCPY(labelSeed + labLen, seed, seedLen);
wolfSSL 16:8e0d178b1d1e 1654
wolfSSL 16:8e0d178b1d1e 1655 /* If a cipher suite wants an algorithm better than sha256, it
wolfSSL 16:8e0d178b1d1e 1656 * should use better. */
wolfSSL 16:8e0d178b1d1e 1657 if (hash_type < sha256_mac || hash_type == blake2b_mac)
wolfSSL 16:8e0d178b1d1e 1658 hash_type = sha256_mac;
wolfSSL 16:8e0d178b1d1e 1659 /* compute PRF for MD5, SHA-1, SHA-256, or SHA-384 for TLSv1.2 PRF */
wolfSSL 16:8e0d178b1d1e 1660 ret = wc_PRF(digest, digLen, secret, secLen, labelSeed,
wolfSSL 16:8e0d178b1d1e 1661 labLen + seedLen, hash_type, heap, devId);
wolfSSL 16:8e0d178b1d1e 1662
wolfSSL 16:8e0d178b1d1e 1663 #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
wolfSSL 16:8e0d178b1d1e 1664 FREE_VAR(labelSeed, heap);
wolfSSL 16:8e0d178b1d1e 1665 #endif
wolfSSL 16:8e0d178b1d1e 1666 }
wolfSSL 16:8e0d178b1d1e 1667 #ifndef NO_OLD_TLS
wolfSSL 16:8e0d178b1d1e 1668 else {
wolfSSL 16:8e0d178b1d1e 1669 /* compute TLSv1 PRF (pseudo random function using HMAC) */
wolfSSL 16:8e0d178b1d1e 1670 ret = wc_PRF_TLSv1(digest, digLen, secret, secLen, label, labLen, seed,
wolfSSL 16:8e0d178b1d1e 1671 seedLen, heap, devId);
wolfSSL 16:8e0d178b1d1e 1672 }
wolfSSL 16:8e0d178b1d1e 1673 #endif
wolfSSL 16:8e0d178b1d1e 1674
wolfSSL 16:8e0d178b1d1e 1675 return ret;
wolfSSL 16:8e0d178b1d1e 1676 }
wolfSSL 16:8e0d178b1d1e 1677 #endif /* WOLFSSL_HAVE_PRF */
wolfSSL 15:117db924cf7c 1678