15 #define OPT(flag, opt, name, desc) \
16 { name "\0" desc, flag, NULL, opt }
18 #define OPT_OPT(opt) ((opt).val)
19 #define OPT_LEN(opt) ((int)strlen((opt).name))
20 #define OPT_NAME(opt) ((opt).name)
21 #define OPT_DESC(opt) (&(opt).name[OPT_LEN(opt)])
23 /* Crypto functions */
31 static struct option long_options[] = {
32 OPT( 0, 'e', "encrypt", "Encrypt data" ),
33 OPT( 0, 'd', "decrypt", "Decrypt data" ),
34 OPT( 0, 't', "test", "Run self tests" ),
35 OPT( 1, 'f', "function", "Specify crypto function" ),
36 OPT( 1, 'i', "input", "Input file" ),
37 OPT( 1, 'o', "output", "Output file" ),
38 OPT( 0, 'q', "quiet", "Decrease verbosity" ),
39 OPT( 0, 'v', "verbose", "Increase verbosity" ),
40 OPT( 0, 'h', "help", "Print usage information" ),
48 [NONE ] { "none", "No-op encryption/decryption" },
49 [SHA256] { "sha256", "SHA-256 hash function" },
50 [SCRYPT] { "scrypt", "Memory intensive hash function" },
54 static int opt_decrypt;
55 static int opt_encrypt;
56 static crypt_t opt_crypt;
57 static char *opt_instr;
58 static char *opt_outstr;
59 static FILE *opt_input;
60 static FILE *opt_output;
65 static const char *crypt_name(crypt_t id)
67 if (id < 0 || id >= N_ELEM(crypt_info))
68 error("invalid crypt id %d", id);
69 return crypt_info[id].name;
72 static crypt_t crypt_id(const char *name)
74 for (int i = 0; i < N_ELEM(crypt_info); i++)
75 if (!strcmp(name, crypt_info[i].name))
77 error("invalid crypt function %s", name);
81 static void usage(const char *name)
84 printf(" %s [OPTION...]\n", name);
87 for (int i = 0; long_options[i].name; i++)
88 printf(" -%c, --%-10s %s\n",
91 long_options[i].name + 1 +
92 strlen(long_options[i].name));
94 printf("Crypto functions:\n");
95 for (int i = 0; i < N_ELEM(crypt_info); i++)
101 static void parse(int argc, char **argv)
104 int c = getopt_long(argc, argv, "edf:i:o:qvth", long_options, NULL);
118 opt_crypt = crypt_id(optarg);
121 opt_input = fopen(optarg, "rb");
125 opt_output = fopen(optarg, "wb+");
145 void run(crypt_t id, FILE *in, FILE *out)
147 uint8_t dec[512], enc[512];
153 while ((len = fread(&dec, 1, 512, in)) > 0) {
154 //debug("<< [%s]:%d", dec, len);
155 none_enc(dec, enc, len);
156 //debug(">> [%s]:%d", enc, len);
157 fwrite(enc, 1, len, out);
163 while ((len = fread(&dec, 1, 512, in)) > 0) {
164 scrypt_hash(dec, sum);
171 static void test(crypt_t id)
177 case NONE: none_test(); break;
178 case SHA256: sha256_test(); break;
179 case SCRYPT: scrypt_test(); break;
184 int main(int argc, char **argv)
186 /* Parse arguments */
188 opt_outstr = "stdout";
192 loglevel = opt_level;
195 debug("encrypt: %s", opt_encrypt ? "true" : "false");
196 debug("decrypt: %s", opt_decrypt ? "true" : "false");
197 debug("crypto: %s", crypt_name(opt_crypt));
198 debug("loglevel: %d", opt_level);
199 debug("input: %s", opt_instr);
200 debug("output: %s", opt_outstr);
206 run(opt_crypt, opt_input, opt_output);