Oihana PHP

assertBlockSizeExponent.php

Table of Contents

Functions

assertBlockSizeExponent()  : int
Asserts that a V3 block-size exponent is one this library will act on, and returns it.

Functions

assertBlockSizeExponent()

Asserts that a V3 block-size exponent is one this library will act on, and returns it.

assertBlockSizeExponent(int $exponent) : int

The exponent is a single byte of the V3 header, so a file can declare any value from 0 to 255 — and the reader meets it before a single byte has been authenticated. Both ends of the range are hostile:

  • Too small. An exponent of 0 asks for 1-byte blocks, which turns a 100 MB file into a hundred million frames, each with its own 16-byte tag and its own openssl_decrypt() call. The file would be 1.6 GB of tags and the read would run for hours. EncryptionFormat::MIN_BLOCK_SIZE_EXPONENT (64 KiB) is the floor.
  • Too large. An exponent of 255 asks the reader to allocate 2^255 bytes for the first block. EncryptionFormat::MAX_BLOCK_SIZE_EXPONENT (1 GiB) is the ceiling, and 1 << 255 is undefined ground this never reaches.

Called on both sides: on write so the library never produces a file it would refuse to read, and on read so a crafted header is refused before it costs anything.

Parameters
$exponent : int

The block-size exponent, as read from a header or chosen for one.

Tags
throws
InvalidArgumentException

If the exponent falls outside the accepted range.

example
use function oihana\files\openssl\assertBlockSizeExponent;

$blockSize = 1 << assertBlockSizeExponent( 23 ) ; // 8388608
assertBlockSizeExponent( 255 ) ;                  // InvalidArgumentException
author

Marc Alcaraz (ekameleon)

since
1.5.0
Return values
int —

The same exponent, so it can be validated in place: 1 << assertBlockSizeExponent( $e ).

On this page

Search results