@@ -50,29 +50,26 @@ final class Core
5050 */
5151 public static function incrementCounter ($ ctr , $ inc )
5252 {
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+ );
7673
7774 /*
7875 * We start at the rightmost byte (big-endian)
@@ -82,11 +79,7 @@ public static function incrementCounter($ctr, $inc)
8279 $ sum = \ord ($ ctr [$ i ]) + $ inc ;
8380
8481 /* 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 ' );
9083
9184 $ ctr [$ i ] = \pack ('C ' , $ sum & 0xFF );
9285 $ inc = $ sum >> 8 ;
@@ -146,12 +139,10 @@ public static function HKDF($hash, $ikm, $length, $info = '', $salt = null)
146139 $ digest_length = Core::ourStrlen (\hash_hmac ($ hash , '' , '' , true ));
147140
148141 // 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+ );
155146
156147 // "if [salt] not provided, is set to a string of HashLen zeroes."
157148 if (\is_null ($ salt )) {
@@ -166,9 +157,7 @@ public static function HKDF($hash, $ikm, $length, $info = '', $salt = null)
166157 // HKDF-Expand:
167158
168159 // 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 );
172161
173162 // T(0) = ''
174163 $ t = '' ;
@@ -188,9 +177,7 @@ public static function HKDF($hash, $ikm, $length, $info = '', $salt = null)
188177 // ORM = first L octets of T
189178 /** @var string $orm */
190179 $ orm = Core::ourSubstr ($ t , 0 , $ length );
191- if (!\is_string ($ orm )) {
192- throw new Ex \EnvironmentIsBrokenException ();
193- }
180+ Core::ensureTrue (\is_string ($ orm ));
194181 return $ orm ;
195182 }
196183
@@ -224,9 +211,7 @@ public static function hashEquals($expected, $given)
224211 // We're not attempting to make variable-length string comparison
225212 // secure, as that's very difficult. Make sure the strings are the same
226213 // length.
227- if (Core::ourStrlen ($ expected ) !== Core::ourStrlen ($ given )) {
228- throw new Ex \EnvironmentIsBrokenException ();
229- }
214+ Core::ensureTrue (Core::ourStrlen ($ expected ) === Core::ourStrlen ($ given ));
230215
231216 $ blind = Core::secureRandom (32 );
232217 $ message_compare = \hash_hmac (Core::HASH_FUNCTION_NAME , $ given , $ blind );
@@ -243,9 +228,7 @@ public static function hashEquals($expected, $given)
243228 */
244229 public static function ensureConstantExists ($ name )
245230 {
246- if (! \defined ($ name )) {
247- throw new Ex \EnvironmentIsBrokenException ();
248- }
231+ Core::ensureTrue (\defined ($ name ));
249232 }
250233
251234 /**
@@ -258,8 +241,22 @@ public static function ensureConstantExists($name)
258241 */
259242 public static function ensureFunctionExists ($ name )
260243 {
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 );
263260 }
264261 }
265262
@@ -286,9 +283,7 @@ public static function ourStrlen($str)
286283 }
287284 if ($ exists ) {
288285 $ length = \mb_strlen ($ str , '8bit ' );
289- if ($ length === false ) {
290- throw new Ex \EnvironmentIsBrokenException ();
291- }
286+ Core::ensureTrue ($ length !== false );
292287 return $ length ;
293288 } else {
294289 return \strlen ($ str );
@@ -403,28 +398,22 @@ public static function pbkdf2($algorithm, $password, $salt, $count, $key_length,
403398 $ key_length += 0 ;
404399
405400 $ 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+ );
411405
412406 // Whitelist, or we could end up with people using CRC32.
413407 $ ok_algorithms = [
414408 'sha1 ' , 'sha224 ' , 'sha256 ' , 'sha384 ' , 'sha512 ' ,
415409 'ripemd160 ' , 'ripemd256 ' , 'ripemd320 ' , 'whirlpool ' ,
416410 ];
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+ );
422415
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. ' );
428417
429418 if (\function_exists ('hash_pbkdf2 ' )) {
430419 // The output length is in NIBBLES (4-bits) if $raw_output is false!
0 commit comments