Latest update.
This commit is contained in:
@@ -219,7 +219,8 @@ static int rsa_decrypt(void *vprsactx, unsigned char *out, size_t *outlen,
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_BAD_TLS_CLIENT_VERSION);
|
||||
return 0;
|
||||
}
|
||||
ret = rsa_padding_check_PKCS1_type_2_TLS(out, outsize,
|
||||
ret = rsa_padding_check_PKCS1_type_2_TLS(prsactx->libctx, out,
|
||||
outsize,
|
||||
tbuf, len,
|
||||
prsactx->client_version,
|
||||
prsactx->alt_version);
|
||||
|
||||
@@ -11,17 +11,17 @@ $NULL_GOAL=../../libimplementations.a
|
||||
$AES_GOAL=../../libimplementations.a
|
||||
$TDES_1_GOAL=../../libimplementations.a
|
||||
$TDES_2_GOAL=../../libimplementations.a
|
||||
$DES_GOAL=../../libimplementations.a
|
||||
$ARIA_GOAL=../../libimplementations.a
|
||||
$CAMELLIA_GOAL=../../libimplementations.a
|
||||
$BLOWFISH_GOAL=../../libimplementations.a
|
||||
$IDEA_GOAL=../../libimplementations.a
|
||||
$CAST5_GOAL=../../libimplementations.a
|
||||
$SEED_GOAL=../../libimplementations.a
|
||||
$DES_GOAL=../../liblegacy.a
|
||||
$BLOWFISH_GOAL=../../liblegacy.a
|
||||
$IDEA_GOAL=../../liblegacy.a
|
||||
$CAST5_GOAL=../../liblegacy.a
|
||||
$RC2_GOAL=../../liblegacy.a
|
||||
$RC4_GOAL=../../liblegacy.a
|
||||
$RC5_GOAL=../../liblegacy.a
|
||||
$SEED_GOAL=../../liblegacy.a
|
||||
$SM4_GOAL=../../libimplementations.a
|
||||
$RC4_GOAL=../../libimplementations.a
|
||||
$RC5_GOAL=../../libimplementations.a
|
||||
$RC2_GOAL=../../libimplementations.a
|
||||
$CHACHA_GOAL=../../libimplementations.a
|
||||
$CHACHAPOLY_GOAL=../../libimplementations.a
|
||||
$SIV_GOAL=../../libimplementations.a
|
||||
@@ -33,7 +33,7 @@ SOURCE[$COMMON_GOAL]=\
|
||||
ciphercommon_ccm.c ciphercommon_ccm_hw.c
|
||||
|
||||
IF[{- !$disabled{des} -}]
|
||||
SOURCE[$TDES_1_GOAL]=cipher_tdes.c cipher_tdes_hw.c
|
||||
SOURCE[$TDES_1_GOAL]=cipher_tdes.c cipher_tdes_common.c cipher_tdes_hw.c
|
||||
ENDIF
|
||||
|
||||
SOURCE[$NULL_GOAL]=\
|
||||
@@ -63,7 +63,7 @@ IF[{- !$disabled{des} -}]
|
||||
cipher_tdes_default.c cipher_tdes_default_hw.c \
|
||||
cipher_tdes_wrap.c cipher_tdes_wrap_hw.c
|
||||
SOURCE[$DES_GOAL]=\
|
||||
cipher_desx.c cipher_desx_hw.c \
|
||||
cipher_desx.c cipher_desx_hw.c cipher_tdes_common.c\
|
||||
cipher_des.c cipher_des_hw.c
|
||||
ENDIF
|
||||
|
||||
|
||||
@@ -150,9 +150,14 @@ static int aes_ocb_block_update_internal(PROV_AES_OCB_CTX *ctx,
|
||||
size_t outsize, const unsigned char *in,
|
||||
size_t inl, OSSL_ocb_cipher_fn ciph)
|
||||
{
|
||||
size_t nextblocks = fillblock(buf, bufsz, AES_BLOCK_SIZE, &in, &inl);
|
||||
size_t nextblocks;
|
||||
size_t outlint = 0;
|
||||
|
||||
if (bufsz != 0)
|
||||
nextblocks = fillblock(buf, bufsz, AES_BLOCK_SIZE, &in, &inl);
|
||||
else
|
||||
nextblocks = inl & ~(AES_BLOCK_SIZE-1);
|
||||
|
||||
if (*bufsz == AES_BLOCK_SIZE) {
|
||||
if (outsize < AES_BLOCK_SIZE) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
|
||||
@@ -179,7 +184,7 @@ static int aes_ocb_block_update_internal(PROV_AES_OCB_CTX *ctx,
|
||||
in += nextblocks;
|
||||
inl -= nextblocks;
|
||||
}
|
||||
if (!trailingdata(buf, bufsz, AES_BLOCK_SIZE, &in, &inl)) {
|
||||
if (inl != 0 && !trailingdata(buf, bufsz, AES_BLOCK_SIZE, &in, &inl)) {
|
||||
/* PROVerr already called */
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -40,6 +40,20 @@ static void *des_newctx(void *provctx, size_t kbits, size_t blkbits,
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void *des_dupctx(void *ctx)
|
||||
{
|
||||
PROV_DES_CTX *in = (PROV_DES_CTX *)ctx;
|
||||
PROV_DES_CTX *ret = OPENSSL_malloc(sizeof(*ret));
|
||||
|
||||
if (ret == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
in->base.hw->copyctx(&ret->base, &in->base);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void des_freectx(void *vctx)
|
||||
{
|
||||
PROV_DES_CTX *ctx = (PROV_DES_CTX *)vctx;
|
||||
@@ -137,6 +151,7 @@ const OSSL_DISPATCH des_##lcmode##_functions[] = { \
|
||||
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \
|
||||
{ OSSL_FUNC_CIPHER_NEWCTX, \
|
||||
(void (*)(void))des_##lcmode##_newctx }, \
|
||||
{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))des_dupctx }, \
|
||||
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))des_freectx }, \
|
||||
{ OSSL_FUNC_CIPHER_GET_PARAMS, \
|
||||
(void (*)(void))des_##lcmode##_get_params }, \
|
||||
|
||||
@@ -38,6 +38,16 @@ static int cipher_hw_des_initkey(PROV_CIPHER_CTX *ctx,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void cipher_hw_des_copyctx(PROV_CIPHER_CTX *dst,
|
||||
const PROV_CIPHER_CTX *src)
|
||||
{
|
||||
PROV_DES_CTX *sctx = (PROV_DES_CTX *)src;
|
||||
PROV_DES_CTX *dctx = (PROV_DES_CTX *)dst;
|
||||
|
||||
*dctx = *sctx;
|
||||
dst->ks = &dctx->dks.ks;
|
||||
}
|
||||
|
||||
static int cipher_hw_des_ecb_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t len)
|
||||
{
|
||||
@@ -164,7 +174,8 @@ static int cipher_hw_des_cfb8_cipher(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
#define PROV_CIPHER_HW_des_mode(mode) \
|
||||
static const PROV_CIPHER_HW des_##mode = { \
|
||||
cipher_hw_des_initkey, \
|
||||
cipher_hw_des_##mode##_cipher \
|
||||
cipher_hw_des_##mode##_cipher, \
|
||||
cipher_hw_des_copyctx \
|
||||
}; \
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_des_##mode(void) \
|
||||
{ \
|
||||
|
||||
@@ -37,6 +37,16 @@ static int cipher_hw_desx_cbc_initkey(PROV_CIPHER_CTX *ctx,
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void cipher_hw_desx_copyctx(PROV_CIPHER_CTX *dst,
|
||||
const PROV_CIPHER_CTX *src)
|
||||
{
|
||||
PROV_TDES_CTX *sctx = (PROV_TDES_CTX *)src;
|
||||
PROV_TDES_CTX *dctx = (PROV_TDES_CTX *)dst;
|
||||
|
||||
*dctx = *sctx;
|
||||
dst->ks = &dctx->tks.ks;
|
||||
}
|
||||
|
||||
static int cipher_hw_desx_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
@@ -60,7 +70,8 @@ static int cipher_hw_desx_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
static const PROV_CIPHER_HW desx_cbc =
|
||||
{
|
||||
cipher_hw_desx_cbc_initkey,
|
||||
cipher_hw_desx_cbc
|
||||
cipher_hw_desx_cbc,
|
||||
cipher_hw_desx_copyctx
|
||||
};
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_desx_cbc(void)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
@@ -19,101 +19,10 @@
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommonerr.h"
|
||||
|
||||
void *tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
|
||||
size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw)
|
||||
{
|
||||
PROV_TDES_CTX *tctx = OPENSSL_zalloc(sizeof(*tctx));
|
||||
|
||||
if (tctx != NULL)
|
||||
cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags, hw,
|
||||
provctx);
|
||||
return tctx;
|
||||
}
|
||||
|
||||
void tdes_freectx(void *vctx)
|
||||
{
|
||||
PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
|
||||
|
||||
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
static int tdes_init(void *vctx, const unsigned char *key, size_t keylen,
|
||||
const unsigned char *iv, size_t ivlen, int enc)
|
||||
{
|
||||
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
|
||||
|
||||
ctx->enc = enc;
|
||||
|
||||
if (iv != NULL) {
|
||||
if (!cipher_generic_initiv(ctx, iv, ivlen))
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (key != NULL) {
|
||||
if (keylen != ctx->keylen) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN);
|
||||
return 0;
|
||||
}
|
||||
return ctx->hw->init(ctx, key, ctx->keylen);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
|
||||
const unsigned char *iv, size_t ivlen)
|
||||
{
|
||||
return tdes_init(vctx, key, keylen, iv, ivlen, 1);
|
||||
}
|
||||
|
||||
int tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
|
||||
const unsigned char *iv, size_t ivlen)
|
||||
{
|
||||
return tdes_init(vctx, key, keylen, iv, ivlen, 0);
|
||||
}
|
||||
|
||||
static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
|
||||
{
|
||||
|
||||
DES_cblock *deskey = ptr;
|
||||
size_t kl = ctx->keylen;
|
||||
|
||||
if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0)
|
||||
return 0;
|
||||
DES_set_odd_parity(deskey);
|
||||
if (kl >= 16)
|
||||
DES_set_odd_parity(deskey + 1);
|
||||
if (kl >= 24) {
|
||||
DES_set_odd_parity(deskey + 2);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(tdes)
|
||||
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
|
||||
CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(tdes)
|
||||
|
||||
int tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
|
||||
{
|
||||
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if (!cipher_generic_get_ctx_params(vctx, params))
|
||||
return 0;
|
||||
|
||||
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
|
||||
if (p != NULL && !tdes_generatekey(ctx, p->data)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* TODO(3.0) - ECB mode does not use an IV - but existing test code is setting
|
||||
* an IV. Fixing this could potentially make applications break.
|
||||
*/
|
||||
|
||||
/* tdes_ede3_ecb_functions */
|
||||
IMPLEMENT_tdes_cipher(ede3, EDE3, ecb, ECB, TDES_FLAGS, 64*3, 64, 64, block);
|
||||
/* tdes_ede3_cbc_functions */
|
||||
|
||||
@@ -30,7 +30,7 @@ typedef struct prov_tdes_ctx_st {
|
||||
|
||||
} PROV_TDES_CTX;
|
||||
|
||||
#define IMPLEMENT_tdes_cipher(type, UCTYPE, lcmode, UCMODE, flags, \
|
||||
#define IMPLEMENT_tdes_cipher(type, UCTYPE, lcmode, UCMODE, flags, \
|
||||
kbits, blkbits, ivbits, block) \
|
||||
static OSSL_OP_cipher_newctx_fn tdes_##type##_##lcmode##_newctx; \
|
||||
static void *tdes_##type##_##lcmode##_newctx(void *provctx) \
|
||||
@@ -53,6 +53,7 @@ const OSSL_DISPATCH tdes_##type##_##lcmode##_functions[] = { \
|
||||
{ OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))cipher_generic_cipher }, \
|
||||
{ OSSL_FUNC_CIPHER_NEWCTX, \
|
||||
(void (*)(void))tdes_##type##_##lcmode##_newctx }, \
|
||||
{ OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))tdes_dupctx }, \
|
||||
{ OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))tdes_freectx }, \
|
||||
{ OSSL_FUNC_CIPHER_GET_PARAMS, \
|
||||
(void (*)(void))tdes_##type##_##lcmode##_get_params }, \
|
||||
@@ -70,16 +71,18 @@ const OSSL_DISPATCH tdes_##type##_##lcmode##_functions[] = { \
|
||||
|
||||
void *tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
|
||||
size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw);
|
||||
OSSL_OP_cipher_dupctx_fn tdes_dupctx;
|
||||
OSSL_OP_cipher_freectx_fn tdes_freectx;
|
||||
OSSL_OP_cipher_encrypt_init_fn tdes_einit;
|
||||
OSSL_OP_cipher_decrypt_init_fn tdes_dinit;
|
||||
OSSL_OP_cipher_get_ctx_params_fn tdes_get_ctx_params;
|
||||
OSSL_OP_cipher_gettable_ctx_params_fn tdes_gettable_ctx_params;
|
||||
|
||||
#define PROV_CIPHER_HW_tdes_mode(type, mode) \
|
||||
#define PROV_CIPHER_HW_tdes_mode(type, mode) \
|
||||
static const PROV_CIPHER_HW type##_##mode = { \
|
||||
cipher_hw_tdes_##type##_initkey, \
|
||||
cipher_hw_tdes_##mode \
|
||||
cipher_hw_tdes_##mode, \
|
||||
cipher_hw_tdes_copyctx \
|
||||
}; \
|
||||
const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_##type##_##mode(void) \
|
||||
{ \
|
||||
@@ -88,6 +91,7 @@ const PROV_CIPHER_HW *PROV_CIPHER_HW_tdes_##type##_##mode(void) \
|
||||
|
||||
int cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
size_t keylen);
|
||||
void cipher_hw_tdes_copyctx(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src);
|
||||
int cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl);
|
||||
int cipher_hw_tdes_ecb(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/*
|
||||
* DES low level APIs are deprecated for public use, but still ok for internal
|
||||
* use.
|
||||
*/
|
||||
#include "internal/deprecated.h"
|
||||
|
||||
#include "prov/ciphercommon.h"
|
||||
#include "cipher_tdes.h"
|
||||
#include <openssl/rand.h>
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommonerr.h"
|
||||
|
||||
void *tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits,
|
||||
size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw)
|
||||
{
|
||||
PROV_TDES_CTX *tctx = OPENSSL_zalloc(sizeof(*tctx));
|
||||
|
||||
if (tctx != NULL)
|
||||
cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags, hw,
|
||||
provctx);
|
||||
return tctx;
|
||||
}
|
||||
|
||||
void *tdes_dupctx(void *ctx)
|
||||
{
|
||||
PROV_TDES_CTX *in = (PROV_TDES_CTX *)ctx;
|
||||
PROV_TDES_CTX *ret = OPENSSL_malloc(sizeof(*ret));
|
||||
|
||||
if (ret == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
in->base.hw->copyctx(&ret->base, &in->base);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void tdes_freectx(void *vctx)
|
||||
{
|
||||
PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx;
|
||||
|
||||
OPENSSL_clear_free(ctx, sizeof(*ctx));
|
||||
}
|
||||
|
||||
static int tdes_init(void *vctx, const unsigned char *key, size_t keylen,
|
||||
const unsigned char *iv, size_t ivlen, int enc)
|
||||
{
|
||||
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
|
||||
|
||||
ctx->enc = enc;
|
||||
|
||||
if (iv != NULL) {
|
||||
if (!cipher_generic_initiv(ctx, iv, ivlen))
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (key != NULL) {
|
||||
if (keylen != ctx->keylen) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN);
|
||||
return 0;
|
||||
}
|
||||
return ctx->hw->init(ctx, key, ctx->keylen);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int tdes_einit(void *vctx, const unsigned char *key, size_t keylen,
|
||||
const unsigned char *iv, size_t ivlen)
|
||||
{
|
||||
return tdes_init(vctx, key, keylen, iv, ivlen, 1);
|
||||
}
|
||||
|
||||
int tdes_dinit(void *vctx, const unsigned char *key, size_t keylen,
|
||||
const unsigned char *iv, size_t ivlen)
|
||||
{
|
||||
return tdes_init(vctx, key, keylen, iv, ivlen, 0);
|
||||
}
|
||||
|
||||
CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(tdes)
|
||||
OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0),
|
||||
CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(tdes)
|
||||
|
||||
static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr)
|
||||
{
|
||||
|
||||
DES_cblock *deskey = ptr;
|
||||
size_t kl = ctx->keylen;
|
||||
|
||||
if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0)
|
||||
return 0;
|
||||
DES_set_odd_parity(deskey);
|
||||
if (kl >= 16)
|
||||
DES_set_odd_parity(deskey + 1);
|
||||
if (kl >= 24) {
|
||||
DES_set_odd_parity(deskey + 2);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int tdes_get_ctx_params(void *vctx, OSSL_PARAM params[])
|
||||
{
|
||||
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if (!cipher_generic_get_ctx_params(vctx, params))
|
||||
return 0;
|
||||
|
||||
p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY);
|
||||
if (p != NULL && !tdes_generatekey(ctx, p->data)) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
@@ -45,6 +45,15 @@ int cipher_hw_tdes_ede3_initkey(PROV_CIPHER_CTX *ctx, const unsigned char *key,
|
||||
return 1;
|
||||
}
|
||||
|
||||
void cipher_hw_tdes_copyctx(PROV_CIPHER_CTX *dst, const PROV_CIPHER_CTX *src)
|
||||
{
|
||||
PROV_TDES_CTX *sctx = (PROV_TDES_CTX *)src;
|
||||
PROV_TDES_CTX *dctx = (PROV_TDES_CTX *)dst;
|
||||
|
||||
*dctx = *sctx;
|
||||
dst->ks = &dctx->tks.ks;
|
||||
}
|
||||
|
||||
int cipher_hw_tdes_cbc(PROV_CIPHER_CTX *ctx, unsigned char *out,
|
||||
const unsigned char *in, size_t inl)
|
||||
{
|
||||
|
||||
@@ -176,7 +176,12 @@ int cipher_generic_block_update(void *vctx, unsigned char *out, size_t *outl,
|
||||
size_t outlint = 0;
|
||||
PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
|
||||
size_t blksz = ctx->blocksize;
|
||||
size_t nextblocks = fillblock(ctx->buf, &ctx->bufsz, blksz, &in, &inl);
|
||||
size_t nextblocks;
|
||||
|
||||
if (ctx->bufsz != 0)
|
||||
nextblocks = fillblock(ctx->buf, &ctx->bufsz, blksz, &in, &inl);
|
||||
else
|
||||
nextblocks = inl & ~(blksz-1);
|
||||
|
||||
/*
|
||||
* If we're decrypting and we end an update on a block boundary we hold
|
||||
@@ -218,7 +223,7 @@ int cipher_generic_block_update(void *vctx, unsigned char *out, size_t *outl,
|
||||
in += nextblocks;
|
||||
inl -= nextblocks;
|
||||
}
|
||||
if (!trailingdata(ctx->buf, &ctx->bufsz, blksz, &in, &inl)) {
|
||||
if (inl != 0 && !trailingdata(ctx->buf, &ctx->bufsz, blksz, &in, &inl)) {
|
||||
/* ERR_raise already called */
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -35,20 +35,17 @@ size_t fillblock(unsigned char *buf, size_t *buflen, size_t blocksize,
|
||||
const unsigned char **in, size_t *inlen)
|
||||
{
|
||||
size_t blockmask = ~(blocksize - 1);
|
||||
size_t bufremain = blocksize - *buflen;
|
||||
|
||||
assert(*buflen <= blocksize);
|
||||
assert(blocksize > 0 && (blocksize & (blocksize - 1)) == 0);
|
||||
|
||||
if (*buflen != blocksize && (*buflen != 0 || *inlen < blocksize)) {
|
||||
size_t bufremain = blocksize - *buflen;
|
||||
|
||||
if (*inlen < bufremain)
|
||||
bufremain = *inlen;
|
||||
memcpy(buf + *buflen, *in, bufremain);
|
||||
*in += bufremain;
|
||||
*inlen -= bufremain;
|
||||
*buflen += bufremain;
|
||||
}
|
||||
if (*inlen < bufremain)
|
||||
bufremain = *inlen;
|
||||
memcpy(buf + *buflen, *in, bufremain);
|
||||
*in += bufremain;
|
||||
*inlen -= bufremain;
|
||||
*buflen += bufremain;
|
||||
|
||||
return *inlen & blockmask;
|
||||
}
|
||||
|
||||
@@ -307,7 +307,7 @@ static int ccm_tls_cipher(PROV_CCM_CTX *ctx,
|
||||
size_t olen = 0;
|
||||
|
||||
/* Encrypt/decrypt must be performed in place */
|
||||
if (out != in || len < (EVP_CCM_TLS_EXPLICIT_IV_LEN + (size_t)ctx->m))
|
||||
if (in == NULL || out != in || len < EVP_CCM_TLS_EXPLICIT_IV_LEN + ctx->m)
|
||||
goto err;
|
||||
|
||||
/* If encrypting set explicit IV from sequence number (start of AAD) */
|
||||
|
||||
@@ -247,7 +247,7 @@ static void *keccak_dupctx(void *ctx)
|
||||
}
|
||||
|
||||
static const OSSL_PARAM known_shake_settable_ctx_params[] = {
|
||||
{OSSL_DIGEST_PARAM_SSL3_MS, OSSL_PARAM_OCTET_STRING, NULL, 0, 0},
|
||||
{OSSL_DIGEST_PARAM_XOFLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL, 0, 0},
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
static const OSSL_PARAM *shake_settable_ctx_params(void)
|
||||
|
||||
@@ -22,5 +22,6 @@ ENDIF
|
||||
IF[{- !$disabled{ec} -}]
|
||||
SOURCE[$ECX_GOAL]=ecx_exch.c
|
||||
DEFINE[$ECX_GOAL]=$ECDEF
|
||||
SOURCE[$ECDH_GOAL]=ecdh_exch.c
|
||||
SOURCE[../../libfips.a]=ecdh_exch.c
|
||||
SOURCE[../../libnonfips.a]=ecdh_exch.c
|
||||
ENDIF
|
||||
@@ -458,6 +458,7 @@ int ecdh_plain_derive(void *vpecdhctx, unsigned char *secret,
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef FIPS_MODE
|
||||
static ossl_inline
|
||||
int ecdh_X9_63_kdf_derive(void *vpecdhctx, unsigned char *secret,
|
||||
size_t *psecretlen, size_t outlen)
|
||||
@@ -497,6 +498,7 @@ int ecdh_X9_63_kdf_derive(void *vpecdhctx, unsigned char *secret,
|
||||
OPENSSL_secure_clear_free(stmp, stmplen);
|
||||
return ret;
|
||||
}
|
||||
#endif /* FIPS_MODE */
|
||||
|
||||
static
|
||||
int ecdh_derive(void *vpecdhctx, unsigned char *secret,
|
||||
@@ -507,8 +509,13 @@ int ecdh_derive(void *vpecdhctx, unsigned char *secret,
|
||||
switch (pecdhctx->kdf_type) {
|
||||
case PROV_ECDH_KDF_NONE:
|
||||
return ecdh_plain_derive(vpecdhctx, secret, psecretlen, outlen);
|
||||
#ifndef FIPS_MODE
|
||||
case PROV_ECDH_KDF_X9_63:
|
||||
return ecdh_X9_63_kdf_derive(vpecdhctx, secret, psecretlen, outlen);
|
||||
|
||||
#endif /* FIPS_MODE */
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -259,6 +259,8 @@ extern const OSSL_DISPATCH dsa_keymgmt_functions[];
|
||||
extern const OSSL_DISPATCH rsa_keymgmt_functions[];
|
||||
extern const OSSL_DISPATCH x25519_keymgmt_functions[];
|
||||
extern const OSSL_DISPATCH x448_keymgmt_functions[];
|
||||
extern const OSSL_DISPATCH ed25519_keymgmt_functions[];
|
||||
extern const OSSL_DISPATCH ed448_keymgmt_functions[];
|
||||
extern const OSSL_DISPATCH ec_keymgmt_functions[];
|
||||
|
||||
/* Key Exchange */
|
||||
@@ -270,6 +272,10 @@ extern const OSSL_DISPATCH ecdh_keyexch_functions[];
|
||||
/* Signature */
|
||||
extern const OSSL_DISPATCH dsa_signature_functions[];
|
||||
extern const OSSL_DISPATCH rsa_signature_functions[];
|
||||
extern const OSSL_DISPATCH ed25519_signature_functions[];
|
||||
extern const OSSL_DISPATCH ed448_signature_functions[];
|
||||
extern const OSSL_DISPATCH ecdsa_signature_functions[];
|
||||
|
||||
|
||||
/* Asym Cipher */
|
||||
extern const OSSL_DISPATCH rsa_asym_cipher_functions[];
|
||||
@@ -316,6 +322,20 @@ extern const OSSL_DISPATCH x448_pub_der_serializer_functions[];
|
||||
extern const OSSL_DISPATCH x448_priv_pem_serializer_functions[];
|
||||
extern const OSSL_DISPATCH x448_pub_pem_serializer_functions[];
|
||||
|
||||
extern const OSSL_DISPATCH ed25519_priv_print_serializer_functions[];
|
||||
extern const OSSL_DISPATCH ed25519_pub_print_serializer_functions[];
|
||||
extern const OSSL_DISPATCH ed25519_priv_der_serializer_functions[];
|
||||
extern const OSSL_DISPATCH ed25519_pub_der_serializer_functions[];
|
||||
extern const OSSL_DISPATCH ed25519_priv_pem_serializer_functions[];
|
||||
extern const OSSL_DISPATCH ed25519_pub_pem_serializer_functions[];
|
||||
|
||||
extern const OSSL_DISPATCH ed448_priv_print_serializer_functions[];
|
||||
extern const OSSL_DISPATCH ed448_pub_print_serializer_functions[];
|
||||
extern const OSSL_DISPATCH ed448_priv_der_serializer_functions[];
|
||||
extern const OSSL_DISPATCH ed448_pub_der_serializer_functions[];
|
||||
extern const OSSL_DISPATCH ed448_priv_pem_serializer_functions[];
|
||||
extern const OSSL_DISPATCH ed448_pub_pem_serializer_functions[];
|
||||
|
||||
extern const OSSL_DISPATCH ec_priv_text_serializer_functions[];
|
||||
extern const OSSL_DISPATCH ec_pub_text_serializer_functions[];
|
||||
extern const OSSL_DISPATCH ec_param_text_serializer_functions[];
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
$DH_GOAL=../../libimplementations.a
|
||||
$DSA_GOAL=../../libimplementations.a
|
||||
$EC_GOAL=../../libimplementations.a
|
||||
$RSA_GOAL=../../libimplementations.a
|
||||
$ECX_GOAL=../../libimplementations.a
|
||||
|
||||
IF[{- !$disabled{dh} -}]
|
||||
@@ -16,7 +15,9 @@ ENDIF
|
||||
IF[{- !$disabled{ec} -}]
|
||||
SOURCE[$EC_GOAL]=ec_kmgmt.c
|
||||
ENDIF
|
||||
SOURCE[$RSA_GOAL]=rsa_kmgmt.c
|
||||
IF[{- !$disabled{ec} -}]
|
||||
SOURCE[$ECX_GOAL]=ecx_kmgmt.c
|
||||
ENDIF
|
||||
|
||||
SOURCE[../../libfips.a]=rsa_kmgmt.c
|
||||
SOURCE[../../libnonfips.a]=rsa_kmgmt.c
|
||||
@@ -22,7 +22,7 @@
|
||||
#include "prov/providercommon.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "crypto/dh.h"
|
||||
#include "internal/param_build.h"
|
||||
#include "openssl/param_build.h"
|
||||
|
||||
static OSSL_OP_keymgmt_new_fn dh_newdata;
|
||||
static OSSL_OP_keymgmt_free_fn dh_freedata;
|
||||
@@ -39,32 +39,6 @@ static OSSL_OP_keymgmt_export_types_fn dh_export_types;
|
||||
#define DH_POSSIBLE_SELECTIONS \
|
||||
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
|
||||
|
||||
static int params_to_domparams(DH *dh, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *param_p, *param_g;
|
||||
BIGNUM *p = NULL, *g = NULL;
|
||||
|
||||
if (dh == NULL)
|
||||
return 0;
|
||||
|
||||
param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
|
||||
param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
|
||||
|
||||
if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
|
||||
|| (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
|
||||
goto err;
|
||||
|
||||
if (!DH_set0_pqg(dh, p, NULL, g))
|
||||
goto err;
|
||||
|
||||
return 1;
|
||||
|
||||
err:
|
||||
BN_free(p);
|
||||
BN_free(g);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int domparams_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
|
||||
{
|
||||
const BIGNUM *dh_p = NULL, *dh_g = NULL;
|
||||
@@ -74,56 +48,15 @@ static int domparams_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
|
||||
|
||||
DH_get0_pqg(dh, &dh_p, NULL, &dh_g);
|
||||
if (dh_p != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dh_p))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dh_p))
|
||||
return 0;
|
||||
if (dh_g != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dh_g))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dh_g))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int params_to_key(DH *dh, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *param_priv_key, *param_pub_key;
|
||||
BIGNUM *priv_key = NULL, *pub_key = NULL;
|
||||
|
||||
if (dh == NULL)
|
||||
return 0;
|
||||
|
||||
if (!params_to_domparams(dh, params))
|
||||
return 0;
|
||||
|
||||
param_priv_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
|
||||
param_pub_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
|
||||
|
||||
/*
|
||||
* DH documentation says that a public key must be present if a
|
||||
* private key is present.
|
||||
* We want to have at least a public key either way, so we end up
|
||||
* requiring it unconditionally.
|
||||
*/
|
||||
if (param_pub_key == NULL)
|
||||
return 0;
|
||||
|
||||
if ((param_priv_key != NULL
|
||||
&& !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
|
||||
|| !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
|
||||
goto err;
|
||||
|
||||
if (!DH_set0_key(dh, pub_key, priv_key))
|
||||
goto err;
|
||||
|
||||
return 1;
|
||||
|
||||
err:
|
||||
BN_clear_free(priv_key);
|
||||
BN_free(pub_key);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int key_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
|
||||
{
|
||||
const BIGNUM *priv_key = NULL, *pub_key = NULL;
|
||||
@@ -135,10 +68,10 @@ static int key_to_params(DH *dh, OSSL_PARAM_BLD *tmpl)
|
||||
|
||||
DH_get0_key(dh, &pub_key, &priv_key);
|
||||
if (priv_key != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
|
||||
return 0;
|
||||
if (pub_key != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -159,15 +92,17 @@ static int dh_has(void *keydata, int selection)
|
||||
DH *dh = keydata;
|
||||
int ok = 0;
|
||||
|
||||
if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
if (dh != NULL) {
|
||||
if ((selection & DH_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && (DH_get0_pub_key(dh) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && (DH_get0_priv_key(dh) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && (DH_get0_p(dh) != NULL && DH_get0_g(dh) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && (DH_get0_pub_key(dh) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && (DH_get0_priv_key(dh) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && (DH_get0_p(dh) != NULL && DH_get0_g(dh) != NULL);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -202,9 +137,9 @@ static int dh_import(void *keydata, int selection, const OSSL_PARAM params[])
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
|
||||
ok = ok && params_to_domparams(dh, params);
|
||||
ok = ok && ffc_fromdata(dh_get0_params(dh), params);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && params_to_key(dh, params);
|
||||
ok = ok && dh_key_fromdata(dh, params);
|
||||
|
||||
return ok;
|
||||
}
|
||||
@@ -213,26 +148,31 @@ static int dh_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
|
||||
void *cbarg)
|
||||
{
|
||||
DH *dh = keydata;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM_BLD *tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ok = 1;
|
||||
|
||||
if (dh == NULL)
|
||||
return 0;
|
||||
|
||||
ossl_param_bld_init(&tmpl);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
|
||||
ok = ok && domparams_to_params(dh, &tmpl);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && key_to_params(dh, &tmpl);
|
||||
|
||||
if (!ok
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
tmpl = OSSL_PARAM_BLD_new();
|
||||
if (tmpl == NULL)
|
||||
return 0;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
|
||||
ok = ok && domparams_to_params(dh, tmpl);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && key_to_params(dh, tmpl);
|
||||
|
||||
if (!ok
|
||||
|| (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) {
|
||||
OSSL_PARAM_BLD_free(tmpl);
|
||||
return 0;
|
||||
}
|
||||
OSSL_PARAM_BLD_free(tmpl);
|
||||
|
||||
ok = param_cb(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
OSSL_PARAM_BLD_free_params(params);
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
#include "prov/providercommon.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "crypto/dsa.h"
|
||||
#include "internal/param_build.h"
|
||||
#include "openssl/param_build.h"
|
||||
|
||||
static OSSL_OP_keymgmt_new_fn dsa_newdata;
|
||||
static OSSL_OP_keymgmt_free_fn dsa_freedata;
|
||||
@@ -39,35 +39,6 @@ static OSSL_OP_keymgmt_export_types_fn dsa_export_types;
|
||||
#define DSA_POSSIBLE_SELECTIONS \
|
||||
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)
|
||||
|
||||
static int params_to_domparams(DSA *dsa, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *param_p, *param_q, *param_g;
|
||||
BIGNUM *p = NULL, *q = NULL, *g = NULL;
|
||||
|
||||
if (dsa == NULL)
|
||||
return 0;
|
||||
|
||||
param_p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_P);
|
||||
param_q = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_Q);
|
||||
param_g = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_FFC_G);
|
||||
|
||||
if ((param_p != NULL && !OSSL_PARAM_get_BN(param_p, &p))
|
||||
|| (param_q != NULL && !OSSL_PARAM_get_BN(param_q, &q))
|
||||
|| (param_g != NULL && !OSSL_PARAM_get_BN(param_g, &g)))
|
||||
goto err;
|
||||
|
||||
if (!DSA_set0_pqg(dsa, p, q, g))
|
||||
goto err;
|
||||
|
||||
return 1;
|
||||
|
||||
err:
|
||||
BN_free(p);
|
||||
BN_free(q);
|
||||
BN_free(g);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int domparams_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
|
||||
{
|
||||
const BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
|
||||
@@ -77,58 +48,18 @@ static int domparams_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
|
||||
|
||||
DSA_get0_pqg(dsa, &dsa_p, &dsa_q, &dsa_g);
|
||||
if (dsa_p != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dsa_p))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, dsa_p))
|
||||
return 0;
|
||||
if (dsa_q != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, dsa_q))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, dsa_q))
|
||||
return 0;
|
||||
if (dsa_g != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dsa_g))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, dsa_g))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int params_to_key(DSA *dsa, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *param_priv_key, *param_pub_key;
|
||||
BIGNUM *priv_key = NULL, *pub_key = NULL;
|
||||
|
||||
if (dsa == NULL)
|
||||
return 0;
|
||||
|
||||
if (!params_to_domparams(dsa, params))
|
||||
return 0;
|
||||
|
||||
param_priv_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
|
||||
param_pub_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
|
||||
|
||||
/*
|
||||
* DSA documentation says that a public key must be present if a private key
|
||||
* is.
|
||||
*/
|
||||
if (param_priv_key != NULL && param_pub_key == NULL)
|
||||
return 0;
|
||||
|
||||
if ((param_priv_key != NULL
|
||||
&& !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
|
||||
|| (param_pub_key != NULL
|
||||
&& !OSSL_PARAM_get_BN(param_pub_key, &pub_key)))
|
||||
goto err;
|
||||
|
||||
if (pub_key != NULL && !DSA_set0_key(dsa, pub_key, priv_key))
|
||||
goto err;
|
||||
|
||||
return 1;
|
||||
|
||||
err:
|
||||
BN_clear_free(priv_key);
|
||||
BN_free(pub_key);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int key_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
|
||||
{
|
||||
const BIGNUM *priv_key = NULL, *pub_key = NULL;
|
||||
@@ -140,10 +71,10 @@ static int key_to_params(DSA *dsa, OSSL_PARAM_BLD *tmpl)
|
||||
|
||||
DSA_get0_key(dsa, &pub_key, &priv_key);
|
||||
if (priv_key != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, priv_key))
|
||||
return 0;
|
||||
if (pub_key != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
|
||||
&& !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, pub_key))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -164,15 +95,17 @@ static int dsa_has(void *keydata, int selection)
|
||||
DSA *dsa = keydata;
|
||||
int ok = 0;
|
||||
|
||||
if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
if (dsa != NULL) {
|
||||
if ((selection & DSA_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && (DSA_get0_pub_key(dsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && (DSA_get0_priv_key(dsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && (DSA_get0_p(dsa) != NULL && DSA_get0_g(dsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && (DSA_get0_pub_key(dsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && (DSA_get0_priv_key(dsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && (DSA_get0_p(dsa) != NULL && DSA_get0_g(dsa) != NULL);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -209,9 +142,9 @@ static int dsa_import(void *keydata, int selection, const OSSL_PARAM params[])
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
|
||||
ok = ok && params_to_domparams(dsa, params);
|
||||
ok = ok && ffc_fromdata(dsa_get0_params(dsa), params);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && params_to_key(dsa, params);
|
||||
ok = ok && dsa_key_fromdata(dsa, params);
|
||||
|
||||
return ok;
|
||||
}
|
||||
@@ -220,26 +153,26 @@ static int dsa_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
|
||||
void *cbarg)
|
||||
{
|
||||
DSA *dsa = keydata;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM_BLD *tmpl = OSSL_PARAM_BLD_new();
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ok = 1;
|
||||
|
||||
if (dsa == NULL)
|
||||
return 0;
|
||||
|
||||
ossl_param_bld_init(&tmpl);
|
||||
goto err;;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
|
||||
ok = ok && domparams_to_params(dsa, &tmpl);
|
||||
ok = ok && domparams_to_params(dsa, tmpl);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && key_to_params(dsa, &tmpl);
|
||||
ok = ok && key_to_params(dsa, tmpl);
|
||||
|
||||
if (!ok
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
return 0;
|
||||
|| (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
|
||||
goto err;;
|
||||
|
||||
ok = param_cb(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
OSSL_PARAM_BLD_free_params(params);
|
||||
err:
|
||||
OSSL_PARAM_BLD_free(tmpl);
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,15 +16,25 @@
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/bn.h>
|
||||
#include <openssl/ec.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/objects.h>
|
||||
#include <openssl/params.h>
|
||||
#include "crypto/bn.h"
|
||||
#include "internal/param_build.h"
|
||||
#include "crypto/ec.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommon.h"
|
||||
#include "prov/providercommonerr.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "internal/param_build_set.h"
|
||||
|
||||
static OSSL_OP_keymgmt_new_fn ec_newdata;
|
||||
static OSSL_OP_keymgmt_gen_init_fn ec_gen_init;
|
||||
static OSSL_OP_keymgmt_gen_set_template_fn ec_gen_set_template;
|
||||
static OSSL_OP_keymgmt_gen_set_params_fn ec_gen_set_params;
|
||||
static OSSL_OP_keymgmt_gen_settable_params_fn ec_gen_settable_params;
|
||||
static OSSL_OP_keymgmt_gen_get_params_fn ec_gen_get_params;
|
||||
static OSSL_OP_keymgmt_gen_gettable_params_fn ec_gen_gettable_params;
|
||||
static OSSL_OP_keymgmt_gen_fn ec_gen;
|
||||
static OSSL_OP_keymgmt_gen_cleanup_fn ec_gen_cleanup;
|
||||
static OSSL_OP_keymgmt_free_fn ec_freedata;
|
||||
static OSSL_OP_keymgmt_get_params_fn ec_get_params;
|
||||
static OSSL_OP_keymgmt_gettable_params_fn ec_gettable_params;
|
||||
@@ -32,14 +42,15 @@ static OSSL_OP_keymgmt_set_params_fn ec_set_params;
|
||||
static OSSL_OP_keymgmt_settable_params_fn ec_settable_params;
|
||||
static OSSL_OP_keymgmt_has_fn ec_has;
|
||||
static OSSL_OP_keymgmt_match_fn ec_match;
|
||||
static OSSL_OP_keymgmt_validate_fn ec_validate;
|
||||
static OSSL_OP_keymgmt_import_fn ec_import;
|
||||
static OSSL_OP_keymgmt_import_types_fn ec_import_types;
|
||||
static OSSL_OP_keymgmt_export_fn ec_export;
|
||||
static OSSL_OP_keymgmt_export_types_fn ec_export_types;
|
||||
static OSSL_OP_keymgmt_query_operation_name_fn ec_query_operation_name;
|
||||
|
||||
#define EC_POSSIBLE_SELECTIONS \
|
||||
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS )
|
||||
#define EC_POSSIBLE_SELECTIONS \
|
||||
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS)
|
||||
|
||||
static
|
||||
const char *ec_query_operation_name(int operation_id)
|
||||
@@ -47,64 +58,15 @@ const char *ec_query_operation_name(int operation_id)
|
||||
switch (operation_id) {
|
||||
case OSSL_OP_KEYEXCH:
|
||||
return "ECDH";
|
||||
#if 0
|
||||
case OSSL_OP_SIGNATURE:
|
||||
return deflt_signature;
|
||||
#endif
|
||||
return "ECDSA";
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static ossl_inline
|
||||
int params_to_domparams(EC_KEY *ec, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *param_ec_name;
|
||||
EC_GROUP *ecg = NULL;
|
||||
char *curve_name = NULL;
|
||||
int ok = 0;
|
||||
|
||||
if (ec == NULL)
|
||||
return 0;
|
||||
|
||||
param_ec_name = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME);
|
||||
if (param_ec_name == NULL) {
|
||||
/* explicit parameters */
|
||||
|
||||
/*
|
||||
* TODO(3.0): should we support explicit parameters curves?
|
||||
*/
|
||||
return 0;
|
||||
} else {
|
||||
/* named curve */
|
||||
int curve_nid;
|
||||
|
||||
if (!OSSL_PARAM_get_utf8_string(param_ec_name, &curve_name, 0)
|
||||
|| curve_name == NULL
|
||||
|| (curve_nid = OBJ_sn2nid(curve_name)) == NID_undef)
|
||||
goto err;
|
||||
|
||||
if ((ecg = EC_GROUP_new_by_curve_name(curve_nid)) == NULL)
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (!EC_KEY_set_group(ec, ecg))
|
||||
goto err;
|
||||
|
||||
/*
|
||||
* TODO(3.0): if the group has changed, should we invalidate the private and
|
||||
* public key?
|
||||
*/
|
||||
|
||||
ok = 1;
|
||||
|
||||
err:
|
||||
OPENSSL_free(curve_name);
|
||||
EC_GROUP_free(ecg);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static ossl_inline
|
||||
int domparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
|
||||
int domparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl,
|
||||
OSSL_PARAM params[])
|
||||
{
|
||||
const EC_GROUP *ecg;
|
||||
int curve_nid;
|
||||
@@ -119,135 +81,24 @@ int domparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
|
||||
curve_nid = EC_GROUP_get_curve_name(ecg);
|
||||
|
||||
if (curve_nid == NID_undef) {
|
||||
/* explicit parameters */
|
||||
|
||||
/*
|
||||
* TODO(3.0): should we support explicit parameters curves?
|
||||
*/
|
||||
/* TODO(3.0): should we support explicit parameters curves? */
|
||||
return 0;
|
||||
} else {
|
||||
/* named curve */
|
||||
const char *curve_name = NULL;
|
||||
|
||||
if ((curve_name = OBJ_nid2sn(curve_nid)) == NULL)
|
||||
if ((curve_name = ec_curve_nid2name(curve_nid)) == NULL)
|
||||
return 0;
|
||||
if (!ossl_param_build_set_utf8_string(tmpl, params,
|
||||
OSSL_PKEY_PARAM_EC_NAME,
|
||||
curve_name))
|
||||
|
||||
if (!ossl_param_bld_push_utf8_string(tmpl, OSSL_PKEY_PARAM_EC_NAME, curve_name, 0))
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Callers of params_to_key MUST make sure that params_to_domparams has been
|
||||
* called before!
|
||||
*
|
||||
* This function only imports the bare keypair, domain parameters and other
|
||||
* parameters are imported separately, and domain parameters are required to
|
||||
* define a keypair.
|
||||
*/
|
||||
static ossl_inline
|
||||
int params_to_key(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
|
||||
{
|
||||
const OSSL_PARAM *param_priv_key, *param_pub_key;
|
||||
BIGNUM *priv_key = NULL;
|
||||
unsigned char *pub_key = NULL;
|
||||
size_t pub_key_len;
|
||||
const EC_GROUP *ecg = NULL;
|
||||
EC_POINT *pub_point = NULL;
|
||||
int ok = 0;
|
||||
|
||||
ecg = EC_KEY_get0_group(ec);
|
||||
if (ecg == NULL)
|
||||
return 0;
|
||||
|
||||
param_priv_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
|
||||
param_pub_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
|
||||
|
||||
/*
|
||||
* We want to have at least a public key either way, so we end up
|
||||
* requiring it unconditionally.
|
||||
*/
|
||||
if (param_pub_key == NULL
|
||||
|| !OSSL_PARAM_get_octet_string(param_pub_key,
|
||||
(void **)&pub_key, 0, &pub_key_len)
|
||||
|| (pub_point = EC_POINT_new(ecg)) == NULL
|
||||
|| !EC_POINT_oct2point(ecg, pub_point,
|
||||
pub_key, pub_key_len, NULL))
|
||||
goto err;
|
||||
|
||||
if (param_priv_key != NULL && include_private) {
|
||||
int fixed_top;
|
||||
const BIGNUM *order;
|
||||
|
||||
/*
|
||||
* Key import/export should never leak the bit length of the secret
|
||||
* scalar in the key.
|
||||
*
|
||||
* For this reason, on export we use padded BIGNUMs with fixed length.
|
||||
*
|
||||
* When importing we also should make sure that, even if short lived,
|
||||
* the newly created BIGNUM is marked with the BN_FLG_CONSTTIME flag as
|
||||
* soon as possible, so that any processing of this BIGNUM might opt for
|
||||
* constant time implementations in the backend.
|
||||
*
|
||||
* Setting the BN_FLG_CONSTTIME flag alone is never enough, we also have
|
||||
* to preallocate the BIGNUM internal buffer to a fixed public size big
|
||||
* enough that operations performed during the processing never trigger
|
||||
* a realloc which would leak the size of the scalar through memory
|
||||
* accesses.
|
||||
*
|
||||
* Fixed Length
|
||||
* ------------
|
||||
*
|
||||
* The order of the large prime subgroup of the curve is our choice for
|
||||
* a fixed public size, as that is generally the upper bound for
|
||||
* generating a private key in EC cryptosystems and should fit all valid
|
||||
* secret scalars.
|
||||
*
|
||||
* For padding on export we just use the bit length of the order
|
||||
* converted to bytes (rounding up).
|
||||
*
|
||||
* For preallocating the BIGNUM storage we look at the number of "words"
|
||||
* required for the internal representation of the order, and we
|
||||
* preallocate 2 extra "words" in case any of the subsequent processing
|
||||
* might temporarily overflow the order length.
|
||||
*/
|
||||
order = EC_GROUP_get0_order(ecg);
|
||||
if (order == NULL || BN_is_zero(order))
|
||||
goto err;
|
||||
|
||||
fixed_top = bn_get_top(order) + 2;
|
||||
|
||||
if ((priv_key = BN_new()) == NULL)
|
||||
goto err;
|
||||
if (bn_wexpand(priv_key, fixed_top) == NULL)
|
||||
goto err;
|
||||
BN_set_flags(priv_key, BN_FLG_CONSTTIME);
|
||||
|
||||
if (!OSSL_PARAM_get_BN(param_priv_key, &priv_key))
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (priv_key != NULL
|
||||
&& !EC_KEY_set_private_key(ec, priv_key))
|
||||
goto err;
|
||||
|
||||
if (!EC_KEY_set_public_key(ec, pub_point))
|
||||
goto err;
|
||||
|
||||
ok = 1;
|
||||
|
||||
err:
|
||||
BN_clear_free(priv_key);
|
||||
OPENSSL_free(pub_key);
|
||||
EC_POINT_free(pub_point);
|
||||
return ok;
|
||||
}
|
||||
|
||||
/*
|
||||
* Callers of key_to_params MUST make sure that domparams_to_params is also
|
||||
* called!
|
||||
@@ -256,34 +107,33 @@ int params_to_key(EC_KEY *ec, const OSSL_PARAM params[], int include_private)
|
||||
* parameters are exported separately.
|
||||
*/
|
||||
static ossl_inline
|
||||
int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl, int include_private)
|
||||
int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl,
|
||||
OSSL_PARAM params[], int include_private,
|
||||
unsigned char **pub_key)
|
||||
{
|
||||
const BIGNUM *priv_key = NULL;
|
||||
const EC_POINT *pub_point = NULL;
|
||||
const EC_GROUP *ecg = NULL;
|
||||
unsigned char *pub_key = NULL;
|
||||
size_t pub_key_len = 0;
|
||||
int ret = 0;
|
||||
|
||||
if (eckey == NULL)
|
||||
if (eckey == NULL
|
||||
|| (ecg = EC_KEY_get0_group(eckey)) == NULL)
|
||||
return 0;
|
||||
|
||||
ecg = EC_KEY_get0_group(eckey);
|
||||
priv_key = EC_KEY_get0_private_key(eckey);
|
||||
pub_point = EC_KEY_get0_public_key(eckey);
|
||||
|
||||
/* group and public_key must be present, priv_key is optional */
|
||||
if (ecg == NULL || pub_point == NULL)
|
||||
return 0;
|
||||
if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
|
||||
POINT_CONVERSION_COMPRESSED,
|
||||
&pub_key, NULL)) == 0)
|
||||
return 0;
|
||||
|
||||
if (!ossl_param_bld_push_octet_string(tmpl,
|
||||
OSSL_PKEY_PARAM_PUB_KEY,
|
||||
pub_key, pub_key_len))
|
||||
goto err;
|
||||
if (pub_point != NULL) {
|
||||
/* convert pub_point to a octet string according to the SECG standard */
|
||||
if ((pub_key_len = EC_POINT_point2buf(ecg, pub_point,
|
||||
POINT_CONVERSION_COMPRESSED,
|
||||
pub_key, NULL)) == 0
|
||||
|| !ossl_param_build_set_octet_string(tmpl, params,
|
||||
OSSL_PKEY_PARAM_PUB_KEY,
|
||||
*pub_key, pub_key_len))
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (priv_key != NULL && include_private) {
|
||||
size_t sz;
|
||||
@@ -326,71 +176,20 @@ int key_to_params(const EC_KEY *eckey, OSSL_PARAM_BLD *tmpl, int include_private
|
||||
if (ecbits <= 0)
|
||||
goto err;
|
||||
sz = (ecbits + 7 ) / 8;
|
||||
if (!ossl_param_bld_push_BN_pad(tmpl,
|
||||
OSSL_PKEY_PARAM_PRIV_KEY,
|
||||
priv_key, sz))
|
||||
|
||||
if (!ossl_param_build_set_bn_pad(tmpl, params,
|
||||
OSSL_PKEY_PARAM_PRIV_KEY,
|
||||
priv_key, sz))
|
||||
goto err;
|
||||
}
|
||||
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
OPENSSL_free(pub_key);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ossl_inline
|
||||
int ec_set_param_ecdh_cofactor_mode(EC_KEY *ec, const OSSL_PARAM *p)
|
||||
{
|
||||
const EC_GROUP *ecg = EC_KEY_get0_group(ec);
|
||||
const BIGNUM *cofactor;
|
||||
int mode;
|
||||
|
||||
if (!OSSL_PARAM_get_int(p, &mode))
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* mode can be only 0 for disable, or 1 for enable here.
|
||||
*
|
||||
* This is in contrast with the same parameter on an ECDH EVP_PKEY_CTX that
|
||||
* also supports mode == -1 with the meaning of "reset to the default for
|
||||
* the associated key".
|
||||
*/
|
||||
if (mode < 0 || mode > 1)
|
||||
return 0;
|
||||
|
||||
if ((cofactor = EC_GROUP_get0_cofactor(ecg)) == NULL )
|
||||
return 0;
|
||||
|
||||
/* ECDH cofactor mode has no effect if cofactor is 1 */
|
||||
if (BN_is_one(cofactor))
|
||||
return 1;
|
||||
|
||||
if (mode == 1)
|
||||
EC_KEY_set_flags(ec, EC_FLAG_COFACTOR_ECDH);
|
||||
else if (mode == 0)
|
||||
EC_KEY_clear_flags(ec, EC_FLAG_COFACTOR_ECDH);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static ossl_inline
|
||||
int params_to_otherparams(EC_KEY *ec, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *p;
|
||||
|
||||
if (ec == NULL)
|
||||
return 0;
|
||||
|
||||
p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_USE_COFACTOR_ECDH);
|
||||
if (p != NULL && !ec_set_param_ecdh_cofactor_mode(ec, p))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static ossl_inline
|
||||
int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
|
||||
int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl,
|
||||
OSSL_PARAM params[])
|
||||
{
|
||||
int ecdh_cofactor_mode = 0;
|
||||
|
||||
@@ -399,18 +198,15 @@ int otherparams_to_params(const EC_KEY *ec, OSSL_PARAM_BLD *tmpl)
|
||||
|
||||
ecdh_cofactor_mode =
|
||||
(EC_KEY_get_flags(ec) & EC_FLAG_COFACTOR_ECDH) ? 1 : 0;
|
||||
if (!ossl_param_bld_push_int(tmpl,
|
||||
OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
|
||||
ecdh_cofactor_mode))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
return ossl_param_build_set_int(tmpl, params,
|
||||
OSSL_PKEY_PARAM_USE_COFACTOR_ECDH,
|
||||
ecdh_cofactor_mode);
|
||||
}
|
||||
|
||||
static
|
||||
void *ec_newdata(void *provctx)
|
||||
{
|
||||
return EC_KEY_new();
|
||||
return EC_KEY_new_ex(PROV_LIBRARY_CONTEXT_OF(provctx));
|
||||
}
|
||||
|
||||
static
|
||||
@@ -425,21 +221,22 @@ int ec_has(void *keydata, int selection)
|
||||
EC_KEY *ec = keydata;
|
||||
int ok = 0;
|
||||
|
||||
if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && (EC_KEY_get0_group(ec) != NULL);
|
||||
/*
|
||||
* We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be available,
|
||||
* so no extra check is needed other than the previous one against
|
||||
* EC_POSSIBLE_SELECTIONS.
|
||||
*/
|
||||
if (ec != NULL) {
|
||||
if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && (EC_KEY_get0_public_key(ec) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && (EC_KEY_get0_private_key(ec) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && (EC_KEY_get0_group(ec) != NULL);
|
||||
/*
|
||||
* We consider OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS to always be
|
||||
* available, so no extra check is needed other than the previous one
|
||||
* against EC_POSSIBLE_SELECTIONS.
|
||||
*/
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -501,15 +298,15 @@ int ec_import(void *keydata, int selection, const OSSL_PARAM params[])
|
||||
return 0;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && params_to_domparams(ec, params);
|
||||
ok = ok && ec_key_domparams_fromdata(ec, params);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
|
||||
int include_private =
|
||||
selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
|
||||
|
||||
ok = ok && params_to_key(ec, params, include_private);
|
||||
ok = ok && ec_key_fromdata(ec, params, include_private);
|
||||
}
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
|
||||
ok = ok && params_to_otherparams(ec, params);
|
||||
ok = ok && ec_key_otherparams_fromdata(ec, params);
|
||||
|
||||
return ok;
|
||||
}
|
||||
@@ -519,8 +316,9 @@ int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
|
||||
void *cbarg)
|
||||
{
|
||||
EC_KEY *ec = keydata;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM_BLD *tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
unsigned char *pub_key = NULL;
|
||||
int ok = 1;
|
||||
|
||||
if (ec == NULL)
|
||||
@@ -548,25 +346,28 @@ int ec_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
|
||||
&& (selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
|
||||
return 0;
|
||||
|
||||
ossl_param_bld_init(&tmpl);
|
||||
tmpl = OSSL_PARAM_BLD_new();
|
||||
if (tmpl == NULL)
|
||||
return 0;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && domparams_to_params(ec, &tmpl);
|
||||
ok = ok && domparams_to_params(ec, tmpl, NULL);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
|
||||
int include_private =
|
||||
selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
|
||||
|
||||
ok = ok && key_to_params(ec, &tmpl, include_private);
|
||||
ok = ok && key_to_params(ec, tmpl, NULL, include_private, &pub_key);
|
||||
}
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
|
||||
ok = ok && otherparams_to_params(ec, &tmpl);
|
||||
ok = ok && otherparams_to_params(ec, tmpl, NULL);
|
||||
|
||||
if (!ok
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
return 0;
|
||||
if (ok && (params = OSSL_PARAM_BLD_to_param(tmpl)) != NULL)
|
||||
ok = param_cb(params, cbarg);
|
||||
|
||||
ok = param_cb(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
OSSL_PARAM_BLD_free_params(params);
|
||||
OSSL_PARAM_BLD_free(tmpl);
|
||||
OPENSSL_free(pub_key);
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -626,9 +427,11 @@ const OSSL_PARAM *ec_export_types(int selection)
|
||||
static
|
||||
int ec_get_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
int ret;
|
||||
EC_KEY *eck = key;
|
||||
const EC_GROUP *ecg = NULL;
|
||||
OSSL_PARAM *p;
|
||||
unsigned char *pub_key = NULL;
|
||||
|
||||
ecg = EC_KEY_get0_group(eck);
|
||||
if (ecg == NULL)
|
||||
@@ -688,15 +491,21 @@ int ec_get_params(void *key, OSSL_PARAM params[])
|
||||
if (!OSSL_PARAM_set_int(p, ecdh_cofactor_mode))
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
ret = domparams_to_params(eck, NULL, params)
|
||||
&& key_to_params(eck, NULL, params, 1, &pub_key)
|
||||
&& otherparams_to_params(eck, NULL, params);
|
||||
OPENSSL_free(pub_key);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM ec_known_gettable_params[] = {
|
||||
OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
|
||||
OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
|
||||
OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
|
||||
OSSL_PARAM_int(OSSL_PKEY_PARAM_USE_COFACTOR_ECDH, NULL),
|
||||
EC_IMEXPORTABLE_DOM_PARAMETERS,
|
||||
EC_IMEXPORTABLE_PUBLIC_KEY,
|
||||
EC_IMEXPORTABLE_PRIVATE_KEY,
|
||||
EC_IMEXPORTABLE_OTHER_PARAMETERS,
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
@@ -730,8 +539,224 @@ int ec_set_params(void *key, const OSSL_PARAM params[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
static
|
||||
int ec_validate(void *keydata, int selection)
|
||||
{
|
||||
EC_KEY *eck = keydata;
|
||||
int ok = 0;
|
||||
BN_CTX *ctx = BN_CTX_new_ex(ec_key_get_libctx(eck));
|
||||
|
||||
if (ctx == NULL)
|
||||
return 0;
|
||||
|
||||
if ((selection & EC_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0)
|
||||
ok = ok && EC_GROUP_check(EC_KEY_get0_group(eck), ctx);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && ec_key_public_check(eck, ctx);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && ec_key_private_check(eck);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == OSSL_KEYMGMT_SELECT_KEYPAIR)
|
||||
ok = ok && ec_key_pairwise_check(eck, ctx);
|
||||
|
||||
BN_CTX_free(ctx);
|
||||
return ok;
|
||||
}
|
||||
|
||||
struct ec_gen_ctx {
|
||||
OPENSSL_CTX *libctx;
|
||||
|
||||
EC_GROUP *gen_group;
|
||||
int selection;
|
||||
};
|
||||
|
||||
static void *ec_gen_init(void *provctx, int selection)
|
||||
{
|
||||
OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
|
||||
struct ec_gen_ctx *gctx = NULL;
|
||||
|
||||
if ((selection & (EC_POSSIBLE_SELECTIONS)) == 0)
|
||||
return NULL;
|
||||
|
||||
if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
|
||||
gctx->libctx = libctx;
|
||||
gctx->gen_group = NULL;
|
||||
gctx->selection = selection;
|
||||
}
|
||||
return gctx;
|
||||
}
|
||||
|
||||
static int ec_gen_set_group(void *genctx, int nid)
|
||||
{
|
||||
struct ec_gen_ctx *gctx = genctx;
|
||||
EC_GROUP *group;
|
||||
|
||||
group = EC_GROUP_new_by_curve_name_ex(gctx->libctx, nid);
|
||||
if (group == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
|
||||
return 0;
|
||||
}
|
||||
EC_GROUP_free(gctx->gen_group);
|
||||
gctx->gen_group = group;
|
||||
return 1;
|
||||
}
|
||||
static int ec_gen_set_template(void *genctx, void *templ)
|
||||
{
|
||||
struct ec_gen_ctx *gctx = genctx;
|
||||
EC_KEY *ec = templ;
|
||||
const EC_GROUP *ec_group;
|
||||
|
||||
if (gctx == NULL || ec == NULL)
|
||||
return 0;
|
||||
if ((ec_group = EC_KEY_get0_group(ec)) == NULL)
|
||||
return 0;
|
||||
return ec_gen_set_group(gctx, EC_GROUP_get_curve_name(ec_group));
|
||||
}
|
||||
|
||||
static int ec_gen_set_params(void *genctx, const OSSL_PARAM params[])
|
||||
{
|
||||
struct ec_gen_ctx *gctx = genctx;
|
||||
const OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_EC_NAME))
|
||||
!= NULL) {
|
||||
const char *curve_name = NULL;
|
||||
int ret = 0;
|
||||
|
||||
switch (p->data_type) {
|
||||
case OSSL_PARAM_UTF8_STRING:
|
||||
/* The OSSL_PARAM functions have no support for this */
|
||||
curve_name = p->data;
|
||||
ret = (curve_name != NULL);
|
||||
break;
|
||||
case OSSL_PARAM_UTF8_PTR:
|
||||
ret = OSSL_PARAM_get_utf8_ptr(p, &curve_name);
|
||||
break;
|
||||
}
|
||||
|
||||
if (ret) {
|
||||
int nid = ec_curve_name2nid(curve_name);
|
||||
|
||||
if (nid == NID_undef) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CURVE);
|
||||
ret = 0;
|
||||
} else {
|
||||
ret = ec_gen_set_group(gctx, nid);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *ec_gen_settable_params(void *provctx)
|
||||
{
|
||||
static OSSL_PARAM settable[] = {
|
||||
{ OSSL_PKEY_PARAM_EC_NAME, OSSL_PARAM_UTF8_STRING, NULL, 0, 0 },
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
return settable;
|
||||
}
|
||||
|
||||
static int ec_gen_get_params(void *genctx, OSSL_PARAM params[])
|
||||
{
|
||||
struct ec_gen_ctx *gctx = genctx;
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_EC_NAME)) != NULL) {
|
||||
int nid = EC_GROUP_get_curve_name(gctx->gen_group);
|
||||
int ret = 0;
|
||||
const char *curve_name = ec_curve_nid2name(nid);
|
||||
|
||||
switch (p->data_type) {
|
||||
case OSSL_PARAM_UTF8_STRING:
|
||||
ret = OSSL_PARAM_set_utf8_string(p, curve_name);
|
||||
break;
|
||||
case OSSL_PARAM_UTF8_PTR:
|
||||
ret = OSSL_PARAM_set_utf8_ptr(p, curve_name);
|
||||
break;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *ec_gen_gettable_params(void *provctx)
|
||||
{
|
||||
static OSSL_PARAM gettable[] = {
|
||||
{ OSSL_PKEY_PARAM_EC_NAME, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
return gettable;
|
||||
}
|
||||
|
||||
static int ec_gen_assign_group(EC_KEY *ec, EC_GROUP *group)
|
||||
{
|
||||
if (group == NULL) {
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_NO_PARAMETERS_SET);
|
||||
return 0;
|
||||
}
|
||||
return EC_KEY_set_group(ec, group) > 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* The callback arguments (osslcb & cbarg) are not used by EC_KEY generation
|
||||
*/
|
||||
static void *ec_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
|
||||
{
|
||||
struct ec_gen_ctx *gctx = genctx;
|
||||
EC_KEY *ec = NULL;
|
||||
int ret = 1; /* Start optimistically */
|
||||
|
||||
if (gctx == NULL
|
||||
|| (ec = EC_KEY_new_ex(gctx->libctx)) == NULL)
|
||||
return NULL;
|
||||
|
||||
/* We must always assign a group, no matter what */
|
||||
ret = ec_gen_assign_group(ec, gctx->gen_group);
|
||||
/* Whether you want it or not, you get a keypair, not just one half */
|
||||
if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ret = ret && EC_KEY_generate_key(ec);
|
||||
|
||||
if (ret)
|
||||
return ec;
|
||||
|
||||
/* Something went wrong, throw the key away */
|
||||
EC_KEY_free(ec);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void ec_gen_cleanup(void *genctx)
|
||||
{
|
||||
struct ec_gen_ctx *gctx = genctx;
|
||||
|
||||
if (gctx == NULL)
|
||||
return;
|
||||
|
||||
EC_GROUP_free(gctx->gen_group);
|
||||
OPENSSL_free(gctx);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH ec_keymgmt_functions[] = {
|
||||
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))ec_newdata },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))ec_gen_init },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_SET_TEMPLATE,
|
||||
(void (*)(void))ec_gen_set_template },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))ec_gen_set_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
|
||||
(void (*)(void))ec_gen_settable_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_GET_PARAMS, (void (*)(void))ec_gen_get_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_GETTABLE_PARAMS,
|
||||
(void (*)(void))ec_gen_gettable_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))ec_gen },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))ec_gen_cleanup },
|
||||
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ec_freedata },
|
||||
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))ec_get_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ec_gettable_params },
|
||||
@@ -739,11 +764,12 @@ const OSSL_DISPATCH ec_keymgmt_functions[] = {
|
||||
{ OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))ec_settable_params },
|
||||
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ec_has },
|
||||
{ OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))ec_match },
|
||||
{ OSSL_FUNC_KEYMGMT_VALIDATE, (void (*)(void))ec_validate },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ec_import },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ec_import_types },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ec_export },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ec_export_types },
|
||||
{ OSSL_FUNC_KEYMGMT_QUERY_OPERATION_NAME,
|
||||
(void (*)(void))ec_query_operation_name },
|
||||
(void (*)(void))ec_query_operation_name },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -10,16 +10,19 @@
|
||||
#include <assert.h>
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/params.h>
|
||||
#include "internal/param_build.h"
|
||||
#include "crypto/ecx.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommon.h"
|
||||
#include "internal/param_build_set.h"
|
||||
|
||||
static OSSL_OP_keymgmt_new_fn x25519_new_key;
|
||||
static OSSL_OP_keymgmt_new_fn x448_new_key;
|
||||
static OSSL_OP_keymgmt_new_fn ed25519_new_key;
|
||||
static OSSL_OP_keymgmt_new_fn ed448_new_key;
|
||||
static OSSL_OP_keymgmt_get_params_fn x25519_get_params;
|
||||
static OSSL_OP_keymgmt_get_params_fn x448_get_params;
|
||||
static OSSL_OP_keymgmt_get_params_fn ed25519_get_params;
|
||||
static OSSL_OP_keymgmt_get_params_fn ed448_get_params;
|
||||
static OSSL_OP_keymgmt_gettable_params_fn ecx_gettable_params;
|
||||
static OSSL_OP_keymgmt_has_fn ecx_has;
|
||||
static OSSL_OP_keymgmt_import_fn ecx_import;
|
||||
@@ -31,37 +34,47 @@ static OSSL_OP_keymgmt_export_types_fn ecx_imexport_types;
|
||||
|
||||
static void *x25519_new_key(void *provctx)
|
||||
{
|
||||
return ecx_key_new(X25519_KEYLEN, 0);
|
||||
return ecx_key_new(ECX_KEY_TYPE_X25519, 0);
|
||||
}
|
||||
|
||||
static void *x448_new_key(void *provctx)
|
||||
{
|
||||
return ecx_key_new(X448_KEYLEN, 0);
|
||||
return ecx_key_new(ECX_KEY_TYPE_X448, 0);
|
||||
}
|
||||
|
||||
static void *ed25519_new_key(void *provctx)
|
||||
{
|
||||
return ecx_key_new(ECX_KEY_TYPE_ED25519, 0);
|
||||
}
|
||||
|
||||
static void *ed448_new_key(void *provctx)
|
||||
{
|
||||
return ecx_key_new(ECX_KEY_TYPE_ED448, 0);
|
||||
}
|
||||
|
||||
static int ecx_has(void *keydata, int selection)
|
||||
{
|
||||
ECX_KEY *key = keydata;
|
||||
int ok = 1;
|
||||
int ok = 0;
|
||||
|
||||
if ((selection & ECX_POSSIBLE_SELECTIONS) == 0)
|
||||
return 0;
|
||||
if (key != NULL) {
|
||||
if ((selection & ECX_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && key->haspubkey;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && key->privkey != NULL;
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && key->haspubkey;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && key->privkey != NULL;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int ecx_import(void *keydata, int selection, const OSSL_PARAM params[])
|
||||
{
|
||||
ECX_KEY *key = keydata;
|
||||
size_t privkeylen = 0, pubkeylen;
|
||||
const OSSL_PARAM *param_priv_key = NULL, *param_pub_key;
|
||||
unsigned char *pubkey;
|
||||
int ok = 1;
|
||||
int include_private = 0;
|
||||
|
||||
if (key == NULL)
|
||||
return 0;
|
||||
@@ -69,52 +82,28 @@ static int ecx_import(void *keydata, int selection, const OSSL_PARAM params[])
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) == 0)
|
||||
return 0;
|
||||
|
||||
param_pub_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
|
||||
include_private = ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && ecx_key_fromdata(key, params, include_private);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
param_priv_key =
|
||||
OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
|
||||
/*
|
||||
* If a private key is present then a public key must also be present.
|
||||
* Alternatively we've just got a public key.
|
||||
*/
|
||||
if (param_pub_key == NULL)
|
||||
return 0;
|
||||
|
||||
if (param_priv_key != NULL
|
||||
&& !OSSL_PARAM_get_octet_string(param_priv_key,
|
||||
(void **)&key->privkey, key->keylen,
|
||||
&privkeylen))
|
||||
return 0;
|
||||
|
||||
pubkey = key->pubkey;
|
||||
if (!OSSL_PARAM_get_octet_string(param_pub_key,
|
||||
(void **)&pubkey,
|
||||
sizeof(key->pubkey), &pubkeylen))
|
||||
return 0;
|
||||
|
||||
if (pubkeylen != key->keylen
|
||||
|| (param_priv_key != NULL && privkeylen != key->keylen))
|
||||
return 0;
|
||||
|
||||
key->haspubkey = 1;
|
||||
|
||||
return 1;
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int key_to_params(ECX_KEY *key, OSSL_PARAM_BLD *tmpl)
|
||||
static int key_to_params(ECX_KEY *key, OSSL_PARAM_BLD *tmpl,
|
||||
OSSL_PARAM params[])
|
||||
{
|
||||
if (key == NULL)
|
||||
return 0;
|
||||
|
||||
if (!ossl_param_bld_push_octet_string(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
|
||||
key->pubkey, key->keylen))
|
||||
if (!ossl_param_build_set_octet_string(tmpl, params,
|
||||
OSSL_PKEY_PARAM_PUB_KEY,
|
||||
key->pubkey, key->keylen))
|
||||
return 0;
|
||||
|
||||
if (key->privkey != NULL
|
||||
&& !ossl_param_bld_push_octet_string(tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
|
||||
key->privkey, key->keylen))
|
||||
&& !ossl_param_build_set_octet_string(tmpl, params,
|
||||
OSSL_PKEY_PARAM_PRIV_KEY,
|
||||
key->privkey, key->keylen))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
@@ -124,32 +113,42 @@ static int ecx_export(void *keydata, int selection, OSSL_CALLBACK *param_cb,
|
||||
void *cbarg)
|
||||
{
|
||||
ECX_KEY *key = keydata;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM_BLD *tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ret;
|
||||
int ret = 0;
|
||||
|
||||
if (key == NULL)
|
||||
return 0;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0
|
||||
&& !key_to_params(key, &tmpl))
|
||||
tmpl = OSSL_PARAM_BLD_new();
|
||||
if (tmpl == NULL)
|
||||
return 0;
|
||||
|
||||
ossl_param_bld_init(&tmpl);
|
||||
params = ossl_param_bld_to_param(&tmpl);
|
||||
if (params == NULL) {
|
||||
ossl_param_bld_free(params);
|
||||
return 0;
|
||||
}
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0
|
||||
&& !key_to_params(key, tmpl, NULL))
|
||||
goto err;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0
|
||||
&& !key_to_params(key, tmpl, NULL))
|
||||
goto err;
|
||||
|
||||
params = OSSL_PARAM_BLD_to_param(tmpl);
|
||||
if (params == NULL)
|
||||
goto err;
|
||||
|
||||
ret = param_cb(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
OSSL_PARAM_BLD_free_params(params);
|
||||
err:
|
||||
OSSL_PARAM_BLD_free(tmpl);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define ECX_KEY_TYPES() \
|
||||
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0), \
|
||||
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0)
|
||||
|
||||
static const OSSL_PARAM ecx_key_types[] = {
|
||||
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
|
||||
OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
|
||||
ECX_KEY_TYPES(),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
static const OSSL_PARAM *ecx_imexport_types(int selection)
|
||||
@@ -159,9 +158,10 @@ static const OSSL_PARAM *ecx_imexport_types(int selection)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int ecx_get_params(OSSL_PARAM params[], int bits, int secbits,
|
||||
static int ecx_get_params(void *key, OSSL_PARAM params[], int bits, int secbits,
|
||||
int size)
|
||||
{
|
||||
ECX_KEY *ecx = key;
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
|
||||
@@ -173,23 +173,38 @@ static int ecx_get_params(OSSL_PARAM params[], int bits, int secbits,
|
||||
if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_MAX_SIZE)) != NULL
|
||||
&& !OSSL_PARAM_set_int(p, size))
|
||||
return 0;
|
||||
return 1;
|
||||
return key_to_params(ecx, NULL, params);
|
||||
}
|
||||
|
||||
static int x25519_get_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
return ecx_get_params(params, X25519_BITS, X25519_SECURITY_BITS, X25519_KEYLEN);
|
||||
return ecx_get_params(key, params, X25519_BITS, X25519_SECURITY_BITS,
|
||||
X25519_KEYLEN);
|
||||
}
|
||||
|
||||
static int x448_get_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
return ecx_get_params(params, X448_BITS, X448_SECURITY_BITS, X448_KEYLEN);
|
||||
return ecx_get_params(key, params, X448_BITS, X448_SECURITY_BITS,
|
||||
X448_KEYLEN);
|
||||
}
|
||||
|
||||
static int ed25519_get_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
return ecx_get_params(key, params, ED25519_BITS, ED25519_SECURITY_BITS,
|
||||
ED25519_KEYLEN);
|
||||
}
|
||||
|
||||
static int ed448_get_params(void *key, OSSL_PARAM params[])
|
||||
{
|
||||
return ecx_get_params(key, params, ED448_BITS, ED448_SECURITY_BITS,
|
||||
ED448_KEYLEN);
|
||||
}
|
||||
|
||||
static const OSSL_PARAM ecx_params[] = {
|
||||
OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
|
||||
OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
|
||||
OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
|
||||
ECX_KEY_TYPES(),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
@@ -198,28 +213,21 @@ static const OSSL_PARAM *ecx_gettable_params(void)
|
||||
return ecx_params;
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH x25519_keymgmt_functions[] = {
|
||||
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))x25519_new_key },
|
||||
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ecx_key_free },
|
||||
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))x25519_get_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ecx_gettable_params },
|
||||
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ecx_has },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ecx_import },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ecx_imexport_types },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ecx_export },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ecx_imexport_types },
|
||||
{ 0, NULL }
|
||||
};
|
||||
#define MAKE_KEYMGMT_FUNCTIONS(alg) \
|
||||
const OSSL_DISPATCH alg##_keymgmt_functions[] = { \
|
||||
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))alg##_new_key }, \
|
||||
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ecx_key_free }, \
|
||||
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))alg##_get_params }, \
|
||||
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ecx_gettable_params }, \
|
||||
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ecx_has }, \
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ecx_import }, \
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ecx_imexport_types }, \
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ecx_export }, \
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ecx_imexport_types }, \
|
||||
{ 0, NULL } \
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH x448_keymgmt_functions[] = {
|
||||
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))x448_new_key },
|
||||
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))ecx_key_free },
|
||||
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))x448_get_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))ecx_gettable_params },
|
||||
{ OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))ecx_has },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))ecx_import },
|
||||
{ OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))ecx_imexport_types },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))ecx_export },
|
||||
{ OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))ecx_imexport_types },
|
||||
{ 0, NULL }
|
||||
};
|
||||
MAKE_KEYMGMT_FUNCTIONS(x25519)
|
||||
MAKE_KEYMGMT_FUNCTIONS(x448)
|
||||
MAKE_KEYMGMT_FUNCTIONS(ed25519)
|
||||
MAKE_KEYMGMT_FUNCTIONS(ed448)
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
|
||||
* Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
@@ -13,15 +13,18 @@
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/params.h>
|
||||
#include <openssl/types.h>
|
||||
#include "internal/param_build.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommon.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "crypto/rsa.h"
|
||||
#include "internal/param_build_set.h"
|
||||
|
||||
static OSSL_OP_keymgmt_new_fn rsa_newdata;
|
||||
static OSSL_OP_keymgmt_gen_init_fn rsa_gen_init;
|
||||
static OSSL_OP_keymgmt_gen_set_params_fn rsa_gen_set_params;
|
||||
static OSSL_OP_keymgmt_gen_settable_params_fn rsa_gen_settable_params;
|
||||
static OSSL_OP_keymgmt_gen_fn rsa_gen;
|
||||
static OSSL_OP_keymgmt_gen_cleanup_fn rsa_gen_cleanup;
|
||||
static OSSL_OP_keymgmt_free_fn rsa_freedata;
|
||||
static OSSL_OP_keymgmt_get_params_fn rsa_get_params;
|
||||
static OSSL_OP_keymgmt_gettable_params_fn rsa_gettable_params;
|
||||
@@ -34,106 +37,13 @@ static OSSL_OP_keymgmt_export_fn rsa_export;
|
||||
static OSSL_OP_keymgmt_export_types_fn rsa_export_types;
|
||||
|
||||
#define RSA_DEFAULT_MD "SHA256"
|
||||
#define RSA_POSSIBLE_SELECTIONS \
|
||||
#define RSA_POSSIBLE_SELECTIONS \
|
||||
(OSSL_KEYMGMT_SELECT_KEYPAIR | OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS)
|
||||
|
||||
DEFINE_STACK_OF(BIGNUM)
|
||||
DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
|
||||
|
||||
static int collect_numbers(STACK_OF(BIGNUM) *numbers,
|
||||
const OSSL_PARAM params[], const char *key)
|
||||
{
|
||||
const OSSL_PARAM *p = NULL;
|
||||
|
||||
if (numbers == NULL)
|
||||
return 0;
|
||||
|
||||
for (p = params; (p = OSSL_PARAM_locate_const(p, key)) != NULL; p++) {
|
||||
BIGNUM *tmp = NULL;
|
||||
|
||||
if (!OSSL_PARAM_get_BN(p, &tmp))
|
||||
return 0;
|
||||
sk_BIGNUM_push(numbers, tmp);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int params_to_key(RSA *rsa, const OSSL_PARAM params[])
|
||||
{
|
||||
const OSSL_PARAM *param_n, *param_e, *param_d;
|
||||
BIGNUM *n = NULL, *e = NULL, *d = NULL;
|
||||
STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
|
||||
int is_private = 0;
|
||||
|
||||
if (rsa == NULL)
|
||||
return 0;
|
||||
|
||||
param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
|
||||
param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
|
||||
param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
|
||||
|
||||
if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
|
||||
|| (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
|
||||
|| (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
|
||||
goto err;
|
||||
|
||||
is_private = (d != NULL);
|
||||
|
||||
if (!RSA_set0_key(rsa, n, e, d))
|
||||
goto err;
|
||||
n = e = d = NULL;
|
||||
|
||||
if (is_private) {
|
||||
if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
|
||||
OSSL_PKEY_PARAM_RSA_FACTOR)
|
||||
|| !collect_numbers(exps = sk_BIGNUM_new_null(), params,
|
||||
OSSL_PKEY_PARAM_RSA_EXPONENT)
|
||||
|| !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
|
||||
OSSL_PKEY_PARAM_RSA_COEFFICIENT))
|
||||
goto err;
|
||||
|
||||
/* It's ok if this private key just has n, e and d */
|
||||
if (sk_BIGNUM_num(factors) != 0
|
||||
&& !rsa_set0_all_params(rsa, factors, exps, coeffs))
|
||||
goto err;
|
||||
}
|
||||
|
||||
sk_BIGNUM_free(factors);
|
||||
sk_BIGNUM_free(exps);
|
||||
sk_BIGNUM_free(coeffs);
|
||||
return 1;
|
||||
|
||||
err:
|
||||
BN_free(n);
|
||||
BN_free(e);
|
||||
BN_free(d);
|
||||
sk_BIGNUM_pop_free(factors, BN_free);
|
||||
sk_BIGNUM_pop_free(exps, BN_free);
|
||||
sk_BIGNUM_pop_free(coeffs, BN_free);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int export_numbers(OSSL_PARAM_BLD *tmpl, const char *key,
|
||||
STACK_OF(BIGNUM_const) *numbers)
|
||||
{
|
||||
int i, nnum;
|
||||
|
||||
if (numbers == NULL)
|
||||
return 0;
|
||||
|
||||
nnum = sk_BIGNUM_const_num(numbers);
|
||||
|
||||
for (i = 0; i < nnum; i++) {
|
||||
if (!ossl_param_bld_push_BN(tmpl, key,
|
||||
sk_BIGNUM_const_value(numbers, i)))
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *tmpl)
|
||||
static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
|
||||
{
|
||||
int ret = 0;
|
||||
const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
|
||||
@@ -147,21 +57,16 @@ static int key_to_params(RSA *rsa, OSSL_PARAM_BLD *tmpl)
|
||||
RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
|
||||
rsa_get0_all_params(rsa, factors, exps, coeffs);
|
||||
|
||||
if (rsa_n != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_N, rsa_n))
|
||||
if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n)
|
||||
|| !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e)
|
||||
|| !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D, rsa_d)
|
||||
|| !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_factor_names,
|
||||
factors)
|
||||
|| !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_exp_names,
|
||||
exps)
|
||||
|| !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_coeff_names,
|
||||
coeffs))
|
||||
goto err;
|
||||
if (rsa_e != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_E, rsa_e))
|
||||
goto err;
|
||||
if (rsa_d != NULL
|
||||
&& !ossl_param_bld_push_BN(tmpl, OSSL_PKEY_PARAM_RSA_D, rsa_d))
|
||||
goto err;
|
||||
|
||||
if (!export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_FACTOR, factors)
|
||||
|| !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_EXPONENT, exps)
|
||||
|| !export_numbers(tmpl, OSSL_PKEY_PARAM_RSA_COEFFICIENT, coeffs))
|
||||
goto err;
|
||||
|
||||
ret = 1;
|
||||
err:
|
||||
sk_BIGNUM_const_free(factors);
|
||||
@@ -187,14 +92,19 @@ static int rsa_has(void *keydata, int selection)
|
||||
RSA *rsa = keydata;
|
||||
int ok = 0;
|
||||
|
||||
if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
if (rsa != NULL) {
|
||||
if ((selection & RSA_POSSIBLE_SELECTIONS) != 0)
|
||||
ok = 1;
|
||||
|
||||
ok = ok && (RSA_get0_e(rsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && (RSA_get0_n(rsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && (RSA_get0_d(rsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_OTHER_PARAMETERS) != 0)
|
||||
ok = ok && 0; /* This will change with PSS and OAEP */
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && (RSA_get0_e(rsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
|
||||
ok = ok && (RSA_get0_n(rsa) != NULL);
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
|
||||
ok = ok && (RSA_get0_d(rsa) != NULL);
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -224,7 +134,7 @@ static int rsa_import(void *keydata, int selection, const OSSL_PARAM params[])
|
||||
/* TODO(3.0) PSS and OAEP should bring on parameters */
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && params_to_key(rsa, params);
|
||||
ok = ok && rsa_fromdata(rsa, params);
|
||||
|
||||
return ok;
|
||||
}
|
||||
@@ -233,7 +143,7 @@ static int rsa_export(void *keydata, int selection,
|
||||
OSSL_CALLBACK *param_callback, void *cbarg)
|
||||
{
|
||||
RSA *rsa = keydata;
|
||||
OSSL_PARAM_BLD tmpl;
|
||||
OSSL_PARAM_BLD *tmpl;
|
||||
OSSL_PARAM *params = NULL;
|
||||
int ok = 1;
|
||||
|
||||
@@ -242,20 +152,75 @@ static int rsa_export(void *keydata, int selection,
|
||||
|
||||
/* TODO(3.0) PSS and OAEP should bring on parameters */
|
||||
|
||||
ossl_param_bld_init(&tmpl);
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && key_to_params(rsa, &tmpl);
|
||||
|
||||
if (!ok
|
||||
|| (params = ossl_param_bld_to_param(&tmpl)) == NULL)
|
||||
tmpl = OSSL_PARAM_BLD_new();
|
||||
if (tmpl == NULL)
|
||||
return 0;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0)
|
||||
ok = ok && key_to_params(rsa, tmpl, NULL);
|
||||
|
||||
if (!ok
|
||||
|| (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
|
||||
goto err;
|
||||
|
||||
ok = param_callback(params, cbarg);
|
||||
ossl_param_bld_free(params);
|
||||
OSSL_PARAM_BLD_free_params(params);
|
||||
err:
|
||||
OSSL_PARAM_BLD_free(tmpl);
|
||||
return ok;
|
||||
}
|
||||
|
||||
#ifdef FIPS_MODE
|
||||
/* In fips mode there are no multi-primes. */
|
||||
# define RSA_KEY_MP_TYPES() \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0),
|
||||
#else
|
||||
/*
|
||||
* We allow up to 10 prime factors (starting with p, q).
|
||||
* NOTE: there is only 9 OSSL_PKEY_PARAM_RSA_COEFFICIENT
|
||||
*/
|
||||
# define RSA_KEY_MP_TYPES() \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR1, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR2, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR3, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR4, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR5, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR6, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR7, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR8, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR9, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR10, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT1, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT2, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT3, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT4, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT5, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT6, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT7, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT8, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT9, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT10, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT2, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT3, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT4, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT5, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT6, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT7, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT8, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT9, NULL, 0),
|
||||
#endif
|
||||
|
||||
#define RSA_KEY_TYPES() \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0), \
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0), \
|
||||
RSA_KEY_MP_TYPES()
|
||||
|
||||
/*
|
||||
* This provider can export everything in an RSA key, so we use the exact
|
||||
* same type description for export as for import. Other providers might
|
||||
@@ -264,41 +229,8 @@ static int rsa_export(void *keydata, int selection,
|
||||
* different arrays.
|
||||
*/
|
||||
static const OSSL_PARAM rsa_key_types[] = {
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_N, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_D, NULL, 0),
|
||||
/* We tolerate up to 10 factors... */
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_FACTOR, NULL, 0),
|
||||
/* ..., up to 10 CRT exponents... */
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_EXPONENT, NULL, 0),
|
||||
/* ..., and up to 9 CRT coefficients */
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_COEFFICIENT, NULL, 0),
|
||||
RSA_KEY_TYPES()
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
/*
|
||||
* We lied about the amount of factors, exponents and coefficients, the
|
||||
@@ -319,7 +251,6 @@ static const OSSL_PARAM *rsa_import_types(int selection)
|
||||
return rsa_imexport_types(selection);
|
||||
}
|
||||
|
||||
|
||||
static const OSSL_PARAM *rsa_export_types(int selection)
|
||||
{
|
||||
return rsa_imexport_types(selection);
|
||||
@@ -365,8 +296,7 @@ static int rsa_get_params(void *key, OSSL_PARAM params[])
|
||||
if (!OSSL_PARAM_set_utf8_string(p, RSA_DEFAULT_MD))
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
return key_to_params(rsa, NULL, params);
|
||||
}
|
||||
|
||||
static const OSSL_PARAM rsa_params[] = {
|
||||
@@ -374,6 +304,7 @@ static const OSSL_PARAM rsa_params[] = {
|
||||
OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
|
||||
OSSL_PARAM_int(OSSL_PKEY_PARAM_MAX_SIZE, NULL),
|
||||
OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_DEFAULT_DIGEST, NULL, 0),
|
||||
RSA_KEY_TYPES()
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
@@ -403,8 +334,127 @@ static int rsa_validate(void *keydata, int selection)
|
||||
return ok;
|
||||
}
|
||||
|
||||
struct rsa_gen_ctx {
|
||||
OPENSSL_CTX *libctx;
|
||||
|
||||
size_t nbits;
|
||||
BIGNUM *pub_exp;
|
||||
size_t primes;
|
||||
|
||||
/* For generation callback */
|
||||
OSSL_CALLBACK *cb;
|
||||
void *cbarg;
|
||||
};
|
||||
|
||||
static int rsa_gencb(int p, int n, BN_GENCB *cb)
|
||||
{
|
||||
struct rsa_gen_ctx *gctx = BN_GENCB_get_arg(cb);
|
||||
OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END };
|
||||
|
||||
params[0] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_POTENTIAL, &p);
|
||||
params[1] = OSSL_PARAM_construct_int(OSSL_GEN_PARAM_ITERATION, &n);
|
||||
|
||||
return gctx->cb(params, gctx->cbarg);
|
||||
}
|
||||
|
||||
static void *rsa_gen_init(void *provctx, int selection)
|
||||
{
|
||||
OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
|
||||
struct rsa_gen_ctx *gctx = NULL;
|
||||
|
||||
if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
|
||||
return NULL;
|
||||
|
||||
if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) {
|
||||
gctx->libctx = libctx;
|
||||
if ((gctx->pub_exp = BN_new()) == NULL
|
||||
|| !BN_set_word(gctx->pub_exp, RSA_F4)) {
|
||||
BN_free(gctx->pub_exp);
|
||||
gctx = NULL;
|
||||
} else {
|
||||
gctx->nbits = 2048;
|
||||
gctx->primes = RSA_DEFAULT_PRIME_NUM;
|
||||
}
|
||||
}
|
||||
return gctx;
|
||||
}
|
||||
|
||||
static int rsa_gen_set_params(void *genctx, const OSSL_PARAM params[])
|
||||
{
|
||||
struct rsa_gen_ctx *gctx = genctx;
|
||||
const OSSL_PARAM *p;
|
||||
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_BITS)) != NULL
|
||||
&& !OSSL_PARAM_get_size_t(p, &gctx->nbits))
|
||||
return 0;
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PRIMES)) != NULL
|
||||
&& !OSSL_PARAM_get_size_t(p, &gctx->primes))
|
||||
return 0;
|
||||
if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E)) != NULL
|
||||
&& !OSSL_PARAM_get_BN(p, &gctx->pub_exp))
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *rsa_gen_settable_params(void *provctx)
|
||||
{
|
||||
static OSSL_PARAM settable[] = {
|
||||
OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_BITS, NULL),
|
||||
OSSL_PARAM_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, NULL),
|
||||
OSSL_PARAM_BN(OSSL_PKEY_PARAM_RSA_E, NULL, 0),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
return settable;
|
||||
}
|
||||
|
||||
static void *rsa_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
|
||||
{
|
||||
struct rsa_gen_ctx *gctx = genctx;
|
||||
RSA *rsa = NULL;
|
||||
BN_GENCB *gencb = NULL;
|
||||
|
||||
if (gctx == NULL
|
||||
|| (rsa = rsa_new_with_ctx(gctx->libctx)) == NULL)
|
||||
return NULL;
|
||||
|
||||
gctx->cb = osslcb;
|
||||
gctx->cbarg = cbarg;
|
||||
gencb = BN_GENCB_new();
|
||||
if (gencb != NULL)
|
||||
BN_GENCB_set(gencb, rsa_gencb, genctx);
|
||||
|
||||
if (!RSA_generate_multi_prime_key(rsa, (int)gctx->nbits, (int)gctx->primes,
|
||||
gctx->pub_exp, gencb)) {
|
||||
RSA_free(rsa);
|
||||
rsa = NULL;
|
||||
}
|
||||
|
||||
BN_GENCB_free(gencb);
|
||||
|
||||
return rsa;
|
||||
}
|
||||
|
||||
static void rsa_gen_cleanup(void *genctx)
|
||||
{
|
||||
struct rsa_gen_ctx *gctx = genctx;
|
||||
|
||||
if (gctx == NULL)
|
||||
return;
|
||||
|
||||
BN_clear_free(gctx->pub_exp);
|
||||
OPENSSL_free(gctx);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH rsa_keymgmt_functions[] = {
|
||||
{ OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))rsa_newdata },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))rsa_gen_init },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS,
|
||||
(void (*)(void))rsa_gen_set_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
|
||||
(void (*)(void))rsa_gen_settable_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))rsa_gen },
|
||||
{ OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))rsa_gen_cleanup },
|
||||
{ OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))rsa_freedata },
|
||||
{ OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))rsa_get_params },
|
||||
{ OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))rsa_gettable_params },
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <openssl/types.h>
|
||||
#include <openssl/x509.h> /* i2d_X509_PUBKEY_bio() */
|
||||
#include "crypto/bn.h" /* bn_get_words() */
|
||||
#include "crypto/ecx.h"
|
||||
#include "prov/bio.h" /* ossl_prov_bio_printf() */
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommonerr.h" /* PROV_R_READ_KEY */
|
||||
|
||||
@@ -26,6 +26,14 @@ void ecx_get_new_free_import(ECX_KEY_TYPE type,
|
||||
*ecx_new = ossl_prov_get_keymgmt_new(x448_keymgmt_functions);
|
||||
*ecx_free = ossl_prov_get_keymgmt_free(x448_keymgmt_functions);
|
||||
*ecx_import = ossl_prov_get_keymgmt_import(x448_keymgmt_functions);
|
||||
} else if (type == ECX_KEY_TYPE_ED25519) {
|
||||
*ecx_new = ossl_prov_get_keymgmt_new(ed25519_keymgmt_functions);
|
||||
*ecx_free = ossl_prov_get_keymgmt_free(ed25519_keymgmt_functions);
|
||||
*ecx_import = ossl_prov_get_keymgmt_import(ed25519_keymgmt_functions);
|
||||
} else if (type == ECX_KEY_TYPE_ED448) {
|
||||
*ecx_new = ossl_prov_get_keymgmt_new(ed448_keymgmt_functions);
|
||||
*ecx_free = ossl_prov_get_keymgmt_free(ed448_keymgmt_functions);
|
||||
*ecx_import = ossl_prov_get_keymgmt_import(ed448_keymgmt_functions);
|
||||
} else {
|
||||
*ecx_new = NULL;
|
||||
*ecx_free = NULL;
|
||||
@@ -40,23 +48,35 @@ int ossl_prov_print_ecx(BIO *out, ECX_KEY *ecxkey, enum ecx_print_type type)
|
||||
|
||||
switch (type) {
|
||||
case ecx_print_priv:
|
||||
switch (ecxkey->keylen) {
|
||||
case X25519_KEYLEN:
|
||||
switch (ecxkey->type) {
|
||||
case ECX_KEY_TYPE_X25519:
|
||||
type_label = "X25519 Private-Key";
|
||||
break;
|
||||
case X448_KEYLEN:
|
||||
case ECX_KEY_TYPE_X448:
|
||||
type_label = "X448 Private-Key";
|
||||
break;
|
||||
case ECX_KEY_TYPE_ED25519:
|
||||
type_label = "ED25519 Private-Key";
|
||||
break;
|
||||
case ECX_KEY_TYPE_ED448:
|
||||
type_label = "ED448 Private-Key";
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ecx_print_pub:
|
||||
switch (ecxkey->keylen) {
|
||||
case X25519_KEYLEN:
|
||||
switch (ecxkey->type) {
|
||||
case ECX_KEY_TYPE_X25519:
|
||||
type_label = "X25519 Public-Key";
|
||||
break;
|
||||
case X448_KEYLEN:
|
||||
case ECX_KEY_TYPE_X448:
|
||||
type_label = "X448 Public-Key";
|
||||
break;
|
||||
case ECX_KEY_TYPE_ED25519:
|
||||
type_label = "ED25519 Public-Key";
|
||||
break;
|
||||
case ECX_KEY_TYPE_ED448:
|
||||
type_label = "ED448 Public-Key";
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -13,12 +13,15 @@
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/types.h>
|
||||
#include <openssl/params.h>
|
||||
#include "crypto/ecx.h"
|
||||
#include "prov/bio.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "serializer_local.h"
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn x25519_priv_newctx;
|
||||
static OSSL_OP_serializer_newctx_fn x448_priv_newctx;
|
||||
static OSSL_OP_serializer_newctx_fn ed25519_priv_newctx;
|
||||
static OSSL_OP_serializer_newctx_fn ed448_priv_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn ecx_priv_freectx;
|
||||
static OSSL_OP_serializer_set_ctx_params_fn ecx_priv_set_ctx_params;
|
||||
static OSSL_OP_serializer_settable_ctx_params_fn ecx_priv_settable_ctx_params;
|
||||
@@ -65,6 +68,16 @@ static void *x448_priv_newctx(void *provctx)
|
||||
return ecx_priv_newctx(provctx, ECX_KEY_TYPE_X448);
|
||||
}
|
||||
|
||||
static void *ed25519_priv_newctx(void *provctx)
|
||||
{
|
||||
return ecx_priv_newctx(provctx, ECX_KEY_TYPE_ED25519);
|
||||
}
|
||||
|
||||
static void *ed448_priv_newctx(void *provctx)
|
||||
{
|
||||
return ecx_priv_newctx(provctx, ECX_KEY_TYPE_ED448);
|
||||
}
|
||||
|
||||
static void ecx_priv_freectx(void *vctx)
|
||||
{
|
||||
struct ecx_priv_ctx_st *ctx = vctx;
|
||||
@@ -150,14 +163,13 @@ static int ecx_priv_der(void *vctx, void *vecxkey, BIO *out,
|
||||
struct ecx_priv_ctx_st *ctx = vctx;
|
||||
ECX_KEY *ecxkey = vecxkey;
|
||||
int ret;
|
||||
int type = (ctx->type == ECX_KEY_TYPE_X25519) ? EVP_PKEY_X25519
|
||||
: EVP_PKEY_X448;
|
||||
int nid = KEYTYPE2NID(ctx->type);
|
||||
|
||||
ctx->sc.cb = cb;
|
||||
ctx->sc.cbarg = cbarg;
|
||||
|
||||
ret = ossl_prov_write_priv_der_from_obj(out, ecxkey,
|
||||
type,
|
||||
nid,
|
||||
NULL,
|
||||
ossl_prov_ecx_priv_to_der,
|
||||
&ctx->sc);
|
||||
@@ -194,14 +206,13 @@ static int ecx_priv_pem(void *vctx, void *ecxkey, BIO *out,
|
||||
{
|
||||
struct ecx_priv_ctx_st *ctx = vctx;
|
||||
int ret;
|
||||
int type = (ctx->type == ECX_KEY_TYPE_X25519) ? EVP_PKEY_X25519
|
||||
: EVP_PKEY_X448;
|
||||
int nid = KEYTYPE2NID(ctx->type);
|
||||
|
||||
ctx->sc.cb = cb;
|
||||
ctx->sc.cbarg = cbarg;
|
||||
|
||||
ret = ossl_prov_write_priv_pem_from_obj(out, ecxkey,
|
||||
type,
|
||||
nid,
|
||||
NULL,
|
||||
ossl_prov_ecx_priv_to_der,
|
||||
&ctx->sc);
|
||||
@@ -268,3 +279,5 @@ static int ecx_priv_print(void *ctx, void *ecxkey, BIO *out,
|
||||
|
||||
MAKE_SERIALIZER_FUNCTIONS_GROUP(x25519)
|
||||
MAKE_SERIALIZER_FUNCTIONS_GROUP(x448)
|
||||
MAKE_SERIALIZER_FUNCTIONS_GROUP(ed25519)
|
||||
MAKE_SERIALIZER_FUNCTIONS_GROUP(ed448)
|
||||
@@ -12,12 +12,15 @@
|
||||
#include <openssl/pem.h>
|
||||
#include <openssl/types.h>
|
||||
#include <openssl/params.h>
|
||||
#include "crypto/ecx.h"
|
||||
#include "prov/bio.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "serializer_local.h"
|
||||
|
||||
static OSSL_OP_serializer_newctx_fn x25519_pub_newctx;
|
||||
static OSSL_OP_serializer_newctx_fn x448_pub_newctx;
|
||||
static OSSL_OP_serializer_newctx_fn ed25519_pub_newctx;
|
||||
static OSSL_OP_serializer_newctx_fn ed448_pub_newctx;
|
||||
static OSSL_OP_serializer_freectx_fn ecx_pub_freectx;
|
||||
static OSSL_OP_serializer_serialize_data_fn ecx_pub_der_data;
|
||||
static OSSL_OP_serializer_serialize_object_fn ecx_pub_der;
|
||||
@@ -57,6 +60,16 @@ static void *x448_pub_newctx(void *provctx)
|
||||
return ecx_pub_newctx(provctx, ECX_KEY_TYPE_X448);
|
||||
}
|
||||
|
||||
static void *ed25519_pub_newctx(void *provctx)
|
||||
{
|
||||
return ecx_pub_newctx(provctx, ECX_KEY_TYPE_ED25519);
|
||||
}
|
||||
|
||||
static void *ed448_pub_newctx(void *provctx)
|
||||
{
|
||||
return ecx_pub_newctx(provctx, ECX_KEY_TYPE_ED448);
|
||||
}
|
||||
|
||||
static void ecx_pub_freectx(void *ctx)
|
||||
{
|
||||
OPENSSL_free(ctx);
|
||||
@@ -92,8 +105,7 @@ static int ecx_pub_der(void *vctx, void *ecxkey, BIO *out,
|
||||
struct ecx_pub_ctx_st *ctx = vctx;
|
||||
|
||||
return ossl_prov_write_pub_der_from_obj(out, ecxkey,
|
||||
ctx->type == ECX_KEY_TYPE_X25519
|
||||
? EVP_PKEY_X25519 : EVP_PKEY_X448,
|
||||
KEYTYPE2NID(ctx->type),
|
||||
NULL,
|
||||
ossl_prov_ecx_pub_to_der);
|
||||
}
|
||||
@@ -128,8 +140,7 @@ static int ecx_pub_pem(void *vctx, void *ecxkey, BIO *out,
|
||||
struct ecx_pub_ctx_st *ctx = vctx;
|
||||
|
||||
return ossl_prov_write_pub_pem_from_obj(out, ecxkey,
|
||||
ctx->type == ECX_KEY_TYPE_X25519
|
||||
? EVP_PKEY_X25519 : EVP_PKEY_X448,
|
||||
KEYTYPE2NID(ctx->type),
|
||||
NULL,
|
||||
ossl_prov_ecx_pub_to_der);
|
||||
|
||||
@@ -182,3 +193,5 @@ static int ecx_pub_print(void *ctx, void *ecxkey, BIO *out,
|
||||
|
||||
MAKE_SERIALIZER_FUNCTIONS_GROUP(x25519)
|
||||
MAKE_SERIALIZER_FUNCTIONS_GROUP(x448)
|
||||
MAKE_SERIALIZER_FUNCTIONS_GROUP(ed25519)
|
||||
MAKE_SERIALIZER_FUNCTIONS_GROUP(ed448)
|
||||
@@ -31,11 +31,6 @@ struct pkcs8_encrypt_ctx_st {
|
||||
void *cbarg;
|
||||
};
|
||||
|
||||
typedef enum {
|
||||
ECX_KEY_TYPE_X25519,
|
||||
ECX_KEY_TYPE_X448
|
||||
} ECX_KEY_TYPE;
|
||||
|
||||
OSSL_OP_keymgmt_new_fn *ossl_prov_get_keymgmt_new(const OSSL_DISPATCH *fns);
|
||||
OSSL_OP_keymgmt_free_fn *ossl_prov_get_keymgmt_free(const OSSL_DISPATCH *fns);
|
||||
OSSL_OP_keymgmt_import_fn *ossl_prov_get_keymgmt_import(const OSSL_DISPATCH *fns);
|
||||
@@ -64,12 +59,14 @@ int ossl_prov_prepare_dh_params(const void *dh, int nid,
|
||||
int ossl_prov_dh_pub_to_der(const void *dh, unsigned char **pder);
|
||||
int ossl_prov_dh_priv_to_der(const void *dh, unsigned char **pder);
|
||||
|
||||
#ifndef OPENSSL_NO_EC
|
||||
void ecx_get_new_free_import(ECX_KEY_TYPE type,
|
||||
OSSL_OP_keymgmt_new_fn **ecx_new,
|
||||
OSSL_OP_keymgmt_free_fn **ecx_free,
|
||||
OSSL_OP_keymgmt_import_fn **ecx_import);
|
||||
int ossl_prov_ecx_pub_to_der(const void *ecxkey, unsigned char **pder);
|
||||
int ossl_prov_ecx_priv_to_der(const void *ecxkey, unsigned char **pder);
|
||||
#endif
|
||||
|
||||
int ossl_prov_prepare_dsa_params(const void *dsa, int nid,
|
||||
void **pstr, int *pstrtype);
|
||||
|
||||
@@ -110,4 +110,3 @@ int ossl_prov_print_rsa(BIO *out, RSA *rsa, int priv)
|
||||
sk_BIGNUM_const_free(coeffs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -2,12 +2,21 @@
|
||||
# switch each to the Legacy provider when needed.
|
||||
|
||||
$DSA_GOAL=../../libimplementations.a
|
||||
$RSA_GOAL=../../libimplementations.a
|
||||
$EC_GOAL=../../libimplementations.a
|
||||
$ECDSA_GOAL=../../libimplementations.a
|
||||
|
||||
IF[{- !$disabled{dsa} -}]
|
||||
SOURCE[$DSA_GOAL]=dsa.c
|
||||
ENDIF
|
||||
|
||||
SOURCE[$RSA_GOAL]=rsa.c
|
||||
IF[{- !$disabled{ec} -}]
|
||||
SOURCE[$EC_GOAL]=eddsa.c
|
||||
SOURCE[$ECDSA_GOAL]=ecdsa.c
|
||||
ENDIF
|
||||
|
||||
SOURCE[../../libfips.a]=rsa.c
|
||||
SOURCE[../../libnonfips.a]=rsa.c
|
||||
|
||||
DEPEND[rsa.o]=../../common/include/prov/der_rsa.h
|
||||
DEPEND[dsa.o]=../../common/include/prov/der_dsa.h
|
||||
DEPEND[ecdsa.o]=../../common/include/prov/der_ec.h
|
||||
@@ -25,11 +25,13 @@
|
||||
#include <openssl/err.h>
|
||||
#include "internal/nelem.h"
|
||||
#include "internal/sizes.h"
|
||||
#include "internal/cryptlib.h"
|
||||
#include "prov/providercommonerr.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommonerr.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "crypto/dsa.h"
|
||||
#include "prov/der_dsa.h"
|
||||
|
||||
static OSSL_OP_signature_newctx_fn dsa_newctx;
|
||||
static OSSL_OP_signature_sign_init_fn dsa_signature_init;
|
||||
@@ -73,8 +75,9 @@ typedef struct {
|
||||
|
||||
char mdname[OSSL_MAX_NAME_SIZE];
|
||||
|
||||
/* The Algorithm Identifier of the combined signature agorithm */
|
||||
unsigned char aid[OSSL_MAX_ALGORITHM_ID_SIZE];
|
||||
/* The Algorithm Identifier of the combined signature algorithm */
|
||||
unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
|
||||
unsigned char *aid;
|
||||
size_t aid_len;
|
||||
|
||||
/* main digest */
|
||||
@@ -146,25 +149,35 @@ static int dsa_setup_md(PROV_DSA_CTX *ctx,
|
||||
if (mdname != NULL) {
|
||||
EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
|
||||
int md_nid = dsa_get_md_nid(md);
|
||||
size_t algorithmidentifier_len = 0;
|
||||
const unsigned char *algorithmidentifier;
|
||||
WPACKET pkt;
|
||||
|
||||
EVP_MD_free(ctx->md);
|
||||
ctx->md = NULL;
|
||||
ctx->mdname[0] = '\0';
|
||||
|
||||
algorithmidentifier =
|
||||
dsa_algorithmidentifier_encoding(md_nid, &algorithmidentifier_len);
|
||||
|
||||
if (algorithmidentifier == NULL) {
|
||||
if (md == NULL || md_nid == NID_undef) {
|
||||
EVP_MD_free(md);
|
||||
return 0;
|
||||
}
|
||||
|
||||
EVP_MD_CTX_free(ctx->mdctx);
|
||||
EVP_MD_free(ctx->md);
|
||||
|
||||
/*
|
||||
* TODO(3.0) Should we care about DER writing errors?
|
||||
* All it really means is that for some reason, there's no
|
||||
* AlgorithmIdentifier to be had, but the operation itself is
|
||||
* still valid, just as long as it's not used to construct
|
||||
* anything that needs an AlgorithmIdentifier.
|
||||
*/
|
||||
ctx->aid_len = 0;
|
||||
if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
|
||||
&& DER_w_algorithmIdentifier_DSA_with(&pkt, -1, ctx->dsa, md_nid)
|
||||
&& WPACKET_finish(&pkt)) {
|
||||
WPACKET_get_total_written(&pkt, &ctx->aid_len);
|
||||
ctx->aid = WPACKET_get_curr(&pkt);
|
||||
}
|
||||
WPACKET_cleanup(&pkt);
|
||||
|
||||
ctx->mdctx = NULL;
|
||||
ctx->md = md;
|
||||
OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
|
||||
memcpy(ctx->aid, algorithmidentifier, algorithmidentifier_len);
|
||||
ctx->aid_len = algorithmidentifier_len;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,527 @@
|
||||
/*
|
||||
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
/*
|
||||
* ECDSA low level APIs are deprecated for public use, but still ok for
|
||||
* internal use.
|
||||
*/
|
||||
#include "internal/deprecated.h"
|
||||
|
||||
#include <string.h> /* memcpy */
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/dsa.h>
|
||||
#include <openssl/params.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/err.h>
|
||||
#include "internal/nelem.h"
|
||||
#include "internal/sizes.h"
|
||||
#include "internal/cryptlib.h"
|
||||
#include "prov/providercommonerr.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "crypto/ec.h"
|
||||
#include "prov/der_ec.h"
|
||||
|
||||
static OSSL_OP_signature_newctx_fn ecdsa_newctx;
|
||||
static OSSL_OP_signature_sign_init_fn ecdsa_signature_init;
|
||||
static OSSL_OP_signature_verify_init_fn ecdsa_signature_init;
|
||||
static OSSL_OP_signature_sign_fn ecdsa_sign;
|
||||
static OSSL_OP_signature_verify_fn ecdsa_verify;
|
||||
static OSSL_OP_signature_digest_sign_init_fn ecdsa_digest_signverify_init;
|
||||
static OSSL_OP_signature_digest_sign_update_fn ecdsa_digest_signverify_update;
|
||||
static OSSL_OP_signature_digest_sign_final_fn ecdsa_digest_sign_final;
|
||||
static OSSL_OP_signature_digest_verify_init_fn ecdsa_digest_signverify_init;
|
||||
static OSSL_OP_signature_digest_verify_update_fn ecdsa_digest_signverify_update;
|
||||
static OSSL_OP_signature_digest_verify_final_fn ecdsa_digest_verify_final;
|
||||
static OSSL_OP_signature_freectx_fn ecdsa_freectx;
|
||||
static OSSL_OP_signature_dupctx_fn ecdsa_dupctx;
|
||||
static OSSL_OP_signature_get_ctx_params_fn ecdsa_get_ctx_params;
|
||||
static OSSL_OP_signature_gettable_ctx_params_fn ecdsa_gettable_ctx_params;
|
||||
static OSSL_OP_signature_set_ctx_params_fn ecdsa_set_ctx_params;
|
||||
static OSSL_OP_signature_settable_ctx_params_fn ecdsa_settable_ctx_params;
|
||||
static OSSL_OP_signature_get_ctx_md_params_fn ecdsa_get_ctx_md_params;
|
||||
static OSSL_OP_signature_gettable_ctx_md_params_fn ecdsa_gettable_ctx_md_params;
|
||||
static OSSL_OP_signature_set_ctx_md_params_fn ecdsa_set_ctx_md_params;
|
||||
static OSSL_OP_signature_settable_ctx_md_params_fn ecdsa_settable_ctx_md_params;
|
||||
|
||||
/*
|
||||
* What's passed as an actual key is defined by the KEYMGMT interface.
|
||||
* We happen to know that our KEYMGMT simply passes DSA structures, so
|
||||
* we use that here too.
|
||||
*/
|
||||
|
||||
typedef struct {
|
||||
OPENSSL_CTX *libctx;
|
||||
EC_KEY *ec;
|
||||
char mdname[OSSL_MAX_NAME_SIZE];
|
||||
|
||||
/* The Algorithm Identifier of the combined signature algorithm */
|
||||
unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
|
||||
unsigned char *aid;
|
||||
size_t aid_len;
|
||||
size_t mdsize;
|
||||
|
||||
EVP_MD *md;
|
||||
EVP_MD_CTX *mdctx;
|
||||
/*
|
||||
* This indicates that KAT (CAVS) test is running. Externally an app will
|
||||
* override the random callback such that the generated private key and k
|
||||
* are known.
|
||||
* Normal operation will loop to choose a new k if the signature is not
|
||||
* valid - but for this mode of operation it forces a failure instead.
|
||||
*/
|
||||
unsigned int kattest;
|
||||
/*
|
||||
* Internally used to cache the results of calling the EC group
|
||||
* sign_setup() methods which are then passed to the sign operation.
|
||||
* This is used by CAVS failure tests to terminate a loop if the signature
|
||||
* is not valid.
|
||||
* This could of also been done with a simple flag.
|
||||
*/
|
||||
BIGNUM *kinv;
|
||||
BIGNUM *r;
|
||||
} PROV_ECDSA_CTX;
|
||||
|
||||
static void *ecdsa_newctx(void *provctx)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = OPENSSL_zalloc(sizeof(PROV_ECDSA_CTX));
|
||||
|
||||
if (ctx == NULL)
|
||||
return NULL;
|
||||
|
||||
ctx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static int ecdsa_signature_init(void *vctx, void *ec)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
|
||||
if (ctx == NULL || ec == NULL || !EC_KEY_up_ref(ec))
|
||||
return 0;
|
||||
EC_KEY_free(ctx->ec);
|
||||
ctx->ec = ec;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ecdsa_sign(void *vctx, unsigned char *sig, size_t *siglen,
|
||||
size_t sigsize, const unsigned char *tbs, size_t tbslen)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
int ret;
|
||||
unsigned int sltmp;
|
||||
size_t ecsize = ECDSA_size(ctx->ec);
|
||||
|
||||
if (sig == NULL) {
|
||||
*siglen = ecsize;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (ctx->kattest && !ECDSA_sign_setup(ctx->ec, NULL, &ctx->kinv, &ctx->r))
|
||||
return 0;
|
||||
|
||||
if (sigsize < (size_t)ecsize)
|
||||
return 0;
|
||||
|
||||
if (ctx->mdsize != 0 && tbslen != ctx->mdsize)
|
||||
return 0;
|
||||
|
||||
ret = ECDSA_sign_ex(0, tbs, tbslen, sig, &sltmp, ctx->kinv, ctx->r, ctx->ec);
|
||||
if (ret <= 0)
|
||||
return 0;
|
||||
|
||||
*siglen = sltmp;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ecdsa_verify(void *vctx, const unsigned char *sig, size_t siglen,
|
||||
const unsigned char *tbs, size_t tbslen)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
|
||||
if (ctx->mdsize != 0 && tbslen != ctx->mdsize)
|
||||
return 0;
|
||||
|
||||
return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->ec);
|
||||
}
|
||||
|
||||
static int get_md_nid(const EVP_MD *md)
|
||||
{
|
||||
/*
|
||||
* Because the ECDSA library deals with NIDs, we need to translate.
|
||||
* We do so using EVP_MD_is_a(), and therefore need a name to NID
|
||||
* map.
|
||||
*/
|
||||
static const OSSL_ITEM name_to_nid[] = {
|
||||
{ NID_sha1, OSSL_DIGEST_NAME_SHA1 },
|
||||
{ NID_sha224, OSSL_DIGEST_NAME_SHA2_224 },
|
||||
{ NID_sha256, OSSL_DIGEST_NAME_SHA2_256 },
|
||||
{ NID_sha384, OSSL_DIGEST_NAME_SHA2_384 },
|
||||
{ NID_sha512, OSSL_DIGEST_NAME_SHA2_512 },
|
||||
{ NID_sha3_224, OSSL_DIGEST_NAME_SHA3_224 },
|
||||
{ NID_sha3_256, OSSL_DIGEST_NAME_SHA3_256 },
|
||||
{ NID_sha3_384, OSSL_DIGEST_NAME_SHA3_384 },
|
||||
{ NID_sha3_512, OSSL_DIGEST_NAME_SHA3_512 },
|
||||
/* TODO - Add SHAKE OIDS when they are standardized */
|
||||
|
||||
};
|
||||
size_t i;
|
||||
int mdnid = NID_undef;
|
||||
|
||||
if (md == NULL)
|
||||
goto end;
|
||||
|
||||
for (i = 0; i < OSSL_NELEM(name_to_nid); i++) {
|
||||
if (EVP_MD_is_a(md, name_to_nid[i].ptr)) {
|
||||
mdnid = (int)name_to_nid[i].id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (mdnid == NID_undef)
|
||||
ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
|
||||
|
||||
end:
|
||||
return mdnid;
|
||||
}
|
||||
|
||||
static void free_md(PROV_ECDSA_CTX *ctx)
|
||||
{
|
||||
EVP_MD_CTX_free(ctx->mdctx);
|
||||
EVP_MD_free(ctx->md);
|
||||
ctx->mdctx = NULL;
|
||||
ctx->md = NULL;
|
||||
ctx->mdsize = 0;
|
||||
}
|
||||
|
||||
static int ecdsa_digest_signverify_init(void *vctx, const char *mdname,
|
||||
const char *props, void *ec)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
int md_nid = NID_undef;
|
||||
WPACKET pkt;
|
||||
|
||||
free_md(ctx);
|
||||
|
||||
if (!ecdsa_signature_init(vctx, ec))
|
||||
return 0;
|
||||
|
||||
ctx->md = EVP_MD_fetch(ctx->libctx, mdname, props);
|
||||
if ((md_nid = get_md_nid(ctx->md)) == NID_undef)
|
||||
goto error;
|
||||
|
||||
ctx->mdsize = EVP_MD_size(ctx->md);
|
||||
ctx->mdctx = EVP_MD_CTX_new();
|
||||
if (ctx->mdctx == NULL)
|
||||
goto error;
|
||||
|
||||
/*
|
||||
* TODO(3.0) Should we care about DER writing errors?
|
||||
* All it really means is that for some reason, there's no
|
||||
* AlgorithmIdentifier to be had, but the operation itself is
|
||||
* still valid, just as long as it's not used to construct
|
||||
* anything that needs an AlgorithmIdentifier.
|
||||
*/
|
||||
ctx->aid_len = 0;
|
||||
if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
|
||||
&& DER_w_algorithmIdentifier_ECDSA_with(&pkt, -1, ctx->ec, md_nid)
|
||||
&& WPACKET_finish(&pkt)) {
|
||||
WPACKET_get_total_written(&pkt, &ctx->aid_len);
|
||||
ctx->aid = WPACKET_get_curr(&pkt);
|
||||
}
|
||||
WPACKET_cleanup(&pkt);
|
||||
|
||||
if (!EVP_DigestInit_ex(ctx->mdctx, ctx->md, NULL))
|
||||
goto error;
|
||||
return 1;
|
||||
error:
|
||||
free_md(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ecdsa_digest_signverify_update(void *vctx, const unsigned char *data,
|
||||
size_t datalen)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
|
||||
if (ctx == NULL || ctx->mdctx == NULL)
|
||||
return 0;
|
||||
|
||||
return EVP_DigestUpdate(ctx->mdctx, data, datalen);
|
||||
}
|
||||
|
||||
int ecdsa_digest_sign_final(void *vctx, unsigned char *sig, size_t *siglen,
|
||||
size_t sigsize)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
unsigned char digest[EVP_MAX_MD_SIZE];
|
||||
unsigned int dlen = 0;
|
||||
|
||||
if (ctx == NULL || ctx->mdctx == NULL)
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* If sig is NULL then we're just finding out the sig size. Other fields
|
||||
* are ignored. Defer to ecdsa_sign.
|
||||
*/
|
||||
if (sig != NULL) {
|
||||
/*
|
||||
* TODO(3.0): There is the possibility that some externally provided
|
||||
* digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
|
||||
* but that problem is much larger than just in DSA.
|
||||
*/
|
||||
if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen))
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ecdsa_sign(vctx, sig, siglen, sigsize, digest, (size_t)dlen);
|
||||
}
|
||||
|
||||
int ecdsa_digest_verify_final(void *vctx, const unsigned char *sig,
|
||||
size_t siglen)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
unsigned char digest[EVP_MAX_MD_SIZE];
|
||||
unsigned int dlen = 0;
|
||||
|
||||
if (ctx == NULL || ctx->mdctx == NULL)
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* TODO(3.0): There is the possibility that some externally provided
|
||||
* digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
|
||||
* but that problem is much larger than just in DSA.
|
||||
*/
|
||||
if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen))
|
||||
return 0;
|
||||
|
||||
return ecdsa_verify(ctx, sig, siglen, digest, (size_t)dlen);
|
||||
}
|
||||
|
||||
static void ecdsa_freectx(void *vctx)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
|
||||
free_md(ctx);
|
||||
EC_KEY_free(ctx->ec);
|
||||
BN_clear_free(ctx->kinv);
|
||||
BN_clear_free(ctx->r);
|
||||
OPENSSL_free(ctx);
|
||||
}
|
||||
|
||||
static void *ecdsa_dupctx(void *vctx)
|
||||
{
|
||||
PROV_ECDSA_CTX *srcctx = (PROV_ECDSA_CTX *)vctx;
|
||||
PROV_ECDSA_CTX *dstctx;
|
||||
|
||||
dstctx = OPENSSL_zalloc(sizeof(*srcctx));
|
||||
if (dstctx == NULL)
|
||||
return NULL;
|
||||
|
||||
*dstctx = *srcctx;
|
||||
dstctx->ec = NULL;
|
||||
dstctx->md = NULL;
|
||||
dstctx->mdctx = NULL;
|
||||
|
||||
if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec))
|
||||
goto err;
|
||||
/* Test KATS should not need to be supported */
|
||||
if (srcctx->kinv != NULL || srcctx->r != NULL)
|
||||
goto err;
|
||||
dstctx->ec = srcctx->ec;
|
||||
|
||||
if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
|
||||
goto err;
|
||||
dstctx->md = srcctx->md;
|
||||
|
||||
if (srcctx->mdctx != NULL) {
|
||||
dstctx->mdctx = EVP_MD_CTX_new();
|
||||
if (dstctx->mdctx == NULL
|
||||
|| !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
|
||||
goto err;
|
||||
}
|
||||
|
||||
return dstctx;
|
||||
err:
|
||||
ecdsa_freectx(dstctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int ecdsa_get_ctx_params(void *vctx, OSSL_PARAM *params)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
OSSL_PARAM *p;
|
||||
|
||||
if (ctx == NULL || params == NULL)
|
||||
return 0;
|
||||
|
||||
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
|
||||
if (p != NULL && !OSSL_PARAM_set_octet_string(p, ctx->aid, ctx->aid_len))
|
||||
return 0;
|
||||
|
||||
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
|
||||
if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->mdsize))
|
||||
return 0;
|
||||
|
||||
p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
|
||||
if (p != NULL && !OSSL_PARAM_set_utf8_string(p, ctx->md == NULL
|
||||
? ctx->mdname
|
||||
: EVP_MD_name(ctx->md)))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM known_gettable_ctx_params[] = {
|
||||
OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
|
||||
OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
|
||||
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
static const OSSL_PARAM *ecdsa_gettable_ctx_params(void)
|
||||
{
|
||||
return known_gettable_ctx_params;
|
||||
}
|
||||
|
||||
static int ecdsa_set_ctx_params(void *vctx, const OSSL_PARAM params[])
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
const OSSL_PARAM *p;
|
||||
char *mdname;
|
||||
|
||||
if (ctx == NULL || params == NULL)
|
||||
return 0;
|
||||
|
||||
if (ctx->md != NULL) {
|
||||
/*
|
||||
* You cannot set the digest name/size when doing a DigestSign or
|
||||
* DigestVerify.
|
||||
*/
|
||||
return 1;
|
||||
}
|
||||
|
||||
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_KAT);
|
||||
if (p != NULL && !OSSL_PARAM_get_uint(p, &ctx->kattest))
|
||||
return 0;
|
||||
|
||||
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE);
|
||||
if (p != NULL && !OSSL_PARAM_get_size_t(p, &ctx->mdsize))
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* We never actually use the mdname, but we do support getting it later.
|
||||
* This can be useful for applications that want to know the MD that they
|
||||
* previously set.
|
||||
*/
|
||||
p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
|
||||
mdname = ctx->mdname;
|
||||
if (p != NULL
|
||||
&& !OSSL_PARAM_get_utf8_string(p, &mdname, sizeof(ctx->mdname)))
|
||||
return 0;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const OSSL_PARAM known_settable_ctx_params[] = {
|
||||
OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL),
|
||||
OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
|
||||
OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL),
|
||||
OSSL_PARAM_END
|
||||
};
|
||||
|
||||
static const OSSL_PARAM *ecdsa_settable_ctx_params(void)
|
||||
{
|
||||
/*
|
||||
* TODO(3.0): Should this function return a different set of settable ctx
|
||||
* params if the ctx is being used for a DigestSign/DigestVerify? In that
|
||||
* case it is not allowed to set the digest size/digest name because the
|
||||
* digest is explicitly set as part of the init.
|
||||
*/
|
||||
return known_settable_ctx_params;
|
||||
}
|
||||
|
||||
static int ecdsa_get_ctx_md_params(void *vctx, OSSL_PARAM *params)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
|
||||
if (ctx->mdctx == NULL)
|
||||
return 0;
|
||||
|
||||
return EVP_MD_CTX_get_params(ctx->mdctx, params);
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *ecdsa_gettable_ctx_md_params(void *vctx)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
|
||||
if (ctx->md == NULL)
|
||||
return 0;
|
||||
|
||||
return EVP_MD_gettable_ctx_params(ctx->md);
|
||||
}
|
||||
|
||||
static int ecdsa_set_ctx_md_params(void *vctx, const OSSL_PARAM params[])
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
|
||||
if (ctx->mdctx == NULL)
|
||||
return 0;
|
||||
|
||||
return EVP_MD_CTX_set_params(ctx->mdctx, params);
|
||||
}
|
||||
|
||||
static const OSSL_PARAM *ecdsa_settable_ctx_md_params(void *vctx)
|
||||
{
|
||||
PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx;
|
||||
|
||||
if (ctx->md == NULL)
|
||||
return 0;
|
||||
|
||||
return EVP_MD_settable_ctx_params(ctx->md);
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH ecdsa_signature_functions[] = {
|
||||
{ OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))ecdsa_newctx },
|
||||
{ OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))ecdsa_signature_init },
|
||||
{ OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))ecdsa_sign },
|
||||
{ OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))ecdsa_signature_init },
|
||||
{ OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))ecdsa_verify },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
|
||||
(void (*)(void))ecdsa_digest_signverify_init },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
|
||||
(void (*)(void))ecdsa_digest_signverify_update },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
|
||||
(void (*)(void))ecdsa_digest_sign_final },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
|
||||
(void (*)(void))ecdsa_digest_signverify_init },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
|
||||
(void (*)(void))ecdsa_digest_signverify_update },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
|
||||
(void (*)(void))ecdsa_digest_verify_final },
|
||||
{ OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))ecdsa_freectx },
|
||||
{ OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))ecdsa_dupctx },
|
||||
{ OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))ecdsa_get_ctx_params },
|
||||
{ OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))ecdsa_gettable_ctx_params },
|
||||
{ OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))ecdsa_set_ctx_params },
|
||||
{ OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
|
||||
(void (*)(void))ecdsa_settable_ctx_params },
|
||||
{ OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
|
||||
(void (*)(void))ecdsa_get_ctx_md_params },
|
||||
{ OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
|
||||
(void (*)(void))ecdsa_gettable_ctx_md_params },
|
||||
{ OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
|
||||
(void (*)(void))ecdsa_set_ctx_md_params },
|
||||
{ OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
|
||||
(void (*)(void))ecdsa_settable_ctx_md_params },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -0,0 +1,213 @@
|
||||
/*
|
||||
* Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#include <openssl/crypto.h>
|
||||
#include <openssl/core_numbers.h>
|
||||
#include <openssl/core_names.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/params.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/err.h>
|
||||
#include "internal/nelem.h"
|
||||
#include "internal/sizes.h"
|
||||
#include "prov/providercommonerr.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/providercommonerr.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "crypto/ecx.h"
|
||||
|
||||
static OSSL_OP_signature_newctx_fn eddsa_newctx;
|
||||
static OSSL_OP_signature_digest_sign_init_fn eddsa_digest_signverify_init;
|
||||
static OSSL_OP_signature_digest_sign_fn ed25519_digest_sign;
|
||||
static OSSL_OP_signature_digest_sign_fn ed448_digest_sign;
|
||||
static OSSL_OP_signature_digest_verify_fn ed25519_digest_verify;
|
||||
static OSSL_OP_signature_digest_verify_fn ed448_digest_verify;
|
||||
static OSSL_OP_signature_freectx_fn eddsa_freectx;
|
||||
static OSSL_OP_signature_dupctx_fn eddsa_dupctx;
|
||||
|
||||
typedef struct {
|
||||
OPENSSL_CTX *libctx;
|
||||
ECX_KEY *key;
|
||||
} PROV_EDDSA_CTX;
|
||||
|
||||
static void *eddsa_newctx(void *provctx)
|
||||
{
|
||||
PROV_EDDSA_CTX *peddsactx = OPENSSL_zalloc(sizeof(PROV_EDDSA_CTX));
|
||||
|
||||
if (peddsactx == NULL) {
|
||||
PROVerr(0, ERR_R_MALLOC_FAILURE);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
peddsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
|
||||
|
||||
return peddsactx;
|
||||
}
|
||||
|
||||
static int eddsa_digest_signverify_init(void *vpeddsactx, const char *mdname,
|
||||
const char *props, void *vedkey)
|
||||
{
|
||||
PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
|
||||
ECX_KEY *edkey = (ECX_KEY *)vedkey;
|
||||
|
||||
if (mdname != NULL) {
|
||||
PROVerr(0, PROV_R_INVALID_DIGEST);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!ecx_key_up_ref(edkey)) {
|
||||
PROVerr(0, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
|
||||
peddsactx->key = edkey;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ed25519_digest_sign(void *vpeddsactx, unsigned char *sigret,
|
||||
size_t *siglen, size_t sigsize,
|
||||
const unsigned char *tbs, size_t tbslen)
|
||||
{
|
||||
PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
|
||||
const ECX_KEY *edkey = peddsactx->key;
|
||||
|
||||
if (sigret == NULL) {
|
||||
*siglen = ED25519_SIGSIZE;
|
||||
return 1;
|
||||
}
|
||||
if (sigsize < ED25519_SIGSIZE) {
|
||||
PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ED25519_sign(sigret, tbs, tbslen, edkey->pubkey, edkey->privkey,
|
||||
peddsactx->libctx, NULL) == 0) {
|
||||
PROVerr(0, PROV_R_FAILED_TO_SIGN);
|
||||
return 0;
|
||||
}
|
||||
*siglen = ED25519_SIGSIZE;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ed448_digest_sign(void *vpeddsactx, unsigned char *sigret,
|
||||
size_t *siglen, size_t sigsize,
|
||||
const unsigned char *tbs, size_t tbslen)
|
||||
{
|
||||
PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
|
||||
const ECX_KEY *edkey = peddsactx->key;
|
||||
|
||||
if (sigret == NULL) {
|
||||
*siglen = ED448_SIGSIZE;
|
||||
return 1;
|
||||
}
|
||||
if (sigsize < ED448_SIGSIZE) {
|
||||
PROVerr(0, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ED448_sign(peddsactx->libctx, sigret, tbs, tbslen, edkey->pubkey,
|
||||
edkey->privkey, NULL, 0) == 0) {
|
||||
PROVerr(0, PROV_R_FAILED_TO_SIGN);
|
||||
return 0;
|
||||
}
|
||||
*siglen = ED448_SIGSIZE;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ed25519_digest_verify(void *vpeddsactx, const unsigned char *sig,
|
||||
size_t siglen, const unsigned char *tbs,
|
||||
size_t tbslen)
|
||||
{
|
||||
PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
|
||||
const ECX_KEY *edkey = peddsactx->key;
|
||||
|
||||
if (siglen != ED25519_SIGSIZE)
|
||||
return 0;
|
||||
|
||||
return ED25519_verify(tbs, tbslen, sig, edkey->pubkey, peddsactx->libctx,
|
||||
NULL);
|
||||
}
|
||||
|
||||
int ed448_digest_verify(void *vpeddsactx, const unsigned char *sig,
|
||||
size_t siglen, const unsigned char *tbs,
|
||||
size_t tbslen)
|
||||
{
|
||||
PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
|
||||
const ECX_KEY *edkey = peddsactx->key;
|
||||
|
||||
if (siglen != ED448_SIGSIZE)
|
||||
return 0;
|
||||
|
||||
return ED448_verify(peddsactx->libctx, tbs, tbslen, sig, edkey->pubkey,
|
||||
NULL, 0);
|
||||
}
|
||||
|
||||
static void eddsa_freectx(void *vpeddsactx)
|
||||
{
|
||||
PROV_EDDSA_CTX *peddsactx = (PROV_EDDSA_CTX *)vpeddsactx;
|
||||
|
||||
ecx_key_free(peddsactx->key);
|
||||
|
||||
OPENSSL_free(peddsactx);
|
||||
}
|
||||
|
||||
static void *eddsa_dupctx(void *vpeddsactx)
|
||||
{
|
||||
PROV_EDDSA_CTX *srcctx = (PROV_EDDSA_CTX *)vpeddsactx;
|
||||
PROV_EDDSA_CTX *dstctx;
|
||||
|
||||
dstctx = OPENSSL_zalloc(sizeof(*srcctx));
|
||||
if (dstctx == NULL)
|
||||
return NULL;
|
||||
|
||||
*dstctx = *srcctx;
|
||||
dstctx->key = NULL;
|
||||
|
||||
if (srcctx->key != NULL && !ecx_key_up_ref(srcctx->key)) {
|
||||
PROVerr(0, ERR_R_INTERNAL_ERROR);
|
||||
goto err;
|
||||
}
|
||||
dstctx->key = srcctx->key;
|
||||
|
||||
return dstctx;
|
||||
err:
|
||||
eddsa_freectx(dstctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const OSSL_DISPATCH ed25519_signature_functions[] = {
|
||||
{ OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))eddsa_newctx },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
|
||||
(void (*)(void))eddsa_digest_signverify_init },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN,
|
||||
(void (*)(void))ed25519_digest_sign },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
|
||||
(void (*)(void))eddsa_digest_signverify_init },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,
|
||||
(void (*)(void))ed25519_digest_verify },
|
||||
{ OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))eddsa_freectx },
|
||||
{ OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))eddsa_dupctx },
|
||||
{ 0, NULL }
|
||||
};
|
||||
|
||||
const OSSL_DISPATCH ed448_signature_functions[] = {
|
||||
{ OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))eddsa_newctx },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
|
||||
(void (*)(void))eddsa_digest_signverify_init },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_SIGN,
|
||||
(void (*)(void))ed448_digest_sign },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
|
||||
(void (*)(void))eddsa_digest_signverify_init },
|
||||
{ OSSL_FUNC_SIGNATURE_DIGEST_VERIFY,
|
||||
(void (*)(void))ed448_digest_verify },
|
||||
{ OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))eddsa_freectx },
|
||||
{ OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))eddsa_dupctx },
|
||||
{ 0, NULL }
|
||||
};
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "prov/providercommonerr.h"
|
||||
#include "prov/implementations.h"
|
||||
#include "prov/provider_ctx.h"
|
||||
#include "prov/der_rsa.h"
|
||||
|
||||
static OSSL_OP_signature_newctx_fn rsa_newctx;
|
||||
static OSSL_OP_signature_sign_init_fn rsa_signature_init;
|
||||
@@ -83,7 +84,8 @@ typedef struct {
|
||||
unsigned int flag_allow_md : 1;
|
||||
|
||||
/* The Algorithm Identifier of the combined signature agorithm */
|
||||
unsigned char aid[128];
|
||||
unsigned char aid_buf[128];
|
||||
unsigned char *aid;
|
||||
size_t aid_len;
|
||||
|
||||
/* main digest */
|
||||
@@ -216,35 +218,38 @@ static int rsa_setup_md(PROV_RSA_CTX *ctx, const char *mdname,
|
||||
if (mdname != NULL) {
|
||||
EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
|
||||
int md_nid = rsa_get_md_nid(md);
|
||||
size_t algorithmidentifier_len = 0;
|
||||
const unsigned char *algorithmidentifier = NULL;
|
||||
WPACKET pkt;
|
||||
|
||||
if (md == NULL)
|
||||
return 0;
|
||||
|
||||
if (!rsa_check_padding(md_nid, ctx->pad_mode)) {
|
||||
if (md == NULL
|
||||
|| md_nid == NID_undef
|
||||
|| !rsa_check_padding(md_nid, ctx->pad_mode)) {
|
||||
EVP_MD_free(md);
|
||||
return 0;
|
||||
}
|
||||
|
||||
EVP_MD_CTX_free(ctx->mdctx);
|
||||
EVP_MD_free(ctx->md);
|
||||
ctx->md = NULL;
|
||||
ctx->mdctx = NULL;
|
||||
ctx->mdname[0] = '\0';
|
||||
ctx->aid[0] = '\0';
|
||||
|
||||
/*
|
||||
* TODO(3.0) Should we care about DER writing errors?
|
||||
* All it really means is that for some reason, there's no
|
||||
* AlgorithmIdentifier to be had (consider RSA with MD5-SHA1),
|
||||
* but the operation itself is still valid, just as long as it's
|
||||
* not used to construct anything that needs an AlgorithmIdentifier.
|
||||
*/
|
||||
ctx->aid_len = 0;
|
||||
if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
|
||||
&& DER_w_algorithmIdentifier_RSA_with(&pkt, -1, ctx->rsa, md_nid)
|
||||
&& WPACKET_finish(&pkt)) {
|
||||
WPACKET_get_total_written(&pkt, &ctx->aid_len);
|
||||
ctx->aid = WPACKET_get_curr(&pkt);
|
||||
}
|
||||
WPACKET_cleanup(&pkt);
|
||||
|
||||
algorithmidentifier =
|
||||
rsa_algorithmidentifier_encoding(md_nid, &algorithmidentifier_len);
|
||||
|
||||
ctx->mdctx = NULL;
|
||||
ctx->md = md;
|
||||
ctx->mdnid = md_nid;
|
||||
OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
|
||||
if (algorithmidentifier != NULL) {
|
||||
memcpy(ctx->aid, algorithmidentifier, algorithmidentifier_len);
|
||||
ctx->aid_len = algorithmidentifier_len;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
@@ -328,7 +333,6 @@ static int rsa_sign(void *vprsactx, unsigned char *sig, size_t *siglen,
|
||||
goto end;
|
||||
}
|
||||
#endif
|
||||
|
||||
switch (prsactx->pad_mode) {
|
||||
case RSA_X931_PADDING:
|
||||
if ((size_t)RSA_size(prsactx->rsa) < tbslen + 1) {
|
||||
|
||||
Reference in New Issue
Block a user