sha1.c 11 KB

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