X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/tripe/blobdiff_plain/62b5e3ecc77424add00ad4e5dc86e7248751cdf6..e14a412e8a38e5fd54c2f7db4a4c2f75dadbeef0:/server/keyset.c diff --git a/server/keyset.c b/server/keyset.c index f21a59a9..a94132b3 100644 --- a/server/keyset.c +++ b/server/keyset.c @@ -9,19 +9,18 @@ * * This file is part of Trivial IP Encryption (TrIPE). * - * TrIPE is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * TrIPE is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your + * option) any later version. * - * TrIPE is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * TrIPE is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. * * You should have received a copy of the GNU General Public License - * along with TrIPE; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * along with TrIPE. If not, see . */ /*----- Header files ------------------------------------------------------*/ @@ -32,8 +31,6 @@ #define KEYOK(ks, now) ((ks)->sz_exp > 0 && (ks)->t_exp > now) -#define SEQSZ 4 /* Size of sequence number packet */ - /*----- Low-level packet encryption and decryption ------------------------*/ /* --- Encrypted data format --- * @@ -77,73 +74,38 @@ static int doencrypt(keyset *ks, unsigned ty, buf *b, buf *bb) { - ghash *h; - gcipher *c = ks->cout; - const octet *p = BCUR(b); + int rc; size_t sz = BLEFT(b); - octet *qmac, *qseq, *qiv, *qpk; - uint32 oseq; - size_t ivsz = GC_CLASS(c)->blksz; - size_t tagsz = ks->tagsz; size_t osz, nsz; - octet t[4]; - int rc = 0; - - /* --- Allocate the required buffer space --- */ - if (buf_ensure(bb, tagsz + SEQSZ + ivsz + sz)) - return (0); /* Caution! */ - qmac = BCUR(bb); qseq = qmac + tagsz; qiv = qseq + SEQSZ; qpk = qiv + ivsz; - BSTEP(bb, tagsz + SEQSZ + ivsz + sz); - STORE32(t, ty); + /* --- Initial tracing --- */ - oseq = ks->oseq++; STORE32(qseq, oseq); IF_TRACING(T_KEYSET, { - trace(T_KEYSET, "keyset: encrypting packet %lu using keyset %u", - (unsigned long)oseq, ks->seq); - trace_block(T_CRYPTO, "crypto: plaintext packet", p, sz); + trace(T_KEYSET, + "keyset: encrypting packet %lu (type %u) using keyset %u", + (unsigned long)ks->oseq, ty, ks->seq); + trace_block(T_CRYPTO, "crypto: plaintext packet", BCUR(b), sz); }) - /* --- Encrypt the packet --- */ + /* --- Apply the bulk-crypto transformation --- */ - if (ivsz) { - rand_get(RAND_GLOBAL, qiv, ivsz); - GC_SETIV(c, qiv); - IF_TRACING(T_KEYSET, { - trace_block(T_CRYPTO, "crypto: initialization vector", qiv, ivsz); - }) - } - GC_ENCRYPT(c, p, qpk, sz); - IF_TRACING(T_KEYSET, { - trace_block(T_CRYPTO, "crypto: encrypted packet", qpk, sz); - }) - - /* --- Now compute the MAC --- */ - - if (tagsz) { - h = GM_INIT(ks->mout); - GH_HASH(h, t, sizeof(t)); - GH_HASH(h, qseq, SEQSZ + ivsz + sz); - memcpy(qmac, GH_DONE(h, 0), tagsz); - GH_DESTROY(h); - IF_TRACING(T_KEYSET, { - trace_block(T_CRYPTO, "crypto: computed MAC", qmac, tagsz); - }) - } + rc = ks->bulk->ops->encrypt(ks->bulk, ty, b, bb, ks->oseq); + if (rc || !BOK(bb)) return (rc); + ks->oseq++; - /* --- Deduct the packet size from the key's data life --- */ + /* --- Do the necessary accounting for data volume --- */ osz = ks->sz_exp; - if (osz > sz) - nsz = osz - sz; - else - nsz = 0; + nsz = osz > sz ? osz - sz : 0; if (osz >= ks->sz_regen && ks->sz_regen > nsz) { T( trace(T_KEYSET, "keyset: keyset %u data regen limit exceeded -- " "forcing exchange", ks->seq); ) rc = KSERR_REGEN; } ks->sz_exp = nsz; + + /* --- We're done --- */ + return (rc); } @@ -167,71 +129,24 @@ static int doencrypt(keyset *ks, unsigned ty, buf *b, buf *bb) static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq) { - const octet *pmac, *piv, *pseq, *ppk; - size_t psz = BLEFT(b); - size_t sz; - octet *q = BCUR(bb); - ghash *h; - gcipher *c = ks->cin; - size_t ivsz = GC_CLASS(c)->blksz; - size_t tagsz = ks->tagsz; - octet *mac; - int eq; - octet t[4]; - - /* --- Break up the packet into its components --- */ - - if (psz < ivsz + SEQSZ + tagsz) { - T( trace(T_KEYSET, "keyset: block too small for keyset %u", ks->seq); ) - return (KSERR_MALFORMED); - } - sz = psz - ivsz - SEQSZ - tagsz; - pmac = BCUR(b); pseq = pmac + tagsz; piv = pseq + SEQSZ; ppk = piv + ivsz; - STORE32(t, ty); + const octet *q = BCUR(bb); + int rc; IF_TRACING(T_KEYSET, { - trace(T_KEYSET, "keyset: decrypting using keyset %u", ks->seq); - trace_block(T_CRYPTO, "crypto: ciphertext packet", ppk, sz); + trace(T_KEYSET, + "keyset: try decrypting packet (type %u) using keyset %u", + ty, ks->seq); + trace_block(T_CRYPTO, "crypto: ciphertext packet", BCUR(b), BLEFT(b)); }) - /* --- Verify the MAC on the packet --- */ - - if (tagsz) { - h = GM_INIT(ks->min); - GH_HASH(h, t, sizeof(t)); - GH_HASH(h, pseq, SEQSZ + ivsz + sz); - mac = GH_DONE(h, 0); - eq = !memcmp(mac, pmac, tagsz); - IF_TRACING(T_KEYSET, { - trace_block(T_CRYPTO, "crypto: computed MAC", mac, tagsz); - }) - GH_DESTROY(h); - if (!eq) { - IF_TRACING(T_KEYSET, { - trace(T_KEYSET, "keyset: incorrect MAC: decryption failed"); - trace_block(T_CRYPTO, "crypto: expected MAC", pmac, tagsz); - }) - return (KSERR_DECRYPT); - } - } - - /* --- Decrypt the packet --- */ + rc = ks->bulk->ops->decrypt(ks->bulk, ty, b, bb, seq); + if (rc) return (rc); - if (ivsz) { - GC_SETIV(c, piv); - IF_TRACING(T_KEYSET, { - trace_block(T_CRYPTO, "crypto: initialization vector", piv, ivsz); - }) - } - GC_DECRYPT(c, ppk, q, sz); - if (seq) - *seq = LOAD32(pseq); IF_TRACING(T_KEYSET, { trace(T_KEYSET, "keyset: decrypted OK (sequence = %lu)", - (unsigned long)LOAD32(pseq)); - trace_block(T_CRYPTO, "crypto: decrypted packet", q, sz); + (unsigned long)*seq); + trace_block(T_CRYPTO, "crypto: decrypted packet", q, BCUR(bb) - q); }) - BSTEP(bb, sz); return (0); } @@ -249,15 +164,66 @@ static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq) void ks_drop(keyset *ks) { - if (--ks->ref) - return; - GC_DESTROY(ks->cin); - GC_DESTROY(ks->cout); - GM_DESTROY(ks->min); - GM_DESTROY(ks->mout); + if (--ks->ref) return; + ks->bulk->ops->freectx(ks->bulk); DESTROY(ks); } +/* --- @ks_derivekey@ --- * + * + * Arguments: @octet *k@ = pointer to an output buffer of at least + * @MAXHASHSZ@ bytes + * @size_t ksz@ = actual size wanted (for tracing) + * @const struct rawkey *rk@ = a raw key, as passed into + * @genkeys@ + * @int dir@ = direction for the key (@DIR_IN@ or @DIR_OUT@) + * @const char *what@ = label for the key (input to derivation) + * + * Returns: --- + * + * Use: Derives a session key, for use on incoming or outgoing data. + * This function is part of a private protocol between @ks_gen@ + * and the bulk crypto transform @genkeys@ operation. + */ + +struct rawkey { + const gchash *hc; + const octet *k; + size_t x, y, z; +}; + +void ks_derivekey(octet *k, size_t ksz, const struct rawkey *rk, + int dir, const char *what) +{ + const gchash *hc = rk->hc; + ghash *h; + + assert(ksz <= hc->hashsz); + assert(hc->hashsz <= MAXHASHSZ); + h = GH_INIT(hc); + GH_HASH(h, "tripe-", 6); GH_HASH(h, what, strlen(what) + 1); + switch (dir) { + case DIR_IN: + GH_HASH(h, rk->k, rk->x); + GH_HASH(h, rk->k + rk->x, rk->y - rk->x); + break; + case DIR_OUT: + GH_HASH(h, rk->k + rk->x, rk->y - rk->x); + GH_HASH(h, rk->k, rk->x); + break; + default: + abort(); + } + GH_HASH(h, rk->k + rk->y, rk->z - rk->y); + GH_DONE(h, k); + GH_DESTROY(h); + IF_TRACING(T_KEYSET, { IF_TRACING(T_CRYPTO, { + char _buf[32]; + sprintf(_buf, "crypto: %s key %s", dir ? "incoming" : "outgoing", what); + trace_block(T_CRYPTO, _buf, k, ksz); + }) }) +} + /* --- @ks_gen@ --- * * * Arguments: @const void *k@ = pointer to key material @@ -283,73 +249,28 @@ void ks_drop(keyset *ks) keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p) { - ghash *h; - const octet *hh; keyset *ks = CREATE(keyset); time_t now = time(0); - const octet *pp = k; + const algswitch *algs = &p->kx.kpriv->algs; + struct rawkey rk; T( static unsigned seq = 0; ) T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); ) - /* --- Construct the various keys --- * - * - * This is done with macros, because it's quite tedious. - */ - -#define MINE GH_HASH(h, pp, x) -#define YOURS GH_HASH(h, pp + x, y - x) -#define OURS GH_HASH(h, pp + y, z - y) - -#define HASH_in MINE; YOURS; OURS -#define HASH_out YOURS; MINE; OURS -#define INIT_c(k) GC_INIT(algs.c, (k), algs.cksz) -#define INIT_m(k) GM_KEY(algs.m, (k), algs.mksz) -#define STR_c "encryption" -#define STR_m "integrity" -#define STR_in "incoming" -#define STR_out "outgoing" - -#define SETKEY(a, dir) do { \ - h = GH_INIT(algs.h); \ - HASH_STRING(h, "tripe-" STR_##a); \ - HASH_##dir; \ - hh = GH_DONE(h, 0); \ - IF_TRACING(T_KEYSET, { \ - trace_block(T_CRYPTO, "crypto: " STR_##dir " key " STR_##a, \ - hh, algs.a##ksz); \ - }) \ - ks->a##dir = INIT_##a(hh); \ - GH_DESTROY(h); \ -} while (0) - - SETKEY(c, in); SETKEY(c, out); - SETKEY(m, in); SETKEY(m, out); - -#undef MINE -#undef YOURS -#undef OURS -#undef STR_c -#undef STR_m -#undef STR_in -#undef STR_out -#undef INIT_c -#undef INIT_m -#undef HASH_in -#undef HASH_out -#undef SETKEY + rk.hc = algs->h; rk.k = k; rk.x = x; rk.y = y; rk.z = z; + ks->bulk = algs->bulk->ops->genkeys(algs->bulk, &rk); + ks->bulk->ops = algs->bulk->ops; T( ks->seq = seq++; ) ks->ref = 1; ks->t_exp = now + T_EXP; - ks->sz_exp = algs.expsz; - ks->sz_regen = algs.expsz/2; + ks->sz_exp = algs->bulk->ops->expsz(algs->bulk); + ks->sz_regen = ks->sz_exp/2; ks->oseq = 0; seq_reset(&ks->iseq); ks->next = 0; ks->p = p; ks->f = KSF_LISTEN; - ks->tagsz = algs.tagsz; return (ks); } @@ -387,6 +308,11 @@ void ks_activate(keyset *ks) * ought to be replaced' notification is only ever given once * for each key. Also note that this call forces a keyset to be * used even if it's marked as not for data output. + * + * The encryption transform is permitted to corrupt @buf_u@ for + * its own purposes. Neither the source nor destination should + * be within @buf_u@; and callers mustn't expect anything stored + * in @buf_u@ to still */ int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb) @@ -414,6 +340,11 @@ int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb) * Use: Attempts to decrypt a message using a given key. Note that * requesting decryption with a key directly won't clear a * marking that it's not for encryption. + * + * The decryption transform is permitted to corrupt @buf_u@ for + * its own purposes. Neither the source nor destination should + * be within @buf_u@; and callers mustn't expect anything stored + * in @buf_u@ to still */ int ks_decrypt(keyset *ks, unsigned ty, buf *b, buf *bb)