This is a port of cyaSSL 2.7.0.
Dependents: CyaSSL_DTLS_Cellular CyaSSL_DTLS_Ethernet
ctaocrypt/src/sha256.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 | /* sha256.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 | |
ashleymills | 0:714293de3836 | 23 | /* code submitted by raphael.huck@efixo.com */ |
ashleymills | 0:714293de3836 | 24 | |
ashleymills | 0:714293de3836 | 25 | #ifdef HAVE_CONFIG_H |
ashleymills | 0:714293de3836 | 26 | #include <config.h> |
ashleymills | 0:714293de3836 | 27 | #endif |
ashleymills | 0:714293de3836 | 28 | |
ashleymills | 0:714293de3836 | 29 | #include <cyassl/ctaocrypt/settings.h> |
ashleymills | 0:714293de3836 | 30 | |
ashleymills | 0:714293de3836 | 31 | #ifndef NO_SHA256 |
ashleymills | 0:714293de3836 | 32 | |
ashleymills | 0:714293de3836 | 33 | #include <cyassl/ctaocrypt/sha256.h> |
ashleymills | 0:714293de3836 | 34 | #ifdef NO_INLINE |
ashleymills | 0:714293de3836 | 35 | #include <cyassl/ctaocrypt/misc.h> |
ashleymills | 0:714293de3836 | 36 | #else |
ashleymills | 0:714293de3836 | 37 | #include <ctaocrypt/src/misc.c> |
ashleymills | 0:714293de3836 | 38 | #endif |
ashleymills | 0:714293de3836 | 39 | |
ashleymills | 0:714293de3836 | 40 | |
ashleymills | 0:714293de3836 | 41 | #ifndef min |
ashleymills | 0:714293de3836 | 42 | |
ashleymills | 0:714293de3836 | 43 | static INLINE word32 min(word32 a, word32 b) |
ashleymills | 0:714293de3836 | 44 | { |
ashleymills | 0:714293de3836 | 45 | return a > b ? b : a; |
ashleymills | 0:714293de3836 | 46 | } |
ashleymills | 0:714293de3836 | 47 | |
ashleymills | 0:714293de3836 | 48 | #endif /* min */ |
ashleymills | 0:714293de3836 | 49 | |
ashleymills | 0:714293de3836 | 50 | |
ashleymills | 0:714293de3836 | 51 | void InitSha256(Sha256* sha256) |
ashleymills | 0:714293de3836 | 52 | { |
ashleymills | 0:714293de3836 | 53 | sha256->digest[0] = 0x6A09E667L; |
ashleymills | 0:714293de3836 | 54 | sha256->digest[1] = 0xBB67AE85L; |
ashleymills | 0:714293de3836 | 55 | sha256->digest[2] = 0x3C6EF372L; |
ashleymills | 0:714293de3836 | 56 | sha256->digest[3] = 0xA54FF53AL; |
ashleymills | 0:714293de3836 | 57 | sha256->digest[4] = 0x510E527FL; |
ashleymills | 0:714293de3836 | 58 | sha256->digest[5] = 0x9B05688CL; |
ashleymills | 0:714293de3836 | 59 | sha256->digest[6] = 0x1F83D9ABL; |
ashleymills | 0:714293de3836 | 60 | sha256->digest[7] = 0x5BE0CD19L; |
ashleymills | 0:714293de3836 | 61 | |
ashleymills | 0:714293de3836 | 62 | sha256->buffLen = 0; |
ashleymills | 0:714293de3836 | 63 | sha256->loLen = 0; |
ashleymills | 0:714293de3836 | 64 | sha256->hiLen = 0; |
ashleymills | 0:714293de3836 | 65 | } |
ashleymills | 0:714293de3836 | 66 | |
ashleymills | 0:714293de3836 | 67 | static const word32 K[64] = { |
ashleymills | 0:714293de3836 | 68 | 0x428A2F98L, 0x71374491L, 0xB5C0FBCFL, 0xE9B5DBA5L, 0x3956C25BL, |
ashleymills | 0:714293de3836 | 69 | 0x59F111F1L, 0x923F82A4L, 0xAB1C5ED5L, 0xD807AA98L, 0x12835B01L, |
ashleymills | 0:714293de3836 | 70 | 0x243185BEL, 0x550C7DC3L, 0x72BE5D74L, 0x80DEB1FEL, 0x9BDC06A7L, |
ashleymills | 0:714293de3836 | 71 | 0xC19BF174L, 0xE49B69C1L, 0xEFBE4786L, 0x0FC19DC6L, 0x240CA1CCL, |
ashleymills | 0:714293de3836 | 72 | 0x2DE92C6FL, 0x4A7484AAL, 0x5CB0A9DCL, 0x76F988DAL, 0x983E5152L, |
ashleymills | 0:714293de3836 | 73 | 0xA831C66DL, 0xB00327C8L, 0xBF597FC7L, 0xC6E00BF3L, 0xD5A79147L, |
ashleymills | 0:714293de3836 | 74 | 0x06CA6351L, 0x14292967L, 0x27B70A85L, 0x2E1B2138L, 0x4D2C6DFCL, |
ashleymills | 0:714293de3836 | 75 | 0x53380D13L, 0x650A7354L, 0x766A0ABBL, 0x81C2C92EL, 0x92722C85L, |
ashleymills | 0:714293de3836 | 76 | 0xA2BFE8A1L, 0xA81A664BL, 0xC24B8B70L, 0xC76C51A3L, 0xD192E819L, |
ashleymills | 0:714293de3836 | 77 | 0xD6990624L, 0xF40E3585L, 0x106AA070L, 0x19A4C116L, 0x1E376C08L, |
ashleymills | 0:714293de3836 | 78 | 0x2748774CL, 0x34B0BCB5L, 0x391C0CB3L, 0x4ED8AA4AL, 0x5B9CCA4FL, |
ashleymills | 0:714293de3836 | 79 | 0x682E6FF3L, 0x748F82EEL, 0x78A5636FL, 0x84C87814L, 0x8CC70208L, |
ashleymills | 0:714293de3836 | 80 | 0x90BEFFFAL, 0xA4506CEBL, 0xBEF9A3F7L, 0xC67178F2L |
ashleymills | 0:714293de3836 | 81 | }; |
ashleymills | 0:714293de3836 | 82 | |
ashleymills | 0:714293de3836 | 83 | #define Ch(x,y,z) (z ^ (x & (y ^ z))) |
ashleymills | 0:714293de3836 | 84 | #define Maj(x,y,z) (((x | y) & z) | (x & y)) |
ashleymills | 0:714293de3836 | 85 | #define S(x, n) rotrFixed(x, n) |
ashleymills | 0:714293de3836 | 86 | #define R(x, n) (((x)&0xFFFFFFFFU)>>(n)) |
ashleymills | 0:714293de3836 | 87 | #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22)) |
ashleymills | 0:714293de3836 | 88 | #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25)) |
ashleymills | 0:714293de3836 | 89 | #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3)) |
ashleymills | 0:714293de3836 | 90 | #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10)) |
ashleymills | 0:714293de3836 | 91 | |
ashleymills | 0:714293de3836 | 92 | #define RND(a,b,c,d,e,f,g,h,i) \ |
ashleymills | 0:714293de3836 | 93 | t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \ |
ashleymills | 0:714293de3836 | 94 | t1 = Sigma0(a) + Maj(a, b, c); \ |
ashleymills | 0:714293de3836 | 95 | d += t0; \ |
ashleymills | 0:714293de3836 | 96 | h = t0 + t1; |
ashleymills | 0:714293de3836 | 97 | |
ashleymills | 0:714293de3836 | 98 | |
ashleymills | 0:714293de3836 | 99 | static void Transform(Sha256* sha256) |
ashleymills | 0:714293de3836 | 100 | { |
ashleymills | 0:714293de3836 | 101 | word32 S[8], W[64], t0, t1; |
ashleymills | 0:714293de3836 | 102 | int i; |
ashleymills | 0:714293de3836 | 103 | |
ashleymills | 0:714293de3836 | 104 | /* Copy context->state[] to working vars */ |
ashleymills | 0:714293de3836 | 105 | for (i = 0; i < 8; i++) |
ashleymills | 0:714293de3836 | 106 | S[i] = sha256->digest[i]; |
ashleymills | 0:714293de3836 | 107 | |
ashleymills | 0:714293de3836 | 108 | for (i = 0; i < 16; i++) |
ashleymills | 0:714293de3836 | 109 | W[i] = sha256->buffer[i]; |
ashleymills | 0:714293de3836 | 110 | |
ashleymills | 0:714293de3836 | 111 | for (i = 16; i < 64; i++) |
ashleymills | 0:714293de3836 | 112 | W[i] = Gamma1(W[i-2]) + W[i-7] + Gamma0(W[i-15]) + W[i-16]; |
ashleymills | 0:714293de3836 | 113 | |
ashleymills | 0:714293de3836 | 114 | for (i = 0; i < 64; i += 8) { |
ashleymills | 0:714293de3836 | 115 | RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0); |
ashleymills | 0:714293de3836 | 116 | RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1); |
ashleymills | 0:714293de3836 | 117 | RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2); |
ashleymills | 0:714293de3836 | 118 | RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3); |
ashleymills | 0:714293de3836 | 119 | RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4); |
ashleymills | 0:714293de3836 | 120 | RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5); |
ashleymills | 0:714293de3836 | 121 | RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6); |
ashleymills | 0:714293de3836 | 122 | RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7); |
ashleymills | 0:714293de3836 | 123 | } |
ashleymills | 0:714293de3836 | 124 | |
ashleymills | 0:714293de3836 | 125 | /* Add the working vars back into digest state[] */ |
ashleymills | 0:714293de3836 | 126 | for (i = 0; i < 8; i++) { |
ashleymills | 0:714293de3836 | 127 | sha256->digest[i] += S[i]; |
ashleymills | 0:714293de3836 | 128 | } |
ashleymills | 0:714293de3836 | 129 | } |
ashleymills | 0:714293de3836 | 130 | |
ashleymills | 0:714293de3836 | 131 | |
ashleymills | 0:714293de3836 | 132 | static INLINE void AddLength(Sha256* sha256, word32 len) |
ashleymills | 0:714293de3836 | 133 | { |
ashleymills | 0:714293de3836 | 134 | word32 tmp = sha256->loLen; |
ashleymills | 0:714293de3836 | 135 | if ( (sha256->loLen += len) < tmp) |
ashleymills | 0:714293de3836 | 136 | sha256->hiLen++; /* carry low to high */ |
ashleymills | 0:714293de3836 | 137 | } |
ashleymills | 0:714293de3836 | 138 | |
ashleymills | 0:714293de3836 | 139 | |
ashleymills | 0:714293de3836 | 140 | void Sha256Update(Sha256* sha256, const byte* data, word32 len) |
ashleymills | 0:714293de3836 | 141 | { |
ashleymills | 0:714293de3836 | 142 | /* do block size increments */ |
ashleymills | 0:714293de3836 | 143 | byte* local = (byte*)sha256->buffer; |
ashleymills | 0:714293de3836 | 144 | |
ashleymills | 0:714293de3836 | 145 | while (len) { |
ashleymills | 0:714293de3836 | 146 | word32 add = min(len, SHA256_BLOCK_SIZE - sha256->buffLen); |
ashleymills | 0:714293de3836 | 147 | XMEMCPY(&local[sha256->buffLen], data, add); |
ashleymills | 0:714293de3836 | 148 | |
ashleymills | 0:714293de3836 | 149 | sha256->buffLen += add; |
ashleymills | 0:714293de3836 | 150 | data += add; |
ashleymills | 0:714293de3836 | 151 | len -= add; |
ashleymills | 0:714293de3836 | 152 | |
ashleymills | 0:714293de3836 | 153 | if (sha256->buffLen == SHA256_BLOCK_SIZE) { |
ashleymills | 0:714293de3836 | 154 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:714293de3836 | 155 | ByteReverseBytes(local, local, SHA256_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 156 | #endif |
ashleymills | 0:714293de3836 | 157 | Transform(sha256); |
ashleymills | 0:714293de3836 | 158 | AddLength(sha256, SHA256_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 159 | sha256->buffLen = 0; |
ashleymills | 0:714293de3836 | 160 | } |
ashleymills | 0:714293de3836 | 161 | } |
ashleymills | 0:714293de3836 | 162 | } |
ashleymills | 0:714293de3836 | 163 | |
ashleymills | 0:714293de3836 | 164 | |
ashleymills | 0:714293de3836 | 165 | void Sha256Final(Sha256* sha256, byte* hash) |
ashleymills | 0:714293de3836 | 166 | { |
ashleymills | 0:714293de3836 | 167 | byte* local = (byte*)sha256->buffer; |
ashleymills | 0:714293de3836 | 168 | |
ashleymills | 0:714293de3836 | 169 | AddLength(sha256, sha256->buffLen); /* before adding pads */ |
ashleymills | 0:714293de3836 | 170 | |
ashleymills | 0:714293de3836 | 171 | local[sha256->buffLen++] = 0x80; /* add 1 */ |
ashleymills | 0:714293de3836 | 172 | |
ashleymills | 0:714293de3836 | 173 | /* pad with zeros */ |
ashleymills | 0:714293de3836 | 174 | if (sha256->buffLen > SHA256_PAD_SIZE) { |
ashleymills | 0:714293de3836 | 175 | XMEMSET(&local[sha256->buffLen], 0, SHA256_BLOCK_SIZE - sha256->buffLen); |
ashleymills | 0:714293de3836 | 176 | sha256->buffLen += SHA256_BLOCK_SIZE - sha256->buffLen; |
ashleymills | 0:714293de3836 | 177 | |
ashleymills | 0:714293de3836 | 178 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:714293de3836 | 179 | ByteReverseBytes(local, local, SHA256_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 180 | #endif |
ashleymills | 0:714293de3836 | 181 | Transform(sha256); |
ashleymills | 0:714293de3836 | 182 | sha256->buffLen = 0; |
ashleymills | 0:714293de3836 | 183 | } |
ashleymills | 0:714293de3836 | 184 | XMEMSET(&local[sha256->buffLen], 0, SHA256_PAD_SIZE - sha256->buffLen); |
ashleymills | 0:714293de3836 | 185 | |
ashleymills | 0:714293de3836 | 186 | /* put lengths in bits */ |
ashleymills | 0:714293de3836 | 187 | sha256->hiLen = (sha256->loLen >> (8*sizeof(sha256->loLen) - 3)) + |
ashleymills | 0:714293de3836 | 188 | (sha256->hiLen << 3); |
ashleymills | 0:714293de3836 | 189 | sha256->loLen = sha256->loLen << 3; |
ashleymills | 0:714293de3836 | 190 | |
ashleymills | 0:714293de3836 | 191 | /* store lengths */ |
ashleymills | 0:714293de3836 | 192 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:714293de3836 | 193 | ByteReverseBytes(local, local, SHA256_BLOCK_SIZE); |
ashleymills | 0:714293de3836 | 194 | #endif |
ashleymills | 0:714293de3836 | 195 | /* ! length ordering dependent on digest endian type ! */ |
ashleymills | 0:714293de3836 | 196 | XMEMCPY(&local[SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32)); |
ashleymills | 0:714293de3836 | 197 | XMEMCPY(&local[SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen, |
ashleymills | 0:714293de3836 | 198 | sizeof(word32)); |
ashleymills | 0:714293de3836 | 199 | |
ashleymills | 0:714293de3836 | 200 | Transform(sha256); |
ashleymills | 0:714293de3836 | 201 | #ifdef LITTLE_ENDIAN_ORDER |
ashleymills | 0:714293de3836 | 202 | ByteReverseWords(sha256->digest, sha256->digest, SHA256_DIGEST_SIZE); |
ashleymills | 0:714293de3836 | 203 | #endif |
ashleymills | 0:714293de3836 | 204 | XMEMCPY(hash, sha256->digest, SHA256_DIGEST_SIZE); |
ashleymills | 0:714293de3836 | 205 | |
ashleymills | 0:714293de3836 | 206 | InitSha256(sha256); /* reset state */ |
ashleymills | 0:714293de3836 | 207 | } |
ashleymills | 0:714293de3836 | 208 | |
ashleymills | 0:714293de3836 | 209 | |
ashleymills | 0:714293de3836 | 210 | #endif /* NO_SHA256 */ |
ashleymills | 0:714293de3836 | 211 |