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 set_pubkey(EVP_PKEY *pkey, const uint8_t pubkey[32]) { 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 } 37 key->has_private = 0; 38 OPENSSL_memcpy(key->key.pub.value, pubkey, 32); 39 40 ed25519_free(pkey); 41 pkey->pkey.ptr = key; 42 return 1; 43 } 44 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); 49 return 0; 50 } 51 key->has_private = 1; 52 OPENSSL_memcpy(key->key.priv, privkey, 64); 53 54 ed25519_free(pkey); 55 pkey->pkey.ptr = key; 56 return 1; 57 } 58 59 static int ed25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) { 60 // See draft-ietf-curdle-pkix-04, section 4. 61 62 // The parameters must be omitted. Public keys have length 32. 63 if (CBS_len(params) != 0 || 64 CBS_len(key) != 32) { 65 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); 66 return 0; 67 } 68 69 return set_pubkey(out, CBS_data(key)); 70 } 71 72 static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey) { 73 const ED25519_KEY *key = pkey->pkey.ptr; 74 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) || 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) { 99 // See draft-ietf-curdle-pkix-04, section 7. 100 101 // Parameters must be empty. The key is a 32-byte value wrapped in an extra 102 // OCTET STRING layer. 103 CBS inner; 104 if (CBS_len(params) != 0 || 105 !CBS_get_asn1(key, &inner, CBS_ASN1_OCTETSTRING) || 106 CBS_len(key) != 0 || 107 CBS_len(&inner) != 32) { 108 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); 109 return 0; 110 } 111 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); 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 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) || 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 148 static int ed25519_bits(const EVP_PKEY *pkey) { return 256; } 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 }; 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 } 191