1 /* $OpenBSD: kexdh.c,v 1.34 2020/12/04 02:29:25 djm Exp $ */
3 * Copyright (c) 2019 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/types.h>
36 #include "openbsd-compat/openssl-compat.h"
37 #include <openssl/dh.h>
48 kex_dh_keygen(struct kex
*kex
)
50 switch (kex
->kex_type
) {
51 case KEX_DH_GRP1_SHA1
:
52 kex
->dh
= dh_new_group1();
54 case KEX_DH_GRP14_SHA1
:
55 case KEX_DH_GRP14_SHA256
:
56 kex
->dh
= dh_new_group14();
58 case KEX_DH_GRP16_SHA512
:
59 kex
->dh
= dh_new_group16();
61 case KEX_DH_GRP18_SHA512
:
62 kex
->dh
= dh_new_group18();
65 return SSH_ERR_INVALID_ARGUMENT
;
68 return SSH_ERR_ALLOC_FAIL
;
69 return (dh_gen_key(kex
->dh
, kex
->we_need
* 8));
73 kex_dh_compute_key(struct kex
*kex
, BIGNUM
*dh_pub
, struct sshbuf
*out
)
75 BIGNUM
*shared_secret
= NULL
;
81 fprintf(stderr
, "dh_pub= ");
82 BN_print_fp(stderr
, dh_pub
);
83 fprintf(stderr
, "\n");
84 debug("bits %d", BN_num_bits(dh_pub
));
85 DHparams_print_fp(stderr
, kex
->dh
);
86 fprintf(stderr
, "\n");
89 if (!dh_pub_is_valid(kex
->dh
, dh_pub
)) {
90 r
= SSH_ERR_MESSAGE_INCOMPLETE
;
93 klen
= DH_size(kex
->dh
);
94 if ((kbuf
= malloc(klen
)) == NULL
||
95 (shared_secret
= BN_new()) == NULL
) {
96 r
= SSH_ERR_ALLOC_FAIL
;
99 if ((kout
= DH_compute_key(kbuf
, dh_pub
, kex
->dh
)) < 0 ||
100 BN_bin2bn(kbuf
, kout
, shared_secret
) == NULL
) {
101 r
= SSH_ERR_LIBCRYPTO_ERROR
;
105 dump_digest("shared secret", kbuf
, kout
);
107 r
= sshbuf_put_bignum2(out
, shared_secret
);
109 freezero(kbuf
, klen
);
110 BN_clear_free(shared_secret
);
115 kex_dh_keypair(struct kex
*kex
)
117 const BIGNUM
*pub_key
;
118 struct sshbuf
*buf
= NULL
;
121 if ((r
= kex_dh_keygen(kex
)) != 0)
123 DH_get0_key(kex
->dh
, &pub_key
, NULL
);
124 if ((buf
= sshbuf_new()) == NULL
)
125 return SSH_ERR_ALLOC_FAIL
;
126 if ((r
= sshbuf_put_bignum2(buf
, pub_key
)) != 0 ||
127 (r
= sshbuf_get_u32(buf
, NULL
)) != 0)
130 DHparams_print_fp(stderr
, kex
->dh
);
131 fprintf(stderr
, "pub= ");
132 BN_print_fp(stderr
, pub_key
);
133 fprintf(stderr
, "\n");
135 kex
->client_pub
= buf
;
143 kex_dh_enc(struct kex
*kex
, const struct sshbuf
*client_blob
,
144 struct sshbuf
**server_blobp
, struct sshbuf
**shared_secretp
)
146 const BIGNUM
*pub_key
;
147 struct sshbuf
*server_blob
= NULL
;
150 *server_blobp
= NULL
;
151 *shared_secretp
= NULL
;
153 if ((r
= kex_dh_keygen(kex
)) != 0)
155 DH_get0_key(kex
->dh
, &pub_key
, NULL
);
156 if ((server_blob
= sshbuf_new()) == NULL
) {
157 r
= SSH_ERR_ALLOC_FAIL
;
160 if ((r
= sshbuf_put_bignum2(server_blob
, pub_key
)) != 0 ||
161 (r
= sshbuf_get_u32(server_blob
, NULL
)) != 0)
163 if ((r
= kex_dh_dec(kex
, client_blob
, shared_secretp
)) != 0)
165 *server_blobp
= server_blob
;
170 sshbuf_free(server_blob
);
175 kex_dh_dec(struct kex
*kex
, const struct sshbuf
*dh_blob
,
176 struct sshbuf
**shared_secretp
)
178 struct sshbuf
*buf
= NULL
;
179 BIGNUM
*dh_pub
= NULL
;
182 *shared_secretp
= NULL
;
184 if ((buf
= sshbuf_new()) == NULL
) {
185 r
= SSH_ERR_ALLOC_FAIL
;
188 if ((r
= sshbuf_put_stringb(buf
, dh_blob
)) != 0 ||
189 (r
= sshbuf_get_bignum2(buf
, &dh_pub
)) != 0)
192 if ((r
= kex_dh_compute_key(kex
, dh_pub
, buf
)) != 0)
194 *shared_secretp
= buf
;
203 #endif /* WITH_OPENSSL */