summaryrefslogtreecommitdiff
path: root/lib/libcrypto/bn
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>2017-05-02 03:59:46 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>2017-05-02 03:59:46 +0000
commitcc4b23eafa00f2d02bd6a5aeb37a603e5616a1b5 (patch)
tree0c263850a80e2d5ef373e8dffa7717aaaa1e4a4a /lib/libcrypto/bn
parentcc386e2f2850053dd843b8a7630c3162a953abc8 (diff)
use freezero() instead of memset/explicit_bzero + free. Substantially
reduces conditional logic (-218, +82). MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH cache alignment calculation bn/bn_exp.c wasn'tt quite right. Two other tricky bits with ASN1_STRING_FLAG_NDEF and BN_FLG_STATIC_DATA where the condition cannot be collapsed completely. Passes regress. ok beck
Diffstat (limited to 'lib/libcrypto/bn')
-rw-r--r--lib/libcrypto/bn/bn_asm.c8
-rw-r--r--lib/libcrypto/bn/bn_exp.c13
-rw-r--r--lib/libcrypto/bn/bn_lib.c14
-rw-r--r--lib/libcrypto/bn/bn_rand.c7
4 files changed, 14 insertions, 28 deletions
diff --git a/lib/libcrypto/bn/bn_asm.c b/lib/libcrypto/bn/bn_asm.c
index 49f0ba5d7b5..993fbb3dc57 100644
--- a/lib/libcrypto/bn/bn_asm.c
+++ b/lib/libcrypto/bn/bn_asm.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bn_asm.c,v 1.14 2015/02/25 15:39:49 bcook Exp $ */
+/* $OpenBSD: bn_asm.c,v 1.15 2017/05/02 03:59:44 deraadt Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
@@ -989,8 +989,7 @@ enter:
}
memcpy(rp, tp, num * sizeof(BN_ULONG));
out:
- explicit_bzero(tp, (num + 2) * sizeof(BN_ULONG));
- free(tp);
+ freezero(tp, (num + 2) * sizeof(BN_ULONG));
return 1;
}
#else
@@ -1081,8 +1080,7 @@ bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
}
memcpy(rp, tp, num * sizeof(BN_ULONG));
out:
- explicit_bzero(tp, (num + 2) * sizeof(BN_ULONG));
- free(tp);
+ freezero(tp, (num + 2) * sizeof(BN_ULONG));
return 1;
}
#else
diff --git a/lib/libcrypto/bn/bn_exp.c b/lib/libcrypto/bn/bn_exp.c
index d388758927f..b778d5d67c7 100644
--- a/lib/libcrypto/bn/bn_exp.c
+++ b/lib/libcrypto/bn/bn_exp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bn_exp.c,v 1.30 2017/01/29 17:49:22 beck Exp $ */
+/* $OpenBSD: bn_exp.c,v 1.31 2017/05/02 03:59:44 deraadt Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
@@ -706,12 +706,10 @@ BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
numPowers = 1 << window;
powerbufLen = sizeof(m->d[0]) * (top * numPowers +
((2*top) > numPowers ? (2*top) : numPowers));
- if ((powerbufFree = malloc(powerbufLen +
- MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL)
+ if ((powerbufFree = calloc(powerbufLen +
+ MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH, 1)) == NULL)
goto err;
-
powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);
- memset(powerbuf, 0, powerbufLen);
/* lay down tmp and am right after powers table */
tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers);
@@ -901,10 +899,7 @@ BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
err:
if ((in_mont == NULL) && (mont != NULL))
BN_MONT_CTX_free(mont);
- if (powerbuf != NULL) {
- explicit_bzero(powerbuf, powerbufLen);
- free(powerbufFree);
- }
+ freezero(powerbufFree, powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);
BN_CTX_end(ctx);
return (ret);
}
diff --git a/lib/libcrypto/bn/bn_lib.c b/lib/libcrypto/bn/bn_lib.c
index f2736e31c3f..8aeeb5304fa 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.37 2017/01/29 17:49:22 beck Exp $ */
+/* $OpenBSD: bn_lib.c,v 1.38 2017/05/02 03:59:44 deraadt Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
@@ -220,10 +220,8 @@ BN_clear_free(BIGNUM *a)
if (a == NULL)
return;
bn_check_top(a);
- if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA))) {
- explicit_bzero(a->d, a->dmax * sizeof(a->d[0]));
- free(a->d);
- }
+ 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)
@@ -393,10 +391,8 @@ bn_expand2(BIGNUM *b, int words)
BN_ULONG *a = bn_expand_internal(b, words);
if (!a)
return NULL;
- if (b->d) {
- explicit_bzero(b->d, b->dmax * sizeof(b->d[0]));
- free(b->d);
- }
+ if (b->d)
+ freezero(b->d, b->dmax * sizeof(b->d[0]));
b->d = a;
b->dmax = words;
}
diff --git a/lib/libcrypto/bn/bn_rand.c b/lib/libcrypto/bn/bn_rand.c
index 812fa6a5752..86257571400 100644
--- a/lib/libcrypto/bn/bn_rand.c
+++ b/lib/libcrypto/bn/bn_rand.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: bn_rand.c,v 1.19 2017/01/29 17:49:22 beck Exp $ */
+/* $OpenBSD: bn_rand.c,v 1.20 2017/05/02 03:59:44 deraadt Exp $ */
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
* All rights reserved.
*
@@ -186,10 +186,7 @@ bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
ret = 1;
err:
- if (buf != NULL) {
- explicit_bzero(buf, bytes);
- free(buf);
- }
+ freezero(buf, bytes);
bn_check_top(rnd);
return (ret);
}