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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
/* $OpenBSD: signify.c,v 1.20 2014/01/09 20:37:25 espie Exp $ */
/*
* Copyright (c) 2013 Ted Unangst <tedu@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <netinet/in.h>
#include <resolv.h>
#include <stdint.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <err.h>
#include <unistd.h>
#include <readpassphrase.h>
#include <util.h>
#include <sha2.h>
#include "crypto_api.h"
#define SIGBYTES crypto_sign_ed25519_BYTES
#define SECRETBYTES crypto_sign_ed25519_SECRETKEYBYTES
#define PUBLICBYTES crypto_sign_ed25519_PUBLICKEYBYTES
#define PKALG "Ed"
#define KDFALG "BK"
#define FPLEN 8
#define COMMENTHDR "untrusted comment: "
#define COMMENTHDRLEN 19
#define COMMENTMAXLEN 1024
struct enckey {
uint8_t pkalg[2];
uint8_t kdfalg[2];
uint32_t kdfrounds;
uint8_t salt[16];
uint8_t checksum[8];
uint8_t fingerprint[FPLEN];
uint8_t seckey[SECRETBYTES];
};
struct pubkey {
uint8_t pkalg[2];
uint8_t fingerprint[FPLEN];
uint8_t pubkey[PUBLICBYTES];
};
struct sig {
uint8_t pkalg[2];
uint8_t fingerprint[FPLEN];
uint8_t sig[SIGBYTES];
};
extern char *__progname;
static void
usage(void)
{
fprintf(stderr, "usage:"
#ifndef VERIFYONLY
"\t%s [-n] -p pubkey -s seckey -G\n"
"\t%s [-e] [-o output] -s seckey -S input\n"
#endif
"\t%s [-e] [-o output] -p pubkey -V input\n",
#ifndef VERIFYONLY
__progname, __progname,
#endif
__progname);
exit(1);
}
static int
xopen(const char *fname, int flags, mode_t mode)
{
int fd;
fd = open(fname, flags, mode);
if (fd == -1)
err(1, "open %s", fname);
return fd;
}
static void *
xmalloc(size_t len)
{
void *p;
p = malloc(len);
if (!p)
err(1, "malloc %zu", len);
return p;
}
static void
readall(int fd, void *buf, size_t len, const char *filename)
{
ssize_t x;
x = read(fd, buf, len);
if (x == -1) {
err(1, "read from %s", filename);
} else if (x != len) {
errx(1, "short read from %s", filename);
}
}
static size_t
parseb64file(const char *filename, char *b64, void *buf, size_t len,
char *comment)
{
int rv;
char *commentend, *b64end;
commentend = strchr(b64, '\n');
if (!commentend || commentend - b64 <= COMMENTHDRLEN ||
memcmp(b64, COMMENTHDR, COMMENTHDRLEN))
errx(1, "invalid comment in %s; must start with '%s'",
filename, COMMENTHDR);
*commentend = 0;
if (comment)
strlcpy(comment, b64 + COMMENTHDRLEN, COMMENTMAXLEN);
b64end = strchr(commentend + 1, '\n');
if (!b64end)
errx(1, "missing new line after b64 in %s", filename);
*b64end = 0;
rv = b64_pton(commentend + 1, buf, len);
if (rv != len)
errx(1, "invalid b64 encoding in %s", filename);
if (memcmp(buf, PKALG, 2))
errx(1, "unsupported file %s", filename);
return b64end - b64 + 1;
}
static void
readb64file(const char *filename, void *buf, size_t len, char *comment)
{
char b64[2048];
int rv, fd;
fd = xopen(filename, O_RDONLY | O_NOFOLLOW, 0);
memset(b64, 0, sizeof(b64));
rv = read(fd, b64, sizeof(b64) - 1);
if (rv == -1)
err(1, "read from %s", filename);
parseb64file(filename, b64, buf, len, comment);
memset(b64, 0, sizeof(b64));
close(fd);
}
uint8_t *
readmsg(const char *filename, unsigned long long *msglenp)
{
unsigned long long msglen;
uint8_t *msg;
struct stat sb;
int fd;
fd = xopen(filename, O_RDONLY | O_NOFOLLOW, 0);
fstat(fd, &sb);
msglen = sb.st_size;
if (msglen > (1UL << 30))
errx(1, "msg too large in %s", filename);
msg = xmalloc(msglen);
readall(fd, msg, msglen, filename);
close(fd);
*msglenp = msglen;
return msg;
}
static void
writeall(int fd, const void *buf, size_t len, const char *filename)
{
ssize_t x;
x = write(fd, buf, len);
if (x == -1) {
err(1, "write to %s", filename);
} else if (x != len) {
errx(1, "short write to %s", filename);
}
}
#ifndef VERIFYONLY
static void
appendall(const char *filename, const void *buf, size_t len)
{
int fd;
fd = xopen(filename, O_NOFOLLOW | O_RDWR | O_APPEND, 0);
writeall(fd, buf, len, filename);
close(fd);
}
static void
writeb64file(const char *filename, const char *comment, const void *buf,
size_t len, int flags, mode_t mode)
{
char header[1024];
char b64[1024];
int fd, rv;
fd = xopen(filename, O_CREAT|flags|O_NOFOLLOW|O_RDWR, mode);
snprintf(header, sizeof(header), "%ssignify %s\n", COMMENTHDR,
comment);
writeall(fd, header, strlen(header), filename);
if ((rv = b64_ntop(buf, len, b64, sizeof(b64)-1)) == -1)
errx(1, "b64 encode failed");
b64[rv++] = '\n';
writeall(fd, b64, rv, filename);
memset(b64, 0, sizeof(b64));
close(fd);
}
static void
kdf(uint8_t *salt, size_t saltlen, int rounds, uint8_t *key, size_t keylen)
{
char pass[1024];
if (rounds == 0) {
memset(key, 0, keylen);
return;
}
if (!readpassphrase("passphrase: ", pass, sizeof(pass), 0))
errx(1, "readpassphrase");
if (bcrypt_pbkdf(pass, strlen(pass), salt, saltlen, key,
keylen, rounds) == -1)
errx(1, "bcrypt pbkdf");
memset(pass, 0, sizeof(pass));
}
static void
signmsg(uint8_t *seckey, uint8_t *msg, unsigned long long msglen,
uint8_t *sig)
{
unsigned long long siglen;
uint8_t *sigbuf;
sigbuf = xmalloc(msglen + SIGBYTES);
crypto_sign_ed25519(sigbuf, &siglen, msg, msglen, seckey);
memcpy(sig, sigbuf, SIGBYTES);
free(sigbuf);
}
static void
generate(const char *pubkeyfile, const char *seckeyfile, int rounds)
{
uint8_t digest[SHA512_DIGEST_LENGTH];
struct pubkey pubkey;
struct enckey enckey;
uint8_t xorkey[sizeof(enckey.seckey)];
uint8_t fingerprint[FPLEN];
SHA2_CTX ctx;
int i;
crypto_sign_ed25519_keypair(pubkey.pubkey, enckey.seckey);
arc4random_buf(fingerprint, sizeof(fingerprint));
SHA512Init(&ctx);
SHA512Update(&ctx, enckey.seckey, sizeof(enckey.seckey));
SHA512Final(digest, &ctx);
memcpy(enckey.pkalg, PKALG, 2);
memcpy(enckey.kdfalg, KDFALG, 2);
enckey.kdfrounds = htonl(rounds);
memcpy(enckey.fingerprint, fingerprint, FPLEN);
arc4random_buf(enckey.salt, sizeof(enckey.salt));
kdf(enckey.salt, sizeof(enckey.salt), rounds, xorkey, sizeof(xorkey));
memcpy(enckey.checksum, digest, sizeof(enckey.checksum));
for (i = 0; i < sizeof(enckey.seckey); i++)
enckey.seckey[i] ^= xorkey[i];
memset(digest, 0, sizeof(digest));
memset(xorkey, 0, sizeof(xorkey));
writeb64file(seckeyfile, "secret key", &enckey,
sizeof(enckey), O_EXCL, 0600);
memset(&enckey, 0, sizeof(enckey));
memcpy(pubkey.pkalg, PKALG, 2);
memcpy(pubkey.fingerprint, fingerprint, FPLEN);
writeb64file(pubkeyfile, "public key", &pubkey,
sizeof(pubkey), O_EXCL, 0666);
}
static void
sign(const char *seckeyfile, const char *msgfile, const char *sigfile,
int embedded)
{
struct sig sig;
uint8_t digest[SHA512_DIGEST_LENGTH];
struct enckey enckey;
uint8_t xorkey[sizeof(enckey.seckey)];
uint8_t *msg;
char comment[COMMENTMAXLEN], sigcomment[1024];
unsigned long long msglen;
int i, rounds;
SHA2_CTX ctx;
readb64file(seckeyfile, &enckey, sizeof(enckey), comment);
if (memcmp(enckey.kdfalg, KDFALG, 2))
errx(1, "unsupported KDF");
rounds = ntohl(enckey.kdfrounds);
kdf(enckey.salt, sizeof(enckey.salt), rounds, xorkey, sizeof(xorkey));
for (i = 0; i < sizeof(enckey.seckey); i++)
enckey.seckey[i] ^= xorkey[i];
memset(xorkey, 0, sizeof(xorkey));
SHA512Init(&ctx);
SHA512Update(&ctx, enckey.seckey, sizeof(enckey.seckey));
SHA512Final(digest, &ctx);
if (memcmp(enckey.checksum, digest, sizeof(enckey.checksum)))
errx(1, "incorrect passphrase");
memset(digest, 0, sizeof(digest));
msg = readmsg(msgfile, &msglen);
signmsg(enckey.seckey, msg, msglen, sig.sig);
memcpy(sig.fingerprint, enckey.fingerprint, FPLEN);
memset(&enckey, 0, sizeof(enckey));
memcpy(sig.pkalg, PKALG, 2);
snprintf(sigcomment, sizeof(sigcomment), "signature from %s", comment);
writeb64file(sigfile, sigcomment, &sig, sizeof(sig), O_TRUNC, 0666);
if (embedded)
appendall(sigfile, msg, msglen);
free(msg);
}
#endif
static void
verifymsg(uint8_t *pubkey, uint8_t *msg, unsigned long long msglen,
uint8_t *sig)
{
uint8_t *sigbuf, *dummybuf;
unsigned long long siglen, dummylen;
siglen = SIGBYTES + msglen;
sigbuf = xmalloc(siglen);
dummybuf = xmalloc(siglen);
memcpy(sigbuf, sig, SIGBYTES);
memcpy(sigbuf + SIGBYTES, msg, msglen);
if (crypto_sign_ed25519_open(dummybuf, &dummylen, sigbuf, siglen,
pubkey) == -1)
errx(1, "signature verification failed");
free(sigbuf);
free(dummybuf);
}
static void
verify(const char *pubkeyfile, const char *msgfile, const char *sigfile,
int embedded)
{
struct sig sig;
struct pubkey pubkey;
unsigned long long msglen, siglen = 0;
uint8_t *msg;
int fd;
msg = readmsg(embedded ? sigfile : msgfile, &msglen);
readb64file(pubkeyfile, &pubkey, sizeof(pubkey), NULL);
if (embedded) {
siglen = parseb64file(sigfile, msg, &sig, sizeof(sig), NULL);
msg += siglen;
msglen -= siglen;
} else {
readb64file(sigfile, &sig, sizeof(sig), NULL);
}
if (memcmp(pubkey.fingerprint, sig.fingerprint, FPLEN))
errx(1, "verification failed: checked against wrong key");
verifymsg(pubkey.pubkey, msg, msglen, sig.sig);
if (embedded) {
fd = xopen(msgfile, O_CREAT|O_TRUNC|O_NOFOLLOW|O_RDWR, 0666);
writeall(fd, msg, msglen, msgfile);
close(fd);
}
printf("verified\n");
free(msg - siglen);
}
int
main(int argc, char **argv)
{
const char *pubkeyfile = NULL, *seckeyfile = NULL, *msgfile = NULL,
*sigfile = NULL;
char sigfilebuf[1024];
int ch, rounds;
int embedded = 0;
enum {
NONE,
GENERATE,
SIGN,
VERIFY
} verb = NONE;
rounds = 42;
while ((ch = getopt(argc, argv, "GSVeno:p:s:")) != -1) {
switch (ch) {
#ifndef VERIFYONLY
case 'G':
if (verb)
usage();
verb = GENERATE;
break;
case 'S':
if (verb)
usage();
verb = SIGN;
break;
#endif
case 'V':
if (verb)
usage();
verb = VERIFY;
break;
case 'e':
embedded = 1;
break;
case 'n':
rounds = 0;
break;
case 'o':
sigfile = optarg;
break;
case 'p':
pubkeyfile = optarg;
break;
case 's':
seckeyfile = optarg;
break;
default:
usage();
break;
}
}
argc -= optind;
argv += optind;
#ifdef VERIFYONLY
if (verb != VERIFY)
#else
if (verb == NONE)
#endif
usage();
#ifndef VERIFYONLY
if (verb == GENERATE) {
if (!pubkeyfile || !seckeyfile || argc != 0)
usage();
generate(pubkeyfile, seckeyfile, rounds);
} else
#endif
{
if (argc != 1)
usage();
msgfile = argv[0];
if (!sigfile) {
if (snprintf(sigfilebuf, sizeof(sigfilebuf), "%s.sig",
msgfile) >= sizeof(sigfilebuf))
errx(1, "path too long");
sigfile = sigfilebuf;
}
#ifndef VERIFYONLY
if (verb == SIGN) {
if (!seckeyfile)
usage();
sign(seckeyfile, msgfile, sigfile, embedded);
} else
#endif
if (verb == VERIFY) {
if (!pubkeyfile)
usage();
verify(pubkeyfile, msgfile, sigfile, embedded);
}
}
return 0;
}
|