xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/crypto/evp/p_x25519_asn1.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
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 = OPENSSL_malloc(sizeof(X25519_KEY));
38   if (key == NULL) {
39     return 0;
40   }
41 
42   OPENSSL_memcpy(key->priv, in, 32);
43   X25519_public_from_private(key->pub, key->priv);
44   key->has_private = 1;
45 
46   x25519_free(pkey);
47   pkey->pkey = key;
48   return 1;
49 }
50 
x25519_set_pub_raw(EVP_PKEY * pkey,const uint8_t * in,size_t len)51 static int x25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
52   if (len != 32) {
53     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
54     return 0;
55   }
56 
57   X25519_KEY *key = OPENSSL_malloc(sizeof(X25519_KEY));
58   if (key == NULL) {
59     return 0;
60   }
61 
62   OPENSSL_memcpy(key->pub, in, 32);
63   key->has_private = 0;
64 
65   x25519_free(pkey);
66   pkey->pkey = key;
67   return 1;
68 }
69 
x25519_get_priv_raw(const EVP_PKEY * pkey,uint8_t * out,size_t * out_len)70 static int x25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out,
71                                size_t *out_len) {
72   const X25519_KEY *key = pkey->pkey;
73   if (!key->has_private) {
74     OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
75     return 0;
76   }
77 
78   if (out == NULL) {
79     *out_len = 32;
80     return 1;
81   }
82 
83   if (*out_len < 32) {
84     OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
85     return 0;
86   }
87 
88   OPENSSL_memcpy(out, key->priv, 32);
89   *out_len = 32;
90   return 1;
91 }
92 
x25519_get_pub_raw(const EVP_PKEY * pkey,uint8_t * out,size_t * out_len)93 static int x25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out,
94                                size_t *out_len) {
95   const X25519_KEY *key = pkey->pkey;
96   if (out == NULL) {
97     *out_len = 32;
98     return 1;
99   }
100 
101   if (*out_len < 32) {
102     OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
103     return 0;
104   }
105 
106   OPENSSL_memcpy(out, key->pub, 32);
107   *out_len = 32;
108   return 1;
109 }
110 
x25519_set1_tls_encodedpoint(EVP_PKEY * pkey,const uint8_t * in,size_t len)111 static int x25519_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in,
112                                         size_t len) {
113   return x25519_set_pub_raw(pkey, in, len);
114 }
115 
x25519_get1_tls_encodedpoint(const EVP_PKEY * pkey,uint8_t ** out_ptr)116 static size_t x25519_get1_tls_encodedpoint(const EVP_PKEY *pkey,
117                                            uint8_t **out_ptr) {
118   const X25519_KEY *key = pkey->pkey;
119   if (key == NULL) {
120     OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET);
121     return 0;
122   }
123 
124   *out_ptr = OPENSSL_memdup(key->pub, 32);
125   return *out_ptr == NULL ? 0 : 32;
126 }
127 
x25519_pub_decode(EVP_PKEY * out,CBS * params,CBS * key)128 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   if (CBS_len(params) != 0) {
133     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
134     return 0;
135   }
136 
137   return x25519_set_pub_raw(out, CBS_data(key), CBS_len(key));
138 }
139 
x25519_pub_encode(CBB * out,const EVP_PKEY * pkey)140 static int x25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
141   const X25519_KEY *key = pkey->pkey;
142 
143   // See RFC 8410, section 4.
144   CBB spki, algorithm, oid, key_bitstring;
145   if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
146       !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
147       !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
148       !CBB_add_bytes(&oid, x25519_asn1_meth.oid, x25519_asn1_meth.oid_len) ||
149       !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
150       !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
151       !CBB_add_bytes(&key_bitstring, key->pub, 32) ||
152       !CBB_flush(out)) {
153     OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
154     return 0;
155   }
156 
157   return 1;
158 }
159 
x25519_pub_cmp(const EVP_PKEY * a,const EVP_PKEY * b)160 static int x25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
161   const X25519_KEY *a_key = a->pkey;
162   const X25519_KEY *b_key = b->pkey;
163   return OPENSSL_memcmp(a_key->pub, b_key->pub, 32) == 0;
164 }
165 
x25519_priv_decode(EVP_PKEY * out,CBS * params,CBS * key)166 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   CBS inner;
172   if (CBS_len(params) != 0 ||
173       !CBS_get_asn1(key, &inner, CBS_ASN1_OCTETSTRING) ||
174       CBS_len(key) != 0) {
175     OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
176     return 0;
177   }
178 
179   return x25519_set_priv_raw(out, CBS_data(&inner), CBS_len(&inner));
180 }
181 
x25519_priv_encode(CBB * out,const EVP_PKEY * pkey)182 static int x25519_priv_encode(CBB *out, const EVP_PKEY *pkey) {
183   const X25519_KEY *key = pkey->pkey;
184   if (!key->has_private) {
185     OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
186     return 0;
187   }
188 
189   // See RFC 8410, section 7.
190   CBB pkcs8, algorithm, oid, private_key, inner;
191   if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
192       !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
193       !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
194       !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
195       !CBB_add_bytes(&oid, x25519_asn1_meth.oid, x25519_asn1_meth.oid_len) ||
196       !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
197       !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       !CBB_add_bytes(&inner, key->priv, 32) ||
201       !CBB_flush(out)) {
202     OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
203     return 0;
204   }
205 
206   return 1;
207 }
208 
x25519_size(const EVP_PKEY * pkey)209 static int x25519_size(const EVP_PKEY *pkey) { return 32; }
210 
x25519_bits(const EVP_PKEY * pkey)211 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 };
237