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 | |
26 | static void ed25519_free(EVP_PKEY *pkey) { |
27 | OPENSSL_free(pkey->pkey.ptr); |
28 | pkey->pkey.ptr = NULL; |
29 | } |
30 | |
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 | OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE); |
40 | return 0; |
41 | } |
42 | |
43 | // The RFC 8032 encoding stores only the 32-byte seed, so we must recover the |
44 | // full representation which we use from it. |
45 | uint8_t pubkey_unused[32]; |
46 | ED25519_keypair_from_seed(pubkey_unused, key->key.priv, in); |
47 | key->has_private = 1; |
48 | |
49 | ed25519_free(pkey); |
50 | pkey->pkey.ptr = key; |
51 | return 1; |
52 | } |
53 | |
54 | static int ed25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) { |
55 | if (len != 32) { |
56 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
57 | return 0; |
58 | } |
59 | |
60 | ED25519_KEY *key = OPENSSL_malloc(sizeof(ED25519_KEY)); |
61 | if (key == NULL) { |
62 | OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE); |
63 | return 0; |
64 | } |
65 | |
66 | OPENSSL_memcpy(key->key.pub.value, in, 32); |
67 | key->has_private = 0; |
68 | |
69 | ed25519_free(pkey); |
70 | pkey->pkey.ptr = key; |
71 | return 1; |
72 | } |
73 | |
74 | static int ed25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out, |
75 | size_t *out_len) { |
76 | const ED25519_KEY *key = pkey->pkey.ptr; |
77 | if (!key->has_private) { |
78 | OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); |
79 | return 0; |
80 | } |
81 | |
82 | if (out == NULL) { |
83 | *out_len = 32; |
84 | return 1; |
85 | } |
86 | |
87 | if (*out_len < 32) { |
88 | OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
89 | return 0; |
90 | } |
91 | |
92 | // The raw private key format is the first 32 bytes of the private key. |
93 | OPENSSL_memcpy(out, key->key.priv, 32); |
94 | *out_len = 32; |
95 | return 1; |
96 | } |
97 | |
98 | static int ed25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out, |
99 | size_t *out_len) { |
100 | const ED25519_KEY *key = pkey->pkey.ptr; |
101 | if (out == NULL) { |
102 | *out_len = 32; |
103 | return 1; |
104 | } |
105 | |
106 | if (*out_len < 32) { |
107 | OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
108 | return 0; |
109 | } |
110 | |
111 | OPENSSL_memcpy(out, key->key.pub.value, 32); |
112 | *out_len = 32; |
113 | return 1; |
114 | } |
115 | |
116 | static int ed25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) { |
117 | // See RFC 8410, section 4. |
118 | |
119 | // The parameters must be omitted. Public keys have length 32. |
120 | if (CBS_len(params) != 0) { |
121 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
122 | return 0; |
123 | } |
124 | |
125 | return ed25519_set_pub_raw(out, CBS_data(key), CBS_len(key)); |
126 | } |
127 | |
128 | static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey) { |
129 | const ED25519_KEY *key = pkey->pkey.ptr; |
130 | |
131 | // See RFC 8410, section 4. |
132 | CBB spki, algorithm, oid, key_bitstring; |
133 | if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) || |
134 | !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) || |
135 | !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) || |
136 | !CBB_add_bytes(&oid, ed25519_asn1_meth.oid, ed25519_asn1_meth.oid_len) || |
137 | !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) || |
138 | !CBB_add_u8(&key_bitstring, 0 /* padding */) || |
139 | !CBB_add_bytes(&key_bitstring, key->key.pub.value, 32) || |
140 | !CBB_flush(out)) { |
141 | OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR); |
142 | return 0; |
143 | } |
144 | |
145 | return 1; |
146 | } |
147 | |
148 | static int ed25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) { |
149 | const ED25519_KEY *a_key = a->pkey.ptr; |
150 | const ED25519_KEY *b_key = b->pkey.ptr; |
151 | return OPENSSL_memcmp(a_key->key.pub.value, b_key->key.pub.value, 32) == 0; |
152 | } |
153 | |
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 | |
170 | static int ed25519_priv_encode(CBB *out, const EVP_PKEY *pkey) { |
171 | ED25519_KEY *key = pkey->pkey.ptr; |
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.priv, 32) || |
189 | !CBB_flush(out)) { |
190 | OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR); |
191 | return 0; |
192 | } |
193 | |
194 | return 1; |
195 | } |
196 | |
197 | static int ed25519_size(const EVP_PKEY *pkey) { return 64; } |
198 | |
199 | static int ed25519_bits(const EVP_PKEY *pkey) { return 256; } |
200 | |
201 | const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth = { |
202 | EVP_PKEY_ED25519, |
203 | {0x2b, 0x65, 0x70}, |
204 | 3, |
205 | ed25519_pub_decode, |
206 | ed25519_pub_encode, |
207 | ed25519_pub_cmp, |
208 | ed25519_priv_decode, |
209 | ed25519_priv_encode, |
210 | ed25519_set_priv_raw, |
211 | ed25519_set_pub_raw, |
212 | ed25519_get_priv_raw, |
213 | ed25519_get_pub_raw, |
214 | NULL /* pkey_opaque */, |
215 | ed25519_size, |
216 | ed25519_bits, |
217 | NULL /* param_missing */, |
218 | NULL /* param_copy */, |
219 | NULL /* param_cmp */, |
220 | ed25519_free, |
221 | }; |
222 | |