]> git.ipfire.org Git - thirdparty/openssl.git/blob - include/crypto/ecx.h
PROV: Add type specific PKCS#8 decoding to the DER->key decoders
[thirdparty/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 <openssl/x509.h>
24 # include "internal/refcount.h"
25 # include "crypto/types.h"
26
27 # define X25519_KEYLEN 32
28 # define X448_KEYLEN 56
29 # define ED25519_KEYLEN 32
30 # define ED448_KEYLEN 57
31
32 # define MAX_KEYLEN ED448_KEYLEN
33
34 # define X25519_BITS 253
35 # define X25519_SECURITY_BITS 128
36
37 # define X448_BITS 448
38 # define X448_SECURITY_BITS 224
39
40 # define ED25519_BITS 256
41 /* RFC8032 Section 8.5 */
42 # define ED25519_SECURITY_BITS 128
43 # define ED25519_SIGSIZE 64
44
45 # define ED448_BITS 456
46 /* RFC8032 Section 8.5 */
47 # define ED448_SECURITY_BITS 224
48 # define ED448_SIGSIZE 114
49
50
51 typedef enum {
52 ECX_KEY_TYPE_X25519,
53 ECX_KEY_TYPE_X448,
54 ECX_KEY_TYPE_ED25519,
55 ECX_KEY_TYPE_ED448
56 } ECX_KEY_TYPE;
57
58 #define KEYTYPE2NID(type) \
59 ((type) == ECX_KEY_TYPE_X25519 \
60 ? EVP_PKEY_X25519 \
61 : ((type) == ECX_KEY_TYPE_X448 \
62 ? EVP_PKEY_X448 \
63 : ((type) == ECX_KEY_TYPE_ED25519 \
64 ? EVP_PKEY_ED25519 \
65 : EVP_PKEY_ED448)))
66
67 struct ecx_key_st {
68 OSSL_LIB_CTX *libctx;
69 char *propq;
70 unsigned int haspubkey:1;
71 unsigned char pubkey[MAX_KEYLEN];
72 unsigned char *privkey;
73 size_t keylen;
74 ECX_KEY_TYPE type;
75 CRYPTO_REF_COUNT references;
76 CRYPTO_RWLOCK *lock;
77 };
78
79 typedef struct ecx_key_st ECX_KEY;
80
81 size_t ossl_ecx_key_length(ECX_KEY_TYPE type);
82 ECX_KEY *ossl_ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type,
83 int haspubkey, const char *propq);
84 void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx);
85 unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key);
86 void ossl_ecx_key_free(ECX_KEY *key);
87 int ossl_ecx_key_up_ref(ECX_KEY *key);
88
89 int ossl_x25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
90 const uint8_t peer_public_value[32]);
91 void ossl_x25519_public_from_private(uint8_t out_public_value[32],
92 const uint8_t private_key[32]);
93
94 int
95 ossl_ed25519_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[32],
96 const uint8_t private_key[32],
97 const char *propq);
98 int
99 ossl_ed25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
100 const uint8_t public_key[32], const uint8_t private_key[32],
101 OSSL_LIB_CTX *libctx, const char *propq);
102 int
103 ossl_ed25519_verify(const uint8_t *message, size_t message_len,
104 const uint8_t signature[64], const uint8_t public_key[32],
105 OSSL_LIB_CTX *libctx, const char *propq);
106
107 int
108 ossl_ed448_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[57],
109 const uint8_t private_key[57], const char *propq);
110 int
111 ossl_ed448_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig, const uint8_t *message,
112 size_t message_len, const uint8_t public_key[57],
113 const uint8_t private_key[57], const uint8_t *context,
114 size_t context_len, const char *propq);
115
116 int
117 ossl_ed448_verify(OSSL_LIB_CTX *ctx, const uint8_t *message, size_t message_len,
118 const uint8_t signature[114], const uint8_t public_key[57],
119 const uint8_t *context, size_t context_len, const char *propq);
120
121 int
122 ossl_x448(uint8_t out_shared_key[56], const uint8_t private_key[56],
123 const uint8_t peer_public_value[56]);
124 void
125 ossl_x448_public_from_private(uint8_t out_public_value[56],
126 const uint8_t private_key[56]);
127
128
129 /* Backend support */
130 typedef enum {
131 KEY_OP_PUBLIC,
132 KEY_OP_PRIVATE,
133 KEY_OP_KEYGEN
134 } ecx_key_op_t;
135
136 ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg,
137 const unsigned char *p, int plen,
138 int pkey_id, ecx_key_op_t op,
139 OSSL_LIB_CTX *libctx, const char *propq);
140
141 int ossl_ecx_public_from_private(ECX_KEY *key);
142 int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
143 int include_private);
144 ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
145 OSSL_LIB_CTX *libctx, const char *propq);
146
147 ECX_KEY *ossl_evp_pkey_get1_X25519(EVP_PKEY *pkey);
148 ECX_KEY *ossl_evp_pkey_get1_X448(EVP_PKEY *pkey);
149 ECX_KEY *ossl_evp_pkey_get1_ED25519(EVP_PKEY *pkey);
150 ECX_KEY *ossl_evp_pkey_get1_ED448(EVP_PKEY *pkey);
151 # endif /* OPENSSL_NO_EC */
152 #endif