pwdbased.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /* pwdbased.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifndef NO_PWDBASED
  26. #if FIPS_VERSION3_GE(6,0,0)
  27. /* set NO_WRAPPERS before headers, use direct internal f()s not wrappers */
  28. #define FIPS_NO_WRAPPERS
  29. #ifdef USE_WINDOWS_API
  30. #pragma code_seg(".fipsA$h")
  31. #pragma const_seg(".fipsB$h")
  32. #endif
  33. #endif
  34. #include <wolfssl/wolfcrypt/pwdbased.h>
  35. #include <wolfssl/wolfcrypt/hmac.h>
  36. #include <wolfssl/wolfcrypt/hash.h>
  37. #include <wolfssl/wolfcrypt/wolfmath.h>
  38. #include <wolfssl/wolfcrypt/error-crypt.h>
  39. #ifdef NO_INLINE
  40. #include <wolfssl/wolfcrypt/misc.h>
  41. #else
  42. #define WOLFSSL_MISC_INCLUDED
  43. #include <wolfcrypt/src/misc.c>
  44. #endif
  45. #if FIPS_VERSION3_GE(6,0,0)
  46. const unsigned int wolfCrypt_FIPS_pbkdf_ro_sanity[2] =
  47. { 0x1a2b3c4d, 0x00000010 };
  48. int wolfCrypt_FIPS_PBKDF_sanity(void)
  49. {
  50. return 0;
  51. }
  52. #endif
  53. #ifdef HAVE_PBKDF1
  54. /* PKCS#5 v1.5 with non standard extension to optionally derive the extra data (IV) */
  55. int wc_PBKDF1_ex(byte* key, int keyLen, byte* iv, int ivLen,
  56. const byte* passwd, int passwdLen, const byte* salt, int saltLen,
  57. int iterations, int hashType, void* heap)
  58. {
  59. int err;
  60. int keyLeft, ivLeft, i;
  61. int store;
  62. int keyOutput = 0;
  63. int digestLen;
  64. byte digest[WC_MAX_DIGEST_SIZE];
  65. #ifdef WOLFSSL_SMALL_STACK
  66. wc_HashAlg* hash = NULL;
  67. #else
  68. wc_HashAlg hash[1];
  69. #endif
  70. enum wc_HashType hashT;
  71. (void)heap;
  72. if (key == NULL || keyLen < 0 || passwdLen < 0 || saltLen < 0 || ivLen < 0){
  73. return BAD_FUNC_ARG;
  74. }
  75. if (iterations <= 0)
  76. iterations = 1;
  77. hashT = wc_HashTypeConvert(hashType);
  78. err = wc_HashGetDigestSize(hashT);
  79. if (err < 0)
  80. return err;
  81. digestLen = err;
  82. /* initialize hash */
  83. #ifdef WOLFSSL_SMALL_STACK
  84. hash = (wc_HashAlg*)XMALLOC(sizeof(wc_HashAlg), heap,
  85. DYNAMIC_TYPE_HASHCTX);
  86. if (hash == NULL)
  87. return MEMORY_E;
  88. #endif
  89. err = wc_HashInit_ex(hash, hashT, heap, INVALID_DEVID);
  90. if (err != 0) {
  91. #ifdef WOLFSSL_SMALL_STACK
  92. XFREE(hash, heap, DYNAMIC_TYPE_HASHCTX);
  93. #endif
  94. return err;
  95. }
  96. keyLeft = keyLen;
  97. ivLeft = ivLen;
  98. while (keyOutput < (keyLen + ivLen)) {
  99. int digestLeft = digestLen;
  100. /* D_(i - 1) */
  101. if (keyOutput) { /* first time D_0 is empty */
  102. err = wc_HashUpdate(hash, hashT, digest, (word32)digestLen);
  103. if (err != 0) break;
  104. }
  105. /* data */
  106. err = wc_HashUpdate(hash, hashT, passwd, (word32)passwdLen);
  107. if (err != 0) break;
  108. /* salt */
  109. if (salt) {
  110. err = wc_HashUpdate(hash, hashT, salt, (word32)saltLen);
  111. if (err != 0) break;
  112. }
  113. err = wc_HashFinal(hash, hashT, digest);
  114. if (err != 0) break;
  115. /* count */
  116. for (i = 1; i < iterations; i++) {
  117. err = wc_HashUpdate(hash, hashT, digest, (word32)digestLen);
  118. if (err != 0) break;
  119. err = wc_HashFinal(hash, hashT, digest);
  120. if (err != 0) break;
  121. }
  122. if (err != 0) break;
  123. if (keyLeft) {
  124. store = (int)min((word32)keyLeft, (word32)digestLen);
  125. XMEMCPY(&key[keyLen - keyLeft], digest, (size_t)store);
  126. keyOutput += store;
  127. keyLeft -= store;
  128. digestLeft -= store;
  129. }
  130. if (ivLeft && digestLeft) {
  131. store = (int)min((word32)ivLeft, (word32)digestLeft);
  132. if (iv != NULL)
  133. XMEMCPY(&iv[ivLen - ivLeft],
  134. &digest[digestLen - digestLeft], (size_t)store);
  135. keyOutput += store;
  136. ivLeft -= store;
  137. }
  138. }
  139. wc_HashFree(hash, hashT);
  140. #ifdef WOLFSSL_SMALL_STACK
  141. XFREE(hash, heap, DYNAMIC_TYPE_HASHCTX);
  142. #endif
  143. if (err != 0)
  144. return err;
  145. if (keyOutput != (keyLen + ivLen))
  146. return BUFFER_E;
  147. return err;
  148. }
  149. /* PKCS#5 v1.5 */
  150. int wc_PBKDF1(byte* output, const byte* passwd, int pLen, const byte* salt,
  151. int sLen, int iterations, int kLen, int hashType)
  152. {
  153. return wc_PBKDF1_ex(output, kLen, NULL, 0,
  154. passwd, pLen, salt, sLen, iterations, hashType, NULL);
  155. }
  156. #endif /* HAVE_PKCS5 */
  157. #if defined(HAVE_PBKDF2) && !defined(NO_HMAC)
  158. int wc_PBKDF2_ex(byte* output, const byte* passwd, int pLen, const byte* salt,
  159. int sLen, int iterations, int kLen, int hashType, void* heap, int devId)
  160. {
  161. int hLen;
  162. int ret;
  163. #ifdef WOLFSSL_SMALL_STACK
  164. byte* buffer;
  165. Hmac* hmac;
  166. #else
  167. byte buffer[WC_MAX_DIGEST_SIZE];
  168. Hmac hmac[1];
  169. #endif
  170. enum wc_HashType hashT;
  171. if (output == NULL || pLen < 0 || sLen < 0 || kLen < 0) {
  172. return BAD_FUNC_ARG;
  173. }
  174. if (iterations <= 0)
  175. iterations = 1;
  176. hashT = wc_HashTypeConvert(hashType);
  177. hLen = wc_HashGetDigestSize(hashT);
  178. if (hLen < 0)
  179. return BAD_FUNC_ARG;
  180. #ifdef WOLFSSL_SMALL_STACK
  181. buffer = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);
  182. if (buffer == NULL)
  183. return MEMORY_E;
  184. hmac = (Hmac*)XMALLOC(sizeof(Hmac), heap, DYNAMIC_TYPE_HMAC);
  185. if (hmac == NULL) {
  186. XFREE(buffer, heap, DYNAMIC_TYPE_TMP_BUFFER);
  187. return MEMORY_E;
  188. }
  189. #endif
  190. ret = wc_HmacInit(hmac, heap, devId);
  191. if (ret == 0) {
  192. word32 i = 1;
  193. /* use int hashType here, since HMAC FIPS uses the old unique value */
  194. #if FIPS_VERSION3_GE(6,0,0)
  195. {
  196. /* Allow passwords that are less than 14-bytes for compatibility
  197. * / interoperability, only since module v6.0.0 */
  198. int allowShortPasswd = 1;
  199. ret = wc_HmacSetKey_ex(hmac, hashType, passwd, (word32)pLen,
  200. allowShortPasswd);
  201. }
  202. #else
  203. ret = wc_HmacSetKey(hmac, hashType, passwd, (word32)pLen);
  204. #endif
  205. while (ret == 0 && kLen) {
  206. int currentLen;
  207. int j;
  208. ret = wc_HmacUpdate(hmac, salt, (word32)sLen);
  209. if (ret != 0)
  210. break;
  211. /* encode i */
  212. for (j = 0; j < 4; j++) {
  213. byte b = (byte)(i >> ((3-j) * 8));
  214. ret = wc_HmacUpdate(hmac, &b, 1);
  215. if (ret != 0)
  216. break;
  217. }
  218. /* check ret from inside for loop */
  219. if (ret != 0)
  220. break;
  221. ret = wc_HmacFinal(hmac, buffer);
  222. if (ret != 0)
  223. break;
  224. currentLen = (int)min((word32)kLen, (word32)hLen);
  225. XMEMCPY(output, buffer, (size_t)currentLen);
  226. for (j = 1; j < iterations; j++) {
  227. ret = wc_HmacUpdate(hmac, buffer, (word32)hLen);
  228. if (ret != 0)
  229. break;
  230. ret = wc_HmacFinal(hmac, buffer);
  231. if (ret != 0)
  232. break;
  233. xorbuf(output, buffer, (word32)currentLen);
  234. }
  235. /* check ret from inside for loop */
  236. if (ret != 0)
  237. break;
  238. output += currentLen;
  239. kLen -= currentLen;
  240. i++;
  241. }
  242. wc_HmacFree(hmac);
  243. }
  244. #ifdef WOLFSSL_SMALL_STACK
  245. XFREE(buffer, heap, DYNAMIC_TYPE_TMP_BUFFER);
  246. XFREE(hmac, heap, DYNAMIC_TYPE_HMAC);
  247. #endif
  248. return ret;
  249. }
  250. int wc_PBKDF2(byte* output, const byte* passwd, int pLen, const byte* salt,
  251. int sLen, int iterations, int kLen, int hashType)
  252. {
  253. return wc_PBKDF2_ex(output, passwd, pLen, salt, sLen, iterations, kLen,
  254. hashType, NULL, INVALID_DEVID);
  255. }
  256. #endif /* HAVE_PBKDF2 && !NO_HMAC */
  257. #ifdef HAVE_PKCS12
  258. /* helper for PKCS12_PBKDF(), does hash operation */
  259. static int DoPKCS12Hash(int hashType, byte* buffer, word32 totalLen,
  260. byte* Ai, word32 u, int iterations)
  261. {
  262. int i;
  263. int ret = 0;
  264. #ifdef WOLFSSL_SMALL_STACK
  265. wc_HashAlg* hash = NULL;
  266. #else
  267. wc_HashAlg hash[1];
  268. #endif
  269. enum wc_HashType hashT;
  270. if (buffer == NULL || Ai == NULL) {
  271. return BAD_FUNC_ARG;
  272. }
  273. hashT = wc_HashTypeConvert(hashType);
  274. /* initialize hash */
  275. #ifdef WOLFSSL_SMALL_STACK
  276. hash = (wc_HashAlg*)XMALLOC(sizeof(wc_HashAlg), NULL,
  277. DYNAMIC_TYPE_HASHCTX);
  278. if (hash == NULL)
  279. return MEMORY_E;
  280. #endif
  281. ret = wc_HashInit(hash, hashT);
  282. if (ret != 0) {
  283. #ifdef WOLFSSL_SMALL_STACK
  284. XFREE(hash, NULL, DYNAMIC_TYPE_HASHCTX);
  285. #endif
  286. return ret;
  287. }
  288. ret = wc_HashUpdate(hash, hashT, buffer, totalLen);
  289. if (ret == 0)
  290. ret = wc_HashFinal(hash, hashT, Ai);
  291. for (i = 1; i < iterations; i++) {
  292. if (ret == 0)
  293. ret = wc_HashUpdate(hash, hashT, Ai, u);
  294. if (ret == 0)
  295. ret = wc_HashFinal(hash, hashT, Ai);
  296. }
  297. wc_HashFree(hash, hashT);
  298. #ifdef WOLFSSL_SMALL_STACK
  299. XFREE(hash, NULL, DYNAMIC_TYPE_HASHCTX);
  300. #endif
  301. return ret;
  302. }
  303. int wc_PKCS12_PBKDF(byte* output, const byte* passwd, int passLen,
  304. const byte* salt, int saltLen, int iterations, int kLen, int hashType,
  305. int id)
  306. {
  307. return wc_PKCS12_PBKDF_ex(output, passwd, passLen, salt, saltLen,
  308. iterations, kLen, hashType, id, NULL);
  309. }
  310. /* extended API that allows a heap hint to be used */
  311. int wc_PKCS12_PBKDF_ex(byte* output, const byte* passwd, int passLen,
  312. const byte* salt, int saltLen, int iterations, int kLen,
  313. int hashType, int id, void* heap)
  314. {
  315. /* all in bytes instead of bits */
  316. word32 u, v, dLen, pLen, iLen, sLen, totalLen;
  317. int dynamic = 0;
  318. int ret = 0;
  319. word32 i;
  320. byte *D, *S, *P, *I;
  321. #ifdef WOLFSSL_SMALL_STACK
  322. byte staticBuffer[1]; /* force dynamic usage */
  323. #else
  324. byte staticBuffer[1024];
  325. #endif
  326. byte* buffer = staticBuffer;
  327. #ifdef WOLFSSL_SMALL_STACK
  328. byte* Ai = NULL;
  329. byte* B = NULL;
  330. mp_int *B1 = NULL;
  331. mp_int *i1 = NULL;
  332. mp_int *res = NULL;
  333. #else
  334. byte Ai[WC_MAX_DIGEST_SIZE];
  335. byte B[WC_MAX_BLOCK_SIZE];
  336. mp_int B1[1];
  337. mp_int i1[1];
  338. mp_int res[1];
  339. #endif
  340. enum wc_HashType hashT;
  341. (void)heap;
  342. if (output == NULL || passLen <= 0 || saltLen <= 0 || kLen < 0) {
  343. return BAD_FUNC_ARG;
  344. }
  345. if (iterations <= 0)
  346. iterations = 1;
  347. hashT = wc_HashTypeConvert(hashType);
  348. ret = wc_HashGetDigestSize(hashT);
  349. if (ret < 0)
  350. return ret;
  351. if (ret == 0)
  352. return BAD_STATE_E;
  353. u = (word32)ret;
  354. ret = wc_HashGetBlockSize(hashT);
  355. if (ret < 0)
  356. return ret;
  357. if (ret == 0)
  358. return BAD_STATE_E;
  359. v = (word32)ret;
  360. #ifdef WOLFSSL_SMALL_STACK
  361. Ai = (byte*)XMALLOC(WC_MAX_DIGEST_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);
  362. if (Ai == NULL)
  363. return MEMORY_E;
  364. B = (byte*)XMALLOC(WC_MAX_BLOCK_SIZE, heap, DYNAMIC_TYPE_TMP_BUFFER);
  365. if (B == NULL) {
  366. XFREE(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER);
  367. return MEMORY_E;
  368. }
  369. #endif
  370. XMEMSET(Ai, 0, WC_MAX_DIGEST_SIZE);
  371. XMEMSET(B, 0, WC_MAX_BLOCK_SIZE);
  372. dLen = v;
  373. sLen = v * (((word32)saltLen + v - 1) / v);
  374. /* with passLen checked at the top of the function for >= 0 then passLen
  375. * must be 1 or greater here and is always 'true' */
  376. pLen = v * (((word32)passLen + v - 1) / v);
  377. iLen = sLen + pLen;
  378. totalLen = dLen + sLen + pLen;
  379. if (totalLen > sizeof(staticBuffer)) {
  380. buffer = (byte*)XMALLOC(totalLen, heap, DYNAMIC_TYPE_KEY);
  381. if (buffer == NULL) {
  382. #ifdef WOLFSSL_SMALL_STACK
  383. XFREE(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER);
  384. XFREE(B, heap, DYNAMIC_TYPE_TMP_BUFFER);
  385. #endif
  386. return MEMORY_E;
  387. }
  388. dynamic = 1;
  389. }
  390. D = buffer;
  391. S = D + dLen;
  392. P = S + sLen;
  393. I = S;
  394. XMEMSET(D, id, dLen);
  395. for (i = 0; i < sLen; i++)
  396. S[i] = salt[i % (word32)saltLen];
  397. for (i = 0; i < pLen; i++)
  398. P[i] = passwd[i % (word32)passLen];
  399. #ifdef WOLFSSL_SMALL_STACK
  400. if (((B1 = (mp_int *)XMALLOC(sizeof(*B1), heap, DYNAMIC_TYPE_TMP_BUFFER))
  401. == NULL) ||
  402. ((i1 = (mp_int *)XMALLOC(sizeof(*i1), heap, DYNAMIC_TYPE_TMP_BUFFER))
  403. == NULL) ||
  404. ((res = (mp_int *)XMALLOC(sizeof(*res), heap, DYNAMIC_TYPE_TMP_BUFFER))
  405. == NULL)) {
  406. ret = MEMORY_E;
  407. goto out;
  408. }
  409. #endif
  410. while (kLen > 0) {
  411. word32 currentLen;
  412. ret = DoPKCS12Hash(hashType, buffer, totalLen, Ai, u, iterations);
  413. if (ret < 0)
  414. break;
  415. for (i = 0; i < v; i++)
  416. B[i] = Ai[(word32)i % u];
  417. if (mp_init(B1) != MP_OKAY)
  418. ret = MP_INIT_E;
  419. else if (mp_read_unsigned_bin(B1, B, v) != MP_OKAY)
  420. ret = MP_READ_E;
  421. else if (mp_add_d(B1, (mp_digit)1, B1) != MP_OKAY)
  422. ret = MP_ADD_E;
  423. if (ret != 0) {
  424. mp_clear(B1);
  425. break;
  426. }
  427. for (i = 0; i < iLen; i += v) {
  428. int outSz;
  429. if (mp_init_multi(i1, res, NULL, NULL, NULL, NULL) != MP_OKAY) {
  430. ret = MP_INIT_E;
  431. break;
  432. }
  433. if (mp_read_unsigned_bin(i1, I + i, v) != MP_OKAY)
  434. ret = MP_READ_E;
  435. else if (mp_add(i1, B1, res) != MP_OKAY)
  436. ret = MP_ADD_E;
  437. else if ( (outSz = mp_unsigned_bin_size(res)) < 0)
  438. ret = MP_TO_E;
  439. else {
  440. if (outSz > (int)v) {
  441. /* take off MSB */
  442. byte tmp[WC_MAX_BLOCK_SIZE + 1];
  443. ret = mp_to_unsigned_bin(res, tmp);
  444. XMEMCPY(I + i, tmp + 1, v);
  445. }
  446. else if (outSz < (int)v) {
  447. XMEMSET(I + i, 0, v - (word32)outSz);
  448. ret = mp_to_unsigned_bin(res, I + i + v - (word32)outSz);
  449. }
  450. else
  451. ret = mp_to_unsigned_bin(res, I + i);
  452. }
  453. mp_clear(i1);
  454. mp_clear(res);
  455. if (ret < 0) break;
  456. }
  457. if (ret < 0) {
  458. mp_clear(B1);
  459. break;
  460. }
  461. currentLen = min((word32)kLen, u);
  462. XMEMCPY(output, Ai, currentLen);
  463. output += currentLen;
  464. kLen -= (int)currentLen;
  465. mp_clear(B1);
  466. }
  467. #ifdef WOLFSSL_SMALL_STACK
  468. out:
  469. if (Ai != NULL)
  470. XFREE(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER);
  471. if (B != NULL)
  472. XFREE(B, heap, DYNAMIC_TYPE_TMP_BUFFER);
  473. if (B1 != NULL)
  474. XFREE(B1, heap, DYNAMIC_TYPE_TMP_BUFFER);
  475. if (i1 != NULL)
  476. XFREE(i1, heap, DYNAMIC_TYPE_TMP_BUFFER);
  477. if (res != NULL)
  478. XFREE(res, heap, DYNAMIC_TYPE_TMP_BUFFER);
  479. #endif
  480. if (dynamic)
  481. XFREE(buffer, heap, DYNAMIC_TYPE_KEY);
  482. return ret;
  483. }
  484. #endif /* HAVE_PKCS12 */
  485. #ifdef HAVE_SCRYPT
  486. #ifdef NO_HMAC
  487. #error scrypt requires HMAC
  488. #endif
  489. /* Rotate the 32-bit value a by b bits to the left.
  490. *
  491. * a 32-bit value.
  492. * b Number of bits to rotate.
  493. * returns rotated value.
  494. */
  495. #define R(a, b) rotlFixed(a, b)
  496. /* (2^32 - 1) */
  497. #define SCRYPT_WORD32_MAX 4294967295U
  498. /* One round of Salsa20/8.
  499. * Code taken from RFC 7914: scrypt PBKDF.
  500. *
  501. * out Output buffer.
  502. * in Input data to hash.
  503. */
  504. static void scryptSalsa(word32* out, word32* in)
  505. {
  506. int i;
  507. word32 x[16];
  508. #ifdef LITTLE_ENDIAN_ORDER
  509. XMEMCPY(x, in, sizeof(x));
  510. #else
  511. for (i = 0; i < 16; i++)
  512. x[i] = ByteReverseWord32(in[i]);
  513. #endif
  514. for (i = 8; i > 0; i -= 2) {
  515. x[ 4] ^= R(x[ 0] + x[12], 7); x[ 8] ^= R(x[ 4] + x[ 0], 9);
  516. x[12] ^= R(x[ 8] + x[ 4], 13); x[ 0] ^= R(x[12] + x[ 8], 18);
  517. x[ 9] ^= R(x[ 5] + x[ 1], 7); x[13] ^= R(x[ 9] + x[ 5], 9);
  518. x[ 1] ^= R(x[13] + x[ 9], 13); x[ 5] ^= R(x[ 1] + x[13], 18);
  519. x[14] ^= R(x[10] + x[ 6], 7); x[ 2] ^= R(x[14] + x[10], 9);
  520. x[ 6] ^= R(x[ 2] + x[14], 13); x[10] ^= R(x[ 6] + x[ 2], 18);
  521. x[ 3] ^= R(x[15] + x[11], 7); x[ 7] ^= R(x[ 3] + x[15], 9);
  522. x[11] ^= R(x[ 7] + x[ 3], 13); x[15] ^= R(x[11] + x[ 7], 18);
  523. x[ 1] ^= R(x[ 0] + x[ 3], 7); x[ 2] ^= R(x[ 1] + x[ 0], 9);
  524. x[ 3] ^= R(x[ 2] + x[ 1], 13); x[ 0] ^= R(x[ 3] + x[ 2], 18);
  525. x[ 6] ^= R(x[ 5] + x[ 4], 7); x[ 7] ^= R(x[ 6] + x[ 5], 9);
  526. x[ 4] ^= R(x[ 7] + x[ 6], 13); x[ 5] ^= R(x[ 4] + x[ 7], 18);
  527. x[11] ^= R(x[10] + x[ 9], 7); x[ 8] ^= R(x[11] + x[10], 9);
  528. x[ 9] ^= R(x[ 8] + x[11], 13); x[10] ^= R(x[ 9] + x[ 8], 18);
  529. x[12] ^= R(x[15] + x[14], 7); x[13] ^= R(x[12] + x[15], 9);
  530. x[14] ^= R(x[13] + x[12], 13); x[15] ^= R(x[14] + x[13], 18);
  531. }
  532. #ifdef LITTLE_ENDIAN_ORDER
  533. for (i = 0; i < 16; ++i)
  534. out[i] = in[i] + x[i];
  535. #else
  536. for (i = 0; i < 16; i++)
  537. out[i] = ByteReverseWord32(ByteReverseWord32(in[i]) + x[i]);
  538. #endif
  539. }
  540. /* Mix a block using Salsa20/8.
  541. * Based on RFC 7914: scrypt PBKDF.
  542. *
  543. * b Blocks to mix.
  544. * y Temporary storage.
  545. * r Size of the block.
  546. */
  547. static void scryptBlockMix(byte* b, byte* y, int r)
  548. {
  549. #ifdef WORD64_AVAILABLE
  550. word64 x[8];
  551. word64* b64 = (word64*)b;
  552. word64* y64 = (word64*)y;
  553. #else
  554. word32 x[16];
  555. word32* b32 = (word32*)b;
  556. word32* y32 = (word32*)y;
  557. #endif
  558. int i;
  559. int j;
  560. /* Step 1. */
  561. XMEMCPY(x, b + (2 * r - 1) * 64, sizeof(x));
  562. /* Step 2. */
  563. for (i = 0; i < 2 * r; i++)
  564. {
  565. #ifdef WORD64_AVAILABLE
  566. for (j = 0; j < 8; j++)
  567. x[j] ^= b64[i * 8 + j];
  568. #else
  569. for (j = 0; j < 16; j++)
  570. x[j] ^= b32[i * 16 + j];
  571. #endif
  572. scryptSalsa((word32*)x, (word32*)x);
  573. XMEMCPY(y + i * 64, x, sizeof(x));
  574. }
  575. /* Step 3. */
  576. for (i = 0; i < r; i++) {
  577. #ifdef WORD64_AVAILABLE
  578. for (j = 0; j < 8; j++) {
  579. b64[i * 8 + j] = y64[2 * i * 8 + j];
  580. b64[(r + i) * 8 + j] = y64[(2 * i + 1) * 8 + j];
  581. }
  582. #else
  583. for (j = 0; j < 16; j++) {
  584. b32[i * 16 + j] = y32[2 * i * 16 + j];
  585. b32[(r + i) * 16 + j] = y32[(2 * i + 1) * 16 + j];
  586. }
  587. #endif
  588. }
  589. }
  590. /* Random oracles mix.
  591. * Based on RFC 7914: scrypt PBKDF.
  592. *
  593. * x Data to mix.
  594. * v Temporary buffer.
  595. * y Temporary buffer for the block mix.
  596. * r Block size parameter.
  597. * n CPU/Memory cost parameter.
  598. */
  599. static void scryptROMix(byte* x, byte* v, byte* y, int r, word32 n)
  600. {
  601. word32 i;
  602. word32 j;
  603. word32 k;
  604. word32 bSz = (word32)(128 * r);
  605. #ifdef WORD64_AVAILABLE
  606. word64* x64 = (word64*)x;
  607. word64* v64 = (word64*)v;
  608. #else
  609. word32* x32 = (word32*)x;
  610. word32* v32 = (word32*)v;
  611. #endif
  612. /* Step 1. X = B (B not needed therefore not implemented) */
  613. /* Step 2. */
  614. for (i = 0; i < n; i++)
  615. {
  616. XMEMCPY(v + i * bSz, x, bSz);
  617. scryptBlockMix(x, y, r);
  618. }
  619. /* Step 3. */
  620. for (i = 0; i < n; i++)
  621. {
  622. #ifdef LITTLE_ENDIAN_ORDER
  623. #ifdef WORD64_AVAILABLE
  624. j = (word32)(*(word64*)(x + (2*r - 1) * 64) & (n-1));
  625. #else
  626. j = *(word32*)(x + (2*r - 1) * 64) & (n-1);
  627. #endif
  628. #else
  629. byte* t = x + (2*r - 1) * 64;
  630. j = (t[0] | (t[1] << 8) | (t[2] << 16) | ((word32)t[3] << 24)) & (n-1);
  631. #endif
  632. #ifdef WORD64_AVAILABLE
  633. for (k = 0; k < bSz / 8; k++)
  634. x64[k] ^= v64[j * bSz / 8 + k];
  635. #else
  636. for (k = 0; k < bSz / 4; k++)
  637. x32[k] ^= v32[j * bSz / 4 + k];
  638. #endif
  639. scryptBlockMix(x, y, r);
  640. }
  641. /* Step 4. B' = X (B = X = B' so not needed, therefore not implemented) */
  642. }
  643. /* Generates an key derived from a password and salt using a memory hard
  644. * algorithm.
  645. * Implements RFC 7914: scrypt PBKDF.
  646. *
  647. * output The derived key.
  648. * passwd The password to derive key from.
  649. * passLen The length of the password.
  650. * salt The key specific data.
  651. * saltLen The length of the salt data.
  652. * cost The CPU/memory cost parameter. Range: 1..(128*r/8-1)
  653. * (Iterations = 2^cost)
  654. * blockSize The number of 128 byte octets in a working block.
  655. * parallel The number of parallel mix operations to perform.
  656. * (Note: this implementation does not use threads.)
  657. * dkLen The length of the derived key in bytes.
  658. * returns BAD_FUNC_ARG when: blockSize is too large for cost.
  659. */
  660. int wc_scrypt(byte* output, const byte* passwd, int passLen,
  661. const byte* salt, int saltLen, int cost, int blockSize,
  662. int parallel, int dkLen)
  663. {
  664. int ret = 0;
  665. int i;
  666. byte* v = NULL;
  667. byte* y = NULL;
  668. byte* blocks = NULL;
  669. word32 blocksSz;
  670. word32 bSz;
  671. if (blockSize > 8)
  672. return BAD_FUNC_ARG;
  673. if (cost < 1 || cost >= 128 * blockSize / 8 || parallel < 1 || dkLen < 1)
  674. return BAD_FUNC_ARG;
  675. /* The following comparison used to be:
  676. * ((word32)parallel > (SCRYPT_MAX / (128 * blockSize)))
  677. * where SCRYPT_MAX is (2^32 - 1) * 32. For some compilers, the RHS of
  678. * the comparison is greater than parallel's type. It wouldn't promote
  679. * both sides to word64. What follows is just arithmetic simplification.
  680. */
  681. if (parallel > (int)((SCRYPT_WORD32_MAX / 4) / (word32)blockSize))
  682. return BAD_FUNC_ARG;
  683. bSz = 128 * (word32)blockSize;
  684. if (parallel > (int)(SCRYPT_WORD32_MAX / bSz))
  685. return BAD_FUNC_ARG;
  686. blocksSz = bSz * (word32)parallel;
  687. blocks = (byte*)XMALLOC((size_t)blocksSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  688. if (blocks == NULL) {
  689. ret = MEMORY_E;
  690. goto end;
  691. }
  692. /* Temporary for scryptROMix. */
  693. v = (byte*)XMALLOC((size_t)((1 << cost) * bSz), NULL,
  694. DYNAMIC_TYPE_TMP_BUFFER);
  695. if (v == NULL) {
  696. ret = MEMORY_E;
  697. goto end;
  698. }
  699. /* Temporary for scryptBlockMix. */
  700. y = (byte*)XMALLOC((size_t)(blockSize * 128), NULL,
  701. DYNAMIC_TYPE_TMP_BUFFER);
  702. if (y == NULL) {
  703. ret = MEMORY_E;
  704. goto end;
  705. }
  706. /* Step 1. */
  707. ret = wc_PBKDF2(blocks, passwd, passLen, salt, saltLen, 1, (int)blocksSz,
  708. WC_SHA256);
  709. if (ret != 0)
  710. goto end;
  711. /* Step 2. */
  712. for (i = 0; i < parallel; i++)
  713. scryptROMix(blocks + i * (int)bSz, v, y, (int)blockSize, 1 << cost);
  714. /* Step 3. */
  715. ret = wc_PBKDF2(output, passwd, passLen, blocks, (int)blocksSz, 1, dkLen,
  716. WC_SHA256);
  717. end:
  718. if (blocks != NULL)
  719. XFREE(blocks, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  720. if (v != NULL)
  721. XFREE(v, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  722. if (y != NULL)
  723. XFREE(y, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  724. return ret;
  725. }
  726. /* Generates an key derived from a password and salt using a memory hard
  727. * algorithm.
  728. * Implements RFC 7914: scrypt PBKDF.
  729. *
  730. * output Derived key.
  731. * passwd Password to derive key from.
  732. * passLen Length of the password.
  733. * salt Key specific data.
  734. * saltLen Length of the salt data.
  735. * iterations Number of iterations to perform. Range: 1 << (1..(128*r/8-1))
  736. * blockSize Number of 128 byte octets in a working block.
  737. * parallel Number of parallel mix operations to perform.
  738. * (Note: this implementation does not use threads.)
  739. * dkLen Length of the derived key in bytes.
  740. * returns BAD_FUNC_ARG when: iterations is not a power of 2 or blockSize is too
  741. * large for iterations.
  742. */
  743. int wc_scrypt_ex(byte* output, const byte* passwd, int passLen,
  744. const byte* salt, int saltLen, word32 iterations,
  745. int blockSize, int parallel, int dkLen)
  746. {
  747. int cost;
  748. /* Iterations must be a power of 2. */
  749. if ((iterations & (iterations - 1)) != 0)
  750. return BAD_FUNC_ARG;
  751. for (cost = -1; iterations != 0; cost++) {
  752. iterations >>= 1;
  753. }
  754. return wc_scrypt(output, passwd, passLen, salt, saltLen, cost, blockSize,
  755. parallel, dkLen);
  756. }
  757. #endif /* HAVE_SCRYPT */
  758. #endif /* NO_PWDBASED */