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