blob: ba08c6d36993df24203ff4e9e167f08d6161a506 [file] [log] [blame]
Robert Sloan572a4e22017-04-17 10:52:19 -07001/* 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
26static void ed25519_free(EVP_PKEY *pkey) {
Robert Sloan2e9e66a2017-09-25 09:08:29 -070027 OPENSSL_free(pkey->pkey.ptr);
28 pkey->pkey.ptr = NULL;
Robert Sloan572a4e22017-04-17 10:52:19 -070029}
30
Srinivas Paladugudd42a612019-08-09 19:30:39 +000031static int set_pubkey(EVP_PKEY *pkey, const uint8_t pubkey[32]) {
Robert Sloan572a4e22017-04-17 10:52:19 -070032 ED25519_KEY *key = OPENSSL_malloc(sizeof(ED25519_KEY));
33 if (key == NULL) {
34 OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
35 return 0;
36 }
Pete Bentleya5c947b2019-08-09 14:24:27 +000037 key->has_private = 0;
Srinivas Paladugudd42a612019-08-09 19:30:39 +000038 OPENSSL_memcpy(key->key.pub.value, pubkey, 32);
Pete Bentleyf8d8b732019-08-08 12:52:37 +010039
Pete Bentley228bd622019-08-08 14:53:19 +000040 ed25519_free(pkey);
41 pkey->pkey.ptr = key;
Pete Bentleyf8d8b732019-08-08 12:52:37 +010042 return 1;
43}
44
Srinivas Paladugudd42a612019-08-09 19:30:39 +000045static int set_privkey(EVP_PKEY *pkey, const uint8_t privkey[64]) {
46 ED25519_KEY *key = OPENSSL_malloc(sizeof(ED25519_KEY));
47 if (key == NULL) {
48 OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
Pete Bentleya5c947b2019-08-09 14:24:27 +000049 return 0;
50 }
Srinivas Paladugudd42a612019-08-09 19:30:39 +000051 key->has_private = 1;
52 OPENSSL_memcpy(key->key.priv, privkey, 64);
Pete Bentleya5c947b2019-08-09 14:24:27 +000053
Srinivas Paladugudd42a612019-08-09 19:30:39 +000054 ed25519_free(pkey);
55 pkey->pkey.ptr = key;
Pete Bentleya5c947b2019-08-09 14:24:27 +000056 return 1;
57}
58
Robert Sloan572a4e22017-04-17 10:52:19 -070059static int ed25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
Robert Sloand9e572d2018-08-27 12:27:00 -070060 // See RFC 8410, section 4.
Robert Sloan572a4e22017-04-17 10:52:19 -070061
Robert Sloan8f860b12017-08-28 07:37:06 -070062 // The parameters must be omitted. Public keys have length 32.
Srinivas Paladugudd42a612019-08-09 19:30:39 +000063 if (CBS_len(params) != 0 ||
64 CBS_len(key) != 32) {
Robert Sloan572a4e22017-04-17 10:52:19 -070065 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
66 return 0;
67 }
68
Srinivas Paladugudd42a612019-08-09 19:30:39 +000069 return set_pubkey(out, CBS_data(key));
Robert Sloan572a4e22017-04-17 10:52:19 -070070}
71
72static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
73 const ED25519_KEY *key = pkey->pkey.ptr;
74
Robert Sloand9e572d2018-08-27 12:27:00 -070075 // See RFC 8410, section 4.
Robert Sloan572a4e22017-04-17 10:52:19 -070076 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) ||
84 !CBB_flush(out)) {
85 OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
86 return 0;
87 }
88
89 return 1;
90}
91
92static 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;
96}
97
98static int ed25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
Robert Sloand9e572d2018-08-27 12:27:00 -070099 // See RFC 8410, section 7.
Robert Sloan572a4e22017-04-17 10:52:19 -0700100
Robert Sloan8f860b12017-08-28 07:37:06 -0700101 // Parameters must be empty. The key is a 32-byte value wrapped in an extra
102 // OCTET STRING layer.
Robert Sloan572a4e22017-04-17 10:52:19 -0700103 CBS inner;
104 if (CBS_len(params) != 0 ||
105 !CBS_get_asn1(key, &inner, CBS_ASN1_OCTETSTRING) ||
Srinivas Paladugudd42a612019-08-09 19:30:39 +0000106 CBS_len(key) != 0 ||
107 CBS_len(&inner) != 32) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700108 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
109 return 0;
110 }
111
Srinivas Paladugudd42a612019-08-09 19:30:39 +0000112 // 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);
Robert Sloan572a4e22017-04-17 10:52:19 -0700117}
118
119static 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);
123 return 0;
124 }
125
Robert Sloand9e572d2018-08-27 12:27:00 -0700126 // See RFC 8410, section 7.
Robert Sloan572a4e22017-04-17 10:52:19 -0700127 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) ||
Robert Sloan8f860b12017-08-28 07:37:06 -0700135 // The PKCS#8 encoding stores only the 32-byte seed which is the first 32
136 // bytes of the private key.
Robert Sloan572a4e22017-04-17 10:52:19 -0700137 !CBB_add_bytes(&inner, key->key.priv, 32) ||
138 !CBB_flush(out)) {
139 OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
140 return 0;
141 }
142
143 return 1;
144}
145
146static int ed25519_size(const EVP_PKEY *pkey) { return 64; }
147
Srinivas Paladugudd42a612019-08-09 19:30:39 +0000148static int ed25519_bits(const EVP_PKEY *pkey) { return 256; }
Robert Sloan572a4e22017-04-17 10:52:19 -0700149
150const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth = {
151 EVP_PKEY_ED25519,
152 {0x2b, 0x65, 0x70},
153 3,
154 ed25519_pub_decode,
155 ed25519_pub_encode,
156 ed25519_pub_cmp,
157 ed25519_priv_decode,
158 ed25519_priv_encode,
159 NULL /* pkey_opaque */,
160 ed25519_size,
161 ed25519_bits,
162 NULL /* param_missing */,
163 NULL /* param_copy */,
164 NULL /* param_cmp */,
165 ed25519_free,
166};
Srinivas Paladugudd42a612019-08-09 19:30:39 +0000167
168EVP_PKEY *EVP_PKEY_new_ed25519_public(const uint8_t public_key[32]) {
169 EVP_PKEY *ret = EVP_PKEY_new();
170 if (ret == NULL ||
171 !EVP_PKEY_set_type(ret, EVP_PKEY_ED25519) ||
172 !set_pubkey(ret, public_key)) {
173 EVP_PKEY_free(ret);
174 return NULL;
175 }
176
177 return ret;
178}
179
180EVP_PKEY *EVP_PKEY_new_ed25519_private(const uint8_t private_key[64]) {
181 EVP_PKEY *ret = EVP_PKEY_new();
182 if (ret == NULL ||
183 !EVP_PKEY_set_type(ret, EVP_PKEY_ED25519) ||
184 !set_privkey(ret, private_key)) {
185 EVP_PKEY_free(ret);
186 return NULL;
187 }
188
189 return ret;
190}