Coverage Report

Created: 2026-03-19 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/ecdh/ecdh.cc
Line
Count
Source
1
// Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
2
// Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.
3
//
4
// Licensed under the Apache License, Version 2.0 (the "License");
5
// you may not use this file except in compliance with the License.
6
// You may obtain a copy of the License at
7
//
8
//     https://www.apache.org/licenses/LICENSE-2.0
9
//
10
// Unless required by applicable law or agreed to in writing, software
11
// distributed under the License is distributed on an "AS IS" BASIS,
12
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
// See the License for the specific language governing permissions and
14
// limitations under the License.
15
16
#include <openssl/ecdh.h>
17
18
#include <limits.h>
19
#include <string.h>
20
21
#include <openssl/digest.h>
22
#include <openssl/err.h>
23
#include <openssl/mem.h>
24
25
#include "../fipsmodule/ec/internal.h"
26
#include "../internal.h"
27
28
29
using namespace bssl;
30
31
int ECDH_compute_key(void *out, size_t out_len, const EC_POINT *pub_key,
32
                     const EC_KEY *priv_key,
33
                     void *(*kdf)(const void *in, size_t inlen, void *out,
34
0
                                  size_t *out_len)) {
35
0
  if (FromOpaque(priv_key)->priv_key == nullptr) {
36
0
    OPENSSL_PUT_ERROR(ECDH, ECDH_R_NO_PRIVATE_VALUE);
37
0
    return -1;
38
0
  }
39
0
  const EC_SCALAR *const priv = &FromOpaque(priv_key)->priv_key->scalar;
40
0
  const EC_GROUP *const group = EC_KEY_get0_group(priv_key);
41
0
  if (EC_GROUP_cmp(group, pub_key->group, nullptr) != 0) {
42
0
    OPENSSL_PUT_ERROR(EC, EC_R_INCOMPATIBLE_OBJECTS);
43
0
    return -1;
44
0
  }
45
46
0
  EC_JACOBIAN shared_point;
47
0
  uint8_t buf[EC_MAX_BYTES];
48
0
  size_t buf_len;
49
0
  if (!ec_point_mul_scalar(group, &shared_point, &pub_key->raw, priv) ||
50
0
      !ec_get_x_coordinate_as_bytes(group, buf, &buf_len, sizeof(buf),
51
0
                                    &shared_point)) {
52
0
    OPENSSL_PUT_ERROR(ECDH, ECDH_R_POINT_ARITHMETIC_FAILURE);
53
0
    return -1;
54
0
  }
55
56
0
  if (kdf != nullptr) {
57
0
    if (kdf(buf, buf_len, out, &out_len) == nullptr) {
58
0
      OPENSSL_PUT_ERROR(ECDH, ECDH_R_KDF_FAILED);
59
0
      return -1;
60
0
    }
61
0
  } else {
62
    // no KDF, just copy as much as we can
63
0
    if (buf_len < out_len) {
64
0
      out_len = buf_len;
65
0
    }
66
0
    OPENSSL_memcpy(out, buf, out_len);
67
0
  }
68
69
0
  if (out_len > INT_MAX) {
70
0
    OPENSSL_PUT_ERROR(ECDH, ERR_R_OVERFLOW);
71
0
    return -1;
72
0
  }
73
74
0
  return (int)out_len;
75
0
}