Coverage Report

Created: 2025-11-17 06:18

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