p_ed25519_asn1.c
Go to the documentation of this file.
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 ed25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
32  if (len != 32) {
34  return 0;
35  }
36 
38  if (key == NULL) {
40  return 0;
41  }
42 
43  // The RFC 8032 encoding stores only the 32-byte seed, so we must recover the
44  // full representation which we use from it.
45  uint8_t pubkey_unused[32];
46  ED25519_keypair_from_seed(pubkey_unused, key->key.priv, in);
47  key->has_private = 1;
48 
49  ed25519_free(pkey);
50  pkey->pkey.ptr = key;
51  return 1;
52 }
53 
54 static int ed25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
55  if (len != 32) {
57  return 0;
58  }
59 
61  if (key == NULL) {
63  return 0;
64  }
65 
66  OPENSSL_memcpy(key->key.pub.value, in, 32);
67  key->has_private = 0;
68 
69  ed25519_free(pkey);
70  pkey->pkey.ptr = key;
71  return 1;
72 }
73 
74 static int ed25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out,
75  size_t *out_len) {
76  const ED25519_KEY *key = pkey->pkey.ptr;
77  if (!key->has_private) {
79  return 0;
80  }
81 
82  if (out == NULL) {
83  *out_len = 32;
84  return 1;
85  }
86 
87  if (*out_len < 32) {
89  return 0;
90  }
91 
92  // The raw private key format is the first 32 bytes of the private key.
93  OPENSSL_memcpy(out, key->key.priv, 32);
94  *out_len = 32;
95  return 1;
96 }
97 
98 static int ed25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out,
99  size_t *out_len) {
100  const ED25519_KEY *key = pkey->pkey.ptr;
101  if (out == NULL) {
102  *out_len = 32;
103  return 1;
104  }
105 
106  if (*out_len < 32) {
108  return 0;
109  }
110 
111  OPENSSL_memcpy(out, key->key.pub.value, 32);
112  *out_len = 32;
113  return 1;
114 }
115 
116 static int ed25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
117  // See RFC 8410, section 4.
118 
119  // The parameters must be omitted. Public keys have length 32.
120  if (CBS_len(params) != 0) {
122  return 0;
123  }
124 
126 }
127 
128 static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
129  const ED25519_KEY *key = pkey->pkey.ptr;
130 
131  // See RFC 8410, section 4.
132  CBB spki, algorithm, oid, key_bitstring;
133  if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
134  !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
135  !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
137  !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
138  !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
139  !CBB_add_bytes(&key_bitstring, key->key.pub.value, 32) ||
140  !CBB_flush(out)) {
142  return 0;
143  }
144 
145  return 1;
146 }
147 
148 static int ed25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
149  const ED25519_KEY *a_key = a->pkey.ptr;
150  const ED25519_KEY *b_key = b->pkey.ptr;
151  return OPENSSL_memcmp(a_key->key.pub.value, b_key->key.pub.value, 32) == 0;
152 }
153 
154 static int ed25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
155  // See RFC 8410, section 7.
156 
157  // Parameters must be empty. The key is a 32-byte value wrapped in an extra
158  // OCTET STRING layer.
159  CBS inner;
160  if (CBS_len(params) != 0 ||
162  CBS_len(key) != 0) {
164  return 0;
165  }
166 
167  return ed25519_set_priv_raw(out, CBS_data(&inner), CBS_len(&inner));
168 }
169 
170 static int ed25519_priv_encode(CBB *out, const EVP_PKEY *pkey) {
171  ED25519_KEY *key = pkey->pkey.ptr;
172  if (!key->has_private) {
174  return 0;
175  }
176 
177  // See RFC 8410, section 7.
178  CBB pkcs8, algorithm, oid, private_key, inner;
179  if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
180  !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
181  !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
182  !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
186  // The PKCS#8 encoding stores only the 32-byte seed which is the first 32
187  // bytes of the private key.
188  !CBB_add_bytes(&inner, key->key.priv, 32) ||
189  !CBB_flush(out)) {
191  return 0;
192  }
193 
194  return 1;
195 }
196 
197 static int ed25519_size(const EVP_PKEY *pkey) { return 64; }
198 
199 static int ed25519_bits(const EVP_PKEY *pkey) { return 253; }
200 
203  {0x2b, 0x65, 0x70},
204  3,
214  NULL /* pkey_opaque */,
215  ed25519_size,
216  ed25519_bits,
217  NULL /* param_missing */,
218  NULL /* param_copy */,
219  NULL /* param_cmp */,
220  ed25519_free,
221 };
ED25519_KEY::key
union ED25519_KEY::@348 key
CBB_flush
#define CBB_flush
Definition: boringssl_prefix_symbols.h:1045
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
cbs_st
Definition: bytestring.h:39
OPENSSL_memcmp
static int OPENSSL_memcmp(const void *s1, const void *s2, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:811
ed25519_free
static void ed25519_free(EVP_PKEY *pkey)
Definition: p_ed25519_asn1.c:26
ed25519_priv_decode
static int ed25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key)
Definition: p_ed25519_asn1.c:154
evp.h
CBS_data
#define CBS_data
Definition: boringssl_prefix_symbols.h:1057
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
CBS_ASN1_OCTETSTRING
#define CBS_ASN1_OCTETSTRING
Definition: bytestring.h:209
CBB_add_u8
#define CBB_add_u8
Definition: boringssl_prefix_symbols.h:1036
evp_pkey_st::ptr
void * ptr
Definition: evp.h:1054
evp_pkey_asn1_method_st::oid_len
uint8_t oid_len
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:72
CBS_len
#define CBS_len
Definition: boringssl_prefix_symbols.h:1089
EVP_R_NOT_A_PRIVATE_KEY
#define EVP_R_NOT_A_PRIVATE_KEY
Definition: evp_errors.h:90
CBS_get_asn1
#define CBS_get_asn1
Definition: boringssl_prefix_symbols.h:1061
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
ed25519_bits
static int ed25519_bits(const EVP_PKEY *pkey)
Definition: p_ed25519_asn1.c:199
EVP_R_ENCODE_ERROR
#define EVP_R_ENCODE_ERROR
Definition: evp_errors.h:65
EVP_PKEY_ED25519
#define EVP_PKEY_ED25519
Definition: evp.h:179
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
EVP_R_DECODE_ERROR
#define EVP_R_DECODE_ERROR
Definition: evp_errors.h:62
bytestring.h
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
ED25519_KEY::pub
struct ED25519_KEY::@348::@349 pub
ED25519_keypair_from_seed
#define ED25519_keypair_from_seed
Definition: boringssl_prefix_symbols.h:1396
internal.h
ed25519_get_pub_raw
static int ed25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out, size_t *out_len)
Definition: p_ed25519_asn1.c:98
evp_pkey_st
Definition: evp.h:1046
oid
uint8_t oid[9]
Definition: digest_extra.c:124
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
CBB_add_asn1
#define CBB_add_asn1
Definition: boringssl_prefix_symbols.h:1019
err.h
ed25519_priv_encode
static int ed25519_priv_encode(CBB *out, const EVP_PKEY *pkey)
Definition: p_ed25519_asn1.c:170
ed25519_pub_decode
static int ed25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key)
Definition: p_ed25519_asn1.c:116
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
ed25519_set_pub_raw
static int ed25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len)
Definition: p_ed25519_asn1.c:54
ed25519_asn1_meth
const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth
Definition: p_ed25519_asn1.c:201
ed25519_pub_cmp
static int ed25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
Definition: p_ed25519_asn1.c:148
ed25519_size
static int ed25519_size(const EVP_PKEY *pkey)
Definition: p_ed25519_asn1.c:197
evp_pkey_st::pkey
union evp_pkey_st::@364 pkey
CBB_add_bytes
#define CBB_add_bytes
Definition: boringssl_prefix_symbols.h:1025
key
const char * key
Definition: hpack_parser_table.cc:164
CBS_ASN1_BITSTRING
#define CBS_ASN1_BITSTRING
Definition: bytestring.h:208
CBS_ASN1_OBJECT
#define CBS_ASN1_OBJECT
Definition: bytestring.h:211
private_key
Definition: hrss.c:1885
ed25519_pub_encode
static int ed25519_pub_encode(CBB *out, const EVP_PKEY *pkey)
Definition: p_ed25519_asn1.c:128
ed25519_get_priv_raw
static int ed25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out, size_t *out_len)
Definition: p_ed25519_asn1.c:74
EVP_R_BUFFER_TOO_SMALL
#define EVP_R_BUFFER_TOO_SMALL
Definition: evp_errors.h:60
curve25519.h
evp_pkey_asn1_method_st
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:69
mem.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
CBS_ASN1_SEQUENCE
#define CBS_ASN1_SEQUENCE
Definition: bytestring.h:214
tests.interop.resources.private_key
def private_key()
Definition: interop/resources.py:29
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
ed25519_set_priv_raw
static int ed25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len)
Definition: p_ed25519_asn1.c:31
ED25519_KEY
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:234
ERR_R_MALLOC_FAILURE
#define ERR_R_MALLOC_FAILURE
Definition: err.h:371
evp_pkey_asn1_method_st::oid
uint8_t oid[9]
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:71
CBB_add_asn1_uint64
#define CBB_add_asn1_uint64
Definition: boringssl_prefix_symbols.h:1024
cbb_st
Definition: bytestring.h:375


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:38