155714Skris/* crypto/bn/bn_recp.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296465Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296465Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296465Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296465Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296465Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include "cryptlib.h" 6155714Skris#include "bn_lcl.h" 6255714Skris 6355714Skrisvoid BN_RECP_CTX_init(BN_RECP_CTX *recp) 64296465Sdelphij{ 65296465Sdelphij BN_init(&(recp->N)); 66296465Sdelphij BN_init(&(recp->Nr)); 67296465Sdelphij recp->num_bits = 0; 68296465Sdelphij recp->flags = 0; 69296465Sdelphij} 7055714Skris 7155714SkrisBN_RECP_CTX *BN_RECP_CTX_new(void) 72296465Sdelphij{ 73296465Sdelphij BN_RECP_CTX *ret; 7455714Skris 75296465Sdelphij if ((ret = (BN_RECP_CTX *)OPENSSL_malloc(sizeof(BN_RECP_CTX))) == NULL) 76296465Sdelphij return (NULL); 7755714Skris 78296465Sdelphij BN_RECP_CTX_init(ret); 79296465Sdelphij ret->flags = BN_FLG_MALLOCED; 80296465Sdelphij return (ret); 81296465Sdelphij} 8255714Skris 8355714Skrisvoid BN_RECP_CTX_free(BN_RECP_CTX *recp) 84296465Sdelphij{ 85296465Sdelphij if (recp == NULL) 86296465Sdelphij return; 8755714Skris 88296465Sdelphij BN_free(&(recp->N)); 89296465Sdelphij BN_free(&(recp->Nr)); 90296465Sdelphij if (recp->flags & BN_FLG_MALLOCED) 91296465Sdelphij OPENSSL_free(recp); 92296465Sdelphij} 9355714Skris 9455714Skrisint BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx) 95296465Sdelphij{ 96296465Sdelphij if (!BN_copy(&(recp->N), d)) 97296465Sdelphij return 0; 98296465Sdelphij BN_zero(&(recp->Nr)); 99296465Sdelphij recp->num_bits = BN_num_bits(d); 100296465Sdelphij recp->shift = 0; 101296465Sdelphij return (1); 102296465Sdelphij} 10355714Skris 104109998Smarkmint BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y, 105296465Sdelphij BN_RECP_CTX *recp, BN_CTX *ctx) 106296465Sdelphij{ 107296465Sdelphij int ret = 0; 108296465Sdelphij BIGNUM *a; 109296465Sdelphij const BIGNUM *ca; 11055714Skris 111296465Sdelphij BN_CTX_start(ctx); 112296465Sdelphij if ((a = BN_CTX_get(ctx)) == NULL) 113296465Sdelphij goto err; 114296465Sdelphij if (y != NULL) { 115296465Sdelphij if (x == y) { 116296465Sdelphij if (!BN_sqr(a, x, ctx)) 117296465Sdelphij goto err; 118296465Sdelphij } else { 119296465Sdelphij if (!BN_mul(a, x, y, ctx)) 120296465Sdelphij goto err; 121296465Sdelphij } 122296465Sdelphij ca = a; 123296465Sdelphij } else 124296465Sdelphij ca = x; /* Just do the mod */ 12555714Skris 126296465Sdelphij ret = BN_div_recp(NULL, r, ca, recp, ctx); 127296465Sdelphij err: 128296465Sdelphij BN_CTX_end(ctx); 129296465Sdelphij bn_check_top(r); 130296465Sdelphij return (ret); 131296465Sdelphij} 13255714Skris 133109998Smarkmint BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, 134296465Sdelphij BN_RECP_CTX *recp, BN_CTX *ctx) 135296465Sdelphij{ 136296465Sdelphij int i, j, ret = 0; 137296465Sdelphij BIGNUM *a, *b, *d, *r; 13855714Skris 139296465Sdelphij BN_CTX_start(ctx); 140296465Sdelphij a = BN_CTX_get(ctx); 141296465Sdelphij b = BN_CTX_get(ctx); 142296465Sdelphij if (dv != NULL) 143296465Sdelphij d = dv; 144296465Sdelphij else 145296465Sdelphij d = BN_CTX_get(ctx); 146296465Sdelphij if (rem != NULL) 147296465Sdelphij r = rem; 148296465Sdelphij else 149296465Sdelphij r = BN_CTX_get(ctx); 150296465Sdelphij if (a == NULL || b == NULL || d == NULL || r == NULL) 151296465Sdelphij goto err; 15255714Skris 153296465Sdelphij if (BN_ucmp(m, &(recp->N)) < 0) { 154296465Sdelphij BN_zero(d); 155296465Sdelphij if (!BN_copy(r, m)) 156296465Sdelphij return 0; 157296465Sdelphij BN_CTX_end(ctx); 158296465Sdelphij return (1); 159296465Sdelphij } 16055714Skris 161296465Sdelphij /* 162296465Sdelphij * We want the remainder Given input of ABCDEF / ab we need multiply 163296465Sdelphij * ABCDEF by 3 digests of the reciprocal of ab 164296465Sdelphij */ 165109998Smarkm 166296465Sdelphij /* i := max(BN_num_bits(m), 2*BN_num_bits(N)) */ 167296465Sdelphij i = BN_num_bits(m); 168296465Sdelphij j = recp->num_bits << 1; 169296465Sdelphij if (j > i) 170296465Sdelphij i = j; 17155714Skris 172296465Sdelphij /* Nr := round(2^i / N) */ 173296465Sdelphij if (i != recp->shift) 174296465Sdelphij recp->shift = BN_reciprocal(&(recp->Nr), &(recp->N), i, ctx); 175296465Sdelphij /* BN_reciprocal could have returned -1 for an error */ 176296465Sdelphij if (recp->shift == -1) 177296465Sdelphij goto err; 17855714Skris 179296465Sdelphij /*- 180296465Sdelphij * d := |round(round(m / 2^BN_num_bits(N)) * recp->Nr / 2^(i - BN_num_bits(N)))| 181296465Sdelphij * = |round(round(m / 2^BN_num_bits(N)) * round(2^i / N) / 2^(i - BN_num_bits(N)))| 182296465Sdelphij * <= |(m / 2^BN_num_bits(N)) * (2^i / N) * (2^BN_num_bits(N) / 2^i)| 183296465Sdelphij * = |m/N| 184296465Sdelphij */ 185296465Sdelphij if (!BN_rshift(a, m, recp->num_bits)) 186296465Sdelphij goto err; 187296465Sdelphij if (!BN_mul(b, a, &(recp->Nr), ctx)) 188296465Sdelphij goto err; 189296465Sdelphij if (!BN_rshift(d, b, i - recp->num_bits)) 190296465Sdelphij goto err; 191296465Sdelphij d->neg = 0; 192109998Smarkm 193296465Sdelphij if (!BN_mul(b, &(recp->N), d, ctx)) 194296465Sdelphij goto err; 195296465Sdelphij if (!BN_usub(r, m, b)) 196296465Sdelphij goto err; 197296465Sdelphij r->neg = 0; 19855714Skris 19959191Skris#if 1 200296465Sdelphij j = 0; 201296465Sdelphij while (BN_ucmp(r, &(recp->N)) >= 0) { 202296465Sdelphij if (j++ > 2) { 203296465Sdelphij BNerr(BN_F_BN_DIV_RECP, BN_R_BAD_RECIPROCAL); 204296465Sdelphij goto err; 205296465Sdelphij } 206296465Sdelphij if (!BN_usub(r, r, &(recp->N))) 207296465Sdelphij goto err; 208296465Sdelphij if (!BN_add_word(d, 1)) 209296465Sdelphij goto err; 210296465Sdelphij } 21155714Skris#endif 21255714Skris 213296465Sdelphij r->neg = BN_is_zero(r) ? 0 : m->neg; 214296465Sdelphij d->neg = m->neg ^ recp->N.neg; 215296465Sdelphij ret = 1; 216296465Sdelphij err: 217296465Sdelphij BN_CTX_end(ctx); 218296465Sdelphij bn_check_top(dv); 219296465Sdelphij bn_check_top(rem); 220296465Sdelphij return (ret); 221296465Sdelphij} 22255714Skris 223296465Sdelphij/* 224296465Sdelphij * len is the expected size of the result We actually calculate with an extra 225296465Sdelphij * word of precision, so we can do faster division if the remainder is not 226296465Sdelphij * required. 22755714Skris */ 228109998Smarkm/* r := 2^len / m */ 229109998Smarkmint BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx) 230296465Sdelphij{ 231296465Sdelphij int ret = -1; 232296465Sdelphij BIGNUM *t; 23355714Skris 234296465Sdelphij BN_CTX_start(ctx); 235296465Sdelphij if ((t = BN_CTX_get(ctx)) == NULL) 236296465Sdelphij goto err; 23755714Skris 238296465Sdelphij if (!BN_set_bit(t, len)) 239296465Sdelphij goto err; 24055714Skris 241296465Sdelphij if (!BN_div(r, NULL, t, m, ctx)) 242296465Sdelphij goto err; 243109998Smarkm 244296465Sdelphij ret = len; 245296465Sdelphij err: 246296465Sdelphij bn_check_top(r); 247296465Sdelphij BN_CTX_end(ctx); 248296465Sdelphij return (ret); 249296465Sdelphij} 250