From 305d6377b7e380e3dfadea7d9bbd38e001cbef9e Mon Sep 17 00:00:00 2001 From: jsing Date: Sun, 19 Feb 2023 13:44:29 +0000 Subject: [PATCH] First pass clean up of bn_mont.c. Use calloc() rather than malloc() with manual initialisation of all struct members to zero, use memset() instead of manually initialising all struct members to zero, use consistent naming, use BN_free() instead of BN_clear_free() (since it is the same thing). ok tb@ --- lib/libcrypto/bn/bn_mont.c | 68 +++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/lib/libcrypto/bn/bn_mont.c b/lib/libcrypto/bn/bn_mont.c index 0d110d5ca48..bf38eac3030 100644 --- a/lib/libcrypto/bn/bn_mont.c +++ b/lib/libcrypto/bn/bn_mont.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bn_mont.c,v 1.39 2023/02/19 13:33:23 jsing Exp $ */ +/* $OpenBSD: bn_mont.c,v 1.40 2023/02/19 13:44:29 jsing Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -118,6 +118,7 @@ #include #include +#include #include "bn_local.h" @@ -138,56 +139,61 @@ struct bn_mont_ctx_st { BN_MONT_CTX * BN_MONT_CTX_new(void) { - BN_MONT_CTX *ret; + BN_MONT_CTX *mctx; + + if ((mctx = calloc(1, sizeof(BN_MONT_CTX))) == NULL) + return NULL; + mctx->flags = BN_FLG_MALLOCED; - if ((ret = malloc(sizeof(BN_MONT_CTX))) == NULL) - return (NULL); + BN_init(&mctx->RR); + BN_init(&mctx->N); + BN_init(&mctx->Ni); - BN_MONT_CTX_init(ret); - ret->flags = BN_FLG_MALLOCED; - return (ret); + return mctx; } void -BN_MONT_CTX_init(BN_MONT_CTX *ctx) +BN_MONT_CTX_init(BN_MONT_CTX *mctx) { - ctx->ri = 0; - BN_init(&(ctx->RR)); - BN_init(&(ctx->N)); - BN_init(&(ctx->Ni)); - ctx->n0[0] = ctx->n0[1] = 0; - ctx->flags = 0; + memset(mctx, 0, sizeof(*mctx)); + + BN_init(&mctx->RR); + BN_init(&mctx->N); + BN_init(&mctx->Ni); } void -BN_MONT_CTX_free(BN_MONT_CTX *mont) +BN_MONT_CTX_free(BN_MONT_CTX *mctx) { - if (mont == NULL) + if (mctx == NULL) return; - BN_clear_free(&(mont->RR)); - BN_clear_free(&(mont->N)); - BN_clear_free(&(mont->Ni)); - if (mont->flags & BN_FLG_MALLOCED) - free(mont); + BN_free(&mctx->RR); + BN_free(&mctx->N); + BN_free(&mctx->Ni); + + if (mctx->flags & BN_FLG_MALLOCED) + free(mctx); } BN_MONT_CTX * -BN_MONT_CTX_copy(BN_MONT_CTX *to, BN_MONT_CTX *from) +BN_MONT_CTX_copy(BN_MONT_CTX *dst, BN_MONT_CTX *src) { - if (to == from) - return (to); + if (dst == src) + return dst; - if (!BN_copy(&(to->RR), &(from->RR))) + if (!BN_copy(&dst->RR, &src->RR)) return NULL; - if (!BN_copy(&(to->N), &(from->N))) + if (!BN_copy(&dst->N, &src->N)) return NULL; - if (!BN_copy(&(to->Ni), &(from->Ni))) + if (!BN_copy(&dst->Ni, &src->Ni)) return NULL; - to->ri = from->ri; - to->n0[0] = from->n0[0]; - to->n0[1] = from->n0[1]; - return (to); + + dst->ri = src->ri; + dst->n0[0] = src->n0[0]; + dst->n0[1] = src->n0[1]; + + return dst; } int -- 2.20.1