sha1.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. * FIPS-180-1 compliant SHA-1 implementation
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. /*
  24. * The SHA-1 standard was published by NIST in 1993.
  25. *
  26. * http://www.itl.nist.gov/fipspubs/fip180-1.htm
  27. */
  28. #if !defined(MBEDTLS_CONFIG_FILE)
  29. #include "mbedtls/config.h"
  30. #else
  31. #include MBEDTLS_CONFIG_FILE
  32. #endif
  33. #if defined(MBEDTLS_SHA1_C)
  34. #include "mbedtls/sha1.h"
  35. #include <string.h>
  36. #if defined(MBEDTLS_SELF_TEST)
  37. #if defined(MBEDTLS_PLATFORM_C)
  38. #include "mbedtls/platform.h"
  39. #else
  40. #include <stdio.h>
  41. #define mbedtls_printf printf
  42. #endif /* MBEDTLS_PLATFORM_C */
  43. #endif /* MBEDTLS_SELF_TEST */
  44. #if !defined(MBEDTLS_SHA1_ALT)
  45. /* Implementation that should never be optimized out by the compiler */
  46. static void mbedtls_zeroize( void *v, size_t n ) {
  47. volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
  48. }
  49. /*
  50. * 32-bit integer manipulation macros (big endian)
  51. */
  52. #ifndef GET_UINT32_BE
  53. #define GET_UINT32_BE(n,b,i) \
  54. { \
  55. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  56. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  57. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  58. | ( (uint32_t) (b)[(i) + 3] ); \
  59. }
  60. #endif
  61. #ifndef PUT_UINT32_BE
  62. #define PUT_UINT32_BE(n,b,i) \
  63. { \
  64. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  65. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  66. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  67. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  68. }
  69. #endif
  70. void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
  71. {
  72. memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
  73. }
  74. void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
  75. {
  76. if( ctx == NULL )
  77. return;
  78. mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
  79. }
  80. void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
  81. const mbedtls_sha1_context *src )
  82. {
  83. *dst = *src;
  84. }
  85. /*
  86. * SHA-1 context setup
  87. */
  88. void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
  89. {
  90. ctx->total[0] = 0;
  91. ctx->total[1] = 0;
  92. ctx->state[0] = 0x67452301;
  93. ctx->state[1] = 0xEFCDAB89;
  94. ctx->state[2] = 0x98BADCFE;
  95. ctx->state[3] = 0x10325476;
  96. ctx->state[4] = 0xC3D2E1F0;
  97. }
  98. #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
  99. void mbedtls_sha1_process( mbedtls_sha1_context *ctx, const unsigned char data[64] )
  100. {
  101. uint32_t temp, W[16], A, B, C, D, E;
  102. GET_UINT32_BE( W[ 0], data, 0 );
  103. GET_UINT32_BE( W[ 1], data, 4 );
  104. GET_UINT32_BE( W[ 2], data, 8 );
  105. GET_UINT32_BE( W[ 3], data, 12 );
  106. GET_UINT32_BE( W[ 4], data, 16 );
  107. GET_UINT32_BE( W[ 5], data, 20 );
  108. GET_UINT32_BE( W[ 6], data, 24 );
  109. GET_UINT32_BE( W[ 7], data, 28 );
  110. GET_UINT32_BE( W[ 8], data, 32 );
  111. GET_UINT32_BE( W[ 9], data, 36 );
  112. GET_UINT32_BE( W[10], data, 40 );
  113. GET_UINT32_BE( W[11], data, 44 );
  114. GET_UINT32_BE( W[12], data, 48 );
  115. GET_UINT32_BE( W[13], data, 52 );
  116. GET_UINT32_BE( W[14], data, 56 );
  117. GET_UINT32_BE( W[15], data, 60 );
  118. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  119. #define R(t) \
  120. ( \
  121. temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
  122. W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
  123. ( W[t & 0x0F] = S(temp,1) ) \
  124. )
  125. #define P(a,b,c,d,e,x) \
  126. { \
  127. e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
  128. }
  129. A = ctx->state[0];
  130. B = ctx->state[1];
  131. C = ctx->state[2];
  132. D = ctx->state[3];
  133. E = ctx->state[4];
  134. #define F(x,y,z) (z ^ (x & (y ^ z)))
  135. #define K 0x5A827999
  136. P( A, B, C, D, E, W[0] );
  137. P( E, A, B, C, D, W[1] );
  138. P( D, E, A, B, C, W[2] );
  139. P( C, D, E, A, B, W[3] );
  140. P( B, C, D, E, A, W[4] );
  141. P( A, B, C, D, E, W[5] );
  142. P( E, A, B, C, D, W[6] );
  143. P( D, E, A, B, C, W[7] );
  144. P( C, D, E, A, B, W[8] );
  145. P( B, C, D, E, A, W[9] );
  146. P( A, B, C, D, E, W[10] );
  147. P( E, A, B, C, D, W[11] );
  148. P( D, E, A, B, C, W[12] );
  149. P( C, D, E, A, B, W[13] );
  150. P( B, C, D, E, A, W[14] );
  151. P( A, B, C, D, E, W[15] );
  152. P( E, A, B, C, D, R(16) );
  153. P( D, E, A, B, C, R(17) );
  154. P( C, D, E, A, B, R(18) );
  155. P( B, C, D, E, A, R(19) );
  156. #undef K
  157. #undef F
  158. #define F(x,y,z) (x ^ y ^ z)
  159. #define K 0x6ED9EBA1
  160. P( A, B, C, D, E, R(20) );
  161. P( E, A, B, C, D, R(21) );
  162. P( D, E, A, B, C, R(22) );
  163. P( C, D, E, A, B, R(23) );
  164. P( B, C, D, E, A, R(24) );
  165. P( A, B, C, D, E, R(25) );
  166. P( E, A, B, C, D, R(26) );
  167. P( D, E, A, B, C, R(27) );
  168. P( C, D, E, A, B, R(28) );
  169. P( B, C, D, E, A, R(29) );
  170. P( A, B, C, D, E, R(30) );
  171. P( E, A, B, C, D, R(31) );
  172. P( D, E, A, B, C, R(32) );
  173. P( C, D, E, A, B, R(33) );
  174. P( B, C, D, E, A, R(34) );
  175. P( A, B, C, D, E, R(35) );
  176. P( E, A, B, C, D, R(36) );
  177. P( D, E, A, B, C, R(37) );
  178. P( C, D, E, A, B, R(38) );
  179. P( B, C, D, E, A, R(39) );
  180. #undef K
  181. #undef F
  182. #define F(x,y,z) ((x & y) | (z & (x | y)))
  183. #define K 0x8F1BBCDC
  184. P( A, B, C, D, E, R(40) );
  185. P( E, A, B, C, D, R(41) );
  186. P( D, E, A, B, C, R(42) );
  187. P( C, D, E, A, B, R(43) );
  188. P( B, C, D, E, A, R(44) );
  189. P( A, B, C, D, E, R(45) );
  190. P( E, A, B, C, D, R(46) );
  191. P( D, E, A, B, C, R(47) );
  192. P( C, D, E, A, B, R(48) );
  193. P( B, C, D, E, A, R(49) );
  194. P( A, B, C, D, E, R(50) );
  195. P( E, A, B, C, D, R(51) );
  196. P( D, E, A, B, C, R(52) );
  197. P( C, D, E, A, B, R(53) );
  198. P( B, C, D, E, A, R(54) );
  199. P( A, B, C, D, E, R(55) );
  200. P( E, A, B, C, D, R(56) );
  201. P( D, E, A, B, C, R(57) );
  202. P( C, D, E, A, B, R(58) );
  203. P( B, C, D, E, A, R(59) );
  204. #undef K
  205. #undef F
  206. #define F(x,y,z) (x ^ y ^ z)
  207. #define K 0xCA62C1D6
  208. P( A, B, C, D, E, R(60) );
  209. P( E, A, B, C, D, R(61) );
  210. P( D, E, A, B, C, R(62) );
  211. P( C, D, E, A, B, R(63) );
  212. P( B, C, D, E, A, R(64) );
  213. P( A, B, C, D, E, R(65) );
  214. P( E, A, B, C, D, R(66) );
  215. P( D, E, A, B, C, R(67) );
  216. P( C, D, E, A, B, R(68) );
  217. P( B, C, D, E, A, R(69) );
  218. P( A, B, C, D, E, R(70) );
  219. P( E, A, B, C, D, R(71) );
  220. P( D, E, A, B, C, R(72) );
  221. P( C, D, E, A, B, R(73) );
  222. P( B, C, D, E, A, R(74) );
  223. P( A, B, C, D, E, R(75) );
  224. P( E, A, B, C, D, R(76) );
  225. P( D, E, A, B, C, R(77) );
  226. P( C, D, E, A, B, R(78) );
  227. P( B, C, D, E, A, R(79) );
  228. #undef K
  229. #undef F
  230. ctx->state[0] += A;
  231. ctx->state[1] += B;
  232. ctx->state[2] += C;
  233. ctx->state[3] += D;
  234. ctx->state[4] += E;
  235. }
  236. #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
  237. /*
  238. * SHA-1 process buffer
  239. */
  240. void mbedtls_sha1_update( mbedtls_sha1_context *ctx, const unsigned char *input, size_t ilen )
  241. {
  242. size_t fill;
  243. uint32_t left;
  244. if( ilen == 0 )
  245. return;
  246. left = ctx->total[0] & 0x3F;
  247. fill = 64 - left;
  248. ctx->total[0] += (uint32_t) ilen;
  249. ctx->total[0] &= 0xFFFFFFFF;
  250. if( ctx->total[0] < (uint32_t) ilen )
  251. ctx->total[1]++;
  252. if( left && ilen >= fill )
  253. {
  254. memcpy( (void *) (ctx->buffer + left), input, fill );
  255. mbedtls_sha1_process( ctx, ctx->buffer );
  256. input += fill;
  257. ilen -= fill;
  258. left = 0;
  259. }
  260. while( ilen >= 64 )
  261. {
  262. mbedtls_sha1_process( ctx, input );
  263. input += 64;
  264. ilen -= 64;
  265. }
  266. if( ilen > 0 )
  267. memcpy( (void *) (ctx->buffer + left), input, ilen );
  268. }
  269. static const unsigned char sha1_padding[64] =
  270. {
  271. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  272. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  273. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  274. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  275. };
  276. /*
  277. * SHA-1 final digest
  278. */
  279. void mbedtls_sha1_finish( mbedtls_sha1_context *ctx, unsigned char output[20] )
  280. {
  281. uint32_t last, padn;
  282. uint32_t high, low;
  283. unsigned char msglen[8];
  284. high = ( ctx->total[0] >> 29 )
  285. | ( ctx->total[1] << 3 );
  286. low = ( ctx->total[0] << 3 );
  287. PUT_UINT32_BE( high, msglen, 0 );
  288. PUT_UINT32_BE( low, msglen, 4 );
  289. last = ctx->total[0] & 0x3F;
  290. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  291. mbedtls_sha1_update( ctx, sha1_padding, padn );
  292. mbedtls_sha1_update( ctx, msglen, 8 );
  293. PUT_UINT32_BE( ctx->state[0], output, 0 );
  294. PUT_UINT32_BE( ctx->state[1], output, 4 );
  295. PUT_UINT32_BE( ctx->state[2], output, 8 );
  296. PUT_UINT32_BE( ctx->state[3], output, 12 );
  297. PUT_UINT32_BE( ctx->state[4], output, 16 );
  298. }
  299. #endif /* !MBEDTLS_SHA1_ALT */
  300. /*
  301. * output = SHA-1( input buffer )
  302. */
  303. void mbedtls_sha1( const unsigned char *input, size_t ilen, unsigned char output[20] )
  304. {
  305. mbedtls_sha1_context ctx;
  306. mbedtls_sha1_init( &ctx );
  307. mbedtls_sha1_starts( &ctx );
  308. mbedtls_sha1_update( &ctx, input, ilen );
  309. mbedtls_sha1_finish( &ctx, output );
  310. mbedtls_sha1_free( &ctx );
  311. }
  312. #if defined(MBEDTLS_SELF_TEST)
  313. /*
  314. * FIPS-180-1 test vectors
  315. */
  316. static const unsigned char sha1_test_buf[3][57] =
  317. {
  318. { "abc" },
  319. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
  320. { "" }
  321. };
  322. static const int sha1_test_buflen[3] =
  323. {
  324. 3, 56, 1000
  325. };
  326. static const unsigned char sha1_test_sum[3][20] =
  327. {
  328. { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
  329. 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
  330. { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
  331. 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
  332. { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
  333. 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
  334. };
  335. /*
  336. * Checkup routine
  337. */
  338. int mbedtls_sha1_self_test( int verbose )
  339. {
  340. int i, j, buflen, ret = 0;
  341. unsigned char buf[1024];
  342. unsigned char sha1sum[20];
  343. mbedtls_sha1_context ctx;
  344. mbedtls_sha1_init( &ctx );
  345. /*
  346. * SHA-1
  347. */
  348. for( i = 0; i < 3; i++ )
  349. {
  350. if( verbose != 0 )
  351. mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
  352. mbedtls_sha1_starts( &ctx );
  353. if( i == 2 )
  354. {
  355. memset( buf, 'a', buflen = 1000 );
  356. for( j = 0; j < 1000; j++ )
  357. mbedtls_sha1_update( &ctx, buf, buflen );
  358. }
  359. else
  360. mbedtls_sha1_update( &ctx, sha1_test_buf[i],
  361. sha1_test_buflen[i] );
  362. mbedtls_sha1_finish( &ctx, sha1sum );
  363. if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
  364. {
  365. if( verbose != 0 )
  366. mbedtls_printf( "failed\n" );
  367. ret = 1;
  368. goto exit;
  369. }
  370. if( verbose != 0 )
  371. mbedtls_printf( "passed\n" );
  372. }
  373. if( verbose != 0 )
  374. mbedtls_printf( "\n" );
  375. exit:
  376. mbedtls_sha1_free( &ctx );
  377. return( ret );
  378. }
  379. #endif /* MBEDTLS_SELF_TEST */
  380. #endif /* MBEDTLS_SHA1_C */