includes/rijndael.php
author Dan
Fri, 22 Feb 2008 12:51:53 -0500
changeset 458 c433348f3628
parent 378 c1c7fa6b329f
parent 440 105457df35e5
child 461 717e71109645
permissions -rw-r--r--
Merging fixes and updates from stable branch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
458
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
     1
<?php
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
     2
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
     3
/**
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
     4
 * Phijndael - an implementation of the AES encryption standard in PHP
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
     5
 * Originally written by Fritz Schneider <fritz AT cd DOT ucsd DOT edu>
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
     6
 * Ported to PHP by Dan Fuhry <dan AT enano DOT homelinux DOT org>
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
     7
 * @package phijndael
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
     8
 * @author Fritz Schneider
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
     9
 * @author Dan Fuhry
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    10
 * @license BSD-style license
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    11
 */
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    12
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    13
define ('ENC_BASE64', 202);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    14
define ('ENC_BINARY', 203);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    15
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    16
$_aes_objcache = array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    17
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    18
class AESCrypt {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    19
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    20
  var $debug = false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    21
  var $mcrypt = false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    22
  var $decrypt_cache = array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    23
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    24
  // Rijndael parameters --  Valid values are 128, 192, or 256
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    25
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    26
  var $keySizeInBits = 128;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    27
  var $blockSizeInBits = 128;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    28
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    29
  ///////  You shouldn't have to modify anything below this line except for
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    30
  ///////  the function getRandomBytes().
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    31
  //
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    32
  // Note: in the following code the two dimensional arrays are indexed as
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    33
  //       you would probably expect, as array[row][column]. The state arrays
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    34
  //       are 2d arrays of the form state[4][Nb].
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    35
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    36
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    37
  // The number of rounds for the cipher, indexed by [Nk][Nb]
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    38
  var $roundsArray = Array(0,0,0,0,Array(0,0,0,0,10,0, 12,0, 14),0, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    39
                               Array(0,0,0,0,12,0, 12,0, 14),0, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    40
                               Array(0,0,0,0,14,0, 14,0, 14) );
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    41
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    42
  // The number of bytes to shift by in shiftRow, indexed by [Nb][row]
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    43
  var $shiftOffsets = Array(0,0,0,0,Array(0,1, 2, 3),0,Array(0,1, 2, 3),0,Array(0,1, 3, 4) );
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    44
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    45
  // The round constants used in subkey expansion
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    46
  var $Rcon = Array( 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    47
  0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    48
  0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    49
  0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    50
  0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    51
  0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91 );
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    52
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    53
  // Precomputed lookup table for the SBox
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    54
  var $SBox = Array(
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    55
   99, 124, 119, 123, 242, 107, 111, 197,  48,   1, 103,  43, 254, 215, 171, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    56
  118, 202, 130, 201, 125, 250,  89,  71, 240, 173, 212, 162, 175, 156, 164, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    57
  114, 192, 183, 253, 147,  38,  54,  63, 247, 204,  52, 165, 229, 241, 113, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    58
  216,  49,  21,   4, 199,  35, 195,  24, 150,   5, 154,   7,  18, 128, 226, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    59
  235,  39, 178, 117,   9, 131,  44,  26,  27, 110,  90, 160,  82,  59, 214, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    60
  179,  41, 227,  47, 132,  83, 209,   0, 237,  32, 252, 177,  91, 106, 203, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    61
  190,  57,  74,  76,  88, 207, 208, 239, 170, 251,  67,  77,  51, 133,  69, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    62
  249,   2, 127,  80,  60, 159, 168,  81, 163,  64, 143, 146, 157,  56, 245, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    63
  188, 182, 218,  33,  16, 255, 243, 210, 205,  12,  19, 236,  95, 151,  68,  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    64
  23,  196, 167, 126,  61, 100,  93,  25, 115,  96, 129,  79, 220,  34,  42, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    65
  144, 136,  70, 238, 184,  20, 222,  94,  11, 219, 224,  50,  58,  10,  73,
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    66
    6,  36,  92, 194, 211, 172,  98, 145, 149, 228, 121, 231, 200,  55, 109, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    67
  141, 213,  78, 169, 108,  86, 244, 234, 101, 122, 174,   8, 186, 120,  37,  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    68
   46,  28, 166, 180, 198, 232, 221, 116,  31,  75, 189, 139, 138, 112,  62, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    69
  181, 102,  72,   3, 246,  14,  97,  53,  87, 185, 134, 193,  29, 158, 225,
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    70
  248, 152,  17, 105, 217, 142, 148, 155,  30, 135, 233, 206,  85,  40, 223,
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    71
  140, 161, 137,  13, 191, 230,  66, 104,  65, 153,  45,  15, 176,  84, 187,  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    72
   22 );
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    73
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    74
  // Precomputed lookup table for the inverse SBox
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    75
  var $SBoxInverse = Array(
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    76
   82,   9, 106, 213,  48,  54, 165,  56, 191,  64, 163, 158, 129, 243, 215, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    77
  251, 124, 227,  57, 130, 155,  47, 255, 135,  52, 142,  67,  68, 196, 222, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    78
  233, 203,  84, 123, 148,  50, 166, 194,  35,  61, 238,  76, 149,  11,  66, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    79
  250, 195,  78,   8,  46, 161, 102,  40, 217,  36, 178, 118,  91, 162,  73, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    80
  109, 139, 209,  37, 114, 248, 246, 100, 134, 104, 152,  22, 212, 164,  92, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    81
  204,  93, 101, 182, 146, 108, 112,  72,  80, 253, 237, 185, 218,  94,  21,  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    82
   70,  87, 167, 141, 157, 132, 144, 216, 171,   0, 140, 188, 211,  10, 247, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    83
  228,  88,   5, 184, 179,  69,   6, 208,  44,  30, 143, 202,  63,  15,   2, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    84
  193, 175, 189,   3,   1,  19, 138, 107,  58, 145,  17,  65,  79, 103, 220, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    85
  234, 151, 242, 207, 206, 240, 180, 230, 115, 150, 172, 116,  34, 231, 173,
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    86
   53, 133, 226, 249,  55, 232,  28, 117, 223, 110,  71, 241,  26, 113,  29, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    87
   41, 197, 137, 111, 183,  98,  14, 170,  24, 190,  27, 252,  86,  62,  75, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    88
  198, 210, 121,  32, 154, 219, 192, 254, 120, 205,  90, 244,  31, 221, 168,
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    89
   51, 136,   7, 199,  49, 177,  18,  16,  89,  39, 128, 236,  95,  96,  81,
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    90
  127, 169,  25, 181,  74,  13,  45, 229, 122, 159, 147, 201, 156, 239, 160,
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    91
  224,  59,  77, 174,  42, 245, 176, 200, 235, 187,  60, 131,  83, 153,  97, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    92
   23,  43,   4, 126, 186, 119, 214,  38, 225, 105,  20,  99,  85,  33,  12,
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    93
  125 );
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    94
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    95
  function __construct($ks = 128, $bs = 128, $debug = false)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    96
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    97
    $this->keySizeInBits = $ks;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    98
    $this->blockSizeInBits = $bs;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    99
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   100
    // Use the Mcrypt library? This speeds things up dramatically.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   101
    if(defined('MCRYPT_RIJNDAEL_' . $ks) && defined('MCRYPT_ACCEL'))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   102
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   103
      eval('$mcb = MCRYPT_RIJNDAEL_' . $ks.';');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   104
      $bks = mcrypt_module_get_algo_block_size($mcb);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   105
      $bks = $bks * 8;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   106
      if ( $bks != $bs )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   107
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   108
        $mcb = false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   109
        echo (string)$bks;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   110
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   111
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   112
    else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   113
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   114
      $mcb = false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   115
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   116
      
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   117
    $this->mcrypt = $mcb;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   118
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   119
    // Cipher parameters ... do not change these
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   120
    $this->Nk = $this->keySizeInBits / 32;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   121
    $this->Nb = $this->blockSizeInBits / 32;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   122
    $this->Nr = $this->roundsArray[$this->Nk][$this->Nb];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   123
    $this->debug = $debug;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   124
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   125
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   126
  public static function singleton($key_size, $block_size)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   127
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   128
    static $_aes_objcache;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   129
    if ( isset($_aes_objcache["$key_size,$block_size"]) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   130
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   131
      return $_aes_objcache["$key_size,$block_size"];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   132
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   133
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   134
    $_aes_objcache["$key_size,$block_size"] = new AESCrypt($key_size, $block_size);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   135
    return $_aes_objcache["$key_size,$block_size"];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   136
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   137
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   138
  // Error handler
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   139
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   140
  function trigger_error($text, $level = E_USER_NOTICE)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   141
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   142
    $bt = debug_backtrace();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   143
    $lastfunc =& $bt[1];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   144
    switch($level)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   145
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   146
      case E_USER_NOTICE:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   147
      default:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   148
        $desc = 'Notice';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   149
        break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   150
      case E_USER_WARNING:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   151
        $desc = 'Warning';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   152
        break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   153
      case E_USER_ERROR:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   154
        $desc = 'Fatal';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   155
        break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   156
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   157
    ob_start();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   158
    if($this->debug || $level == E_USER_ERROR) echo "AES encryption: <b>{$desc}:</b> $text in {$lastfunc['file']} on line {$lastfunc['line']} in function {$lastfunc['function']}<br />";
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   159
    if($this->debug)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   160
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   161
      //echo '<pre>'.enano_debug_print_backtrace(true).'</pre>';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   162
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   163
    ob_end_flush();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   164
    if($level == E_USER_ERROR)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   165
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   166
      echo '<p><b>This can sometimes happen if you are upgrading Enano to a new version and did not log out first.</b> <a href="'.$_SERVER['PHP_SELF'].'?do=diag&amp;sub=cookie_destroy">Click here</a> to force cookies to clear and try again. You will be logged out.</p>';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   167
      exit;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   168
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   169
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   170
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   171
  function array_slice_js_compat($array, $start, $finish = 0)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   172
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   173
    $len = $finish - $start;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   174
    if($len < 0) $len = 0 - $len;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   175
    //if($this->debug) echo (string)$len . ' ';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   176
    //if(count($array) < $start + $len)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   177
    //  $this->trigger_error('Index out of range', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   178
    return array_slice($array, $start, $len);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   179
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   180
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   181
  function concat($s1, $s2)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   182
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   183
    if(is_array($s1) && is_array($s2))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   184
      return array_merge($s1, $s2);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   185
    elseif( ( is_array($s1) && !is_array($s2) ) || ( !is_array($s1) && is_array($s2) ) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   186
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   187
      $this->trigger_error('incompatible types - you can\'t combine a non-array with an array', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   188
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   189
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   190
    else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   191
      return $s1 . $s2;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   192
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   193
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   194
  // This method circularly shifts the array left by the number of elements
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   195
  // given in its parameter. It returns the resulting array and is used for 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   196
  // the ShiftRow step. Note that shift() and push() could be used for a more 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   197
  // elegant solution, but they require IE5.5+, so I chose to do it manually. 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   198
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   199
  function cyclicShiftLeft($theArray, $positions) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   200
    if(!is_int($positions))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   201
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   202
      $this->trigger_error('$positions is not an integer! Backtrace:<br /><pre>'.print_r(debug_backtrace(), true).'</pre>', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   203
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   204
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   205
    $second = array_slice($theArray, 0, $positions);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   206
    $first = array_slice($theArray, $positions);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   207
    $theArray = array_merge($first, $second);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   208
    return $theArray;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   209
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   210
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   211
  // Multiplies the element "poly" of GF(2^8) by x. See the Rijndael spec.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   212
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   213
  function xtime($poly) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   214
    $poly <<= 1;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   215
    return (($poly & 0x100) ? ($poly ^ 0x11B) : ($poly));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   216
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   217
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   218
  // Multiplies the two elements of GF(2^8) together and returns the result.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   219
  // See the Rijndael spec, but should be straightforward: for each power of
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   220
  // the indeterminant that has a 1 coefficient in x, add y times that power
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   221
  // to the result. x and y should be bytes representing elements of GF(2^8)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   222
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   223
  function mult_GF256($x, $y) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   224
    $result = 0;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   225
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   226
    for ($bit = 1; $bit < 256; $bit *= 2, $y = $this->xtime($y)) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   227
      if ($x & $bit) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   228
        $result ^= $y;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   229
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   230
    return $result;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   231
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   232
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   233
  // Performs the substitution step of the cipher. State is the 2d array of
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   234
  // state information (see spec) and direction is string indicating whether
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   235
  // we are performing the forward substitution ("encrypt") or inverse 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   236
  // substitution (anything else)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   237
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   238
  function byteSub(&$state, $direction) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   239
    //global $this->SBox, $this->SBoxInverse, $this->Nb;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   240
    if ($direction == "encrypt")           // Point S to the SBox we're using
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   241
      $S =& $this->SBox;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   242
    else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   243
      $S =& $this->SBoxInverse;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   244
    for ($i = 0; $i < 4; $i++)           // Substitute for every byte in state
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   245
      for ($j = 0; $j < $this->Nb; $j++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   246
         $state[$i][$j] = $S[$state[$i][$j]];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   247
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   248
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   249
  // Performs the row shifting step of the cipher.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   250
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   251
  function shiftRow(&$state, $direction) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   252
    //global $this->Nb, $this->shiftOffsets;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   253
    for ($i=1; $i<4; $i++)               // Row 0 never shifts
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   254
      if ($direction == "encrypt")
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   255
         $state[$i] = $this->cyclicShiftLeft($state[$i], $this->shiftOffsets[$this->Nb][$i]);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   256
      else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   257
         $state[$i] = $this->cyclicShiftLeft($state[$i], $this->Nb - $this->shiftOffsets[$this->Nb][$i]);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   258
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   259
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   260
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   261
  // Performs the column mixing step of the cipher. Most of these steps can
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   262
  // be combined into table lookups on 32bit values (at least for encryption)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   263
  // to greatly increase the speed. 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   264
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   265
  function mixColumn(&$state, $direction) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   266
    //global $this->Nb;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   267
    $b = Array();                                  // Result of matrix multiplications
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   268
    for ($j = 0; $j < $this->Nb; $j++) {                 // Go through each column...
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   269
      for ($i = 0; $i < 4; $i++) {                 // and for each row in the column...
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   270
        if ($direction == "encrypt")
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   271
          $b[$i] = $this->mult_GF256($state[$i][$j], 2) ^ // perform mixing
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   272
                   $this->mult_GF256($state[($i+1)%4][$j], 3) ^ 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   273
                   $state[($i+2)%4][$j] ^ 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   274
                   $state[($i+3)%4][$j];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   275
        else 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   276
          $b[$i] = $this->mult_GF256($state[$i][$j], 0xE) ^ 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   277
                   $this->mult_GF256($state[($i+1)%4][$j], 0xB) ^
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   278
                   $this->mult_GF256($state[($i+2)%4][$j], 0xD) ^
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   279
                   $this->mult_GF256($state[($i+3)%4][$j], 9);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   280
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   281
      for ($i = 0; $i < 4; $i++)          // Place result back into column
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   282
        $state[$i][$j] = $b[$i];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   283
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   284
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   285
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   286
  // Adds the current round key to the state information. Straightforward.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   287
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   288
  function addRoundKey(&$state, $roundKey) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   289
    //global $this->Nb;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   290
    for ($j = 0; $j < $this->Nb; $j++) {                      // Step through columns...
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   291
      $state[0][$j] ^= ( $roundKey[$j] & 0xFF);         // and XOR
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   292
      $state[1][$j] ^= (($roundKey[$j]>>8) & 0xFF);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   293
      $state[2][$j] ^= (($roundKey[$j]>>16) & 0xFF);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   294
      $state[3][$j] ^= (($roundKey[$j]>>24) & 0xFF);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   295
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   296
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   297
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   298
  // This function creates the expanded key from the input (128/192/256-bit)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   299
  // key. The parameter key is an array of bytes holding the value of the key.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   300
  // The returned value is an array whose elements are the 32-bit words that 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   301
  // make up the expanded key.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   302
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   303
  function keyExpansion($key) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   304
    //global $this->keySizeInBits, $this->blockSizeInBits, $this->roundsArray, $this->Nk, $this->Nb, $this->Nr, $this->Nk, $this->SBox, $this->Rcon;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   305
    $expandedKey = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   306
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   307
    // in case the key size or parameters were changed...
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   308
    $this->Nk = $this->keySizeInBits / 32;                   
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   309
    $this->Nb = $this->blockSizeInBits / 32;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   310
    $this->Nr = $this->roundsArray[$this->Nk][$this->Nb];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   311
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   312
    for ($j=0; $j < $this->Nk; $j++)     // Fill in input key first
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   313
      $expandedKey[$j] = 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   314
        ($key[4*$j]) | ($key[4*$j+1]<<8) | ($key[4*$j+2]<<16) | ($key[4*$j+3]<<24);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   315
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   316
    // Now walk down the rest of the array filling in expanded key bytes as
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   317
    // per Rijndael's spec
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   318
    for ($j = $this->Nk; $j < $this->Nb * ($this->Nr + 1); $j++) {    // For each word of expanded key
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   319
      $temp = $expandedKey[$j - 1];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   320
      if ($j % $this->Nk == 0) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   321
        $temp = ( ($this->SBox[($temp>>8) & 0xFF]) |
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   322
                  ($this->SBox[($temp>>16) & 0xFF]<<8) |
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   323
                  ($this->SBox[($temp>>24) & 0xFF]<<16) |
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   324
                  ($this->SBox[$temp & 0xFF]<<24) ) ^ $this->Rcon[floor($j / $this->Nk) - 1];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   325
      elseif  ($this->Nk > 6 && $j % $this->Nk == 4)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   326
        $temp = ($this->SBox[($temp>>24) & 0xFF]<<24) |
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   327
               ($this->SBox[($temp>>16) & 0xFF]<<16) |
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   328
               ($this->SBox[($temp>>8) & 0xFF]<<8) |
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   329
               ($this->SBox[ $temp & 0xFF]);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   330
      $expandedKey[$j] = $expandedKey[$j-$this->Nk] ^ $temp;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   331
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   332
    return $expandedKey;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   333
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   334
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   335
  // Rijndael's round functions... 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   336
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   337
  function RijndaelRound(&$state, $roundKey) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   338
    $this->byteSub($state, "encrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   339
    $this->shiftRow($state, "encrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   340
    $this->mixColumn($state, "encrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   341
    $this->addRoundKey($state, $roundKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   342
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   343
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   344
  function InverseRijndaelRound(&$state, $roundKey) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   345
    $this->addRoundKey($state, $roundKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   346
    $this->mixColumn($state, "decrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   347
    $this->shiftRow($state, "decrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   348
    $this->byteSub($state, "decrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   349
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   350
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   351
  function FinalRijndaelRound(&$state, $roundKey) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   352
    $this->byteSub($state, "encrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   353
    $this->shiftRow($state, "encrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   354
    $this->addRoundKey($state, $roundKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   355
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   356
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   357
  function InverseFinalRijndaelRound(&$state, $roundKey){
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   358
    $this->addRoundKey($state, $roundKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   359
    $this->shiftRow($state, "decrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   360
    $this->byteSub($state, "decrypt");  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   361
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   362
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   363
  // encrypt is the basic encryption function. It takes parameters
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   364
  // block, an array of bytes representing a plaintext block, and expandedKey,
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   365
  // an array of words representing the expanded key previously returned by
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   366
  // keyExpansion(). The ciphertext block is returned as an array of bytes.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   367
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   368
  function cryptBlock($block, $expandedKey) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   369
    //global $this->blockSizeInBits, $this->Nb, $this->Nr;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   370
    $t=count($block)*8;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   371
    if (!is_array($block) || count($block)*8 != $this->blockSizeInBits)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   372
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   373
      $this->trigger_error('block is bad or block size is wrong<pre>'.print_r($block, true).'</pre><p>Aiming for size '.$this->blockSizeInBits.', got '.$t.'.', E_USER_WARNING); 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   374
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   375
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   376
    if (!$expandedKey)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   377
      return;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   378
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   379
    $block = $this->packBytes($block);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   380
    $this->addRoundKey($block, $expandedKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   381
    for ($i=1; $i<$this->Nr; $i++) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   382
      $this->RijndaelRound($block, $this->array_slice_js_compat($expandedKey, $this->Nb*$i, $this->Nb*($i+1)));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   383
    $this->FinalRijndaelRound($block, $this->array_slice_js_compat($expandedKey, $this->Nb*$this->Nr));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   384
    $ret = $this->unpackBytes($block);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   385
    return $ret;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   386
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   387
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   388
  // decrypt is the basic decryption function. It takes parameters
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   389
  // block, an array of bytes representing a ciphertext block, and expandedKey,
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   390
  // an array of words representing the expanded key previously returned by
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   391
  // keyExpansion(). The decrypted block is returned as an array of bytes.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   392
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   393
  function unCryptBlock($block, $expandedKey) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   394
    $t = count($block)*8;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   395
    if (!is_array($block) || count($block)*8 != $this->blockSizeInBits)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   396
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   397
      $this->trigger_error('$block is not a valid rijndael-block array: '.$this->byteArrayToHex($block).'<pre>'.print_r($block, true).'</pre><p>Block size is '.$t.', should be '.$this->blockSizeInBits.'</p>', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   398
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   399
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   400
    if (!$expandedKey)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   401
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   402
      $this->trigger_error('$expandedKey is invalid', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   403
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   404
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   405
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   406
    $block = $this->packBytes($block);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   407
    $this->InverseFinalRijndaelRound($block, $this->array_slice_js_compat($expandedKey, $this->Nb*$this->Nr)); 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   408
    for ($i = $this->Nr - 1; $i>0; $i--) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   409
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   410
      $this->InverseRijndaelRound($block, $this->array_slice_js_compat($expandedKey, $this->Nb*$i, $this->Nb*($i+1)));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   411
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   412
    $this->addRoundKey($block, $expandedKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   413
    $ret = $this->unpackBytes($block);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   414
    if(!is_array($ret))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   415
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   416
      $this->trigger_error('$ret is not an array', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   417
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   418
    return $ret;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   419
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   420
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   421
  // This method takes a byte array (byteArray) and converts it to a string by
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   422
  // applying String.fromCharCode() to each value and concatenating the result.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   423
  // The resulting string is returned. Note that this function SKIPS zero bytes
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   424
  // under the assumption that they are padding added in formatPlaintext().
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   425
  // Obviously, do not invoke this method on raw data that can contain zero
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   426
  // bytes. It is really only appropriate for printable ASCII/Latin-1 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   427
  // values. Roll your own function for more robust functionality :)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   428
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   429
  function byteArrayToString($byteArray) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   430
    $result = "";
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   431
    for($i=0; $i<count($byteArray); $i++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   432
      if ($byteArray[$i] != 0) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   433
        $result .= chr($byteArray[$i]);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   434
    return $result;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   435
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   436
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   437
  // This function takes an array of bytes (byteArray) and converts them
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   438
  // to a hexadecimal string. Array element 0 is found at the beginning of 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   439
  // the resulting string, high nibble first. Consecutive elements follow
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   440
  // similarly, for example [16, 255] --> "10ff". The function returns a 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   441
  // string.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   442
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   443
  /*
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   444
  function byteArrayToHex($byteArray) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   445
    $result = "";
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   446
    if (!$byteArray)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   447
      return;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   448
    for ($i=0; $i<count($byteArray); $i++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   449
      $result .= (($byteArray[$i]<16) ? "0" : "") + toString($byteArray[$i]); // magic number here is 16, not sure how to handle this...
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   450
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   451
    return $result;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   452
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   453
  */
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   454
  function byteArrayToHex($arr)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   455
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   456
    $ret = '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   457
    foreach($arr as $a)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   458
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   459
      $nibble = (string)dechex(intval($a));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   460
      if(strlen($nibble) == 1) $nibble = '0' . $nibble;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   461
      $ret .= $nibble;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   462
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   463
    return $ret;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   464
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   465
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   466
  // PHP equivalent of Javascript's toString()
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   467
  function toString($bool)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   468
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   469
    if(is_bool($bool))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   470
      return ($bool) ? 'true' : 'false';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   471
    elseif(is_array($bool))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   472
      return implode(',', $bool);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   473
    else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   474
      return (string)$bool;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   475
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   476
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   477
  // This function converts a string containing hexadecimal digits to an 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   478
  // array of bytes. The resulting byte array is filled in the order the
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   479
  // values occur in the string, for example "10FF" --> [16, 255]. This
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   480
  // function returns an array. 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   481
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   482
  /*
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   483
  function hexToByteArray($hexString) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   484
    $byteArray = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   485
    if (strlen($hexString) % 2)             // must have even length
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   486
      return;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   487
    if (strstr($hexString, "0x") == $hexString || strstr($hexString, "0X") == $hexString)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   488
      $hexString = substr($hexString, 2);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   489
    for ($i = 0; $i<strlen($hexString); $i++,$i++) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   490
      $byteArray[floor($i/2)] = intval(substr($hexString, $i, 2)); // again, that strange magic number: 16
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   491
    return $byteArray;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   492
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   493
  */
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   494
  function hexToByteArray($str)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   495
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   496
    if(substr($str, 0, 2) == '0x' || substr($str, 0, 2) == '0X')
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   497
      $str = substr($str, 2);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   498
    $arr = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   499
    $str = $this->enano_str_split($str, 2);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   500
    foreach($str as $s)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   501
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   502
      $arr[] = intval(hexdec($s));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   503
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   504
    return $arr;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   505
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   506
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   507
  // This function packs an array of bytes into the four row form defined by
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   508
  // Rijndael. It assumes the length of the array of bytes is divisible by
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   509
  // four. Bytes are filled in according to the Rijndael spec (starting with
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   510
  // column 0, row 0 to 3). This function returns a 2d array.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   511
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   512
  function packBytes($octets) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   513
    $state = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   514
    if (!$octets || count($octets) % 4)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   515
      return;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   516
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   517
    $state[0] = Array(); $state[1] = Array(); 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   518
    $state[2] = Array(); $state[3] = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   519
    for ($j=0; $j<count($octets); $j = $j+4) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   520
       $state[0][$j/4] = $octets[$j];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   521
       $state[1][$j/4] = $octets[$j+1];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   522
       $state[2][$j/4] = $octets[$j+2];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   523
       $state[3][$j/4] = $octets[$j+3];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   524
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   525
    return $state;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   526
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   527
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   528
  // This function unpacks an array of bytes from the four row format preferred
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   529
  // by Rijndael into a single 1d array of bytes. It assumes the input "packed"
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   530
  // is a packed array. Bytes are filled in according to the Rijndael spec. 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   531
  // This function returns a 1d array of bytes.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   532
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   533
  function unpackBytes($packed) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   534
    $result = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   535
    for ($j=0; $j<count($packed[0]); $j++) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   536
      $result[] = $packed[0][$j];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   537
      $result[] = $packed[1][$j];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   538
      $result[] = $packed[2][$j];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   539
      $result[] = $packed[3][$j];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   540
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   541
    return $result;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   542
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   543
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   544
  function charCodeAt($str, $i)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   545
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   546
    return ord(substr($str, $i, 1));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   547
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   548
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   549
  function fromCharCode($str)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   550
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   551
    return chr($str);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   552
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   553
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   554
  // This function takes a prospective plaintext (string or array of bytes)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   555
  // and pads it with zero bytes if its length is not a multiple of the block 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   556
  // size. If plaintext is a string, it is converted to an array of bytes
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   557
  // in the process. The type checking can be made much nicer using the 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   558
  // instanceof operator, but this operator is not available until IE5.0 so I 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   559
  // chose to use the heuristic below. 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   560
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   561
  function formatPlaintext($plaintext) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   562
    //global $this->blockSizeInBits;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   563
    $bpb = $this->blockSizeInBits / 8;               // bytes per block
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   564
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   565
    // if primitive string or String instance
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   566
    if (is_string($plaintext)) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   567
      $plaintext = $this->enano_str_split($plaintext);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   568
      // Unicode issues here (ignoring high byte)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   569
      for ($i=0; $i<sizeof($plaintext); $i++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   570
        $plaintext[$i] = $this->charCodeAt($plaintext[$i], 0) & 0xFF;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   571
    } 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   572
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   573
    for ($i = $bpb - (sizeof($plaintext) % $bpb); $i > 0 && $i < $bpb; $i--) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   574
      $plaintext[] = 0;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   575
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   576
    return $plaintext;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   577
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   578
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   579
  // Returns an array containing "howMany" random bytes. YOU SHOULD CHANGE THIS
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   580
  // TO RETURN HIGHER QUALITY RANDOM BYTES IF YOU ARE USING THIS FOR A "REAL"
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   581
  // APPLICATION. (edit: done, mt_rand() is relatively secure)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   582
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   583
  function getRandomBytes($howMany) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   584
    $bytes = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   585
    for ($i=0; $i<$howMany; $i++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   586
      $bytes[$i] = mt_rand(0, 255);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   587
    return $bytes;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   588
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   589
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   590
  // rijndaelEncrypt(plaintext, key, mode)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   591
  // Encrypts the plaintext using the given key and in the given mode. 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   592
  // The parameter "plaintext" can either be a string or an array of bytes. 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   593
  // The parameter "key" must be an array of key bytes. If you have a hex 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   594
  // string representing the key, invoke hexToByteArray() on it to convert it 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   595
  // to an array of bytes. The third parameter "mode" is a string indicating
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   596
  // the encryption mode to use, either "ECB" or "CBC". If the parameter is
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   597
  // omitted, ECB is assumed.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   598
  // 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   599
  // An array of bytes representing the cihpertext is returned. To convert 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   600
  // this array to hex, invoke byteArrayToHex() on it. If you are using this 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   601
  // "for real" it is a good idea to change the function getRandomBytes() to 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   602
  // something that returns truly random bits.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   603
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   604
  function rijndaelEncrypt($plaintext, $key, $mode = 'ECB') {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   605
    //global $this->blockSizeInBits, $this->keySizeInBits;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   606
    $bpb = $this->blockSizeInBits / 8;          // bytes per block
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   607
    // var ct;                                 // ciphertext
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   608
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   609
    if($mode == 'CBC')
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   610
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   611
      if (!is_string($plaintext) || !is_array($key))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   612
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   613
        $this->trigger_error('In CBC mode the first and second parameters should be strings', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   614
        return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   615
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   616
    } else {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   617
      if (!is_array($plaintext) || !is_array($key))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   618
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   619
        $this->trigger_error('In ECB mode the first and second parameters should be byte arrays', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   620
        return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   621
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   622
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   623
    if (sizeof($key)*8 != $this->keySizeInBits)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   624
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   625
      $this->trigger_error('The key needs to be '. ( $this->keySizeInBits / 8 ) .' bytes in length', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   626
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   627
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   628
    if ($mode == "CBC")
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   629
      $ct = $this->getRandomBytes($bpb);             // get IV
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   630
    else {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   631
      $mode = "ECB";
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   632
      $ct = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   633
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   634
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   635
    // convert plaintext to byte array and pad with zeros if necessary. 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   636
    $plaintext = $this->formatPlaintext($plaintext);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   637
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   638
    $expandedKey = $this->keyExpansion($key);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   639
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   640
    for ($block=0; $block<sizeof($plaintext) / $bpb; $block++) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   641
      $aBlock = $this->array_slice_js_compat($plaintext, $block*$bpb, ($block+1)*$bpb);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   642
      if ($mode == "CBC")
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   643
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   644
        for ($i=0; $i<$bpb; $i++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   645
        {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   646
          $aBlock[$i] ^= $ct[$block*$bpb + $i];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   647
        }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   648
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   649
      $cp = $this->cryptBlock($aBlock, $expandedKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   650
      $ct = $this->concat($ct, $cp);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   651
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   652
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   653
    return $ct;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   654
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   655
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   656
  // rijndaelDecrypt(ciphertext, key, mode)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   657
  // Decrypts the using the given key and mode. The parameter "ciphertext" 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   658
  // must be an array of bytes. The parameter "key" must be an array of key 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   659
  // bytes. If you have a hex string representing the ciphertext or key, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   660
  // invoke hexToByteArray() on it to convert it to an array of bytes. The
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   661
  // parameter "mode" is a string, either "CBC" or "ECB".
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   662
  // 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   663
  // An array of bytes representing the plaintext is returned. To convert 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   664
  // this array to a hex string, invoke byteArrayToHex() on it. To convert it 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   665
  // to a string of characters, you can use byteArrayToString().
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   666
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   667
  function rijndaelDecrypt($ciphertext, $key, $mode = 'ECB') {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   668
    //global $this->blockSizeInBits, $this->keySizeInBits;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   669
    $bpb = $this->blockSizeInBits / 8;          // bytes per block
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   670
    $pt = Array();                   // plaintext array
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   671
    // $aBlock;                             // a decrypted block
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   672
    // $block;                              // current block number
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   673
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   674
    if (!$ciphertext)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   675
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   676
      $this->trigger_error('$ciphertext should be a byte array', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   677
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   678
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   679
    if(  !is_array($key) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   680
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   681
      $this->trigger_error('$key should be a byte array', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   682
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   683
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   684
    if( is_string($ciphertext) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   685
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   686
      $this->trigger_error('$ciphertext should be a byte array', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   687
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   688
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   689
    if (sizeof($key)*8 != $this->keySizeInBits)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   690
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   691
      $this->trigger_error('Encryption key is the wrong length', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   692
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   693
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   694
    if (!$mode)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   695
      $mode = "ECB";                         // assume ECB if mode omitted
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   696
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   697
    $expandedKey = $this->keyExpansion($key);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   698
   
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   699
    // work backwards to accomodate CBC mode 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   700
    for ($block=(sizeof($ciphertext) / $bpb)-1; $block>0; $block--)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   701
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   702
      if( ( $block*$bpb ) + ( ($block+1)*$bpb ) > count($ciphertext) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   703
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   704
        //$this->trigger_error('$ciphertext index out of bounds', E_USER_ERROR);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   705
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   706
      $current_block = $this->array_slice_js_compat($ciphertext, $block*$bpb, ($block+1)*$bpb);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   707
      if(count($current_block) * 8 != $this->blockSizeInBits)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   708
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   709
        // $c=count($current_block)*8;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   710
        // $this->trigger_error('We got a '.$c.'-bit block, instead of '.$this->blockSizeInBits.'', E_USER_ERROR);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   711
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   712
      $aBlock = $this->uncryptBlock($current_block, $expandedKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   713
      if(!$aBlock)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   714
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   715
        $this->trigger_error('Shared block decryption routine returned false', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   716
        return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   717
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   718
      if ($mode == "CBC")
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   719
        for ($i=0; $i<$bpb; $i++) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   720
          $pt[($block-1)*$bpb + $i] = $aBlock[$i] ^ $ciphertext[($block-1)*$bpb + $i];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   721
      else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   722
        $pt = $this->concat($aBlock, $pt);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   723
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   724
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   725
    // do last block if ECB (skips the IV in CBC)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   726
    if ($mode == "ECB")
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   727
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   728
      $x = $this->uncryptBlock($this->array_slice_js_compat($ciphertext, 0, $bpb), $expandedKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   729
      if(!$x)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   730
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   731
        $this->trigger_error('ECB block decryption routine returned false', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   732
        return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   733
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   734
      $pt = $this->concat($x, $pt);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   735
      if(!$pt)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   736
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   737
        $this->trigger_error('ECB concatenation routine returned false', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   738
        return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   739
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   740
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   741
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   742
    return $pt;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   743
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   744
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   745
  /**
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   746
   * Wrapper for encryption.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   747
   * @param string $text the text to encrypt
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   748
   * @param string $key the raw binary key to encrypt with
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   749
   * @param int $return_encoding optional - can be ENC_BINARY, ENC_HEX or ENC_BASE64
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   750
   */
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   751
   
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   752
  function encrypt($text, $key, $return_encoding = ENC_HEX)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   753
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   754
    if ( $text == '' )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   755
      return '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   756
    if ( $this->mcrypt && $this->blockSizeInBits == mcrypt_module_get_algo_block_size(eval('return MCRYPT_RIJNDAEL_'.$this->keySizeInBits.';')) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   757
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   758
      $iv_size = mcrypt_get_iv_size($this->mcrypt, MCRYPT_MODE_ECB);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   759
      $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   760
      $cryptext = mcrypt_encrypt($this->mcrypt, $key, $text, MCRYPT_MODE_ECB, $iv);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   761
      switch($return_encoding)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   762
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   763
        case ENC_HEX:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   764
        default:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   765
          $cryptext = $this->strtohex($cryptext);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   766
          break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   767
        case ENC_BINARY:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   768
          $cryptext = $cryptext;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   769
          break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   770
        case ENC_BASE64:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   771
          $cryptext = base64_encode($cryptext);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   772
          break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   773
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   774
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   775
    else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   776
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   777
      $key = $this->prepare_string($key);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   778
      $text = $this->prepare_string($text);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   779
      profiler_log('AES: Started encryption of a string');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   780
      $cryptext = $this->rijndaelEncrypt($text, $key, 'ECB');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   781
      profiler_log('AES: Finished encryption of a string');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   782
      if(!is_array($cryptext))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   783
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   784
        echo 'Warning: encryption failed for string: '.print_r($text,true).'<br />';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   785
        return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   786
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   787
      switch($return_encoding)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   788
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   789
        case ENC_HEX:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   790
        default:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   791
          $cryptext = $this->byteArrayToHex($cryptext);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   792
          break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   793
        case ENC_BINARY:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   794
          $cryptext = $this->byteArrayToString($cryptext);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   795
          break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   796
        case ENC_BASE64:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   797
          $cryptext = base64_encode($this->byteArrayToString($cryptext));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   798
          break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   799
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   800
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   801
    return $cryptext;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   802
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   803
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   804
  /**
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   805
   * Wrapper for decryption.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   806
   * @param string $text the encrypted text
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   807
   * @param string $key the raw binary key used to encrypt the text
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   808
   * @param int $input_encoding the encoding used for the encrypted string. Can be ENC_BINARY, ENC_HEX, or ENC_BASE64.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   809
   * @return string
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   810
   */
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   811
   
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   812
  function decrypt($text, $key, $input_encoding = ENC_HEX)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   813
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   814
    if ( $text == '' )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   815
      return '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   816
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   817
    switch($input_encoding)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   818
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   819
      case ENC_BINARY:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   820
      default:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   821
        break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   822
      case ENC_HEX:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   823
        $text = $this->hextostring($text);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   824
        break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   825
      case ENC_BASE64:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   826
        $text = base64_decode($text);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   827
        break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   828
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   829
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   830
    // Run memory-cache check
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   831
    if ( isset($this->decrypt_cache[$key]) && is_array($this->decrypt_cache[$key]) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   832
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   833
      if ( isset($this->decrypt_cache[$key][$text]) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   834
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   835
        return $this->decrypt_cache[$key][$text];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   836
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   837
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   838
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   839
    // Run disk-cache check
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   840
    $hash = sha1($text . '::' . $key);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   841
    if ( $dypt = aes_decrypt_cache_fetch($hash) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   842
      return $dypt;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   843
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   844
    $text_bin = $text;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   845
    $key_bin = $key;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   846
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   847
    if ( $this->mcrypt )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   848
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   849
      $iv_size = mcrypt_get_iv_size($this->mcrypt, MCRYPT_MODE_ECB);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   850
      $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   851
      $dypt = mcrypt_decrypt($this->mcrypt, $key, $text, MCRYPT_MODE_ECB, $iv);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   852
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   853
    else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   854
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   855
      $etext = $this->prepare_string($text);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   856
      $ekey  = $this->prepare_string($key);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   857
      $mod = count($etext) % $this->blockSizeInBits;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   858
      profiler_log('AES: Started decryption of a string');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   859
      $dypt = $this->rijndaelDecrypt($etext, $ekey, 'ECB');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   860
      profiler_log('AES: Finished decryption of a string');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   861
      if(!$dypt)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   862
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   863
        echo '<pre>'.print_r($dypt, true).'</pre>';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   864
        $this->trigger_error('Rijndael main decryption routine failed', E_USER_ERROR);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   865
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   866
      $dypt = $this->byteArrayToString($dypt);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   867
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   868
    if ( !isset($this->decrypt_cache[$key_bin]) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   869
      $this->decrypt_cache[$key_bin] = array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   870
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   871
    $this->decrypt_cache[$key_bin][$text_bin] = $dypt;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   872
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   873
    aes_decrypt_cache_store($text_bin, $dypt, $key_bin);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   874
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   875
    return $dypt;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   876
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   877
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   878
  /**
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   879
   * Enano-ese equivalent of str_split() which is only found in PHP5
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   880
   * @param $text string the text to split
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   881
   * @param $inc int size of each block
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   882
   * @return array
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   883
   */
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   884
   
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   885
  function enano_str_split($text, $inc = 1)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   886
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   887
    if($inc < 1) return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   888
    if($inc >= strlen($text)) return Array($text);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   889
    $len = ceil(strlen($text) / $inc);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   890
    $ret = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   891
    for($i=0;$i<strlen($text);$i=$i+$inc)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   892
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   893
      $ret[] = substr($text, $i, $inc);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   894
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   895
    return $ret;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   896
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   897
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   898
  /**
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   899
   * Generates a random key suitable for encryption
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   900
   * @param int $len the length of the key, in bytes
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   901
   * @return string a BINARY key
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   902
   */
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   903
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   904
  function randkey($len = 32)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   905
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   906
    $key = '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   907
    for($i=0;$i<$len;$i++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   908
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   909
      $key .= chr(mt_rand(0, 255));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   910
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   911
    if ( file_exists('/dev/urandom') && is_readable('/dev/urandom') )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   912
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   913
      // Let's use something a little more secure
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   914
      $ur = @fopen('/dev/urandom', 'r');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   915
      if ( !$ur )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   916
        return $key;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   917
      $ukey = @fread($ur, $len);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   918
      fclose($ur);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   919
      if ( strlen($ukey) != $len )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   920
        return $key;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   921
      return $ukey;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   922
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   923
    return $key;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   924
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   925
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   926
  /*
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   927
  function byteArrayToString($arr)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   928
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   929
    if(!is_array($arr))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   930
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   931
      $this->trigger_error('First parameter should be an array', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   932
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   933
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   934
    $ret = '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   935
    foreach($arr as $a)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   936
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   937
      if($a != 0) $ret .= chr($a);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   938
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   939
    return $ret;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   940
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   941
  */
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   942
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   943
  function strtohex($str)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   944
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   945
    $str = $this->enano_str_split($str);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   946
    $ret = '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   947
    foreach($str as $s)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   948
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   949
      $chr = dechex(ord($s));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   950
      if(strlen($chr) < 2) $chr = '0' . $chr;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   951
      $ret .= $chr;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   952
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   953
    return $ret;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   954
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   955
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   956
  function gen_readymade_key()
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   957
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   958
    $key = $this->strtohex($this->randkey($this->keySizeInBits / 8));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   959
    return $key;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   960
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   961
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   962
  function prepare_string($text)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   963
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   964
    $ret = $this->hexToByteArray($this->strtohex($text));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   965
    if(count($ret) != strlen($text))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   966
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   967
      die('Could not convert string "' . $text . '" to hex byte array for encryption');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   968
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   969
    return $ret;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   970
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   971
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   972
  /**
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   973
   * Decodes a hex string.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   974
   * @param string $hex The hex code to decode
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   975
   * @return string
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   976
   */
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   977
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   978
  function hextostring($hex)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   979
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   980
    $hex = $this->enano_str_split($hex, 2);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   981
    $bin_key = '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   982
    foreach($hex as $nibble)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   983
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   984
      $byte = chr(hexdec($nibble));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   985
      $bin_key .= $byte;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   986
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   987
    return $bin_key;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   988
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   989
}
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   990
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   991
function aes_decrypt_cache_store($encrypted, $decrypted, $key)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   992
{
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   993
  $cache_file = ENANO_ROOT . '/cache/aes_decrypt.php';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   994
  // only cache if $decrypted is long enough to actually warrant caching
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   995
  if ( strlen($decrypted) < 32 )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   996
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   997
    profiler_log("AES: Skipped caching a string (probably a password, we dunno) because it's too short");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   998
    return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   999
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1000
  if ( file_exists($cache_file) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1001
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1002
    require_once($cache_file);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1003
    global $aes_decrypt_cache;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1004
    $cachekey = sha1($encrypted . '::' . $key);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1005
    $aes_decrypt_cache[$cachekey] = $decrypted;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1006
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1007
    if ( count($aes_decrypt_cache) > 5000 )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1008
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1009
      // we've got a lot of strings in the cache, clear out a few
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1010
      $keys = array_keys($aes_decrypt_cache);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1011
      for ( $i = 0; $i < 2500; $i++ )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1012
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1013
        unset($aes_decrypt_cache[$keys[$i]]);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1014
        unset($aes_decrypt_cache[$keys[$i]]);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1015
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1016
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1017
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1018
  else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1019
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1020
    $aes_decrypt_cache = array(
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1021
      sha1($encrypted . '::' . $key) => $decrypted
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1022
    );
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1023
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1024
  // call var_export and collect contents
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1025
  ob_start();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1026
  var_export($aes_decrypt_cache);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1027
  $dec_cache_string = ob_get_contents();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1028
  ob_end_clean();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1029
  $f = @fopen($cache_file, 'w');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1030
  if ( !$f )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1031
    return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1032
  fwrite($f, "<?php
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1033
\$GLOBALS['aes_decrypt_cache'] = $dec_cache_string;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1034
");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1035
  fclose($f);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1036
  return true;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1037
}
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1038
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1039
function aes_decrypt_cache_fetch($hash)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1040
{
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1041
  $cache_file = ENANO_ROOT . '/cache/aes_decrypt.php';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1042
  if ( !file_exists($cache_file) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1043
    return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1044
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1045
  require_once($cache_file);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1046
  global $aes_decrypt_cache;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1047
  if ( isset($aes_decrypt_cache[$hash]) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1048
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1049
    profiler_log("AES: Loaded cached decrypted string, hash is $hash");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1050
    return $aes_decrypt_cache[$hash];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1051
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1052
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1053
  return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1054
}
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1055
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1056
function aes_decrypt_cache_destroy($hash)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1057
{
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1058
  $cache_file = ENANO_ROOT . '/cache/aes_decrypt.php';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1059
  if ( !file_exists($cache_file) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1060
    return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1061
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1062
  require_once($cache_file);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1063
  global $aes_decrypt_cache;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1064
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1065
  if ( isset($aes_decrypt_cache[$hash]) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1066
    unset($aes_decrypt_cache[$hash]);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1067
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1068
  // call var_export and collect contents
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1069
  ob_start();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1070
  var_export($aes_decrypt_cache);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1071
  $dec_cache_string = ob_get_contents();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1072
  ob_end_clean();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1073
  $f = @fopen($cache_file, 'w');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1074
  if ( !$f )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1075
    return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1076
  fwrite($f, "<?php
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1077
\$GLOBALS['aes_decrypt_cache'] = $dec_cache_string;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1078
");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1079
  fclose($f);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1080
  return true;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1081
}
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1082
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1083
?>