|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
* this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
* in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include "internal/cryptlib.h"
|
|
Packit Service |
084de1 |
#include "bn_local.h"
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void BN_RECP_CTX_init(BN_RECP_CTX *recp)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
memset(recp, 0, sizeof(*recp));
|
|
Packit Service |
084de1 |
bn_init(&(recp->N));
|
|
Packit Service |
084de1 |
bn_init(&(recp->Nr));
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BN_RECP_CTX *BN_RECP_CTX_new(void)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
BN_RECP_CTX *ret;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
|
|
Packit Service |
084de1 |
BNerr(BN_F_BN_RECP_CTX_NEW, ERR_R_MALLOC_FAILURE);
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
bn_init(&(ret->N));
|
|
Packit Service |
084de1 |
bn_init(&(ret->Nr));
|
|
Packit Service |
084de1 |
ret->flags = BN_FLG_MALLOCED;
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
void BN_RECP_CTX_free(BN_RECP_CTX *recp)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
if (recp == NULL)
|
|
Packit Service |
084de1 |
return;
|
|
Packit Service |
084de1 |
BN_free(&recp->N);
|
|
Packit Service |
084de1 |
BN_free(&recp->Nr);
|
|
Packit Service |
084de1 |
if (recp->flags & BN_FLG_MALLOCED)
|
|
Packit Service |
084de1 |
OPENSSL_free(recp);
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
if (!BN_copy(&(recp->N), d))
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
BN_zero(&(recp->Nr));
|
|
Packit Service |
084de1 |
recp->num_bits = BN_num_bits(d);
|
|
Packit Service |
084de1 |
recp->shift = 0;
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
|
|
Packit Service |
084de1 |
BN_RECP_CTX *recp, BN_CTX *ctx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int ret = 0;
|
|
Packit Service |
084de1 |
BIGNUM *a;
|
|
Packit Service |
084de1 |
const BIGNUM *ca;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BN_CTX_start(ctx);
|
|
Packit Service |
084de1 |
if ((a = BN_CTX_get(ctx)) == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if (y != NULL) {
|
|
Packit Service |
084de1 |
if (x == y) {
|
|
Packit Service |
084de1 |
if (!BN_sqr(a, x, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
} else {
|
|
Packit Service |
084de1 |
if (!BN_mul(a, x, y, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
ca = a;
|
|
Packit Service |
084de1 |
} else
|
|
Packit Service |
084de1 |
ca = x; /* Just do the mod */
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = BN_div_recp(NULL, r, ca, recp, ctx);
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
BN_CTX_end(ctx);
|
|
Packit Service |
084de1 |
bn_check_top(r);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
|
|
Packit Service |
084de1 |
BN_RECP_CTX *recp, BN_CTX *ctx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int i, j, ret = 0;
|
|
Packit Service |
084de1 |
BIGNUM *a, *b, *d, *r;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BN_CTX_start(ctx);
|
|
Packit Service |
084de1 |
d = (dv != NULL) ? dv : BN_CTX_get(ctx);
|
|
Packit Service |
084de1 |
r = (rem != NULL) ? rem : BN_CTX_get(ctx);
|
|
Packit Service |
084de1 |
a = BN_CTX_get(ctx);
|
|
Packit Service |
084de1 |
b = BN_CTX_get(ctx);
|
|
Packit Service |
084de1 |
if (b == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (BN_ucmp(m, &(recp->N)) < 0) {
|
|
Packit Service |
084de1 |
BN_zero(d);
|
|
Packit Service |
084de1 |
if (!BN_copy(r, m)) {
|
|
Packit Service |
084de1 |
BN_CTX_end(ctx);
|
|
Packit Service |
084de1 |
return 0;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
BN_CTX_end(ctx);
|
|
Packit Service |
084de1 |
return 1;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* We want the remainder Given input of ABCDEF / ab we need multiply
|
|
Packit Service |
084de1 |
* ABCDEF by 3 digests of the reciprocal of ab
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* i := max(BN_num_bits(m), 2*BN_num_bits(N)) */
|
|
Packit Service |
084de1 |
i = BN_num_bits(m);
|
|
Packit Service |
084de1 |
j = recp->num_bits << 1;
|
|
Packit Service |
084de1 |
if (j > i)
|
|
Packit Service |
084de1 |
i = j;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* Nr := round(2^i / N) */
|
|
Packit Service |
084de1 |
if (i != recp->shift)
|
|
Packit Service |
084de1 |
recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx);
|
|
Packit Service |
084de1 |
/* BN_reciprocal could have returned -1 for an error */
|
|
Packit Service |
084de1 |
if (recp->shift == -1)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*-
|
|
Packit Service |
084de1 |
* d := |round(round(m / 2^BN_num_bits(N)) * recp->Nr / 2^(i - BN_num_bits(N)))|
|
|
Packit Service |
084de1 |
* = |round(round(m / 2^BN_num_bits(N)) * round(2^i / N) / 2^(i - BN_num_bits(N)))|
|
|
Packit Service |
084de1 |
* <= |(m / 2^BN_num_bits(N)) * (2^i / N) * (2^BN_num_bits(N) / 2^i)|
|
|
Packit Service |
084de1 |
* = |m/N|
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
if (!BN_rshift(a, m, recp->num_bits))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if (!BN_mul(b, a, &(recp->Nr), ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if (!BN_rshift(d, b, i - recp->num_bits))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
d->neg = 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!BN_mul(b, &(recp->N), d, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if (!BN_usub(r, m, b))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
r->neg = 0;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
j = 0;
|
|
Packit Service |
084de1 |
while (BN_ucmp(r, &(recp->N)) >= 0) {
|
|
Packit Service |
084de1 |
if (j++ > 2) {
|
|
Packit Service |
084de1 |
BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL);
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
if (!BN_usub(r, r, &(recp->N)))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
if (!BN_add_word(d, 1))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
r->neg = BN_is_zero(r) ? 0 : m->neg;
|
|
Packit Service |
084de1 |
d->neg = m->neg ^ recp->N.neg;
|
|
Packit Service |
084de1 |
ret = 1;
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
BN_CTX_end(ctx);
|
|
Packit Service |
084de1 |
bn_check_top(dv);
|
|
Packit Service |
084de1 |
bn_check_top(rem);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* len is the expected size of the result We actually calculate with an extra
|
|
Packit Service |
084de1 |
* word of precision, so we can do faster division if the remainder is not
|
|
Packit Service |
084de1 |
* required.
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
/* r := 2^len / m */
|
|
Packit Service |
084de1 |
int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
int ret = -1;
|
|
Packit Service |
084de1 |
BIGNUM *t;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BN_CTX_start(ctx);
|
|
Packit Service |
084de1 |
if ((t = BN_CTX_get(ctx)) == NULL)
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!BN_set_bit(t, len))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!BN_div(r, NULL, t, m, ctx))
|
|
Packit Service |
084de1 |
goto err;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = len;
|
|
Packit Service |
084de1 |
err:
|
|
Packit Service |
084de1 |
bn_check_top(r);
|
|
Packit Service |
084de1 |
BN_CTX_end(ctx);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|