1 /* Taken from public domain libtomcrypt library and the code and all changes
2 to it are in public domain. -Pekka */
4 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
7 #include "sha256_internal.h"
11 * SILC Hash API for SHA256
14 SILC_HASH_API_INIT(sha256)
19 SILC_HASH_API_UPDATE(sha256)
21 sha256_process(context, (unsigned char *)data, len);
24 SILC_HASH_API_FINAL(sha256)
26 sha256_done(context, digest);
29 SILC_HASH_API_TRANSFORM(sha256)
31 sha256_transform(state, (unsigned char *)buffer);
34 SILC_HASH_API_CONTEXT_LEN(sha256)
36 return sizeof(sha256_state);
40 #pragma intrinsic(_lrotr,_lrotl)
41 #define RORc(x,n) _lrotr(x,n)
43 #define RORc(x, y) silc_ror(x, y)
46 /* Various logical functions */
47 #define Ch(x,y,z) (z ^ (x & (y ^ z)))
48 #define Maj(x,y,z) (((x | y) & z) | (x & y))
49 #define S(x, n) RORc((x),(n))
50 #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
51 #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
52 #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
53 #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
54 #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
56 #ifndef SILC_SHA256_ASM
58 /* Transform 512-bits */
59 void sha256_transform(SilcUInt32 *state, unsigned char *buf)
61 SilcUInt32 S[8], W[64], t0, t1;
64 /* copy state into S */
65 for (i = 0; i < 8; i++) {
69 /* copy the state into 512-bits into W[0..15] */
70 for (i = 0; i < 16; i++)
71 SILC_GET32_MSB(W[i], buf + (4 * i));
74 for (i = 16; i < 64; i++) {
75 W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
79 #define RND(a,b,c,d,e,f,g,h,i,ki) \
80 t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
81 t1 = Sigma0(a) + Maj(a, b, c); \
85 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
86 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
87 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
88 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
89 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
90 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
91 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
92 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
93 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
94 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
95 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
96 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
97 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
98 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
99 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
100 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
101 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
102 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
103 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
104 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
105 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
106 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
107 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
108 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
109 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
110 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
111 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
112 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
113 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
114 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
115 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
116 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
117 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
118 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
119 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
120 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
121 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
122 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
123 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
124 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
125 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
126 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
127 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
128 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
129 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
130 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
131 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
132 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
133 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
134 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
135 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
136 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
137 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
138 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
139 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
140 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
141 RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
142 RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
143 RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
144 RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
145 RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
146 RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
147 RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
148 RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
153 for (i = 0; i < 8; i++) {
154 state[i] = state[i] + S[i];
158 #endif /* !SILC_SHA256_ASM */
160 int sha256_init(sha256_state * md)
164 md->state[0] = 0x6A09E667UL;
165 md->state[1] = 0xBB67AE85UL;
166 md->state[2] = 0x3C6EF372UL;
167 md->state[3] = 0xA54FF53AUL;
168 md->state[4] = 0x510E527FUL;
169 md->state[5] = 0x9B05688CUL;
170 md->state[6] = 0x1F83D9ABUL;
171 md->state[7] = 0x5BE0CD19UL;
176 #define MIN(x,y) ((x)<(y)?(x):(y))
179 int sha256_process(sha256_state * md, const unsigned char *in,
183 int block_size = sizeof(md->buf);
185 if (md->curlen > block_size)
189 if (md->curlen == 0 && inlen >= block_size) {
190 sha256_transform(md->state, (unsigned char *)in);
191 md->length += block_size * 8;
195 n = MIN(inlen, (block_size - md->curlen));
196 memcpy(md->buf + md->curlen, in, (size_t)n);
200 if (md->curlen == block_size) {
201 sha256_transform(md->state, md->buf);
202 md->length += block_size * 8;
210 int sha256_done(sha256_state * md, unsigned char *out)
214 if (md->curlen >= sizeof(md->buf))
217 /* increase the length of the message */
218 md->length += md->curlen * 8;
220 /* append the '1' bit */
221 md->buf[md->curlen++] = (unsigned char)0x80;
223 /* if the length is currently above 56 bytes we append zeros
224 * then compress. Then we can fall back to padding zeros and length
225 * encoding like normal.
227 if (md->curlen > 56) {
228 while (md->curlen < 64) {
229 md->buf[md->curlen++] = (unsigned char)0;
231 sha256_transform(md->state, md->buf);
235 /* pad upto 56 bytes of zeroes */
236 while (md->curlen < 56) {
237 md->buf[md->curlen++] = (unsigned char)0;
241 SILC_PUT64_MSB(md->length, md->buf + 56);
242 sha256_transform(md->state, md->buf);
245 for (i = 0; i < 8; i += 2) {
246 SILC_PUT32_MSB(md->state[i], out + (4 * i));
247 SILC_PUT32_MSB(md->state[i + 1], out + (4 * (i + 1)));