main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 "readpass.h"
  35. #include "scryptenc.h"
  36. #include "sha256.h"
  37. #include "warn.h"
  38. #define dkLen 64
  39. static void
  40. usage(void)
  41. {
  42. fprintf(stderr,
  43. "usage: scrypt {enc | dec} [...] infile [outfile]\n");
  44. exit(1);
  45. }
  46. int
  47. main(int argc, char *argv[])
  48. {
  49. FILE * infile = NULL;
  50. FILE * outfile = stdout;
  51. int dec = 0;
  52. size_t maxmem = 0;
  53. double maxmemfrac = 0.5;
  54. double maxtime = 300.0;
  55. char ch;
  56. char * passwd;
  57. int rc;
  58. #ifdef NEED_WARN_PROGNAME
  59. warn_progname = "scrypt";
  60. #endif
  61. /* We should have "enc" or "dec" first. */
  62. if (argc < 2)
  63. usage();
  64. if (strcmp(argv[1], "enc") == 0) {
  65. maxmem = 0;
  66. maxmemfrac = 0.125;
  67. maxtime = 5.0;
  68. } else if (strcmp(argv[1], "dec") == 0) {
  69. dec = 1;
  70. } else
  71. usage();
  72. argc--;
  73. argv++;
  74. /* Parse arguments. */
  75. while ((ch = getopt(argc, argv, "hm:M:t:")) != -1) {
  76. switch (ch) {
  77. case 'M':
  78. maxmem = strtoumax(optarg, NULL, 0);
  79. break;
  80. case 'm':
  81. maxmemfrac = strtod(optarg, NULL);
  82. break;
  83. case 't':
  84. maxtime = strtod(optarg, NULL);
  85. break;
  86. default:
  87. usage();
  88. }
  89. }
  90. argc -= optind;
  91. argv += optind;
  92. /* We must have one or two parameters left. */
  93. if ((argc < 1) || (argc > 2))
  94. usage();
  95. /* Open the input file. */
  96. if ((infile = fopen(argv[0], "r")) == NULL) {
  97. warn("Cannot open input file: %s", argv[0]);
  98. exit(1);
  99. }
  100. /* If we have an output file, open it. */
  101. if (argc > 1) {
  102. if ((outfile = fopen(argv[1], "w")) == NULL) {
  103. warn("Cannot open output file: %s", argv[1]);
  104. exit(1);
  105. }
  106. }
  107. /* Prompt for a password. */
  108. if (tarsnap_readpass(&passwd, "Please enter passphrase",
  109. dec ? NULL : "Please confirm passphrase", 1))
  110. exit(1);
  111. /* Encrypt or decrypt. */
  112. if (dec)
  113. rc = scryptdec_file(infile, outfile, (uint8_t *)passwd,
  114. strlen(passwd), maxmem, maxmemfrac, maxtime);
  115. else
  116. rc = scryptenc_file(infile, outfile, (uint8_t *)passwd,
  117. strlen(passwd), maxmem, maxmemfrac, maxtime);
  118. /* If we failed, print the right error message and exit. */
  119. if (rc != 0) {
  120. switch (rc) {
  121. case 1:
  122. warn("Error determining amount of available memory");
  123. break;
  124. case 2:
  125. warn("Error reading clocks");
  126. break;
  127. case 3:
  128. warn("Error computing derived key");
  129. break;
  130. case 4:
  131. warn("Error reading salt");
  132. break;
  133. case 5:
  134. warn("OpenSSL error");
  135. break;
  136. case 6:
  137. warn("Error allocating memory");
  138. break;
  139. case 7:
  140. warnx("Input is not valid scrypt-encrypted block");
  141. break;
  142. case 8:
  143. warnx("Unrecognized scrypt format version");
  144. break;
  145. case 9:
  146. warnx("Decrypting file would require too much memory");
  147. break;
  148. case 10:
  149. warnx("Decrypting file would take too much CPU time");
  150. break;
  151. case 11:
  152. warnx("Passphrase is incorrect");
  153. break;
  154. case 12:
  155. warn("Error writing file: %s",
  156. (argc > 1) ? argv[1] : "standard output");
  157. break;
  158. case 13:
  159. warn("Error reading file: %s", argv[0]);
  160. break;
  161. }
  162. exit(1);
  163. }
  164. /* Zero and free the password. */
  165. memset(passwd, 0, strlen(passwd));
  166. free(passwd);
  167. return (0);
  168. }