rsa: add ossl_ prefix to internal rsa_ calls.
[openssl.git] / include / crypto / ecx.h
1 /*
2  * Copyright 2020 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 # include <openssl/opensslconf.h>
15
16 # ifndef OPENSSL_NO_EC
17
18 #  include <openssl/core.h>
19 #  include <openssl/e_os2.h>
20 #  include <openssl/crypto.h>
21 #  include "internal/refcount.h"
22
23 #  define X25519_KEYLEN         32
24 #  define X448_KEYLEN           56
25 #  define ED25519_KEYLEN        32
26 #  define ED448_KEYLEN          57
27
28 #  define MAX_KEYLEN  ED448_KEYLEN
29
30 #  define X25519_BITS           253
31 #  define X25519_SECURITY_BITS  128
32
33 #  define X448_BITS             448
34 #  define X448_SECURITY_BITS    224
35
36 #  define ED25519_BITS          256
37 /* RFC8032 Section 8.5 */
38 #  define ED25519_SECURITY_BITS 128
39 #  define ED25519_SIGSIZE       64
40
41 #  define ED448_BITS            456
42 /* RFC8032 Section 8.5 */
43 #  define ED448_SECURITY_BITS   224
44 #  define ED448_SIGSIZE         114
45
46
47 typedef enum {
48     ECX_KEY_TYPE_X25519,
49     ECX_KEY_TYPE_X448,
50     ECX_KEY_TYPE_ED25519,
51     ECX_KEY_TYPE_ED448
52 } ECX_KEY_TYPE;
53
54 #define KEYTYPE2NID(type) \
55     ((type) == ECX_KEY_TYPE_X25519 \
56      ?  EVP_PKEY_X25519 \
57      : ((type) == ECX_KEY_TYPE_X448 \
58         ? EVP_PKEY_X448 \
59         : ((type) == ECX_KEY_TYPE_ED25519 \
60            ? EVP_PKEY_ED25519 \
61            : EVP_PKEY_ED448)))
62
63 struct ecx_key_st {
64     OPENSSL_CTX *libctx;
65     char *propq;
66     unsigned int haspubkey:1;
67     unsigned char pubkey[MAX_KEYLEN];
68     unsigned char *privkey;
69     size_t keylen;
70     ECX_KEY_TYPE type;
71     CRYPTO_REF_COUNT references;
72     CRYPTO_RWLOCK *lock;
73 };
74
75 typedef struct ecx_key_st ECX_KEY;
76
77 size_t ecx_key_length(ECX_KEY_TYPE type);
78 ECX_KEY *ecx_key_new(OPENSSL_CTX *libctx, ECX_KEY_TYPE type, int haspubkey,
79                      const char *propq);
80 unsigned char *ecx_key_allocate_privkey(ECX_KEY *key);
81 void ecx_key_free(ECX_KEY *key);
82 int ecx_key_up_ref(ECX_KEY *key);
83
84 int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
85            const uint8_t peer_public_value[32]);
86 void X25519_public_from_private(uint8_t out_public_value[32],
87                                 const uint8_t private_key[32]);
88
89 int ED25519_public_from_private(OPENSSL_CTX *ctx, uint8_t out_public_key[32],
90                                 const uint8_t private_key[32], const char *propq);
91 int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
92                  const uint8_t public_key[32], const uint8_t private_key[32],
93                  OPENSSL_CTX *libctx, const char *propq);
94 int ED25519_verify(const uint8_t *message, size_t message_len,
95                    const uint8_t signature[64], const uint8_t public_key[32],
96                    OPENSSL_CTX *libctx, const char *propq);
97
98 int ED448_public_from_private(OPENSSL_CTX *ctx, uint8_t out_public_key[57],
99                               const uint8_t private_key[57], const char *propq);
100 int ED448_sign(OPENSSL_CTX *ctx, uint8_t *out_sig, const uint8_t *message,
101                size_t message_len, const uint8_t public_key[57],
102                const uint8_t private_key[57], const uint8_t *context,
103                size_t context_len, const char *propq);
104
105 int ED448_verify(OPENSSL_CTX *ctx, const uint8_t *message, size_t message_len,
106                  const uint8_t signature[114], const uint8_t public_key[57],
107                  const uint8_t *context, size_t context_len, const char *propq);
108
109 int X448(uint8_t out_shared_key[56], const uint8_t private_key[56],
110          const uint8_t peer_public_value[56]);
111 void X448_public_from_private(uint8_t out_public_value[56],
112                               const uint8_t private_key[56]);
113
114
115 /* Backend support */
116 int ecx_public_from_private(ECX_KEY *key);
117 int ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
118                      int include_private);
119
120 ECX_KEY *evp_pkey_get1_X25519(EVP_PKEY *pkey);
121 ECX_KEY *evp_pkey_get1_X448(EVP_PKEY *pkey);
122 ECX_KEY *evp_pkey_get1_ED25519(EVP_PKEY *pkey);
123 ECX_KEY *evp_pkey_get1_ED448(EVP_PKEY *pkey);
124 # endif /* OPENSSL_NO_EC */
125 #endif