]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - crypto/openssh/kex.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / crypto / openssh / kex.c
1 /* $OpenBSD: kex.c,v 1.91 2013/05/17 00:13:13 djm 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 struct kexalg {
67         char *name;
68         int type;
69         int ec_nid;
70         const EVP_MD *(*mdfunc)(void);
71 };
72 static const struct kexalg kexalgs[] = {
73         { KEX_DH1, KEX_DH_GRP1_SHA1, 0, EVP_sha1 },
74         { KEX_DH14, KEX_DH_GRP14_SHA1, 0, EVP_sha1 },
75         { KEX_DHGEX_SHA1, KEX_DH_GEX_SHA1, 0, EVP_sha1 },
76 #ifdef HAVE_EVP_SHA256
77         { KEX_DHGEX_SHA256, KEX_DH_GEX_SHA256, 0, EVP_sha256 },
78 #endif
79 #ifdef OPENSSL_HAS_ECC
80         { KEX_ECDH_SHA2_NISTP256, KEX_ECDH_SHA2, NID_X9_62_prime256v1, EVP_sha256 },
81         { KEX_ECDH_SHA2_NISTP384, KEX_ECDH_SHA2, NID_secp384r1, EVP_sha384 },
82         { KEX_ECDH_SHA2_NISTP521, KEX_ECDH_SHA2, NID_secp521r1, EVP_sha512 },
83 #endif
84         { NULL, -1, -1, NULL},
85 };
86
87 char *
88 kex_alg_list(void)
89 {
90         char *ret = NULL;
91         size_t nlen, rlen = 0;
92         const struct kexalg *k;
93
94         for (k = kexalgs; k->name != NULL; k++) {
95                 if (ret != NULL)
96                         ret[rlen++] = '\n';
97                 nlen = strlen(k->name);
98                 ret = xrealloc(ret, 1, rlen + nlen + 2);
99                 memcpy(ret + rlen, k->name, nlen + 1);
100                 rlen += nlen;
101         }
102         return ret;
103 }
104
105 static const struct kexalg *
106 kex_alg_by_name(const char *name)
107 {
108         const struct kexalg *k;
109
110         for (k = kexalgs; k->name != NULL; k++) {
111                 if (strcmp(k->name, name) == 0)
112                         return k;
113         }
114         return NULL;
115 }
116
117 /* Validate KEX method name list */
118 int
119 kex_names_valid(const char *names)
120 {
121         char *s, *cp, *p;
122
123         if (names == NULL || strcmp(names, "") == 0)
124                 return 0;
125         s = cp = xstrdup(names);
126         for ((p = strsep(&cp, ",")); p && *p != '\0';
127             (p = strsep(&cp, ","))) {
128                 if (kex_alg_by_name(p) == NULL) {
129                         error("Unsupported KEX algorithm \"%.100s\"", p);
130                         free(s);
131                         return 0;
132                 }
133         }
134         debug3("kex names ok: [%s]", names);
135         free(s);
136         return 1;
137 }
138
139 /* put algorithm proposal into buffer. */
140 #ifndef NONE_CIPHER_ENABLED
141 static void
142 #else
143 /* Also used in sshconnect2.c. */
144 void
145 #endif
146 kex_prop2buf(Buffer *b, char *proposal[PROPOSAL_MAX])
147 {
148         u_int i;
149
150         buffer_clear(b);
151         /*
152          * add a dummy cookie, the cookie will be overwritten by
153          * kex_send_kexinit(), each time a kexinit is set
154          */
155         for (i = 0; i < KEX_COOKIE_LEN; i++)
156                 buffer_put_char(b, 0);
157         for (i = 0; i < PROPOSAL_MAX; i++)
158                 buffer_put_cstring(b, proposal[i]);
159         buffer_put_char(b, 0);                  /* first_kex_packet_follows */
160         buffer_put_int(b, 0);                   /* uint32 reserved */
161 }
162
163 /* parse buffer and return algorithm proposal */
164 static char **
165 kex_buf2prop(Buffer *raw, int *first_kex_follows)
166 {
167         Buffer b;
168         u_int i;
169         char **proposal;
170
171         proposal = xcalloc(PROPOSAL_MAX, sizeof(char *));
172
173         buffer_init(&b);
174         buffer_append(&b, buffer_ptr(raw), buffer_len(raw));
175         /* skip cookie */
176         for (i = 0; i < KEX_COOKIE_LEN; i++)
177                 buffer_get_char(&b);
178         /* extract kex init proposal strings */
179         for (i = 0; i < PROPOSAL_MAX; i++) {
180                 proposal[i] = buffer_get_cstring(&b,NULL);
181                 debug2("kex_parse_kexinit: %s", proposal[i]);
182         }
183         /* first kex follows / reserved */
184         i = buffer_get_char(&b);
185         if (first_kex_follows != NULL)
186                 *first_kex_follows = i;
187         debug2("kex_parse_kexinit: first_kex_follows %d ", i);
188         i = buffer_get_int(&b);
189         debug2("kex_parse_kexinit: reserved %u ", i);
190         buffer_free(&b);
191         return proposal;
192 }
193
194 static void
195 kex_prop_free(char **proposal)
196 {
197         u_int i;
198
199         for (i = 0; i < PROPOSAL_MAX; i++)
200                 free(proposal[i]);
201         free(proposal);
202 }
203
204 /* ARGSUSED */
205 static void
206 kex_protocol_error(int type, u_int32_t seq, void *ctxt)
207 {
208         error("Hm, kex protocol error: type %d seq %u", type, seq);
209 }
210
211 static void
212 kex_reset_dispatch(void)
213 {
214         dispatch_range(SSH2_MSG_TRANSPORT_MIN,
215             SSH2_MSG_TRANSPORT_MAX, &kex_protocol_error);
216         dispatch_set(SSH2_MSG_KEXINIT, &kex_input_kexinit);
217 }
218
219 void
220 kex_finish(Kex *kex)
221 {
222         kex_reset_dispatch();
223
224         packet_start(SSH2_MSG_NEWKEYS);
225         packet_send();
226         /* packet_write_wait(); */
227         debug("SSH2_MSG_NEWKEYS sent");
228
229         debug("expecting SSH2_MSG_NEWKEYS");
230         packet_read_expect(SSH2_MSG_NEWKEYS);
231         packet_check_eom();
232         debug("SSH2_MSG_NEWKEYS received");
233
234         kex->done = 1;
235         buffer_clear(&kex->peer);
236         /* buffer_clear(&kex->my); */
237         kex->flags &= ~KEX_INIT_SENT;
238         free(kex->name);
239         kex->name = NULL;
240 }
241
242 void
243 kex_send_kexinit(Kex *kex)
244 {
245         u_int32_t rnd = 0;
246         u_char *cookie;
247         u_int i;
248
249         if (kex == NULL) {
250                 error("kex_send_kexinit: no kex, cannot rekey");
251                 return;
252         }
253         if (kex->flags & KEX_INIT_SENT) {
254                 debug("KEX_INIT_SENT");
255                 return;
256         }
257         kex->done = 0;
258
259         /* generate a random cookie */
260         if (buffer_len(&kex->my) < KEX_COOKIE_LEN)
261                 fatal("kex_send_kexinit: kex proposal too short");
262         cookie = buffer_ptr(&kex->my);
263         for (i = 0; i < KEX_COOKIE_LEN; i++) {
264                 if (i % 4 == 0)
265                         rnd = arc4random();
266                 cookie[i] = rnd;
267                 rnd >>= 8;
268         }
269         packet_start(SSH2_MSG_KEXINIT);
270         packet_put_raw(buffer_ptr(&kex->my), buffer_len(&kex->my));
271         packet_send();
272         debug("SSH2_MSG_KEXINIT sent");
273         kex->flags |= KEX_INIT_SENT;
274 }
275
276 /* ARGSUSED */
277 void
278 kex_input_kexinit(int type, u_int32_t seq, void *ctxt)
279 {
280         char *ptr;
281         u_int i, dlen;
282         Kex *kex = (Kex *)ctxt;
283
284         debug("SSH2_MSG_KEXINIT received");
285         if (kex == NULL)
286                 fatal("kex_input_kexinit: no kex, cannot rekey");
287
288         ptr = packet_get_raw(&dlen);
289         buffer_append(&kex->peer, ptr, dlen);
290
291         /* discard packet */
292         for (i = 0; i < KEX_COOKIE_LEN; i++)
293                 packet_get_char();
294         for (i = 0; i < PROPOSAL_MAX; i++)
295                 free(packet_get_string(NULL));
296         /*
297          * XXX RFC4253 sec 7: "each side MAY guess" - currently no supported
298          * KEX method has the server move first, but a server might be using
299          * a custom method or one that we otherwise don't support. We should
300          * be prepared to remember first_kex_follows here so we can eat a
301          * packet later.
302          * XXX2 - RFC4253 is kind of ambiguous on what first_kex_follows means
303          * for cases where the server *doesn't* go first. I guess we should
304          * ignore it when it is set for these cases, which is what we do now.
305          */
306         (void) packet_get_char();       /* first_kex_follows */
307         (void) packet_get_int();        /* reserved */
308         packet_check_eom();
309
310         kex_kexinit_finish(kex);
311 }
312
313 Kex *
314 kex_setup(char *proposal[PROPOSAL_MAX])
315 {
316         Kex *kex;
317
318         kex = xcalloc(1, sizeof(*kex));
319         buffer_init(&kex->peer);
320         buffer_init(&kex->my);
321         kex_prop2buf(&kex->my, proposal);
322         kex->done = 0;
323
324         kex_send_kexinit(kex);                                  /* we start */
325         kex_reset_dispatch();
326
327         return kex;
328 }
329
330 static void
331 kex_kexinit_finish(Kex *kex)
332 {
333         if (!(kex->flags & KEX_INIT_SENT))
334                 kex_send_kexinit(kex);
335
336         kex_choose_conf(kex);
337
338         if (kex->kex_type >= 0 && kex->kex_type < KEX_MAX &&
339             kex->kex[kex->kex_type] != NULL) {
340                 (kex->kex[kex->kex_type])(kex);
341         } else {
342                 fatal("Unsupported key exchange %d", kex->kex_type);
343         }
344 }
345
346 static void
347 choose_enc(Enc *enc, char *client, char *server)
348 {
349         char *name = match_list(client, server, NULL);
350         if (name == NULL)
351                 fatal("no matching cipher found: client %s server %s",
352                     client, server);
353         if ((enc->cipher = cipher_by_name(name)) == NULL)
354                 fatal("matching cipher is not supported: %s", name);
355         enc->name = name;
356         enc->enabled = 0;
357         enc->iv = NULL;
358         enc->iv_len = cipher_ivlen(enc->cipher);
359         enc->key = NULL;
360         enc->key_len = cipher_keylen(enc->cipher);
361         enc->block_size = cipher_blocksize(enc->cipher);
362 }
363
364 static void
365 choose_mac(Mac *mac, char *client, char *server)
366 {
367         char *name = match_list(client, server, NULL);
368         if (name == NULL)
369                 fatal("no matching mac found: client %s server %s",
370                     client, server);
371         if (mac_setup(mac, name) < 0)
372                 fatal("unsupported mac %s", name);
373         /* truncate the key */
374         if (datafellows & SSH_BUG_HMAC)
375                 mac->key_len = 16;
376         mac->name = name;
377         mac->key = NULL;
378         mac->enabled = 0;
379 }
380
381 static void
382 choose_comp(Comp *comp, char *client, char *server)
383 {
384         char *name = match_list(client, server, NULL);
385         if (name == NULL)
386                 fatal("no matching comp found: client %s server %s", client, server);
387         if (strcmp(name, "zlib@openssh.com") == 0) {
388                 comp->type = COMP_DELAYED;
389         } else if (strcmp(name, "zlib") == 0) {
390                 comp->type = COMP_ZLIB;
391         } else if (strcmp(name, "none") == 0) {
392                 comp->type = COMP_NONE;
393         } else {
394                 fatal("unsupported comp %s", name);
395         }
396         comp->name = name;
397 }
398
399 static void
400 choose_kex(Kex *k, char *client, char *server)
401 {
402         const struct kexalg *kexalg;
403
404         k->name = match_list(client, server, NULL);
405         if (k->name == NULL)
406                 fatal("Unable to negotiate a key exchange method");
407         if ((kexalg = kex_alg_by_name(k->name)) == NULL)
408                 fatal("unsupported kex alg %s", k->name);
409         k->kex_type = kexalg->type;
410         k->evp_md = kexalg->mdfunc();
411         k->ec_nid = kexalg->ec_nid;
412 }
413
414 static void
415 choose_hostkeyalg(Kex *k, char *client, char *server)
416 {
417         char *hostkeyalg = match_list(client, server, NULL);
418         if (hostkeyalg == NULL)
419                 fatal("no hostkey alg");
420         k->hostkey_type = key_type_from_name(hostkeyalg);
421         if (k->hostkey_type == KEY_UNSPEC)
422                 fatal("bad hostkey alg '%s'", hostkeyalg);
423         free(hostkeyalg);
424 }
425
426 static int
427 proposals_match(char *my[PROPOSAL_MAX], char *peer[PROPOSAL_MAX])
428 {
429         static int check[] = {
430                 PROPOSAL_KEX_ALGS, PROPOSAL_SERVER_HOST_KEY_ALGS, -1
431         };
432         int *idx;
433         char *p;
434
435         for (idx = &check[0]; *idx != -1; idx++) {
436                 if ((p = strchr(my[*idx], ',')) != NULL)
437                         *p = '\0';
438                 if ((p = strchr(peer[*idx], ',')) != NULL)
439                         *p = '\0';
440                 if (strcmp(my[*idx], peer[*idx]) != 0) {
441                         debug2("proposal mismatch: my %s peer %s",
442                             my[*idx], peer[*idx]);
443                         return (0);
444                 }
445         }
446         debug2("proposals match");
447         return (1);
448 }
449
450 static void
451 kex_choose_conf(Kex *kex)
452 {
453         Newkeys *newkeys;
454         char **my, **peer;
455         char **cprop, **sprop;
456         int nenc, nmac, ncomp;
457         u_int mode, ctos, need, authlen;
458         int first_kex_follows, type;
459 #ifdef  NONE_CIPHER_ENABLED
460         int auth_flag;
461 #endif
462
463         my   = kex_buf2prop(&kex->my, NULL);
464         peer = kex_buf2prop(&kex->peer, &first_kex_follows);
465
466         if (kex->server) {
467                 cprop=peer;
468                 sprop=my;
469         } else {
470                 cprop=my;
471                 sprop=peer;
472         }
473
474         /* Check whether server offers roaming */
475         if (!kex->server) {
476                 char *roaming;
477                 roaming = match_list(KEX_RESUME, peer[PROPOSAL_KEX_ALGS], NULL);
478                 if (roaming) {
479                         kex->roaming = 1;
480                         free(roaming);
481                 }
482         }
483
484         /* Algorithm Negotiation */
485 #ifdef  NONE_CIPHER_ENABLED
486         auth_flag = packet_get_authentication_state();
487         debug ("AUTH STATE is %d", auth_flag);
488 #endif
489         for (mode = 0; mode < MODE_MAX; mode++) {
490                 newkeys = xcalloc(1, sizeof(*newkeys));
491                 kex->newkeys[mode] = newkeys;
492                 ctos = (!kex->server && mode == MODE_OUT) ||
493                     (kex->server && mode == MODE_IN);
494                 nenc  = ctos ? PROPOSAL_ENC_ALGS_CTOS  : PROPOSAL_ENC_ALGS_STOC;
495                 nmac  = ctos ? PROPOSAL_MAC_ALGS_CTOS  : PROPOSAL_MAC_ALGS_STOC;
496                 ncomp = ctos ? PROPOSAL_COMP_ALGS_CTOS : PROPOSAL_COMP_ALGS_STOC;
497                 choose_enc(&newkeys->enc, cprop[nenc], sprop[nenc]);
498                 /* ignore mac for authenticated encryption */
499                 authlen = cipher_authlen(newkeys->enc.cipher);
500                 if (authlen == 0)
501                         choose_mac(&newkeys->mac, cprop[nmac], sprop[nmac]);
502                 choose_comp(&newkeys->comp, cprop[ncomp], sprop[ncomp]);
503 #ifdef  NONE_CIPHER_ENABLED
504                 debug("REQUESTED ENC.NAME is '%s'", newkeys->enc.name);
505                 if (strcmp(newkeys->enc.name, "none") == 0) {
506                         debug("Requesting NONE. Authflag is %d", auth_flag);
507                         if (auth_flag == 1)
508                                 debug("None requested post authentication.");
509                         else
510                                 fatal("Pre-authentication none cipher requests "
511                                     "are not allowed.");
512                 }
513 #endif
514                 debug("kex: %s %s %s %s",
515                     ctos ? "client->server" : "server->client",
516                     newkeys->enc.name,
517                     authlen == 0 ? newkeys->mac.name : "<implicit>",
518                     newkeys->comp.name);
519         }
520         choose_kex(kex, cprop[PROPOSAL_KEX_ALGS], sprop[PROPOSAL_KEX_ALGS]);
521         choose_hostkeyalg(kex, cprop[PROPOSAL_SERVER_HOST_KEY_ALGS],
522             sprop[PROPOSAL_SERVER_HOST_KEY_ALGS]);
523         need = 0;
524         for (mode = 0; mode < MODE_MAX; mode++) {
525                 newkeys = kex->newkeys[mode];
526                 if (need < newkeys->enc.key_len)
527                         need = newkeys->enc.key_len;
528                 if (need < newkeys->enc.block_size)
529                         need = newkeys->enc.block_size;
530                 if (need < newkeys->enc.iv_len)
531                         need = newkeys->enc.iv_len;
532                 if (need < newkeys->mac.key_len)
533                         need = newkeys->mac.key_len;
534         }
535         /* XXX need runden? */
536         kex->we_need = need;
537
538         /* ignore the next message if the proposals do not match */
539         if (first_kex_follows && !proposals_match(my, peer) &&
540             !(datafellows & SSH_BUG_FIRSTKEX)) {
541                 type = packet_read();
542                 debug2("skipping next packet (type %u)", type);
543         }
544
545         kex_prop_free(my);
546         kex_prop_free(peer);
547 }
548
549 static u_char *
550 derive_key(Kex *kex, int id, u_int need, u_char *hash, u_int hashlen,
551     BIGNUM *shared_secret)
552 {
553         Buffer b;
554         EVP_MD_CTX md;
555         char c = id;
556         u_int have;
557         int mdsz;
558         u_char *digest;
559
560         if ((mdsz = EVP_MD_size(kex->evp_md)) <= 0)
561                 fatal("bad kex md size %d", mdsz);
562         digest = xmalloc(roundup(need, mdsz));
563
564         buffer_init(&b);
565         buffer_put_bignum2(&b, shared_secret);
566
567         /* K1 = HASH(K || H || "A" || session_id) */
568         EVP_DigestInit(&md, kex->evp_md);
569         if (!(datafellows & SSH_BUG_DERIVEKEY))
570                 EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
571         EVP_DigestUpdate(&md, hash, hashlen);
572         EVP_DigestUpdate(&md, &c, 1);
573         EVP_DigestUpdate(&md, kex->session_id, kex->session_id_len);
574         EVP_DigestFinal(&md, digest, NULL);
575
576         /*
577          * expand key:
578          * Kn = HASH(K || H || K1 || K2 || ... || Kn-1)
579          * Key = K1 || K2 || ... || Kn
580          */
581         for (have = mdsz; need > have; have += mdsz) {
582                 EVP_DigestInit(&md, kex->evp_md);
583                 if (!(datafellows & SSH_BUG_DERIVEKEY))
584                         EVP_DigestUpdate(&md, buffer_ptr(&b), buffer_len(&b));
585                 EVP_DigestUpdate(&md, hash, hashlen);
586                 EVP_DigestUpdate(&md, digest, have);
587                 EVP_DigestFinal(&md, digest + have, NULL);
588         }
589         buffer_free(&b);
590 #ifdef DEBUG_KEX
591         fprintf(stderr, "key '%c'== ", c);
592         dump_digest("key", digest, need);
593 #endif
594         return digest;
595 }
596
597 Newkeys *current_keys[MODE_MAX];
598
599 #define NKEYS   6
600 void
601 kex_derive_keys(Kex *kex, u_char *hash, u_int hashlen, BIGNUM *shared_secret)
602 {
603         u_char *keys[NKEYS];
604         u_int i, mode, ctos;
605
606         for (i = 0; i < NKEYS; i++) {
607                 keys[i] = derive_key(kex, 'A'+i, kex->we_need, hash, hashlen,
608                     shared_secret);
609         }
610
611         debug2("kex_derive_keys");
612         for (mode = 0; mode < MODE_MAX; mode++) {
613                 current_keys[mode] = kex->newkeys[mode];
614                 kex->newkeys[mode] = NULL;
615                 ctos = (!kex->server && mode == MODE_OUT) ||
616                     (kex->server && mode == MODE_IN);
617                 current_keys[mode]->enc.iv  = keys[ctos ? 0 : 1];
618                 current_keys[mode]->enc.key = keys[ctos ? 2 : 3];
619                 current_keys[mode]->mac.key = keys[ctos ? 4 : 5];
620         }
621 }
622
623 Newkeys *
624 kex_get_newkeys(int mode)
625 {
626         Newkeys *ret;
627
628         ret = current_keys[mode];
629         current_keys[mode] = NULL;
630         return ret;
631 }
632
633 void
634 derive_ssh1_session_id(BIGNUM *host_modulus, BIGNUM *server_modulus,
635     u_int8_t cookie[8], u_int8_t id[16])
636 {
637         const EVP_MD *evp_md = EVP_md5();
638         EVP_MD_CTX md;
639         u_int8_t nbuf[2048], obuf[EVP_MAX_MD_SIZE];
640         int len;
641
642         EVP_DigestInit(&md, evp_md);
643
644         len = BN_num_bytes(host_modulus);
645         if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
646                 fatal("%s: bad host modulus (len %d)", __func__, len);
647         BN_bn2bin(host_modulus, nbuf);
648         EVP_DigestUpdate(&md, nbuf, len);
649
650         len = BN_num_bytes(server_modulus);
651         if (len < (512 / 8) || (u_int)len > sizeof(nbuf))
652                 fatal("%s: bad server modulus (len %d)", __func__, len);
653         BN_bn2bin(server_modulus, nbuf);
654         EVP_DigestUpdate(&md, nbuf, len);
655
656         EVP_DigestUpdate(&md, cookie, 8);
657
658         EVP_DigestFinal(&md, obuf, NULL);
659         memcpy(id, obuf, 16);
660
661         memset(nbuf, 0, sizeof(nbuf));
662         memset(obuf, 0, sizeof(obuf));
663         memset(&md, 0, sizeof(md));
664 }
665
666 #if defined(DEBUG_KEX) || defined(DEBUG_KEXDH) || defined(DEBUG_KEXECDH)
667 void
668 dump_digest(char *msg, u_char *digest, int len)
669 {
670         int i;
671
672         fprintf(stderr, "%s\n", msg);
673         for (i = 0; i < len; i++) {
674                 fprintf(stderr, "%02x", digest[i]);
675                 if (i%32 == 31)
676                         fprintf(stderr, "\n");
677                 else if (i%8 == 7)
678                         fprintf(stderr, " ");
679         }
680         fprintf(stderr, "\n");
681 }
682 #endif