Update OpenSSL-1.1.1-pre8-dev
This commit is contained in:
+63
-7
@@ -196,7 +196,8 @@ ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
|
||||
EC_KEY *eckey)
|
||||
{
|
||||
int ok = 0, i;
|
||||
BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL;
|
||||
BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *blind = NULL;
|
||||
BIGNUM *blindm = NULL;
|
||||
const BIGNUM *order, *ckinv;
|
||||
BN_CTX *ctx = NULL;
|
||||
const EC_GROUP *group;
|
||||
@@ -229,8 +230,18 @@ ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
|
||||
}
|
||||
s = ret->s;
|
||||
|
||||
if ((ctx = BN_CTX_new()) == NULL ||
|
||||
(tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
|
||||
ctx = BN_CTX_secure_new();
|
||||
if (ctx == NULL) {
|
||||
ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
|
||||
BN_CTX_start(ctx);
|
||||
tmp = BN_CTX_get(ctx);
|
||||
m = BN_CTX_get(ctx);
|
||||
blind = BN_CTX_get(ctx);
|
||||
blindm = BN_CTX_get(ctx);
|
||||
if (blindm == NULL) {
|
||||
ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
|
||||
goto err;
|
||||
}
|
||||
@@ -270,18 +281,64 @@ ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
|
||||
}
|
||||
}
|
||||
|
||||
if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
|
||||
/*
|
||||
* The normal signature calculation is:
|
||||
*
|
||||
* s := k^-1 * (m + r * priv_key) mod order
|
||||
*
|
||||
* We will blind this to protect against side channel attacks
|
||||
*
|
||||
* s := k^-1 * blind^-1 * (blind * m + blind * r * priv_key) mod order
|
||||
*/
|
||||
|
||||
/* Generate a blinding value */
|
||||
do {
|
||||
if (!BN_priv_rand(blind, BN_num_bits(order) - 1,
|
||||
BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
|
||||
goto err;
|
||||
} while (BN_is_zero(blind));
|
||||
BN_set_flags(blind, BN_FLG_CONSTTIME);
|
||||
BN_set_flags(blindm, BN_FLG_CONSTTIME);
|
||||
BN_set_flags(tmp, BN_FLG_CONSTTIME);
|
||||
|
||||
/* tmp := blind * priv_key * r mod order */
|
||||
if (!BN_mod_mul(tmp, blind, priv_key, order, ctx)) {
|
||||
ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
if (!BN_mod_add_quick(s, tmp, m, order)) {
|
||||
if (!BN_mod_mul(tmp, tmp, ret->r, order, ctx)) {
|
||||
ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* blindm := blind * m mod order */
|
||||
if (!BN_mod_mul(blindm, blind, m, order, ctx)) {
|
||||
ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* s : = (blind * priv_key * r) + (blind * m) mod order */
|
||||
if (!BN_mod_add_quick(s, tmp, blindm, order)) {
|
||||
ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* s:= s * blind^-1 mod order */
|
||||
if (BN_mod_inverse(blind, blind, order, ctx) == NULL) {
|
||||
ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
if (!BN_mod_mul(s, s, blind, order, ctx)) {
|
||||
ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
|
||||
/* s := s * k^-1 mod order */
|
||||
if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
|
||||
ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (BN_is_zero(s)) {
|
||||
/*
|
||||
* if kinv and r have been supplied by the caller, don't
|
||||
@@ -303,9 +360,8 @@ ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
|
||||
ECDSA_SIG_free(ret);
|
||||
ret = NULL;
|
||||
}
|
||||
BN_CTX_end(ctx);
|
||||
BN_CTX_free(ctx);
|
||||
BN_clear_free(m);
|
||||
BN_clear_free(tmp);
|
||||
BN_clear_free(kinv);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -354,6 +354,47 @@ static int ecx_set_pub_key(EVP_PKEY *pkey, const unsigned char *pub, size_t len)
|
||||
KEY_OP_PUBLIC);
|
||||
}
|
||||
|
||||
static int ecx_get_priv_key(const EVP_PKEY *pkey, unsigned char *priv,
|
||||
size_t *len)
|
||||
{
|
||||
const ECX_KEY *key = pkey->pkey.ecx;
|
||||
|
||||
if (priv == NULL) {
|
||||
*len = KEYLENID(pkey->ameth->pkey_id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (key == NULL
|
||||
|| key->privkey == NULL
|
||||
|| *len < (size_t)KEYLENID(pkey->ameth->pkey_id))
|
||||
return 0;
|
||||
|
||||
*len = KEYLENID(pkey->ameth->pkey_id);
|
||||
memcpy(priv, key->privkey, *len);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ecx_get_pub_key(const EVP_PKEY *pkey, unsigned char *pub,
|
||||
size_t *len)
|
||||
{
|
||||
const ECX_KEY *key = pkey->pkey.ecx;
|
||||
|
||||
if (pub == NULL) {
|
||||
*len = KEYLENID(pkey->ameth->pkey_id);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (key == NULL
|
||||
|| *len < (size_t)KEYLENID(pkey->ameth->pkey_id))
|
||||
return 0;
|
||||
|
||||
*len = KEYLENID(pkey->ameth->pkey_id);
|
||||
memcpy(pub, key->pubkey, *len);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const EVP_PKEY_ASN1_METHOD ecx25519_asn1_meth = {
|
||||
EVP_PKEY_X25519,
|
||||
EVP_PKEY_X25519,
|
||||
@@ -393,6 +434,8 @@ const EVP_PKEY_ASN1_METHOD ecx25519_asn1_meth = {
|
||||
|
||||
ecx_set_priv_key,
|
||||
ecx_set_pub_key,
|
||||
ecx_get_priv_key,
|
||||
ecx_get_pub_key,
|
||||
};
|
||||
|
||||
const EVP_PKEY_ASN1_METHOD ecx448_asn1_meth = {
|
||||
@@ -434,6 +477,8 @@ const EVP_PKEY_ASN1_METHOD ecx448_asn1_meth = {
|
||||
|
||||
ecx_set_priv_key,
|
||||
ecx_set_pub_key,
|
||||
ecx_get_priv_key,
|
||||
ecx_get_pub_key,
|
||||
};
|
||||
|
||||
static int ecd_size25519(const EVP_PKEY *pkey)
|
||||
@@ -547,6 +592,8 @@ const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth = {
|
||||
|
||||
ecx_set_priv_key,
|
||||
ecx_set_pub_key,
|
||||
ecx_get_priv_key,
|
||||
ecx_get_pub_key,
|
||||
};
|
||||
|
||||
const EVP_PKEY_ASN1_METHOD ed448_asn1_meth = {
|
||||
@@ -587,6 +634,8 @@ const EVP_PKEY_ASN1_METHOD ed448_asn1_meth = {
|
||||
|
||||
ecx_set_priv_key,
|
||||
ecx_set_pub_key,
|
||||
ecx_get_priv_key,
|
||||
ecx_get_pub_key,
|
||||
};
|
||||
|
||||
static int pkey_ecx_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
|
||||
|
||||
Reference in New Issue
Block a user