includes/rijndael.php
author Dan
Tue, 01 Apr 2008 01:26:22 -0400
changeset 517 c6118b9e13bd
parent 472 bc4b58034f4d
child 518 2b826f2640e9
permissions -rw-r--r--
Fixed bad captcha refresh code on registration page
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
461
717e71109645 Fixed a number of IE6 bugs
Dan
parents: 458
diff changeset
    13
define ('ENC_HEX', 201);
458
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    14
define ('ENC_BASE64', 202);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    15
define ('ENC_BINARY', 203);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    16
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    17
$_aes_objcache = array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    18
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    19
class AESCrypt {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    20
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    21
  var $debug = false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    22
  var $mcrypt = false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    23
  var $decrypt_cache = array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    24
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    25
  // Rijndael parameters --  Valid values are 128, 192, or 256
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    26
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    27
  var $keySizeInBits = 128;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    28
  var $blockSizeInBits = 128;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    29
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    30
  ///////  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
    31
  ///////  the function getRandomBytes().
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    32
  //
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    33
  // 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
    34
  //       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
    35
  //       are 2d arrays of the form state[4][Nb].
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    38
  // 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
    39
  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
    40
                               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
    41
                               Array(0,0,0,0,14,0, 14,0, 14) );
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    42
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    43
  // 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
    44
  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
    45
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    46
  // The round constants used in subkey expansion
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    47
  var $Rcon = Array( 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    48
  0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    49
  0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    50
  0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    51
  0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    52
  0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91 );
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    53
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    54
  // Precomputed lookup table for the SBox
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    55
  var $SBox = Array(
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    56
   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
    57
  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
    58
  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
    59
  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
    60
  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
    61
  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
    62
  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
    63
  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
    64
  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
    65
  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
    66
  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
    67
    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
    68
  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
    69
   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
    70
  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
    71
  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
    72
  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
    73
   22 );
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    74
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    75
  // Precomputed lookup table for the inverse SBox
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    76
  var $SBoxInverse = Array(
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    77
   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
    78
  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
    79
  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
    80
  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
    81
  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
    82
  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
    83
   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
    84
  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
    85
  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
    86
  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
    87
   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
    88
   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
    89
  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
    90
   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
    91
  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
    92
  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
    93
   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
    94
  125 );
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    95
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    96
  function __construct($ks = 128, $bs = 128, $debug = false)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    97
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    98
    $this->keySizeInBits = $ks;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
    99
    $this->blockSizeInBits = $bs;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   100
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   101
    // Use the Mcrypt library? This speeds things up dramatically.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   102
    if(defined('MCRYPT_RIJNDAEL_' . $ks) && defined('MCRYPT_ACCEL'))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   103
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   104
      eval('$mcb = MCRYPT_RIJNDAEL_' . $ks.';');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   105
      $bks = mcrypt_module_get_algo_block_size($mcb);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   106
      $bks = $bks * 8;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   107
      if ( $bks != $bs )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   108
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   109
        $mcb = false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   110
        echo (string)$bks;
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
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   113
    else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   114
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   115
      $mcb = false;
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
      
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   118
    $this->mcrypt = $mcb;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   119
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   120
    // Cipher parameters ... do not change these
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   121
    $this->Nk = $this->keySizeInBits / 32;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   122
    $this->Nb = $this->blockSizeInBits / 32;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   123
    $this->Nr = $this->roundsArray[$this->Nk][$this->Nb];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   124
    $this->debug = $debug;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   127
  public static function singleton($key_size, $block_size)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   128
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   129
    static $_aes_objcache;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   130
    if ( isset($_aes_objcache["$key_size,$block_size"]) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   131
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   132
      return $_aes_objcache["$key_size,$block_size"];
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
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   135
    $_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
   136
    return $_aes_objcache["$key_size,$block_size"];
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   139
  // Error handler
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   140
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   141
  function trigger_error($text, $level = E_USER_NOTICE)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   142
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   143
    $bt = debug_backtrace();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   144
    $lastfunc =& $bt[1];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   145
    switch($level)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   146
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   147
      case E_USER_NOTICE:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   148
      default:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   149
        $desc = 'Notice';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   150
        break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   151
      case E_USER_WARNING:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   152
        $desc = 'Warning';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   153
        break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   154
      case E_USER_ERROR:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   155
        $desc = 'Fatal';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   156
        break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   157
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   158
    ob_start();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   159
    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
   160
    if($this->debug)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   161
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   162
      //echo '<pre>'.enano_debug_print_backtrace(true).'</pre>';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   163
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   164
    ob_end_flush();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   165
    if($level == E_USER_ERROR)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   166
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   167
      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
   168
      exit;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   172
  function array_slice_js_compat($array, $start, $finish = 0)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   173
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   174
    $len = $finish - $start;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   175
    if($len < 0) $len = 0 - $len;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   176
    //if($this->debug) echo (string)$len . ' ';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   177
    //if(count($array) < $start + $len)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   178
    //  $this->trigger_error('Index out of range', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   179
    return array_slice($array, $start, $len);
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   182
  function concat($s1, $s2)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   183
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   184
    if(is_array($s1) && is_array($s2))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   185
      return array_merge($s1, $s2);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   186
    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
   187
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   188
      $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
   189
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   190
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   191
    else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   192
      return $s1 . $s2;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   195
  // 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
   196
  // 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
   197
  // 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
   198
  // 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
   199
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   200
  function cyclicShiftLeft($theArray, $positions) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   201
    if(!is_int($positions))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   202
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   203
      $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
   204
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   205
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   206
    $second = array_slice($theArray, 0, $positions);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   207
    $first = array_slice($theArray, $positions);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   208
    $theArray = array_merge($first, $second);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   209
    return $theArray;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   212
  // 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
   213
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   214
  function xtime($poly) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   215
    $poly <<= 1;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   216
    return (($poly & 0x100) ? ($poly ^ 0x11B) : ($poly));
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   219
  // 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
   220
  // 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
   221
  // 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
   222
  // 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
   223
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   224
  function mult_GF256($x, $y) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   225
    $result = 0;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   226
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   227
    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
   228
      if ($x & $bit) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   229
        $result ^= $y;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   230
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   231
    return $result;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   234
  // 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
   235
  // state information (see spec) and direction is string indicating whether
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   236
  // we are performing the forward substitution ("encrypt") or inverse 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   237
  // substitution (anything else)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   238
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   239
  function byteSub(&$state, $direction) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   240
    //global $this->SBox, $this->SBoxInverse, $this->Nb;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   241
    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
   242
      $S =& $this->SBox;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   243
    else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   244
      $S =& $this->SBoxInverse;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   245
    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
   246
      for ($j = 0; $j < $this->Nb; $j++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   247
         $state[$i][$j] = $S[$state[$i][$j]];
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   250
  // Performs the row shifting step of the cipher.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   251
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   252
  function shiftRow(&$state, $direction) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   253
    //global $this->Nb, $this->shiftOffsets;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   254
    for ($i=1; $i<4; $i++)               // Row 0 never shifts
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   255
      if ($direction == "encrypt")
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   256
         $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
   257
      else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   258
         $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
   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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   262
  // 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
   263
  // 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
   264
  // to greatly increase the speed. 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   265
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   266
  function mixColumn(&$state, $direction) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   267
    //global $this->Nb;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   268
    $b = Array();                                  // Result of matrix multiplications
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   269
    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
   270
      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
   271
        if ($direction == "encrypt")
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   272
          $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
   273
                   $this->mult_GF256($state[($i+1)%4][$j], 3) ^ 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   274
                   $state[($i+2)%4][$j] ^ 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   275
                   $state[($i+3)%4][$j];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   276
        else 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   277
          $b[$i] = $this->mult_GF256($state[$i][$j], 0xE) ^ 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   278
                   $this->mult_GF256($state[($i+1)%4][$j], 0xB) ^
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   279
                   $this->mult_GF256($state[($i+2)%4][$j], 0xD) ^
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   280
                   $this->mult_GF256($state[($i+3)%4][$j], 9);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   281
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   282
      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
   283
        $state[$i][$j] = $b[$i];
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   287
  // Adds the current round key to the state information. Straightforward.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   288
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   289
  function addRoundKey(&$state, $roundKey) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   290
    //global $this->Nb;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   291
    for ($j = 0; $j < $this->Nb; $j++) {                      // Step through columns...
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   292
      $state[0][$j] ^= ( $roundKey[$j] & 0xFF);         // and XOR
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   293
      $state[1][$j] ^= (($roundKey[$j]>>8) & 0xFF);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   294
      $state[2][$j] ^= (($roundKey[$j]>>16) & 0xFF);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   295
      $state[3][$j] ^= (($roundKey[$j]>>24) & 0xFF);
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   299
  // 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
   300
  // 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
   301
  // 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
   302
  // make up the expanded key.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   303
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   304
  function keyExpansion($key) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   305
    //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
   306
    $expandedKey = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   307
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   308
    // in case the key size or parameters were changed...
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   309
    $this->Nk = $this->keySizeInBits / 32;                   
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   310
    $this->Nb = $this->blockSizeInBits / 32;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   311
    $this->Nr = $this->roundsArray[$this->Nk][$this->Nb];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   312
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   313
    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
   314
      $expandedKey[$j] = 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   315
        ($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
   316
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   317
    // 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
   318
    // per Rijndael's spec
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   319
    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
   320
      $temp = $expandedKey[$j - 1];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   321
      if ($j % $this->Nk == 0) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   322
        $temp = ( ($this->SBox[($temp>>8) & 0xFF]) |
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   323
                  ($this->SBox[($temp>>16) & 0xFF]<<8) |
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   324
                  ($this->SBox[($temp>>24) & 0xFF]<<16) |
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   325
                  ($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
   326
      elseif  ($this->Nk > 6 && $j % $this->Nk == 4)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   327
        $temp = ($this->SBox[($temp>>24) & 0xFF]<<24) |
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   328
               ($this->SBox[($temp>>16) & 0xFF]<<16) |
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   329
               ($this->SBox[($temp>>8) & 0xFF]<<8) |
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   330
               ($this->SBox[ $temp & 0xFF]);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   331
      $expandedKey[$j] = $expandedKey[$j-$this->Nk] ^ $temp;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   332
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   333
    return $expandedKey;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   336
  // Rijndael's round functions... 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   337
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   338
  function RijndaelRound(&$state, $roundKey) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   339
    $this->byteSub($state, "encrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   340
    $this->shiftRow($state, "encrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   341
    $this->mixColumn($state, "encrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   342
    $this->addRoundKey($state, $roundKey);
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   345
  function InverseRijndaelRound(&$state, $roundKey) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   346
    $this->addRoundKey($state, $roundKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   347
    $this->mixColumn($state, "decrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   348
    $this->shiftRow($state, "decrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   349
    $this->byteSub($state, "decrypt");
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   352
  function FinalRijndaelRound(&$state, $roundKey) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   353
    $this->byteSub($state, "encrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   354
    $this->shiftRow($state, "encrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   355
    $this->addRoundKey($state, $roundKey);
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   358
  function InverseFinalRijndaelRound(&$state, $roundKey){
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   359
    $this->addRoundKey($state, $roundKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   360
    $this->shiftRow($state, "decrypt");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   361
    $this->byteSub($state, "decrypt");  
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   364
  // encrypt is the basic encryption function. It takes parameters
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   365
  // 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
   366
  // 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
   367
  // 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
   368
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   369
  function cryptBlock($block, $expandedKey) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   370
    //global $this->blockSizeInBits, $this->Nb, $this->Nr;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   371
    $t=count($block)*8;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   372
    if (!is_array($block) || count($block)*8 != $this->blockSizeInBits)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   373
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   374
      $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
   375
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   376
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   377
    if (!$expandedKey)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   378
      return;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   379
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   380
    $block = $this->packBytes($block);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   381
    $this->addRoundKey($block, $expandedKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   382
    for ($i=1; $i<$this->Nr; $i++) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   383
      $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
   384
    $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
   385
    $ret = $this->unpackBytes($block);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   386
    return $ret;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   389
  // decrypt is the basic decryption function. It takes parameters
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   390
  // 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
   391
  // 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
   392
  // 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
   393
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   394
  function unCryptBlock($block, $expandedKey) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   395
    $t = count($block)*8;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   396
    if (!is_array($block) || count($block)*8 != $this->blockSizeInBits)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   397
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   398
      $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
   399
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   400
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   401
    if (!$expandedKey)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   402
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   403
      $this->trigger_error('$expandedKey is invalid', E_USER_WARNING);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   404
      return false;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   407
    $block = $this->packBytes($block);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   408
    $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
   409
    for ($i = $this->Nr - 1; $i>0; $i--) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   410
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   411
      $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
   412
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   413
    $this->addRoundKey($block, $expandedKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   414
    $ret = $this->unpackBytes($block);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   415
    if(!is_array($ret))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   416
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   417
      $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
   418
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   419
    return $ret;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   422
  // 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
   423
  // applying String.fromCharCode() to each value and concatenating the result.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   424
  // 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
   425
  // under the assumption that they are padding added in formatPlaintext().
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   426
  // 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
   427
  // 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
   428
  // values. Roll your own function for more robust functionality :)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   429
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   430
  function byteArrayToString($byteArray) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   431
    $result = "";
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   432
    for($i=0; $i<count($byteArray); $i++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   433
      if ($byteArray[$i] != 0) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   434
        $result .= chr($byteArray[$i]);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   435
    return $result;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   438
  // 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
   439
  // 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
   440
  // the resulting string, high nibble first. Consecutive elements follow
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   441
  // similarly, for example [16, 255] --> "10ff". The function returns a 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   442
  // string.
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
  /*
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   445
  function byteArrayToHex($byteArray) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   446
    $result = "";
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   447
    if (!$byteArray)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   448
      return;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   449
    for ($i=0; $i<count($byteArray); $i++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   450
      $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
   451
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   452
    return $result;
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
  */
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   455
  function byteArrayToHex($arr)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   456
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   457
    $ret = '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   458
    foreach($arr as $a)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   459
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   460
      $nibble = (string)dechex(intval($a));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   461
      if(strlen($nibble) == 1) $nibble = '0' . $nibble;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   462
      $ret .= $nibble;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   463
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   464
    return $ret;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   467
  // PHP equivalent of Javascript's toString()
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   468
  function toString($bool)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   469
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   470
    if(is_bool($bool))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   471
      return ($bool) ? 'true' : 'false';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   472
    elseif(is_array($bool))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   473
      return implode(',', $bool);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   474
    else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   475
      return (string)$bool;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   478
  // This function converts a string containing hexadecimal digits to an 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   479
  // 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
   480
  // 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
   481
  // function returns an array. 
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
  /*
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   484
  function hexToByteArray($hexString) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   485
    $byteArray = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   486
    if (strlen($hexString) % 2)             // must have even length
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   487
      return;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   488
    if (strstr($hexString, "0x") == $hexString || strstr($hexString, "0X") == $hexString)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   489
      $hexString = substr($hexString, 2);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   490
    for ($i = 0; $i<strlen($hexString); $i++,$i++) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   491
      $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
   492
    return $byteArray;
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
  */
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   495
  function hexToByteArray($str)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   496
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   497
    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
   498
      $str = substr($str, 2);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   499
    $arr = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   500
    $str = $this->enano_str_split($str, 2);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   501
    foreach($str as $s)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   502
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   503
      $arr[] = intval(hexdec($s));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   504
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   505
    return $arr;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   508
  // 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
   509
  // 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
   510
  // 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
   511
  // 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
   512
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   513
  function packBytes($octets) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   514
    $state = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   515
    if (!$octets || count($octets) % 4)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   516
      return;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   517
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   518
    $state[0] = Array(); $state[1] = Array(); 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   519
    $state[2] = Array(); $state[3] = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   520
    for ($j=0; $j<count($octets); $j = $j+4) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   521
       $state[0][$j/4] = $octets[$j];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   522
       $state[1][$j/4] = $octets[$j+1];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   523
       $state[2][$j/4] = $octets[$j+2];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   524
       $state[3][$j/4] = $octets[$j+3];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   525
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   526
    return $state;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   529
  // 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
   530
  // 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
   531
  // 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
   532
  // This function returns a 1d array of bytes.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   533
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   534
  function unpackBytes($packed) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   535
    $result = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   536
    for ($j=0; $j<count($packed[0]); $j++) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   537
      $result[] = $packed[0][$j];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   538
      $result[] = $packed[1][$j];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   539
      $result[] = $packed[2][$j];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   540
      $result[] = $packed[3][$j];
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   541
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   542
    return $result;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   545
  function charCodeAt($str, $i)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   546
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   547
    return ord(substr($str, $i, 1));
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   550
  function fromCharCode($str)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   551
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   552
    return chr($str);
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   555
  // 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
   556
  // 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
   557
  // 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
   558
  // 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
   559
  // 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
   560
  // chose to use the heuristic below. 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   561
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   562
  function formatPlaintext($plaintext) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   563
    //global $this->blockSizeInBits;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   564
    $bpb = $this->blockSizeInBits / 8;               // bytes per block
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   565
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   566
    // if primitive string or String instance
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   567
    if (is_string($plaintext)) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   568
      $plaintext = $this->enano_str_split($plaintext);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   569
      // Unicode issues here (ignoring high byte)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   570
      for ($i=0; $i<sizeof($plaintext); $i++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   571
        $plaintext[$i] = $this->charCodeAt($plaintext[$i], 0) & 0xFF;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   574
    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
   575
      $plaintext[] = 0;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   576
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   577
    return $plaintext;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   580
  // 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
   581
  // 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
   582
  // APPLICATION. (edit: done, mt_rand() is relatively secure)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   583
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   584
  function getRandomBytes($howMany) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   585
    $bytes = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   586
    for ($i=0; $i<$howMany; $i++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   587
      $bytes[$i] = mt_rand(0, 255);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   588
    return $bytes;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   591
  // rijndaelEncrypt(plaintext, key, mode)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   592
  // 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
   593
  // 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
   594
  // 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
   595
  // 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
   596
  // 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
   597
  // 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
   598
  // omitted, ECB is assumed.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   599
  // 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   600
  // 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
   601
  // 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
   602
  // "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
   603
  // something that returns truly random bits.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   604
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   605
  function rijndaelEncrypt($plaintext, $key, $mode = 'ECB') {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   606
    //global $this->blockSizeInBits, $this->keySizeInBits;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   607
    $bpb = $this->blockSizeInBits / 8;          // bytes per block
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   608
    // var ct;                                 // ciphertext
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   609
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   610
    if($mode == 'CBC')
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   611
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   612
      if (!is_string($plaintext) || !is_array($key))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   613
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   614
        $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
   615
        return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   616
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   617
    } else {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   618
      if (!is_array($plaintext) || !is_array($key))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   619
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   620
        $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
   621
        return false;
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
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   624
    if (sizeof($key)*8 != $this->keySizeInBits)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   625
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   626
      $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
   627
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   628
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   629
    if ($mode == "CBC")
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   630
      $ct = $this->getRandomBytes($bpb);             // get IV
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   631
    else {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   632
      $mode = "ECB";
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   633
      $ct = Array();
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
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   636
    // 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
   637
    $plaintext = $this->formatPlaintext($plaintext);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   638
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   639
    $expandedKey = $this->keyExpansion($key);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   640
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   641
    for ($block=0; $block<sizeof($plaintext) / $bpb; $block++) {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   642
      $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
   643
      if ($mode == "CBC")
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   644
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   645
        for ($i=0; $i<$bpb; $i++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   646
        {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   647
          $aBlock[$i] ^= $ct[$block*$bpb + $i];
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
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   650
      $cp = $this->cryptBlock($aBlock, $expandedKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   651
      $ct = $this->concat($ct, $cp);
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   654
    return $ct;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   657
  // rijndaelDecrypt(ciphertext, key, mode)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   658
  // 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
   659
  // 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
   660
  // 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
   661
  // 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
   662
  // parameter "mode" is a string, either "CBC" or "ECB".
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   663
  // 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   664
  // 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
   665
  // 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
   666
  // to a string of characters, you can use byteArrayToString().
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   667
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   668
  function rijndaelDecrypt($ciphertext, $key, $mode = 'ECB') {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   669
    //global $this->blockSizeInBits, $this->keySizeInBits;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   670
    $bpb = $this->blockSizeInBits / 8;          // bytes per block
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   671
    $pt = Array();                   // plaintext array
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   672
    // $aBlock;                             // a decrypted block
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   673
    // $block;                              // current block number
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   674
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   675
    if (!$ciphertext)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   676
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   677
      $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
   678
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   679
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   680
    if(  !is_array($key) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   681
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   682
      $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
   683
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   684
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   685
    if( is_string($ciphertext) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   686
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   687
      $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
   688
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   689
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   690
    if (sizeof($key)*8 != $this->keySizeInBits)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   691
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   692
      $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
   693
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   694
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   695
    if (!$mode)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   696
      $mode = "ECB";                         // assume ECB if mode omitted
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   697
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   698
    $expandedKey = $this->keyExpansion($key);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   699
   
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   700
    // work backwards to accomodate CBC mode 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   701
    for ($block=(sizeof($ciphertext) / $bpb)-1; $block>0; $block--)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   702
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   703
      if( ( $block*$bpb ) + ( ($block+1)*$bpb ) > count($ciphertext) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   704
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   705
        //$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
   706
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   707
      $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
   708
      if(count($current_block) * 8 != $this->blockSizeInBits)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   709
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   710
        // $c=count($current_block)*8;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   711
        // $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
   712
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   713
      $aBlock = $this->uncryptBlock($current_block, $expandedKey);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   714
      if(!$aBlock)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   715
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   716
        $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
   717
        return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   718
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   719
      if ($mode == "CBC")
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   720
        for ($i=0; $i<$bpb; $i++) 
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   721
          $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
   722
      else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   723
        $pt = $this->concat($aBlock, $pt);
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   726
    // do last block if ECB (skips the IV in CBC)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   727
    if ($mode == "ECB")
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   728
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   729
      $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
   730
      if(!$x)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   731
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   732
        $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
   733
        return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   734
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   735
      $pt = $this->concat($x, $pt);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   736
      if(!$pt)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   737
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   738
        $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
   739
        return false;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   743
    return $pt;
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
  /**
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   747
   * Wrapper for encryption.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   748
   * @param string $text the text to encrypt
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   749
   * @param string $key the raw binary key to encrypt with
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   750
   * @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
   751
   */
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   752
   
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   753
  function encrypt($text, $key, $return_encoding = ENC_HEX)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   754
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   755
    if ( $text == '' )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   756
      return '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   757
    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
   758
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   759
      $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
   760
      $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   761
      $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
   762
      switch($return_encoding)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   763
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   764
        case ENC_HEX:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   765
        default:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   766
          $cryptext = $this->strtohex($cryptext);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   767
          break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   768
        case ENC_BINARY:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   769
          $cryptext = $cryptext;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   770
          break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   771
        case ENC_BASE64:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   772
          $cryptext = base64_encode($cryptext);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   773
          break;
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
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   776
    else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   777
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   778
      $key = $this->prepare_string($key);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   779
      $text = $this->prepare_string($text);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   780
      profiler_log('AES: Started encryption of a string');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   781
      $cryptext = $this->rijndaelEncrypt($text, $key, 'ECB');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   782
      profiler_log('AES: Finished encryption of a string');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   783
      if(!is_array($cryptext))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   784
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   785
        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
   786
        return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   787
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   788
      switch($return_encoding)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   789
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   790
        case ENC_HEX:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   791
        default:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   792
          $cryptext = $this->byteArrayToHex($cryptext);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   793
          break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   794
        case ENC_BINARY:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   795
          $cryptext = $this->byteArrayToString($cryptext);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   796
          break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   797
        case ENC_BASE64:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   798
          $cryptext = base64_encode($this->byteArrayToString($cryptext));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   799
          break;
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
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   802
    return $cryptext;
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
  /**
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   806
   * Wrapper for decryption.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   807
   * @param string $text the encrypted text
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   808
   * @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
   809
   * @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
   810
   * @return string
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
   
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   813
  function decrypt($text, $key, $input_encoding = ENC_HEX)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   814
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   815
    if ( $text == '' )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   816
      return '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   817
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   818
    switch($input_encoding)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   819
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   820
      case ENC_BINARY:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   821
      default:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   822
        break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   823
      case ENC_HEX:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   824
        $text = $this->hextostring($text);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   825
        break;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   826
      case ENC_BASE64:
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   827
        $text = base64_decode($text);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   828
        break;
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
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   831
    // Run memory-cache check
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   832
    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
   833
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   834
      if ( isset($this->decrypt_cache[$key][$text]) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   835
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   836
        return $this->decrypt_cache[$key][$text];
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
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   840
    // Run disk-cache check
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   841
    $hash = sha1($text . '::' . $key);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   842
    if ( $dypt = aes_decrypt_cache_fetch($hash) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   843
      return $dypt;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   844
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   845
    $text_bin = $text;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   846
    $key_bin = $key;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   847
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   848
    if ( $this->mcrypt )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   849
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   850
      $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
   851
      $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   852
      $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
   853
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   854
    else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   855
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   856
      $etext = $this->prepare_string($text);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   857
      $ekey  = $this->prepare_string($key);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   858
      $mod = count($etext) % $this->blockSizeInBits;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   859
      profiler_log('AES: Started decryption of a string');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   860
      $dypt = $this->rijndaelDecrypt($etext, $ekey, 'ECB');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   861
      profiler_log('AES: Finished decryption of a string');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   862
      if(!$dypt)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   863
      {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   864
        echo '<pre>'.print_r($dypt, true).'</pre>';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   865
        $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
   866
      }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   867
      $dypt = $this->byteArrayToString($dypt);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   868
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   869
    if ( !isset($this->decrypt_cache[$key_bin]) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   870
      $this->decrypt_cache[$key_bin] = array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   871
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   872
    $this->decrypt_cache[$key_bin][$text_bin] = $dypt;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   873
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   874
    aes_decrypt_cache_store($text_bin, $dypt, $key_bin);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   875
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   876
    return $dypt;
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
  /**
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   880
   * 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
   881
   * @param $text string the text to split
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   882
   * @param $inc int size of each block
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   883
   * @return array
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
   
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   886
  function enano_str_split($text, $inc = 1)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   887
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   888
    if($inc < 1) return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   889
    if($inc >= strlen($text)) return Array($text);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   890
    $len = ceil(strlen($text) / $inc);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   891
    $ret = Array();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   892
    for($i=0;$i<strlen($text);$i=$i+$inc)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   893
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   894
      $ret[] = substr($text, $i, $inc);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   895
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   896
    return $ret;
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
  /**
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   900
   * Generates a random key suitable for encryption
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   901
   * @param int $len the length of the key, in bytes
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   902
   * @return string a BINARY key
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   905
  function randkey($len = 32)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   906
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   907
    $key = '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   908
    for($i=0;$i<$len;$i++)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   909
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   910
      $key .= chr(mt_rand(0, 255));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   911
    }
472
bc4b58034f4d Implemented password reset (albeit hackishly) into the new login API; added dummy window.console object to hopefully reduce errors when Firebug isn't around; fixed the longstanding ACL dismiss/close button bug; fixed a couple undefined variables in mailer; fixed PHP error on attempted opening of /dev/(u)random in rijndael.php; clarified documentation for PageProcessor::update_page(); fixed some logic problems in theme ACL code; disabled CAPTCHA debug
Dan
parents: 461
diff changeset
   912
    if ( @file_exists('/dev/urandom') && @is_readable('/dev/urandom') )
458
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   913
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   914
      // Let's use something a little more secure
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   915
      $ur = @fopen('/dev/urandom', 'r');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   916
      if ( !$ur )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   917
        return $key;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   918
      $ukey = @fread($ur, $len);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   919
      fclose($ur);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   920
      if ( strlen($ukey) != $len )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   921
        return $key;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   922
      return $ukey;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   923
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   924
    return $key;
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
  /*
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   928
  function byteArrayToString($arr)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   929
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   930
    if(!is_array($arr))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   931
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   932
      $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
   933
      return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   934
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   935
    $ret = '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   936
    foreach($arr as $a)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   937
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   938
      if($a != 0) $ret .= chr($a);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   939
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   940
    return $ret;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   944
  function strtohex($str)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   945
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   946
    $str = $this->enano_str_split($str);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   947
    $ret = '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   948
    foreach($str as $s)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   949
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   950
      $chr = dechex(ord($s));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   951
      if(strlen($chr) < 2) $chr = '0' . $chr;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   952
      $ret .= $chr;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   953
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   954
    return $ret;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   957
  function gen_readymade_key()
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   958
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   959
    $key = $this->strtohex($this->randkey($this->keySizeInBits / 8));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   960
    return $key;
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   963
  function prepare_string($text)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   964
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   965
    $ret = $this->hexToByteArray($this->strtohex($text));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   966
    if(count($ret) != strlen($text))
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   967
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   968
      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
   969
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   970
    return $ret;
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
  /**
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   974
   * Decodes a hex string.
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   975
   * @param string $hex The hex code to decode
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   976
   * @return string
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   979
  function hextostring($hex)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   980
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   981
    $hex = $this->enano_str_split($hex, 2);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   982
    $bin_key = '';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   983
    foreach($hex as $nibble)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   984
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   985
      $byte = chr(hexdec($nibble));
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   986
      $bin_key .= $byte;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   987
    }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   988
    return $bin_key;
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
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   992
function aes_decrypt_cache_store($encrypted, $decrypted, $key)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   993
{
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   994
  $cache_file = ENANO_ROOT . '/cache/aes_decrypt.php';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   995
  // 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
   996
  if ( strlen($decrypted) < 32 )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   997
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
   998
    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
   999
    return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1000
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1001
  if ( file_exists($cache_file) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1002
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1003
    require_once($cache_file);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1004
    global $aes_decrypt_cache;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1005
    $cachekey = sha1($encrypted . '::' . $key);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1006
    $aes_decrypt_cache[$cachekey] = $decrypted;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1007
    
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1008
    if ( count($aes_decrypt_cache) > 5000 )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1009
    {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1010
      // 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
  1011
      $keys = array_keys($aes_decrypt_cache);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1012
      for ( $i = 0; $i < 2500; $i++ )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1013
      {
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
        unset($aes_decrypt_cache[$keys[$i]]);
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
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1019
  else
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1020
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1021
    $aes_decrypt_cache = array(
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1022
      sha1($encrypted . '::' . $key) => $decrypted
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
  }
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1025
  // call var_export and collect contents
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1026
  ob_start();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1027
  var_export($aes_decrypt_cache);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1028
  $dec_cache_string = ob_get_contents();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1029
  ob_end_clean();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1030
  $f = @fopen($cache_file, 'w');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1031
  if ( !$f )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1032
    return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1033
  fwrite($f, "<?php
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1034
\$GLOBALS['aes_decrypt_cache'] = $dec_cache_string;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1035
");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1036
  fclose($f);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1037
  return true;
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
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1040
function aes_decrypt_cache_fetch($hash)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1041
{
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1042
  $cache_file = ENANO_ROOT . '/cache/aes_decrypt.php';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1043
  if ( !file_exists($cache_file) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1044
    return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1045
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1046
  require_once($cache_file);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1047
  global $aes_decrypt_cache;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1048
  if ( isset($aes_decrypt_cache[$hash]) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1049
  {
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1050
    profiler_log("AES: Loaded cached decrypted string, hash is $hash");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1051
    return $aes_decrypt_cache[$hash];
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
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1054
  return false;
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
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1057
function aes_decrypt_cache_destroy($hash)
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1058
{
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1059
  $cache_file = ENANO_ROOT . '/cache/aes_decrypt.php';
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1060
  if ( !file_exists($cache_file) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1061
    return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1062
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1063
  require_once($cache_file);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1064
  global $aes_decrypt_cache;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1065
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1066
  if ( isset($aes_decrypt_cache[$hash]) )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1067
    unset($aes_decrypt_cache[$hash]);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1068
  
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1069
  // call var_export and collect contents
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1070
  ob_start();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1071
  var_export($aes_decrypt_cache);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1072
  $dec_cache_string = ob_get_contents();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1073
  ob_end_clean();
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1074
  $f = @fopen($cache_file, 'w');
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1075
  if ( !$f )
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1076
    return false;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1077
  fwrite($f, "<?php
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1078
\$GLOBALS['aes_decrypt_cache'] = $dec_cache_string;
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1079
");
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1080
  fclose($f);
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1081
  return true;
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
c433348f3628 Merging fixes and updates from stable branch
Dan
parents: 378 440
diff changeset
  1084
?>