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