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