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