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 "internal.h"
24
25
x25519_free(EVP_PKEY * pkey)26 static void x25519_free(EVP_PKEY *pkey) {
27 OPENSSL_free(pkey->pkey);
28 pkey->pkey = NULL;
29 }
30
x25519_set_priv_raw(EVP_PKEY * pkey,const uint8_t * in,size_t len)31 static int x25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
32 if (len != 32) {
33 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
34 return 0;
35 }
36
37 X25519_KEY *key =
38 reinterpret_cast<X25519_KEY *>(OPENSSL_malloc(sizeof(X25519_KEY)));
39 if (key == NULL) {
40 return 0;
41 }
42
43 OPENSSL_memcpy(key->priv, in, 32);
44 X25519_public_from_private(key->pub, key->priv);
45 key->has_private = 1;
46
47 x25519_free(pkey);
48 pkey->pkey = key;
49 return 1;
50 }
51
x25519_set_pub_raw(EVP_PKEY * pkey,const uint8_t * in,size_t len)52 static int x25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
53 if (len != 32) {
54 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
55 return 0;
56 }
57
58 X25519_KEY *key =
59 reinterpret_cast<X25519_KEY *>(OPENSSL_malloc(sizeof(X25519_KEY)));
60 if (key == NULL) {
61 return 0;
62 }
63
64 OPENSSL_memcpy(key->pub, in, 32);
65 key->has_private = 0;
66
67 x25519_free(pkey);
68 pkey->pkey = key;
69 return 1;
70 }
71
x25519_get_priv_raw(const EVP_PKEY * pkey,uint8_t * out,size_t * out_len)72 static int x25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out,
73 size_t *out_len) {
74 const X25519_KEY *key = reinterpret_cast<X25519_KEY *>(pkey->pkey);
75 if (!key->has_private) {
76 OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
77 return 0;
78 }
79
80 if (out == NULL) {
81 *out_len = 32;
82 return 1;
83 }
84
85 if (*out_len < 32) {
86 OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
87 return 0;
88 }
89
90 OPENSSL_memcpy(out, key->priv, 32);
91 *out_len = 32;
92 return 1;
93 }
94
x25519_get_pub_raw(const EVP_PKEY * pkey,uint8_t * out,size_t * out_len)95 static int x25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out,
96 size_t *out_len) {
97 const X25519_KEY *key = reinterpret_cast<X25519_KEY *>(pkey->pkey);
98 if (out == NULL) {
99 *out_len = 32;
100 return 1;
101 }
102
103 if (*out_len < 32) {
104 OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
105 return 0;
106 }
107
108 OPENSSL_memcpy(out, key->pub, 32);
109 *out_len = 32;
110 return 1;
111 }
112
x25519_set1_tls_encodedpoint(EVP_PKEY * pkey,const uint8_t * in,size_t len)113 static int x25519_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in,
114 size_t len) {
115 return x25519_set_pub_raw(pkey, in, len);
116 }
117
x25519_get1_tls_encodedpoint(const EVP_PKEY * pkey,uint8_t ** out_ptr)118 static size_t x25519_get1_tls_encodedpoint(const EVP_PKEY *pkey,
119 uint8_t **out_ptr) {
120 const X25519_KEY *key = reinterpret_cast<X25519_KEY *>(pkey->pkey);
121 if (key == NULL) {
122 OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
123 return 0;
124 }
125
126 *out_ptr = reinterpret_cast<uint8_t *>(OPENSSL_memdup(key->pub, 32));
127 return *out_ptr == NULL ? 0 : 32;
128 }
129
x25519_pub_decode(EVP_PKEY * out,CBS * params,CBS * key)130 static int x25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
131 // See RFC 8410, section 4.
132
133 // The parameters must be omitted. Public keys have length 32.
134 if (CBS_len(params) != 0) {
135 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
136 return 0;
137 }
138
139 return x25519_set_pub_raw(out, CBS_data(key), CBS_len(key));
140 }
141
x25519_pub_encode(CBB * out,const EVP_PKEY * pkey)142 static int x25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
143 const X25519_KEY *key = reinterpret_cast<X25519_KEY *>(pkey->pkey);
144
145 // See RFC 8410, section 4.
146 CBB spki, algorithm, key_bitstring;
147 if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
148 !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
149 !CBB_add_asn1_element(&algorithm, CBS_ASN1_OBJECT, x25519_asn1_meth.oid,
150 x25519_asn1_meth.oid_len) ||
151 !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
152 !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
153 !CBB_add_bytes(&key_bitstring, key->pub, 32) || //
154 !CBB_flush(out)) {
155 OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
156 return 0;
157 }
158
159 return 1;
160 }
161
x25519_pub_cmp(const EVP_PKEY * a,const EVP_PKEY * b)162 static int x25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
163 const X25519_KEY *a_key = reinterpret_cast<const X25519_KEY *>(a->pkey);
164 const X25519_KEY *b_key = reinterpret_cast<const X25519_KEY *>(b->pkey);
165 return OPENSSL_memcmp(a_key->pub, b_key->pub, 32) == 0;
166 }
167
x25519_priv_decode(EVP_PKEY * out,CBS * params,CBS * key)168 static int x25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
169 // See RFC 8410, section 7.
170
171 // Parameters must be empty. The key is a 32-byte value wrapped in an extra
172 // OCTET STRING layer.
173 CBS inner;
174 if (CBS_len(params) != 0 ||
175 !CBS_get_asn1(key, &inner, CBS_ASN1_OCTETSTRING) || CBS_len(key) != 0) {
176 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
177 return 0;
178 }
179
180 return x25519_set_priv_raw(out, CBS_data(&inner), CBS_len(&inner));
181 }
182
x25519_priv_encode(CBB * out,const EVP_PKEY * pkey)183 static int x25519_priv_encode(CBB *out, const EVP_PKEY *pkey) {
184 const X25519_KEY *key = reinterpret_cast<const X25519_KEY *>(pkey->pkey);
185 if (!key->has_private) {
186 OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
187 return 0;
188 }
189
190 // See RFC 8410, section 7.
191 CBB pkcs8, algorithm, private_key, inner;
192 if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
193 !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
194 !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
195 !CBB_add_asn1_element(&algorithm, CBS_ASN1_OBJECT, x25519_asn1_meth.oid,
196 x25519_asn1_meth.oid_len) ||
197 !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
198 !CBB_add_asn1(&private_key, &inner, CBS_ASN1_OCTETSTRING) ||
199 // The PKCS#8 encoding stores only the 32-byte seed which is the first 32
200 // bytes of the private key.
201 !CBB_add_bytes(&inner, key->priv, 32) || //
202 !CBB_flush(out)) {
203 OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
204 return 0;
205 }
206
207 return 1;
208 }
209
x25519_size(const EVP_PKEY * pkey)210 static int x25519_size(const EVP_PKEY *pkey) { return 32; }
211
x25519_bits(const EVP_PKEY * pkey)212 static int x25519_bits(const EVP_PKEY *pkey) { return 253; }
213
214 const EVP_PKEY_ASN1_METHOD x25519_asn1_meth = {
215 EVP_PKEY_X25519,
216 {0x2b, 0x65, 0x6e},
217 3,
218 &x25519_pkey_meth,
219 x25519_pub_decode,
220 x25519_pub_encode,
221 x25519_pub_cmp,
222 x25519_priv_decode,
223 x25519_priv_encode,
224 x25519_set_priv_raw,
225 x25519_set_pub_raw,
226 x25519_get_priv_raw,
227 x25519_get_pub_raw,
228 x25519_set1_tls_encodedpoint,
229 x25519_get1_tls_encodedpoint,
230 /*pkey_opaque=*/NULL,
231 x25519_size,
232 x25519_bits,
233 /*param_missing=*/NULL,
234 /*param_copy=*/NULL,
235 /*param_cmp=*/NULL,
236 x25519_free,
237 };
238