From: jsing Date: Mon, 27 Dec 2021 15:12:22 +0000 (+0000) Subject: Pull BN_{new,init,clear,clear_free,free} up to the top of bn_lib.c X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=7a74894e481dbeddb8c7be9e6e43ece16fec4f8d;p=openbsd Pull BN_{new,init,clear,clear_free,free} up to the top of bn_lib.c Discussed with tb@ --- diff --git a/lib/libcrypto/bn/bn_lib.c b/lib/libcrypto/bn/bn_lib.c index 2544722ea9a..6e828f1e74e 100644 --- a/lib/libcrypto/bn/bn_lib.c +++ b/lib/libcrypto/bn/bn_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_lib.c,v 1.52 2021/12/04 16:02:44 tb Exp $ */ +/* $OpenBSD: bn_lib.c,v 1.53 2021/12/27 15:12:22 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -92,6 +92,63 @@ static int bn_limit_num_high = 8; /* (1<flags = BN_FLG_MALLOCED; + ret->top = 0; + ret->neg = 0; + ret->dmax = 0; + ret->d = NULL; + bn_check_top(ret); + return (ret); +} + +void +BN_init(BIGNUM *a) +{ + memset(a, 0, sizeof(BIGNUM)); + bn_check_top(a); +} + +void +BN_clear(BIGNUM *a) +{ + bn_check_top(a); + if (a->d != NULL) + explicit_bzero(a->d, a->dmax * sizeof(a->d[0])); + a->top = 0; + a->neg = 0; +} + +void +BN_clear_free(BIGNUM *a) +{ + int i; + + if (a == NULL) + return; + bn_check_top(a); + if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA))) + freezero(a->d, a->dmax * sizeof(a->d[0])); + i = BN_get_flags(a, BN_FLG_MALLOCED); + explicit_bzero(a, sizeof(BIGNUM)); + if (i) + free(a); +} + +void +BN_free(BIGNUM *a) +{ + BN_clear_free(a); +} + void BN_set_params(int mult, int high, int low, int mont) { @@ -206,53 +263,6 @@ BN_num_bits(const BIGNUM *a) return ((i * BN_BITS2) + BN_num_bits_word(a->d[i])); } -void -BN_clear_free(BIGNUM *a) -{ - int i; - - if (a == NULL) - return; - bn_check_top(a); - if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA))) - freezero(a->d, a->dmax * sizeof(a->d[0])); - i = BN_get_flags(a, BN_FLG_MALLOCED); - explicit_bzero(a, sizeof(BIGNUM)); - if (i) - free(a); -} - -void -BN_free(BIGNUM *a) -{ - BN_clear_free(a); -} - -void -BN_init(BIGNUM *a) -{ - memset(a, 0, sizeof(BIGNUM)); - bn_check_top(a); -} - -BIGNUM * -BN_new(void) -{ - BIGNUM *ret; - - if ((ret = malloc(sizeof(BIGNUM))) == NULL) { - BNerror(ERR_R_MALLOC_FAILURE); - return (NULL); - } - ret->flags = BN_FLG_MALLOCED; - ret->top = 0; - ret->neg = 0; - ret->dmax = 0; - ret->d = NULL; - bn_check_top(ret); - return (ret); -} - /* This is used both by bn_expand2() and bn_dup_expand() */ /* The caller MUST check that words > b->dmax before calling this */ static BN_ULONG * @@ -518,16 +528,6 @@ BN_swap(BIGNUM *a, BIGNUM *b) bn_check_top(b); } -void -BN_clear(BIGNUM *a) -{ - bn_check_top(a); - if (a->d != NULL) - explicit_bzero(a->d, a->dmax * sizeof(a->d[0])); - a->top = 0; - a->neg = 0; -} - BN_ULONG BN_get_word(const BIGNUM *a) {