sshsh256.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * SHA-256 algorithm as described at
  3. *
  4. * http://csrc.nist.gov/cryptval/shs.html
  5. */
  6. #include "ssh.h"
  7. /* ----------------------------------------------------------------------
  8. * Core SHA256 algorithm: processes 16-word blocks into a message digest.
  9. */
  10. #define ror(x,y) ( ((x) << (32-y)) | (((uint32)(x)) >> (y)) )
  11. #define shr(x,y) ( (((uint32)(x)) >> (y)) )
  12. #define Ch(x,y,z) ( ((x) & (y)) ^ (~(x) & (z)) )
  13. #define Maj(x,y,z) ( ((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)) )
  14. #define bigsigma0(x) ( ror((x),2) ^ ror((x),13) ^ ror((x),22) )
  15. #define bigsigma1(x) ( ror((x),6) ^ ror((x),11) ^ ror((x),25) )
  16. #define smallsigma0(x) ( ror((x),7) ^ ror((x),18) ^ shr((x),3) )
  17. #define smallsigma1(x) ( ror((x),17) ^ ror((x),19) ^ shr((x),10) )
  18. void SHA256_Core_Init(SHA256_State *s) {
  19. s->h[0] = 0x6a09e667;
  20. s->h[1] = 0xbb67ae85;
  21. s->h[2] = 0x3c6ef372;
  22. s->h[3] = 0xa54ff53a;
  23. s->h[4] = 0x510e527f;
  24. s->h[5] = 0x9b05688c;
  25. s->h[6] = 0x1f83d9ab;
  26. s->h[7] = 0x5be0cd19;
  27. }
  28. void SHA256_Block(SHA256_State *s, uint32 *block) {
  29. uint32 w[80];
  30. uint32 a,b,c,d,e,f,g,h;
  31. static const int k[] = {
  32. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  33. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  34. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  35. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  36. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  37. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  38. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  39. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  40. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  41. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  42. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  43. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  44. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  45. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  46. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  47. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  48. };
  49. int t;
  50. for (t = 0; t < 16; t++)
  51. w[t] = block[t];
  52. for (t = 16; t < 64; t++)
  53. w[t] = smallsigma1(w[t-2]) + w[t-7] + smallsigma0(w[t-15]) + w[t-16];
  54. a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3];
  55. e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7];
  56. for (t = 0; t < 64; t+=8) {
  57. uint32 t1, t2;
  58. #define ROUND(j,a,b,c,d,e,f,g,h) \
  59. t1 = h + bigsigma1(e) + Ch(e,f,g) + k[j] + w[j]; \
  60. t2 = bigsigma0(a) + Maj(a,b,c); \
  61. d = d + t1; h = t1 + t2;
  62. ROUND(t+0, a,b,c,d,e,f,g,h);
  63. ROUND(t+1, h,a,b,c,d,e,f,g);
  64. ROUND(t+2, g,h,a,b,c,d,e,f);
  65. ROUND(t+3, f,g,h,a,b,c,d,e);
  66. ROUND(t+4, e,f,g,h,a,b,c,d);
  67. ROUND(t+5, d,e,f,g,h,a,b,c);
  68. ROUND(t+6, c,d,e,f,g,h,a,b);
  69. ROUND(t+7, b,c,d,e,f,g,h,a);
  70. }
  71. s->h[0] += a; s->h[1] += b; s->h[2] += c; s->h[3] += d;
  72. s->h[4] += e; s->h[5] += f; s->h[6] += g; s->h[7] += h;
  73. }
  74. /* ----------------------------------------------------------------------
  75. * Outer SHA256 algorithm: take an arbitrary length byte string,
  76. * convert it into 16-word blocks with the prescribed padding at
  77. * the end, and pass those blocks to the core SHA256 algorithm.
  78. */
  79. #define BLKSIZE 64
  80. void SHA256_Init(SHA256_State *s) {
  81. SHA256_Core_Init(s);
  82. s->blkused = 0;
  83. s->lenhi = s->lenlo = 0;
  84. }
  85. void SHA256_Bytes(SHA256_State *s, const void *p, int len) {
  86. unsigned char *q = (unsigned char *)p;
  87. uint32 wordblock[16];
  88. uint32 lenw = len;
  89. int i;
  90. /*
  91. * Update the length field.
  92. */
  93. s->lenlo += lenw;
  94. s->lenhi += (s->lenlo < lenw);
  95. if (s->blkused && s->blkused+len < BLKSIZE) {
  96. /*
  97. * Trivial case: just add to the block.
  98. */
  99. memcpy(s->block + s->blkused, q, len);
  100. s->blkused += len;
  101. } else {
  102. /*
  103. * We must complete and process at least one block.
  104. */
  105. while (s->blkused + len >= BLKSIZE) {
  106. memcpy(s->block + s->blkused, q, BLKSIZE - s->blkused);
  107. q += BLKSIZE - s->blkused;
  108. len -= BLKSIZE - s->blkused;
  109. /* Now process the block. Gather bytes big-endian into words */
  110. for (i = 0; i < 16; i++) {
  111. wordblock[i] =
  112. ( ((uint32)s->block[i*4+0]) << 24 ) |
  113. ( ((uint32)s->block[i*4+1]) << 16 ) |
  114. ( ((uint32)s->block[i*4+2]) << 8 ) |
  115. ( ((uint32)s->block[i*4+3]) << 0 );
  116. }
  117. SHA256_Block(s, wordblock);
  118. s->blkused = 0;
  119. }
  120. memcpy(s->block, q, len);
  121. s->blkused = len;
  122. }
  123. }
  124. void SHA256_Final(SHA256_State *s, unsigned char *digest) {
  125. int i;
  126. int pad;
  127. unsigned char c[64];
  128. uint32 lenhi, lenlo;
  129. if (s->blkused >= 56)
  130. pad = 56 + 64 - s->blkused;
  131. else
  132. pad = 56 - s->blkused;
  133. lenhi = (s->lenhi << 3) | (s->lenlo >> (32-3));
  134. lenlo = (s->lenlo << 3);
  135. memset(c, 0, pad);
  136. c[0] = 0x80;
  137. SHA256_Bytes(s, &c, pad);
  138. c[0] = (lenhi >> 24) & 0xFF;
  139. c[1] = (lenhi >> 16) & 0xFF;
  140. c[2] = (lenhi >> 8) & 0xFF;
  141. c[3] = (lenhi >> 0) & 0xFF;
  142. c[4] = (lenlo >> 24) & 0xFF;
  143. c[5] = (lenlo >> 16) & 0xFF;
  144. c[6] = (lenlo >> 8) & 0xFF;
  145. c[7] = (lenlo >> 0) & 0xFF;
  146. SHA256_Bytes(s, &c, 8);
  147. for (i = 0; i < 8; i++) {
  148. digest[i*4+0] = (s->h[i] >> 24) & 0xFF;
  149. digest[i*4+1] = (s->h[i] >> 16) & 0xFF;
  150. digest[i*4+2] = (s->h[i] >> 8) & 0xFF;
  151. digest[i*4+3] = (s->h[i] >> 0) & 0xFF;
  152. }
  153. }
  154. void SHA256_Simple(const void *p, int len, unsigned char *output) {
  155. SHA256_State s;
  156. SHA256_Init(&s);
  157. SHA256_Bytes(&s, p, len);
  158. SHA256_Final(&s, output);
  159. }
  160. /*
  161. * Thin abstraction for things where hashes are pluggable.
  162. */
  163. static void *sha256_init(void)
  164. {
  165. SHA256_State *s;
  166. s = snew(SHA256_State);
  167. SHA256_Init(s);
  168. return s;
  169. }
  170. static void sha256_bytes(void *handle, void *p, int len)
  171. {
  172. SHA256_State *s = handle;
  173. SHA256_Bytes(s, p, len);
  174. }
  175. static void sha256_final(void *handle, unsigned char *output)
  176. {
  177. SHA256_State *s = handle;
  178. SHA256_Final(s, output);
  179. sfree(s);
  180. }
  181. const struct ssh_hash ssh_sha256 = {
  182. sha256_init, sha256_bytes, sha256_final, 32, "SHA-256"
  183. };
  184. /* ----------------------------------------------------------------------
  185. * The above is the SHA-256 algorithm itself. Now we implement the
  186. * HMAC wrapper on it.
  187. */
  188. static void *sha256_make_context(void)
  189. {
  190. return snewn(3, SHA256_State);
  191. }
  192. static void sha256_free_context(void *handle)
  193. {
  194. sfree(handle);
  195. }
  196. static void sha256_key_internal(void *handle, unsigned char *key, int len)
  197. {
  198. SHA256_State *keys = (SHA256_State *)handle;
  199. unsigned char foo[64];
  200. int i;
  201. memset(foo, 0x36, 64);
  202. for (i = 0; i < len && i < 64; i++)
  203. foo[i] ^= key[i];
  204. SHA256_Init(&keys[0]);
  205. SHA256_Bytes(&keys[0], foo, 64);
  206. memset(foo, 0x5C, 64);
  207. for (i = 0; i < len && i < 64; i++)
  208. foo[i] ^= key[i];
  209. SHA256_Init(&keys[1]);
  210. SHA256_Bytes(&keys[1], foo, 64);
  211. smemclr(foo, 64); /* burn the evidence */
  212. }
  213. static void sha256_key(void *handle, unsigned char *key)
  214. {
  215. sha256_key_internal(handle, key, 32);
  216. }
  217. static void hmacsha256_start(void *handle)
  218. {
  219. SHA256_State *keys = (SHA256_State *)handle;
  220. keys[2] = keys[0]; /* structure copy */
  221. }
  222. static void hmacsha256_bytes(void *handle, unsigned char const *blk, int len)
  223. {
  224. SHA256_State *keys = (SHA256_State *)handle;
  225. SHA256_Bytes(&keys[2], (void *)blk, len);
  226. }
  227. static void hmacsha256_genresult(void *handle, unsigned char *hmac)
  228. {
  229. SHA256_State *keys = (SHA256_State *)handle;
  230. SHA256_State s;
  231. unsigned char intermediate[32];
  232. s = keys[2]; /* structure copy */
  233. SHA256_Final(&s, intermediate);
  234. s = keys[1]; /* structure copy */
  235. SHA256_Bytes(&s, intermediate, 32);
  236. SHA256_Final(&s, hmac);
  237. }
  238. static void sha256_do_hmac(void *handle, unsigned char *blk, int len,
  239. unsigned long seq, unsigned char *hmac)
  240. {
  241. unsigned char seqbuf[4];
  242. PUT_32BIT_MSB_FIRST(seqbuf, seq);
  243. hmacsha256_start(handle);
  244. hmacsha256_bytes(handle, seqbuf, 4);
  245. hmacsha256_bytes(handle, blk, len);
  246. hmacsha256_genresult(handle, hmac);
  247. }
  248. static void sha256_generate(void *handle, unsigned char *blk, int len,
  249. unsigned long seq)
  250. {
  251. sha256_do_hmac(handle, blk, len, seq, blk + len);
  252. }
  253. static int hmacsha256_verresult(void *handle, unsigned char const *hmac)
  254. {
  255. unsigned char correct[32];
  256. hmacsha256_genresult(handle, correct);
  257. return !memcmp(correct, hmac, 32);
  258. }
  259. static int sha256_verify(void *handle, unsigned char *blk, int len,
  260. unsigned long seq)
  261. {
  262. unsigned char correct[32];
  263. sha256_do_hmac(handle, blk, len, seq, correct);
  264. return !memcmp(correct, blk + len, 32);
  265. }
  266. const struct ssh_mac ssh_hmac_sha256 = {
  267. sha256_make_context, sha256_free_context, sha256_key,
  268. sha256_generate, sha256_verify,
  269. hmacsha256_start, hmacsha256_bytes,
  270. hmacsha256_genresult, hmacsha256_verresult,
  271. "hmac-sha2-256",
  272. 32,
  273. "HMAC-SHA-256"
  274. };
  275. #ifdef TEST
  276. #include <stdio.h>
  277. #include <stdlib.h>
  278. #include <assert.h>
  279. int main(void) {
  280. unsigned char digest[32];
  281. int i, j, errors;
  282. struct {
  283. const char *teststring;
  284. unsigned char digest[32];
  285. } tests[] = {
  286. { "abc", {
  287. 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
  288. 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
  289. 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
  290. 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad,
  291. } },
  292. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", {
  293. 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
  294. 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
  295. 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
  296. 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1,
  297. } },
  298. };
  299. errors = 0;
  300. for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
  301. SHA256_Simple(tests[i].teststring,
  302. strlen(tests[i].teststring), digest);
  303. for (j = 0; j < 32; j++) {
  304. if (digest[j] != tests[i].digest[j]) {
  305. fprintf(stderr,
  306. "\"%s\" digest byte %d should be 0x%02x, is 0x%02x\n",
  307. tests[i].teststring, j, tests[i].digest[j], digest[j]);
  308. errors++;
  309. }
  310. }
  311. }
  312. printf("%d errors\n", errors);
  313. return 0;
  314. }
  315. #endif