]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/CyaSSL/ctaocrypt/src/sha.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS-Plus / CyaSSL / ctaocrypt / src / sha.c
1 /* sha.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 #include <cyassl/ctaocrypt/sha.h>
27 #ifdef NO_INLINE
28     #include <cyassl/ctaocrypt/misc.h>
29 #else
30     #include <ctaocrypt/src/misc.c>
31 #endif
32
33
34 #ifndef min
35
36     static INLINE word32 min(word32 a, word32 b)
37     {
38         return a > b ? b : a;
39     }
40
41 #endif /* min */
42
43
44 void InitSha(Sha* sha)
45 {
46     sha->digest[0] = 0x67452301L;
47     sha->digest[1] = 0xEFCDAB89L;
48     sha->digest[2] = 0x98BADCFEL;
49     sha->digest[3] = 0x10325476L;
50     sha->digest[4] = 0xC3D2E1F0L;
51
52     sha->buffLen = 0;
53     sha->loLen   = 0;
54     sha->hiLen   = 0;
55 }
56
57 #define blk0(i) (W[i] = sha->buffer[i])
58 #define blk1(i) (W[i&15] = \
59                    rotlFixed(W[(i+13)&15]^W[(i+8)&15]^W[(i+2)&15]^W[i&15],1))
60
61 #define f1(x,y,z) (z^(x &(y^z)))
62 #define f2(x,y,z) (x^y^z)
63 #define f3(x,y,z) ((x&y)|(z&(x|y)))
64 #define f4(x,y,z) (x^y^z)
65
66 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
67 #define R0(v,w,x,y,z,i) z+= f1(w,x,y) + blk0(i) + 0x5A827999+ \
68                         rotlFixed(v,5); w = rotlFixed(w,30);
69 #define R1(v,w,x,y,z,i) z+= f1(w,x,y) + blk1(i) + 0x5A827999+ \
70                         rotlFixed(v,5); w = rotlFixed(w,30);
71 #define R2(v,w,x,y,z,i) z+= f2(w,x,y) + blk1(i) + 0x6ED9EBA1+ \
72                         rotlFixed(v,5); w = rotlFixed(w,30);
73 #define R3(v,w,x,y,z,i) z+= f3(w,x,y) + blk1(i) + 0x8F1BBCDC+ \
74                         rotlFixed(v,5); w = rotlFixed(w,30);
75 #define R4(v,w,x,y,z,i) z+= f4(w,x,y) + blk1(i) + 0xCA62C1D6+ \
76                         rotlFixed(v,5); w = rotlFixed(w,30);
77
78
79 static void Transform(Sha* sha)
80 {
81     word32 W[SHA_BLOCK_SIZE / sizeof(word32)];
82
83     /* Copy context->state[] to working vars */ 
84     word32 a = sha->digest[0];
85     word32 b = sha->digest[1];
86     word32 c = sha->digest[2];
87     word32 d = sha->digest[3];
88     word32 e = sha->digest[4];
89
90     /* nearly 1 K bigger in code size but 25% faster  */
91     /* 4 rounds of 20 operations each. Loop unrolled. */
92     R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
93     R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
94     R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
95     R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
96
97     R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
98
99     R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
100     R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
101     R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
102     R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
103     R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
104
105     R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
106     R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
107     R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
108     R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
109     R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
110
111     R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
112     R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
113     R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
114     R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
115     R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
116
117     /* Add the working vars back into digest state[] */
118     sha->digest[0] += a;
119     sha->digest[1] += b;
120     sha->digest[2] += c;
121     sha->digest[3] += d;
122     sha->digest[4] += e;
123 }
124
125
126 static INLINE void AddLength(Sha* sha, word32 len)
127 {
128     word32 tmp = sha->loLen;
129     if ( (sha->loLen += len) < tmp)
130         sha->hiLen++;                       /* carry low to high */
131 }
132
133
134 void ShaUpdate(Sha* sha, const byte* data, word32 len)
135 {
136     /* do block size increments */
137     byte* local = (byte*)sha->buffer;
138
139     while (len) {
140         word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen);
141         XMEMCPY(&local[sha->buffLen], data, add);
142
143         sha->buffLen += add;
144         data         += add;
145         len          -= add;
146
147         if (sha->buffLen == SHA_BLOCK_SIZE) {
148             #ifdef LITTLE_ENDIAN_ORDER
149                 ByteReverseBytes(local, local, SHA_BLOCK_SIZE);
150             #endif
151             Transform(sha);
152             AddLength(sha, SHA_BLOCK_SIZE);
153             sha->buffLen = 0;
154         }
155     }
156 }
157
158
159 void ShaFinal(Sha* sha, byte* hash)
160 {
161     byte* local = (byte*)sha->buffer;
162
163     AddLength(sha, sha->buffLen);               /* before adding pads */
164
165     local[sha->buffLen++] = 0x80;  /* add 1 */
166
167     /* pad with zeros */
168     if (sha->buffLen > SHA_PAD_SIZE) {
169         XMEMSET(&local[sha->buffLen], 0, SHA_BLOCK_SIZE - sha->buffLen);
170         sha->buffLen += SHA_BLOCK_SIZE - sha->buffLen;
171
172         #ifdef LITTLE_ENDIAN_ORDER
173             ByteReverseBytes(local, local, SHA_BLOCK_SIZE);
174         #endif
175         Transform(sha);
176         sha->buffLen = 0;
177     }
178     XMEMSET(&local[sha->buffLen], 0, SHA_PAD_SIZE - sha->buffLen);
179    
180     /* put lengths in bits */
181     sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) + 
182                  (sha->hiLen << 3);
183     sha->loLen = sha->loLen << 3;
184
185     /* store lengths */
186     #ifdef LITTLE_ENDIAN_ORDER
187         ByteReverseBytes(local, local, SHA_BLOCK_SIZE);
188     #endif
189     /* ! length ordering dependent on digest endian type ! */
190     XMEMCPY(&local[SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
191     XMEMCPY(&local[SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));
192
193     Transform(sha);
194     #ifdef LITTLE_ENDIAN_ORDER
195         ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
196     #endif
197     XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);
198
199     InitSha(sha);  /* reset state */
200 }
201