1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 #include "tomcrypt_private.h"
4 
5 /**
6   @file sha256.c
7   LTC_SHA256 by Tom St Denis
8 */
9 
10 #ifdef LTC_SHA256
11 
12 const struct ltc_hash_descriptor sha256_desc =
13 {
14     "sha256",
15     0,
16     32,
17     64,
18 
19     /* OID */
20    { 2, 16, 840, 1, 101, 3, 4, 2, 1,  },
21    9,
22 
23     &sha256_init,
24     &sha256_process,
25     &sha256_done,
26     &sha256_test,
27     NULL
28 };
29 
30 #ifdef LTC_SMALL_CODE
31 /* the K array */
32 static const ulong32 K[64] = {
33     0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
34     0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
35     0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
36     0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
37     0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
38     0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
39     0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
40     0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
41     0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
42     0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
43     0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
44     0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
45     0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
46 };
47 #endif
48 
49 /* Various logical functions */
50 #define Ch(x,y,z)       (z ^ (x & (y ^ z)))
51 #define Maj(x,y,z)      (((x | y) & z) | (x & y))
52 #define S(x, n)         RORc((x),(n))
53 #define R(x, n)         (((x)&0xFFFFFFFFUL)>>(n))
54 #define Sigma0(x)       (S(x, 2) ^ S(x, 13) ^ S(x, 22))
55 #define Sigma1(x)       (S(x, 6) ^ S(x, 11) ^ S(x, 25))
56 #define Gamma0(x)       (S(x, 7) ^ S(x, 18) ^ R(x, 3))
57 #define Gamma1(x)       (S(x, 17) ^ S(x, 19) ^ R(x, 10))
58 
59 /* compress 512-bits */
60 #ifdef LTC_CLEAN_STACK
ss_sha256_compress(hash_state * md,const unsigned char * buf)61 static int ss_sha256_compress(hash_state * md, const unsigned char *buf)
62 #else
63 static int s_sha256_compress(hash_state * md, const unsigned char *buf)
64 #endif
65 {
66     ulong32 S[8], W[64], t0, t1;
67 #ifdef LTC_SMALL_CODE
68     ulong32 t;
69 #endif
70     int i;
71 
72     /* copy state into S */
73     for (i = 0; i < 8; i++) {
74         S[i] = md->sha256.state[i];
75     }
76 
77     /* copy the state into 512-bits into W[0..15] */
78     for (i = 0; i < 16; i++) {
79         LOAD32H(W[i], buf + (4*i));
80     }
81 
82     /* fill W[16..63] */
83     for (i = 16; i < 64; i++) {
84         W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
85     }
86 
87     /* Compress */
88 #ifdef LTC_SMALL_CODE
89 #define RND(a,b,c,d,e,f,g,h,i)                         \
90      t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i];   \
91      t1 = Sigma0(a) + Maj(a, b, c);                    \
92      d += t0;                                          \
93      h  = t0 + t1;
94 
95      for (i = 0; i < 64; ++i) {
96          RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i);
97          t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
98          S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
99      }
100 #else
101 #define RND(a,b,c,d,e,f,g,h,i,ki)                    \
102      t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i];   \
103      t1 = Sigma0(a) + Maj(a, b, c);                  \
104      d += t0;                                        \
105      h  = t0 + t1;
106 
107     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
108     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
109     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
110     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
111     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
112     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
113     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
114     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
115     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
116     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
117     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
118     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
119     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
120     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
121     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
122     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
123     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
124     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
125     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
126     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
127     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
128     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
129     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
130     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
131     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
132     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
133     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
134     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
135     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
136     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
137     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
138     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
139     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
140     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
141     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
142     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
143     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
144     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
145     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
146     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
147     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
148     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
149     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
150     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
151     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
152     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
153     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
154     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
155     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
156     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
157     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
158     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
159     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
160     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
161     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
162     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
163     RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
164     RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
165     RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
166     RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
167     RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
168     RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
169     RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
170     RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
171 #endif
172 #undef RND
173 
174     /* feedback */
175     for (i = 0; i < 8; i++) {
176         md->sha256.state[i] = md->sha256.state[i] + S[i];
177     }
178     return CRYPT_OK;
179 }
180 
181 #ifdef LTC_CLEAN_STACK
s_sha256_compress(hash_state * md,const unsigned char * buf)182 static int s_sha256_compress(hash_state * md, const unsigned char *buf)
183 {
184     int err;
185     err = ss_sha256_compress(md, buf);
186     burn_stack(sizeof(ulong32) * 74);
187     return err;
188 }
189 #endif
190 
191 /**
192    Initialize the hash state
193    @param md   The hash state you wish to initialize
194    @return CRYPT_OK if successful
195 */
sha256_init(hash_state * md)196 int sha256_init(hash_state * md)
197 {
198     LTC_ARGCHK(md != NULL);
199 
200     md->sha256.curlen = 0;
201     md->sha256.length = 0;
202     md->sha256.state[0] = 0x6A09E667UL;
203     md->sha256.state[1] = 0xBB67AE85UL;
204     md->sha256.state[2] = 0x3C6EF372UL;
205     md->sha256.state[3] = 0xA54FF53AUL;
206     md->sha256.state[4] = 0x510E527FUL;
207     md->sha256.state[5] = 0x9B05688CUL;
208     md->sha256.state[6] = 0x1F83D9ABUL;
209     md->sha256.state[7] = 0x5BE0CD19UL;
210     return CRYPT_OK;
211 }
212 
213 /**
214    Process a block of memory though the hash
215    @param md     The hash state
216    @param in     The data to hash
217    @param inlen  The length of the data (octets)
218    @return CRYPT_OK if successful
219 */
220 HASH_PROCESS(sha256_process,s_sha256_compress, sha256, 64)
221 
222 /**
223    Terminate the hash to get the digest
224    @param md  The hash state
225    @param out [out] The destination of the hash (32 bytes)
226    @return CRYPT_OK if successful
227 */
sha256_done(hash_state * md,unsigned char * out)228 int sha256_done(hash_state * md, unsigned char *out)
229 {
230     int i;
231 
232     LTC_ARGCHK(md  != NULL);
233     LTC_ARGCHK(out != NULL);
234 
235     if (md->sha256.curlen >= sizeof(md->sha256.buf)) {
236        return CRYPT_INVALID_ARG;
237     }
238 
239 
240     /* increase the length of the message */
241     md->sha256.length += md->sha256.curlen * 8;
242 
243     /* append the '1' bit */
244     md->sha256.buf[md->sha256.curlen++] = (unsigned char)0x80;
245 
246     /* if the length is currently above 56 bytes we append zeros
247      * then compress.  Then we can fall back to padding zeros and length
248      * encoding like normal.
249      */
250     if (md->sha256.curlen > 56) {
251         while (md->sha256.curlen < 64) {
252             md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
253         }
254         s_sha256_compress(md, md->sha256.buf);
255         md->sha256.curlen = 0;
256     }
257 
258     /* pad upto 56 bytes of zeroes */
259     while (md->sha256.curlen < 56) {
260         md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
261     }
262 
263     /* store length */
264     STORE64H(md->sha256.length, md->sha256.buf+56);
265     s_sha256_compress(md, md->sha256.buf);
266 
267     /* copy output */
268     for (i = 0; i < 8; i++) {
269         STORE32H(md->sha256.state[i], out+(4*i));
270     }
271 #ifdef LTC_CLEAN_STACK
272     zeromem(md, sizeof(hash_state));
273 #endif
274     return CRYPT_OK;
275 }
276 
277 /**
278   Self-test the hash
279   @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled
280 */
sha256_test(void)281 int  sha256_test(void)
282 {
283  #ifndef LTC_TEST
284     return CRYPT_NOP;
285  #else
286   static const struct {
287       const char *msg;
288       unsigned char hash[32];
289   } tests[] = {
290     { "abc",
291       { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
292         0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
293         0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
294         0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }
295     },
296     { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
297       { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
298         0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
299         0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
300         0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }
301     },
302   };
303 
304   int i;
305   unsigned char tmp[32];
306   hash_state md;
307 
308   for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
309       sha256_init(&md);
310       sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)XSTRLEN(tests[i].msg));
311       sha256_done(&md, tmp);
312       if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "SHA256", i)) {
313          return CRYPT_FAIL_TESTVECTOR;
314       }
315   }
316   return CRYPT_OK;
317  #endif
318 }
319 
320 #endif
321 
322 
323