sshsh256.c 11 KB

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