sshsh256.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. smemclr(&s, sizeof(s));
  160. }
  161. /*
  162. * Thin abstraction for things where hashes are pluggable.
  163. */
  164. static void *sha256_init(void)
  165. {
  166. SHA256_State *s;
  167. s = snew(SHA256_State);
  168. SHA256_Init(s);
  169. return s;
  170. }
  171. static void *sha256_copy(const void *vold)
  172. {
  173. const SHA256_State *old = (const SHA256_State *)vold;
  174. SHA256_State *s;
  175. s = snew(SHA256_State);
  176. *s = *old;
  177. return s;
  178. }
  179. static void sha256_free(void *handle)
  180. {
  181. SHA256_State *s = handle;
  182. smemclr(s, sizeof(*s));
  183. sfree(s);
  184. }
  185. static void sha256_bytes(void *handle, const void *p, int len)
  186. {
  187. SHA256_State *s = handle;
  188. SHA256_Bytes(s, p, len);
  189. }
  190. static void sha256_final(void *handle, unsigned char *output)
  191. {
  192. SHA256_State *s = handle;
  193. SHA256_Final(s, output);
  194. sha256_free(s);
  195. }
  196. const struct ssh_hash ssh_sha256 = {
  197. sha256_init, sha256_copy, sha256_bytes, sha256_final, sha256_free,
  198. 32, "SHA-256"
  199. };
  200. /* ----------------------------------------------------------------------
  201. * The above is the SHA-256 algorithm itself. Now we implement the
  202. * HMAC wrapper on it.
  203. */
  204. static void *sha256_make_context(void *cipher_ctx)
  205. {
  206. return snewn(3, SHA256_State);
  207. }
  208. static void sha256_free_context(void *handle)
  209. {
  210. smemclr(handle, 3 * sizeof(SHA256_State));
  211. sfree(handle);
  212. }
  213. static void sha256_key_internal(void *handle, unsigned char *key, int len)
  214. {
  215. SHA256_State *keys = (SHA256_State *)handle;
  216. unsigned char foo[64];
  217. int i;
  218. memset(foo, 0x36, 64);
  219. for (i = 0; i < len && i < 64; i++)
  220. foo[i] ^= key[i];
  221. SHA256_Init(&keys[0]);
  222. SHA256_Bytes(&keys[0], foo, 64);
  223. memset(foo, 0x5C, 64);
  224. for (i = 0; i < len && i < 64; i++)
  225. foo[i] ^= key[i];
  226. SHA256_Init(&keys[1]);
  227. SHA256_Bytes(&keys[1], foo, 64);
  228. smemclr(foo, 64); /* burn the evidence */
  229. }
  230. static void sha256_key(void *handle, unsigned char *key)
  231. {
  232. sha256_key_internal(handle, key, 32);
  233. }
  234. static void hmacsha256_start(void *handle)
  235. {
  236. SHA256_State *keys = (SHA256_State *)handle;
  237. keys[2] = keys[0]; /* structure copy */
  238. }
  239. static void hmacsha256_bytes(void *handle, unsigned char const *blk, int len)
  240. {
  241. SHA256_State *keys = (SHA256_State *)handle;
  242. SHA256_Bytes(&keys[2], (void *)blk, len);
  243. }
  244. static void hmacsha256_genresult(void *handle, unsigned char *hmac)
  245. {
  246. SHA256_State *keys = (SHA256_State *)handle;
  247. SHA256_State s;
  248. unsigned char intermediate[32];
  249. s = keys[2]; /* structure copy */
  250. SHA256_Final(&s, intermediate);
  251. s = keys[1]; /* structure copy */
  252. SHA256_Bytes(&s, intermediate, 32);
  253. SHA256_Final(&s, hmac);
  254. }
  255. static void sha256_do_hmac(void *handle, unsigned char *blk, int len,
  256. unsigned long seq, unsigned char *hmac)
  257. {
  258. unsigned char seqbuf[4];
  259. PUT_32BIT_MSB_FIRST(seqbuf, seq);
  260. hmacsha256_start(handle);
  261. hmacsha256_bytes(handle, seqbuf, 4);
  262. hmacsha256_bytes(handle, blk, len);
  263. hmacsha256_genresult(handle, hmac);
  264. }
  265. static void sha256_generate(void *handle, unsigned char *blk, int len,
  266. unsigned long seq)
  267. {
  268. sha256_do_hmac(handle, blk, len, seq, blk + len);
  269. }
  270. static int hmacsha256_verresult(void *handle, unsigned char const *hmac)
  271. {
  272. unsigned char correct[32];
  273. hmacsha256_genresult(handle, correct);
  274. return smemeq(correct, hmac, 32);
  275. }
  276. static int sha256_verify(void *handle, unsigned char *blk, int len,
  277. unsigned long seq)
  278. {
  279. unsigned char correct[32];
  280. sha256_do_hmac(handle, blk, len, seq, correct);
  281. return smemeq(correct, blk + len, 32);
  282. }
  283. const struct ssh_mac ssh_hmac_sha256 = {
  284. sha256_make_context, sha256_free_context, sha256_key,
  285. sha256_generate, sha256_verify,
  286. hmacsha256_start, hmacsha256_bytes,
  287. hmacsha256_genresult, hmacsha256_verresult,
  288. "hmac-sha2-256", "hmac-sha2-256-etm@openssh.com",
  289. 32, 32,
  290. "HMAC-SHA-256"
  291. };
  292. #ifdef TEST
  293. #include <stdio.h>
  294. #include <stdlib.h>
  295. #include <assert.h>
  296. int main(void) {
  297. unsigned char digest[32];
  298. int i, j, errors;
  299. struct {
  300. const char *teststring;
  301. unsigned char digest[32];
  302. } tests[] = {
  303. { "abc", {
  304. 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
  305. 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
  306. 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
  307. 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad,
  308. } },
  309. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", {
  310. 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
  311. 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
  312. 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
  313. 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1,
  314. } },
  315. };
  316. errors = 0;
  317. for (i = 0; i < sizeof(tests) / sizeof(*tests); i++) {
  318. SHA256_Simple(tests[i].teststring,
  319. strlen(tests[i].teststring), digest);
  320. for (j = 0; j < 32; j++) {
  321. if (digest[j] != tests[i].digest[j]) {
  322. fprintf(stderr,
  323. "\"%s\" digest byte %d should be 0x%02x, is 0x%02x\n",
  324. tests[i].teststring, j, tests[i].digest[j], digest[j]);
  325. errors++;
  326. }
  327. }
  328. }
  329. printf("%d errors\n", errors);
  330. return 0;
  331. }
  332. #endif