cya_u

Fork of CyaSSL-forEncrypt by Mobius IoT

Committer:
vbahl2
Date:
Wed May 10 18:20:47 2017 +0000
Revision:
2:d0516dc143b1
Parent:
0:5045d2638c29
updated

Who changed what in which revision?

UserRevisionLine numberNew contents of line
toddouska 0:5045d2638c29 1 /* sha256.c
toddouska 0:5045d2638c29 2 *
toddouska 0:5045d2638c29 3 * Copyright (C) 2006-2009 Sawtooth Consulting Ltd.
toddouska 0:5045d2638c29 4 *
toddouska 0:5045d2638c29 5 * This file is part of CyaSSL.
toddouska 0:5045d2638c29 6 *
toddouska 0:5045d2638c29 7 * CyaSSL is free software; you can redistribute it and/or modify
toddouska 0:5045d2638c29 8 * it under the terms of the GNU General Public License as published by
toddouska 0:5045d2638c29 9 * the Free Software Foundation; either version 2 of the License, or
toddouska 0:5045d2638c29 10 * (at your option) any later version.
toddouska 0:5045d2638c29 11 *
toddouska 0:5045d2638c29 12 * CyaSSL is distributed in the hope that it will be useful,
toddouska 0:5045d2638c29 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
toddouska 0:5045d2638c29 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
toddouska 0:5045d2638c29 15 * GNU General Public License for more details.
toddouska 0:5045d2638c29 16 *
toddouska 0:5045d2638c29 17 * You should have received a copy of the GNU General Public License
toddouska 0:5045d2638c29 18 * along with this program; if not, write to the Free Software
toddouska 0:5045d2638c29 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
toddouska 0:5045d2638c29 20 */
toddouska 0:5045d2638c29 21
toddouska 0:5045d2638c29 22 /* code submitted by raphael.huck@efixo.com */
toddouska 0:5045d2638c29 23
toddouska 0:5045d2638c29 24
toddouska 0:5045d2638c29 25
toddouska 0:5045d2638c29 26 #ifndef NO_SHA256
toddouska 0:5045d2638c29 27
toddouska 0:5045d2638c29 28 #include "sha256.h"
toddouska 0:5045d2638c29 29 #ifdef NO_INLINE
toddouska 0:5045d2638c29 30 #include "misc.h"
toddouska 0:5045d2638c29 31 #else
toddouska 0:5045d2638c29 32 #include "misc.c"
toddouska 0:5045d2638c29 33 #endif
toddouska 0:5045d2638c29 34
toddouska 0:5045d2638c29 35
toddouska 0:5045d2638c29 36 #ifndef min
toddouska 0:5045d2638c29 37
toddouska 0:5045d2638c29 38 static INLINE word32 min(word32 a, word32 b)
toddouska 0:5045d2638c29 39 {
toddouska 0:5045d2638c29 40 return a > b ? b : a;
toddouska 0:5045d2638c29 41 }
toddouska 0:5045d2638c29 42
toddouska 0:5045d2638c29 43 #endif /* min */
toddouska 0:5045d2638c29 44
toddouska 0:5045d2638c29 45
toddouska 0:5045d2638c29 46 void InitSha256(Sha256* sha256)
toddouska 0:5045d2638c29 47 {
toddouska 0:5045d2638c29 48 sha256->digest[0] = 0x6A09E667L;
toddouska 0:5045d2638c29 49 sha256->digest[1] = 0xBB67AE85L;
toddouska 0:5045d2638c29 50 sha256->digest[2] = 0x3C6EF372L;
toddouska 0:5045d2638c29 51 sha256->digest[3] = 0xA54FF53AL;
toddouska 0:5045d2638c29 52 sha256->digest[4] = 0x510E527FL;
toddouska 0:5045d2638c29 53 sha256->digest[5] = 0x9B05688CL;
toddouska 0:5045d2638c29 54 sha256->digest[6] = 0x1F83D9ABL;
toddouska 0:5045d2638c29 55 sha256->digest[7] = 0x5BE0CD19L;
toddouska 0:5045d2638c29 56
toddouska 0:5045d2638c29 57 sha256->buffLen = 0;
toddouska 0:5045d2638c29 58 sha256->loLen = 0;
toddouska 0:5045d2638c29 59 sha256->hiLen = 0;
toddouska 0:5045d2638c29 60 }
toddouska 0:5045d2638c29 61
toddouska 0:5045d2638c29 62 static const word32 K[64] = {
toddouska 0:5045d2638c29 63 0x428A2F98L, 0x71374491L, 0xB5C0FBCFL, 0xE9B5DBA5L, 0x3956C25BL,
toddouska 0:5045d2638c29 64 0x59F111F1L, 0x923F82A4L, 0xAB1C5ED5L, 0xD807AA98L, 0x12835B01L,
toddouska 0:5045d2638c29 65 0x243185BEL, 0x550C7DC3L, 0x72BE5D74L, 0x80DEB1FEL, 0x9BDC06A7L,
toddouska 0:5045d2638c29 66 0xC19BF174L, 0xE49B69C1L, 0xEFBE4786L, 0x0FC19DC6L, 0x240CA1CCL,
toddouska 0:5045d2638c29 67 0x2DE92C6FL, 0x4A7484AAL, 0x5CB0A9DCL, 0x76F988DAL, 0x983E5152L,
toddouska 0:5045d2638c29 68 0xA831C66DL, 0xB00327C8L, 0xBF597FC7L, 0xC6E00BF3L, 0xD5A79147L,
toddouska 0:5045d2638c29 69 0x06CA6351L, 0x14292967L, 0x27B70A85L, 0x2E1B2138L, 0x4D2C6DFCL,
toddouska 0:5045d2638c29 70 0x53380D13L, 0x650A7354L, 0x766A0ABBL, 0x81C2C92EL, 0x92722C85L,
toddouska 0:5045d2638c29 71 0xA2BFE8A1L, 0xA81A664BL, 0xC24B8B70L, 0xC76C51A3L, 0xD192E819L,
toddouska 0:5045d2638c29 72 0xD6990624L, 0xF40E3585L, 0x106AA070L, 0x19A4C116L, 0x1E376C08L,
toddouska 0:5045d2638c29 73 0x2748774CL, 0x34B0BCB5L, 0x391C0CB3L, 0x4ED8AA4AL, 0x5B9CCA4FL,
toddouska 0:5045d2638c29 74 0x682E6FF3L, 0x748F82EEL, 0x78A5636FL, 0x84C87814L, 0x8CC70208L,
toddouska 0:5045d2638c29 75 0x90BEFFFAL, 0xA4506CEBL, 0xBEF9A3F7L, 0xC67178F2L
toddouska 0:5045d2638c29 76 };
toddouska 0:5045d2638c29 77
toddouska 0:5045d2638c29 78 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
toddouska 0:5045d2638c29 79 #define Maj(x,y,z) (((x | y) & z) | (x & y))
toddouska 0:5045d2638c29 80 #define S(x, n) rotrFixed(x, n)
toddouska 0:5045d2638c29 81 #define R(x, n) (((x)&0xFFFFFFFFL)>>(n))
toddouska 0:5045d2638c29 82 #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
toddouska 0:5045d2638c29 83 #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
toddouska 0:5045d2638c29 84 #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
toddouska 0:5045d2638c29 85 #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
toddouska 0:5045d2638c29 86
toddouska 0:5045d2638c29 87 #define RND(a,b,c,d,e,f,g,h,i) \
toddouska 0:5045d2638c29 88 t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
toddouska 0:5045d2638c29 89 t1 = Sigma0(a) + Maj(a, b, c); \
toddouska 0:5045d2638c29 90 d += t0; \
toddouska 0:5045d2638c29 91 h = t0 + t1;
toddouska 0:5045d2638c29 92
toddouska 0:5045d2638c29 93
toddouska 0:5045d2638c29 94 static void Transform(Sha256* sha256)
toddouska 0:5045d2638c29 95 {
toddouska 0:5045d2638c29 96 word32 S[8], W[64], t0, t1;
toddouska 0:5045d2638c29 97 int i;
toddouska 0:5045d2638c29 98
toddouska 0:5045d2638c29 99 /* Copy context->state[] to working vars */
toddouska 0:5045d2638c29 100 for (i = 0; i < 8; i++)
toddouska 0:5045d2638c29 101 S[i] = sha256->digest[i];
toddouska 0:5045d2638c29 102
toddouska 0:5045d2638c29 103 for (i = 0; i < 16; i++)
toddouska 0:5045d2638c29 104 W[i] = sha256->buffer[i];
toddouska 0:5045d2638c29 105
toddouska 0:5045d2638c29 106 for (i = 16; i < 64; i++)
toddouska 0:5045d2638c29 107 W[i] = Gamma1(W[i-2]) + W[i-7] + Gamma0(W[i-15]) + W[i-16];
toddouska 0:5045d2638c29 108
toddouska 0:5045d2638c29 109 for (i = 0; i < 64; i += 8) {
toddouska 0:5045d2638c29 110 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0);
toddouska 0:5045d2638c29 111 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1);
toddouska 0:5045d2638c29 112 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2);
toddouska 0:5045d2638c29 113 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3);
toddouska 0:5045d2638c29 114 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4);
toddouska 0:5045d2638c29 115 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5);
toddouska 0:5045d2638c29 116 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
toddouska 0:5045d2638c29 117 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
toddouska 0:5045d2638c29 118 }
toddouska 0:5045d2638c29 119
toddouska 0:5045d2638c29 120 /* Add the working vars back into digest state[] */
toddouska 0:5045d2638c29 121 for (i = 0; i < 8; i++) {
toddouska 0:5045d2638c29 122 sha256->digest[i] += S[i];
toddouska 0:5045d2638c29 123 }
toddouska 0:5045d2638c29 124 }
toddouska 0:5045d2638c29 125
toddouska 0:5045d2638c29 126
toddouska 0:5045d2638c29 127 static INLINE void AddLength(Sha256* sha256, word32 len)
toddouska 0:5045d2638c29 128 {
toddouska 0:5045d2638c29 129 word32 tmp = sha256->loLen;
toddouska 0:5045d2638c29 130 if ( (sha256->loLen += len) < tmp)
toddouska 0:5045d2638c29 131 sha256->hiLen++; /* carry low to high */
toddouska 0:5045d2638c29 132 }
toddouska 0:5045d2638c29 133
toddouska 0:5045d2638c29 134
toddouska 0:5045d2638c29 135 void Sha256Update(Sha256* sha256, const byte* data, word32 len)
toddouska 0:5045d2638c29 136 {
toddouska 0:5045d2638c29 137 /* do block size increments */
toddouska 0:5045d2638c29 138 byte* local = (byte*)sha256->buffer;
toddouska 0:5045d2638c29 139
toddouska 0:5045d2638c29 140 while (len) {
toddouska 0:5045d2638c29 141 word32 add = min(len, SHA256_BLOCK_SIZE - sha256->buffLen);
toddouska 0:5045d2638c29 142 XMEMCPY(&local[sha256->buffLen], data, add);
toddouska 0:5045d2638c29 143
toddouska 0:5045d2638c29 144 sha256->buffLen += add;
toddouska 0:5045d2638c29 145 data += add;
toddouska 0:5045d2638c29 146 len -= add;
toddouska 0:5045d2638c29 147
toddouska 0:5045d2638c29 148 if (sha256->buffLen == SHA256_BLOCK_SIZE) {
toddouska 0:5045d2638c29 149 #ifdef LITTLE_ENDIAN_ORDER
toddouska 0:5045d2638c29 150 ByteReverseBytes(local, local, SHA256_BLOCK_SIZE);
toddouska 0:5045d2638c29 151 #endif
toddouska 0:5045d2638c29 152 Transform(sha256);
toddouska 0:5045d2638c29 153 AddLength(sha256, SHA256_BLOCK_SIZE);
toddouska 0:5045d2638c29 154 sha256->buffLen = 0;
toddouska 0:5045d2638c29 155 }
toddouska 0:5045d2638c29 156 }
toddouska 0:5045d2638c29 157 }
toddouska 0:5045d2638c29 158
toddouska 0:5045d2638c29 159
toddouska 0:5045d2638c29 160 void Sha256Final(Sha256* sha256, byte* hash)
toddouska 0:5045d2638c29 161 {
toddouska 0:5045d2638c29 162 byte* local = (byte*)sha256->buffer;
toddouska 0:5045d2638c29 163
toddouska 0:5045d2638c29 164 AddLength(sha256, sha256->buffLen); /* before adding pads */
toddouska 0:5045d2638c29 165
toddouska 0:5045d2638c29 166 local[sha256->buffLen++] = 0x80; /* add 1 */
toddouska 0:5045d2638c29 167
toddouska 0:5045d2638c29 168 /* pad with zeros */
toddouska 0:5045d2638c29 169 if (sha256->buffLen > SHA256_PAD_SIZE) {
toddouska 0:5045d2638c29 170 XMEMSET(&local[sha256->buffLen], 0, SHA256_BLOCK_SIZE - sha256->buffLen);
toddouska 0:5045d2638c29 171 sha256->buffLen += SHA256_BLOCK_SIZE - sha256->buffLen;
toddouska 0:5045d2638c29 172
toddouska 0:5045d2638c29 173 #ifdef LITTLE_ENDIAN_ORDER
toddouska 0:5045d2638c29 174 ByteReverseBytes(local, local, SHA256_BLOCK_SIZE);
toddouska 0:5045d2638c29 175 #endif
toddouska 0:5045d2638c29 176 Transform(sha256);
toddouska 0:5045d2638c29 177 sha256->buffLen = 0;
toddouska 0:5045d2638c29 178 }
toddouska 0:5045d2638c29 179 XMEMSET(&local[sha256->buffLen], 0, SHA256_PAD_SIZE - sha256->buffLen);
toddouska 0:5045d2638c29 180
toddouska 0:5045d2638c29 181 /* put lengths in bits */
toddouska 0:5045d2638c29 182 sha256->loLen = sha256->loLen << 3;
toddouska 0:5045d2638c29 183 sha256->hiLen = (sha256->loLen >> (8*sizeof(sha256->loLen) - 3)) +
toddouska 0:5045d2638c29 184 (sha256->hiLen << 3);
toddouska 0:5045d2638c29 185
toddouska 0:5045d2638c29 186 /* store lengths */
toddouska 0:5045d2638c29 187 #ifdef LITTLE_ENDIAN_ORDER
toddouska 0:5045d2638c29 188 ByteReverseBytes(local, local, SHA256_BLOCK_SIZE);
toddouska 0:5045d2638c29 189 #endif
toddouska 0:5045d2638c29 190 /* ! length ordering dependent on digest endian type ! */
toddouska 0:5045d2638c29 191 XMEMCPY(&local[SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32));
toddouska 0:5045d2638c29 192 XMEMCPY(&local[SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen,
toddouska 0:5045d2638c29 193 sizeof(word32));
toddouska 0:5045d2638c29 194
toddouska 0:5045d2638c29 195 Transform(sha256);
toddouska 0:5045d2638c29 196 #ifdef LITTLE_ENDIAN_ORDER
toddouska 0:5045d2638c29 197 ByteReverseWords(sha256->digest, sha256->digest, SHA256_DIGEST_SIZE);
toddouska 0:5045d2638c29 198 #endif
toddouska 0:5045d2638c29 199 XMEMCPY(hash, sha256->digest, SHA256_DIGEST_SIZE);
toddouska 0:5045d2638c29 200
toddouska 0:5045d2638c29 201 InitSha256(sha256); /* reset state */
toddouska 0:5045d2638c29 202 }
toddouska 0:5045d2638c29 203
toddouska 0:5045d2638c29 204
toddouska 0:5045d2638c29 205 #endif /* NO_SHA256 */
toddouska 0:5045d2638c29 206