2 * Copyright 2009 Colin Percival
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 #include "scrypt_platform.h"
36 #include "scryptenc.h"
44 "usage: scrypt {enc | dec} [...] infile [outfile]\n");
49 main(int argc, char *argv[])
52 FILE * outfile = stdout;
55 double maxmemfrac = 0.5;
56 double maxtime = 300.0;
61 #ifdef NEED_WARN_PROGNAME
62 warn_progname = "scrypt";
65 /* We should have "enc" or "dec" first. */
68 if (strcmp(argv[1], "enc") == 0) {
72 } else if (strcmp(argv[1], "dec") == 0) {
79 /* Parse arguments. */
80 while ((ch = getopt(argc, argv, "hm:M:t:")) != -1) {
83 maxmem = strtoumax(optarg, NULL, 0);
86 maxmemfrac = strtod(optarg, NULL);
89 maxtime = strtod(optarg, NULL);
98 /* We must have one or two parameters left. */
99 if ((argc < 1) || (argc > 2))
102 /* Open the input file. */
103 if ((infile = fopen(argv[0], "r")) == NULL) {
104 warn("Cannot open input file: %s", argv[0]);
108 /* If we have an output file, open it. */
110 if ((outfile = fopen(argv[1], "w")) == NULL) {
111 warn("Cannot open output file: %s", argv[1]);
116 /* Prompt for a password. */
117 if (tarsnap_readpass(&passwd, "Please enter passphrase",
118 dec ? NULL : "Please confirm passphrase", 1))
121 /* Encrypt or decrypt. */
123 rc = scryptdec_file(infile, outfile, (uint8_t *)passwd,
124 strlen(passwd), maxmem, maxmemfrac, maxtime);
126 rc = scryptenc_file(infile, outfile, (uint8_t *)passwd,
127 strlen(passwd), maxmem, maxmemfrac, maxtime);
129 /* Zero and free the password. */
130 memset(passwd, 0, strlen(passwd));
133 /* If we failed, print the right error message and exit. */
137 warn("Error determining amount of available memory");
140 warn("Error reading clocks");
143 warn("Error computing derived key");
146 warn("Error reading salt");
149 warn("OpenSSL error");
152 warn("Error allocating memory");
155 warnx("Input is not valid scrypt-encrypted block");
158 warnx("Unrecognized scrypt format version");
161 warnx("Decrypting file would require too much memory");
164 warnx("Decrypting file would take too much CPU time");
167 warnx("Passphrase is incorrect");
170 warn("Error writing file: %s",
171 (argc > 1) ? argv[1] : "standard output");
174 warn("Error reading file: %s", argv[0]);