sha1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 "mbedtls/platform_util.h"
  34. #include <string.h>
  35. #if defined(MBEDTLS_SELF_TEST)
  36. #if defined(MBEDTLS_PLATFORM_C)
  37. #include "mbedtls/platform.h"
  38. #else
  39. #include <stdio.h>
  40. #define mbedtls_printf printf
  41. #endif /* MBEDTLS_PLATFORM_C */
  42. #endif /* MBEDTLS_SELF_TEST */
  43. #if !defined(MBEDTLS_SHA1_ALT)
  44. /*
  45. * 32-bit integer manipulation macros (big endian)
  46. */
  47. #ifndef GET_UINT32_BE
  48. #define GET_UINT32_BE(n,b,i) \
  49. { \
  50. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  51. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  52. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  53. | ( (uint32_t) (b)[(i) + 3] ); \
  54. }
  55. #endif
  56. #ifndef PUT_UINT32_BE
  57. #define PUT_UINT32_BE(n,b,i) \
  58. { \
  59. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  60. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  61. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  62. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  63. }
  64. #endif
  65. void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
  66. {
  67. memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
  68. }
  69. void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
  70. {
  71. if( ctx == NULL )
  72. return;
  73. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
  74. }
  75. void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
  76. const mbedtls_sha1_context *src )
  77. {
  78. *dst = *src;
  79. }
  80. /*
  81. * SHA-1 context setup
  82. */
  83. int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
  84. {
  85. ctx->total[0] = 0;
  86. ctx->total[1] = 0;
  87. ctx->state[0] = 0x67452301;
  88. ctx->state[1] = 0xEFCDAB89;
  89. ctx->state[2] = 0x98BADCFE;
  90. ctx->state[3] = 0x10325476;
  91. ctx->state[4] = 0xC3D2E1F0;
  92. return( 0 );
  93. }
  94. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  95. void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
  96. {
  97. mbedtls_sha1_starts_ret( ctx );
  98. }
  99. #endif
  100. #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
  101. int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
  102. const unsigned char data[64] )
  103. {
  104. uint32_t temp, W[16], A, B, C, D, E;
  105. GET_UINT32_BE( W[ 0], data, 0 );
  106. GET_UINT32_BE( W[ 1], data, 4 );
  107. GET_UINT32_BE( W[ 2], data, 8 );
  108. GET_UINT32_BE( W[ 3], data, 12 );
  109. GET_UINT32_BE( W[ 4], data, 16 );
  110. GET_UINT32_BE( W[ 5], data, 20 );
  111. GET_UINT32_BE( W[ 6], data, 24 );
  112. GET_UINT32_BE( W[ 7], data, 28 );
  113. GET_UINT32_BE( W[ 8], data, 32 );
  114. GET_UINT32_BE( W[ 9], data, 36 );
  115. GET_UINT32_BE( W[10], data, 40 );
  116. GET_UINT32_BE( W[11], data, 44 );
  117. GET_UINT32_BE( W[12], data, 48 );
  118. GET_UINT32_BE( W[13], data, 52 );
  119. GET_UINT32_BE( W[14], data, 56 );
  120. GET_UINT32_BE( W[15], data, 60 );
  121. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  122. #define R(t) \
  123. ( \
  124. temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
  125. W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
  126. ( W[t & 0x0F] = S(temp,1) ) \
  127. )
  128. #define P(a,b,c,d,e,x) \
  129. { \
  130. e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
  131. }
  132. A = ctx->state[0];
  133. B = ctx->state[1];
  134. C = ctx->state[2];
  135. D = ctx->state[3];
  136. E = ctx->state[4];
  137. #define F(x,y,z) (z ^ (x & (y ^ z)))
  138. #define K 0x5A827999
  139. P( A, B, C, D, E, W[0] );
  140. P( E, A, B, C, D, W[1] );
  141. P( D, E, A, B, C, W[2] );
  142. P( C, D, E, A, B, W[3] );
  143. P( B, C, D, E, A, W[4] );
  144. P( A, B, C, D, E, W[5] );
  145. P( E, A, B, C, D, W[6] );
  146. P( D, E, A, B, C, W[7] );
  147. P( C, D, E, A, B, W[8] );
  148. P( B, C, D, E, A, W[9] );
  149. P( A, B, C, D, E, W[10] );
  150. P( E, A, B, C, D, W[11] );
  151. P( D, E, A, B, C, W[12] );
  152. P( C, D, E, A, B, W[13] );
  153. P( B, C, D, E, A, W[14] );
  154. P( A, B, C, D, E, W[15] );
  155. P( E, A, B, C, D, R(16) );
  156. P( D, E, A, B, C, R(17) );
  157. P( C, D, E, A, B, R(18) );
  158. P( B, C, D, E, A, R(19) );
  159. #undef K
  160. #undef F
  161. #define F(x,y,z) (x ^ y ^ z)
  162. #define K 0x6ED9EBA1
  163. P( A, B, C, D, E, R(20) );
  164. P( E, A, B, C, D, R(21) );
  165. P( D, E, A, B, C, R(22) );
  166. P( C, D, E, A, B, R(23) );
  167. P( B, C, D, E, A, R(24) );
  168. P( A, B, C, D, E, R(25) );
  169. P( E, A, B, C, D, R(26) );
  170. P( D, E, A, B, C, R(27) );
  171. P( C, D, E, A, B, R(28) );
  172. P( B, C, D, E, A, R(29) );
  173. P( A, B, C, D, E, R(30) );
  174. P( E, A, B, C, D, R(31) );
  175. P( D, E, A, B, C, R(32) );
  176. P( C, D, E, A, B, R(33) );
  177. P( B, C, D, E, A, R(34) );
  178. P( A, B, C, D, E, R(35) );
  179. P( E, A, B, C, D, R(36) );
  180. P( D, E, A, B, C, R(37) );
  181. P( C, D, E, A, B, R(38) );
  182. P( B, C, D, E, A, R(39) );
  183. #undef K
  184. #undef F
  185. #define F(x,y,z) ((x & y) | (z & (x | y)))
  186. #define K 0x8F1BBCDC
  187. P( A, B, C, D, E, R(40) );
  188. P( E, A, B, C, D, R(41) );
  189. P( D, E, A, B, C, R(42) );
  190. P( C, D, E, A, B, R(43) );
  191. P( B, C, D, E, A, R(44) );
  192. P( A, B, C, D, E, R(45) );
  193. P( E, A, B, C, D, R(46) );
  194. P( D, E, A, B, C, R(47) );
  195. P( C, D, E, A, B, R(48) );
  196. P( B, C, D, E, A, R(49) );
  197. P( A, B, C, D, E, R(50) );
  198. P( E, A, B, C, D, R(51) );
  199. P( D, E, A, B, C, R(52) );
  200. P( C, D, E, A, B, R(53) );
  201. P( B, C, D, E, A, R(54) );
  202. P( A, B, C, D, E, R(55) );
  203. P( E, A, B, C, D, R(56) );
  204. P( D, E, A, B, C, R(57) );
  205. P( C, D, E, A, B, R(58) );
  206. P( B, C, D, E, A, R(59) );
  207. #undef K
  208. #undef F
  209. #define F(x,y,z) (x ^ y ^ z)
  210. #define K 0xCA62C1D6
  211. P( A, B, C, D, E, R(60) );
  212. P( E, A, B, C, D, R(61) );
  213. P( D, E, A, B, C, R(62) );
  214. P( C, D, E, A, B, R(63) );
  215. P( B, C, D, E, A, R(64) );
  216. P( A, B, C, D, E, R(65) );
  217. P( E, A, B, C, D, R(66) );
  218. P( D, E, A, B, C, R(67) );
  219. P( C, D, E, A, B, R(68) );
  220. P( B, C, D, E, A, R(69) );
  221. P( A, B, C, D, E, R(70) );
  222. P( E, A, B, C, D, R(71) );
  223. P( D, E, A, B, C, R(72) );
  224. P( C, D, E, A, B, R(73) );
  225. P( B, C, D, E, A, R(74) );
  226. P( A, B, C, D, E, R(75) );
  227. P( E, A, B, C, D, R(76) );
  228. P( D, E, A, B, C, R(77) );
  229. P( C, D, E, A, B, R(78) );
  230. P( B, C, D, E, A, R(79) );
  231. #undef K
  232. #undef F
  233. ctx->state[0] += A;
  234. ctx->state[1] += B;
  235. ctx->state[2] += C;
  236. ctx->state[3] += D;
  237. ctx->state[4] += E;
  238. return( 0 );
  239. }
  240. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  241. void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
  242. const unsigned char data[64] )
  243. {
  244. mbedtls_internal_sha1_process( ctx, data );
  245. }
  246. #endif
  247. #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
  248. /*
  249. * SHA-1 process buffer
  250. */
  251. int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
  252. const unsigned char *input,
  253. size_t ilen )
  254. {
  255. int ret;
  256. size_t fill;
  257. uint32_t left;
  258. if( ilen == 0 )
  259. return( 0 );
  260. left = ctx->total[0] & 0x3F;
  261. fill = 64 - left;
  262. ctx->total[0] += (uint32_t) ilen;
  263. ctx->total[0] &= 0xFFFFFFFF;
  264. if( ctx->total[0] < (uint32_t) ilen )
  265. ctx->total[1]++;
  266. if( left && ilen >= fill )
  267. {
  268. memcpy( (void *) (ctx->buffer + left), input, fill );
  269. if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
  270. return( ret );
  271. input += fill;
  272. ilen -= fill;
  273. left = 0;
  274. }
  275. while( ilen >= 64 )
  276. {
  277. if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
  278. return( ret );
  279. input += 64;
  280. ilen -= 64;
  281. }
  282. if( ilen > 0 )
  283. memcpy( (void *) (ctx->buffer + left), input, ilen );
  284. return( 0 );
  285. }
  286. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  287. void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
  288. const unsigned char *input,
  289. size_t ilen )
  290. {
  291. mbedtls_sha1_update_ret( ctx, input, ilen );
  292. }
  293. #endif
  294. /*
  295. * SHA-1 final digest
  296. */
  297. int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
  298. unsigned char output[20] )
  299. {
  300. int ret;
  301. uint32_t used;
  302. uint32_t high, low;
  303. /*
  304. * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
  305. */
  306. used = ctx->total[0] & 0x3F;
  307. ctx->buffer[used++] = 0x80;
  308. if( used <= 56 )
  309. {
  310. /* Enough room for padding + length in current block */
  311. memset( ctx->buffer + used, 0, 56 - used );
  312. }
  313. else
  314. {
  315. /* We'll need an extra block */
  316. memset( ctx->buffer + used, 0, 64 - used );
  317. if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
  318. return( ret );
  319. memset( ctx->buffer, 0, 56 );
  320. }
  321. /*
  322. * Add message length
  323. */
  324. high = ( ctx->total[0] >> 29 )
  325. | ( ctx->total[1] << 3 );
  326. low = ( ctx->total[0] << 3 );
  327. PUT_UINT32_BE( high, ctx->buffer, 56 );
  328. PUT_UINT32_BE( low, ctx->buffer, 60 );
  329. if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
  330. return( ret );
  331. /*
  332. * Output final state
  333. */
  334. PUT_UINT32_BE( ctx->state[0], output, 0 );
  335. PUT_UINT32_BE( ctx->state[1], output, 4 );
  336. PUT_UINT32_BE( ctx->state[2], output, 8 );
  337. PUT_UINT32_BE( ctx->state[3], output, 12 );
  338. PUT_UINT32_BE( ctx->state[4], output, 16 );
  339. return( 0 );
  340. }
  341. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  342. void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
  343. unsigned char output[20] )
  344. {
  345. mbedtls_sha1_finish_ret( ctx, output );
  346. }
  347. #endif
  348. #endif /* !MBEDTLS_SHA1_ALT */
  349. /*
  350. * output = SHA-1( input buffer )
  351. */
  352. int mbedtls_sha1_ret( const unsigned char *input,
  353. size_t ilen,
  354. unsigned char output[20] )
  355. {
  356. int ret;
  357. mbedtls_sha1_context ctx;
  358. mbedtls_sha1_init( &ctx );
  359. if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
  360. goto exit;
  361. if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
  362. goto exit;
  363. if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
  364. goto exit;
  365. exit:
  366. mbedtls_sha1_free( &ctx );
  367. return( ret );
  368. }
  369. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  370. void mbedtls_sha1( const unsigned char *input,
  371. size_t ilen,
  372. unsigned char output[20] )
  373. {
  374. mbedtls_sha1_ret( input, ilen, output );
  375. }
  376. #endif
  377. #if defined(MBEDTLS_SELF_TEST)
  378. /*
  379. * FIPS-180-1 test vectors
  380. */
  381. static const unsigned char sha1_test_buf[3][57] =
  382. {
  383. { "abc" },
  384. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
  385. { "" }
  386. };
  387. static const size_t sha1_test_buflen[3] =
  388. {
  389. 3, 56, 1000
  390. };
  391. static const unsigned char sha1_test_sum[3][20] =
  392. {
  393. { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
  394. 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
  395. { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
  396. 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
  397. { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
  398. 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
  399. };
  400. /*
  401. * Checkup routine
  402. */
  403. int mbedtls_sha1_self_test( int verbose )
  404. {
  405. int i, j, buflen, ret = 0;
  406. unsigned char buf[1024];
  407. unsigned char sha1sum[20];
  408. mbedtls_sha1_context ctx;
  409. mbedtls_sha1_init( &ctx );
  410. /*
  411. * SHA-1
  412. */
  413. for( i = 0; i < 3; i++ )
  414. {
  415. if( verbose != 0 )
  416. mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
  417. if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
  418. goto fail;
  419. if( i == 2 )
  420. {
  421. memset( buf, 'a', buflen = 1000 );
  422. for( j = 0; j < 1000; j++ )
  423. {
  424. ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
  425. if( ret != 0 )
  426. goto fail;
  427. }
  428. }
  429. else
  430. {
  431. ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
  432. sha1_test_buflen[i] );
  433. if( ret != 0 )
  434. goto fail;
  435. }
  436. if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
  437. goto fail;
  438. if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
  439. {
  440. ret = 1;
  441. goto fail;
  442. }
  443. if( verbose != 0 )
  444. mbedtls_printf( "passed\n" );
  445. }
  446. if( verbose != 0 )
  447. mbedtls_printf( "\n" );
  448. goto exit;
  449. fail:
  450. if( verbose != 0 )
  451. mbedtls_printf( "failed\n" );
  452. exit:
  453. mbedtls_sha1_free( &ctx );
  454. return( ret );
  455. }
  456. #endif /* MBEDTLS_SELF_TEST */
  457. #endif /* MBEDTLS_SHA1_C */