Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 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) { |
Robert Sloan | 2e9e66a | 2017-09-25 09:08:29 -0700 | [diff] [blame] | 27 | OPENSSL_free(pkey->pkey.ptr); |
| 28 | pkey->pkey.ptr = NULL; |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Srinivas Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 31 | static int set_pubkey(EVP_PKEY *pkey, const uint8_t pubkey[32]) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 32 | 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 Bentley | a5c947b | 2019-08-09 14:24:27 +0000 | [diff] [blame] | 37 | key->has_private = 0; |
Srinivas Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 38 | OPENSSL_memcpy(key->key.pub.value, pubkey, 32); |
Pete Bentley | f8d8b73 | 2019-08-08 12:52:37 +0100 | [diff] [blame] | 39 | |
Pete Bentley | 228bd62 | 2019-08-08 14:53:19 +0000 | [diff] [blame] | 40 | ed25519_free(pkey); |
| 41 | pkey->pkey.ptr = key; |
Pete Bentley | f8d8b73 | 2019-08-08 12:52:37 +0100 | [diff] [blame] | 42 | return 1; |
| 43 | } |
| 44 | |
Srinivas Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 45 | static 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 Bentley | a5c947b | 2019-08-09 14:24:27 +0000 | [diff] [blame] | 49 | return 0; |
| 50 | } |
Srinivas Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 51 | key->has_private = 1; |
| 52 | OPENSSL_memcpy(key->key.priv, privkey, 64); |
Pete Bentley | a5c947b | 2019-08-09 14:24:27 +0000 | [diff] [blame] | 53 | |
Srinivas Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 54 | ed25519_free(pkey); |
| 55 | pkey->pkey.ptr = key; |
Pete Bentley | a5c947b | 2019-08-09 14:24:27 +0000 | [diff] [blame] | 56 | return 1; |
| 57 | } |
| 58 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 59 | static int ed25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) { |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 60 | // See RFC 8410, section 4. |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 61 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 62 | // The parameters must be omitted. Public keys have length 32. |
Srinivas Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 63 | if (CBS_len(params) != 0 || |
| 64 | CBS_len(key) != 32) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 65 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 66 | return 0; |
| 67 | } |
| 68 | |
Srinivas Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 69 | return set_pubkey(out, CBS_data(key)); |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey) { |
| 73 | const ED25519_KEY *key = pkey->pkey.ptr; |
| 74 | |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 75 | // See RFC 8410, section 4. |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 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) || |
| 84 | !CBB_flush(out)) { |
| 85 | OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR); |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 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; |
| 96 | } |
| 97 | |
| 98 | static int ed25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) { |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 99 | // See RFC 8410, section 7. |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 100 | |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 101 | // Parameters must be empty. The key is a 32-byte value wrapped in an extra |
| 102 | // OCTET STRING layer. |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 103 | CBS inner; |
| 104 | if (CBS_len(params) != 0 || |
| 105 | !CBS_get_asn1(key, &inner, CBS_ASN1_OCTETSTRING) || |
Srinivas Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 106 | CBS_len(key) != 0 || |
| 107 | CBS_len(&inner) != 32) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 108 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 109 | return 0; |
| 110 | } |
| 111 | |
Srinivas Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 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); |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 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); |
| 123 | return 0; |
| 124 | } |
| 125 | |
Robert Sloan | d9e572d | 2018-08-27 12:27:00 -0700 | [diff] [blame] | 126 | // See RFC 8410, section 7. |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 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) || |
Robert Sloan | 8f860b1 | 2017-08-28 07:37:06 -0700 | [diff] [blame] | 135 | // The PKCS#8 encoding stores only the 32-byte seed which is the first 32 |
| 136 | // bytes of the private key. |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 137 | !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 | |
| 146 | static int ed25519_size(const EVP_PKEY *pkey) { return 64; } |
| 147 | |
Srinivas Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 148 | static int ed25519_bits(const EVP_PKEY *pkey) { return 256; } |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 149 | |
| 150 | const 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 Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 167 | |
| 168 | EVP_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 | |
| 180 | EVP_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 | } |