summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortedu <>2016-05-03 12:38:53 +0000
committertedu <>2016-05-03 12:38:53 +0000
commitd137168706e1e6c7bf062b7023d10b2efa857a92 (patch)
treeb82aa12b453d40dd8f8ba316bad714a4590f3c18
parent47a689581163ba7f141a964239fe8ba672ff4737 (diff)
downloadopenbsd-d137168706e1e6c7bf062b7023d10b2efa857a92.tar.gz
openbsd-d137168706e1e6c7bf062b7023d10b2efa857a92.tar.bz2
openbsd-d137168706e1e6c7bf062b7023d10b2efa857a92.zip
patch from openssl for multiple issues:
missing padding check in aesni functions overflow in evp encode functions use of invalid negative asn.1 types ok beck
-rw-r--r--src/lib/libcrypto/asn1/a_d2i_fp.c51
-rw-r--r--src/lib/libcrypto/asn1/a_type.c4
-rw-r--r--src/lib/libcrypto/asn1/tasn_dec.c4
-rw-r--r--src/lib/libcrypto/asn1/tasn_enc.c4
-rw-r--r--src/lib/libcrypto/constant_time_locl.h209
-rw-r--r--src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c5
-rw-r--r--src/lib/libcrypto/evp/encode.c14
-rw-r--r--src/lib/libcrypto/evp/evp_enc.c4
-rw-r--r--src/lib/libssl/src/crypto/asn1/a_d2i_fp.c51
-rw-r--r--src/lib/libssl/src/crypto/asn1/a_type.c4
-rw-r--r--src/lib/libssl/src/crypto/asn1/tasn_dec.c4
-rw-r--r--src/lib/libssl/src/crypto/asn1/tasn_enc.c4
-rw-r--r--src/lib/libssl/src/crypto/constant_time_locl.h209
-rw-r--r--src/lib/libssl/src/crypto/evp/e_aes_cbc_hmac_sha1.c5
-rw-r--r--src/lib/libssl/src/crypto/evp/encode.c14
-rw-r--r--src/lib/libssl/src/crypto/evp/evp_enc.c4
16 files changed, 530 insertions, 60 deletions
diff --git a/src/lib/libcrypto/asn1/a_d2i_fp.c b/src/lib/libcrypto/asn1/a_d2i_fp.c
index c0fb0a3802..d12890ec15 100644
--- a/src/lib/libcrypto/asn1/a_d2i_fp.c
+++ b/src/lib/libcrypto/asn1/a_d2i_fp.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_d2i_fp.c,v 1.11 2014/07/13 11:10:20 miod Exp $ */ 1/* $OpenBSD: a_d2i_fp.c,v 1.12 2016/05/03 12:38:53 tedu 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 *
@@ -144,6 +144,7 @@ ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
144} 144}
145 145
146#define HEADER_SIZE 8 146#define HEADER_SIZE 8
147#define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
147static int 148static int
148asn1_d2i_read_bio(BIO *in, BUF_MEM **pb) 149asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
149{ 150{
@@ -167,18 +168,22 @@ asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
167 if (want >= (len - off)) { 168 if (want >= (len - off)) {
168 want -= (len - off); 169 want -= (len - off);
169 170
170 if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) { 171 if (len + want < len ||
171 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE); 172 !BUF_MEM_grow_clean(b, len + want)) {
173 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
174 ERR_R_MALLOC_FAILURE);
172 goto err; 175 goto err;
173 } 176 }
174 i = BIO_read(in, &(b->data[len]), want); 177 i = BIO_read(in, &(b->data[len]), want);
175 if ((i < 0) && ((len - off) == 0)) { 178 if ((i < 0) && ((len - off) == 0)) {
176 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_NOT_ENOUGH_DATA); 179 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
180 ASN1_R_NOT_ENOUGH_DATA);
177 goto err; 181 goto err;
178 } 182 }
179 if (i > 0) { 183 if (i > 0) {
180 if (len + i < len) { 184 if (len + i < len) {
181 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG); 185 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
186 ASN1_R_TOO_LONG);
182 goto err; 187 goto err;
183 } 188 }
184 len += i; 189 len += i;
@@ -206,7 +211,8 @@ asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
206 /* no data body so go round again */ 211 /* no data body so go round again */
207 eos++; 212 eos++;
208 if (eos < 0) { 213 if (eos < 0) {
209 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_HEADER_TOO_LONG); 214 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
215 ASN1_R_HEADER_TOO_LONG);
210 goto err; 216 goto err;
211 } 217 }
212 want = HEADER_SIZE; 218 want = HEADER_SIZE;
@@ -221,28 +227,45 @@ asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
221 /* suck in c.slen bytes of data */ 227 /* suck in c.slen bytes of data */
222 want = c.slen; 228 want = c.slen;
223 if (want > (len - off)) { 229 if (want > (len - off)) {
230 size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
231
224 want -= (len - off); 232 want -= (len - off);
225 if (want > INT_MAX /* BIO_read takes an int length */ || 233 if (want > INT_MAX /* BIO_read takes an int length */ ||
226 len+want < len) { 234 len+want < len) {
227 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG); 235 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
236 ASN1_R_TOO_LONG);
228 goto err; 237 goto err;
229 } 238 }
230 if (!BUF_MEM_grow_clean(b, len + want)) { 239 /*
231 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE); 240 * Read content in chunks of increasing size
241 * so we can return an error for EOF without
242 * having to allocate the entire content length
243 * in one go.
244 */
245 size_t chunk = want > chunk_max ? chunk_max : want;
246
247 if (!BUF_MEM_grow_clean(b, len + chunk)) {
248 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
249 ERR_R_MALLOC_FAILURE);
232 goto err; 250 goto err;
233 } 251 }
234 while (want > 0) { 252 want -= chunk;
235 i = BIO_read(in, &(b->data[len]), want); 253 while (chunk > 0) {
254 i = BIO_read(in, &(b->data[len]), chunk);
236 if (i <= 0) { 255 if (i <= 0) {
237 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, 256 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
238 ASN1_R_NOT_ENOUGH_DATA); 257 ASN1_R_NOT_ENOUGH_DATA);
239 goto err; 258 goto err;
240 } 259 }
241 /* This can't overflow because 260 /*
242 * |len+want| didn't overflow. */ 261 * This can't overflow because |len+want|
262 * didn't overflow.
263 */
243 len += i; 264 len += i;
244 want -= i; 265 chunk -= i;
245 } 266 }
267 if (chunk_max < INT_MAX/2)
268 chunk_max *= 2;
246 } 269 }
247 if (off + c.slen < off) { 270 if (off + c.slen < off) {
248 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG); 271 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
diff --git a/src/lib/libcrypto/asn1/a_type.c b/src/lib/libcrypto/asn1/a_type.c
index 38b3c65beb..24f8756e73 100644
--- a/src/lib/libcrypto/asn1/a_type.c
+++ b/src/lib/libcrypto/asn1/a_type.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_type.c,v 1.16 2015/03/19 14:00:22 tedu Exp $ */ 1/* $OpenBSD: a_type.c,v 1.17 2016/05/03 12:38:53 tedu 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 *
@@ -127,9 +127,7 @@ ASN1_TYPE_cmp(ASN1_TYPE *a, ASN1_TYPE *b)
127 break; 127 break;
128 128
129 case V_ASN1_INTEGER: 129 case V_ASN1_INTEGER:
130 case V_ASN1_NEG_INTEGER:
131 case V_ASN1_ENUMERATED: 130 case V_ASN1_ENUMERATED:
132 case V_ASN1_NEG_ENUMERATED:
133 case V_ASN1_BIT_STRING: 131 case V_ASN1_BIT_STRING:
134 case V_ASN1_OCTET_STRING: 132 case V_ASN1_OCTET_STRING:
135 case V_ASN1_SEQUENCE: 133 case V_ASN1_SEQUENCE:
diff --git a/src/lib/libcrypto/asn1/tasn_dec.c b/src/lib/libcrypto/asn1/tasn_dec.c
index 23a6740115..55809babb8 100644
--- a/src/lib/libcrypto/asn1/tasn_dec.c
+++ b/src/lib/libcrypto/asn1/tasn_dec.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tasn_dec.c,v 1.29 2015/12/12 21:05:11 beck Exp $ */ 1/* $OpenBSD: tasn_dec.c,v 1.30 2016/05/03 12:38:53 tedu 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 */
@@ -861,9 +861,7 @@ asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype,
861 break; 861 break;
862 862
863 case V_ASN1_INTEGER: 863 case V_ASN1_INTEGER:
864 case V_ASN1_NEG_INTEGER:
865 case V_ASN1_ENUMERATED: 864 case V_ASN1_ENUMERATED:
866 case V_ASN1_NEG_ENUMERATED:
867 tint = (ASN1_INTEGER **)pval; 865 tint = (ASN1_INTEGER **)pval;
868 if (!c2i_ASN1_INTEGER(tint, &cont, len)) 866 if (!c2i_ASN1_INTEGER(tint, &cont, len))
869 goto err; 867 goto err;
diff --git a/src/lib/libcrypto/asn1/tasn_enc.c b/src/lib/libcrypto/asn1/tasn_enc.c
index 0a6426a95e..f4b8b300ca 100644
--- a/src/lib/libcrypto/asn1/tasn_enc.c
+++ b/src/lib/libcrypto/asn1/tasn_enc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tasn_enc.c,v 1.17 2015/12/22 08:44:44 mmcc Exp $ */ 1/* $OpenBSD: tasn_enc.c,v 1.18 2016/05/03 12:38:53 tedu 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 */
@@ -603,9 +603,7 @@ asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
603 break; 603 break;
604 604
605 case V_ASN1_INTEGER: 605 case V_ASN1_INTEGER:
606 case V_ASN1_NEG_INTEGER:
607 case V_ASN1_ENUMERATED: 606 case V_ASN1_ENUMERATED:
608 case V_ASN1_NEG_ENUMERATED:
609 /* These are all have the same content format 607 /* These are all have the same content format
610 * as ASN1_INTEGER 608 * as ASN1_INTEGER
611 */ 609 */
diff --git a/src/lib/libcrypto/constant_time_locl.h b/src/lib/libcrypto/constant_time_locl.h
new file mode 100644
index 0000000000..f8f0eae7ea
--- /dev/null
+++ b/src/lib/libcrypto/constant_time_locl.h
@@ -0,0 +1,209 @@
1/* crypto/constant_time_locl.h */
2/*-
3 * Utilities for constant-time cryptography.
4 *
5 * Author: Emilia Kasper (emilia@openssl.org)
6 * Based on previous work by Bodo Moeller, Emilia Kasper, Adam Langley
7 * (Google).
8 * ====================================================================
9 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * "This product includes cryptographic software written by
22 * Eric Young (eay@cryptsoft.com)"
23 * The word 'cryptographic' can be left out if the rouines from the library
24 * being used are not cryptographic related :-).
25 * 4. If you include any Windows specific code (or a derivative thereof) from
26 * the apps directory (application code) you must include an acknowledgement:
27 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
28 *
29 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * The licence and distribution terms for any publically available version or
42 * derivative of this code cannot be changed. i.e. this code cannot simply be
43 * copied and put under another distribution licence
44 * [including the GNU Public Licence.]
45 */
46
47#ifndef HEADER_CONSTANT_TIME_LOCL_H
48# define HEADER_CONSTANT_TIME_LOCL_H
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
54/*-
55 * The boolean methods return a bitmask of all ones (0xff...f) for true
56 * and 0 for false. This is useful for choosing a value based on the result
57 * of a conditional in constant time. For example,
58 *
59 * if (a < b) {
60 * c = a;
61 * } else {
62 * c = b;
63 * }
64 *
65 * can be written as
66 *
67 * unsigned int lt = constant_time_lt(a, b);
68 * c = constant_time_select(lt, a, b);
69 */
70
71/*
72 * Returns the given value with the MSB copied to all the other
73 * bits. Uses the fact that arithmetic shift shifts-in the sign bit.
74 * However, this is not ensured by the C standard so you may need to
75 * replace this with something else on odd CPUs.
76 */
77static inline unsigned int constant_time_msb(unsigned int a);
78
79/*
80 * Returns 0xff..f if a < b and 0 otherwise.
81 */
82static inline unsigned int constant_time_lt(unsigned int a, unsigned int b);
83/* Convenience method for getting an 8-bit mask. */
84static inline unsigned char constant_time_lt_8(unsigned int a,
85 unsigned int b);
86
87/*
88 * Returns 0xff..f if a >= b and 0 otherwise.
89 */
90static inline unsigned int constant_time_ge(unsigned int a, unsigned int b);
91/* Convenience method for getting an 8-bit mask. */
92static inline unsigned char constant_time_ge_8(unsigned int a,
93 unsigned int b);
94
95/*
96 * Returns 0xff..f if a == 0 and 0 otherwise.
97 */
98static inline unsigned int constant_time_is_zero(unsigned int a);
99/* Convenience method for getting an 8-bit mask. */
100static inline unsigned char constant_time_is_zero_8(unsigned int a);
101
102/*
103 * Returns 0xff..f if a == b and 0 otherwise.
104 */
105static inline unsigned int constant_time_eq(unsigned int a, unsigned int b);
106/* Convenience method for getting an 8-bit mask. */
107static inline unsigned char constant_time_eq_8(unsigned int a,
108 unsigned int b);
109/* Signed integers. */
110static inline unsigned int constant_time_eq_int(int a, int b);
111/* Convenience method for getting an 8-bit mask. */
112static inline unsigned char constant_time_eq_int_8(int a, int b);
113
114/*-
115 * Returns (mask & a) | (~mask & b).
116 *
117 * When |mask| is all 1s or all 0s (as returned by the methods above),
118 * the select methods return either |a| (if |mask| is nonzero) or |b|
119 * (if |mask| is zero).
120 */
121static inline unsigned int constant_time_select(unsigned int mask,
122 unsigned int a,
123 unsigned int b);
124/* Convenience method for unsigned chars. */
125static inline unsigned char constant_time_select_8(unsigned char mask,
126 unsigned char a,
127 unsigned char b);
128/* Convenience method for signed integers. */
129static inline int constant_time_select_int(unsigned int mask, int a, int b);
130
131static inline unsigned int constant_time_msb(unsigned int a)
132{
133 return 0 - (a >> (sizeof(a) * 8 - 1));
134}
135
136static inline unsigned int constant_time_lt(unsigned int a, unsigned int b)
137{
138 return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
139}
140
141static inline unsigned char constant_time_lt_8(unsigned int a, unsigned int b)
142{
143 return (unsigned char)(constant_time_lt(a, b));
144}
145
146static inline unsigned int constant_time_ge(unsigned int a, unsigned int b)
147{
148 return ~constant_time_lt(a, b);
149}
150
151static inline unsigned char constant_time_ge_8(unsigned int a, unsigned int b)
152{
153 return (unsigned char)(constant_time_ge(a, b));
154}
155
156static inline unsigned int constant_time_is_zero(unsigned int a)
157{
158 return constant_time_msb(~a & (a - 1));
159}
160
161static inline unsigned char constant_time_is_zero_8(unsigned int a)
162{
163 return (unsigned char)(constant_time_is_zero(a));
164}
165
166static inline unsigned int constant_time_eq(unsigned int a, unsigned int b)
167{
168 return constant_time_is_zero(a ^ b);
169}
170
171static inline unsigned char constant_time_eq_8(unsigned int a, unsigned int b)
172{
173 return (unsigned char)(constant_time_eq(a, b));
174}
175
176static inline unsigned int constant_time_eq_int(int a, int b)
177{
178 return constant_time_eq((unsigned)(a), (unsigned)(b));
179}
180
181static inline unsigned char constant_time_eq_int_8(int a, int b)
182{
183 return constant_time_eq_8((unsigned)(a), (unsigned)(b));
184}
185
186static inline unsigned int constant_time_select(unsigned int mask,
187 unsigned int a,
188 unsigned int b)
189{
190 return (mask & a) | (~mask & b);
191}
192
193static inline unsigned char constant_time_select_8(unsigned char mask,
194 unsigned char a,
195 unsigned char b)
196{
197 return (unsigned char)(constant_time_select(mask, a, b));
198}
199
200static inline int constant_time_select_int(unsigned int mask, int a, int b)
201{
202 return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
203}
204
205#ifdef __cplusplus
206}
207#endif
208
209#endif /* HEADER_CONSTANT_TIME_LOCL_H */
diff --git a/src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c b/src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c
index c76c2b1c52..8d33896e1c 100644
--- a/src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c
+++ b/src/lib/libcrypto/evp/e_aes_cbc_hmac_sha1.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: e_aes_cbc_hmac_sha1.c,v 1.9 2015/09/10 15:56:25 jsing Exp $ */ 1/* $OpenBSD: e_aes_cbc_hmac_sha1.c,v 1.10 2016/05/03 12:38:53 tedu Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2011-2013 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2011-2013 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -60,6 +60,7 @@
60#include <openssl/aes.h> 60#include <openssl/aes.h>
61#include <openssl/sha.h> 61#include <openssl/sha.h>
62#include "evp_locl.h" 62#include "evp_locl.h"
63#include "constant_time_locl.h"
63 64
64#ifndef EVP_CIPH_FLAG_AEAD_CIPHER 65#ifndef EVP_CIPH_FLAG_AEAD_CIPHER
65#define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000 66#define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000
@@ -282,6 +283,8 @@ aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
282 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8); 283 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
283 maxpad &= 255; 284 maxpad &= 255;
284 285
286 ret &= constant_time_ge(maxpad, pad);
287
285 inp_len = len - (SHA_DIGEST_LENGTH + pad + 1); 288 inp_len = len - (SHA_DIGEST_LENGTH + pad + 1);
286 mask = (0 - ((inp_len - len) >> 289 mask = (0 - ((inp_len - len) >>
287 (sizeof(inp_len) * 8 - 1))); 290 (sizeof(inp_len) * 8 - 1)));
diff --git a/src/lib/libcrypto/evp/encode.c b/src/lib/libcrypto/evp/encode.c
index 725667bfff..0dd87eb1a9 100644
--- a/src/lib/libcrypto/evp/encode.c
+++ b/src/lib/libcrypto/evp/encode.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: encode.c,v 1.20 2015/02/07 13:19:15 doug Exp $ */ 1/* $OpenBSD: encode.c,v 1.21 2016/05/03 12:38:53 tedu 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 *
@@ -56,6 +56,7 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#include <sys/limits.h>
59#include <stdio.h> 60#include <stdio.h>
60#include <string.h> 61#include <string.h>
61 62
@@ -124,13 +125,13 @@ EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
124 const unsigned char *in, int inl) 125 const unsigned char *in, int inl)
125{ 126{
126 int i, j; 127 int i, j;
127 unsigned int total = 0; 128 size_t total = 0;
128 129
129 *outl = 0; 130 *outl = 0;
130 if (inl == 0) 131 if (inl == 0)
131 return; 132 return;
132 OPENSSL_assert(ctx->length <= (int)sizeof(ctx->enc_data)); 133 OPENSSL_assert(ctx->length <= (int)sizeof(ctx->enc_data));
133 if ((ctx->num + inl) < ctx->length) { 134 if (ctx->length - ctx->num > inl) {
134 memcpy(&(ctx->enc_data[ctx->num]), in, inl); 135 memcpy(&(ctx->enc_data[ctx->num]), in, inl);
135 ctx->num += inl; 136 ctx->num += inl;
136 return; 137 return;
@@ -147,7 +148,7 @@ EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
147 *out = '\0'; 148 *out = '\0';
148 total = j + 1; 149 total = j + 1;
149 } 150 }
150 while (inl >= ctx->length) { 151 while (inl >= ctx->length && total <= INT_MAX) {
151 j = EVP_EncodeBlock(out, in, ctx->length); 152 j = EVP_EncodeBlock(out, in, ctx->length);
152 in += ctx->length; 153 in += ctx->length;
153 inl -= ctx->length; 154 inl -= ctx->length;
@@ -156,6 +157,11 @@ EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
156 *out = '\0'; 157 *out = '\0';
157 total += j + 1; 158 total += j + 1;
158 } 159 }
160 if (total > INT_MAX) {
161 /* Too much output data! */
162 *outl = 0;
163 return;
164 }
159 if (inl != 0) 165 if (inl != 0)
160 memcpy(&(ctx->enc_data[0]), in, inl); 166 memcpy(&(ctx->enc_data[0]), in, inl);
161 ctx->num = inl; 167 ctx->num = inl;
diff --git a/src/lib/libcrypto/evp/evp_enc.c b/src/lib/libcrypto/evp/evp_enc.c
index 99bf59e05f..30941ed83d 100644
--- a/src/lib/libcrypto/evp/evp_enc.c
+++ b/src/lib/libcrypto/evp/evp_enc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: evp_enc.c,v 1.27 2015/09/10 15:56:25 jsing Exp $ */ 1/* $OpenBSD: evp_enc.c,v 1.28 2016/05/03 12:38:53 tedu 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 *
@@ -334,7 +334,7 @@ EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
334 return 0; 334 return 0;
335 } 335 }
336 if (i != 0) { 336 if (i != 0) {
337 if (i + inl < bl) { 337 if (bl - i > inl) {
338 memcpy(&(ctx->buf[i]), in, inl); 338 memcpy(&(ctx->buf[i]), in, inl);
339 ctx->buf_len += inl; 339 ctx->buf_len += inl;
340 *outl = 0; 340 *outl = 0;
diff --git a/src/lib/libssl/src/crypto/asn1/a_d2i_fp.c b/src/lib/libssl/src/crypto/asn1/a_d2i_fp.c
index c0fb0a3802..d12890ec15 100644
--- a/src/lib/libssl/src/crypto/asn1/a_d2i_fp.c
+++ b/src/lib/libssl/src/crypto/asn1/a_d2i_fp.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_d2i_fp.c,v 1.11 2014/07/13 11:10:20 miod Exp $ */ 1/* $OpenBSD: a_d2i_fp.c,v 1.12 2016/05/03 12:38:53 tedu 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 *
@@ -144,6 +144,7 @@ ASN1_item_d2i_fp(const ASN1_ITEM *it, FILE *in, void *x)
144} 144}
145 145
146#define HEADER_SIZE 8 146#define HEADER_SIZE 8
147#define ASN1_CHUNK_INITIAL_SIZE (16 * 1024)
147static int 148static int
148asn1_d2i_read_bio(BIO *in, BUF_MEM **pb) 149asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
149{ 150{
@@ -167,18 +168,22 @@ asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
167 if (want >= (len - off)) { 168 if (want >= (len - off)) {
168 want -= (len - off); 169 want -= (len - off);
169 170
170 if (len + want < len || !BUF_MEM_grow_clean(b, len + want)) { 171 if (len + want < len ||
171 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE); 172 !BUF_MEM_grow_clean(b, len + want)) {
173 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
174 ERR_R_MALLOC_FAILURE);
172 goto err; 175 goto err;
173 } 176 }
174 i = BIO_read(in, &(b->data[len]), want); 177 i = BIO_read(in, &(b->data[len]), want);
175 if ((i < 0) && ((len - off) == 0)) { 178 if ((i < 0) && ((len - off) == 0)) {
176 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_NOT_ENOUGH_DATA); 179 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
180 ASN1_R_NOT_ENOUGH_DATA);
177 goto err; 181 goto err;
178 } 182 }
179 if (i > 0) { 183 if (i > 0) {
180 if (len + i < len) { 184 if (len + i < len) {
181 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG); 185 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
186 ASN1_R_TOO_LONG);
182 goto err; 187 goto err;
183 } 188 }
184 len += i; 189 len += i;
@@ -206,7 +211,8 @@ asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
206 /* no data body so go round again */ 211 /* no data body so go round again */
207 eos++; 212 eos++;
208 if (eos < 0) { 213 if (eos < 0) {
209 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_HEADER_TOO_LONG); 214 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
215 ASN1_R_HEADER_TOO_LONG);
210 goto err; 216 goto err;
211 } 217 }
212 want = HEADER_SIZE; 218 want = HEADER_SIZE;
@@ -221,28 +227,45 @@ asn1_d2i_read_bio(BIO *in, BUF_MEM **pb)
221 /* suck in c.slen bytes of data */ 227 /* suck in c.slen bytes of data */
222 want = c.slen; 228 want = c.slen;
223 if (want > (len - off)) { 229 if (want > (len - off)) {
230 size_t chunk_max = ASN1_CHUNK_INITIAL_SIZE;
231
224 want -= (len - off); 232 want -= (len - off);
225 if (want > INT_MAX /* BIO_read takes an int length */ || 233 if (want > INT_MAX /* BIO_read takes an int length */ ||
226 len+want < len) { 234 len+want < len) {
227 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG); 235 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
236 ASN1_R_TOO_LONG);
228 goto err; 237 goto err;
229 } 238 }
230 if (!BUF_MEM_grow_clean(b, len + want)) { 239 /*
231 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ERR_R_MALLOC_FAILURE); 240 * Read content in chunks of increasing size
241 * so we can return an error for EOF without
242 * having to allocate the entire content length
243 * in one go.
244 */
245 size_t chunk = want > chunk_max ? chunk_max : want;
246
247 if (!BUF_MEM_grow_clean(b, len + chunk)) {
248 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
249 ERR_R_MALLOC_FAILURE);
232 goto err; 250 goto err;
233 } 251 }
234 while (want > 0) { 252 want -= chunk;
235 i = BIO_read(in, &(b->data[len]), want); 253 while (chunk > 0) {
254 i = BIO_read(in, &(b->data[len]), chunk);
236 if (i <= 0) { 255 if (i <= 0) {
237 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, 256 ASN1err(ASN1_F_ASN1_D2I_READ_BIO,
238 ASN1_R_NOT_ENOUGH_DATA); 257 ASN1_R_NOT_ENOUGH_DATA);
239 goto err; 258 goto err;
240 } 259 }
241 /* This can't overflow because 260 /*
242 * |len+want| didn't overflow. */ 261 * This can't overflow because |len+want|
262 * didn't overflow.
263 */
243 len += i; 264 len += i;
244 want -= i; 265 chunk -= i;
245 } 266 }
267 if (chunk_max < INT_MAX/2)
268 chunk_max *= 2;
246 } 269 }
247 if (off + c.slen < off) { 270 if (off + c.slen < off) {
248 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG); 271 ASN1err(ASN1_F_ASN1_D2I_READ_BIO, ASN1_R_TOO_LONG);
diff --git a/src/lib/libssl/src/crypto/asn1/a_type.c b/src/lib/libssl/src/crypto/asn1/a_type.c
index 38b3c65beb..24f8756e73 100644
--- a/src/lib/libssl/src/crypto/asn1/a_type.c
+++ b/src/lib/libssl/src/crypto/asn1/a_type.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: a_type.c,v 1.16 2015/03/19 14:00:22 tedu Exp $ */ 1/* $OpenBSD: a_type.c,v 1.17 2016/05/03 12:38:53 tedu 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 *
@@ -127,9 +127,7 @@ ASN1_TYPE_cmp(ASN1_TYPE *a, ASN1_TYPE *b)
127 break; 127 break;
128 128
129 case V_ASN1_INTEGER: 129 case V_ASN1_INTEGER:
130 case V_ASN1_NEG_INTEGER:
131 case V_ASN1_ENUMERATED: 130 case V_ASN1_ENUMERATED:
132 case V_ASN1_NEG_ENUMERATED:
133 case V_ASN1_BIT_STRING: 131 case V_ASN1_BIT_STRING:
134 case V_ASN1_OCTET_STRING: 132 case V_ASN1_OCTET_STRING:
135 case V_ASN1_SEQUENCE: 133 case V_ASN1_SEQUENCE:
diff --git a/src/lib/libssl/src/crypto/asn1/tasn_dec.c b/src/lib/libssl/src/crypto/asn1/tasn_dec.c
index 23a6740115..55809babb8 100644
--- a/src/lib/libssl/src/crypto/asn1/tasn_dec.c
+++ b/src/lib/libssl/src/crypto/asn1/tasn_dec.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tasn_dec.c,v 1.29 2015/12/12 21:05:11 beck Exp $ */ 1/* $OpenBSD: tasn_dec.c,v 1.30 2016/05/03 12:38:53 tedu 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 */
@@ -861,9 +861,7 @@ asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype,
861 break; 861 break;
862 862
863 case V_ASN1_INTEGER: 863 case V_ASN1_INTEGER:
864 case V_ASN1_NEG_INTEGER:
865 case V_ASN1_ENUMERATED: 864 case V_ASN1_ENUMERATED:
866 case V_ASN1_NEG_ENUMERATED:
867 tint = (ASN1_INTEGER **)pval; 865 tint = (ASN1_INTEGER **)pval;
868 if (!c2i_ASN1_INTEGER(tint, &cont, len)) 866 if (!c2i_ASN1_INTEGER(tint, &cont, len))
869 goto err; 867 goto err;
diff --git a/src/lib/libssl/src/crypto/asn1/tasn_enc.c b/src/lib/libssl/src/crypto/asn1/tasn_enc.c
index 0a6426a95e..f4b8b300ca 100644
--- a/src/lib/libssl/src/crypto/asn1/tasn_enc.c
+++ b/src/lib/libssl/src/crypto/asn1/tasn_enc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tasn_enc.c,v 1.17 2015/12/22 08:44:44 mmcc Exp $ */ 1/* $OpenBSD: tasn_enc.c,v 1.18 2016/05/03 12:38:53 tedu 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 */
@@ -603,9 +603,7 @@ asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
603 break; 603 break;
604 604
605 case V_ASN1_INTEGER: 605 case V_ASN1_INTEGER:
606 case V_ASN1_NEG_INTEGER:
607 case V_ASN1_ENUMERATED: 606 case V_ASN1_ENUMERATED:
608 case V_ASN1_NEG_ENUMERATED:
609 /* These are all have the same content format 607 /* These are all have the same content format
610 * as ASN1_INTEGER 608 * as ASN1_INTEGER
611 */ 609 */
diff --git a/src/lib/libssl/src/crypto/constant_time_locl.h b/src/lib/libssl/src/crypto/constant_time_locl.h
new file mode 100644
index 0000000000..f8f0eae7ea
--- /dev/null
+++ b/src/lib/libssl/src/crypto/constant_time_locl.h
@@ -0,0 +1,209 @@
1/* crypto/constant_time_locl.h */
2/*-
3 * Utilities for constant-time cryptography.
4 *
5 * Author: Emilia Kasper (emilia@openssl.org)
6 * Based on previous work by Bodo Moeller, Emilia Kasper, Adam Langley
7 * (Google).
8 * ====================================================================
9 * Copyright (c) 2014 The OpenSSL Project. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * "This product includes cryptographic software written by
22 * Eric Young (eay@cryptsoft.com)"
23 * The word 'cryptographic' can be left out if the rouines from the library
24 * being used are not cryptographic related :-).
25 * 4. If you include any Windows specific code (or a derivative thereof) from
26 * the apps directory (application code) you must include an acknowledgement:
27 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
28 *
29 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 * The licence and distribution terms for any publically available version or
42 * derivative of this code cannot be changed. i.e. this code cannot simply be
43 * copied and put under another distribution licence
44 * [including the GNU Public Licence.]
45 */
46
47#ifndef HEADER_CONSTANT_TIME_LOCL_H
48# define HEADER_CONSTANT_TIME_LOCL_H
49
50#ifdef __cplusplus
51extern "C" {
52#endif
53
54/*-
55 * The boolean methods return a bitmask of all ones (0xff...f) for true
56 * and 0 for false. This is useful for choosing a value based on the result
57 * of a conditional in constant time. For example,
58 *
59 * if (a < b) {
60 * c = a;
61 * } else {
62 * c = b;
63 * }
64 *
65 * can be written as
66 *
67 * unsigned int lt = constant_time_lt(a, b);
68 * c = constant_time_select(lt, a, b);
69 */
70
71/*
72 * Returns the given value with the MSB copied to all the other
73 * bits. Uses the fact that arithmetic shift shifts-in the sign bit.
74 * However, this is not ensured by the C standard so you may need to
75 * replace this with something else on odd CPUs.
76 */
77static inline unsigned int constant_time_msb(unsigned int a);
78
79/*
80 * Returns 0xff..f if a < b and 0 otherwise.
81 */
82static inline unsigned int constant_time_lt(unsigned int a, unsigned int b);
83/* Convenience method for getting an 8-bit mask. */
84static inline unsigned char constant_time_lt_8(unsigned int a,
85 unsigned int b);
86
87/*
88 * Returns 0xff..f if a >= b and 0 otherwise.
89 */
90static inline unsigned int constant_time_ge(unsigned int a, unsigned int b);
91/* Convenience method for getting an 8-bit mask. */
92static inline unsigned char constant_time_ge_8(unsigned int a,
93 unsigned int b);
94
95/*
96 * Returns 0xff..f if a == 0 and 0 otherwise.
97 */
98static inline unsigned int constant_time_is_zero(unsigned int a);
99/* Convenience method for getting an 8-bit mask. */
100static inline unsigned char constant_time_is_zero_8(unsigned int a);
101
102/*
103 * Returns 0xff..f if a == b and 0 otherwise.
104 */
105static inline unsigned int constant_time_eq(unsigned int a, unsigned int b);
106/* Convenience method for getting an 8-bit mask. */
107static inline unsigned char constant_time_eq_8(unsigned int a,
108 unsigned int b);
109/* Signed integers. */
110static inline unsigned int constant_time_eq_int(int a, int b);
111/* Convenience method for getting an 8-bit mask. */
112static inline unsigned char constant_time_eq_int_8(int a, int b);
113
114/*-
115 * Returns (mask & a) | (~mask & b).
116 *
117 * When |mask| is all 1s or all 0s (as returned by the methods above),
118 * the select methods return either |a| (if |mask| is nonzero) or |b|
119 * (if |mask| is zero).
120 */
121static inline unsigned int constant_time_select(unsigned int mask,
122 unsigned int a,
123 unsigned int b);
124/* Convenience method for unsigned chars. */
125static inline unsigned char constant_time_select_8(unsigned char mask,
126 unsigned char a,
127 unsigned char b);
128/* Convenience method for signed integers. */
129static inline int constant_time_select_int(unsigned int mask, int a, int b);
130
131static inline unsigned int constant_time_msb(unsigned int a)
132{
133 return 0 - (a >> (sizeof(a) * 8 - 1));
134}
135
136static inline unsigned int constant_time_lt(unsigned int a, unsigned int b)
137{
138 return constant_time_msb(a ^ ((a ^ b) | ((a - b) ^ b)));
139}
140
141static inline unsigned char constant_time_lt_8(unsigned int a, unsigned int b)
142{
143 return (unsigned char)(constant_time_lt(a, b));
144}
145
146static inline unsigned int constant_time_ge(unsigned int a, unsigned int b)
147{
148 return ~constant_time_lt(a, b);
149}
150
151static inline unsigned char constant_time_ge_8(unsigned int a, unsigned int b)
152{
153 return (unsigned char)(constant_time_ge(a, b));
154}
155
156static inline unsigned int constant_time_is_zero(unsigned int a)
157{
158 return constant_time_msb(~a & (a - 1));
159}
160
161static inline unsigned char constant_time_is_zero_8(unsigned int a)
162{
163 return (unsigned char)(constant_time_is_zero(a));
164}
165
166static inline unsigned int constant_time_eq(unsigned int a, unsigned int b)
167{
168 return constant_time_is_zero(a ^ b);
169}
170
171static inline unsigned char constant_time_eq_8(unsigned int a, unsigned int b)
172{
173 return (unsigned char)(constant_time_eq(a, b));
174}
175
176static inline unsigned int constant_time_eq_int(int a, int b)
177{
178 return constant_time_eq((unsigned)(a), (unsigned)(b));
179}
180
181static inline unsigned char constant_time_eq_int_8(int a, int b)
182{
183 return constant_time_eq_8((unsigned)(a), (unsigned)(b));
184}
185
186static inline unsigned int constant_time_select(unsigned int mask,
187 unsigned int a,
188 unsigned int b)
189{
190 return (mask & a) | (~mask & b);
191}
192
193static inline unsigned char constant_time_select_8(unsigned char mask,
194 unsigned char a,
195 unsigned char b)
196{
197 return (unsigned char)(constant_time_select(mask, a, b));
198}
199
200static inline int constant_time_select_int(unsigned int mask, int a, int b)
201{
202 return (int)(constant_time_select(mask, (unsigned)(a), (unsigned)(b)));
203}
204
205#ifdef __cplusplus
206}
207#endif
208
209#endif /* HEADER_CONSTANT_TIME_LOCL_H */
diff --git a/src/lib/libssl/src/crypto/evp/e_aes_cbc_hmac_sha1.c b/src/lib/libssl/src/crypto/evp/e_aes_cbc_hmac_sha1.c
index c76c2b1c52..8d33896e1c 100644
--- a/src/lib/libssl/src/crypto/evp/e_aes_cbc_hmac_sha1.c
+++ b/src/lib/libssl/src/crypto/evp/e_aes_cbc_hmac_sha1.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: e_aes_cbc_hmac_sha1.c,v 1.9 2015/09/10 15:56:25 jsing Exp $ */ 1/* $OpenBSD: e_aes_cbc_hmac_sha1.c,v 1.10 2016/05/03 12:38:53 tedu Exp $ */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 2011-2013 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 2011-2013 The OpenSSL Project. All rights reserved.
4 * 4 *
@@ -60,6 +60,7 @@
60#include <openssl/aes.h> 60#include <openssl/aes.h>
61#include <openssl/sha.h> 61#include <openssl/sha.h>
62#include "evp_locl.h" 62#include "evp_locl.h"
63#include "constant_time_locl.h"
63 64
64#ifndef EVP_CIPH_FLAG_AEAD_CIPHER 65#ifndef EVP_CIPH_FLAG_AEAD_CIPHER
65#define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000 66#define EVP_CIPH_FLAG_AEAD_CIPHER 0x200000
@@ -282,6 +283,8 @@ aesni_cbc_hmac_sha1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
282 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8); 283 maxpad |= (255 - maxpad) >> (sizeof(maxpad) * 8 - 8);
283 maxpad &= 255; 284 maxpad &= 255;
284 285
286 ret &= constant_time_ge(maxpad, pad);
287
285 inp_len = len - (SHA_DIGEST_LENGTH + pad + 1); 288 inp_len = len - (SHA_DIGEST_LENGTH + pad + 1);
286 mask = (0 - ((inp_len - len) >> 289 mask = (0 - ((inp_len - len) >>
287 (sizeof(inp_len) * 8 - 1))); 290 (sizeof(inp_len) * 8 - 1)));
diff --git a/src/lib/libssl/src/crypto/evp/encode.c b/src/lib/libssl/src/crypto/evp/encode.c
index 725667bfff..0dd87eb1a9 100644
--- a/src/lib/libssl/src/crypto/evp/encode.c
+++ b/src/lib/libssl/src/crypto/evp/encode.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: encode.c,v 1.20 2015/02/07 13:19:15 doug Exp $ */ 1/* $OpenBSD: encode.c,v 1.21 2016/05/03 12:38:53 tedu 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 *
@@ -56,6 +56,7 @@
56 * [including the GNU Public Licence.] 56 * [including the GNU Public Licence.]
57 */ 57 */
58 58
59#include <sys/limits.h>
59#include <stdio.h> 60#include <stdio.h>
60#include <string.h> 61#include <string.h>
61 62
@@ -124,13 +125,13 @@ EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
124 const unsigned char *in, int inl) 125 const unsigned char *in, int inl)
125{ 126{
126 int i, j; 127 int i, j;
127 unsigned int total = 0; 128 size_t total = 0;
128 129
129 *outl = 0; 130 *outl = 0;
130 if (inl == 0) 131 if (inl == 0)
131 return; 132 return;
132 OPENSSL_assert(ctx->length <= (int)sizeof(ctx->enc_data)); 133 OPENSSL_assert(ctx->length <= (int)sizeof(ctx->enc_data));
133 if ((ctx->num + inl) < ctx->length) { 134 if (ctx->length - ctx->num > inl) {
134 memcpy(&(ctx->enc_data[ctx->num]), in, inl); 135 memcpy(&(ctx->enc_data[ctx->num]), in, inl);
135 ctx->num += inl; 136 ctx->num += inl;
136 return; 137 return;
@@ -147,7 +148,7 @@ EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
147 *out = '\0'; 148 *out = '\0';
148 total = j + 1; 149 total = j + 1;
149 } 150 }
150 while (inl >= ctx->length) { 151 while (inl >= ctx->length && total <= INT_MAX) {
151 j = EVP_EncodeBlock(out, in, ctx->length); 152 j = EVP_EncodeBlock(out, in, ctx->length);
152 in += ctx->length; 153 in += ctx->length;
153 inl -= ctx->length; 154 inl -= ctx->length;
@@ -156,6 +157,11 @@ EVP_EncodeUpdate(EVP_ENCODE_CTX *ctx, unsigned char *out, int *outl,
156 *out = '\0'; 157 *out = '\0';
157 total += j + 1; 158 total += j + 1;
158 } 159 }
160 if (total > INT_MAX) {
161 /* Too much output data! */
162 *outl = 0;
163 return;
164 }
159 if (inl != 0) 165 if (inl != 0)
160 memcpy(&(ctx->enc_data[0]), in, inl); 166 memcpy(&(ctx->enc_data[0]), in, inl);
161 ctx->num = inl; 167 ctx->num = inl;
diff --git a/src/lib/libssl/src/crypto/evp/evp_enc.c b/src/lib/libssl/src/crypto/evp/evp_enc.c
index 99bf59e05f..30941ed83d 100644
--- a/src/lib/libssl/src/crypto/evp/evp_enc.c
+++ b/src/lib/libssl/src/crypto/evp/evp_enc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: evp_enc.c,v 1.27 2015/09/10 15:56:25 jsing Exp $ */ 1/* $OpenBSD: evp_enc.c,v 1.28 2016/05/03 12:38:53 tedu 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 *
@@ -334,7 +334,7 @@ EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
334 return 0; 334 return 0;
335 } 335 }
336 if (i != 0) { 336 if (i != 0) {
337 if (i + inl < bl) { 337 if (bl - i > inl) {
338 memcpy(&(ctx->buf[i]), in, inl); 338 memcpy(&(ctx->buf[i]), in, inl);
339 ctx->buf_len += inl; 339 ctx->buf_len += inl;
340 *outl = 0; 340 *outl = 0;