Update copyright year
[openssl.git] / include / crypto / ecx.h
1 /*
2  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* Internal EC functions for other submodules: not for application use */
11
12 #ifndef OSSL_CRYPTO_ECX_H
13 # define OSSL_CRYPTO_ECX_H
14 # pragma once
15
16 # include <openssl/opensslconf.h>
17
18 # ifndef OPENSSL_NO_EC
19
20 #  include <openssl/core.h>
21 #  include <openssl/e_os2.h>
22 #  include <openssl/crypto.h>
23 #  include "internal/refcount.h"
24
25 #  define X25519_KEYLEN         32
26 #  define X448_KEYLEN           56
27 #  define ED25519_KEYLEN        32
28 #  define ED448_KEYLEN          57
29
30 #  define MAX_KEYLEN  ED448_KEYLEN
31
32 #  define X25519_BITS           253
33 #  define X25519_SECURITY_BITS  128
34
35 #  define X448_BITS             448
36 #  define X448_SECURITY_BITS    224
37
38 #  define ED25519_BITS          256
39 /* RFC8032 Section 8.5 */
40 #  define ED25519_SECURITY_BITS 128
41 #  define ED25519_SIGSIZE       64
42
43 #  define ED448_BITS            456
44 /* RFC8032 Section 8.5 */
45 #  define ED448_SECURITY_BITS   224
46 #  define ED448_SIGSIZE         114
47
48
49 typedef enum {
50     ECX_KEY_TYPE_X25519,
51     ECX_KEY_TYPE_X448,
52     ECX_KEY_TYPE_ED25519,
53     ECX_KEY_TYPE_ED448
54 } ECX_KEY_TYPE;
55
56 #define KEYTYPE2NID(type) \
57     ((type) == ECX_KEY_TYPE_X25519 \
58      ?  EVP_PKEY_X25519 \
59      : ((type) == ECX_KEY_TYPE_X448 \
60         ? EVP_PKEY_X448 \
61         : ((type) == ECX_KEY_TYPE_ED25519 \
62            ? EVP_PKEY_ED25519 \
63            : EVP_PKEY_ED448)))
64
65 struct ecx_key_st {
66     OSSL_LIB_CTX *libctx;
67     char *propq;
68     unsigned int haspubkey:1;
69     unsigned char pubkey[MAX_KEYLEN];
70     unsigned char *privkey;
71     size_t keylen;
72     ECX_KEY_TYPE type;
73     CRYPTO_REF_COUNT references;
74     CRYPTO_RWLOCK *lock;
75 };
76
77 typedef struct ecx_key_st ECX_KEY;
78
79 size_t ecx_key_length(ECX_KEY_TYPE type);
80 ECX_KEY *ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type, int haspubkey,
81                      const char *propq);
82 void ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx);
83 unsigned char *ecx_key_allocate_privkey(ECX_KEY *key);
84 void ecx_key_free(ECX_KEY *key);
85 int ecx_key_up_ref(ECX_KEY *key);
86
87 int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
88            const uint8_t peer_public_value[32]);
89 void X25519_public_from_private(uint8_t out_public_value[32],
90                                 const uint8_t private_key[32]);
91
92 int ED25519_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[32],
93                                 const uint8_t private_key[32], const char *propq);
94 int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
95                  const uint8_t public_key[32], const uint8_t private_key[32],
96                  OSSL_LIB_CTX *libctx, const char *propq);
97 int ED25519_verify(const uint8_t *message, size_t message_len,
98                    const uint8_t signature[64], const uint8_t public_key[32],
99                    OSSL_LIB_CTX *libctx, const char *propq);
100
101 int ED448_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[57],
102                               const uint8_t private_key[57], const char *propq);
103 int ED448_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig, const uint8_t *message,
104                size_t message_len, const uint8_t public_key[57],
105                const uint8_t private_key[57], const uint8_t *context,
106                size_t context_len, const char *propq);
107
108 int ED448_verify(OSSL_LIB_CTX *ctx, const uint8_t *message, size_t message_len,
109                  const uint8_t signature[114], const uint8_t public_key[57],
110                  const uint8_t *context, size_t context_len, const char *propq);
111
112 int X448(uint8_t out_shared_key[56], const uint8_t private_key[56],
113          const uint8_t peer_public_value[56]);
114 void X448_public_from_private(uint8_t out_public_value[56],
115                               const uint8_t private_key[56]);
116
117
118 /* Backend support */
119 int ecx_public_from_private(ECX_KEY *key);
120 int ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
121                      int include_private);
122
123 ECX_KEY *evp_pkey_get1_X25519(EVP_PKEY *pkey);
124 ECX_KEY *evp_pkey_get1_X448(EVP_PKEY *pkey);
125 ECX_KEY *evp_pkey_get1_ED25519(EVP_PKEY *pkey);
126 ECX_KEY *evp_pkey_get1_ED448(EVP_PKEY *pkey);
127 # endif /* OPENSSL_NO_EC */
128 #endif