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 /*----- Static tables -----------------------------------------------------*/
80 static const char *const pkname
[] = {
81 "pre-challenge", "challenge", "reply", "switch-rq", "switch-ok"
84 /*----- Various utilities -------------------------------------------------*/
88 * Arguments: @const keyexch *kx@ = key exchange state
89 * @time_t now@ = current time in seconds
91 * Returns: Whether the challenge in the key-exchange state is still
92 * valid or should be regenerated.
95 #define VALIDP(kx, now) ((now) < (kx)->t_valid)
99 * Arguments: @ghash *h@ = pointer to hash context
100 * @group *g@ = pointer to group
101 * @ge *x@ = pointer to group element
105 * Use: Adds the hash of a group element to the context. Corrupts
109 static void hashge(ghash
*h
, group
*g
, ge
*x
)
113 buf_init(&b
, buf_t
, sizeof(buf_t
));
116 GH_HASH(h
, BBASE(&b
), BLEN(&b
));
119 /* --- @mpmask@ --- *
121 * Arguments: @buf *b@ = output buffer
122 * @mp *x@ = the plaintext integer
123 * @size_t n@ = the expected size of the plaintext
124 * @gcipher *mgfc@ = mask-generating function to use
125 * @const octet *k@ = pointer to key material
126 * @size_t ksz@ = size of the key
128 * Returns: Pointer to the output.
130 * Use: Masks a multiprecision integer: returns %$x \xor H(k)$%, so
131 * it's a random oracle thing rather than an encryption thing.
134 static octet
*mpmask(buf
*b
, mp
*x
, size_t n
,
135 const gccipher
*mgfc
, const octet
*k
, size_t ksz
)
140 if ((p
= buf_get(b
, n
)) == 0)
142 mgf
= GC_INIT(mgfc
, k
, ksz
);
143 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
144 trace(T_CRYPTO
, "crypto: masking index = %s", mpstr(x
));
145 trace_block(T_CRYPTO
, "crypto: masking key", k
, ksz
);
147 mp_storeb(x
, buf_t
, n
);
148 GC_ENCRYPT(mgf
, buf_t
, p
, n
);
149 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
150 trace_block(T_CRYPTO
, "crypto: index plaintext", buf_t
, n
);
151 trace_block(T_CRYPTO
, "crypto: masked ciphertext", p
, n
);
157 /* --- @mpunmask@ --- *
159 * Arguments: @mp *d@ = the output integer
160 * @const octet *p@ = pointer to the ciphertext
161 * @size_t n@ = the size of the ciphertext
162 * @gcipher *mgfc@ = mask-generating function to use
163 * @const octet *k@ = pointer to key material
164 * @size_t ksz@ = size of the key
166 * Returns: The decrypted integer, or null.
168 * Use: Unmasks a multiprecision integer.
171 static mp
*mpunmask(mp
*d
, const octet
*p
, size_t n
,
172 const gccipher
*mgfc
, const octet
*k
, size_t ksz
)
176 mgf
= GC_INIT(mgfc
, k
, ksz
);
177 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
178 trace_block(T_CRYPTO
, "crypto: unmasking key", k
, ksz
);
179 trace_block(T_CRYPTO
, "crypto: masked ciphertext", p
, n
);
181 GC_DECRYPT(mgf
, p
, buf_t
, n
);
182 d
= mp_loadb(d
, buf_t
, n
);
183 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
184 trace_block(T_CRYPTO
, "crypto: index plaintext", buf_t
, n
);
185 trace(T_CRYPTO
, "crypto: unmasked index = %s", mpstr(d
));
191 /* --- @hashcheck@ --- *
193 * Arguments: @keyexch *kx@ = pointer to key-exchange block
194 * @ge *kpub@ = sender's public key
195 * @ge *cc@ = receiver's challenge
196 * @ge *c@ = sender's challenge
197 * @ge *y@ = reply to sender's challenge
199 * Returns: Pointer to the hash value (in @buf_t@)
201 * Use: Computes the check-value hash, used to mask or unmask
202 * indices to prove the validity of challenges. This computes
203 * the masking key used in challenge check values. This is
204 * really the heart of the whole thing, since it ensures that
205 * the index can be recovered from the history of hashing
206 * queries, which gives us (a) a proof that the authentication
207 * process is zero-knowledge, and (b) a proof that the whole
208 * key-exchange is deniable.
211 static const octet
*hashcheck(keyexch
*kx
, ge
*kpub
, ge
*cc
, ge
*c
, ge
*y
)
213 ghash
*h
= GH_INIT(kx
->kpriv
->algs
.h
);
214 group
*g
= kx
->kpriv
->g
;
216 HASH_STRING(h
, "tripe-expected-reply");
222 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
223 trace(T_CRYPTO
, "crypto: computing challenge check hash");
224 trace(T_CRYPTO
, "crypto: public key = %s", gestr(g
, kpub
));
225 trace(T_CRYPTO
, "crypto: receiver challenge = %s", gestr(g
, cc
));
226 trace(T_CRYPTO
, "crypto: sender challenge = %s", gestr(g
, c
));
227 trace(T_CRYPTO
, "crypto: sender reply = %s", gestr(g
, y
));
228 trace_block(T_CRYPTO
, "crypto: hash output", buf_t
, kx
->kpriv
->algs
.hashsz
);
234 /* --- @sendchallenge@ --- *
236 * Arguments: @keyexch *kx@ = pointer to key exchange block
237 * @buf *b@ = output buffer for challenge
238 * @ge *c@ = peer's actual challenge
239 * @const octet *hc@ = peer's challenge cookie
243 * Use: Writes a full challenge to the message buffer.
246 static void sendchallenge(keyexch
*kx
, buf
*b
, ge
*c
, const octet
*hc
)
248 G_TOBUF(kx
->kpriv
->g
, b
, kx
->c
);
249 buf_put(b
, hc
, kx
->kpriv
->algs
.hashsz
);
250 mpmask(b
, kx
->alpha
, kx
->kpriv
->indexsz
, kx
->kpriv
->algs
.mgf
,
251 hashcheck(kx
, kx
->kpriv
->kpub
, c
, kx
->c
, kx
->rx
),
252 kx
->kpriv
->algs
.hashsz
);
257 * Arguments: @struct timeval *tv@ = the current time
258 * @void *v@ = pointer to key exchange context
262 * Use: Acts when the key exchange timer goes off.
265 static void timer(struct timeval
*tv
, void *v
)
269 T( trace(T_KEYEXCH
, "keyexch: timer has popped"); )
273 /* --- @settimer@ --- *
275 * Arguments: @keyexch *kx@ = pointer to key exchange context
276 * @struct timeval *tv@ = when to set the timer for
280 * Use: Sets the timer for the next key exchange attempt.
283 static void settimer(keyexch
*kx
, struct timeval
*tv
)
285 if (kx
->f
& KXF_TIMER
) sel_rmtimer(&kx
->t
);
286 sel_addtimer(&sel
, &kx
->t
, tv
, timer
, kx
);
292 * Arguments: @struct timeval *tv@ = where to write the timeval
293 * @double t@ = a time as a floating point number
297 * Use: Converts a floating-point time into a timeval.
300 static void f2tv(struct timeval
*tv
, double t
)
303 tv
->tv_usec
= (t
- tv
->tv_sec
)*MILLION
;
306 /* --- @wobble@ --- *
308 * Arguments: @double t@ = a time interval
310 * Returns: The same time interval, with a random error applied.
313 static double wobble(double t
)
315 uint32 r
= rand_global
.ops
->word(&rand_global
);
316 double w
= (r
/F_2P32
) - 0.5;
317 return (t
+ t
*w
*T_WOBBLE
);
320 /* --- @rs_time@ --- *
322 * Arguments: @retry *rs@ = current retry state
323 * @struct timeval *tv@ = where to write the result
324 * @const struct timeval *now@ = current time, or null
328 * Use: Computes a time at which to retry sending a key-exchange
329 * packet. This algorithm is subject to change, but it's
330 * currently a capped exponential backoff, slightly randomized
331 * to try to keep clients from hammering a server that's only
334 * If @now@ is null then the function works out the time for
338 static void rs_time(retry
*rs
, struct timeval
*tv
, const struct timeval
*now
)
347 if (t
> MIN(5)) t
= MIN(5);
355 f2tv(&rtv
, wobble(t
));
356 TV_ADD(tv
, now
, &rtv
);
359 /* --- @retry_reset@ --- *
361 * Arguments: @retry *rs@ = retry state
365 * Use: Resets a retry state to indicate that progress has been
366 * made. Also useful for initializing the state in the first
370 static void rs_reset(retry
*rs
) { rs
->t
= 0; }
372 /*----- Challenge management ----------------------------------------------*/
374 /* --- Notes on challenge management --- *
376 * We may get multiple different replies to our key exchange; some will be
377 * correct, some inserted by attackers. Up until @KX_THRESH@, all challenges
378 * received will be added to the table and given a full response. After
379 * @KX_THRESH@ distinct challenges are received, we return only a `cookie':
380 * our existing challenge, followed by a hash of the sender's challenge. We
381 * do %%\emph{not}%% give a bare challenge a reply slot at this stage. All
382 * properly-formed cookies are assigned a table slot: if none is spare, a
383 * used slot is randomly selected and destroyed. A cookie always receives a
387 /* --- @kxc_destroy@ --- *
389 * Arguments: @kxchal *kxc@ = pointer to the challenge block
393 * Use: Disposes of a challenge block.
396 static void kxc_destroy(kxchal
*kxc
)
398 if (kxc
->f
& KXF_TIMER
)
399 sel_rmtimer(&kxc
->t
);
400 G_DESTROY(kxc
->kx
->kpriv
->g
, kxc
->c
);
401 G_DESTROY(kxc
->kx
->kpriv
->g
, kxc
->r
);
406 /* --- @kxc_stoptimer@ --- *
408 * Arguments: @kxchal *kxc@ = pointer to the challenge block
412 * Use: Stops the challenge's retry timer from sending messages.
413 * Useful when the state machine is in the endgame of the
417 static void kxc_stoptimer(kxchal
*kxc
)
419 if (kxc
->f
& KXF_TIMER
)
420 sel_rmtimer(&kxc
->t
);
421 kxc
->f
&= ~KXF_TIMER
;
424 /* --- @kxc_new@ --- *
426 * Arguments: @keyexch *kx@ = pointer to key exchange block
428 * Returns: A pointer to the challenge block.
430 * Use: Returns a pointer to a new challenge block to fill in.
433 static kxchal
*kxc_new(keyexch
*kx
)
438 /* --- If we're over reply threshold, discard one at random --- */
440 if (kx
->nr
< KX_NCHAL
)
443 i
= rand_global
.ops
->range(&rand_global
, KX_NCHAL
);
444 kxc_destroy(kx
->r
[i
]);
447 /* --- Fill in the new structure --- */
449 kxc
= CREATE(kxchal
);
450 kxc
->c
= G_CREATE(kx
->kpriv
->g
);
451 kxc
->r
= G_CREATE(kx
->kpriv
->g
);
460 /* --- @kxc_bychal@ --- *
462 * Arguments: @keyexch *kx@ = pointer to key exchange block
463 * @ge *c@ = challenge from remote host
465 * Returns: Pointer to the challenge block, or null.
467 * Use: Finds a challenge block, given its challenge.
470 static kxchal
*kxc_bychal(keyexch
*kx
, ge
*c
)
474 for (i
= 0; i
< kx
->nr
; i
++) {
475 if (G_EQ(kx
->kpriv
->g
, c
, kx
->r
[i
]->c
))
481 /* --- @kxc_byhc@ --- *
483 * Arguments: @keyexch *kx@ = pointer to key exchange block
484 * @const octet *hc@ = challenge hash from remote host
486 * Returns: Pointer to the challenge block, or null.
488 * Use: Finds a challenge block, given a hash of its challenge.
491 static kxchal
*kxc_byhc(keyexch
*kx
, const octet
*hc
)
495 for (i
= 0; i
< kx
->nr
; i
++) {
496 if (memcmp(hc
, kx
->r
[i
]->hc
, kx
->kpriv
->algs
.hashsz
) == 0)
502 /* --- @kxc_answer@ --- *
504 * Arguments: @keyexch *kx@ = pointer to key exchange block
505 * @kxchal *kxc@ = pointer to challenge block
509 * Use: Sends a reply to the remote host, according to the data in
510 * this challenge block.
513 static void kxc_answer(keyexch
*kx
, kxchal
*kxc
);
515 static void kxc_timer(struct timeval
*tv
, void *v
)
518 kxc
->f
&= ~KXF_TIMER
;
519 kxc_answer(kxc
->kx
, kxc
);
522 static void kxc_answer(keyexch
*kx
, kxchal
*kxc
)
524 stats
*st
= p_stats(kx
->p
);
525 buf
*b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_REPLY
);
529 /* --- Build the reply packet --- */
531 T( trace(T_KEYEXCH
, "keyexch: sending reply to `%s'", p_name(kx
->p
)); )
532 sendchallenge(kx
, b
, kxc
->c
, kxc
->hc
);
533 buf_init(&bb
, buf_i
, sizeof(buf_i
));
534 G_TORAW(kx
->kpriv
->g
, &bb
, kxc
->r
);
536 ks_encrypt(kxc
->ks
, MSG_KEYEXCH
| KX_REPLY
, &bb
, b
);
538 /* --- Update the statistics --- */
542 st
->sz_kxout
+= BLEN(b
);
546 /* --- Schedule another resend --- */
548 if (kxc
->f
& KXF_TIMER
)
549 sel_rmtimer(&kxc
->t
);
550 gettimeofday(&tv
, 0);
551 rs_time(&kxc
->rs
, &tv
, &tv
);
552 sel_addtimer(&sel
, &kxc
->t
, &tv
, kxc_timer
, kxc
);
556 /*----- Individual message handlers ---------------------------------------*/
558 /* --- @doprechallenge@ --- *
560 * Arguments: @keyexch *kx@ = pointer to key exchange block
561 * @buf *b@ = buffer containing the packet
563 * Returns: Zero if OK, nonzero of the packet was rejected.
565 * Use: Processes a pre-challenge message.
568 static int doprechallenge(keyexch
*kx
, buf
*b
)
570 stats
*st
= p_stats(kx
->p
);
571 ge
*c
= G_CREATE(kx
->kpriv
->g
);
574 /* --- Ensure that we're in a sensible state --- */
576 if (kx
->s
!= KXS_CHAL
) {
577 a_warn("KX", "?PEER", kx
->p
, "unexpected", "pre-challenge", A_END
);
581 /* --- Unpack the packet --- */
583 if (G_FROMBUF(kx
->kpriv
->g
, b
, c
) || BLEFT(b
))
586 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
587 trace(T_CRYPTO
, "crypto: challenge = %s", gestr(kx
->kpriv
->g
, c
));
590 /* --- Send out a full challenge by return --- */
592 b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_CHAL
);
593 h
= GH_INIT(kx
->kpriv
->algs
.h
);
594 HASH_STRING(h
, "tripe-cookie");
595 hashge(h
, kx
->kpriv
->g
, c
);
596 sendchallenge(kx
, b
, c
, GH_DONE(h
, 0));
599 st
->sz_kxout
+= BLEN(b
);
604 G_DESTROY(kx
->kpriv
->g
, c
);
608 if (c
) G_DESTROY(kx
->kpriv
->g
, c
);
612 /* --- @respond@ --- *
614 * Arguments: @keyexch *kx@ = pointer to key exchange block
615 * @unsigned msg@ = message code for this packet
616 * @buf *b@ = buffer containing the packet
618 * Returns: Key-exchange challenge block, or null.
620 * Use: Computes a response for the given challenge, entering it into
621 * a challenge block and so on.
624 static kxchal
*respond(keyexch
*kx
, unsigned msg
, buf
*b
)
626 group
*g
= kx
->kpriv
->g
;
627 const algswitch
*algs
= &kx
->kpriv
->algs
;
628 size_t ixsz
= kx
->kpriv
->indexsz
;
631 ge
*cc
= G_CREATE(g
);
632 const octet
*hc
, *ck
;
640 /* --- Unpack the packet --- */
642 if (G_FROMBUF(g
, b
, c
) ||
643 (hc
= buf_get(b
, algs
->hashsz
)) == 0 ||
644 (ck
= buf_get(b
, ixsz
)) == 0) {
645 a_warn("KX", "?PEER", kx
->p
, "invalid", "%s", pkname
[msg
], A_END
);
648 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
649 trace(T_CRYPTO
, "crypto: challenge = %s", gestr(g
, c
));
650 trace_block(T_CRYPTO
, "crypto: cookie", hc
, algs
->hashsz
);
651 trace_block(T_CRYPTO
, "crypto: check-value", ck
, ixsz
);
654 /* --- Discard a packet with an invalid cookie --- */
656 if (hc
&& memcmp(hc
, kx
->hc
, algs
->hashsz
) != 0) {
657 a_warn("KX", "?PEER", kx
->p
, "incorrect", "cookie", A_END
);
661 /* --- Recover the check value and verify it --- *
663 * To avoid recomputation on replays, we store a hash of the `right'
664 * value. The `correct' value is unique, so this is right.
666 * This will also find a challenge block and, if necessary, populate it.
669 if ((kxc
= kxc_bychal(kx
, c
)) != 0) {
670 h
= GH_INIT(algs
->h
);
671 HASH_STRING(h
, "tripe-check-hash");
672 GH_HASH(h
, ck
, ixsz
);
673 ok
= !memcmp(kxc
->ck
, GH_DONE(h
, 0), algs
->hashsz
);
675 if (!ok
) goto badcheck
;
678 /* --- Compute the reply, and check the magic --- */
680 G_EXP(g
, r
, c
, kx
->kpriv
->kpriv
);
681 cv
= mpunmask(MP_NEW
, ck
, ixsz
, algs
->mgf
,
682 hashcheck(kx
, kx
->kpub
->kpub
, kx
->c
, c
, r
),
684 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
685 trace(T_CRYPTO
, "crypto: computed reply = %s", gestr(g
, r
));
686 trace(T_CRYPTO
, "crypto: recovered log = %s", mpstr(cv
));
688 if (MP_CMP(cv
, >, g
->r
) ||
689 (G_EXP(g
, cc
, g
->g
, cv
),
693 /* --- Fill in a new challenge block --- */
696 G_COPY(g
, kxc
->c
, c
);
697 G_COPY(g
, kxc
->r
, r
);
699 h
= GH_INIT(algs
->h
); HASH_STRING(h
, "tripe-check-hash");
700 GH_HASH(h
, ck
, ixsz
);
701 GH_DONE(h
, kxc
->ck
); GH_DESTROY(h
);
703 h
= GH_INIT(algs
->h
); HASH_STRING(h
, "tripe-cookie");
704 hashge(h
, g
, kxc
->c
);
705 GH_DONE(h
, kxc
->hc
); GH_DESTROY(h
);
707 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
708 trace_block(T_CRYPTO
, "crypto: computed cookie",
709 kxc
->hc
, algs
->hashsz
);
712 /* --- Work out the shared key --- */
714 G_EXP(g
, r
, c
, kx
->alpha
);
715 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
716 trace(T_CRYPTO
, "crypto: shared secret = %s", gestr(g
, r
));
719 /* --- Compute the switch messages --- */
721 h
= GH_INIT(algs
->h
); HASH_STRING(h
, "tripe-switch-request");
722 hashge(h
, g
, kx
->c
); hashge(h
, g
, kxc
->c
);
723 GH_DONE(h
, kxc
->hswrq_out
); GH_DESTROY(h
);
724 h
= GH_INIT(algs
->h
); HASH_STRING(h
, "tripe-switch-confirm");
725 hashge(h
, g
, kx
->c
); hashge(h
, g
, kxc
->c
);
726 GH_DONE(h
, kxc
->hswok_out
); GH_DESTROY(h
);
728 h
= GH_INIT(algs
->h
); HASH_STRING(h
, "tripe-switch-request");
729 hashge(h
, g
, kxc
->c
); hashge(h
, g
, kx
->c
);
730 GH_DONE(h
, kxc
->hswrq_in
); GH_DESTROY(h
);
731 h
= GH_INIT(algs
->h
); HASH_STRING(h
, "tripe-switch-confirm");
732 hashge(h
, g
, kxc
->c
); hashge(h
, g
, kx
->c
);
733 GH_DONE(h
, kxc
->hswok_in
); GH_DESTROY(h
);
735 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
736 trace_block(T_CRYPTO
, "crypto: outbound switch request",
737 kxc
->hswrq_out
, algs
->hashsz
);
738 trace_block(T_CRYPTO
, "crypto: outbound switch confirm",
739 kxc
->hswok_out
, algs
->hashsz
);
740 trace_block(T_CRYPTO
, "crypto: inbound switch request",
741 kxc
->hswrq_in
, algs
->hashsz
);
742 trace_block(T_CRYPTO
, "crypto: inbound switch confirm",
743 kxc
->hswok_in
, algs
->hashsz
);
746 /* --- Create a new symmetric keyset --- */
748 buf_init(&bb
, buf_o
, sizeof(buf_o
));
749 G_TOBUF(g
, &bb
, kx
->c
); x
= BLEN(&bb
);
750 G_TOBUF(g
, &bb
, kxc
->c
); y
= BLEN(&bb
);
751 G_TOBUF(g
, &bb
, r
); z
= BLEN(&bb
);
754 kxc
->ks
= ks_gen(BBASE(&bb
), x
, y
, z
, kx
->p
);
764 a_warn("KX", "?PEER", kx
->p
, "bad-expected-reply-log", A_END
);
774 /* --- @dochallenge@ --- *
776 * Arguments: @keyexch *kx@ = pointer to key exchange block
777 * @unsigned msg@ = message code for the packet
778 * @buf *b@ = buffer containing the packet
780 * Returns: Zero if OK, nonzero if the packet was rejected.
782 * Use: Processes a packet containing a challenge.
785 static int dochallenge(keyexch
*kx
, buf
*b
)
789 if (kx
->s
!= KXS_CHAL
) {
790 a_warn("KX", "?PEER", kx
->p
, "unexpected", "challenge", A_END
);
793 if ((kxc
= respond(kx
, KX_CHAL
, b
)) == 0)
796 a_warn("KX", "?PEER", kx
->p
, "invalid", "challenge", A_END
);
806 /* --- @resend@ --- *
808 * Arguments: @keyexch *kx@ = pointer to key exchange context
812 * Use: Sends the next message for a key exchange.
815 static void resend(keyexch
*kx
)
819 stats
*st
= p_stats(kx
->p
);
825 T( trace(T_KEYEXCH
, "keyexch: sending prechallenge to `%s'",
827 b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_PRECHAL
);
828 G_TOBUF(kx
->kpriv
->g
, b
, kx
->c
);
831 T( trace(T_KEYEXCH
, "keyexch: sending switch request to `%s'",
834 b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_SWITCH
);
835 buf_put(b
, kx
->hc
, kx
->kpriv
->algs
.hashsz
);
836 buf_put(b
, kxc
->hc
, kx
->kpriv
->algs
.hashsz
);
837 buf_init(&bb
, buf_i
, sizeof(buf_i
));
838 G_TORAW(kx
->kpriv
->g
, &bb
, kxc
->r
);
839 buf_put(&bb
, kxc
->hswrq_out
, kx
->kpriv
->algs
.hashsz
);
841 ks_encrypt(kxc
->ks
, MSG_KEYEXCH
| KX_SWITCH
, &bb
, b
);
844 T( trace(T_KEYEXCH
, "keyexch: sending switch confirmation to `%s'",
847 b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_SWITCHOK
);
848 buf_init(&bb
, buf_i
, sizeof(buf_i
));
849 buf_put(&bb
, kxc
->hswok_out
, kx
->kpriv
->algs
.hashsz
);
851 ks_encrypt(kxc
->ks
, MSG_KEYEXCH
| KX_SWITCHOK
, &bb
, b
);
859 st
->sz_kxout
+= BLEN(b
);
863 if (kx
->s
< KXS_SWITCH
) {
864 rs_time(&kx
->rs
, &tv
, 0);
869 /* --- @decryptrest@ --- *
871 * Arguments: @keyexch *kx@ = pointer to key exchange context
872 * @kxchal *kxc@ = pointer to challenge block
873 * @unsigned msg@ = type of incoming message
874 * @buf *b@ = encrypted remainder of the packet
876 * Returns: Zero if OK, nonzero on some kind of error.
878 * Use: Decrypts the remainder of the packet, and points @b@ at the
879 * recovered plaintext.
882 static int decryptrest(keyexch
*kx
, kxchal
*kxc
, unsigned msg
, buf
*b
)
886 buf_init(&bb
, buf_o
, sizeof(buf_o
));
887 if (ks_decrypt(kxc
->ks
, MSG_KEYEXCH
| msg
, b
, &bb
)) {
888 a_warn("KX", "?PEER", kx
->p
, "decrypt-failed", "%s", pkname
[msg
], A_END
);
891 if (!BOK(&bb
)) return (-1);
892 buf_init(b
, BBASE(&bb
), BLEN(&bb
));
896 /* --- @checkresponse@ --- *
898 * Arguments: @keyexch *kx@ = pointer to key exchange context
899 * @unsigned msg@ = type of incoming message
900 * @buf *b@ = decrypted remainder of the packet
902 * Returns: Zero if OK, nonzero on some kind of error.
904 * Use: Checks a reply or switch packet, ensuring that its response
908 static int checkresponse(keyexch
*kx
, unsigned msg
, buf
*b
)
910 group
*g
= kx
->kpriv
->g
;
913 if (G_FROMRAW(g
, b
, r
)) {
914 a_warn("KX", "?PEER", kx
->p
, "invalid", "%s", pkname
[msg
], A_END
);
917 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
918 trace(T_CRYPTO
, "crypto: reply = %s", gestr(g
, r
));
920 if (!G_EQ(g
, r
, kx
->rx
)) {
921 a_warn("KX", "?PEER", kx
->p
, "incorrect", "response", A_END
);
933 /* --- @commit@ --- *
935 * Arguments: @keyexch *kx@ = pointer to key exchange context
936 * @kxchal *kxc@ = pointer to challenge to commit to
940 * Use: Commits to a particular challenge as being the `right' one,
941 * since a reply has arrived for it.
944 static void commit(keyexch
*kx
, kxchal
*kxc
)
948 for (i
= 0; i
< kx
->nr
; i
++) {
950 kxc_destroy(kx
->r
[i
]);
955 ksl_link(kx
->ks
, kxc
->ks
);
958 /* --- @doreply@ --- *
960 * Arguments: @keyexch *kx@ = pointer to key exchange context
961 * @buf *b@ = buffer containing packet
963 * Returns: Zero if OK, nonzero if the packet was rejected.
965 * Use: Handles a reply packet. This doesn't handle the various
966 * switch packets: they're rather too different.
969 static int doreply(keyexch
*kx
, buf
*b
)
973 if (kx
->s
!= KXS_CHAL
&& kx
->s
!= KXS_COMMIT
) {
974 a_warn("KX", "?PEER", kx
->p
, "unexpected", "reply", A_END
);
977 if ((kxc
= respond(kx
, KX_REPLY
, b
)) == 0 ||
978 decryptrest(kx
, kxc
, KX_REPLY
, b
) ||
979 checkresponse(kx
, KX_REPLY
, b
))
982 a_warn("KX", "?PEER", kx
->p
, "invalid", "reply", A_END
);
985 if (kx
->s
== KXS_CHAL
) {
996 /* --- @kxfinish@ --- *
998 * Arguments: @keyexch *kx@ = pointer to key exchange block
1002 * Use: Sets everything up following a successful key exchange.
1005 static void kxfinish(keyexch
*kx
)
1007 kxchal
*kxc
= kx
->r
[0];
1008 struct timeval now
, tv
;
1010 ks_activate(kxc
->ks
);
1011 gettimeofday(&now
, 0);
1012 f2tv(&tv
, wobble(T_REGEN
));
1013 TV_ADD(&tv
, &now
, &tv
);
1016 a_notify("KXDONE", "?PEER", kx
->p
, A_END
);
1017 p_stats(kx
->p
)->t_kx
= time(0);
1020 /* --- @doswitch@ --- *
1022 * Arguments: @keyexch *kx@ = pointer to key exchange block
1023 * @buf *b@ = pointer to buffer containing packet
1025 * Returns: Zero if OK, nonzero if the packet was rejected.
1027 * Use: Handles a reply with a switch request bolted onto it.
1030 static int doswitch(keyexch
*kx
, buf
*b
)
1032 size_t hsz
= kx
->kpriv
->algs
.hashsz
;
1033 const octet
*hc_in
, *hc_out
, *hswrq
;
1036 if ((hc_in
= buf_get(b
, hsz
)) == 0 ||
1037 (hc_out
= buf_get(b
, hsz
)) == 0) {
1038 a_warn("KX", "?PEER", kx
->p
, "invalid", "switch-rq", A_END
);
1041 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
1042 trace_block(T_CRYPTO
, "crypto: challenge", hc_in
, hsz
);
1043 trace_block(T_CRYPTO
, "crypto: cookie", hc_out
, hsz
);
1045 if ((kxc
= kxc_byhc(kx
, hc_in
)) == 0 ||
1046 memcmp(hc_out
, kx
->hc
, hsz
) != 0) {
1047 a_warn("KX", "?PEER", kx
->p
, "incorrect", "switch-rq", A_END
);
1050 if (decryptrest(kx
, kxc
, KX_SWITCH
, b
) ||
1051 checkresponse(kx
, KX_SWITCH
, b
))
1053 if ((hswrq
= buf_get(b
, hsz
)) == 0 || BLEFT(b
)) {
1054 a_warn("KX", "?PEER", kx
->p
, "invalid", "switch-rq", A_END
);
1057 IF_TRACING(T_KEYEXCH
, {
1058 trace_block(T_CRYPTO
, "crypto: switch request hash", hswrq
, hsz
);
1060 if (memcmp(hswrq
, kxc
->hswrq_in
, hsz
) != 0) {
1061 a_warn("KX", "?PEER", kx
->p
, "incorrect", "switch-rq", A_END
);
1064 if (kx
->s
== KXS_CHAL
)
1066 if (kx
->s
< KXS_SWITCH
)
1075 /* --- @doswitchok@ --- *
1077 * Arguments: @keyexch *kx@ = pointer to key exchange block
1078 * @buf *b@ = pointer to buffer containing packet
1080 * Returns: Zero if OK, nonzero if the packet was rejected.
1082 * Use: Handles a reply with a switch request bolted onto it.
1085 static int doswitchok(keyexch
*kx
, buf
*b
)
1087 size_t hsz
= kx
->kpriv
->algs
.hashsz
;
1092 if (kx
->s
< KXS_COMMIT
) {
1093 a_warn("KX", "?PEER", kx
->p
, "unexpected", "switch-ok", A_END
);
1097 buf_init(&bb
, buf_o
, sizeof(buf_o
));
1098 if (decryptrest(kx
, kxc
, KX_SWITCHOK
, b
))
1100 if ((hswok
= buf_get(b
, hsz
)) == 0 || BLEFT(b
)) {
1101 a_warn("KX", "?PEER", kx
->p
, "invalid", "switch-ok", A_END
);
1104 IF_TRACING(T_KEYEXCH
, {
1105 trace_block(T_CRYPTO
, "crypto: switch confirmation hash",
1108 if (memcmp(hswok
, kxc
->hswok_in
, hsz
) != 0) {
1109 a_warn("KX", "?PEER", kx
->p
, "incorrect", "switch-ok", A_END
);
1112 if (kx
->s
< KXS_SWITCH
)
1120 /*----- Main code ---------------------------------------------------------*/
1124 * Arguments: @keyexch *kx@ = pointer to key exchange context
1128 * Use: Stops a key exchange dead in its tracks. Throws away all of
1129 * the context information. The context is left in an
1130 * inconsistent state. The only functions which understand this
1131 * state are @kx_free@ and @kx_init@ (which cause it internally
1132 * it), and @start@ (which expects it to be the prevailing
1136 static void stop(keyexch
*kx
)
1140 if (kx
->f
& KXF_DEAD
)
1143 if (kx
->f
& KXF_TIMER
)
1144 sel_rmtimer(&kx
->t
);
1145 for (i
= 0; i
< kx
->nr
; i
++)
1146 kxc_destroy(kx
->r
[i
]);
1148 G_DESTROY(kx
->kpriv
->g
, kx
->c
);
1149 G_DESTROY(kx
->kpriv
->g
, kx
->rx
);
1152 kx
->f
&= ~KXF_TIMER
;
1155 /* --- @start@ --- *
1157 * Arguments: @keyexch *kx@ = pointer to key exchange context
1158 * @time_t now@ = the current time
1162 * Use: Starts a new key exchange with the peer. The context must be
1163 * in the bizarre state left by @stop@ or @kx_init@.
1166 static void start(keyexch
*kx
, time_t now
)
1168 algswitch
*algs
= &kx
->kpriv
->algs
;
1169 group
*g
= kx
->kpriv
->g
;
1172 assert(kx
->f
& KXF_DEAD
);
1174 kx
->f
&= ~(KXF_DEAD
| KXF_CORK
);
1176 kx
->alpha
= mprand_range(MP_NEW
, g
->r
, &rand_global
, 0);
1177 kx
->c
= G_CREATE(g
); G_EXP(g
, kx
->c
, g
->g
, kx
->alpha
);
1178 kx
->rx
= G_CREATE(g
); G_EXP(g
, kx
->rx
, kx
->kpub
->kpub
, kx
->alpha
);
1180 kx
->t_valid
= now
+ T_VALID
;
1182 h
= GH_INIT(algs
->h
);
1183 HASH_STRING(h
, "tripe-cookie");
1184 hashge(h
, g
, kx
->c
);
1188 IF_TRACING(T_KEYEXCH
, {
1189 trace(T_KEYEXCH
, "keyexch: creating new challenge");
1190 IF_TRACING(T_CRYPTO
, {
1191 trace(T_CRYPTO
, "crypto: secret = %s", mpstr(kx
->alpha
));
1192 trace(T_CRYPTO
, "crypto: challenge = %s", gestr(g
, kx
->c
));
1193 trace(T_CRYPTO
, "crypto: expected response = %s", gestr(g
, kx
->rx
));
1194 trace_block(T_CRYPTO
, "crypto: challenge cookie",
1195 kx
->hc
, algs
->hashsz
);
1200 /* --- @checkpub@ --- *
1202 * Arguments: @keyexch *kx@ = pointer to key exchange context
1204 * Returns: Zero if OK, nonzero if the peer's public key has expired.
1206 * Use: Deactivates the key-exchange until the peer acquires a new
1210 static int checkpub(keyexch
*kx
)
1215 if (kx
->f
& KXF_DEAD
)
1218 if (KEY_EXPIRED(now
, kx
->kpriv
->t_exp
)) f
|= 1;
1219 if (KEY_EXPIRED(now
, kx
->kpub
->t_exp
)) f
|= 2;
1222 if (f
& 1) a_warn("KX", "?PEER", kx
->p
, "private-key-expired", A_END
);
1223 if (f
& 2) a_warn("KX", "?PEER", kx
->p
, "public-key-expired", A_END
);
1224 kx
->f
&= ~KXF_PUBKEY
;
1230 /* --- @kx_start@ --- *
1232 * Arguments: @keyexch *kx@ = pointer to key exchange context
1233 * @int forcep@ = nonzero to ignore the quiet timer
1237 * Use: Stimulates a key exchange. If a key exchage is in progress,
1238 * a new challenge is sent (unless the quiet timer forbids
1239 * this); if no exchange is in progress, one is commenced.
1242 void kx_start(keyexch
*kx
, int forcep
)
1244 time_t now
= time(0);
1248 if (forcep
|| !VALIDP(kx
, now
)) {
1251 a_notify("KXSTART", "?PEER", kx
->p
, A_END
);
1256 /* --- @kx_message@ --- *
1258 * Arguments: @keyexch *kx@ = pointer to key exchange context
1259 * @unsigned msg@ = the message code
1260 * @buf *b@ = pointer to buffer containing the packet
1264 * Use: Reads a packet containing key exchange messages and handles
1268 void kx_message(keyexch
*kx
, unsigned msg
, buf
*b
)
1270 struct timeval now
, tv
;
1271 stats
*st
= p_stats(kx
->p
);
1275 gettimeofday(&now
, 0);
1277 if (kx
->f
& KXF_CORK
) {
1278 start(kx
, now
.tv_sec
);
1279 rs_time(&kx
->rs
, &tv
, &now
);
1281 a_notify("KXSTART", "?PEER", kx
->p
, A_END
);
1287 if (!VALIDP(kx
, now
.tv_sec
)) {
1289 start(kx
, now
.tv_sec
);
1291 T( trace(T_KEYEXCH
, "keyexch: processing %s packet from `%s'",
1292 msg
< KX_NMSG ? pkname
[msg
] : "unknown", p_name(kx
->p
)); )
1296 rc
= doprechallenge(kx
, b
);
1299 rc
= dochallenge(kx
, b
);
1302 rc
= doreply(kx
, b
);
1305 rc
= doswitch(kx
, b
);
1308 rc
= doswitchok(kx
, b
);
1311 a_warn("KX", "?PEER", kx
->p
, "unknown-message", "0x%02x", msg
, A_END
);
1324 /* --- @kx_free@ --- *
1326 * Arguments: @keyexch *kx@ = pointer to key exchange context
1330 * Use: Frees everything in a key exchange context.
1333 void kx_free(keyexch
*kx
)
1337 km_unref(kx
->kpriv
);
1340 /* --- @kx_newkeys@ --- *
1342 * Arguments: @keyexch *kx@ = pointer to key exchange context
1346 * Use: Informs the key exchange module that its keys may have
1347 * changed. If fetching the new keys fails, the peer will be
1348 * destroyed, we log messages and struggle along with the old
1352 void kx_newkeys(keyexch
*kx
)
1354 kdata
*kpriv
, *kpub
;
1357 time_t now
= time(0);
1359 T( trace(T_KEYEXCH
, "keyexch: checking new keys for `%s'",
1362 /* --- Find out whether we can use new keys --- *
1364 * Try each available combination of new and old, public and private,
1365 * except both old (which is status quo anyway). The selection is encoded
1366 * in @i@, with bit 0 for the private key and bit 1 for public key; a set
1367 * bit means to use the old value, and a clear bit means to use the new
1370 * This means that we currently prefer `old private and new public' over
1371 * `new private and old public'. I'm not sure which way round this should
1375 for (i
= 0; i
< 3; i
++) {
1377 /* --- Select the keys we're going to examine --- *
1379 * If we're meant to have a new key and don't, then skip this
1383 T( trace(T_KEYEXCH
, "keyexch: checking %s private, %s public",
1384 i
& 1 ?
"old" : "new", i
& 2 ?
"old" : "new"); )
1386 if (i
& 1) kpriv
= kx
->kpriv
;
1387 else if (kx
->kpriv
->kn
->kd
!= kx
->kpriv
) kpriv
= kx
->kpriv
->kn
->kd
;
1389 T( trace(T_KEYEXCH
, "keyexch: private key unchanged, skipping"); )
1393 if (i
& 2) kpub
= kx
->kpub
;
1394 else if (kx
->kpub
->kn
->kd
!= kx
->kpub
) kpub
= kx
->kpub
->kn
->kd
;
1396 T( trace(T_KEYEXCH
, "keyexch: public key unchanged, skipping"); )
1400 /* --- Skip if either key is expired --- *
1402 * We're not going to get far with expired keys, and this simplifies the
1406 if (KEY_EXPIRED(now
, kx
->kpriv
->t_exp
) ||
1407 KEY_EXPIRED(now
, kx
->kpub
->t_exp
)) {
1408 T( trace(T_KEYEXCH
, "keyexch: %s expired, skipping",
1409 !KEY_EXPIRED(now
, kx
->kpriv
->t_exp
) ?
"public key" :
1410 !KEY_EXPIRED(now
, kx
->kpub
->t_exp
) ?
"private key" :
1415 /* --- If the groups don't match then we can't use this pair --- */
1417 if (!km_samealgsp(kpriv
, kpub
)) {
1418 T( trace(T_KEYEXCH
, "keyexch: peer `%s' group mismatch; "
1419 "%s priv `%s' and %s pub `%s'", p_name(kx
->p
),
1420 i
& 1 ?
"old" : "new", km_tag(kx
->kpriv
),
1421 i
& 2 ?
"old" : "new", km_tag(kx
->kpub
)); )
1426 T( trace(T_KEYEXCH
, "keyexch: peer `%s' continuing with old keys",
1430 /* --- We've chosen new keys --- *
1432 * Switch the new ones into place. Neither of the keys we're switching to
1433 * is expired (we checked that above), so we should just crank everything
1436 * A complication arises: we don't really want to force a new key exchange
1437 * unless we have to. If the group is unchanged, and we're currently
1438 * running OK, then we should just let things lie.
1442 switchp
= ((kx
->f
& KXF_DEAD
) ||
1443 kx
->s
!= KXS_SWITCH
||
1444 !group_samep(kx
->kpriv
->g
, kpriv
->g
));
1446 T( trace(T_KEYEXCH
, "keyexch: peer `%s' adopting "
1447 "%s priv `%s' and %s pub `%s'; %sforcing exchange", p_name(kx
->p
),
1448 i
& 1 ?
"old" : "new", km_tag(kx
->kpriv
),
1449 i
& 2 ?
"old" : "new", km_tag(kx
->kpub
),
1450 switchp ?
"" : "not "); )
1452 if (switchp
) stop(kx
);
1453 km_ref(kpriv
); km_unref(kx
->kpriv
); kx
->kpriv
= kpriv
;
1454 km_ref(kpub
); km_unref(kx
->kpub
); kx
->kpub
= kpub
;
1455 kx
->f
|= KXF_PUBKEY
;
1457 T( trace(T_KEYEXCH
, "keyexch: restarting key negotiation with `%s'",
1464 /* --- @kx_init@ --- *
1466 * Arguments: @keyexch *kx@ = pointer to key exchange context
1467 * @peer *p@ = pointer to peer context
1468 * @keyset **ks@ = pointer to keyset list
1469 * @unsigned f@ = various useful flags
1471 * Returns: Zero if OK, nonzero if it failed.
1473 * Use: Initializes a key exchange module. The module currently
1474 * contains no keys, and will attempt to initiate a key
1478 int kx_init(keyexch
*kx
, peer
*p
, keyset
**ks
, unsigned f
)
1480 if ((kx
->kpriv
= km_findpriv(p_privtag(p
))) == 0) goto fail_0
;
1481 if ((kx
->kpub
= km_findpub(p_tag(p
))) == 0) goto fail_1
;
1482 if (!group_samep(kx
->kpriv
->g
, kx
->kpub
->g
)) {
1483 a_warn("KX", "?PEER", kx
->p
, "group-mismatch",
1484 "local-private-key", "%s", p_privtag(p
),
1485 "peer-public-key", "%s", p_tag(p
),
1492 kx
->f
= KXF_DEAD
| KXF_PUBKEY
| f
;
1494 if (!(kx
->f
& KXF_CORK
)) {
1497 /* Don't notify here: the ADD message hasn't gone out yet. */
1504 km_unref(kx
->kpriv
);
1509 /*----- That's all, folks -------------------------------------------------*/