1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
/*-
* Copyright 2009 Colin Percival
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "platform.h"
#include <errno.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "getopt.h"
#include "humansize.h"
#include "insecure_memzero.h"
#include "parsenum.h"
#include "passphrase_entry.h"
#include "scryptenc.h"
#include "scryptenc_print_error.h"
#include "warnp.h"
static void
usage(void)
{
fprintf(stderr,
"usage: scrypt {enc | dec | info} [-f] [--logN value] [-M maxmem]\n"
" [-m maxmemfrac] [-P] [-p value] [-r value]"
" [-t maxtime] [-v]\n"
" [--passphrase method:arg] infile [outfile]\n"
" scrypt --version\n");
exit(1);
}
/**
* scrypt_mode_info(infilename):
* Print scrypt parameters used for the specified ${infilename}, or read from
* stdin if that argument is NULL.
*/
static int
scrypt_mode_info(const char * infilename)
{
FILE * infile;
int rc;
/* If the input isn't stdin, open the file. */
if (infilename != NULL) {
if ((infile = fopen(infilename, "rb")) == NULL) {
warnp("Cannot open input file: %s", infilename);
goto err0;
}
} else {
infile = stdin;
}
/* Print the encryption parameters used for the file. */
if ((rc = scryptdec_file_printparams(infile)) != SCRYPT_OK) {
scryptenc_print_error(rc, infilename, NULL);
goto err1;
}
/* Clean up. */
if ((infile != stdin) && fclose(infile))
warnp("fclose");
/* Success! */
return (0);
err1:
if ((infile != stdin) && fclose(infile))
warnp("fclose");
err0:
/* Failure! */
return (-1);
}
/**
* scrypt_mode_enc_dec(params, passphrase_entry, passphrase_arg, dec, verbose,
* force_resources, infilename, outfilename):
* Either encrypt (if ${dec} is 0) or decrypt (if ${dec} is non-zero)
* ${infilename} (or standard input if this is NULL) to ${outfilename}.
* Use scrypt parameters ${params}, with passphrase entry method
* ${passphrase_entry} and argument ${passphrase_arg}. If ${verbose} is
* non-zero, print verbose messages. If ${force_resources} is non-zero,
* do not check whether encryption or decryption will exceed the estimated
* time or memory requirements.
*/
static int
scrypt_mode_enc_dec(struct scryptenc_params params,
enum passphrase_entry passphrase_entry, const char * passphrase_arg,
int dec, int verbose, int force_resources,
const char * infilename, const char * outfilename)
{
struct scryptdec_file_cookie * C = NULL;
FILE * infile;
FILE * outfile = stdout;
char * passwd;
int rc;
/* If the input isn't stdin, open the file. */
if (infilename != NULL) {
if ((infile = fopen(infilename, "rb")) == NULL) {
warnp("Cannot open input file: %s", infilename);
goto err0;
}
} else {
infile = stdin;
}
/* Get the password. */
if (passphrase_entry_readpass(&passwd, passphrase_entry,
passphrase_arg, "Please enter passphrase",
"Please confirm passphrase", dec)) {
warnp("passphrase_entry_readpass");
goto err1;
}
/*-
* If we're decrypting, open the input file and process its header;
* doing this here allows us to abort without creating an output
* file if the input file does not have a valid scrypt header or if
* we have the wrong passphrase.
*
* If successful, we get back a cookie containing the decryption
* parameters (which we'll use after we open the output file).
*/
if (dec) {
if ((rc = scryptdec_file_prep(infile, (uint8_t *)passwd,
strlen(passwd), ¶ms, verbose, force_resources,
&C)) != 0) {
goto cleanup;
}
}
/* If we have an output filename, open it. */
if (outfilename != NULL) {
if ((outfile = fopen(outfilename, "wb")) == NULL) {
warnp("Cannot open output file: %s", outfilename);
goto err2;
}
}
/* Encrypt or decrypt. */
if (dec)
rc = scryptdec_file_copy(C, outfile);
else
rc = scryptenc_file(infile, outfile, (uint8_t *)passwd,
strlen(passwd), ¶ms, verbose, force_resources);
cleanup:
/* Free the decryption cookie, if any. */
scryptdec_file_cookie_free(C);
/* Zero and free the password. */
insecure_memzero(passwd, strlen(passwd));
free(passwd);
/* Close any files we opened. */
if ((infile != stdin) && fclose(infile))
warnp("fclose");
if ((outfile != stdout) && fclose(outfile))
warnp("fclose");
/* If we failed, print the right error message and exit. */
if (rc != SCRYPT_OK) {
scryptenc_print_error(rc, infilename, outfilename);
goto err0;
}
/* Success! */
return (0);
err2:
scryptdec_file_cookie_free(C);
insecure_memzero(passwd, strlen(passwd));
free(passwd);
err1:
if ((infile != stdin) && fclose(infile))
warnp("fclose");
err0:
/* Failure! */
return (-1);
}
/* Parse a numeric optarg within a GETOPT context. (Requires ch and optarg.) */
#define GETOPT_PARSENUM_WITHIN_UNSIGNED(var, min, max) do { \
if (PARSENUM((var), optarg, (min), (max))) { \
if (errno == ERANGE) { \
warn0("%s must be between %ju and %ju" \
" (inclusive)", ch, (uintmax_t)(min), \
(uintmax_t)(max)); \
} else \
warnp("Invalid option: %s %s", ch, optarg); \
exit(1); \
} \
} while (0)
int
main(int argc, char * argv[])
{
int dec = 0;
int info = 0;
int force_resources = 0;
uint64_t maxmem64;
struct scryptenc_params params = {0, 0.5, 300.0, 0, 0, 0};
const char * ch;
const char * infilename;
const char * outfilename;
int verbose = 0;
enum passphrase_entry passphrase_entry = PASSPHRASE_UNSET;
const char * passphrase_arg;
WARNP_INIT;
/* We should have "enc", "dec", or "info" first. */
if (argc < 2)
usage();
if (strcmp(argv[1], "enc") == 0) {
params.maxmem = 0;
params.maxmemfrac = 0.125;
params.maxtime = 5.0;
} else if (strcmp(argv[1], "dec") == 0) {
dec = 1;
} else if (strcmp(argv[1], "info") == 0) {
info = 1;
} else if (strcmp(argv[1], "--version") == 0) {
fprintf(stdout, "scrypt %s\n", PACKAGE_VERSION);
exit(0);
} else {
warn0("First argument must be 'enc', 'dec', or 'info'.");
usage();
}
argc--;
argv++;
/* Parse arguments. */
while ((ch = GETOPT(argc, argv)) != NULL) {
GETOPT_SWITCH(ch) {
GETOPT_OPT("-f"):
force_resources = 1;
break;
GETOPT_OPTARG("--logN"):
GETOPT_PARSENUM_WITHIN_UNSIGNED(¶ms.logN, 10, 40);
break;
GETOPT_OPTARG("-M"):
if (humansize_parse(optarg, &maxmem64)) {
warn0("Could not parse the parameter to -M.");
exit(1);
}
if (maxmem64 > SIZE_MAX) {
warn0("The parameter to -M is too large.");
exit(1);
}
params.maxmem = (size_t)maxmem64;
break;
GETOPT_OPTARG("-m"):
if (PARSENUM(¶ms.maxmemfrac, optarg, 0, 0.5)) {
warnp("Invalid option: -m %s", optarg);
exit(1);
}
break;
GETOPT_OPTARG("-p"):
GETOPT_PARSENUM_WITHIN_UNSIGNED(¶ms.p, 1, 2048);
break;
GETOPT_OPTARG("--passphrase"):
if (passphrase_entry != PASSPHRASE_UNSET) {
warn0("You can only enter one --passphrase or"
" -P argument");
exit(1);
}
/* Parse "method:arg" optarg. */
if (passphrase_entry_parse(optarg, &passphrase_entry,
&passphrase_arg))
exit(1);
break;
GETOPT_OPTARG("-r"):
GETOPT_PARSENUM_WITHIN_UNSIGNED(¶ms.r, 1, 32);
break;
GETOPT_OPTARG("-t"):
if (PARSENUM(¶ms.maxtime, optarg, 0, INFINITY)) {
warnp("Invalid option: -t %s", optarg);
exit(1);
}
break;
GETOPT_OPT("-v"):
verbose = 1;
break;
GETOPT_OPT("-P"):
if (passphrase_entry != PASSPHRASE_UNSET) {
warn0("You can only enter one --passphrase or"
" -P argument");
exit(1);
}
passphrase_entry = PASSPHRASE_STDIN_ONCE;
passphrase_arg = "";
break;
GETOPT_MISSING_ARG:
warn0("Missing argument to %s", ch);
usage();
GETOPT_DEFAULT:
warn0("illegal option -- %s", ch);
usage();
}
}
argc -= optind;
argv += optind;
/* We must have one or two parameters left. */
if ((argc < 1) || (argc > 2))
usage();
/* The explicit parameters must be zero, or all non-zero. */
if ((params.logN != 0) && ((params.r == 0) || (params.p == 0))) {
warn0("If --logN is set, -r and -p must also be set");
goto err0;
}
if ((params.r != 0) && ((params.logN == 0) || (params.p == 0))) {
warn0("If -r is set, --logN and -p must also be set");
goto err0;
}
if ((params.p != 0) && ((params.logN == 0) || (params.r == 0))) {
warn0("If -p is set, --logN and -r must also be set");
goto err0;
}
/* We can't have a maxmemfrac of 0. */
if (params.maxmemfrac == 0.0) {
warn0("-m must be greater than 0");
goto err0;
}
/* Set the input filename. */
if (strcmp(argv[0], "-"))
infilename = argv[0];
else
infilename = NULL;
/* Set the output filename. */
if (argc > 1)
outfilename = argv[1];
else
outfilename = NULL;
/* Set the default passphrase entry method. */
if (passphrase_entry == PASSPHRASE_UNSET) {
passphrase_entry = PASSPHRASE_TTY_STDIN;
passphrase_arg = "";
}
/* Sanity check passphrase entry method and input filename. */
if ((passphrase_entry == PASSPHRASE_STDIN_ONCE) &&
(infilename == NULL)) {
warn0("Cannot read both passphrase and input file"
" from standard input");
goto err0;
}
/* What type of operation are we doing? */
if (info) {
/* User selected 'info' mode. */
if (scrypt_mode_info(infilename))
goto err0;
} else {
/* User selected encryption or decryption. */
if (scrypt_mode_enc_dec(params, passphrase_entry,
passphrase_arg, dec, verbose, force_resources,
infilename, outfilename))
goto err0;
}
/* Success! */
exit(0);
err0:
/* Failure! */
exit(1);
}
|