summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bn
diff options
context:
space:
mode:
authorjsing <>2015-02-09 15:49:22 +0000
committerjsing <>2015-02-09 15:49:22 +0000
commit16f790d01f7a6fc6c94e2a033a67b80c8ec5291c (patch)
treed924c624d5eb949a9e7e395dc99d92616e911ce9 /src/lib/libcrypto/bn
parent42f7780549de5b7b5e3e7943cfef87e0e41970fc (diff)
downloadopenbsd-16f790d01f7a6fc6c94e2a033a67b80c8ec5291c.tar.gz
openbsd-16f790d01f7a6fc6c94e2a033a67b80c8ec5291c.tar.bz2
openbsd-16f790d01f7a6fc6c94e2a033a67b80c8ec5291c.zip
BN_CTX_get() can fail - consistently check its return value.
There are currently cases where the return from each call is checked, the return from only the last call is checked and cases where it is not checked at all (including code in bn, ec and engine). Checking the last return value is valid as once the function fails it will continue to return NULL. However, in order to be consistent check each call with the same idiom. This makes it easy to verify. Note there are still a handful of cases that do not follow the idiom - these will be handled separately. ok beck@ doug@
Diffstat (limited to 'src/lib/libcrypto/bn')
-rw-r--r--src/lib/libcrypto/bn/bn_div.c4
-rw-r--r--src/lib/libcrypto/bn/bn_exp.c32
-rw-r--r--src/lib/libcrypto/bn/bn_exp2.c14
-rw-r--r--src/lib/libcrypto/bn/bn_gcd.c50
-rw-r--r--src/lib/libcrypto/bn/bn_gf2m.c35
-rw-r--r--src/lib/libcrypto/bn/bn_kron.c8
-rw-r--r--src/lib/libcrypto/bn/bn_mont.c11
-rw-r--r--src/lib/libcrypto/bn/bn_mul.c8
-rw-r--r--src/lib/libcrypto/bn/bn_prime.c23
-rw-r--r--src/lib/libcrypto/bn/bn_sqr.c4
-rw-r--r--src/lib/libcrypto/bn/bn_sqrt.c20
-rw-r--r--src/lib/libcrypto/bn/bn_x931p.c41
12 files changed, 140 insertions, 110 deletions
diff --git a/src/lib/libcrypto/bn/bn_div.c b/src/lib/libcrypto/bn/bn_div.c
index f4deccf77f..fefc53f9fa 100644
--- a/src/lib/libcrypto/bn/bn_div.c
+++ b/src/lib/libcrypto/bn/bn_div.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_div.c,v 1.22 2014/07/11 08:44:47 jsing Exp $ */ 1/* $OpenBSD: bn_div.c,v 1.23 2015/02/09 15:49:22 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -170,7 +170,7 @@ BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
170 res = BN_CTX_get(ctx); 170 res = BN_CTX_get(ctx);
171 else 171 else
172 res = dv; 172 res = dv;
173 if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL) 173 if (tmp == NULL || snum == NULL || sdiv == NULL || res == NULL)
174 goto err; 174 goto err;
175 175
176 /* First we normalise the numbers */ 176 /* First we normalise the numbers */
diff --git a/src/lib/libcrypto/bn/bn_exp.c b/src/lib/libcrypto/bn/bn_exp.c
index 1aa5503dae..eecab5163b 100644
--- a/src/lib/libcrypto/bn/bn_exp.c
+++ b/src/lib/libcrypto/bn/bn_exp.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_exp.c,v 1.19 2014/07/11 15:01:49 miod Exp $ */ 1/* $OpenBSD: bn_exp.c,v 1.20 2015/02/09 15:49:22 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -272,9 +272,9 @@ BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
272 } 272 }
273 273
274 BN_CTX_start(ctx); 274 BN_CTX_start(ctx);
275 aa = BN_CTX_get(ctx); 275 if ((aa = BN_CTX_get(ctx)) == NULL)
276 val[0] = BN_CTX_get(ctx); 276 goto err;
277 if (!aa || !val[0]) 277 if ((val[0] = BN_CTX_get(ctx)) == NULL)
278 goto err; 278 goto err;
279 279
280 BN_RECP_CTX_init(&recp); 280 BN_RECP_CTX_init(&recp);
@@ -408,10 +408,11 @@ BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
408 } 408 }
409 409
410 BN_CTX_start(ctx); 410 BN_CTX_start(ctx);
411 d = BN_CTX_get(ctx); 411 if ((d = BN_CTX_get(ctx)) == NULL)
412 r = BN_CTX_get(ctx); 412 goto err;
413 val[0] = BN_CTX_get(ctx); 413 if ((r = BN_CTX_get(ctx)) == NULL)
414 if (!d || !r || !val[0]) 414 goto err;
415 if ((val[0] = BN_CTX_get(ctx)) == NULL)
415 goto err; 416 goto err;
416 417
417 /* If this is not done, things will break in the montgomery 418 /* If this is not done, things will break in the montgomery
@@ -885,10 +886,11 @@ BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p, const BIGNUM *m,
885 } 886 }
886 887
887 BN_CTX_start(ctx); 888 BN_CTX_start(ctx);
888 d = BN_CTX_get(ctx); 889 if ((d = BN_CTX_get(ctx)) == NULL)
889 r = BN_CTX_get(ctx); 890 goto err;
890 t = BN_CTX_get(ctx); 891 if ((r = BN_CTX_get(ctx)) == NULL)
891 if (d == NULL || r == NULL || t == NULL) 892 goto err;
893 if ((t = BN_CTX_get(ctx)) == NULL)
892 goto err; 894 goto err;
893 895
894 if (in_mont != NULL) 896 if (in_mont != NULL)
@@ -1003,9 +1005,9 @@ BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
1003 } 1005 }
1004 1006
1005 BN_CTX_start(ctx); 1007 BN_CTX_start(ctx);
1006 d = BN_CTX_get(ctx); 1008 if ((d = BN_CTX_get(ctx)) == NULL)
1007 val[0] = BN_CTX_get(ctx); 1009 goto err;
1008 if (!d || !val[0]) 1010 if ((val[0] = BN_CTX_get(ctx)) == NULL)
1009 goto err; 1011 goto err;
1010 1012
1011 if (!BN_nnmod(val[0],a,m,ctx)) 1013 if (!BN_nnmod(val[0],a,m,ctx))
diff --git a/src/lib/libcrypto/bn/bn_exp2.c b/src/lib/libcrypto/bn/bn_exp2.c
index c8f0294f7a..38bf467a38 100644
--- a/src/lib/libcrypto/bn/bn_exp2.c
+++ b/src/lib/libcrypto/bn/bn_exp2.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_exp2.c,v 1.9 2014/07/11 08:44:47 jsing Exp $ */ 1/* $OpenBSD: bn_exp2.c,v 1.10 2015/02/09 15:49:22 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -150,11 +150,13 @@ BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
150 bits = (bits1 > bits2) ? bits1 : bits2; 150 bits = (bits1 > bits2) ? bits1 : bits2;
151 151
152 BN_CTX_start(ctx); 152 BN_CTX_start(ctx);
153 d = BN_CTX_get(ctx); 153 if ((d = BN_CTX_get(ctx)) == NULL)
154 r = BN_CTX_get(ctx); 154 goto err;
155 val1[0] = BN_CTX_get(ctx); 155 if ((r = BN_CTX_get(ctx)) == NULL)
156 val2[0] = BN_CTX_get(ctx); 156 goto err;
157 if (!d || !r || !val1[0] || !val2[0]) 157 if ((val1[0] = BN_CTX_get(ctx)) == NULL)
158 goto err;
159 if ((val2[0] = BN_CTX_get(ctx)) == NULL)
158 goto err; 160 goto err;
159 161
160 if (in_mont != NULL) 162 if (in_mont != NULL)
diff --git a/src/lib/libcrypto/bn/bn_gcd.c b/src/lib/libcrypto/bn/bn_gcd.c
index 379bea99ad..da9c29a8e5 100644
--- a/src/lib/libcrypto/bn/bn_gcd.c
+++ b/src/lib/libcrypto/bn/bn_gcd.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_gcd.c,v 1.9 2014/07/11 08:44:47 jsing Exp $ */ 1/* $OpenBSD: bn_gcd.c,v 1.10 2015/02/09 15:49:22 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -125,9 +125,9 @@ BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
125 bn_check_top(in_b); 125 bn_check_top(in_b);
126 126
127 BN_CTX_start(ctx); 127 BN_CTX_start(ctx);
128 a = BN_CTX_get(ctx); 128 if ((a = BN_CTX_get(ctx)) == NULL)
129 b = BN_CTX_get(ctx); 129 goto err;
130 if (a == NULL || b == NULL) 130 if ((b = BN_CTX_get(ctx)) == NULL)
131 goto err; 131 goto err;
132 132
133 if (BN_copy(a, in_a) == NULL) 133 if (BN_copy(a, in_a) == NULL)
@@ -247,14 +247,19 @@ BN_mod_inverse(BIGNUM *in, const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
247 bn_check_top(n); 247 bn_check_top(n);
248 248
249 BN_CTX_start(ctx); 249 BN_CTX_start(ctx);
250 A = BN_CTX_get(ctx); 250 if ((A = BN_CTX_get(ctx)) == NULL)
251 B = BN_CTX_get(ctx); 251 goto err;
252 X = BN_CTX_get(ctx); 252 if ((B = BN_CTX_get(ctx)) == NULL)
253 D = BN_CTX_get(ctx); 253 goto err;
254 M = BN_CTX_get(ctx); 254 if ((X = BN_CTX_get(ctx)) == NULL)
255 Y = BN_CTX_get(ctx); 255 goto err;
256 T = BN_CTX_get(ctx); 256 if ((D = BN_CTX_get(ctx)) == NULL)
257 if (T == NULL) 257 goto err;
258 if ((M = BN_CTX_get(ctx)) == NULL)
259 goto err;
260 if ((Y = BN_CTX_get(ctx)) == NULL)
261 goto err;
262 if ((T = BN_CTX_get(ctx)) == NULL)
258 goto err; 263 goto err;
259 264
260 if (in == NULL) 265 if (in == NULL)
@@ -537,14 +542,19 @@ BN_mod_inverse_no_branch(BIGNUM *in, const BIGNUM *a, const BIGNUM *n,
537 bn_check_top(n); 542 bn_check_top(n);
538 543
539 BN_CTX_start(ctx); 544 BN_CTX_start(ctx);
540 A = BN_CTX_get(ctx); 545 if ((A = BN_CTX_get(ctx)) == NULL)
541 B = BN_CTX_get(ctx); 546 goto err;
542 X = BN_CTX_get(ctx); 547 if ((B = BN_CTX_get(ctx)) == NULL)
543 D = BN_CTX_get(ctx); 548 goto err;
544 M = BN_CTX_get(ctx); 549 if ((X = BN_CTX_get(ctx)) == NULL)
545 Y = BN_CTX_get(ctx); 550 goto err;
546 T = BN_CTX_get(ctx); 551 if ((D = BN_CTX_get(ctx)) == NULL)
547 if (T == NULL) 552 goto err;
553 if ((M = BN_CTX_get(ctx)) == NULL)
554 goto err;
555 if ((Y = BN_CTX_get(ctx)) == NULL)
556 goto err;
557 if ((T = BN_CTX_get(ctx)) == NULL)
548 goto err; 558 goto err;
549 559
550 if (in == NULL) 560 if (in == NULL)
diff --git a/src/lib/libcrypto/bn/bn_gf2m.c b/src/lib/libcrypto/bn/bn_gf2m.c
index 1cd38c7797..4544369248 100644
--- a/src/lib/libcrypto/bn/bn_gf2m.c
+++ b/src/lib/libcrypto/bn/bn_gf2m.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_gf2m.c,v 1.16 2014/10/28 07:35:58 jsg Exp $ */ 1/* $OpenBSD: bn_gf2m.c,v 1.17 2015/02/09 15:49:22 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4 * 4 *
@@ -840,8 +840,7 @@ BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p,
840 bn_check_top(p); 840 bn_check_top(p);
841 841
842 BN_CTX_start(ctx); 842 BN_CTX_start(ctx);
843 xinv = BN_CTX_get(ctx); 843 if ((xinv = BN_CTX_get(ctx)) == NULL)
844 if (xinv == NULL)
845 goto err; 844 goto err;
846 845
847 if (!BN_GF2m_mod_inv(xinv, x, p, ctx)) 846 if (!BN_GF2m_mod_inv(xinv, x, p, ctx))
@@ -875,11 +874,13 @@ BN_GF2m_mod_div(BIGNUM *r, const BIGNUM *y, const BIGNUM *x, const BIGNUM *p,
875 874
876 BN_CTX_start(ctx); 875 BN_CTX_start(ctx);
877 876
878 a = BN_CTX_get(ctx); 877 if ((a = BN_CTX_get(ctx)) == NULL)
879 b = BN_CTX_get(ctx); 878 goto err;
880 u = BN_CTX_get(ctx); 879 if ((b = BN_CTX_get(ctx)) == NULL)
881 v = BN_CTX_get(ctx); 880 goto err;
882 if (v == NULL) 881 if ((u = BN_CTX_get(ctx)) == NULL)
882 goto err;
883 if ((v = BN_CTX_get(ctx)) == NULL)
883 goto err; 884 goto err;
884 885
885 /* reduce x and y mod p */ 886 /* reduce x and y mod p */
@@ -1137,10 +1138,11 @@ BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
1137 } 1138 }
1138 1139
1139 BN_CTX_start(ctx); 1140 BN_CTX_start(ctx);
1140 a = BN_CTX_get(ctx); 1141 if ((a = BN_CTX_get(ctx)) == NULL)
1141 z = BN_CTX_get(ctx); 1142 goto err;
1142 w = BN_CTX_get(ctx); 1143 if ((z = BN_CTX_get(ctx)) == NULL)
1143 if (w == NULL) 1144 goto err;
1145 if ((w = BN_CTX_get(ctx)) == NULL)
1144 goto err; 1146 goto err;
1145 1147
1146 if (!BN_GF2m_mod_arr(a, a_, p)) 1148 if (!BN_GF2m_mod_arr(a, a_, p))
@@ -1169,10 +1171,11 @@ BN_GF2m_mod_solve_quad_arr(BIGNUM *r, const BIGNUM *a_, const int p[],
1169 } 1171 }
1170 else /* m is even */ 1172 else /* m is even */
1171 { 1173 {
1172 rho = BN_CTX_get(ctx); 1174 if ((rho = BN_CTX_get(ctx)) == NULL)
1173 w2 = BN_CTX_get(ctx); 1175 goto err;
1174 tmp = BN_CTX_get(ctx); 1176 if ((w2 = BN_CTX_get(ctx)) == NULL)
1175 if (tmp == NULL) 1177 goto err;
1178 if ((tmp = BN_CTX_get(ctx)) == NULL)
1176 goto err; 1179 goto err;
1177 do { 1180 do {
1178 if (!BN_rand(rho, p[0], 0, 0)) 1181 if (!BN_rand(rho, p[0], 0, 0))
diff --git a/src/lib/libcrypto/bn/bn_kron.c b/src/lib/libcrypto/bn/bn_kron.c
index 699cda55f0..274da5d186 100644
--- a/src/lib/libcrypto/bn/bn_kron.c
+++ b/src/lib/libcrypto/bn/bn_kron.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_kron.c,v 1.5 2014/07/11 08:44:48 jsing Exp $ */ 1/* $OpenBSD: bn_kron.c,v 1.6 2015/02/09 15:49:22 jsing Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -79,9 +79,9 @@ BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
79 bn_check_top(b); 79 bn_check_top(b);
80 80
81 BN_CTX_start(ctx); 81 BN_CTX_start(ctx);
82 A = BN_CTX_get(ctx); 82 if ((A = BN_CTX_get(ctx)) == NULL)
83 B = BN_CTX_get(ctx); 83 goto end;
84 if (B == NULL) 84 if ((B = BN_CTX_get(ctx)) == NULL)
85 goto end; 85 goto end;
86 86
87 err = !BN_copy(A, a); 87 err = !BN_copy(A, a);
diff --git a/src/lib/libcrypto/bn/bn_mont.c b/src/lib/libcrypto/bn/bn_mont.c
index 5803ca493d..3eb9913a9e 100644
--- a/src/lib/libcrypto/bn/bn_mont.c
+++ b/src/lib/libcrypto/bn/bn_mont.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mont.c,v 1.23 2014/07/11 08:44:48 jsing Exp $ */ 1/* $OpenBSD: bn_mont.c,v 1.24 2015/02/09 15:49:22 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -149,8 +149,7 @@ BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
149#endif 149#endif
150 150
151 BN_CTX_start(ctx); 151 BN_CTX_start(ctx);
152 tmp = BN_CTX_get(ctx); 152 if ((tmp = BN_CTX_get(ctx)) == NULL)
153 if (tmp == NULL)
154 goto err; 153 goto err;
155 154
156 bn_check_top(tmp); 155 bn_check_top(tmp);
@@ -288,9 +287,9 @@ BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont, BN_CTX *ctx)
288 BIGNUM *t1, *t2; 287 BIGNUM *t1, *t2;
289 288
290 BN_CTX_start(ctx); 289 BN_CTX_start(ctx);
291 t1 = BN_CTX_get(ctx); 290 if ((t1 = BN_CTX_get(ctx)) == NULL)
292 t2 = BN_CTX_get(ctx); 291 goto err;
293 if (t1 == NULL || t2 == NULL) 292 if ((t2 = BN_CTX_get(ctx)) == NULL)
294 goto err; 293 goto err;
295 294
296 if (!BN_copy(t1, a)) 295 if (!BN_copy(t1, a))
diff --git a/src/lib/libcrypto/bn/bn_mul.c b/src/lib/libcrypto/bn/bn_mul.c
index daba02d6ca..7794d59707 100644
--- a/src/lib/libcrypto/bn/bn_mul.c
+++ b/src/lib/libcrypto/bn/bn_mul.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_mul.c,v 1.19 2014/07/11 08:44:48 jsing Exp $ */ 1/* $OpenBSD: bn_mul.c,v 1.20 2015/02/09 15:49:22 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -1012,8 +1012,7 @@ BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
1012 j = 1 << (j - 1); 1012 j = 1 << (j - 1);
1013 assert(j <= al || j <= bl); 1013 assert(j <= al || j <= bl);
1014 k = j + j; 1014 k = j + j;
1015 t = BN_CTX_get(ctx); 1015 if ((t = BN_CTX_get(ctx)) == NULL)
1016 if (t == NULL)
1017 goto err; 1016 goto err;
1018 if (al > j || bl > j) { 1017 if (al > j || bl > j) {
1019 if (bn_wexpand(t, k * 4) == NULL) 1018 if (bn_wexpand(t, k * 4) == NULL)
@@ -1057,7 +1056,8 @@ BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
1057 j = BN_num_bits_word((BN_ULONG)al); 1056 j = BN_num_bits_word((BN_ULONG)al);
1058 j = 1 << (j - 1); 1057 j = 1 << (j - 1);
1059 k = j + j; 1058 k = j + j;
1060 t = BN_CTX_get(ctx); 1059 if ((t = BN_CTX_get(ctx)) == NULL)
1060 goto err;
1061 if (al == j) /* exact multiple */ 1061 if (al == j) /* exact multiple */
1062 { 1062 {
1063 if (bn_wexpand(t, k * 2) == NULL) 1063 if (bn_wexpand(t, k * 2) == NULL)
diff --git a/src/lib/libcrypto/bn/bn_prime.c b/src/lib/libcrypto/bn/bn_prime.c
index e5cd315e47..02780d32e6 100644
--- a/src/lib/libcrypto/bn/bn_prime.c
+++ b/src/lib/libcrypto/bn/bn_prime.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_prime.c,v 1.12 2014/10/18 17:20:40 jsing Exp $ */ 1/* $OpenBSD: bn_prime.c,v 1.13 2015/02/09 15:49:22 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -170,8 +170,7 @@ BN_generate_prime_ex(BIGNUM *ret, int bits, int safe, const BIGNUM *add,
170 if (ctx == NULL) 170 if (ctx == NULL)
171 goto err; 171 goto err;
172 BN_CTX_start(ctx); 172 BN_CTX_start(ctx);
173 t = BN_CTX_get(ctx); 173 if ((t = BN_CTX_get(ctx)) == NULL)
174 if (!t)
175 goto err; 174 goto err;
176loop: 175loop:
177 /* make a random number and set the top and bottom bits */ 176 /* make a random number and set the top and bottom bits */
@@ -287,10 +286,11 @@ BN_is_prime_fasttest_ex(const BIGNUM *a, int checks, BN_CTX *ctx_passed,
287 A = t; 286 A = t;
288 } else 287 } else
289 A = a; 288 A = a;
290 A1 = BN_CTX_get(ctx); 289 if ((A1 = BN_CTX_get(ctx)) == NULL)
291 A1_odd = BN_CTX_get(ctx); 290 goto err;
292 check = BN_CTX_get(ctx); 291 if ((A1_odd = BN_CTX_get(ctx)) == NULL)
293 if (check == NULL) 292 goto err;
293 if ((check = BN_CTX_get(ctx)) == NULL)
294 goto err; 294 goto err;
295 295
296 /* compute A1 := A - 1 */ 296 /* compute A1 := A - 1 */
@@ -461,10 +461,11 @@ probable_prime_dh_safe(BIGNUM *p, int bits, const BIGNUM *padd,
461 461
462 bits--; 462 bits--;
463 BN_CTX_start(ctx); 463 BN_CTX_start(ctx);
464 t1 = BN_CTX_get(ctx); 464 if ((t1 = BN_CTX_get(ctx)) == NULL)
465 q = BN_CTX_get(ctx); 465 goto err;
466 qadd = BN_CTX_get(ctx); 466 if ((q = BN_CTX_get(ctx)) == NULL)
467 if (qadd == NULL) 467 goto err;
468 if ((qadd = BN_CTX_get(ctx)) == NULL)
468 goto err; 469 goto err;
469 470
470 if (!BN_rshift1(qadd, padd)) 471 if (!BN_rshift1(qadd, padd))
diff --git a/src/lib/libcrypto/bn/bn_sqr.c b/src/lib/libcrypto/bn/bn_sqr.c
index 5ea9fb083d..a0dce6ea81 100644
--- a/src/lib/libcrypto/bn/bn_sqr.c
+++ b/src/lib/libcrypto/bn/bn_sqr.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_sqr.c,v 1.11 2014/07/11 13:26:31 miod Exp $ */ 1/* $OpenBSD: bn_sqr.c,v 1.12 2015/02/09 15:49:22 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -85,7 +85,7 @@ BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
85 BN_CTX_start(ctx); 85 BN_CTX_start(ctx);
86 rr = (a != r) ? r : BN_CTX_get(ctx); 86 rr = (a != r) ? r : BN_CTX_get(ctx);
87 tmp = BN_CTX_get(ctx); 87 tmp = BN_CTX_get(ctx);
88 if (!rr || !tmp) 88 if (rr == NULL || tmp == NULL)
89 goto err; 89 goto err;
90 90
91 max = 2 * al; /* Non-zero (from above) */ 91 max = 2 * al; /* Non-zero (from above) */
diff --git a/src/lib/libcrypto/bn/bn_sqrt.c b/src/lib/libcrypto/bn/bn_sqrt.c
index 9a59d56b3f..f94fa41094 100644
--- a/src/lib/libcrypto/bn/bn_sqrt.c
+++ b/src/lib/libcrypto/bn/bn_sqrt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_sqrt.c,v 1.5 2014/07/11 08:44:48 jsing Exp $ */ 1/* $OpenBSD: bn_sqrt.c,v 1.6 2015/02/09 15:49:22 jsing Exp $ */
2/* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de> 2/* Written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3 * and Bodo Moeller for the OpenSSL project. */ 3 * and Bodo Moeller for the OpenSSL project. */
4/* ==================================================================== 4/* ====================================================================
@@ -108,13 +108,17 @@ BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
108 } 108 }
109 109
110 BN_CTX_start(ctx); 110 BN_CTX_start(ctx);
111 A = BN_CTX_get(ctx); 111 if ((A = BN_CTX_get(ctx)) == NULL)
112 b = BN_CTX_get(ctx); 112 goto end;
113 q = BN_CTX_get(ctx); 113 if ((b = BN_CTX_get(ctx)) == NULL)
114 t = BN_CTX_get(ctx); 114 goto end;
115 x = BN_CTX_get(ctx); 115 if ((q = BN_CTX_get(ctx)) == NULL)
116 y = BN_CTX_get(ctx); 116 goto end;
117 if (y == NULL) 117 if ((t = BN_CTX_get(ctx)) == NULL)
118 goto end;
119 if ((x = BN_CTX_get(ctx)) == NULL)
120 goto end;
121 if ((y = BN_CTX_get(ctx)) == NULL)
118 goto end; 122 goto end;
119 123
120 if (ret == NULL) 124 if (ret == NULL)
diff --git a/src/lib/libcrypto/bn/bn_x931p.c b/src/lib/libcrypto/bn/bn_x931p.c
index fc8d4c3c49..89c86c1ff7 100644
--- a/src/lib/libcrypto/bn/bn_x931p.c
+++ b/src/lib/libcrypto/bn/bn_x931p.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_x931p.c,v 1.5 2014/06/12 15:49:28 deraadt Exp $ */ 1/* $OpenBSD: bn_x931p.c,v 1.6 2015/02/09 15:49:22 jsing Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2005. 3 * project 2005.
4 */ 4 */
@@ -107,17 +107,21 @@ BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, const BIGNUM *Xp,
107 return 0; 107 return 0;
108 108
109 BN_CTX_start(ctx); 109 BN_CTX_start(ctx);
110 if (!p1) 110 if (p1 != NULL) {
111 p1 = BN_CTX_get(ctx); 111 if ((p1 = BN_CTX_get(ctx)) == NULL)
112 112 goto err;
113 if (!p2) 113 }
114 p2 = BN_CTX_get(ctx); 114 if (p2 != NULL) {
115 115 if ((p2 = BN_CTX_get(ctx)) == NULL)
116 t = BN_CTX_get(ctx); 116 goto err;
117 117 }
118 p1p2 = BN_CTX_get(ctx);
119 118
120 pm1 = BN_CTX_get(ctx); 119 if ((t = BN_CTX_get(ctx)) == NULL)
120 goto err;
121 if ((p1p2 = BN_CTX_get(ctx)) == NULL)
122 goto err;
123 if ((pm1 = BN_CTX_get(ctx)) == NULL)
124 goto err;
121 125
122 if (!bn_x931_derive_pi(p1, Xp1, ctx, cb)) 126 if (!bn_x931_derive_pi(p1, Xp1, ctx, cb))
123 goto err; 127 goto err;
@@ -213,7 +217,8 @@ BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx)
213 return 0; 217 return 0;
214 218
215 BN_CTX_start(ctx); 219 BN_CTX_start(ctx);
216 t = BN_CTX_get(ctx); 220 if ((t = BN_CTX_get(ctx)) == NULL)
221 return 0;
217 222
218 for (i = 0; i < 1000; i++) { 223 for (i = 0; i < 1000; i++) {
219 if (!BN_rand(Xq, nbits, 1, 0)) 224 if (!BN_rand(Xq, nbits, 1, 0))
@@ -247,10 +252,14 @@ BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2, BIGNUM *Xp1,
247 int ret = 0; 252 int ret = 0;
248 253
249 BN_CTX_start(ctx); 254 BN_CTX_start(ctx);
250 if (!Xp1) 255 if (Xp1 != NULL) {
251 Xp1 = BN_CTX_get(ctx); 256 if ((Xp1 = BN_CTX_get(ctx)) == NULL)
252 if (!Xp2) 257 goto error;
253 Xp2 = BN_CTX_get(ctx); 258 }
259 if (Xp2 != NULL) {
260 if ((Xp2 = BN_CTX_get(ctx)) == NULL)
261 goto error;
262 }
254 263
255 if (!BN_rand(Xp1, 101, 0, 0)) 264 if (!BN_rand(Xp1, 101, 0, 0))
256 goto error; 265 goto error;