aruu

[MIRROR] Actual replacement for Busybox | suckless
git clone https://git.daat.foo/aruu.git
Log | Files | Refs | README | LICENSE

commit ee7601cd438c7ee7eab356d497f7e487c2b56f11
parent ef95fc21c6913ca08228b9bb0ad508a515f86fa6
Author: xplshn <anto@xplshn.com.ar>
Date:   Thu, 26 Nov 2026 17:15:29 -0300

ed25519, base64, and signify: keys and signatures in openbsd's files

Diffstat:
Acmd/extra/signify.c | 151++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ashared/base64.h | 18++++++++++++++++++
Ashared/ed25519.h | 33+++++++++++++++++++++++++++++++++
Ashared/libutil/base64.c | 78++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ashared/libutil/ed25519.c | 457+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ashared/libutil/signify.c | 215+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ashared/signify.h | 43+++++++++++++++++++++++++++++++++++++++++++
7 files changed, 995 insertions(+), 0 deletions(-)

diff --git a/cmd/extra/signify.c b/cmd/extra/signify.c @@ -0,0 +1,151 @@ +/* see LICENSE file for copyright and license details */ +// ?man signify: make keys, sign and verify files +// ?man arguments: -G [-n] [-c comment] -p pubkey -s seckey | -S -s seckey -m message [-x sigfile] | -V [-q] -p pubkey -m message [-x sigfile] +// ?man signify signs a file with an ed25519 key and checks such a signature, +// ?man in the files openbsd's signify reads and writes. a secret key is kept +// ?man without a passphrase. the signature of message is message.sig unless +// ?man -x says otherwise +#include "arg.h" +#include "signify.h" +#include "util.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +static void +usage(void) +{ + fprintf(stderr, + "usage: %s -G [-n] [-c comment] -p pubkey -s seckey\n" + " %s -S -s seckey -m message [-x sigfile]\n" + " %s -V [-q] -p pubkey -m message [-x sigfile]\n", + argv0, argv0, argv0); + exit(1); +} + +static uint8_t * +slurp(const char *path, size_t *n) +{ + FILE *fp = fopen(path, "rb"); + uint8_t *buf = NULL; + size_t cap = 0, r; + + if (!fp) + eprintf("open %s:", path); + *n = 0; + do { + if (*n == cap) + buf = erealloc(buf, cap = cap ? cap * 2 : 65536); + r = fread(buf + *n, 1, cap - *n, fp); + *n += r; + } while (r > 0); + if (ferror(fp)) + eprintf("read %s:", path); + fclose(fp); + return buf; +} + +int +main(int argc, char *argv[]) +{ + const char *pub = NULL, *sec = NULL, *msg = NULL, *sig = NULL, *comment = "signify"; + char sigbuf[4096]; + int mode = 0, quiet = 0; + uint8_t *m; + size_t n; + + ARGBEGIN + { + // ?man -G: make a key pair + case 'G': + // ?man -S: sign message + case 'S': + // ?man -V: verify message + case 'V': + if (mode) + usage(); + mode = ARGC(); + break; + // ?man -c:str: -c comment: the comment of a new key pair + case 'c': + comment = EARGF(usage()); + break; + // ?man -m:str: -m message: the file signed or checked + case 'm': + msg = EARGF(usage()); + break; + // ?man -n: a key without a passphrase, which is the only kind made here + case 'n': + break; + // ?man -p:str: -p pubkey: the public key + case 'p': + pub = EARGF(usage()); + break; + // ?man -q: say nothing when the signature matches + case 'q': + quiet = 1; + break; + // ?man -s:str: -s seckey: the secret key + case 's': + sec = EARGF(usage()); + break; + // ?man -x:str: -x sigfile: the signature, message.sig when not given + case 'x': + sig = EARGF(usage()); + break; + default: + usage(); + } + ARGEND + + if (argc) + usage(); + if (mode != 'G' && msg && !sig) { + snprintf(sigbuf, sizeof(sigbuf), "%s.sig", msg); + sig = sigbuf; + } + switch (mode) { + case 'G': + if (!pub || !sec) + usage(); + return signify_generate(pub, sec, comment) != 0; + case 'S': { + struct signify_sec k; + const char *base; + char pubname[1024]; + size_t l; + + if (!sec || !msg) + usage(); + if (signify_read_sec(sec, &k)) + return 1; + // the comment names the public key the way signify does: the secret + // key's name with .pub for .sec + base = strrchr(sec, '/'); + base = base ? base + 1 : sec; + l = strlen(base); + if (l > 4 && !strcmp(base + l - 4, ".sec")) + snprintf(pubname, sizeof(pubname), "%.*s.pub", (int)(l - 4), base); + else + snprintf(pubname, sizeof(pubname), "%s", base); + m = slurp(msg, &n); + return signify_sign(&k, m, n, pubname, sig) != 0; + } + case 'V': { + struct signify_pub k; + + if (!pub || !msg) + usage(); + if (signify_read_pub(pub, &k)) + return 1; + m = slurp(msg, &n); + if (signify_verify(&k, m, n, sig)) + return 1; + if (!quiet) + puts("Signature Verified"); + return 0; + } + } + usage(); +} diff --git a/shared/base64.h b/shared/base64.h @@ -0,0 +1,18 @@ +/* see LICENSE file for copyright and license details */ +#ifndef BASE64_H +#define BASE64_H + +#include <stddef.h> +#include <stdint.h> + +/* the length base64_encode writes for n bytes, its terminator not counted */ +#define BASE64_LEN(n) ((((n) + 2) / 3) * 4) + +/* rfc 4648 base64 of in into out, terminated. returns the length */ +size_t base64_encode(char *out, const uint8_t *in, size_t n); + +/* the bytes of the n characters of in, into out (cap bytes at most). + blanks are skipped. returns their number, -1 on a bad or long input */ +long base64_decode(uint8_t *out, size_t cap, const char *in, size_t n); + +#endif diff --git a/shared/ed25519.h b/shared/ed25519.h @@ -0,0 +1,33 @@ +/* see LICENSE file for copyright and license details */ +#ifndef ED25519_H +#define ED25519_H + +#include <stddef.h> +#include <stdint.h> + +/* + * ed25519 signatures (rfc 8032), small and constant time where it matters, + * after tweetnacl. a secret key is the 32 byte seed followed by the public + * key, as nacl and signify keep it. hashing is aruu's sha512 + */ + +enum { + ED25519_SEED = 32, + ED25519_PUBLIC = 32, + ED25519_SECRET = 64, + ED25519_SIG = 64, +}; + +/* the key pair of seed: sk is seed then pk */ +void ed25519_keypair(uint8_t pk[ED25519_PUBLIC], uint8_t sk[ED25519_SECRET], + const uint8_t seed[ED25519_SEED]); + +/* sig over the n bytes of m */ +void ed25519_sign(uint8_t sig[ED25519_SIG], const uint8_t *m, size_t n, + const uint8_t sk[ED25519_SECRET]); + +/* 0 when sig is pk's over m, -1 otherwise */ +int ed25519_verify(const uint8_t sig[ED25519_SIG], const uint8_t *m, size_t n, + const uint8_t pk[ED25519_PUBLIC]); + +#endif diff --git a/shared/libutil/base64.c b/shared/libutil/base64.c @@ -0,0 +1,78 @@ +/* see LICENSE file for copyright and license details */ +#include <stddef.h> +#include <stdint.h> + +#include "../base64.h" + +static const char alphabet[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +size_t +base64_encode(char *out, const uint8_t *in, size_t n) +{ + size_t i, o = 0; + + for (i = 0; i + 2 < n; i += 3) { + out[o++] = alphabet[in[i] >> 2]; + out[o++] = alphabet[((in[i] & 3) << 4) | (in[i + 1] >> 4)]; + out[o++] = alphabet[((in[i + 1] & 15) << 2) | (in[i + 2] >> 6)]; + out[o++] = alphabet[in[i + 2] & 63]; + } + if (i < n) { + out[o++] = alphabet[in[i] >> 2]; + if (i + 1 < n) { + out[o++] = alphabet[((in[i] & 3) << 4) | (in[i + 1] >> 4)]; + out[o++] = alphabet[(in[i + 1] & 15) << 2]; + } else { + out[o++] = alphabet[(in[i] & 3) << 4]; + out[o++] = '='; + } + out[o++] = '='; + } + out[o] = '\0'; + return o; +} + +static int +value(char c) +{ + if (c >= 'A' && c <= 'Z') + return c - 'A'; + if (c >= 'a' && c <= 'z') + return c - 'a' + 26; + if (c >= '0' && c <= '9') + return c - '0' + 52; + if (c == '+') + return 62; + if (c == '/') + return 63; + return -1; +} + +long +base64_decode(uint8_t *out, size_t cap, const char *in, size_t n) +{ + uint32_t acc = 0; + size_t i, o = 0; + int bits = 0, pad = 0, v; + + for (i = 0; i < n; i++) { + if (in[i] == ' ' || in[i] == '\t' || in[i] == '\r' || in[i] == '\n') + continue; + if (in[i] == '=') { + pad++; + continue; + } + if (pad || (v = value(in[i])) < 0) + return -1; + acc = (acc << 6) | (uint32_t)v; + bits += 6; + if (bits >= 8) { + bits -= 8; + if (o >= cap) + return -1; + out[o++] = (acc >> bits) & 0xff; + } + } + return pad > 2 ? -1 : (long)o; +} diff --git a/shared/libutil/ed25519.c b/shared/libutil/ed25519.c @@ -0,0 +1,457 @@ +/* see LICENSE file for copyright and license details */ +/* field and group arithmetic after tweetnacl (public domain) */ +#include <stdint.h> +#include <string.h> + +#include "../ed25519.h" +#include "../sha512.h" + +typedef int64_t gf[16]; + +static const gf gf0; +static const gf gf1 = {1}; +static const gf D = {0x78a3, 0x1359, 0x4dca, 0x75eb, 0xd8ab, 0x4141, 0x0a4d, 0x0070, + 0xe898, 0x7779, 0x4079, 0x8cc7, 0xfe73, 0x2b6f, 0x6cee, 0x5203}; +static const gf D2 = {0xf159, 0x26b2, 0x9b94, 0xebd6, 0xb156, 0x8283, 0x149a, 0x00e0, + 0xd130, 0xeef3, 0x80f2, 0x198e, 0xfce7, 0x56df, 0xd9dc, 0x2406}; +static const gf X = {0xd51a, 0x8f25, 0x2d60, 0xc956, 0xa7b2, 0x9525, 0xc760, 0x692c, + 0xdc5c, 0xfdd6, 0xe231, 0xc0a4, 0x53fe, 0xcd6e, 0x36d3, 0x2169}; +static const gf Y = {0x6658, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, + 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666, 0x6666}; +static const gf I = {0xa0b0, 0x4a0e, 0x1b27, 0xc4ee, 0xe478, 0xad2f, 0x1806, 0x2f43, + 0xd7a7, 0x3dfb, 0x0099, 0x2b4d, 0xdf0b, 0x4fc1, 0x2480, 0x2b83}; +static const uint64_t L[32] = {0xed, 0xd3, 0xf5, 0x5c, 0x1a, 0x63, 0x12, 0x58, + 0xd6, 0x9c, 0xf7, 0xa2, 0xde, 0xf9, 0xde, 0x14, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x10}; + +static int +verify32(const uint8_t *x, const uint8_t *y) +{ + uint32_t d = 0; + int i; + + for (i = 0; i < 32; i++) + d |= x[i] ^ y[i]; + return (1 & ((d - 1) >> 8)) - 1; +} + +static void +set25519(gf r, const gf a) +{ + int i; + + for (i = 0; i < 16; i++) + r[i] = a[i]; +} + +static void +car25519(gf o) +{ + int64_t c; + int i; + + for (i = 0; i < 16; i++) { + o[i] += (int64_t)1 << 16; + c = o[i] >> 16; + o[(i + 1) * (i < 15)] += c - 1 + 37 * (c - 1) * (i == 15); + o[i] -= c * ((int64_t)1 << 16); + } +} + +static void +sel25519(gf p, gf q, int b) +{ + int64_t t, c = ~(b - 1); + int i; + + for (i = 0; i < 16; i++) { + t = c & (p[i] ^ q[i]); + p[i] ^= t; + q[i] ^= t; + } +} + +static void +pack25519(uint8_t *o, const gf n) +{ + int i, j, b; + gf m, t; + + for (i = 0; i < 16; i++) + t[i] = n[i]; + car25519(t); + car25519(t); + car25519(t); + for (j = 0; j < 2; j++) { + m[0] = t[0] - 0xffed; + for (i = 1; i < 15; i++) { + m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1); + m[i - 1] &= 0xffff; + } + m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1); + b = (m[15] >> 16) & 1; + m[14] &= 0xffff; + sel25519(t, m, 1 - b); + } + for (i = 0; i < 16; i++) { + o[2 * i] = t[i] & 0xff; + o[2 * i + 1] = t[i] >> 8; + } +} + +static int +neq25519(const gf a, const gf b) +{ + uint8_t c[32], d[32]; + + pack25519(c, a); + pack25519(d, b); + return verify32(c, d); +} + +static uint8_t +par25519(const gf a) +{ + uint8_t d[32]; + + pack25519(d, a); + return d[0] & 1; +} + +static void +unpack25519(gf o, const uint8_t *n) +{ + int i; + + for (i = 0; i < 16; i++) + o[i] = n[2 * i] + ((int64_t)n[2 * i + 1] << 8); + o[15] &= 0x7fff; +} + +static void +A(gf o, const gf a, const gf b) +{ + int i; + + for (i = 0; i < 16; i++) + o[i] = a[i] + b[i]; +} + +static void +Z(gf o, const gf a, const gf b) +{ + int i; + + for (i = 0; i < 16; i++) + o[i] = a[i] - b[i]; +} + +static void +M(gf o, const gf a, const gf b) +{ + int64_t t[31]; + int i, j; + + for (i = 0; i < 31; i++) + t[i] = 0; + for (i = 0; i < 16; i++) + for (j = 0; j < 16; j++) + t[i + j] += a[i] * b[j]; + for (i = 0; i < 15; i++) + t[i] += 38 * t[i + 16]; + for (i = 0; i < 16; i++) + o[i] = t[i]; + car25519(o); + car25519(o); +} + +static void +S(gf o, const gf a) +{ + M(o, a, a); +} + +static void +inv25519(gf o, const gf i) +{ + gf c; + int a; + + for (a = 0; a < 16; a++) + c[a] = i[a]; + for (a = 253; a >= 0; a--) { + S(c, c); + if (a != 2 && a != 4) + M(c, c, i); + } + for (a = 0; a < 16; a++) + o[a] = c[a]; +} + +static void +pow2523(gf o, const gf i) +{ + gf c; + int a; + + for (a = 0; a < 16; a++) + c[a] = i[a]; + for (a = 250; a >= 0; a--) { + S(c, c); + if (a != 1) + M(c, c, i); + } + for (a = 0; a < 16; a++) + o[a] = c[a]; +} + +static void +add(gf p[4], gf q[4]) +{ + gf a, b, c, d, t, e, f, g, h; + + Z(a, p[1], p[0]); + Z(t, q[1], q[0]); + M(a, a, t); + A(b, p[0], p[1]); + A(t, q[0], q[1]); + M(b, b, t); + M(c, p[3], q[3]); + M(c, c, D2); + M(d, p[2], q[2]); + A(d, d, d); + Z(e, b, a); + Z(f, d, c); + A(g, d, c); + A(h, b, a); + M(p[0], e, f); + M(p[1], h, g); + M(p[2], g, f); + M(p[3], e, h); +} + +static void +cswap(gf p[4], gf q[4], uint8_t b) +{ + int i; + + for (i = 0; i < 4; i++) + sel25519(p[i], q[i], b); +} + +static void +pack(uint8_t *r, gf p[4]) +{ + gf tx, ty, zi; + + inv25519(zi, p[2]); + M(tx, p[0], zi); + M(ty, p[1], zi); + pack25519(r, ty); + r[31] ^= par25519(tx) << 7; +} + +static void +scalarmult(gf p[4], gf q[4], const uint8_t *s) +{ + int i; + + set25519(p[0], gf0); + set25519(p[1], gf1); + set25519(p[2], gf1); + set25519(p[3], gf0); + for (i = 255; i >= 0; --i) { + uint8_t b = (s[i / 8] >> (i & 7)) & 1; + + cswap(p, q, b); + add(q, p); + add(p, p); + cswap(p, q, b); + } +} + +static void +scalarbase(gf p[4], const uint8_t *s) +{ + gf q[4]; + + set25519(q[0], X); + set25519(q[1], Y); + set25519(q[2], gf1); + M(q[3], X, Y); + scalarmult(p, q, s); +} + +static void +modL(uint8_t *r, int64_t x[64]) +{ + int64_t carry; + int i, j; + + for (i = 63; i >= 32; --i) { + carry = 0; + for (j = i - 32; j < i - 12; ++j) { + x[j] += carry - 16 * x[i] * (int64_t)L[j - (i - 32)]; + carry = (x[j] + 128) >> 8; + x[j] -= carry * 256; + } + x[j] += carry; + x[i] = 0; + } + carry = 0; + for (j = 0; j < 32; j++) { + x[j] += carry - (x[31] >> 4) * (int64_t)L[j]; + carry = x[j] >> 8; + x[j] &= 255; + } + for (j = 0; j < 32; j++) + x[j] -= carry * (int64_t)L[j]; + for (i = 0; i < 32; i++) { + x[i + 1] += x[i] >> 8; + r[i] = x[i] & 255; + } +} + +static void +reduce(uint8_t *r) +{ + int64_t x[64]; + int i; + + for (i = 0; i < 64; i++) + x[i] = (uint64_t)r[i]; + for (i = 0; i < 64; i++) + r[i] = 0; + modL(r, x); +} + +static int +unpackneg(gf r[4], const uint8_t p[32]) +{ + gf t, chk, num, den, den2, den4, den6; + + set25519(r[2], gf1); + unpack25519(r[1], p); + S(num, r[1]); + M(den, num, D); + Z(num, num, r[2]); + A(den, r[2], den); + S(den2, den); + S(den4, den2); + M(den6, den4, den2); + M(t, den6, num); + M(t, t, den); + pow2523(t, t); + M(t, t, num); + M(t, t, den); + M(t, t, den); + M(r[0], t, den); + S(chk, r[0]); + M(chk, chk, den); + if (neq25519(chk, num)) + M(r[0], r[0], I); + S(chk, r[0]); + M(chk, chk, den); + if (neq25519(chk, num)) + return -1; + if (par25519(r[0]) == (p[31] >> 7)) + Z(r[0], gf0, r[0]); + M(r[3], r[0], r[1]); + return 0; +} + +/* the secret scalar and the nonce prefix: sha512 of the seed, clamped */ +static void +expand(uint8_t d[64], const uint8_t seed[ED25519_SEED]) +{ + struct sha512 h; + + sha512_init(&h); + sha512_update(&h, seed, ED25519_SEED); + sha512_sum(&h, d); + d[0] &= 248; + d[31] &= 127; + d[31] |= 64; +} + +void +ed25519_keypair(uint8_t pk[ED25519_PUBLIC], uint8_t sk[ED25519_SECRET], + const uint8_t seed[ED25519_SEED]) +{ + uint8_t d[64]; + gf p[4]; + + expand(d, seed); + scalarbase(p, d); + pack(pk, p); + memcpy(sk, seed, ED25519_SEED); + memcpy(sk + ED25519_SEED, pk, ED25519_PUBLIC); + memset(d, 0, sizeof(d)); +} + +/* sha512 of a, b and m, reduced mod L */ +static void +hram(uint8_t out[64], const uint8_t *a, size_t na, const uint8_t *b, size_t nb, + const uint8_t *m, size_t n) +{ + struct sha512 h; + + sha512_init(&h); + sha512_update(&h, a, na); + if (nb) + sha512_update(&h, b, nb); + sha512_update(&h, m, n); + sha512_sum(&h, out); + reduce(out); +} + +void +ed25519_sign(uint8_t sig[ED25519_SIG], const uint8_t *m, size_t n, + const uint8_t sk[ED25519_SECRET]) +{ + uint8_t d[64], r[64], h[64]; + int64_t x[64]; + gf p[4]; + int i, j; + + expand(d, sk); + hram(r, d + 32, 32, NULL, 0, m, n); + scalarbase(p, r); + pack(sig, p); + hram(h, sig, 32, sk + ED25519_SEED, ED25519_PUBLIC, m, n); + for (i = 0; i < 64; i++) + x[i] = 0; + for (i = 0; i < 32; i++) + x[i] = (uint64_t)r[i]; + for (i = 0; i < 32; i++) + for (j = 0; j < 32; j++) + x[i + j] += h[i] * (uint64_t)d[j]; + modL(sig + 32, x); + memset(d, 0, sizeof(d)); + memset(r, 0, sizeof(r)); +} + +int +ed25519_verify(const uint8_t sig[ED25519_SIG], const uint8_t *m, size_t n, + const uint8_t pk[ED25519_PUBLIC]) +{ + uint8_t h[64], t[32]; + gf p[4], q[4]; + int i; + + /* s must be below L, or the signature is malleable */ + for (i = 31; i >= 0; i--) { + if (sig[32 + i] < L[i]) + break; + if (sig[32 + i] > L[i] || i == 0) + return -1; + } + if (unpackneg(q, pk)) + return -1; + hram(h, sig, 32, pk, ED25519_PUBLIC, m, n); + scalarmult(p, q, h); + scalarbase(q, sig + 32); + add(p, q); + pack(t, p); + return verify32(sig, t) ? -1 : 0; +} diff --git a/shared/libutil/signify.c b/shared/libutil/signify.c @@ -0,0 +1,215 @@ +/* see LICENSE file for copyright and license details */ +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +#include "../base64.h" +#include "../sha512.h" +#include "../signify.h" +#include "../util.h" + +#define COMMENT "untrusted comment: " + +/* the layouts signify writes, byte for byte */ +struct pubfile { + uint8_t alg[2]; + uint8_t keynum[8]; + uint8_t pk[ED25519_PUBLIC]; +}; + +struct secfile { + uint8_t alg[2]; + uint8_t kdfalg[2]; + uint8_t rounds[4]; + uint8_t salt[16]; + uint8_t checksum[8]; + uint8_t keynum[8]; + uint8_t sk[ED25519_SECRET]; +}; + +struct sigfile { + uint8_t alg[2]; + uint8_t keynum[8]; + uint8_t sig[ED25519_SIG]; +}; + +static int +randombytes(uint8_t *buf, size_t n) +{ + int fd = open("/dev/urandom", O_RDONLY); + ssize_t r; + size_t got = 0; + + if (fd < 0) { + weprintf("open /dev/urandom:"); + return -1; + } + while (got < n && (r = read(fd, buf + got, n - got)) > 0) + got += (size_t)r; + close(fd); + if (got < n) { + weprintf("read /dev/urandom:"); + return -1; + } + return 0; +} + +/* comment and blob into path, which must not be there yet */ +static int +writefile(const char *path, const char *comment, const void *blob, size_t n, int mode) +{ + char b64[256]; + FILE *fp; + int fd; + + if ((fd = open(path, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0) { + weprintf("open %s:", path); + return -1; + } + if (!(fp = fdopen(fd, "w"))) { + close(fd); + return -1; + } + base64_encode(b64, blob, n); + fprintf(fp, COMMENT "%s\n%s\n", comment, b64); + return fshut(fp, path) ? -1 : 0; +} + +/* the blob of path's second line, exactly n bytes of it */ +static int +readfile(const char *path, void *blob, size_t n) +{ + char line[512]; + FILE *fp; + int ok; + + if (!(fp = fopen(path, "r"))) { + weprintf("open %s:", path); + return -1; + } + ok = fgets(line, sizeof(line), fp) && !strncmp(line, COMMENT, strlen(COMMENT)) && + fgets(line, sizeof(line), fp) && + base64_decode(blob, n, line, strlen(line)) == (long)n; + fclose(fp); + if (!ok) { + weprintf("%s: not a signify file of the right kind\n", path); + return -1; + } + if (memcmp(blob, "Ed", 2)) { + weprintf("%s: not an ed25519 key or signature\n", path); + return -1; + } + return 0; +} + +static void +checksum(uint8_t out[8], const uint8_t sk[ED25519_SECRET]) +{ + struct sha512 h; + uint8_t d[SHA512_DIGEST_LENGTH]; + + sha512_init(&h); + sha512_update(&h, sk, ED25519_SECRET); + sha512_sum(&h, d); + memcpy(out, d, 8); +} + +int +signify_generate(const char *pubpath, const char *secpath, const char *comment) +{ + struct pubfile pub; + struct secfile sec; + uint8_t seed[ED25519_SEED]; + char c[200]; + int rc; + + memset(&sec, 0, sizeof(sec)); + if (randombytes(seed, sizeof(seed)) || randombytes(sec.keynum, sizeof(sec.keynum)) || + randombytes(sec.salt, sizeof(sec.salt))) + return -1; + ed25519_keypair(pub.pk, sec.sk, seed); + memcpy(sec.alg, "Ed", 2); + memcpy(sec.kdfalg, "BK", 2); + checksum(sec.checksum, sec.sk); + memcpy(pub.alg, "Ed", 2); + memcpy(pub.keynum, sec.keynum, 8); + + snprintf(c, sizeof(c), "%s secret key", comment); + rc = writefile(secpath, c, &sec, sizeof(sec), 0600); + if (rc == 0) { + snprintf(c, sizeof(c), "%s public key", comment); + rc = writefile(pubpath, c, &pub, sizeof(pub), 0644); + } + memset(&sec, 0, sizeof(sec)); + memset(seed, 0, sizeof(seed)); + return rc; +} + +int +signify_read_pub(const char *path, struct signify_pub *out) +{ + struct pubfile f; + + if (readfile(path, &f, sizeof(f))) + return -1; + memcpy(out->keynum, f.keynum, 8); + memcpy(out->pk, f.pk, ED25519_PUBLIC); + return 0; +} + +int +signify_read_sec(const char *path, struct signify_sec *out) +{ + struct secfile f; + uint8_t sum[8]; + + if (readfile(path, &f, sizeof(f))) + return -1; + if (memcmp(f.kdfalg, "BK", 2) || f.rounds[0] | f.rounds[1] | f.rounds[2] | f.rounds[3]) { + weprintf("%s: a key with a passphrase, make one without (signify -n)\n", path); + return -1; + } + checksum(sum, f.sk); + if (memcmp(sum, f.checksum, 8)) { + weprintf("%s: the key does not match its checksum\n", path); + return -1; + } + memcpy(out->keynum, f.keynum, 8); + memcpy(out->sk, f.sk, ED25519_SECRET); + memset(&f, 0, sizeof(f)); + return 0; +} + +int +signify_sign(const struct signify_sec *sec, const uint8_t *m, size_t n, const char *pubname, + const char *sigpath) +{ + struct sigfile f; + char c[300]; + + memcpy(f.alg, "Ed", 2); + memcpy(f.keynum, sec->keynum, 8); + ed25519_sign(f.sig, m, n, sec->sk); + snprintf(c, sizeof(c), "verify with %s", pubname); + unlink(sigpath); + return writefile(sigpath, c, &f, sizeof(f), 0644); +} + +int +signify_verify(const struct signify_pub *pub, const uint8_t *m, size_t n, const char *sigpath) +{ + struct sigfile f; + + if (readfile(sigpath, &f, sizeof(f))) + return -1; + if (memcmp(f.keynum, pub->keynum, 8)) { + weprintf("%s: signed with another key\n", sigpath); + return -1; + } + if (ed25519_verify(f.sig, m, n, pub->pk)) { + weprintf("%s: the signature does not match\n", sigpath); + return -1; + } + return 0; +} diff --git a/shared/signify.h b/shared/signify.h @@ -0,0 +1,43 @@ +/* see LICENSE file for copyright and license details */ +#ifndef SIGNIFY_H +#define SIGNIFY_H + +#include <stddef.h> +#include <stdint.h> + +#include "ed25519.h" + +/* + * openbsd signify's key and signature files, so either program reads what + * the other wrote: an "untrusted comment:" line, then a line of base64. the + * keys are ed25519. a secret key is kept unencrypted (kdf rounds 0); one + * made with a passphrase is refused here, signify -n makes one we take + */ + +struct signify_pub { + uint8_t keynum[8]; + uint8_t pk[ED25519_PUBLIC]; +}; + +struct signify_sec { + uint8_t keynum[8]; + uint8_t sk[ED25519_SECRET]; +}; + +/* a new key pair into pubpath and secpath, neither there before. 0 when made */ +int signify_generate(const char *pubpath, const char *secpath, const char *comment); + +/* the key in path. 0 when read, -1 said otherwise */ +int signify_read_pub(const char *path, struct signify_pub *out); +int signify_read_sec(const char *path, struct signify_sec *out); + +/* the signature of m into sigpath, its comment naming the public key + pubname to check it with. 0 when written */ +int signify_sign(const struct signify_sec *sec, const uint8_t *m, size_t n, + const char *pubname, const char *sigpath); + +/* 0 when sigpath is pub's signature of m, -1 said otherwise */ +int signify_verify(const struct signify_pub *pub, const uint8_t *m, size_t n, + const char *sigpath); + +#endif