sha2.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * FIPS-180-2 compliant SHA-256 implementation
  3. *
  4. * Copyright (C) 2011, Con Kolivas
  5. * Copyright (C) 2006-2010, Brainspark B.V.
  6. *
  7. * This file is part of PolarSSL (http://www.polarssl.org)
  8. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  25. */
  26. /*
  27. * The SHA-256 Secure Hash Standard was published by NIST in 2002.
  28. *
  29. * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
  30. */
  31. #include "sha2.h"
  32. #include <string.h>
  33. #include <stdio.h>
  34. /*
  35. * 32-bit integer manipulation macros (big endian)
  36. */
  37. #ifndef GET_ULONG_BE
  38. #define GET_ULONG_BE(n,b,i) \
  39. { \
  40. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  41. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  42. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  43. | ( (uint32_t) (b)[(i) + 3] ); \
  44. }
  45. #endif
  46. #ifndef PUT_ULONG_BE
  47. #define PUT_ULONG_BE(n,b,i) \
  48. { \
  49. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  50. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  51. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  52. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  53. }
  54. #endif
  55. /*
  56. * SHA-256 context setup
  57. */
  58. void sha2_starts( sha2_context *ctx )
  59. {
  60. ctx->total[0] = 0;
  61. ctx->total[1] = 0;
  62. ctx->state[0] = 0x6A09E667;
  63. ctx->state[1] = 0xBB67AE85;
  64. ctx->state[2] = 0x3C6EF372;
  65. ctx->state[3] = 0xA54FF53A;
  66. ctx->state[4] = 0x510E527F;
  67. ctx->state[5] = 0x9B05688C;
  68. ctx->state[6] = 0x1F83D9AB;
  69. ctx->state[7] = 0x5BE0CD19;
  70. }
  71. static void sha2_process( sha2_context *ctx, const unsigned char data[64] )
  72. {
  73. uint32_t temp1, temp2, W[64];
  74. uint32_t A, B, C, D, E, F, G, H;
  75. GET_ULONG_BE( W[ 0], data, 0 );
  76. GET_ULONG_BE( W[ 1], data, 4 );
  77. GET_ULONG_BE( W[ 2], data, 8 );
  78. GET_ULONG_BE( W[ 3], data, 12 );
  79. GET_ULONG_BE( W[ 4], data, 16 );
  80. GET_ULONG_BE( W[ 5], data, 20 );
  81. GET_ULONG_BE( W[ 6], data, 24 );
  82. GET_ULONG_BE( W[ 7], data, 28 );
  83. GET_ULONG_BE( W[ 8], data, 32 );
  84. GET_ULONG_BE( W[ 9], data, 36 );
  85. GET_ULONG_BE( W[10], data, 40 );
  86. GET_ULONG_BE( W[11], data, 44 );
  87. GET_ULONG_BE( W[12], data, 48 );
  88. GET_ULONG_BE( W[13], data, 52 );
  89. GET_ULONG_BE( W[14], data, 56 );
  90. GET_ULONG_BE( W[15], data, 60 );
  91. #define SHR(x,n) ((x & 0xFFFFFFFF) >> n)
  92. #define ROTR(x,n) (SHR(x,n) | (x << (32 - n)))
  93. #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
  94. #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
  95. #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
  96. #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
  97. #define F0(x,y,z) ((x & y) | (z & (x | y)))
  98. #define F1(x,y,z) (z ^ (x & (y ^ z)))
  99. #define R(t) \
  100. ( \
  101. W[t] = S1(W[t - 2]) + W[t - 7] + \
  102. S0(W[t - 15]) + W[t - 16] \
  103. )
  104. #define P(a,b,c,d,e,f,g,h,x,K) \
  105. { \
  106. temp1 = h + S3(e) + F1(e,f,g) + K + x; \
  107. temp2 = S2(a) + F0(a,b,c); \
  108. d += temp1; h = temp1 + temp2; \
  109. }
  110. A = ctx->state[0];
  111. B = ctx->state[1];
  112. C = ctx->state[2];
  113. D = ctx->state[3];
  114. E = ctx->state[4];
  115. F = ctx->state[5];
  116. G = ctx->state[6];
  117. H = ctx->state[7];
  118. P( A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98 );
  119. P( H, A, B, C, D, E, F, G, W[ 1], 0x71374491 );
  120. P( G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF );
  121. P( F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5 );
  122. P( E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B );
  123. P( D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1 );
  124. P( C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4 );
  125. P( B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5 );
  126. P( A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98 );
  127. P( H, A, B, C, D, E, F, G, W[ 9], 0x12835B01 );
  128. P( G, H, A, B, C, D, E, F, W[10], 0x243185BE );
  129. P( F, G, H, A, B, C, D, E, W[11], 0x550C7DC3 );
  130. P( E, F, G, H, A, B, C, D, W[12], 0x72BE5D74 );
  131. P( D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE );
  132. P( C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7 );
  133. P( B, C, D, E, F, G, H, A, W[15], 0xC19BF174 );
  134. P( A, B, C, D, E, F, G, H, R(16), 0xE49B69C1 );
  135. P( H, A, B, C, D, E, F, G, R(17), 0xEFBE4786 );
  136. P( G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6 );
  137. P( F, G, H, A, B, C, D, E, R(19), 0x240CA1CC );
  138. P( E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F );
  139. P( D, E, F, G, H, A, B, C, R(21), 0x4A7484AA );
  140. P( C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC );
  141. P( B, C, D, E, F, G, H, A, R(23), 0x76F988DA );
  142. P( A, B, C, D, E, F, G, H, R(24), 0x983E5152 );
  143. P( H, A, B, C, D, E, F, G, R(25), 0xA831C66D );
  144. P( G, H, A, B, C, D, E, F, R(26), 0xB00327C8 );
  145. P( F, G, H, A, B, C, D, E, R(27), 0xBF597FC7 );
  146. P( E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3 );
  147. P( D, E, F, G, H, A, B, C, R(29), 0xD5A79147 );
  148. P( C, D, E, F, G, H, A, B, R(30), 0x06CA6351 );
  149. P( B, C, D, E, F, G, H, A, R(31), 0x14292967 );
  150. P( A, B, C, D, E, F, G, H, R(32), 0x27B70A85 );
  151. P( H, A, B, C, D, E, F, G, R(33), 0x2E1B2138 );
  152. P( G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC );
  153. P( F, G, H, A, B, C, D, E, R(35), 0x53380D13 );
  154. P( E, F, G, H, A, B, C, D, R(36), 0x650A7354 );
  155. P( D, E, F, G, H, A, B, C, R(37), 0x766A0ABB );
  156. P( C, D, E, F, G, H, A, B, R(38), 0x81C2C92E );
  157. P( B, C, D, E, F, G, H, A, R(39), 0x92722C85 );
  158. P( A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1 );
  159. P( H, A, B, C, D, E, F, G, R(41), 0xA81A664B );
  160. P( G, H, A, B, C, D, E, F, R(42), 0xC24B8B70 );
  161. P( F, G, H, A, B, C, D, E, R(43), 0xC76C51A3 );
  162. P( E, F, G, H, A, B, C, D, R(44), 0xD192E819 );
  163. P( D, E, F, G, H, A, B, C, R(45), 0xD6990624 );
  164. P( C, D, E, F, G, H, A, B, R(46), 0xF40E3585 );
  165. P( B, C, D, E, F, G, H, A, R(47), 0x106AA070 );
  166. P( A, B, C, D, E, F, G, H, R(48), 0x19A4C116 );
  167. P( H, A, B, C, D, E, F, G, R(49), 0x1E376C08 );
  168. P( G, H, A, B, C, D, E, F, R(50), 0x2748774C );
  169. P( F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5 );
  170. P( E, F, G, H, A, B, C, D, R(52), 0x391C0CB3 );
  171. P( D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A );
  172. P( C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F );
  173. P( B, C, D, E, F, G, H, A, R(55), 0x682E6FF3 );
  174. P( A, B, C, D, E, F, G, H, R(56), 0x748F82EE );
  175. P( H, A, B, C, D, E, F, G, R(57), 0x78A5636F );
  176. P( G, H, A, B, C, D, E, F, R(58), 0x84C87814 );
  177. P( F, G, H, A, B, C, D, E, R(59), 0x8CC70208 );
  178. P( E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA );
  179. P( D, E, F, G, H, A, B, C, R(61), 0xA4506CEB );
  180. P( C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7 );
  181. P( B, C, D, E, F, G, H, A, R(63), 0xC67178F2 );
  182. ctx->state[0] += A;
  183. ctx->state[1] += B;
  184. ctx->state[2] += C;
  185. ctx->state[3] += D;
  186. ctx->state[4] += E;
  187. ctx->state[5] += F;
  188. ctx->state[6] += G;
  189. ctx->state[7] += H;
  190. }
  191. /*
  192. * SHA-256 process buffer
  193. */
  194. void sha2_update( sha2_context *ctx, const unsigned char *input, int ilen )
  195. {
  196. int fill;
  197. uint32_t left;
  198. if( ilen <= 0 )
  199. return;
  200. left = ctx->total[0] & 0x3F;
  201. fill = 64 - left;
  202. ctx->total[0] += ilen;
  203. ctx->total[0] &= 0xFFFFFFFF;
  204. if( ctx->total[0] < (uint32_t) ilen )
  205. ctx->total[1]++;
  206. if( left && ilen >= fill )
  207. {
  208. memcpy( (void *) (ctx->buffer + left),
  209. (void *) input, fill );
  210. sha2_process( ctx, ctx->buffer );
  211. input += fill;
  212. ilen -= fill;
  213. left = 0;
  214. }
  215. while( ilen >= 64 )
  216. {
  217. sha2_process( ctx, input );
  218. input += 64;
  219. ilen -= 64;
  220. }
  221. if( ilen > 0 )
  222. {
  223. memcpy( (void *) (ctx->buffer + left),
  224. (void *) input, ilen );
  225. }
  226. }
  227. static const unsigned char sha2_padding[64] =
  228. {
  229. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  230. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  231. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  232. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  233. };
  234. /*
  235. * SHA-256 final digest
  236. */
  237. void sha2_finish( sha2_context *ctx, unsigned char output[32] )
  238. {
  239. uint32_t last, padn;
  240. uint32_t high, low;
  241. unsigned char msglen[8];
  242. high = ( ctx->total[0] >> 29 )
  243. | ( ctx->total[1] << 3 );
  244. low = ( ctx->total[0] << 3 );
  245. PUT_ULONG_BE( high, msglen, 0 );
  246. PUT_ULONG_BE( low, msglen, 4 );
  247. last = ctx->total[0] & 0x3F;
  248. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  249. sha2_update( ctx, (unsigned char *) sha2_padding, padn );
  250. sha2_update( ctx, msglen, 8 );
  251. PUT_ULONG_BE( ctx->state[0], output, 0 );
  252. PUT_ULONG_BE( ctx->state[1], output, 4 );
  253. PUT_ULONG_BE( ctx->state[2], output, 8 );
  254. PUT_ULONG_BE( ctx->state[3], output, 12 );
  255. PUT_ULONG_BE( ctx->state[4], output, 16 );
  256. PUT_ULONG_BE( ctx->state[5], output, 20 );
  257. PUT_ULONG_BE( ctx->state[6], output, 24 );
  258. PUT_ULONG_BE( ctx->state[7], output, 28 );
  259. }
  260. /*
  261. * output = SHA-256( input buffer )
  262. */
  263. void sha2( const unsigned char *input, int ilen,
  264. unsigned char output[32] )
  265. {
  266. sha2_context ctx;
  267. sha2_starts( &ctx );
  268. sha2_update( &ctx, input, ilen );
  269. sha2_finish( &ctx, output );
  270. memset( &ctx, 0, sizeof( sha2_context ) );
  271. }