]> git.ipfire.org Git - thirdparty/openssl.git/blame - include/crypto/ecx.h
configure: introduce no-ecx to remove ECX related feature
[thirdparty/openssl.git] / include / crypto / ecx.h
CommitLineData
4de88fe6 1/*
a28d06f3 2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
4de88fe6
MC
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
80ce21fe
F
14# pragma once
15
4de88fe6
MC
16# include <openssl/opensslconf.h>
17
4032cd9a 18# ifndef OPENSSL_NO_ECX
4de88fe6 19
0abae163 20# include <openssl/core.h>
4de88fe6
MC
21# include <openssl/e_os2.h>
22# include <openssl/crypto.h>
23# include "internal/refcount.h"
cf333799 24# include "crypto/types.h"
4de88fe6 25
af6d8dd3
MC
26# define X25519_KEYLEN 32
27# define X448_KEYLEN 56
28# define ED25519_KEYLEN 32
29# define ED448_KEYLEN 57
4de88fe6
MC
30
31# define MAX_KEYLEN ED448_KEYLEN
32
af6d8dd3
MC
33# define X25519_BITS 253
34# define X25519_SECURITY_BITS 128
4de88fe6 35
af6d8dd3
MC
36# define X448_BITS 448
37# define X448_SECURITY_BITS 224
4de88fe6 38
af6d8dd3
MC
39# define ED25519_BITS 256
40/* RFC8032 Section 8.5 */
41# define ED25519_SECURITY_BITS 128
42# define ED25519_SIGSIZE 64
4de88fe6 43
af6d8dd3
MC
44# define ED448_BITS 456
45/* RFC8032 Section 8.5 */
46# define ED448_SECURITY_BITS 224
47# define ED448_SIGSIZE 114
4de88fe6 48
244bc297
MC
49
50typedef enum {
51 ECX_KEY_TYPE_X25519,
52 ECX_KEY_TYPE_X448,
53 ECX_KEY_TYPE_ED25519,
54 ECX_KEY_TYPE_ED448
55} ECX_KEY_TYPE;
56
57#define KEYTYPE2NID(type) \
58 ((type) == ECX_KEY_TYPE_X25519 \
59 ? EVP_PKEY_X25519 \
60 : ((type) == ECX_KEY_TYPE_X448 \
61 ? EVP_PKEY_X448 \
62 : ((type) == ECX_KEY_TYPE_ED25519 \
63 ? EVP_PKEY_ED25519 \
64 : EVP_PKEY_ED448)))
65
4de88fe6 66struct ecx_key_st {
b4250010 67 OSSL_LIB_CTX *libctx;
8dbef010 68 char *propq;
4de88fe6
MC
69 unsigned int haspubkey:1;
70 unsigned char pubkey[MAX_KEYLEN];
71 unsigned char *privkey;
72 size_t keylen;
244bc297 73 ECX_KEY_TYPE type;
4de88fe6
MC
74 CRYPTO_REF_COUNT references;
75 CRYPTO_RWLOCK *lock;
76};
77
cf333799 78size_t ossl_ecx_key_length(ECX_KEY_TYPE type);
32ab57cb
SL
79ECX_KEY *ossl_ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type,
80 int haspubkey, const char *propq);
81void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx);
82unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key);
83void ossl_ecx_key_free(ECX_KEY *key);
84int ossl_ecx_key_up_ref(ECX_KEY *key);
b4f447c0 85ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection);
78c44b05 86int ossl_ecx_compute_key(ECX_KEY *peer, ECX_KEY *priv, size_t keylen,
87 unsigned char *secret, size_t *secretlen,
88 size_t outlen);
4de88fe6 89
054d43ff
SL
90int ossl_x25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
91 const uint8_t peer_public_value[32]);
92void ossl_x25519_public_from_private(uint8_t out_public_value[32],
93 const uint8_t private_key[32]);
94
95int
96ossl_ed25519_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[32],
97 const uint8_t private_key[32],
98 const char *propq);
99int
836080a8 100ossl_ed25519_sign(uint8_t *out_sig, const uint8_t *tbs, size_t tbs_len,
054d43ff 101 const uint8_t public_key[32], const uint8_t private_key[32],
836080a8
JM
102 const uint8_t dom2flag, const uint8_t phflag, const uint8_t csflag,
103 const uint8_t *context, size_t context_len,
054d43ff
SL
104 OSSL_LIB_CTX *libctx, const char *propq);
105int
836080a8 106ossl_ed25519_verify(const uint8_t *tbs, size_t tbs_len,
054d43ff 107 const uint8_t signature[64], const uint8_t public_key[32],
836080a8
JM
108 const uint8_t dom2flag, const uint8_t phflag, const uint8_t csflag,
109 const uint8_t *context, size_t context_len,
054d43ff 110 OSSL_LIB_CTX *libctx, const char *propq);
054d43ff
SL
111int
112ossl_ed448_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[57],
113 const uint8_t private_key[57], const char *propq);
114int
836080a8
JM
115ossl_ed448_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig,
116 const uint8_t *message, size_t message_len,
117 const uint8_t public_key[57], const uint8_t private_key[57],
118 const uint8_t *context, size_t context_len,
119 const uint8_t phflag, const char *propq);
054d43ff
SL
120
121int
836080a8
JM
122ossl_ed448_verify(OSSL_LIB_CTX *ctx,
123 const uint8_t *message, size_t message_len,
054d43ff 124 const uint8_t signature[114], const uint8_t public_key[57],
836080a8
JM
125 const uint8_t *context, size_t context_len,
126 const uint8_t phflag, const char *propq);
054d43ff
SL
127
128int
129ossl_x448(uint8_t out_shared_key[56], const uint8_t private_key[56],
130 const uint8_t peer_public_value[56]);
131void
132ossl_x448_public_from_private(uint8_t out_public_value[56],
4de88fe6
MC
133 const uint8_t private_key[56]);
134
25b16562 135
0abae163 136/* Backend support */
cf333799
RL
137typedef enum {
138 KEY_OP_PUBLIC,
139 KEY_OP_PRIVATE,
140 KEY_OP_KEYGEN
141} ecx_key_op_t;
142
143ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg,
144 const unsigned char *p, int plen,
145 int pkey_id, ecx_key_op_t op,
146 OSSL_LIB_CTX *libctx, const char *propq);
147
32ab57cb
SL
148int ossl_ecx_public_from_private(ECX_KEY *key);
149int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
150 int include_private);
cf333799
RL
151ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
152 OSSL_LIB_CTX *libctx, const char *propq);
32ab57cb
SL
153
154ECX_KEY *ossl_evp_pkey_get1_X25519(EVP_PKEY *pkey);
155ECX_KEY *ossl_evp_pkey_get1_X448(EVP_PKEY *pkey);
156ECX_KEY *ossl_evp_pkey_get1_ED25519(EVP_PKEY *pkey);
157ECX_KEY *ossl_evp_pkey_get1_ED448(EVP_PKEY *pkey);
4032cd9a 158# endif /* OPENSSL_NO_ECX */
4de88fe6 159#endif