1 /* Copyright (c) 2017, 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
ed25519_free(EVP_PKEY * pkey)26 static void ed25519_free(EVP_PKEY *pkey) {
27 OPENSSL_free(pkey->pkey);
28 pkey->pkey = NULL;
29 }
30
ed25519_set_priv_raw(EVP_PKEY * pkey,const uint8_t * in,size_t len)31 static int ed25519_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 ED25519_KEY *key = OPENSSL_malloc(sizeof(ED25519_KEY));
38 if (key == NULL) {
39 return 0;
40 }
41
42 // The RFC 8032 encoding stores only the 32-byte seed, so we must recover the
43 // full representation which we use from it.
44 uint8_t pubkey_unused[32];
45 ED25519_keypair_from_seed(pubkey_unused, key->key, in);
46 key->has_private = 1;
47
48 ed25519_free(pkey);
49 pkey->pkey = key;
50 return 1;
51 }
52
ed25519_set_pub_raw(EVP_PKEY * pkey,const uint8_t * in,size_t len)53 static int ed25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
54 if (len != 32) {
55 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
56 return 0;
57 }
58
59 ED25519_KEY *key = OPENSSL_malloc(sizeof(ED25519_KEY));
60 if (key == NULL) {
61 return 0;
62 }
63
64 OPENSSL_memcpy(key->key + ED25519_PUBLIC_KEY_OFFSET, in, 32);
65 key->has_private = 0;
66
67 ed25519_free(pkey);
68 pkey->pkey = key;
69 return 1;
70 }
71
ed25519_get_priv_raw(const EVP_PKEY * pkey,uint8_t * out,size_t * out_len)72 static int ed25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out,
73 size_t *out_len) {
74 const ED25519_KEY *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 // The raw private key format is the first 32 bytes of the private key.
91 OPENSSL_memcpy(out, key->key, 32);
92 *out_len = 32;
93 return 1;
94 }
95
ed25519_get_pub_raw(const EVP_PKEY * pkey,uint8_t * out,size_t * out_len)96 static int ed25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out,
97 size_t *out_len) {
98 const ED25519_KEY *key = pkey->pkey;
99 if (out == NULL) {
100 *out_len = 32;
101 return 1;
102 }
103
104 if (*out_len < 32) {
105 OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
106 return 0;
107 }
108
109 OPENSSL_memcpy(out, key->key + ED25519_PUBLIC_KEY_OFFSET, 32);
110 *out_len = 32;
111 return 1;
112 }
113
ed25519_pub_decode(EVP_PKEY * out,CBS * params,CBS * key)114 static int ed25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
115 // See RFC 8410, section 4.
116
117 // The parameters must be omitted. Public keys have length 32.
118 if (CBS_len(params) != 0) {
119 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
120 return 0;
121 }
122
123 return ed25519_set_pub_raw(out, CBS_data(key), CBS_len(key));
124 }
125
ed25519_pub_encode(CBB * out,const EVP_PKEY * pkey)126 static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
127 const ED25519_KEY *key = pkey->pkey;
128
129 // See RFC 8410, section 4.
130 CBB spki, algorithm, oid, key_bitstring;
131 if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
132 !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
133 !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
134 !CBB_add_bytes(&oid, ed25519_asn1_meth.oid, ed25519_asn1_meth.oid_len) ||
135 !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
136 !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
137 !CBB_add_bytes(&key_bitstring, key->key + ED25519_PUBLIC_KEY_OFFSET,
138 32) ||
139 !CBB_flush(out)) {
140 OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
141 return 0;
142 }
143
144 return 1;
145 }
146
ed25519_pub_cmp(const EVP_PKEY * a,const EVP_PKEY * b)147 static int ed25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
148 const ED25519_KEY *a_key = a->pkey;
149 const ED25519_KEY *b_key = b->pkey;
150 return OPENSSL_memcmp(a_key->key + ED25519_PUBLIC_KEY_OFFSET,
151 b_key->key + ED25519_PUBLIC_KEY_OFFSET, 32) == 0;
152 }
153
ed25519_priv_decode(EVP_PKEY * out,CBS * params,CBS * key)154 static int ed25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
155 // See RFC 8410, section 7.
156
157 // Parameters must be empty. The key is a 32-byte value wrapped in an extra
158 // OCTET STRING layer.
159 CBS inner;
160 if (CBS_len(params) != 0 ||
161 !CBS_get_asn1(key, &inner, CBS_ASN1_OCTETSTRING) ||
162 CBS_len(key) != 0) {
163 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
164 return 0;
165 }
166
167 return ed25519_set_priv_raw(out, CBS_data(&inner), CBS_len(&inner));
168 }
169
ed25519_priv_encode(CBB * out,const EVP_PKEY * pkey)170 static int ed25519_priv_encode(CBB *out, const EVP_PKEY *pkey) {
171 const ED25519_KEY *key = pkey->pkey;
172 if (!key->has_private) {
173 OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
174 return 0;
175 }
176
177 // See RFC 8410, section 7.
178 CBB pkcs8, algorithm, oid, private_key, inner;
179 if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
180 !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
181 !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
182 !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
183 !CBB_add_bytes(&oid, ed25519_asn1_meth.oid, ed25519_asn1_meth.oid_len) ||
184 !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
185 !CBB_add_asn1(&private_key, &inner, CBS_ASN1_OCTETSTRING) ||
186 // The PKCS#8 encoding stores only the 32-byte seed which is the first 32
187 // bytes of the private key.
188 !CBB_add_bytes(&inner, key->key, 32) ||
189 !CBB_flush(out)) {
190 OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
191 return 0;
192 }
193
194 return 1;
195 }
196
ed25519_size(const EVP_PKEY * pkey)197 static int ed25519_size(const EVP_PKEY *pkey) { return 64; }
198
ed25519_bits(const EVP_PKEY * pkey)199 static int ed25519_bits(const EVP_PKEY *pkey) { return 253; }
200
201 const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth = {
202 EVP_PKEY_ED25519,
203 {0x2b, 0x65, 0x70},
204 3,
205 &ed25519_pkey_meth,
206 ed25519_pub_decode,
207 ed25519_pub_encode,
208 ed25519_pub_cmp,
209 ed25519_priv_decode,
210 ed25519_priv_encode,
211 ed25519_set_priv_raw,
212 ed25519_set_pub_raw,
213 ed25519_get_priv_raw,
214 ed25519_get_pub_raw,
215 /*set1_tls_encodedpoint=*/NULL,
216 /*get1_tls_encodedpoint=*/NULL,
217 /*pkey_opaque=*/NULL,
218 ed25519_size,
219 ed25519_bits,
220 /*param_missing=*/NULL,
221 /*param_copy=*/NULL,
222 /*param_cmp=*/NULL,
223 ed25519_free,
224 };
225