3 * Various unit-level tests
5 * (c) 2017 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
30 /*----- Data structures ---------------------------------------------------*/
32 /*----- Global variables --------------------------------------------------*/
36 /*----- Static variables --------------------------------------------------*/
40 /*----- Main code ---------------------------------------------------------*/
42 static void usage(FILE *fp)
45 "Usage: $ [-k FILE] [-t KEYTAG] [-T TRACE-OPTS] COMMAND [ARGS...]\n");
48 static void version(FILE *fp)
49 { pquis(fp, "$, TrIPE version " VERSION "\n"); }
51 static void help(FILE *fp)
53 version(fp); fputc('\n', fp);
54 usage(fp); fputc('\n', fp);
58 -h, --help Show this help text.\n\
59 -v, --version Show version number.\n\
60 -u, --usage Show brief usage message.\n\
62 -k, --keyring=FILE Get keys from FILE.\n\
63 -t, --tag=KEYTAG Use KEYTAG as master private key.\n\
64 -T, --trace=TRACE-OPTS Turn on tracing options.\n\
68 ies-encrypt TY MESSAGE\n\
69 ies-decrypt TY CIPHERTEXT\n\
73 static uint32 parseu32(const char *p)
80 i = strtoul(p, &q, 0);
81 while (*q && isspace((unsigned char)*q)) q++;
82 if (errno || *q || i > 0xffffffffu) die(2, "bad 32-bit integer `%s'", p);
87 static const char *getarg(void)
88 { if (!*args) die(2, "missing argument"); else return (*args++); }
90 static void lastarg(void)
91 { if (*args) die(2, "unexpected argument `%s'", *args); }
93 void iv_addreason(void) { ; }
94 void iv_rmreason(void) { ; }
96 void lp_end(void) { ; }
98 int main(int argc, char *argv[])
100 const char *kr = "keyring";
101 const char *tag = "tripe";
113 static const struct option opts[] = {
114 { "help", 0, 0, 'h' },
115 { "version", 0, 0, 'v' },
116 { "usage", 0, 0, 'u' },
117 { "keyring", OPTF_ARGREQ, 0, 'k' },
118 { "tag", OPTF_ARGREQ, 0, 't' },
119 { "trace", OPTF_ARGREQ, 0, 'T' },
123 i = mdwopt(argc, argv, "hvuk:t:T:", opts, 0, 0, 0); if (i < 0) break;
125 case 'h': help(stdout); exit(0);
126 case 'v': version(stdout); exit(0);
127 case 'u': usage(stdout); exit(0);
128 case 'k': kr = optarg; break;
129 case 't': tag = optarg; break;
131 tr_flags = traceopt(tr_opts, optarg, tr_flags, 0);
133 default: f |= f_bogus; break;
136 if (f&f_bogus) { usage(stderr); exit(2); }
137 args = argv + optind;
139 km_init(kr, kr, tag);
140 trace_on(stderr, tr_flags);
143 if (strcmp(arg, "ies-encrypt") == 0) {
144 arg = getarg(); ty = parseu32(arg);
145 arg = getarg(); buf_init(&b, (/*unconst*/ octet *)arg, strlen(arg));
146 buf_init(&bb, buf_t, sizeof(buf_t));
148 if ((err = ies_encrypt(master, ty, &b, &bb)) != 0)
149 die(3, "ies_encrypt failed: err = %d", err);
150 b64 = base64_class.encoder(0, "\n", 72);
151 if ((err = b64->ops->code(b64, BBASE(&bb), BLEN(&bb), &d)) != 0 ||
152 (err = b64->ops->code(b64, 0, 0, &d)) != 0)
153 die(3, "base64 encoding failed: %s", codec_strerror(err));
154 b64->ops->destroy(b64);
156 fwrite(d.buf, 1, d.len, stdout);
158 } else if (strcmp(arg, "ies-decrypt") == 0) {
159 arg = getarg(); ty = parseu32(arg);
161 b64 = base64_class.decoder(CDCF_IGNSPC | CDCF_IGNNEWL);
162 if ((err = b64->ops->code(b64, arg, strlen(arg), &d)) != 0 ||
163 (err = b64->ops->code(b64, 0, 0, &d)) != 0)
164 die(3, "base64 decoding failed: %s", codec_strerror(err));
165 b64->ops->destroy(b64);
167 buf_init(&b, d.buf, d.len); buf_init(&bb, buf_t, sizeof(buf_t));
168 if ((err = ies_decrypt(master, ty, &b, &bb)) != 0)
169 die(3, "ies_decrypt failed: err = %d", err);
170 fwrite(BBASE(&bb), 1, BLEN(&bb), stdout);
173 die(2, "unknown command `%s'", arg);
178 /*----- That's all, folks -------------------------------------------------*/