@@ -50,29 +50,26 @@ final class Core
50
50
*/
51
51
public static function incrementCounter ($ ctr , $ inc )
52
52
{
53
- if (Core::ourStrlen ($ ctr ) !== Core::BLOCK_BYTE_SIZE ) {
54
- throw new Ex \EnvironmentIsBrokenException (
55
- 'Trying to increment a nonce of the wrong size. '
56
- );
57
- }
58
-
59
- if (! \is_int ($ inc )) {
60
- throw new Ex \EnvironmentIsBrokenException (
61
- 'Trying to increment nonce by a non-integer. '
62
- );
63
- }
64
-
65
- if ($ inc < 0 ) {
66
- throw new Ex \EnvironmentIsBrokenException (
67
- 'Trying to increment nonce by a negative amount. '
68
- );
69
- }
70
-
71
- if ($ inc > PHP_INT_MAX - 255 ) {
72
- throw new Ex \EnvironmentIsBrokenException (
73
- 'Integer overflow may occur. '
74
- );
75
- }
53
+ Core::ensureTrue (
54
+ Core::ourStrlen ($ ctr ) === Core::BLOCK_BYTE_SIZE ,
55
+ 'Trying to increment a nonce of the wrong size. '
56
+ );
57
+
58
+ Core::ensureTrue (
59
+ \is_int ($ inc ),
60
+ 'Trying to increment nonce by a non-integer. '
61
+ );
62
+
63
+ // The caller is probably re-using CTR-mode keystream if they increment by 0.
64
+ Core::ensureTrue (
65
+ $ inc > 0 ,
66
+ 'Trying to increment a nonce by a nonpositive amount '
67
+ );
68
+
69
+ Core::ensureTrue (
70
+ $ inc <= PHP_INT_MAX - 255 ,
71
+ 'Integer overflow may occur '
72
+ );
76
73
77
74
/*
78
75
* We start at the rightmost byte (big-endian)
@@ -82,11 +79,7 @@ public static function incrementCounter($ctr, $inc)
82
79
$ sum = \ord ($ ctr [$ i ]) + $ inc ;
83
80
84
81
/* Detect integer overflow and fail. */
85
- if (! \is_int ($ sum )) {
86
- throw new Ex \EnvironmentIsBrokenException (
87
- 'Integer overflow in CTR mode nonce increment. '
88
- );
89
- }
82
+ Core::ensureTrue (\is_int ($ sum ), 'Integer overflow in CTR mode nonce increment ' );
90
83
91
84
$ ctr [$ i ] = \pack ('C ' , $ sum & 0xFF );
92
85
$ inc = $ sum >> 8 ;
@@ -146,12 +139,10 @@ public static function HKDF($hash, $ikm, $length, $info = '', $salt = null)
146
139
$ digest_length = Core::ourStrlen (\hash_hmac ($ hash , '' , '' , true ));
147
140
148
141
// Sanity-check the desired output length.
149
- if (empty ($ length ) || ! \is_int ($ length ) ||
150
- $ length < 0 || $ length > 255 * $ digest_length ) {
151
- throw new Ex \EnvironmentIsBrokenException (
152
- 'Bad output length requested of HKDF. '
153
- );
154
- }
142
+ Core::ensureTrue (
143
+ !empty ($ length ) && \is_int ($ length ) && $ length >= 0 && $ length <= 255 * $ digest_length ,
144
+ 'Bad output length requested of HDKF. '
145
+ );
155
146
156
147
// "if [salt] not provided, is set to a string of HashLen zeroes."
157
148
if (\is_null ($ salt )) {
@@ -166,9 +157,7 @@ public static function HKDF($hash, $ikm, $length, $info = '', $salt = null)
166
157
// HKDF-Expand:
167
158
168
159
// This check is useless, but it serves as a reminder to the spec.
169
- if (Core::ourStrlen ($ prk ) < $ digest_length ) {
170
- throw new Ex \EnvironmentIsBrokenException ();
171
- }
160
+ Core::ensureTrue (Core::ourStrlen ($ prk ) >= $ digest_length );
172
161
173
162
// T(0) = ''
174
163
$ t = '' ;
@@ -188,9 +177,7 @@ public static function HKDF($hash, $ikm, $length, $info = '', $salt = null)
188
177
// ORM = first L octets of T
189
178
/** @var string $orm */
190
179
$ orm = Core::ourSubstr ($ t , 0 , $ length );
191
- if (!\is_string ($ orm )) {
192
- throw new Ex \EnvironmentIsBrokenException ();
193
- }
180
+ Core::ensureTrue (\is_string ($ orm ));
194
181
return $ orm ;
195
182
}
196
183
@@ -224,9 +211,7 @@ public static function hashEquals($expected, $given)
224
211
// We're not attempting to make variable-length string comparison
225
212
// secure, as that's very difficult. Make sure the strings are the same
226
213
// length.
227
- if (Core::ourStrlen ($ expected ) !== Core::ourStrlen ($ given )) {
228
- throw new Ex \EnvironmentIsBrokenException ();
229
- }
214
+ Core::ensureTrue (Core::ourStrlen ($ expected ) === Core::ourStrlen ($ given ));
230
215
231
216
$ blind = Core::secureRandom (32 );
232
217
$ message_compare = \hash_hmac (Core::HASH_FUNCTION_NAME , $ given , $ blind );
@@ -243,9 +228,7 @@ public static function hashEquals($expected, $given)
243
228
*/
244
229
public static function ensureConstantExists ($ name )
245
230
{
246
- if (! \defined ($ name )) {
247
- throw new Ex \EnvironmentIsBrokenException ();
248
- }
231
+ Core::ensureTrue (\defined ($ name ));
249
232
}
250
233
251
234
/**
@@ -258,8 +241,22 @@ public static function ensureConstantExists($name)
258
241
*/
259
242
public static function ensureFunctionExists ($ name )
260
243
{
261
- if (! \function_exists ($ name )) {
262
- throw new Ex \EnvironmentIsBrokenException ();
244
+ Core::ensureTrue (\function_exists ($ name ));
245
+ }
246
+
247
+ /**
248
+ * Throws an exception if the condition is false.
249
+ *
250
+ * @param bool $condition
251
+ * @param string $message
252
+ * @return void
253
+ *
254
+ * @throws Ex\EnvironmentIsBrokenException
255
+ */
256
+ public static function ensureTrue ($ condition , $ message = '' )
257
+ {
258
+ if (!$ condition ) {
259
+ throw new Ex \EnvironmentIsBrokenException ($ message );
263
260
}
264
261
}
265
262
@@ -286,9 +283,7 @@ public static function ourStrlen($str)
286
283
}
287
284
if ($ exists ) {
288
285
$ length = \mb_strlen ($ str , '8bit ' );
289
- if ($ length === false ) {
290
- throw new Ex \EnvironmentIsBrokenException ();
291
- }
286
+ Core::ensureTrue ($ length !== false );
292
287
return $ length ;
293
288
} else {
294
289
return \strlen ($ str );
@@ -403,28 +398,22 @@ public static function pbkdf2($algorithm, $password, $salt, $count, $key_length,
403
398
$ key_length += 0 ;
404
399
405
400
$ algorithm = \strtolower ($ algorithm );
406
- if (! \in_array ($ algorithm , \hash_algos (), true )) {
407
- throw new Ex \EnvironmentIsBrokenException (
408
- 'Invalid or unsupported hash algorithm. '
409
- );
410
- }
401
+ Core::ensureTrue (
402
+ \in_array ($ algorithm , \hash_algos (), true ),
403
+ 'Invalid or unsupported hash algorithm. '
404
+ );
411
405
412
406
// Whitelist, or we could end up with people using CRC32.
413
407
$ ok_algorithms = [
414
408
'sha1 ' , 'sha224 ' , 'sha256 ' , 'sha384 ' , 'sha512 ' ,
415
409
'ripemd160 ' , 'ripemd256 ' , 'ripemd320 ' , 'whirlpool ' ,
416
410
];
417
- if (! \in_array ($ algorithm , $ ok_algorithms , true )) {
418
- throw new Ex \EnvironmentIsBrokenException (
419
- 'Algorithm is not a secure cryptographic hash function. '
420
- );
421
- }
411
+ Core::ensureTrue (
412
+ \in_array ($ algorithm , $ ok_algorithms , true ),
413
+ 'Algorithm is not a secure cryptographic hash function. '
414
+ );
422
415
423
- if ($ count <= 0 || $ key_length <= 0 ) {
424
- throw new Ex \EnvironmentIsBrokenException (
425
- 'Invalid PBKDF2 parameters. '
426
- );
427
- }
416
+ Core::ensureTrue ($ count > 0 && $ key_length > 0 , 'Invalid PBKDF2 parameters. ' );
428
417
429
418
if (\function_exists ('hash_pbkdf2 ' )) {
430
419
// The output length is in NIBBLES (4-bits) if $raw_output is false!
0 commit comments