p_x25519.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/curve25519.h>
18 #include <openssl/err.h>
19 #include <openssl/mem.h>
20 
21 #include "internal.h"
22 
23 
24 // X25519 has no parameters to copy.
25 static int pkey_x25519_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { return 1; }
26 
29  if (key == NULL) {
31  return 0;
32  }
33 
34  if (!EVP_PKEY_set_type(pkey, EVP_PKEY_X25519)) {
36  return 0;
37  }
38 
39  X25519_keypair(key->pub, key->priv);
40  key->has_private = 1;
41 
42  OPENSSL_free(pkey->pkey.ptr);
43  pkey->pkey.ptr = key;
44  return 1;
45 }
46 
48  size_t *out_len) {
49  if (ctx->pkey == NULL || ctx->peerkey == NULL) {
51  return 0;
52  }
53 
54  const X25519_KEY *our_key = ctx->pkey->pkey.ptr;
55  const X25519_KEY *peer_key = ctx->peerkey->pkey.ptr;
56  if (our_key == NULL || peer_key == NULL) {
58  return 0;
59  }
60 
61  if (!our_key->has_private) {
63  return 0;
64  }
65 
66  if (out != NULL) {
67  if (*out_len < 32) {
69  return 0;
70  }
71  if (!X25519(out, our_key->priv, peer_key->pub)) {
73  return 0;
74  }
75  }
76 
77  *out_len = 32;
78  return 1;
79 }
80 
81 static int pkey_x25519_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
82  switch (type) {
84  // |EVP_PKEY_derive_set_peer| requires the key implement this command,
85  // even if it is a no-op.
86  return 1;
87 
88  default:
90  return 0;
91  }
92 }
93 
96  NULL /* init */,
98  NULL /* cleanup */,
100  NULL /* sign */,
101  NULL /* sign_message */,
102  NULL /* verify */,
103  NULL /* verify_message */,
104  NULL /* verify_recover */,
105  NULL /* encrypt */,
106  NULL /* decrypt */,
108  NULL /* paramgen */,
110 };
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
ctx
Definition: benchmark-async.c:30
EVP_R_COMMAND_NOT_SUPPORTED
#define EVP_R_COMMAND_NOT_SUPPORTED
Definition: evp_errors.h:61
evp.h
EVP_PKEY_CTRL_PEER_KEY
#define EVP_PKEY_CTRL_PEER_KEY
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:162
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
evp_pkey_st::ptr
void * ptr
Definition: evp.h:1054
EVP_R_NOT_A_PRIVATE_KEY
#define EVP_R_NOT_A_PRIVATE_KEY
Definition: evp_errors.h:90
pkey_x25519_copy
static int pkey_x25519_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
Definition: p_x25519.c:25
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
EVP_R_INVALID_PEER_KEY
#define EVP_R_INVALID_PEER_KEY
Definition: evp_errors.h:94
pkey_x25519_derive
static int pkey_x25519_derive(EVP_PKEY_CTX *ctx, uint8_t *out, size_t *out_len)
Definition: p_x25519.c:47
pkey_x25519_keygen
static int pkey_x25519_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
Definition: p_x25519.c:27
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
pkey_x25519_ctrl
static int pkey_x25519_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
Definition: p_x25519.c:81
EVP_PKEY_X25519
#define EVP_PKEY_X25519
Definition: evp.h:180
evp_pkey_ctx_st
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:182
internal.h
evp_pkey_st
Definition: evp.h:1046
EVP_PKEY_set_type
#define EVP_PKEY_set_type
Definition: boringssl_prefix_symbols.h:1655
err.h
evp_pkey_method_st
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:197
X25519_KEY::priv
uint8_t priv[32]
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:249
X25519_keypair
#define X25519_keypair
Definition: boringssl_prefix_symbols.h:2210
evp_pkey_st::pkey
union evp_pkey_st::@364 pkey
key
const char * key
Definition: hpack_parser_table.cc:164
x25519_pkey_meth
const EVP_PKEY_METHOD x25519_pkey_meth
Definition: p_x25519.c:94
EVP_R_BUFFER_TOO_SMALL
#define EVP_R_BUFFER_TOO_SMALL
Definition: evp_errors.h:60
curve25519.h
X25519_KEY::has_private
char has_private
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:250
X25519_KEY
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:247
mem.h
asyncio_get_stats.type
type
Definition: asyncio_get_stats.py:37
X25519_KEY::pub
uint8_t pub[32]
Definition: third_party/boringssl-with-bazel/src/crypto/evp/internal.h:248
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_R_KEYS_NOT_SET
#define EVP_R_KEYS_NOT_SET
Definition: evp_errors.h:77
X25519
#define X25519
Definition: boringssl_prefix_symbols.h:2209


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