Do not put BN_CTX structures on the stack, but instead allocate them

runtime using BN_CTX_new().  This is done since in OpenSSL 0.9.7e we
can only allocate BN_CTX on the stack by including an internal OpenSSL
header file, and in OpenSSL 0.9.8 BN_CTX is entirely opaque, so having
it on the stack is not possible at all.

This is done as preparation for OpenSSL 0.9.8b import.

Tested on:	amd64 i386 ia64
Tested with:	src/tools/regression/lib/libmp
This commit is contained in:
Simon L. B. Nielsen 2006-07-28 23:00:16 +00:00
parent 1dd3ff6658
commit 76f29359f7
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=160805

View File

@ -82,7 +82,6 @@ __FBSDID("$FreeBSD$");
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include <openssl/err.h> #include <openssl/err.h>
#include "openssl/crypto/bn/bn_lcl.h"
#include "mp.h" #include "mp.h"
#define MPERR(s) do { warn s; abort(); } while (0) #define MPERR(s) do { warn s; abort(); } while (0)
@ -143,14 +142,16 @@ void
gcd(const MINT *mp1, const MINT *mp2, MINT *rmp) gcd(const MINT *mp1, const MINT *mp2, MINT *rmp)
{ {
BIGNUM b; BIGNUM b;
BN_CTX c; BN_CTX *c;
BN_CTX_init(&c); c = BN_CTX_new();
if (c == NULL)
_bnerr("gcd");
BN_init(&b); BN_init(&b);
BN_ERRCHECK("gcd", BN_gcd(&b, mp1->bn, mp2->bn, &c)); BN_ERRCHECK("gcd", BN_gcd(&b, mp1->bn, mp2->bn, c));
_moveb("gcd", &b, rmp); _moveb("gcd", &b, rmp);
BN_free(&b); BN_free(&b);
BN_CTX_free(&c); BN_CTX_free(c);
} }
/* /*
@ -225,17 +226,19 @@ static void
_mdiv(const char *msg, const MINT *nmp, const MINT *dmp, MINT *qmp, MINT *rmp) _mdiv(const char *msg, const MINT *nmp, const MINT *dmp, MINT *qmp, MINT *rmp)
{ {
BIGNUM q, r; BIGNUM q, r;
BN_CTX c; BN_CTX *c;
BN_CTX_init(&c); c = BN_CTX_new();
if (c == NULL)
_bnerr(msg);
BN_init(&r); BN_init(&r);
BN_init(&q); BN_init(&q);
BN_ERRCHECK(msg, BN_div(&q, &r, nmp->bn, dmp->bn, &c)); BN_ERRCHECK(msg, BN_div(&q, &r, nmp->bn, dmp->bn, c));
_moveb(msg, &q, qmp); _moveb(msg, &q, qmp);
_moveb(msg, &r, rmp); _moveb(msg, &r, rmp);
BN_free(&q); BN_free(&q);
BN_free(&r); BN_free(&r);
BN_CTX_free(&c); BN_CTX_free(c);
} }
void void
@ -470,14 +473,16 @@ static void
_mult(const char *msg, const MINT *mp1, const MINT *mp2, MINT *rmp) _mult(const char *msg, const MINT *mp1, const MINT *mp2, MINT *rmp)
{ {
BIGNUM b; BIGNUM b;
BN_CTX c; BN_CTX *c;
BN_CTX_init(&c); c = BN_CTX_new();
if (c == NULL)
_bnerr(msg);
BN_init(&b); BN_init(&b);
BN_ERRCHECK(msg, BN_mul(&b, mp1->bn, mp2->bn, &c)); BN_ERRCHECK(msg, BN_mul(&b, mp1->bn, mp2->bn, c));
_moveb(msg, &b, rmp); _moveb(msg, &b, rmp);
BN_free(&b); BN_free(&b);
BN_CTX_free(&c); BN_CTX_free(c);
} }
void void
@ -495,14 +500,16 @@ void
pow(const MINT *bmp, const MINT *emp, const MINT *mmp, MINT *rmp) pow(const MINT *bmp, const MINT *emp, const MINT *mmp, MINT *rmp)
{ {
BIGNUM b; BIGNUM b;
BN_CTX c; BN_CTX *c;
BN_CTX_init(&c); c = BN_CTX_new();
if (c == NULL)
_bnerr("pow");
BN_init(&b); BN_init(&b);
BN_ERRCHECK("pow", BN_mod_exp(&b, bmp->bn, emp->bn, mmp->bn, &c)); BN_ERRCHECK("pow", BN_mod_exp(&b, bmp->bn, emp->bn, mmp->bn, c));
_moveb("pow", &b, rmp); _moveb("pow", &b, rmp);
BN_free(&b); BN_free(&b);
BN_CTX_free(&c); BN_CTX_free(c);
} }
/* /*
@ -513,16 +520,18 @@ rpow(const MINT *bmp, short e, MINT *rmp)
{ {
MINT *emp; MINT *emp;
BIGNUM b; BIGNUM b;
BN_CTX c; BN_CTX *c;
BN_CTX_init(&c); c = BN_CTX_new();
if (c == NULL)
_bnerr("rpow");
BN_init(&b); BN_init(&b);
emp = _itom("rpow", e); emp = _itom("rpow", e);
BN_ERRCHECK("rpow", BN_exp(&b, bmp->bn, emp->bn, &c)); BN_ERRCHECK("rpow", BN_exp(&b, bmp->bn, emp->bn, c));
_moveb("rpow", &b, rmp); _moveb("rpow", &b, rmp);
_mfree("rpow", emp); _mfree("rpow", emp);
BN_free(&b); BN_free(&b);
BN_CTX_free(&c); BN_CTX_free(c);
} }
/* /*
@ -533,15 +542,17 @@ _sdiv(const char *msg, const MINT *nmp, short d, MINT *qmp, short *ro)
{ {
MINT *dmp, *rmp; MINT *dmp, *rmp;
BIGNUM q, r; BIGNUM q, r;
BN_CTX c; BN_CTX *c;
char *s; char *s;
BN_CTX_init(&c); c = BN_CTX_new();
if (c == NULL)
_bnerr(msg);
BN_init(&q); BN_init(&q);
BN_init(&r); BN_init(&r);
dmp = _itom(msg, d); dmp = _itom(msg, d);
rmp = _itom(msg, 0); rmp = _itom(msg, 0);
BN_ERRCHECK(msg, BN_div(&q, &r, nmp->bn, dmp->bn, &c)); BN_ERRCHECK(msg, BN_div(&q, &r, nmp->bn, dmp->bn, c));
_moveb(msg, &q, qmp); _moveb(msg, &q, qmp);
_moveb(msg, &r, rmp); _moveb(msg, &r, rmp);
s = _mtox(msg, rmp); s = _mtox(msg, rmp);
@ -554,7 +565,7 @@ _sdiv(const char *msg, const MINT *nmp, short d, MINT *qmp, short *ro)
_mfree(msg, rmp); _mfree(msg, rmp);
BN_free(&r); BN_free(&r);
BN_free(&q); BN_free(&q);
BN_CTX_free(&c); BN_CTX_free(c);
} }
void void