p_x25519_asn1.c
Go to the documentation of this file.
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/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 x25519_free(EVP_PKEY *pkey) {
27  OPENSSL_free(pkey->pkey.ptr);
28  pkey->pkey.ptr = NULL;
29 }
30 
31 static int x25519_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  OPENSSL_memcpy(key->priv, in, 32);
44  X25519_public_from_private(key->pub, key->priv);
45  key->has_private = 1;
46 
47  x25519_free(pkey);
48  pkey->pkey.ptr = key;
49  return 1;
50 }
51 
52 static int x25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len) {
53  if (len != 32) {
55  return 0;
56  }
57 
59  if (key == NULL) {
61  return 0;
62  }
63 
64  OPENSSL_memcpy(key->pub, in, 32);
65  key->has_private = 0;
66 
67  x25519_free(pkey);
68  pkey->pkey.ptr = key;
69  return 1;
70 }
71 
72 static int x25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out,
73  size_t *out_len) {
74  const X25519_KEY *key = pkey->pkey.ptr;
75  if (!key->has_private) {
77  return 0;
78  }
79 
80  if (out == NULL) {
81  *out_len = 32;
82  return 1;
83  }
84 
85  if (*out_len < 32) {
87  return 0;
88  }
89 
90  OPENSSL_memcpy(out, key->priv, 32);
91  *out_len = 32;
92  return 1;
93 }
94 
95 static int x25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out,
96  size_t *out_len) {
97  const X25519_KEY *key = pkey->pkey.ptr;
98  if (out == NULL) {
99  *out_len = 32;
100  return 1;
101  }
102 
103  if (*out_len < 32) {
105  return 0;
106  }
107 
108  OPENSSL_memcpy(out, key->pub, 32);
109  *out_len = 32;
110  return 1;
111 }
112 
113 static int x25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
114  // See RFC 8410, section 4.
115 
116  // The parameters must be omitted. Public keys have length 32.
117  if (CBS_len(params) != 0) {
119  return 0;
120  }
121 
123 }
124 
125 static int x25519_pub_encode(CBB *out, const EVP_PKEY *pkey) {
126  const X25519_KEY *key = pkey->pkey.ptr;
127 
128  // See RFC 8410, section 4.
129  CBB spki, algorithm, oid, key_bitstring;
130  if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
131  !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
132  !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
134  !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
135  !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
136  !CBB_add_bytes(&key_bitstring, key->pub, 32) ||
137  !CBB_flush(out)) {
139  return 0;
140  }
141 
142  return 1;
143 }
144 
145 static int x25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
146  const X25519_KEY *a_key = a->pkey.ptr;
147  const X25519_KEY *b_key = b->pkey.ptr;
148  return OPENSSL_memcmp(a_key->pub, b_key->pub, 32) == 0;
149 }
150 
151 static int x25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
152  // See RFC 8410, section 7.
153 
154  // Parameters must be empty. The key is a 32-byte value wrapped in an extra
155  // OCTET STRING layer.
156  CBS inner;
157  if (CBS_len(params) != 0 ||
159  CBS_len(key) != 0) {
161  return 0;
162  }
163 
164  return x25519_set_priv_raw(out, CBS_data(&inner), CBS_len(&inner));
165 }
166 
167 static int x25519_priv_encode(CBB *out, const EVP_PKEY *pkey) {
168  X25519_KEY *key = pkey->pkey.ptr;
169  if (!key->has_private) {
171  return 0;
172  }
173 
174  // See RFC 8410, section 7.
175  CBB pkcs8, algorithm, oid, private_key, inner;
176  if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
177  !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
178  !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
179  !CBB_add_asn1(&algorithm, &oid, CBS_ASN1_OBJECT) ||
183  // The PKCS#8 encoding stores only the 32-byte seed which is the first 32
184  // bytes of the private key.
185  !CBB_add_bytes(&inner, key->priv, 32) ||
186  !CBB_flush(out)) {
188  return 0;
189  }
190 
191  return 1;
192 }
193 
194 static int x25519_size(const EVP_PKEY *pkey) { return 32; }
195 
196 static int x25519_bits(const EVP_PKEY *pkey) { return 253; }
197 
200  {0x2b, 0x65, 0x6e},
201  3,
211  NULL /* pkey_opaque */,
212  x25519_size,
213  x25519_bits,
214  NULL /* param_missing */,
215  NULL /* param_copy */,
216  NULL /* param_cmp */,
217  x25519_free,
218 };
219 
221  size_t len) {
222  // TODO(davidben): In OpenSSL, this function also works for |EVP_PKEY_EC|
223  // keys. Add support if it ever comes up.
224  if (pkey->type != EVP_PKEY_X25519) {
226  return 0;
227  }
228 
229  return x25519_set_pub_raw(pkey, in, len);
230 }
231 
232 size_t EVP_PKEY_get1_tls_encodedpoint(const EVP_PKEY *pkey, uint8_t **out_ptr) {
233  // TODO(davidben): In OpenSSL, this function also works for |EVP_PKEY_EC|
234  // keys. Add support if it ever comes up.
235  if (pkey->type != EVP_PKEY_X25519) {
237  return 0;
238  }
239 
240  const X25519_KEY *key = pkey->pkey.ptr;
241  if (key == NULL) {
243  return 0;
244  }
245 
246  *out_ptr = OPENSSL_memdup(key->pub, 32);
247  return *out_ptr == NULL ? 0 : 32;
248 }
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
x25519_get_priv_raw
static int x25519_get_priv_raw(const EVP_PKEY *pkey, uint8_t *out, size_t *out_len)
Definition: p_x25519_asn1.c:72
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
x25519_set_pub_raw
static int x25519_set_pub_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len)
Definition: p_x25519_asn1.c:52
x25519_priv_decode
static int x25519_priv_decode(EVP_PKEY *out, CBS *params, CBS *key)
Definition: p_x25519_asn1.c:151
EVP_R_ENCODE_ERROR
#define EVP_R_ENCODE_ERROR
Definition: evp_errors.h:65
x25519_size
static int x25519_size(const EVP_PKEY *pkey)
Definition: p_x25519_asn1.c:194
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
EVP_PKEY_X25519
#define EVP_PKEY_X25519
Definition: evp.h:180
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
internal.h
EVP_PKEY_set1_tls_encodedpoint
int EVP_PKEY_set1_tls_encodedpoint(EVP_PKEY *pkey, const uint8_t *in, size_t len)
Definition: p_x25519_asn1.c:220
evp_pkey_st
Definition: evp.h:1046
oid
uint8_t oid[9]
Definition: digest_extra.c:124
EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE
#define EVP_R_UNSUPPORTED_PUBLIC_KEY_TYPE
Definition: evp_errors.h:89
x25519_pub_cmp
static int x25519_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
Definition: p_x25519_asn1.c:145
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
EVP_PKEY_get1_tls_encodedpoint
size_t EVP_PKEY_get1_tls_encodedpoint(const EVP_PKEY *pkey, uint8_t **out_ptr)
Definition: p_x25519_asn1.c:232
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
x25519_priv_encode
static int x25519_priv_encode(CBB *out, const EVP_PKEY *pkey)
Definition: p_x25519_asn1.c:167
evp_pkey_st::type
int type
Definition: evp.h:1051
x25519_asn1_meth
const EVP_PKEY_ASN1_METHOD x25519_asn1_meth
Definition: p_x25519_asn1.c:198
evp_pkey_st::pkey
union evp_pkey_st::@364 pkey
x25519_pub_encode
static int x25519_pub_encode(CBB *out, const EVP_PKEY *pkey)
Definition: p_x25519_asn1.c:125
CBB_add_bytes
#define CBB_add_bytes
Definition: boringssl_prefix_symbols.h:1025
x25519_get_pub_raw
static int x25519_get_pub_raw(const EVP_PKEY *pkey, uint8_t *out, size_t *out_len)
Definition: p_x25519_asn1.c:95
key
const char * key
Definition: hpack_parser_table.cc:164
CBS_ASN1_BITSTRING
#define CBS_ASN1_BITSTRING
Definition: bytestring.h:208
x25519_pub_decode
static int x25519_pub_decode(EVP_PKEY *out, CBS *params, CBS *key)
Definition: p_x25519_asn1.c:113
X25519_public_from_private
#define X25519_public_from_private
Definition: boringssl_prefix_symbols.h:2211
CBS_ASN1_OBJECT
#define CBS_ASN1_OBJECT
Definition: bytestring.h:211
private_key
Definition: hrss.c:1885
OPENSSL_memdup
#define OPENSSL_memdup
Definition: boringssl_prefix_symbols.h:1887
x25519_bits
static int x25519_bits(const EVP_PKEY *pkey)
Definition: p_x25519_asn1.c:196
EVP_R_BUFFER_TOO_SMALL
#define EVP_R_BUFFER_TOO_SMALL
Definition: evp_errors.h:60
curve25519.h
EVP_R_NO_KEY_SET
#define EVP_R_NO_KEY_SET
Definition: evp_errors.h:80
X25519_KEY
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:247
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
X25519_KEY::pub
uint8_t pub[32]
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:248
x25519_free
static void x25519_free(EVP_PKEY *pkey)
Definition: p_x25519_asn1.c:26
CBS_ASN1_SEQUENCE
#define CBS_ASN1_SEQUENCE
Definition: bytestring.h:214
tests.interop.resources.private_key
def private_key()
Definition: interop/resources.py:29
x25519_set_priv_raw
static int x25519_set_priv_raw(EVP_PKEY *pkey, const uint8_t *in, size_t len)
Definition: p_x25519_asn1.c:31
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
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