2020-01-28 00:50:47 +08:00
|
|
|
/*
|
2021-02-18 22:57:13 +08:00
|
|
|
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2020-01-28 00:50:47 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Internal EC functions for other submodules: not for application use */
|
|
|
|
|
|
|
|
#ifndef OSSL_CRYPTO_ECX_H
|
|
|
|
# define OSSL_CRYPTO_ECX_H
|
2021-02-07 05:36:46 +08:00
|
|
|
# pragma once
|
|
|
|
|
2020-01-28 00:50:47 +08:00
|
|
|
# include <openssl/opensslconf.h>
|
|
|
|
|
|
|
|
# ifndef OPENSSL_NO_EC
|
|
|
|
|
2020-03-23 12:40:47 +08:00
|
|
|
# include <openssl/core.h>
|
2020-01-28 00:50:47 +08:00
|
|
|
# include <openssl/e_os2.h>
|
|
|
|
# include <openssl/crypto.h>
|
|
|
|
# include "internal/refcount.h"
|
2021-03-18 17:41:53 +08:00
|
|
|
# include "crypto/types.h"
|
2020-01-28 00:50:47 +08:00
|
|
|
|
2020-02-18 00:37:24 +08:00
|
|
|
# define X25519_KEYLEN 32
|
|
|
|
# define X448_KEYLEN 56
|
|
|
|
# define ED25519_KEYLEN 32
|
|
|
|
# define ED448_KEYLEN 57
|
2020-01-28 00:50:47 +08:00
|
|
|
|
|
|
|
# define MAX_KEYLEN ED448_KEYLEN
|
|
|
|
|
2020-02-18 00:37:24 +08:00
|
|
|
# define X25519_BITS 253
|
|
|
|
# define X25519_SECURITY_BITS 128
|
2020-01-28 00:50:47 +08:00
|
|
|
|
2020-02-18 00:37:24 +08:00
|
|
|
# define X448_BITS 448
|
|
|
|
# define X448_SECURITY_BITS 224
|
2020-01-28 00:50:47 +08:00
|
|
|
|
2020-02-18 00:37:24 +08:00
|
|
|
# define ED25519_BITS 256
|
|
|
|
/* RFC8032 Section 8.5 */
|
|
|
|
# define ED25519_SECURITY_BITS 128
|
|
|
|
# define ED25519_SIGSIZE 64
|
2020-01-28 00:50:47 +08:00
|
|
|
|
2020-02-18 00:37:24 +08:00
|
|
|
# define ED448_BITS 456
|
|
|
|
/* RFC8032 Section 8.5 */
|
|
|
|
# define ED448_SECURITY_BITS 224
|
|
|
|
# define ED448_SIGSIZE 114
|
2020-01-28 00:50:47 +08:00
|
|
|
|
2020-03-18 06:40:33 +08:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
ECX_KEY_TYPE_X25519,
|
|
|
|
ECX_KEY_TYPE_X448,
|
|
|
|
ECX_KEY_TYPE_ED25519,
|
|
|
|
ECX_KEY_TYPE_ED448
|
|
|
|
} ECX_KEY_TYPE;
|
|
|
|
|
|
|
|
#define KEYTYPE2NID(type) \
|
|
|
|
((type) == ECX_KEY_TYPE_X25519 \
|
|
|
|
? EVP_PKEY_X25519 \
|
|
|
|
: ((type) == ECX_KEY_TYPE_X448 \
|
|
|
|
? EVP_PKEY_X448 \
|
|
|
|
: ((type) == ECX_KEY_TYPE_ED25519 \
|
|
|
|
? EVP_PKEY_ED25519 \
|
|
|
|
: EVP_PKEY_ED448)))
|
|
|
|
|
2020-01-28 00:50:47 +08:00
|
|
|
struct ecx_key_st {
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx;
|
2020-09-22 13:51:49 +08:00
|
|
|
char *propq;
|
2020-01-28 00:50:47 +08:00
|
|
|
unsigned int haspubkey:1;
|
|
|
|
unsigned char pubkey[MAX_KEYLEN];
|
|
|
|
unsigned char *privkey;
|
|
|
|
size_t keylen;
|
2020-03-18 06:40:33 +08:00
|
|
|
ECX_KEY_TYPE type;
|
2020-01-28 00:50:47 +08:00
|
|
|
CRYPTO_REF_COUNT references;
|
|
|
|
CRYPTO_RWLOCK *lock;
|
|
|
|
};
|
|
|
|
|
2021-03-18 17:41:53 +08:00
|
|
|
size_t ossl_ecx_key_length(ECX_KEY_TYPE type);
|
2021-02-18 18:27:26 +08:00
|
|
|
ECX_KEY *ossl_ecx_key_new(OSSL_LIB_CTX *libctx, ECX_KEY_TYPE type,
|
|
|
|
int haspubkey, const char *propq);
|
|
|
|
void ossl_ecx_key_set0_libctx(ECX_KEY *key, OSSL_LIB_CTX *libctx);
|
|
|
|
unsigned char *ossl_ecx_key_allocate_privkey(ECX_KEY *key);
|
|
|
|
void ossl_ecx_key_free(ECX_KEY *key);
|
|
|
|
int ossl_ecx_key_up_ref(ECX_KEY *key);
|
2021-04-09 00:25:26 +08:00
|
|
|
ECX_KEY *ossl_ecx_key_dup(const ECX_KEY *key, int selection);
|
2020-01-28 00:50:47 +08:00
|
|
|
|
2021-03-09 09:04:21 +08:00
|
|
|
int ossl_x25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
|
|
|
|
const uint8_t peer_public_value[32]);
|
|
|
|
void ossl_x25519_public_from_private(uint8_t out_public_value[32],
|
|
|
|
const uint8_t private_key[32]);
|
|
|
|
|
|
|
|
int
|
|
|
|
ossl_ed25519_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[32],
|
|
|
|
const uint8_t private_key[32],
|
|
|
|
const char *propq);
|
|
|
|
int
|
|
|
|
ossl_ed25519_sign(uint8_t *out_sig, const uint8_t *message, size_t message_len,
|
|
|
|
const uint8_t public_key[32], const uint8_t private_key[32],
|
|
|
|
OSSL_LIB_CTX *libctx, const char *propq);
|
|
|
|
int
|
|
|
|
ossl_ed25519_verify(const uint8_t *message, size_t message_len,
|
|
|
|
const uint8_t signature[64], const uint8_t public_key[32],
|
|
|
|
OSSL_LIB_CTX *libctx, const char *propq);
|
|
|
|
|
|
|
|
int
|
|
|
|
ossl_ed448_public_from_private(OSSL_LIB_CTX *ctx, uint8_t out_public_key[57],
|
|
|
|
const uint8_t private_key[57], const char *propq);
|
|
|
|
int
|
|
|
|
ossl_ed448_sign(OSSL_LIB_CTX *ctx, uint8_t *out_sig, const uint8_t *message,
|
|
|
|
size_t message_len, const uint8_t public_key[57],
|
|
|
|
const uint8_t private_key[57], const uint8_t *context,
|
|
|
|
size_t context_len, const char *propq);
|
|
|
|
|
|
|
|
int
|
|
|
|
ossl_ed448_verify(OSSL_LIB_CTX *ctx, const uint8_t *message, size_t message_len,
|
|
|
|
const uint8_t signature[114], const uint8_t public_key[57],
|
|
|
|
const uint8_t *context, size_t context_len, const char *propq);
|
|
|
|
|
|
|
|
int
|
|
|
|
ossl_x448(uint8_t out_shared_key[56], const uint8_t private_key[56],
|
|
|
|
const uint8_t peer_public_value[56]);
|
|
|
|
void
|
|
|
|
ossl_x448_public_from_private(uint8_t out_public_value[56],
|
2020-01-28 00:50:47 +08:00
|
|
|
const uint8_t private_key[56]);
|
|
|
|
|
2020-09-25 10:12:22 +08:00
|
|
|
|
2020-03-23 12:40:47 +08:00
|
|
|
/* Backend support */
|
2021-03-18 17:41:53 +08:00
|
|
|
typedef enum {
|
|
|
|
KEY_OP_PUBLIC,
|
|
|
|
KEY_OP_PRIVATE,
|
|
|
|
KEY_OP_KEYGEN
|
|
|
|
} ecx_key_op_t;
|
|
|
|
|
|
|
|
ECX_KEY *ossl_ecx_key_op(const X509_ALGOR *palg,
|
|
|
|
const unsigned char *p, int plen,
|
|
|
|
int pkey_id, ecx_key_op_t op,
|
|
|
|
OSSL_LIB_CTX *libctx, const char *propq);
|
|
|
|
|
2021-02-18 18:27:26 +08:00
|
|
|
int ossl_ecx_public_from_private(ECX_KEY *key);
|
|
|
|
int ossl_ecx_key_fromdata(ECX_KEY *ecx, const OSSL_PARAM params[],
|
|
|
|
int include_private);
|
2021-03-18 17:41:53 +08:00
|
|
|
ECX_KEY *ossl_ecx_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
|
|
|
|
OSSL_LIB_CTX *libctx, const char *propq);
|
2021-02-18 18:27:26 +08:00
|
|
|
|
|
|
|
ECX_KEY *ossl_evp_pkey_get1_X25519(EVP_PKEY *pkey);
|
|
|
|
ECX_KEY *ossl_evp_pkey_get1_X448(EVP_PKEY *pkey);
|
|
|
|
ECX_KEY *ossl_evp_pkey_get1_ED25519(EVP_PKEY *pkey);
|
|
|
|
ECX_KEY *ossl_evp_pkey_get1_ED448(EVP_PKEY *pkey);
|
2020-01-28 00:50:47 +08:00
|
|
|
# endif /* OPENSSL_NO_EC */
|
|
|
|
#endif
|