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