Fork of CyaSSL for my specific settings

Dependents:   CyaSSL_Example

Fork of CyaSSL by wolf SSL

Committer:
wolfSSL
Date:
Sat Jul 12 07:18:23 2014 +0000
Revision:
0:1239e9b70ca2
CyaSSL 3.0.0;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wolfSSL 0:1239e9b70ca2 1 /* sha512.c
wolfSSL 0:1239e9b70ca2 2 *
wolfSSL 0:1239e9b70ca2 3 * Copyright (C) 2006-2014 wolfSSL Inc.
wolfSSL 0:1239e9b70ca2 4 *
wolfSSL 0:1239e9b70ca2 5 * This file is part of CyaSSL.
wolfSSL 0:1239e9b70ca2 6 *
wolfSSL 0:1239e9b70ca2 7 * CyaSSL is free software; you can redistribute it and/or modify
wolfSSL 0:1239e9b70ca2 8 * it under the terms of the GNU General Public License as published by
wolfSSL 0:1239e9b70ca2 9 * the Free Software Foundation; either version 2 of the License, or
wolfSSL 0:1239e9b70ca2 10 * (at your option) any later version.
wolfSSL 0:1239e9b70ca2 11 *
wolfSSL 0:1239e9b70ca2 12 * CyaSSL is distributed in the hope that it will be useful,
wolfSSL 0:1239e9b70ca2 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wolfSSL 0:1239e9b70ca2 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wolfSSL 0:1239e9b70ca2 15 * GNU General Public License for more details.
wolfSSL 0:1239e9b70ca2 16 *
wolfSSL 0:1239e9b70ca2 17 * You should have received a copy of the GNU General Public License
wolfSSL 0:1239e9b70ca2 18 * along with this program; if not, write to the Free Software
wolfSSL 0:1239e9b70ca2 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
wolfSSL 0:1239e9b70ca2 20 */
wolfSSL 0:1239e9b70ca2 21
wolfSSL 0:1239e9b70ca2 22 #ifdef HAVE_CONFIG_H
wolfSSL 0:1239e9b70ca2 23 #include <config.h>
wolfSSL 0:1239e9b70ca2 24 #endif
wolfSSL 0:1239e9b70ca2 25
wolfSSL 0:1239e9b70ca2 26 #include <cyassl/ctaocrypt/settings.h>
wolfSSL 0:1239e9b70ca2 27
wolfSSL 0:1239e9b70ca2 28 #ifdef CYASSL_SHA512
wolfSSL 0:1239e9b70ca2 29
wolfSSL 0:1239e9b70ca2 30 #ifdef HAVE_FIPS
wolfSSL 0:1239e9b70ca2 31 /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
wolfSSL 0:1239e9b70ca2 32 #define FIPS_NO_WRAPPERS
wolfSSL 0:1239e9b70ca2 33 #endif
wolfSSL 0:1239e9b70ca2 34
wolfSSL 0:1239e9b70ca2 35 #include <cyassl/ctaocrypt/sha512.h>
wolfSSL 0:1239e9b70ca2 36 #include <cyassl/ctaocrypt/error-crypt.h>
wolfSSL 0:1239e9b70ca2 37 #ifdef NO_INLINE
wolfSSL 0:1239e9b70ca2 38 #include <cyassl/ctaocrypt/misc.h>
wolfSSL 0:1239e9b70ca2 39 #else
wolfSSL 0:1239e9b70ca2 40 #include <ctaocrypt/src/misc.c>
wolfSSL 0:1239e9b70ca2 41 #endif
wolfSSL 0:1239e9b70ca2 42
wolfSSL 0:1239e9b70ca2 43
wolfSSL 0:1239e9b70ca2 44 #ifndef min
wolfSSL 0:1239e9b70ca2 45
wolfSSL 0:1239e9b70ca2 46 static INLINE word32 min(word32 a, word32 b)
wolfSSL 0:1239e9b70ca2 47 {
wolfSSL 0:1239e9b70ca2 48 return a > b ? b : a;
wolfSSL 0:1239e9b70ca2 49 }
wolfSSL 0:1239e9b70ca2 50
wolfSSL 0:1239e9b70ca2 51 #endif /* min */
wolfSSL 0:1239e9b70ca2 52
wolfSSL 0:1239e9b70ca2 53
wolfSSL 0:1239e9b70ca2 54 int InitSha512(Sha512* sha512)
wolfSSL 0:1239e9b70ca2 55 {
wolfSSL 0:1239e9b70ca2 56 sha512->digest[0] = W64LIT(0x6a09e667f3bcc908);
wolfSSL 0:1239e9b70ca2 57 sha512->digest[1] = W64LIT(0xbb67ae8584caa73b);
wolfSSL 0:1239e9b70ca2 58 sha512->digest[2] = W64LIT(0x3c6ef372fe94f82b);
wolfSSL 0:1239e9b70ca2 59 sha512->digest[3] = W64LIT(0xa54ff53a5f1d36f1);
wolfSSL 0:1239e9b70ca2 60 sha512->digest[4] = W64LIT(0x510e527fade682d1);
wolfSSL 0:1239e9b70ca2 61 sha512->digest[5] = W64LIT(0x9b05688c2b3e6c1f);
wolfSSL 0:1239e9b70ca2 62 sha512->digest[6] = W64LIT(0x1f83d9abfb41bd6b);
wolfSSL 0:1239e9b70ca2 63 sha512->digest[7] = W64LIT(0x5be0cd19137e2179);
wolfSSL 0:1239e9b70ca2 64
wolfSSL 0:1239e9b70ca2 65 sha512->buffLen = 0;
wolfSSL 0:1239e9b70ca2 66 sha512->loLen = 0;
wolfSSL 0:1239e9b70ca2 67 sha512->hiLen = 0;
wolfSSL 0:1239e9b70ca2 68
wolfSSL 0:1239e9b70ca2 69 return 0;
wolfSSL 0:1239e9b70ca2 70 }
wolfSSL 0:1239e9b70ca2 71
wolfSSL 0:1239e9b70ca2 72
wolfSSL 0:1239e9b70ca2 73 static const word64 K512[80] = {
wolfSSL 0:1239e9b70ca2 74 W64LIT(0x428a2f98d728ae22), W64LIT(0x7137449123ef65cd),
wolfSSL 0:1239e9b70ca2 75 W64LIT(0xb5c0fbcfec4d3b2f), W64LIT(0xe9b5dba58189dbbc),
wolfSSL 0:1239e9b70ca2 76 W64LIT(0x3956c25bf348b538), W64LIT(0x59f111f1b605d019),
wolfSSL 0:1239e9b70ca2 77 W64LIT(0x923f82a4af194f9b), W64LIT(0xab1c5ed5da6d8118),
wolfSSL 0:1239e9b70ca2 78 W64LIT(0xd807aa98a3030242), W64LIT(0x12835b0145706fbe),
wolfSSL 0:1239e9b70ca2 79 W64LIT(0x243185be4ee4b28c), W64LIT(0x550c7dc3d5ffb4e2),
wolfSSL 0:1239e9b70ca2 80 W64LIT(0x72be5d74f27b896f), W64LIT(0x80deb1fe3b1696b1),
wolfSSL 0:1239e9b70ca2 81 W64LIT(0x9bdc06a725c71235), W64LIT(0xc19bf174cf692694),
wolfSSL 0:1239e9b70ca2 82 W64LIT(0xe49b69c19ef14ad2), W64LIT(0xefbe4786384f25e3),
wolfSSL 0:1239e9b70ca2 83 W64LIT(0x0fc19dc68b8cd5b5), W64LIT(0x240ca1cc77ac9c65),
wolfSSL 0:1239e9b70ca2 84 W64LIT(0x2de92c6f592b0275), W64LIT(0x4a7484aa6ea6e483),
wolfSSL 0:1239e9b70ca2 85 W64LIT(0x5cb0a9dcbd41fbd4), W64LIT(0x76f988da831153b5),
wolfSSL 0:1239e9b70ca2 86 W64LIT(0x983e5152ee66dfab), W64LIT(0xa831c66d2db43210),
wolfSSL 0:1239e9b70ca2 87 W64LIT(0xb00327c898fb213f), W64LIT(0xbf597fc7beef0ee4),
wolfSSL 0:1239e9b70ca2 88 W64LIT(0xc6e00bf33da88fc2), W64LIT(0xd5a79147930aa725),
wolfSSL 0:1239e9b70ca2 89 W64LIT(0x06ca6351e003826f), W64LIT(0x142929670a0e6e70),
wolfSSL 0:1239e9b70ca2 90 W64LIT(0x27b70a8546d22ffc), W64LIT(0x2e1b21385c26c926),
wolfSSL 0:1239e9b70ca2 91 W64LIT(0x4d2c6dfc5ac42aed), W64LIT(0x53380d139d95b3df),
wolfSSL 0:1239e9b70ca2 92 W64LIT(0x650a73548baf63de), W64LIT(0x766a0abb3c77b2a8),
wolfSSL 0:1239e9b70ca2 93 W64LIT(0x81c2c92e47edaee6), W64LIT(0x92722c851482353b),
wolfSSL 0:1239e9b70ca2 94 W64LIT(0xa2bfe8a14cf10364), W64LIT(0xa81a664bbc423001),
wolfSSL 0:1239e9b70ca2 95 W64LIT(0xc24b8b70d0f89791), W64LIT(0xc76c51a30654be30),
wolfSSL 0:1239e9b70ca2 96 W64LIT(0xd192e819d6ef5218), W64LIT(0xd69906245565a910),
wolfSSL 0:1239e9b70ca2 97 W64LIT(0xf40e35855771202a), W64LIT(0x106aa07032bbd1b8),
wolfSSL 0:1239e9b70ca2 98 W64LIT(0x19a4c116b8d2d0c8), W64LIT(0x1e376c085141ab53),
wolfSSL 0:1239e9b70ca2 99 W64LIT(0x2748774cdf8eeb99), W64LIT(0x34b0bcb5e19b48a8),
wolfSSL 0:1239e9b70ca2 100 W64LIT(0x391c0cb3c5c95a63), W64LIT(0x4ed8aa4ae3418acb),
wolfSSL 0:1239e9b70ca2 101 W64LIT(0x5b9cca4f7763e373), W64LIT(0x682e6ff3d6b2b8a3),
wolfSSL 0:1239e9b70ca2 102 W64LIT(0x748f82ee5defb2fc), W64LIT(0x78a5636f43172f60),
wolfSSL 0:1239e9b70ca2 103 W64LIT(0x84c87814a1f0ab72), W64LIT(0x8cc702081a6439ec),
wolfSSL 0:1239e9b70ca2 104 W64LIT(0x90befffa23631e28), W64LIT(0xa4506cebde82bde9),
wolfSSL 0:1239e9b70ca2 105 W64LIT(0xbef9a3f7b2c67915), W64LIT(0xc67178f2e372532b),
wolfSSL 0:1239e9b70ca2 106 W64LIT(0xca273eceea26619c), W64LIT(0xd186b8c721c0c207),
wolfSSL 0:1239e9b70ca2 107 W64LIT(0xeada7dd6cde0eb1e), W64LIT(0xf57d4f7fee6ed178),
wolfSSL 0:1239e9b70ca2 108 W64LIT(0x06f067aa72176fba), W64LIT(0x0a637dc5a2c898a6),
wolfSSL 0:1239e9b70ca2 109 W64LIT(0x113f9804bef90dae), W64LIT(0x1b710b35131c471b),
wolfSSL 0:1239e9b70ca2 110 W64LIT(0x28db77f523047d84), W64LIT(0x32caab7b40c72493),
wolfSSL 0:1239e9b70ca2 111 W64LIT(0x3c9ebe0a15c9bebc), W64LIT(0x431d67c49c100d4c),
wolfSSL 0:1239e9b70ca2 112 W64LIT(0x4cc5d4becb3e42b6), W64LIT(0x597f299cfc657e2a),
wolfSSL 0:1239e9b70ca2 113 W64LIT(0x5fcb6fab3ad6faec), W64LIT(0x6c44198c4a475817)
wolfSSL 0:1239e9b70ca2 114 };
wolfSSL 0:1239e9b70ca2 115
wolfSSL 0:1239e9b70ca2 116
wolfSSL 0:1239e9b70ca2 117 #define blk0(i) (W[i] = sha512->buffer[i])
wolfSSL 0:1239e9b70ca2 118 #define blk2(i) (W[i&15]+=s1(W[(i-2)&15])+W[(i-7)&15]+s0(W[(i-15)&15]))
wolfSSL 0:1239e9b70ca2 119
wolfSSL 0:1239e9b70ca2 120 #define Ch(x,y,z) (z^(x&(y^z)))
wolfSSL 0:1239e9b70ca2 121 #define Maj(x,y,z) ((x&y)|(z&(x|y)))
wolfSSL 0:1239e9b70ca2 122
wolfSSL 0:1239e9b70ca2 123 #define a(i) T[(0-i)&7]
wolfSSL 0:1239e9b70ca2 124 #define b(i) T[(1-i)&7]
wolfSSL 0:1239e9b70ca2 125 #define c(i) T[(2-i)&7]
wolfSSL 0:1239e9b70ca2 126 #define d(i) T[(3-i)&7]
wolfSSL 0:1239e9b70ca2 127 #define e(i) T[(4-i)&7]
wolfSSL 0:1239e9b70ca2 128 #define f(i) T[(5-i)&7]
wolfSSL 0:1239e9b70ca2 129 #define g(i) T[(6-i)&7]
wolfSSL 0:1239e9b70ca2 130 #define h(i) T[(7-i)&7]
wolfSSL 0:1239e9b70ca2 131
wolfSSL 0:1239e9b70ca2 132 #define S0(x) (rotrFixed64(x,28)^rotrFixed64(x,34)^rotrFixed64(x,39))
wolfSSL 0:1239e9b70ca2 133 #define S1(x) (rotrFixed64(x,14)^rotrFixed64(x,18)^rotrFixed64(x,41))
wolfSSL 0:1239e9b70ca2 134 #define s0(x) (rotrFixed64(x,1)^rotrFixed64(x,8)^(x>>7))
wolfSSL 0:1239e9b70ca2 135 #define s1(x) (rotrFixed64(x,19)^rotrFixed64(x,61)^(x>>6))
wolfSSL 0:1239e9b70ca2 136
wolfSSL 0:1239e9b70ca2 137 #define R(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j]+(j?blk2(i):blk0(i));\
wolfSSL 0:1239e9b70ca2 138 d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i))
wolfSSL 0:1239e9b70ca2 139
wolfSSL 0:1239e9b70ca2 140 #define blk384(i) (W[i] = sha384->buffer[i])
wolfSSL 0:1239e9b70ca2 141
wolfSSL 0:1239e9b70ca2 142 #define R2(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j]+(j?blk2(i):blk384(i));\
wolfSSL 0:1239e9b70ca2 143 d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i))
wolfSSL 0:1239e9b70ca2 144
wolfSSL 0:1239e9b70ca2 145
wolfSSL 0:1239e9b70ca2 146 static int Transform(Sha512* sha512)
wolfSSL 0:1239e9b70ca2 147 {
wolfSSL 0:1239e9b70ca2 148 const word64* K = K512;
wolfSSL 0:1239e9b70ca2 149
wolfSSL 0:1239e9b70ca2 150 word32 j;
wolfSSL 0:1239e9b70ca2 151 word64 T[8];
wolfSSL 0:1239e9b70ca2 152
wolfSSL 0:1239e9b70ca2 153 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 154 word64* W;
wolfSSL 0:1239e9b70ca2 155
wolfSSL 0:1239e9b70ca2 156 W = (word64*) XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 157 if (W == NULL)
wolfSSL 0:1239e9b70ca2 158 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 159 #else
wolfSSL 0:1239e9b70ca2 160 word64 W[16];
wolfSSL 0:1239e9b70ca2 161 #endif
wolfSSL 0:1239e9b70ca2 162
wolfSSL 0:1239e9b70ca2 163 /* Copy digest to working vars */
wolfSSL 0:1239e9b70ca2 164 XMEMCPY(T, sha512->digest, sizeof(T));
wolfSSL 0:1239e9b70ca2 165
wolfSSL 0:1239e9b70ca2 166 #ifdef USE_SLOW_SHA2
wolfSSL 0:1239e9b70ca2 167 /* over twice as small, but 50% slower */
wolfSSL 0:1239e9b70ca2 168 /* 80 operations, not unrolled */
wolfSSL 0:1239e9b70ca2 169 for (j = 0; j < 80; j += 16) {
wolfSSL 0:1239e9b70ca2 170 int m;
wolfSSL 0:1239e9b70ca2 171 for (m = 0; m < 16; m++) { /* braces needed here for macros {} */
wolfSSL 0:1239e9b70ca2 172 R(m);
wolfSSL 0:1239e9b70ca2 173 }
wolfSSL 0:1239e9b70ca2 174 }
wolfSSL 0:1239e9b70ca2 175 #else
wolfSSL 0:1239e9b70ca2 176 /* 80 operations, partially loop unrolled */
wolfSSL 0:1239e9b70ca2 177 for (j = 0; j < 80; j += 16) {
wolfSSL 0:1239e9b70ca2 178 R( 0); R( 1); R( 2); R( 3);
wolfSSL 0:1239e9b70ca2 179 R( 4); R( 5); R( 6); R( 7);
wolfSSL 0:1239e9b70ca2 180 R( 8); R( 9); R(10); R(11);
wolfSSL 0:1239e9b70ca2 181 R(12); R(13); R(14); R(15);
wolfSSL 0:1239e9b70ca2 182 }
wolfSSL 0:1239e9b70ca2 183 #endif /* USE_SLOW_SHA2 */
wolfSSL 0:1239e9b70ca2 184
wolfSSL 0:1239e9b70ca2 185 /* Add the working vars back into digest */
wolfSSL 0:1239e9b70ca2 186
wolfSSL 0:1239e9b70ca2 187 sha512->digest[0] += a(0);
wolfSSL 0:1239e9b70ca2 188 sha512->digest[1] += b(0);
wolfSSL 0:1239e9b70ca2 189 sha512->digest[2] += c(0);
wolfSSL 0:1239e9b70ca2 190 sha512->digest[3] += d(0);
wolfSSL 0:1239e9b70ca2 191 sha512->digest[4] += e(0);
wolfSSL 0:1239e9b70ca2 192 sha512->digest[5] += f(0);
wolfSSL 0:1239e9b70ca2 193 sha512->digest[6] += g(0);
wolfSSL 0:1239e9b70ca2 194 sha512->digest[7] += h(0);
wolfSSL 0:1239e9b70ca2 195
wolfSSL 0:1239e9b70ca2 196 /* Wipe variables */
wolfSSL 0:1239e9b70ca2 197 XMEMSET(W, 0, sizeof(word64) * 16);
wolfSSL 0:1239e9b70ca2 198 XMEMSET(T, 0, sizeof(T));
wolfSSL 0:1239e9b70ca2 199
wolfSSL 0:1239e9b70ca2 200 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 201 XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 202 #endif
wolfSSL 0:1239e9b70ca2 203
wolfSSL 0:1239e9b70ca2 204 return 0;
wolfSSL 0:1239e9b70ca2 205 }
wolfSSL 0:1239e9b70ca2 206
wolfSSL 0:1239e9b70ca2 207
wolfSSL 0:1239e9b70ca2 208 static INLINE void AddLength(Sha512* sha512, word32 len)
wolfSSL 0:1239e9b70ca2 209 {
wolfSSL 0:1239e9b70ca2 210 word32 tmp = sha512->loLen;
wolfSSL 0:1239e9b70ca2 211 if ( (sha512->loLen += len) < tmp)
wolfSSL 0:1239e9b70ca2 212 sha512->hiLen++; /* carry low to high */
wolfSSL 0:1239e9b70ca2 213 }
wolfSSL 0:1239e9b70ca2 214
wolfSSL 0:1239e9b70ca2 215
wolfSSL 0:1239e9b70ca2 216 int Sha512Update(Sha512* sha512, const byte* data, word32 len)
wolfSSL 0:1239e9b70ca2 217 {
wolfSSL 0:1239e9b70ca2 218 /* do block size increments */
wolfSSL 0:1239e9b70ca2 219 byte* local = (byte*)sha512->buffer;
wolfSSL 0:1239e9b70ca2 220
wolfSSL 0:1239e9b70ca2 221 while (len) {
wolfSSL 0:1239e9b70ca2 222 word32 add = min(len, SHA512_BLOCK_SIZE - sha512->buffLen);
wolfSSL 0:1239e9b70ca2 223 XMEMCPY(&local[sha512->buffLen], data, add);
wolfSSL 0:1239e9b70ca2 224
wolfSSL 0:1239e9b70ca2 225 sha512->buffLen += add;
wolfSSL 0:1239e9b70ca2 226 data += add;
wolfSSL 0:1239e9b70ca2 227 len -= add;
wolfSSL 0:1239e9b70ca2 228
wolfSSL 0:1239e9b70ca2 229 if (sha512->buffLen == SHA512_BLOCK_SIZE) {
wolfSSL 0:1239e9b70ca2 230 int ret;
wolfSSL 0:1239e9b70ca2 231
wolfSSL 0:1239e9b70ca2 232 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 233 ByteReverseWords64(sha512->buffer, sha512->buffer,
wolfSSL 0:1239e9b70ca2 234 SHA512_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 235 #endif
wolfSSL 0:1239e9b70ca2 236 ret = Transform(sha512);
wolfSSL 0:1239e9b70ca2 237 if (ret != 0)
wolfSSL 0:1239e9b70ca2 238 return ret;
wolfSSL 0:1239e9b70ca2 239
wolfSSL 0:1239e9b70ca2 240 AddLength(sha512, SHA512_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 241 sha512->buffLen = 0;
wolfSSL 0:1239e9b70ca2 242 }
wolfSSL 0:1239e9b70ca2 243 }
wolfSSL 0:1239e9b70ca2 244 return 0;
wolfSSL 0:1239e9b70ca2 245 }
wolfSSL 0:1239e9b70ca2 246
wolfSSL 0:1239e9b70ca2 247
wolfSSL 0:1239e9b70ca2 248 int Sha512Final(Sha512* sha512, byte* hash)
wolfSSL 0:1239e9b70ca2 249 {
wolfSSL 0:1239e9b70ca2 250 byte* local = (byte*)sha512->buffer;
wolfSSL 0:1239e9b70ca2 251 int ret;
wolfSSL 0:1239e9b70ca2 252
wolfSSL 0:1239e9b70ca2 253 AddLength(sha512, sha512->buffLen); /* before adding pads */
wolfSSL 0:1239e9b70ca2 254
wolfSSL 0:1239e9b70ca2 255 local[sha512->buffLen++] = 0x80; /* add 1 */
wolfSSL 0:1239e9b70ca2 256
wolfSSL 0:1239e9b70ca2 257 /* pad with zeros */
wolfSSL 0:1239e9b70ca2 258 if (sha512->buffLen > SHA512_PAD_SIZE) {
wolfSSL 0:1239e9b70ca2 259 XMEMSET(&local[sha512->buffLen], 0, SHA512_BLOCK_SIZE -sha512->buffLen);
wolfSSL 0:1239e9b70ca2 260 sha512->buffLen += SHA512_BLOCK_SIZE - sha512->buffLen;
wolfSSL 0:1239e9b70ca2 261
wolfSSL 0:1239e9b70ca2 262 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 263 ByteReverseWords64(sha512->buffer,sha512->buffer,SHA512_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 264 #endif
wolfSSL 0:1239e9b70ca2 265 ret = Transform(sha512);
wolfSSL 0:1239e9b70ca2 266 if (ret != 0)
wolfSSL 0:1239e9b70ca2 267 return ret;
wolfSSL 0:1239e9b70ca2 268
wolfSSL 0:1239e9b70ca2 269 sha512->buffLen = 0;
wolfSSL 0:1239e9b70ca2 270 }
wolfSSL 0:1239e9b70ca2 271 XMEMSET(&local[sha512->buffLen], 0, SHA512_PAD_SIZE - sha512->buffLen);
wolfSSL 0:1239e9b70ca2 272
wolfSSL 0:1239e9b70ca2 273 /* put lengths in bits */
wolfSSL 0:1239e9b70ca2 274 sha512->hiLen = (sha512->loLen >> (8*sizeof(sha512->loLen) - 3)) +
wolfSSL 0:1239e9b70ca2 275 (sha512->hiLen << 3);
wolfSSL 0:1239e9b70ca2 276 sha512->loLen = sha512->loLen << 3;
wolfSSL 0:1239e9b70ca2 277
wolfSSL 0:1239e9b70ca2 278 /* store lengths */
wolfSSL 0:1239e9b70ca2 279 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 280 ByteReverseWords64(sha512->buffer, sha512->buffer, SHA512_PAD_SIZE);
wolfSSL 0:1239e9b70ca2 281 #endif
wolfSSL 0:1239e9b70ca2 282 /* ! length ordering dependent on digest endian type ! */
wolfSSL 0:1239e9b70ca2 283 sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 2] = sha512->hiLen;
wolfSSL 0:1239e9b70ca2 284 sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 1] = sha512->loLen;
wolfSSL 0:1239e9b70ca2 285
wolfSSL 0:1239e9b70ca2 286 ret = Transform(sha512);
wolfSSL 0:1239e9b70ca2 287 if (ret != 0)
wolfSSL 0:1239e9b70ca2 288 return ret;
wolfSSL 0:1239e9b70ca2 289
wolfSSL 0:1239e9b70ca2 290 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 291 ByteReverseWords64(sha512->digest, sha512->digest, SHA512_DIGEST_SIZE);
wolfSSL 0:1239e9b70ca2 292 #endif
wolfSSL 0:1239e9b70ca2 293 XMEMCPY(hash, sha512->digest, SHA512_DIGEST_SIZE);
wolfSSL 0:1239e9b70ca2 294
wolfSSL 0:1239e9b70ca2 295 return InitSha512(sha512); /* reset state */
wolfSSL 0:1239e9b70ca2 296 }
wolfSSL 0:1239e9b70ca2 297
wolfSSL 0:1239e9b70ca2 298
wolfSSL 0:1239e9b70ca2 299
wolfSSL 0:1239e9b70ca2 300 #ifdef CYASSL_SHA384
wolfSSL 0:1239e9b70ca2 301
wolfSSL 0:1239e9b70ca2 302 int InitSha384(Sha384* sha384)
wolfSSL 0:1239e9b70ca2 303 {
wolfSSL 0:1239e9b70ca2 304 sha384->digest[0] = W64LIT(0xcbbb9d5dc1059ed8);
wolfSSL 0:1239e9b70ca2 305 sha384->digest[1] = W64LIT(0x629a292a367cd507);
wolfSSL 0:1239e9b70ca2 306 sha384->digest[2] = W64LIT(0x9159015a3070dd17);
wolfSSL 0:1239e9b70ca2 307 sha384->digest[3] = W64LIT(0x152fecd8f70e5939);
wolfSSL 0:1239e9b70ca2 308 sha384->digest[4] = W64LIT(0x67332667ffc00b31);
wolfSSL 0:1239e9b70ca2 309 sha384->digest[5] = W64LIT(0x8eb44a8768581511);
wolfSSL 0:1239e9b70ca2 310 sha384->digest[6] = W64LIT(0xdb0c2e0d64f98fa7);
wolfSSL 0:1239e9b70ca2 311 sha384->digest[7] = W64LIT(0x47b5481dbefa4fa4);
wolfSSL 0:1239e9b70ca2 312
wolfSSL 0:1239e9b70ca2 313 sha384->buffLen = 0;
wolfSSL 0:1239e9b70ca2 314 sha384->loLen = 0;
wolfSSL 0:1239e9b70ca2 315 sha384->hiLen = 0;
wolfSSL 0:1239e9b70ca2 316
wolfSSL 0:1239e9b70ca2 317 return 0;
wolfSSL 0:1239e9b70ca2 318 }
wolfSSL 0:1239e9b70ca2 319
wolfSSL 0:1239e9b70ca2 320
wolfSSL 0:1239e9b70ca2 321 static int Transform384(Sha384* sha384)
wolfSSL 0:1239e9b70ca2 322 {
wolfSSL 0:1239e9b70ca2 323 const word64* K = K512;
wolfSSL 0:1239e9b70ca2 324
wolfSSL 0:1239e9b70ca2 325 word32 j;
wolfSSL 0:1239e9b70ca2 326 word64 T[8];
wolfSSL 0:1239e9b70ca2 327
wolfSSL 0:1239e9b70ca2 328 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 329 word64* W;
wolfSSL 0:1239e9b70ca2 330
wolfSSL 0:1239e9b70ca2 331 W = (word64*) XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 332 if (W == NULL)
wolfSSL 0:1239e9b70ca2 333 return MEMORY_E;
wolfSSL 0:1239e9b70ca2 334 #else
wolfSSL 0:1239e9b70ca2 335 word64 W[16];
wolfSSL 0:1239e9b70ca2 336 #endif
wolfSSL 0:1239e9b70ca2 337
wolfSSL 0:1239e9b70ca2 338 /* Copy digest to working vars */
wolfSSL 0:1239e9b70ca2 339 XMEMCPY(T, sha384->digest, sizeof(T));
wolfSSL 0:1239e9b70ca2 340
wolfSSL 0:1239e9b70ca2 341 #ifdef USE_SLOW_SHA2
wolfSSL 0:1239e9b70ca2 342 /* over twice as small, but 50% slower */
wolfSSL 0:1239e9b70ca2 343 /* 80 operations, not unrolled */
wolfSSL 0:1239e9b70ca2 344 for (j = 0; j < 80; j += 16) {
wolfSSL 0:1239e9b70ca2 345 int m;
wolfSSL 0:1239e9b70ca2 346 for (m = 0; m < 16; m++) { /* braces needed for macros {} */
wolfSSL 0:1239e9b70ca2 347 R2(m);
wolfSSL 0:1239e9b70ca2 348 }
wolfSSL 0:1239e9b70ca2 349 }
wolfSSL 0:1239e9b70ca2 350 #else
wolfSSL 0:1239e9b70ca2 351 /* 80 operations, partially loop unrolled */
wolfSSL 0:1239e9b70ca2 352 for (j = 0; j < 80; j += 16) {
wolfSSL 0:1239e9b70ca2 353 R2( 0); R2( 1); R2( 2); R2( 3);
wolfSSL 0:1239e9b70ca2 354 R2( 4); R2( 5); R2( 6); R2( 7);
wolfSSL 0:1239e9b70ca2 355 R2( 8); R2( 9); R2(10); R2(11);
wolfSSL 0:1239e9b70ca2 356 R2(12); R2(13); R2(14); R2(15);
wolfSSL 0:1239e9b70ca2 357 }
wolfSSL 0:1239e9b70ca2 358 #endif /* USE_SLOW_SHA2 */
wolfSSL 0:1239e9b70ca2 359
wolfSSL 0:1239e9b70ca2 360 /* Add the working vars back into digest */
wolfSSL 0:1239e9b70ca2 361
wolfSSL 0:1239e9b70ca2 362 sha384->digest[0] += a(0);
wolfSSL 0:1239e9b70ca2 363 sha384->digest[1] += b(0);
wolfSSL 0:1239e9b70ca2 364 sha384->digest[2] += c(0);
wolfSSL 0:1239e9b70ca2 365 sha384->digest[3] += d(0);
wolfSSL 0:1239e9b70ca2 366 sha384->digest[4] += e(0);
wolfSSL 0:1239e9b70ca2 367 sha384->digest[5] += f(0);
wolfSSL 0:1239e9b70ca2 368 sha384->digest[6] += g(0);
wolfSSL 0:1239e9b70ca2 369 sha384->digest[7] += h(0);
wolfSSL 0:1239e9b70ca2 370
wolfSSL 0:1239e9b70ca2 371 /* Wipe variables */
wolfSSL 0:1239e9b70ca2 372 XMEMSET(W, 0, sizeof(word64) * 16);
wolfSSL 0:1239e9b70ca2 373 XMEMSET(T, 0, sizeof(T));
wolfSSL 0:1239e9b70ca2 374
wolfSSL 0:1239e9b70ca2 375 #ifdef CYASSL_SMALL_STACK
wolfSSL 0:1239e9b70ca2 376 XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
wolfSSL 0:1239e9b70ca2 377 #endif
wolfSSL 0:1239e9b70ca2 378
wolfSSL 0:1239e9b70ca2 379 return 0;
wolfSSL 0:1239e9b70ca2 380 }
wolfSSL 0:1239e9b70ca2 381
wolfSSL 0:1239e9b70ca2 382
wolfSSL 0:1239e9b70ca2 383 static INLINE void AddLength384(Sha384* sha384, word32 len)
wolfSSL 0:1239e9b70ca2 384 {
wolfSSL 0:1239e9b70ca2 385 word32 tmp = sha384->loLen;
wolfSSL 0:1239e9b70ca2 386 if ( (sha384->loLen += len) < tmp)
wolfSSL 0:1239e9b70ca2 387 sha384->hiLen++; /* carry low to high */
wolfSSL 0:1239e9b70ca2 388 }
wolfSSL 0:1239e9b70ca2 389
wolfSSL 0:1239e9b70ca2 390
wolfSSL 0:1239e9b70ca2 391 int Sha384Update(Sha384* sha384, const byte* data, word32 len)
wolfSSL 0:1239e9b70ca2 392 {
wolfSSL 0:1239e9b70ca2 393 /* do block size increments */
wolfSSL 0:1239e9b70ca2 394 byte* local = (byte*)sha384->buffer;
wolfSSL 0:1239e9b70ca2 395
wolfSSL 0:1239e9b70ca2 396 while (len) {
wolfSSL 0:1239e9b70ca2 397 word32 add = min(len, SHA384_BLOCK_SIZE - sha384->buffLen);
wolfSSL 0:1239e9b70ca2 398 XMEMCPY(&local[sha384->buffLen], data, add);
wolfSSL 0:1239e9b70ca2 399
wolfSSL 0:1239e9b70ca2 400 sha384->buffLen += add;
wolfSSL 0:1239e9b70ca2 401 data += add;
wolfSSL 0:1239e9b70ca2 402 len -= add;
wolfSSL 0:1239e9b70ca2 403
wolfSSL 0:1239e9b70ca2 404 if (sha384->buffLen == SHA384_BLOCK_SIZE) {
wolfSSL 0:1239e9b70ca2 405 int ret;
wolfSSL 0:1239e9b70ca2 406
wolfSSL 0:1239e9b70ca2 407 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 408 ByteReverseWords64(sha384->buffer, sha384->buffer,
wolfSSL 0:1239e9b70ca2 409 SHA384_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 410 #endif
wolfSSL 0:1239e9b70ca2 411 ret = Transform384(sha384);
wolfSSL 0:1239e9b70ca2 412 if (ret != 0)
wolfSSL 0:1239e9b70ca2 413 return ret;
wolfSSL 0:1239e9b70ca2 414
wolfSSL 0:1239e9b70ca2 415 AddLength384(sha384, SHA384_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 416 sha384->buffLen = 0;
wolfSSL 0:1239e9b70ca2 417 }
wolfSSL 0:1239e9b70ca2 418 }
wolfSSL 0:1239e9b70ca2 419 return 0;
wolfSSL 0:1239e9b70ca2 420 }
wolfSSL 0:1239e9b70ca2 421
wolfSSL 0:1239e9b70ca2 422
wolfSSL 0:1239e9b70ca2 423 int Sha384Final(Sha384* sha384, byte* hash)
wolfSSL 0:1239e9b70ca2 424 {
wolfSSL 0:1239e9b70ca2 425 byte* local = (byte*)sha384->buffer;
wolfSSL 0:1239e9b70ca2 426 int ret;
wolfSSL 0:1239e9b70ca2 427
wolfSSL 0:1239e9b70ca2 428 AddLength384(sha384, sha384->buffLen); /* before adding pads */
wolfSSL 0:1239e9b70ca2 429
wolfSSL 0:1239e9b70ca2 430 local[sha384->buffLen++] = 0x80; /* add 1 */
wolfSSL 0:1239e9b70ca2 431
wolfSSL 0:1239e9b70ca2 432 /* pad with zeros */
wolfSSL 0:1239e9b70ca2 433 if (sha384->buffLen > SHA384_PAD_SIZE) {
wolfSSL 0:1239e9b70ca2 434 XMEMSET(&local[sha384->buffLen], 0, SHA384_BLOCK_SIZE -sha384->buffLen);
wolfSSL 0:1239e9b70ca2 435 sha384->buffLen += SHA384_BLOCK_SIZE - sha384->buffLen;
wolfSSL 0:1239e9b70ca2 436
wolfSSL 0:1239e9b70ca2 437 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 438 ByteReverseWords64(sha384->buffer,sha384->buffer,SHA384_BLOCK_SIZE);
wolfSSL 0:1239e9b70ca2 439 #endif
wolfSSL 0:1239e9b70ca2 440 ret = Transform384(sha384);
wolfSSL 0:1239e9b70ca2 441 if (ret != 0)
wolfSSL 0:1239e9b70ca2 442 return ret;
wolfSSL 0:1239e9b70ca2 443
wolfSSL 0:1239e9b70ca2 444 sha384->buffLen = 0;
wolfSSL 0:1239e9b70ca2 445 }
wolfSSL 0:1239e9b70ca2 446 XMEMSET(&local[sha384->buffLen], 0, SHA384_PAD_SIZE - sha384->buffLen);
wolfSSL 0:1239e9b70ca2 447
wolfSSL 0:1239e9b70ca2 448 /* put lengths in bits */
wolfSSL 0:1239e9b70ca2 449 sha384->hiLen = (sha384->loLen >> (8*sizeof(sha384->loLen) - 3)) +
wolfSSL 0:1239e9b70ca2 450 (sha384->hiLen << 3);
wolfSSL 0:1239e9b70ca2 451 sha384->loLen = sha384->loLen << 3;
wolfSSL 0:1239e9b70ca2 452
wolfSSL 0:1239e9b70ca2 453 /* store lengths */
wolfSSL 0:1239e9b70ca2 454 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 455 ByteReverseWords64(sha384->buffer, sha384->buffer, SHA384_PAD_SIZE);
wolfSSL 0:1239e9b70ca2 456 #endif
wolfSSL 0:1239e9b70ca2 457 /* ! length ordering dependent on digest endian type ! */
wolfSSL 0:1239e9b70ca2 458 sha384->buffer[SHA384_BLOCK_SIZE / sizeof(word64) - 2] = sha384->hiLen;
wolfSSL 0:1239e9b70ca2 459 sha384->buffer[SHA384_BLOCK_SIZE / sizeof(word64) - 1] = sha384->loLen;
wolfSSL 0:1239e9b70ca2 460
wolfSSL 0:1239e9b70ca2 461 ret = Transform384(sha384);
wolfSSL 0:1239e9b70ca2 462 if (ret != 0)
wolfSSL 0:1239e9b70ca2 463 return ret;
wolfSSL 0:1239e9b70ca2 464
wolfSSL 0:1239e9b70ca2 465 #ifdef LITTLE_ENDIAN_ORDER
wolfSSL 0:1239e9b70ca2 466 ByteReverseWords64(sha384->digest, sha384->digest, SHA384_DIGEST_SIZE);
wolfSSL 0:1239e9b70ca2 467 #endif
wolfSSL 0:1239e9b70ca2 468 XMEMCPY(hash, sha384->digest, SHA384_DIGEST_SIZE);
wolfSSL 0:1239e9b70ca2 469
wolfSSL 0:1239e9b70ca2 470 return InitSha384(sha384); /* reset state */
wolfSSL 0:1239e9b70ca2 471 }
wolfSSL 0:1239e9b70ca2 472
wolfSSL 0:1239e9b70ca2 473 #endif /* CYASSL_SHA384 */
wolfSSL 0:1239e9b70ca2 474
wolfSSL 0:1239e9b70ca2 475 #endif /* CYASSL_SHA512 */
wolfSSL 0:1239e9b70ca2 476