]>
Commit | Line | Data |
---|---|---|
4de88fe6 MC |
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 | |
80ce21fe F |
14 | # pragma once |
15 | ||
4de88fe6 MC |
16 | # include <openssl/opensslconf.h> |
17 | ||
18 | # ifndef OPENSSL_NO_EC | |
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" | |
24 | ||
af6d8dd3 MC |
25 | # define X25519_KEYLEN 32 |
26 | # define X448_KEYLEN 56 | |
27 | # define ED25519_KEYLEN 32 | |
28 | # define ED448_KEYLEN 57 | |
4de88fe6 MC |
29 | |
30 | # define MAX_KEYLEN ED448_KEYLEN | |
31 | ||
af6d8dd3 MC |
32 | # define X25519_BITS 253 |
33 | # define X25519_SECURITY_BITS 128 | |
4de88fe6 | 34 | |
af6d8dd3 MC |
35 | # define X448_BITS 448 |
36 | # define X448_SECURITY_BITS 224 | |
4de88fe6 | 37 | |
af6d8dd3 MC |
38 | # define ED25519_BITS 256 |
39 | /* RFC8032 Section 8.5 */ | |
40 | # define ED25519_SECURITY_BITS 128 | |
41 | # define ED25519_SIGSIZE 64 | |
4de88fe6 | 42 | |
af6d8dd3 MC |
43 | # define ED448_BITS 456 |
44 | /* RFC8032 Section 8.5 */ | |
45 | # define ED448_SECURITY_BITS 224 | |
46 | # define ED448_SIGSIZE 114 | |
4de88fe6 | 47 | |
244bc297 MC |
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 | ||
4de88fe6 | 65 | struct ecx_key_st { |
b4250010 | 66 | OSSL_LIB_CTX *libctx; |
8dbef010 | 67 | char *propq; |
4de88fe6 MC |
68 | unsigned int haspubkey:1; |
69 | unsigned char pubkey[MAX_KEYLEN]; | |
70 | unsigned char *privkey; | |
71 | size_t keylen; | |
244bc297 | 72 | ECX_KEY_TYPE type; |
4de88fe6 MC |
73 | CRYPTO_REF_COUNT references; |
74 | CRYPTO_RWLOCK *lock; | |
75 | }; | |
76 | ||
77 | typedef struct ecx_key_st ECX_KEY; | |
78 | ||
43cd3701 | 79 | size_t ecx_key_length(ECX_KEY_TYPE type); |
b4250010 | 80 | ECX_KEY *ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type, int haspubkey, |
8dbef010 | 81 | const char *propq); |
6963979f | 82 | void ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx); |
4de88fe6 MC |
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 | ||
b4250010 | 92 | int ED25519_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[32], |
8dbef010 | 93 | const uint8_t private_key[32], const char *propq); |
3965480c | 94 | int ED25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len, |
5435044f | 95 | const uint8_t public_key[32], const uint8_t private_key[32], |
b4250010 | 96 | OSSL_LIB_CTX *libctx, const char *propq); |
3965480c | 97 | int ED25519_verify(const uint8_t *message, size_t message_len, |
5435044f | 98 | const uint8_t signature[64], const uint8_t public_key[32], |
b4250010 | 99 | OSSL_LIB_CTX *libctx, const char *propq); |
3965480c | 100 | |
b4250010 | 101 | int ED448_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[57], |
8dbef010 | 102 | const uint8_t private_key[57], const char *propq); |
b4250010 | 103 | int ED448_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig, const uint8_t *message, |
3965480c MC |
104 | size_t message_len, const uint8_t public_key[57], |
105 | const uint8_t private_key[57], const uint8_t *context, | |
8dbef010 | 106 | size_t context_len, const char *propq); |
3965480c | 107 | |
b4250010 | 108 | int ED448_verify(OSSL_LIB_CTX *ctx, const uint8_t *message, size_t message_len, |
3965480c | 109 | const uint8_t signature[114], const uint8_t public_key[57], |
8dbef010 | 110 | const uint8_t *context, size_t context_len, const char *propq); |
3965480c | 111 | |
4de88fe6 MC |
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 | ||
25b16562 | 117 | |
0abae163 | 118 | /* Backend support */ |
969024b4 | 119 | int ecx_public_from_private(ECX_KEY *key); |
0abae163 RL |
120 | int ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[], |
121 | int include_private); | |
122 | ||
25b16562 RL |
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); | |
4de88fe6 MC |
127 | # endif /* OPENSSL_NO_EC */ |
128 | #endif |