Coverage Report

Created: 2026-06-10 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hpn-ssh/kexecdh.c
Line
Count
Source
1
/* $OpenBSD: kexecdh.c,v 1.12 2026/02/14 00:18:34 jsg Exp $ */
2
/*
3
 * Copyright (c) 2010 Damien Miller.  All rights reserved.
4
 * Copyright (c) 2019 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
#if defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC)
30
31
#include <sys/types.h>
32
33
#include <stdio.h>
34
#include <signal.h>
35
36
#include <openssl/bn.h>
37
#include <openssl/ecdh.h>
38
39
#include "sshkey.h"
40
#include "kex.h"
41
#include "sshbuf.h"
42
#include "ssherr.h"
43
44
static int
45
kex_ecdh_dec_key_group(struct kex *, const struct sshbuf *, EC_KEY *key,
46
    const EC_GROUP *, struct sshbuf **);
47
48
int
49
kex_ecdh_keypair(struct kex *kex)
50
0
{
51
0
  EC_KEY *client_key = NULL;
52
0
  const EC_GROUP *group;
53
0
  const EC_POINT *public_key;
54
0
  struct sshbuf *buf = NULL;
55
0
  int r;
56
57
0
  if ((client_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
58
0
    r = SSH_ERR_ALLOC_FAIL;
59
0
    goto out;
60
0
  }
61
0
  if (EC_KEY_generate_key(client_key) != 1) {
62
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
63
0
    goto out;
64
0
  }
65
0
  group = EC_KEY_get0_group(client_key);
66
0
  public_key = EC_KEY_get0_public_key(client_key);
67
68
0
  if ((buf = sshbuf_new()) == NULL) {
69
0
    r = SSH_ERR_ALLOC_FAIL;
70
0
    goto out;
71
0
  }
72
0
  if ((r = sshbuf_put_ec(buf, public_key, group)) != 0 ||
73
0
      (r = sshbuf_get_u32(buf, NULL)) != 0)
74
0
    goto out;
75
#ifdef DEBUG_KEXECDH
76
  fputs("client private key:\n", stderr);
77
  sshkey_dump_ec_key(client_key);
78
#endif
79
0
  kex->ec_client_key = client_key;
80
0
  kex->ec_group = group;
81
0
  client_key = NULL; /* owned by the kex */
82
0
  kex->client_pub = buf;
83
0
  buf = NULL;
84
0
 out:
85
0
  EC_KEY_free(client_key);
86
0
  sshbuf_free(buf);
87
0
  return r;
88
0
}
89
90
int
91
kex_ecdh_enc(struct kex *kex, const struct sshbuf *client_blob,
92
    struct sshbuf **server_blobp, struct sshbuf **shared_secretp)
93
0
{
94
0
  const EC_GROUP *group;
95
0
  const EC_POINT *pub_key;
96
0
  EC_KEY *server_key = NULL;
97
0
  struct sshbuf *server_blob = NULL;
98
0
  int r;
99
100
0
  *server_blobp = NULL;
101
0
  *shared_secretp = NULL;
102
103
0
  if ((server_key = EC_KEY_new_by_curve_name(kex->ec_nid)) == NULL) {
104
0
    r = SSH_ERR_ALLOC_FAIL;
105
0
    goto out;
106
0
  }
107
0
  if (EC_KEY_generate_key(server_key) != 1) {
108
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
109
0
    goto out;
110
0
  }
111
0
  group = EC_KEY_get0_group(server_key);
112
113
#ifdef DEBUG_KEXECDH
114
  fputs("server private key:\n", stderr);
115
  sshkey_dump_ec_key(server_key);
116
#endif
117
0
  pub_key = EC_KEY_get0_public_key(server_key);
118
0
  if ((server_blob = sshbuf_new()) == NULL) {
119
0
    r = SSH_ERR_ALLOC_FAIL;
120
0
    goto out;
121
0
  }
122
0
  if ((r = sshbuf_put_ec(server_blob, pub_key, group)) != 0 ||
123
0
      (r = sshbuf_get_u32(server_blob, NULL)) != 0)
124
0
    goto out;
125
0
  if ((r = kex_ecdh_dec_key_group(kex, client_blob, server_key, group,
126
0
      shared_secretp)) != 0)
127
0
    goto out;
128
0
  *server_blobp = server_blob;
129
0
  server_blob = NULL;
130
0
 out:
131
0
  EC_KEY_free(server_key);
132
0
  sshbuf_free(server_blob);
133
0
  return r;
134
0
}
135
136
static int
137
kex_ecdh_dec_key_group(struct kex *kex, const struct sshbuf *ec_blob,
138
    EC_KEY *key, const EC_GROUP *group, struct sshbuf **shared_secretp)
139
0
{
140
0
  struct sshbuf *buf = NULL;
141
0
  BIGNUM *shared_secret = NULL;
142
0
  EC_POINT *dh_pub = NULL;
143
0
  u_char *kbuf = NULL;
144
0
  size_t klen = 0;
145
0
  int r;
146
147
0
  *shared_secretp = NULL;
148
149
0
  if ((buf = sshbuf_new()) == NULL) {
150
0
    r = SSH_ERR_ALLOC_FAIL;
151
0
    goto out;
152
0
  }
153
0
  if ((r = sshbuf_put_stringb(buf, ec_blob)) != 0)
154
0
    goto out;
155
0
  if ((dh_pub = EC_POINT_new(group)) == NULL) {
156
0
    r = SSH_ERR_ALLOC_FAIL;
157
0
    goto out;
158
0
  }
159
0
  if ((r = sshbuf_get_ec(buf, dh_pub, group)) != 0) {
160
0
    goto out;
161
0
  }
162
0
  sshbuf_reset(buf);
163
164
#ifdef DEBUG_KEXECDH
165
  fputs("public key:\n", stderr);
166
  sshkey_dump_ec_point(group, dh_pub);
167
#endif
168
0
  if (sshkey_ec_validate_public(group, dh_pub) != 0) {
169
0
    r = SSH_ERR_MESSAGE_INCOMPLETE;
170
0
    goto out;
171
0
  }
172
0
  klen = (EC_GROUP_get_degree(group) + 7) / 8;
173
0
  if ((kbuf = malloc(klen)) == NULL ||
174
0
      (shared_secret = BN_new()) == NULL) {
175
0
    r = SSH_ERR_ALLOC_FAIL;
176
0
    goto out;
177
0
  }
178
0
  if (ECDH_compute_key(kbuf, klen, dh_pub, key, NULL) != (int)klen ||
179
0
      BN_bin2bn(kbuf, klen, shared_secret) == NULL) {
180
0
    r = SSH_ERR_LIBCRYPTO_ERROR;
181
0
    goto out;
182
0
  }
183
#ifdef DEBUG_KEXECDH
184
  dump_digest("shared secret", kbuf, klen);
185
#endif
186
0
  if ((r = sshbuf_put_bignum2(buf, shared_secret)) != 0)
187
0
    goto out;
188
0
  *shared_secretp = buf;
189
0
  buf = NULL;
190
0
 out:
191
0
  EC_POINT_clear_free(dh_pub);
192
0
  BN_clear_free(shared_secret);
193
0
  freezero(kbuf, klen);
194
0
  sshbuf_free(buf);
195
0
  return r;
196
0
}
197
198
int
199
kex_ecdh_dec(struct kex *kex, const struct sshbuf *server_blob,
200
    struct sshbuf **shared_secretp)
201
0
{
202
0
  int r;
203
204
0
  r = kex_ecdh_dec_key_group(kex, server_blob, kex->ec_client_key,
205
0
      kex->ec_group, shared_secretp);
206
0
  EC_KEY_free(kex->ec_client_key);
207
  kex->ec_client_key = NULL;
208
0
  return r;
209
0
}
210
211
#else
212
213
#include "ssherr.h"
214
215
struct kex;
216
struct sshbuf;
217
struct sshkey;
218
219
int
220
kex_ecdh_keypair(struct kex *kex)
221
{
222
  return SSH_ERR_SIGN_ALG_UNSUPPORTED;
223
}
224
225
int
226
kex_ecdh_enc(struct kex *kex, const struct sshbuf *client_blob,
227
    struct sshbuf **server_blobp, struct sshbuf **shared_secretp)
228
{
229
  return SSH_ERR_SIGN_ALG_UNSUPPORTED;
230
}
231
232
int
233
kex_ecdh_dec(struct kex *kex, const struct sshbuf *server_blob,
234
    struct sshbuf **shared_secretp)
235
{
236
  return SSH_ERR_SIGN_ALG_UNSUPPORTED;
237
}
238
#endif /* defined(WITH_OPENSSL) && defined(OPENSSL_HAS_ECC) */