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