1 /* Copyright (c) 2017, Google Inc.
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.
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. */
15 #include <openssl/evp.h>
17 #include <openssl/bytestring.h>
18 #include <openssl/curve25519.h>
19 #include <openssl/err.h>
20 #include <openssl/mem.h>
23 #include "../internal.h"
26 static void ed25519_free(EVP_PKEY *pkey) {
27 OPENSSL_free(pkey->pkey.ptr);
28 pkey->pkey.ptr = NULL;
31 static int set_pubkey(EVP_PKEY *pkey, const uint8_t pubkey[32]) {
32 ED25519_KEY *key = OPENSSL_malloc(sizeof(ED25519_KEY));
34 OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
38 OPENSSL_memcpy(key->key.pub.value, pubkey, 32);
45 static int set_privkey(EVP_PKEY *pkey, const uint8_t privkey[64]) {
46 ED25519_KEY *key = OPENSSL_malloc(sizeof(ED25519_KEY));
48 OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
52 OPENSSL_memcpy(key->key.priv, privkey, 64);
59 static int ed25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
60 // See draft-ietf-curdle-pkix-04, section 4.
62 // The parameters must be omitted. Public keys have length 32.
63 if (CBS_len(params) != 0 ||
65 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
69 return set_pubkey(out, CBS_data(key));
72 static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
73 const ED25519_KEY *key = pkey->pkey.ptr;
75 // See draft-ietf-curdle-pkix-04, section 4.
76 CBB spki, algorithm, oid, key_bitstring;
77 if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
78 !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
79 !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
80 !CBB_add_bytes(&oid, ed25519_asn1_meth.oid, ed25519_asn1_meth.oid_len) ||
81 !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
82 !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
83 !CBB_add_bytes(&key_bitstring, key->key.pub.value, 32) ||
85 OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
92 static int ed25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
93 const ED25519_KEY *a_key = a->pkey.ptr;
94 const ED25519_KEY *b_key = b->pkey.ptr;
95 return OPENSSL_memcmp(a_key->key.pub.value, b_key->key.pub.value, 32) == 0;
98 static int ed25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
99 // See draft-ietf-curdle-pkix-04, section 7.
101 // Parameters must be empty. The key is a 32-byte value wrapped in an extra
102 // OCTET STRING layer.
104 if (CBS_len(params) != 0 ||
105 !CBS_get_asn1(key, &inner, CBS_ASN1_OCTETSTRING) ||
107 CBS_len(&inner) != 32) {
108 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
112 // The PKCS#8 encoding stores only the 32-byte seed, so we must recover the
113 // full representation which we use from it.
114 uint8_t pubkey[32], privkey[64];
115 ED25519_keypair_from_seed(pubkey, privkey, CBS_data(&inner));
116 return set_privkey(out, privkey);
119 static int ed25519_priv_encode(CBB *out, const EVP_PKEY *pkey) {
120 ED25519_KEY *key = pkey->pkey.ptr;
121 if (!key->has_private) {
122 OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
126 // See draft-ietf-curdle-pkix-04, section 7.
127 CBB pkcs8, algorithm, oid, private_key, inner;
128 if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
129 !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
130 !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
131 !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
132 !CBB_add_bytes(&oid, ed25519_asn1_meth.oid, ed25519_asn1_meth.oid_len) ||
133 !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
134 !CBB_add_asn1(&private_key, &inner, CBS_ASN1_OCTETSTRING) ||
135 // The PKCS#8 encoding stores only the 32-byte seed which is the first 32
136 // bytes of the private key.
137 !CBB_add_bytes(&inner, key->key.priv, 32) ||
139 OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
146 static int ed25519_size(const EVP_PKEY *pkey) { return 64; }
148 static int ed25519_bits(const EVP_PKEY *pkey) { return 256; }
150 const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth = {
159 NULL /* pkey_opaque */,
162 NULL /* param_missing */,
163 NULL /* param_copy */,
164 NULL /* param_cmp */,
168 EVP_PKEY *EVP_PKEY_new_ed25519_public(const uint8_t public_key[32]) {
169 EVP_PKEY *ret = EVP_PKEY_new();
171 !EVP_PKEY_set_type(ret, EVP_PKEY_ED25519) ||
172 !set_pubkey(ret, public_key)) {
180 EVP_PKEY *EVP_PKEY_new_ed25519_private(const uint8_t private_key[64]) {
181 EVP_PKEY *ret = EVP_PKEY_new();
183 !EVP_PKEY_set_type(ret, EVP_PKEY_ED25519) ||
184 !set_privkey(ret, private_key)) {