Added SILC Server library.
[silc.git] / lib / silccrypt / sha256.c
1 /* Modified for SILC -Pekka */
2
3 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
4  *
5  * LibTomCrypt is a library that provides various cryptographic
6  * algorithms in a highly modular and flexible manner.
7  *
8  * The library is free for all purposes without any express
9  * guarantee it works.
10  *
11  * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
12  */
13 #include "silc.h"
14 #include "sha256_internal.h"
15 #include "sha256.h"
16
17 /*
18  * SILC Hash API for SHA256
19  */
20
21 SILC_HASH_API_INIT(sha256)
22 {
23   sha256_init(context);
24 }
25
26 SILC_HASH_API_UPDATE(sha256)
27 {
28   sha256_process(context, (unsigned char *)data, len);
29 }
30
31 SILC_HASH_API_FINAL(sha256)
32 {
33   sha256_done(context, digest);
34 }
35
36 SILC_HASH_API_TRANSFORM(sha256)
37 {
38   sha256_compress(state, (unsigned char *)buffer);
39 }
40
41 SILC_HASH_API_CONTEXT_LEN(sha256)
42 {
43   return sizeof(sha256_state);
44 }
45
46 #if defined(_MSC_VER)
47 #pragma intrinsic(_lrotr,_lrotl)
48 #define RORc(x,n) _lrotr(x,n)
49
50 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC)
51
52 static inline unsigned RORc(unsigned word, int i)
53 {
54    asm ("rorl %%cl,%0"
55       :"=r" (word)
56       :"0" (word),"c" (i));
57    return word;
58 }
59
60 #else
61 #define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsignedlong)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) &0xFFFFFFFFUL)
62 #endif /* _MSC_VER */
63
64 /* Various logical functions */
65 #define Ch(x,y,z)       (z ^ (x & (y ^ z)))
66 #define Maj(x,y,z)      (((x | y) & z) | (x & y)) 
67 #define S(x, n)         RORc((x),(n))
68 #define R(x, n)         (((x)&0xFFFFFFFFUL)>>(n))
69 #define Sigma0(x)       (S(x, 2) ^ S(x, 13) ^ S(x, 22))
70 #define Sigma1(x)       (S(x, 6) ^ S(x, 11) ^ S(x, 25))
71 #define Gamma0(x)       (S(x, 7) ^ S(x, 18) ^ R(x, 3))
72 #define Gamma1(x)       (S(x, 17) ^ S(x, 19) ^ R(x, 10))
73
74 /* compress 512-bits */
75 int  sha256_compress(SilcUInt32 *state, unsigned char *buf)
76 {
77     SilcUInt32 S[8], W[64], t0, t1;
78     int i;
79
80     /* copy state into S */
81     for (i = 0; i < 8; i++) {
82         S[i] = state[i];
83     }
84
85     /* copy the state into 512-bits into W[0..15] */
86     for (i = 0; i < 16; i++)
87         SILC_GET32_MSB(W[i], buf + (4 * i));
88
89     /* fill W[16..63] */
90     for (i = 16; i < 64; i++) {
91         W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
92     }        
93
94     /* Compress */
95 #define RND(a,b,c,d,e,f,g,h,i,ki)                    \
96      t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i];   \
97      t1 = Sigma0(a) + Maj(a, b, c);                  \
98      d += t0;                                        \
99      h  = t0 + t1;
100
101     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
102     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
103     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
104     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
105     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
106     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
107     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
108     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
109     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
110     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
111     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
112     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
113     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
114     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
115     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
116     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
117     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
118     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
119     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
120     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
121     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
122     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
123     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
124     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
125     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
126     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
127     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
128     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
129     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
130     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
131     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
132     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
133     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
134     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
135     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
136     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
137     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
138     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
139     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
140     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
141     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
142     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
143     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
144     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
145     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
146     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
147     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
148     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
149     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
150     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
151     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
152     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
153     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
154     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
155     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
156     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
157     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
158     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
159     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
160     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
161     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
162     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
163     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
164     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
165     
166 #undef RND     
167
168     /* feedback */
169     for (i = 0; i < 8; i++) {
170         state[i] = state[i] + S[i];
171     }
172     return TRUE;
173 }
174
175 /**
176    Initialize the hash state
177    @param md   The hash state you wish to initialize
178    @return CRYPT_OK if successful
179 */
180 int sha256_init(sha256_state * md)
181 {
182     md->length = 0;
183     md->curlen = 0;
184     md->state[0] = 0x6A09E667UL;
185     md->state[1] = 0xBB67AE85UL;
186     md->state[2] = 0x3C6EF372UL;
187     md->state[3] = 0xA54FF53AUL;
188     md->state[4] = 0x510E527FUL;
189     md->state[5] = 0x9B05688CUL;
190     md->state[6] = 0x1F83D9ABUL;
191     md->state[7] = 0x5BE0CD19UL;
192     return TRUE;
193 }
194
195 #if !defined(MIN)
196 #define MIN(x,y) ((x)<(y)?(x):(y))
197 #endif
198
199 /**
200    Process a block of memory though the hash
201    @param md     The hash state
202    @param in     The data to hash
203    @param inlen  The length of the data (octets)
204    @return CRYPT_OK if successful
205 */
206 int sha256_process(sha256_state * md, const unsigned char *in,
207                    unsigned long inlen)
208 {
209     unsigned long n;
210     int err, block_size = sizeof(md->buf);
211
212     if (md->curlen > block_size)
213         return FALSE;
214
215     while (inlen > 0) {                                                        
216         if (md->curlen == 0 && inlen >= block_size) {
217             if ((err = sha256_compress(md->state, (unsigned char *)in)) != TRUE)
218                 return err;
219             md->length += block_size * 8;
220             in             += block_size;
221             inlen          -= block_size;
222         } else {
223             n = MIN(inlen, (block_size - md->curlen));              
224             memcpy(md->buf + md->curlen, in, (size_t)n);
225             md->curlen += n;
226             in             += n;
227             inlen          -= n;
228             if (md->curlen == block_size) {
229                 if ((err = sha256_compress(md->state, md->buf)) != TRUE)
230                      return err;
231                 md->length += block_size * 8;
232                 md->curlen = 0;
233             }
234         }
235     }
236     return TRUE;
237 }
238
239 /**
240    Terminate the hash to get the digest
241    @param md  The hash state
242    @param out [out] The destination of the hash (32 bytes)
243    @return CRYPT_OK if successful
244 */
245 int sha256_done(sha256_state * md, unsigned char *out)
246 {
247     int i;
248
249     if (md->curlen >= sizeof(md->buf))
250         return FALSE;
251
252     /* increase the length of the message */
253     md->length += md->curlen * 8;
254
255     /* append the '1' bit */
256     md->buf[md->curlen++] = (unsigned char)0x80;
257
258     /* if the length is currently above 56 bytes we append zeros
259      * then compress.  Then we can fall back to padding zeros and length
260      * encoding like normal.
261      */
262     if (md->curlen > 56) {
263         while (md->curlen < 64) {
264             md->buf[md->curlen++] = (unsigned char)0;
265         }
266         sha256_compress(md->state, md->buf);
267         md->curlen = 0;
268     }
269
270     /* pad upto 56 bytes of zeroes */
271     while (md->curlen < 56) {
272         md->buf[md->curlen++] = (unsigned char)0;
273     }
274
275     /* store length */
276     SILC_PUT64_MSB(md->length, md->buf + 56);
277     sha256_compress(md->state, md->buf);
278
279     /* copy output */
280     for (i = 0; i < 8; i++)
281         SILC_PUT32_MSB(md->state[i], out + (4 * i));
282
283     return TRUE;
284 }