diff options
| author | markus <> | 2002-09-05 12:51:50 +0000 |
|---|---|---|
| committer | markus <> | 2002-09-05 12:51:50 +0000 |
| commit | 15b5d84f9da2ce4bfae8580e56e34a859f74ad71 (patch) | |
| tree | bf939e82d7fd73cc8a01cf6959002209972091bc /src/lib/libcrypto/bn/bn_lib.c | |
| parent | 027351f729b9e837200dae6e1520cda6577ab930 (diff) | |
| download | openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.gz openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.tar.bz2 openbsd-15b5d84f9da2ce4bfae8580e56e34a859f74ad71.zip | |
import openssl-0.9.7-beta1
Diffstat (limited to 'src/lib/libcrypto/bn/bn_lib.c')
| -rw-r--r-- | src/lib/libcrypto/bn/bn_lib.c | 571 |
1 files changed, 389 insertions, 182 deletions
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c index bfe7628ad4..a016cb7f53 100644 --- a/src/lib/libcrypto/bn/bn_lib.c +++ b/src/lib/libcrypto/bn/bn_lib.c | |||
| @@ -56,13 +56,79 @@ | |||
| 56 | * [including the GNU Public Licence.] | 56 | * [including the GNU Public Licence.] |
| 57 | */ | 57 | */ |
| 58 | 58 | ||
| 59 | #ifndef BN_DEBUG | ||
| 60 | # undef NDEBUG /* avoid conflicting definitions */ | ||
| 61 | # define NDEBUG | ||
| 62 | #endif | ||
| 63 | |||
| 64 | #include <assert.h> | ||
| 65 | #include <limits.h> | ||
| 59 | #include <stdio.h> | 66 | #include <stdio.h> |
| 60 | #include "cryptlib.h" | 67 | #include "cryptlib.h" |
| 61 | #include "bn_lcl.h" | 68 | #include "bn_lcl.h" |
| 62 | 69 | ||
| 63 | char *BN_version="Big Number part of SSLeay 0.9.0b 29-Jun-1998"; | 70 | const char *BN_version="Big Number" OPENSSL_VERSION_PTEXT; |
| 64 | 71 | ||
| 65 | BIGNUM *BN_value_one() | 72 | /* For a 32 bit machine |
| 73 | * 2 - 4 == 128 | ||
| 74 | * 3 - 8 == 256 | ||
| 75 | * 4 - 16 == 512 | ||
| 76 | * 5 - 32 == 1024 | ||
| 77 | * 6 - 64 == 2048 | ||
| 78 | * 7 - 128 == 4096 | ||
| 79 | * 8 - 256 == 8192 | ||
| 80 | */ | ||
| 81 | static int bn_limit_bits=0; | ||
| 82 | static int bn_limit_num=8; /* (1<<bn_limit_bits) */ | ||
| 83 | static int bn_limit_bits_low=0; | ||
| 84 | static int bn_limit_num_low=8; /* (1<<bn_limit_bits_low) */ | ||
| 85 | static int bn_limit_bits_high=0; | ||
| 86 | static int bn_limit_num_high=8; /* (1<<bn_limit_bits_high) */ | ||
| 87 | static int bn_limit_bits_mont=0; | ||
| 88 | static int bn_limit_num_mont=8; /* (1<<bn_limit_bits_mont) */ | ||
| 89 | |||
| 90 | void BN_set_params(int mult, int high, int low, int mont) | ||
| 91 | { | ||
| 92 | if (mult >= 0) | ||
| 93 | { | ||
| 94 | if (mult > (sizeof(int)*8)-1) | ||
| 95 | mult=sizeof(int)*8-1; | ||
| 96 | bn_limit_bits=mult; | ||
| 97 | bn_limit_num=1<<mult; | ||
| 98 | } | ||
| 99 | if (high >= 0) | ||
| 100 | { | ||
| 101 | if (high > (sizeof(int)*8)-1) | ||
| 102 | high=sizeof(int)*8-1; | ||
| 103 | bn_limit_bits_high=high; | ||
| 104 | bn_limit_num_high=1<<high; | ||
| 105 | } | ||
| 106 | if (low >= 0) | ||
| 107 | { | ||
| 108 | if (low > (sizeof(int)*8)-1) | ||
| 109 | low=sizeof(int)*8-1; | ||
| 110 | bn_limit_bits_low=low; | ||
| 111 | bn_limit_num_low=1<<low; | ||
| 112 | } | ||
| 113 | if (mont >= 0) | ||
| 114 | { | ||
| 115 | if (mont > (sizeof(int)*8)-1) | ||
| 116 | mont=sizeof(int)*8-1; | ||
| 117 | bn_limit_bits_mont=mont; | ||
| 118 | bn_limit_num_mont=1<<mont; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | int BN_get_params(int which) | ||
| 123 | { | ||
| 124 | if (which == 0) return(bn_limit_bits); | ||
| 125 | else if (which == 1) return(bn_limit_bits_high); | ||
| 126 | else if (which == 2) return(bn_limit_bits_low); | ||
| 127 | else if (which == 3) return(bn_limit_bits_mont); | ||
| 128 | else return(0); | ||
| 129 | } | ||
| 130 | |||
| 131 | const BIGNUM *BN_value_one(void) | ||
| 66 | { | 132 | { |
| 67 | static BN_ULONG data_one=1L; | 133 | static BN_ULONG data_one=1L; |
| 68 | static BIGNUM const_one={&data_one,1,1,0}; | 134 | static BIGNUM const_one={&data_one,1,1,0}; |
| @@ -70,7 +136,7 @@ BIGNUM *BN_value_one() | |||
| 70 | return(&const_one); | 136 | return(&const_one); |
| 71 | } | 137 | } |
| 72 | 138 | ||
| 73 | char *BN_options() | 139 | char *BN_options(void) |
| 74 | { | 140 | { |
| 75 | static int init=0; | 141 | static int init=0; |
| 76 | static char data[16]; | 142 | static char data[16]; |
| @@ -89,10 +155,9 @@ char *BN_options() | |||
| 89 | return(data); | 155 | return(data); |
| 90 | } | 156 | } |
| 91 | 157 | ||
| 92 | int BN_num_bits_word(l) | 158 | int BN_num_bits_word(BN_ULONG l) |
| 93 | BN_ULONG l; | ||
| 94 | { | 159 | { |
| 95 | static char bits[256]={ | 160 | static const char bits[256]={ |
| 96 | 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4, | 161 | 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4, |
| 97 | 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, | 162 | 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, |
| 98 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, | 163 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, |
| @@ -111,24 +176,24 @@ BN_ULONG l; | |||
| 111 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, | 176 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, |
| 112 | }; | 177 | }; |
| 113 | 178 | ||
| 114 | #ifdef SIXTY_FOUR_BIT_LONG | 179 | #if defined(SIXTY_FOUR_BIT_LONG) |
| 115 | if (l & 0xffffffff00000000L) | 180 | if (l & 0xffffffff00000000L) |
| 116 | { | 181 | { |
| 117 | if (l & 0xffff000000000000L) | 182 | if (l & 0xffff000000000000L) |
| 118 | { | 183 | { |
| 119 | if (l & 0xff00000000000000L) | 184 | if (l & 0xff00000000000000L) |
| 120 | { | 185 | { |
| 121 | return(bits[l>>56]+56); | 186 | return(bits[(int)(l>>56)]+56); |
| 122 | } | 187 | } |
| 123 | else return(bits[l>>48]+48); | 188 | else return(bits[(int)(l>>48)]+48); |
| 124 | } | 189 | } |
| 125 | else | 190 | else |
| 126 | { | 191 | { |
| 127 | if (l & 0x0000ff0000000000L) | 192 | if (l & 0x0000ff0000000000L) |
| 128 | { | 193 | { |
| 129 | return(bits[l>>40]+40); | 194 | return(bits[(int)(l>>40)]+40); |
| 130 | } | 195 | } |
| 131 | else return(bits[l>>32]+32); | 196 | else return(bits[(int)(l>>32)]+32); |
| 132 | } | 197 | } |
| 133 | } | 198 | } |
| 134 | else | 199 | else |
| @@ -140,17 +205,17 @@ BN_ULONG l; | |||
| 140 | { | 205 | { |
| 141 | if (l & 0xff00000000000000LL) | 206 | if (l & 0xff00000000000000LL) |
| 142 | { | 207 | { |
| 143 | return(bits[l>>56]+56); | 208 | return(bits[(int)(l>>56)]+56); |
| 144 | } | 209 | } |
| 145 | else return(bits[l>>48]+48); | 210 | else return(bits[(int)(l>>48)]+48); |
| 146 | } | 211 | } |
| 147 | else | 212 | else |
| 148 | { | 213 | { |
| 149 | if (l & 0x0000ff0000000000LL) | 214 | if (l & 0x0000ff0000000000LL) |
| 150 | { | 215 | { |
| 151 | return(bits[l>>40]+40); | 216 | return(bits[(int)(l>>40)]+40); |
| 152 | } | 217 | } |
| 153 | else return(bits[l>>32]+32); | 218 | else return(bits[(int)(l>>32)]+32); |
| 154 | } | 219 | } |
| 155 | } | 220 | } |
| 156 | else | 221 | else |
| @@ -161,161 +226,256 @@ BN_ULONG l; | |||
| 161 | if (l & 0xffff0000L) | 226 | if (l & 0xffff0000L) |
| 162 | { | 227 | { |
| 163 | if (l & 0xff000000L) | 228 | if (l & 0xff000000L) |
| 164 | return(bits[l>>24L]+24); | 229 | return(bits[(int)(l>>24L)]+24); |
| 165 | else return(bits[l>>16L]+16); | 230 | else return(bits[(int)(l>>16L)]+16); |
| 166 | } | 231 | } |
| 167 | else | 232 | else |
| 168 | #endif | 233 | #endif |
| 169 | { | 234 | { |
| 170 | #if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG) | 235 | #if defined(SIXTEEN_BIT) || defined(THIRTY_TWO_BIT) || defined(SIXTY_FOUR_BIT) || defined(SIXTY_FOUR_BIT_LONG) |
| 171 | if (l & 0xff00L) | 236 | if (l & 0xff00L) |
| 172 | return(bits[l>>8]+8); | 237 | return(bits[(int)(l>>8)]+8); |
| 173 | else | 238 | else |
| 174 | #endif | 239 | #endif |
| 175 | return(bits[l ] ); | 240 | return(bits[(int)(l )] ); |
| 176 | } | 241 | } |
| 177 | } | 242 | } |
| 178 | } | 243 | } |
| 179 | 244 | ||
| 180 | int BN_num_bits(a) | 245 | int BN_num_bits(const BIGNUM *a) |
| 181 | BIGNUM *a; | ||
| 182 | { | 246 | { |
| 183 | BN_ULONG l; | 247 | BN_ULONG l; |
| 184 | int i; | 248 | int i; |
| 185 | 249 | ||
| 250 | bn_check_top(a); | ||
| 251 | |||
| 186 | if (a->top == 0) return(0); | 252 | if (a->top == 0) return(0); |
| 187 | l=a->d[a->top-1]; | 253 | l=a->d[a->top-1]; |
| 254 | assert(l != 0); | ||
| 188 | i=(a->top-1)*BN_BITS2; | 255 | i=(a->top-1)*BN_BITS2; |
| 189 | if (l == 0) | ||
| 190 | { | ||
| 191 | #if !defined(NO_STDIO) && !defined(WIN16) | ||
| 192 | fprintf(stderr,"BAD TOP VALUE\n"); | ||
| 193 | #endif | ||
| 194 | abort(); | ||
| 195 | } | ||
| 196 | return(i+BN_num_bits_word(l)); | 256 | return(i+BN_num_bits_word(l)); |
| 197 | } | 257 | } |
| 198 | 258 | ||
| 199 | void BN_clear_free(a) | 259 | void BN_clear_free(BIGNUM *a) |
| 200 | BIGNUM *a; | ||
| 201 | { | 260 | { |
| 261 | int i; | ||
| 262 | |||
| 202 | if (a == NULL) return; | 263 | if (a == NULL) return; |
| 203 | if (a->d != NULL) | 264 | if (a->d != NULL) |
| 204 | { | 265 | { |
| 205 | memset(a->d,0,a->max*sizeof(a->d[0])); | 266 | memset(a->d,0,a->dmax*sizeof(a->d[0])); |
| 206 | Free(a->d); | 267 | if (!(BN_get_flags(a,BN_FLG_STATIC_DATA))) |
| 268 | OPENSSL_free(a->d); | ||
| 207 | } | 269 | } |
| 270 | i=BN_get_flags(a,BN_FLG_MALLOCED); | ||
| 208 | memset(a,0,sizeof(BIGNUM)); | 271 | memset(a,0,sizeof(BIGNUM)); |
| 209 | Free(a); | 272 | if (i) |
| 273 | OPENSSL_free(a); | ||
| 210 | } | 274 | } |
| 211 | 275 | ||
| 212 | void BN_free(a) | 276 | void BN_free(BIGNUM *a) |
| 213 | BIGNUM *a; | ||
| 214 | { | 277 | { |
| 215 | if (a == NULL) return; | 278 | if (a == NULL) return; |
| 216 | if (a->d != NULL) Free(a->d); | 279 | if ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA))) |
| 217 | Free(a); | 280 | OPENSSL_free(a->d); |
| 281 | a->flags|=BN_FLG_FREE; /* REMOVE? */ | ||
| 282 | if (a->flags & BN_FLG_MALLOCED) | ||
| 283 | OPENSSL_free(a); | ||
| 218 | } | 284 | } |
| 219 | 285 | ||
| 220 | BIGNUM *BN_new() | 286 | void BN_init(BIGNUM *a) |
| 287 | { | ||
| 288 | memset(a,0,sizeof(BIGNUM)); | ||
| 289 | } | ||
| 290 | |||
| 291 | BIGNUM *BN_new(void) | ||
| 221 | { | 292 | { |
| 222 | BIGNUM *ret; | 293 | BIGNUM *ret; |
| 223 | BN_ULONG *p; | ||
| 224 | 294 | ||
| 225 | ret=(BIGNUM *)Malloc(sizeof(BIGNUM)); | 295 | if ((ret=(BIGNUM *)OPENSSL_malloc(sizeof(BIGNUM))) == NULL) |
| 226 | if (ret == NULL) goto err; | 296 | { |
| 297 | BNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE); | ||
| 298 | return(NULL); | ||
| 299 | } | ||
| 300 | ret->flags=BN_FLG_MALLOCED; | ||
| 227 | ret->top=0; | 301 | ret->top=0; |
| 228 | ret->neg=0; | 302 | ret->neg=0; |
| 229 | ret->max=(BN_DEFAULT_BITS/BN_BITS2); | 303 | ret->dmax=0; |
| 230 | p=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(ret->max+1)); | 304 | ret->d=NULL; |
| 231 | if (p == NULL) goto err; | ||
| 232 | ret->d=p; | ||
| 233 | |||
| 234 | memset(p,0,(ret->max+1)*sizeof(p[0])); | ||
| 235 | return(ret); | 305 | return(ret); |
| 236 | err: | ||
| 237 | BNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE); | ||
| 238 | return(NULL); | ||
| 239 | } | 306 | } |
| 240 | 307 | ||
| 241 | BN_CTX *BN_CTX_new() | 308 | /* This is used both by bn_expand2() and bn_dup_expand() */ |
| 309 | /* The caller MUST check that words > b->dmax before calling this */ | ||
| 310 | static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words) | ||
| 242 | { | 311 | { |
| 243 | BN_CTX *ret; | 312 | BN_ULONG *A,*a = NULL; |
| 244 | BIGNUM *n; | 313 | const BN_ULONG *B; |
| 245 | int i,j; | 314 | int i; |
| 246 | 315 | ||
| 247 | ret=(BN_CTX *)Malloc(sizeof(BN_CTX)); | 316 | if (words > (INT_MAX/(4*BN_BITS2))) |
| 248 | if (ret == NULL) goto err2; | 317 | { |
| 318 | BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_BIGNUM_TOO_LONG); | ||
| 319 | return NULL; | ||
| 320 | } | ||
| 249 | 321 | ||
| 250 | for (i=0; i<BN_CTX_NUM; i++) | 322 | bn_check_top(b); |
| 323 | if (BN_get_flags(b,BN_FLG_STATIC_DATA)) | ||
| 251 | { | 324 | { |
| 252 | n=BN_new(); | 325 | BNerr(BN_F_BN_EXPAND_INTERNAL,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); |
| 253 | if (n == NULL) goto err; | 326 | return(NULL); |
| 254 | ret->bn[i]=n; | 327 | } |
| 328 | a=A=(BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG)*(words+1)); | ||
| 329 | if (A == NULL) | ||
| 330 | { | ||
| 331 | BNerr(BN_F_BN_EXPAND_INTERNAL,ERR_R_MALLOC_FAILURE); | ||
| 332 | return(NULL); | ||
| 333 | } | ||
| 334 | #if 1 | ||
| 335 | B=b->d; | ||
| 336 | /* Check if the previous number needs to be copied */ | ||
| 337 | if (B != NULL) | ||
| 338 | { | ||
| 339 | for (i=b->top>>2; i>0; i--,A+=4,B+=4) | ||
| 340 | { | ||
| 341 | /* | ||
| 342 | * The fact that the loop is unrolled | ||
| 343 | * 4-wise is a tribute to Intel. It's | ||
| 344 | * the one that doesn't have enough | ||
| 345 | * registers to accomodate more data. | ||
| 346 | * I'd unroll it 8-wise otherwise:-) | ||
| 347 | * | ||
| 348 | * <appro@fy.chalmers.se> | ||
| 349 | */ | ||
| 350 | BN_ULONG a0,a1,a2,a3; | ||
| 351 | a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3]; | ||
| 352 | A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3; | ||
| 353 | } | ||
| 354 | switch (b->top&3) | ||
| 355 | { | ||
| 356 | case 3: A[2]=B[2]; | ||
| 357 | case 2: A[1]=B[1]; | ||
| 358 | case 1: A[0]=B[0]; | ||
| 359 | case 0: /* workaround for ultrix cc: without 'case 0', the optimizer does | ||
| 360 | * the switch table by doing a=top&3; a--; goto jump_table[a]; | ||
| 361 | * which fails for top== 0 */ | ||
| 362 | ; | ||
| 363 | } | ||
| 255 | } | 364 | } |
| 256 | 365 | ||
| 257 | /* There is actually an extra one, this is for debugging my | 366 | /* Now need to zero any data between b->top and b->max */ |
| 258 | * stuff */ | 367 | /* XXX Why? */ |
| 259 | ret->bn[BN_CTX_NUM]=NULL; | ||
| 260 | 368 | ||
| 261 | ret->tos=0; | 369 | A= &(a[b->top]); |
| 262 | return(ret); | 370 | for (i=(words - b->top)>>3; i>0; i--,A+=8) |
| 263 | err: | 371 | { |
| 264 | for (j=0; j<i; j++) | 372 | A[0]=0; A[1]=0; A[2]=0; A[3]=0; |
| 265 | BN_free(ret->bn[j]); | 373 | A[4]=0; A[5]=0; A[6]=0; A[7]=0; |
| 266 | Free(ret); | 374 | } |
| 267 | err2: | 375 | for (i=(words - b->top)&7; i>0; i--,A++) |
| 268 | BNerr(BN_F_BN_CTX_NEW,ERR_R_MALLOC_FAILURE); | 376 | A[0]=0; |
| 269 | return(NULL); | 377 | #else |
| 378 | memset(A,0,sizeof(BN_ULONG)*(words+1)); | ||
| 379 | memcpy(A,b->d,sizeof(b->d[0])*b->top); | ||
| 380 | #endif | ||
| 381 | |||
| 382 | return(a); | ||
| 270 | } | 383 | } |
| 271 | 384 | ||
| 272 | void BN_CTX_free(c) | 385 | /* This is an internal function that can be used instead of bn_expand2() |
| 273 | BN_CTX *c; | 386 | * when there is a need to copy BIGNUMs instead of only expanding the |
| 387 | * data part, while still expanding them. | ||
| 388 | * Especially useful when needing to expand BIGNUMs that are declared | ||
| 389 | * 'const' and should therefore not be changed. | ||
| 390 | * The reason to use this instead of a BN_dup() followed by a bn_expand2() | ||
| 391 | * is memory allocation overhead. A BN_dup() followed by a bn_expand2() | ||
| 392 | * will allocate new memory for the BIGNUM data twice, and free it once, | ||
| 393 | * while bn_dup_expand() makes sure allocation is made only once. | ||
| 394 | */ | ||
| 395 | |||
| 396 | BIGNUM *bn_dup_expand(const BIGNUM *b, int words) | ||
| 274 | { | 397 | { |
| 275 | int i; | 398 | BIGNUM *r = NULL; |
| 399 | |||
| 400 | if (words > b->dmax) | ||
| 401 | { | ||
| 402 | BN_ULONG *a = bn_expand_internal(b, words); | ||
| 276 | 403 | ||
| 277 | for (i=0; i<BN_CTX_NUM; i++) | 404 | if (a) |
| 278 | BN_clear_free(c->bn[i]); | 405 | { |
| 279 | Free(c); | 406 | r = BN_new(); |
| 407 | if (r) | ||
| 408 | { | ||
| 409 | r->top = b->top; | ||
| 410 | r->dmax = words; | ||
| 411 | r->neg = b->neg; | ||
| 412 | r->d = a; | ||
| 413 | } | ||
| 414 | else | ||
| 415 | { | ||
| 416 | /* r == NULL, BN_new failure */ | ||
| 417 | OPENSSL_free(a); | ||
| 418 | } | ||
| 419 | } | ||
| 420 | /* If a == NULL, there was an error in allocation in | ||
| 421 | bn_expand_internal(), and NULL should be returned */ | ||
| 422 | } | ||
| 423 | else | ||
| 424 | { | ||
| 425 | r = BN_dup(b); | ||
| 426 | } | ||
| 427 | |||
| 428 | return r; | ||
| 280 | } | 429 | } |
| 281 | 430 | ||
| 282 | BIGNUM *bn_expand2(b, words) | 431 | /* This is an internal function that should not be used in applications. |
| 283 | BIGNUM *b; | 432 | * It ensures that 'b' has enough room for a 'words' word number number. |
| 284 | int words; | 433 | * It is mostly used by the various BIGNUM routines. If there is an error, |
| 285 | { | 434 | * NULL is returned. If not, 'b' is returned. */ |
| 286 | BN_ULONG *p; | ||
| 287 | 435 | ||
| 288 | if (words > b->max) | 436 | BIGNUM *bn_expand2(BIGNUM *b, int words) |
| 437 | { | ||
| 438 | if (words > b->dmax) | ||
| 289 | { | 439 | { |
| 290 | p=(BN_ULONG *)Realloc(b->d,sizeof(BN_ULONG)*(words+1)); | 440 | BN_ULONG *a = bn_expand_internal(b, words); |
| 291 | if (p == NULL) | 441 | |
| 442 | if (a) | ||
| 292 | { | 443 | { |
| 293 | BNerr(BN_F_BN_EXPAND2,ERR_R_MALLOC_FAILURE); | 444 | if (b->d) |
| 294 | return(NULL); | 445 | OPENSSL_free(b->d); |
| 446 | b->d=a; | ||
| 447 | b->dmax=words; | ||
| 295 | } | 448 | } |
| 296 | b->d=p; | 449 | else |
| 297 | memset(&(p[b->max]),0,((words+1)-b->max)*sizeof(BN_ULONG)); | 450 | b = NULL; |
| 298 | b->max=words; | ||
| 299 | } | 451 | } |
| 300 | return(b); | 452 | return b; |
| 301 | } | 453 | } |
| 302 | 454 | ||
| 303 | BIGNUM *BN_dup(a) | 455 | BIGNUM *BN_dup(const BIGNUM *a) |
| 304 | BIGNUM *a; | ||
| 305 | { | 456 | { |
| 306 | BIGNUM *r; | 457 | BIGNUM *r, *t; |
| 307 | 458 | ||
| 308 | r=BN_new(); | 459 | if (a == NULL) return NULL; |
| 309 | if (r == NULL) return(NULL); | 460 | |
| 310 | return((BIGNUM *)BN_copy(r,a)); | 461 | bn_check_top(a); |
| 462 | |||
| 463 | t = BN_new(); | ||
| 464 | if (t == NULL) return(NULL); | ||
| 465 | r = BN_copy(t, a); | ||
| 466 | /* now r == t || r == NULL */ | ||
| 467 | if (r == NULL) | ||
| 468 | BN_free(t); | ||
| 469 | return r; | ||
| 311 | } | 470 | } |
| 312 | 471 | ||
| 313 | BIGNUM *BN_copy(a, b) | 472 | BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b) |
| 314 | BIGNUM *a; | ||
| 315 | BIGNUM *b; | ||
| 316 | { | 473 | { |
| 317 | int i; | 474 | int i; |
| 318 | BN_ULONG *A,*B; | 475 | BN_ULONG *A; |
| 476 | const BN_ULONG *B; | ||
| 477 | |||
| 478 | bn_check_top(b); | ||
| 319 | 479 | ||
| 320 | if (a == b) return(a); | 480 | if (a == b) return(a); |
| 321 | if (bn_wexpand(a,b->top) == NULL) return(NULL); | 481 | if (bn_wexpand(a,b->top) == NULL) return(NULL); |
| @@ -323,35 +483,18 @@ BIGNUM *b; | |||
| 323 | #if 1 | 483 | #if 1 |
| 324 | A=a->d; | 484 | A=a->d; |
| 325 | B=b->d; | 485 | B=b->d; |
| 326 | for (i=b->top&(~7); i>0; i-=8) | 486 | for (i=b->top>>2; i>0; i--,A+=4,B+=4) |
| 327 | { | 487 | { |
| 328 | A[0]=B[0]; | 488 | BN_ULONG a0,a1,a2,a3; |
| 329 | A[1]=B[1]; | 489 | a0=B[0]; a1=B[1]; a2=B[2]; a3=B[3]; |
| 330 | A[2]=B[2]; | 490 | A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3; |
| 331 | A[3]=B[3]; | 491 | } |
| 332 | A[4]=B[4]; | 492 | switch (b->top&3) |
| 333 | A[5]=B[5]; | 493 | { |
| 334 | A[6]=B[6]; | 494 | case 3: A[2]=B[2]; |
| 335 | A[7]=B[7]; | 495 | case 2: A[1]=B[1]; |
| 336 | A+=8; | 496 | case 1: A[0]=B[0]; |
| 337 | B+=8; | 497 | case 0: ; /* ultrix cc workaround, see comments in bn_expand_internal */ |
| 338 | } | ||
| 339 | switch (b->top&7) | ||
| 340 | { | ||
| 341 | case 7: | ||
| 342 | A[6]=B[6]; | ||
| 343 | case 6: | ||
| 344 | A[5]=B[5]; | ||
| 345 | case 5: | ||
| 346 | A[4]=B[4]; | ||
| 347 | case 4: | ||
| 348 | A[3]=B[3]; | ||
| 349 | case 3: | ||
| 350 | A[2]=B[2]; | ||
| 351 | case 2: | ||
| 352 | A[1]=B[1]; | ||
| 353 | case 1: | ||
| 354 | A[0]=B[0]; | ||
| 355 | } | 498 | } |
| 356 | #else | 499 | #else |
| 357 | memcpy(a->d,b->d,sizeof(b->d[0])*b->top); | 500 | memcpy(a->d,b->d,sizeof(b->d[0])*b->top); |
| @@ -359,52 +502,76 @@ BIGNUM *b; | |||
| 359 | 502 | ||
| 360 | /* memset(&(a->d[b->top]),0,sizeof(a->d[0])*(a->max-b->top));*/ | 503 | /* memset(&(a->d[b->top]),0,sizeof(a->d[0])*(a->max-b->top));*/ |
| 361 | a->top=b->top; | 504 | a->top=b->top; |
| 362 | if (a->top == 0) | 505 | if ((a->top == 0) && (a->d != NULL)) |
| 363 | a->d[0]=0; | 506 | a->d[0]=0; |
| 364 | a->neg=b->neg; | 507 | a->neg=b->neg; |
| 365 | return(a); | 508 | return(a); |
| 366 | } | 509 | } |
| 367 | 510 | ||
| 368 | void BN_clear(a) | 511 | void BN_swap(BIGNUM *a, BIGNUM *b) |
| 369 | BIGNUM *a; | 512 | { |
| 513 | int flags_old_a, flags_old_b; | ||
| 514 | BN_ULONG *tmp_d; | ||
| 515 | int tmp_top, tmp_dmax, tmp_neg; | ||
| 516 | |||
| 517 | flags_old_a = a->flags; | ||
| 518 | flags_old_b = b->flags; | ||
| 519 | |||
| 520 | tmp_d = a->d; | ||
| 521 | tmp_top = a->top; | ||
| 522 | tmp_dmax = a->dmax; | ||
| 523 | tmp_neg = a->neg; | ||
| 524 | |||
| 525 | a->d = b->d; | ||
| 526 | a->top = b->top; | ||
| 527 | a->dmax = b->dmax; | ||
| 528 | a->neg = b->neg; | ||
| 529 | |||
| 530 | b->d = tmp_d; | ||
| 531 | b->top = tmp_top; | ||
| 532 | b->dmax = tmp_dmax; | ||
| 533 | b->neg = tmp_neg; | ||
| 534 | |||
| 535 | a->flags = (flags_old_a & BN_FLG_MALLOCED) | (flags_old_b & BN_FLG_STATIC_DATA); | ||
| 536 | b->flags = (flags_old_b & BN_FLG_MALLOCED) | (flags_old_a & BN_FLG_STATIC_DATA); | ||
| 537 | } | ||
| 538 | |||
| 539 | |||
| 540 | void BN_clear(BIGNUM *a) | ||
| 370 | { | 541 | { |
| 371 | memset(a->d,0,a->max*sizeof(a->d[0])); | 542 | if (a->d != NULL) |
| 543 | memset(a->d,0,a->dmax*sizeof(a->d[0])); | ||
| 372 | a->top=0; | 544 | a->top=0; |
| 373 | a->neg=0; | 545 | a->neg=0; |
| 374 | } | 546 | } |
| 375 | 547 | ||
| 376 | unsigned long BN_get_word(a) | 548 | BN_ULONG BN_get_word(const BIGNUM *a) |
| 377 | BIGNUM *a; | ||
| 378 | { | 549 | { |
| 379 | int i,n; | 550 | int i,n; |
| 380 | unsigned long ret=0; | 551 | BN_ULONG ret=0; |
| 381 | 552 | ||
| 382 | n=BN_num_bytes(a); | 553 | n=BN_num_bytes(a); |
| 383 | if (n > sizeof(unsigned long)) | 554 | if (n > sizeof(BN_ULONG)) |
| 384 | #ifdef SIXTY_FOUR_BIT_LONG | ||
| 385 | return(BN_MASK2); | 555 | return(BN_MASK2); |
| 386 | #else | ||
| 387 | return(0xFFFFFFFFL); | ||
| 388 | #endif | ||
| 389 | for (i=a->top-1; i>=0; i--) | 556 | for (i=a->top-1; i>=0; i--) |
| 390 | { | 557 | { |
| 391 | #ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */ | 558 | #ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */ |
| 392 | ret<<=BN_BITS4; /* stops the compiler complaining */ | 559 | ret<<=BN_BITS4; /* stops the compiler complaining */ |
| 393 | ret<<=BN_BITS4; | 560 | ret<<=BN_BITS4; |
| 561 | #else | ||
| 562 | ret=0; | ||
| 394 | #endif | 563 | #endif |
| 395 | ret|=a->d[i]; | 564 | ret|=a->d[i]; |
| 396 | } | 565 | } |
| 397 | return(ret); | 566 | return(ret); |
| 398 | } | 567 | } |
| 399 | 568 | ||
| 400 | int BN_set_word(a,w) | 569 | int BN_set_word(BIGNUM *a, BN_ULONG w) |
| 401 | BIGNUM *a; | ||
| 402 | unsigned long w; | ||
| 403 | { | 570 | { |
| 404 | int i,n; | 571 | int i,n; |
| 405 | if (bn_expand(a,sizeof(unsigned long)*8) == NULL) return(0); | 572 | if (bn_expand(a,sizeof(BN_ULONG)*8) == NULL) return(0); |
| 406 | 573 | ||
| 407 | n=sizeof(unsigned long)/BN_BYTES; | 574 | n=sizeof(BN_ULONG)/BN_BYTES; |
| 408 | a->neg=0; | 575 | a->neg=0; |
| 409 | a->top=0; | 576 | a->top=0; |
| 410 | a->d[0]=(BN_ULONG)w&BN_MASK2; | 577 | a->d[0]=(BN_ULONG)w&BN_MASK2; |
| @@ -417,6 +584,8 @@ unsigned long w; | |||
| 417 | #ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */ | 584 | #ifndef SIXTY_FOUR_BIT /* the data item > unsigned long */ |
| 418 | w>>=BN_BITS4; | 585 | w>>=BN_BITS4; |
| 419 | w>>=BN_BITS4; | 586 | w>>=BN_BITS4; |
| 587 | #else | ||
| 588 | w=0; | ||
| 420 | #endif | 589 | #endif |
| 421 | a->d[i]=(BN_ULONG)w&BN_MASK2; | 590 | a->d[i]=(BN_ULONG)w&BN_MASK2; |
| 422 | if (a->d[i] != 0) a->top=i+1; | 591 | if (a->d[i] != 0) a->top=i+1; |
| @@ -424,11 +593,7 @@ unsigned long w; | |||
| 424 | return(1); | 593 | return(1); |
| 425 | } | 594 | } |
| 426 | 595 | ||
| 427 | /* ignore negative */ | 596 | BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret) |
| 428 | BIGNUM *BN_bin2bn(s, len, ret) | ||
| 429 | unsigned char *s; | ||
| 430 | int len; | ||
| 431 | BIGNUM *ret; | ||
| 432 | { | 597 | { |
| 433 | unsigned int i,m; | 598 | unsigned int i,m; |
| 434 | unsigned int n; | 599 | unsigned int n; |
| @@ -448,6 +613,7 @@ BIGNUM *ret; | |||
| 448 | i=((n-1)/BN_BYTES)+1; | 613 | i=((n-1)/BN_BYTES)+1; |
| 449 | m=((n-1)%(BN_BYTES)); | 614 | m=((n-1)%(BN_BYTES)); |
| 450 | ret->top=i; | 615 | ret->top=i; |
| 616 | ret->neg=0; | ||
| 451 | while (n-- > 0) | 617 | while (n-- > 0) |
| 452 | { | 618 | { |
| 453 | l=(l<<8L)| *(s++); | 619 | l=(l<<8L)| *(s++); |
| @@ -465,9 +631,7 @@ BIGNUM *ret; | |||
| 465 | } | 631 | } |
| 466 | 632 | ||
| 467 | /* ignore negative */ | 633 | /* ignore negative */ |
| 468 | int BN_bn2bin(a, to) | 634 | int BN_bn2bin(const BIGNUM *a, unsigned char *to) |
| 469 | BIGNUM *a; | ||
| 470 | unsigned char *to; | ||
| 471 | { | 635 | { |
| 472 | int n,i; | 636 | int n,i; |
| 473 | BN_ULONG l; | 637 | BN_ULONG l; |
| @@ -481,13 +645,14 @@ unsigned char *to; | |||
| 481 | return(n); | 645 | return(n); |
| 482 | } | 646 | } |
| 483 | 647 | ||
| 484 | int BN_ucmp(a, b) | 648 | int BN_ucmp(const BIGNUM *a, const BIGNUM *b) |
| 485 | BIGNUM *a; | ||
| 486 | BIGNUM *b; | ||
| 487 | { | 649 | { |
| 488 | int i; | 650 | int i; |
| 489 | BN_ULONG t1,t2,*ap,*bp; | 651 | BN_ULONG t1,t2,*ap,*bp; |
| 490 | 652 | ||
| 653 | bn_check_top(a); | ||
| 654 | bn_check_top(b); | ||
| 655 | |||
| 491 | i=a->top-b->top; | 656 | i=a->top-b->top; |
| 492 | if (i != 0) return(i); | 657 | if (i != 0) return(i); |
| 493 | ap=a->d; | 658 | ap=a->d; |
| @@ -502,9 +667,7 @@ BIGNUM *b; | |||
| 502 | return(0); | 667 | return(0); |
| 503 | } | 668 | } |
| 504 | 669 | ||
| 505 | int BN_cmp(a, b) | 670 | int BN_cmp(const BIGNUM *a, const BIGNUM *b) |
| 506 | BIGNUM *a; | ||
| 507 | BIGNUM *b; | ||
| 508 | { | 671 | { |
| 509 | int i; | 672 | int i; |
| 510 | int gt,lt; | 673 | int gt,lt; |
| @@ -519,6 +682,10 @@ BIGNUM *b; | |||
| 519 | else | 682 | else |
| 520 | return(0); | 683 | return(0); |
| 521 | } | 684 | } |
| 685 | |||
| 686 | bn_check_top(a); | ||
| 687 | bn_check_top(b); | ||
| 688 | |||
| 522 | if (a->neg != b->neg) | 689 | if (a->neg != b->neg) |
| 523 | { | 690 | { |
| 524 | if (a->neg) | 691 | if (a->neg) |
| @@ -541,27 +708,25 @@ BIGNUM *b; | |||
| 541 | return(0); | 708 | return(0); |
| 542 | } | 709 | } |
| 543 | 710 | ||
| 544 | int BN_set_bit(a, n) | 711 | int BN_set_bit(BIGNUM *a, int n) |
| 545 | BIGNUM *a; | ||
| 546 | int n; | ||
| 547 | { | 712 | { |
| 548 | int i,j; | 713 | int i,j,k; |
| 549 | 714 | ||
| 550 | i=n/BN_BITS2; | 715 | i=n/BN_BITS2; |
| 551 | j=n%BN_BITS2; | 716 | j=n%BN_BITS2; |
| 552 | if (a->top <= i) | 717 | if (a->top <= i) |
| 553 | { | 718 | { |
| 554 | if (bn_expand(a,n) == NULL) return(0); | 719 | if (bn_wexpand(a,i+1) == NULL) return(0); |
| 720 | for(k=a->top; k<i+1; k++) | ||
| 721 | a->d[k]=0; | ||
| 555 | a->top=i+1; | 722 | a->top=i+1; |
| 556 | } | 723 | } |
| 557 | 724 | ||
| 558 | a->d[i]|=(1L<<j); | 725 | a->d[i]|=(((BN_ULONG)1)<<j); |
| 559 | return(1); | 726 | return(1); |
| 560 | } | 727 | } |
| 561 | 728 | ||
| 562 | int BN_clear_bit(a, n) | 729 | int BN_clear_bit(BIGNUM *a, int n) |
| 563 | BIGNUM *a; | ||
| 564 | int n; | ||
| 565 | { | 730 | { |
| 566 | int i,j; | 731 | int i,j; |
| 567 | 732 | ||
| @@ -569,13 +734,12 @@ int n; | |||
| 569 | j=n%BN_BITS2; | 734 | j=n%BN_BITS2; |
| 570 | if (a->top <= i) return(0); | 735 | if (a->top <= i) return(0); |
| 571 | 736 | ||
| 572 | a->d[i]&=(~(1L<<j)); | 737 | a->d[i]&=(~(((BN_ULONG)1)<<j)); |
| 738 | bn_fix_top(a); | ||
| 573 | return(1); | 739 | return(1); |
| 574 | } | 740 | } |
| 575 | 741 | ||
| 576 | int BN_is_bit_set(a, n) | 742 | int BN_is_bit_set(const BIGNUM *a, int n) |
| 577 | BIGNUM *a; | ||
| 578 | int n; | ||
| 579 | { | 743 | { |
| 580 | int i,j; | 744 | int i,j; |
| 581 | 745 | ||
| @@ -586,9 +750,7 @@ int n; | |||
| 586 | return((a->d[i]&(((BN_ULONG)1)<<j))?1:0); | 750 | return((a->d[i]&(((BN_ULONG)1)<<j))?1:0); |
| 587 | } | 751 | } |
| 588 | 752 | ||
| 589 | int BN_mask_bits(a,n) | 753 | int BN_mask_bits(BIGNUM *a, int n) |
| 590 | BIGNUM *a; | ||
| 591 | int n; | ||
| 592 | { | 754 | { |
| 593 | int b,w; | 755 | int b,w; |
| 594 | 756 | ||
| @@ -601,11 +763,56 @@ int n; | |||
| 601 | { | 763 | { |
| 602 | a->top=w+1; | 764 | a->top=w+1; |
| 603 | a->d[w]&= ~(BN_MASK2<<b); | 765 | a->d[w]&= ~(BN_MASK2<<b); |
| 604 | while ((w >= 0) && (a->d[w] == 0)) | 766 | } |
| 767 | bn_fix_top(a); | ||
| 768 | return(1); | ||
| 769 | } | ||
| 770 | |||
| 771 | int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n) | ||
| 772 | { | ||
| 773 | int i; | ||
| 774 | BN_ULONG aa,bb; | ||
| 775 | |||
| 776 | aa=a[n-1]; | ||
| 777 | bb=b[n-1]; | ||
| 778 | if (aa != bb) return((aa > bb)?1:-1); | ||
| 779 | for (i=n-2; i>=0; i--) | ||
| 780 | { | ||
| 781 | aa=a[i]; | ||
| 782 | bb=b[i]; | ||
| 783 | if (aa != bb) return((aa > bb)?1:-1); | ||
| 784 | } | ||
| 785 | return(0); | ||
| 786 | } | ||
| 787 | |||
| 788 | /* Here follows a specialised variants of bn_cmp_words(). It has the | ||
| 789 | property of performing the operation on arrays of different sizes. | ||
| 790 | The sizes of those arrays is expressed through cl, which is the | ||
| 791 | common length ( basicall, min(len(a),len(b)) ), and dl, which is the | ||
| 792 | delta between the two lengths, calculated as len(a)-len(b). | ||
| 793 | All lengths are the number of BN_ULONGs... */ | ||
| 794 | |||
| 795 | int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, | ||
| 796 | int cl, int dl) | ||
| 797 | { | ||
| 798 | int n,i; | ||
| 799 | n = cl-1; | ||
| 800 | |||
| 801 | if (dl < 0) | ||
| 802 | { | ||
| 803 | for (i=dl; i<0; i++) | ||
| 605 | { | 804 | { |
| 606 | a->top--; | 805 | if (b[n-i] != 0) |
| 607 | w--; | 806 | return -1; /* a < b */ |
| 608 | } | 807 | } |
| 609 | } | 808 | } |
| 610 | return(1); | 809 | if (dl > 0) |
| 810 | { | ||
| 811 | for (i=dl; i>0; i--) | ||
| 812 | { | ||
| 813 | if (a[n+i] != 0) | ||
| 814 | return 1; /* a > b */ | ||
| 815 | } | ||
| 816 | } | ||
| 817 | return bn_cmp_words(a,b,cl); | ||
| 611 | } | 818 | } |
