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.
pkey_x25519_copy(EVP_PKEY_CTX * dst,EVP_PKEY_CTX * src)25 static int pkey_x25519_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { return 1; }
26
pkey_x25519_keygen(EVP_PKEY_CTX * ctx,EVP_PKEY * pkey)27 static int pkey_x25519_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
28 X25519_KEY *key = OPENSSL_malloc(sizeof(X25519_KEY));
29 if (key == NULL) {
30 return 0;
31 }
32
33 if (!EVP_PKEY_set_type(pkey, EVP_PKEY_X25519)) {
34 OPENSSL_free(key);
35 return 0;
36 }
37
38 X25519_keypair(key->pub, key->priv);
39 key->has_private = 1;
40
41 OPENSSL_free(pkey->pkey);
42 pkey->pkey = key;
43 return 1;
44 }
45
pkey_x25519_derive(EVP_PKEY_CTX * ctx,uint8_t * out,size_t * out_len)46 static int pkey_x25519_derive(EVP_PKEY_CTX *ctx, uint8_t *out,
47 size_t *out_len) {
48 if (ctx->pkey == NULL || ctx->peerkey == NULL) {
49 OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET);
50 return 0;
51 }
52
53 const X25519_KEY *our_key = ctx->pkey->pkey;
54 const X25519_KEY *peer_key = ctx->peerkey->pkey;
55 if (our_key == NULL || peer_key == NULL) {
56 OPENSSL_PUT_ERROR(EVP, EVP_R_KEYS_NOT_SET);
57 return 0;
58 }
59
60 if (!our_key->has_private) {
61 OPENSSL_PUT_ERROR(EVP, EVP_R_NOT_A_PRIVATE_KEY);
62 return 0;
63 }
64
65 if (out != NULL) {
66 if (*out_len < 32) {
67 OPENSSL_PUT_ERROR(EVP, EVP_R_BUFFER_TOO_SMALL);
68 return 0;
69 }
70 if (!X25519(out, our_key->priv, peer_key->pub)) {
71 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_PEER_KEY);
72 return 0;
73 }
74 }
75
76 *out_len = 32;
77 return 1;
78 }
79
pkey_x25519_ctrl(EVP_PKEY_CTX * ctx,int type,int p1,void * p2)80 static int pkey_x25519_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2) {
81 switch (type) {
82 case EVP_PKEY_CTRL_PEER_KEY:
83 // |EVP_PKEY_derive_set_peer| requires the key implement this command,
84 // even if it is a no-op.
85 return 1;
86
87 default:
88 OPENSSL_PUT_ERROR(EVP, EVP_R_COMMAND_NOT_SUPPORTED);
89 return 0;
90 }
91 }
92
93 const EVP_PKEY_METHOD x25519_pkey_meth = {
94 EVP_PKEY_X25519,
95 NULL /* init */,
96 pkey_x25519_copy,
97 NULL /* cleanup */,
98 pkey_x25519_keygen,
99 NULL /* sign */,
100 NULL /* sign_message */,
101 NULL /* verify */,
102 NULL /* verify_message */,
103 NULL /* verify_recover */,
104 NULL /* encrypt */,
105 NULL /* decrypt */,
106 pkey_x25519_derive,
107 NULL /* paramgen */,
108 pkey_x25519_ctrl,
109 };
110