Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
wolfcrypt/src/sha3.c@12:1a06964c2adb, 2017-08-22 (annotated)
- Committer:
- wolfSSL
- Date:
- Tue Aug 22 10:47:28 2017 +0000
- Revision:
- 12:1a06964c2adb
wolfSSL 3.12.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 12:1a06964c2adb | 1 | /* sha3.c |
wolfSSL | 12:1a06964c2adb | 2 | * |
wolfSSL | 12:1a06964c2adb | 3 | * Copyright (C) 2006-2016 wolfSSL Inc. |
wolfSSL | 12:1a06964c2adb | 4 | * |
wolfSSL | 12:1a06964c2adb | 5 | * This file is part of wolfSSL. |
wolfSSL | 12:1a06964c2adb | 6 | * |
wolfSSL | 12:1a06964c2adb | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 12:1a06964c2adb | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 12:1a06964c2adb | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 12:1a06964c2adb | 10 | * (at your option) any later version. |
wolfSSL | 12:1a06964c2adb | 11 | * |
wolfSSL | 12:1a06964c2adb | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 12:1a06964c2adb | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 12:1a06964c2adb | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 12:1a06964c2adb | 15 | * GNU General Public License for more details. |
wolfSSL | 12:1a06964c2adb | 16 | * |
wolfSSL | 12:1a06964c2adb | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 12:1a06964c2adb | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 12:1a06964c2adb | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 12:1a06964c2adb | 20 | */ |
wolfSSL | 12:1a06964c2adb | 21 | |
wolfSSL | 12:1a06964c2adb | 22 | |
wolfSSL | 12:1a06964c2adb | 23 | #ifdef HAVE_CONFIG_H |
wolfSSL | 12:1a06964c2adb | 24 | #include <config.h> |
wolfSSL | 12:1a06964c2adb | 25 | #endif |
wolfSSL | 12:1a06964c2adb | 26 | |
wolfSSL | 12:1a06964c2adb | 27 | #include <wolfssl/wolfcrypt/settings.h> |
wolfSSL | 12:1a06964c2adb | 28 | |
wolfSSL | 12:1a06964c2adb | 29 | #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) |
wolfSSL | 12:1a06964c2adb | 30 | |
wolfSSL | 12:1a06964c2adb | 31 | #include <wolfssl/wolfcrypt/sha3.h> |
wolfSSL | 12:1a06964c2adb | 32 | #include <wolfssl/wolfcrypt/error-crypt.h> |
wolfSSL | 12:1a06964c2adb | 33 | |
wolfSSL | 12:1a06964c2adb | 34 | /* fips wrapper calls, user can call direct */ |
wolfSSL | 12:1a06964c2adb | 35 | #ifdef HAVE_FIPS |
wolfSSL | 12:1a06964c2adb | 36 | |
wolfSSL | 12:1a06964c2adb | 37 | int wc_InitSha3_224(Sha3* sha, void* heap, int devId) |
wolfSSL | 12:1a06964c2adb | 38 | { |
wolfSSL | 12:1a06964c2adb | 39 | (void)heap; |
wolfSSL | 12:1a06964c2adb | 40 | (void)devId; |
wolfSSL | 12:1a06964c2adb | 41 | if (sha == NULL) { |
wolfSSL | 12:1a06964c2adb | 42 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 43 | } |
wolfSSL | 12:1a06964c2adb | 44 | return InitSha3_224_fips(sha); |
wolfSSL | 12:1a06964c2adb | 45 | } |
wolfSSL | 12:1a06964c2adb | 46 | int wc_Sha3_224_Update(Sha3* sha, const byte* data, word32 len) |
wolfSSL | 12:1a06964c2adb | 47 | { |
wolfSSL | 12:1a06964c2adb | 48 | if (sha == NULL || (data == NULL && len > 0)) { |
wolfSSL | 12:1a06964c2adb | 49 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 50 | } |
wolfSSL | 12:1a06964c2adb | 51 | return Sha3_224_Update_fips(sha, data, len); |
wolfSSL | 12:1a06964c2adb | 52 | } |
wolfSSL | 12:1a06964c2adb | 53 | int wc_Sha3_224_Final(Sha3* sha, byte* out) |
wolfSSL | 12:1a06964c2adb | 54 | { |
wolfSSL | 12:1a06964c2adb | 55 | if (sha == NULL || out == NULL) { |
wolfSSL | 12:1a06964c2adb | 56 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 57 | } |
wolfSSL | 12:1a06964c2adb | 58 | return Sha3_224_Final_fips(sha, out); |
wolfSSL | 12:1a06964c2adb | 59 | } |
wolfSSL | 12:1a06964c2adb | 60 | void wc_Sha3_224_Free(Sha3* sha) |
wolfSSL | 12:1a06964c2adb | 61 | { |
wolfSSL | 12:1a06964c2adb | 62 | (void)sha; |
wolfSSL | 12:1a06964c2adb | 63 | /* Not supported in FIPS */ |
wolfSSL | 12:1a06964c2adb | 64 | } |
wolfSSL | 12:1a06964c2adb | 65 | |
wolfSSL | 12:1a06964c2adb | 66 | int wc_InitSha3_256(Sha3* sha, void* heap, int devId) |
wolfSSL | 12:1a06964c2adb | 67 | { |
wolfSSL | 12:1a06964c2adb | 68 | (void)heap; |
wolfSSL | 12:1a06964c2adb | 69 | (void)devId; |
wolfSSL | 12:1a06964c2adb | 70 | if (sha == NULL) { |
wolfSSL | 12:1a06964c2adb | 71 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 72 | } |
wolfSSL | 12:1a06964c2adb | 73 | return InitSha3_256_fips(sha); |
wolfSSL | 12:1a06964c2adb | 74 | } |
wolfSSL | 12:1a06964c2adb | 75 | int wc_Sha3_256_Update(Sha3* sha, const byte* data, word32 len) |
wolfSSL | 12:1a06964c2adb | 76 | { |
wolfSSL | 12:1a06964c2adb | 77 | if (sha == NULL || (data == NULL && len > 0)) { |
wolfSSL | 12:1a06964c2adb | 78 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 79 | } |
wolfSSL | 12:1a06964c2adb | 80 | return Sha3_256_Update_fips(sha, data, len); |
wolfSSL | 12:1a06964c2adb | 81 | } |
wolfSSL | 12:1a06964c2adb | 82 | int wc_Sha3_256_Final(Sha3* sha, byte* out) |
wolfSSL | 12:1a06964c2adb | 83 | { |
wolfSSL | 12:1a06964c2adb | 84 | if (sha == NULL || out == NULL) { |
wolfSSL | 12:1a06964c2adb | 85 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 86 | } |
wolfSSL | 12:1a06964c2adb | 87 | return Sha3_256_Final_fips(sha, out); |
wolfSSL | 12:1a06964c2adb | 88 | } |
wolfSSL | 12:1a06964c2adb | 89 | void wc_Sha3_256_Free(Sha3* sha) |
wolfSSL | 12:1a06964c2adb | 90 | { |
wolfSSL | 12:1a06964c2adb | 91 | (void)sha; |
wolfSSL | 12:1a06964c2adb | 92 | /* Not supported in FIPS */ |
wolfSSL | 12:1a06964c2adb | 93 | } |
wolfSSL | 12:1a06964c2adb | 94 | |
wolfSSL | 12:1a06964c2adb | 95 | int wc_InitSha3_384(Sha3* sha, void* heap, int devId) |
wolfSSL | 12:1a06964c2adb | 96 | { |
wolfSSL | 12:1a06964c2adb | 97 | (void)heap; |
wolfSSL | 12:1a06964c2adb | 98 | (void)devId; |
wolfSSL | 12:1a06964c2adb | 99 | if (sha == NULL) { |
wolfSSL | 12:1a06964c2adb | 100 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 101 | } |
wolfSSL | 12:1a06964c2adb | 102 | return InitSha3_384_fips(sha); |
wolfSSL | 12:1a06964c2adb | 103 | } |
wolfSSL | 12:1a06964c2adb | 104 | int wc_Sha3_384_Update(Sha3* sha, const byte* data, word32 len) |
wolfSSL | 12:1a06964c2adb | 105 | { |
wolfSSL | 12:1a06964c2adb | 106 | if (sha == NULL || (data == NULL && len > 0)) { |
wolfSSL | 12:1a06964c2adb | 107 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 108 | } |
wolfSSL | 12:1a06964c2adb | 109 | return Sha3_384_Update_fips(sha, data, len); |
wolfSSL | 12:1a06964c2adb | 110 | } |
wolfSSL | 12:1a06964c2adb | 111 | int wc_Sha3_384_Final(Sha3* sha, byte* out) |
wolfSSL | 12:1a06964c2adb | 112 | { |
wolfSSL | 12:1a06964c2adb | 113 | if (sha == NULL || out == NULL) { |
wolfSSL | 12:1a06964c2adb | 114 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 115 | } |
wolfSSL | 12:1a06964c2adb | 116 | return Sha3_384_Final_fips(sha, out); |
wolfSSL | 12:1a06964c2adb | 117 | } |
wolfSSL | 12:1a06964c2adb | 118 | void wc_Sha3_384_Free(Sha3* sha) |
wolfSSL | 12:1a06964c2adb | 119 | { |
wolfSSL | 12:1a06964c2adb | 120 | (void)sha; |
wolfSSL | 12:1a06964c2adb | 121 | /* Not supported in FIPS */ |
wolfSSL | 12:1a06964c2adb | 122 | } |
wolfSSL | 12:1a06964c2adb | 123 | |
wolfSSL | 12:1a06964c2adb | 124 | int wc_InitSha3_512(Sha3* sha, void* heap, int devId) |
wolfSSL | 12:1a06964c2adb | 125 | { |
wolfSSL | 12:1a06964c2adb | 126 | (void)heap; |
wolfSSL | 12:1a06964c2adb | 127 | (void)devId; |
wolfSSL | 12:1a06964c2adb | 128 | if (sha == NULL) { |
wolfSSL | 12:1a06964c2adb | 129 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 130 | } |
wolfSSL | 12:1a06964c2adb | 131 | return InitSha3_512_fips(sha); |
wolfSSL | 12:1a06964c2adb | 132 | } |
wolfSSL | 12:1a06964c2adb | 133 | int wc_Sha3_512_Update(Sha3* sha, const byte* data, word32 len) |
wolfSSL | 12:1a06964c2adb | 134 | { |
wolfSSL | 12:1a06964c2adb | 135 | if (sha == NULL || (data == NULL && len > 0)) { |
wolfSSL | 12:1a06964c2adb | 136 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 137 | } |
wolfSSL | 12:1a06964c2adb | 138 | return Sha3_512_Update_fips(sha, data, len); |
wolfSSL | 12:1a06964c2adb | 139 | } |
wolfSSL | 12:1a06964c2adb | 140 | int wc_Sha3_512_Final(Sha3* sha, byte* out) |
wolfSSL | 12:1a06964c2adb | 141 | { |
wolfSSL | 12:1a06964c2adb | 142 | if (sha == NULL || out == NULL) { |
wolfSSL | 12:1a06964c2adb | 143 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 144 | } |
wolfSSL | 12:1a06964c2adb | 145 | return Sha3_512_Final_fips(sha, out); |
wolfSSL | 12:1a06964c2adb | 146 | } |
wolfSSL | 12:1a06964c2adb | 147 | void wc_Sha3_512_Free(Sha3* sha) |
wolfSSL | 12:1a06964c2adb | 148 | { |
wolfSSL | 12:1a06964c2adb | 149 | (void)sha; |
wolfSSL | 12:1a06964c2adb | 150 | /* Not supported in FIPS */ |
wolfSSL | 12:1a06964c2adb | 151 | } |
wolfSSL | 12:1a06964c2adb | 152 | |
wolfSSL | 12:1a06964c2adb | 153 | #else /* else build without fips */ |
wolfSSL | 12:1a06964c2adb | 154 | |
wolfSSL | 12:1a06964c2adb | 155 | |
wolfSSL | 12:1a06964c2adb | 156 | #ifdef NO_INLINE |
wolfSSL | 12:1a06964c2adb | 157 | #include <wolfssl/wolfcrypt/misc.h> |
wolfSSL | 12:1a06964c2adb | 158 | #else |
wolfSSL | 12:1a06964c2adb | 159 | #define WOLFSSL_MISC_INCLUDED |
wolfSSL | 12:1a06964c2adb | 160 | #include <wolfcrypt/src/misc.c> |
wolfSSL | 12:1a06964c2adb | 161 | #endif |
wolfSSL | 12:1a06964c2adb | 162 | |
wolfSSL | 12:1a06964c2adb | 163 | |
wolfSSL | 12:1a06964c2adb | 164 | #ifdef WOLFSSL_SHA3_SMALL |
wolfSSL | 12:1a06964c2adb | 165 | /* Rotate a 64-bit value left. |
wolfSSL | 12:1a06964c2adb | 166 | * |
wolfSSL | 12:1a06964c2adb | 167 | * a Number to rotate left. |
wolfSSL | 12:1a06964c2adb | 168 | * r Number od bits to rotate left. |
wolfSSL | 12:1a06964c2adb | 169 | * returns the rotated number. |
wolfSSL | 12:1a06964c2adb | 170 | */ |
wolfSSL | 12:1a06964c2adb | 171 | #define ROTL64(a, n) (((a)<<(n))|((a)>>(64-(n)))) |
wolfSSL | 12:1a06964c2adb | 172 | |
wolfSSL | 12:1a06964c2adb | 173 | /* An array of values to XOR for block operation. */ |
wolfSSL | 12:1a06964c2adb | 174 | static const word64 hash_keccak_r[24] = |
wolfSSL | 12:1a06964c2adb | 175 | { |
wolfSSL | 12:1a06964c2adb | 176 | 0x0000000000000001UL, 0x0000000000008082UL, |
wolfSSL | 12:1a06964c2adb | 177 | 0x800000000000808aUL, 0x8000000080008000UL, |
wolfSSL | 12:1a06964c2adb | 178 | 0x000000000000808bUL, 0x0000000080000001UL, |
wolfSSL | 12:1a06964c2adb | 179 | 0x8000000080008081UL, 0x8000000000008009UL, |
wolfSSL | 12:1a06964c2adb | 180 | 0x000000000000008aUL, 0x0000000000000088UL, |
wolfSSL | 12:1a06964c2adb | 181 | 0x0000000080008009UL, 0x000000008000000aUL, |
wolfSSL | 12:1a06964c2adb | 182 | 0x000000008000808bUL, 0x800000000000008bUL, |
wolfSSL | 12:1a06964c2adb | 183 | 0x8000000000008089UL, 0x8000000000008003UL, |
wolfSSL | 12:1a06964c2adb | 184 | 0x8000000000008002UL, 0x8000000000000080UL, |
wolfSSL | 12:1a06964c2adb | 185 | 0x000000000000800aUL, 0x800000008000000aUL, |
wolfSSL | 12:1a06964c2adb | 186 | 0x8000000080008081UL, 0x8000000000008080UL, |
wolfSSL | 12:1a06964c2adb | 187 | 0x0000000080000001UL, 0x8000000080008008UL |
wolfSSL | 12:1a06964c2adb | 188 | }; |
wolfSSL | 12:1a06964c2adb | 189 | |
wolfSSL | 12:1a06964c2adb | 190 | /* Indeces used in swap and rotate operation. */ |
wolfSSL | 12:1a06964c2adb | 191 | #define K_I_0 10 |
wolfSSL | 12:1a06964c2adb | 192 | #define K_I_1 7 |
wolfSSL | 12:1a06964c2adb | 193 | #define K_I_2 11 |
wolfSSL | 12:1a06964c2adb | 194 | #define K_I_3 17 |
wolfSSL | 12:1a06964c2adb | 195 | #define K_I_4 18 |
wolfSSL | 12:1a06964c2adb | 196 | #define K_I_5 3 |
wolfSSL | 12:1a06964c2adb | 197 | #define K_I_6 5 |
wolfSSL | 12:1a06964c2adb | 198 | #define K_I_7 16 |
wolfSSL | 12:1a06964c2adb | 199 | #define K_I_8 8 |
wolfSSL | 12:1a06964c2adb | 200 | #define K_I_9 21 |
wolfSSL | 12:1a06964c2adb | 201 | #define K_I_10 24 |
wolfSSL | 12:1a06964c2adb | 202 | #define K_I_11 4 |
wolfSSL | 12:1a06964c2adb | 203 | #define K_I_12 15 |
wolfSSL | 12:1a06964c2adb | 204 | #define K_I_13 23 |
wolfSSL | 12:1a06964c2adb | 205 | #define K_I_14 19 |
wolfSSL | 12:1a06964c2adb | 206 | #define K_I_15 13 |
wolfSSL | 12:1a06964c2adb | 207 | #define K_I_16 12 |
wolfSSL | 12:1a06964c2adb | 208 | #define K_I_17 2 |
wolfSSL | 12:1a06964c2adb | 209 | #define K_I_18 20 |
wolfSSL | 12:1a06964c2adb | 210 | #define K_I_19 14 |
wolfSSL | 12:1a06964c2adb | 211 | #define K_I_20 22 |
wolfSSL | 12:1a06964c2adb | 212 | #define K_I_21 9 |
wolfSSL | 12:1a06964c2adb | 213 | #define K_I_22 6 |
wolfSSL | 12:1a06964c2adb | 214 | #define K_I_23 1 |
wolfSSL | 12:1a06964c2adb | 215 | |
wolfSSL | 12:1a06964c2adb | 216 | /* Number of bits to rotate in swap and rotate operation. */ |
wolfSSL | 12:1a06964c2adb | 217 | #define K_R_0 1 |
wolfSSL | 12:1a06964c2adb | 218 | #define K_R_1 3 |
wolfSSL | 12:1a06964c2adb | 219 | #define K_R_2 6 |
wolfSSL | 12:1a06964c2adb | 220 | #define K_R_3 10 |
wolfSSL | 12:1a06964c2adb | 221 | #define K_R_4 15 |
wolfSSL | 12:1a06964c2adb | 222 | #define K_R_5 21 |
wolfSSL | 12:1a06964c2adb | 223 | #define K_R_6 28 |
wolfSSL | 12:1a06964c2adb | 224 | #define K_R_7 36 |
wolfSSL | 12:1a06964c2adb | 225 | #define K_R_8 45 |
wolfSSL | 12:1a06964c2adb | 226 | #define K_R_9 55 |
wolfSSL | 12:1a06964c2adb | 227 | #define K_R_10 2 |
wolfSSL | 12:1a06964c2adb | 228 | #define K_R_11 14 |
wolfSSL | 12:1a06964c2adb | 229 | #define K_R_12 27 |
wolfSSL | 12:1a06964c2adb | 230 | #define K_R_13 41 |
wolfSSL | 12:1a06964c2adb | 231 | #define K_R_14 56 |
wolfSSL | 12:1a06964c2adb | 232 | #define K_R_15 8 |
wolfSSL | 12:1a06964c2adb | 233 | #define K_R_16 25 |
wolfSSL | 12:1a06964c2adb | 234 | #define K_R_17 43 |
wolfSSL | 12:1a06964c2adb | 235 | #define K_R_18 62 |
wolfSSL | 12:1a06964c2adb | 236 | #define K_R_19 18 |
wolfSSL | 12:1a06964c2adb | 237 | #define K_R_20 39 |
wolfSSL | 12:1a06964c2adb | 238 | #define K_R_21 61 |
wolfSSL | 12:1a06964c2adb | 239 | #define K_R_22 20 |
wolfSSL | 12:1a06964c2adb | 240 | #define K_R_23 44 |
wolfSSL | 12:1a06964c2adb | 241 | |
wolfSSL | 12:1a06964c2adb | 242 | /* Swap and rotate left operation. |
wolfSSL | 12:1a06964c2adb | 243 | * |
wolfSSL | 12:1a06964c2adb | 244 | * s The state. |
wolfSSL | 12:1a06964c2adb | 245 | * t1 Temporary value. |
wolfSSL | 12:1a06964c2adb | 246 | * t2 Second temporary value. |
wolfSSL | 12:1a06964c2adb | 247 | * i The index of the loop. |
wolfSSL | 12:1a06964c2adb | 248 | */ |
wolfSSL | 12:1a06964c2adb | 249 | #define SWAP_ROTL(s, t1, t2, i) \ |
wolfSSL | 12:1a06964c2adb | 250 | do \ |
wolfSSL | 12:1a06964c2adb | 251 | { \ |
wolfSSL | 12:1a06964c2adb | 252 | t2 = s[K_I_##i]; s[K_I_##i] = ROTL64(t1, K_R_##i); \ |
wolfSSL | 12:1a06964c2adb | 253 | } \ |
wolfSSL | 12:1a06964c2adb | 254 | while (0) |
wolfSSL | 12:1a06964c2adb | 255 | |
wolfSSL | 12:1a06964c2adb | 256 | /* Mix the XOR of the column's values into each number by column. |
wolfSSL | 12:1a06964c2adb | 257 | * |
wolfSSL | 12:1a06964c2adb | 258 | * s The state. |
wolfSSL | 12:1a06964c2adb | 259 | * b Temporary array of XORed column values. |
wolfSSL | 12:1a06964c2adb | 260 | * x The index of the column. |
wolfSSL | 12:1a06964c2adb | 261 | * t Temporary variable. |
wolfSSL | 12:1a06964c2adb | 262 | */ |
wolfSSL | 12:1a06964c2adb | 263 | #define COL_MIX(s, b, x, t) \ |
wolfSSL | 12:1a06964c2adb | 264 | do \ |
wolfSSL | 12:1a06964c2adb | 265 | { \ |
wolfSSL | 12:1a06964c2adb | 266 | for (x = 0; x < 5; x++) \ |
wolfSSL | 12:1a06964c2adb | 267 | b[x] = s[x + 0] ^ s[x + 5] ^ s[x + 10] ^ s[x + 15] ^ s[x + 20]; \ |
wolfSSL | 12:1a06964c2adb | 268 | for (x = 0; x < 5; x++) \ |
wolfSSL | 12:1a06964c2adb | 269 | { \ |
wolfSSL | 12:1a06964c2adb | 270 | t = b[(x + 4) % 5] ^ ROTL64(b[(x + 1) % 5], 1); \ |
wolfSSL | 12:1a06964c2adb | 271 | s[x + 0] ^= t; \ |
wolfSSL | 12:1a06964c2adb | 272 | s[x + 5] ^= t; \ |
wolfSSL | 12:1a06964c2adb | 273 | s[x + 10] ^= t; \ |
wolfSSL | 12:1a06964c2adb | 274 | s[x + 15] ^= t; \ |
wolfSSL | 12:1a06964c2adb | 275 | s[x + 20] ^= t; \ |
wolfSSL | 12:1a06964c2adb | 276 | } \ |
wolfSSL | 12:1a06964c2adb | 277 | } \ |
wolfSSL | 12:1a06964c2adb | 278 | while (0) |
wolfSSL | 12:1a06964c2adb | 279 | |
wolfSSL | 12:1a06964c2adb | 280 | #ifdef SHA3_BY_SPEC |
wolfSSL | 12:1a06964c2adb | 281 | /* Mix the row values. |
wolfSSL | 12:1a06964c2adb | 282 | * BMI1 has ANDN instruction ((~a) & b) - Haswell and above. |
wolfSSL | 12:1a06964c2adb | 283 | * |
wolfSSL | 12:1a06964c2adb | 284 | * s The state. |
wolfSSL | 12:1a06964c2adb | 285 | * b Temporary array of XORed row values. |
wolfSSL | 12:1a06964c2adb | 286 | * y The index of the row to work on. |
wolfSSL | 12:1a06964c2adb | 287 | * x The index of the column. |
wolfSSL | 12:1a06964c2adb | 288 | * t0 Temporary variable. |
wolfSSL | 12:1a06964c2adb | 289 | * t1 Temporary variable. |
wolfSSL | 12:1a06964c2adb | 290 | */ |
wolfSSL | 12:1a06964c2adb | 291 | #define ROW_MIX(s, b, y, x, t0, t1) \ |
wolfSSL | 12:1a06964c2adb | 292 | do \ |
wolfSSL | 12:1a06964c2adb | 293 | { \ |
wolfSSL | 12:1a06964c2adb | 294 | for (y = 0; y < 5; y++) \ |
wolfSSL | 12:1a06964c2adb | 295 | { \ |
wolfSSL | 12:1a06964c2adb | 296 | for (x = 0; x < 5; x++) \ |
wolfSSL | 12:1a06964c2adb | 297 | b[x] = s[y * 5 + x]; \ |
wolfSSL | 12:1a06964c2adb | 298 | for (x = 0; x < 5; x++) \ |
wolfSSL | 12:1a06964c2adb | 299 | s[y * 5 + x] = b[x] ^ (~b[(x + 1) % 5] & b[(x + 2) % 5]); \ |
wolfSSL | 12:1a06964c2adb | 300 | } \ |
wolfSSL | 12:1a06964c2adb | 301 | } \ |
wolfSSL | 12:1a06964c2adb | 302 | while (0) |
wolfSSL | 12:1a06964c2adb | 303 | #else |
wolfSSL | 12:1a06964c2adb | 304 | /* Mix the row values. |
wolfSSL | 12:1a06964c2adb | 305 | * a ^ (~b & c) == a ^ (c & (b ^ c)) == (a ^ b) ^ (b | c) |
wolfSSL | 12:1a06964c2adb | 306 | * |
wolfSSL | 12:1a06964c2adb | 307 | * s The state. |
wolfSSL | 12:1a06964c2adb | 308 | * b Temporary array of XORed row values. |
wolfSSL | 12:1a06964c2adb | 309 | * y The index of the row to work on. |
wolfSSL | 12:1a06964c2adb | 310 | * x The index of the column. |
wolfSSL | 12:1a06964c2adb | 311 | * t0 Temporary variable. |
wolfSSL | 12:1a06964c2adb | 312 | * t1 Temporary variable. |
wolfSSL | 12:1a06964c2adb | 313 | */ |
wolfSSL | 12:1a06964c2adb | 314 | #define ROW_MIX(s, b, y, x, t12, t34) \ |
wolfSSL | 12:1a06964c2adb | 315 | do \ |
wolfSSL | 12:1a06964c2adb | 316 | { \ |
wolfSSL | 12:1a06964c2adb | 317 | for (y = 0; y < 5; y++) \ |
wolfSSL | 12:1a06964c2adb | 318 | { \ |
wolfSSL | 12:1a06964c2adb | 319 | for (x = 0; x < 5; x++) \ |
wolfSSL | 12:1a06964c2adb | 320 | b[x] = s[y * 5 + x]; \ |
wolfSSL | 12:1a06964c2adb | 321 | t12 = (b[1] ^ b[2]); t34 = (b[3] ^ b[4]); \ |
wolfSSL | 12:1a06964c2adb | 322 | s[y * 5 + 0] = b[0] ^ (b[2] & t12); \ |
wolfSSL | 12:1a06964c2adb | 323 | s[y * 5 + 1] = t12 ^ (b[2] | b[3]); \ |
wolfSSL | 12:1a06964c2adb | 324 | s[y * 5 + 2] = b[2] ^ (b[4] & t34); \ |
wolfSSL | 12:1a06964c2adb | 325 | s[y * 5 + 3] = t34 ^ (b[4] | b[0]); \ |
wolfSSL | 12:1a06964c2adb | 326 | s[y * 5 + 4] = b[4] ^ (b[1] & (b[0] ^ b[1])); \ |
wolfSSL | 12:1a06964c2adb | 327 | } \ |
wolfSSL | 12:1a06964c2adb | 328 | } \ |
wolfSSL | 12:1a06964c2adb | 329 | while (0) |
wolfSSL | 12:1a06964c2adb | 330 | #endif |
wolfSSL | 12:1a06964c2adb | 331 | |
wolfSSL | 12:1a06964c2adb | 332 | /* The block operation performed on the state. |
wolfSSL | 12:1a06964c2adb | 333 | * |
wolfSSL | 12:1a06964c2adb | 334 | * s The state. |
wolfSSL | 12:1a06964c2adb | 335 | */ |
wolfSSL | 12:1a06964c2adb | 336 | static void BlockSha3(word64 *s) |
wolfSSL | 12:1a06964c2adb | 337 | { |
wolfSSL | 12:1a06964c2adb | 338 | byte i, x, y; |
wolfSSL | 12:1a06964c2adb | 339 | word64 t0, t1; |
wolfSSL | 12:1a06964c2adb | 340 | word64 b[5]; |
wolfSSL | 12:1a06964c2adb | 341 | |
wolfSSL | 12:1a06964c2adb | 342 | for (i = 0; i < 24; i++) |
wolfSSL | 12:1a06964c2adb | 343 | { |
wolfSSL | 12:1a06964c2adb | 344 | COL_MIX(s, b, x, t0); |
wolfSSL | 12:1a06964c2adb | 345 | |
wolfSSL | 12:1a06964c2adb | 346 | t0 = s[1]; |
wolfSSL | 12:1a06964c2adb | 347 | SWAP_ROTL(s, t0, t1, 0); |
wolfSSL | 12:1a06964c2adb | 348 | SWAP_ROTL(s, t1, t0, 1); |
wolfSSL | 12:1a06964c2adb | 349 | SWAP_ROTL(s, t0, t1, 2); |
wolfSSL | 12:1a06964c2adb | 350 | SWAP_ROTL(s, t1, t0, 3); |
wolfSSL | 12:1a06964c2adb | 351 | SWAP_ROTL(s, t0, t1, 4); |
wolfSSL | 12:1a06964c2adb | 352 | SWAP_ROTL(s, t1, t0, 5); |
wolfSSL | 12:1a06964c2adb | 353 | SWAP_ROTL(s, t0, t1, 6); |
wolfSSL | 12:1a06964c2adb | 354 | SWAP_ROTL(s, t1, t0, 7); |
wolfSSL | 12:1a06964c2adb | 355 | SWAP_ROTL(s, t0, t1, 8); |
wolfSSL | 12:1a06964c2adb | 356 | SWAP_ROTL(s, t1, t0, 9); |
wolfSSL | 12:1a06964c2adb | 357 | SWAP_ROTL(s, t0, t1, 10); |
wolfSSL | 12:1a06964c2adb | 358 | SWAP_ROTL(s, t1, t0, 11); |
wolfSSL | 12:1a06964c2adb | 359 | SWAP_ROTL(s, t0, t1, 12); |
wolfSSL | 12:1a06964c2adb | 360 | SWAP_ROTL(s, t1, t0, 13); |
wolfSSL | 12:1a06964c2adb | 361 | SWAP_ROTL(s, t0, t1, 14); |
wolfSSL | 12:1a06964c2adb | 362 | SWAP_ROTL(s, t1, t0, 15); |
wolfSSL | 12:1a06964c2adb | 363 | SWAP_ROTL(s, t0, t1, 16); |
wolfSSL | 12:1a06964c2adb | 364 | SWAP_ROTL(s, t1, t0, 17); |
wolfSSL | 12:1a06964c2adb | 365 | SWAP_ROTL(s, t0, t1, 18); |
wolfSSL | 12:1a06964c2adb | 366 | SWAP_ROTL(s, t1, t0, 19); |
wolfSSL | 12:1a06964c2adb | 367 | SWAP_ROTL(s, t0, t1, 20); |
wolfSSL | 12:1a06964c2adb | 368 | SWAP_ROTL(s, t1, t0, 21); |
wolfSSL | 12:1a06964c2adb | 369 | SWAP_ROTL(s, t0, t1, 22); |
wolfSSL | 12:1a06964c2adb | 370 | SWAP_ROTL(s, t1, t0, 23); |
wolfSSL | 12:1a06964c2adb | 371 | |
wolfSSL | 12:1a06964c2adb | 372 | ROW_MIX(s, b, y, x, t0, t1); |
wolfSSL | 12:1a06964c2adb | 373 | |
wolfSSL | 12:1a06964c2adb | 374 | s[0] ^= hash_keccak_r[i]; |
wolfSSL | 12:1a06964c2adb | 375 | } |
wolfSSL | 12:1a06964c2adb | 376 | } |
wolfSSL | 12:1a06964c2adb | 377 | #else |
wolfSSL | 12:1a06964c2adb | 378 | #include "sha3_long.i" |
wolfSSL | 12:1a06964c2adb | 379 | #endif |
wolfSSL | 12:1a06964c2adb | 380 | |
wolfSSL | 12:1a06964c2adb | 381 | /* Convert the array of bytes, in little-endian order, to a 64-bit integer. |
wolfSSL | 12:1a06964c2adb | 382 | * |
wolfSSL | 12:1a06964c2adb | 383 | * a Array of bytes. |
wolfSSL | 12:1a06964c2adb | 384 | * returns a 64-bit integer. |
wolfSSL | 12:1a06964c2adb | 385 | */ |
wolfSSL | 12:1a06964c2adb | 386 | static word64 Load64BitBigEndian(const byte* a) |
wolfSSL | 12:1a06964c2adb | 387 | { |
wolfSSL | 12:1a06964c2adb | 388 | #ifdef BIG_ENDIAN_ORDER |
wolfSSL | 12:1a06964c2adb | 389 | word64 n = 0; |
wolfSSL | 12:1a06964c2adb | 390 | int i; |
wolfSSL | 12:1a06964c2adb | 391 | |
wolfSSL | 12:1a06964c2adb | 392 | for (i = 0; i < 8; i++) |
wolfSSL | 12:1a06964c2adb | 393 | n |= (word64)a[i] << (8 * i); |
wolfSSL | 12:1a06964c2adb | 394 | |
wolfSSL | 12:1a06964c2adb | 395 | return n; |
wolfSSL | 12:1a06964c2adb | 396 | #else |
wolfSSL | 12:1a06964c2adb | 397 | return *(word64*)a; |
wolfSSL | 12:1a06964c2adb | 398 | #endif |
wolfSSL | 12:1a06964c2adb | 399 | } |
wolfSSL | 12:1a06964c2adb | 400 | |
wolfSSL | 12:1a06964c2adb | 401 | /* Initialize the state for a SHA3-224 hash operation. |
wolfSSL | 12:1a06964c2adb | 402 | * |
wolfSSL | 12:1a06964c2adb | 403 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 404 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 405 | */ |
wolfSSL | 12:1a06964c2adb | 406 | static int InitSha3(Sha3* sha3) |
wolfSSL | 12:1a06964c2adb | 407 | { |
wolfSSL | 12:1a06964c2adb | 408 | int i; |
wolfSSL | 12:1a06964c2adb | 409 | |
wolfSSL | 12:1a06964c2adb | 410 | for (i = 0; i < 25; i++) |
wolfSSL | 12:1a06964c2adb | 411 | sha3->s[i] = 0; |
wolfSSL | 12:1a06964c2adb | 412 | sha3->i = 0; |
wolfSSL | 12:1a06964c2adb | 413 | |
wolfSSL | 12:1a06964c2adb | 414 | return 0; |
wolfSSL | 12:1a06964c2adb | 415 | } |
wolfSSL | 12:1a06964c2adb | 416 | |
wolfSSL | 12:1a06964c2adb | 417 | /* Update the SHA-3 hash state with message data. |
wolfSSL | 12:1a06964c2adb | 418 | * |
wolfSSL | 12:1a06964c2adb | 419 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 420 | * data Message data to be hashed. |
wolfSSL | 12:1a06964c2adb | 421 | * len Length of the message data. |
wolfSSL | 12:1a06964c2adb | 422 | * p Number of 64-bit numbers in a block of data to process. |
wolfSSL | 12:1a06964c2adb | 423 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 424 | */ |
wolfSSL | 12:1a06964c2adb | 425 | static int Sha3Update(Sha3* sha3, const byte* data, word32 len, byte p) |
wolfSSL | 12:1a06964c2adb | 426 | { |
wolfSSL | 12:1a06964c2adb | 427 | byte i; |
wolfSSL | 12:1a06964c2adb | 428 | byte l; |
wolfSSL | 12:1a06964c2adb | 429 | byte *t; |
wolfSSL | 12:1a06964c2adb | 430 | |
wolfSSL | 12:1a06964c2adb | 431 | if (sha3->i > 0) |
wolfSSL | 12:1a06964c2adb | 432 | { |
wolfSSL | 12:1a06964c2adb | 433 | l = p * 8 - sha3->i; |
wolfSSL | 12:1a06964c2adb | 434 | if (l > len) |
wolfSSL | 12:1a06964c2adb | 435 | l = len; |
wolfSSL | 12:1a06964c2adb | 436 | |
wolfSSL | 12:1a06964c2adb | 437 | t = &sha3->t[sha3->i]; |
wolfSSL | 12:1a06964c2adb | 438 | for (i = 0; i < l; i++) |
wolfSSL | 12:1a06964c2adb | 439 | t[i] = data[i]; |
wolfSSL | 12:1a06964c2adb | 440 | data += i; |
wolfSSL | 12:1a06964c2adb | 441 | len -= i; |
wolfSSL | 12:1a06964c2adb | 442 | sha3->i += i; |
wolfSSL | 12:1a06964c2adb | 443 | |
wolfSSL | 12:1a06964c2adb | 444 | if (sha3->i == p * 8) |
wolfSSL | 12:1a06964c2adb | 445 | { |
wolfSSL | 12:1a06964c2adb | 446 | for (i = 0; i < p; i++) |
wolfSSL | 12:1a06964c2adb | 447 | sha3->s[i] ^= Load64BitBigEndian(sha3->t + 8 * i); |
wolfSSL | 12:1a06964c2adb | 448 | BlockSha3(sha3->s); |
wolfSSL | 12:1a06964c2adb | 449 | sha3->i = 0; |
wolfSSL | 12:1a06964c2adb | 450 | } |
wolfSSL | 12:1a06964c2adb | 451 | } |
wolfSSL | 12:1a06964c2adb | 452 | while (len >= p * 8) |
wolfSSL | 12:1a06964c2adb | 453 | { |
wolfSSL | 12:1a06964c2adb | 454 | for (i = 0; i < p; i++) |
wolfSSL | 12:1a06964c2adb | 455 | sha3->s[i] ^= Load64BitBigEndian(data + 8 * i); |
wolfSSL | 12:1a06964c2adb | 456 | BlockSha3(sha3->s); |
wolfSSL | 12:1a06964c2adb | 457 | len -= p * 8; |
wolfSSL | 12:1a06964c2adb | 458 | data += p * 8; |
wolfSSL | 12:1a06964c2adb | 459 | } |
wolfSSL | 12:1a06964c2adb | 460 | for (i = 0; i < len; i++) |
wolfSSL | 12:1a06964c2adb | 461 | sha3->t[i] = data[i]; |
wolfSSL | 12:1a06964c2adb | 462 | sha3->i += i; |
wolfSSL | 12:1a06964c2adb | 463 | |
wolfSSL | 12:1a06964c2adb | 464 | return 0; |
wolfSSL | 12:1a06964c2adb | 465 | } |
wolfSSL | 12:1a06964c2adb | 466 | |
wolfSSL | 12:1a06964c2adb | 467 | /* Calculate the SHA-3 hash based on all the message data seen. |
wolfSSL | 12:1a06964c2adb | 468 | * |
wolfSSL | 12:1a06964c2adb | 469 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 470 | * hash Buffer to hold the hash result. |
wolfSSL | 12:1a06964c2adb | 471 | * p Number of 64-bit numbers in a block of data to process. |
wolfSSL | 12:1a06964c2adb | 472 | * len Number of bytes in output. |
wolfSSL | 12:1a06964c2adb | 473 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 474 | */ |
wolfSSL | 12:1a06964c2adb | 475 | static int Sha3Final(Sha3* sha3, byte* hash, byte p, byte l) |
wolfSSL | 12:1a06964c2adb | 476 | { |
wolfSSL | 12:1a06964c2adb | 477 | byte i; |
wolfSSL | 12:1a06964c2adb | 478 | byte *s8 = (byte *)sha3->s; |
wolfSSL | 12:1a06964c2adb | 479 | |
wolfSSL | 12:1a06964c2adb | 480 | sha3->t[p * 8 - 1] = 0x00; |
wolfSSL | 12:1a06964c2adb | 481 | sha3->t[ sha3->i] = 0x06; |
wolfSSL | 12:1a06964c2adb | 482 | sha3->t[p * 8 - 1] |= 0x80; |
wolfSSL | 12:1a06964c2adb | 483 | for (i=sha3->i + 1; i < p * 8 - 1; i++) |
wolfSSL | 12:1a06964c2adb | 484 | sha3->t[i] = 0; |
wolfSSL | 12:1a06964c2adb | 485 | for (i = 0; i < p; i++) |
wolfSSL | 12:1a06964c2adb | 486 | sha3->s[i] ^= Load64BitBigEndian(sha3->t + 8 * i); |
wolfSSL | 12:1a06964c2adb | 487 | BlockSha3(sha3->s); |
wolfSSL | 12:1a06964c2adb | 488 | #if defined(BIG_ENDIAN_ORDER) |
wolfSSL | 12:1a06964c2adb | 489 | ByteReverseWords64(sha3->s, sha3->s, ((l+7)/8)*8); |
wolfSSL | 12:1a06964c2adb | 490 | #endif |
wolfSSL | 12:1a06964c2adb | 491 | for (i = 0; i < l; i++) |
wolfSSL | 12:1a06964c2adb | 492 | hash[i] = s8[i]; |
wolfSSL | 12:1a06964c2adb | 493 | |
wolfSSL | 12:1a06964c2adb | 494 | return 0; |
wolfSSL | 12:1a06964c2adb | 495 | } |
wolfSSL | 12:1a06964c2adb | 496 | |
wolfSSL | 12:1a06964c2adb | 497 | /* Initialize the state for a SHA-3 hash operation. |
wolfSSL | 12:1a06964c2adb | 498 | * |
wolfSSL | 12:1a06964c2adb | 499 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 500 | * heap Heap reference for dynamic memory allocation. (Used in async ops.) |
wolfSSL | 12:1a06964c2adb | 501 | * devId Device identifier for asynchronous operation. |
wolfSSL | 12:1a06964c2adb | 502 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 503 | */ |
wolfSSL | 12:1a06964c2adb | 504 | static int wc_InitSha3(Sha3* sha3, void* heap, int devId) |
wolfSSL | 12:1a06964c2adb | 505 | { |
wolfSSL | 12:1a06964c2adb | 506 | int ret = 0; |
wolfSSL | 12:1a06964c2adb | 507 | |
wolfSSL | 12:1a06964c2adb | 508 | if (sha3 == NULL) |
wolfSSL | 12:1a06964c2adb | 509 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 510 | |
wolfSSL | 12:1a06964c2adb | 511 | sha3->heap = heap; |
wolfSSL | 12:1a06964c2adb | 512 | ret = InitSha3(sha3); |
wolfSSL | 12:1a06964c2adb | 513 | if (ret != 0) |
wolfSSL | 12:1a06964c2adb | 514 | return ret; |
wolfSSL | 12:1a06964c2adb | 515 | |
wolfSSL | 12:1a06964c2adb | 516 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3) |
wolfSSL | 12:1a06964c2adb | 517 | ret = wolfAsync_DevCtxInit(&sha3->asyncDev, |
wolfSSL | 12:1a06964c2adb | 518 | WOLFSSL_ASYNC_MARKER_SHA3, sha3->heap, devId); |
wolfSSL | 12:1a06964c2adb | 519 | #else |
wolfSSL | 12:1a06964c2adb | 520 | (void)devId; |
wolfSSL | 12:1a06964c2adb | 521 | #endif /* WOLFSSL_ASYNC_CRYPT */ |
wolfSSL | 12:1a06964c2adb | 522 | |
wolfSSL | 12:1a06964c2adb | 523 | return ret; |
wolfSSL | 12:1a06964c2adb | 524 | } |
wolfSSL | 12:1a06964c2adb | 525 | |
wolfSSL | 12:1a06964c2adb | 526 | /* Update the SHA-3 hash state with message data. |
wolfSSL | 12:1a06964c2adb | 527 | * |
wolfSSL | 12:1a06964c2adb | 528 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 529 | * data Message data to be hashed. |
wolfSSL | 12:1a06964c2adb | 530 | * len Length of the message data. |
wolfSSL | 12:1a06964c2adb | 531 | * p Number of 64-bit numbers in a block of data to process. |
wolfSSL | 12:1a06964c2adb | 532 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 533 | */ |
wolfSSL | 12:1a06964c2adb | 534 | static int wc_Sha3Update(Sha3* sha3, const byte* data, word32 len, byte p) |
wolfSSL | 12:1a06964c2adb | 535 | { |
wolfSSL | 12:1a06964c2adb | 536 | int ret = 0; |
wolfSSL | 12:1a06964c2adb | 537 | |
wolfSSL | 12:1a06964c2adb | 538 | if (sha3 == NULL || (data == NULL && len > 0)) { |
wolfSSL | 12:1a06964c2adb | 539 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 540 | } |
wolfSSL | 12:1a06964c2adb | 541 | |
wolfSSL | 12:1a06964c2adb | 542 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3) |
wolfSSL | 12:1a06964c2adb | 543 | if (sha3->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA3) { |
wolfSSL | 12:1a06964c2adb | 544 | #if defined(HAVE_INTEL_QA) |
wolfSSL | 12:1a06964c2adb | 545 | return IntelQaSymSha3(&sha3->asyncDev, NULL, data, len); |
wolfSSL | 12:1a06964c2adb | 546 | #endif |
wolfSSL | 12:1a06964c2adb | 547 | } |
wolfSSL | 12:1a06964c2adb | 548 | #endif /* WOLFSSL_ASYNC_CRYPT */ |
wolfSSL | 12:1a06964c2adb | 549 | |
wolfSSL | 12:1a06964c2adb | 550 | Sha3Update(sha3, data, len, p); |
wolfSSL | 12:1a06964c2adb | 551 | |
wolfSSL | 12:1a06964c2adb | 552 | return ret; |
wolfSSL | 12:1a06964c2adb | 553 | } |
wolfSSL | 12:1a06964c2adb | 554 | |
wolfSSL | 12:1a06964c2adb | 555 | /* Calculate the SHA-3 hash based on all the message data seen. |
wolfSSL | 12:1a06964c2adb | 556 | * |
wolfSSL | 12:1a06964c2adb | 557 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 558 | * hash Buffer to hold the hash result. |
wolfSSL | 12:1a06964c2adb | 559 | * p Number of 64-bit numbers in a block of data to process. |
wolfSSL | 12:1a06964c2adb | 560 | * len Number of bytes in output. |
wolfSSL | 12:1a06964c2adb | 561 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 562 | */ |
wolfSSL | 12:1a06964c2adb | 563 | static int wc_Sha3Final(Sha3* sha3, byte* hash, byte p, byte len) |
wolfSSL | 12:1a06964c2adb | 564 | { |
wolfSSL | 12:1a06964c2adb | 565 | int ret; |
wolfSSL | 12:1a06964c2adb | 566 | |
wolfSSL | 12:1a06964c2adb | 567 | if (sha3 == NULL || hash == NULL) { |
wolfSSL | 12:1a06964c2adb | 568 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 569 | } |
wolfSSL | 12:1a06964c2adb | 570 | |
wolfSSL | 12:1a06964c2adb | 571 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3) |
wolfSSL | 12:1a06964c2adb | 572 | if (sha3->asyncDev.marker == WOLFSSL_ASYNC_MARKER_SHA3) { |
wolfSSL | 12:1a06964c2adb | 573 | #if defined(HAVE_INTEL_QA) |
wolfSSL | 12:1a06964c2adb | 574 | return IntelQaSymSha3(&sha3->asyncDev, hash, NULL, |
wolfSSL | 12:1a06964c2adb | 575 | SHA3_DIGEST_SIZE); |
wolfSSL | 12:1a06964c2adb | 576 | #endif |
wolfSSL | 12:1a06964c2adb | 577 | } |
wolfSSL | 12:1a06964c2adb | 578 | #endif /* WOLFSSL_ASYNC_CRYPT */ |
wolfSSL | 12:1a06964c2adb | 579 | |
wolfSSL | 12:1a06964c2adb | 580 | ret = Sha3Final(sha3, hash, p, len); |
wolfSSL | 12:1a06964c2adb | 581 | if (ret != 0) |
wolfSSL | 12:1a06964c2adb | 582 | return ret; |
wolfSSL | 12:1a06964c2adb | 583 | |
wolfSSL | 12:1a06964c2adb | 584 | return InitSha3(sha3); /* reset state */ |
wolfSSL | 12:1a06964c2adb | 585 | } |
wolfSSL | 12:1a06964c2adb | 586 | |
wolfSSL | 12:1a06964c2adb | 587 | /* Dispose of any dynamically allocated data from the SHA3-384 operation. |
wolfSSL | 12:1a06964c2adb | 588 | * (Required for async ops.) |
wolfSSL | 12:1a06964c2adb | 589 | * |
wolfSSL | 12:1a06964c2adb | 590 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 591 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 592 | */ |
wolfSSL | 12:1a06964c2adb | 593 | static void wc_Sha3Free(Sha3* sha3) |
wolfSSL | 12:1a06964c2adb | 594 | { |
wolfSSL | 12:1a06964c2adb | 595 | (void)sha3; |
wolfSSL | 12:1a06964c2adb | 596 | |
wolfSSL | 12:1a06964c2adb | 597 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_SHA3) |
wolfSSL | 12:1a06964c2adb | 598 | if (sha3 == NULL) |
wolfSSL | 12:1a06964c2adb | 599 | return; |
wolfSSL | 12:1a06964c2adb | 600 | |
wolfSSL | 12:1a06964c2adb | 601 | wolfAsync_DevCtxFree(&sha3->asyncDev, WOLFSSL_ASYNC_MARKER_SHA3); |
wolfSSL | 12:1a06964c2adb | 602 | #endif /* WOLFSSL_ASYNC_CRYPT */ |
wolfSSL | 12:1a06964c2adb | 603 | } |
wolfSSL | 12:1a06964c2adb | 604 | #endif /* HAVE_FIPS */ |
wolfSSL | 12:1a06964c2adb | 605 | |
wolfSSL | 12:1a06964c2adb | 606 | /* Copy the state of the SHA3 operation. |
wolfSSL | 12:1a06964c2adb | 607 | * |
wolfSSL | 12:1a06964c2adb | 608 | * src Sha3 object holding state top copy. |
wolfSSL | 12:1a06964c2adb | 609 | * dst Sha3 object to copy into. |
wolfSSL | 12:1a06964c2adb | 610 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 611 | */ |
wolfSSL | 12:1a06964c2adb | 612 | static int wc_Sha3Copy(Sha3* src, Sha3* dst) |
wolfSSL | 12:1a06964c2adb | 613 | { |
wolfSSL | 12:1a06964c2adb | 614 | int ret = 0; |
wolfSSL | 12:1a06964c2adb | 615 | |
wolfSSL | 12:1a06964c2adb | 616 | if (src == NULL || dst == NULL) |
wolfSSL | 12:1a06964c2adb | 617 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 618 | |
wolfSSL | 12:1a06964c2adb | 619 | XMEMCPY(dst, src, sizeof(Sha3)); |
wolfSSL | 12:1a06964c2adb | 620 | |
wolfSSL | 12:1a06964c2adb | 621 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 12:1a06964c2adb | 622 | ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev); |
wolfSSL | 12:1a06964c2adb | 623 | #endif |
wolfSSL | 12:1a06964c2adb | 624 | |
wolfSSL | 12:1a06964c2adb | 625 | return ret; |
wolfSSL | 12:1a06964c2adb | 626 | } |
wolfSSL | 12:1a06964c2adb | 627 | |
wolfSSL | 12:1a06964c2adb | 628 | /* Calculate the SHA3-224 hash based on all the message data so far. |
wolfSSL | 12:1a06964c2adb | 629 | * More message data can be added, after this operation, using the current |
wolfSSL | 12:1a06964c2adb | 630 | * state. |
wolfSSL | 12:1a06964c2adb | 631 | * |
wolfSSL | 12:1a06964c2adb | 632 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 633 | * hash Buffer to hold the hash result. Must be at least 28 bytes. |
wolfSSL | 12:1a06964c2adb | 634 | * p Number of 64-bit numbers in a block of data to process. |
wolfSSL | 12:1a06964c2adb | 635 | * len Number of bytes in output. |
wolfSSL | 12:1a06964c2adb | 636 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 637 | */ |
wolfSSL | 12:1a06964c2adb | 638 | static int wc_Sha3GetHash(Sha3* sha3, byte* hash, byte p, byte len) |
wolfSSL | 12:1a06964c2adb | 639 | { |
wolfSSL | 12:1a06964c2adb | 640 | int ret; |
wolfSSL | 12:1a06964c2adb | 641 | Sha3 tmpSha3; |
wolfSSL | 12:1a06964c2adb | 642 | |
wolfSSL | 12:1a06964c2adb | 643 | if (sha3 == NULL || hash == NULL) |
wolfSSL | 12:1a06964c2adb | 644 | return BAD_FUNC_ARG; |
wolfSSL | 12:1a06964c2adb | 645 | |
wolfSSL | 12:1a06964c2adb | 646 | ret = wc_Sha3Copy(sha3, &tmpSha3); |
wolfSSL | 12:1a06964c2adb | 647 | if (ret == 0) { |
wolfSSL | 12:1a06964c2adb | 648 | ret = wc_Sha3Final(&tmpSha3, hash, p, len); |
wolfSSL | 12:1a06964c2adb | 649 | } |
wolfSSL | 12:1a06964c2adb | 650 | return ret; |
wolfSSL | 12:1a06964c2adb | 651 | } |
wolfSSL | 12:1a06964c2adb | 652 | |
wolfSSL | 12:1a06964c2adb | 653 | |
wolfSSL | 12:1a06964c2adb | 654 | /* Initialize the state for a SHA3-224 hash operation. |
wolfSSL | 12:1a06964c2adb | 655 | * |
wolfSSL | 12:1a06964c2adb | 656 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 657 | * heap Heap reference for dynamic memory allocation. (Used in async ops.) |
wolfSSL | 12:1a06964c2adb | 658 | * devId Device identifier for asynchronous operation. |
wolfSSL | 12:1a06964c2adb | 659 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 660 | */ |
wolfSSL | 12:1a06964c2adb | 661 | WOLFSSL_API int wc_InitSha3_224(Sha3* sha3, void* heap, int devId) |
wolfSSL | 12:1a06964c2adb | 662 | { |
wolfSSL | 12:1a06964c2adb | 663 | return wc_InitSha3(sha3, heap, devId); |
wolfSSL | 12:1a06964c2adb | 664 | } |
wolfSSL | 12:1a06964c2adb | 665 | |
wolfSSL | 12:1a06964c2adb | 666 | /* Update the SHA3-224 hash state with message data. |
wolfSSL | 12:1a06964c2adb | 667 | * |
wolfSSL | 12:1a06964c2adb | 668 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 669 | * data Message data to be hashed. |
wolfSSL | 12:1a06964c2adb | 670 | * len Length of the message data. |
wolfSSL | 12:1a06964c2adb | 671 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 672 | */ |
wolfSSL | 12:1a06964c2adb | 673 | WOLFSSL_API int wc_Sha3_224_Update(Sha3* sha3, const byte* data, word32 len) |
wolfSSL | 12:1a06964c2adb | 674 | { |
wolfSSL | 12:1a06964c2adb | 675 | return wc_Sha3Update(sha3, data, len, SHA3_224_COUNT); |
wolfSSL | 12:1a06964c2adb | 676 | } |
wolfSSL | 12:1a06964c2adb | 677 | |
wolfSSL | 12:1a06964c2adb | 678 | /* Calculate the SHA3-224 hash based on all the message data seen. |
wolfSSL | 12:1a06964c2adb | 679 | * The state is initialized ready for a new message to hash. |
wolfSSL | 12:1a06964c2adb | 680 | * |
wolfSSL | 12:1a06964c2adb | 681 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 682 | * hash Buffer to hold the hash result. Must be at least 28 bytes. |
wolfSSL | 12:1a06964c2adb | 683 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 684 | */ |
wolfSSL | 12:1a06964c2adb | 685 | WOLFSSL_API int wc_Sha3_224_Final(Sha3* sha3, byte* hash) |
wolfSSL | 12:1a06964c2adb | 686 | { |
wolfSSL | 12:1a06964c2adb | 687 | return wc_Sha3Final(sha3, hash, SHA3_224_COUNT, SHA3_224_DIGEST_SIZE); |
wolfSSL | 12:1a06964c2adb | 688 | } |
wolfSSL | 12:1a06964c2adb | 689 | |
wolfSSL | 12:1a06964c2adb | 690 | /* Dispose of any dynamically allocated data from the SHA3-224 operation. |
wolfSSL | 12:1a06964c2adb | 691 | * (Required for async ops.) |
wolfSSL | 12:1a06964c2adb | 692 | * |
wolfSSL | 12:1a06964c2adb | 693 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 694 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 695 | */ |
wolfSSL | 12:1a06964c2adb | 696 | WOLFSSL_API void wc_Sha3_224_Free(Sha3* sha3) |
wolfSSL | 12:1a06964c2adb | 697 | { |
wolfSSL | 12:1a06964c2adb | 698 | wc_Sha3Free(sha3); |
wolfSSL | 12:1a06964c2adb | 699 | } |
wolfSSL | 12:1a06964c2adb | 700 | |
wolfSSL | 12:1a06964c2adb | 701 | /* Calculate the SHA3-224 hash based on all the message data so far. |
wolfSSL | 12:1a06964c2adb | 702 | * More message data can be added, after this operation, using the current |
wolfSSL | 12:1a06964c2adb | 703 | * state. |
wolfSSL | 12:1a06964c2adb | 704 | * |
wolfSSL | 12:1a06964c2adb | 705 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 706 | * hash Buffer to hold the hash result. Must be at least 28 bytes. |
wolfSSL | 12:1a06964c2adb | 707 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 708 | */ |
wolfSSL | 12:1a06964c2adb | 709 | WOLFSSL_API int wc_Sha3_224_GetHash(Sha3* sha3, byte* hash) |
wolfSSL | 12:1a06964c2adb | 710 | { |
wolfSSL | 12:1a06964c2adb | 711 | return wc_Sha3GetHash(sha3, hash, SHA3_224_COUNT, SHA3_224_DIGEST_SIZE); |
wolfSSL | 12:1a06964c2adb | 712 | } |
wolfSSL | 12:1a06964c2adb | 713 | |
wolfSSL | 12:1a06964c2adb | 714 | /* Copy the state of the SHA3-224 operation. |
wolfSSL | 12:1a06964c2adb | 715 | * |
wolfSSL | 12:1a06964c2adb | 716 | * src Sha3 object holding state top copy. |
wolfSSL | 12:1a06964c2adb | 717 | * dst Sha3 object to copy into. |
wolfSSL | 12:1a06964c2adb | 718 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 719 | */ |
wolfSSL | 12:1a06964c2adb | 720 | WOLFSSL_API int wc_Sha3_224_Copy(Sha3* src, Sha3* dst) |
wolfSSL | 12:1a06964c2adb | 721 | { |
wolfSSL | 12:1a06964c2adb | 722 | return wc_Sha3Copy(src, dst); |
wolfSSL | 12:1a06964c2adb | 723 | } |
wolfSSL | 12:1a06964c2adb | 724 | |
wolfSSL | 12:1a06964c2adb | 725 | |
wolfSSL | 12:1a06964c2adb | 726 | /* Initialize the state for a SHA3-256 hash operation. |
wolfSSL | 12:1a06964c2adb | 727 | * |
wolfSSL | 12:1a06964c2adb | 728 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 729 | * heap Heap reference for dynamic memory allocation. (Used in async ops.) |
wolfSSL | 12:1a06964c2adb | 730 | * devId Device identifier for asynchronous operation. |
wolfSSL | 12:1a06964c2adb | 731 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 732 | */ |
wolfSSL | 12:1a06964c2adb | 733 | WOLFSSL_API int wc_InitSha3_256(Sha3* sha3, void* heap, int devId) |
wolfSSL | 12:1a06964c2adb | 734 | { |
wolfSSL | 12:1a06964c2adb | 735 | return wc_InitSha3(sha3, heap, devId); |
wolfSSL | 12:1a06964c2adb | 736 | } |
wolfSSL | 12:1a06964c2adb | 737 | |
wolfSSL | 12:1a06964c2adb | 738 | /* Update the SHA3-256 hash state with message data. |
wolfSSL | 12:1a06964c2adb | 739 | * |
wolfSSL | 12:1a06964c2adb | 740 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 741 | * data Message data to be hashed. |
wolfSSL | 12:1a06964c2adb | 742 | * len Length of the message data. |
wolfSSL | 12:1a06964c2adb | 743 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 744 | */ |
wolfSSL | 12:1a06964c2adb | 745 | WOLFSSL_API int wc_Sha3_256_Update(Sha3* sha3, const byte* data, word32 len) |
wolfSSL | 12:1a06964c2adb | 746 | { |
wolfSSL | 12:1a06964c2adb | 747 | return wc_Sha3Update(sha3, data, len, SHA3_256_COUNT); |
wolfSSL | 12:1a06964c2adb | 748 | } |
wolfSSL | 12:1a06964c2adb | 749 | |
wolfSSL | 12:1a06964c2adb | 750 | /* Calculate the SHA3-256 hash based on all the message data seen. |
wolfSSL | 12:1a06964c2adb | 751 | * The state is initialized ready for a new message to hash. |
wolfSSL | 12:1a06964c2adb | 752 | * |
wolfSSL | 12:1a06964c2adb | 753 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 754 | * hash Buffer to hold the hash result. Must be at least 32 bytes. |
wolfSSL | 12:1a06964c2adb | 755 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 756 | */ |
wolfSSL | 12:1a06964c2adb | 757 | WOLFSSL_API int wc_Sha3_256_Final(Sha3* sha3, byte* hash) |
wolfSSL | 12:1a06964c2adb | 758 | { |
wolfSSL | 12:1a06964c2adb | 759 | return wc_Sha3Final(sha3, hash, SHA3_256_COUNT, SHA3_256_DIGEST_SIZE); |
wolfSSL | 12:1a06964c2adb | 760 | } |
wolfSSL | 12:1a06964c2adb | 761 | |
wolfSSL | 12:1a06964c2adb | 762 | /* Dispose of any dynamically allocated data from the SHA3-256 operation. |
wolfSSL | 12:1a06964c2adb | 763 | * (Required for async ops.) |
wolfSSL | 12:1a06964c2adb | 764 | * |
wolfSSL | 12:1a06964c2adb | 765 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 766 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 767 | */ |
wolfSSL | 12:1a06964c2adb | 768 | WOLFSSL_API void wc_Sha3_256_Free(Sha3* sha3) |
wolfSSL | 12:1a06964c2adb | 769 | { |
wolfSSL | 12:1a06964c2adb | 770 | wc_Sha3Free(sha3); |
wolfSSL | 12:1a06964c2adb | 771 | } |
wolfSSL | 12:1a06964c2adb | 772 | |
wolfSSL | 12:1a06964c2adb | 773 | /* Calculate the SHA3-256 hash based on all the message data so far. |
wolfSSL | 12:1a06964c2adb | 774 | * More message data can be added, after this operation, using the current |
wolfSSL | 12:1a06964c2adb | 775 | * state. |
wolfSSL | 12:1a06964c2adb | 776 | * |
wolfSSL | 12:1a06964c2adb | 777 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 778 | * hash Buffer to hold the hash result. Must be at least 32 bytes. |
wolfSSL | 12:1a06964c2adb | 779 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 780 | */ |
wolfSSL | 12:1a06964c2adb | 781 | WOLFSSL_API int wc_Sha3_256_GetHash(Sha3* sha3, byte* hash) |
wolfSSL | 12:1a06964c2adb | 782 | { |
wolfSSL | 12:1a06964c2adb | 783 | return wc_Sha3GetHash(sha3, hash, SHA3_256_COUNT, SHA3_256_DIGEST_SIZE); |
wolfSSL | 12:1a06964c2adb | 784 | } |
wolfSSL | 12:1a06964c2adb | 785 | |
wolfSSL | 12:1a06964c2adb | 786 | /* Copy the state of the SHA3-256 operation. |
wolfSSL | 12:1a06964c2adb | 787 | * |
wolfSSL | 12:1a06964c2adb | 788 | * src Sha3 object holding state top copy. |
wolfSSL | 12:1a06964c2adb | 789 | * dst Sha3 object to copy into. |
wolfSSL | 12:1a06964c2adb | 790 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 791 | */ |
wolfSSL | 12:1a06964c2adb | 792 | WOLFSSL_API int wc_Sha3_256_Copy(Sha3* src, Sha3* dst) |
wolfSSL | 12:1a06964c2adb | 793 | { |
wolfSSL | 12:1a06964c2adb | 794 | return wc_Sha3Copy(src, dst); |
wolfSSL | 12:1a06964c2adb | 795 | } |
wolfSSL | 12:1a06964c2adb | 796 | |
wolfSSL | 12:1a06964c2adb | 797 | |
wolfSSL | 12:1a06964c2adb | 798 | /* Initialize the state for a SHA3-384 hash operation. |
wolfSSL | 12:1a06964c2adb | 799 | * |
wolfSSL | 12:1a06964c2adb | 800 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 801 | * heap Heap reference for dynamic memory allocation. (Used in async ops.) |
wolfSSL | 12:1a06964c2adb | 802 | * devId Device identifier for asynchronous operation. |
wolfSSL | 12:1a06964c2adb | 803 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 804 | */ |
wolfSSL | 12:1a06964c2adb | 805 | WOLFSSL_API int wc_InitSha3_384(Sha3* sha3, void* heap, int devId) |
wolfSSL | 12:1a06964c2adb | 806 | { |
wolfSSL | 12:1a06964c2adb | 807 | return wc_InitSha3(sha3, heap, devId); |
wolfSSL | 12:1a06964c2adb | 808 | } |
wolfSSL | 12:1a06964c2adb | 809 | |
wolfSSL | 12:1a06964c2adb | 810 | /* Update the SHA3-384 hash state with message data. |
wolfSSL | 12:1a06964c2adb | 811 | * |
wolfSSL | 12:1a06964c2adb | 812 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 813 | * data Message data to be hashed. |
wolfSSL | 12:1a06964c2adb | 814 | * len Length of the message data. |
wolfSSL | 12:1a06964c2adb | 815 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 816 | */ |
wolfSSL | 12:1a06964c2adb | 817 | WOLFSSL_API int wc_Sha3_384_Update(Sha3* sha3, const byte* data, word32 len) |
wolfSSL | 12:1a06964c2adb | 818 | { |
wolfSSL | 12:1a06964c2adb | 819 | return wc_Sha3Update(sha3, data, len, SHA3_384_COUNT); |
wolfSSL | 12:1a06964c2adb | 820 | } |
wolfSSL | 12:1a06964c2adb | 821 | |
wolfSSL | 12:1a06964c2adb | 822 | /* Calculate the SHA3-384 hash based on all the message data seen. |
wolfSSL | 12:1a06964c2adb | 823 | * The state is initialized ready for a new message to hash. |
wolfSSL | 12:1a06964c2adb | 824 | * |
wolfSSL | 12:1a06964c2adb | 825 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 826 | * hash Buffer to hold the hash result. Must be at least 48 bytes. |
wolfSSL | 12:1a06964c2adb | 827 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 828 | */ |
wolfSSL | 12:1a06964c2adb | 829 | WOLFSSL_API int wc_Sha3_384_Final(Sha3* sha3, byte* hash) |
wolfSSL | 12:1a06964c2adb | 830 | { |
wolfSSL | 12:1a06964c2adb | 831 | return wc_Sha3Final(sha3, hash, SHA3_384_COUNT, SHA3_384_DIGEST_SIZE); |
wolfSSL | 12:1a06964c2adb | 832 | } |
wolfSSL | 12:1a06964c2adb | 833 | |
wolfSSL | 12:1a06964c2adb | 834 | /* Dispose of any dynamically allocated data from the SHA3-384 operation. |
wolfSSL | 12:1a06964c2adb | 835 | * (Required for async ops.) |
wolfSSL | 12:1a06964c2adb | 836 | * |
wolfSSL | 12:1a06964c2adb | 837 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 838 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 839 | */ |
wolfSSL | 12:1a06964c2adb | 840 | WOLFSSL_API void wc_Sha3_384_Free(Sha3* sha3) |
wolfSSL | 12:1a06964c2adb | 841 | { |
wolfSSL | 12:1a06964c2adb | 842 | wc_Sha3Free(sha3); |
wolfSSL | 12:1a06964c2adb | 843 | } |
wolfSSL | 12:1a06964c2adb | 844 | |
wolfSSL | 12:1a06964c2adb | 845 | /* Calculate the SHA3-384 hash based on all the message data so far. |
wolfSSL | 12:1a06964c2adb | 846 | * More message data can be added, after this operation, using the current |
wolfSSL | 12:1a06964c2adb | 847 | * state. |
wolfSSL | 12:1a06964c2adb | 848 | * |
wolfSSL | 12:1a06964c2adb | 849 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 850 | * hash Buffer to hold the hash result. Must be at least 48 bytes. |
wolfSSL | 12:1a06964c2adb | 851 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 852 | */ |
wolfSSL | 12:1a06964c2adb | 853 | WOLFSSL_API int wc_Sha3_384_GetHash(Sha3* sha3, byte* hash) |
wolfSSL | 12:1a06964c2adb | 854 | { |
wolfSSL | 12:1a06964c2adb | 855 | return wc_Sha3GetHash(sha3, hash, SHA3_384_COUNT, SHA3_384_DIGEST_SIZE); |
wolfSSL | 12:1a06964c2adb | 856 | } |
wolfSSL | 12:1a06964c2adb | 857 | |
wolfSSL | 12:1a06964c2adb | 858 | /* Copy the state of the SHA3-384 operation. |
wolfSSL | 12:1a06964c2adb | 859 | * |
wolfSSL | 12:1a06964c2adb | 860 | * src Sha3 object holding state top copy. |
wolfSSL | 12:1a06964c2adb | 861 | * dst Sha3 object to copy into. |
wolfSSL | 12:1a06964c2adb | 862 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 863 | */ |
wolfSSL | 12:1a06964c2adb | 864 | WOLFSSL_API int wc_Sha3_384_Copy(Sha3* src, Sha3* dst) |
wolfSSL | 12:1a06964c2adb | 865 | { |
wolfSSL | 12:1a06964c2adb | 866 | return wc_Sha3Copy(src, dst); |
wolfSSL | 12:1a06964c2adb | 867 | } |
wolfSSL | 12:1a06964c2adb | 868 | |
wolfSSL | 12:1a06964c2adb | 869 | |
wolfSSL | 12:1a06964c2adb | 870 | /* Initialize the state for a SHA3-512 hash operation. |
wolfSSL | 12:1a06964c2adb | 871 | * |
wolfSSL | 12:1a06964c2adb | 872 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 873 | * heap Heap reference for dynamic memory allocation. (Used in async ops.) |
wolfSSL | 12:1a06964c2adb | 874 | * devId Device identifier for asynchronous operation. |
wolfSSL | 12:1a06964c2adb | 875 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 876 | */ |
wolfSSL | 12:1a06964c2adb | 877 | WOLFSSL_API int wc_InitSha3_512(Sha3* sha3, void* heap, int devId) |
wolfSSL | 12:1a06964c2adb | 878 | { |
wolfSSL | 12:1a06964c2adb | 879 | return wc_InitSha3(sha3, heap, devId); |
wolfSSL | 12:1a06964c2adb | 880 | } |
wolfSSL | 12:1a06964c2adb | 881 | |
wolfSSL | 12:1a06964c2adb | 882 | /* Update the SHA3-512 hash state with message data. |
wolfSSL | 12:1a06964c2adb | 883 | * |
wolfSSL | 12:1a06964c2adb | 884 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 885 | * data Message data to be hashed. |
wolfSSL | 12:1a06964c2adb | 886 | * len Length of the message data. |
wolfSSL | 12:1a06964c2adb | 887 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 888 | */ |
wolfSSL | 12:1a06964c2adb | 889 | WOLFSSL_API int wc_Sha3_512_Update(Sha3* sha3, const byte* data, word32 len) |
wolfSSL | 12:1a06964c2adb | 890 | { |
wolfSSL | 12:1a06964c2adb | 891 | return wc_Sha3Update(sha3, data, len, SHA3_512_COUNT); |
wolfSSL | 12:1a06964c2adb | 892 | } |
wolfSSL | 12:1a06964c2adb | 893 | |
wolfSSL | 12:1a06964c2adb | 894 | /* Calculate the SHA3-512 hash based on all the message data seen. |
wolfSSL | 12:1a06964c2adb | 895 | * The state is initialized ready for a new message to hash. |
wolfSSL | 12:1a06964c2adb | 896 | * |
wolfSSL | 12:1a06964c2adb | 897 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 898 | * hash Buffer to hold the hash result. Must be at least 64 bytes. |
wolfSSL | 12:1a06964c2adb | 899 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 900 | */ |
wolfSSL | 12:1a06964c2adb | 901 | WOLFSSL_API int wc_Sha3_512_Final(Sha3* sha3, byte* hash) |
wolfSSL | 12:1a06964c2adb | 902 | { |
wolfSSL | 12:1a06964c2adb | 903 | return wc_Sha3Final(sha3, hash, SHA3_512_COUNT, SHA3_512_DIGEST_SIZE); |
wolfSSL | 12:1a06964c2adb | 904 | } |
wolfSSL | 12:1a06964c2adb | 905 | |
wolfSSL | 12:1a06964c2adb | 906 | /* Dispose of any dynamically allocated data from the SHA3-512 operation. |
wolfSSL | 12:1a06964c2adb | 907 | * (Required for async ops.) |
wolfSSL | 12:1a06964c2adb | 908 | * |
wolfSSL | 12:1a06964c2adb | 909 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 910 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 911 | */ |
wolfSSL | 12:1a06964c2adb | 912 | WOLFSSL_API void wc_Sha3_512_Free(Sha3* sha3) |
wolfSSL | 12:1a06964c2adb | 913 | { |
wolfSSL | 12:1a06964c2adb | 914 | wc_Sha3Free(sha3); |
wolfSSL | 12:1a06964c2adb | 915 | } |
wolfSSL | 12:1a06964c2adb | 916 | |
wolfSSL | 12:1a06964c2adb | 917 | /* Calculate the SHA3-512 hash based on all the message data so far. |
wolfSSL | 12:1a06964c2adb | 918 | * More message data can be added, after this operation, using the current |
wolfSSL | 12:1a06964c2adb | 919 | * state. |
wolfSSL | 12:1a06964c2adb | 920 | * |
wolfSSL | 12:1a06964c2adb | 921 | * sha3 Sha3 object holding state. |
wolfSSL | 12:1a06964c2adb | 922 | * hash Buffer to hold the hash result. Must be at least 64 bytes. |
wolfSSL | 12:1a06964c2adb | 923 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 924 | */ |
wolfSSL | 12:1a06964c2adb | 925 | WOLFSSL_API int wc_Sha3_512_GetHash(Sha3* sha3, byte* hash) |
wolfSSL | 12:1a06964c2adb | 926 | { |
wolfSSL | 12:1a06964c2adb | 927 | return wc_Sha3GetHash(sha3, hash, SHA3_512_COUNT, SHA3_512_DIGEST_SIZE); |
wolfSSL | 12:1a06964c2adb | 928 | } |
wolfSSL | 12:1a06964c2adb | 929 | |
wolfSSL | 12:1a06964c2adb | 930 | /* Copy the state of the SHA3-512 operation. |
wolfSSL | 12:1a06964c2adb | 931 | * |
wolfSSL | 12:1a06964c2adb | 932 | * src Sha3 object holding state top copy. |
wolfSSL | 12:1a06964c2adb | 933 | * dst Sha3 object to copy into. |
wolfSSL | 12:1a06964c2adb | 934 | * returns 0 on success. |
wolfSSL | 12:1a06964c2adb | 935 | */ |
wolfSSL | 12:1a06964c2adb | 936 | WOLFSSL_API int wc_Sha3_512_Copy(Sha3* src, Sha3* dst) |
wolfSSL | 12:1a06964c2adb | 937 | { |
wolfSSL | 12:1a06964c2adb | 938 | return wc_Sha3Copy(src, dst); |
wolfSSL | 12:1a06964c2adb | 939 | } |
wolfSSL | 12:1a06964c2adb | 940 | |
wolfSSL | 12:1a06964c2adb | 941 | #endif /* WOLFSSL_SHA3 */ |
wolfSSL | 12:1a06964c2adb | 942 |