summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorderaadt <>2017-05-02 03:59:45 +0000
committerderaadt <>2017-05-02 03:59:45 +0000
commit2b561cb0e87f2ee535e8c64907883cd275ad3fec (patch)
treebb9d050c5c2984047e6475e087694d6764f24157
parent024e2580a5280d4df3724dab76ce52e14fe2060c (diff)
downloadopenbsd-2b561cb0e87f2ee535e8c64907883cd275ad3fec.tar.gz
openbsd-2b561cb0e87f2ee535e8c64907883cd275ad3fec.tar.bz2
openbsd-2b561cb0e87f2ee535e8c64907883cd275ad3fec.zip
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
-rw-r--r--src/lib/libcrypto/asn1/a_object.c16
-rw-r--r--src/lib/libcrypto/asn1/a_sign.c12
-rw-r--r--src/lib/libcrypto/asn1/a_verify.c5
-rw-r--r--src/lib/libcrypto/asn1/asn1_lib.c12
-rw-r--r--src/lib/libcrypto/bn/bn_asm.c8
-rw-r--r--src/lib/libcrypto/bn/bn_exp.c13
-rw-r--r--src/lib/libcrypto/bn/bn_lib.c14
-rw-r--r--src/lib/libcrypto/bn/bn_rand.c7
-rw-r--r--src/lib/libcrypto/buffer/buffer.c7
-rw-r--r--src/lib/libcrypto/dsa/dsa_asn1.c7
-rw-r--r--src/lib/libcrypto/ec/ec_key.c6
-rw-r--r--src/lib/libcrypto/ec/ec_lib.c13
-rw-r--r--src/lib/libcrypto/ec/ec_mult.c5
-rw-r--r--src/lib/libcrypto/ec/ecp_nistp224.c5
-rw-r--r--src/lib/libcrypto/ec/ecp_nistp256.c5
-rw-r--r--src/lib/libcrypto/ec/ecp_nistp521.c5
-rw-r--r--src/lib/libcrypto/ec/ecp_nistz256.c8
-rw-r--r--src/lib/libcrypto/ecdh/ech_lib.c6
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_lib.c6
-rw-r--r--src/lib/libcrypto/ecdsa/ecs_vrf.c7
-rw-r--r--src/lib/libcrypto/evp/bio_enc.c5
-rw-r--r--src/lib/libcrypto/evp/digest.c11
-rw-r--r--src/lib/libcrypto/evp/e_aes.c5
-rw-r--r--src/lib/libcrypto/evp/e_chacha20poly1305.c5
-rw-r--r--src/lib/libcrypto/evp/p_open.c6
-rw-r--r--src/lib/libcrypto/gost/gostr341001_key.c5
-rw-r--r--src/lib/libcrypto/hmac/hm_pmeth.c9
-rw-r--r--src/lib/libcrypto/malloc-wrapper.c5
-rw-r--r--src/lib/libcrypto/modes/gcm128.c7
-rw-r--r--src/lib/libcrypto/pem/pem_lib.c15
-rw-r--r--src/lib/libcrypto/pem/pem_pkey.c5
-rw-r--r--src/lib/libcrypto/pem/pvkfmt.c7
-rw-r--r--src/lib/libcrypto/pkcs12/p12_key.c7
-rw-r--r--src/lib/libcrypto/pkcs7/pk7_doit.c34
-rw-r--r--src/lib/libcrypto/rsa/rsa_eay.c22
-rw-r--r--src/lib/libcrypto/rsa/rsa_saos.c10
-rw-r--r--src/lib/libcrypto/rsa/rsa_sign.c16
-rw-r--r--src/lib/libtls/tls_config.c6
38 files changed, 109 insertions, 238 deletions
diff --git a/src/lib/libcrypto/asn1/a_object.c b/src/lib/libcrypto/asn1/a_object.c
index 711b01f149..e10af97d36 100644
--- a/src/lib/libcrypto/asn1/a_object.c
+++ b/src/lib/libcrypto/asn1/a_object.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_object.c,v 1.29 2017/01/29 17:49:22 beck Exp $ */ 1/* $OpenBSD: a_object.c,v 1.30 2017/05/02 03:59:44 deraadt 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 *
@@ -231,8 +231,7 @@ i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
231 return -1; 231 return -1;
232 i = i2t_ASN1_OBJECT(tmp, tlen, a); 232 i = i2t_ASN1_OBJECT(tmp, tlen, a);
233 if (i > (int)(tlen - 1)) { 233 if (i > (int)(tlen - 1)) {
234 explicit_bzero(tmp, tlen); 234 freezero(tmp, tlen);
235 free(tmp);
236 if ((tmp = malloc(i + 1)) == NULL) 235 if ((tmp = malloc(i + 1)) == NULL)
237 return -1; 236 return -1;
238 tlen = i + 1; 237 tlen = i + 1;
@@ -242,8 +241,7 @@ i2a_ASN1_OBJECT(BIO *bp, ASN1_OBJECT *a)
242 i = BIO_write(bp, "<INVALID>", 9); 241 i = BIO_write(bp, "<INVALID>", 9);
243 else 242 else
244 i = BIO_write(bp, tmp, i); 243 i = BIO_write(bp, tmp, i);
245 explicit_bzero(tmp, tlen); 244 freezero(tmp, tlen);
246 free(tmp);
247 return (i); 245 return (i);
248} 246}
249 247
@@ -319,9 +317,7 @@ c2i_ASN1_OBJECT(ASN1_OBJECT **a, const unsigned char **pp, long len)
319 317
320 /* detach data from object */ 318 /* detach data from object */
321 data = (unsigned char *)ret->data; 319 data = (unsigned char *)ret->data;
322 if (data != NULL) 320 freezero(data, ret->length);
323 explicit_bzero(data, ret->length);
324 free(data);
325 321
326 data = malloc(length); 322 data = malloc(length);
327 if (data == NULL) { 323 if (data == NULL) {
@@ -380,9 +376,7 @@ ASN1_OBJECT_free(ASN1_OBJECT *a)
380 a->sn = a->ln = NULL; 376 a->sn = a->ln = NULL;
381 } 377 }
382 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) { 378 if (a->flags & ASN1_OBJECT_FLAG_DYNAMIC_DATA) {
383 if (a->data != NULL) 379 freezero((void *)a->data, a->length);
384 explicit_bzero((void *)a->data, a->length);
385 free((void *)a->data);
386 a->data = NULL; 380 a->data = NULL;
387 a->length = 0; 381 a->length = 0;
388 } 382 }
diff --git a/src/lib/libcrypto/asn1/a_sign.c b/src/lib/libcrypto/asn1/a_sign.c
index 4e545eb719..df955be745 100644
--- a/src/lib/libcrypto/asn1/a_sign.c
+++ b/src/lib/libcrypto/asn1/a_sign.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_sign.c,v 1.22 2017/01/29 17:49:22 beck Exp $ */ 1/* $OpenBSD: a_sign.c,v 1.23 2017/05/02 03:59:44 deraadt 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 *
@@ -227,13 +227,7 @@ ASN1_item_sign_ctx(const ASN1_ITEM *it, X509_ALGOR *algor1, X509_ALGOR *algor2,
227 227
228err: 228err:
229 EVP_MD_CTX_cleanup(ctx); 229 EVP_MD_CTX_cleanup(ctx);
230 if (buf_in != NULL) { 230 freezero((char *)buf_in, inl);
231 explicit_bzero((char *)buf_in, inl); 231 freezero((char *)buf_out, outll);
232 free(buf_in);
233 }
234 if (buf_out != NULL) {
235 explicit_bzero((char *)buf_out, outll);
236 free(buf_out);
237 }
238 return (outl); 232 return (outl);
239} 233}
diff --git a/src/lib/libcrypto/asn1/a_verify.c b/src/lib/libcrypto/asn1/a_verify.c
index 8f8e58c095..6f0cd1080b 100644
--- a/src/lib/libcrypto/asn1/a_verify.c
+++ b/src/lib/libcrypto/asn1/a_verify.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_verify.c,v 1.23 2017/01/29 17:49:22 beck Exp $ */ 1/* $OpenBSD: a_verify.c,v 1.24 2017/05/02 03:59:44 deraadt 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 *
@@ -148,8 +148,7 @@ ASN1_item_verify(const ASN1_ITEM *it, X509_ALGOR *a,
148 goto err; 148 goto err;
149 } 149 }
150 150
151 explicit_bzero(buf_in, (unsigned int)inl); 151 freezero(buf_in, (unsigned int)inl);
152 free(buf_in);
153 152
154 if (EVP_DigestVerifyFinal(&ctx, signature->data, 153 if (EVP_DigestVerifyFinal(&ctx, signature->data,
155 (size_t)signature->length) <= 0) { 154 (size_t)signature->length) <= 0) {
diff --git a/src/lib/libcrypto/asn1/asn1_lib.c b/src/lib/libcrypto/asn1/asn1_lib.c
index a90873d54a..852644a781 100644
--- a/src/lib/libcrypto/asn1/asn1_lib.c
+++ b/src/lib/libcrypto/asn1/asn1_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: asn1_lib.c,v 1.38 2017/01/29 17:49:22 beck Exp $ */ 1/* $OpenBSD: asn1_lib.c,v 1.39 2017/05/02 03:59:44 deraadt 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 *
@@ -401,9 +401,7 @@ ASN1_STRING_set(ASN1_STRING *str, const void *_data, int len)
401void 401void
402ASN1_STRING_set0(ASN1_STRING *str, void *data, int len) 402ASN1_STRING_set0(ASN1_STRING *str, void *data, int len)
403{ 403{
404 if (str->data != NULL) 404 freezero(str->data, str->length);
405 explicit_bzero(str->data, str->length);
406 free(str->data);
407 str->data = data; 405 str->data = data;
408 str->length = len; 406 str->length = len;
409} 407}
@@ -436,10 +434,8 @@ ASN1_STRING_free(ASN1_STRING *a)
436{ 434{
437 if (a == NULL) 435 if (a == NULL)
438 return; 436 return;
439 if (a->data != NULL && !(a->flags & ASN1_STRING_FLAG_NDEF)) { 437 if (a->data != NULL && !(a->flags & ASN1_STRING_FLAG_NDEF))
440 explicit_bzero(a->data, a->length); 438 freezero(a->data, a->length);
441 free(a->data);
442 }
443 free(a); 439 free(a);
444} 440}
445 441
diff --git a/src/lib/libcrypto/bn/bn_asm.c b/src/lib/libcrypto/bn/bn_asm.c
index 49f0ba5d7b..993fbb3dc5 100644
--- a/src/lib/libcrypto/bn/bn_asm.c
+++ b/src/lib/libcrypto/bn/bn_asm.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_asm.c,v 1.14 2015/02/25 15:39:49 bcook Exp $ */ 1/* $OpenBSD: bn_asm.c,v 1.15 2017/05/02 03:59:44 deraadt 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 *
@@ -989,8 +989,7 @@ enter:
989 } 989 }
990 memcpy(rp, tp, num * sizeof(BN_ULONG)); 990 memcpy(rp, tp, num * sizeof(BN_ULONG));
991out: 991out:
992 explicit_bzero(tp, (num + 2) * sizeof(BN_ULONG)); 992 freezero(tp, (num + 2) * sizeof(BN_ULONG));
993 free(tp);
994 return 1; 993 return 1;
995} 994}
996#else 995#else
@@ -1081,8 +1080,7 @@ bn_mul_mont(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,
1081 } 1080 }
1082 memcpy(rp, tp, num * sizeof(BN_ULONG)); 1081 memcpy(rp, tp, num * sizeof(BN_ULONG));
1083out: 1082out:
1084 explicit_bzero(tp, (num + 2) * sizeof(BN_ULONG)); 1083 freezero(tp, (num + 2) * sizeof(BN_ULONG));
1085 free(tp);
1086 return 1; 1084 return 1;
1087} 1085}
1088#else 1086#else
diff --git a/src/lib/libcrypto/bn/bn_exp.c b/src/lib/libcrypto/bn/bn_exp.c
index d388758927..b778d5d67c 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.30 2017/01/29 17:49:22 beck Exp $ */ 1/* $OpenBSD: bn_exp.c,v 1.31 2017/05/02 03:59:44 deraadt 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 *
@@ -706,12 +706,10 @@ BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
706 numPowers = 1 << window; 706 numPowers = 1 << window;
707 powerbufLen = sizeof(m->d[0]) * (top * numPowers + 707 powerbufLen = sizeof(m->d[0]) * (top * numPowers +
708 ((2*top) > numPowers ? (2*top) : numPowers)); 708 ((2*top) > numPowers ? (2*top) : numPowers));
709 if ((powerbufFree = malloc(powerbufLen + 709 if ((powerbufFree = calloc(powerbufLen +
710 MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH)) == NULL) 710 MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH, 1)) == NULL)
711 goto err; 711 goto err;
712
713 powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree); 712 powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);
714 memset(powerbuf, 0, powerbufLen);
715 713
716 /* lay down tmp and am right after powers table */ 714 /* lay down tmp and am right after powers table */
717 tmp.d = (BN_ULONG *)(powerbuf + sizeof(m->d[0]) * top * numPowers); 715 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,
901err: 899err:
902 if ((in_mont == NULL) && (mont != NULL)) 900 if ((in_mont == NULL) && (mont != NULL))
903 BN_MONT_CTX_free(mont); 901 BN_MONT_CTX_free(mont);
904 if (powerbuf != NULL) { 902 freezero(powerbufFree, powerbufLen + MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH);
905 explicit_bzero(powerbuf, powerbufLen);
906 free(powerbufFree);
907 }
908 BN_CTX_end(ctx); 903 BN_CTX_end(ctx);
909 return (ret); 904 return (ret);
910} 905}
diff --git a/src/lib/libcrypto/bn/bn_lib.c b/src/lib/libcrypto/bn/bn_lib.c
index f2736e31c3..8aeeb5304f 100644
--- a/src/lib/libcrypto/bn/bn_lib.c
+++ b/src/lib/libcrypto/bn/bn_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_lib.c,v 1.37 2017/01/29 17:49:22 beck Exp $ */ 1/* $OpenBSD: bn_lib.c,v 1.38 2017/05/02 03:59:44 deraadt 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 *
@@ -220,10 +220,8 @@ BN_clear_free(BIGNUM *a)
220 if (a == NULL) 220 if (a == NULL)
221 return; 221 return;
222 bn_check_top(a); 222 bn_check_top(a);
223 if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA))) { 223 if (a->d != NULL && !(BN_get_flags(a, BN_FLG_STATIC_DATA)))
224 explicit_bzero(a->d, a->dmax * sizeof(a->d[0])); 224 freezero(a->d, a->dmax * sizeof(a->d[0]));
225 free(a->d);
226 }
227 i = BN_get_flags(a, BN_FLG_MALLOCED); 225 i = BN_get_flags(a, BN_FLG_MALLOCED);
228 explicit_bzero(a, sizeof(BIGNUM)); 226 explicit_bzero(a, sizeof(BIGNUM));
229 if (i) 227 if (i)
@@ -393,10 +391,8 @@ bn_expand2(BIGNUM *b, int words)
393 BN_ULONG *a = bn_expand_internal(b, words); 391 BN_ULONG *a = bn_expand_internal(b, words);
394 if (!a) 392 if (!a)
395 return NULL; 393 return NULL;
396 if (b->d) { 394 if (b->d)
397 explicit_bzero(b->d, b->dmax * sizeof(b->d[0])); 395 freezero(b->d, b->dmax * sizeof(b->d[0]));
398 free(b->d);
399 }
400 b->d = a; 396 b->d = a;
401 b->dmax = words; 397 b->dmax = words;
402 } 398 }
diff --git a/src/lib/libcrypto/bn/bn_rand.c b/src/lib/libcrypto/bn/bn_rand.c
index 812fa6a575..8625757140 100644
--- a/src/lib/libcrypto/bn/bn_rand.c
+++ b/src/lib/libcrypto/bn/bn_rand.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bn_rand.c,v 1.19 2017/01/29 17:49:22 beck Exp $ */ 1/* $OpenBSD: bn_rand.c,v 1.20 2017/05/02 03:59:44 deraadt 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 *
@@ -186,10 +186,7 @@ bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
186 ret = 1; 186 ret = 1;
187 187
188err: 188err:
189 if (buf != NULL) { 189 freezero(buf, bytes);
190 explicit_bzero(buf, bytes);
191 free(buf);
192 }
193 bn_check_top(rnd); 190 bn_check_top(rnd);
194 return (ret); 191 return (ret);
195} 192}
diff --git a/src/lib/libcrypto/buffer/buffer.c b/src/lib/libcrypto/buffer/buffer.c
index 5ed893f5f9..f4e84c4478 100644
--- a/src/lib/libcrypto/buffer/buffer.c
+++ b/src/lib/libcrypto/buffer/buffer.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: buffer.c,v 1.26 2017/04/09 15:06:20 jsing Exp $ */ 1/* $OpenBSD: buffer.c,v 1.27 2017/05/02 03:59:44 deraadt 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 *
@@ -89,10 +89,7 @@ BUF_MEM_free(BUF_MEM *a)
89 if (a == NULL) 89 if (a == NULL)
90 return; 90 return;
91 91
92 if (a->data != NULL) { 92 freezero(a->data, a->max);
93 explicit_bzero(a->data, a->max);
94 free(a->data);
95 }
96 free(a); 93 free(a);
97} 94}
98 95
diff --git a/src/lib/libcrypto/dsa/dsa_asn1.c b/src/lib/libcrypto/dsa/dsa_asn1.c
index d7b77470bd..f7dfaf1d9c 100644
--- a/src/lib/libcrypto/dsa/dsa_asn1.c
+++ b/src/lib/libcrypto/dsa/dsa_asn1.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: dsa_asn1.c,v 1.19 2017/01/29 17:49:22 beck Exp $ */ 1/* $OpenBSD: dsa_asn1.c,v 1.20 2017/05/02 03:59:44 deraadt 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 2000. 3 * project 2000.
4 */ 4 */
@@ -454,10 +454,7 @@ DSA_verify(int type, const unsigned char *dgst, int dgst_len,
454 goto err; 454 goto err;
455 ret = DSA_do_verify(dgst, dgst_len, s, dsa); 455 ret = DSA_do_verify(dgst, dgst_len, s, dsa);
456err: 456err:
457 if (derlen > 0) { 457 freezero(der, derlen);
458 explicit_bzero(der, derlen);
459 free(der);
460 }
461 DSA_SIG_free(s); 458 DSA_SIG_free(s);
462 return ret; 459 return ret;
463} 460}
diff --git a/src/lib/libcrypto/ec/ec_key.c b/src/lib/libcrypto/ec/ec_key.c
index 5a23a9823d..595b88cefc 100644
--- a/src/lib/libcrypto/ec/ec_key.c
+++ b/src/lib/libcrypto/ec/ec_key.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_key.c,v 1.13 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: ec_key.c,v 1.14 2017/05/02 03:59:44 deraadt Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project. 3 * Written by Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -122,9 +122,7 @@ EC_KEY_free(EC_KEY * r)
122 122
123 EC_EX_DATA_free_all_data(&r->method_data); 123 EC_EX_DATA_free_all_data(&r->method_data);
124 124
125 explicit_bzero((void *) r, sizeof(EC_KEY)); 125 freezero(r, sizeof(EC_KEY));
126
127 free(r);
128} 126}
129 127
130EC_KEY * 128EC_KEY *
diff --git a/src/lib/libcrypto/ec/ec_lib.c b/src/lib/libcrypto/ec/ec_lib.c
index baddbf6dc8..0d062111b5 100644
--- a/src/lib/libcrypto/ec/ec_lib.c
+++ b/src/lib/libcrypto/ec/ec_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_lib.c,v 1.23 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: ec_lib.c,v 1.24 2017/05/02 03:59:44 deraadt Exp $ */
2/* 2/*
3 * Originally written by Bodo Moeller for the OpenSSL project. 3 * Originally written by Bodo Moeller for the OpenSSL project.
4 */ 4 */
@@ -151,12 +151,8 @@ EC_GROUP_clear_free(EC_GROUP * group)
151 BN_clear_free(&group->order); 151 BN_clear_free(&group->order);
152 BN_clear_free(&group->cofactor); 152 BN_clear_free(&group->cofactor);
153 153
154 if (group->seed) { 154 freezero(group->seed, group->seed_len);
155 explicit_bzero(group->seed, group->seed_len); 155 freezero(group, sizeof *group);
156 free(group->seed);
157 }
158 explicit_bzero(group, sizeof *group);
159 free(group);
160} 156}
161 157
162 158
@@ -743,8 +739,7 @@ EC_POINT_clear_free(EC_POINT * point)
743 point->meth->point_clear_finish(point); 739 point->meth->point_clear_finish(point);
744 else if (point->meth->point_finish != 0) 740 else if (point->meth->point_finish != 0)
745 point->meth->point_finish(point); 741 point->meth->point_finish(point);
746 explicit_bzero(point, sizeof *point); 742 freezero(point, sizeof *point);
747 free(point);
748} 743}
749 744
750 745
diff --git a/src/lib/libcrypto/ec/ec_mult.c b/src/lib/libcrypto/ec/ec_mult.c
index e44104d21c..a565263bb6 100644
--- a/src/lib/libcrypto/ec/ec_mult.c
+++ b/src/lib/libcrypto/ec/ec_mult.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ec_mult.c,v 1.20 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: ec_mult.c,v 1.21 2017/05/02 03:59:44 deraadt Exp $ */
2/* 2/*
3 * Originally written by Bodo Moeller and Nils Larsch for the OpenSSL project. 3 * Originally written by Bodo Moeller and Nils Larsch for the OpenSSL project.
4 */ 4 */
@@ -177,8 +177,7 @@ ec_pre_comp_clear_free(void *pre_)
177 } 177 }
178 free(pre->points); 178 free(pre->points);
179 } 179 }
180 explicit_bzero(pre, sizeof *pre); 180 freezero(pre, sizeof *pre);
181 free(pre);
182} 181}
183 182
184 183
diff --git a/src/lib/libcrypto/ec/ecp_nistp224.c b/src/lib/libcrypto/ec/ecp_nistp224.c
index 38dd83b6d9..fce74578c7 100644
--- a/src/lib/libcrypto/ec/ecp_nistp224.c
+++ b/src/lib/libcrypto/ec/ecp_nistp224.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecp_nistp224.c,v 1.18 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: ecp_nistp224.c,v 1.19 2017/05/02 03:59:44 deraadt Exp $ */
2/* 2/*
3 * Written by Emilia Kasper (Google) for the OpenSSL project. 3 * Written by Emilia Kasper (Google) for the OpenSSL project.
4 */ 4 */
@@ -1239,8 +1239,7 @@ nistp224_pre_comp_clear_free(void *pre_)
1239 if (i > 0) 1239 if (i > 0)
1240 return; 1240 return;
1241 1241
1242 explicit_bzero(pre, sizeof *pre); 1242 freezero(pre, sizeof *pre);
1243 free(pre);
1244} 1243}
1245 1244
1246/******************************************************************************/ 1245/******************************************************************************/
diff --git a/src/lib/libcrypto/ec/ecp_nistp256.c b/src/lib/libcrypto/ec/ecp_nistp256.c
index 4771a92efd..6f3ec3c250 100644
--- a/src/lib/libcrypto/ec/ecp_nistp256.c
+++ b/src/lib/libcrypto/ec/ecp_nistp256.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecp_nistp256.c,v 1.17 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: ecp_nistp256.c,v 1.18 2017/05/02 03:59:44 deraadt Exp $ */
2/* 2/*
3 * Written by Adam Langley (Google) for the OpenSSL project 3 * Written by Adam Langley (Google) for the OpenSSL project
4 */ 4 */
@@ -1788,8 +1788,7 @@ nistp256_pre_comp_clear_free(void *pre_)
1788 if (i > 0) 1788 if (i > 0)
1789 return; 1789 return;
1790 1790
1791 explicit_bzero(pre, sizeof *pre); 1791 freezero(pre, sizeof *pre);
1792 free(pre);
1793} 1792}
1794 1793
1795/******************************************************************************/ 1794/******************************************************************************/
diff --git a/src/lib/libcrypto/ec/ecp_nistp521.c b/src/lib/libcrypto/ec/ecp_nistp521.c
index 22bafe392f..6dbc266937 100644
--- a/src/lib/libcrypto/ec/ecp_nistp521.c
+++ b/src/lib/libcrypto/ec/ecp_nistp521.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecp_nistp521.c,v 1.18 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: ecp_nistp521.c,v 1.19 2017/05/02 03:59:44 deraadt Exp $ */
2/* 2/*
3 * Written by Adam Langley (Google) for the OpenSSL project 3 * Written by Adam Langley (Google) for the OpenSSL project
4 */ 4 */
@@ -1679,8 +1679,7 @@ nistp521_pre_comp_clear_free(void *pre_)
1679 if (i > 0) 1679 if (i > 0)
1680 return; 1680 return;
1681 1681
1682 explicit_bzero(pre, sizeof(*pre)); 1682 freezero(pre, sizeof(*pre));
1683 free(pre);
1684} 1683}
1685 1684
1686/******************************************************************************/ 1685/******************************************************************************/
diff --git a/src/lib/libcrypto/ec/ecp_nistz256.c b/src/lib/libcrypto/ec/ecp_nistz256.c
index 848f15cb17..b3b8f74b14 100644
--- a/src/lib/libcrypto/ec/ecp_nistz256.c
+++ b/src/lib/libcrypto/ec/ecp_nistz256.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecp_nistz256.c,v 1.2 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: ecp_nistz256.c,v 1.3 2017/05/02 03:59:44 deraadt Exp $ */
2/* Copyright (c) 2014, Intel Corporation. 2/* Copyright (c) 2014, Intel Corporation.
3 * 3 *
4 * Permission to use, copy, modify, and/or distribute this software for any 4 * Permission to use, copy, modify, and/or distribute this software for any
@@ -1118,11 +1118,9 @@ ecp_nistz256_pre_comp_clear_free(void *pre_)
1118 * here, but the only place where we allocate this uses 1118 * here, but the only place where we allocate this uses
1119 * PRECOMP256_ROW (i.e. 64 P256_POINT_AFFINE) but sets w == 7. 1119 * PRECOMP256_ROW (i.e. 64 P256_POINT_AFFINE) but sets w == 7.
1120 */ 1120 */
1121 explicit_bzero(pre->precomp, 37 * sizeof(PRECOMP256_ROW)); 1121 freezero(pre->precomp, 37 * sizeof(PRECOMP256_ROW));
1122 free(pre->precomp);
1123 } 1122 }
1124 explicit_bzero(pre, sizeof *pre); 1123 freezero(pre, sizeof *pre);
1125 free(pre);
1126} 1124}
1127 1125
1128static int 1126static int
diff --git a/src/lib/libcrypto/ecdh/ech_lib.c b/src/lib/libcrypto/ecdh/ech_lib.c
index 06e6a1ee22..2846a88612 100644
--- a/src/lib/libcrypto/ecdh/ech_lib.c
+++ b/src/lib/libcrypto/ecdh/ech_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ech_lib.c,v 1.12 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: ech_lib.c,v 1.13 2017/05/02 03:59:44 deraadt Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 3 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
4 * 4 *
@@ -182,9 +182,7 @@ ecdh_data_free(void *data)
182 182
183 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDH, r, &r->ex_data); 183 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDH, r, &r->ex_data);
184 184
185 explicit_bzero((void *)r, sizeof(ECDH_DATA)); 185 freezero(r, sizeof(ECDH_DATA));
186
187 free(r);
188} 186}
189 187
190ECDH_DATA * 188ECDH_DATA *
diff --git a/src/lib/libcrypto/ecdsa/ecs_lib.c b/src/lib/libcrypto/ecdsa/ecs_lib.c
index ca0f51b366..4fbe1e060b 100644
--- a/src/lib/libcrypto/ecdsa/ecs_lib.c
+++ b/src/lib/libcrypto/ecdsa/ecs_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecs_lib.c,v 1.11 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: ecs_lib.c,v 1.12 2017/05/02 03:59:44 deraadt Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -169,9 +169,7 @@ ecdsa_data_free(void *data)
169#endif 169#endif
170 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDSA, r, &r->ex_data); 170 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ECDSA, r, &r->ex_data);
171 171
172 explicit_bzero((void *)r, sizeof(ECDSA_DATA)); 172 freezero(r, sizeof(ECDSA_DATA));
173
174 free(r);
175} 173}
176 174
177ECDSA_DATA * 175ECDSA_DATA *
diff --git a/src/lib/libcrypto/ecdsa/ecs_vrf.c b/src/lib/libcrypto/ecdsa/ecs_vrf.c
index a4b627ace6..270af94c0d 100644
--- a/src/lib/libcrypto/ecdsa/ecs_vrf.c
+++ b/src/lib/libcrypto/ecdsa/ecs_vrf.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: ecs_vrf.c,v 1.5 2015/02/08 13:35:07 jsing Exp $ */ 1/* $OpenBSD: ecs_vrf.c,v 1.6 2017/05/02 03:59:44 deraadt Exp $ */
2/* 2/*
3 * Written by Nils Larsch for the OpenSSL project 3 * Written by Nils Larsch for the OpenSSL project
4 */ 4 */
@@ -107,10 +107,7 @@ ECDSA_verify(int type, const unsigned char *dgst, int dgst_len,
107 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey); 107 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
108 108
109err: 109err:
110 if (derlen > 0) { 110 freezero(der, derlen);
111 explicit_bzero(der, derlen);
112 free(der);
113 }
114 ECDSA_SIG_free(s); 111 ECDSA_SIG_free(s);
115 return (ret); 112 return (ret);
116} 113}
diff --git a/src/lib/libcrypto/evp/bio_enc.c b/src/lib/libcrypto/evp/bio_enc.c
index 1920c6d180..7c7cf9a8b3 100644
--- a/src/lib/libcrypto/evp/bio_enc.c
+++ b/src/lib/libcrypto/evp/bio_enc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: bio_enc.c,v 1.19 2015/09/10 15:56:25 jsing Exp $ */ 1/* $OpenBSD: bio_enc.c,v 1.20 2017/05/02 03:59:44 deraadt 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 *
@@ -135,8 +135,7 @@ enc_free(BIO *a)
135 return (0); 135 return (0);
136 b = (BIO_ENC_CTX *)a->ptr; 136 b = (BIO_ENC_CTX *)a->ptr;
137 EVP_CIPHER_CTX_cleanup(&(b->cipher)); 137 EVP_CIPHER_CTX_cleanup(&(b->cipher));
138 explicit_bzero(a->ptr, sizeof(BIO_ENC_CTX)); 138 freezero(a->ptr, sizeof(BIO_ENC_CTX));
139 free(a->ptr);
140 a->ptr = NULL; 139 a->ptr = NULL;
141 a->init = 0; 140 a->init = 0;
142 a->flags = 0; 141 a->flags = 0;
diff --git a/src/lib/libcrypto/evp/digest.c b/src/lib/libcrypto/evp/digest.c
index ee1f955959..7471c1e822 100644
--- a/src/lib/libcrypto/evp/digest.c
+++ b/src/lib/libcrypto/evp/digest.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: digest.c,v 1.27 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: digest.c,v 1.28 2017/05/02 03:59:44 deraadt 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 *
@@ -193,8 +193,7 @@ EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl)
193 if (ctx->digest != type) { 193 if (ctx->digest != type) {
194 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data && 194 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data &&
195 !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) { 195 !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) {
196 explicit_bzero(ctx->md_data, ctx->digest->ctx_size); 196 freezero(ctx->md_data, ctx->digest->ctx_size);
197 free(ctx->md_data);
198 ctx->md_data = NULL; 197 ctx->md_data = NULL;
199 } 198 }
200 ctx->digest = type; 199 ctx->digest = type;
@@ -360,10 +359,8 @@ EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx)
360 !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED)) 359 !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_CLEANED))
361 ctx->digest->cleanup(ctx); 360 ctx->digest->cleanup(ctx);
362 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data && 361 if (ctx->digest && ctx->digest->ctx_size && ctx->md_data &&
363 !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) { 362 !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE))
364 explicit_bzero(ctx->md_data, ctx->digest->ctx_size); 363 freezero(ctx->md_data, ctx->digest->ctx_size);
365 free(ctx->md_data);
366 }
367 EVP_PKEY_CTX_free(ctx->pctx); 364 EVP_PKEY_CTX_free(ctx->pctx);
368#ifndef OPENSSL_NO_ENGINE 365#ifndef OPENSSL_NO_ENGINE
369 if (ctx->engine) 366 if (ctx->engine)
diff --git a/src/lib/libcrypto/evp/e_aes.c b/src/lib/libcrypto/evp/e_aes.c
index 97cb5154a5..7c713db026 100644
--- a/src/lib/libcrypto/evp/e_aes.c
+++ b/src/lib/libcrypto/evp/e_aes.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: e_aes.c,v 1.33 2017/01/31 13:17:21 inoguchi Exp $ */ 1/* $OpenBSD: e_aes.c,v 1.34 2017/05/02 03:59:44 deraadt Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -1422,8 +1422,7 @@ aead_aes_gcm_cleanup(EVP_AEAD_CTX *ctx)
1422{ 1422{
1423 struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state; 1423 struct aead_aes_gcm_ctx *gcm_ctx = ctx->aead_state;
1424 1424
1425 explicit_bzero(gcm_ctx, sizeof(*gcm_ctx)); 1425 freezero(gcm_ctx, sizeof(*gcm_ctx));
1426 free(gcm_ctx);
1427} 1426}
1428 1427
1429static int 1428static int
diff --git a/src/lib/libcrypto/evp/e_chacha20poly1305.c b/src/lib/libcrypto/evp/e_chacha20poly1305.c
index e135f9a104..051d5548b5 100644
--- a/src/lib/libcrypto/evp/e_chacha20poly1305.c
+++ b/src/lib/libcrypto/evp/e_chacha20poly1305.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: e_chacha20poly1305.c,v 1.15 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: e_chacha20poly1305.c,v 1.16 2017/05/02 03:59:44 deraadt Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2015 Reyk Floter <reyk@openbsd.org> 4 * Copyright (c) 2015 Reyk Floter <reyk@openbsd.org>
@@ -83,8 +83,7 @@ aead_chacha20_poly1305_cleanup(EVP_AEAD_CTX *ctx)
83{ 83{
84 struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state; 84 struct aead_chacha20_poly1305_ctx *c20_ctx = ctx->aead_state;
85 85
86 explicit_bzero(c20_ctx->key, sizeof(c20_ctx->key)); 86 freezero(c20_ctx, sizeof(c20_ctx));
87 free(c20_ctx);
88} 87}
89 88
90static void 89static void
diff --git a/src/lib/libcrypto/evp/p_open.c b/src/lib/libcrypto/evp/p_open.c
index 1eb238dfde..57a46706b9 100644
--- a/src/lib/libcrypto/evp/p_open.c
+++ b/src/lib/libcrypto/evp/p_open.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: p_open.c,v 1.18 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: p_open.c,v 1.19 2017/05/02 03:59:44 deraadt 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 *
@@ -109,9 +109,7 @@ EVP_OpenInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
109 ret = 1; 109 ret = 1;
110 110
111err: 111err:
112 if (key != NULL) 112 freezero(key, size);
113 explicit_bzero(key, size);
114 free(key);
115 return (ret); 113 return (ret);
116} 114}
117 115
diff --git a/src/lib/libcrypto/gost/gostr341001_key.c b/src/lib/libcrypto/gost/gostr341001_key.c
index 0a42a15378..0af39f21bf 100644
--- a/src/lib/libcrypto/gost/gostr341001_key.c
+++ b/src/lib/libcrypto/gost/gostr341001_key.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: gostr341001_key.c,v 1.7 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: gostr341001_key.c,v 1.8 2017/05/02 03:59:44 deraadt Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> 3 * Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
4 * Copyright (c) 2005-2006 Cryptocom LTD 4 * Copyright (c) 2005-2006 Cryptocom LTD
@@ -105,8 +105,7 @@ GOST_KEY_free(GOST_KEY *r)
105 EC_POINT_free(r->pub_key); 105 EC_POINT_free(r->pub_key);
106 BN_clear_free(r->priv_key); 106 BN_clear_free(r->priv_key);
107 107
108 explicit_bzero((void *)r, sizeof(GOST_KEY)); 108 freezero(r, sizeof(GOST_KEY));
109 free(r);
110} 109}
111 110
112int 111int
diff --git a/src/lib/libcrypto/hmac/hm_pmeth.c b/src/lib/libcrypto/hmac/hm_pmeth.c
index c5ac6c00c0..390725fa25 100644
--- a/src/lib/libcrypto/hmac/hm_pmeth.c
+++ b/src/lib/libcrypto/hmac/hm_pmeth.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: hm_pmeth.c,v 1.9 2015/09/10 15:56:25 jsing Exp $ */ 1/* $OpenBSD: hm_pmeth.c,v 1.10 2017/05/02 03:59:44 deraadt 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 2007. 3 * project 2007.
4 */ 4 */
@@ -122,12 +122,7 @@ pkey_hmac_cleanup(EVP_PKEY_CTX *ctx)
122 HMAC_PKEY_CTX *hctx = ctx->data; 122 HMAC_PKEY_CTX *hctx = ctx->data;
123 123
124 HMAC_CTX_cleanup(&hctx->ctx); 124 HMAC_CTX_cleanup(&hctx->ctx);
125 if (hctx->ktmp.data) { 125 freezero(hctx->ktmp.data, hctx->ktmp.length);
126 if (hctx->ktmp.length)
127 explicit_bzero(hctx->ktmp.data, hctx->ktmp.length);
128 free(hctx->ktmp.data);
129 hctx->ktmp.data = NULL;
130 }
131 free(hctx); 126 free(hctx);
132} 127}
133 128
diff --git a/src/lib/libcrypto/malloc-wrapper.c b/src/lib/libcrypto/malloc-wrapper.c
index 6ba0aad7e4..12867387bf 100644
--- a/src/lib/libcrypto/malloc-wrapper.c
+++ b/src/lib/libcrypto/malloc-wrapper.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: malloc-wrapper.c,v 1.5 2017/04/28 22:46:40 beck Exp $ */ 1/* $OpenBSD: malloc-wrapper.c,v 1.6 2017/05/02 03:59:44 deraadt Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Bob Beck 3 * Copyright (c) 2014 Bob Beck
4 * 4 *
@@ -165,8 +165,7 @@ CRYPTO_realloc_clean(void *ptr, int old_len, int num, const char *file,
165 ret = malloc(num); 165 ret = malloc(num);
166 if (ret && ptr && old_len > 0) { 166 if (ret && ptr && old_len > 0) {
167 memcpy(ret, ptr, old_len); 167 memcpy(ret, ptr, old_len);
168 explicit_bzero(ptr, old_len); 168 freezero(ptr, old_len);
169 free(ptr);
170 } 169 }
171 return ret; 170 return ret;
172} 171}
diff --git a/src/lib/libcrypto/modes/gcm128.c b/src/lib/libcrypto/modes/gcm128.c
index 95ee755f83..c080d9c16a 100644
--- a/src/lib/libcrypto/modes/gcm128.c
+++ b/src/lib/libcrypto/modes/gcm128.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: gcm128.c,v 1.15 2016/11/04 17:30:30 miod Exp $ */ 1/* $OpenBSD: gcm128.c,v 1.16 2017/05/02 03:59:44 deraadt Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2010 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2010 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -1538,8 +1538,5 @@ GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block)
1538 1538
1539void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx) 1539void CRYPTO_gcm128_release(GCM128_CONTEXT *ctx)
1540{ 1540{
1541 if (ctx) { 1541 freezero(ctx, sizeof(*ctx));
1542 explicit_bzero(ctx,sizeof(*ctx));
1543 free(ctx);
1544 }
1545} 1542}
diff --git a/src/lib/libcrypto/pem/pem_lib.c b/src/lib/libcrypto/pem/pem_lib.c
index b2c72e1d76..0f7c36d21c 100644
--- a/src/lib/libcrypto/pem/pem_lib.c
+++ b/src/lib/libcrypto/pem/pem_lib.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: pem_lib.c,v 1.44 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: pem_lib.c,v 1.45 2017/05/02 03:59:44 deraadt 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 *
@@ -425,10 +425,7 @@ err:
425 explicit_bzero(iv, sizeof(iv)); 425 explicit_bzero(iv, sizeof(iv));
426 explicit_bzero((char *)&ctx, sizeof(ctx)); 426 explicit_bzero((char *)&ctx, sizeof(ctx));
427 explicit_bzero(buf, PEM_BUFSIZE); 427 explicit_bzero(buf, PEM_BUFSIZE);
428 if (data != NULL) { 428 freezero(data, (unsigned int)dsize);
429 explicit_bzero(data, (unsigned int)dsize);
430 free(data);
431 }
432 return (ret); 429 return (ret);
433} 430}
434 431
@@ -626,8 +623,7 @@ PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data,
626 EVP_EncodeFinal(&ctx, buf, &outl); 623 EVP_EncodeFinal(&ctx, buf, &outl);
627 if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl)) 624 if ((outl > 0) && (BIO_write(bp, (char *)buf, outl) != outl))
628 goto err; 625 goto err;
629 explicit_bzero(buf, PEM_BUFSIZE * 8); 626 freezero(buf, PEM_BUFSIZE * 8);
630 free(buf);
631 buf = NULL; 627 buf = NULL;
632 if ((BIO_write(bp, "-----END ", 9) != 9) || 628 if ((BIO_write(bp, "-----END ", 9) != 9) ||
633 (BIO_write(bp, name, nlen) != nlen) || 629 (BIO_write(bp, name, nlen) != nlen) ||
@@ -636,10 +632,7 @@ PEM_write_bio(BIO *bp, const char *name, char *header, unsigned char *data,
636 return (i + outl); 632 return (i + outl);
637 633
638err: 634err:
639 if (buf) { 635 freezero(buf, PEM_BUFSIZE * 8);
640 explicit_bzero(buf, PEM_BUFSIZE * 8);
641 free(buf);
642 }
643 PEMerror(reason); 636 PEMerror(reason);
644 return (0); 637 return (0);
645} 638}
diff --git a/src/lib/libcrypto/pem/pem_pkey.c b/src/lib/libcrypto/pem/pem_pkey.c
index 6651ef9419..89181a25f7 100644
--- a/src/lib/libcrypto/pem/pem_pkey.c
+++ b/src/lib/libcrypto/pem/pem_pkey.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: pem_pkey.c,v 1.22 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: pem_pkey.c,v 1.23 2017/05/02 03:59:44 deraadt 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 *
@@ -143,8 +143,7 @@ p8err:
143 PEMerror(ERR_R_ASN1_LIB); 143 PEMerror(ERR_R_ASN1_LIB);
144err: 144err:
145 free(nm); 145 free(nm);
146 explicit_bzero(data, len); 146 freezero(data, len);
147 free(data);
148 return (ret); 147 return (ret);
149} 148}
150 149
diff --git a/src/lib/libcrypto/pem/pvkfmt.c b/src/lib/libcrypto/pem/pvkfmt.c
index 5ed8df585f..18de5d52a4 100644
--- a/src/lib/libcrypto/pem/pvkfmt.c
+++ b/src/lib/libcrypto/pem/pvkfmt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: pvkfmt.c,v 1.18 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: pvkfmt.c,v 1.19 2017/05/02 03:59:44 deraadt 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 */
@@ -828,10 +828,7 @@ b2i_PVK_bio(BIO *in, pem_password_cb *cb, void *u)
828 ret = do_PVK_body(&p, saltlen, keylen, cb, u); 828 ret = do_PVK_body(&p, saltlen, keylen, cb, u);
829 829
830err: 830err:
831 if (buf) { 831 freezero(buf, buflen);
832 explicit_bzero(buf, buflen);
833 free(buf);
834 }
835 return ret; 832 return ret;
836} 833}
837 834
diff --git a/src/lib/libcrypto/pkcs12/p12_key.c b/src/lib/libcrypto/pkcs12/p12_key.c
index fd710771a5..d419a9d835 100644
--- a/src/lib/libcrypto/pkcs12/p12_key.c
+++ b/src/lib/libcrypto/pkcs12/p12_key.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: p12_key.c,v 1.25 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: p12_key.c,v 1.26 2017/05/02 03:59:45 deraadt 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 1999. 3 * project 1999.
4 */ 4 */
@@ -88,10 +88,7 @@ PKCS12_key_gen_asc(const char *pass, int passlen, unsigned char *salt,
88 id, iter, n, out, md_type); 88 id, iter, n, out, md_type);
89 if (ret <= 0) 89 if (ret <= 0)
90 return 0; 90 return 0;
91 if (unipass) { 91 freezero(unipass, uniplen);
92 explicit_bzero(unipass, uniplen);
93 free(unipass);
94 }
95 return ret; 92 return ret;
96} 93}
97 94
diff --git a/src/lib/libcrypto/pkcs7/pk7_doit.c b/src/lib/libcrypto/pkcs7/pk7_doit.c
index 484620a686..24ab957b4c 100644
--- a/src/lib/libcrypto/pkcs7/pk7_doit.c
+++ b/src/lib/libcrypto/pkcs7/pk7_doit.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: pk7_doit.c,v 1.41 2017/01/29 17:49:23 beck Exp $ */ 1/* $OpenBSD: pk7_doit.c,v 1.42 2017/05/02 03:59:45 deraadt 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 *
@@ -231,10 +231,7 @@ pkcs7_decrypt_rinfo(unsigned char **pek, int *peklen, PKCS7_RECIP_INFO *ri,
231 231
232 ret = 1; 232 ret = 1;
233 233
234 if (*pek) { 234 freezero(*pek, *peklen);
235 explicit_bzero(*pek, *peklen);
236 free(*pek);
237 }
238 235
239 *pek = ek; 236 *pek = ek;
240 *peklen = eklen; 237 *peklen = eklen;
@@ -577,8 +574,7 @@ PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
577 */ 574 */
578 if (!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen)) { 575 if (!EVP_CIPHER_CTX_set_key_length(evp_ctx, eklen)) {
579 /* Use random key as MMA defence */ 576 /* Use random key as MMA defence */
580 explicit_bzero(ek, eklen); 577 freezero(ek, eklen);
581 free(ek);
582 ek = tkey; 578 ek = tkey;
583 eklen = tkeylen; 579 eklen = tkeylen;
584 tkey = NULL; 580 tkey = NULL;
@@ -589,16 +585,10 @@ PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
589 if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0) 585 if (EVP_CipherInit_ex(evp_ctx, NULL, NULL, ek, NULL, 0) <= 0)
590 goto err; 586 goto err;
591 587
592 if (ek) { 588 freezero(ek, eklen);
593 explicit_bzero(ek, eklen); 589 ek = NULL;
594 free(ek); 590 freezero(tkey, tkeylen);
595 ek = NULL; 591 tkey = NULL;
596 }
597 if (tkey) {
598 explicit_bzero(tkey, tkeylen);
599 free(tkey);
600 tkey = NULL;
601 }
602 592
603 if (out == NULL) 593 if (out == NULL)
604 out = etmp; 594 out = etmp;
@@ -623,14 +613,8 @@ PKCS7_dataDecode(PKCS7 *p7, EVP_PKEY *pkey, BIO *in_bio, X509 *pcert)
623 613
624 if (0) { 614 if (0) {
625err: 615err:
626 if (ek) { 616 freezero(ek, eklen);
627 explicit_bzero(ek, eklen); 617 freezero(tkey, tkeylen);
628 free(ek);
629 }
630 if (tkey) {
631 explicit_bzero(tkey, tkeylen);
632 free(tkey);
633 }
634 if (out != NULL) 618 if (out != NULL)
635 BIO_free_all(out); 619 BIO_free_all(out);
636 if (btmp != NULL) 620 if (btmp != NULL)
diff --git a/src/lib/libcrypto/rsa/rsa_eay.c b/src/lib/libcrypto/rsa/rsa_eay.c
index f9f620ae23..6c3da0d656 100644
--- a/src/lib/libcrypto/rsa/rsa_eay.c
+++ b/src/lib/libcrypto/rsa/rsa_eay.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: rsa_eay.c,v 1.48 2017/04/28 22:46:40 beck Exp $ */ 1/* $OpenBSD: rsa_eay.c,v 1.49 2017/05/02 03:59:45 deraadt 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 *
@@ -244,10 +244,7 @@ err:
244 BN_CTX_end(ctx); 244 BN_CTX_end(ctx);
245 BN_CTX_free(ctx); 245 BN_CTX_free(ctx);
246 } 246 }
247 if (buf != NULL) { 247 freezero(buf, num);
248 explicit_bzero(buf, num);
249 free(buf);
250 }
251 return r; 248 return r;
252} 249}
253 250
@@ -468,10 +465,7 @@ err:
468 BN_CTX_end(ctx); 465 BN_CTX_end(ctx);
469 BN_CTX_free(ctx); 466 BN_CTX_free(ctx);
470 } 467 }
471 if (buf != NULL) { 468 freezero(buf, num);
472 explicit_bzero(buf, num);
473 free(buf);
474 }
475 return r; 469 return r;
476} 470}
477 471
@@ -597,10 +591,7 @@ err:
597 BN_CTX_end(ctx); 591 BN_CTX_end(ctx);
598 BN_CTX_free(ctx); 592 BN_CTX_free(ctx);
599 } 593 }
600 if (buf != NULL) { 594 freezero(buf, num);
601 explicit_bzero(buf, num);
602 free(buf);
603 }
604 return r; 595 return r;
605} 596}
606 597
@@ -700,10 +691,7 @@ err:
700 BN_CTX_end(ctx); 691 BN_CTX_end(ctx);
701 BN_CTX_free(ctx); 692 BN_CTX_free(ctx);
702 } 693 }
703 if (buf != NULL) { 694 freezero(buf, num);
704 explicit_bzero(buf, num);
705 free(buf);
706 }
707 return r; 695 return r;
708} 696}
709 697
diff --git a/src/lib/libcrypto/rsa/rsa_saos.c b/src/lib/libcrypto/rsa/rsa_saos.c
index 10184b6c18..e1fbdcb5df 100644
--- a/src/lib/libcrypto/rsa/rsa_saos.c
+++ b/src/lib/libcrypto/rsa/rsa_saos.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: rsa_saos.c,v 1.22 2017/04/28 22:46:40 beck Exp $ */ 1/* $OpenBSD: rsa_saos.c,v 1.23 2017/05/02 03:59:45 deraadt 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 *
@@ -96,8 +96,7 @@ RSA_sign_ASN1_OCTET_STRING(int type, const unsigned char *m, unsigned int m_len,
96 else 96 else
97 *siglen = i; 97 *siglen = i;
98 98
99 explicit_bzero(s, (unsigned int)j + 1); 99 freezero(s, (unsigned int)j + 1);
100 free(s);
101 return ret; 100 return ret;
102} 101}
103 102
@@ -137,9 +136,6 @@ RSA_verify_ASN1_OCTET_STRING(int dtype, const unsigned char *m,
137 ret = 1; 136 ret = 1;
138err: 137err:
139 ASN1_OCTET_STRING_free(sig); 138 ASN1_OCTET_STRING_free(sig);
140 if (s != NULL) { 139 freezero(s, (unsigned int)siglen);
141 explicit_bzero(s, (unsigned int)siglen);
142 free(s);
143 }
144 return ret; 140 return ret;
145} 141}
diff --git a/src/lib/libcrypto/rsa/rsa_sign.c b/src/lib/libcrypto/rsa/rsa_sign.c
index 818b88c604..6e9e869f0a 100644
--- a/src/lib/libcrypto/rsa/rsa_sign.c
+++ b/src/lib/libcrypto/rsa/rsa_sign.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: rsa_sign.c,v 1.28 2017/04/28 22:46:40 beck Exp $ */ 1/* $OpenBSD: rsa_sign.c,v 1.29 2017/05/02 03:59:45 deraadt 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 *
@@ -135,10 +135,8 @@ RSA_sign(int type, const unsigned char *m, unsigned int m_len,
135 else 135 else
136 *siglen = i; 136 *siglen = i;
137 137
138 if (type != NID_md5_sha1) { 138 if (type != NID_md5_sha1)
139 explicit_bzero(tmps, (unsigned int)j + 1); 139 freezero(tmps, (unsigned int)j + 1);
140 free(tmps);
141 }
142 return (ret); 140 return (ret);
143} 141}
144 142
@@ -232,12 +230,8 @@ int_rsa_verify(int dtype, const unsigned char *m, unsigned int m_len,
232 ret = 1; 230 ret = 1;
233 } 231 }
234err: 232err:
235 if (sig != NULL) 233 X509_SIG_free(sig);
236 X509_SIG_free(sig); 234 freezero(s, (unsigned int)siglen);
237 if (s != NULL) {
238 explicit_bzero(s, (unsigned int)siglen);
239 free(s);
240 }
241 return ret; 235 return ret;
242} 236}
243 237
diff --git a/src/lib/libtls/tls_config.c b/src/lib/libtls/tls_config.c
index 65063117e2..3945da75ac 100644
--- a/src/lib/libtls/tls_config.c
+++ b/src/lib/libtls/tls_config.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls_config.c,v 1.38 2017/04/30 02:10:22 jsing Exp $ */ 1/* $OpenBSD: tls_config.c,v 1.39 2017/05/02 03:59:45 deraadt Exp $ */
2/* 2/*
3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2014 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -188,9 +188,7 @@ tls_config_load_file(struct tls_error *error, const char *filetype,
188 fail: 188 fail:
189 if (fd != -1) 189 if (fd != -1)
190 close(fd); 190 close(fd);
191 if (*buf != NULL) 191 freezero(*buf, *len);
192 explicit_bzero(*buf, *len);
193 free(*buf);
194 *buf = NULL; 192 *buf = NULL;
195 *len = 0; 193 *len = 0;
196 194