* [including the GNU Public Licence.]
*/
-#include <stdio.h>
+#define OPENSSL_FIPSAPI
+
#include "cryptlib.h"
#include "bn_lcl.h"
int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx)
{
if (!BN_copy(&(recp->N),d)) return 0;
- if (!BN_zero(&(recp->Nr))) return 0;
+ BN_zero(&(recp->Nr));
recp->num_bits=BN_num_bits(d);
recp->shift=0;
return(1);
ret = BN_div_recp(NULL,r,ca,recp,ctx);
err:
BN_CTX_end(ctx);
+ bn_check_top(r);
return(ret);
}
if (BN_ucmp(m,&(recp->N)) < 0)
{
- if (!BN_zero(d)) return 0;
+ BN_zero(d);
if (!BN_copy(r,m)) return 0;
BN_CTX_end(ctx);
return(1);
{
if (j++ > 2)
{
- BNerr(BN_F_BN_MOD_MUL_RECIPROCAL,BN_R_BAD_RECIPROCAL);
+ BNerr(BN_F_BN_DIV_RECP,BN_R_BAD_RECIPROCAL);
goto err;
}
if (!BN_usub(r,r,&(recp->N))) goto err;
ret=1;
err:
BN_CTX_end(ctx);
+ bn_check_top(dv);
+ bn_check_top(rem);
return(ret);
}
int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx)
{
int ret= -1;
- BIGNUM t;
-
- BN_init(&t);
-
- if (!BN_zero(&t)) goto err;
- if (!BN_set_bit(&t,len)) goto err;
-
- if (!BN_div(r,NULL,&t,m,ctx)) goto err;
-
-#if 1
- {
- BIGNUM v;
-
- BN_init(&v);
- BN_mul(&v,r,m,ctx);
- if (BN_num_bits(&v) > BN_num_bits(r) + BN_num_bits(m))
- {
- fprintf(stderr,"bn_recp.c: BN_mul does not work\n");
- fprintf(stderr,"r =");
- BN_print_fp(stderr,r);
- fprintf(stderr,"\nm =");
- BN_print_fp(stderr,m);
- fprintf(stderr,"\nr*m =");
- BN_print_fp(stderr,&v);
- fprintf(stderr,"\n");
- abort();
+ BIGNUM *t;
-/* Example output (Linux x86):
-
-bn_recp.c: BN_mul does not work
-r =11F5575B94E4AA12CA5D2B7A3DDC5E1A68C77758A941F3C50749D2BB2C65F8D2424E23642AC2CEEFE520FE594626AF7440772AD8C2F3801925E13B11B4398A51A
-m =E415484B146C8AC93EE7B5CAA1C0B0182324E60263BE95C3E26542CD3ADF818D92DD52C073E2B38AEEA5F6C926D2D3D53D7190461D3DF62A20449B5BEAF4F74D
-r*m =1B96E67C0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001B96E67AB2626FFC8A5076B1BE234C8A69F72D9D73A71EDB1649209D42FA20ACA2FAE36B481D9C6F2FE021A437FD81ABB62B5F13E8DEB58366ACEE8493B4F610BCFDBED2
-
-The result should be
-r*m =FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFB2626FFC8A5076B1BE234C8A69F72D9D73A71EDB1649209D42FA20ACA2FAE36B481D9C6F2FE021A437FD81ABB62B5F13E8DEB58366ACEE8493B4F610BCFDBED2
-(according to GNU bc).
+ BN_CTX_start(ctx);
+ if((t = BN_CTX_get(ctx)) == NULL) goto err;
-*/
+ if (!BN_set_bit(t,len)) goto err;
-
- }
- BN_free(&v);
- }
-#endif
+ if (!BN_div(r,NULL,t,m,ctx)) goto err;
ret=len;
err:
- BN_free(&t);
+ bn_check_top(r);
+ BN_CTX_end(ctx);
return(ret);
}