/* -*-c-*-
*
- * $Id: keyset.c,v 1.1 2001/02/03 20:26:37 mdw Exp $
+ * $Id: keyset.c,v 1.9 2003/10/15 09:29:38 mdw Exp $
*
* Handling of symmetric keysets
*
/*----- Revision history --------------------------------------------------*
*
* $Log: keyset.c,v $
+ * Revision 1.9 2003/10/15 09:29:38 mdw
+ * Cosmetic fix to changelog comment.
+ *
+ * Revision 1.8 2003/07/13 11:19:49 mdw
+ * Incompatible protocol fix! Include message type code under MAC tag to
+ * prevent cut-and-paste from key-exchange messages to general packet
+ * transport.
+ *
+ * Revision 1.7 2003/05/17 11:00:47 mdw
+ * Don't make scary messages just because one key didn't work on a message:
+ * only be frightened if they all fail. Set initial keyset refcount
+ * correctly.
+ *
+ * Revision 1.6 2003/04/06 10:26:35 mdw
+ * Report peer name on decrypt errors.
+ *
+ * Revision 1.5 2001/06/19 22:07:43 mdw
+ * Change the encrypted packet format to be non-malleable.
+ *
+ * Revision 1.4 2001/06/16 14:06:40 mdw
+ * Quantify collision probabilities for the stated data volume bounds.
+ *
+ * Revision 1.3 2001/02/16 21:39:55 mdw
+ * Major overhaul. Separate functions for manipulating keysets from
+ * functions for manipulating keyset lists. Introduce a concept of
+ * listening-only keys.
+ *
+ * Revision 1.2 2001/02/05 19:53:23 mdw
+ * Add sequence number protection.
+ *
* Revision 1.1 2001/02/03 20:26:37 mdw
* Initial checkin.
*
/*----- Tunable parameters ------------------------------------------------*/
-#define KEY_EXPTIME MIN(60) /* Expiry time for a key */
-#define KEY_REGENTIME MIN(45) /* Regeneration time for a key */
-#define KEY_EXPSZ MEG(512) /* Expiry data size for a key */
-#define KEY_REGENSZ MEG(256) /* Data size threshold for regen */
+/* --- Note on size limits --- *
+ *
+ * For a 64-bit block cipher (e.g., Blowfish), the probability of a collision
+ * occurring after 32 MB is less than %$2^{-21}$%, and the probability of a
+ * collision occurring after 64 MB is less than %$2^{-19}$%.
+ */
+
+#define T_EXP MIN(60) /* Expiry time for a key */
+#define T_REGEN MIN(45) /* Regeneration time for a key */
+#define SZ_EXP MEG(64) /* Expiry data size for a key */
+#define SZ_REGEN MEG(32) /* Data size threshold for regen */
/*----- Handy macros ------------------------------------------------------*/
#define KEYOK(ks, now) ((ks)->sz_exp > 0 && (ks)->t_exp > now)
-/*----- Main code ---------------------------------------------------------*/
+/*----- Low-level packet encryption and decryption ------------------------*/
-/* --- @freeks@ --- *
+/* --- Encrypted data format --- *
*
- * Arguments: @keyset *ks@ = pointer to a keyset
+ * Let %$p_i$% be the %$i$%-th plaintext message, with type %$t$%. We first
+ * compute
*
- * Returns: ---
+ * %$c_i = \mathcal{E}\textrm{-CBC}_{K_{\text{E}}}(p_i)$%
+ *
+ * as the CBC-ciphertext of %$p_i$%, and then
+ *
+ * %$\sigma_i = \mathcal{T}_{K_{\text{M}}}(t, i, c_i)$%
+ *
+ * as a MAC on the %%\emph{ciphertext}%%. The message sent is then the pair
+ * %$(\sigma_i, c_i)$%. This construction is provably secure in the NM-CCA
+ * sense (assuming that the cipher is IND-CPA, and the MAC is SUF-CMA)
+ * [Bellare and Namprempre].
*
- * Use: Frees a keyset.
+ * This also ensures that, assuming the key is good, we have a secure channel
+ * [Krawczyk]. Actually, [Krawczyk] shows that, if the cipher is either a
+ * simple stream cipher or a block cipher in CBC mode, we can use the MAC-
+ * then-encrypt scheme and still have a secure channel. However, I like the
+ * NM-CCA guarantee from [Bellare and Namprempre]. I'm less worried about
+ * the Horton Principle [Wagner and Schneier].
*/
-static void freeks(keyset *ks)
+/* --- @doencrypt@ --- *
+ *
+ * Arguments: @keyset *ks@ = pointer to keyset to use
+ * @unsigned ty@ = type of message this is
+ * @buf *b@ = pointer to an input buffer
+ * @buf *bb@ = pointer to an output buffer
+ *
+ * Returns: Zero if OK, nonzero if a new key is required.
+ *
+ * Use: Encrypts a message with the given key. We assume that the
+ * keyset is OK to use.
+ */
+
+static int doencrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
{
- ks->c->ops->destroy(ks->c);
- ks->m->ops->destroy(ks->m);
- DESTROY(ks);
+ ghash *h;
+ gcipher *c;
+ const octet *p = BCUR(b);
+ size_t sz = BLEFT(b);
+ octet *qmac, *qseq, *qiv, *qpk;
+ uint32 oseq;
+ size_t osz, nsz;
+ octet t[4];
+ int rc = 0;
+
+ /* --- Allocate the required buffer space --- */
+
+ c = ks->cout;
+ if (buf_ensure(bb, MACSZ + SEQSZ + IVSZ + sz))
+ return (0); /* Caution! */
+ qmac = BCUR(bb); qseq = qmac + MACSZ; qiv = qseq + SEQSZ; qpk = qiv + IVSZ;
+ BSTEP(bb, MACSZ + SEQSZ + IVSZ + sz);
+ STORE32(t, ty);
+
+ /* --- Encrypt the packet --- */
+
+ oseq = ks->oseq++; STORE32(qseq, oseq);
+ rand_get(RAND_GLOBAL, qiv, IVSZ);
+ c->ops->setiv(c, qiv);
+ c->ops->encrypt(c, p, qpk, sz);
+ IF_TRACING(T_KEYSET, {
+ trace(T_KEYSET, "keyset: encrypting packet %lu using keyset %u",
+ (unsigned long)oseq, ks->seq);
+ trace_block(T_CRYPTO, "crypto: encrypted packet", qpk, sz);
+ })
+
+ /* --- Now compute the MAC --- */
+
+ h = ks->mout->ops->init(ks->mout);
+ h->ops->hash(h, t, sizeof(t));
+ h->ops->hash(h, qseq, SEQSZ + IVSZ + sz);
+ memcpy(qmac, h->ops->done(h, 0), MACSZ);
+ h->ops->destroy(h);
+ IF_TRACING(T_KEYSET, {
+ trace_block(T_CRYPTO, "crypto: computed MAC", qmac, MACSZ);
+ })
+
+ /* --- Deduct the packet size from the key's data life --- */
+
+ osz = ks->sz_exp;
+ if (osz > sz)
+ nsz = osz - sz;
+ else
+ nsz = 0;
+ if (osz >= SZ_REGEN && nsz < SZ_REGEN) {
+ T( trace(T_KEYSET, "keyset: keyset %u data regen limit exceeded -- "
+ "forcing exchange", ks->seq); )
+ rc = -1;
+ }
+ ks->sz_exp = nsz;
+ return (rc);
}
-/* --- @ks_free@ --- *
+/* --- @dodecrypt@ --- *
*
- * Arguments: @keyset **ksroot@ = pointer to keyset list head
+ * Arguments: @keyset *ks@ = pointer to keyset to use
+ * @unsigned ty@ = expected type code
+ * @buf *b@ = pointer to an input buffer
+ * @buf *bb@ = pointer to an output buffer
+ * @uint32 *seq@ = where to store the sequence number
*
- * Returns: ---
+ * Returns: Zero if OK, nonzero if it failed.
*
- * Use: Frees all of the keys in a keyset.
+ * Use: Attempts to decrypt a message with the given key. No other
+ * checking (e.g., sequence number checks) is performed. We
+ * assume that the keyset is OK to use, and that there is
+ * sufficient output buffer space reserved. If the decryption
+ * is successful, the buffer pointer is moved past the decrypted
+ * packet, and the packet's sequence number is stored in @*seq@.
*/
-void ks_free(keyset **ksroot)
+static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq)
{
- keyset *ks, *ksn;
- for (ks = *ksroot; ks; ks = ksn) {
- ksn = ks->next;
- freeks(ks);
+ 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 = c->ops->c->blksz;
+ octet *mac;
+ int eq;
+ octet t[4];
+
+ /* --- Break up the packet into its components --- */
+
+ if (psz < ivsz + 4) {
+ T( trace(T_KEYSET, "keyset: block too small for keyset %u", ks->seq); )
+ return (-1);
}
+ sz = psz - IVSZ - SEQSZ - MACSZ;
+ pmac = BCUR(b); pseq = pmac + MACSZ; piv = pseq + SEQSZ; ppk = piv + IVSZ;
+ STORE32(t, ty);
+
+ /* --- Verify the MAC on the packet --- */
+
+ h = ks->min->ops->init(ks->min);
+ h->ops->hash(h, t, sizeof(t));
+ h->ops->hash(h, pseq, SEQSZ + IVSZ + sz);
+ mac = h->ops->done(h, 0);
+ eq = !memcmp(mac, pmac, MACSZ);
+ IF_TRACING(T_KEYSET, {
+ trace(T_KEYSET, "keyset: decrypting using keyset %u", ks->seq);
+ trace_block(T_CRYPTO, "crypto: computed MAC", mac, MACSZ);
+ })
+ h->ops->destroy(h);
+ if (!eq) {
+ IF_TRACING(T_KEYSET, {
+ trace(T_KEYSET, "keyset: incorrect MAC: decryption failed");
+ trace_block(T_CRYPTO, "crypto: expected MAC", pmac, MACSZ);
+ })
+ return (-1);
+ }
+
+ /* --- Decrypt the packet --- */
+
+ c->ops->setiv(c, piv);
+ c->ops->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);
+ })
+ BSTEP(bb, sz);
+ return (0);
}
-/* --- @ks_prune@ --- *
+/* --- @dosequence@ --- *
*
- * Arguments: @keyset **ksroot@ = pointer to keyset list head
+ * Arguments: @keyset *ks@ = pointer to a keyset
+ * @uint32 seq@ = a sequence number from a packet
*
- * Returns: ---
+ * Returns: Zero if the sequence number is OK, nonzero if it's not.
*
- * Use: Prunes the keyset list by removing keys which mustn't be used
- * any more.
+ * Use: Checks a sequence number. The data in the keyset which keeps
+ * track of valid sequence numbers is updated if the sequence
+ * number given is good. It's assumed that the sequence number
+ * has already been checked for authenticity.
*/
-void ks_prune(keyset **ksroot)
+static int dosequence(keyset *ks, uint32 seq)
{
- time_t now = time(0);
+ uint32 seqbit;
+ uint32 n;
- while (*ksroot) {
- keyset *ks = *ksroot;
- if (ks->t_exp <= now) {
- T( trace(T_KEYSET, "keyset: expiring keyset %u (time limit reached)",
- ks->seq); )
- *ksroot = ks->next;
- freeks(ks);
- } else if (ks->sz_exp == 0) {
- T( trace(T_KEYSET, "keyset: expiring keyset %u (data limit reached)",
- ks->seq); )
- *ksroot = ks->next;
- freeks(ks);
- } else
- ksroot = &ks->next;
+ if (seq < ks->iseq) {
+ a_warn("received packet has old sequence number (possible replay)");
+ return (-1);
+ }
+ if (seq >= ks->iseq + KS_SEQWINSZ) {
+ n = seq - (ks->iseq + KS_SEQWINSZ - 1);
+ if (n < KS_SEQWINSZ)
+ ks->iwin >>= n;
+ else
+ ks->iwin = 0;
+ ks->iseq += n;
}
+ seqbit = 1 << (seq - ks->iseq);
+ if (ks->iwin & seqbit) {
+ a_warn("received packet repeats old sequence number");
+ return (-1);
+ }
+ ks->iwin |= seqbit;
+ return (0);
+}
+
+/*----- Operations on a single keyset -------------------------------------*/
+
+/* --- @ks_drop@ --- *
+ *
+ * Arguments: @keyset *ks@ = pointer to a keyset
+ *
+ * Returns: ---
+ *
+ * Use: Decrements a keyset's reference counter. If the counter hits
+ * zero, the keyset is freed.
+ */
+
+void ks_drop(keyset *ks)
+{
+ if (--ks->ref)
+ return;
+ ks->cin->ops->destroy(ks->cin);
+ ks->cout->ops->destroy(ks->cout);
+ ks->min->ops->destroy(ks->min);
+ ks->mout->ops->destroy(ks->mout);
+ DESTROY(ks);
}
/* --- @ks_gen@ --- *
*
- * Arguments: @keyset **ksroot@ = pointer to keyset list head
- * @const void *k@ = pointer to key material
- * @size_t sz@ = size of the key material
+ * Arguments: @const void *k@ = pointer to key material
+ * @size_t x, y, z@ = offsets into key material (see below)
+ * @peer *p@ = pointer to peer information
+ *
+ * Returns: A pointer to the new keyset.
*
- * Returns: The regeneration time for the new key.
+ * Use: Derives a new keyset from the given key material. The
+ * offsets @x@, @y@ and @z@ separate the key material into three
+ * parts. Between the @k@ and @k + x@ is `my' contribution to
+ * the key material; between @k + x@ and @k + y@ is `your'
+ * contribution; and between @k + y@ and @k + z@ is a shared
+ * value we made together. These are used to construct two
+ * pairs of symmetric keys. Each pair consists of an encryption
+ * key and a message authentication key. One pair is used for
+ * outgoing messages, the other for incoming messages.
*
- * Use: Derives a keyset from the given key material and adds it to
- * the list.
+ * The new key is marked so that it won't be selected for output
+ * by @ksl_encrypt@. You can still encrypt data with it by
+ * calling @ks_encrypt@ directly.
*/
-time_t ks_gen(keyset **ksroot, const void *k, size_t sz)
+keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p)
{
- rmd160_ctx r;
- octet buf[RMD160_HASHSZ];
+ HASH_CTX h;
+ octet buf[HASHSZ];
keyset *ks = CREATE(keyset);
time_t now = time(0);
+ const octet *pp = k;
T( static unsigned seq = 0; )
T( trace(T_KEYSET, "keyset: adding new keyset %u", seq); )
-#define GETHASH(str) do { \
- rmd160_init(&r); \
- rmd160_hash(&r, str, sizeof(str) - 1); \
- rmd160_hash(&r, k, sz); \
- rmd160_done(&r, buf); \
+ /* --- Construct the various keys --- *
+ *
+ * This is done with macros, because it's quite tedious.
+ */
+
+#define MINE HASH(&h, pp, x)
+#define YOURS HASH(&h, pp + x, y - x)
+#define OURS HASH(&h, pp + y, z - y)
+
+#define IN MINE; YOURS; OURS
+#define OUT YOURS; MINE; OURS
+#define STR_IN "incoming"
+#define STR_OUT "outgoing"
+
+#define GETHASH(str, dir) do { \
+ HASH_INIT(&h); \
+ HASH_STRING(&h, "tripe-" str); \
+ dir; \
+ HASH_DONE(&h, buf); \
IF_TRACING(T_KEYSET, { \
- trace_block(T_CRYPTO, "crypto: key " str, buf, sizeof(buf)); \
+ trace_block(T_CRYPTO, "crypto: " STR_##dir " key " str, \
+ buf, sizeof(buf)); \
}) \
} while (0)
- GETHASH("tripe-encryption "); ks->c = blowfish_cbc.init(buf, sizeof(buf));
- GETHASH("tripe-integrity "); ks->m = rmd160_hmac.key(buf, sizeof(buf));
-
+ GETHASH("encryption", IN); ks->cin = CIPHER->init(buf, sizeof(buf));
+ GETHASH("integrity", IN); ks->min = MAC->key(buf, sizeof(buf));
+ GETHASH("encryption", OUT); ks->cout = CIPHER->init(buf, sizeof(buf));
+ GETHASH("integrity", OUT); ks->mout = MAC->key(buf, sizeof(buf));
+
+#undef MINE
+#undef YOURS
+#undef OURS
+#undef IN
+#undef OUT
+#undef STR_IN
+#undef STR_OUT
#undef GETHASH
T( ks->seq = seq++; )
- ks->t_exp = now + KEY_EXPTIME;
- ks->sz_exp = KEY_EXPSZ;
- ks->next = *ksroot;
- *ksroot = ks;
+ ks->ref = 1;
+ ks->t_exp = now + T_EXP;
+ ks->sz_exp = SZ_EXP;
+ ks->oseq = ks->iseq = 0;
+ ks->iwin = 0;
+ ks->next = 0;
+ ks->p = p;
+ ks->f = KSF_LISTEN;
BURN(buf);
- return (now + KEY_REGENTIME);
+ return (ks);
+}
+
+/* --- @ks_tregen@ --- *
+ *
+ * Arguments: @keyset *ks@ = pointer to a keyset
+ *
+ * Returns: The time at which moves ought to be made to replace this key.
+ */
+
+time_t ks_tregen(keyset *ks) { return (ks->t_exp - T_EXP + T_REGEN); }
+
+/* --- @ks_activate@ --- *
+ *
+ * Arguments: @keyset *ks@ = pointer to a keyset
+ *
+ * Returns: ---
+ *
+ * Use: Activates a keyset, so that it can be used for encrypting
+ * outgoing messages.
+ */
+
+void ks_activate(keyset *ks)
+{
+ if (ks->f & KSF_LISTEN) {
+ T( trace(T_KEYSET, "keyset: activating keyset %u", ks->seq); )
+ ks->f &= ~KSF_LISTEN;
+ }
}
/* --- @ks_encrypt@ --- *
+ *
+ * Arguments: @keyset *ks@ = pointer to a keyset
+ * @unsigned ty@ = message type
+ * @buf *b@ = pointer to input buffer
+ * @buf *bb@ = pointer to output buffer
+ *
+ * Returns: Zero if OK, nonzero if the key needs replacing. If the
+ * encryption failed, the output buffer is broken and zero is
+ * returned.
+ *
+ * Use: Encrypts a block of data using the key. Note that the `key
+ * 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.
+ */
+
+int ks_encrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
+{
+ time_t now = time(0);
+
+ if (!KEYOK(ks, now)) {
+ buf_break(bb);
+ return (0);
+ }
+ return (doencrypt(ks, ty, b, bb));
+}
+
+/* --- @ks_decrypt@ --- *
+ *
+ * Arguments: @keyset *ks@ = pointer to a keyset
+ * @unsigned ty@ = expected type code
+ * @buf *b@ = pointer to an input buffer
+ * @buf *bb@ = pointer to an output buffer
+ *
+ * Returns: Zero on success, or nonzero if there was some problem.
+ *
+ * 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.
+ */
+
+int ks_decrypt(keyset *ks, unsigned ty, buf *b, buf *bb)
+{
+ time_t now = time(0);
+ uint32 seq;
+
+ if (!KEYOK(ks, now) ||
+ buf_ensure(bb, BLEN(b)) ||
+ dodecrypt(ks, ty, b, bb, &seq) ||
+ dosequence(ks, seq))
+ return (-1);
+ return (0);
+}
+
+/*----- Keyset list handling ----------------------------------------------*/
+
+/* --- @ksl_free@ --- *
+ *
+ * Arguments: @keyset **ksroot@ = pointer to keyset list head
+ *
+ * Returns: ---
+ *
+ * Use: Frees (releases references to) all of the keys in a keyset.
+ */
+
+void ksl_free(keyset **ksroot)
+{
+ keyset *ks, *ksn;
+ for (ks = *ksroot; ks; ks = ksn) {
+ ksn = ks->next;
+ ks->f &= ~KSF_LINK;
+ ks_drop(ks);
+ }
+}
+
+/* --- @ksl_link@ --- *
+ *
+ * Arguments: @keyset **ksroot@ = pointer to keyset list head
+ * @keyset *ks@ = pointer to a keyset
+ *
+ * Returns: ---
+ *
+ * Use: Links a keyset into a list. A keyset can only be on one list
+ * at a time. Bad things happen otherwise.
+ */
+
+void ksl_link(keyset **ksroot, keyset *ks)
+{
+ assert(!(ks->f & KSF_LINK));
+ ks->next = *ksroot;
+ *ksroot = ks;
+ ks->f |= KSF_LINK;
+ ks->ref++;
+}
+
+/* --- @ksl_prune@ --- *
+ *
+ * Arguments: @keyset **ksroot@ = pointer to keyset list head
+ *
+ * Returns: ---
+ *
+ * Use: Prunes the keyset list by removing keys which mustn't be used
+ * any more.
+ */
+
+void ksl_prune(keyset **ksroot)
+{
+ time_t now = time(0);
+
+ while (*ksroot) {
+ keyset *ks = *ksroot;
+
+ if (ks->t_exp <= now) {
+ T( trace(T_KEYSET, "keyset: expiring keyset %u (time limit reached)",
+ ks->seq); )
+ goto kill;
+ } else if (ks->sz_exp == 0) {
+ T( trace(T_KEYSET, "keyset: expiring keyset %u (data limit reached)",
+ ks->seq); )
+ goto kill;
+ } else {
+ ksroot = &ks->next;
+ continue;
+ }
+
+ kill:
+ *ksroot = ks->next;
+ ks->f &= ~KSF_LINK;
+ ks_drop(ks);
+ }
+}
+
+/* --- @ksl_encrypt@ --- *
*
* Arguments: @keyset **ksroot@ = pointer to keyset list head
+ * @unsigned ty@ = message type
* @buf *b@ = pointer to input buffer
* @buf *bb@ = pointer to output buffer
*
* Use: Encrypts a packet.
*/
-int ks_encrypt(keyset **ksroot, buf *b, buf *bb)
+int ksl_encrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb)
{
time_t now = time(0);
- keyset *ks;
- ghash *h;
- gcipher *c;
- size_t ivsz;
- const octet *p = BCUR(b);
- octet *q = BCUR(bb);
- size_t sz = BLEFT(b);
- size_t osz, nsz;
- int rc = 0;
-
- /* --- Get the latest valid key --- */
+ keyset *ks = *ksroot;
- ks = *ksroot;
for (;;) {
if (!ks) {
- T( trace(T_KEYSET, "keyset: no active keys -- forcing exchange"); )
+ T( trace(T_KEYSET, "keyset: no suitable keysets found"); )
buf_break(bb);
return (-1);
}
- if (KEYOK(ks, now))
+ if (KEYOK(ks, now) && !(ks->f & KSF_LISTEN))
break;
ks = ks->next;
}
- /* --- MAC and encrypt the packet --- */
-
- c = ks->c;
- ivsz = c->ops->c->blksz;
- if (buf_ensure(bb, ivsz + sz))
- return (0);
- h = ks->m->ops->init(ks->m);
- h->ops->hash(h, p, sz);
- h->ops->done(h, q);
- IF_TRACING(T_KEYSET, {
- trace(T_KEYSET, "keyset: encrypting using keyset %u", ks->seq);
- trace_block(T_CRYPTO, "crypto: computed MAC", q, ivsz);
- })
- c->ops->setiv(c, q);
- h->ops->destroy(h);
-
- if (buf_ensure(bb, sz))
- return (0);
- c->ops->encrypt(c, p, q + ivsz, sz);
- IF_TRACING(T_KEYSET, {
- trace_block(T_CRYPTO, "crypto: encrypted packet", q + ivsz, sz);
- })
- BSTEP(bb, ivsz + sz);
-
- /* --- Deduct the packet size from the key's data life --- */
-
- osz = ks->sz_exp;
- if (osz > sz)
- nsz = osz - sz;
- else
- nsz = 0;
- if (osz >= KEY_REGENSZ && nsz < KEY_REGENSZ) {
- T( trace(T_KEYSET, "keyset: keyset %u data regen limit exceeded -- "
- "forcing exchange", ks->seq); )
- rc = -1;
- }
- ks->sz_exp = nsz;
- return (rc);
+ return (doencrypt(ks, ty, b, bb));
}
-/* --- @ks_decrypt@ --- *
+/* --- @ksl_decrypt@ --- *
*
* Arguments: @keyset **ksroot@ = pointer to keyset list head
+ * @unsigned ty@ = expected type code
* @buf *b@ = pointer to input buffer
* @buf *bb@ = pointer to output buffer
*
* Use: Decrypts a packet.
*/
-int ks_decrypt(keyset **ksroot, buf *b, buf *bb)
+int ksl_decrypt(keyset **ksroot, unsigned ty, buf *b, buf *bb)
{
time_t now = time(0);
- const octet *pp = BCUR(b);
- const octet *p;
- size_t sz = BLEFT(b);
- octet *q = BCUR(bb);
keyset *ks;
+ uint32 seq;
- T( trace(T_KEYSET, "keyset: attempting to decrypt packet"); )
- if (buf_ensure(bb, sz))
+ if (buf_ensure(bb, BLEN(b)))
return (-1);
- for (ks = *ksroot; ks; ks = ks->next) {
- ghash *h;
- gcipher *c = ks->c;
- size_t ivsz = c->ops->c->blksz;
- octet *mac;
- int eq;
+ for (ks = *ksroot; ks; ks = ks->next) {
if (!KEYOK(ks, now))
continue;
- if (sz < ivsz) {
- T( trace(T_KEYSET, "keyset: block too small for keyset %u", ks->seq); )
- continue;
+ if (!dodecrypt(ks, ty, b, bb, &seq)) {
+ if (ks->f & KSF_LISTEN) {
+ T( trace(T_KEYSET, "keyset: implicitly activating keyset %u",
+ ks->seq); )
+ ks->f &= ~KSF_LISTEN;
+ }
+ return (dosequence(ks, seq));
}
- p = pp + ivsz;
- c->ops->setiv(c, pp);
- c->ops->decrypt(c, p, q, sz - ivsz);
- h = ks->m->ops->init(ks->m);
- h->ops->hash(h, q, sz - ivsz);
- mac = h->ops->done(h, 0);
- eq = !memcmp(mac, pp, ivsz);
- IF_TRACING(T_KEYSET, {
- trace(T_KEYSET, "keyset: decrypting using keyset %u", ks->seq);
- trace_block(T_CRYPTO, "crypto: computed MAC", mac, ivsz);
- })
- h->ops->destroy(h);
- if (eq) {
- BSTEP(bb, sz - ivsz);
- IF_TRACING(T_KEYSET, {
- trace(T_KEYSET, "keyset: decrypted OK");
- trace_block(T_CRYPTO, "crypto: decrypted packet", q, sz - ivsz);
- })
- return (0);
- }
- IF_TRACING(T_KEYSET, {
- trace(T_KEYSET, "keyset: decryption failed");
- trace_block(T_CRYPTO, "crypto: expected MAC", pp, ivsz);
- })
}
- T( trace(T_KEYSET, "keyset: no matching keys"); )
+ T( trace(T_KEYSET, "keyset: no matching keys, or incorrect MAC"); )
return (-1);
}