Xuyi Wang / wolfSSL

Dependents:   OS

Committer:
wolfSSL
Date:
Sat Aug 18 22:20:43 2018 +0000
Revision:
15:117db924cf7c
wolfSSL 3.15.3

Who changed what in which revision?

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