sha1.c 14 KB

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