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