cryptal  latest
Cryptography Abstraction Layer
Lseries.php
1 <?php
2 
4 
6 
7 class Lseries implements \ArrayAccess
8 {
9  private $values;
10 
11  public function __construct(CryptoInterface $cipher)
12  {
13  $this->values['*'] = $value = $cipher->encrypt('', str_repeat("\x00", 16));
14  $this->values['$'] = $value = self::doubling($value);
15  for ($i = 0; $i <= 128; $i++) {
16  $this->values[$i] = $value = self::doubling($value);
17  }
18  }
19 
20  protected static function doubling($value)
21  {
22  $codes = array_map('ord', str_split($value));
23  $binary = vsprintf(str_repeat("%08b", strlen($value)), $codes);
24  $codes = array_map('bindec', str_split(substr($binary, 1) . '0', 8));
25 
26  // Make this method resilient against timing attacks
27  if (ord($value[0]) & 0x80) {
28  $codes[15] ^= 0x87;
29  } else {
30  $codes[15] ^= 0x0;
31  }
32 
33  return implode('', array_map('chr', $codes));
34  }
35 
36  public function offsetExists($offset)
37  {
38  return isset($this->values[$offset]);
39  }
40 
41  public function offsetGet($offset)
42  {
43  return isset($this->values[$offset]) ? $this->values[$offset] : null;
44  }
45 
46  public function offsetSet($offset, $value)
47  {
48  throw new \RuntimeException('Cannot set offset');
49  }
50 
51  public function offsetUnset($offset)
52  {
53  throw new \RuntimeException('Cannot unset offset');
54  }
55 }
encrypt($iv, $data, &$tag=null, $aad= '')
$cipher
Approved block cipher with a 128-bit block size.
Definition: OCB.php:19