bn_lib.c (59191) | bn_lib.c (68651) |
---|---|
1/* crypto/bn/bn_lib.c */ 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * --- 42 unchanged lines hidden (view full) --- 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 | 1/* crypto/bn/bn_lib.c */ 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * --- 42 unchanged lines hidden (view full) --- 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 |
59#ifndef BN_DEBUG 60# undef NDEBUG /* avoid conflicting definitions */ 61# define NDEBUG 62#endif 63 64#include <assert.h> |
|
59#include <stdio.h> 60#include "cryptlib.h" 61#include "bn_lcl.h" 62 63const char *BN_version="Big Number" OPENSSL_VERSION_PTEXT; 64 65/* For a 32 bit machine 66 * 2 - 4 == 128 --- 172 unchanged lines hidden (view full) --- 239 { 240 BN_ULONG l; 241 int i; 242 243 bn_check_top(a); 244 245 if (a->top == 0) return(0); 246 l=a->d[a->top-1]; | 65#include <stdio.h> 66#include "cryptlib.h" 67#include "bn_lcl.h" 68 69const char *BN_version="Big Number" OPENSSL_VERSION_PTEXT; 70 71/* For a 32 bit machine 72 * 2 - 4 == 128 --- 172 unchanged lines hidden (view full) --- 245 { 246 BN_ULONG l; 247 int i; 248 249 bn_check_top(a); 250 251 if (a->top == 0) return(0); 252 l=a->d[a->top-1]; |
253 assert(l != 0); |
|
247 i=(a->top-1)*BN_BITS2; | 254 i=(a->top-1)*BN_BITS2; |
248 if (l == 0) 249 { 250#if !defined(NO_STDIO) && !defined(WIN16) 251 fprintf(stderr,"BAD TOP VALUE\n"); 252#endif 253 abort(); 254 } | |
255 return(i+BN_num_bits_word(l)); 256 } 257 258void BN_clear_free(BIGNUM *a) 259 { 260 int i; 261 262 if (a == NULL) return; 263 if (a->d != NULL) 264 { | 255 return(i+BN_num_bits_word(l)); 256 } 257 258void BN_clear_free(BIGNUM *a) 259 { 260 int i; 261 262 if (a == NULL) return; 263 if (a->d != NULL) 264 { |
265 memset(a->d,0,a->max*sizeof(a->d[0])); | 265 memset(a->d,0,a->dmax*sizeof(a->d[0])); |
266 if (!(BN_get_flags(a,BN_FLG_STATIC_DATA))) | 266 if (!(BN_get_flags(a,BN_FLG_STATIC_DATA))) |
267 Free(a->d); | 267 OPENSSL_free(a->d); |
268 } 269 i=BN_get_flags(a,BN_FLG_MALLOCED); 270 memset(a,0,sizeof(BIGNUM)); 271 if (i) | 268 } 269 i=BN_get_flags(a,BN_FLG_MALLOCED); 270 memset(a,0,sizeof(BIGNUM)); 271 if (i) |
272 Free(a); | 272 OPENSSL_free(a); |
273 } 274 275void BN_free(BIGNUM *a) 276 { 277 if (a == NULL) return; 278 if ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA))) | 273 } 274 275void BN_free(BIGNUM *a) 276 { 277 if (a == NULL) return; 278 if ((a->d != NULL) && !(BN_get_flags(a,BN_FLG_STATIC_DATA))) |
279 Free(a->d); | 279 OPENSSL_free(a->d); |
280 a->flags|=BN_FLG_FREE; /* REMOVE? */ 281 if (a->flags & BN_FLG_MALLOCED) | 280 a->flags|=BN_FLG_FREE; /* REMOVE? */ 281 if (a->flags & BN_FLG_MALLOCED) |
282 Free(a); | 282 OPENSSL_free(a); |
283 } 284 285void BN_init(BIGNUM *a) 286 { 287 memset(a,0,sizeof(BIGNUM)); 288 } 289 290BIGNUM *BN_new(void) 291 { 292 BIGNUM *ret; 293 | 283 } 284 285void BN_init(BIGNUM *a) 286 { 287 memset(a,0,sizeof(BIGNUM)); 288 } 289 290BIGNUM *BN_new(void) 291 { 292 BIGNUM *ret; 293 |
294 if ((ret=(BIGNUM *)Malloc(sizeof(BIGNUM))) == NULL) | 294 if ((ret=(BIGNUM *)OPENSSL_malloc(sizeof(BIGNUM))) == NULL) |
295 { 296 BNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE); 297 return(NULL); 298 } 299 ret->flags=BN_FLG_MALLOCED; 300 ret->top=0; 301 ret->neg=0; | 295 { 296 BNerr(BN_F_BN_NEW,ERR_R_MALLOC_FAILURE); 297 return(NULL); 298 } 299 ret->flags=BN_FLG_MALLOCED; 300 ret->top=0; 301 ret->neg=0; |
302 ret->max=0; | 302 ret->dmax=0; |
303 ret->d=NULL; 304 return(ret); 305 } 306 307/* This is an internal function that should not be used in applications. 308 * It ensures that 'b' has enough room for a 'words' word number number. 309 * It is mostly used by the various BIGNUM routines. If there is an error, 310 * NULL is returned. If not, 'b' is returned. */ 311 312BIGNUM *bn_expand2(BIGNUM *b, int words) 313 { 314 BN_ULONG *A,*a; 315 const BN_ULONG *B; 316 int i; 317 318 bn_check_top(b); 319 | 303 ret->d=NULL; 304 return(ret); 305 } 306 307/* This is an internal function that should not be used in applications. 308 * It ensures that 'b' has enough room for a 'words' word number number. 309 * It is mostly used by the various BIGNUM routines. If there is an error, 310 * NULL is returned. If not, 'b' is returned. */ 311 312BIGNUM *bn_expand2(BIGNUM *b, int words) 313 { 314 BN_ULONG *A,*a; 315 const BN_ULONG *B; 316 int i; 317 318 bn_check_top(b); 319 |
320 if (words > b->max) | 320 if (words > b->dmax) |
321 { 322 bn_check_top(b); 323 if (BN_get_flags(b,BN_FLG_STATIC_DATA)) 324 { 325 BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); 326 return(NULL); 327 } | 321 { 322 bn_check_top(b); 323 if (BN_get_flags(b,BN_FLG_STATIC_DATA)) 324 { 325 BNerr(BN_F_BN_EXPAND2,BN_R_EXPAND_ON_STATIC_BIGNUM_DATA); 326 return(NULL); 327 } |
328 a=A=(BN_ULONG *)Malloc(sizeof(BN_ULONG)*(words+1)); | 328 a=A=(BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG)*(words+1)); |
329 if (A == NULL) 330 { 331 BNerr(BN_F_BN_EXPAND2,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 */ --- 81 unchanged lines hidden (view full) --- 418 switch (b->top&3) 419 { 420 case 3: A[2]=B[2]; 421 case 2: A[1]=B[1]; 422 case 1: A[0]=B[0]; 423 case 0: ; /* ultrix cc workaround, see above */ 424 } 425#endif | 329 if (A == NULL) 330 { 331 BNerr(BN_F_BN_EXPAND2,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 */ --- 81 unchanged lines hidden (view full) --- 418 switch (b->top&3) 419 { 420 case 3: A[2]=B[2]; 421 case 2: A[1]=B[1]; 422 case 1: A[0]=B[0]; 423 case 0: ; /* ultrix cc workaround, see above */ 424 } 425#endif |
426 Free(b->d); | 426 OPENSSL_free(b->d); |
427 } 428 429 b->d=a; | 427 } 428 429 b->d=a; |
430 b->max=words; | 430 b->dmax=words; |
431 432 /* Now need to zero any data between b->top and b->max */ 433 434 A= &(b->d[b->top]); | 431 432 /* Now need to zero any data between b->top and b->max */ 433 434 A= &(b->d[b->top]); |
435 for (i=(b->max - b->top)>>3; i>0; i--,A+=8) | 435 for (i=(b->dmax - b->top)>>3; i>0; i--,A+=8) |
436 { 437 A[0]=0; A[1]=0; A[2]=0; A[3]=0; 438 A[4]=0; A[5]=0; A[6]=0; A[7]=0; 439 } | 436 { 437 A[0]=0; A[1]=0; A[2]=0; A[3]=0; 438 A[4]=0; A[5]=0; A[6]=0; A[7]=0; 439 } |
440 for (i=(b->max - b->top)&7; i>0; i--,A++) | 440 for (i=(b->dmax - b->top)&7; i>0; i--,A++) |
441 A[0]=0; 442#else 443 memset(A,0,sizeof(BN_ULONG)*(words+1)); 444 memcpy(A,b->d,sizeof(b->d[0])*b->top); 445 b->d=a; 446 b->max=words; 447#endif 448 --- 54 unchanged lines hidden (view full) --- 503 a->d[0]=0; 504 a->neg=b->neg; 505 return(a); 506 } 507 508void BN_clear(BIGNUM *a) 509 { 510 if (a->d != NULL) | 441 A[0]=0; 442#else 443 memset(A,0,sizeof(BN_ULONG)*(words+1)); 444 memcpy(A,b->d,sizeof(b->d[0])*b->top); 445 b->d=a; 446 b->max=words; 447#endif 448 --- 54 unchanged lines hidden (view full) --- 503 a->d[0]=0; 504 a->neg=b->neg; 505 return(a); 506 } 507 508void BN_clear(BIGNUM *a) 509 { 510 if (a->d != NULL) |
511 memset(a->d,0,a->max*sizeof(a->d[0])); | 511 memset(a->d,0,a->dmax*sizeof(a->d[0])); |
512 a->top=0; 513 a->neg=0; 514 } 515 516BN_ULONG BN_get_word(BIGNUM *a) 517 { 518 int i,n; 519 BN_ULONG ret=0; --- 236 unchanged lines hidden --- | 512 a->top=0; 513 a->neg=0; 514 } 515 516BN_ULONG BN_get_word(BIGNUM *a) 517 { 518 int i,n; 519 BN_ULONG ret=0; --- 236 unchanged lines hidden --- |