3 * Key exchange protocol
5 * (c) 2001 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
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Brief protocol overview -------------------------------------------*
33 * Let %$G$% be a cyclic group; let %$g$% be a generator of %$G$%, and let
34 * %$q$% be the order of %$G$%; for a key %$K$%, let %$E_K(\cdot)$% denote
35 * application of the symmetric packet protocol to a message; let
36 * %$H(\cdot)$% be the random oracle. Let $\alpha \inr \{0,\ldots,q - 1\}$%
37 * be Alice's private key; let %$a = g^\alpha$% be her public key; let %$b$%
38 * be Bob's public key.
40 * At the beginning of the session, Alice chooses
42 * %$\rho_A \inr \{0, \ldots q - 1\}$%
46 * %$r_A = g^{\rho_A}$% Alice's challenge
47 * %$c_A = H(\cookie{cookie}, r_A)$% Alice's cookie
48 * %$v_A = \rho_A \xor H(\cookie{expected-reply}, a, r_A, r_B, b^{\rho_A})$%
49 * Alice's challenge check value
50 * %$r_B^\alpha = a^{\rho_B}$% Alice's reply
51 * %$K = r_B^{\rho_A} = r_B^{\rho_A} = g^{\rho_A\rho_B}$%
52 * Alice and Bob's shared secret key
53 * %$w_A = H(\cookie{switch-request}, c_A, c_B)$%
54 * Alice's switch request value
55 * %$u_A = H(\cookie{switch-confirm}, c_A, c_B)$%
56 * Alice's switch confirm value
58 * The messages are then:
60 * %$\cookie{kx-pre-challenge}, r_A$%
61 * Initial greeting. In state @KXS_CHAL@.
63 * %$\cookie{kx-challenge}, r_A, c_B, v_A$%
64 * Here's a full challenge for you to answer.
66 * %$\cookie{kx-reply}, r_A, c_B, v_A, E_K(r_B^\alpha))$%
67 * Challenge accpeted: here's the answer. Commit to my challenge. Move
70 * %$\cookie{kx-switch-rq}, c_A, c_B, E_K(r_B^\alpha, w_A))$%
71 * Reply received: here's my reply. Committed; send data; move to
74 * %$\cookie{kx-switch-ok}, E_K(u_A))$%
75 * Switch received. Committed; send data; move to @KXS_SWITCH@.
78 /*----- Tunable parameters ------------------------------------------------*/
80 #define T_VALID SEC(20) /* Challenge validity period */
81 #define T_RETRY SEC(10) /* Challenge retransmit interval */
83 #define VALIDP(kx, now) ((now) < (kx)->t_valid)
85 /*----- Static tables -----------------------------------------------------*/
87 static const char *const pkname[] = {
88 "pre-challenge", "challenge", "reply", "switch-rq", "switch-ok"
91 /*----- Various utilities -------------------------------------------------*/
95 * Arguments: @ghash *h@ = pointer to hash context
96 * @ge *x@ = pointer to group element
100 * Use: Adds the hash of a group element to the context. Corrupts
104 static void hashge(ghash *h, ge *x)
107 buf_init(&b, buf_t, sizeof(buf_t));
110 GH_HASH(h, BBASE(&b), BLEN(&b));
113 /* --- @mpmask@ --- *
115 * Arguments: @buf *b@ = output buffer
116 * @mp *x@ = the plaintext integer
117 * @size_t n@ = the expected size of the plaintext
118 * @const octet *k@ = pointer to key material
119 * @size_t ksz@ = size of the key
121 * Returns: Pointer to the output.
123 * Use: Masks a multiprecision integer: returns %$x \xor H(k)$%, so
124 * it's a random oracle thing rather than an encryption thing.
127 static octet *mpmask(buf *b, mp *x, size_t n, const octet *k, size_t ksz)
132 if ((p = buf_get(b, n)) == 0)
134 mgf = GC_INIT(algs.mgf, k, ksz);
135 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
136 trace(T_CRYPTO, "masking index = %s", mpstr(x));
137 trace_block(T_CRYPTO, "masking key", k, ksz);
139 mp_storeb(x, buf_t, n);
140 GC_ENCRYPT(mgf, buf_t, p, n);
141 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
142 trace_block(T_CRYPTO, "index plaintext", buf_t, n);
143 trace_block(T_CRYPTO, "masked ciphertext", p, n);
149 /* --- @mpunmask@ --- *
151 * Arguments: @mp *d@ = the output integer
152 * @const octet *p@ = pointer to the ciphertext
153 * @size_t n@ = the size of the ciphertext
154 * @const octet *k@ = pointer to key material
155 * @size_t ksz@ = size of the key
157 * Returns: The decrypted integer, or null.
159 * Use: Unmasks a multiprecision integer.
162 static mp *mpunmask(mp *d, const octet *p, size_t n,
163 const octet *k, size_t ksz)
167 mgf = GC_INIT(algs.mgf, k, ksz);
168 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
169 trace_block(T_CRYPTO, "unmasking key", k, ksz);
170 trace_block(T_CRYPTO, "masked ciphertext", p, n);
172 GC_DECRYPT(mgf, p, buf_t, n);
173 d = mp_loadb(d, buf_t, n);
174 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
175 trace_block(T_CRYPTO, "index plaintext", buf_t, n);
176 trace(T_CRYPTO, "unmasked index = %s", mpstr(d));
182 /* --- @hashcheck@ --- *
184 * Arguments: @ge *kpub@ = sender's public key
185 * @ge *cc@ = receiver's challenge
186 * @ge *c@ = sender's challenge
187 * @ge *y@ = reply to sender's challenge
189 * Returns: Pointer to the hash value (in @buf_t@)
191 * Use: Computes the check-value hash, used to mask or unmask
192 * indices to prove the validity of challenges. This computes
193 * the masking key used in challenge check values. This is
194 * really the heart of the whole thing, since it ensures that
195 * the index can be recovered from the history of hashing
196 * queries, which gives us (a) a proof that the authentication
197 * process is zero-knowledge, and (b) a proof that the whole
198 * key-exchange is deniable.
201 static const octet *hashcheck(ge *kpub, ge *cc, ge *c, ge *y)
203 ghash *h = GH_INIT(algs.h);
205 HASH_STRING(h, "tripe-expected-reply");
211 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
212 trace(T_CRYPTO, "computing challenge check hash");
213 trace(T_CRYPTO, "public key = %s", gestr(gg, kpub));
214 trace(T_CRYPTO, "receiver challenge = %s", gestr(gg, cc));
215 trace(T_CRYPTO, "sender challenge = %s", gestr(gg, c));
216 trace(T_CRYPTO, "sender reply = %s", gestr(gg, y));
217 trace_block(T_CRYPTO, "hash output", buf_t, algs.hashsz);
223 /* --- @sendchallenge@ --- *
225 * Arguments: @keyexch *kx@ = pointer to key exchange block
226 * @buf *b@ = output buffer for challenge
227 * @ge *c@ = peer's actual challenge
228 * @const octet *hc@ = peer's challenge cookie
232 * Use: Writes a full challenge to the message buffer.
235 static void sendchallenge(keyexch *kx, buf *b, ge *c, const octet *hc)
237 G_TOBUF(gg, b, kx->c);
238 buf_put(b, hc, algs.hashsz);
239 mpmask(b, kx->alpha, indexsz,
240 hashcheck(kpub, c, kx->c, kx->rx), algs.hashsz);
245 * Arguments: @struct timeval *tv@ = the current time
246 * @void *v@ = pointer to key exchange context
250 * Use: Acts when the key exchange timer goes off.
253 static void timer(struct timeval *tv, void *v)
257 T( trace(T_KEYEXCH, "keyexch: timer has popped"); )
261 /* --- @settimer@ --- *
263 * Arguments: @keyexch *kx@ = pointer to key exchange context
264 * @time_t t@ = when to set the timer for
268 * Use: Sets the timer for the next key exchange attempt.
271 static void settimer(keyexch *kx, time_t t)
274 if (kx->f & KXF_TIMER)
278 sel_addtimer(&sel, &kx->t, &tv, timer, kx);
282 /*----- Challenge management ----------------------------------------------*/
284 /* --- Notes on challenge management --- *
286 * We may get multiple different replies to our key exchange; some will be
287 * correct, some inserted by attackers. Up until @KX_THRESH@, all challenges
288 * received will be added to the table and given a full response. After
289 * @KX_THRESH@ distinct challenges are received, we return only a `cookie':
290 * our existing challenge, followed by a hash of the sender's challenge. We
291 * do %%\emph{not}%% give a bare challenge a reply slot at this stage. All
292 * properly-formed cookies are assigned a table slot: if none is spare, a
293 * used slot is randomly selected and destroyed. A cookie always receives a
297 /* --- @kxc_destroy@ --- *
299 * Arguments: @kxchal *kxc@ = pointer to the challenge block
303 * Use: Disposes of a challenge block.
306 static void kxc_destroy(kxchal *kxc)
308 if (kxc->f & KXF_TIMER)
309 sel_rmtimer(&kxc->t);
310 G_DESTROY(gg, kxc->c);
311 G_DESTROY(gg, kxc->r);
316 /* --- @kxc_stoptimer@ --- *
318 * Arguments: @kxchal *kxc@ = pointer to the challenge block
322 * Use: Stops the challenge's retry timer from sending messages.
323 * Useful when the state machine is in the endgame of the
327 static void kxc_stoptimer(kxchal *kxc)
329 if (kxc->f & KXF_TIMER)
330 sel_rmtimer(&kxc->t);
331 kxc->f &= ~KXF_TIMER;
334 /* --- @kxc_new@ --- *
336 * Arguments: @keyexch *kx@ = pointer to key exchange block
338 * Returns: A pointer to the challenge block.
340 * Use: Returns a pointer to a new challenge block to fill in.
343 static kxchal *kxc_new(keyexch *kx)
348 /* --- If we're over reply threshold, discard one at random --- */
350 if (kx->nr < KX_NCHAL)
353 i = rand_global.ops->range(&rand_global, KX_NCHAL);
354 kxc_destroy(kx->r[i]);
357 /* --- Fill in the new structure --- */
359 kxc = CREATE(kxchal);
360 kxc->c = G_CREATE(gg);
361 kxc->r = G_CREATE(gg);
369 /* --- @kxc_bychal@ --- *
371 * Arguments: @keyexch *kx@ = pointer to key exchange block
372 * @ge *c@ = challenge from remote host
374 * Returns: Pointer to the challenge block, or null.
376 * Use: Finds a challenge block, given its challenge.
379 static kxchal *kxc_bychal(keyexch *kx, ge *c)
383 for (i = 0; i < kx->nr; i++) {
384 if (G_EQ(gg, c, kx->r[i]->c))
390 /* --- @kxc_byhc@ --- *
392 * Arguments: @keyexch *kx@ = pointer to key exchange block
393 * @const octet *hc@ = challenge hash from remote host
395 * Returns: Pointer to the challenge block, or null.
397 * Use: Finds a challenge block, given a hash of its challenge.
400 static kxchal *kxc_byhc(keyexch *kx, const octet *hc)
404 for (i = 0; i < kx->nr; i++) {
405 if (memcmp(hc, kx->r[i]->hc, algs.hashsz) == 0)
411 /* --- @kxc_answer@ --- *
413 * Arguments: @keyexch *kx@ = pointer to key exchange block
414 * @kxchal *kxc@ = pointer to challenge block
418 * Use: Sends a reply to the remote host, according to the data in
419 * this challenge block.
422 static void kxc_answer(keyexch *kx, kxchal *kxc);
424 static void kxc_timer(struct timeval *tv, void *v)
427 kxc->f &= ~KXF_TIMER;
428 kxc_answer(kxc->kx, kxc);
431 static void kxc_answer(keyexch *kx, kxchal *kxc)
433 stats *st = p_stats(kx->p);
434 buf *b = p_txstart(kx->p, MSG_KEYEXCH | KX_REPLY);
438 /* --- Build the reply packet --- */
440 T( trace(T_KEYEXCH, "keyexch: sending reply to `%s'", p_name(kx->p)); )
441 sendchallenge(kx, b, kxc->c, kxc->hc);
442 buf_init(&bb, buf_i, sizeof(buf_i));
443 G_TORAW(gg, &bb, kxc->r);
445 ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_REPLY, &bb, b);
447 /* --- Update the statistics --- */
451 st->sz_kxout += BLEN(b);
455 /* --- Schedule another resend --- */
457 if (kxc->f & KXF_TIMER)
458 sel_rmtimer(&kxc->t);
459 gettimeofday(&tv, 0);
460 tv.tv_sec += T_RETRY;
461 sel_addtimer(&sel, &kxc->t, &tv, kxc_timer, kxc);
465 /*----- Individual message handlers ---------------------------------------*/
467 /* --- @doprechallenge@ --- *
469 * Arguments: @keyexch *kx@ = pointer to key exchange block
470 * @buf *b@ = buffer containing the packet
472 * Returns: Zero if OK, nonzero of the packet was rejected.
474 * Use: Processes a pre-challenge message.
477 static int doprechallenge(keyexch *kx, buf *b)
479 stats *st = p_stats(kx->p);
480 ge *c = G_CREATE(gg);
483 /* --- Ensure that we're in a sensible state --- */
485 if (kx->s != KXS_CHAL) {
486 a_warn("KX", "?PEER", kx->p, "unexpected", "pre-challenge", A_END);
490 /* --- Unpack the packet --- */
492 if (G_FROMBUF(gg, b, c) || BLEFT(b))
495 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
496 trace(T_CRYPTO, "crypto: challenge = %s", gestr(gg, c));
499 /* --- Send out a full challenge by return --- */
501 b = p_txstart(kx->p, MSG_KEYEXCH | KX_CHAL);
503 HASH_STRING(h, "tripe-cookie");
505 sendchallenge(kx, b, c, GH_DONE(h, 0));
508 st->sz_kxout += BLEN(b);
517 if (c) G_DESTROY(gg, c);
521 /* --- @respond@ --- *
523 * Arguments: @keyexch *kx@ = pointer to key exchange block
524 * @unsigned msg@ = message code for this packet
525 * @buf *b@ = buffer containing the packet
527 * Returns: Key-exchange challenge block, or null.
529 * Use: Computes a response for the given challenge, entering it into
530 * a challenge block and so on.
533 static kxchal *respond(keyexch *kx, unsigned msg, buf *b)
535 ge *c = G_CREATE(gg);
536 ge *r = G_CREATE(gg);
537 ge *cc = G_CREATE(gg);
538 const octet *hc, *ck;
546 /* --- Unpack the packet --- */
548 if (G_FROMBUF(gg, b, c) ||
549 (hc = buf_get(b, algs.hashsz)) == 0 ||
550 (ck = buf_get(b, indexsz)) == 0) {
551 a_warn("KX", "?PEER", kx->p, "invalid", "%s", pkname[msg], A_END);
554 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
555 trace(T_CRYPTO, "crypto: challenge = %s", gestr(gg, c));
556 trace_block(T_CRYPTO, "crypto: cookie", hc, algs.hashsz);
557 trace_block(T_CRYPTO, "crypto: check-value", ck, indexsz);
560 /* --- Discard a packet with an invalid cookie --- */
562 if (hc && memcmp(hc, kx->hc, algs.hashsz) != 0) {
563 a_warn("KX", "?PEER", kx->p, "incorrect", "cookie", A_END);
567 /* --- Recover the check value and verify it --- *
569 * To avoid recomputation on replays, we store a hash of the `right'
570 * value. The `correct' value is unique, so this is right.
572 * This will also find a challenge block and, if necessary, populate it.
575 if ((kxc = kxc_bychal(kx, c)) != 0) {
577 HASH_STRING(h, "tripe-check-hash");
578 GH_HASH(h, ck, indexsz);
579 ok = !memcmp(kxc->ck, GH_DONE(h, 0), algs.hashsz);
581 if (!ok) goto badcheck;
584 /* --- Compute the reply, and check the magic --- */
586 G_EXP(gg, r, c, kpriv);
587 cv = mpunmask(MP_NEW, ck, indexsz,
588 hashcheck(kx->kpub, kx->c, c, r), algs.hashsz);
589 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
590 trace(T_CRYPTO, "crypto: computed reply = %s", gestr(gg, r));
591 trace(T_CRYPTO, "crypto: recovered log = %s", mpstr(cv));
593 if (MP_CMP(cv, >, gg->r) ||
594 (G_EXP(gg, cc, gg->g, cv), !G_EQ(gg, c, cc)))
597 /* --- Fill in a new challenge block --- */
600 G_COPY(gg, kxc->c, c);
601 G_COPY(gg, kxc->r, r);
604 HASH_STRING(h, "tripe-check-hash");
605 GH_HASH(h, ck, indexsz);
610 HASH_STRING(h, "tripe-cookie");
615 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
616 trace_block(T_CRYPTO, "crypto: computed cookie", kxc->hc, algs.hashsz);
619 /* --- Work out the shared key --- */
621 G_EXP(gg, r, c, kx->alpha);
622 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
623 trace(T_CRYPTO, "crypto: shared secret = %s", gestr(gg, r));
626 /* --- Compute the switch messages --- */
628 h = GH_INIT(algs.h); HASH_STRING(h, "tripe-switch-request");
629 hashge(h, kx->c); hashge(h, kxc->c);
630 GH_DONE(h, kxc->hswrq_out); GH_DESTROY(h);
631 h = GH_INIT(algs.h); HASH_STRING(h, "tripe-switch-confirm");
632 hashge(h, kx->c); hashge(h, kxc->c);
633 GH_DONE(h, kxc->hswok_out); GH_DESTROY(h);
635 h = GH_INIT(algs.h); HASH_STRING(h, "tripe-switch-request");
636 hashge(h, kxc->c); hashge(h, kx->c);
637 GH_DONE(h, kxc->hswrq_in); GH_DESTROY(h);
638 h = GH_INIT(algs.h); HASH_STRING(h, "tripe-switch-confirm");
639 hashge(h, kxc->c); hashge(h, kx->c);
640 GH_DONE(h, kxc->hswok_in); GH_DESTROY(h);
642 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
643 trace_block(T_CRYPTO, "crypto: outbound switch request",
644 kxc->hswrq_out, algs.hashsz);
645 trace_block(T_CRYPTO, "crypto: outbound switch confirm",
646 kxc->hswok_out, algs.hashsz);
647 trace_block(T_CRYPTO, "crypto: inbound switch request",
648 kxc->hswrq_in, algs.hashsz);
649 trace_block(T_CRYPTO, "crypto: inbound switch confirm",
650 kxc->hswok_in, algs.hashsz);
653 /* --- Create a new symmetric keyset --- */
655 buf_init(&bb, buf_o, sizeof(buf_o));
656 G_TOBUF(gg, &bb, kx->c); x = BLEN(&bb);
657 G_TOBUF(gg, &bb, kxc->c); y = BLEN(&bb);
658 G_TOBUF(gg, &bb, r); z = BLEN(&bb);
661 kxc->ks = ks_gen(BBASE(&bb), x, y, z, kx->p);
671 a_warn("KX", "?PEER", kx->p, "bad-expected-reply-log", A_END);
681 /* --- @dochallenge@ --- *
683 * Arguments: @keyexch *kx@ = pointer to key exchange block
684 * @unsigned msg@ = message code for the packet
685 * @buf *b@ = buffer containing the packet
687 * Returns: Zero if OK, nonzero if the packet was rejected.
689 * Use: Processes a packet containing a challenge.
692 static int dochallenge(keyexch *kx, buf *b)
696 if (kx->s != KXS_CHAL) {
697 a_warn("KX", "?PEER", kx->p, "unexpected", "challenge", A_END);
700 if ((kxc = respond(kx, KX_CHAL, b)) == 0)
703 a_warn("KX", "?PEER", kx->p, "invalid", "challenge", A_END);
713 /* --- @resend@ --- *
715 * Arguments: @keyexch *kx@ = pointer to key exchange context
719 * Use: Sends the next message for a key exchange.
722 static void resend(keyexch *kx)
726 stats *st = p_stats(kx->p);
731 T( trace(T_KEYEXCH, "keyexch: sending prechallenge to `%s'",
733 b = p_txstart(kx->p, MSG_KEYEXCH | KX_PRECHAL);
734 G_TOBUF(gg, b, kx->c);
737 T( trace(T_KEYEXCH, "keyexch: sending switch request to `%s'",
740 b = p_txstart(kx->p, MSG_KEYEXCH | KX_SWITCH);
741 buf_put(b, kx->hc, algs.hashsz);
742 buf_put(b, kxc->hc, algs.hashsz);
743 buf_init(&bb, buf_i, sizeof(buf_i));
744 G_TORAW(gg, &bb, kxc->r);
745 buf_put(&bb, kxc->hswrq_out, algs.hashsz);
747 ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCH, &bb, b);
750 T( trace(T_KEYEXCH, "keyexch: sending switch confirmation to `%s'",
753 b = p_txstart(kx->p, MSG_KEYEXCH | KX_SWITCHOK);
754 buf_init(&bb, buf_i, sizeof(buf_i));
755 buf_put(&bb, kxc->hswok_out, algs.hashsz);
757 ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCHOK, &bb, b);
765 st->sz_kxout += BLEN(b);
769 if (kx->s < KXS_SWITCH)
770 settimer(kx, time(0) + T_RETRY);
773 /* --- @decryptrest@ --- *
775 * Arguments: @keyexch *kx@ = pointer to key exchange context
776 * @kxchal *kxc@ = pointer to challenge block
777 * @unsigned msg@ = type of incoming message
778 * @buf *b@ = encrypted remainder of the packet
780 * Returns: Zero if OK, nonzero on some kind of error.
782 * Use: Decrypts the remainder of the packet, and points @b@ at the
783 * recovered plaintext.
786 static int decryptrest(keyexch *kx, kxchal *kxc, unsigned msg, buf *b)
790 buf_init(&bb, buf_o, sizeof(buf_o));
791 if (ks_decrypt(kxc->ks, MSG_KEYEXCH | msg, b, &bb)) {
792 a_warn("KX", "?PEER", kx->p, "decrypt-failed", "%s", pkname[msg], A_END);
795 buf_init(b, BBASE(&bb), BLEN(&bb));
799 /* --- @checkresponse@ --- *
801 * Arguments: @keyexch *kx@ = pointer to key exchange context
802 * @unsigned msg@ = type of incoming message
803 * @buf *b@ = decrypted remainder of the packet
805 * Returns: Zero if OK, nonzero on some kind of error.
807 * Use: Checks a reply or switch packet, ensuring that its response
811 static int checkresponse(keyexch *kx, unsigned msg, buf *b)
813 ge *r = G_CREATE(gg);
815 if (G_FROMRAW(gg, b, r)) {
816 a_warn("KX", "?PEER", kx->p, "invalid", "%s", pkname[msg], A_END);
819 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
820 trace(T_CRYPTO, "crypto: reply = %s", gestr(gg, r));
822 if (!G_EQ(gg, r, kx->rx)) {
823 a_warn("KX", "?PEER", kx->p, "incorrect", "response", A_END);
835 /* --- @commit@ --- *
837 * Arguments: @keyexch *kx@ = pointer to key exchange context
838 * @kxchal *kxc@ = pointer to challenge to commit to
842 * Use: Commits to a particular challenge as being the `right' one,
843 * since a reply has arrived for it.
846 static void commit(keyexch *kx, kxchal *kxc)
850 for (i = 0; i < kx->nr; i++) {
852 kxc_destroy(kx->r[i]);
857 ksl_link(kx->ks, kxc->ks);
860 /* --- @doreply@ --- *
862 * Arguments: @keyexch *kx@ = pointer to key exchange context
863 * @buf *b@ = buffer containing packet
865 * Returns: Zero if OK, nonzero if the packet was rejected.
867 * Use: Handles a reply packet. This doesn't handle the various
868 * switch packets: they're rather too different.
871 static int doreply(keyexch *kx, buf *b)
875 if (kx->s != KXS_CHAL && kx->s != KXS_COMMIT) {
876 a_warn("KX", "?PEER", kx->p, "unexpected", "reply", A_END);
879 if ((kxc = respond(kx, KX_REPLY, b)) == 0 ||
880 decryptrest(kx, kxc, KX_REPLY, b) ||
881 checkresponse(kx, KX_REPLY, b))
884 a_warn("KX", "?PEER", kx->p, "invalid", "reply", A_END);
887 if (kx->s == KXS_CHAL) {
898 /* --- @kxfinish@ --- *
900 * Arguments: @keyexch *kx@ = pointer to key exchange block
904 * Use: Sets everything up following a successful key exchange.
907 static void kxfinish(keyexch *kx)
909 kxchal *kxc = kx->r[0];
910 ks_activate(kxc->ks);
911 settimer(kx, ks_tregen(kxc->ks));
913 a_notify("KXDONE", "?PEER", kx->p, A_END);
914 p_stats(kx->p)->t_kx = time(0);
917 /* --- @doswitch@ --- *
919 * Arguments: @keyexch *kx@ = pointer to key exchange block
920 * @buf *b@ = pointer to buffer containing packet
922 * Returns: Zero if OK, nonzero if the packet was rejected.
924 * Use: Handles a reply with a switch request bolted onto it.
927 static int doswitch(keyexch *kx, buf *b)
929 const octet *hc_in, *hc_out, *hswrq;
932 if ((hc_in = buf_get(b, algs.hashsz)) == 0 ||
933 (hc_out = buf_get(b, algs.hashsz)) == 0) {
934 a_warn("KX", "?PEER", kx->p, "invalid", "switch-rq", A_END);
937 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
938 trace_block(T_CRYPTO, "crypto: challenge", hc_in, algs.hashsz);
939 trace_block(T_CRYPTO, "crypto: cookie", hc_out, algs.hashsz);
941 if ((kxc = kxc_byhc(kx, hc_in)) == 0 ||
942 memcmp(hc_out, kx->hc, algs.hashsz) != 0) {
943 a_warn("KX", "?PEER", kx->p, "incorrect", "switch-rq", A_END);
946 if (decryptrest(kx, kxc, KX_SWITCH, b) ||
947 checkresponse(kx, KX_SWITCH, b))
949 if ((hswrq = buf_get(b, algs.hashsz)) == 0 || BLEFT(b)) {
950 a_warn("KX", "?PEER", kx->p, "invalid", "switch-rq", A_END);
953 IF_TRACING(T_KEYEXCH, {
954 trace_block(T_CRYPTO, "crypto: switch request hash", hswrq, algs.hashsz);
956 if (memcmp(hswrq, kxc->hswrq_in, algs.hashsz) != 0) {
957 a_warn("KX", "?PEER", kx->p, "incorrect", "switch-rq", A_END);
960 if (kx->s == KXS_CHAL)
962 if (kx->s < KXS_SWITCH)
971 /* --- @doswitchok@ --- *
973 * Arguments: @keyexch *kx@ = pointer to key exchange block
974 * @buf *b@ = pointer to buffer containing packet
976 * Returns: Zero if OK, nonzero if the packet was rejected.
978 * Use: Handles a reply with a switch request bolted onto it.
981 static int doswitchok(keyexch *kx, buf *b)
987 if (kx->s < KXS_COMMIT) {
988 a_warn("KX", "?PEER", kx->p, "unexpected", "switch-ok", A_END);
992 buf_init(&bb, buf_o, sizeof(buf_o));
993 if (decryptrest(kx, kxc, KX_SWITCHOK, b))
995 if ((hswok = buf_get(b, algs.hashsz)) == 0 || BLEFT(b)) {
996 a_warn("KX", "?PEER", kx->p, "invalid", "switch-ok", A_END);
999 IF_TRACING(T_KEYEXCH, {
1000 trace_block(T_CRYPTO, "crypto: switch confirmation hash",
1001 hswok, algs.hashsz);
1003 if (memcmp(hswok, kxc->hswok_in, algs.hashsz) != 0) {
1004 a_warn("KX", "?PEER", kx->p, "incorrect", "switch-ok", A_END);
1007 if (kx->s < KXS_SWITCH)
1015 /*----- Main code ---------------------------------------------------------*/
1019 * Arguments: @keyexch *kx@ = pointer to key exchange context
1023 * Use: Stops a key exchange dead in its tracks. Throws away all of
1024 * the context information. The context is left in an
1025 * inconsistent state. The only functions which understand this
1026 * state are @kx_free@ and @kx_init@ (which cause it internally
1027 * it), and @start@ (which expects it to be the prevailing
1031 static void stop(keyexch *kx)
1035 if (kx->f & KXF_DEAD)
1038 if (kx->f & KXF_TIMER)
1039 sel_rmtimer(&kx->t);
1040 for (i = 0; i < kx->nr; i++)
1041 kxc_destroy(kx->r[i]);
1043 G_DESTROY(gg, kx->c);
1044 G_DESTROY(gg, kx->rx);
1047 kx->f &= ~KXF_TIMER;
1050 /* --- @start@ --- *
1052 * Arguments: @keyexch *kx@ = pointer to key exchange context
1053 * @time_t now@ = the current time
1057 * Use: Starts a new key exchange with the peer. The context must be
1058 * in the bizarre state left by @stop@ or @kx_init@.
1061 static void start(keyexch *kx, time_t now)
1065 assert(kx->f & KXF_DEAD);
1067 kx->f &= ~(KXF_DEAD | KXF_CORK);
1069 kx->alpha = mprand_range(MP_NEW, gg->r, &rand_global, 0);
1070 kx->c = G_CREATE(gg); G_EXP(gg, kx->c, gg->g, kx->alpha);
1071 kx->rx = G_CREATE(gg); G_EXP(gg, kx->rx, kx->kpub, kx->alpha);
1073 kx->t_valid = now + T_VALID;
1075 h = GH_INIT(algs.h);
1076 HASH_STRING(h, "tripe-cookie");
1081 IF_TRACING(T_KEYEXCH, {
1082 trace(T_KEYEXCH, "keyexch: creating new challenge");
1083 IF_TRACING(T_CRYPTO, {
1084 trace(T_CRYPTO, "crypto: secret = %s", mpstr(kx->alpha));
1085 trace(T_CRYPTO, "crypto: challenge = %s", gestr(gg, kx->c));
1086 trace(T_CRYPTO, "crypto: expected response = %s", gestr(gg, kx->rx));
1087 trace_block(T_CRYPTO, "crypto: challenge cookie", kx->hc, algs.hashsz);
1092 /* --- @checkpub@ --- *
1094 * Arguments: @keyexch *kx@ = pointer to key exchange context
1096 * Returns: Zero if OK, nonzero if the peer's public key has expired.
1098 * Use: Deactivates the key-exchange until the peer acquires a new
1102 static int checkpub(keyexch *kx)
1105 if (kx->f & KXF_DEAD)
1108 if (KEY_EXPIRED(now, kx->texp_kpub)) {
1110 a_warn("KX", "?PEER", kx->p, "public-key-expired", A_END);
1111 G_COPY(gg, kx->kpub, gg->i);
1112 kx->f &= ~KXF_PUBKEY;
1118 /* --- @kx_start@ --- *
1120 * Arguments: @keyexch *kx@ = pointer to key exchange context
1121 * @int forcep@ = nonzero to ignore the quiet timer
1125 * Use: Stimulates a key exchange. If a key exchage is in progress,
1126 * a new challenge is sent (unless the quiet timer forbids
1127 * this); if no exchange is in progress, one is commenced.
1130 void kx_start(keyexch *kx, int forcep)
1132 time_t now = time(0);
1136 if (forcep || !VALIDP(kx, now)) {
1139 a_notify("KXSTART", "?PEER", kx->p, A_END);
1144 /* --- @kx_message@ --- *
1146 * Arguments: @keyexch *kx@ = pointer to key exchange context
1147 * @unsigned msg@ = the message code
1148 * @buf *b@ = pointer to buffer containing the packet
1152 * Use: Reads a packet containing key exchange messages and handles
1156 void kx_message(keyexch *kx, unsigned msg, buf *b)
1158 time_t now = time(0);
1159 stats *st = p_stats(kx->p);
1163 if (kx->f & KXF_CORK) {
1165 settimer(kx, now + T_RETRY);
1166 a_notify("KXSTART", A_END);
1172 if (!VALIDP(kx, now)) {
1176 T( trace(T_KEYEXCH, "keyexch: processing %s packet from `%s'",
1177 msg < KX_NMSG ? pkname[msg] : "unknown", p_name(kx->p)); )
1181 rc = doprechallenge(kx, b);
1184 rc = dochallenge(kx, b);
1187 rc = doreply(kx, b);
1190 rc = doswitch(kx, b);
1193 rc = doswitchok(kx, b);
1196 a_warn("KX", "?PEER", kx->p, "unknown-message", "0x%02x", msg, A_END);
1209 /* --- @kx_free@ --- *
1211 * Arguments: @keyexch *kx@ = pointer to key exchange context
1215 * Use: Frees everything in a key exchange context.
1218 void kx_free(keyexch *kx)
1221 G_DESTROY(gg, kx->kpub);
1224 /* --- @kx_newkeys@ --- *
1226 * Arguments: @keyexch *kx@ = pointer to key exchange context
1230 * Use: Informs the key exchange module that its keys may have
1231 * changed. If fetching the new keys fails, the peer will be
1232 * destroyed, we log messages and struggle along with the old
1236 void kx_newkeys(keyexch *kx)
1238 if (km_getpubkey(p_tag(kx->p), kx->kpub, &kx->texp_kpub))
1240 kx->f |= KXF_PUBKEY;
1241 if ((kx->f & KXF_DEAD) || kx->s != KXS_SWITCH) {
1242 T( trace(T_KEYEXCH, "keyexch: restarting key negotiation with `%s'",
1250 /* --- @kx_init@ --- *
1252 * Arguments: @keyexch *kx@ = pointer to key exchange context
1253 * @peer *p@ = pointer to peer context
1254 * @keyset **ks@ = pointer to keyset list
1255 * @unsigned f@ = various useful flags
1257 * Returns: Zero if OK, nonzero if it failed.
1259 * Use: Initializes a key exchange module. The module currently
1260 * contains no keys, and will attempt to initiate a key
1264 int kx_init(keyexch *kx, peer *p, keyset **ks, unsigned f)
1268 kx->kpub = G_CREATE(gg);
1269 if (km_getpubkey(p_tag(p), kx->kpub, &kx->texp_kpub)) {
1270 G_DESTROY(gg, kx->kpub);
1273 kx->f = KXF_DEAD | KXF_PUBKEY | f;
1274 if (!(kx->f & KXF_CORK)) {
1277 /* Don't notify here: the ADD message hasn't gone out yet. */
1282 /*----- That's all, folks -------------------------------------------------*/