summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn/bn_div.c
diff options
context:
space:
mode:
authormiod <>2014-04-24 21:31:02 +0000
committermiod <>2014-04-24 21:31:02 +0000
commit7e3ee009cb00e65ea487ba61ae3b5271c73c19d6 (patch)
treee53472b5e8d9eec9f62ca322e053d1c8f66dc1cc /src/lib/libcrypto/bn/bn_div.c
parent04cb67d517205b357d70ac65a314fea6e6a1a479 (diff)
downloadopenbsd-7e3ee009cb00e65ea487ba61ae3b5271c73c19d6.tar.gz
openbsd-7e3ee009cb00e65ea487ba61ae3b5271c73c19d6.tar.bz2
openbsd-7e3ee009cb00e65ea487ba61ae3b5271c73c19d6.zip
Try to clean the maze of <openssl/bn.h> defines regarding the BN internals.
The intent of this change is to only keep support for two kind of architectures: - those with 32-bit int and long, and 64-bit long long, where ``long * long -> long long'' multiplication routines are available. - those with 64-bit int and long, and no 128-bit long long type. This gets rid of the SIXTY_FOUR_BIT_LONG, SIXTY_FOUR_BIT (not the same!), THIRTY_TWO_BIT, SIXTEEN_BIT and EIGHT_BIT defines. After this change, the types and defines are as follows: arch: 64bit 32bit rationale BN_LLONG undefined defined defined if l * l -> ll BN_ULLONG undefined u long long result of BN_LONG * BN_LONG BN_ULONG u long u int native register size BN_LONG long int the same, signed BN_BITS 128 64 size of 2*BN_ULONG in bits BN_BYTES 8 4 size of 2*BN_ULONG in bytes BN_BITS2 64 32 BN_BITS / 2 Tested on various 32-bit and 64-bit OpenBSD systems of various endianness.
Diffstat (limited to 'src/lib/libcrypto/bn/bn_div.c')
-rw-r--r--src/lib/libcrypto/bn/bn_div.c71
1 files changed, 2 insertions, 69 deletions
diff --git a/src/lib/libcrypto/bn/bn_div.c b/src/lib/libcrypto/bn/bn_div.c
index 871f29e34f..e3e06ac054 100644
--- a/src/lib/libcrypto/bn/bn_div.c
+++ b/src/lib/libcrypto/bn/bn_div.c
@@ -61,72 +61,6 @@
61#include "cryptlib.h" 61#include "cryptlib.h"
62#include "bn_lcl.h" 62#include "bn_lcl.h"
63 63
64
65/* The old slow way */
66#if 0
67int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
68 BN_CTX *ctx)
69 {
70 int i,nm,nd;
71 int ret = 0;
72 BIGNUM *D;
73
74 bn_check_top(m);
75 bn_check_top(d);
76 if (BN_is_zero(d))
77 {
78 BNerr(BN_F_BN_DIV,BN_R_DIV_BY_ZERO);
79 return(0);
80 }
81
82 if (BN_ucmp(m,d) < 0)
83 {
84 if (rem != NULL)
85 { if (BN_copy(rem,m) == NULL) return(0); }
86 if (dv != NULL) BN_zero(dv);
87 return(1);
88 }
89
90 BN_CTX_start(ctx);
91 D = BN_CTX_get(ctx);
92 if (dv == NULL) dv = BN_CTX_get(ctx);
93 if (rem == NULL) rem = BN_CTX_get(ctx);
94 if (D == NULL || dv == NULL || rem == NULL)
95 goto end;
96
97 nd=BN_num_bits(d);
98 nm=BN_num_bits(m);
99 if (BN_copy(D,d) == NULL) goto end;
100 if (BN_copy(rem,m) == NULL) goto end;
101
102 /* The next 2 are needed so we can do a dv->d[0]|=1 later
103 * since BN_lshift1 will only work once there is a value :-) */
104 BN_zero(dv);
105 if(bn_wexpand(dv,1) == NULL) goto end;
106 dv->top=1;
107
108 if (!BN_lshift(D,D,nm-nd)) goto end;
109 for (i=nm-nd; i>=0; i--)
110 {
111 if (!BN_lshift1(dv,dv)) goto end;
112 if (BN_ucmp(rem,D) >= 0)
113 {
114 dv->d[0]|=1;
115 if (!BN_usub(rem,rem,D)) goto end;
116 }
117/* CAN IMPROVE (and have now :=) */
118 if (!BN_rshift1(D,D)) goto end;
119 }
120 rem->neg=BN_is_zero(rem)?0:m->neg;
121 dv->neg=m->neg^d->neg;
122 ret = 1;
123 end:
124 BN_CTX_end(ctx);
125 return(ret);
126 }
127
128#else
129
130#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) \ 64#if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) \
131 && !defined(BN_DIV3W) 65 && !defined(BN_DIV3W)
132# if defined(__GNUC__) && __GNUC__>=2 66# if defined(__GNUC__) && __GNUC__>=2
@@ -151,7 +85,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
151 q; \ 85 q; \
152 }) 86 })
153# define REMAINDER_IS_ALREADY_CALCULATED 87# define REMAINDER_IS_ALREADY_CALCULATED
154# elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG) 88# elif defined(__x86_64)
155 /* 89 /*
156 * Same story here, but it's 128-bit by 64-bit division. Wow! 90 * Same story here, but it's 128-bit by 64-bit division. Wow!
157 * <appro@fy.chalmers.se> 91 * <appro@fy.chalmers.se>
@@ -333,7 +267,7 @@ int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
333#ifdef BN_LLONG 267#ifdef BN_LLONG
334 BN_ULLONG t2; 268 BN_ULLONG t2;
335 269
336#if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words) 270#if defined(BN_DIV2W) && !defined(bn_div_words)
337 q=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0); 271 q=(BN_ULONG)(((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0);
338#else 272#else
339 q=bn_div_words(n0,n1,d0); 273 q=bn_div_words(n0,n1,d0);
@@ -435,4 +369,3 @@ err:
435 BN_CTX_end(ctx); 369 BN_CTX_end(ctx);
436 return(0); 370 return(0);
437 } 371 }
438#endif