scryptenc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 <stdint.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <openssl/aes.h>
  34. #include "crypto_aesctr.h"
  35. #include "memlimit.h"
  36. #include "scrypt.h"
  37. #include "scrypt_cpuperf.h"
  38. #include "sha256.h"
  39. #include "sysendian.h"
  40. #include "scryptenc.h"
  41. #define ENCBLOCK 65536
  42. static int pickparams(size_t, double, double,
  43. int *, uint32_t *, uint32_t *);
  44. static int checkparams(size_t, double, double, int, uint32_t, uint32_t);
  45. static int getsalt(uint8_t[32]);
  46. static int
  47. pickparams(size_t maxmem, double maxmemfrac, double maxtime,
  48. int * logN, uint32_t * r, uint32_t * p)
  49. {
  50. size_t memlimit;
  51. double opps;
  52. double opslimit;
  53. double maxN, maxrp;
  54. int rc;
  55. /* Figure out how much memory to use. */
  56. if (memtouse(maxmem, maxmemfrac, &memlimit))
  57. return (1);
  58. /* Figure out how fast the CPU is. */
  59. if ((rc = scrypt_cpuperf(&opps)) != 0)
  60. return (rc);
  61. opslimit = opps * maxtime;
  62. /* Allow a minimum of 2^15 salsa20/8 cores. */
  63. if (opslimit < 32768)
  64. opslimit = 32768;
  65. /* Fix r = 8 for now. */
  66. *r = 8;
  67. /*
  68. * The memory limit requires that 128Nr <= memlimit, while the CPU
  69. * limit requires that 4Nrp <= opslimit. If opslimit < memlimit/32,
  70. * opslimit imposes the stronger limit on N.
  71. */
  72. #ifdef DEBUG
  73. fprintf(stderr, "Requiring 128Nr <= %zu, 4Nrp <= %f\n",
  74. memlimit, opslimit);
  75. #endif
  76. if (opslimit < memlimit/32) {
  77. /* Set p = 1 and choose N based on the CPU limit. */
  78. *p = 1;
  79. maxN = opslimit / (*r * 4);
  80. for (*logN = 1; *logN < 63; *logN += 1) {
  81. if ((uint64_t)(1) << *logN > maxN / 2)
  82. break;
  83. }
  84. } else {
  85. /* Set N based on the memory limit. */
  86. maxN = memlimit / (*r * 128);
  87. for (*logN = 1; *logN < 63; *logN += 1) {
  88. if ((uint64_t)(1) << *logN > maxN / 2)
  89. break;
  90. }
  91. /* Choose p based on the CPU limit. */
  92. maxrp = (opslimit / 4) / ((uint64_t)(1) << *logN);
  93. if (maxrp > 0x3fffffff)
  94. maxrp = 0x3fffffff;
  95. *p = (uint32_t)(maxrp) / *r;
  96. }
  97. #ifdef DEBUG
  98. fprintf(stderr, "N = %zu r = %d p = %d\n",
  99. (size_t)(1) << *logN, (int)(*r), (int)(*p));
  100. #endif
  101. /* Success! */
  102. return (0);
  103. }
  104. static int
  105. checkparams(size_t maxmem, double maxmemfrac, double maxtime,
  106. int logN, uint32_t r, uint32_t p)
  107. {
  108. size_t memlimit;
  109. double opps;
  110. double opslimit;
  111. uint64_t N;
  112. int rc;
  113. /* Figure out the maximum amount of memory we can use. */
  114. if (memtouse(maxmem, maxmemfrac, &memlimit))
  115. return (1);
  116. /* Figure out how fast the CPU is. */
  117. if ((rc = scrypt_cpuperf(&opps)) != 0)
  118. return (rc);
  119. opslimit = opps * maxtime;
  120. /* Sanity-check values. */
  121. if ((logN < 1) || (logN > 63))
  122. return (7);
  123. if ((uint64_t)(r) * (uint64_t)(p) >= 0x40000000)
  124. return (7);
  125. /* Check limits. */
  126. N = (uint64_t)(1) << logN;
  127. if ((memlimit / N) / r < 128)
  128. return (9);
  129. if ((opslimit / N) / (r * p) < 4)
  130. return (10);
  131. /* Success! */
  132. return (0);
  133. }
  134. static int
  135. getsalt(uint8_t salt[32])
  136. {
  137. FILE * f;
  138. /* Use /dev/urandom. */
  139. if ((f = fopen("/dev/urandom", "r")) == NULL)
  140. return (4);
  141. if (fread(salt, 32, 1, f) != 1)
  142. return (4);
  143. if (fclose(f))
  144. return (4);
  145. /* Success! */
  146. return (0);
  147. }
  148. static int
  149. scryptenc_setup(uint8_t header[96], uint8_t dk[64],
  150. const uint8_t * passwd, size_t passwdlen,
  151. size_t maxmem, double maxmemfrac, double maxtime)
  152. {
  153. uint8_t salt[32];
  154. uint8_t hbuf[32];
  155. int logN;
  156. uint64_t N;
  157. uint32_t r;
  158. uint32_t p;
  159. SHA256_CTX ctx;
  160. uint8_t * key_hmac = &dk[32];
  161. HMAC_SHA256_CTX hctx;
  162. int rc;
  163. /* Pick values for N, r, p. */
  164. if ((rc = pickparams(maxmem, maxmemfrac, maxtime,
  165. &logN, &r, &p)) != 0)
  166. return (rc);
  167. N = (uint64_t)(1) << logN;
  168. /* Get some salt. */
  169. if ((rc = getsalt(salt)) != 0)
  170. return (rc);
  171. /* Generate the derived keys. */
  172. if (scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64))
  173. return (3);
  174. /* Construct the file header. */
  175. memcpy(header, "scrypt", 6);
  176. header[6] = 0;
  177. header[7] = logN;
  178. be32enc(&header[8], r);
  179. be32enc(&header[12], p);
  180. memcpy(&header[16], salt, 32);
  181. /* Add header checksum. */
  182. SHA256_Init(&ctx);
  183. SHA256_Update(&ctx, header, 48);
  184. SHA256_Final(hbuf, &ctx);
  185. memcpy(&header[48], hbuf, 16);
  186. /* Add header signature (used for verifying password). */
  187. HMAC_SHA256_Init(&hctx, key_hmac, 32);
  188. HMAC_SHA256_Update(&hctx, header, 64);
  189. HMAC_SHA256_Final(hbuf, &hctx);
  190. memcpy(&header[64], hbuf, 32);
  191. /* Success! */
  192. return (0);
  193. }
  194. static int
  195. scryptdec_setup(const uint8_t header[96], uint8_t dk[64],
  196. const uint8_t * passwd, size_t passwdlen,
  197. size_t maxmem, double maxmemfrac, double maxtime)
  198. {
  199. uint8_t salt[32];
  200. uint8_t hbuf[32];
  201. int logN;
  202. uint32_t r;
  203. uint32_t p;
  204. uint64_t N;
  205. SHA256_CTX ctx;
  206. uint8_t * key_hmac = &dk[32];
  207. HMAC_SHA256_CTX hctx;
  208. int rc;
  209. /* Parse N, r, p, salt. */
  210. logN = header[7];
  211. r = be32dec(&header[8]);
  212. p = be32dec(&header[12]);
  213. memcpy(salt, &header[16], 32);
  214. /* Verify header checksum. */
  215. SHA256_Init(&ctx);
  216. SHA256_Update(&ctx, header, 48);
  217. SHA256_Final(hbuf, &ctx);
  218. if (memcmp(&header[48], hbuf, 16))
  219. return (7);
  220. /*
  221. * Check whether the provided parameters are valid and whether the
  222. * key derivation function can be computed within the allowed memory
  223. * and CPU time.
  224. */
  225. if ((rc = checkparams(maxmem, maxmemfrac, maxtime, logN, r, p)) != 0)
  226. return (rc);
  227. /* Compute the derived keys. */
  228. N = (uint64_t)(1) << logN;
  229. if (scrypt(passwd, passwdlen, salt, 32, N, r, p, dk, 64))
  230. return (3);
  231. /* Check header signature (i.e., verify password). */
  232. HMAC_SHA256_Init(&hctx, key_hmac, 32);
  233. HMAC_SHA256_Update(&hctx, header, 64);
  234. HMAC_SHA256_Final(hbuf, &hctx);
  235. if (memcmp(hbuf, &header[64], 32))
  236. return (11);
  237. /* Success! */
  238. return (0);
  239. }
  240. /**
  241. * scryptenc_buf(inbuf, inbuflen, outbuf, passwd, passwdlen,
  242. * maxmem, maxmemfrac, maxtime):
  243. * Encrypt inbuflen bytes from inbuf, writing the resulting inbuflen + 128
  244. * bytes to outbuf.
  245. */
  246. int
  247. scryptenc_buf(const uint8_t * inbuf, size_t inbuflen, uint8_t * outbuf,
  248. const uint8_t * passwd, size_t passwdlen,
  249. size_t maxmem, double maxmemfrac, double maxtime)
  250. {
  251. uint8_t dk[64];
  252. uint8_t hbuf[32];
  253. uint8_t header[96];
  254. uint8_t * key_enc = dk;
  255. uint8_t * key_hmac = &dk[32];
  256. int rc;
  257. HMAC_SHA256_CTX hctx;
  258. AES_KEY key_enc_exp;
  259. struct crypto_aesctr * AES;
  260. /* Generate the header and derived key. */
  261. if ((rc = scryptenc_setup(header, dk, passwd, passwdlen,
  262. maxmem, maxmemfrac, maxtime)) != 0)
  263. return (rc);
  264. /* Copy header into output buffer. */
  265. memcpy(outbuf, header, 96);
  266. /* Encrypt data. */
  267. if (AES_set_encrypt_key(key_enc, 256, &key_enc_exp))
  268. return (5);
  269. if ((AES = crypto_aesctr_init(&key_enc_exp, 0)) == NULL)
  270. return (6);
  271. crypto_aesctr_stream(AES, inbuf, &outbuf[96], inbuflen);
  272. crypto_aesctr_free(AES);
  273. /* Add signature. */
  274. HMAC_SHA256_Init(&hctx, key_hmac, 32);
  275. HMAC_SHA256_Update(&hctx, outbuf, 96 + inbuflen);
  276. HMAC_SHA256_Final(hbuf, &hctx);
  277. memcpy(&outbuf[96 + inbuflen], hbuf, 32);
  278. /* Zero sensitive data. */
  279. memset(dk, 0, 64);
  280. memset(&key_enc_exp, 0, sizeof(AES_KEY));
  281. /* Success! */
  282. return (0);
  283. }
  284. /**
  285. * scryptdec_buf(inbuf, inbuflen, outbuf, outlen, passwd, passwdlen,
  286. * maxmem, maxmemfrac, maxtime):
  287. * Decrypt inbuflen bytes fro inbuf, writing the result into outbuf and the
  288. * decrypted data length to outlen. The allocated length of outbuf must
  289. * be at least inbuflen.
  290. */
  291. int
  292. scryptdec_buf(const uint8_t * inbuf, size_t inbuflen, uint8_t * outbuf,
  293. size_t * outlen, const uint8_t * passwd, size_t passwdlen,
  294. size_t maxmem, double maxmemfrac, double maxtime)
  295. {
  296. uint8_t hbuf[32];
  297. uint8_t dk[64];
  298. uint8_t * key_enc = dk;
  299. uint8_t * key_hmac = &dk[32];
  300. int rc;
  301. HMAC_SHA256_CTX hctx;
  302. AES_KEY key_enc_exp;
  303. struct crypto_aesctr * AES;
  304. /*
  305. * All versions of the scrypt format will start with "scrypt" and
  306. * have at least 7 bytes of header.
  307. */
  308. if ((inbuflen < 7) || (memcmp(inbuf, "scrypt", 6) != 0))
  309. return (7);
  310. /* Check the format. */
  311. if (inbuf[6] != 0)
  312. return (8);
  313. /* We must have at least 128 bytes. */
  314. if (inbuflen < 128)
  315. return (7);
  316. /* Parse the header and generate derived keys. */
  317. if ((rc = scryptdec_setup(inbuf, dk, passwd, passwdlen,
  318. maxmem, maxmemfrac, maxtime)) != 0)
  319. return (rc);
  320. /* Decrypt data. */
  321. if (AES_set_encrypt_key(key_enc, 256, &key_enc_exp))
  322. return (5);
  323. if ((AES = crypto_aesctr_init(&key_enc_exp, 0)) == NULL)
  324. return (6);
  325. crypto_aesctr_stream(AES, &inbuf[96], outbuf, inbuflen - 128);
  326. crypto_aesctr_free(AES);
  327. *outlen = inbuflen - 128;
  328. /* Verify signature. */
  329. HMAC_SHA256_Init(&hctx, key_hmac, 32);
  330. HMAC_SHA256_Update(&hctx, inbuf, inbuflen - 32);
  331. HMAC_SHA256_Final(hbuf, &hctx);
  332. if (memcmp(hbuf, &inbuf[inbuflen - 32], 32))
  333. return (7);
  334. /* Zero sensitive data. */
  335. memset(dk, 0, 64);
  336. memset(&key_enc_exp, 0, sizeof(AES_KEY));
  337. /* Success! */
  338. return (0);
  339. }
  340. /**
  341. * scryptenc_file(infile, outfile, passwd, passwdlen,
  342. * maxmem, maxmemfrac, maxtime):
  343. * Read a stream from infile and encrypt it, writing the resulting stream to
  344. * outfile.
  345. */
  346. int
  347. scryptenc_file(FILE * infile, FILE * outfile,
  348. const uint8_t * passwd, size_t passwdlen,
  349. size_t maxmem, double maxmemfrac, double maxtime)
  350. {
  351. uint8_t buf[ENCBLOCK];
  352. uint8_t dk[64];
  353. uint8_t hbuf[32];
  354. uint8_t header[96];
  355. uint8_t * key_enc = dk;
  356. uint8_t * key_hmac = &dk[32];
  357. size_t readlen;
  358. HMAC_SHA256_CTX hctx;
  359. AES_KEY key_enc_exp;
  360. struct crypto_aesctr * AES;
  361. int rc;
  362. /* Generate the header and derived key. */
  363. if ((rc = scryptenc_setup(header, dk, passwd, passwdlen,
  364. maxmem, maxmemfrac, maxtime)) != 0)
  365. return (rc);
  366. /* Hash and write the header. */
  367. HMAC_SHA256_Init(&hctx, key_hmac, 32);
  368. HMAC_SHA256_Update(&hctx, header, 96);
  369. if (fwrite(header, 96, 1, outfile) != 1)
  370. return (12);
  371. /*
  372. * Read blocks of data, encrypt them, and write them out; hash the
  373. * data as it is produced.
  374. */
  375. if (AES_set_encrypt_key(key_enc, 256, &key_enc_exp))
  376. return (5);
  377. if ((AES = crypto_aesctr_init(&key_enc_exp, 0)) == NULL)
  378. return (6);
  379. do {
  380. if ((readlen = fread(buf, 1, ENCBLOCK, infile)) == 0)
  381. break;
  382. crypto_aesctr_stream(AES, buf, buf, readlen);
  383. HMAC_SHA256_Update(&hctx, buf, readlen);
  384. if (fwrite(buf, 1, readlen, outfile) < readlen)
  385. return (12);
  386. } while (1);
  387. crypto_aesctr_free(AES);
  388. /* Did we exit the loop due to a read error? */
  389. if (ferror(infile))
  390. return (13);
  391. /* Compute the final HMAC and output it. */
  392. HMAC_SHA256_Final(hbuf, &hctx);
  393. if (fwrite(hbuf, 32, 1, outfile) != 1)
  394. return (12);
  395. /* Zero sensitive data. */
  396. memset(dk, 0, 64);
  397. memset(&key_enc_exp, 0, sizeof(AES_KEY));
  398. /* Success! */
  399. return (0);
  400. }
  401. /**
  402. * scryptdec_file(infile, outfile, passwd, passwdlen,
  403. * maxmem, maxmemfrac, maxtime):
  404. * Read a stream from infile and decrypt it, writing the resulting stream to
  405. * outfile.
  406. */
  407. int
  408. scryptdec_file(FILE * infile, FILE * outfile,
  409. const uint8_t * passwd, size_t passwdlen,
  410. size_t maxmem, double maxmemfrac, double maxtime)
  411. {
  412. uint8_t buf[ENCBLOCK + 32];
  413. uint8_t header[96];
  414. uint8_t hbuf[32];
  415. uint8_t dk[64];
  416. uint8_t * key_enc = dk;
  417. uint8_t * key_hmac = &dk[32];
  418. size_t buflen = 0;
  419. size_t readlen;
  420. HMAC_SHA256_CTX hctx;
  421. AES_KEY key_enc_exp;
  422. struct crypto_aesctr * AES;
  423. int rc;
  424. /*
  425. * Read the first 7 bytes of the file; all future version of scrypt
  426. * are guaranteed to have at least 7 bytes of header.
  427. */
  428. if (fread(header, 7, 1, infile) < 1) {
  429. if (ferror(infile))
  430. return (13);
  431. else
  432. return (7);
  433. }
  434. /* Do we have the right magic? */
  435. if (memcmp(header, "scrypt", 6))
  436. return (7);
  437. if (header[6] != 0)
  438. return (8);
  439. /*
  440. * Read another 89 bytes of the file; version 0 of the srypt file
  441. * format has a 96-byte header.
  442. */
  443. if (fread(&header[7], 89, 1, infile) < 1) {
  444. if (ferror(infile))
  445. return (13);
  446. else
  447. return (7);
  448. }
  449. /* Parse the header and generate derived keys. */
  450. if ((rc = scryptdec_setup(header, dk, passwd, passwdlen,
  451. maxmem, maxmemfrac, maxtime)) != 0)
  452. return (rc);
  453. /* Start hashing with the header. */
  454. HMAC_SHA256_Init(&hctx, key_hmac, 32);
  455. HMAC_SHA256_Update(&hctx, header, 96);
  456. /*
  457. * We don't know how long the encrypted data block is (we can't know,
  458. * since data can be streamed into 'scrypt enc') so we need to read
  459. * data and decrypt all of it except the final 32 bytes, then check
  460. * if that final 32 bytes is the correct signature.
  461. */
  462. if (AES_set_encrypt_key(key_enc, 256, &key_enc_exp))
  463. return (5);
  464. if ((AES = crypto_aesctr_init(&key_enc_exp, 0)) == NULL)
  465. return (6);
  466. do {
  467. /* Read data until we have more than 32 bytes of it. */
  468. if ((readlen = fread(&buf[buflen], 1,
  469. ENCBLOCK + 32 - buflen, infile)) == 0)
  470. break;
  471. buflen += readlen;
  472. if (buflen <= 32)
  473. continue;
  474. /*
  475. * Decrypt, hash, and output everything except the last 32
  476. * bytes out of what we have in our buffer.
  477. */
  478. HMAC_SHA256_Update(&hctx, buf, buflen - 32);
  479. crypto_aesctr_stream(AES, buf, buf, buflen - 32);
  480. if (fwrite(buf, 1, buflen - 32, outfile) < buflen - 32)
  481. return (12);
  482. /* Move the last 32 bytes to the start of the buffer. */
  483. memmove(buf, &buf[buflen - 32], 32);
  484. buflen = 32;
  485. } while (1);
  486. crypto_aesctr_free(AES);
  487. /* Did we exit the loop due to a read error? */
  488. if (ferror(infile))
  489. return (13);
  490. /* Did we read enough data that we *might* have a valid signature? */
  491. if (buflen < 32)
  492. return (7);
  493. /* Verify signature. */
  494. HMAC_SHA256_Final(hbuf, &hctx);
  495. if (memcmp(hbuf, buf, 32))
  496. return (7);
  497. /* Zero sensitive data. */
  498. memset(dk, 0, 64);
  499. memset(&key_enc_exp, 0, sizeof(AES_KEY));
  500. return (0);
  501. }