sha1.c 20 KB

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