scryptenc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. /*-
  2. * Copyright 2009 Colin Percival
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * This file was originally written by Colin Percival as part of the Tarsnap
  27. * online backup system.
  28. */
  29. #include "scrypt_platform.h"
  30. #include <sys/types.h>
  31. #include <sys/resource.h>
  32. #ifdef HAVE_SYSCTL_HW_USERMEM_ULONG
  33. #include <sys/sysctl.h>
  34. #endif
  35. #include <sys/time.h>
  36. #include <stdint.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <time.h>
  40. #include <openssl/aes.h>
  41. #include "crypto_aesctr.h"
  42. #include "scrypt.h"
  43. #include "sha256.h"
  44. #include "sysendian.h"
  45. #include "scryptenc.h"
  46. #define ENCBLOCK 64
  47. static int memtouse(size_t, double, size_t *);
  48. static int cpuperf(double *);
  49. static int pickparams(size_t, double, double,
  50. int *, uint32_t *, uint32_t *);
  51. static int checkparams(size_t, double, double, int, uint32_t, uint32_t);
  52. static int getsalt(uint8_t[32]);
  53. static int
  54. memtouse(size_t maxmem, double maxmemfrac, size_t * memlimit)
  55. {
  56. struct rlimit rl;
  57. size_t memavail;
  58. #ifdef HAVE_SYSCTL_HW_USERMEM_ULONG
  59. unsigned long usermem;
  60. size_t usermemlen;
  61. #endif
  62. /* Find the least of RLIMIT_AS, RLIMIT_DATA, and RLIMIT_RSS. */
  63. memavail = (size_t)(-1);
  64. if (getrlimit(RLIMIT_AS, &rl))
  65. return (1);
  66. if ((rl.rlim_cur != RLIM_INFINITY) &&
  67. ((size_t)rl.rlim_cur < memavail))
  68. memavail = rl.rlim_cur;
  69. if (getrlimit(RLIMIT_DATA, &rl))
  70. return (1);
  71. if ((rl.rlim_cur != RLIM_INFINITY) &&
  72. ((size_t)rl.rlim_cur < memavail))
  73. memavail = rl.rlim_cur;
  74. #ifdef RLIMIT_RSS
  75. if (getrlimit(RLIMIT_RSS, &rl))
  76. return (1);
  77. if ((rl.rlim_cur != RLIM_INFINITY) &&
  78. ((size_t)rl.rlim_cur < memavail))
  79. memavail = rl.rlim_cur;
  80. #endif
  81. #ifdef HAVE_SYSCTL_HW_USERMEM_ULONG
  82. usermemlen = sizeof(unsigned long);
  83. if (sysctlbyname("hw.usermem", &usermem, &usermemlen, NULL, 0))
  84. return (1);
  85. if (usermemlen != sizeof(unsigned long))
  86. return (1);
  87. if (usermem < memavail)
  88. memavail = usermem;
  89. #endif
  90. #ifdef DEBUG
  91. fprintf(stderr, "Lowest memory limit is %ju\n", memavail);
  92. #endif
  93. /* Only use the specified fraction of the available memory. */
  94. if ((maxmemfrac > 0.5) || (maxmemfrac == 0.0))
  95. maxmemfrac = 0.5;
  96. memavail = maxmemfrac * memavail;
  97. /* Don't use more than the specified maximum. */
  98. if ((maxmem > 0) && (memavail > maxmem))
  99. memavail = maxmem;
  100. /* But always allow at least 1 MiB. */
  101. if (memavail < 1048576)
  102. memavail = 1048576;
  103. #ifdef DEBUG
  104. fprintf(stderr, "Allowing up to %ju memory to be used\n", memavail);
  105. #endif
  106. /* Return limit via the provided pointer. */
  107. *memlimit = memavail;
  108. return (0);
  109. }
  110. static int
  111. cpuperf(double * opps)
  112. {
  113. clockid_t clk;
  114. struct timespec res;
  115. struct timespec st;
  116. struct timespec en;
  117. double resd, timd;
  118. uint64_t i = 0;
  119. /* If we have a virtual clock use that; otherwise, use realtime. */
  120. clk = CLOCK_REALTIME;
  121. #ifdef CLOCK_VIRTUAL
  122. clk = CLOCK_VIRTUAL;
  123. #endif
  124. /* Get the clock resolution. */
  125. if (clock_getres(clk, &res))
  126. return (2);
  127. resd = res.tv_sec + res.tv_nsec * 0.000000001;
  128. #ifdef DEBUG
  129. fprintf(stderr, "Clock resolution is %f\n", resd);
  130. #endif
  131. /* Loop until the clock ticks. */
  132. if (clock_gettime(clk, &st))
  133. return (2);
  134. do {
  135. /* Do an scrypt. */
  136. if (scrypt(NULL, 0, NULL, 0, 128, 1, 1, NULL, 0))
  137. return (3);
  138. /* Has the clock ticked? */
  139. if (clock_gettime(clk, &en))
  140. return (2);
  141. if ((en.tv_sec != st.tv_sec) || (en.tv_nsec != st.tv_nsec))
  142. break;
  143. } while (1);
  144. /* Could how many scryps we can do before the next tick. */
  145. st.tv_sec = en.tv_sec;
  146. st.tv_nsec = en.tv_nsec;
  147. do {
  148. /* Do an scrypt. */
  149. if (scrypt(NULL, 0, NULL, 0, 128, 1, 1, NULL, 0))
  150. return (3);
  151. /* We invoked the salsa20/8 core 512 times. */
  152. i += 512;
  153. /* Check if we have looped for long enough. */
  154. if (clock_gettime(clk, &en))
  155. return (2);
  156. timd = (en.tv_sec - st.tv_sec) +
  157. (en.tv_nsec - st.tv_nsec) * 0.000000001;
  158. if (timd > resd)
  159. break;
  160. } while (1);
  161. #ifdef DEBUG
  162. fprintf(stderr, "%ju salsa20/8 cores performed in %f seconds\n",
  163. (uintmax_t)i, timd);
  164. #endif
  165. /* We can do approximately i salsa20/8 cores per (en - st) time. */
  166. *opps = i / timd;
  167. return (0);
  168. }
  169. static int
  170. pickparams(size_t maxmem, double maxmemfrac, double maxtime,
  171. int * logN, uint32_t * r, uint32_t * p)
  172. {
  173. size_t memlimit;
  174. double opps;
  175. double opslimit;
  176. double maxN, maxrp;
  177. int rc;
  178. /* Figure out how much memory to use. */
  179. if (memtouse(maxmem, maxmemfrac, &memlimit))
  180. return (1);
  181. /* Figure out how fast the CPU is. */
  182. if ((rc = cpuperf(&opps)) != 0)
  183. return (rc);
  184. opslimit = opps * maxtime;
  185. #ifdef DEBUG
  186. fprintf(stderr, "Allowing up to %f salsa20/8 cores\n", opslimit);
  187. #endif
  188. /* Allow a minimum of 2^15 salsa20/8 cores. */
  189. if (opslimit < 32768)
  190. opslimit = 32768;
  191. /* Fix r = 8 for now. */
  192. *r = 8;
  193. /*
  194. * The memory limit requires that 128Nr <= memlimit, while the CPU
  195. * limit requires that 4Nrp <= opslimit. If opslimit < memlimit/32,
  196. * opslimit imposes the stronger limit on N.
  197. */
  198. if (opslimit < memlimit/32) {
  199. /* Set p = 1 and choose N based on the CPU limit. */
  200. *p = 1;
  201. maxN = opslimit / (*r * 4);
  202. for (*logN = 1; *logN < 63; *logN += 1) {
  203. if ((uint64_t)(1) << *logN > maxN / 2)
  204. break;
  205. }
  206. } else {
  207. /* Set N based on the memory limit. */
  208. maxN = memlimit / (*r * 128);
  209. for (*logN = 1; *logN < 63; *logN += 1) {
  210. if ((uint64_t)(1) << *logN > maxN / 2)
  211. break;
  212. }
  213. /* Choose p based on the CPU limit. */
  214. maxrp = (opslimit / 4) / ((uint64_t)(1) << *logN);
  215. if (maxrp > 0x3fffffff)
  216. maxrp = 0x3fffffff;
  217. *p = (uint32_t)(maxrp) / *r;
  218. }
  219. #ifdef DEBUG
  220. fprintf(stderr, "N = %zu r = %d p = %d\n",
  221. (size_t)(1) << *logN, (int)(*r), (int)(*p));
  222. #endif
  223. /* Success! */
  224. return (0);
  225. }
  226. static int
  227. checkparams(size_t maxmem, double maxmemfrac, double maxtime,
  228. int logN, uint32_t r, uint32_t p)
  229. {
  230. size_t memlimit;
  231. double opps;
  232. double opslimit;
  233. uint64_t N;
  234. int rc;
  235. /* Figure out the maximum amount of memory we can use. */
  236. if (memtouse(maxmem, maxmemfrac, &memlimit))
  237. return (1);
  238. /* Figure out how fast the CPU is. */
  239. if ((rc = cpuperf(&opps)) != 0)
  240. return (rc);
  241. opslimit = opps * maxtime;
  242. /* Sanity-check values. */
  243. if ((logN < 1) || (logN > 63))
  244. return (7);
  245. if ((uint64_t)(r) * (uint64_t)(p) >= 0x40000000)
  246. return (7);
  247. /* Check limits. */
  248. N = (uint64_t)(1) << logN;
  249. if ((memlimit / N) / r < 128)
  250. return (9);
  251. if ((opslimit / N) / (r * p) < 4)
  252. return (10);
  253. /* Success! */
  254. return (0);
  255. }
  256. static int
  257. getsalt(uint8_t salt[32])
  258. {
  259. FILE * f;
  260. /* Use /dev/urandom. */
  261. if ((f = fopen("/dev/urandom", "r")) == NULL)
  262. return (4);
  263. if (fread(salt, 32, 1, f) != 1)
  264. return (4);
  265. if (fclose(f))
  266. return (4);
  267. /* Success! */
  268. return (0);
  269. }
  270. static int
  271. scryptenc_setup(uint8_t header[96], uint8_t dk[64],
  272. const uint8_t * passwd, size_t passwdlen,
  273. size_t maxmem, double maxmemfrac, double maxtime)
  274. {
  275. uint8_t salt[32];
  276. uint8_t hbuf[32];
  277. int logN;
  278. uint64_t N;
  279. uint32_t r;
  280. uint32_t p;
  281. SHA256_CTX ctx;
  282. uint8_t * key_hmac = &dk[32];
  283. HMAC_SHA256_CTX hctx;
  284. int rc;
  285. /* Pick values for N, r, p. */
  286. if ((rc = pickparams(maxmem, maxmemfrac, maxtime,
  287. &logN, &r, &p)) != 0)
  288. return (rc);
  289. N = (uint64_t)(1) << logN;
  290. /* Get some salt. */
  291. if ((rc = getsalt(salt)) != 0)
  292. return (rc);
  293. /* Generate the derived keys. */
  294. if (scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64))
  295. return (3);
  296. /* Construct the file header. */
  297. memcpy(header, "scrypt", 6);
  298. header[6] = 0;
  299. header[7] = logN;
  300. be32enc(&header[8], r);
  301. be32enc(&header[12], p);
  302. memcpy(&header[16], salt, 32);
  303. /* Add header checksum. */
  304. SHA256_Init(&ctx);
  305. SHA256_Update(&ctx, header, 48);
  306. SHA256_Final(hbuf, &ctx);
  307. memcpy(&header[48], hbuf, 16);
  308. /* Add header signature (used for verifying password). */
  309. HMAC_SHA256_Init(&hctx, key_hmac, 32);
  310. HMAC_SHA256_Update(&hctx, header, 64);
  311. HMAC_SHA256_Final(hbuf, &hctx);
  312. memcpy(&header[64], hbuf, 32);
  313. /* Success! */
  314. return (0);
  315. }
  316. static int
  317. scryptdec_setup(const uint8_t header[96], uint8_t dk[64],
  318. const uint8_t * passwd, size_t passwdlen,
  319. size_t maxmem, double maxmemfrac, double maxtime)
  320. {
  321. uint8_t salt[32];
  322. uint8_t hbuf[32];
  323. int logN;
  324. uint32_t r;
  325. uint32_t p;
  326. uint64_t N;
  327. SHA256_CTX ctx;
  328. uint8_t * key_hmac = &dk[32];
  329. HMAC_SHA256_CTX hctx;
  330. int rc;
  331. /* Parse N, r, p, salt. */
  332. logN = header[7];
  333. r = be32dec(&header[8]);
  334. p = be32dec(&header[12]);
  335. memcpy(salt, &header[16], 32);
  336. /* Verify header checksum. */
  337. SHA256_Init(&ctx);
  338. SHA256_Update(&ctx, header, 48);
  339. SHA256_Final(hbuf, &ctx);
  340. if (memcmp(&header[48], hbuf, 16))
  341. return (7);
  342. /*
  343. * Check whether the provided parameters are valid and whether the
  344. * key derivation function can be computed within the allowed memory
  345. * and CPU time.
  346. */
  347. if ((rc = checkparams(maxmem, maxmemfrac, maxtime, logN, r, p)) != 0)
  348. return (rc);
  349. /* Compute the derived keys. */
  350. N = (uint64_t)(1) << logN;
  351. if (scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64))
  352. return (3);
  353. /* Check header signature (i.e., verify password). */
  354. HMAC_SHA256_Init(&hctx, key_hmac, 32);
  355. HMAC_SHA256_Update(&hctx, header, 64);
  356. HMAC_SHA256_Final(hbuf, &hctx);
  357. if (memcmp(hbuf, &header[64], 32))
  358. return (11);
  359. /* Success! */
  360. return (0);
  361. }
  362. /**
  363. * scryptenc_buf(inbuf, inbuflen, outbuf, passwd, passwdlen,
  364. * maxmem, maxmemfrac, maxtime):
  365. * Encrypt inbuflen bytes from inbuf, writing the resulting inbuflen + 128
  366. * bytes to outbuf.
  367. */
  368. int
  369. scryptenc_buf(const uint8_t * inbuf, size_t inbuflen, uint8_t * outbuf,
  370. const uint8_t * passwd, size_t passwdlen,
  371. size_t maxmem, double maxmemfrac, double maxtime)
  372. {
  373. uint8_t dk[64];
  374. uint8_t hbuf[32];
  375. uint8_t header[96];
  376. uint8_t * key_enc = dk;
  377. uint8_t * key_hmac = &dk[32];
  378. int rc;
  379. HMAC_SHA256_CTX hctx;
  380. AES_KEY key_enc_exp;
  381. struct crypto_aesctr * AES;
  382. /* Generate the header and derived key. */
  383. if ((rc = scryptenc_setup(header, dk, passwd, passwdlen,
  384. maxmem, maxmemfrac, maxtime)) != 0)
  385. return (rc);
  386. /* Copy header into output buffer. */
  387. memcpy(outbuf, header, 96);
  388. /* Encrypt data. */
  389. if (AES_set_encrypt_key(key_enc, 256, &key_enc_exp))
  390. return (5);
  391. if ((AES = crypto_aesctr_init(&key_enc_exp, 0)) == NULL)
  392. return (6);
  393. crypto_aesctr_stream(AES, inbuf, &outbuf[96], inbuflen);
  394. crypto_aesctr_free(AES);
  395. /* Add signature. */
  396. HMAC_SHA256_Init(&hctx, key_hmac, 32);
  397. HMAC_SHA256_Update(&hctx, outbuf, 96 + inbuflen);
  398. HMAC_SHA256_Final(hbuf, &hctx);
  399. memcpy(&outbuf[96 + inbuflen], hbuf, 32);
  400. /* Zero sensitive data. */
  401. memset(dk, 0, 64);
  402. memset(&key_enc_exp, 0, sizeof(AES_KEY));
  403. /* Success! */
  404. return (0);
  405. }
  406. /**
  407. * scryptdec_buf(inbuf, inbuflen, outbuf, outlen, passwd, passwdlen,
  408. * maxmem, maxmemfrac, maxtime):
  409. * Decrypt inbuflen bytes fro inbuf, writing the result into outbuf and the
  410. * decrypted data length to outlen. The allocated length of outbuf must
  411. * be at least inbuflen.
  412. */
  413. int
  414. scryptdec_buf(const uint8_t * inbuf, size_t inbuflen, uint8_t * outbuf,
  415. size_t * outlen, const uint8_t * passwd, size_t passwdlen,
  416. size_t maxmem, double maxmemfrac, double maxtime)
  417. {
  418. uint8_t hbuf[32];
  419. uint8_t dk[64];
  420. uint8_t * key_enc = dk;
  421. uint8_t * key_hmac = &dk[32];
  422. int rc;
  423. HMAC_SHA256_CTX hctx;
  424. AES_KEY key_enc_exp;
  425. struct crypto_aesctr * AES;
  426. /*
  427. * All versions of the scrypt format will start with "scrypt" and
  428. * have at least 7 bytes of header.
  429. */
  430. if ((inbuflen < 7) || (memcmp(inbuf, "scrypt", 6) != 0))
  431. return (7);
  432. /* Check the format. */
  433. if (inbuf[6] != 0)
  434. return (8);
  435. /* We must have at least 128 bytes. */
  436. if (inbuflen < 128)
  437. return (7);
  438. /* Parse the header and generate derived keys. */
  439. if ((rc = scryptdec_setup(inbuf, dk, passwd, passwdlen,
  440. maxmem, maxmemfrac, maxtime)) != 0)
  441. return (rc);
  442. /* Decrypt data. */
  443. if (AES_set_encrypt_key(key_enc, 256, &key_enc_exp))
  444. return (5);
  445. if ((AES = crypto_aesctr_init(&key_enc_exp, 0)) == NULL)
  446. return (6);
  447. crypto_aesctr_stream(AES, &inbuf[96], outbuf, inbuflen - 128);
  448. crypto_aesctr_free(AES);
  449. *outlen = inbuflen - 128;
  450. /* Verify signature. */
  451. HMAC_SHA256_Init(&hctx, key_hmac, 32);
  452. HMAC_SHA256_Update(&hctx, inbuf, inbuflen - 32);
  453. HMAC_SHA256_Final(hbuf, &hctx);
  454. if (memcmp(hbuf, &inbuf[inbuflen - 32], 32))
  455. return (7);
  456. /* Zero sensitive data. */
  457. memset(dk, 0, 64);
  458. memset(&key_enc_exp, 0, sizeof(AES_KEY));
  459. /* Success! */
  460. return (0);
  461. }
  462. /**
  463. * scryptenc_file(infile, outfile, passwd, passwdlen,
  464. * maxmem, maxmemfrac, maxtime):
  465. * Read a stream from infile and encrypt it, writing the resulting stream to
  466. * outfile.
  467. */
  468. int
  469. scryptenc_file(FILE * infile, FILE * outfile,
  470. const uint8_t * passwd, size_t passwdlen,
  471. size_t maxmem, double maxmemfrac, double maxtime)
  472. {
  473. uint8_t buf[ENCBLOCK];
  474. uint8_t dk[64];
  475. uint8_t hbuf[32];
  476. uint8_t header[96];
  477. uint8_t * key_enc = dk;
  478. uint8_t * key_hmac = &dk[32];
  479. size_t readlen;
  480. HMAC_SHA256_CTX hctx;
  481. AES_KEY key_enc_exp;
  482. struct crypto_aesctr * AES;
  483. int rc;
  484. /* Generate the header and derived key. */
  485. if ((rc = scryptenc_setup(header, dk, passwd, passwdlen,
  486. maxmem, maxmemfrac, maxtime)) != 0)
  487. return (rc);
  488. /* Hash and write the header. */
  489. HMAC_SHA256_Init(&hctx, key_hmac, 32);
  490. HMAC_SHA256_Update(&hctx, header, 96);
  491. if (fwrite(header, 96, 1, outfile) != 1)
  492. return (12);
  493. /*
  494. * Read blocks of data, encrypt them, and write them out; hash the
  495. * data as it is produced.
  496. */
  497. if (AES_set_encrypt_key(key_enc, 256, &key_enc_exp))
  498. return (5);
  499. if ((AES = crypto_aesctr_init(&key_enc_exp, 0)) == NULL)
  500. return (6);
  501. do {
  502. if ((readlen = fread(buf, 1, ENCBLOCK, infile)) == 0)
  503. break;
  504. crypto_aesctr_stream(AES, buf, buf, readlen);
  505. HMAC_SHA256_Update(&hctx, buf, readlen);
  506. if (fwrite(buf, 1, readlen, outfile) < readlen)
  507. return (12);
  508. } while (1);
  509. crypto_aesctr_free(AES);
  510. /* Did we exit the loop due to a read error? */
  511. if (ferror(infile))
  512. return (13);
  513. /* Compute the final HMAC and output it. */
  514. HMAC_SHA256_Final(hbuf, &hctx);
  515. if (fwrite(hbuf, 32, 1, outfile) != 1)
  516. return (12);
  517. /* Zero sensitive data. */
  518. memset(dk, 0, 64);
  519. memset(&key_enc_exp, 0, sizeof(AES_KEY));
  520. /* Success! */
  521. return (0);
  522. }
  523. /**
  524. * scryptdec_file(infile, outfile, passwd, passwdlen,
  525. * maxmem, maxmemfrac, maxtime):
  526. * Read a stream from infile and decrypt it, writing the resulting stream to
  527. * outfile.
  528. */
  529. int
  530. scryptdec_file(FILE * infile, FILE * outfile,
  531. const uint8_t * passwd, size_t passwdlen,
  532. size_t maxmem, double maxmemfrac, double maxtime)
  533. {
  534. uint8_t buf[ENCBLOCK + 32];
  535. uint8_t header[96];
  536. uint8_t hbuf[32];
  537. uint8_t dk[64];
  538. uint8_t * key_enc = dk;
  539. uint8_t * key_hmac = &dk[32];
  540. size_t buflen = 0;
  541. size_t readlen;
  542. HMAC_SHA256_CTX hctx;
  543. AES_KEY key_enc_exp;
  544. struct crypto_aesctr * AES;
  545. int rc;
  546. /*
  547. * Read the first 7 bytes of the file; all future version of scrypt
  548. * are guaranteed to have at least 7 bytes of header.
  549. */
  550. if (fread(header, 7, 1, infile) < 1) {
  551. if (ferror(infile))
  552. return (13);
  553. else
  554. return (7);
  555. }
  556. /* Do we have the right magic? */
  557. if (memcmp(header, "scrypt", 6))
  558. return (7);
  559. if (header[6] != 0)
  560. return (8);
  561. /*
  562. * Read another 89 bytes of the file; version 0 of the srypt file
  563. * format has a 96-byte header.
  564. */
  565. if (fread(&header[7], 89, 1, infile) < 1) {
  566. if (ferror(infile))
  567. return (13);
  568. else
  569. return (7);
  570. }
  571. /* Parse the header and generate derived keys. */
  572. if ((rc = scryptdec_setup(header, dk, passwd, passwdlen,
  573. maxmem, maxmemfrac, maxtime)) != 0)
  574. return (rc);
  575. /* Start hashing with the header. */
  576. HMAC_SHA256_Init(&hctx, key_hmac, 32);
  577. HMAC_SHA256_Update(&hctx, header, 96);
  578. /*
  579. * We don't know how long the encrypted data block is (we can't know,
  580. * since data can be streamed into 'scrypt enc') so we need to read
  581. * data and decrypt all of it except the final 32 bytes, then check
  582. * if that final 32 bytes is the correct signature.
  583. */
  584. if (AES_set_encrypt_key(key_enc, 256, &key_enc_exp))
  585. return (5);
  586. if ((AES = crypto_aesctr_init(&key_enc_exp, 0)) == NULL)
  587. return (6);
  588. do {
  589. /* Read data until we have more than 32 bytes of it. */
  590. if ((readlen = fread(&buf[buflen], 1,
  591. ENCBLOCK + 32 - buflen, infile)) == 0)
  592. break;
  593. buflen += readlen;
  594. if (buflen <= 32)
  595. continue;
  596. /*
  597. * Decrypt, hash, and output everything except the last 32
  598. * bytes out of what we have in our buffer.
  599. */
  600. HMAC_SHA256_Update(&hctx, buf, buflen - 32);
  601. crypto_aesctr_stream(AES, buf, buf, buflen - 32);
  602. if (fwrite(buf, 1, buflen - 32, outfile) < buflen - 32)
  603. return (12);
  604. /* Move the last 32 bytes to the start of the buffer. */
  605. memmove(buf, &buf[buflen - 32], 32);
  606. buflen = 32;
  607. } while (1);
  608. crypto_aesctr_free(AES);
  609. /* Did we exit the loop due to a read error? */
  610. if (ferror(infile))
  611. return (13);
  612. /* Did we read enough data that we *might* have a valid signature? */
  613. if (buflen < 32)
  614. return (7);
  615. /* Verify signature. */
  616. HMAC_SHA256_Final(hbuf, &hctx);
  617. if (memcmp(hbuf, buf, 32))
  618. return (7);
  619. /* Zero sensitive data. */
  620. memset(dk, 0, 64);
  621. memset(&key_enc_exp, 0, sizeof(AES_KEY));
  622. return (0);
  623. }