Example #1
0
File: main.c Project: ciyam/scrypt
int
main(int argc, char *argv[])
{
	FILE * infile = NULL;
	FILE * outfile = stdout;
	int dec = 0;
	size_t maxmem = 0;
	double maxmemfrac = 0.5;
	double maxtime = 300.0;
	char ch;
	char * passwd;
	int rc;

#ifdef NEED_WARN_PROGNAME
	warn_progname = "scrypt";
#endif

	/* We should have "enc" or "dec" first. */
	if (argc < 2)
		usage();
	if (strcmp(argv[1], "enc") == 0) {
		maxmem = 0;
		maxmemfrac = 0.125;
		maxtime = 5.0;
	} else if (strcmp(argv[1], "dec") == 0) {
		dec = 1;
	} else
		usage();
	argc--;
	argv++;

	/* Parse arguments. */
	while ((ch = getopt(argc, argv, "hm:M:t:")) != -1) {
		switch (ch) {
		case 'M':
			maxmem = strtoumax(optarg, NULL, 0);
			break;
		case 'm':
			maxmemfrac = strtod(optarg, NULL);
			break;
		case 't':
			maxtime = strtod(optarg, NULL);
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	/* We must have one, two or three parameters left. */
	if ((argc < 1) || (argc > 3))
		usage();

	/* Open the input file. */
	if ((infile = fopen(argv[0], "r")) == NULL) {
		warn("Cannot open input file: %s", argv[0]);
		exit(1);
	}

	/* If we have an output file, open it. */
	if (argc > 1) {
		if ((outfile = fopen(argv[1], "w")) == NULL) {
			warn("Cannot open output file: %s", argv[1]);
			exit(1);
		}
	}

   int has_pwd_arg = ( argc > 2 );

	/* If a password was passed as an arg then use. */
   if( has_pwd_arg )
      passwd = argv[ 2 ];
   else
   {
	   /* Prompt for a password. */
	   if (tarsnap_readpass(&passwd, "Please enter password",
	      dec ? NULL : "Please confirm password", 1))
		   exit(1);
   }

	/* Encrypt or decrypt. */
	if (dec)
		rc = scryptdec_file(infile, outfile, (uint8_t *)passwd,
		    strlen(passwd), maxmem, maxmemfrac, maxtime);
	else
		rc = scryptenc_file(infile, outfile, (uint8_t *)passwd,
		    strlen(passwd), maxmem, maxmemfrac, maxtime);

   if( !has_pwd_arg )
   {
	   /* Zero and free the password. */
	   memset(passwd, 0, strlen(passwd));
	   free(passwd);
   }

	/* If we failed, print the right error message and exit. */
	if (rc != 0) {
		switch (rc) {
		case 1:
			warn("Error determining amount of available memory");
			break;
		case 2:
			warn("Error reading clocks");
			break;
		case 3:
			warn("Error computing derived key");
			break;
		case 4:
			warn("Error reading salt");
			break;
		case 5:
			warn("OpenSSL error");
			break;
		case 6:
			warn("Error allocating memory");
			break;
		case 7:
			warnx("Input is not valid scrypt-encrypted block");
			break;
		case 8:
			warnx("Unrecognized scrypt format version");
			break;
		case 9:
			warnx("Decrypting file would require too much memory");
			break;
		case 10:
			warnx("Decrypting file would take too much CPU time");
			break;
		case 11:
			warnx("Password is incorrect");
			break;
		case 12:
			warn("Error writing file: %s",
			    (argc > 1) ? argv[1] : "standard output");
			break;
		case 13:
			warn("Error reading file: %s", argv[0]);
			break;
		}
		exit(1);
	}

	return (0);
}
Example #2
0
int
main(int argc, char* argv[])
{
	char * passwd;
  uint64_t N = 1 << 14;
  uint32_t r = 8;
  uint32_t p = 1;
  char buf[65];
  size_t buflen = 64;
  int result;
  int i = 0;
  int punctuation = 1; // Whether to allow punctuation in the output
  int addbang = 0; // The the first character '!'
  int addfive = 0; // Make the 5th character '5'
  int length = 10; // Desired password length

  if (argc < 2) {
    usage();
  }

  // Disable punctuation in output for sites that are jerks
  if (strcmp(argv[1], "chase.com") == 0 ||
      strcmp(argv[1], "fandango.com") == 0 ||
      //strcmp(argv[1], "apple.com") == 0 ||
      strcmp(argv[1], "comcast.com") == 0 ||
      strcmp(argv[1], "verizonwireless.com") == 0 ||
      strcmp(argv[1], "gap.com") == 0 ||
      strcmp(argv[1], "audible.com") == 0 ||
      strcmp(argv[1], "purpletie.com") == 0 ||
      strcmp(argv[1], "opentable.com") == 0 ||
      strcmp(argv[1], "videoeta.com") == 0 ||
      strcmp(argv[1], "topcoder.com") == 0 ||
      strcmp(argv[1], "hondafinancialservices.com") == 0 ||
      strcmp(argv[1], "americanexpress.com") == 0 ||
      argc == 3) {
    punctuation = 0;
  }
  else if (strcmp(argv[1], "kaiserpermanente.org") == 0) {
    addbang = 1;
  }
  else if (strcmp(argv[1], "schwab.com") == 0) {
    // Schwab may have the stupidest password requirements I've ever
    // encountered.
    length = 8;
    addfive = 1;
    punctuation = 0;
  }
  else if (strcmp(argv[1], "deltadentalins.com") == 0 ||
           strcmp(argv[1], "mozilla.org") == 0 ||
           strcmp(argv[1], "morganstanleysmithbarney.com") == 0) {
    addfive = 1;
  }

	/* Prompt for a password. */
	if (tarsnap_readpass(&passwd, "Please enter passphrase", NULL, 1)) {
		exit(1);
  }

  result = crypto_scrypt(passwd, strlen(passwd), argv[1], strlen(argv[1]), N, r,
                         p, buf, buflen);
  buf[64] = 0;

  if (result != 0) {
    fprintf(stderr, "Something went wrong!\n");
    print_error(errno);
    exit(1);
  } else {
    print_it(buf, punctuation, addbang, length, addfive);
    printf("\n");
  }

	/* Zero and free the password. */
	memset(passwd, 0, strlen(passwd));
	free(passwd);

  return 0;
}