cyassl re-port with cellular comms, PSK test

Dependencies:   VodafoneUSBModem_bleedingedge2 mbed-rtos mbed-src

Committer:
ashleymills
Date:
Fri Apr 26 16:59:36 2013 +0000
Revision:
1:b211d97b0068
Parent:
0:e979170e02e7
nothing

Who changed what in which revision?

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