sync code with last fixes and improvements from OpenBSD
This commit is contained in:
parent
691f97cc10
commit
371ae113c6
175 changed files with 2932 additions and 1512 deletions
|
@ -1,4 +1,4 @@
|
|||
/* $OpenBSD: bn.h,v 1.74 2023/07/28 10:07:30 tb Exp $ */
|
||||
/* $OpenBSD: bn.h,v 1.75 2023/07/31 05:04:06 tb Exp $ */
|
||||
/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
|
@ -318,7 +318,6 @@ void BN_zero(BIGNUM *a);
|
|||
int BN_one(BIGNUM *a);
|
||||
|
||||
const BIGNUM *BN_value_one(void);
|
||||
char * BN_options(void);
|
||||
BN_CTX *BN_CTX_new(void);
|
||||
void BN_CTX_free(BN_CTX *c);
|
||||
void BN_CTX_start(BN_CTX *ctx);
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $OpenBSD: bn_blind.c,v 1.24 2023/07/28 10:05:16 tb Exp $ */
|
||||
/* $OpenBSD: bn_blind.c,v 1.32 2023/08/02 09:25:36 tb Exp $ */
|
||||
/* ====================================================================
|
||||
* Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
|
||||
*
|
||||
|
@ -123,27 +123,25 @@ struct bn_blinding_st {
|
|||
BIGNUM *A;
|
||||
BIGNUM *Ai;
|
||||
BIGNUM *e;
|
||||
BIGNUM *mod; /* just a reference */
|
||||
BIGNUM *mod;
|
||||
CRYPTO_THREADID tid;
|
||||
int counter;
|
||||
unsigned long flags;
|
||||
BN_MONT_CTX *m_ctx;
|
||||
int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
|
||||
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
|
||||
};
|
||||
|
||||
BN_BLINDING *
|
||||
static BN_BLINDING *
|
||||
BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
|
||||
{
|
||||
BN_BLINDING *ret = NULL;
|
||||
|
||||
|
||||
if ((ret = calloc(1, sizeof(BN_BLINDING))) == NULL) {
|
||||
BNerror(ERR_R_MALLOC_FAILURE);
|
||||
return (NULL);
|
||||
return NULL;
|
||||
}
|
||||
if (A != NULL) {
|
||||
if ((ret->A = BN_dup(A)) == NULL)
|
||||
if ((ret->A = BN_dup(A)) == NULL)
|
||||
goto err;
|
||||
}
|
||||
if (Ai != NULL) {
|
||||
|
@ -164,10 +162,11 @@ BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod)
|
|||
CRYPTO_THREADID_current(&ret->tid);
|
||||
return (ret);
|
||||
|
||||
err:
|
||||
err:
|
||||
if (ret != NULL)
|
||||
BN_BLINDING_free(ret);
|
||||
return (NULL);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -183,12 +182,12 @@ BN_BLINDING_free(BN_BLINDING *r)
|
|||
free(r);
|
||||
}
|
||||
|
||||
int
|
||||
static int
|
||||
BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if ((b->A == NULL) || (b->Ai == NULL)) {
|
||||
if (b->A == NULL || b->Ai == NULL) {
|
||||
BNerror(BN_R_NOT_INITIALIZED);
|
||||
goto err;
|
||||
}
|
||||
|
@ -196,12 +195,11 @@ BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx)
|
|||
if (b->counter == -1)
|
||||
b->counter = 0;
|
||||
|
||||
if (++b->counter == BN_BLINDING_COUNTER && b->e != NULL &&
|
||||
!(b->flags & BN_BLINDING_NO_RECREATE)) {
|
||||
if (++b->counter == BN_BLINDING_COUNTER && b->e != NULL) {
|
||||
/* re-create blinding parameters */
|
||||
if (!BN_BLINDING_create_param(b, NULL, NULL, ctx, NULL, NULL))
|
||||
goto err;
|
||||
} else if (!(b->flags & BN_BLINDING_NO_UPDATE)) {
|
||||
} else {
|
||||
if (!BN_mod_mul(b->A, b->A, b->A, b->mod, ctx))
|
||||
goto err;
|
||||
if (!BN_mod_mul(b->Ai, b->Ai, b->Ai, b->mod, ctx))
|
||||
|
@ -210,58 +208,45 @@ BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx)
|
|||
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
err:
|
||||
if (b->counter == BN_BLINDING_COUNTER)
|
||||
b->counter = 0;
|
||||
return (ret);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
|
||||
{
|
||||
return BN_BLINDING_convert_ex(n, NULL, b, ctx);
|
||||
}
|
||||
|
||||
int
|
||||
BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
|
||||
BN_BLINDING_convert(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
|
||||
{
|
||||
int ret = 1;
|
||||
|
||||
|
||||
if ((b->A == NULL) || (b->Ai == NULL)) {
|
||||
if (b->A == NULL || b->Ai == NULL) {
|
||||
BNerror(BN_R_NOT_INITIALIZED);
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (b->counter == -1)
|
||||
/* Fresh blinding, doesn't need updating. */
|
||||
b->counter = 0;
|
||||
else if (!BN_BLINDING_update(b, ctx))
|
||||
return (0);
|
||||
return 0;
|
||||
|
||||
if (r != NULL) {
|
||||
if (!bn_copy(r, b->Ai))
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
if (!BN_mod_mul(n, n,b->A, b->mod, ctx))
|
||||
if (!BN_mod_mul(n, n, b->A, b->mod, ctx))
|
||||
ret = 0;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx)
|
||||
{
|
||||
return BN_BLINDING_invert_ex(n, NULL, b, ctx);
|
||||
}
|
||||
|
||||
int
|
||||
BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
|
||||
BN_BLINDING_invert(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
|
||||
{
|
||||
int ret;
|
||||
|
||||
|
||||
if (r != NULL)
|
||||
ret = BN_mod_mul(n, n, r, b->mod, ctx);
|
||||
else {
|
||||
|
@ -272,7 +257,7 @@ BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *ctx)
|
|||
ret = BN_mod_mul(n, n, b->Ai, b->mod, ctx);
|
||||
}
|
||||
|
||||
return (ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
CRYPTO_THREADID *
|
||||
|
@ -281,25 +266,13 @@ BN_BLINDING_thread_id(BN_BLINDING *b)
|
|||
return &b->tid;
|
||||
}
|
||||
|
||||
unsigned long
|
||||
BN_BLINDING_get_flags(const BN_BLINDING *b)
|
||||
{
|
||||
return b->flags;
|
||||
}
|
||||
|
||||
void
|
||||
BN_BLINDING_set_flags(BN_BLINDING *b, unsigned long flags)
|
||||
{
|
||||
b->flags = flags;
|
||||
}
|
||||
|
||||
BN_BLINDING *
|
||||
BN_BLINDING_create_param(BN_BLINDING *b, const BIGNUM *e, BIGNUM *m,
|
||||
BN_CTX *ctx, int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
|
||||
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx), BN_MONT_CTX *m_ctx)
|
||||
BN_BLINDING_create_param(BN_BLINDING *b, const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
|
||||
int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
|
||||
const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx), BN_MONT_CTX *m_ctx)
|
||||
{
|
||||
int retry_counter = 32;
|
||||
BN_BLINDING *ret = NULL;
|
||||
int retry_counter = 32;
|
||||
|
||||
if (b == NULL)
|
||||
ret = BN_BLINDING_new(NULL, NULL, m);
|
||||
|
@ -309,7 +282,7 @@ BN_BLINDING_create_param(BN_BLINDING *b, const BIGNUM *e, BIGNUM *m,
|
|||
if (ret == NULL)
|
||||
goto err;
|
||||
|
||||
if (ret->A == NULL && (ret->A = BN_new()) == NULL)
|
||||
if (ret->A == NULL && (ret->A = BN_new()) == NULL)
|
||||
goto err;
|
||||
if (ret->Ai == NULL && (ret->Ai = BN_new()) == NULL)
|
||||
goto err;
|
||||
|
@ -355,7 +328,7 @@ BN_BLINDING_create_param(BN_BLINDING *b, const BIGNUM *e, BIGNUM *m,
|
|||
|
||||
return ret;
|
||||
|
||||
err:
|
||||
err:
|
||||
if (b == NULL && ret != NULL) {
|
||||
BN_BLINDING_free(ret);
|
||||
ret = NULL;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $OpenBSD: bn_bpsw.c,v 1.10 2023/05/10 21:05:24 tb Exp $ */
|
||||
/* $OpenBSD: bn_bpsw.c,v 1.11 2023/08/03 18:53:55 tb Exp $ */
|
||||
/*
|
||||
* Copyright (c) 2022 Martin Grenouilloux <martin.grenouilloux@lse.epita.fr>
|
||||
* Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
|
||||
|
@ -385,7 +385,7 @@ bn_miller_rabin(int *is_pseudoprime, const BIGNUM *n, BN_CTX *ctx,
|
|||
size_t rounds)
|
||||
{
|
||||
BN_MONT_CTX *mctx = NULL;
|
||||
BIGNUM *base, *k, *n_minus_one, *three;
|
||||
BIGNUM *base, *k, *n_minus_one;
|
||||
size_t i;
|
||||
int s;
|
||||
int ret = 0;
|
||||
|
@ -398,8 +398,6 @@ bn_miller_rabin(int *is_pseudoprime, const BIGNUM *n, BN_CTX *ctx,
|
|||
goto err;
|
||||
if ((n_minus_one = BN_CTX_get(ctx)) == NULL)
|
||||
goto err;
|
||||
if ((three = BN_CTX_get(ctx)) == NULL)
|
||||
goto err;
|
||||
|
||||
if (BN_is_word(n, 2) || BN_is_word(n, 3)) {
|
||||
*is_pseudoprime = 1;
|
||||
|
@ -451,11 +449,8 @@ bn_miller_rabin(int *is_pseudoprime, const BIGNUM *n, BN_CTX *ctx,
|
|||
* risk of false positives in BPSW.
|
||||
*/
|
||||
|
||||
if (!BN_set_word(three, 3))
|
||||
goto err;
|
||||
|
||||
for (i = 0; i < rounds; i++) {
|
||||
if (!bn_rand_interval(base, three, n_minus_one))
|
||||
if (!bn_rand_interval(base, 3, n_minus_one))
|
||||
goto err;
|
||||
|
||||
if (!bn_fermat(is_pseudoprime, n, n_minus_one, k, s, base, ctx,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $OpenBSD: bn_local.h,v 1.27 2023/07/28 10:05:16 tb Exp $ */
|
||||
/* $OpenBSD: bn_local.h,v 1.33 2023/08/03 18:53:55 tb Exp $ */
|
||||
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
|
@ -274,7 +274,8 @@ void bn_div_rem_words(BN_ULONG h, BN_ULONG l, BN_ULONG d, BN_ULONG *out_q,
|
|||
BN_ULONG *out_r);
|
||||
|
||||
int BN_bntest_rand(BIGNUM *rnd, int bits, int top, int bottom);
|
||||
int bn_rand_interval(BIGNUM *rnd, const BIGNUM *lower_inc, const BIGNUM *upper_exc);
|
||||
int bn_rand_in_range(BIGNUM *rnd, const BIGNUM *lower_inc, const BIGNUM *upper_exc);
|
||||
int bn_rand_interval(BIGNUM *rnd, BN_ULONG lower_word, const BIGNUM *upper_exc);
|
||||
|
||||
void BN_init(BIGNUM *);
|
||||
|
||||
|
@ -291,21 +292,11 @@ int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
|
|||
int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
|
||||
BN_RECP_CTX *recp, BN_CTX *ctx);
|
||||
|
||||
/* BN_BLINDING flags */
|
||||
#define BN_BLINDING_NO_UPDATE 0x00000001
|
||||
#define BN_BLINDING_NO_RECREATE 0x00000002
|
||||
|
||||
BN_BLINDING *BN_BLINDING_new(const BIGNUM *A, const BIGNUM *Ai, BIGNUM *mod);
|
||||
void BN_BLINDING_free(BN_BLINDING *b);
|
||||
int BN_BLINDING_update(BN_BLINDING *b, BN_CTX *ctx);
|
||||
int BN_BLINDING_convert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
|
||||
int BN_BLINDING_invert(BIGNUM *n, BN_BLINDING *b, BN_CTX *ctx);
|
||||
int BN_BLINDING_convert_ex(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *);
|
||||
int BN_BLINDING_invert_ex(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *);
|
||||
int BN_BLINDING_convert(BIGNUM *n, BIGNUM *r, BN_BLINDING *b, BN_CTX *);
|
||||
int BN_BLINDING_invert(BIGNUM *n, const BIGNUM *r, BN_BLINDING *b, BN_CTX *);
|
||||
|
||||
CRYPTO_THREADID *BN_BLINDING_thread_id(BN_BLINDING *);
|
||||
unsigned long BN_BLINDING_get_flags(const BN_BLINDING *);
|
||||
void BN_BLINDING_set_flags(BN_BLINDING *, unsigned long);
|
||||
BN_BLINDING *BN_BLINDING_create_param(BN_BLINDING *b,
|
||||
const BIGNUM *e, BIGNUM *m, BN_CTX *ctx,
|
||||
int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $OpenBSD: bn_mod_sqrt.c,v 1.2 2023/07/08 12:21:58 beck Exp $ */
|
||||
/* $OpenBSD: bn_mod_sqrt.c,v 1.3 2023/08/03 18:53:55 tb Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
|
||||
|
@ -237,7 +237,7 @@ static int
|
|||
bn_mod_sqrt_find_sylow_generator(BIGNUM *out_generator, const BIGNUM *p,
|
||||
const BIGNUM *q, BN_CTX *ctx)
|
||||
{
|
||||
BIGNUM *n, *p_abs, *thirty_two;
|
||||
BIGNUM *n, *p_abs;
|
||||
int i, is_non_residue;
|
||||
int ret = 0;
|
||||
|
||||
|
@ -245,8 +245,6 @@ bn_mod_sqrt_find_sylow_generator(BIGNUM *out_generator, const BIGNUM *p,
|
|||
|
||||
if ((n = BN_CTX_get(ctx)) == NULL)
|
||||
goto err;
|
||||
if ((thirty_two = BN_CTX_get(ctx)) == NULL)
|
||||
goto err;
|
||||
if ((p_abs = BN_CTX_get(ctx)) == NULL)
|
||||
goto err;
|
||||
|
||||
|
@ -259,14 +257,12 @@ bn_mod_sqrt_find_sylow_generator(BIGNUM *out_generator, const BIGNUM *p,
|
|||
goto found;
|
||||
}
|
||||
|
||||
if (!BN_set_word(thirty_two, 32))
|
||||
goto err;
|
||||
if (!bn_copy(p_abs, p))
|
||||
goto err;
|
||||
BN_set_negative(p_abs, 0);
|
||||
|
||||
for (i = 0; i < 128; i++) {
|
||||
if (!bn_rand_interval(n, thirty_two, p_abs))
|
||||
if (!bn_rand_interval(n, 32, p_abs))
|
||||
goto err;
|
||||
if (!bn_mod_sqrt_n_is_non_residue(&is_non_residue, n, p, ctx))
|
||||
goto err;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
/* $OpenBSD: bn_rand.c,v 1.28 2023/07/08 12:21:58 beck Exp $ */
|
||||
/* $OpenBSD: bn_rand.c,v 1.29 2023/08/03 18:53:55 tb Exp $ */
|
||||
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
||||
* All rights reserved.
|
||||
*
|
||||
|
@ -284,29 +284,46 @@ BN_rand_range(BIGNUM *r, const BIGNUM *range)
|
|||
LCRYPTO_ALIAS(BN_rand_range);
|
||||
|
||||
int
|
||||
bn_rand_interval(BIGNUM *rnd, const BIGNUM *lower_inc, const BIGNUM *upper_exc)
|
||||
bn_rand_in_range(BIGNUM *rnd, const BIGNUM *lower_inc, const BIGNUM *upper_exc)
|
||||
{
|
||||
BIGNUM *len = NULL;
|
||||
BIGNUM *len;
|
||||
int ret = 0;
|
||||
|
||||
if (BN_cmp(lower_inc, upper_exc) >= 0)
|
||||
goto err;
|
||||
|
||||
if ((len = BN_new()) == NULL)
|
||||
goto err;
|
||||
|
||||
if (!BN_sub(len, upper_exc, lower_inc))
|
||||
goto err;
|
||||
|
||||
if (!bn_rand_range(0, rnd, len))
|
||||
if (!BN_rand_range(rnd, len))
|
||||
goto err;
|
||||
|
||||
if (!BN_add(rnd, rnd, lower_inc))
|
||||
goto err;
|
||||
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
BN_free(len);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int
|
||||
bn_rand_interval(BIGNUM *rnd, BN_ULONG lower_word, const BIGNUM *upper_exc)
|
||||
{
|
||||
BIGNUM *lower_inc = NULL;
|
||||
int ret = 0;
|
||||
|
||||
if ((lower_inc = BN_new()) == NULL)
|
||||
goto err;
|
||||
if (!BN_set_word(lower_inc, lower_word))
|
||||
goto err;
|
||||
if (!bn_rand_in_range(rnd, lower_inc, upper_exc))
|
||||
goto err;
|
||||
|
||||
ret = 1;
|
||||
|
||||
err:
|
||||
BN_free(lower_inc);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue