wolfSSL 3.11.1 for TLS1.3 beta
Fork of wolfSSL by
wolfcrypt/src/hash.c@4:1b0d80432c79, 2016-04-28 (annotated)
- Committer:
- wolfSSL
- Date:
- Thu Apr 28 00:57:21 2016 +0000
- Revision:
- 4:1b0d80432c79
wolfSSL 3.9.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 4:1b0d80432c79 | 1 | /* hash.c |
wolfSSL | 4:1b0d80432c79 | 2 | * |
wolfSSL | 4:1b0d80432c79 | 3 | * Copyright (C) 2006-2016 wolfSSL Inc. |
wolfSSL | 4:1b0d80432c79 | 4 | * |
wolfSSL | 4:1b0d80432c79 | 5 | * This file is part of wolfSSL. |
wolfSSL | 4:1b0d80432c79 | 6 | * |
wolfSSL | 4:1b0d80432c79 | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 4:1b0d80432c79 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 4:1b0d80432c79 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 4:1b0d80432c79 | 10 | * (at your option) any later version. |
wolfSSL | 4:1b0d80432c79 | 11 | * |
wolfSSL | 4:1b0d80432c79 | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 4:1b0d80432c79 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 4:1b0d80432c79 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 4:1b0d80432c79 | 15 | * GNU General Public License for more details. |
wolfSSL | 4:1b0d80432c79 | 16 | * |
wolfSSL | 4:1b0d80432c79 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 4:1b0d80432c79 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 4:1b0d80432c79 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 4:1b0d80432c79 | 20 | */ |
wolfSSL | 4:1b0d80432c79 | 21 | |
wolfSSL | 4:1b0d80432c79 | 22 | |
wolfSSL | 4:1b0d80432c79 | 23 | #ifdef HAVE_CONFIG_H |
wolfSSL | 4:1b0d80432c79 | 24 | #include <config.h> |
wolfSSL | 4:1b0d80432c79 | 25 | #endif |
wolfSSL | 4:1b0d80432c79 | 26 | |
wolfSSL | 4:1b0d80432c79 | 27 | #include <wolfssl/wolfcrypt/settings.h> |
wolfSSL | 4:1b0d80432c79 | 28 | #include <wolfssl/wolfcrypt/logging.h> |
wolfSSL | 4:1b0d80432c79 | 29 | #include <wolfssl/wolfcrypt/error-crypt.h> |
wolfSSL | 4:1b0d80432c79 | 30 | #ifndef NO_ASN |
wolfSSL | 4:1b0d80432c79 | 31 | #include <wolfssl/wolfcrypt/asn.h> |
wolfSSL | 4:1b0d80432c79 | 32 | #endif |
wolfSSL | 4:1b0d80432c79 | 33 | |
wolfSSL | 4:1b0d80432c79 | 34 | #include <wolfssl/wolfcrypt/hash.h> |
wolfSSL | 4:1b0d80432c79 | 35 | |
wolfSSL | 4:1b0d80432c79 | 36 | |
wolfSSL | 4:1b0d80432c79 | 37 | #ifndef NO_ASN |
wolfSSL | 4:1b0d80432c79 | 38 | int wc_HashGetOID(enum wc_HashType hash_type) |
wolfSSL | 4:1b0d80432c79 | 39 | { |
wolfSSL | 4:1b0d80432c79 | 40 | int oid = HASH_TYPE_E; /* Default to hash type error */ |
wolfSSL | 4:1b0d80432c79 | 41 | switch(hash_type) |
wolfSSL | 4:1b0d80432c79 | 42 | { |
wolfSSL | 4:1b0d80432c79 | 43 | case WC_HASH_TYPE_MD2: |
wolfSSL | 4:1b0d80432c79 | 44 | #ifdef WOLFSSL_MD2 |
wolfSSL | 4:1b0d80432c79 | 45 | oid = MD2h; |
wolfSSL | 4:1b0d80432c79 | 46 | #endif |
wolfSSL | 4:1b0d80432c79 | 47 | break; |
wolfSSL | 4:1b0d80432c79 | 48 | case WC_HASH_TYPE_MD5: |
wolfSSL | 4:1b0d80432c79 | 49 | #ifndef NO_MD5 |
wolfSSL | 4:1b0d80432c79 | 50 | oid = MD5h; |
wolfSSL | 4:1b0d80432c79 | 51 | #endif |
wolfSSL | 4:1b0d80432c79 | 52 | break; |
wolfSSL | 4:1b0d80432c79 | 53 | case WC_HASH_TYPE_SHA: |
wolfSSL | 4:1b0d80432c79 | 54 | #ifndef NO_SHA |
wolfSSL | 4:1b0d80432c79 | 55 | oid = SHAh; |
wolfSSL | 4:1b0d80432c79 | 56 | #endif |
wolfSSL | 4:1b0d80432c79 | 57 | break; |
wolfSSL | 4:1b0d80432c79 | 58 | case WC_HASH_TYPE_SHA256: |
wolfSSL | 4:1b0d80432c79 | 59 | #ifndef NO_SHA256 |
wolfSSL | 4:1b0d80432c79 | 60 | oid = SHA256h; |
wolfSSL | 4:1b0d80432c79 | 61 | #endif |
wolfSSL | 4:1b0d80432c79 | 62 | break; |
wolfSSL | 4:1b0d80432c79 | 63 | case WC_HASH_TYPE_SHA384: |
wolfSSL | 4:1b0d80432c79 | 64 | #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384) |
wolfSSL | 4:1b0d80432c79 | 65 | oid = SHA384h; |
wolfSSL | 4:1b0d80432c79 | 66 | #endif |
wolfSSL | 4:1b0d80432c79 | 67 | break; |
wolfSSL | 4:1b0d80432c79 | 68 | case WC_HASH_TYPE_SHA512: |
wolfSSL | 4:1b0d80432c79 | 69 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 4:1b0d80432c79 | 70 | oid = SHA512h; |
wolfSSL | 4:1b0d80432c79 | 71 | #endif |
wolfSSL | 4:1b0d80432c79 | 72 | break; |
wolfSSL | 4:1b0d80432c79 | 73 | |
wolfSSL | 4:1b0d80432c79 | 74 | /* Not Supported */ |
wolfSSL | 4:1b0d80432c79 | 75 | case WC_HASH_TYPE_MD4: |
wolfSSL | 4:1b0d80432c79 | 76 | case WC_HASH_TYPE_NONE: |
wolfSSL | 4:1b0d80432c79 | 77 | default: |
wolfSSL | 4:1b0d80432c79 | 78 | oid = BAD_FUNC_ARG; |
wolfSSL | 4:1b0d80432c79 | 79 | break; |
wolfSSL | 4:1b0d80432c79 | 80 | } |
wolfSSL | 4:1b0d80432c79 | 81 | return oid; |
wolfSSL | 4:1b0d80432c79 | 82 | } |
wolfSSL | 4:1b0d80432c79 | 83 | #endif |
wolfSSL | 4:1b0d80432c79 | 84 | |
wolfSSL | 4:1b0d80432c79 | 85 | /* Get Hash digest size */ |
wolfSSL | 4:1b0d80432c79 | 86 | int wc_HashGetDigestSize(enum wc_HashType hash_type) |
wolfSSL | 4:1b0d80432c79 | 87 | { |
wolfSSL | 4:1b0d80432c79 | 88 | int dig_size = HASH_TYPE_E; /* Default to hash type error */ |
wolfSSL | 4:1b0d80432c79 | 89 | switch(hash_type) |
wolfSSL | 4:1b0d80432c79 | 90 | { |
wolfSSL | 4:1b0d80432c79 | 91 | case WC_HASH_TYPE_MD5: |
wolfSSL | 4:1b0d80432c79 | 92 | #ifndef NO_MD5 |
wolfSSL | 4:1b0d80432c79 | 93 | dig_size = MD5_DIGEST_SIZE; |
wolfSSL | 4:1b0d80432c79 | 94 | #endif |
wolfSSL | 4:1b0d80432c79 | 95 | break; |
wolfSSL | 4:1b0d80432c79 | 96 | case WC_HASH_TYPE_SHA: |
wolfSSL | 4:1b0d80432c79 | 97 | #ifndef NO_SHA |
wolfSSL | 4:1b0d80432c79 | 98 | dig_size = SHA_DIGEST_SIZE; |
wolfSSL | 4:1b0d80432c79 | 99 | #endif |
wolfSSL | 4:1b0d80432c79 | 100 | break; |
wolfSSL | 4:1b0d80432c79 | 101 | case WC_HASH_TYPE_SHA256: |
wolfSSL | 4:1b0d80432c79 | 102 | #ifndef NO_SHA256 |
wolfSSL | 4:1b0d80432c79 | 103 | dig_size = SHA256_DIGEST_SIZE; |
wolfSSL | 4:1b0d80432c79 | 104 | #endif |
wolfSSL | 4:1b0d80432c79 | 105 | break; |
wolfSSL | 4:1b0d80432c79 | 106 | case WC_HASH_TYPE_SHA384: |
wolfSSL | 4:1b0d80432c79 | 107 | #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384) |
wolfSSL | 4:1b0d80432c79 | 108 | dig_size = SHA384_DIGEST_SIZE; |
wolfSSL | 4:1b0d80432c79 | 109 | #endif |
wolfSSL | 4:1b0d80432c79 | 110 | break; |
wolfSSL | 4:1b0d80432c79 | 111 | case WC_HASH_TYPE_SHA512: |
wolfSSL | 4:1b0d80432c79 | 112 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 4:1b0d80432c79 | 113 | dig_size = SHA512_DIGEST_SIZE; |
wolfSSL | 4:1b0d80432c79 | 114 | #endif |
wolfSSL | 4:1b0d80432c79 | 115 | break; |
wolfSSL | 4:1b0d80432c79 | 116 | |
wolfSSL | 4:1b0d80432c79 | 117 | /* Not Supported */ |
wolfSSL | 4:1b0d80432c79 | 118 | case WC_HASH_TYPE_MD2: |
wolfSSL | 4:1b0d80432c79 | 119 | case WC_HASH_TYPE_MD4: |
wolfSSL | 4:1b0d80432c79 | 120 | case WC_HASH_TYPE_NONE: |
wolfSSL | 4:1b0d80432c79 | 121 | default: |
wolfSSL | 4:1b0d80432c79 | 122 | dig_size = BAD_FUNC_ARG; |
wolfSSL | 4:1b0d80432c79 | 123 | break; |
wolfSSL | 4:1b0d80432c79 | 124 | } |
wolfSSL | 4:1b0d80432c79 | 125 | return dig_size; |
wolfSSL | 4:1b0d80432c79 | 126 | } |
wolfSSL | 4:1b0d80432c79 | 127 | |
wolfSSL | 4:1b0d80432c79 | 128 | /* Generic Hashing Wrapper */ |
wolfSSL | 4:1b0d80432c79 | 129 | int wc_Hash(enum wc_HashType hash_type, const byte* data, |
wolfSSL | 4:1b0d80432c79 | 130 | word32 data_len, byte* hash, word32 hash_len) |
wolfSSL | 4:1b0d80432c79 | 131 | { |
wolfSSL | 4:1b0d80432c79 | 132 | int ret = HASH_TYPE_E; /* Default to hash type error */ |
wolfSSL | 4:1b0d80432c79 | 133 | word32 dig_size; |
wolfSSL | 4:1b0d80432c79 | 134 | |
wolfSSL | 4:1b0d80432c79 | 135 | /* Validate hash buffer size */ |
wolfSSL | 4:1b0d80432c79 | 136 | dig_size = wc_HashGetDigestSize(hash_type); |
wolfSSL | 4:1b0d80432c79 | 137 | if (hash_len < dig_size) { |
wolfSSL | 4:1b0d80432c79 | 138 | return BUFFER_E; |
wolfSSL | 4:1b0d80432c79 | 139 | } |
wolfSSL | 4:1b0d80432c79 | 140 | |
wolfSSL | 4:1b0d80432c79 | 141 | /* Suppress possible unused arg if all hashing is disabled */ |
wolfSSL | 4:1b0d80432c79 | 142 | (void)data; |
wolfSSL | 4:1b0d80432c79 | 143 | (void)data_len; |
wolfSSL | 4:1b0d80432c79 | 144 | (void)hash; |
wolfSSL | 4:1b0d80432c79 | 145 | (void)hash_len; |
wolfSSL | 4:1b0d80432c79 | 146 | |
wolfSSL | 4:1b0d80432c79 | 147 | switch(hash_type) |
wolfSSL | 4:1b0d80432c79 | 148 | { |
wolfSSL | 4:1b0d80432c79 | 149 | case WC_HASH_TYPE_MD5: |
wolfSSL | 4:1b0d80432c79 | 150 | #ifndef NO_MD5 |
wolfSSL | 4:1b0d80432c79 | 151 | ret = wc_Md5Hash(data, data_len, hash); |
wolfSSL | 4:1b0d80432c79 | 152 | #endif |
wolfSSL | 4:1b0d80432c79 | 153 | break; |
wolfSSL | 4:1b0d80432c79 | 154 | case WC_HASH_TYPE_SHA: |
wolfSSL | 4:1b0d80432c79 | 155 | #ifndef NO_SHA |
wolfSSL | 4:1b0d80432c79 | 156 | ret = wc_ShaHash(data, data_len, hash); |
wolfSSL | 4:1b0d80432c79 | 157 | #endif |
wolfSSL | 4:1b0d80432c79 | 158 | break; |
wolfSSL | 4:1b0d80432c79 | 159 | case WC_HASH_TYPE_SHA256: |
wolfSSL | 4:1b0d80432c79 | 160 | #ifndef NO_SHA256 |
wolfSSL | 4:1b0d80432c79 | 161 | ret = wc_Sha256Hash(data, data_len, hash); |
wolfSSL | 4:1b0d80432c79 | 162 | #endif |
wolfSSL | 4:1b0d80432c79 | 163 | break; |
wolfSSL | 4:1b0d80432c79 | 164 | case WC_HASH_TYPE_SHA384: |
wolfSSL | 4:1b0d80432c79 | 165 | #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_SHA384) |
wolfSSL | 4:1b0d80432c79 | 166 | ret = wc_Sha384Hash(data, data_len, hash); |
wolfSSL | 4:1b0d80432c79 | 167 | #endif |
wolfSSL | 4:1b0d80432c79 | 168 | break; |
wolfSSL | 4:1b0d80432c79 | 169 | case WC_HASH_TYPE_SHA512: |
wolfSSL | 4:1b0d80432c79 | 170 | #ifdef WOLFSSL_SHA512 |
wolfSSL | 4:1b0d80432c79 | 171 | ret = wc_Sha512Hash(data, data_len, hash); |
wolfSSL | 4:1b0d80432c79 | 172 | #endif |
wolfSSL | 4:1b0d80432c79 | 173 | break; |
wolfSSL | 4:1b0d80432c79 | 174 | |
wolfSSL | 4:1b0d80432c79 | 175 | /* Not Supported */ |
wolfSSL | 4:1b0d80432c79 | 176 | case WC_HASH_TYPE_MD2: |
wolfSSL | 4:1b0d80432c79 | 177 | case WC_HASH_TYPE_MD4: |
wolfSSL | 4:1b0d80432c79 | 178 | case WC_HASH_TYPE_NONE: |
wolfSSL | 4:1b0d80432c79 | 179 | default: |
wolfSSL | 4:1b0d80432c79 | 180 | ret = BAD_FUNC_ARG; |
wolfSSL | 4:1b0d80432c79 | 181 | break; |
wolfSSL | 4:1b0d80432c79 | 182 | } |
wolfSSL | 4:1b0d80432c79 | 183 | return ret; |
wolfSSL | 4:1b0d80432c79 | 184 | } |
wolfSSL | 4:1b0d80432c79 | 185 | |
wolfSSL | 4:1b0d80432c79 | 186 | |
wolfSSL | 4:1b0d80432c79 | 187 | #if !defined(WOLFSSL_TI_HASH) |
wolfSSL | 4:1b0d80432c79 | 188 | |
wolfSSL | 4:1b0d80432c79 | 189 | #if !defined(NO_MD5) |
wolfSSL | 4:1b0d80432c79 | 190 | void wc_Md5GetHash(Md5* md5, byte* hash) |
wolfSSL | 4:1b0d80432c79 | 191 | { |
wolfSSL | 4:1b0d80432c79 | 192 | Md5 save = *md5 ; |
wolfSSL | 4:1b0d80432c79 | 193 | wc_Md5Final(md5, hash) ; |
wolfSSL | 4:1b0d80432c79 | 194 | *md5 = save ; |
wolfSSL | 4:1b0d80432c79 | 195 | } |
wolfSSL | 4:1b0d80432c79 | 196 | |
wolfSSL | 4:1b0d80432c79 | 197 | WOLFSSL_API void wc_Md5RestorePos(Md5* m1, Md5* m2) { |
wolfSSL | 4:1b0d80432c79 | 198 | *m1 = *m2 ; |
wolfSSL | 4:1b0d80432c79 | 199 | } |
wolfSSL | 4:1b0d80432c79 | 200 | |
wolfSSL | 4:1b0d80432c79 | 201 | #endif |
wolfSSL | 4:1b0d80432c79 | 202 | |
wolfSSL | 4:1b0d80432c79 | 203 | #if !defined(NO_SHA) |
wolfSSL | 4:1b0d80432c79 | 204 | int wc_ShaGetHash(Sha* sha, byte* hash) |
wolfSSL | 4:1b0d80432c79 | 205 | { |
wolfSSL | 4:1b0d80432c79 | 206 | int ret ; |
wolfSSL | 4:1b0d80432c79 | 207 | Sha save = *sha ; |
wolfSSL | 4:1b0d80432c79 | 208 | ret = wc_ShaFinal(sha, hash) ; |
wolfSSL | 4:1b0d80432c79 | 209 | *sha = save ; |
wolfSSL | 4:1b0d80432c79 | 210 | return ret ; |
wolfSSL | 4:1b0d80432c79 | 211 | } |
wolfSSL | 4:1b0d80432c79 | 212 | |
wolfSSL | 4:1b0d80432c79 | 213 | void wc_ShaRestorePos(Sha* s1, Sha* s2) { |
wolfSSL | 4:1b0d80432c79 | 214 | *s1 = *s2 ; |
wolfSSL | 4:1b0d80432c79 | 215 | } |
wolfSSL | 4:1b0d80432c79 | 216 | |
wolfSSL | 4:1b0d80432c79 | 217 | int wc_ShaHash(const byte* data, word32 len, byte* hash) |
wolfSSL | 4:1b0d80432c79 | 218 | { |
wolfSSL | 4:1b0d80432c79 | 219 | int ret = 0; |
wolfSSL | 4:1b0d80432c79 | 220 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 4:1b0d80432c79 | 221 | Sha* sha; |
wolfSSL | 4:1b0d80432c79 | 222 | #else |
wolfSSL | 4:1b0d80432c79 | 223 | Sha sha[1]; |
wolfSSL | 4:1b0d80432c79 | 224 | #endif |
wolfSSL | 4:1b0d80432c79 | 225 | |
wolfSSL | 4:1b0d80432c79 | 226 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 4:1b0d80432c79 | 227 | sha = (Sha*)XMALLOC(sizeof(Sha), NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 4:1b0d80432c79 | 228 | if (sha == NULL) |
wolfSSL | 4:1b0d80432c79 | 229 | return MEMORY_E; |
wolfSSL | 4:1b0d80432c79 | 230 | #endif |
wolfSSL | 4:1b0d80432c79 | 231 | |
wolfSSL | 4:1b0d80432c79 | 232 | if ((ret = wc_InitSha(sha)) != 0) { |
wolfSSL | 4:1b0d80432c79 | 233 | WOLFSSL_MSG("wc_InitSha failed"); |
wolfSSL | 4:1b0d80432c79 | 234 | } |
wolfSSL | 4:1b0d80432c79 | 235 | else { |
wolfSSL | 4:1b0d80432c79 | 236 | wc_ShaUpdate(sha, data, len); |
wolfSSL | 4:1b0d80432c79 | 237 | wc_ShaFinal(sha, hash); |
wolfSSL | 4:1b0d80432c79 | 238 | } |
wolfSSL | 4:1b0d80432c79 | 239 | |
wolfSSL | 4:1b0d80432c79 | 240 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 4:1b0d80432c79 | 241 | XFREE(sha, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 4:1b0d80432c79 | 242 | #endif |
wolfSSL | 4:1b0d80432c79 | 243 | |
wolfSSL | 4:1b0d80432c79 | 244 | return ret; |
wolfSSL | 4:1b0d80432c79 | 245 | |
wolfSSL | 4:1b0d80432c79 | 246 | } |
wolfSSL | 4:1b0d80432c79 | 247 | |
wolfSSL | 4:1b0d80432c79 | 248 | #endif /* !defined(NO_SHA) */ |
wolfSSL | 4:1b0d80432c79 | 249 | |
wolfSSL | 4:1b0d80432c79 | 250 | #if !defined(NO_SHA256) |
wolfSSL | 4:1b0d80432c79 | 251 | int wc_Sha256GetHash(Sha256* sha256, byte* hash) |
wolfSSL | 4:1b0d80432c79 | 252 | { |
wolfSSL | 4:1b0d80432c79 | 253 | int ret ; |
wolfSSL | 4:1b0d80432c79 | 254 | Sha256 save = *sha256 ; |
wolfSSL | 4:1b0d80432c79 | 255 | ret = wc_Sha256Final(sha256, hash) ; |
wolfSSL | 4:1b0d80432c79 | 256 | *sha256 = save ; |
wolfSSL | 4:1b0d80432c79 | 257 | return ret ; |
wolfSSL | 4:1b0d80432c79 | 258 | } |
wolfSSL | 4:1b0d80432c79 | 259 | |
wolfSSL | 4:1b0d80432c79 | 260 | void wc_Sha256RestorePos(Sha256* s1, Sha256* s2) { |
wolfSSL | 4:1b0d80432c79 | 261 | *s1 = *s2 ; |
wolfSSL | 4:1b0d80432c79 | 262 | } |
wolfSSL | 4:1b0d80432c79 | 263 | |
wolfSSL | 4:1b0d80432c79 | 264 | int wc_Sha256Hash(const byte* data, word32 len, byte* hash) |
wolfSSL | 4:1b0d80432c79 | 265 | { |
wolfSSL | 4:1b0d80432c79 | 266 | int ret = 0; |
wolfSSL | 4:1b0d80432c79 | 267 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 4:1b0d80432c79 | 268 | Sha256* sha256; |
wolfSSL | 4:1b0d80432c79 | 269 | #else |
wolfSSL | 4:1b0d80432c79 | 270 | Sha256 sha256[1]; |
wolfSSL | 4:1b0d80432c79 | 271 | #endif |
wolfSSL | 4:1b0d80432c79 | 272 | |
wolfSSL | 4:1b0d80432c79 | 273 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 4:1b0d80432c79 | 274 | sha256 = (Sha256*)XMALLOC(sizeof(Sha256), NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 4:1b0d80432c79 | 275 | if (sha256 == NULL) |
wolfSSL | 4:1b0d80432c79 | 276 | return MEMORY_E; |
wolfSSL | 4:1b0d80432c79 | 277 | #endif |
wolfSSL | 4:1b0d80432c79 | 278 | |
wolfSSL | 4:1b0d80432c79 | 279 | if ((ret = wc_InitSha256(sha256)) != 0) { |
wolfSSL | 4:1b0d80432c79 | 280 | WOLFSSL_MSG("InitSha256 failed"); |
wolfSSL | 4:1b0d80432c79 | 281 | } |
wolfSSL | 4:1b0d80432c79 | 282 | else if ((ret = wc_Sha256Update(sha256, data, len)) != 0) { |
wolfSSL | 4:1b0d80432c79 | 283 | WOLFSSL_MSG("Sha256Update failed"); |
wolfSSL | 4:1b0d80432c79 | 284 | } |
wolfSSL | 4:1b0d80432c79 | 285 | else if ((ret = wc_Sha256Final(sha256, hash)) != 0) { |
wolfSSL | 4:1b0d80432c79 | 286 | WOLFSSL_MSG("Sha256Final failed"); |
wolfSSL | 4:1b0d80432c79 | 287 | } |
wolfSSL | 4:1b0d80432c79 | 288 | |
wolfSSL | 4:1b0d80432c79 | 289 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 4:1b0d80432c79 | 290 | XFREE(sha256, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 4:1b0d80432c79 | 291 | #endif |
wolfSSL | 4:1b0d80432c79 | 292 | |
wolfSSL | 4:1b0d80432c79 | 293 | return ret; |
wolfSSL | 4:1b0d80432c79 | 294 | } |
wolfSSL | 4:1b0d80432c79 | 295 | |
wolfSSL | 4:1b0d80432c79 | 296 | #endif /* !defined(NO_SHA256) */ |
wolfSSL | 4:1b0d80432c79 | 297 | |
wolfSSL | 4:1b0d80432c79 | 298 | #endif /* !defined(WOLFSSL_TI_HASH) */ |
wolfSSL | 4:1b0d80432c79 | 299 | |
wolfSSL | 4:1b0d80432c79 | 300 | #if defined(WOLFSSL_SHA512) |
wolfSSL | 4:1b0d80432c79 | 301 | int wc_Sha512Hash(const byte* data, word32 len, byte* hash) |
wolfSSL | 4:1b0d80432c79 | 302 | { |
wolfSSL | 4:1b0d80432c79 | 303 | int ret = 0; |
wolfSSL | 4:1b0d80432c79 | 304 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 4:1b0d80432c79 | 305 | Sha512* sha512; |
wolfSSL | 4:1b0d80432c79 | 306 | #else |
wolfSSL | 4:1b0d80432c79 | 307 | Sha512 sha512[1]; |
wolfSSL | 4:1b0d80432c79 | 308 | #endif |
wolfSSL | 4:1b0d80432c79 | 309 | |
wolfSSL | 4:1b0d80432c79 | 310 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 4:1b0d80432c79 | 311 | sha512 = (Sha512*)XMALLOC(sizeof(Sha512), NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 4:1b0d80432c79 | 312 | if (sha512 == NULL) |
wolfSSL | 4:1b0d80432c79 | 313 | return MEMORY_E; |
wolfSSL | 4:1b0d80432c79 | 314 | #endif |
wolfSSL | 4:1b0d80432c79 | 315 | |
wolfSSL | 4:1b0d80432c79 | 316 | if ((ret = wc_InitSha512(sha512)) != 0) { |
wolfSSL | 4:1b0d80432c79 | 317 | WOLFSSL_MSG("InitSha512 failed"); |
wolfSSL | 4:1b0d80432c79 | 318 | } |
wolfSSL | 4:1b0d80432c79 | 319 | else if ((ret = wc_Sha512Update(sha512, data, len)) != 0) { |
wolfSSL | 4:1b0d80432c79 | 320 | WOLFSSL_MSG("Sha512Update failed"); |
wolfSSL | 4:1b0d80432c79 | 321 | } |
wolfSSL | 4:1b0d80432c79 | 322 | else if ((ret = wc_Sha512Final(sha512, hash)) != 0) { |
wolfSSL | 4:1b0d80432c79 | 323 | WOLFSSL_MSG("Sha512Final failed"); |
wolfSSL | 4:1b0d80432c79 | 324 | } |
wolfSSL | 4:1b0d80432c79 | 325 | |
wolfSSL | 4:1b0d80432c79 | 326 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 4:1b0d80432c79 | 327 | XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 4:1b0d80432c79 | 328 | #endif |
wolfSSL | 4:1b0d80432c79 | 329 | |
wolfSSL | 4:1b0d80432c79 | 330 | return ret; |
wolfSSL | 4:1b0d80432c79 | 331 | } |
wolfSSL | 4:1b0d80432c79 | 332 | |
wolfSSL | 4:1b0d80432c79 | 333 | #if defined(WOLFSSL_SHA384) |
wolfSSL | 4:1b0d80432c79 | 334 | int wc_Sha384Hash(const byte* data, word32 len, byte* hash) |
wolfSSL | 4:1b0d80432c79 | 335 | { |
wolfSSL | 4:1b0d80432c79 | 336 | int ret = 0; |
wolfSSL | 4:1b0d80432c79 | 337 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 4:1b0d80432c79 | 338 | Sha384* sha384; |
wolfSSL | 4:1b0d80432c79 | 339 | #else |
wolfSSL | 4:1b0d80432c79 | 340 | Sha384 sha384[1]; |
wolfSSL | 4:1b0d80432c79 | 341 | #endif |
wolfSSL | 4:1b0d80432c79 | 342 | |
wolfSSL | 4:1b0d80432c79 | 343 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 4:1b0d80432c79 | 344 | sha384 = (Sha384*)XMALLOC(sizeof(Sha384), NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 4:1b0d80432c79 | 345 | if (sha384 == NULL) |
wolfSSL | 4:1b0d80432c79 | 346 | return MEMORY_E; |
wolfSSL | 4:1b0d80432c79 | 347 | #endif |
wolfSSL | 4:1b0d80432c79 | 348 | |
wolfSSL | 4:1b0d80432c79 | 349 | if ((ret = wc_InitSha384(sha384)) != 0) { |
wolfSSL | 4:1b0d80432c79 | 350 | WOLFSSL_MSG("InitSha384 failed"); |
wolfSSL | 4:1b0d80432c79 | 351 | } |
wolfSSL | 4:1b0d80432c79 | 352 | else if ((ret = wc_Sha384Update(sha384, data, len)) != 0) { |
wolfSSL | 4:1b0d80432c79 | 353 | WOLFSSL_MSG("Sha384Update failed"); |
wolfSSL | 4:1b0d80432c79 | 354 | } |
wolfSSL | 4:1b0d80432c79 | 355 | else if ((ret = wc_Sha384Final(sha384, hash)) != 0) { |
wolfSSL | 4:1b0d80432c79 | 356 | WOLFSSL_MSG("Sha384Final failed"); |
wolfSSL | 4:1b0d80432c79 | 357 | } |
wolfSSL | 4:1b0d80432c79 | 358 | |
wolfSSL | 4:1b0d80432c79 | 359 | #ifdef WOLFSSL_SMALL_STACK |
wolfSSL | 4:1b0d80432c79 | 360 | XFREE(sha384, NULL, DYNAMIC_TYPE_TMP_BUFFER); |
wolfSSL | 4:1b0d80432c79 | 361 | #endif |
wolfSSL | 4:1b0d80432c79 | 362 | |
wolfSSL | 4:1b0d80432c79 | 363 | return ret; |
wolfSSL | 4:1b0d80432c79 | 364 | } |
wolfSSL | 4:1b0d80432c79 | 365 | |
wolfSSL | 4:1b0d80432c79 | 366 | #endif /* defined(WOLFSSL_SHA384) */ |
wolfSSL | 4:1b0d80432c79 | 367 | #endif /* defined(WOLFSSL_SHA512) */ |
wolfSSL | 4:1b0d80432c79 | 368 |