| 1 |
/* |
| 2 |
* Copyright 2020-2025 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_ECX |
| 19 |
|
| 20 |
#include <openssl/core.h> |
| 21 |
#include <openssl/e_os2.h> |
| 22 |
#include <openssl/crypto.h> |
| 23 |
#include "internal/refcount.h" |
| 24 |
#include "crypto/types.h" |
| 25 |
|
| 26 |
#define X25519_KEYLEN 32 |
| 27 |
#define X448_KEYLEN 56 |
| 28 |
#define ED25519_KEYLEN 32 |
| 29 |
#define ED448_KEYLEN 57 |
| 30 |
|
| 31 |
#define MAX_KEYLEN ED448_KEYLEN |
| 32 |
|
| 33 |
#define X25519_BITS 253 |
| 34 |
#define X25519_SECURITY_BITS 128 |
| 35 |
|
| 36 |
#define X448_BITS 448 |
| 37 |
#define X448_SECURITY_BITS 224 |
| 38 |
|
| 39 |
#define ED25519_BITS 256 |
| 40 |
/* RFC8032 Section 8.5 */ |
| 41 |
#define ED25519_SECURITY_BITS 128 |
| 42 |
#define ED25519_SIGSIZE 64 |
| 43 |
|
| 44 |
#define ED448_BITS 456 |
| 45 |
/* RFC8032 Section 8.5 */ |
| 46 |
#define ED448_SECURITY_BITS 224 |
| 47 |
#define ED448_SIGSIZE 114 |
| 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 |
}; |
| 75 |
|
| 76 |
size_t ossl_ecx_key_length(ECX_KEY_TYPE type); |
| 77 |
ECX_KEY *ossl_ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type, |
| 78 |
int haspubkey, const char *propq); |
| 79 |
void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx); |
| 80 |
unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key); |
| 81 |
void ossl_ecx_key_free(ECX_KEY *key); |
| 82 |
int ossl_ecx_key_up_ref(ECX_KEY *key); |
| 83 |
ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection); |
| 84 |
int ossl_ecx_compute_key(ECX_KEY *peer, ECX_KEY *priv, size_t keylen, |
| 85 |
unsigned char *secret, size_t *secretlen, |
| 86 |
size_t outlen); |
| 87 |
|
| 88 |
int ossl_x25519(uint8_t out_shared_key[32], const uint8_t private_key[32], |
| 89 |
const uint8_t peer_public_value[32]); |
| 90 |
void ossl_x25519_public_from_private(uint8_t out_public_value[32], |
| 91 |
const uint8_t private_key[32]); |
| 92 |
|
| 93 |
int ossl_ed25519_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[32], |
| 94 |
const uint8_t private_key[32], |
| 95 |
const char *propq); |
| 96 |
int ossl_ed25519_sign(uint8_t *out_sig, const uint8_t *tbs, size_t tbs_len, |
| 97 |
const uint8_t public_key[32], const uint8_t private_key[32], |
| 98 |
const uint8_t dom2flag, const uint8_t phflag, const uint8_t csflag, |
| 99 |
const uint8_t *context, size_t context_len, |
| 100 |
OSSL_LIB_CTX *libctx, const char *propq); |
| 101 |
int ossl_ed25519_verify(const uint8_t *tbs, size_t tbs_len, |
| 102 |
const uint8_t signature[64], const uint8_t public_key[32], |
| 103 |
const uint8_t dom2flag, const uint8_t phflag, const uint8_t csflag, |
| 104 |
const uint8_t *context, size_t context_len, |
| 105 |
OSSL_LIB_CTX *libctx, const char *propq); |
| 106 |
int ossl_ed25519_pubkey_verify(const uint8_t *pub, size_t pub_len); |
| 107 |
int ossl_ed448_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[57], |
| 108 |
const uint8_t private_key[57], const char *propq); |
| 109 |
int ossl_ed448_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig, |
| 110 |
const uint8_t *message, size_t message_len, |
| 111 |
const uint8_t public_key[57], const uint8_t private_key[57], |
| 112 |
const uint8_t *context, size_t context_len, |
| 113 |
const uint8_t phflag, const char *propq); |
| 114 |
|
| 115 |
int ossl_ed448_verify(OSSL_LIB_CTX *ctx, |
| 116 |
const uint8_t *message, size_t message_len, |
| 117 |
const uint8_t signature[114], const uint8_t public_key[57], |
| 118 |
const uint8_t *context, size_t context_len, |
| 119 |
const uint8_t phflag, const char *propq); |
| 120 |
|
| 121 |
int ossl_ed448_pubkey_verify(const uint8_t *pub, size_t pub_len); |
| 122 |
|
| 123 |
int ossl_x448(uint8_t out_shared_key[56], const uint8_t private_key[56], |
| 124 |
const uint8_t peer_public_value[56]); |
| 125 |
void ossl_x448_public_from_private(uint8_t out_public_value[56], |
| 126 |
const uint8_t private_key[56]); |
| 127 |
|
| 128 |
/* Backend support */ |
| 129 |
typedef enum { |
| 130 |
KEY_OP_PUBLIC, |
| 131 |
KEY_OP_PRIVATE, |
| 132 |
KEY_OP_KEYGEN |
| 133 |
} ecx_key_op_t; |
| 134 |
|
| 135 |
ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg, |
| 136 |
const unsigned char *p, int plen, |
| 137 |
int pkey_id, ecx_key_op_t op, |
| 138 |
OSSL_LIB_CTX *libctx, const char *propq); |
| 139 |
|
| 140 |
int ossl_ecx_public_from_private(ECX_KEY *key); |
| 141 |
int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM *param_pub_key, |
| 142 |
const OSSL_PARAM *param_priv_key, |
| 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_ECX */ |
| 152 |
#endif |