main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <inttypes.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <termios.h>
  32. #include <time.h>
  33. #include <unistd.h>
  34. #include "scrypt.h"
  35. #include "scryptenc.h"
  36. #include "sha256.h"
  37. #include "warn.h"
  38. #define dkLen 64
  39. #define MAXPASSLEN 2048
  40. static int
  41. readpass(char ** passwd, const char * prompt, const char * confirmprompt)
  42. {
  43. FILE * readfrom;
  44. char passbuf[MAXPASSLEN];
  45. char confpassbuf[MAXPASSLEN];
  46. struct termios term, term_old;
  47. int usingtty;
  48. /* Try to open /dev/tty. If that doesn't work, use stdin. */
  49. if ((readfrom = fopen("/dev/tty", "r")) == NULL)
  50. readfrom = stdin;
  51. /* If we're reading from a terminal, try to disable echo. */
  52. if ((usingtty = isatty(fileno(readfrom))) != 0) {
  53. if (tcgetattr(fileno(readfrom), &term_old)) {
  54. warn("Cannot read terminal settings");
  55. goto err1;
  56. }
  57. memcpy(&term, &term_old, sizeof(struct termios));
  58. term.c_lflag = (term.c_lflag & ~ECHO) | ECHONL;
  59. if (tcsetattr(fileno(readfrom), TCSANOW, &term)) {
  60. warn("Cannot set terminal settings");
  61. goto err1;
  62. }
  63. }
  64. retry:
  65. /* If we have a terminal, prompt the user to enter the password. */
  66. if (usingtty)
  67. fprintf(stderr, "%s: ", prompt);
  68. /* Read the password. */
  69. if (fgets(passbuf, MAXPASSLEN, readfrom) == NULL) {
  70. warn("Cannot read password");
  71. goto err2;
  72. }
  73. /* Confirm the password if necessary. */
  74. if (confirmprompt != NULL) {
  75. if (usingtty)
  76. fprintf(stderr, "%s: ", confirmprompt);
  77. if (fgets(confpassbuf, MAXPASSLEN, readfrom) == NULL) {
  78. warn("Cannot read password");
  79. goto err2;
  80. }
  81. if (strcmp(passbuf, confpassbuf)) {
  82. fprintf(stderr,
  83. "Passwords mismatch, please try again\n");
  84. goto retry;
  85. }
  86. }
  87. /* Terminate the string at the first "\r" or "\n" (if any). */
  88. passbuf[strcspn(passbuf, "\r\n")] = '\0';
  89. /* If we changed terminal settings, reset them. */
  90. if (usingtty)
  91. tcsetattr(fileno(readfrom), TCSANOW, &term_old);
  92. /* Close /dev/tty if we opened it. */
  93. if (readfrom != stdin)
  94. fclose(readfrom);
  95. /* Copy the password out. */
  96. if ((*passwd = strdup(passbuf)) == NULL) {
  97. warn("Cannot allocate memory");
  98. goto err1;
  99. }
  100. /* Zero any stored passwords. */
  101. memset(passbuf, 0, MAXPASSLEN);
  102. memset(confpassbuf, 0, MAXPASSLEN);
  103. /* Success! */
  104. return (0);
  105. err2:
  106. /* Reset terminal settings if necessary. */
  107. if (usingtty)
  108. tcsetattr(fileno(readfrom), TCSAFLUSH, &term_old);
  109. err1:
  110. /* Close /dev/tty if we opened it. */
  111. if (readfrom != stdin)
  112. fclose(readfrom);
  113. /* Failure! */
  114. return (-1);
  115. }
  116. static void
  117. usage(void)
  118. {
  119. fprintf(stderr,
  120. "usage: scrypt {enc | dec} [...] infile [outfile]\n");
  121. exit(1);
  122. }
  123. int
  124. main(int argc, char *argv[])
  125. {
  126. FILE * infile = NULL;
  127. FILE * outfile = stdout;
  128. int dec = 0;
  129. size_t maxmem = 0;
  130. double maxmemfrac = 0.5;
  131. double maxtime = 300.0;
  132. char ch;
  133. char * passwd;
  134. int rc;
  135. #ifdef NEED_WARN_PROGNAME
  136. warn_progname = "scrypt";
  137. #endif
  138. /* We should have "enc" or "dec" first. */
  139. if (argc < 2)
  140. usage();
  141. if (strcmp(argv[1], "enc") == 0) {
  142. maxmem = 0;
  143. maxmemfrac = 0.125;
  144. maxtime = 5.0;
  145. } else if (strcmp(argv[1], "dec") == 0) {
  146. dec = 1;
  147. } else
  148. usage();
  149. argc--;
  150. argv++;
  151. /* Parse arguments. */
  152. while ((ch = getopt(argc, argv, "hm:M:t:")) != -1) {
  153. switch (ch) {
  154. case 'M':
  155. maxmem = strtoumax(optarg, NULL, 0);
  156. break;
  157. case 'm':
  158. maxmemfrac = strtod(optarg, NULL);
  159. break;
  160. case 't':
  161. maxtime = strtod(optarg, NULL);
  162. break;
  163. default:
  164. usage();
  165. }
  166. }
  167. argc -= optind;
  168. argv += optind;
  169. /* We must have one or two parameters left. */
  170. if ((argc < 1) || (argc > 2))
  171. usage();
  172. /* Open the input file. */
  173. if ((infile = fopen(argv[0], "r")) == NULL) {
  174. warn("Cannot open input file: %s", argv[0]);
  175. exit(1);
  176. }
  177. /* If we have an output file, open it. */
  178. if (argc > 1) {
  179. if ((outfile = fopen(argv[1], "w")) == NULL) {
  180. warn("Cannot open output file: %s", argv[1]);
  181. exit(1);
  182. }
  183. }
  184. /* Prompt for a password. */
  185. if (readpass(&passwd, "Please enter passphrase",
  186. dec ? NULL : "Please confirm passphrase"))
  187. exit(1);
  188. /* Encrypt or decrypt. */
  189. if (dec)
  190. rc = scryptdec_file(infile, outfile, (uint8_t *)passwd,
  191. strlen(passwd), maxmem, maxmemfrac, maxtime);
  192. else
  193. rc = scryptenc_file(infile, outfile, (uint8_t *)passwd,
  194. strlen(passwd), maxmem, maxmemfrac, maxtime);
  195. /* If we failed, print the right error message and exit. */
  196. if (rc != 0) {
  197. switch (rc) {
  198. case 1:
  199. warn("Error determining amount of available memory");
  200. break;
  201. case 2:
  202. warn("Error reading clocks");
  203. break;
  204. case 3:
  205. warn("Error computing derived key");
  206. break;
  207. case 4:
  208. warn("Error reading salt");
  209. break;
  210. case 5:
  211. warn("OpenSSL error");
  212. break;
  213. case 6:
  214. warn("Error allocating memory");
  215. break;
  216. case 7:
  217. warnx("Input is not valid scrypt-encrypted block");
  218. break;
  219. case 8:
  220. warnx("Unrecognized scrypt format version");
  221. break;
  222. case 9:
  223. warnx("Decrypting file would require too much memory");
  224. break;
  225. case 10:
  226. warnx("Decrypting file would take too much CPU time");
  227. break;
  228. case 11:
  229. warnx("Passphrase is incorrect");
  230. break;
  231. case 12:
  232. warn("Error writing file: %s",
  233. (argc > 1) ? argv[1] : "standard output");
  234. break;
  235. case 13:
  236. warn("Error reading file: %s", argv[0]);
  237. break;
  238. }
  239. exit(1);
  240. }
  241. /* Zero and free the password. */
  242. memset(passwd, 0, strlen(passwd));
  243. free(passwd);
  244. return (0);
  245. }