Coverage Report

Created: 2026-08-14 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/fipsmodule/dh/dh.cc.inc
Line
Count
Source
1
// Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/dh.h>
16
17
#include <string.h>
18
19
#include <iterator>
20
21
#include <openssl/bn.h>
22
#include <openssl/digest.h>
23
#include <openssl/err.h>
24
#include <openssl/mem.h>
25
26
#include "../../internal.h"
27
#include "../../mem_internal.h"
28
#include "../bn/internal.h"
29
#include "../service_indicator/internal.h"
30
#include "internal.h"
31
32
33
using namespace bssl;
34
35
0
DH *DH_new() { return New<DHImpl>(); }
36
37
0
void DH_free(DH *dh) {
38
0
  if (dh != nullptr) {
39
0
    FromOpaque(dh)->DecRefInternal();
40
0
  }
41
0
}
42
43
0
unsigned DH_bits(const DH *dh) { return BN_num_bits(FromOpaque(dh)->p.get()); }
44
45
0
const BIGNUM *DH_get0_pub_key(const DH *dh) {
46
0
  return FromOpaque(dh)->pub_key.get();
47
0
}
48
49
0
const BIGNUM *DH_get0_priv_key(const DH *dh) {
50
0
  return FromOpaque(dh)->priv_key.get();
51
0
}
52
53
0
const BIGNUM *DH_get0_p(const DH *dh) { return FromOpaque(dh)->p.get(); }
54
55
0
const BIGNUM *DH_get0_q(const DH *dh) { return FromOpaque(dh)->q.get(); }
56
57
0
const BIGNUM *DH_get0_g(const DH *dh) { return FromOpaque(dh)->g.get(); }
58
59
void DH_get0_key(const DH *dh, const BIGNUM **out_pub_key,
60
0
                 const BIGNUM **out_priv_key) {
61
0
  auto *impl = FromOpaque(dh);
62
0
  if (out_pub_key != nullptr) {
63
0
    *out_pub_key = impl->pub_key.get();
64
0
  }
65
0
  if (out_priv_key != nullptr) {
66
0
    *out_priv_key = impl->priv_key.get();
67
0
  }
68
0
}
69
70
0
int DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key) {
71
0
  auto *impl = FromOpaque(dh);
72
0
  if (pub_key != nullptr) {
73
0
    impl->pub_key.reset(pub_key);
74
0
  }
75
76
0
  if (priv_key != nullptr) {
77
0
    impl->priv_key.reset(priv_key);
78
0
  }
79
80
0
  return 1;
81
0
}
82
83
void DH_get0_pqg(const DH *dh, const BIGNUM **out_p, const BIGNUM **out_q,
84
0
                 const BIGNUM **out_g) {
85
0
  auto *impl = FromOpaque(dh);
86
0
  if (out_p != nullptr) {
87
0
    *out_p = impl->p.get();
88
0
  }
89
0
  if (out_q != nullptr) {
90
0
    *out_q = impl->q.get();
91
0
  }
92
0
  if (out_g != nullptr) {
93
0
    *out_g = impl->g.get();
94
0
  }
95
0
}
96
97
0
int DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g) {
98
0
  auto *impl = FromOpaque(dh);
99
0
  if ((impl->p == nullptr && p == nullptr) ||
100
0
      (impl->g == nullptr && g == nullptr)) {
101
0
    return 0;
102
0
  }
103
104
0
  if (p != nullptr) {
105
0
    impl->p.reset(p);
106
0
  }
107
108
0
  if (q != nullptr) {
109
0
    impl->q.reset(q);
110
0
  }
111
112
0
  if (g != nullptr) {
113
0
    impl->g.reset(g);
114
0
  }
115
116
  // Invalidate the cached Montgomery parameters.
117
0
  impl->method_mont_p = nullptr;
118
0
  return 1;
119
0
}
120
121
0
int DH_set_length(DH *dh, unsigned priv_length) {
122
0
  auto *impl = FromOpaque(dh);
123
0
  impl->priv_length = priv_length;
124
0
  return 1;
125
0
}
126
127
0
int DH_generate_key(DH *dh) {
128
0
  boringssl_ensure_ffdh_self_test();
129
130
0
  if (!dh_check_params_fast(dh)) {
131
0
    return 0;
132
0
  }
133
134
0
  auto *impl = FromOpaque(dh);
135
0
  UniquePtr<BN_CTX> ctx(BN_CTX_new());
136
0
  if (ctx == nullptr) {
137
0
    OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
138
0
    return 0;
139
0
  }
140
141
0
  if (!BN_MONT_CTX_set_locked(&impl->method_mont_p, &impl->method_mont_p_lock,
142
0
                              impl->p.get(), ctx.get())) {
143
0
    OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
144
0
    return 0;
145
0
  }
146
147
  // Only generate a private key if there's already one. Otherwise,
148
  // `DH_generate_key` recomputes the public key.
149
0
  const BIGNUM *priv_key = impl->priv_key.get();
150
0
  UniquePtr<BIGNUM> new_priv_key;
151
0
  if (priv_key == nullptr) {
152
0
    new_priv_key.reset(BN_new());
153
0
    if (new_priv_key == nullptr) {
154
0
      OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
155
0
      return 0;
156
0
    }
157
0
    if (impl->q) {
158
      // Section 5.6.1.1.4 of SP 800-56A Rev3 generates a private key uniformly
159
      // from [1, min(2^N-1, q-1)].
160
      //
161
      // Although SP 800-56A Rev3 now permits a private key length N,
162
      // `impl->priv_length` historically was ignored when q is available. We
163
      // continue to ignore it and interpret such a configuration as N = len(q).
164
0
      if (!BN_rand_range_ex(new_priv_key.get(), 1, impl->q.get())) {
165
0
        OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
166
0
        return 0;
167
0
      }
168
0
    } else {
169
      // If q is unspecified, we expect p to be a safe prime, with g generating
170
      // the (p-1)/2 subgroup. So, we use q = (p-1)/2. (If g generates a smaller
171
      // prime-order subgroup, q will still divide (p-1)/2.)
172
      //
173
      // We set N from `impl->priv_length`. Section 5.6.1.1.4 of SP 800-56A Rev3
174
      // says to reject N > len(q), or N > num_bits(p) - 1. However, this logic
175
      // originally aligned with PKCS#3, which allows num_bits(p). Instead, we
176
      // clamp `impl->priv_length` before invoking the algorithm.
177
178
      // Compute M = min(2^N, q).
179
0
      UniquePtr<BIGNUM> priv_key_limit(BN_new());
180
0
      if (priv_key_limit == nullptr) {
181
0
        OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
182
0
        return 0;
183
0
      }
184
0
      if (impl->priv_length == 0 ||
185
0
          impl->priv_length >= BN_num_bits(impl->p.get()) - 1) {
186
        // M = q = (p - 1) / 2.
187
0
        if (!BN_rshift1(priv_key_limit.get(), impl->p.get())) {
188
0
          OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
189
0
          return 0;
190
0
        }
191
0
      } else {
192
        // M = 2^N.
193
0
        if (!BN_set_bit(priv_key_limit.get(), impl->priv_length)) {
194
0
          OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
195
0
          return 0;
196
0
        }
197
0
      }
198
199
      // Choose a private key uniformly from [1, M-1].
200
0
      if (!BN_rand_range_ex(new_priv_key.get(), 1, priv_key_limit.get())) {
201
0
        OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
202
0
        return 0;
203
0
      }
204
0
    }
205
0
    priv_key = new_priv_key.get();
206
0
  }
207
208
0
  UniquePtr<BIGNUM> new_pub_key(BN_new());
209
0
  if (new_pub_key == nullptr ||
210
0
      !BN_mod_exp_mont_consttime(new_pub_key.get(), impl->g.get(), priv_key,
211
0
                                 impl->p.get(), ctx.get(),
212
0
                                 impl->method_mont_p.get())) {
213
0
    OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
214
0
    return 0;
215
0
  }
216
217
0
  impl->pub_key = std::move(new_pub_key);
218
0
  if (new_priv_key != nullptr) {
219
0
    impl->priv_key = std::move(new_priv_key);
220
0
  }
221
0
  return 1;
222
0
}
223
224
static int dh_compute_key(DH *dh, BIGNUM *out_shared_key,
225
0
                          const BIGNUM *peers_key, BN_CTX *ctx) {
226
0
  auto *impl = FromOpaque(dh);
227
228
0
  if (!dh_check_params_fast(dh)) {
229
0
    return 0;
230
0
  }
231
232
0
  if (impl->priv_key == nullptr) {
233
0
    OPENSSL_PUT_ERROR(DH, DH_R_NO_PRIVATE_VALUE);
234
0
    return 0;
235
0
  }
236
237
0
  int check_result;
238
0
  if (!DH_check_pub_key(dh, peers_key, &check_result) || check_result) {
239
0
    OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY);
240
0
    return 0;
241
0
  }
242
243
0
  BN_CTXScope scope(ctx);
244
0
  BIGNUM *p_minus_1 = BN_CTX_get(ctx);
245
0
  if (!p_minus_1 ||
246
0
      !BN_MONT_CTX_set_locked(&impl->method_mont_p, &impl->method_mont_p_lock,
247
0
                              impl->p.get(), ctx)) {
248
0
    return 0;
249
0
  }
250
251
0
  if (!BN_mod_exp_mont_consttime(out_shared_key, peers_key,
252
0
                                 impl->priv_key.get(), impl->p.get(), ctx,
253
0
                                 impl->method_mont_p.get()) ||
254
0
      !BN_copy(p_minus_1, impl->p.get()) || !BN_sub_word(p_minus_1, 1)) {
255
0
    OPENSSL_PUT_ERROR(DH, ERR_R_BN_LIB);
256
0
    return 0;
257
0
  }
258
259
  // This performs the check required by SP 800-56Ar3 section 5.7.1.1 step two.
260
0
  if (BN_cmp_word(out_shared_key, 1) <= 0 ||
261
0
      BN_cmp(out_shared_key, p_minus_1) == 0) {
262
0
    OPENSSL_PUT_ERROR(DH, DH_R_INVALID_PUBKEY);
263
0
    return 0;
264
0
  }
265
266
0
  return 1;
267
0
}
268
269
int bssl::dh_compute_key_padded_no_self_test(unsigned char *out,
270
0
                                             const BIGNUM *peers_key, DH *dh) {
271
0
  UniquePtr<BN_CTX> ctx(BN_CTX_new());
272
0
  if (ctx == nullptr) {
273
0
    return -1;
274
0
  }
275
0
  BN_CTXScope scope(ctx.get());
276
0
  int dh_size = DH_size(dh);
277
0
  BIGNUM *shared_key = BN_CTX_get(ctx.get());
278
0
  if (shared_key == nullptr ||
279
0
      !dh_compute_key(dh, shared_key, peers_key, ctx.get()) ||
280
0
      !BN_bn2bin_padded(out, dh_size, shared_key)) {
281
0
    return -1;
282
0
  }
283
0
  return dh_size;
284
0
}
285
286
0
int DH_compute_key_padded(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
287
0
  boringssl_ensure_ffdh_self_test();
288
289
0
  return dh_compute_key_padded_no_self_test(out, peers_key, dh);
290
0
}
291
292
0
int DH_compute_key(unsigned char *out, const BIGNUM *peers_key, DH *dh) {
293
0
  boringssl_ensure_ffdh_self_test();
294
295
0
  UniquePtr<BN_CTX> ctx(BN_CTX_new());
296
0
  if (ctx == nullptr) {
297
0
    return -1;
298
0
  }
299
0
  BN_CTXScope scope(ctx.get());
300
0
  BIGNUM *shared_key = BN_CTX_get(ctx.get());
301
0
  if (shared_key == nullptr ||
302
0
      !dh_compute_key(dh, shared_key, peers_key, ctx.get())) {
303
0
    return -1;
304
0
  }
305
  // A `BIGNUM`'s byte count fits in `int`.
306
0
  return static_cast<int>(BN_bn2bin(shared_key, out));
307
0
}
308
309
int DH_compute_key_hashed(DH *dh, uint8_t *out, size_t *out_len,
310
                          size_t max_out_len, const BIGNUM *peers_key,
311
0
                          const EVP_MD *digest) {
312
0
  *out_len = SIZE_MAX;
313
314
0
  const size_t digest_len = EVP_MD_size(digest);
315
0
  if (digest_len > max_out_len) {
316
0
    return 0;
317
0
  }
318
319
0
  FIPS_service_indicator_lock_state();
320
321
0
  int ret = 0;
322
0
  const size_t dh_len = DH_size(dh);
323
0
  uint8_t *shared_bytes = reinterpret_cast<uint8_t *>(OPENSSL_malloc(dh_len));
324
0
  unsigned out_len_unsigned;
325
0
  if (!shared_bytes ||
326
      // SP 800-56A is ambiguous about whether the output should be padded prior
327
      // to revision three. But revision three, section C.1, awkwardly specifies
328
      // padding to the length of p.
329
      //
330
      // Also, padded output avoids side-channels, so is always strongly
331
      // advisable.
332
0
      DH_compute_key_padded(shared_bytes, peers_key, dh) != (int)dh_len ||
333
0
      !EVP_Digest(shared_bytes, dh_len, out, &out_len_unsigned, digest,
334
0
                  nullptr) ||
335
0
      out_len_unsigned != digest_len) {
336
0
    goto err;
337
0
  }
338
339
0
  *out_len = digest_len;
340
0
  ret = 1;
341
342
0
err:
343
0
  FIPS_service_indicator_unlock_state();
344
0
  OPENSSL_free(shared_bytes);
345
0
  return ret;
346
0
}
347
348
0
int DH_size(const DH *dh) { return BN_num_bytes(FromOpaque(dh)->p.get()); }
349
350
0
int DH_up_ref(DH *dh) {
351
0
  auto *impl = FromOpaque(dh);
352
0
  impl->UpRefInternal();
353
0
  return 1;
354
0
}
355
356
0
DH *DH_get_rfc7919_2048() {
357
  // This is the prime from https://tools.ietf.org/html/rfc7919#appendix-A.1,
358
  // which is specifically approved for FIPS in appendix D of SP 800-56Ar3.
359
0
  static const BN_ULONG kFFDHE2048Data[] = {
360
0
      TOBN(0xffffffff, 0xffffffff), TOBN(0x886b4238, 0x61285c97),
361
0
      TOBN(0xc6f34a26, 0xc1b2effa), TOBN(0xc58ef183, 0x7d1683b2),
362
0
      TOBN(0x3bb5fcbc, 0x2ec22005), TOBN(0xc3fe3b1b, 0x4c6fad73),
363
0
      TOBN(0x8e4f1232, 0xeef28183), TOBN(0x9172fe9c, 0xe98583ff),
364
0
      TOBN(0xc03404cd, 0x28342f61), TOBN(0x9e02fce1, 0xcdf7e2ec),
365
0
      TOBN(0x0b07a7c8, 0xee0a6d70), TOBN(0xae56ede7, 0x6372bb19),
366
0
      TOBN(0x1d4f42a3, 0xde394df4), TOBN(0xb96adab7, 0x60d7f468),
367
0
      TOBN(0xd108a94b, 0xb2c8e3fb), TOBN(0xbc0ab182, 0xb324fb61),
368
0
      TOBN(0x30acca4f, 0x483a797a), TOBN(0x1df158a1, 0x36ade735),
369
0
      TOBN(0xe2a689da, 0xf3efe872), TOBN(0x984f0c70, 0xe0e68b77),
370
0
      TOBN(0xb557135e, 0x7f57c935), TOBN(0x85636555, 0x3ded1af3),
371
0
      TOBN(0x2433f51f, 0x5f066ed0), TOBN(0xd3df1ed5, 0xd5fd6561),
372
0
      TOBN(0xf681b202, 0xaec4617a), TOBN(0x7d2fe363, 0x630c75d8),
373
0
      TOBN(0xcc939dce, 0x249b3ef9), TOBN(0xa9e13641, 0x146433fb),
374
0
      TOBN(0xd8b9c583, 0xce2d3695), TOBN(0xafdc5620, 0x273d3cf1),
375
0
      TOBN(0xadf85458, 0xa2bb4a9a), TOBN(0xffffffff, 0xffffffff),
376
0
  };
377
378
0
  UniquePtr<BIGNUM> ffdhe2048_p(BN_new());
379
0
  UniquePtr<BIGNUM> ffdhe2048_q(BN_new());
380
0
  UniquePtr<BIGNUM> ffdhe2048_g(BN_new());
381
0
  UniquePtr<DH> dh(DH_new());
382
0
  if (!ffdhe2048_p || !ffdhe2048_q || !ffdhe2048_g || !dh) {
383
0
    return nullptr;
384
0
  }
385
386
0
  bn_set_static_words(ffdhe2048_p.get(), kFFDHE2048Data,
387
0
                      std::size(kFFDHE2048Data));
388
389
0
  if (!BN_rshift1(ffdhe2048_q.get(), ffdhe2048_p.get()) ||
390
0
      !BN_set_word(ffdhe2048_g.get(), 2) ||
391
0
      !DH_set0_pqg(dh.get(), ffdhe2048_p.get(), ffdhe2048_q.get(),
392
0
                   ffdhe2048_g.get())) {
393
0
    return nullptr;
394
0
  }
395
  // `DH_set0_pqg` takes ownership on success.
396
0
  ffdhe2048_p.release();
397
0
  ffdhe2048_q.release();
398
0
  ffdhe2048_g.release();
399
400
0
  return dh.release();
401
0
}