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