1 /* Modified for SILC -Pekka */
3 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
5 * LibTomCrypt is a library that provides various cryptographic
6 * algorithms in a highly modular and flexible manner.
8 * The library is free for all purposes without any express
11 * Tom St Denis, tomstdenis@gmail.com, http://libtomcrypt.org
14 #include "sha256_internal.h"
18 * SILC Hash API for SHA256
21 SILC_HASH_API_INIT(sha256)
26 SILC_HASH_API_UPDATE(sha256)
28 sha256_process(context, (unsigned char *)data, len);
31 SILC_HASH_API_FINAL(sha256)
33 sha256_done(context, digest);
36 SILC_HASH_API_TRANSFORM(sha256)
38 sha256_compress(state, (unsigned char *)buffer);
41 SILC_HASH_API_CONTEXT_LEN(sha256)
43 return sizeof(sha256_state);
47 #pragma intrinsic(_lrotr,_lrotl)
48 #define RORc(x,n) _lrotr(x,n)
50 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC)
52 static inline unsigned RORc(unsigned word, int i)
61 #define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) &0xFFFFFFFFUL)
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))
74 /* compress 512-bits */
75 int sha256_compress(SilcUInt32 *state, unsigned char *buf)
77 SilcUInt32 S[8], W[64], t0, t1;
80 /* copy state into S */
81 for (i = 0; i < 8; i++) {
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));
90 for (i = 16; i < 64; i++) {
91 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
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); \
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);
169 for (i = 0; i < 8; i++) {
170 state[i] = state[i] + S[i];
176 Initialize the hash state
177 @param md The hash state you wish to initialize
178 @return CRYPT_OK if successful
180 int sha256_init(sha256_state * md)
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;
196 #define MIN(x,y) ((x)<(y)?(x):(y))
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
206 int sha256_process(sha256_state * md, const unsigned char *in,
210 int err, block_size = sizeof(md->buf);
212 if (md->curlen > block_size)
216 if (md->curlen == 0 && inlen >= block_size) {
217 if ((err = sha256_compress(md->state, (unsigned char *)in)) != TRUE)
219 md->length += block_size * 8;
223 n = MIN(inlen, (block_size - md->curlen));
224 memcpy(md->buf + md->curlen, in, (size_t)n);
228 if (md->curlen == block_size) {
229 if ((err = sha256_compress(md->state, md->buf)) != TRUE)
231 md->length += block_size * 8;
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
245 int sha256_done(sha256_state * md, unsigned char *out)
249 if (md->curlen >= sizeof(md->buf))
252 /* increase the length of the message */
253 md->length += md->curlen * 8;
255 /* append the '1' bit */
256 md->buf[md->curlen++] = (unsigned char)0x80;
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.
262 if (md->curlen > 56) {
263 while (md->curlen < 64) {
264 md->buf[md->curlen++] = (unsigned char)0;
266 sha256_compress(md->state, md->buf);
270 /* pad upto 56 bytes of zeroes */
271 while (md->curlen < 56) {
272 md->buf[md->curlen++] = (unsigned char)0;
276 SILC_PUT64_MSB(md->length, md->buf + 56);
277 sha256_compress(md->state, md->buf);
280 for (i = 0; i < 8; i++)
281 SILC_PUT32_MSB(md->state[i], out + (4 * i));