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 int main(int argc
, char *argv
[])
95 const char *kr
= "keyring";
96 const char *tag
= "tripe";
108 static const struct option opts
[] = {
109 { "help", 0, 0, 'h' },
110 { "version", 0, 0, 'v' },
111 { "usage", 0, 0, 'u' },
112 { "keyring", OPTF_ARGREQ
, 0, 'k' },
113 { "tag", OPTF_ARGREQ
, 0, 't' },
114 { "trace", OPTF_ARGREQ
, 0, 'T' },
118 i
= mdwopt(argc
, argv
, "hvuk:t:T:", opts
, 0, 0, 0); if (i
< 0) break;
120 case 'h': help(stdout
); exit(0);
121 case 'v': version(stdout
); exit(0);
122 case 'u': usage(stdout
); exit(0);
123 case 'k': kr
= optarg
; break;
124 case 't': tag
= optarg
; break;
126 tr_flags
= traceopt(tr_opts
, optarg
, tr_flags
, 0);
128 default: f
|= f_bogus
; break;
131 if (f
&f_bogus
) { usage(stderr
); exit(2); }
132 args
= argv
+ optind
;
134 km_init(kr
, kr
, tag
);
135 if (!master
) die(3, "failed to load the master key");
136 T( trace_on(stderr
, tr_flags
); )
139 if (strcmp(arg
, "ies-encrypt") == 0) {
140 arg
= getarg(); ty
= parseu32(arg
);
141 arg
= getarg(); buf_init(&b
, (/*unconst*/ octet
*)arg
, strlen(arg
));
142 buf_init(&bb
, buf_t
, sizeof(buf_t
));
144 if ((err
= ies_encrypt(master
, ty
, &b
, &bb
)) != 0)
145 die(3, "ies_encrypt failed: err = %d", err
);
146 b64
= base64_class
.encoder(0, "\n", 72);
147 if ((err
= b64
->ops
->code(b64
, BBASE(&bb
), BLEN(&bb
), &d
)) != 0 ||
148 (err
= b64
->ops
->code(b64
, 0, 0, &d
)) != 0)
149 die(3, "base64 encoding failed: %s", codec_strerror(err
));
150 b64
->ops
->destroy(b64
);
152 fwrite(d
.buf
, 1, d
.len
, stdout
);
154 } else if (strcmp(arg
, "ies-decrypt") == 0) {
155 arg
= getarg(); ty
= parseu32(arg
);
157 b64
= base64_class
.decoder(CDCF_IGNSPC
| CDCF_IGNNEWL
);
158 if ((err
= b64
->ops
->code(b64
, arg
, strlen(arg
), &d
)) != 0 ||
159 (err
= b64
->ops
->code(b64
, 0, 0, &d
)) != 0)
160 die(3, "base64 decoding failed: %s", codec_strerror(err
));
161 b64
->ops
->destroy(b64
);
163 buf_init(&b
, d
.buf
, d
.len
); buf_init(&bb
, buf_t
, sizeof(buf_t
));
164 if ((err
= ies_decrypt(master
, ty
, &b
, &bb
)) != 0)
165 die(3, "ies_decrypt failed: err = %d", err
);
166 fwrite(BBASE(&bb
), 1, BLEN(&bb
), stdout
);
169 die(2, "unknown command `%s'", arg
);
174 /*----- That's all, folks -------------------------------------------------*/