pbkdf2-sha1.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. this file is from https://github.com/kholia/PKCS5_PBKDF2
  3. *
  4. * FIPS-180-1 compliant SHA-1 implementation
  5. *
  6. * Copyright (C) 2006-2010, Brainspark B.V.
  7. *
  8. * This file is part of PolarSSL (http://www.polarssl.org)
  9. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10. *
  11. * All rights reserved.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License along
  24. * with this program; if not, write to the Free Software Foundation, Inc.,
  25. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26. *
  27. * The SHA-1 standard was published by NIST in 1993.
  28. *
  29. * http://www.itl.nist.gov/fipspubs/fip180-1.htm
  30. *
  31. * Copyright 2012 Mathias Olsson [email protected]
  32. *
  33. * This file is dual licensed as either GPL version 2 or Apache License 2.0 at your choice
  34. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  35. * http://www.apache.org/licenses/
  36. *
  37. * Note that PolarSSL uses GPL with a FOSS License Exception */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #if defined(TEST) ||defined(DEBUG)
  42. #undef TEST
  43. #undef DEBUG
  44. #warning "undefined TEST/DEBUG"
  45. #endif
  46. typedef struct {
  47. unsigned long total[2]; /*!< number of bytes processed */
  48. unsigned long state[5]; /*!< intermediate digest state */
  49. unsigned char buffer[64]; /*!< data block being processed */
  50. unsigned char ipad[64]; /*!< HMAC: inner padding */
  51. unsigned char opad[64]; /*!< HMAC: outer padding */
  52. } sha1_context;
  53. /*
  54. * 32-bit integer manipulation macros (big endian)
  55. */
  56. #ifndef GET_ULONG_BE
  57. #define GET_ULONG_BE(n,b,i) \
  58. { \
  59. (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
  60. | ( (unsigned long) (b)[(i) + 1] << 16 ) \
  61. | ( (unsigned long) (b)[(i) + 2] << 8 ) \
  62. | ( (unsigned long) (b)[(i) + 3] ); \
  63. }
  64. #endif
  65. #ifndef PUT_ULONG_BE
  66. #define PUT_ULONG_BE(n,b,i) \
  67. { \
  68. (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
  69. (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
  70. (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
  71. (b)[(i) + 3] = (unsigned char) ( (n) ); \
  72. }
  73. #endif
  74. /*
  75. * SHA-1 context setup
  76. */
  77. void sha1_starts(sha1_context * ctx)
  78. {
  79. ctx->total[0] = 0;
  80. ctx->total[1] = 0;
  81. ctx->state[0] = 0x67452301;
  82. ctx->state[1] = 0xEFCDAB89;
  83. ctx->state[2] = 0x98BADCFE;
  84. ctx->state[3] = 0x10325476;
  85. ctx->state[4] = 0xC3D2E1F0;
  86. }
  87. static void sha1_process(sha1_context * ctx, const unsigned char data[64])
  88. {
  89. unsigned long temp, W[16], A, B, C, D, E;
  90. GET_ULONG_BE(W[0], data, 0);
  91. GET_ULONG_BE(W[1], data, 4);
  92. GET_ULONG_BE(W[2], data, 8);
  93. GET_ULONG_BE(W[3], data, 12);
  94. GET_ULONG_BE(W[4], data, 16);
  95. GET_ULONG_BE(W[5], data, 20);
  96. GET_ULONG_BE(W[6], data, 24);
  97. GET_ULONG_BE(W[7], data, 28);
  98. GET_ULONG_BE(W[8], data, 32);
  99. GET_ULONG_BE(W[9], data, 36);
  100. GET_ULONG_BE(W[10], data, 40);
  101. GET_ULONG_BE(W[11], data, 44);
  102. GET_ULONG_BE(W[12], data, 48);
  103. GET_ULONG_BE(W[13], data, 52);
  104. GET_ULONG_BE(W[14], data, 56);
  105. GET_ULONG_BE(W[15], data, 60);
  106. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  107. #define R(t) \
  108. ( \
  109. temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \
  110. W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \
  111. ( W[t & 0x0F] = S(temp,1) ) \
  112. )
  113. #define P(a,b,c,d,e,x) \
  114. { \
  115. e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
  116. }
  117. A = ctx->state[0];
  118. B = ctx->state[1];
  119. C = ctx->state[2];
  120. D = ctx->state[3];
  121. E = ctx->state[4];
  122. #define F(x,y,z) (z ^ (x & (y ^ z)))
  123. #define K 0x5A827999
  124. P(A, B, C, D, E, W[0]);
  125. P(E, A, B, C, D, W[1]);
  126. P(D, E, A, B, C, W[2]);
  127. P(C, D, E, A, B, W[3]);
  128. P(B, C, D, E, A, W[4]);
  129. P(A, B, C, D, E, W[5]);
  130. P(E, A, B, C, D, W[6]);
  131. P(D, E, A, B, C, W[7]);
  132. P(C, D, E, A, B, W[8]);
  133. P(B, C, D, E, A, W[9]);
  134. P(A, B, C, D, E, W[10]);
  135. P(E, A, B, C, D, W[11]);
  136. P(D, E, A, B, C, W[12]);
  137. P(C, D, E, A, B, W[13]);
  138. P(B, C, D, E, A, W[14]);
  139. P(A, B, C, D, E, W[15]);
  140. P(E, A, B, C, D, R(16));
  141. P(D, E, A, B, C, R(17));
  142. P(C, D, E, A, B, R(18));
  143. P(B, C, D, E, A, R(19));
  144. #undef K
  145. #undef F
  146. #define F(x,y,z) (x ^ y ^ z)
  147. #define K 0x6ED9EBA1
  148. P(A, B, C, D, E, R(20));
  149. P(E, A, B, C, D, R(21));
  150. P(D, E, A, B, C, R(22));
  151. P(C, D, E, A, B, R(23));
  152. P(B, C, D, E, A, R(24));
  153. P(A, B, C, D, E, R(25));
  154. P(E, A, B, C, D, R(26));
  155. P(D, E, A, B, C, R(27));
  156. P(C, D, E, A, B, R(28));
  157. P(B, C, D, E, A, R(29));
  158. P(A, B, C, D, E, R(30));
  159. P(E, A, B, C, D, R(31));
  160. P(D, E, A, B, C, R(32));
  161. P(C, D, E, A, B, R(33));
  162. P(B, C, D, E, A, R(34));
  163. P(A, B, C, D, E, R(35));
  164. P(E, A, B, C, D, R(36));
  165. P(D, E, A, B, C, R(37));
  166. P(C, D, E, A, B, R(38));
  167. P(B, C, D, E, A, R(39));
  168. #undef K
  169. #undef F
  170. #define F(x,y,z) ((x & y) | (z & (x | y)))
  171. #define K 0x8F1BBCDC
  172. P(A, B, C, D, E, R(40));
  173. P(E, A, B, C, D, R(41));
  174. P(D, E, A, B, C, R(42));
  175. P(C, D, E, A, B, R(43));
  176. P(B, C, D, E, A, R(44));
  177. P(A, B, C, D, E, R(45));
  178. P(E, A, B, C, D, R(46));
  179. P(D, E, A, B, C, R(47));
  180. P(C, D, E, A, B, R(48));
  181. P(B, C, D, E, A, R(49));
  182. P(A, B, C, D, E, R(50));
  183. P(E, A, B, C, D, R(51));
  184. P(D, E, A, B, C, R(52));
  185. P(C, D, E, A, B, R(53));
  186. P(B, C, D, E, A, R(54));
  187. P(A, B, C, D, E, R(55));
  188. P(E, A, B, C, D, R(56));
  189. P(D, E, A, B, C, R(57));
  190. P(C, D, E, A, B, R(58));
  191. P(B, C, D, E, A, R(59));
  192. #undef K
  193. #undef F
  194. #define F(x,y,z) (x ^ y ^ z)
  195. #define K 0xCA62C1D6
  196. P(A, B, C, D, E, R(60));
  197. P(E, A, B, C, D, R(61));
  198. P(D, E, A, B, C, R(62));
  199. P(C, D, E, A, B, R(63));
  200. P(B, C, D, E, A, R(64));
  201. P(A, B, C, D, E, R(65));
  202. P(E, A, B, C, D, R(66));
  203. P(D, E, A, B, C, R(67));
  204. P(C, D, E, A, B, R(68));
  205. P(B, C, D, E, A, R(69));
  206. P(A, B, C, D, E, R(70));
  207. P(E, A, B, C, D, R(71));
  208. P(D, E, A, B, C, R(72));
  209. P(C, D, E, A, B, R(73));
  210. P(B, C, D, E, A, R(74));
  211. P(A, B, C, D, E, R(75));
  212. P(E, A, B, C, D, R(76));
  213. P(D, E, A, B, C, R(77));
  214. P(C, D, E, A, B, R(78));
  215. P(B, C, D, E, A, R(79));
  216. #undef K
  217. #undef F
  218. ctx->state[0] += A;
  219. ctx->state[1] += B;
  220. ctx->state[2] += C;
  221. ctx->state[3] += D;
  222. ctx->state[4] += E;
  223. }
  224. /*
  225. * SHA-1 process buffer
  226. */
  227. void sha1_update(sha1_context * ctx, const unsigned char *input, int ilen)
  228. {
  229. int fill;
  230. unsigned long left;
  231. if (ilen <= 0)
  232. return;
  233. left = ctx->total[0] & 0x3F;
  234. fill = 64 - left;
  235. ctx->total[0] += (unsigned long) ilen;
  236. ctx->total[0] &= 0xFFFFFFFF;
  237. if (ctx->total[0] < (unsigned long) ilen)
  238. ctx->total[1]++;
  239. if (left && ilen >= fill) {
  240. memcpy((void *) (ctx->buffer + left), (void *) input, fill);
  241. sha1_process(ctx, ctx->buffer);
  242. input += fill;
  243. ilen -= fill;
  244. left = 0;
  245. }
  246. while (ilen >= 64) {
  247. sha1_process(ctx, input);
  248. input += 64;
  249. ilen -= 64;
  250. }
  251. if (ilen > 0) {
  252. memcpy((void *) (ctx->buffer + left), (void *) input, ilen);
  253. }
  254. }
  255. static const unsigned char sha1_padding[64] = {
  256. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  257. 0, 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. };
  261. /*
  262. * SHA-1 final digest
  263. */
  264. void sha1_finish(sha1_context * ctx, unsigned char output[20])
  265. {
  266. unsigned long last, padn;
  267. unsigned long high, low;
  268. unsigned char msglen[8];
  269. high = (ctx->total[0] >> 29)
  270. | (ctx->total[1] << 3);
  271. low = (ctx->total[0] << 3);
  272. PUT_ULONG_BE(high, msglen, 0);
  273. PUT_ULONG_BE(low, msglen, 4);
  274. last = ctx->total[0] & 0x3F;
  275. padn = (last < 56) ? (56 - last) : (120 - last);
  276. sha1_update(ctx, (unsigned char *) sha1_padding, padn);
  277. sha1_update(ctx, msglen, 8);
  278. PUT_ULONG_BE(ctx->state[0], output, 0);
  279. PUT_ULONG_BE(ctx->state[1], output, 4);
  280. PUT_ULONG_BE(ctx->state[2], output, 8);
  281. PUT_ULONG_BE(ctx->state[3], output, 12);
  282. PUT_ULONG_BE(ctx->state[4], output, 16);
  283. }
  284. /*
  285. * output = SHA-1( input buffer )
  286. */
  287. void sha1(const unsigned char *input, int ilen, unsigned char output[20])
  288. {
  289. sha1_context ctx;
  290. sha1_starts(&ctx);
  291. sha1_update(&ctx, input, ilen);
  292. sha1_finish(&ctx, output);
  293. }
  294. /*
  295. * SHA-1 HMAC context setup
  296. */
  297. void sha1_hmac_starts(sha1_context * ctx, const unsigned char *key, int keylen)
  298. {
  299. int i;
  300. unsigned char sum[20];
  301. if (keylen > 64) {
  302. sha1(key, keylen, sum);
  303. keylen = 20;
  304. key = sum;
  305. }
  306. memset(ctx->ipad, 0x36, 64);
  307. memset(ctx->opad, 0x5C, 64);
  308. for (i = 0; i < keylen; i++) {
  309. ctx->ipad[i] = (unsigned char) (ctx->ipad[i] ^ key[i]);
  310. ctx->opad[i] = (unsigned char) (ctx->opad[i] ^ key[i]);
  311. }
  312. sha1_starts(ctx);
  313. sha1_update(ctx, ctx->ipad, 64);
  314. }
  315. /*
  316. * SHA-1 HMAC process buffer
  317. */
  318. void sha1_hmac_update(sha1_context * ctx, const unsigned char *input, int ilen)
  319. {
  320. sha1_update(ctx, input, ilen);
  321. }
  322. /*
  323. * SHA-1 HMAC final digest
  324. */
  325. void sha1_hmac_finish(sha1_context * ctx, unsigned char output[20])
  326. {
  327. unsigned char tmpbuf[20];
  328. sha1_finish(ctx, tmpbuf);
  329. sha1_starts(ctx);
  330. sha1_update(ctx, ctx->opad, 64);
  331. sha1_update(ctx, tmpbuf, 20);
  332. sha1_finish(ctx, output);
  333. }
  334. /*
  335. * SHA1 HMAC context reset
  336. */
  337. void sha1_hmac_reset(sha1_context * ctx)
  338. {
  339. sha1_starts(ctx);
  340. sha1_update(ctx, ctx->ipad, 64);
  341. }
  342. /*
  343. * output = HMAC-SHA-1( hmac key, input buffer )
  344. */
  345. void sha1_hmac(const unsigned char *key, int keylen,
  346. const unsigned char *input, int ilen, unsigned char output[20])
  347. {
  348. sha1_context ctx;
  349. sha1_hmac_starts(&ctx, key, keylen);
  350. sha1_hmac_update(&ctx, input, ilen);
  351. sha1_hmac_finish(&ctx, output);
  352. }
  353. #ifndef min
  354. #define min( a, b ) ( ((a) < (b)) ? (a) : (b) )
  355. #endif
  356. void PKCS5_PBKDF2_HMAC_SHA1(const unsigned char *password, size_t plen,
  357. const unsigned char *salt, size_t slen,
  358. const unsigned long iteration_count, const unsigned long key_length,
  359. unsigned char *output)
  360. {
  361. sha1_context ctx;
  362. sha1_starts(&ctx);
  363. // Size of the generated digest
  364. unsigned char md_size = 20;
  365. unsigned char md1[20];
  366. unsigned char work[20];
  367. unsigned long counter = 1;
  368. unsigned long generated_key_length = 0;
  369. while (generated_key_length < key_length) {
  370. // U1 ends up in md1 and work
  371. unsigned char c[4];
  372. c[0] = (counter >> 24) & 0xff;
  373. c[1] = (counter >> 16) & 0xff;
  374. c[2] = (counter >> 8) & 0xff;
  375. c[3] = (counter >> 0) & 0xff;
  376. sha1_hmac_starts(&ctx, password, plen);
  377. sha1_hmac_update(&ctx, salt, slen);
  378. sha1_hmac_update(&ctx, c, 4);
  379. sha1_hmac_finish(&ctx, md1);
  380. memcpy(work, md1, md_size);
  381. unsigned long ic = 1;
  382. for (ic = 1; ic < iteration_count; ic++) {
  383. // U2 ends up in md1
  384. sha1_hmac_starts(&ctx, password, plen);
  385. sha1_hmac_update(&ctx, md1, md_size);
  386. sha1_hmac_finish(&ctx, md1);
  387. // U1 xor U2
  388. unsigned long i = 0;
  389. for (i = 0; i < md_size; i++) {
  390. work[i] ^= md1[i];
  391. }
  392. // and so on until iteration_count
  393. }
  394. // Copy the generated bytes to the key
  395. unsigned long bytes_to_write =
  396. min((key_length - generated_key_length), md_size);
  397. memcpy(output + generated_key_length, work, bytes_to_write);
  398. generated_key_length += bytes_to_write;
  399. ++counter;
  400. }
  401. }
  402. #if defined(TEST)
  403. /*
  404. * FIPS-180-1 test vectors
  405. */
  406. static unsigned char sha1_test_buf[3][57] = {
  407. {"abc"},
  408. {"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"},
  409. {""}
  410. };
  411. static const int sha1_test_buflen[3] = {
  412. 3, 56, 1000
  413. };
  414. static const unsigned char sha1_test_sum[3][20] = {
  415. {0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
  416. 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D},
  417. {0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
  418. 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1},
  419. {0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
  420. 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F}
  421. };
  422. /*
  423. * RFC 2202 test vectors
  424. */
  425. static unsigned char sha1_hmac_test_key[7][26] = {
  426. {"\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B\x0B"
  427. "\x0B\x0B\x0B\x0B"},
  428. {"Jefe"},
  429. {"\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  430. "\xAA\xAA\xAA\xAA"},
  431. {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10"
  432. "\x11\x12\x13\x14\x15\x16\x17\x18\x19"},
  433. {"\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C\x0C"
  434. "\x0C\x0C\x0C\x0C"},
  435. {""}, /* 0xAA 80 times */
  436. {""}
  437. };
  438. static const int sha1_hmac_test_keylen[7] = {
  439. 20, 4, 20, 25, 20, 80, 80
  440. };
  441. static unsigned char sha1_hmac_test_buf[7][74] = {
  442. {"Hi There"},
  443. {"what do ya want for nothing?"},
  444. {"\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  445. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  446. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  447. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  448. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"},
  449. {"\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
  450. "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
  451. "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
  452. "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"
  453. "\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD\xCD"},
  454. {"Test With Truncation"},
  455. {"Test Using Larger Than Block-Size Key - Hash Key First"},
  456. {"Test Using Larger Than Block-Size Key and Larger"
  457. " Than One Block-Size Data"}
  458. };
  459. static const int sha1_hmac_test_buflen[7] = {
  460. 8, 28, 50, 50, 20, 54, 73
  461. };
  462. static const unsigned char sha1_hmac_test_sum[7][20] = {
  463. {0xB6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xE2, 0x8B,
  464. 0xC0, 0xB6, 0xFB, 0x37, 0x8C, 0x8E, 0xF1, 0x46, 0xBE, 0x00},
  465. {0xEF, 0xFC, 0xDF, 0x6A, 0xE5, 0xEB, 0x2F, 0xA2, 0xD2, 0x74,
  466. 0x16, 0xD5, 0xF1, 0x84, 0xDF, 0x9C, 0x25, 0x9A, 0x7C, 0x79},
  467. {0x12, 0x5D, 0x73, 0x42, 0xB9, 0xAC, 0x11, 0xCD, 0x91, 0xA3,
  468. 0x9A, 0xF4, 0x8A, 0xA1, 0x7B, 0x4F, 0x63, 0xF1, 0x75, 0xD3},
  469. {0x4C, 0x90, 0x07, 0xF4, 0x02, 0x62, 0x50, 0xC6, 0xBC, 0x84,
  470. 0x14, 0xF9, 0xBF, 0x50, 0xC8, 0x6C, 0x2D, 0x72, 0x35, 0xDA},
  471. {0x4C, 0x1A, 0x03, 0x42, 0x4B, 0x55, 0xE0, 0x7F, 0xE7, 0xF2,
  472. 0x7B, 0xE1},
  473. {0xAA, 0x4A, 0xE5, 0xE1, 0x52, 0x72, 0xD0, 0x0E, 0x95, 0x70,
  474. 0x56, 0x37, 0xCE, 0x8A, 0x3B, 0x55, 0xED, 0x40, 0x21, 0x12},
  475. {0xE8, 0xE9, 0x9D, 0x0F, 0x45, 0x23, 0x7D, 0x78, 0x6D, 0x6B,
  476. 0xBA, 0xA7, 0x96, 0x5C, 0x78, 0x08, 0xBB, 0xFF, 0x1A, 0x91}
  477. };
  478. typedef struct {
  479. char *t;
  480. char *p;
  481. int plen;
  482. char *s;
  483. int slen;
  484. int c;
  485. int dkLen;
  486. char dk[1024]; // Remember to set this to max dkLen
  487. } testvector;
  488. int do_test(testvector * tv)
  489. {
  490. printf("Started %s\n", tv->t);
  491. fflush(stdout);
  492. char *key = malloc(tv->dkLen);
  493. if (key == 0) {
  494. return -1;
  495. }
  496. PKCS5_PBKDF2_HMAC(tv->p, tv->plen, tv->s, tv->slen, tv->c,
  497. tv->dkLen, key);
  498. if (memcmp(tv->dk, key, tv->dkLen) != 0) {
  499. // Failed
  500. return -1;
  501. }
  502. return 0;
  503. }
  504. #ifdef DEBUG
  505. static void print_hex(unsigned char *str, int len)
  506. {
  507. int i;
  508. for (i = 0; i < len; ++i)
  509. printf("%02x", str[i]);
  510. printf("\n");
  511. }
  512. #endif
  513. /*
  514. * Checkup routine
  515. */
  516. int main(int argc,char * argv[])
  517. {
  518. int verbose = 1;
  519. int i, j, buflen;
  520. unsigned char buf[1024];
  521. unsigned char sha1sum[20];
  522. sha1_context ctx;
  523. /*
  524. * SHA-1
  525. */
  526. for (i = 0; i < 3; i++) {
  527. if (verbose != 0)
  528. printf(" SHA-1 test #%d: ", i + 1);
  529. sha1_starts(&ctx);
  530. if (i == 2) {
  531. memset(buf, 'a', buflen = 1000);
  532. for (j = 0; j < 1000; j++)
  533. sha1_update(&ctx, buf, buflen);
  534. } else
  535. sha1_update(&ctx, sha1_test_buf[i],
  536. sha1_test_buflen[i]);
  537. sha1_finish(&ctx, sha1sum);
  538. if (memcmp(sha1sum, sha1_test_sum[i], 20) != 0) {
  539. if (verbose != 0)
  540. printf("failed\n");
  541. return (1);
  542. }
  543. if (verbose != 0)
  544. printf("passed\n");
  545. }
  546. if (verbose != 0)
  547. printf("\n");
  548. for (i = 0; i < 7; i++) {
  549. if (verbose != 0)
  550. printf(" HMAC-SHA-1 test #%d: ", i + 1);
  551. if (i == 5 || i == 6) {
  552. memset(buf, '\xAA', buflen = 80);
  553. sha1_hmac_starts(&ctx, buf, buflen);
  554. } else
  555. sha1_hmac_starts(&ctx, sha1_hmac_test_key[i],
  556. sha1_hmac_test_keylen[i]);
  557. sha1_hmac_update(&ctx, sha1_hmac_test_buf[i],
  558. sha1_hmac_test_buflen[i]);
  559. sha1_hmac_finish(&ctx, sha1sum);
  560. buflen = (i == 4) ? 12 : 20;
  561. if (memcmp(sha1sum, sha1_hmac_test_sum[i], buflen) != 0) {
  562. if (verbose != 0)
  563. printf("failed\n");
  564. return (1);
  565. }
  566. if (verbose != 0)
  567. printf("passed\n");
  568. }
  569. if (verbose != 0)
  570. printf("\n");
  571. // Test vectors from RFC 6070
  572. testvector *tv = 0;
  573. int res = 0;
  574. /*
  575. Input:
  576. P = "password" (8 octets)
  577. S = "salt" (4 octets)
  578. c = 1
  579. dkLen = 20
  580. Output:
  581. DK = 0c 60 c8 0f 96 1f 0e 71
  582. f3 a9 b5 24 af 60 12 06
  583. 2f e0 37 a6 (20 octets)
  584. */
  585. testvector t1 = {
  586. "Test 1",
  587. "password", 8, "salt", 4, 1, 20,
  588. .dk = {0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, 0x71,
  589. 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, 0x12, 0x06,
  590. 0x2f, 0xe0, 0x37, 0xa6}
  591. };
  592. tv = &t1;
  593. res = do_test(tv);
  594. if (res != 0) {
  595. printf("%s failed\n", tv->t);
  596. return res;
  597. }
  598. /*
  599. Input:
  600. P = "password" (8 octets)
  601. S = "salt" (4 octets)
  602. c = 2
  603. dkLen = 20
  604. Output:
  605. DK = ea 6c 01 4d c7 2d 6f 8c
  606. cd 1e d9 2a ce 1d 41 f0
  607. d8 de 89 57 (20 octets)
  608. */
  609. testvector t2 = {
  610. "Test 2",
  611. "password", 8, "salt", 4, 2, 20,
  612. {0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, 0x8c,
  613. 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, 0x41, 0xf0,
  614. 0xd8, 0xde, 0x89, 0x57}
  615. };
  616. tv = &t2;
  617. res = do_test(tv);
  618. if (res != 0) {
  619. printf("%s failed\n", tv->t);
  620. return res;
  621. }
  622. /*
  623. Input:
  624. P = "password" (8 octets)
  625. S = "salt" (4 octets)
  626. c = 4096
  627. dkLen = 20
  628. Output:
  629. DK = 4b 00 79 01 b7 65 48 9a
  630. be ad 49 d9 26 f7 21 d0
  631. 65 a4 29 c1 (20 octets)
  632. */
  633. testvector t3 = {
  634. "Test 3",
  635. "password", 8, "salt", 4, 4096, 20,
  636. {0x4b, 0x00, 0x79, 0x01, 0xb7, 0x65, 0x48, 0x9a,
  637. 0xbe, 0xad, 0x49, 0xd9, 0x26, 0xf7, 0x21, 0xd0,
  638. 0x65, 0xa4, 0x29, 0xc1}
  639. };
  640. tv = &t3;
  641. res = do_test(tv);
  642. if (res != 0) {
  643. printf("%s failed\n", tv->t);
  644. return res;
  645. }
  646. /*
  647. Input:
  648. P = "password" (8 octets)
  649. S = "salt" (4 octets)
  650. c = 16777216
  651. dkLen = 20
  652. Output:
  653. DK = ee fe 3d 61 cd 4d a4 e4
  654. e9 94 5b 3d 6b a2 15 8c
  655. 26 34 e9 84 (20 octets)
  656. */
  657. testvector t4 = {
  658. "Test 4",
  659. "password", 8, "salt", 4, 16777216, 20,
  660. {0xee, 0xfe, 0x3d, 0x61, 0xcd, 0x4d, 0xa4, 0xe4,
  661. 0xe9, 0x94, 0x5b, 0x3d, 0x6b, 0xa2, 0x15, 0x8c,
  662. 0x26, 0x34, 0xe9, 0x84}
  663. };
  664. tv = &t4;
  665. // res = do_test(tv);
  666. if (res != 0) {
  667. printf("%s failed\n", tv->t);
  668. return res;
  669. }
  670. /*
  671. Input:
  672. P = "passwordPASSWORDpassword" (24 octets)
  673. S = "saltSALTsaltSALTsaltSALTsaltSALTsalt" (36 octets)
  674. c = 4096
  675. dkLen = 25
  676. Output:
  677. DK = 3d 2e ec 4f e4 1c 84 9b
  678. 80 c8 d8 36 62 c0 e4 4a
  679. 8b 29 1a 96 4c f2 f0 70
  680. 38 (25 octets)
  681. */
  682. testvector t5 = {
  683. "Test 5",
  684. "passwordPASSWORDpassword", 24,
  685. "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, 4096, 25,
  686. {0x3d, 0x2e, 0xec, 0x4f, 0xe4, 0x1c, 0x84, 0x9b,
  687. 0x80, 0xc8, 0xd8, 0x36, 0x62, 0xc0, 0xe4, 0x4a,
  688. 0x8b, 0x29, 0x1a, 0x96, 0x4c, 0xf2, 0xf0, 0x70,
  689. 0x38}
  690. };
  691. tv = &t5;
  692. res = do_test(tv);
  693. if (res != 0) {
  694. printf("%s failed\n", tv->t);
  695. return res;
  696. }
  697. /*
  698. Input:
  699. P = "pass\0word" (9 octets)
  700. S = "sa\0lt" (5 octets)
  701. c = 4096
  702. dkLen = 16
  703. Output:
  704. DK = 56 fa 6a a7 55 48 09 9d
  705. cc 37 d7 f0 34 25 e0 c3 (16 octets)
  706. */
  707. testvector t6 = {
  708. "Test 6",
  709. "pass\0word", 9, "sa\0lt", 5, 4096, 16,
  710. {0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
  711. 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3,
  712. }
  713. };
  714. tv = &t6;
  715. res = do_test(tv);
  716. if (res != 0) {
  717. printf("%s failed\n", tv->t);
  718. return res;
  719. }
  720. printf("All tests successful\n");
  721. return 0;
  722. }
  723. #endif
  724. /*
  725. int main()
  726. {
  727. }*/