sha1.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * FIPS-180-1 compliant SHA-1 implementation
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * The SHA-1 standard was published by NIST in 1993.
  27. *
  28. * http://www.itl.nist.gov/fipspubs/fip180-1.htm
  29. */
  30. /* Note: This file has been modified by ST's MCD Application Team, to support
  31. the hardware crypto engine embedded in STM32F417xx */
  32. #include "config.h"
  33. #if defined(POLARSSL_SHA1_C)
  34. #include "main.h"
  35. #include "polarssl/sha1.h"
  36. #if defined(POLARSSL_FS_IO) || defined(POLARSSL_SELF_TEST)
  37. #include <stdio.h>
  38. #endif
  39. #ifdef USE_STM32F4XX_HW_CRYPTO /* use HW Crypto */
  40. HASH_InitTypeDef SHA1_HASH_InitStructure;
  41. HASH_MsgDigest SHA1_MessageDigest;
  42. #endif /* USE_STM32F4XX_HW_CRYPTO */
  43. /*
  44. * 32-bit integer manipulation macros (big endian)
  45. */
  46. #ifndef GET_ULONG_BE
  47. #define GET_ULONG_BE(n,b,i) \
  48. { \
  49. (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
  50. | ( (unsigned long) (b)[(i) + 1] << 16 ) \
  51. | ( (unsigned long) (b)[(i) + 2] << 8 ) \
  52. | ( (unsigned long) (b)[(i) + 3] ); \
  53. }
  54. #endif
  55. #ifndef PUT_ULONG_BE
  56. #define PUT_ULONG_BE(n,b,i) \
  57. { \
  58. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  59. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  60. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  61. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  62. }
  63. #endif
  64. /*
  65. * SHA-1 context setup
  66. */
  67. void sha1_starts( sha1_context *ctx )
  68. {
  69. ctx->total[0] = 0;
  70. ctx->total[1] = 0;
  71. ctx->state[0] = 0x67452301;
  72. ctx->state[1] = 0xEFCDAB89;
  73. ctx->state[2] = 0x98BADCFE;
  74. ctx->state[3] = 0x10325476;
  75. ctx->state[4] = 0xC3D2E1F0;
  76. }
  77. static void sha1_process( sha1_context *ctx, const unsigned char data[64] )
  78. {
  79. unsigned long temp, W[16], A, B, C, D, E;
  80. GET_ULONG_BE( W[ 0], data, 0 );
  81. GET_ULONG_BE( W[ 1], data, 4 );
  82. GET_ULONG_BE( W[ 2], data, 8 );
  83. GET_ULONG_BE( W[ 3], data, 12 );
  84. GET_ULONG_BE( W[ 4], data, 16 );
  85. GET_ULONG_BE( W[ 5], data, 20 );
  86. GET_ULONG_BE( W[ 6], data, 24 );
  87. GET_ULONG_BE( W[ 7], data, 28 );
  88. GET_ULONG_BE( W[ 8], data, 32 );
  89. GET_ULONG_BE( W[ 9], data, 36 );
  90. GET_ULONG_BE( W[10], data, 40 );
  91. GET_ULONG_BE( W[11], data, 44 );
  92. GET_ULONG_BE( W[12], data, 48 );
  93. GET_ULONG_BE( W[13], data, 52 );
  94. GET_ULONG_BE( W[14], data, 56 );
  95. GET_ULONG_BE( W[15], data, 60 );
  96. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  97. #define R(t) \
  98. ( \
  99. temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \
  100. W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \
  101. ( W[t & 0x0F] = S(temp,1) ) \
  102. )
  103. #define P(a,b,c,d,e,x) \
  104. { \
  105. e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
  106. }
  107. A = ctx->state[0];
  108. B = ctx->state[1];
  109. C = ctx->state[2];
  110. D = ctx->state[3];
  111. E = ctx->state[4];
  112. #define F(x,y,z) (z ^ (x & (y ^ z)))
  113. #define K 0x5A827999
  114. P( A, B, C, D, E, W[0] );
  115. P( E, A, B, C, D, W[1] );
  116. P( D, E, A, B, C, W[2] );
  117. P( C, D, E, A, B, W[3] );
  118. P( B, C, D, E, A, W[4] );
  119. P( A, B, C, D, E, W[5] );
  120. P( E, A, B, C, D, W[6] );
  121. P( D, E, A, B, C, W[7] );
  122. P( C, D, E, A, B, W[8] );
  123. P( B, C, D, E, A, W[9] );
  124. P( A, B, C, D, E, W[10] );
  125. P( E, A, B, C, D, W[11] );
  126. P( D, E, A, B, C, W[12] );
  127. P( C, D, E, A, B, W[13] );
  128. P( B, C, D, E, A, W[14] );
  129. P( A, B, C, D, E, W[15] );
  130. P( E, A, B, C, D, R(16) );
  131. P( D, E, A, B, C, R(17) );
  132. P( C, D, E, A, B, R(18) );
  133. P( B, C, D, E, A, R(19) );
  134. #undef K
  135. #undef F
  136. #define F(x,y,z) (x ^ y ^ z)
  137. #define K 0x6ED9EBA1
  138. P( A, B, C, D, E, R(20) );
  139. P( E, A, B, C, D, R(21) );
  140. P( D, E, A, B, C, R(22) );
  141. P( C, D, E, A, B, R(23) );
  142. P( B, C, D, E, A, R(24) );
  143. P( A, B, C, D, E, R(25) );
  144. P( E, A, B, C, D, R(26) );
  145. P( D, E, A, B, C, R(27) );
  146. P( C, D, E, A, B, R(28) );
  147. P( B, C, D, E, A, R(29) );
  148. P( A, B, C, D, E, R(30) );
  149. P( E, A, B, C, D, R(31) );
  150. P( D, E, A, B, C, R(32) );
  151. P( C, D, E, A, B, R(33) );
  152. P( B, C, D, E, A, R(34) );
  153. P( A, B, C, D, E, R(35) );
  154. P( E, A, B, C, D, R(36) );
  155. P( D, E, A, B, C, R(37) );
  156. P( C, D, E, A, B, R(38) );
  157. P( B, C, D, E, A, R(39) );
  158. #undef K
  159. #undef F
  160. #define F(x,y,z) ((x & y) | (z & (x | y)))
  161. #define K 0x8F1BBCDC
  162. P( A, B, C, D, E, R(40) );
  163. P( E, A, B, C, D, R(41) );
  164. P( D, E, A, B, C, R(42) );
  165. P( C, D, E, A, B, R(43) );
  166. P( B, C, D, E, A, R(44) );
  167. P( A, B, C, D, E, R(45) );
  168. P( E, A, B, C, D, R(46) );
  169. P( D, E, A, B, C, R(47) );
  170. P( C, D, E, A, B, R(48) );
  171. P( B, C, D, E, A, R(49) );
  172. P( A, B, C, D, E, R(50) );
  173. P( E, A, B, C, D, R(51) );
  174. P( D, E, A, B, C, R(52) );
  175. P( C, D, E, A, B, R(53) );
  176. P( B, C, D, E, A, R(54) );
  177. P( A, B, C, D, E, R(55) );
  178. P( E, A, B, C, D, R(56) );
  179. P( D, E, A, B, C, R(57) );
  180. P( C, D, E, A, B, R(58) );
  181. P( B, C, D, E, A, R(59) );
  182. #undef K
  183. #undef F
  184. #define F(x,y,z) (x ^ y ^ z)
  185. #define K 0xCA62C1D6
  186. P( A, B, C, D, E, R(60) );
  187. P( E, A, B, C, D, R(61) );
  188. P( D, E, A, B, C, R(62) );
  189. P( C, D, E, A, B, R(63) );
  190. P( B, C, D, E, A, R(64) );
  191. P( A, B, C, D, E, R(65) );
  192. P( E, A, B, C, D, R(66) );
  193. P( D, E, A, B, C, R(67) );
  194. P( C, D, E, A, B, R(68) );
  195. P( B, C, D, E, A, R(69) );
  196. P( A, B, C, D, E, R(70) );
  197. P( E, A, B, C, D, R(71) );
  198. P( D, E, A, B, C, R(72) );
  199. P( C, D, E, A, B, R(73) );
  200. P( B, C, D, E, A, R(74) );
  201. P( A, B, C, D, E, R(75) );
  202. P( E, A, B, C, D, R(76) );
  203. P( D, E, A, B, C, R(77) );
  204. P( C, D, E, A, B, R(78) );
  205. P( B, C, D, E, A, R(79) );
  206. #undef K
  207. #undef F
  208. ctx->state[0] += A;
  209. ctx->state[1] += B;
  210. ctx->state[2] += C;
  211. ctx->state[3] += D;
  212. ctx->state[4] += E;
  213. }
  214. /*
  215. * SHA-1 process buffer
  216. */
  217. void sha1_update( sha1_context *ctx, const unsigned char *input, size_t ilen )
  218. {
  219. size_t fill;
  220. unsigned long left;
  221. if( ilen <= 0 )
  222. return;
  223. left = ctx->total[0] & 0x3F;
  224. fill = 64 - left;
  225. ctx->total[0] += (unsigned long) ilen;
  226. ctx->total[0] &= 0xFFFFFFFF;
  227. if( ctx->total[0] < (unsigned long) ilen )
  228. ctx->total[1]++;
  229. if( left && ilen >= fill )
  230. {
  231. memcpy( (void *) (ctx->buffer + left),
  232. (void *) input, fill );
  233. sha1_process( ctx, ctx->buffer );
  234. input += fill;
  235. ilen -= fill;
  236. left = 0;
  237. }
  238. while( ilen >= 64 )
  239. {
  240. sha1_process( ctx, input );
  241. input += 64;
  242. ilen -= 64;
  243. }
  244. if( ilen > 0 )
  245. {
  246. memcpy( (void *) (ctx->buffer + left),
  247. (void *) input, ilen );
  248. }
  249. }
  250. static const unsigned char sha1_padding[64] =
  251. {
  252. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  253. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  254. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  255. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  256. };
  257. /*
  258. * SHA-1 final digest
  259. */
  260. void sha1_finish( sha1_context *ctx, unsigned char output[20] )
  261. {
  262. unsigned long last, padn;
  263. unsigned long high, low;
  264. unsigned char msglen[8];
  265. high = ( ctx->total[0] >> 29 )
  266. | ( ctx->total[1] << 3 );
  267. low = ( ctx->total[0] << 3 );
  268. PUT_ULONG_BE( high, msglen, 0 );
  269. PUT_ULONG_BE( low, msglen, 4 );
  270. last = ctx->total[0] & 0x3F;
  271. padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  272. sha1_update( ctx, (unsigned char *) sha1_padding, padn );
  273. sha1_update( ctx, msglen, 8 );
  274. PUT_ULONG_BE( ctx->state[0], output, 0 );
  275. PUT_ULONG_BE( ctx->state[1], output, 4 );
  276. PUT_ULONG_BE( ctx->state[2], output, 8 );
  277. PUT_ULONG_BE( ctx->state[3], output, 12 );
  278. PUT_ULONG_BE( ctx->state[4], output, 16 );
  279. }
  280. /*
  281. * output = SHA-1( input buffer )
  282. */
  283. void sha1( const unsigned char *input, size_t ilen, unsigned char output[20] )
  284. {
  285. sha1_context ctx;
  286. sha1_starts( &ctx );
  287. sha1_update( &ctx, input, ilen );
  288. sha1_finish( &ctx, output );
  289. memset( &ctx, 0, sizeof( sha1_context ) );
  290. }
  291. #if defined(POLARSSL_FS_IO)
  292. /*
  293. * output = SHA-1( file contents )
  294. */
  295. int sha1_file( const char *path, unsigned char output[20] )
  296. {
  297. FILE *f;
  298. size_t n;
  299. sha1_context ctx;
  300. unsigned char buf[1024];
  301. if( ( f = fopen( path, "rb" ) ) == NULL )
  302. return( 1 );
  303. sha1_starts( &ctx );
  304. while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
  305. sha1_update( &ctx, buf, n );
  306. sha1_finish( &ctx, output );
  307. memset( &ctx, 0, sizeof( sha1_context ) );
  308. if( ferror( f ) != 0 )
  309. {
  310. fclose( f );
  311. return( 2 );
  312. }
  313. fclose( f );
  314. return( 0 );
  315. }
  316. #endif /* POLARSSL_FS_IO */
  317. /*
  318. * SHA-1 HMAC context setup
  319. */
  320. void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, size_t keylen )
  321. {
  322. size_t i;
  323. unsigned char sum[20];
  324. if( keylen > 64 )
  325. {
  326. sha1( key, keylen, sum );
  327. keylen = 20;
  328. key = sum;
  329. }
  330. memset( ctx->ipad, 0x36, 64 );
  331. memset( ctx->opad, 0x5C, 64 );
  332. for( i = 0; i < keylen; i++ )
  333. {
  334. ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
  335. ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
  336. }
  337. sha1_starts( ctx );
  338. sha1_update( ctx, ctx->ipad, 64 );
  339. memset( sum, 0, sizeof( sum ) );
  340. }
  341. /*
  342. * SHA-1 HMAC process buffer
  343. */
  344. void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, size_t ilen )
  345. {
  346. sha1_update( ctx, input, ilen );
  347. }
  348. /*
  349. * SHA-1 HMAC final digest
  350. */
  351. void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] )
  352. {
  353. unsigned char tmpbuf[20];
  354. sha1_finish( ctx, tmpbuf );
  355. sha1_starts( ctx );
  356. sha1_update( ctx, ctx->opad, 64 );
  357. sha1_update( ctx, tmpbuf, 20 );
  358. sha1_finish( ctx, output );
  359. memset( tmpbuf, 0, sizeof( tmpbuf ) );
  360. }
  361. /*
  362. * SHA1 HMAC context reset
  363. */
  364. void sha1_hmac_reset( sha1_context *ctx )
  365. {
  366. sha1_starts( ctx );
  367. sha1_update( ctx, ctx->ipad, 64 );
  368. }
  369. /*
  370. * output = HMAC-SHA-1( hmac key, input buffer )
  371. */
  372. void sha1_hmac( const unsigned char *key, size_t keylen,
  373. const unsigned char *input, size_t ilen,
  374. unsigned char output[20] )
  375. {
  376. #ifdef USE_STM32F4XX_HW_CRYPTO /* use HW Crypto */
  377. __IO uint16_t nbvalidbitsdata = 0;
  378. __IO uint16_t nbvalidbitskey = 0;
  379. uint32_t i = 0;
  380. /* Number of valid bits in last word of the input data */
  381. nbvalidbitsdata = 8 * (ilen % 4);
  382. /* Number of valid bits in last word of the Key */
  383. nbvalidbitskey = 8 * (keylen % 4);
  384. /* HASH IP initialization */
  385. HASH_DeInit();
  386. /* HASH Configuration */
  387. HASH_StructInit(&SHA1_HASH_InitStructure);
  388. SHA1_HASH_InitStructure.HASH_AlgoSelection = HASH_AlgoSelection_SHA1;
  389. SHA1_HASH_InitStructure.HASH_AlgoMode = HASH_AlgoMode_HMAC;
  390. SHA1_HASH_InitStructure.HASH_DataType = HASH_DataType_8b;
  391. if(keylen > 64)
  392. {
  393. /* HMAC long key */
  394. SHA1_HASH_InitStructure.HASH_HMACKeyType = HASH_HMACKeyType_LongKey;
  395. }
  396. else
  397. {
  398. /* HMAC short key */
  399. SHA1_HASH_InitStructure.HASH_HMACKeyType = HASH_HMACKeyType_ShortKey;
  400. }
  401. HASH_Init(&SHA1_HASH_InitStructure);
  402. /* Configure the number of valid bits in last word of the key */
  403. HASH_SetLastWordValidBitsNbr(nbvalidbitskey);
  404. /* Write the Key */
  405. for(i = 0; i < keylen; i++)
  406. {
  407. HASH_DataIn(*(uint32_t*)&key[i]);
  408. i = i + 3;
  409. }
  410. /* Start the HASH processor */
  411. HASH_StartDigest();
  412. /* wait until the Busy flag is RESET */
  413. while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET);
  414. /* Configure the number of valid bits in last word of the input data */
  415. HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
  416. /* Write the input block in the IN FIFO */
  417. for(i = 0; i < ilen; i++)
  418. {
  419. HASH_DataIn(*(uint32_t*)&input[i]);
  420. i = i + 3;
  421. }
  422. /* Start the HASH processor */
  423. HASH_StartDigest();
  424. /* wait until the Busy flag is RESET */
  425. while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET);
  426. /* Configure the number of valid bits in last word of the key */
  427. HASH_SetLastWordValidBitsNbr(nbvalidbitskey);
  428. /* Write the Key */
  429. for(i = 0; i < keylen; i++)
  430. {
  431. HASH_DataIn(*(uint32_t*)&key[i]);
  432. i = i + 3;
  433. }
  434. /* Start the HASH processor */
  435. HASH_StartDigest();
  436. /* wait until the Busy flag is RESET */
  437. while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET);
  438. /* Read the message digest */
  439. HASH_GetDigest(&SHA1_MessageDigest);
  440. *(uint32_t*)&output[0] = __REV(SHA1_MessageDigest.Data[0]);
  441. *(uint32_t*)&output[4] = __REV(SHA1_MessageDigest.Data[1]);
  442. *(uint32_t*)&output[8] = __REV(SHA1_MessageDigest.Data[2]);
  443. *(uint32_t*)&output[12] = __REV(SHA1_MessageDigest.Data[3]);
  444. *(uint32_t*)&output[16] = __REV(SHA1_MessageDigest.Data[4]);
  445. #else /* use SW Crypto */
  446. sha1_context ctx;
  447. sha1_hmac_starts( &ctx, key, keylen );
  448. sha1_hmac_update( &ctx, input, ilen );
  449. sha1_hmac_finish( &ctx, output );
  450. memset( &ctx, 0, sizeof( sha1_context ) );
  451. #endif /* USE_STM32F4XX_HW_CRYPTO */
  452. }
  453. #if defined(POLARSSL_SELF_TEST)
  454. /*
  455. * FIPS-180-1 test vectors
  456. */
  457. static unsigned char sha1_test_buf[3][57] =
  458. {
  459. { "abc" },
  460. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
  461. { "" }
  462. };
  463. static const int sha1_test_buflen[3] =
  464. {
  465. 3, 56, 1000
  466. };
  467. static const unsigned char sha1_test_sum[3][20] =
  468. {
  469. { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
  470. 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
  471. { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
  472. 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
  473. { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
  474. 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
  475. };
  476. /*
  477. * RFC 2202 test vectors
  478. */
  479. static unsigned char sha1_hmac_test_key[7][26] =
  480. {
  481. { "\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B"
  482. "\x0B\x0B\x0B\x0B" },
  483. { "Jefe" },
  484. { "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  485. "\xAA\xAA\xAA\xAA" },
  486. { "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
  487. "\x11\x12\x13\x14\x15\x16\x17\x18\x19" },
  488. { "\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C"
  489. "\x0C\x0C\x0C\x0C" },
  490. { "" }, /* 0xAA 80 times */
  491. { "" }
  492. };
  493. static const int sha1_hmac_test_keylen[7] =
  494. {
  495. 20, 4, 20, 25, 20, 80, 80
  496. };
  497. static unsigned char sha1_hmac_test_buf[7][74] =
  498. {
  499. { "Hi There" },
  500. { "what do ya want for nothing?" },
  501. { "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  502. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  503. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  504. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  505. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD" },
  506. { "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
  507. "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
  508. "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
  509. "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
  510. "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD" },
  511. { "Test With Truncation" },
  512. { "Test Using Larger Than Block-Size Key - Hash Key First" },
  513. { "Test Using Larger Than Block-Size Key and Larger"
  514. " Than One Block-Size Data" }
  515. };
  516. static const int sha1_hmac_test_buflen[7] =
  517. {
  518. 8, 28, 50, 50, 20, 54, 73
  519. };
  520. static const unsigned char sha1_hmac_test_sum[7][20] =
  521. {
  522. { 0xB6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xE2, 0x8B,
  523. 0xC0, 0xB6, 0xFB, 0x37, 0x8C, 0x8E, 0xF1, 0x46, 0xBE, 0x00 },
  524. { 0xEF, 0xFC, 0xDF, 0x6A, 0xE5, 0xEB, 0x2F, 0xA2, 0xD2, 0x74,
  525. 0x16, 0xD5, 0xF1, 0x84, 0xDF, 0x9C, 0x25, 0x9A, 0x7C, 0x79 },
  526. { 0x12, 0x5D, 0x73, 0x42, 0xB9, 0xAC, 0x11, 0xCD, 0x91, 0xA3,
  527. 0x9A, 0xF4, 0x8A, 0xA1, 0x7B, 0x4F, 0x63, 0xF1, 0x75, 0xD3 },
  528. { 0x4C, 0x90, 0x07, 0xF4, 0x02, 0x62, 0x50, 0xC6, 0xBC, 0x84,
  529. 0x14, 0xF9, 0xBF, 0x50, 0xC8, 0x6C, 0x2D, 0x72, 0x35, 0xDA },
  530. { 0x4C, 0x1A, 0x03, 0x42, 0x4B, 0x55, 0xE0, 0x7F, 0xE7, 0xF2,
  531. 0x7B, 0xE1 },
  532. { 0xAA, 0x4A, 0xE5, 0xE1, 0x52, 0x72, 0xD0, 0x0E, 0x95, 0x70,
  533. 0x56, 0x37, 0xCE, 0x8A, 0x3B, 0x55, 0xED, 0x40, 0x21, 0x12 },
  534. { 0xE8, 0xE9, 0x9D, 0x0F, 0x45, 0x23, 0x7D, 0x78, 0x6D, 0x6B,
  535. 0xBA, 0xA7, 0x96, 0x5C, 0x78, 0x08, 0xBB, 0xFF, 0x1A, 0x91 }
  536. };
  537. /*
  538. * Checkup routine
  539. */
  540. int sha1_self_test( int verbose )
  541. {
  542. int i, j, buflen;
  543. unsigned char buf[1024];
  544. unsigned char sha1sum[20];
  545. sha1_context ctx;
  546. /*
  547. * SHA-1
  548. */
  549. for( i = 0; i < 3; i++ )
  550. {
  551. if( verbose != 0 )
  552. printf( " SHA-1 test #%d: ", i + 1 );
  553. sha1_starts( &ctx );
  554. if( i == 2 )
  555. {
  556. memset( buf, 'a', buflen = 1000 );
  557. for( j = 0; j < 1000; j++ )
  558. sha1_update( &ctx, buf, buflen );
  559. }
  560. else
  561. sha1_update( &ctx, sha1_test_buf[i],
  562. sha1_test_buflen[i] );
  563. sha1_finish( &ctx, sha1sum );
  564. if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
  565. {
  566. if( verbose != 0 )
  567. printf( "failed\n" );
  568. return( 1 );
  569. }
  570. if( verbose != 0 )
  571. printf( "passed\n" );
  572. }
  573. if( verbose != 0 )
  574. printf( "\n" );
  575. for( i = 0; i < 7; i++ )
  576. {
  577. if( verbose != 0 )
  578. printf( " HMAC-SHA-1 test #%d: ", i + 1 );
  579. if( i == 5 || i == 6 )
  580. {
  581. memset( buf, '\xAA', buflen = 80 );
  582. sha1_hmac_starts( &ctx, buf, buflen );
  583. }
  584. else
  585. sha1_hmac_starts( &ctx, sha1_hmac_test_key[i],
  586. sha1_hmac_test_keylen[i] );
  587. sha1_hmac_update( &ctx, sha1_hmac_test_buf[i],
  588. sha1_hmac_test_buflen[i] );
  589. sha1_hmac_finish( &ctx, sha1sum );
  590. buflen = ( i == 4 ) ? 12 : 20;
  591. if( memcmp( sha1sum, sha1_hmac_test_sum[i], buflen ) != 0 )
  592. {
  593. if( verbose != 0 )
  594. printf( "failed\n" );
  595. return( 1 );
  596. }
  597. if( verbose != 0 )
  598. printf( "passed\n" );
  599. }
  600. if( verbose != 0 )
  601. printf( "\n" );
  602. return( 0 );
  603. }
  604. #endif
  605. #endif