@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | parent::__construct(PHP_Crypt::MODE_CBC, $cipher); |
49 | 49 | |
50 | 50 | // this works with only block Ciphers |
51 | - if($cipher->type() != Cipher::BLOCK) |
|
51 | + if ($cipher->type() != Cipher::BLOCK) |
|
52 | 52 | trigger_error("CBC mode requires a block cipher", E_USER_WARNING); |
53 | 53 | } |
54 | 54 | |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | $blocksz = $this->cipher->blockSize(); |
84 | 84 | |
85 | 85 | $max = strlen($text) / $blocksz; |
86 | - for($i = 0; $i < $max; ++$i) |
|
86 | + for ($i = 0; $i < $max; ++$i) |
|
87 | 87 | { |
88 | 88 | // get the current position in $text |
89 | 89 | $pos = $i * $blocksz; |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | $block = substr($text, $pos, $blocksz); |
93 | 93 | |
94 | 94 | // xor the block with the register |
95 | - for($j = 0; $j < $blocksz; ++$j) |
|
95 | + for ($j = 0; $j < $blocksz; ++$j) |
|
96 | 96 | $block[$j] = $block[$j] ^ $this->register[$j]; |
97 | 97 | |
98 | 98 | // encrypt the block, and save it back to the register |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | $blocksz = $this->cipher->blockSize(); |
127 | 127 | |
128 | 128 | $max = strlen($text) / $blocksz; |
129 | - for($i = 0; $i < $max; ++$i) |
|
129 | + for ($i = 0; $i < $max; ++$i) |
|
130 | 130 | { |
131 | 131 | // get the current position in $text |
132 | 132 | $pos = $i * $blocksz; |
@@ -139,7 +139,7 @@ discard block |
||
139 | 139 | $this->cipher->decrypt($block); |
140 | 140 | |
141 | 141 | // xor the block with the register |
142 | - for($j = 0; $j < $blocksz; ++$j) |
|
142 | + for ($j = 0; $j < $blocksz; ++$j) |
|
143 | 143 | $block[$j] = $block[$j] ^ $this->register[$j]; |
144 | 144 | |
145 | 145 | // replace the block of cipher text with plain text |
@@ -50,13 +50,13 @@ |
||
50 | 50 | return 'false'; |
51 | 51 | } |
52 | 52 | |
53 | - return '"' . addslashes($value) . '"'; |
|
53 | + return '"'.addslashes($value).'"'; |
|
54 | 54 | } |
55 | 55 | |
56 | - throw new \InvalidArgumentException('value is invalid: ' . var_export($value, 1)); |
|
56 | + throw new \InvalidArgumentException('value is invalid: '.var_export($value, 1)); |
|
57 | 57 | }, $line); |
58 | 58 | |
59 | - return 'INSERT INTO ' . $this->table . '(' . join(', ', $this->columns) . ')' . |
|
60 | - ' VALUES(' . join(', ', $line) . ');'; |
|
59 | + return 'INSERT INTO '.$this->table.'('.join(', ', $this->columns).')'. |
|
60 | + ' VALUES('.join(', ', $line).');'; |
|
61 | 61 | } |
62 | 62 | } |
@@ -55,15 +55,15 @@ |
||
55 | 55 | return $value; |
56 | 56 | } |
57 | 57 | |
58 | - throw new \InvalidArgumentException('value is invalid: ' . var_export($value, 1)); |
|
58 | + throw new \InvalidArgumentException('value is invalid: '.var_export($value, 1)); |
|
59 | 59 | }, $line); |
60 | 60 | |
61 | 61 | $prepare = array_map(function() { |
62 | 62 | return '?'; |
63 | 63 | }, $line); |
64 | 64 | |
65 | - $sql = 'INSERT INTO ' . $this->table . '(' . join(', ', $this->columns) . ')' . |
|
66 | - ' VALUES(' . join(',', $prepare) . ')'; |
|
65 | + $sql = 'INSERT INTO '.$this->table.'('.join(', ', $this->columns).')'. |
|
66 | + ' VALUES('.join(',', $prepare).')'; |
|
67 | 67 | |
68 | 68 | $stmt = $this->pdo->prepare($sql); |
69 | 69 | $stmt->execute($line); |
@@ -48,20 +48,20 @@ |
||
48 | 48 | //Paging |
49 | 49 | $sLimit = ""; |
50 | 50 | if (isset($_GET['iDisplayStart']) && $_GET['iDisplayLength'] != '-1') { |
51 | - $sLimit = "LIMIT ". filter_var($_GET['iDisplayStart'], FILTER_SANITIZE_NUMBER_INT) .", ". filter_var($_GET['iDisplayLength'], FILTER_SANITIZE_NUMBER_INT).""; |
|
51 | + $sLimit = "LIMIT ".filter_var($_GET['iDisplayStart'], FILTER_SANITIZE_NUMBER_INT).", ".filter_var($_GET['iDisplayLength'], FILTER_SANITIZE_NUMBER_INT).""; |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | //Ordering |
55 | 55 | |
56 | 56 | if (isset($_GET['iSortCol_0']) && in_array($_GET['iSortCol_0'], $aSortTypes)) { |
57 | 57 | $sOrder = "ORDER BY "; |
58 | - for ($i=0; $i<intval($_GET['iSortingCols']); $i++) { |
|
58 | + for ($i = 0; $i < intval($_GET['iSortingCols']); $i++) { |
|
59 | 59 | if ( |
60 | - $_GET[ 'bSortable_'.filter_var($_GET['iSortCol_'.$i], FILTER_SANITIZE_NUMBER_INT)] == "true" && |
|
60 | + $_GET['bSortable_'.filter_var($_GET['iSortCol_'.$i], FILTER_SANITIZE_NUMBER_INT)] == "true" && |
|
61 | 61 | preg_match("#^(asc|desc)\$#i", $_GET['sSortDir_'.$i]) |
62 | 62 | ) { |
63 | - $sOrder .= "".$aColumns[ filter_var($_GET['iSortCol_'.$i], FILTER_SANITIZE_NUMBER_INT) ]." " |
|
64 | - .mysqli_escape_string($link, $_GET['sSortDir_'.$i]) .", "; |
|
63 | + $sOrder .= "".$aColumns[filter_var($_GET['iSortCol_'.$i], FILTER_SANITIZE_NUMBER_INT)]." " |
|
64 | + .mysqli_escape_string($link, $_GET['sSortDir_'.$i]).", "; |
|
65 | 65 | } |
66 | 66 | } |
67 | 67 |
@@ -1347,11 +1347,11 @@ |
||
1347 | 1347 | /** |
1348 | 1348 | * @param string $message |
1349 | 1349 | */ |
1350 | -function send_syslog($message, $component = "teampass", $program = "php", $host , $port) |
|
1350 | +function send_syslog($message, $component = "teampass", $program = "php", $host, $port) |
|
1351 | 1351 | { |
1352 | 1352 | $sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); |
1353 | 1353 | //$syslog_message = "<123>" . date('M d H:i:s ') . " " .$host . " " . $component . ": " . $message; |
1354 | - $syslog_message = "<123>" . date('M d H:i:s ') . $component . ": " . $message; |
|
1354 | + $syslog_message = "<123>".date('M d H:i:s ').$component.": ".$message; |
|
1355 | 1355 | socket_sendto($sock, $syslog_message, strlen($syslog_message), 0, $host, $port); |
1356 | 1356 | socket_close($sock); |
1357 | 1357 | } |
@@ -2048,31 +2048,31 @@ |
||
2048 | 2048 | ldap_start_tls($ldapconn); |
2049 | 2049 | } |
2050 | 2050 | |
2051 | - $debug_ldap .= "LDAP connection : " . ($ldapconn ? "Connected" : "Failed") . "<br/>"; |
|
2051 | + $debug_ldap .= "LDAP connection : ".($ldapconn ? "Connected" : "Failed")."<br/>"; |
|
2052 | 2052 | |
2053 | 2053 | ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3); |
2054 | 2054 | if ($ldapconn) { |
2055 | 2055 | $ldapbind = @ldap_bind($ldapconn, $dataReceived[0]['ldap_bind_dn'], $dataReceived[0]['ldap_bind_passwd']); |
2056 | 2056 | |
2057 | - $debug_ldap .= "LDAP bind : " . ($ldapbind ? "Bound" : "Failed") . "<br/>"; |
|
2057 | + $debug_ldap .= "LDAP bind : ".($ldapbind ? "Bound" : "Failed")."<br/>"; |
|
2058 | 2058 | |
2059 | 2059 | if ($ldapbind) { |
2060 | - $filter="(&(" . $dataReceived[0]['ldap_user_attribute']. "=$username)(objectClass=" . $dataReceived[0]['ldap_object_class'] ."))"; |
|
2061 | - $result=ldap_search($ldapconn, $dataReceived[0]['ldap_search_base'], $filter, array('dn','mail','givenname','sn')); |
|
2060 | + $filter = "(&(".$dataReceived[0]['ldap_user_attribute']."=$username)(objectClass=".$dataReceived[0]['ldap_object_class']."))"; |
|
2061 | + $result = ldap_search($ldapconn, $dataReceived[0]['ldap_search_base'], $filter, array('dn', 'mail', 'givenname', 'sn')); |
|
2062 | 2062 | if (isset($dataReceived[0]['ldap_usergroup'])) { |
2063 | 2063 | $filter_group = "memberUid=".$username; |
2064 | - $result_group = ldap_search($ldapconn, $dataReceived[0]['ldap_usergroup'],$filter_group, array('dn')); |
|
2064 | + $result_group = ldap_search($ldapconn, $dataReceived[0]['ldap_usergroup'], $filter_group, array('dn')); |
|
2065 | 2065 | |
2066 | - $debug_ldap .= 'Search filter (group): ' . $filter_group . "<br/>" . |
|
2067 | - 'Results : ' . print_r(ldap_get_entries($ldapconn, $result_group), true) . "<br/>"; |
|
2066 | + $debug_ldap .= 'Search filter (group): '.$filter_group."<br/>". |
|
2067 | + 'Results : '.print_r(ldap_get_entries($ldapconn, $result_group), true)."<br/>"; |
|
2068 | 2068 | |
2069 | 2069 | if (!ldap_count_entries($ldapconn, $result_group)) { |
2070 | 2070 | $ldapConnection = "Error - No entries found"; |
2071 | 2071 | } |
2072 | 2072 | } |
2073 | 2073 | |
2074 | - $debug_ldap .= 'Search filter : ' . $filter . "<br/>" . |
|
2075 | - 'Results : ' . print_r(ldap_get_entries($ldapconn, $result), true) . "<br/>"; |
|
2074 | + $debug_ldap .= 'Search filter : '.$filter."<br/>". |
|
2075 | + 'Results : '.print_r(ldap_get_entries($ldapconn, $result), true)."<br/>"; |
|
2076 | 2076 | |
2077 | 2077 | if (ldap_count_entries($ldapconn, $result)) { |
2078 | 2078 | // try auth |
@@ -350,25 +350,25 @@ |
||
350 | 350 | ldap_start_tls($ldapconn); |
351 | 351 | } |
352 | 352 | if ($debugLdap == 1) { |
353 | - fputs($dbgLdap, "LDAP connection : " . ($ldapconn ? "Connected" : "Failed") . "\n"); |
|
353 | + fputs($dbgLdap, "LDAP connection : ".($ldapconn ? "Connected" : "Failed")."\n"); |
|
354 | 354 | } |
355 | 355 | ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3); |
356 | 356 | if ($ldapconn) { |
357 | 357 | $ldapbind = ldap_bind($ldapconn, $_SESSION['settings']['ldap_bind_dn'], $_SESSION['settings']['ldap_bind_passwd']); |
358 | 358 | if ($debugLdap == 1) { |
359 | - fputs($dbgLdap, "LDAP bind : " . ($ldapbind ? "Bound" : "Failed") . "\n"); |
|
359 | + fputs($dbgLdap, "LDAP bind : ".($ldapbind ? "Bound" : "Failed")."\n"); |
|
360 | 360 | } |
361 | 361 | if ($ldapbind) { |
362 | - $filter="(&(" . $_SESSION['settings']['ldap_user_attribute']. "=$username)(objectClass=" . $_SESSION['settings']['ldap_object_class'] ."))"; |
|
363 | - $result=ldap_search($ldapconn, $_SESSION['settings']['ldap_search_base'], $filter, array('dn','mail','givenname','sn')); |
|
362 | + $filter = "(&(".$_SESSION['settings']['ldap_user_attribute']."=$username)(objectClass=".$_SESSION['settings']['ldap_object_class']."))"; |
|
363 | + $result = ldap_search($ldapconn, $_SESSION['settings']['ldap_search_base'], $filter, array('dn', 'mail', 'givenname', 'sn')); |
|
364 | 364 | if (isset($_SESSION['settings']['ldap_usergroup'])) { |
365 | 365 | $filter_group = "memberUid=".$username; |
366 | - $result_group = ldap_search($ldapconn, $_SESSION['settings']['ldap_usergroup'],$filter_group, array('dn')); |
|
366 | + $result_group = ldap_search($ldapconn, $_SESSION['settings']['ldap_usergroup'], $filter_group, array('dn')); |
|
367 | 367 | if ($debugLdap == 1) { |
368 | 368 | fputs( |
369 | 369 | $dbgLdap, |
370 | - 'Search filter (group): ' . $filter_group . "\n" . |
|
371 | - 'Results : ' . print_r(ldap_get_entries($ldapconn, $result_group), true) . "\n" |
|
370 | + 'Search filter (group): '.$filter_group."\n". |
|
371 | + 'Results : '.print_r(ldap_get_entries($ldapconn, $result_group), true)."\n" |
|
372 | 372 | ); |
373 | 373 | } |
374 | 374 | if (!ldap_count_entries($ldapconn, $result_group)) { |
@@ -15,17 +15,17 @@ discard block |
||
15 | 15 | * @return ciphertext as byte-array (16 bytes) |
16 | 16 | */ |
17 | 17 | public static function cipher($input, $w) { // main cipher function [é5.1] |
18 | - $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
19 | - $Nr = count($w)/$Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
18 | + $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
19 | + $Nr = count($w) / $Nb - 1; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
20 | 20 | |
21 | - $state = array(); // initialise 4xNb byte-array 'state' with input [é3.4] |
|
22 | - for ($i=0; $i<4*$Nb; $i++) { |
|
23 | - $state[$i%4][floor($i/4)] = $input[$i]; |
|
21 | + $state = array(); // initialise 4xNb byte-array 'state' with input [é3.4] |
|
22 | + for ($i = 0; $i < 4 * $Nb; $i++) { |
|
23 | + $state[$i % 4][floor($i / 4)] = $input[$i]; |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | $state = self::addRoundKey($state, $w, 0, $Nb); |
27 | 27 | |
28 | - for ($round=1; $round<$Nr; $round++) { // apply Nr rounds |
|
28 | + for ($round = 1; $round < $Nr; $round++) { // apply Nr rounds |
|
29 | 29 | $state = self::subBytes($state, $Nb); |
30 | 30 | $state = self::shiftRows($state, $Nb); |
31 | 31 | $state = self::mixColumns($state, $Nb); |
@@ -36,8 +36,8 @@ discard block |
||
36 | 36 | $state = self::shiftRows($state, $Nb); |
37 | 37 | $state = self::addRoundKey($state, $w, $Nr, $Nb); |
38 | 38 | |
39 | - $output = array(4*$Nb); // convert state to 1-d array before returning [é3.4] |
|
40 | - for ($i=0; $i<4*$Nb; $i++) $output[$i] = $state[$i%4][floor($i/4)]; |
|
39 | + $output = array(4 * $Nb); // convert state to 1-d array before returning [é3.4] |
|
40 | + for ($i = 0; $i < 4 * $Nb; $i++) $output[$i] = $state[$i % 4][floor($i / 4)]; |
|
41 | 41 | |
42 | 42 | return $output; |
43 | 43 | } |
@@ -47,8 +47,8 @@ discard block |
||
47 | 47 | * @param integer $Nb |
48 | 48 | */ |
49 | 49 | private static function addRoundKey($state, $w, $rnd, $Nb) { // xor Round Key into state S [é5.1.4] |
50 | - for ($r=0; $r<4; $r++) { |
|
51 | - for ($c=0; $c<$Nb; $c++) $state[$r][$c] ^= $w[$rnd*4+$c][$r]; |
|
50 | + for ($r = 0; $r < 4; $r++) { |
|
51 | + for ($c = 0; $c < $Nb; $c++) $state[$r][$c] ^= $w[$rnd * 4 + $c][$r]; |
|
52 | 52 | } |
53 | 53 | |
54 | 54 | return $state; |
@@ -58,8 +58,8 @@ discard block |
||
58 | 58 | * @param integer $Nb |
59 | 59 | */ |
60 | 60 | private static function subBytes($s, $Nb) { // apply SBox to state S [é5.1.1] |
61 | - for ($r=0; $r<4; $r++) { |
|
62 | - for ($c=0; $c<$Nb; $c++) { |
|
61 | + for ($r = 0; $r < 4; $r++) { |
|
62 | + for ($c = 0; $c < $Nb; $c++) { |
|
63 | 63 | $s[$r][$c] = self::$sBox[$s[$r][$c]]; |
64 | 64 | } |
65 | 65 | } |
@@ -72,29 +72,29 @@ discard block |
||
72 | 72 | */ |
73 | 73 | private static function shiftRows($s, $Nb) { // shift row r of state S left by r bytes [é5.1.2] |
74 | 74 | $t = array(4); |
75 | - for ($r=1; $r<4; $r++) { |
|
76 | - for ($c=0; $c<4; $c++) { |
|
77 | - $t[$c] = $s[$r][($c+$r)%$Nb]; |
|
75 | + for ($r = 1; $r < 4; $r++) { |
|
76 | + for ($c = 0; $c < 4; $c++) { |
|
77 | + $t[$c] = $s[$r][($c + $r) % $Nb]; |
|
78 | 78 | } |
79 | 79 | // shift into temp copy |
80 | - for ($c=0; $c<4; $c++) { |
|
80 | + for ($c = 0; $c < 4; $c++) { |
|
81 | 81 | $s[$r][$c] = $t[$c]; |
82 | 82 | } |
83 | 83 | // and copy back |
84 | 84 | } // note that this will work for Nb=4,5,6, but not 7,8 (always 4 for AES): |
85 | - return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
|
85 | + return $s; // see fp.gladman.plus.com/cryptography_technology/rijndael/aes.spec.311.pdf |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | /** |
89 | 89 | * @param integer $Nb |
90 | 90 | */ |
91 | 91 | private static function mixColumns($s, $Nb) { // combine bytes of each col of state S [é5.1.3] |
92 | - for ($c=0; $c<4; $c++) { |
|
93 | - $a = array(4); // 'a' is a copy of the current column from 's' |
|
94 | - $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
95 | - for ($i=0; $i<4; $i++) { |
|
92 | + for ($c = 0; $c < 4; $c++) { |
|
93 | + $a = array(4); // 'a' is a copy of the current column from 's' |
|
94 | + $b = array(4); // 'b' is aé{02} in GF(2^8) |
|
95 | + for ($i = 0; $i < 4; $i++) { |
|
96 | 96 | $a[$i] = $s[$i][$c]; |
97 | - $b[$i] = $s[$i][$c]&0x80 ? $s[$i][$c]<<1 ^ 0x011b : $s[$i][$c]<<1; |
|
97 | + $b[$i] = $s[$i][$c] & 0x80 ? $s[$i][$c] << 1 ^ 0x011b : $s[$i][$c] << 1; |
|
98 | 98 | } |
99 | 99 | // a[n] ^ b[n] is aé{03} in GF(2^8) |
100 | 100 | $s[0][$c] = $b[0] ^ $a[1] ^ $b[1] ^ $a[2] ^ $a[3]; // 2*a0 + 3*a1 + a2 + a3 |
@@ -114,42 +114,42 @@ discard block |
||
114 | 114 | * @return key schedule as 2D byte-array (Nr+1 x Nb bytes) |
115 | 115 | */ |
116 | 116 | public static function keyExpansion($key) { // generate Key Schedule from Cipher Key [é5.2] |
117 | - $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
118 | - $Nk = count($key)/4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
|
119 | - $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
117 | + $Nb = 4; // block size (in words): no of columns in state (fixed at 4 for AES) |
|
118 | + $Nk = count($key) / 4; // key length (in words): 4/6/8 for 128/192/256-bit keys |
|
119 | + $Nr = $Nk + 6; // no of rounds: 10/12/14 for 128/192/256-bit keys |
|
120 | 120 | |
121 | 121 | $w = array(); |
122 | 122 | $temp = array(); |
123 | 123 | |
124 | - for ($i=0; $i<$Nk; $i++) { |
|
125 | - $r = array($key[4*$i], $key[4*$i+1], $key[4*$i+2], $key[4*$i+3]); |
|
124 | + for ($i = 0; $i < $Nk; $i++) { |
|
125 | + $r = array($key[4 * $i], $key[4 * $i + 1], $key[4 * $i + 2], $key[4 * $i + 3]); |
|
126 | 126 | $w[$i] = $r; |
127 | 127 | } |
128 | 128 | |
129 | - for ($i=$Nk; $i<($Nb*($Nr+1)); $i++) { |
|
129 | + for ($i = $Nk; $i < ($Nb * ($Nr + 1)); $i++) { |
|
130 | 130 | $w[$i] = array(); |
131 | - for ($t=0; $t<4; $t++) $temp[$t] = $w[$i-1][$t]; |
|
131 | + for ($t = 0; $t < 4; $t++) $temp[$t] = $w[$i - 1][$t]; |
|
132 | 132 | if ($i % $Nk == 0) { |
133 | 133 | $temp = self::subWord(self::rotWord($temp)); |
134 | - for ($t=0; $t<4; $t++) $temp[$t] ^= self::$rCon[$i/$Nk][$t]; |
|
135 | - } elseif ($Nk > 6 && $i%$Nk == 4) { |
|
134 | + for ($t = 0; $t < 4; $t++) $temp[$t] ^= self::$rCon[$i / $Nk][$t]; |
|
135 | + } elseif ($Nk > 6 && $i % $Nk == 4) { |
|
136 | 136 | $temp = self::subWord($temp); |
137 | 137 | } |
138 | - for ($t=0; $t<4; $t++) $w[$i][$t] = $w[$i-$Nk][$t] ^ $temp[$t]; |
|
138 | + for ($t = 0; $t < 4; $t++) $w[$i][$t] = $w[$i - $Nk][$t] ^ $temp[$t]; |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | return $w; |
142 | 142 | } |
143 | 143 | |
144 | 144 | private static function subWord($w) { // apply SBox to 4-byte word w |
145 | - for ($i=0; $i<4; $i++) $w[$i] = self::$sBox[$w[$i]]; |
|
145 | + for ($i = 0; $i < 4; $i++) $w[$i] = self::$sBox[$w[$i]]; |
|
146 | 146 | |
147 | 147 | return $w; |
148 | 148 | } |
149 | 149 | |
150 | 150 | private static function rotWord($w) { // rotate 4-byte word w left by one byte |
151 | 151 | $tmp = $w[0]; |
152 | - for ($i=0; $i<3; $i++) $w[$i] = $w[$i+1]; |
|
152 | + for ($i = 0; $i < 3; $i++) $w[$i] = $w[$i + 1]; |
|
153 | 153 | $w[3] = $tmp; |
154 | 154 | |
155 | 155 | return $w; |
@@ -157,22 +157,22 @@ discard block |
||
157 | 157 | |
158 | 158 | // sBox is pre-computed multiplicative inverse in GF(2^8) used in subBytes and keyExpansion [é5.1.1] |
159 | 159 | private static $sBox = array( |
160 | - 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76, |
|
161 | - 0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0, |
|
162 | - 0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15, |
|
163 | - 0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75, |
|
164 | - 0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84, |
|
165 | - 0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf, |
|
166 | - 0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8, |
|
167 | - 0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2, |
|
168 | - 0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73, |
|
169 | - 0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb, |
|
170 | - 0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79, |
|
171 | - 0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08, |
|
172 | - 0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a, |
|
173 | - 0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e, |
|
174 | - 0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf, |
|
175 | - 0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16); |
|
160 | + 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76, |
|
161 | + 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, |
|
162 | + 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15, |
|
163 | + 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75, |
|
164 | + 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84, |
|
165 | + 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf, |
|
166 | + 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8, |
|
167 | + 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2, |
|
168 | + 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73, |
|
169 | + 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb, |
|
170 | + 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79, |
|
171 | + 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08, |
|
172 | + 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a, |
|
173 | + 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e, |
|
174 | + 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf, |
|
175 | + 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16); |
|
176 | 176 | |
177 | 177 | // rCon is Round Constant used for the Key Expansion [1st col is 2^(r-1) in GF(2^8)] [é5.2] |
178 | 178 | private static $rCon = array( |
@@ -20,8 +20,8 @@ discard block |
||
20 | 20 | */ |
21 | 21 | public static function encrypt($plaintext, $password, $nBits) |
22 | 22 | { |
23 | - $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
24 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
|
23 | + $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
24 | + if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { |
|
25 | 25 | return ''; |
26 | 26 | } |
27 | 27 | // standard allows 128/192/256 bit keys |
@@ -29,30 +29,30 @@ discard block |
||
29 | 29 | |
30 | 30 | // use AES itself to encrypt password to get cipher key (using plain password as source for |
31 | 31 | // key expansion) - gives us well encrypted key |
32 | - $nBytes = $nBits/8; // no bytes in key |
|
32 | + $nBytes = $nBits / 8; // no bytes in key |
|
33 | 33 | $pwBytes = array(); |
34 | - for ($i=0; $i<$nBytes; $i++) { |
|
35 | - $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff; |
|
34 | + for ($i = 0; $i < $nBytes; $i++) { |
|
35 | + $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; |
|
36 | 36 | } |
37 | 37 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
38 | - $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long |
|
38 | + $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long |
|
39 | 39 | |
40 | 40 | // initialise counter block (NIST SP800-38A §B.2): millisecond time-stamp for nonce in |
41 | 41 | // 1st 8 bytes, block counter in 2nd 8 bytes |
42 | 42 | $counterBlock = array(); |
43 | - $nonce = floor(microtime(true)*1000); // timestamp: milliseconds since 1-Jan-1970 |
|
44 | - $nonceSec = floor($nonce/1000); |
|
45 | - $nonceMs = $nonce%1000; |
|
43 | + $nonce = floor(microtime(true) * 1000); // timestamp: milliseconds since 1-Jan-1970 |
|
44 | + $nonceSec = floor($nonce / 1000); |
|
45 | + $nonceMs = $nonce % 1000; |
|
46 | 46 | // encode nonce with seconds in 1st 4 bytes, and (repeated) ms part filling 2nd 4 bytes |
47 | - for ($i=0; $i<4; $i++) { |
|
48 | - $counterBlock[$i] = self::urs($nonceSec, $i*8) & 0xff; |
|
47 | + for ($i = 0; $i < 4; $i++) { |
|
48 | + $counterBlock[$i] = self::urs($nonceSec, $i * 8) & 0xff; |
|
49 | 49 | } |
50 | - for ($i=0; $i<4; $i++) { |
|
51 | - $counterBlock[$i+4] = $nonceMs & 0xff; |
|
50 | + for ($i = 0; $i < 4; $i++) { |
|
51 | + $counterBlock[$i + 4] = $nonceMs & 0xff; |
|
52 | 52 | } |
53 | 53 | // and convert it to a string to go on the front of the ciphertext |
54 | 54 | $ctrTxt = ''; |
55 | - for ($i=0; $i<8; $i++) { |
|
55 | + for ($i = 0; $i < 8; $i++) { |
|
56 | 56 | $ctrTxt .= chr($counterBlock[$i]); |
57 | 57 | } |
58 | 58 | |
@@ -60,26 +60,26 @@ discard block |
||
60 | 60 | $keySchedule = Aes::keyExpansion($key); |
61 | 61 | //print_r($keySchedule); |
62 | 62 | |
63 | - $blockCount = ceil(strlen($plaintext)/$blockSize); |
|
64 | - $ciphertxt = array(); // ciphertext as array of strings |
|
63 | + $blockCount = ceil(strlen($plaintext) / $blockSize); |
|
64 | + $ciphertxt = array(); // ciphertext as array of strings |
|
65 | 65 | |
66 | - for ($b=0; $b<$blockCount; $b++) { |
|
66 | + for ($b = 0; $b < $blockCount; $b++) { |
|
67 | 67 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
68 | 68 | // done in two stages for 32-bit ops: using two words allows us to go past 2^32 blocks (68GB) |
69 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
70 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs($b/0x100000000, $c*8); |
|
69 | + for ($c = 0; $c < 4; $c++) $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
|
70 | + for ($c = 0; $c < 4; $c++) $counterBlock[15 - $c - 4] = self::urs($b / 0x100000000, $c * 8); |
|
71 | 71 | |
72 | - $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
|
72 | + $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // -- encrypt counter block -- |
|
73 | 73 | |
74 | 74 | // block size is reduced on final block |
75 | - $blockLength = $b<$blockCount-1 ? $blockSize : (strlen($plaintext)-1)%$blockSize+1; |
|
75 | + $blockLength = $b < $blockCount - 1 ? $blockSize : (strlen($plaintext) - 1) % $blockSize + 1; |
|
76 | 76 | $cipherByte = array(); |
77 | 77 | |
78 | - for ($i=0; $i<$blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte -- |
|
79 | - $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b*$blockSize+$i, 1)); |
|
78 | + for ($i = 0; $i < $blockLength; $i++) { // -- xor plaintext with ciphered counter byte-by-byte -- |
|
79 | + $cipherByte[$i] = $cipherCntr[$i] ^ ord(substr($plaintext, $b * $blockSize + $i, 1)); |
|
80 | 80 | $cipherByte[$i] = chr($cipherByte[$i]); |
81 | 81 | } |
82 | - $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext |
|
82 | + $ciphertxt[$b] = implode('', $cipherByte); // escape troublesome characters in ciphertext |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | // implode is more efficient than repeated string concatenation |
@@ -99,54 +99,54 @@ discard block |
||
99 | 99 | */ |
100 | 100 | public static function decrypt($ciphertext, $password, $nBits) |
101 | 101 | { |
102 | - $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
103 | - if (!($nBits==128 || $nBits==192 || $nBits==256)) { |
|
102 | + $blockSize = 16; // block size fixed at 16 bytes / 128 bits (Nb=4) for AES |
|
103 | + if (!($nBits == 128 || $nBits == 192 || $nBits == 256)) { |
|
104 | 104 | return ''; |
105 | 105 | } |
106 | 106 | // standard allows 128/192/256 bit keys |
107 | 107 | $ciphertext = base64_decode($ciphertext); |
108 | 108 | |
109 | 109 | // use AES to encrypt password (mirroring encrypt routine) |
110 | - $nBytes = $nBits/8; // no bytes in key |
|
110 | + $nBytes = $nBits / 8; // no bytes in key |
|
111 | 111 | $pwBytes = array(); |
112 | - for ($i=0; $i<$nBytes; $i++) { |
|
113 | - $pwBytes[$i] = ord(substr($password,$i,1)) & 0xff; |
|
112 | + for ($i = 0; $i < $nBytes; $i++) { |
|
113 | + $pwBytes[$i] = ord(substr($password, $i, 1)) & 0xff; |
|
114 | 114 | } |
115 | 115 | $key = Aes::cipher($pwBytes, Aes::keyExpansion($pwBytes)); |
116 | - $key = array_merge($key, array_slice($key, 0, $nBytes-16)); // expand key to 16/24/32 bytes long |
|
116 | + $key = array_merge($key, array_slice($key, 0, $nBytes - 16)); // expand key to 16/24/32 bytes long |
|
117 | 117 | |
118 | 118 | // recover nonce from 1st element of ciphertext |
119 | 119 | $counterBlock = array(); |
120 | 120 | $ctrTxt = substr($ciphertext, 0, 8); |
121 | - for ($i=0; $i<8; $i++) { |
|
122 | - $counterBlock[$i] = ord(substr($ctrTxt,$i,1)); |
|
121 | + for ($i = 0; $i < 8; $i++) { |
|
122 | + $counterBlock[$i] = ord(substr($ctrTxt, $i, 1)); |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | // generate key schedule |
126 | 126 | $keySchedule = Aes::keyExpansion($key); |
127 | 127 | |
128 | 128 | // separate ciphertext into blocks (skipping past initial 8 bytes) |
129 | - $nBlocks = ceil((strlen($ciphertext)-8) / $blockSize); |
|
129 | + $nBlocks = ceil((strlen($ciphertext) - 8) / $blockSize); |
|
130 | 130 | $ct = array(); |
131 | - for ($b=0; $b<$nBlocks; $b++) { |
|
132 | - $ct[$b] = substr($ciphertext, 8+$b*$blockSize, 16); |
|
131 | + for ($b = 0; $b < $nBlocks; $b++) { |
|
132 | + $ct[$b] = substr($ciphertext, 8 + $b * $blockSize, 16); |
|
133 | 133 | } |
134 | - $ciphertext = $ct; // ciphertext is now array of block-length strings |
|
134 | + $ciphertext = $ct; // ciphertext is now array of block-length strings |
|
135 | 135 | |
136 | 136 | // plaintext will get generated block-by-block into array of block-length strings |
137 | 137 | $plaintxt = array(); |
138 | 138 | |
139 | - for ($b=0; $b<$nBlocks; $b++) { |
|
139 | + for ($b = 0; $b < $nBlocks; $b++) { |
|
140 | 140 | // set counter (block #) in last 8 bytes of counter block (leaving nonce in 1st 8 bytes) |
141 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c] = self::urs($b, $c*8) & 0xff; |
|
142 | - for ($c=0; $c<4; $c++) $counterBlock[15-$c-4] = self::urs(($b+1)/0x100000000-1, $c*8) & 0xff; |
|
141 | + for ($c = 0; $c < 4; $c++) $counterBlock[15 - $c] = self::urs($b, $c * 8) & 0xff; |
|
142 | + for ($c = 0; $c < 4; $c++) $counterBlock[15 - $c - 4] = self::urs(($b + 1) / 0x100000000 - 1, $c * 8) & 0xff; |
|
143 | 143 | |
144 | - $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
144 | + $cipherCntr = Aes::cipher($counterBlock, $keySchedule); // encrypt counter block |
|
145 | 145 | |
146 | 146 | $plaintxtByte = array(); |
147 | - for ($i=0; $i<strlen($ciphertext[$b]); $i++) { |
|
147 | + for ($i = 0; $i < strlen($ciphertext[$b]); $i++) { |
|
148 | 148 | // -- xor plaintext with ciphered counter byte-by-byte -- |
149 | - $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b],$i,1)); |
|
149 | + $plaintxtByte[$i] = $cipherCntr[$i] ^ ord(substr($ciphertext[$b], $i, 1)); |
|
150 | 150 | $plaintxtByte[$i] = chr($plaintxtByte[$i]); |
151 | 151 | |
152 | 152 | } |
@@ -168,12 +168,12 @@ discard block |
||
168 | 168 | */ |
169 | 169 | private static function urs($a, $b) |
170 | 170 | { |
171 | - $a &= 0xffffffff; $b &= 0x1f; // (bounds check) |
|
172 | - if ($a&0x80000000 && $b>0) { // if left-most bit set |
|
173 | - $a = ($a>>1) & 0x7fffffff; // right-shift one bit & clear left-most bit |
|
174 | - $a = $a >> ($b-1); // remaining right-shifts |
|
171 | + $a &= 0xffffffff; $b &= 0x1f; // (bounds check) |
|
172 | + if ($a & 0x80000000 && $b > 0) { // if left-most bit set |
|
173 | + $a = ($a >> 1) & 0x7fffffff; // right-shift one bit & clear left-most bit |
|
174 | + $a = $a >> ($b - 1); // remaining right-shifts |
|
175 | 175 | } else { // otherwise |
176 | - $a = ($a>>$b); // use normal right-shift |
|
176 | + $a = ($a >> $b); // use normal right-shift |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | return $a; |