main.c 4.3 KB

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