sha1.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*
  2. * FIPS-180-1 compliant SHA-1 implementation
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. /*
  47. * The SHA-1 standard was published by NIST in 1993.
  48. *
  49. * http://www.itl.nist.gov/fipspubs/fip180-1.htm
  50. */
  51. #if !defined(MBEDTLS_CONFIG_FILE)
  52. #include "mbedtls/config.h"
  53. #else
  54. #include MBEDTLS_CONFIG_FILE
  55. #endif
  56. #if defined(MBEDTLS_SHA1_C)
  57. #include "mbedtls/sha1.h"
  58. #include "mbedtls/platform_util.h"
  59. #include <string.h>
  60. #if defined(MBEDTLS_SELF_TEST)
  61. #if defined(MBEDTLS_PLATFORM_C)
  62. #include "mbedtls/platform.h"
  63. #else
  64. #include <stdio.h>
  65. #define mbedtls_printf printf
  66. #endif /* MBEDTLS_PLATFORM_C */
  67. #endif /* MBEDTLS_SELF_TEST */
  68. #define SHA1_VALIDATE_RET(cond) \
  69. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
  70. #define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
  71. #if !defined(MBEDTLS_SHA1_ALT)
  72. /*
  73. * 32-bit integer manipulation macros (big endian)
  74. */
  75. #ifndef GET_UINT32_BE
  76. #define GET_UINT32_BE(n,b,i) \
  77. { \
  78. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  79. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  80. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  81. | ( (uint32_t) (b)[(i) + 3] ); \
  82. }
  83. #endif
  84. #ifndef PUT_UINT32_BE
  85. #define PUT_UINT32_BE(n,b,i) \
  86. { \
  87. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  88. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  89. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  90. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  91. }
  92. #endif
  93. void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
  94. {
  95. SHA1_VALIDATE( ctx != NULL );
  96. memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
  97. }
  98. void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
  99. {
  100. if( ctx == NULL )
  101. return;
  102. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
  103. }
  104. void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
  105. const mbedtls_sha1_context *src )
  106. {
  107. SHA1_VALIDATE( dst != NULL );
  108. SHA1_VALIDATE( src != NULL );
  109. *dst = *src;
  110. }
  111. /*
  112. * SHA-1 context setup
  113. */
  114. int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
  115. {
  116. SHA1_VALIDATE_RET( ctx != NULL );
  117. ctx->total[0] = 0;
  118. ctx->total[1] = 0;
  119. ctx->state[0] = 0x67452301;
  120. ctx->state[1] = 0xEFCDAB89;
  121. ctx->state[2] = 0x98BADCFE;
  122. ctx->state[3] = 0x10325476;
  123. ctx->state[4] = 0xC3D2E1F0;
  124. return( 0 );
  125. }
  126. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  127. void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
  128. {
  129. mbedtls_sha1_starts_ret( ctx );
  130. }
  131. #endif
  132. #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
  133. int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
  134. const unsigned char data[64] )
  135. {
  136. struct
  137. {
  138. uint32_t temp, W[16], A, B, C, D, E;
  139. } local;
  140. SHA1_VALIDATE_RET( ctx != NULL );
  141. SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
  142. GET_UINT32_BE( local.W[ 0], data, 0 );
  143. GET_UINT32_BE( local.W[ 1], data, 4 );
  144. GET_UINT32_BE( local.W[ 2], data, 8 );
  145. GET_UINT32_BE( local.W[ 3], data, 12 );
  146. GET_UINT32_BE( local.W[ 4], data, 16 );
  147. GET_UINT32_BE( local.W[ 5], data, 20 );
  148. GET_UINT32_BE( local.W[ 6], data, 24 );
  149. GET_UINT32_BE( local.W[ 7], data, 28 );
  150. GET_UINT32_BE( local.W[ 8], data, 32 );
  151. GET_UINT32_BE( local.W[ 9], data, 36 );
  152. GET_UINT32_BE( local.W[10], data, 40 );
  153. GET_UINT32_BE( local.W[11], data, 44 );
  154. GET_UINT32_BE( local.W[12], data, 48 );
  155. GET_UINT32_BE( local.W[13], data, 52 );
  156. GET_UINT32_BE( local.W[14], data, 56 );
  157. GET_UINT32_BE( local.W[15], data, 60 );
  158. #define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
  159. #define R(t) \
  160. ( \
  161. local.temp = local.W[( (t) - 3 ) & 0x0F] ^ \
  162. local.W[( (t) - 8 ) & 0x0F] ^ \
  163. local.W[( (t) - 14 ) & 0x0F] ^ \
  164. local.W[ (t) & 0x0F], \
  165. ( local.W[(t) & 0x0F] = S(local.temp,1) ) \
  166. )
  167. #define P(a,b,c,d,e,x) \
  168. do \
  169. { \
  170. (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
  171. (b) = S((b),30); \
  172. } while( 0 )
  173. local.A = ctx->state[0];
  174. local.B = ctx->state[1];
  175. local.C = ctx->state[2];
  176. local.D = ctx->state[3];
  177. local.E = ctx->state[4];
  178. #define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  179. #define K 0x5A827999
  180. P( local.A, local.B, local.C, local.D, local.E, local.W[0] );
  181. P( local.E, local.A, local.B, local.C, local.D, local.W[1] );
  182. P( local.D, local.E, local.A, local.B, local.C, local.W[2] );
  183. P( local.C, local.D, local.E, local.A, local.B, local.W[3] );
  184. P( local.B, local.C, local.D, local.E, local.A, local.W[4] );
  185. P( local.A, local.B, local.C, local.D, local.E, local.W[5] );
  186. P( local.E, local.A, local.B, local.C, local.D, local.W[6] );
  187. P( local.D, local.E, local.A, local.B, local.C, local.W[7] );
  188. P( local.C, local.D, local.E, local.A, local.B, local.W[8] );
  189. P( local.B, local.C, local.D, local.E, local.A, local.W[9] );
  190. P( local.A, local.B, local.C, local.D, local.E, local.W[10] );
  191. P( local.E, local.A, local.B, local.C, local.D, local.W[11] );
  192. P( local.D, local.E, local.A, local.B, local.C, local.W[12] );
  193. P( local.C, local.D, local.E, local.A, local.B, local.W[13] );
  194. P( local.B, local.C, local.D, local.E, local.A, local.W[14] );
  195. P( local.A, local.B, local.C, local.D, local.E, local.W[15] );
  196. P( local.E, local.A, local.B, local.C, local.D, R(16) );
  197. P( local.D, local.E, local.A, local.B, local.C, R(17) );
  198. P( local.C, local.D, local.E, local.A, local.B, R(18) );
  199. P( local.B, local.C, local.D, local.E, local.A, R(19) );
  200. #undef K
  201. #undef F
  202. #define F(x,y,z) ((x) ^ (y) ^ (z))
  203. #define K 0x6ED9EBA1
  204. P( local.A, local.B, local.C, local.D, local.E, R(20) );
  205. P( local.E, local.A, local.B, local.C, local.D, R(21) );
  206. P( local.D, local.E, local.A, local.B, local.C, R(22) );
  207. P( local.C, local.D, local.E, local.A, local.B, R(23) );
  208. P( local.B, local.C, local.D, local.E, local.A, R(24) );
  209. P( local.A, local.B, local.C, local.D, local.E, R(25) );
  210. P( local.E, local.A, local.B, local.C, local.D, R(26) );
  211. P( local.D, local.E, local.A, local.B, local.C, R(27) );
  212. P( local.C, local.D, local.E, local.A, local.B, R(28) );
  213. P( local.B, local.C, local.D, local.E, local.A, R(29) );
  214. P( local.A, local.B, local.C, local.D, local.E, R(30) );
  215. P( local.E, local.A, local.B, local.C, local.D, R(31) );
  216. P( local.D, local.E, local.A, local.B, local.C, R(32) );
  217. P( local.C, local.D, local.E, local.A, local.B, R(33) );
  218. P( local.B, local.C, local.D, local.E, local.A, R(34) );
  219. P( local.A, local.B, local.C, local.D, local.E, R(35) );
  220. P( local.E, local.A, local.B, local.C, local.D, R(36) );
  221. P( local.D, local.E, local.A, local.B, local.C, R(37) );
  222. P( local.C, local.D, local.E, local.A, local.B, R(38) );
  223. P( local.B, local.C, local.D, local.E, local.A, R(39) );
  224. #undef K
  225. #undef F
  226. #define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
  227. #define K 0x8F1BBCDC
  228. P( local.A, local.B, local.C, local.D, local.E, R(40) );
  229. P( local.E, local.A, local.B, local.C, local.D, R(41) );
  230. P( local.D, local.E, local.A, local.B, local.C, R(42) );
  231. P( local.C, local.D, local.E, local.A, local.B, R(43) );
  232. P( local.B, local.C, local.D, local.E, local.A, R(44) );
  233. P( local.A, local.B, local.C, local.D, local.E, R(45) );
  234. P( local.E, local.A, local.B, local.C, local.D, R(46) );
  235. P( local.D, local.E, local.A, local.B, local.C, R(47) );
  236. P( local.C, local.D, local.E, local.A, local.B, R(48) );
  237. P( local.B, local.C, local.D, local.E, local.A, R(49) );
  238. P( local.A, local.B, local.C, local.D, local.E, R(50) );
  239. P( local.E, local.A, local.B, local.C, local.D, R(51) );
  240. P( local.D, local.E, local.A, local.B, local.C, R(52) );
  241. P( local.C, local.D, local.E, local.A, local.B, R(53) );
  242. P( local.B, local.C, local.D, local.E, local.A, R(54) );
  243. P( local.A, local.B, local.C, local.D, local.E, R(55) );
  244. P( local.E, local.A, local.B, local.C, local.D, R(56) );
  245. P( local.D, local.E, local.A, local.B, local.C, R(57) );
  246. P( local.C, local.D, local.E, local.A, local.B, R(58) );
  247. P( local.B, local.C, local.D, local.E, local.A, R(59) );
  248. #undef K
  249. #undef F
  250. #define F(x,y,z) ((x) ^ (y) ^ (z))
  251. #define K 0xCA62C1D6
  252. P( local.A, local.B, local.C, local.D, local.E, R(60) );
  253. P( local.E, local.A, local.B, local.C, local.D, R(61) );
  254. P( local.D, local.E, local.A, local.B, local.C, R(62) );
  255. P( local.C, local.D, local.E, local.A, local.B, R(63) );
  256. P( local.B, local.C, local.D, local.E, local.A, R(64) );
  257. P( local.A, local.B, local.C, local.D, local.E, R(65) );
  258. P( local.E, local.A, local.B, local.C, local.D, R(66) );
  259. P( local.D, local.E, local.A, local.B, local.C, R(67) );
  260. P( local.C, local.D, local.E, local.A, local.B, R(68) );
  261. P( local.B, local.C, local.D, local.E, local.A, R(69) );
  262. P( local.A, local.B, local.C, local.D, local.E, R(70) );
  263. P( local.E, local.A, local.B, local.C, local.D, R(71) );
  264. P( local.D, local.E, local.A, local.B, local.C, R(72) );
  265. P( local.C, local.D, local.E, local.A, local.B, R(73) );
  266. P( local.B, local.C, local.D, local.E, local.A, R(74) );
  267. P( local.A, local.B, local.C, local.D, local.E, R(75) );
  268. P( local.E, local.A, local.B, local.C, local.D, R(76) );
  269. P( local.D, local.E, local.A, local.B, local.C, R(77) );
  270. P( local.C, local.D, local.E, local.A, local.B, R(78) );
  271. P( local.B, local.C, local.D, local.E, local.A, R(79) );
  272. #undef K
  273. #undef F
  274. ctx->state[0] += local.A;
  275. ctx->state[1] += local.B;
  276. ctx->state[2] += local.C;
  277. ctx->state[3] += local.D;
  278. ctx->state[4] += local.E;
  279. /* Zeroise buffers and variables to clear sensitive data from memory. */
  280. mbedtls_platform_zeroize( &local, sizeof( local ) );
  281. return( 0 );
  282. }
  283. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  284. void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
  285. const unsigned char data[64] )
  286. {
  287. mbedtls_internal_sha1_process( ctx, data );
  288. }
  289. #endif
  290. #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
  291. /*
  292. * SHA-1 process buffer
  293. */
  294. int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
  295. const unsigned char *input,
  296. size_t ilen )
  297. {
  298. int ret;
  299. size_t fill;
  300. uint32_t left;
  301. SHA1_VALIDATE_RET( ctx != NULL );
  302. SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
  303. if( ilen == 0 )
  304. return( 0 );
  305. left = ctx->total[0] & 0x3F;
  306. fill = 64 - left;
  307. ctx->total[0] += (uint32_t) ilen;
  308. ctx->total[0] &= 0xFFFFFFFF;
  309. if( ctx->total[0] < (uint32_t) ilen )
  310. ctx->total[1]++;
  311. if( left && ilen >= fill )
  312. {
  313. memcpy( (void *) (ctx->buffer + left), input, fill );
  314. if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
  315. return( ret );
  316. input += fill;
  317. ilen -= fill;
  318. left = 0;
  319. }
  320. while( ilen >= 64 )
  321. {
  322. if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
  323. return( ret );
  324. input += 64;
  325. ilen -= 64;
  326. }
  327. if( ilen > 0 )
  328. memcpy( (void *) (ctx->buffer + left), input, ilen );
  329. return( 0 );
  330. }
  331. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  332. void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
  333. const unsigned char *input,
  334. size_t ilen )
  335. {
  336. mbedtls_sha1_update_ret( ctx, input, ilen );
  337. }
  338. #endif
  339. /*
  340. * SHA-1 final digest
  341. */
  342. int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
  343. unsigned char output[20] )
  344. {
  345. int ret;
  346. uint32_t used;
  347. uint32_t high, low;
  348. SHA1_VALIDATE_RET( ctx != NULL );
  349. SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
  350. /*
  351. * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
  352. */
  353. used = ctx->total[0] & 0x3F;
  354. ctx->buffer[used++] = 0x80;
  355. if( used <= 56 )
  356. {
  357. /* Enough room for padding + length in current block */
  358. memset( ctx->buffer + used, 0, 56 - used );
  359. }
  360. else
  361. {
  362. /* We'll need an extra block */
  363. memset( ctx->buffer + used, 0, 64 - used );
  364. if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
  365. return( ret );
  366. memset( ctx->buffer, 0, 56 );
  367. }
  368. /*
  369. * Add message length
  370. */
  371. high = ( ctx->total[0] >> 29 )
  372. | ( ctx->total[1] << 3 );
  373. low = ( ctx->total[0] << 3 );
  374. PUT_UINT32_BE( high, ctx->buffer, 56 );
  375. PUT_UINT32_BE( low, ctx->buffer, 60 );
  376. if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
  377. return( ret );
  378. /*
  379. * Output final state
  380. */
  381. PUT_UINT32_BE( ctx->state[0], output, 0 );
  382. PUT_UINT32_BE( ctx->state[1], output, 4 );
  383. PUT_UINT32_BE( ctx->state[2], output, 8 );
  384. PUT_UINT32_BE( ctx->state[3], output, 12 );
  385. PUT_UINT32_BE( ctx->state[4], output, 16 );
  386. return( 0 );
  387. }
  388. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  389. void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
  390. unsigned char output[20] )
  391. {
  392. mbedtls_sha1_finish_ret( ctx, output );
  393. }
  394. #endif
  395. #endif /* !MBEDTLS_SHA1_ALT */
  396. /*
  397. * output = SHA-1( input buffer )
  398. */
  399. int mbedtls_sha1_ret( const unsigned char *input,
  400. size_t ilen,
  401. unsigned char output[20] )
  402. {
  403. int ret;
  404. mbedtls_sha1_context ctx;
  405. SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
  406. SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
  407. mbedtls_sha1_init( &ctx );
  408. if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
  409. goto exit;
  410. if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
  411. goto exit;
  412. if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
  413. goto exit;
  414. exit:
  415. mbedtls_sha1_free( &ctx );
  416. return( ret );
  417. }
  418. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  419. void mbedtls_sha1( const unsigned char *input,
  420. size_t ilen,
  421. unsigned char output[20] )
  422. {
  423. mbedtls_sha1_ret( input, ilen, output );
  424. }
  425. #endif
  426. #if defined(MBEDTLS_SELF_TEST)
  427. /*
  428. * FIPS-180-1 test vectors
  429. */
  430. static const unsigned char sha1_test_buf[3][57] =
  431. {
  432. { "abc" },
  433. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
  434. { "" }
  435. };
  436. static const size_t sha1_test_buflen[3] =
  437. {
  438. 3, 56, 1000
  439. };
  440. static const unsigned char sha1_test_sum[3][20] =
  441. {
  442. { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
  443. 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
  444. { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
  445. 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
  446. { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
  447. 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
  448. };
  449. /*
  450. * Checkup routine
  451. */
  452. int mbedtls_sha1_self_test( int verbose )
  453. {
  454. int i, j, buflen, ret = 0;
  455. unsigned char buf[1024];
  456. unsigned char sha1sum[20];
  457. mbedtls_sha1_context ctx;
  458. mbedtls_sha1_init( &ctx );
  459. /*
  460. * SHA-1
  461. */
  462. for( i = 0; i < 3; i++ )
  463. {
  464. if( verbose != 0 )
  465. mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
  466. if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
  467. goto fail;
  468. if( i == 2 )
  469. {
  470. memset( buf, 'a', buflen = 1000 );
  471. for( j = 0; j < 1000; j++ )
  472. {
  473. ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
  474. if( ret != 0 )
  475. goto fail;
  476. }
  477. }
  478. else
  479. {
  480. ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
  481. sha1_test_buflen[i] );
  482. if( ret != 0 )
  483. goto fail;
  484. }
  485. if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
  486. goto fail;
  487. if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
  488. {
  489. ret = 1;
  490. goto fail;
  491. }
  492. if( verbose != 0 )
  493. mbedtls_printf( "passed\n" );
  494. }
  495. if( verbose != 0 )
  496. mbedtls_printf( "\n" );
  497. goto exit;
  498. fail:
  499. if( verbose != 0 )
  500. mbedtls_printf( "failed\n" );
  501. exit:
  502. mbedtls_sha1_free( &ctx );
  503. return( ret );
  504. }
  505. #endif /* MBEDTLS_SELF_TEST */
  506. #endif /* MBEDTLS_SHA1_C */