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