Coverage Report

Created: 2024-11-21 07:03

/src/boringssl/crypto/evp/p_x25519_asn1.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (c) 2019, Google Inc.
2
 *
3
 * Permission to use, copy, modify, and/or distribute this software for any
4
 * purpose with or without fee is hereby granted, provided that the above
5
 * copyright notice and this permission notice appear in all copies.
6
 *
7
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
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 "../internal.h"
24
25
26
0
static void x25519_free(EVP_PKEY *pkey) {
27
0
  OPENSSL_free(pkey->pkey);
28
0
  pkey->pkey = NULL;
29
0
}
30
31
0
static int x25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
32
0
  if (len != 32) {
33
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
34
0
    return 0;
35
0
  }
36
37
0
  X25519_KEY *key = OPENSSL_malloc(sizeof(X25519_KEY));
38
0
  if (key == NULL) {
39
0
    return 0;
40
0
  }
41
42
0
  OPENSSL_memcpy(key->priv, in, 32);
43
0
  X25519_public_from_private(key->pub, key->priv);
44
0
  key->has_private = 1;
45
46
0
  x25519_free(pkey);
47
0
  pkey->pkey = key;
48
0
  return 1;
49
0
}
50
51
0
static int x25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
52
0
  if (len != 32) {
53
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
54
0
    return 0;
55
0
  }
56
57
0
  X25519_KEY *key = OPENSSL_malloc(sizeof(X25519_KEY));
58
0
  if (key == NULL) {
59
0
    return 0;
60
0
  }
61
62
0
  OPENSSL_memcpy(key->pub, in, 32);
63
0
  key->has_private = 0;
64
65
0
  x25519_free(pkey);
66
0
  pkey->pkey = key;
67
0
  return 1;
68
0
}
69
70
static int x25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out,
71
0
                               size_t *out_len) {
72
0
  const X25519_KEY *key = pkey->pkey;
73
0
  if (!key->has_private) {
74
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
75
0
    return 0;
76
0
  }
77
78
0
  if (out == NULL) {
79
0
    *out_len = 32;
80
0
    return 1;
81
0
  }
82
83
0
  if (*out_len < 32) {
84
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
85
0
    return 0;
86
0
  }
87
88
0
  OPENSSL_memcpy(out, key->priv, 32);
89
0
  *out_len = 32;
90
0
  return 1;
91
0
}
92
93
static int x25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out,
94
0
                               size_t *out_len) {
95
0
  const X25519_KEY *key = pkey->pkey;
96
0
  if (out == NULL) {
97
0
    *out_len = 32;
98
0
    return 1;
99
0
  }
100
101
0
  if (*out_len < 32) {
102
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
103
0
    return 0;
104
0
  }
105
106
0
  OPENSSL_memcpy(out, key->pub, 32);
107
0
  *out_len = 32;
108
0
  return 1;
109
0
}
110
111
static int x25519_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in,
112
0
                                        size_t len) {
113
0
  return x25519_set_pub_raw(pkey, in, len);
114
0
}
115
116
static size_t x25519_get1_tls_encodedpoint(const EVP_PKEY *pkey,
117
0
                                           uint8_t **out_ptr) {
118
0
  const X25519_KEY *key = pkey->pkey;
119
0
  if (key == NULL) {
120
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
121
0
    return 0;
122
0
  }
123
124
0
  *out_ptr = OPENSSL_memdup(key->pub, 32);
125
0
  return *out_ptr == NULL ? 0 : 32;
126
0
}
127
128
0
static int x25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
129
  // See RFC 8410, section 4.
130
131
  // The parameters must be omitted. Public keys have length 32.
132
0
  if (CBS_len(params) != 0) {
133
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
134
0
    return 0;
135
0
  }
136
137
0
  return x25519_set_pub_raw(out, CBS_data(key), CBS_len(key));
138
0
}
139
140
0
static int x25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
141
0
  const X25519_KEY *key = pkey->pkey;
142
143
  // See RFC 8410, section 4.
144
0
  CBB spki, algorithm, oid, key_bitstring;
145
0
  if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
146
0
      !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
147
0
      !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
148
0
      !CBB_add_bytes(&oid, x25519_asn1_meth.oid, x25519_asn1_meth.oid_len) ||
149
0
      !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
150
0
      !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
151
0
      !CBB_add_bytes(&key_bitstring, key->pub, 32) ||
152
0
      !CBB_flush(out)) {
153
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
154
0
    return 0;
155
0
  }
156
157
0
  return 1;
158
0
}
159
160
0
static int x25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
161
0
  const X25519_KEY *a_key = a->pkey;
162
0
  const X25519_KEY *b_key = b->pkey;
163
0
  return OPENSSL_memcmp(a_key->pub, b_key->pub, 32) == 0;
164
0
}
165
166
0
static int x25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
167
  // See RFC 8410, section 7.
168
169
  // Parameters must be empty. The key is a 32-byte value wrapped in an extra
170
  // OCTET STRING layer.
171
0
  CBS inner;
172
0
  if (CBS_len(params) != 0 ||
173
0
      !CBS_get_asn1(key, &inner, CBS_ASN1_OCTETSTRING) ||
174
0
      CBS_len(key) != 0) {
175
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
176
0
    return 0;
177
0
  }
178
179
0
  return x25519_set_priv_raw(out, CBS_data(&inner), CBS_len(&inner));
180
0
}
181
182
0
static int x25519_priv_encode(CBB *out, const EVP_PKEY *pkey) {
183
0
  const X25519_KEY *key = pkey->pkey;
184
0
  if (!key->has_private) {
185
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
186
0
    return 0;
187
0
  }
188
189
  // See RFC 8410, section 7.
190
0
  CBB pkcs8, algorithm, oid, private_key, inner;
191
0
  if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
192
0
      !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
193
0
      !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
194
0
      !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
195
0
      !CBB_add_bytes(&oid, x25519_asn1_meth.oid, x25519_asn1_meth.oid_len) ||
196
0
      !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
197
0
      !CBB_add_asn1(&private_key, &inner, CBS_ASN1_OCTETSTRING) ||
198
      // The PKCS#8 encoding stores only the 32-byte seed which is the first 32
199
      // bytes of the private key.
200
0
      !CBB_add_bytes(&inner, key->priv, 32) ||
201
0
      !CBB_flush(out)) {
202
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
203
0
    return 0;
204
0
  }
205
206
0
  return 1;
207
0
}
208
209
0
static int x25519_size(const EVP_PKEY *pkey) { return 32; }
210
211
0
static int x25519_bits(const EVP_PKEY *pkey) { return 253; }
212
213
const EVP_PKEY_ASN1_METHOD x25519_asn1_meth = {
214
    EVP_PKEY_X25519,
215
    {0x2b, 0x65, 0x6e},
216
    3,
217
    &x25519_pkey_meth,
218
    x25519_pub_decode,
219
    x25519_pub_encode,
220
    x25519_pub_cmp,
221
    x25519_priv_decode,
222
    x25519_priv_encode,
223
    x25519_set_priv_raw,
224
    x25519_set_pub_raw,
225
    x25519_get_priv_raw,
226
    x25519_get_pub_raw,
227
    x25519_set1_tls_encodedpoint,
228
    x25519_get1_tls_encodedpoint,
229
    /*pkey_opaque=*/NULL,
230
    x25519_size,
231
    x25519_bits,
232
    /*param_missing=*/NULL,
233
    /*param_copy=*/NULL,
234
    /*param_cmp=*/NULL,
235
    x25519_free,
236
};