p_ed25519_asn1.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include <openssl/evp.h>
  15. #include <openssl/bytestring.h>
  16. #include <openssl/curve25519.h>
  17. #include <openssl/err.h>
  18. #include <openssl/mem.h>
  19. #include "internal.h"
  20. #include "../internal.h"
  21. static void ed25519_free(EVP_PKEY *pkey) {
  22. OPENSSL_free(pkey->pkey.ptr);
  23. pkey->pkey.ptr = NULL;
  24. }
  25. static int set_pubkey(EVP_PKEY *pkey, const uint8_t pubkey[32]) {
  26. ED25519_KEY *key = OPENSSL_malloc(sizeof(ED25519_KEY));
  27. if (key == NULL) {
  28. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  29. return 0;
  30. }
  31. key->has_private = 0;
  32. OPENSSL_memcpy(key->key.pub.value, pubkey, 32);
  33. ed25519_free(pkey);
  34. pkey->pkey.ptr = key;
  35. return 1;
  36. }
  37. static int set_privkey(EVP_PKEY *pkey, const uint8_t privkey[64]) {
  38. ED25519_KEY *key = OPENSSL_malloc(sizeof(ED25519_KEY));
  39. if (key == NULL) {
  40. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  41. return 0;
  42. }
  43. key->has_private = 1;
  44. OPENSSL_memcpy(key->key.priv, privkey, 64);
  45. ed25519_free(pkey);
  46. pkey->pkey.ptr = key;
  47. return 1;
  48. }
  49. static int ed25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
  50. // See draft-ietf-curdle-pkix-04, section 4.
  51. // The parameters must be omitted. Public keys have length 32.
  52. if (CBS_len(params) != 0 ||
  53. CBS_len(key) != 32) {
  54. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  55. return 0;
  56. }
  57. return set_pubkey(out, CBS_data(key));
  58. }
  59. static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
  60. const ED25519_KEY *key = pkey->pkey.ptr;
  61. // See draft-ietf-curdle-pkix-04, section 4.
  62. CBB spki, algorithm, oid, key_bitstring;
  63. if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
  64. !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
  65. !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
  66. !CBB_add_bytes(&oid, ed25519_asn1_meth.oid, ed25519_asn1_meth.oid_len) ||
  67. !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
  68. !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
  69. !CBB_add_bytes(&key_bitstring, key->key.pub.value, 32) ||
  70. !CBB_flush(out)) {
  71. OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
  72. return 0;
  73. }
  74. return 1;
  75. }
  76. static int ed25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
  77. const ED25519_KEY *a_key = a->pkey.ptr;
  78. const ED25519_KEY *b_key = b->pkey.ptr;
  79. return OPENSSL_memcmp(a_key->key.pub.value, b_key->key.pub.value, 32) == 0;
  80. }
  81. static int ed25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
  82. // See draft-ietf-curdle-pkix-04, section 7.
  83. // Parameters must be empty. The key is a 32-byte value wrapped in an extra
  84. // OCTET STRING layer.
  85. CBS inner;
  86. if (CBS_len(params) != 0 ||
  87. !CBS_get_asn1(key, &inner, CBS_ASN1_OCTETSTRING) ||
  88. CBS_len(key) != 0 ||
  89. CBS_len(&inner) != 32) {
  90. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  91. return 0;
  92. }
  93. // The PKCS#8 encoding stores only the 32-byte seed, so we must recover the
  94. // full representation which we use from it.
  95. uint8_t pubkey[32], privkey[64];
  96. ED25519_keypair_from_seed(pubkey, privkey, CBS_data(&inner));
  97. return set_privkey(out, privkey);
  98. }
  99. static int ed25519_priv_encode(CBB *out, const EVP_PKEY *pkey) {
  100. ED25519_KEY *key = pkey->pkey.ptr;
  101. if (!key->has_private) {
  102. OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
  103. return 0;
  104. }
  105. // See draft-ietf-curdle-pkix-04, section 7.
  106. CBB pkcs8, algorithm, oid, private_key, inner;
  107. if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
  108. !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
  109. !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
  110. !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
  111. !CBB_add_bytes(&oid, ed25519_asn1_meth.oid, ed25519_asn1_meth.oid_len) ||
  112. !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
  113. !CBB_add_asn1(&private_key, &inner, CBS_ASN1_OCTETSTRING) ||
  114. // The PKCS#8 encoding stores only the 32-byte seed which is the first 32
  115. // bytes of the private key.
  116. !CBB_add_bytes(&inner, key->key.priv, 32) ||
  117. !CBB_flush(out)) {
  118. OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
  119. return 0;
  120. }
  121. return 1;
  122. }
  123. static int ed25519_size(const EVP_PKEY *pkey) { return 64; }
  124. static int ed25519_bits(const EVP_PKEY *pkey) { return 256; }
  125. const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth = {
  126. EVP_PKEY_ED25519,
  127. {0x2b, 0x65, 0x70},
  128. 3,
  129. ed25519_pub_decode,
  130. ed25519_pub_encode,
  131. ed25519_pub_cmp,
  132. ed25519_priv_decode,
  133. ed25519_priv_encode,
  134. NULL /* pkey_opaque */,
  135. ed25519_size,
  136. ed25519_bits,
  137. NULL /* param_missing */,
  138. NULL /* param_copy */,
  139. NULL /* param_cmp */,
  140. ed25519_free,
  141. };
  142. EVP_PKEY *EVP_PKEY_new_ed25519_public(const uint8_t public_key[32]) {
  143. EVP_PKEY *ret = EVP_PKEY_new();
  144. if (ret == NULL ||
  145. !EVP_PKEY_set_type(ret, EVP_PKEY_ED25519) ||
  146. !set_pubkey(ret, public_key)) {
  147. EVP_PKEY_free(ret);
  148. return NULL;
  149. }
  150. return ret;
  151. }
  152. EVP_PKEY *EVP_PKEY_new_ed25519_private(const uint8_t private_key[64]) {
  153. EVP_PKEY *ret = EVP_PKEY_new();
  154. if (ret == NULL ||
  155. !EVP_PKEY_set_type(ret, EVP_PKEY_ED25519) ||
  156. !set_privkey(ret, private_key)) {
  157. EVP_PKEY_free(ret);
  158. return NULL;
  159. }
  160. return ret;
  161. }