main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #include "platform.h"
  27. #include <math.h>
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "getopt.h"
  33. #include "humansize.h"
  34. #include "insecure_memzero.h"
  35. #include "parsenum.h"
  36. #include "readpass.h"
  37. #include "scryptenc.h"
  38. #include "warnp.h"
  39. static void
  40. usage(void)
  41. {
  42. fprintf(stderr,
  43. "usage: scrypt {enc | dec | info} [-f] [-M maxmem]"
  44. " [-m maxmemfrac]\n"
  45. " [-t maxtime] [-v] [-P] infile [outfile]\n"
  46. " scrypt --version\n");
  47. exit(1);
  48. }
  49. int
  50. main(int argc, char *argv[])
  51. {
  52. FILE * infile;
  53. FILE * outfile = stdout;
  54. int devtty = 1;
  55. int dec = 0;
  56. int info = 0;
  57. size_t maxmem = 0;
  58. int force_resources = 0;
  59. uint64_t maxmem64;
  60. double maxmemfrac = 0.5;
  61. double maxtime = 300.0;
  62. const char * ch;
  63. const char * infilename;
  64. const char * outfilename;
  65. char * passwd;
  66. int rc;
  67. int verbose = 0;
  68. struct scryptdec_file_cookie * C = NULL;
  69. WARNP_INIT;
  70. /* We should have "enc", "dec", or "info" first. */
  71. if (argc < 2)
  72. usage();
  73. if (strcmp(argv[1], "enc") == 0) {
  74. maxmem = 0;
  75. maxmemfrac = 0.125;
  76. maxtime = 5.0;
  77. } else if (strcmp(argv[1], "dec") == 0) {
  78. dec = 1;
  79. } else if (strcmp(argv[1], "info") == 0) {
  80. info = 1;
  81. } else if (strcmp(argv[1], "--version") == 0) {
  82. fprintf(stdout, "scrypt %s\n", PACKAGE_VERSION);
  83. exit(0);
  84. } else {
  85. warn0("First argument must be 'enc', 'dec', or 'info'.\n");
  86. usage();
  87. }
  88. argc--;
  89. argv++;
  90. /* Parse arguments. */
  91. while ((ch = GETOPT(argc, argv)) != NULL) {
  92. GETOPT_SWITCH(ch) {
  93. GETOPT_OPT("-f"):
  94. force_resources = 1;
  95. break;
  96. GETOPT_OPTARG("-M"):
  97. if (humansize_parse(optarg, &maxmem64)) {
  98. warn0("Could not parse the parameter to -M.");
  99. exit(1);
  100. }
  101. if (maxmem64 > SIZE_MAX) {
  102. warn0("The parameter to -M is too large.");
  103. exit(1);
  104. }
  105. maxmem = (size_t)maxmem64;
  106. break;
  107. GETOPT_OPTARG("-m"):
  108. if (PARSENUM(&maxmemfrac, optarg, 0, 1)) {
  109. warnp("Invalid option: -m %s", optarg);
  110. exit(1);
  111. }
  112. break;
  113. GETOPT_OPTARG("-t"):
  114. if (PARSENUM(&maxtime, optarg, 0, INFINITY)) {
  115. warnp("Invalid option: -t %s", optarg);
  116. exit(1);
  117. }
  118. break;
  119. GETOPT_OPT("-v"):
  120. verbose = 1;
  121. break;
  122. GETOPT_OPT("-P"):
  123. devtty = 0;
  124. break;
  125. GETOPT_MISSING_ARG:
  126. warn0("Missing argument to %s\n", ch);
  127. usage();
  128. GETOPT_DEFAULT:
  129. warn0("illegal option -- %s\n", ch);
  130. usage();
  131. }
  132. }
  133. argc -= optind;
  134. argv += optind;
  135. /* We must have one or two parameters left. */
  136. if ((argc < 1) || (argc > 2))
  137. usage();
  138. /* Set the input filename. */
  139. if (strcmp(argv[0], "-"))
  140. infilename = argv[0];
  141. else
  142. infilename = NULL;
  143. /* Set the output filename. */
  144. if (argc > 1)
  145. outfilename = argv[1];
  146. else
  147. outfilename = NULL;
  148. /* If the input isn't stdin, open the file. */
  149. if (infilename != NULL) {
  150. if ((infile = fopen(infilename, "rb")) == NULL) {
  151. warnp("Cannot open input file: %s", infilename);
  152. goto err0;
  153. }
  154. } else {
  155. infile = stdin;
  156. /* Error if given incompatible options. */
  157. if (devtty == 0) {
  158. warn0("Cannot read both passphrase and input file"
  159. " from standard input");
  160. goto err0;
  161. }
  162. }
  163. /* User selected 'info' mode. */
  164. if (info) {
  165. /* Print the encryption parameters used for the file. */
  166. rc = scryptdec_file_printparams(infile);
  167. /* Clean up. */
  168. if (infile != stdin)
  169. fclose(infile);
  170. /* Finished! */
  171. goto done;
  172. }
  173. /* Prompt for a password. */
  174. if (readpass(&passwd, "Please enter passphrase",
  175. (dec || !devtty) ? NULL : "Please confirm passphrase", devtty))
  176. goto err1;
  177. /*-
  178. * If we're decrypting, open the input file and process its header;
  179. * doing this here allows us to abort without creating an output
  180. * file if the input file does not have a valid scrypt header or if
  181. * we have the wrong passphrase.
  182. *
  183. * If successful, we get back a cookie containing the decryption
  184. * parameters (which we'll use after we open the output file).
  185. */
  186. if (dec) {
  187. if ((rc = scryptdec_file_prep(infile, (uint8_t *)passwd,
  188. strlen(passwd), maxmem, maxmemfrac, maxtime, verbose,
  189. force_resources, &C)) != 0) {
  190. goto cleanup;
  191. }
  192. }
  193. /* If we have an output file, open it. */
  194. if (outfilename != NULL) {
  195. if ((outfile = fopen(outfilename, "wb")) == NULL) {
  196. warnp("Cannot open output file: %s", outfilename);
  197. goto err2;
  198. }
  199. }
  200. /* Encrypt or decrypt. */
  201. if (dec)
  202. rc = scryptdec_file_copy(C, outfile);
  203. else
  204. rc = scryptenc_file(infile, outfile, (uint8_t *)passwd,
  205. strlen(passwd), maxmem, maxmemfrac, maxtime, verbose);
  206. cleanup:
  207. /* Free the decryption cookie, if any. */
  208. scryptdec_file_cookie_free(C);
  209. /* Zero and free the password. */
  210. insecure_memzero(passwd, strlen(passwd));
  211. free(passwd);
  212. /* Close any files we opened. */
  213. if (infile != stdin)
  214. fclose(infile);
  215. if (outfile != stdout)
  216. fclose(outfile);
  217. done:
  218. /* If we failed, print the right error message and exit. */
  219. if (rc != 0) {
  220. switch (rc) {
  221. case 1:
  222. warnp("Error determining amount of available memory");
  223. break;
  224. case 2:
  225. warnp("Error reading clocks");
  226. break;
  227. case 3:
  228. warnp("Error computing derived key");
  229. break;
  230. case 4:
  231. warnp("Error reading salt");
  232. break;
  233. case 5:
  234. warnp("OpenSSL error");
  235. break;
  236. case 6:
  237. warnp("Error allocating memory");
  238. break;
  239. case 7:
  240. warn0("Input is not valid scrypt-encrypted block");
  241. break;
  242. case 8:
  243. warn0("Unrecognized scrypt format version");
  244. break;
  245. case 9:
  246. warn0("Decrypting file would require too much memory");
  247. break;
  248. case 10:
  249. warn0("Decrypting file would take too much CPU time");
  250. break;
  251. case 11:
  252. warn0("Passphrase is incorrect");
  253. break;
  254. case 12:
  255. warnp("Error writing file: %s",
  256. (outfilename != NULL) ? outfilename
  257. : "standard output");
  258. break;
  259. case 13:
  260. warnp("Error reading file: %s",
  261. (infilename != NULL) ? infilename
  262. : "standard input");
  263. break;
  264. }
  265. goto err0;
  266. }
  267. /* Success! */
  268. return (0);
  269. err2:
  270. scryptdec_file_cookie_free(C);
  271. insecure_memzero(passwd, strlen(passwd));
  272. free(passwd);
  273. err1:
  274. if (infile != stdin)
  275. fclose(infile);
  276. err0:
  277. /* Failure! */
  278. exit(1);
  279. }