Coverage Report

Created: 2026-10-31 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/evp/p_x25519.cc
Line
Count
Source
1
// Copyright 2019 The BoringSSL Authors
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/evp.h>
16
17
#include <openssl/bytestring.h>
18
#include <openssl/curve25519.h>
19
#include <openssl/err.h>
20
#include <openssl/mem.h>
21
22
#include "../internal.h"
23
#include "../mem_internal.h"
24
#include "internal.h"
25
26
27
using namespace bssl;
28
29
namespace {
30
31
struct X25519_KEY {
32
  uint8_t pub[32];
33
  uint8_t priv[32];
34
  bool has_private;
35
};
36
37
extern const EVP_PKEY_ASN1_METHOD x25519_asn1_meth;
38
extern const EVP_PKEY_CTX_METHOD x25519_pkey_meth;
39
40
14.9k
void x25519_free(EvpPkey *pkey) {
41
14.9k
  X25519_KEY *key = reinterpret_cast<X25519_KEY *>(pkey->pkey);
42
14.9k
  OPENSSL_free(key);
43
14.9k
  pkey->pkey = nullptr;
44
14.9k
}
45
46
15.4k
int x25519_set_priv_raw(EvpPkey *pkey, const uint8_t *in, size_t len) {
47
15.4k
  if (len != 32) {
48
860
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
49
860
    return 0;
50
860
  }
51
52
14.5k
  X25519_KEY *key = New<X25519_KEY>();
53
14.5k
  if (key == nullptr) {
54
0
    return 0;
55
0
  }
56
57
14.5k
  OPENSSL_memcpy(key->priv, in, 32);
58
14.5k
  X25519_public_from_private(key->pub, key->priv);
59
14.5k
  key->has_private = true;
60
61
14.5k
  evp_pkey_set0(pkey, &x25519_asn1_meth, key);
62
14.5k
  return 1;
63
14.5k
}
64
65
389
int x25519_set_pub_raw(EvpPkey *pkey, const uint8_t *in, size_t len) {
66
389
  if (len != 32) {
67
13
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
68
13
    return 0;
69
13
  }
70
71
376
  X25519_KEY *key = New<X25519_KEY>();
72
376
  if (key == nullptr) {
73
0
    return 0;
74
0
  }
75
76
376
  OPENSSL_memcpy(key->pub, in, 32);
77
376
  key->has_private = false;
78
79
376
  evp_pkey_set0(pkey, &x25519_asn1_meth, key);
80
376
  return 1;
81
376
}
82
83
0
int x25519_get_priv_raw(const EvpPkey *pkey, uint8_t *out, size_t *out_len) {
84
0
  const X25519_KEY *key = reinterpret_cast<X25519_KEY *>(pkey->pkey);
85
0
  if (!key->has_private) {
86
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
87
0
    return 0;
88
0
  }
89
90
0
  if (out == nullptr) {
91
0
    *out_len = 32;
92
0
    return 1;
93
0
  }
94
95
0
  if (*out_len < 32) {
96
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
97
0
    return 0;
98
0
  }
99
100
0
  OPENSSL_memcpy(out, key->priv, 32);
101
0
  *out_len = 32;
102
0
  return 1;
103
0
}
104
105
793
int x25519_get_pub_raw(const EvpPkey *pkey, uint8_t *out, size_t *out_len) {
106
793
  const X25519_KEY *key = reinterpret_cast<X25519_KEY *>(pkey->pkey);
107
793
  if (out == nullptr) {
108
0
    *out_len = 32;
109
0
    return 1;
110
0
  }
111
112
793
  if (*out_len < 32) {
113
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
114
0
    return 0;
115
0
  }
116
117
793
  OPENSSL_memcpy(out, key->pub, 32);
118
793
  *out_len = 32;
119
793
  return 1;
120
793
}
121
122
0
int x25519_set1_tls_encodedpoint(EvpPkey *pkey, const uint8_t *in, size_t len) {
123
0
  return x25519_set_pub_raw(pkey, in, len);
124
0
}
125
126
0
size_t x25519_get1_tls_encodedpoint(const EvpPkey *pkey, uint8_t **out_ptr) {
127
0
  const X25519_KEY *key = reinterpret_cast<X25519_KEY *>(pkey->pkey);
128
0
  if (key == nullptr) {
129
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
130
0
    return 0;
131
0
  }
132
133
0
  *out_ptr = reinterpret_cast<uint8_t *>(OPENSSL_memdup(key->pub, 32));
134
0
  return *out_ptr == nullptr ? 0 : 32;
135
0
}
136
137
bssl::evp_decode_result_t x25519_pub_decode(const EVP_PKEY_ALG *alg,
138
                                            EvpPkey *out, CBS *params,
139
123
                                            CBS *key) {
140
  // See RFC 8410, section 4.
141
142
  // The parameters must be omitted. Public keys have length 32.
143
123
  if (CBS_len(params) != 0) {
144
27
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
145
27
    return evp_decode_error;
146
27
  }
147
148
96
  return x25519_set_pub_raw(out, CBS_data(key), CBS_len(key))
149
96
             ? evp_decode_ok
150
96
             : evp_decode_error;
151
123
}
152
153
137
int x25519_pub_encode(CBB *out, const EvpPkey *pkey) {
154
137
  const X25519_KEY *key = reinterpret_cast<X25519_KEY *>(pkey->pkey);
155
156
  // See RFC 8410, section 4.
157
137
  CBB spki, algorithm, key_bitstring;
158
137
  if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
159
137
      !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
160
137
      !CBB_add_asn1_element(&algorithm, CBS_ASN1_OBJECT, x25519_asn1_meth.oid,
161
137
                            x25519_asn1_meth.oid_len) ||
162
137
      !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
163
137
      !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
164
137
      !CBB_add_bytes(&key_bitstring, key->pub, 32) ||  //
165
137
      !CBB_flush(out)) {
166
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
167
0
    return 0;
168
0
  }
169
170
137
  return 1;
171
137
}
172
173
0
bool x25519_pub_equal(const EvpPkey *a, const EvpPkey *b) {
174
0
  const X25519_KEY *a_key = reinterpret_cast<const X25519_KEY *>(a->pkey);
175
0
  const X25519_KEY *b_key = reinterpret_cast<const X25519_KEY *>(b->pkey);
176
0
  return OPENSSL_memcmp(a_key->pub, b_key->pub, 32) == 0;
177
0
}
178
179
bssl::evp_decode_result_t x25519_priv_decode(const EVP_PKEY_ALG *alg,
180
                                             EvpPkey *out, CBS *params,
181
527
                                             CBS *key) {
182
  // See RFC 8410, section 7.
183
184
  // Parameters must be empty. The key is a 32-byte value wrapped in an extra
185
  // OCTET STRING layer.
186
527
  CBS inner;
187
527
  if (CBS_len(params) != 0 ||
188
503
      !CBS_get_asn1(key, &inner, CBS_ASN1_OCTETSTRING) || CBS_len(key) != 0) {
189
46
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
190
46
    return evp_decode_error;
191
46
  }
192
193
481
  return x25519_set_priv_raw(out, CBS_data(&inner), CBS_len(&inner))
194
481
             ? evp_decode_ok
195
481
             : evp_decode_error;
196
527
}
197
198
298
int x25519_priv_encode(CBB *out, const EvpPkey *pkey) {
199
298
  const X25519_KEY *key = reinterpret_cast<const X25519_KEY *>(pkey->pkey);
200
298
  if (!key->has_private) {
201
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
202
0
    return 0;
203
0
  }
204
205
  // See RFC 8410, section 7.
206
298
  CBB pkcs8, algorithm, private_key, inner;
207
298
  if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
208
298
      !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
209
298
      !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
210
298
      !CBB_add_asn1_element(&algorithm, CBS_ASN1_OBJECT, x25519_asn1_meth.oid,
211
298
                            x25519_asn1_meth.oid_len) ||
212
298
      !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
213
298
      !CBB_add_asn1(&private_key, &inner, CBS_ASN1_OCTETSTRING) ||
214
      // The PKCS#8 encoding stores only the 32-byte seed which is the first 32
215
      // bytes of the private key.
216
298
      !CBB_add_bytes(&inner, key->priv, 32) ||  //
217
298
      !CBB_flush(out)) {
218
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
219
0
    return 0;
220
0
  }
221
222
298
  return 1;
223
298
}
224
225
0
bool x25519_pub_present(const EvpPkey *) { return true; }
226
227
0
bool x25519_pub_copy(EvpPkey *out, const EvpPkey *pkey) {
228
0
  const X25519_KEY *pkey_x25519 =
229
0
      reinterpret_cast<const X25519_KEY *>(pkey->pkey);
230
0
  X25519_KEY *public_copy = New<X25519_KEY>();
231
0
  if (public_copy == nullptr) {
232
0
    return false;
233
0
  }
234
0
  OPENSSL_memcpy(public_copy->pub, pkey_x25519->pub, 32);
235
0
  public_copy->has_private = false;
236
0
  evp_pkey_set0(out, pkey->ameth, public_copy);
237
0
  return true;
238
0
}
239
240
0
bool x25519_priv_present(const EvpPkey *pk) {
241
0
  const X25519_KEY *key = reinterpret_cast<const X25519_KEY *>(pk->pkey);
242
0
  return key->has_private;
243
0
}
244
245
0
int x25519_size(const EvpPkey *pkey) { return 32; }
246
247
0
int x25519_bits(const EvpPkey *pkey) { return 253; }
248
249
const EVP_PKEY_ASN1_METHOD x25519_asn1_meth = {
250
    EVP_PKEY_X25519,
251
    {0x2b, 0x65, 0x6e},
252
    3,
253
    &x25519_pkey_meth,
254
    x25519_pub_decode,
255
    x25519_pub_encode,
256
    x25519_pub_equal,
257
    x25519_pub_present,
258
    x25519_pub_copy,
259
    x25519_priv_decode,
260
    x25519_priv_encode,
261
    x25519_priv_present,
262
    x25519_set_priv_raw,
263
    /*set_priv_seed=*/nullptr,
264
    x25519_set_pub_raw,
265
    x25519_get_priv_raw,
266
    /*get_priv_seed=*/nullptr,
267
    x25519_get_pub_raw,
268
    x25519_set1_tls_encodedpoint,
269
    x25519_get1_tls_encodedpoint,
270
    /*pkey_opaque=*/nullptr,
271
    x25519_size,
272
    x25519_bits,
273
    /*param_missing=*/nullptr,
274
    /*param_copy=*/nullptr,
275
    /*param_equal=*/nullptr,
276
    x25519_free,
277
};
278
279
// X25519 has no parameters to copy.
280
0
int pkey_x25519_copy(EvpPkeyCtx *dst, EvpPkeyCtx *src) { return 1; }
281
282
0
int pkey_x25519_keygen(EvpPkeyCtx *ctx, EvpPkey *pkey) {
283
0
  X25519_KEY *key = New<X25519_KEY>();
284
0
  if (key == nullptr) {
285
0
    return 0;
286
0
  }
287
288
0
  X25519_keypair(key->pub, key->priv);
289
0
  key->has_private = true;
290
0
  evp_pkey_set0(pkey, &x25519_asn1_meth, key);
291
0
  return 1;
292
0
}
293
294
293
int pkey_x25519_derive(EvpPkeyCtx *ctx, uint8_t *out, size_t *out_len) {
295
293
  if (ctx->pkey == nullptr || ctx->peerkey == nullptr) {
296
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET);
297
0
    return 0;
298
0
  }
299
300
293
  const X25519_KEY *our_key =
301
293
      reinterpret_cast<const X25519_KEY *>(ctx->pkey->pkey);
302
293
  const X25519_KEY *peer_key =
303
293
      reinterpret_cast<const X25519_KEY *>(ctx->peerkey->pkey);
304
293
  if (our_key == nullptr || peer_key == nullptr) {
305
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET);
306
0
    return 0;
307
0
  }
308
309
293
  if (!our_key->has_private) {
310
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
311
0
    return 0;
312
0
  }
313
314
293
  if (out != nullptr) {
315
293
    if (*out_len < 32) {
316
0
      OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
317
0
      return 0;
318
0
    }
319
293
    if (!X25519(out, our_key->priv, peer_key->pub)) {
320
3
      OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
321
3
      return 0;
322
3
    }
323
293
  }
324
325
290
  *out_len = 32;
326
290
  return 1;
327
293
}
328
329
586
int pkey_x25519_ctrl(EvpPkeyCtx *ctx, int type, int p1, void *p2) {
330
586
  switch (type) {
331
586
    case EVP_PKEY_CTRL_PEER_KEY:
332
      // `EVP_PKEY_derive_set_peer` requires the key implement this command,
333
      // even if it is a no-op.
334
586
      return 1;
335
336
0
    default:
337
0
      OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
338
0
      return 0;
339
586
  }
340
586
}
341
342
const EVP_PKEY_CTX_METHOD x25519_pkey_meth = {
343
    /*pkey_id=*/EVP_PKEY_X25519,
344
    /*init=*/nullptr,
345
    /*copy=*/pkey_x25519_copy,
346
    /*cleanup=*/nullptr,
347
    /*keygen=*/pkey_x25519_keygen,
348
    /*sign=*/nullptr,
349
    /*sign_message=*/nullptr,
350
    /*verify=*/nullptr,
351
    /*verify_message=*/nullptr,
352
    /*verify_recover=*/nullptr,
353
    /*encrypt=*/nullptr,
354
    /*decrypt=*/nullptr,
355
    /*derive=*/pkey_x25519_derive,
356
    /*paramgen=*/nullptr,
357
    /*encap=*/nullptr,
358
    /*decap=*/nullptr,
359
    /*ctrl=*/pkey_x25519_ctrl,
360
};
361
362
}  // namespace
363
364
226k
const EVP_PKEY_ALG *EVP_pkey_x25519() {
365
226k
  static const EVP_PKEY_ALG kAlg = {&x25519_asn1_meth, &x25519_pkey_meth};
366
226k
  return &kAlg;
367
226k
}