xref: /freebsd/crypto/openssh/kex.c (revision 595e514d0df2bac5b813d35f83e32875dbf16a83)
1 /* $OpenBSD: kex.c,v 1.88 2013/01/08 18:49:04 markus Exp $ */
2 /* $FreeBSD$ */
3 /*
4  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "includes.h"
28 
29 #include <sys/param.h>
30 
31 #include <signal.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 
37 #include <openssl/crypto.h>
38 
39 #include "xmalloc.h"
40 #include "ssh2.h"
41 #include "buffer.h"
42 #include "packet.h"
43 #include "compat.h"
44 #include "cipher.h"
45 #include "key.h"
46 #include "kex.h"
47 #include "log.h"
48 #include "mac.h"
49 #include "match.h"
50 #include "dispatch.h"
51 #include "monitor.h"
52 #include "roaming.h"
53 
54 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
55 # if defined(HAVE_EVP_SHA256)
56 # define evp_ssh_sha256 EVP_sha256
57 # else
58 extern const EVP_MD *evp_ssh_sha256(void);
59 # endif
60 #endif
61 
62 /* prototype */
63 static void kex_kexinit_finish(Kex *);
64 static void kex_choose_conf(Kex *);
65 
66 /* Validate KEX method name list */
67 int
68 kex_names_valid(const char *names)
69 {
70 	char *s, *cp, *p;
71 
72 	if (names == NULL || strcmp(names, "") == 0)
73 		return 0;
74 	s = cp = xstrdup(names);
75 	for ((p = strsep(&cp, ",")); p && *p != '\0';
76 	    (p = strsep(&cp, ","))) {
77 	    	if (strcmp(p, KEX_DHGEX_SHA256) != 0 &&
78 		    strcmp(p, KEX_DHGEX_SHA1) != 0 &&
79 		    strcmp(p, KEX_DH14) != 0 &&
80 		    strcmp(p, KEX_DH1) != 0 &&
81 		    (strncmp(p, KEX_ECDH_SHA2_STEM,
82 		    sizeof(KEX_ECDH_SHA2_STEM) - 1) != 0 ||
83 		    kex_ecdh_name_to_nid(p) == -1)) {
84 			error("Unsupported KEX algorithm \"%.100s\"", p);
85 			xfree(s);
86 			return 0;
87 		}
88 	}
89 	debug3("kex names ok: [%s]", names);
90 	xfree(s);
91 	return 1;
92 }
93 
94 /* put algorithm proposal into buffer. */
95 #ifndef NONE_CIPHER_ENABLED
96 static void
97 #else
98 /* Also used in sshconnect2.c. */
99 void
100 #endif
101 kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX])
102 {
103 	u_int i;
104 
105 	buffer_clear(b);
106 	/*
107 	 * add a dummy cookie, the cookie will be overwritten by
108 	 * kex_send_kexinit(), each time a kexinit is set
109 	 */
110 	for (i = 0; i < KEX_COOKIE_LEN; i++)
111 		buffer_put_char(b, 0);
112 	for (i = 0; i < PROPOSAL_MAX; i++)
113 		buffer_put_cstring(b, proposal[i]);
114 	buffer_put_char(b, 0);			/* first_kex_packet_follows */
115 	buffer_put_int(b, 0);			/* uint32 reserved */
116 }
117 
118 /* parse buffer and return algorithm proposal */
119 static char **
120 kex_buf2prop(Buffer *raw, int *first_kex_follows)
121 {
122 	Buffer b;
123 	u_int i;
124 	char **proposal;
125 
126 	proposal = xcalloc(PROPOSAL_MAX, sizeof(char *));
127 
128 	buffer_init(&b);
129 	buffer_append(&b, buffer_ptr(raw), buffer_len(raw));
130 	/* skip cookie */
131 	for (i = 0; i < KEX_COOKIE_LEN; i++)
132 		buffer_get_char(&b);
133 	/* extract kex init proposal strings */
134 	for (i = 0; i < PROPOSAL_MAX; i++) {
135 		proposal[i] = buffer_get_cstring(&b,NULL);
136 		debug2("kex_parse_kexinit: %s", proposal[i]);
137 	}
138 	/* first kex follows / reserved */
139 	i = buffer_get_char(&b);
140 	if (first_kex_follows != NULL)
141 		*first_kex_follows = i;
142 	debug2("kex_parse_kexinit: first_kex_follows %d ", i);
143 	i = buffer_get_int(&b);
144 	debug2("kex_parse_kexinit: reserved %u ", i);
145 	buffer_free(&b);
146 	return proposal;
147 }
148 
149 static void
150 kex_prop_free(char **proposal)
151 {
152 	u_int i;
153 
154 	for (i = 0; i < PROPOSAL_MAX; i++)
155 		xfree(proposal[i]);
156 	xfree(proposal);
157 }
158 
159 /* ARGSUSED */
160 static void
161 kex_protocol_error(int type, u_int32_t seq, void *ctxt)
162 {
163 	error("Hm, kex protocol error: type %d seq %u", type, seq);
164 }
165 
166 static void
167 kex_reset_dispatch(void)
168 {
169 	dispatch_range(SSH2_MSG_TRANSPORT_MIN,
170 	    SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
171 	dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
172 }
173 
174 void
175 kex_finish(Kex *kex)
176 {
177 	kex_reset_dispatch();
178 
179 	packet_start(SSH2_MSG_NEWKEYS);
180 	packet_send();
181 	/* packet_write_wait(); */
182 	debug("SSH2_MSG_NEWKEYS sent");
183 
184 	debug("expecting SSH2_MSG_NEWKEYS");
185 	packet_read_expect(SSH2_MSG_NEWKEYS);
186 	packet_check_eom();
187 	debug("SSH2_MSG_NEWKEYS received");
188 
189 	kex->done = 1;
190 	buffer_clear(&kex->peer);
191 	/* buffer_clear(&kex->my); */
192 	kex->flags &= ~KEX_INIT_SENT;
193 	xfree(kex->name);
194 	kex->name = NULL;
195 }
196 
197 void
198 kex_send_kexinit(Kex *kex)
199 {
200 	u_int32_t rnd = 0;
201 	u_char *cookie;
202 	u_int i;
203 
204 	if (kex == NULL) {
205 		error("kex_send_kexinit: no kex, cannot rekey");
206 		return;
207 	}
208 	if (kex->flags & KEX_INIT_SENT) {
209 		debug("KEX_INIT_SENT");
210 		return;
211 	}
212 	kex->done = 0;
213 
214 	/* generate a random cookie */
215 	if (buffer_len(&kex->my) < KEX_COOKIE_LEN)
216 		fatal("kex_send_kexinit: kex proposal too short");
217 	cookie = buffer_ptr(&kex->my);
218 	for (i = 0; i < KEX_COOKIE_LEN; i++) {
219 		if (i % 4 == 0)
220 			rnd = arc4random();
221 		cookie[i] = rnd;
222 		rnd >>= 8;
223 	}
224 	packet_start(SSH2_MSG_KEXINIT);
225 	packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my));
226 	packet_send();
227 	debug("SSH2_MSG_KEXINIT sent");
228 	kex->flags |= KEX_INIT_SENT;
229 }
230 
231 /* ARGSUSED */
232 void
233 kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
234 {
235 	char *ptr;
236 	u_int i, dlen;
237 	Kex *kex = (Kex *)ctxt;
238 
239 	debug("SSH2_MSG_KEXINIT received");
240 	if (kex == NULL)
241 		fatal("kex_input_kexinit: no kex, cannot rekey");
242 
243 	ptr = packet_get_raw(&dlen);
244 	buffer_append(&kex->peer, ptr, dlen);
245 
246 	/* discard packet */
247 	for (i = 0; i < KEX_COOKIE_LEN; i++)
248 		packet_get_char();
249 	for (i = 0; i < PROPOSAL_MAX; i++)
250 		xfree(packet_get_string(NULL));
251 	/*
252 	 * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
253 	 * KEX method has the server move first, but a server might be using
254 	 * a custom method or one that we otherwise don't support. We should
255 	 * be prepared to remember first_kex_follows here so we can eat a
256 	 * packet later.
257 	 * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means
258 	 * for cases where the server *doesn't* go first. I guess we should
259 	 * ignore it when it is set for these cases, which is what we do now.
260 	 */
261 	(void) packet_get_char();	/* first_kex_follows */
262 	(void) packet_get_int();	/* reserved */
263 	packet_check_eom();
264 
265 	kex_kexinit_finish(kex);
266 }
267 
268 Kex *
269 kex_setup(char *proposal[PROPOSAL_MAX])
270 {
271 	Kex *kex;
272 
273 	kex = xcalloc(1, sizeof(*kex));
274 	buffer_init(&kex->peer);
275 	buffer_init(&kex->my);
276 	kex_prop2buf(&kex->my, proposal);
277 	kex->done = 0;
278 
279 	kex_send_kexinit(kex);					/* we start */
280 	kex_reset_dispatch();
281 
282 	return kex;
283 }
284 
285 static void
286 kex_kexinit_finish(Kex *kex)
287 {
288 	if (!(kex->flags & KEX_INIT_SENT))
289 		kex_send_kexinit(kex);
290 
291 	kex_choose_conf(kex);
292 
293 	if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX &&
294 	    kex->kex[kex->kex_type] != NULL) {
295 		(kex->kex[kex->kex_type])(kex);
296 	} else {
297 		fatal("Unsupported key exchange %d", kex->kex_type);
298 	}
299 }
300 
301 static void
302 choose_enc(Enc *enc, char *client, char *server)
303 {
304 	char *name = match_list(client, server, NULL);
305 	if (name == NULL)
306 		fatal("no matching cipher found: client %s server %s",
307 		    client, server);
308 	if ((enc->cipher = cipher_by_name(name)) == NULL)
309 		fatal("matching cipher is not supported: %s", name);
310 	enc->name = name;
311 	enc->enabled = 0;
312 	enc->iv = NULL;
313 	enc->iv_len = cipher_ivlen(enc->cipher);
314 	enc->key = NULL;
315 	enc->key_len = cipher_keylen(enc->cipher);
316 	enc->block_size = cipher_blocksize(enc->cipher);
317 }
318 
319 static void
320 choose_mac(Mac *mac, char *client, char *server)
321 {
322 	char *name = match_list(client, server, NULL);
323 	if (name == NULL)
324 		fatal("no matching mac found: client %s server %s",
325 		    client, server);
326 	if (mac_setup(mac, name) < 0)
327 		fatal("unsupported mac %s", name);
328 	/* truncate the key */
329 	if (datafellows & SSH_BUG_HMAC)
330 		mac->key_len = 16;
331 	mac->name = name;
332 	mac->key = NULL;
333 	mac->enabled = 0;
334 }
335 
336 static void
337 choose_comp(Comp *comp, char *client, char *server)
338 {
339 	char *name = match_list(client, server, NULL);
340 	if (name == NULL)
341 		fatal("no matching comp found: client %s server %s", client, server);
342 	if (strcmp(name, "zlib@openssh.com") == 0) {
343 		comp->type = COMP_DELAYED;
344 	} else if (strcmp(name, "zlib") == 0) {
345 		comp->type = COMP_ZLIB;
346 	} else if (strcmp(name, "none") == 0) {
347 		comp->type = COMP_NONE;
348 	} else {
349 		fatal("unsupported comp %s", name);
350 	}
351 	comp->name = name;
352 }
353 
354 static void
355 choose_kex(Kex *k, char *client, char *server)
356 {
357 	k->name = match_list(client, server, NULL);
358 	if (k->name == NULL)
359 		fatal("Unable to negotiate a key exchange method");
360 	if (strcmp(k->name, KEX_DH1) == 0) {
361 		k->kex_type = KEX_DH_GRP1_SHA1;
362 		k->evp_md = EVP_sha1();
363 	} else if (strcmp(k->name, KEX_DH14) == 0) {
364 		k->kex_type = KEX_DH_GRP14_SHA1;
365 		k->evp_md = EVP_sha1();
366 	} else if (strcmp(k->name, KEX_DHGEX_SHA1) == 0) {
367 		k->kex_type = KEX_DH_GEX_SHA1;
368 		k->evp_md = EVP_sha1();
369 #if OPENSSL_VERSION_NUMBER >= 0x00907000L
370 	} else if (strcmp(k->name, KEX_DHGEX_SHA256) == 0) {
371 		k->kex_type = KEX_DH_GEX_SHA256;
372 		k->evp_md = evp_ssh_sha256();
373 	} else if (strncmp(k->name, KEX_ECDH_SHA2_STEM,
374 	    sizeof(KEX_ECDH_SHA2_STEM) - 1) == 0) {
375  		k->kex_type = KEX_ECDH_SHA2;
376 		k->evp_md = kex_ecdh_name_to_evpmd(k->name);
377 #endif
378 	} else
379 		fatal("bad kex alg %s", k->name);
380 }
381 
382 static void
383 choose_hostkeyalg(Kex *k, char *client, char *server)
384 {
385 	char *hostkeyalg = match_list(client, server, NULL);
386 	if (hostkeyalg == NULL)
387 		fatal("no hostkey alg");
388 	k->hostkey_type = key_type_from_name(hostkeyalg);
389 	if (k->hostkey_type == KEY_UNSPEC)
390 		fatal("bad hostkey alg '%s'", hostkeyalg);
391 	xfree(hostkeyalg);
392 }
393 
394 static int
395 proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
396 {
397 	static int check[] = {
398 		PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
399 	};
400 	int *idx;
401 	char *p;
402 
403 	for (idx = &check[0]; *idx != -1; idx++) {
404 		if ((p = strchr(my[*idx], ',')) != NULL)
405 			*p = '\0';
406 		if ((p = strchr(peer[*idx], ',')) != NULL)
407 			*p = '\0';
408 		if (strcmp(my[*idx], peer[*idx]) != 0) {
409 			debug2("proposal mismatch: my %s peer %s",
410 			    my[*idx], peer[*idx]);
411 			return (0);
412 		}
413 	}
414 	debug2("proposals match");
415 	return (1);
416 }
417 
418 static void
419 kex_choose_conf(Kex *kex)
420 {
421 	Newkeys *newkeys;
422 	char **my, **peer;
423 	char **cprop, **sprop;
424 	int nenc, nmac, ncomp;
425 	u_int mode, ctos, need, authlen;
426 	int first_kex_follows, type;
427 #ifdef	NONE_CIPHER_ENABLED
428 	int auth_flag;
429 #endif
430 
431 	my   = kex_buf2prop(&kex->my, NULL);
432 	peer = kex_buf2prop(&kex->peer, &first_kex_follows);
433 
434 	if (kex->server) {
435 		cprop=peer;
436 		sprop=my;
437 	} else {
438 		cprop=my;
439 		sprop=peer;
440 	}
441 
442 	/* Check whether server offers roaming */
443 	if (!kex->server) {
444 		char *roaming;
445 		roaming = match_list(KEX_RESUME, peer[PROPOSAL_KEX_ALGS], NULL);
446 		if (roaming) {
447 			kex->roaming = 1;
448 			xfree(roaming);
449 		}
450 	}
451 
452 	/* Algorithm Negotiation */
453 #ifdef	NONE_CIPHER_ENABLED
454 	auth_flag = packet_get_authentication_state();
455 	debug ("AUTH STATE is %d", auth_flag);
456 #endif
457 	for (mode = 0; mode < MODE_MAX; mode++) {
458 		newkeys = xcalloc(1, sizeof(*newkeys));
459 		kex->newkeys[mode] = newkeys;
460 		ctos = (!kex->server && mode == MODE_OUT) ||
461 		    (kex->server && mode == MODE_IN);
462 		nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
463 		nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
464 		ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
465 		choose_enc(&newkeys->enc, cprop[nenc], sprop[nenc]);
466 		/* ignore mac for authenticated encryption */
467 		authlen = cipher_authlen(newkeys->enc.cipher);
468 		if (authlen == 0)
469 			choose_mac(&newkeys->mac, cprop[nmac], sprop[nmac]);
470 		choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
471 #ifdef	NONE_CIPHER_ENABLED
472 		debug("REQUESTED ENC.NAME is '%s'", newkeys->enc.name);
473 		if (strcmp(newkeys->enc.name, "none") == 0) {
474 			debug("Requesting NONE. Authflag is %d", auth_flag);
475 			if (auth_flag == 1)
476 				debug("None requested post authentication.");
477 			else
478 				fatal("Pre-authentication none cipher requests "
479 				    "are not allowed.");
480 		}
481 #endif
482 		debug("kex: %s %s %s %s",
483 		    ctos ? "client->server" : "server->client",
484 		    newkeys->enc.name,
485 		    authlen == 0 ? newkeys->mac.name : "<implicit>",
486 		    newkeys->comp.name);
487 	}
488 	choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
489 	choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
490 	    sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
491 	need = 0;
492 	for (mode = 0; mode < MODE_MAX; mode++) {
493 		newkeys = kex->newkeys[mode];
494 		if (need < newkeys->enc.key_len)
495 			need = newkeys->enc.key_len;
496 		if (need < newkeys->enc.block_size)
497 			need = newkeys->enc.block_size;
498 		if (need < newkeys->enc.iv_len)
499 			need = newkeys->enc.iv_len;
500 		if (need < newkeys->mac.key_len)
501 			need = newkeys->mac.key_len;
502 	}
503 	/* XXX need runden? */
504 	kex->we_need = need;
505 
506 	/* ignore the next message if the proposals do not match */
507 	if (first_kex_follows && !proposals_match(my, peer) &&
508 	    !(datafellows & SSH_BUG_FIRSTKEX)) {
509 		type = packet_read();
510 		debug2("skipping next packet (type %u)", type);
511 	}
512 
513 	kex_prop_free(my);
514 	kex_prop_free(peer);
515 }
516 
517 static u_char *
518 derive_key(Kex *kex, int id, u_int need, u_char *hash, u_int hashlen,
519     BIGNUM *shared_secret)
520 {
521 	Buffer b;
522 	EVP_MD_CTX md;
523 	char c = id;
524 	u_int have;
525 	int mdsz;
526 	u_char *digest;
527 
528 	if ((mdsz = EVP_MD_size(kex->evp_md)) <= 0)
529 		fatal("bad kex md size %d", mdsz);
530 	digest = xmalloc(roundup(need, mdsz));
531 
532 	buffer_init(&b);
533 	buffer_put_bignum2(&b, shared_secret);
534 
535 	/* K1 = HASH(K || H || "A" || session_id) */
536 	EVP_DigestInit(&md, kex->evp_md);
537 	if (!(datafellows & SSH_BUG_DERIVEKEY))
538 		EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
539 	EVP_DigestUpdate(&md, hash, hashlen);
540 	EVP_DigestUpdate(&md, &c, 1);
541 	EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len);
542 	EVP_DigestFinal(&md, digest, NULL);
543 
544 	/*
545 	 * expand key:
546 	 * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
547 	 * Key = K1 || K2 || ... || Kn
548 	 */
549 	for (have = mdsz; need > have; have += mdsz) {
550 		EVP_DigestInit(&md, kex->evp_md);
551 		if (!(datafellows & SSH_BUG_DERIVEKEY))
552 			EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
553 		EVP_DigestUpdate(&md, hash, hashlen);
554 		EVP_DigestUpdate(&md, digest, have);
555 		EVP_DigestFinal(&md, digest + have, NULL);
556 	}
557 	buffer_free(&b);
558 #ifdef DEBUG_KEX
559 	fprintf(stderr, "key '%c'== ", c);
560 	dump_digest("key", digest, need);
561 #endif
562 	return digest;
563 }
564 
565 Newkeys *current_keys[MODE_MAX];
566 
567 #define NKEYS	6
568 void
569 kex_derive_keys(Kex *kex, u_char *hash, u_int hashlen, BIGNUM *shared_secret)
570 {
571 	u_char *keys[NKEYS];
572 	u_int i, mode, ctos;
573 
574 	for (i = 0; i < NKEYS; i++) {
575 		keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, hashlen,
576 		    shared_secret);
577 	}
578 
579 	debug2("kex_derive_keys");
580 	for (mode = 0; mode < MODE_MAX; mode++) {
581 		current_keys[mode] = kex->newkeys[mode];
582 		kex->newkeys[mode] = NULL;
583 		ctos = (!kex->server && mode == MODE_OUT) ||
584 		    (kex->server && mode == MODE_IN);
585 		current_keys[mode]->enc.iv  = keys[ctos ? 0 : 1];
586 		current_keys[mode]->enc.key = keys[ctos ? 2 : 3];
587 		current_keys[mode]->mac.key = keys[ctos ? 4 : 5];
588 	}
589 }
590 
591 Newkeys *
592 kex_get_newkeys(int mode)
593 {
594 	Newkeys *ret;
595 
596 	ret = current_keys[mode];
597 	current_keys[mode] = NULL;
598 	return ret;
599 }
600 
601 void
602 derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
603     u_int8_t cookie[8], u_int8_t id[16])
604 {
605 	const EVP_MD *evp_md = EVP_md5();
606 	EVP_MD_CTX md;
607 	u_int8_t nbuf[2048], obuf[EVP_MAX_MD_SIZE];
608 	int len;
609 
610 	EVP_DigestInit(&md, evp_md);
611 
612 	len = BN_num_bytes(host_modulus);
613 	if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
614 		fatal("%s: bad host modulus (len %d)", __func__, len);
615 	BN_bn2bin(host_modulus, nbuf);
616 	EVP_DigestUpdate(&md, nbuf, len);
617 
618 	len = BN_num_bytes(server_modulus);
619 	if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
620 		fatal("%s: bad server modulus (len %d)", __func__, len);
621 	BN_bn2bin(server_modulus, nbuf);
622 	EVP_DigestUpdate(&md, nbuf, len);
623 
624 	EVP_DigestUpdate(&md, cookie, 8);
625 
626 	EVP_DigestFinal(&md, obuf, NULL);
627 	memcpy(id, obuf, 16);
628 
629 	memset(nbuf, 0, sizeof(nbuf));
630 	memset(obuf, 0, sizeof(obuf));
631 	memset(&md, 0, sizeof(md));
632 }
633 
634 #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
635 void
636 dump_digest(char *msg, u_char *digest, int len)
637 {
638 	int i;
639 
640 	fprintf(stderr, "%s\n", msg);
641 	for (i = 0; i < len; i++) {
642 		fprintf(stderr, "%02x", digest[i]);
643 		if (i%32 == 31)
644 			fprintf(stderr, "\n");
645 		else if (i%8 == 7)
646 			fprintf(stderr, " ");
647 	}
648 	fprintf(stderr, "\n");
649 }
650 #endif
651