Pete Bentley | 0c61efe | 2019-08-13 09:32:23 +0100 | [diff] [blame^] | 1 | /* Copyright (c) 2019, 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/buf.h> |
| 18 | #include <openssl/bytestring.h> |
| 19 | #include <openssl/curve25519.h> |
| 20 | #include <openssl/err.h> |
| 21 | #include <openssl/mem.h> |
| 22 | |
| 23 | #include "internal.h" |
| 24 | #include "../internal.h" |
| 25 | |
| 26 | |
| 27 | static void x25519_free(EVP_PKEY *pkey) { |
| 28 | OPENSSL_free(pkey->pkey.ptr); |
| 29 | pkey->pkey.ptr = NULL; |
| 30 | } |
| 31 | |
| 32 | static int x25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) { |
| 33 | if (len != 32) { |
| 34 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | X25519_KEY *key = OPENSSL_malloc(sizeof(X25519_KEY)); |
| 39 | if (key == NULL) { |
| 40 | OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE); |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | OPENSSL_memcpy(key->priv, in, 32); |
| 45 | X25519_public_from_private(key->pub, key->priv); |
| 46 | key->has_private = 1; |
| 47 | |
| 48 | x25519_free(pkey); |
| 49 | pkey->pkey.ptr = key; |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | static int x25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) { |
| 54 | if (len != 32) { |
| 55 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | X25519_KEY *key = OPENSSL_malloc(sizeof(X25519_KEY)); |
| 60 | if (key == NULL) { |
| 61 | OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | OPENSSL_memcpy(key->pub, in, 32); |
| 66 | key->has_private = 0; |
| 67 | |
| 68 | x25519_free(pkey); |
| 69 | pkey->pkey.ptr = key; |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | static int x25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out, |
| 74 | size_t *out_len) { |
| 75 | const X25519_KEY *key = pkey->pkey.ptr; |
| 76 | if (!key->has_private) { |
| 77 | OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | if (out == NULL) { |
| 82 | *out_len = 32; |
| 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | if (*out_len < 32) { |
| 87 | OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | OPENSSL_memcpy(out, key->priv, 32); |
| 92 | *out_len = 32; |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | static int x25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out, |
| 97 | size_t *out_len) { |
| 98 | const X25519_KEY *key = pkey->pkey.ptr; |
| 99 | if (out == NULL) { |
| 100 | *out_len = 32; |
| 101 | return 1; |
| 102 | } |
| 103 | |
| 104 | if (*out_len < 32) { |
| 105 | OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | OPENSSL_memcpy(out, key->pub, 32); |
| 110 | *out_len = 32; |
| 111 | return 1; |
| 112 | } |
| 113 | |
| 114 | static int x25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) { |
| 115 | // See RFC 8410, section 4. |
| 116 | |
| 117 | // The parameters must be omitted. Public keys have length 32. |
| 118 | if (CBS_len(params) != 0) { |
| 119 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | return x25519_set_pub_raw(out, CBS_data(key), CBS_len(key)); |
| 124 | } |
| 125 | |
| 126 | static int x25519_pub_encode(CBB *out, const EVP_PKEY *pkey) { |
| 127 | const X25519_KEY *key = pkey->pkey.ptr; |
| 128 | |
| 129 | // See RFC 8410, section 4. |
| 130 | CBB spki, algorithm, oid, key_bitstring; |
| 131 | if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) || |
| 132 | !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) || |
| 133 | !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) || |
| 134 | !CBB_add_bytes(&oid, x25519_asn1_meth.oid, x25519_asn1_meth.oid_len) || |
| 135 | !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) || |
| 136 | !CBB_add_u8(&key_bitstring, 0 /* padding */) || |
| 137 | !CBB_add_bytes(&key_bitstring, key->pub, 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 x25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) { |
| 147 | const X25519_KEY *a_key = a->pkey.ptr; |
| 148 | const X25519_KEY *b_key = b->pkey.ptr; |
| 149 | return OPENSSL_memcmp(a_key->pub, b_key->pub, 32) == 0; |
| 150 | } |
| 151 | |
| 152 | static int x25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) { |
| 153 | // See RFC 8410, section 7. |
| 154 | |
| 155 | // Parameters must be empty. The key is a 32-byte value wrapped in an extra |
| 156 | // OCTET STRING layer. |
| 157 | CBS inner; |
| 158 | if (CBS_len(params) != 0 || |
| 159 | !CBS_get_asn1(key, &inner, CBS_ASN1_OCTETSTRING) || |
| 160 | CBS_len(key) != 0) { |
| 161 | OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | return x25519_set_priv_raw(out, CBS_data(&inner), CBS_len(&inner)); |
| 166 | } |
| 167 | |
| 168 | static int x25519_priv_encode(CBB *out, const EVP_PKEY *pkey) { |
| 169 | X25519_KEY *key = pkey->pkey.ptr; |
| 170 | if (!key->has_private) { |
| 171 | OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY); |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | // See RFC 8410, section 7. |
| 176 | CBB pkcs8, algorithm, oid, private_key, inner; |
| 177 | if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) || |
| 178 | !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) || |
| 179 | !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) || |
| 180 | !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) || |
| 181 | !CBB_add_bytes(&oid, x25519_asn1_meth.oid, x25519_asn1_meth.oid_len) || |
| 182 | !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) || |
| 183 | !CBB_add_asn1(&private_key, &inner, CBS_ASN1_OCTETSTRING) || |
| 184 | // The PKCS#8 encoding stores only the 32-byte seed which is the first 32 |
| 185 | // bytes of the private key. |
| 186 | !CBB_add_bytes(&inner, key->priv, 32) || |
| 187 | !CBB_flush(out)) { |
| 188 | OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR); |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | return 1; |
| 193 | } |
| 194 | |
| 195 | static int x25519_size(const EVP_PKEY *pkey) { return 32; } |
| 196 | |
| 197 | static int x25519_bits(const EVP_PKEY *pkey) { return 253; } |
| 198 | |
| 199 | const EVP_PKEY_ASN1_METHOD x25519_asn1_meth = { |
| 200 | EVP_PKEY_X25519, |
| 201 | {0x2b, 0x65, 0x6e}, |
| 202 | 3, |
| 203 | x25519_pub_decode, |
| 204 | x25519_pub_encode, |
| 205 | x25519_pub_cmp, |
| 206 | x25519_priv_decode, |
| 207 | x25519_priv_encode, |
| 208 | x25519_set_priv_raw, |
| 209 | x25519_set_pub_raw, |
| 210 | x25519_get_priv_raw, |
| 211 | x25519_get_pub_raw, |
| 212 | NULL /* pkey_opaque */, |
| 213 | x25519_size, |
| 214 | x25519_bits, |
| 215 | NULL /* param_missing */, |
| 216 | NULL /* param_copy */, |
| 217 | NULL /* param_cmp */, |
| 218 | x25519_free, |
| 219 | }; |
| 220 | |
| 221 | int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in, |
| 222 | size_t len) { |
| 223 | // TODO(davidben): In OpenSSL, this function also works for |EVP_PKEY_EC| |
| 224 | // keys. Add support if it ever comes up. |
| 225 | if (pkey->type != EVP_PKEY_X25519) { |
| 226 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE); |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | return x25519_set_pub_raw(pkey, in, len); |
| 231 | } |
| 232 | |
| 233 | size_t EVP_PKEY_get1_tls_encodedpoint(const EVP_PKEY *pkey, uint8_t **out_ptr) { |
| 234 | // TODO(davidben): In OpenSSL, this function also works for |EVP_PKEY_EC| |
| 235 | // keys. Add support if it ever comes up. |
| 236 | if (pkey->type != EVP_PKEY_X25519) { |
| 237 | OPENSSL_PUT_ERROR(EVP, EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | const X25519_KEY *key = pkey->pkey.ptr; |
| 242 | if (key == NULL) { |
| 243 | OPENSSL_PUT_ERROR(EVP, EVP_R_NO_KEY_SET); |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | *out_ptr = BUF_memdup(key->pub, 32); |
| 248 | return *out_ptr == NULL ? 0 : 32; |
| 249 | } |