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"
34 #include "humansize.h"
35 #include "insecure_memzero.h"
37 #include "scryptenc.h"
45 "usage: scrypt {enc | dec} [-f] [-M maxmem]"
47 " [-t maxtime] [-v] [-P] infile [outfile]\n"
48 " scrypt --version\n");
53 main(int argc
, char *argv
[])
60 int force_resources
= 0;
62 double maxmemfrac
= 0.5;
63 double maxtime
= 300.0;
71 /* We should have "enc" or "dec" first. */
74 if (strcmp(argv
[1], "enc") == 0) {
78 } else if (strcmp(argv
[1], "dec") == 0) {
80 } else if (strcmp(argv
[1], "--version") == 0) {
81 fprintf(stdout
, "scrypt %s\n", PACKAGE_VERSION
);
84 warn0("First argument must be 'enc' or 'dec'.\n");
90 /* Parse arguments. */
91 while ((ch
= GETOPT(argc
, argv
)) != NULL
) {
97 if (humansize_parse(optarg
, &maxmem64
)) {
98 warn0("Could not parse the parameter to -M.");
101 if (maxmem64
> SIZE_MAX
) {
102 warn0("The parameter to -M is too large.");
105 maxmem
= (size_t)maxmem64
;
108 maxmemfrac
= strtod(optarg
, NULL
);
111 maxtime
= strtod(optarg
, NULL
);
120 warn0("Missing argument to %s\n", ch
);
123 warn0("illegal option -- %s\n", ch
);
130 /* We must have one or two parameters left. */
131 if ((argc
< 1) || (argc
> 2))
134 /* If the input isn't stdin, open the file. */
135 if (strcmp(argv
[0], "-")) {
136 if ((infile
= fopen(argv
[0], "rb")) == NULL
) {
137 warnp("Cannot open input file: %s", argv
[0]);
144 /* If we have an output file, open it. */
146 if ((outfile
= fopen(argv
[1], "wb")) == NULL
) {
147 warnp("Cannot open output file: %s", argv
[1]);
154 /* Prompt for a password. */
155 if (readpass(&passwd
, "Please enter passphrase",
156 (dec
|| !devtty
) ? NULL
: "Please confirm passphrase", devtty
))
159 /* Encrypt or decrypt. */
161 rc
= scryptdec_file(infile
, outfile
, (uint8_t *)passwd
,
162 strlen(passwd
), maxmem
, maxmemfrac
, maxtime
, verbose
,
165 rc
= scryptenc_file(infile
, outfile
, (uint8_t *)passwd
,
166 strlen(passwd
), maxmem
, maxmemfrac
, maxtime
, verbose
);
168 /* Zero and free the password. */
169 insecure_memzero(passwd
, strlen(passwd
));
172 /* Close any files we opened. */
175 if (outfile
!= stdout
)
178 /* If we failed, print the right error message and exit. */
182 warnp("Error determining amount of available memory");
185 warnp("Error reading clocks");
188 warnp("Error computing derived key");
191 warnp("Error reading salt");
194 warnp("OpenSSL error");
197 warnp("Error allocating memory");
200 warn0("Input is not valid scrypt-encrypted block");
203 warn0("Unrecognized scrypt format version");
206 warn0("Decrypting file would require too much memory");
209 warn0("Decrypting file would take too much CPU time");
212 warn0("Passphrase is incorrect");
215 warnp("Error writing file: %s",
216 (argc
> 1) ? argv
[1] : "standard output");
219 warnp("Error reading file: %s", argv
[0]);
This page took 0.027553 seconds and 4 git commands to generate.