tiny_sha2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * FIPS-180-2 compliant SHA-256 implementation
  3. *
  4. * Based on TropicSSL: Copyright (C) 2017 Shanghai Real-Thread Technology Co., Ltd
  5. *
  6. * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
  7. *
  8. * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. *
  16. * * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. * * Redistributions in binary form must reproduce the above copyright
  19. * notice, this list of conditions and the following disclaimer in the
  20. * documentation and/or other materials provided with the distribution.
  21. * * Neither the names of PolarSSL or XySSL nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  31. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * The SHA-256 Secure Hash Standard was published by NIST in 2002.
  39. *
  40. * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
  41. */
  42. #include "tinycrypt_config.h"
  43. #if defined(TINY_CRYPT_SHA256)
  44. #include "tinycrypt.h"
  45. #include <string.h>
  46. #include <stdio.h>
  47. /*
  48. * 32-bit integer manipulation macros (big endian)
  49. */
  50. #ifndef GET_ULONG_BE
  51. #define GET_ULONG_BE(n,b,i) \
  52. { \
  53. (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
  54. | ( (unsigned long) (b)[(i) + 1] << 16 ) \
  55. | ( (unsigned long) (b)[(i) + 2] << 8 ) \
  56. | ( (unsigned long) (b)[(i) + 3] ); \
  57. }
  58. #endif
  59. #ifndef PUT_ULONG_BE
  60. #define PUT_ULONG_BE(n,b,i) \
  61. { \
  62. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  63. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  64. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  65. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  66. }
  67. #endif
  68. /*
  69. * SHA-256 context setup
  70. */
  71. void tiny_sha2_starts(tiny_sha2_context * ctx, int is224)
  72. {
  73. ctx->total[0] = 0;
  74. ctx->total[1] = 0;
  75. if (is224 == 0) {
  76. /* SHA-256 */
  77. ctx->state[0] = 0x6A09E667;
  78. ctx->state[1] = 0xBB67AE85;
  79. ctx->state[2] = 0x3C6EF372;
  80. ctx->state[3] = 0xA54FF53A;
  81. ctx->state[4] = 0x510E527F;
  82. ctx->state[5] = 0x9B05688C;
  83. ctx->state[6] = 0x1F83D9AB;
  84. ctx->state[7] = 0x5BE0CD19;
  85. } else {
  86. /* SHA-224 */
  87. ctx->state[0] = 0xC1059ED8;
  88. ctx->state[1] = 0x367CD507;
  89. ctx->state[2] = 0x3070DD17;
  90. ctx->state[3] = 0xF70E5939;
  91. ctx->state[4] = 0xFFC00B31;
  92. ctx->state[5] = 0x68581511;
  93. ctx->state[6] = 0x64F98FA7;
  94. ctx->state[7] = 0xBEFA4FA4;
  95. }
  96. ctx->is224 = is224;
  97. }
  98. static void sha2_process(tiny_sha2_context * ctx, unsigned char data[64])
  99. {
  100. unsigned long temp1, temp2, W[64];
  101. unsigned long A, B, C, D, E, F, G, H;
  102. GET_ULONG_BE(W[0], data, 0);
  103. GET_ULONG_BE(W[1], data, 4);
  104. GET_ULONG_BE(W[2], data, 8);
  105. GET_ULONG_BE(W[3], data, 12);
  106. GET_ULONG_BE(W[4], data, 16);
  107. GET_ULONG_BE(W[5], data, 20);
  108. GET_ULONG_BE(W[6], data, 24);
  109. GET_ULONG_BE(W[7], data, 28);
  110. GET_ULONG_BE(W[8], data, 32);
  111. GET_ULONG_BE(W[9], data, 36);
  112. GET_ULONG_BE(W[10], data, 40);
  113. GET_ULONG_BE(W[11], data, 44);
  114. GET_ULONG_BE(W[12], data, 48);
  115. GET_ULONG_BE(W[13], data, 52);
  116. GET_ULONG_BE(W[14], data, 56);
  117. GET_ULONG_BE(W[15], data, 60);
  118. #define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
  119. #define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
  120. #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
  121. #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
  122. #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
  123. #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
  124. #define F0(x,y,z) ((x & y) | (z & (x | y)))
  125. #define F1(x,y,z) (z ^ (x & (y ^ z)))
  126. #define R(t) \
  127. ( \
  128. W[t] = S1(W[t - 2]) + W[t - 7] + \
  129. S0(W[t - 15]) + W[t - 16] \
  130. )
  131. #define P(a,b,c,d,e,f,g,h,x,K) \
  132. { \
  133. temp1 = h + S3(e) + F1(e,f,g) + K + x; \
  134. temp2 = S2(a) + F0(a,b,c); \
  135. d += temp1; h = temp1 + temp2; \
  136. }
  137. A = ctx->state[0];
  138. B = ctx->state[1];
  139. C = ctx->state[2];
  140. D = ctx->state[3];
  141. E = ctx->state[4];
  142. F = ctx->state[5];
  143. G = ctx->state[6];
  144. H = ctx->state[7];
  145. P(A, B, C, D, E, F, G, H, W[0], 0x428A2F98);
  146. P(H, A, B, C, D, E, F, G, W[1], 0x71374491);
  147. P(G, H, A, B, C, D, E, F, W[2], 0xB5C0FBCF);
  148. P(F, G, H, A, B, C, D, E, W[3], 0xE9B5DBA5);
  149. P(E, F, G, H, A, B, C, D, W[4], 0x3956C25B);
  150. P(D, E, F, G, H, A, B, C, W[5], 0x59F111F1);
  151. P(C, D, E, F, G, H, A, B, W[6], 0x923F82A4);
  152. P(B, C, D, E, F, G, H, A, W[7], 0xAB1C5ED5);
  153. P(A, B, C, D, E, F, G, H, W[8], 0xD807AA98);
  154. P(H, A, B, C, D, E, F, G, W[9], 0x12835B01);
  155. P(G, H, A, B, C, D, E, F, W[10], 0x243185BE);
  156. P(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3);
  157. P(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74);
  158. P(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE);
  159. P(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7);
  160. P(B, C, D, E, F, G, H, A, W[15], 0xC19BF174);
  161. P(A, B, C, D, E, F, G, H, R(16), 0xE49B69C1);
  162. P(H, A, B, C, D, E, F, G, R(17), 0xEFBE4786);
  163. P(G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6);
  164. P(F, G, H, A, B, C, D, E, R(19), 0x240CA1CC);
  165. P(E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F);
  166. P(D, E, F, G, H, A, B, C, R(21), 0x4A7484AA);
  167. P(C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC);
  168. P(B, C, D, E, F, G, H, A, R(23), 0x76F988DA);
  169. P(A, B, C, D, E, F, G, H, R(24), 0x983E5152);
  170. P(H, A, B, C, D, E, F, G, R(25), 0xA831C66D);
  171. P(G, H, A, B, C, D, E, F, R(26), 0xB00327C8);
  172. P(F, G, H, A, B, C, D, E, R(27), 0xBF597FC7);
  173. P(E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3);
  174. P(D, E, F, G, H, A, B, C, R(29), 0xD5A79147);
  175. P(C, D, E, F, G, H, A, B, R(30), 0x06CA6351);
  176. P(B, C, D, E, F, G, H, A, R(31), 0x14292967);
  177. P(A, B, C, D, E, F, G, H, R(32), 0x27B70A85);
  178. P(H, A, B, C, D, E, F, G, R(33), 0x2E1B2138);
  179. P(G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC);
  180. P(F, G, H, A, B, C, D, E, R(35), 0x53380D13);
  181. P(E, F, G, H, A, B, C, D, R(36), 0x650A7354);
  182. P(D, E, F, G, H, A, B, C, R(37), 0x766A0ABB);
  183. P(C, D, E, F, G, H, A, B, R(38), 0x81C2C92E);
  184. P(B, C, D, E, F, G, H, A, R(39), 0x92722C85);
  185. P(A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1);
  186. P(H, A, B, C, D, E, F, G, R(41), 0xA81A664B);
  187. P(G, H, A, B, C, D, E, F, R(42), 0xC24B8B70);
  188. P(F, G, H, A, B, C, D, E, R(43), 0xC76C51A3);
  189. P(E, F, G, H, A, B, C, D, R(44), 0xD192E819);
  190. P(D, E, F, G, H, A, B, C, R(45), 0xD6990624);
  191. P(C, D, E, F, G, H, A, B, R(46), 0xF40E3585);
  192. P(B, C, D, E, F, G, H, A, R(47), 0x106AA070);
  193. P(A, B, C, D, E, F, G, H, R(48), 0x19A4C116);
  194. P(H, A, B, C, D, E, F, G, R(49), 0x1E376C08);
  195. P(G, H, A, B, C, D, E, F, R(50), 0x2748774C);
  196. P(F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5);
  197. P(E, F, G, H, A, B, C, D, R(52), 0x391C0CB3);
  198. P(D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A);
  199. P(C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F);
  200. P(B, C, D, E, F, G, H, A, R(55), 0x682E6FF3);
  201. P(A, B, C, D, E, F, G, H, R(56), 0x748F82EE);
  202. P(H, A, B, C, D, E, F, G, R(57), 0x78A5636F);
  203. P(G, H, A, B, C, D, E, F, R(58), 0x84C87814);
  204. P(F, G, H, A, B, C, D, E, R(59), 0x8CC70208);
  205. P(E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA);
  206. P(D, E, F, G, H, A, B, C, R(61), 0xA4506CEB);
  207. P(C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7);
  208. P(B, C, D, E, F, G, H, A, R(63), 0xC67178F2);
  209. ctx->state[0] += A;
  210. ctx->state[1] += B;
  211. ctx->state[2] += C;
  212. ctx->state[3] += D;
  213. ctx->state[4] += E;
  214. ctx->state[5] += F;
  215. ctx->state[6] += G;
  216. ctx->state[7] += H;
  217. }
  218. /*
  219. * SHA-256 process buffer
  220. */
  221. void tiny_sha2_update(tiny_sha2_context * ctx, unsigned char *input, int ilen)
  222. {
  223. int fill;
  224. unsigned long left;
  225. if (ilen <= 0)
  226. return;
  227. left = ctx->total[0] & 0x3F;
  228. fill = 64 - left;
  229. ctx->total[0] += ilen;
  230. ctx->total[0] &= 0xFFFFFFFF;
  231. if (ctx->total[0] < (unsigned long)ilen)
  232. ctx->total[1]++;
  233. if (left && ilen >= fill) {
  234. memcpy((void *)(ctx->buffer + left), (void *)input, fill);
  235. sha2_process(ctx, ctx->buffer);
  236. input += fill;
  237. ilen -= fill;
  238. left = 0;
  239. }
  240. while (ilen >= 64) {
  241. sha2_process(ctx, input);
  242. input += 64;
  243. ilen -= 64;
  244. }
  245. if (ilen > 0) {
  246. memcpy((void *)(ctx->buffer + left), (void *)input, ilen);
  247. }
  248. }
  249. static const unsigned char sha2_padding[64] = {
  250. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  251. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  252. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  253. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  254. };
  255. /*
  256. * SHA-256 final digest
  257. */
  258. void tiny_sha2_finish(tiny_sha2_context * ctx, unsigned char output[32])
  259. {
  260. unsigned long last, padn;
  261. unsigned long high, low;
  262. unsigned char msglen[8];
  263. high = (ctx->total[0] >> 29)
  264. | (ctx->total[1] << 3);
  265. low = (ctx->total[0] << 3);
  266. PUT_ULONG_BE(high, msglen, 0);
  267. PUT_ULONG_BE(low, msglen, 4);
  268. last = ctx->total[0] & 0x3F;
  269. padn = (last < 56) ? (56 - last) : (120 - last);
  270. tiny_sha2_update(ctx, (unsigned char *)sha2_padding, padn);
  271. tiny_sha2_update(ctx, msglen, 8);
  272. PUT_ULONG_BE(ctx->state[0], output, 0);
  273. PUT_ULONG_BE(ctx->state[1], output, 4);
  274. PUT_ULONG_BE(ctx->state[2], output, 8);
  275. PUT_ULONG_BE(ctx->state[3], output, 12);
  276. PUT_ULONG_BE(ctx->state[4], output, 16);
  277. PUT_ULONG_BE(ctx->state[5], output, 20);
  278. PUT_ULONG_BE(ctx->state[6], output, 24);
  279. if (ctx->is224 == 0)
  280. PUT_ULONG_BE(ctx->state[7], output, 28);
  281. }
  282. /*
  283. * output = SHA-256( input buffer )
  284. */
  285. void tiny_sha2(unsigned char *input, int ilen, unsigned char output[32], int is224)
  286. {
  287. tiny_sha2_context ctx;
  288. tiny_sha2_starts(&ctx, is224);
  289. tiny_sha2_update(&ctx, input, ilen);
  290. tiny_sha2_finish(&ctx, output);
  291. memset(&ctx, 0, sizeof(tiny_sha2_context));
  292. }
  293. /*
  294. * SHA-256 HMAC context setup
  295. */
  296. void tiny_sha2_hmac_starts(tiny_sha2_context * ctx, unsigned char *key, int keylen,
  297. int is224)
  298. {
  299. int i;
  300. unsigned char sum[32];
  301. if (keylen > 64) {
  302. tiny_sha2(key, keylen, sum, is224);
  303. keylen = (is224) ? 28 : 32;
  304. key = sum;
  305. }
  306. memset(ctx->ipad, 0x36, 64);
  307. memset(ctx->opad, 0x5C, 64);
  308. for (i = 0; i < keylen; i++) {
  309. ctx->ipad[i] = (unsigned char)(ctx->ipad[i] ^ key[i]);
  310. ctx->opad[i] = (unsigned char)(ctx->opad[i] ^ key[i]);
  311. }
  312. tiny_sha2_starts(ctx, is224);
  313. tiny_sha2_update(ctx, ctx->ipad, 64);
  314. memset(sum, 0, sizeof(sum));
  315. }
  316. /*
  317. * SHA-256 HMAC process buffer
  318. */
  319. void tiny_sha2_hmac_update(tiny_sha2_context * ctx, unsigned char *input, int ilen)
  320. {
  321. tiny_sha2_update(ctx, input, ilen);
  322. }
  323. /*
  324. * SHA-256 HMAC final digest
  325. */
  326. void tiny_sha2_hmac_finish(tiny_sha2_context * ctx, unsigned char output[32])
  327. {
  328. int is224, hlen;
  329. unsigned char tmpbuf[32];
  330. is224 = ctx->is224;
  331. hlen = (is224 == 0) ? 32 : 28;
  332. tiny_sha2_finish(ctx, tmpbuf);
  333. tiny_sha2_starts(ctx, is224);
  334. tiny_sha2_update(ctx, ctx->opad, 64);
  335. tiny_sha2_update(ctx, tmpbuf, hlen);
  336. tiny_sha2_finish(ctx, output);
  337. memset(tmpbuf, 0, sizeof(tmpbuf));
  338. }
  339. /*
  340. * output = HMAC-SHA-256( hmac key, input buffer )
  341. */
  342. void tiny_sha2_hmac(unsigned char *key, int keylen,
  343. unsigned char *input, int ilen,
  344. unsigned char output[32], int is224)
  345. {
  346. tiny_sha2_context ctx;
  347. tiny_sha2_hmac_starts(&ctx, key, keylen, is224);
  348. tiny_sha2_hmac_update(&ctx, input, ilen);
  349. tiny_sha2_hmac_finish(&ctx, output);
  350. memset(&ctx, 0, sizeof(tiny_sha2_context));
  351. }
  352. #endif