@@ -21,7 +21,7 @@ |
||
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | 23 | * ParagonIE_Sodium_Core32_Int64 constructor. |
| 24 | - * @param array $array |
|
| 24 | + * @param integer[] $array |
|
| 25 | 25 | */ |
| 26 | 26 | public function __construct($array = array(0, 0, 0, 0)) |
| 27 | 27 | { |
@@ -9,588 +9,588 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class ParagonIE_Sodium_Core32_Int64 |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * @var array<int, int> - four 16-bit integers |
|
| 14 | - */ |
|
| 15 | - public $limbs; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @var int |
|
| 19 | - */ |
|
| 20 | - public $overflow = 0; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * ParagonIE_Sodium_Core32_Int64 constructor. |
|
| 24 | - * @param array $array |
|
| 25 | - */ |
|
| 26 | - public function __construct($array = array(0, 0, 0, 0)) |
|
| 27 | - { |
|
| 28 | - $this->limbs = array( |
|
| 29 | - (int) $array[0], |
|
| 30 | - (int) $array[1], |
|
| 31 | - (int) $array[2], |
|
| 32 | - (int) $array[3] |
|
| 33 | - ); |
|
| 34 | - $this->overflow = 0; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Adds two int64 objects |
|
| 39 | - * |
|
| 40 | - * @param ParagonIE_Sodium_Core32_Int64 $addend |
|
| 41 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 42 | - */ |
|
| 43 | - public function addInt64(ParagonIE_Sodium_Core32_Int64 $addend) |
|
| 44 | - { |
|
| 45 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 46 | - $carry = 0; |
|
| 47 | - for ($i = 3; $i >= 0; --$i) { |
|
| 48 | - $tmp = $this->limbs[$i] + $addend->limbs[$i] + $carry; |
|
| 49 | - $carry = $tmp >> 16; |
|
| 50 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 51 | - } |
|
| 52 | - $return->overflow = $carry; |
|
| 53 | - return $return; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Adds a normal integer to an int64 object |
|
| 58 | - * |
|
| 59 | - * @param int $int |
|
| 60 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 61 | - */ |
|
| 62 | - public function addInt($int) |
|
| 63 | - { |
|
| 64 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 65 | - |
|
| 66 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 67 | - $carry = 0; |
|
| 68 | - for ($i = 3; $i >= 0; --$i) { |
|
| 69 | - $step = (3 - $i) << 4; // 0, 16, 32, 48 |
|
| 70 | - if ($i < 2) { |
|
| 71 | - $toAdd = 0; |
|
| 72 | - } else { |
|
| 73 | - $toAdd = (($int >> $step) & 0xffff); |
|
| 74 | - } |
|
| 75 | - $tmp = $this->limbs[$i] + $toAdd + $carry; |
|
| 76 | - $carry = $tmp >> 16; |
|
| 77 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 78 | - } |
|
| 79 | - $return->overflow = $carry; |
|
| 80 | - return $return; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * @param int $b |
|
| 85 | - * @return int |
|
| 86 | - */ |
|
| 87 | - public function compareInt($b = 0) |
|
| 88 | - { |
|
| 89 | - $gt = 0; |
|
| 90 | - $eq = 1; |
|
| 91 | - |
|
| 92 | - $i = 4; |
|
| 93 | - $j = 0; |
|
| 94 | - while ($i > 0) { |
|
| 95 | - --$i; |
|
| 96 | - $x1 = $this->limbs[$i]; |
|
| 97 | - $x2 = ($b >> ($j << 4)) & 0xffff; |
|
| 98 | - $gt |= (($x2 - $x1) >> 8) & $eq; |
|
| 99 | - $eq &= (($x2 ^ $x1) - 1) >> 8; |
|
| 100 | - } |
|
| 101 | - return ($gt + $gt - $eq) + 1; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @param int $b |
|
| 106 | - * @return bool |
|
| 107 | - */ |
|
| 108 | - public function isGreaterThan($b = 0) |
|
| 109 | - { |
|
| 110 | - return $this->compareInt($b) > 0; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @param int $b |
|
| 115 | - * @return bool |
|
| 116 | - */ |
|
| 117 | - public function isLessThanInt($b = 0) |
|
| 118 | - { |
|
| 119 | - return $this->compareInt($b) < 0; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * @param int $hi |
|
| 125 | - * @param int $lo |
|
| 126 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 127 | - */ |
|
| 128 | - public function mask64($hi = 0, $lo = 0) |
|
| 129 | - { |
|
| 130 | - $a = ($hi >> 16) & 0xffff; |
|
| 131 | - $b = ($hi) & 0xffff; |
|
| 132 | - $c = ($lo >> 16) & 0xffff; |
|
| 133 | - $d = ($lo & 0xffff); |
|
| 134 | - return new ParagonIE_Sodium_Core32_Int64( |
|
| 135 | - array( |
|
| 136 | - $this->limbs[0] & $a, |
|
| 137 | - $this->limbs[1] & $b, |
|
| 138 | - $this->limbs[2] & $c, |
|
| 139 | - $this->limbs[3] & $d |
|
| 140 | - ) |
|
| 141 | - ); |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * @param int $int |
|
| 146 | - * @param int $size |
|
| 147 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 148 | - */ |
|
| 149 | - public function mulInt($int = 0, $size = 0) |
|
| 150 | - { |
|
| 151 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 152 | - ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
| 153 | - if (!$size) { |
|
| 154 | - $size = 63; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $a = clone $this; |
|
| 158 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 159 | - |
|
| 160 | - for ($i = $size; $i >= 0; --$i) { |
|
| 161 | - $return = $return->addInt64( |
|
| 162 | - $a->mask64( |
|
| 163 | - (int) (-($int & 1)), |
|
| 164 | - (int) (-($int & 1)) |
|
| 165 | - ) |
|
| 166 | - ); |
|
| 167 | - $a = $a->shiftLeft(1); |
|
| 168 | - $int >>= 1; |
|
| 169 | - } |
|
| 170 | - return $return; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * @param ParagonIE_Sodium_Core32_Int64 $int |
|
| 175 | - * @param int $size |
|
| 176 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 177 | - */ |
|
| 178 | - public function mulInt64(ParagonIE_Sodium_Core32_Int64 $int, $size = 0) |
|
| 179 | - { |
|
| 180 | - ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
| 181 | - if (!$size) { |
|
| 182 | - $size = 63; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - $a = clone $this; |
|
| 186 | - $b = clone $int; |
|
| 187 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 188 | - |
|
| 189 | - for ($i = $size; $i >= 0; --$i) { |
|
| 190 | - /* |
|
| 12 | + /** |
|
| 13 | + * @var array<int, int> - four 16-bit integers |
|
| 14 | + */ |
|
| 15 | + public $limbs; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @var int |
|
| 19 | + */ |
|
| 20 | + public $overflow = 0; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * ParagonIE_Sodium_Core32_Int64 constructor. |
|
| 24 | + * @param array $array |
|
| 25 | + */ |
|
| 26 | + public function __construct($array = array(0, 0, 0, 0)) |
|
| 27 | + { |
|
| 28 | + $this->limbs = array( |
|
| 29 | + (int) $array[0], |
|
| 30 | + (int) $array[1], |
|
| 31 | + (int) $array[2], |
|
| 32 | + (int) $array[3] |
|
| 33 | + ); |
|
| 34 | + $this->overflow = 0; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Adds two int64 objects |
|
| 39 | + * |
|
| 40 | + * @param ParagonIE_Sodium_Core32_Int64 $addend |
|
| 41 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 42 | + */ |
|
| 43 | + public function addInt64(ParagonIE_Sodium_Core32_Int64 $addend) |
|
| 44 | + { |
|
| 45 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 46 | + $carry = 0; |
|
| 47 | + for ($i = 3; $i >= 0; --$i) { |
|
| 48 | + $tmp = $this->limbs[$i] + $addend->limbs[$i] + $carry; |
|
| 49 | + $carry = $tmp >> 16; |
|
| 50 | + $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 51 | + } |
|
| 52 | + $return->overflow = $carry; |
|
| 53 | + return $return; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Adds a normal integer to an int64 object |
|
| 58 | + * |
|
| 59 | + * @param int $int |
|
| 60 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 61 | + */ |
|
| 62 | + public function addInt($int) |
|
| 63 | + { |
|
| 64 | + ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 65 | + |
|
| 66 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 67 | + $carry = 0; |
|
| 68 | + for ($i = 3; $i >= 0; --$i) { |
|
| 69 | + $step = (3 - $i) << 4; // 0, 16, 32, 48 |
|
| 70 | + if ($i < 2) { |
|
| 71 | + $toAdd = 0; |
|
| 72 | + } else { |
|
| 73 | + $toAdd = (($int >> $step) & 0xffff); |
|
| 74 | + } |
|
| 75 | + $tmp = $this->limbs[$i] + $toAdd + $carry; |
|
| 76 | + $carry = $tmp >> 16; |
|
| 77 | + $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 78 | + } |
|
| 79 | + $return->overflow = $carry; |
|
| 80 | + return $return; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * @param int $b |
|
| 85 | + * @return int |
|
| 86 | + */ |
|
| 87 | + public function compareInt($b = 0) |
|
| 88 | + { |
|
| 89 | + $gt = 0; |
|
| 90 | + $eq = 1; |
|
| 91 | + |
|
| 92 | + $i = 4; |
|
| 93 | + $j = 0; |
|
| 94 | + while ($i > 0) { |
|
| 95 | + --$i; |
|
| 96 | + $x1 = $this->limbs[$i]; |
|
| 97 | + $x2 = ($b >> ($j << 4)) & 0xffff; |
|
| 98 | + $gt |= (($x2 - $x1) >> 8) & $eq; |
|
| 99 | + $eq &= (($x2 ^ $x1) - 1) >> 8; |
|
| 100 | + } |
|
| 101 | + return ($gt + $gt - $eq) + 1; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @param int $b |
|
| 106 | + * @return bool |
|
| 107 | + */ |
|
| 108 | + public function isGreaterThan($b = 0) |
|
| 109 | + { |
|
| 110 | + return $this->compareInt($b) > 0; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @param int $b |
|
| 115 | + * @return bool |
|
| 116 | + */ |
|
| 117 | + public function isLessThanInt($b = 0) |
|
| 118 | + { |
|
| 119 | + return $this->compareInt($b) < 0; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * @param int $hi |
|
| 125 | + * @param int $lo |
|
| 126 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 127 | + */ |
|
| 128 | + public function mask64($hi = 0, $lo = 0) |
|
| 129 | + { |
|
| 130 | + $a = ($hi >> 16) & 0xffff; |
|
| 131 | + $b = ($hi) & 0xffff; |
|
| 132 | + $c = ($lo >> 16) & 0xffff; |
|
| 133 | + $d = ($lo & 0xffff); |
|
| 134 | + return new ParagonIE_Sodium_Core32_Int64( |
|
| 135 | + array( |
|
| 136 | + $this->limbs[0] & $a, |
|
| 137 | + $this->limbs[1] & $b, |
|
| 138 | + $this->limbs[2] & $c, |
|
| 139 | + $this->limbs[3] & $d |
|
| 140 | + ) |
|
| 141 | + ); |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * @param int $int |
|
| 146 | + * @param int $size |
|
| 147 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 148 | + */ |
|
| 149 | + public function mulInt($int = 0, $size = 0) |
|
| 150 | + { |
|
| 151 | + ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 152 | + ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
| 153 | + if (!$size) { |
|
| 154 | + $size = 63; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $a = clone $this; |
|
| 158 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 159 | + |
|
| 160 | + for ($i = $size; $i >= 0; --$i) { |
|
| 161 | + $return = $return->addInt64( |
|
| 162 | + $a->mask64( |
|
| 163 | + (int) (-($int & 1)), |
|
| 164 | + (int) (-($int & 1)) |
|
| 165 | + ) |
|
| 166 | + ); |
|
| 167 | + $a = $a->shiftLeft(1); |
|
| 168 | + $int >>= 1; |
|
| 169 | + } |
|
| 170 | + return $return; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * @param ParagonIE_Sodium_Core32_Int64 $int |
|
| 175 | + * @param int $size |
|
| 176 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 177 | + */ |
|
| 178 | + public function mulInt64(ParagonIE_Sodium_Core32_Int64 $int, $size = 0) |
|
| 179 | + { |
|
| 180 | + ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
| 181 | + if (!$size) { |
|
| 182 | + $size = 63; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + $a = clone $this; |
|
| 186 | + $b = clone $int; |
|
| 187 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 188 | + |
|
| 189 | + for ($i = $size; $i >= 0; --$i) { |
|
| 190 | + /* |
|
| 191 | 191 | $c += (int) ($a & -($b & 1)); |
| 192 | 192 | $a <<= 1; |
| 193 | 193 | $b >>= 1; |
| 194 | 194 | */ |
| 195 | - $return = $return->addInt64( |
|
| 196 | - $a->mask64( |
|
| 197 | - (int) (-($b->limbs[3] & 1)), |
|
| 198 | - (int) (-($b->limbs[3] & 1)) |
|
| 199 | - ) |
|
| 200 | - ); |
|
| 201 | - $a = $a->shiftLeft(1); |
|
| 202 | - $b = $b->shiftRight(1); |
|
| 203 | - } |
|
| 204 | - return $return; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * OR this 64-bit integer with another. |
|
| 209 | - * |
|
| 210 | - * @param ParagonIE_Sodium_Core32_Int64 $b |
|
| 211 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 212 | - */ |
|
| 213 | - public function orInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
| 214 | - { |
|
| 215 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 216 | - $return->limbs = array( |
|
| 217 | - (int) ($this->limbs[0] | $b->limbs[0]), |
|
| 218 | - (int) ($this->limbs[1] | $b->limbs[1]), |
|
| 219 | - (int) ($this->limbs[2] | $b->limbs[2]), |
|
| 220 | - (int) ($this->limbs[3] | $b->limbs[3]) |
|
| 221 | - ); |
|
| 222 | - return $return; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * @param int $c |
|
| 227 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 228 | - */ |
|
| 229 | - public function rotateLeft($c = 0) |
|
| 230 | - { |
|
| 231 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 232 | - |
|
| 233 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 234 | - $c &= 63; |
|
| 235 | - if ($c === 0) { |
|
| 236 | - // NOP, but we want a copy. |
|
| 237 | - $return->limbs = $this->limbs; |
|
| 238 | - } else { |
|
| 239 | - $idx_shift = ($c >> 4) & 3; |
|
| 240 | - $sub_shift = $c & 15; |
|
| 241 | - |
|
| 242 | - for ($i = 3; $i >= 0; --$i) { |
|
| 243 | - $j = ($i + $idx_shift) & 3; |
|
| 244 | - $k = ($i + $idx_shift + 1) & 3; |
|
| 245 | - $return->limbs[$i] = (int) ( |
|
| 246 | - ( |
|
| 247 | - ($this->limbs[$j] << $sub_shift) |
|
| 248 | - | |
|
| 249 | - ($this->limbs[$k] >> (16 - $sub_shift)) |
|
| 250 | - ) & 0xffff |
|
| 251 | - ); |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - return $return; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Rotate to the right |
|
| 259 | - * |
|
| 260 | - * @param int $c |
|
| 261 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 262 | - */ |
|
| 263 | - public function rotateRight($c = 0) |
|
| 264 | - { |
|
| 265 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 266 | - |
|
| 267 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 268 | - $c &= 63; |
|
| 269 | - if ($c === 0) { |
|
| 270 | - // NOP, but we want a copy. |
|
| 271 | - $return->limbs = $this->limbs; |
|
| 272 | - } else { |
|
| 273 | - $idx_shift = ($c >> 4) & 3; |
|
| 274 | - $sub_shift = $c & 15; |
|
| 275 | - |
|
| 276 | - for ($i = 3; $i >= 0; --$i) { |
|
| 277 | - $j = ($i - $idx_shift) & 3; |
|
| 278 | - $k = ($i - $idx_shift - 1) & 3; |
|
| 279 | - $return->limbs[$i] = (int) ( |
|
| 280 | - ( |
|
| 281 | - ($this->limbs[$j] >> ($sub_shift)) |
|
| 282 | - | |
|
| 283 | - ($this->limbs[$k] << (16 - $sub_shift)) |
|
| 284 | - ) & 0xffff |
|
| 285 | - ); |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - return $return; |
|
| 289 | - } |
|
| 290 | - /** |
|
| 291 | - * @param int $c |
|
| 292 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 293 | - * @throws TypeError |
|
| 294 | - */ |
|
| 295 | - public function shiftLeft($c = 0) |
|
| 296 | - { |
|
| 297 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 298 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 299 | - $c &= 63; |
|
| 300 | - |
|
| 301 | - if ($c >= 16) { |
|
| 302 | - if ($c >= 48) { |
|
| 303 | - $return->limbs = array( |
|
| 304 | - $this->limbs[3], 0, 0, 0 |
|
| 305 | - ); |
|
| 306 | - } elseif ($c >= 32) { |
|
| 307 | - $return->limbs = array( |
|
| 308 | - $this->limbs[2], $this->limbs[3], 0, 0 |
|
| 309 | - ); |
|
| 310 | - } else { |
|
| 311 | - $return->limbs = array( |
|
| 312 | - $this->limbs[1], $this->limbs[2], $this->limbs[3], 0 |
|
| 313 | - ); |
|
| 314 | - } |
|
| 315 | - return $return->shiftLeft($c & 15); |
|
| 316 | - } |
|
| 317 | - if ($c === 0) { |
|
| 318 | - $return->limbs = $this->limbs; |
|
| 319 | - } elseif ($c < 0) { |
|
| 320 | - return $this->shiftRight(-$c); |
|
| 321 | - } else { |
|
| 322 | - if (is_null($c)) { |
|
| 323 | - throw new TypeError(); |
|
| 324 | - } |
|
| 325 | - $carry = 0; |
|
| 326 | - for ($i = 3; $i >= 0; --$i) { |
|
| 327 | - $tmp = ($this->limbs[$i] << $c) | ($carry & 0xffff); |
|
| 328 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 329 | - $carry = $tmp >> 16; |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - return $return; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * @param int $c |
|
| 337 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 338 | - * @throws TypeError |
|
| 339 | - */ |
|
| 340 | - public function shiftRight($c = 0) |
|
| 341 | - { |
|
| 342 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 343 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 344 | - $c &= 63; |
|
| 345 | - |
|
| 346 | - $negative = -(($this->limbs[0] >> 15) & 1); |
|
| 347 | - if ($c >= 16) { |
|
| 348 | - if ($c >= 48) { |
|
| 349 | - $return->limbs = array( |
|
| 350 | - (int) ($negative & 0xffff), |
|
| 351 | - (int) ($negative & 0xffff), |
|
| 352 | - (int) ($negative & 0xffff), |
|
| 353 | - (int) $this->limbs[0] |
|
| 354 | - ); |
|
| 355 | - } elseif ($c >= 32) { |
|
| 356 | - $return->limbs = array( |
|
| 357 | - (int) ($negative & 0xffff), |
|
| 358 | - (int) ($negative & 0xffff), |
|
| 359 | - (int) $this->limbs[0], |
|
| 360 | - (int) $this->limbs[1] |
|
| 361 | - ); |
|
| 362 | - } else { |
|
| 363 | - $return->limbs = array( |
|
| 364 | - (int) ($negative & 0xffff), |
|
| 365 | - (int) $this->limbs[0], |
|
| 366 | - (int) $this->limbs[1], |
|
| 367 | - (int) $this->limbs[2] |
|
| 368 | - ); |
|
| 369 | - } |
|
| 370 | - return $return->shiftRight($c & 15); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - if ($c === 0) { |
|
| 374 | - $return->limbs = $this->limbs; |
|
| 375 | - } elseif ($c < 0) { |
|
| 376 | - return $this->shiftLeft(-$c); |
|
| 377 | - } else { |
|
| 378 | - if (is_null($c)) { |
|
| 379 | - throw new TypeError(); |
|
| 380 | - } |
|
| 381 | - $carryRight = ($negative & 0xffff); |
|
| 382 | - $mask = (int) (((1 << ($c + 1)) - 1) & 0xffff); |
|
| 383 | - for ($i = 0; $i < 4; ++$i) { |
|
| 384 | - $return->limbs[$i] = (int) ( |
|
| 385 | - (($this->limbs[$i] >> $c) | ($carryRight << (16 - $c))) & 0xffff |
|
| 386 | - ); |
|
| 387 | - $carryRight = (int) ($this->limbs[$i] & $mask); |
|
| 388 | - } |
|
| 389 | - } |
|
| 390 | - return $return; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - |
|
| 394 | - /** |
|
| 395 | - * Subtract a normal integer from an int64 object. |
|
| 396 | - * |
|
| 397 | - * @param int $int |
|
| 398 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 399 | - */ |
|
| 400 | - public function subInt($int) |
|
| 401 | - { |
|
| 402 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 403 | - |
|
| 404 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 405 | - |
|
| 406 | - $carry = 0; |
|
| 407 | - for ($i = 3; $i >= 0; --$i) { |
|
| 408 | - $tmp = $this->limbs[$i] - (($int >> 16) & 0xffff) + $carry; |
|
| 409 | - $carry = $tmp >> 16; |
|
| 410 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 411 | - } |
|
| 412 | - return $return; |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * The difference between two Int64 objects. |
|
| 417 | - * |
|
| 418 | - * @param ParagonIE_Sodium_Core32_Int64 $b |
|
| 419 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 420 | - */ |
|
| 421 | - public function subInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
| 422 | - { |
|
| 423 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 424 | - $carry = 0; |
|
| 425 | - for ($i = 3; $i >= 0; --$i) { |
|
| 426 | - $tmp = $this->limbs[$i] - $b->limbs[$i] + $carry; |
|
| 427 | - $carry = ($tmp >> 16); |
|
| 428 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 429 | - |
|
| 430 | - } |
|
| 431 | - return $return; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * XOR this 64-bit integer with another. |
|
| 436 | - * |
|
| 437 | - * @param ParagonIE_Sodium_Core32_Int64 $b |
|
| 438 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 439 | - */ |
|
| 440 | - public function xorInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
| 441 | - { |
|
| 442 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 443 | - $return->limbs = array( |
|
| 444 | - (int) ($this->limbs[0] ^ $b->limbs[0]), |
|
| 445 | - (int) ($this->limbs[1] ^ $b->limbs[1]), |
|
| 446 | - (int) ($this->limbs[2] ^ $b->limbs[2]), |
|
| 447 | - (int) ($this->limbs[3] ^ $b->limbs[3]) |
|
| 448 | - ); |
|
| 449 | - return $return; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * @param int $low |
|
| 454 | - * @param int $high |
|
| 455 | - * @return self |
|
| 456 | - */ |
|
| 457 | - public static function fromInts($low, $high) |
|
| 458 | - { |
|
| 459 | - ParagonIE_Sodium_Core32_Util::declareScalarType($low, 'int', 1); |
|
| 460 | - ParagonIE_Sodium_Core32_Util::declareScalarType($high, 'int', 2); |
|
| 461 | - |
|
| 462 | - return new ParagonIE_Sodium_Core32_Int64( |
|
| 463 | - array( |
|
| 464 | - (int) (($high >> 16) & 0xffff), |
|
| 465 | - (int) ($high & 0xffff), |
|
| 466 | - (int) (($low >> 16) & 0xffff), |
|
| 467 | - (int) ($low & 0xffff) |
|
| 468 | - ) |
|
| 469 | - ); |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * @param string $string |
|
| 474 | - * @return self |
|
| 475 | - */ |
|
| 476 | - public static function fromString($string) |
|
| 477 | - { |
|
| 478 | - ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
| 479 | - $string = (string) $string; |
|
| 480 | - if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { |
|
| 481 | - throw new RangeException( |
|
| 482 | - 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
| 483 | - ); |
|
| 484 | - } |
|
| 485 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 486 | - |
|
| 487 | - $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8); |
|
| 488 | - $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff); |
|
| 489 | - $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8); |
|
| 490 | - $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff); |
|
| 491 | - $return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff) << 8); |
|
| 492 | - $return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff); |
|
| 493 | - $return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff) << 8); |
|
| 494 | - $return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff); |
|
| 495 | - return $return; |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * @param string $string |
|
| 500 | - * @return self |
|
| 501 | - */ |
|
| 502 | - public static function fromReverseString($string) |
|
| 503 | - { |
|
| 504 | - ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
| 505 | - $string = (string) $string; |
|
| 506 | - if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { |
|
| 507 | - throw new RangeException( |
|
| 508 | - 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
| 509 | - ); |
|
| 510 | - } |
|
| 511 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 512 | - |
|
| 513 | - $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff) << 8); |
|
| 514 | - $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff); |
|
| 515 | - $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff) << 8); |
|
| 516 | - $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff); |
|
| 517 | - $return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8); |
|
| 518 | - $return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff); |
|
| 519 | - $return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8); |
|
| 520 | - $return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff); |
|
| 521 | - return $return; |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - /** |
|
| 525 | - * @return array<int, int> |
|
| 526 | - */ |
|
| 527 | - public function toArray() |
|
| 528 | - { |
|
| 529 | - return array( |
|
| 530 | - (int) ((($this->limbs[0] & 0xffff) << 16) | ($this->limbs[1] & 0xffff)), |
|
| 531 | - (int) ((($this->limbs[2] & 0xffff) << 16) | ($this->limbs[3] & 0xffff)) |
|
| 532 | - ); |
|
| 533 | - } |
|
| 534 | - |
|
| 535 | - /** |
|
| 536 | - * @return ParagonIE_Sodium_Core32_Int32 |
|
| 537 | - */ |
|
| 538 | - public function toInt32() |
|
| 539 | - { |
|
| 540 | - $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 541 | - $return->limbs[0] = (int) ($this->limbs[2]); |
|
| 542 | - $return->limbs[1] = (int) ($this->limbs[3]); |
|
| 543 | - return $return; |
|
| 544 | - } |
|
| 545 | - |
|
| 546 | - /** |
|
| 547 | - * @return ParagonIE_Sodium_Core32_Int64 |
|
| 548 | - */ |
|
| 549 | - public function toInt64() |
|
| 550 | - { |
|
| 551 | - $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 552 | - $return->limbs[0] = (int) ($this->limbs[0]); |
|
| 553 | - $return->limbs[1] = (int) ($this->limbs[1]); |
|
| 554 | - $return->limbs[2] = (int) ($this->limbs[2]); |
|
| 555 | - $return->limbs[3] = (int) ($this->limbs[3]); |
|
| 556 | - return $return; |
|
| 557 | - } |
|
| 558 | - |
|
| 559 | - /** |
|
| 560 | - * @return string |
|
| 561 | - */ |
|
| 562 | - public function toString() |
|
| 563 | - { |
|
| 564 | - return ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . |
|
| 565 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
| 566 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
| 567 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
| 568 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) . |
|
| 569 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . |
|
| 570 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . |
|
| 571 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff); |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - /** |
|
| 575 | - * @return string |
|
| 576 | - */ |
|
| 577 | - public function toReverseString() |
|
| 578 | - { |
|
| 579 | - return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff) . |
|
| 580 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . |
|
| 581 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . |
|
| 582 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) . |
|
| 583 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
| 584 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
| 585 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
| 586 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - /** |
|
| 590 | - * @return string |
|
| 591 | - */ |
|
| 592 | - public function __toString() |
|
| 593 | - { |
|
| 594 | - return $this->toString(); |
|
| 595 | - } |
|
| 195 | + $return = $return->addInt64( |
|
| 196 | + $a->mask64( |
|
| 197 | + (int) (-($b->limbs[3] & 1)), |
|
| 198 | + (int) (-($b->limbs[3] & 1)) |
|
| 199 | + ) |
|
| 200 | + ); |
|
| 201 | + $a = $a->shiftLeft(1); |
|
| 202 | + $b = $b->shiftRight(1); |
|
| 203 | + } |
|
| 204 | + return $return; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * OR this 64-bit integer with another. |
|
| 209 | + * |
|
| 210 | + * @param ParagonIE_Sodium_Core32_Int64 $b |
|
| 211 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 212 | + */ |
|
| 213 | + public function orInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
| 214 | + { |
|
| 215 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 216 | + $return->limbs = array( |
|
| 217 | + (int) ($this->limbs[0] | $b->limbs[0]), |
|
| 218 | + (int) ($this->limbs[1] | $b->limbs[1]), |
|
| 219 | + (int) ($this->limbs[2] | $b->limbs[2]), |
|
| 220 | + (int) ($this->limbs[3] | $b->limbs[3]) |
|
| 221 | + ); |
|
| 222 | + return $return; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * @param int $c |
|
| 227 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 228 | + */ |
|
| 229 | + public function rotateLeft($c = 0) |
|
| 230 | + { |
|
| 231 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 232 | + |
|
| 233 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 234 | + $c &= 63; |
|
| 235 | + if ($c === 0) { |
|
| 236 | + // NOP, but we want a copy. |
|
| 237 | + $return->limbs = $this->limbs; |
|
| 238 | + } else { |
|
| 239 | + $idx_shift = ($c >> 4) & 3; |
|
| 240 | + $sub_shift = $c & 15; |
|
| 241 | + |
|
| 242 | + for ($i = 3; $i >= 0; --$i) { |
|
| 243 | + $j = ($i + $idx_shift) & 3; |
|
| 244 | + $k = ($i + $idx_shift + 1) & 3; |
|
| 245 | + $return->limbs[$i] = (int) ( |
|
| 246 | + ( |
|
| 247 | + ($this->limbs[$j] << $sub_shift) |
|
| 248 | + | |
|
| 249 | + ($this->limbs[$k] >> (16 - $sub_shift)) |
|
| 250 | + ) & 0xffff |
|
| 251 | + ); |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + return $return; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Rotate to the right |
|
| 259 | + * |
|
| 260 | + * @param int $c |
|
| 261 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 262 | + */ |
|
| 263 | + public function rotateRight($c = 0) |
|
| 264 | + { |
|
| 265 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 266 | + |
|
| 267 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 268 | + $c &= 63; |
|
| 269 | + if ($c === 0) { |
|
| 270 | + // NOP, but we want a copy. |
|
| 271 | + $return->limbs = $this->limbs; |
|
| 272 | + } else { |
|
| 273 | + $idx_shift = ($c >> 4) & 3; |
|
| 274 | + $sub_shift = $c & 15; |
|
| 275 | + |
|
| 276 | + for ($i = 3; $i >= 0; --$i) { |
|
| 277 | + $j = ($i - $idx_shift) & 3; |
|
| 278 | + $k = ($i - $idx_shift - 1) & 3; |
|
| 279 | + $return->limbs[$i] = (int) ( |
|
| 280 | + ( |
|
| 281 | + ($this->limbs[$j] >> ($sub_shift)) |
|
| 282 | + | |
|
| 283 | + ($this->limbs[$k] << (16 - $sub_shift)) |
|
| 284 | + ) & 0xffff |
|
| 285 | + ); |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + return $return; |
|
| 289 | + } |
|
| 290 | + /** |
|
| 291 | + * @param int $c |
|
| 292 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 293 | + * @throws TypeError |
|
| 294 | + */ |
|
| 295 | + public function shiftLeft($c = 0) |
|
| 296 | + { |
|
| 297 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 298 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 299 | + $c &= 63; |
|
| 300 | + |
|
| 301 | + if ($c >= 16) { |
|
| 302 | + if ($c >= 48) { |
|
| 303 | + $return->limbs = array( |
|
| 304 | + $this->limbs[3], 0, 0, 0 |
|
| 305 | + ); |
|
| 306 | + } elseif ($c >= 32) { |
|
| 307 | + $return->limbs = array( |
|
| 308 | + $this->limbs[2], $this->limbs[3], 0, 0 |
|
| 309 | + ); |
|
| 310 | + } else { |
|
| 311 | + $return->limbs = array( |
|
| 312 | + $this->limbs[1], $this->limbs[2], $this->limbs[3], 0 |
|
| 313 | + ); |
|
| 314 | + } |
|
| 315 | + return $return->shiftLeft($c & 15); |
|
| 316 | + } |
|
| 317 | + if ($c === 0) { |
|
| 318 | + $return->limbs = $this->limbs; |
|
| 319 | + } elseif ($c < 0) { |
|
| 320 | + return $this->shiftRight(-$c); |
|
| 321 | + } else { |
|
| 322 | + if (is_null($c)) { |
|
| 323 | + throw new TypeError(); |
|
| 324 | + } |
|
| 325 | + $carry = 0; |
|
| 326 | + for ($i = 3; $i >= 0; --$i) { |
|
| 327 | + $tmp = ($this->limbs[$i] << $c) | ($carry & 0xffff); |
|
| 328 | + $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 329 | + $carry = $tmp >> 16; |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + return $return; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * @param int $c |
|
| 337 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 338 | + * @throws TypeError |
|
| 339 | + */ |
|
| 340 | + public function shiftRight($c = 0) |
|
| 341 | + { |
|
| 342 | + ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 343 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 344 | + $c &= 63; |
|
| 345 | + |
|
| 346 | + $negative = -(($this->limbs[0] >> 15) & 1); |
|
| 347 | + if ($c >= 16) { |
|
| 348 | + if ($c >= 48) { |
|
| 349 | + $return->limbs = array( |
|
| 350 | + (int) ($negative & 0xffff), |
|
| 351 | + (int) ($negative & 0xffff), |
|
| 352 | + (int) ($negative & 0xffff), |
|
| 353 | + (int) $this->limbs[0] |
|
| 354 | + ); |
|
| 355 | + } elseif ($c >= 32) { |
|
| 356 | + $return->limbs = array( |
|
| 357 | + (int) ($negative & 0xffff), |
|
| 358 | + (int) ($negative & 0xffff), |
|
| 359 | + (int) $this->limbs[0], |
|
| 360 | + (int) $this->limbs[1] |
|
| 361 | + ); |
|
| 362 | + } else { |
|
| 363 | + $return->limbs = array( |
|
| 364 | + (int) ($negative & 0xffff), |
|
| 365 | + (int) $this->limbs[0], |
|
| 366 | + (int) $this->limbs[1], |
|
| 367 | + (int) $this->limbs[2] |
|
| 368 | + ); |
|
| 369 | + } |
|
| 370 | + return $return->shiftRight($c & 15); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + if ($c === 0) { |
|
| 374 | + $return->limbs = $this->limbs; |
|
| 375 | + } elseif ($c < 0) { |
|
| 376 | + return $this->shiftLeft(-$c); |
|
| 377 | + } else { |
|
| 378 | + if (is_null($c)) { |
|
| 379 | + throw new TypeError(); |
|
| 380 | + } |
|
| 381 | + $carryRight = ($negative & 0xffff); |
|
| 382 | + $mask = (int) (((1 << ($c + 1)) - 1) & 0xffff); |
|
| 383 | + for ($i = 0; $i < 4; ++$i) { |
|
| 384 | + $return->limbs[$i] = (int) ( |
|
| 385 | + (($this->limbs[$i] >> $c) | ($carryRight << (16 - $c))) & 0xffff |
|
| 386 | + ); |
|
| 387 | + $carryRight = (int) ($this->limbs[$i] & $mask); |
|
| 388 | + } |
|
| 389 | + } |
|
| 390 | + return $return; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + |
|
| 394 | + /** |
|
| 395 | + * Subtract a normal integer from an int64 object. |
|
| 396 | + * |
|
| 397 | + * @param int $int |
|
| 398 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 399 | + */ |
|
| 400 | + public function subInt($int) |
|
| 401 | + { |
|
| 402 | + ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 403 | + |
|
| 404 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 405 | + |
|
| 406 | + $carry = 0; |
|
| 407 | + for ($i = 3; $i >= 0; --$i) { |
|
| 408 | + $tmp = $this->limbs[$i] - (($int >> 16) & 0xffff) + $carry; |
|
| 409 | + $carry = $tmp >> 16; |
|
| 410 | + $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 411 | + } |
|
| 412 | + return $return; |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * The difference between two Int64 objects. |
|
| 417 | + * |
|
| 418 | + * @param ParagonIE_Sodium_Core32_Int64 $b |
|
| 419 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 420 | + */ |
|
| 421 | + public function subInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
| 422 | + { |
|
| 423 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 424 | + $carry = 0; |
|
| 425 | + for ($i = 3; $i >= 0; --$i) { |
|
| 426 | + $tmp = $this->limbs[$i] - $b->limbs[$i] + $carry; |
|
| 427 | + $carry = ($tmp >> 16); |
|
| 428 | + $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 429 | + |
|
| 430 | + } |
|
| 431 | + return $return; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * XOR this 64-bit integer with another. |
|
| 436 | + * |
|
| 437 | + * @param ParagonIE_Sodium_Core32_Int64 $b |
|
| 438 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 439 | + */ |
|
| 440 | + public function xorInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
| 441 | + { |
|
| 442 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 443 | + $return->limbs = array( |
|
| 444 | + (int) ($this->limbs[0] ^ $b->limbs[0]), |
|
| 445 | + (int) ($this->limbs[1] ^ $b->limbs[1]), |
|
| 446 | + (int) ($this->limbs[2] ^ $b->limbs[2]), |
|
| 447 | + (int) ($this->limbs[3] ^ $b->limbs[3]) |
|
| 448 | + ); |
|
| 449 | + return $return; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * @param int $low |
|
| 454 | + * @param int $high |
|
| 455 | + * @return self |
|
| 456 | + */ |
|
| 457 | + public static function fromInts($low, $high) |
|
| 458 | + { |
|
| 459 | + ParagonIE_Sodium_Core32_Util::declareScalarType($low, 'int', 1); |
|
| 460 | + ParagonIE_Sodium_Core32_Util::declareScalarType($high, 'int', 2); |
|
| 461 | + |
|
| 462 | + return new ParagonIE_Sodium_Core32_Int64( |
|
| 463 | + array( |
|
| 464 | + (int) (($high >> 16) & 0xffff), |
|
| 465 | + (int) ($high & 0xffff), |
|
| 466 | + (int) (($low >> 16) & 0xffff), |
|
| 467 | + (int) ($low & 0xffff) |
|
| 468 | + ) |
|
| 469 | + ); |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * @param string $string |
|
| 474 | + * @return self |
|
| 475 | + */ |
|
| 476 | + public static function fromString($string) |
|
| 477 | + { |
|
| 478 | + ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
| 479 | + $string = (string) $string; |
|
| 480 | + if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { |
|
| 481 | + throw new RangeException( |
|
| 482 | + 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
| 483 | + ); |
|
| 484 | + } |
|
| 485 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 486 | + |
|
| 487 | + $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8); |
|
| 488 | + $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff); |
|
| 489 | + $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8); |
|
| 490 | + $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff); |
|
| 491 | + $return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff) << 8); |
|
| 492 | + $return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff); |
|
| 493 | + $return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff) << 8); |
|
| 494 | + $return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff); |
|
| 495 | + return $return; |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * @param string $string |
|
| 500 | + * @return self |
|
| 501 | + */ |
|
| 502 | + public static function fromReverseString($string) |
|
| 503 | + { |
|
| 504 | + ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
| 505 | + $string = (string) $string; |
|
| 506 | + if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { |
|
| 507 | + throw new RangeException( |
|
| 508 | + 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
| 509 | + ); |
|
| 510 | + } |
|
| 511 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 512 | + |
|
| 513 | + $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff) << 8); |
|
| 514 | + $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff); |
|
| 515 | + $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff) << 8); |
|
| 516 | + $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff); |
|
| 517 | + $return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8); |
|
| 518 | + $return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff); |
|
| 519 | + $return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8); |
|
| 520 | + $return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff); |
|
| 521 | + return $return; |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + /** |
|
| 525 | + * @return array<int, int> |
|
| 526 | + */ |
|
| 527 | + public function toArray() |
|
| 528 | + { |
|
| 529 | + return array( |
|
| 530 | + (int) ((($this->limbs[0] & 0xffff) << 16) | ($this->limbs[1] & 0xffff)), |
|
| 531 | + (int) ((($this->limbs[2] & 0xffff) << 16) | ($this->limbs[3] & 0xffff)) |
|
| 532 | + ); |
|
| 533 | + } |
|
| 534 | + |
|
| 535 | + /** |
|
| 536 | + * @return ParagonIE_Sodium_Core32_Int32 |
|
| 537 | + */ |
|
| 538 | + public function toInt32() |
|
| 539 | + { |
|
| 540 | + $return = new ParagonIE_Sodium_Core32_Int32(); |
|
| 541 | + $return->limbs[0] = (int) ($this->limbs[2]); |
|
| 542 | + $return->limbs[1] = (int) ($this->limbs[3]); |
|
| 543 | + return $return; |
|
| 544 | + } |
|
| 545 | + |
|
| 546 | + /** |
|
| 547 | + * @return ParagonIE_Sodium_Core32_Int64 |
|
| 548 | + */ |
|
| 549 | + public function toInt64() |
|
| 550 | + { |
|
| 551 | + $return = new ParagonIE_Sodium_Core32_Int64(); |
|
| 552 | + $return->limbs[0] = (int) ($this->limbs[0]); |
|
| 553 | + $return->limbs[1] = (int) ($this->limbs[1]); |
|
| 554 | + $return->limbs[2] = (int) ($this->limbs[2]); |
|
| 555 | + $return->limbs[3] = (int) ($this->limbs[3]); |
|
| 556 | + return $return; |
|
| 557 | + } |
|
| 558 | + |
|
| 559 | + /** |
|
| 560 | + * @return string |
|
| 561 | + */ |
|
| 562 | + public function toString() |
|
| 563 | + { |
|
| 564 | + return ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . |
|
| 565 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
| 566 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
| 567 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
| 568 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) . |
|
| 569 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . |
|
| 570 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . |
|
| 571 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff); |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + /** |
|
| 575 | + * @return string |
|
| 576 | + */ |
|
| 577 | + public function toReverseString() |
|
| 578 | + { |
|
| 579 | + return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff) . |
|
| 580 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . |
|
| 581 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . |
|
| 582 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) . |
|
| 583 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
| 584 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
| 585 | + ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
| 586 | + ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + /** |
|
| 590 | + * @return string |
|
| 591 | + */ |
|
| 592 | + public function __toString() |
|
| 593 | + { |
|
| 594 | + return $this->toString(); |
|
| 595 | + } |
|
| 596 | 596 | } |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | * ParagonIE_Sodium_Core32_Int64 constructor. |
| 24 | 24 | * @param array $array |
| 25 | 25 | */ |
| 26 | - public function __construct($array = array(0, 0, 0, 0)) |
|
| 26 | + public function __construct( $array = array( 0, 0, 0, 0 ) ) |
|
| 27 | 27 | { |
| 28 | 28 | $this->limbs = array( |
| 29 | 29 | (int) $array[0], |
@@ -40,14 +40,14 @@ discard block |
||
| 40 | 40 | * @param ParagonIE_Sodium_Core32_Int64 $addend |
| 41 | 41 | * @return ParagonIE_Sodium_Core32_Int64 |
| 42 | 42 | */ |
| 43 | - public function addInt64(ParagonIE_Sodium_Core32_Int64 $addend) |
|
| 43 | + public function addInt64( ParagonIE_Sodium_Core32_Int64 $addend ) |
|
| 44 | 44 | { |
| 45 | 45 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 46 | 46 | $carry = 0; |
| 47 | - for ($i = 3; $i >= 0; --$i) { |
|
| 47 | + for ( $i = 3; $i >= 0; --$i ) { |
|
| 48 | 48 | $tmp = $this->limbs[$i] + $addend->limbs[$i] + $carry; |
| 49 | 49 | $carry = $tmp >> 16; |
| 50 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 50 | + $return->limbs[$i] = (int) ( $tmp & 0xffff ); |
|
| 51 | 51 | } |
| 52 | 52 | $return->overflow = $carry; |
| 53 | 53 | return $return; |
@@ -59,22 +59,22 @@ discard block |
||
| 59 | 59 | * @param int $int |
| 60 | 60 | * @return ParagonIE_Sodium_Core32_Int64 |
| 61 | 61 | */ |
| 62 | - public function addInt($int) |
|
| 62 | + public function addInt( $int ) |
|
| 63 | 63 | { |
| 64 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 64 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $int, 'int', 1 ); |
|
| 65 | 65 | |
| 66 | 66 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 67 | 67 | $carry = 0; |
| 68 | - for ($i = 3; $i >= 0; --$i) { |
|
| 69 | - $step = (3 - $i) << 4; // 0, 16, 32, 48 |
|
| 70 | - if ($i < 2) { |
|
| 68 | + for ( $i = 3; $i >= 0; --$i ) { |
|
| 69 | + $step = ( 3 - $i ) << 4; // 0, 16, 32, 48 |
|
| 70 | + if ( $i < 2 ) { |
|
| 71 | 71 | $toAdd = 0; |
| 72 | 72 | } else { |
| 73 | - $toAdd = (($int >> $step) & 0xffff); |
|
| 73 | + $toAdd = ( ( $int >> $step ) & 0xffff ); |
|
| 74 | 74 | } |
| 75 | 75 | $tmp = $this->limbs[$i] + $toAdd + $carry; |
| 76 | 76 | $carry = $tmp >> 16; |
| 77 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 77 | + $return->limbs[$i] = (int) ( $tmp & 0xffff ); |
|
| 78 | 78 | } |
| 79 | 79 | $return->overflow = $carry; |
| 80 | 80 | return $return; |
@@ -84,39 +84,39 @@ discard block |
||
| 84 | 84 | * @param int $b |
| 85 | 85 | * @return int |
| 86 | 86 | */ |
| 87 | - public function compareInt($b = 0) |
|
| 87 | + public function compareInt( $b = 0 ) |
|
| 88 | 88 | { |
| 89 | 89 | $gt = 0; |
| 90 | 90 | $eq = 1; |
| 91 | 91 | |
| 92 | 92 | $i = 4; |
| 93 | 93 | $j = 0; |
| 94 | - while ($i > 0) { |
|
| 94 | + while ( $i > 0 ) { |
|
| 95 | 95 | --$i; |
| 96 | 96 | $x1 = $this->limbs[$i]; |
| 97 | - $x2 = ($b >> ($j << 4)) & 0xffff; |
|
| 98 | - $gt |= (($x2 - $x1) >> 8) & $eq; |
|
| 99 | - $eq &= (($x2 ^ $x1) - 1) >> 8; |
|
| 97 | + $x2 = ( $b >> ( $j << 4 ) ) & 0xffff; |
|
| 98 | + $gt |= ( ( $x2 - $x1 ) >> 8 ) & $eq; |
|
| 99 | + $eq &= ( ( $x2 ^ $x1 ) - 1 ) >> 8; |
|
| 100 | 100 | } |
| 101 | - return ($gt + $gt - $eq) + 1; |
|
| 101 | + return ( $gt + $gt - $eq ) + 1; |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | /** |
| 105 | 105 | * @param int $b |
| 106 | 106 | * @return bool |
| 107 | 107 | */ |
| 108 | - public function isGreaterThan($b = 0) |
|
| 108 | + public function isGreaterThan( $b = 0 ) |
|
| 109 | 109 | { |
| 110 | - return $this->compareInt($b) > 0; |
|
| 110 | + return $this->compareInt( $b ) > 0; |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | /** |
| 114 | 114 | * @param int $b |
| 115 | 115 | * @return bool |
| 116 | 116 | */ |
| 117 | - public function isLessThanInt($b = 0) |
|
| 117 | + public function isLessThanInt( $b = 0 ) |
|
| 118 | 118 | { |
| 119 | - return $this->compareInt($b) < 0; |
|
| 119 | + return $this->compareInt( $b ) < 0; |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | |
@@ -125,12 +125,12 @@ discard block |
||
| 125 | 125 | * @param int $lo |
| 126 | 126 | * @return ParagonIE_Sodium_Core32_Int64 |
| 127 | 127 | */ |
| 128 | - public function mask64($hi = 0, $lo = 0) |
|
| 128 | + public function mask64( $hi = 0, $lo = 0 ) |
|
| 129 | 129 | { |
| 130 | - $a = ($hi >> 16) & 0xffff; |
|
| 131 | - $b = ($hi) & 0xffff; |
|
| 132 | - $c = ($lo >> 16) & 0xffff; |
|
| 133 | - $d = ($lo & 0xffff); |
|
| 130 | + $a = ( $hi >> 16 ) & 0xffff; |
|
| 131 | + $b = ( $hi ) & 0xffff; |
|
| 132 | + $c = ( $lo >> 16 ) & 0xffff; |
|
| 133 | + $d = ( $lo & 0xffff ); |
|
| 134 | 134 | return new ParagonIE_Sodium_Core32_Int64( |
| 135 | 135 | array( |
| 136 | 136 | $this->limbs[0] & $a, |
@@ -146,25 +146,25 @@ discard block |
||
| 146 | 146 | * @param int $size |
| 147 | 147 | * @return ParagonIE_Sodium_Core32_Int64 |
| 148 | 148 | */ |
| 149 | - public function mulInt($int = 0, $size = 0) |
|
| 149 | + public function mulInt( $int = 0, $size = 0 ) |
|
| 150 | 150 | { |
| 151 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 152 | - ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
| 153 | - if (!$size) { |
|
| 151 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $int, 'int', 1 ); |
|
| 152 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $size, 'int', 2 ); |
|
| 153 | + if ( ! $size ) { |
|
| 154 | 154 | $size = 63; |
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | $a = clone $this; |
| 158 | 158 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 159 | 159 | |
| 160 | - for ($i = $size; $i >= 0; --$i) { |
|
| 160 | + for ( $i = $size; $i >= 0; --$i ) { |
|
| 161 | 161 | $return = $return->addInt64( |
| 162 | 162 | $a->mask64( |
| 163 | - (int) (-($int & 1)), |
|
| 164 | - (int) (-($int & 1)) |
|
| 163 | + (int) (-( $int & 1 )), |
|
| 164 | + (int) (-( $int & 1 )) |
|
| 165 | 165 | ) |
| 166 | 166 | ); |
| 167 | - $a = $a->shiftLeft(1); |
|
| 167 | + $a = $a->shiftLeft( 1 ); |
|
| 168 | 168 | $int >>= 1; |
| 169 | 169 | } |
| 170 | 170 | return $return; |
@@ -175,10 +175,10 @@ discard block |
||
| 175 | 175 | * @param int $size |
| 176 | 176 | * @return ParagonIE_Sodium_Core32_Int64 |
| 177 | 177 | */ |
| 178 | - public function mulInt64(ParagonIE_Sodium_Core32_Int64 $int, $size = 0) |
|
| 178 | + public function mulInt64( ParagonIE_Sodium_Core32_Int64 $int, $size = 0 ) |
|
| 179 | 179 | { |
| 180 | - ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
|
| 181 | - if (!$size) { |
|
| 180 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $size, 'int', 2 ); |
|
| 181 | + if ( ! $size ) { |
|
| 182 | 182 | $size = 63; |
| 183 | 183 | } |
| 184 | 184 | |
@@ -186,7 +186,7 @@ discard block |
||
| 186 | 186 | $b = clone $int; |
| 187 | 187 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 188 | 188 | |
| 189 | - for ($i = $size; $i >= 0; --$i) { |
|
| 189 | + for ( $i = $size; $i >= 0; --$i ) { |
|
| 190 | 190 | /* |
| 191 | 191 | $c += (int) ($a & -($b & 1)); |
| 192 | 192 | $a <<= 1; |
@@ -194,12 +194,12 @@ discard block |
||
| 194 | 194 | */ |
| 195 | 195 | $return = $return->addInt64( |
| 196 | 196 | $a->mask64( |
| 197 | - (int) (-($b->limbs[3] & 1)), |
|
| 198 | - (int) (-($b->limbs[3] & 1)) |
|
| 197 | + (int) (-( $b->limbs[3] & 1 )), |
|
| 198 | + (int) (-( $b->limbs[3] & 1 )) |
|
| 199 | 199 | ) |
| 200 | 200 | ); |
| 201 | - $a = $a->shiftLeft(1); |
|
| 202 | - $b = $b->shiftRight(1); |
|
| 201 | + $a = $a->shiftLeft( 1 ); |
|
| 202 | + $b = $b->shiftRight( 1 ); |
|
| 203 | 203 | } |
| 204 | 204 | return $return; |
| 205 | 205 | } |
@@ -210,14 +210,14 @@ discard block |
||
| 210 | 210 | * @param ParagonIE_Sodium_Core32_Int64 $b |
| 211 | 211 | * @return ParagonIE_Sodium_Core32_Int64 |
| 212 | 212 | */ |
| 213 | - public function orInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
| 213 | + public function orInt64( ParagonIE_Sodium_Core32_Int64 $b ) |
|
| 214 | 214 | { |
| 215 | 215 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 216 | 216 | $return->limbs = array( |
| 217 | - (int) ($this->limbs[0] | $b->limbs[0]), |
|
| 218 | - (int) ($this->limbs[1] | $b->limbs[1]), |
|
| 219 | - (int) ($this->limbs[2] | $b->limbs[2]), |
|
| 220 | - (int) ($this->limbs[3] | $b->limbs[3]) |
|
| 217 | + (int) ( $this->limbs[0] | $b->limbs[0] ), |
|
| 218 | + (int) ( $this->limbs[1] | $b->limbs[1] ), |
|
| 219 | + (int) ( $this->limbs[2] | $b->limbs[2] ), |
|
| 220 | + (int) ( $this->limbs[3] | $b->limbs[3] ) |
|
| 221 | 221 | ); |
| 222 | 222 | return $return; |
| 223 | 223 | } |
@@ -226,27 +226,27 @@ discard block |
||
| 226 | 226 | * @param int $c |
| 227 | 227 | * @return ParagonIE_Sodium_Core32_Int64 |
| 228 | 228 | */ |
| 229 | - public function rotateLeft($c = 0) |
|
| 229 | + public function rotateLeft( $c = 0 ) |
|
| 230 | 230 | { |
| 231 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 231 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $c, 'int', 1 ); |
|
| 232 | 232 | |
| 233 | 233 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 234 | 234 | $c &= 63; |
| 235 | - if ($c === 0) { |
|
| 235 | + if ( $c === 0 ) { |
|
| 236 | 236 | // NOP, but we want a copy. |
| 237 | 237 | $return->limbs = $this->limbs; |
| 238 | 238 | } else { |
| 239 | - $idx_shift = ($c >> 4) & 3; |
|
| 239 | + $idx_shift = ( $c >> 4 ) & 3; |
|
| 240 | 240 | $sub_shift = $c & 15; |
| 241 | 241 | |
| 242 | - for ($i = 3; $i >= 0; --$i) { |
|
| 243 | - $j = ($i + $idx_shift) & 3; |
|
| 244 | - $k = ($i + $idx_shift + 1) & 3; |
|
| 242 | + for ( $i = 3; $i >= 0; --$i ) { |
|
| 243 | + $j = ( $i + $idx_shift ) & 3; |
|
| 244 | + $k = ( $i + $idx_shift + 1 ) & 3; |
|
| 245 | 245 | $return->limbs[$i] = (int) ( |
| 246 | 246 | ( |
| 247 | - ($this->limbs[$j] << $sub_shift) |
|
| 247 | + ( $this->limbs[$j] << $sub_shift ) |
|
| 248 | 248 | | |
| 249 | - ($this->limbs[$k] >> (16 - $sub_shift)) |
|
| 249 | + ( $this->limbs[$k] >> ( 16 - $sub_shift ) ) |
|
| 250 | 250 | ) & 0xffff |
| 251 | 251 | ); |
| 252 | 252 | } |
@@ -260,27 +260,27 @@ discard block |
||
| 260 | 260 | * @param int $c |
| 261 | 261 | * @return ParagonIE_Sodium_Core32_Int64 |
| 262 | 262 | */ |
| 263 | - public function rotateRight($c = 0) |
|
| 263 | + public function rotateRight( $c = 0 ) |
|
| 264 | 264 | { |
| 265 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 265 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $c, 'int', 1 ); |
|
| 266 | 266 | |
| 267 | 267 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 268 | 268 | $c &= 63; |
| 269 | - if ($c === 0) { |
|
| 269 | + if ( $c === 0 ) { |
|
| 270 | 270 | // NOP, but we want a copy. |
| 271 | 271 | $return->limbs = $this->limbs; |
| 272 | 272 | } else { |
| 273 | - $idx_shift = ($c >> 4) & 3; |
|
| 273 | + $idx_shift = ( $c >> 4 ) & 3; |
|
| 274 | 274 | $sub_shift = $c & 15; |
| 275 | 275 | |
| 276 | - for ($i = 3; $i >= 0; --$i) { |
|
| 277 | - $j = ($i - $idx_shift) & 3; |
|
| 278 | - $k = ($i - $idx_shift - 1) & 3; |
|
| 276 | + for ( $i = 3; $i >= 0; --$i ) { |
|
| 277 | + $j = ( $i - $idx_shift ) & 3; |
|
| 278 | + $k = ( $i - $idx_shift - 1 ) & 3; |
|
| 279 | 279 | $return->limbs[$i] = (int) ( |
| 280 | 280 | ( |
| 281 | - ($this->limbs[$j] >> ($sub_shift)) |
|
| 281 | + ( $this->limbs[$j] >> ( $sub_shift ) ) |
|
| 282 | 282 | | |
| 283 | - ($this->limbs[$k] << (16 - $sub_shift)) |
|
| 283 | + ( $this->limbs[$k] << ( 16 - $sub_shift ) ) |
|
| 284 | 284 | ) & 0xffff |
| 285 | 285 | ); |
| 286 | 286 | } |
@@ -292,18 +292,18 @@ discard block |
||
| 292 | 292 | * @return ParagonIE_Sodium_Core32_Int64 |
| 293 | 293 | * @throws TypeError |
| 294 | 294 | */ |
| 295 | - public function shiftLeft($c = 0) |
|
| 295 | + public function shiftLeft( $c = 0 ) |
|
| 296 | 296 | { |
| 297 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 297 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $c, 'int', 1 ); |
|
| 298 | 298 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 299 | 299 | $c &= 63; |
| 300 | 300 | |
| 301 | - if ($c >= 16) { |
|
| 302 | - if ($c >= 48) { |
|
| 301 | + if ( $c >= 16 ) { |
|
| 302 | + if ( $c >= 48 ) { |
|
| 303 | 303 | $return->limbs = array( |
| 304 | 304 | $this->limbs[3], 0, 0, 0 |
| 305 | 305 | ); |
| 306 | - } elseif ($c >= 32) { |
|
| 306 | + } elseif ( $c >= 32 ) { |
|
| 307 | 307 | $return->limbs = array( |
| 308 | 308 | $this->limbs[2], $this->limbs[3], 0, 0 |
| 309 | 309 | ); |
@@ -312,20 +312,20 @@ discard block |
||
| 312 | 312 | $this->limbs[1], $this->limbs[2], $this->limbs[3], 0 |
| 313 | 313 | ); |
| 314 | 314 | } |
| 315 | - return $return->shiftLeft($c & 15); |
|
| 315 | + return $return->shiftLeft( $c & 15 ); |
|
| 316 | 316 | } |
| 317 | - if ($c === 0) { |
|
| 317 | + if ( $c === 0 ) { |
|
| 318 | 318 | $return->limbs = $this->limbs; |
| 319 | - } elseif ($c < 0) { |
|
| 319 | + } elseif ( $c < 0 ) { |
|
| 320 | 320 | return $this->shiftRight(-$c); |
| 321 | 321 | } else { |
| 322 | - if (is_null($c)) { |
|
| 322 | + if ( is_null( $c ) ) { |
|
| 323 | 323 | throw new TypeError(); |
| 324 | 324 | } |
| 325 | 325 | $carry = 0; |
| 326 | - for ($i = 3; $i >= 0; --$i) { |
|
| 327 | - $tmp = ($this->limbs[$i] << $c) | ($carry & 0xffff); |
|
| 328 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 326 | + for ( $i = 3; $i >= 0; --$i ) { |
|
| 327 | + $tmp = ( $this->limbs[$i] << $c ) | ( $carry & 0xffff ); |
|
| 328 | + $return->limbs[$i] = (int) ( $tmp & 0xffff ); |
|
| 329 | 329 | $carry = $tmp >> 16; |
| 330 | 330 | } |
| 331 | 331 | } |
@@ -337,54 +337,54 @@ discard block |
||
| 337 | 337 | * @return ParagonIE_Sodium_Core32_Int64 |
| 338 | 338 | * @throws TypeError |
| 339 | 339 | */ |
| 340 | - public function shiftRight($c = 0) |
|
| 340 | + public function shiftRight( $c = 0 ) |
|
| 341 | 341 | { |
| 342 | - ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
|
| 342 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $c, 'int', 1 ); |
|
| 343 | 343 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 344 | 344 | $c &= 63; |
| 345 | 345 | |
| 346 | - $negative = -(($this->limbs[0] >> 15) & 1); |
|
| 347 | - if ($c >= 16) { |
|
| 348 | - if ($c >= 48) { |
|
| 346 | + $negative = -( ( $this->limbs[0] >> 15 ) & 1 ); |
|
| 347 | + if ( $c >= 16 ) { |
|
| 348 | + if ( $c >= 48 ) { |
|
| 349 | 349 | $return->limbs = array( |
| 350 | - (int) ($negative & 0xffff), |
|
| 351 | - (int) ($negative & 0xffff), |
|
| 352 | - (int) ($negative & 0xffff), |
|
| 350 | + (int) ( $negative & 0xffff ), |
|
| 351 | + (int) ( $negative & 0xffff ), |
|
| 352 | + (int) ( $negative & 0xffff ), |
|
| 353 | 353 | (int) $this->limbs[0] |
| 354 | 354 | ); |
| 355 | - } elseif ($c >= 32) { |
|
| 355 | + } elseif ( $c >= 32 ) { |
|
| 356 | 356 | $return->limbs = array( |
| 357 | - (int) ($negative & 0xffff), |
|
| 358 | - (int) ($negative & 0xffff), |
|
| 357 | + (int) ( $negative & 0xffff ), |
|
| 358 | + (int) ( $negative & 0xffff ), |
|
| 359 | 359 | (int) $this->limbs[0], |
| 360 | 360 | (int) $this->limbs[1] |
| 361 | 361 | ); |
| 362 | 362 | } else { |
| 363 | 363 | $return->limbs = array( |
| 364 | - (int) ($negative & 0xffff), |
|
| 364 | + (int) ( $negative & 0xffff ), |
|
| 365 | 365 | (int) $this->limbs[0], |
| 366 | 366 | (int) $this->limbs[1], |
| 367 | 367 | (int) $this->limbs[2] |
| 368 | 368 | ); |
| 369 | 369 | } |
| 370 | - return $return->shiftRight($c & 15); |
|
| 370 | + return $return->shiftRight( $c & 15 ); |
|
| 371 | 371 | } |
| 372 | 372 | |
| 373 | - if ($c === 0) { |
|
| 373 | + if ( $c === 0 ) { |
|
| 374 | 374 | $return->limbs = $this->limbs; |
| 375 | - } elseif ($c < 0) { |
|
| 375 | + } elseif ( $c < 0 ) { |
|
| 376 | 376 | return $this->shiftLeft(-$c); |
| 377 | 377 | } else { |
| 378 | - if (is_null($c)) { |
|
| 378 | + if ( is_null( $c ) ) { |
|
| 379 | 379 | throw new TypeError(); |
| 380 | 380 | } |
| 381 | - $carryRight = ($negative & 0xffff); |
|
| 382 | - $mask = (int) (((1 << ($c + 1)) - 1) & 0xffff); |
|
| 383 | - for ($i = 0; $i < 4; ++$i) { |
|
| 381 | + $carryRight = ( $negative & 0xffff ); |
|
| 382 | + $mask = (int) ( ( ( 1 << ( $c + 1 ) ) - 1 ) & 0xffff ); |
|
| 383 | + for ( $i = 0; $i < 4; ++$i ) { |
|
| 384 | 384 | $return->limbs[$i] = (int) ( |
| 385 | - (($this->limbs[$i] >> $c) | ($carryRight << (16 - $c))) & 0xffff |
|
| 385 | + ( ( $this->limbs[$i] >> $c ) | ( $carryRight << ( 16 - $c ) ) ) & 0xffff |
|
| 386 | 386 | ); |
| 387 | - $carryRight = (int) ($this->limbs[$i] & $mask); |
|
| 387 | + $carryRight = (int) ( $this->limbs[$i] & $mask ); |
|
| 388 | 388 | } |
| 389 | 389 | } |
| 390 | 390 | return $return; |
@@ -397,17 +397,17 @@ discard block |
||
| 397 | 397 | * @param int $int |
| 398 | 398 | * @return ParagonIE_Sodium_Core32_Int64 |
| 399 | 399 | */ |
| 400 | - public function subInt($int) |
|
| 400 | + public function subInt( $int ) |
|
| 401 | 401 | { |
| 402 | - ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
|
| 402 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $int, 'int', 1 ); |
|
| 403 | 403 | |
| 404 | 404 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 405 | 405 | |
| 406 | 406 | $carry = 0; |
| 407 | - for ($i = 3; $i >= 0; --$i) { |
|
| 408 | - $tmp = $this->limbs[$i] - (($int >> 16) & 0xffff) + $carry; |
|
| 407 | + for ( $i = 3; $i >= 0; --$i ) { |
|
| 408 | + $tmp = $this->limbs[$i] - ( ( $int >> 16 ) & 0xffff ) + $carry; |
|
| 409 | 409 | $carry = $tmp >> 16; |
| 410 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 410 | + $return->limbs[$i] = (int) ( $tmp & 0xffff ); |
|
| 411 | 411 | } |
| 412 | 412 | return $return; |
| 413 | 413 | } |
@@ -418,14 +418,14 @@ discard block |
||
| 418 | 418 | * @param ParagonIE_Sodium_Core32_Int64 $b |
| 419 | 419 | * @return ParagonIE_Sodium_Core32_Int64 |
| 420 | 420 | */ |
| 421 | - public function subInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
| 421 | + public function subInt64( ParagonIE_Sodium_Core32_Int64 $b ) |
|
| 422 | 422 | { |
| 423 | 423 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 424 | 424 | $carry = 0; |
| 425 | - for ($i = 3; $i >= 0; --$i) { |
|
| 425 | + for ( $i = 3; $i >= 0; --$i ) { |
|
| 426 | 426 | $tmp = $this->limbs[$i] - $b->limbs[$i] + $carry; |
| 427 | - $carry = ($tmp >> 16); |
|
| 428 | - $return->limbs[$i] = (int) ($tmp & 0xffff); |
|
| 427 | + $carry = ( $tmp >> 16 ); |
|
| 428 | + $return->limbs[$i] = (int) ( $tmp & 0xffff ); |
|
| 429 | 429 | |
| 430 | 430 | } |
| 431 | 431 | return $return; |
@@ -437,14 +437,14 @@ discard block |
||
| 437 | 437 | * @param ParagonIE_Sodium_Core32_Int64 $b |
| 438 | 438 | * @return ParagonIE_Sodium_Core32_Int64 |
| 439 | 439 | */ |
| 440 | - public function xorInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
| 440 | + public function xorInt64( ParagonIE_Sodium_Core32_Int64 $b ) |
|
| 441 | 441 | { |
| 442 | 442 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 443 | 443 | $return->limbs = array( |
| 444 | - (int) ($this->limbs[0] ^ $b->limbs[0]), |
|
| 445 | - (int) ($this->limbs[1] ^ $b->limbs[1]), |
|
| 446 | - (int) ($this->limbs[2] ^ $b->limbs[2]), |
|
| 447 | - (int) ($this->limbs[3] ^ $b->limbs[3]) |
|
| 444 | + (int) ( $this->limbs[0] ^ $b->limbs[0] ), |
|
| 445 | + (int) ( $this->limbs[1] ^ $b->limbs[1] ), |
|
| 446 | + (int) ( $this->limbs[2] ^ $b->limbs[2] ), |
|
| 447 | + (int) ( $this->limbs[3] ^ $b->limbs[3] ) |
|
| 448 | 448 | ); |
| 449 | 449 | return $return; |
| 450 | 450 | } |
@@ -454,17 +454,17 @@ discard block |
||
| 454 | 454 | * @param int $high |
| 455 | 455 | * @return self |
| 456 | 456 | */ |
| 457 | - public static function fromInts($low, $high) |
|
| 457 | + public static function fromInts( $low, $high ) |
|
| 458 | 458 | { |
| 459 | - ParagonIE_Sodium_Core32_Util::declareScalarType($low, 'int', 1); |
|
| 460 | - ParagonIE_Sodium_Core32_Util::declareScalarType($high, 'int', 2); |
|
| 459 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $low, 'int', 1 ); |
|
| 460 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $high, 'int', 2 ); |
|
| 461 | 461 | |
| 462 | 462 | return new ParagonIE_Sodium_Core32_Int64( |
| 463 | 463 | array( |
| 464 | - (int) (($high >> 16) & 0xffff), |
|
| 465 | - (int) ($high & 0xffff), |
|
| 466 | - (int) (($low >> 16) & 0xffff), |
|
| 467 | - (int) ($low & 0xffff) |
|
| 464 | + (int) ( ( $high >> 16 ) & 0xffff ), |
|
| 465 | + (int) ( $high & 0xffff ), |
|
| 466 | + (int) ( ( $low >> 16 ) & 0xffff ), |
|
| 467 | + (int) ( $low & 0xffff ) |
|
| 468 | 468 | ) |
| 469 | 469 | ); |
| 470 | 470 | } |
@@ -473,25 +473,25 @@ discard block |
||
| 473 | 473 | * @param string $string |
| 474 | 474 | * @return self |
| 475 | 475 | */ |
| 476 | - public static function fromString($string) |
|
| 476 | + public static function fromString( $string ) |
|
| 477 | 477 | { |
| 478 | - ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
| 478 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $string, 'string', 1 ); |
|
| 479 | 479 | $string = (string) $string; |
| 480 | - if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { |
|
| 480 | + if ( ParagonIE_Sodium_Core32_Util::strlen( $string ) !== 8 ) { |
|
| 481 | 481 | throw new RangeException( |
| 482 | - 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
| 482 | + 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen( $string ) . ' given.' |
|
| 483 | 483 | ); |
| 484 | 484 | } |
| 485 | 485 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 486 | 486 | |
| 487 | - $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff) << 8); |
|
| 488 | - $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff); |
|
| 489 | - $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff) << 8); |
|
| 490 | - $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff); |
|
| 491 | - $return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff) << 8); |
|
| 492 | - $return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff); |
|
| 493 | - $return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff) << 8); |
|
| 494 | - $return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff); |
|
| 487 | + $return->limbs[0] = (int) ( ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[0] ) & 0xff ) << 8 ); |
|
| 488 | + $return->limbs[0] |= ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[1] ) & 0xff ); |
|
| 489 | + $return->limbs[1] = (int) ( ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[2] ) & 0xff ) << 8 ); |
|
| 490 | + $return->limbs[1] |= ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[3] ) & 0xff ); |
|
| 491 | + $return->limbs[2] = (int) ( ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[4] ) & 0xff ) << 8 ); |
|
| 492 | + $return->limbs[2] |= ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[5] ) & 0xff ); |
|
| 493 | + $return->limbs[3] = (int) ( ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[6] ) & 0xff ) << 8 ); |
|
| 494 | + $return->limbs[3] |= ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[7] ) & 0xff ); |
|
| 495 | 495 | return $return; |
| 496 | 496 | } |
| 497 | 497 | |
@@ -499,25 +499,25 @@ discard block |
||
| 499 | 499 | * @param string $string |
| 500 | 500 | * @return self |
| 501 | 501 | */ |
| 502 | - public static function fromReverseString($string) |
|
| 502 | + public static function fromReverseString( $string ) |
|
| 503 | 503 | { |
| 504 | - ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
|
| 504 | + ParagonIE_Sodium_Core32_Util::declareScalarType( $string, 'string', 1 ); |
|
| 505 | 505 | $string = (string) $string; |
| 506 | - if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { |
|
| 506 | + if ( ParagonIE_Sodium_Core32_Util::strlen( $string ) !== 8 ) { |
|
| 507 | 507 | throw new RangeException( |
| 508 | - 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen($string) . ' given.' |
|
| 508 | + 'String must be 8 bytes; ' . ParagonIE_Sodium_Core32_Util::strlen( $string ) . ' given.' |
|
| 509 | 509 | ); |
| 510 | 510 | } |
| 511 | 511 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 512 | 512 | |
| 513 | - $return->limbs[0] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[7]) & 0xff) << 8); |
|
| 514 | - $return->limbs[0] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[6]) & 0xff); |
|
| 515 | - $return->limbs[1] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[5]) & 0xff) << 8); |
|
| 516 | - $return->limbs[1] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[4]) & 0xff); |
|
| 517 | - $return->limbs[2] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[3]) & 0xff) << 8); |
|
| 518 | - $return->limbs[2] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[2]) & 0xff); |
|
| 519 | - $return->limbs[3] = (int) ((ParagonIE_Sodium_Core32_Util::chrToInt($string[1]) & 0xff) << 8); |
|
| 520 | - $return->limbs[3] |= (ParagonIE_Sodium_Core32_Util::chrToInt($string[0]) & 0xff); |
|
| 513 | + $return->limbs[0] = (int) ( ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[7] ) & 0xff ) << 8 ); |
|
| 514 | + $return->limbs[0] |= ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[6] ) & 0xff ); |
|
| 515 | + $return->limbs[1] = (int) ( ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[5] ) & 0xff ) << 8 ); |
|
| 516 | + $return->limbs[1] |= ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[4] ) & 0xff ); |
|
| 517 | + $return->limbs[2] = (int) ( ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[3] ) & 0xff ) << 8 ); |
|
| 518 | + $return->limbs[2] |= ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[2] ) & 0xff ); |
|
| 519 | + $return->limbs[3] = (int) ( ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[1] ) & 0xff ) << 8 ); |
|
| 520 | + $return->limbs[3] |= ( ParagonIE_Sodium_Core32_Util::chrToInt( $string[0] ) & 0xff ); |
|
| 521 | 521 | return $return; |
| 522 | 522 | } |
| 523 | 523 | |
@@ -527,8 +527,8 @@ discard block |
||
| 527 | 527 | public function toArray() |
| 528 | 528 | { |
| 529 | 529 | return array( |
| 530 | - (int) ((($this->limbs[0] & 0xffff) << 16) | ($this->limbs[1] & 0xffff)), |
|
| 531 | - (int) ((($this->limbs[2] & 0xffff) << 16) | ($this->limbs[3] & 0xffff)) |
|
| 530 | + (int) ( ( ( $this->limbs[0] & 0xffff ) << 16 ) | ( $this->limbs[1] & 0xffff ) ), |
|
| 531 | + (int) ( ( ( $this->limbs[2] & 0xffff ) << 16 ) | ( $this->limbs[3] & 0xffff ) ) |
|
| 532 | 532 | ); |
| 533 | 533 | } |
| 534 | 534 | |
@@ -538,8 +538,8 @@ discard block |
||
| 538 | 538 | public function toInt32() |
| 539 | 539 | { |
| 540 | 540 | $return = new ParagonIE_Sodium_Core32_Int32(); |
| 541 | - $return->limbs[0] = (int) ($this->limbs[2]); |
|
| 542 | - $return->limbs[1] = (int) ($this->limbs[3]); |
|
| 541 | + $return->limbs[0] = (int) ( $this->limbs[2] ); |
|
| 542 | + $return->limbs[1] = (int) ( $this->limbs[3] ); |
|
| 543 | 543 | return $return; |
| 544 | 544 | } |
| 545 | 545 | |
@@ -549,10 +549,10 @@ discard block |
||
| 549 | 549 | public function toInt64() |
| 550 | 550 | { |
| 551 | 551 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 552 | - $return->limbs[0] = (int) ($this->limbs[0]); |
|
| 553 | - $return->limbs[1] = (int) ($this->limbs[1]); |
|
| 554 | - $return->limbs[2] = (int) ($this->limbs[2]); |
|
| 555 | - $return->limbs[3] = (int) ($this->limbs[3]); |
|
| 552 | + $return->limbs[0] = (int) ( $this->limbs[0] ); |
|
| 553 | + $return->limbs[1] = (int) ( $this->limbs[1] ); |
|
| 554 | + $return->limbs[2] = (int) ( $this->limbs[2] ); |
|
| 555 | + $return->limbs[3] = (int) ( $this->limbs[3] ); |
|
| 556 | 556 | return $return; |
| 557 | 557 | } |
| 558 | 558 | |
@@ -561,14 +561,14 @@ discard block |
||
| 561 | 561 | */ |
| 562 | 562 | public function toString() |
| 563 | 563 | { |
| 564 | - return ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . |
|
| 565 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
| 566 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
| 567 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
| 568 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) . |
|
| 569 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . |
|
| 570 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . |
|
| 571 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff); |
|
| 564 | + return ParagonIE_Sodium_Core32_Util::intToChr( ( $this->limbs[0] >> 8 ) & 0xff ) . |
|
| 565 | + ParagonIE_Sodium_Core32_Util::intToChr( $this->limbs[0] & 0xff ) . |
|
| 566 | + ParagonIE_Sodium_Core32_Util::intToChr( ( $this->limbs[1] >> 8 ) & 0xff ) . |
|
| 567 | + ParagonIE_Sodium_Core32_Util::intToChr( $this->limbs[1] & 0xff ) . |
|
| 568 | + ParagonIE_Sodium_Core32_Util::intToChr( ( $this->limbs[2] >> 8 ) & 0xff ) . |
|
| 569 | + ParagonIE_Sodium_Core32_Util::intToChr( $this->limbs[2] & 0xff ) . |
|
| 570 | + ParagonIE_Sodium_Core32_Util::intToChr( ( $this->limbs[3] >> 8 ) & 0xff ) . |
|
| 571 | + ParagonIE_Sodium_Core32_Util::intToChr( $this->limbs[3] & 0xff ); |
|
| 572 | 572 | } |
| 573 | 573 | |
| 574 | 574 | /** |
@@ -576,14 +576,14 @@ discard block |
||
| 576 | 576 | */ |
| 577 | 577 | public function toReverseString() |
| 578 | 578 | { |
| 579 | - return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff) . |
|
| 580 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . |
|
| 581 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . |
|
| 582 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[2] >> 8) & 0xff) . |
|
| 583 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[1] & 0xff) . |
|
| 584 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
|
| 585 | - ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
|
| 586 | - ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff); |
|
| 579 | + return ParagonIE_Sodium_Core32_Util::intToChr( $this->limbs[3] & 0xff ) . |
|
| 580 | + ParagonIE_Sodium_Core32_Util::intToChr( ( $this->limbs[3] >> 8 ) & 0xff ) . |
|
| 581 | + ParagonIE_Sodium_Core32_Util::intToChr( $this->limbs[2] & 0xff ) . |
|
| 582 | + ParagonIE_Sodium_Core32_Util::intToChr( ( $this->limbs[2] >> 8 ) & 0xff ) . |
|
| 583 | + ParagonIE_Sodium_Core32_Util::intToChr( $this->limbs[1] & 0xff ) . |
|
| 584 | + ParagonIE_Sodium_Core32_Util::intToChr( ( $this->limbs[1] >> 8 ) & 0xff ) . |
|
| 585 | + ParagonIE_Sodium_Core32_Util::intToChr( $this->limbs[0] & 0xff ) . |
|
| 586 | + ParagonIE_Sodium_Core32_Util::intToChr( ( $this->limbs[0] >> 8 ) & 0xff ); |
|
| 587 | 587 | } |
| 588 | 588 | |
| 589 | 589 | /** |
@@ -7,8 +7,7 @@ discard block |
||
| 7 | 7 | * |
| 8 | 8 | * These are immutable. It always returns a new instance. |
| 9 | 9 | */ |
| 10 | -class ParagonIE_Sodium_Core32_Int64 |
|
| 11 | -{ |
|
| 10 | +class ParagonIE_Sodium_Core32_Int64 { |
|
| 12 | 11 | /** |
| 13 | 12 | * @var array<int, int> - four 16-bit integers |
| 14 | 13 | */ |
@@ -23,8 +22,7 @@ discard block |
||
| 23 | 22 | * ParagonIE_Sodium_Core32_Int64 constructor. |
| 24 | 23 | * @param array $array |
| 25 | 24 | */ |
| 26 | - public function __construct($array = array(0, 0, 0, 0)) |
|
| 27 | - { |
|
| 25 | + public function __construct($array = array(0, 0, 0, 0)) { |
|
| 28 | 26 | $this->limbs = array( |
| 29 | 27 | (int) $array[0], |
| 30 | 28 | (int) $array[1], |
@@ -40,8 +38,7 @@ discard block |
||
| 40 | 38 | * @param ParagonIE_Sodium_Core32_Int64 $addend |
| 41 | 39 | * @return ParagonIE_Sodium_Core32_Int64 |
| 42 | 40 | */ |
| 43 | - public function addInt64(ParagonIE_Sodium_Core32_Int64 $addend) |
|
| 44 | - { |
|
| 41 | + public function addInt64(ParagonIE_Sodium_Core32_Int64 $addend) { |
|
| 45 | 42 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 46 | 43 | $carry = 0; |
| 47 | 44 | for ($i = 3; $i >= 0; --$i) { |
@@ -59,8 +56,7 @@ discard block |
||
| 59 | 56 | * @param int $int |
| 60 | 57 | * @return ParagonIE_Sodium_Core32_Int64 |
| 61 | 58 | */ |
| 62 | - public function addInt($int) |
|
| 63 | - { |
|
| 59 | + public function addInt($int) { |
|
| 64 | 60 | ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
| 65 | 61 | |
| 66 | 62 | $return = new ParagonIE_Sodium_Core32_Int64(); |
@@ -84,8 +80,7 @@ discard block |
||
| 84 | 80 | * @param int $b |
| 85 | 81 | * @return int |
| 86 | 82 | */ |
| 87 | - public function compareInt($b = 0) |
|
| 88 | - { |
|
| 83 | + public function compareInt($b = 0) { |
|
| 89 | 84 | $gt = 0; |
| 90 | 85 | $eq = 1; |
| 91 | 86 | |
@@ -105,8 +100,7 @@ discard block |
||
| 105 | 100 | * @param int $b |
| 106 | 101 | * @return bool |
| 107 | 102 | */ |
| 108 | - public function isGreaterThan($b = 0) |
|
| 109 | - { |
|
| 103 | + public function isGreaterThan($b = 0) { |
|
| 110 | 104 | return $this->compareInt($b) > 0; |
| 111 | 105 | } |
| 112 | 106 | |
@@ -114,8 +108,7 @@ discard block |
||
| 114 | 108 | * @param int $b |
| 115 | 109 | * @return bool |
| 116 | 110 | */ |
| 117 | - public function isLessThanInt($b = 0) |
|
| 118 | - { |
|
| 111 | + public function isLessThanInt($b = 0) { |
|
| 119 | 112 | return $this->compareInt($b) < 0; |
| 120 | 113 | } |
| 121 | 114 | |
@@ -125,8 +118,7 @@ discard block |
||
| 125 | 118 | * @param int $lo |
| 126 | 119 | * @return ParagonIE_Sodium_Core32_Int64 |
| 127 | 120 | */ |
| 128 | - public function mask64($hi = 0, $lo = 0) |
|
| 129 | - { |
|
| 121 | + public function mask64($hi = 0, $lo = 0) { |
|
| 130 | 122 | $a = ($hi >> 16) & 0xffff; |
| 131 | 123 | $b = ($hi) & 0xffff; |
| 132 | 124 | $c = ($lo >> 16) & 0xffff; |
@@ -146,8 +138,7 @@ discard block |
||
| 146 | 138 | * @param int $size |
| 147 | 139 | * @return ParagonIE_Sodium_Core32_Int64 |
| 148 | 140 | */ |
| 149 | - public function mulInt($int = 0, $size = 0) |
|
| 150 | - { |
|
| 141 | + public function mulInt($int = 0, $size = 0) { |
|
| 151 | 142 | ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
| 152 | 143 | ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
| 153 | 144 | if (!$size) { |
@@ -175,8 +166,7 @@ discard block |
||
| 175 | 166 | * @param int $size |
| 176 | 167 | * @return ParagonIE_Sodium_Core32_Int64 |
| 177 | 168 | */ |
| 178 | - public function mulInt64(ParagonIE_Sodium_Core32_Int64 $int, $size = 0) |
|
| 179 | - { |
|
| 169 | + public function mulInt64(ParagonIE_Sodium_Core32_Int64 $int, $size = 0) { |
|
| 180 | 170 | ParagonIE_Sodium_Core32_Util::declareScalarType($size, 'int', 2); |
| 181 | 171 | if (!$size) { |
| 182 | 172 | $size = 63; |
@@ -210,8 +200,7 @@ discard block |
||
| 210 | 200 | * @param ParagonIE_Sodium_Core32_Int64 $b |
| 211 | 201 | * @return ParagonIE_Sodium_Core32_Int64 |
| 212 | 202 | */ |
| 213 | - public function orInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
| 214 | - { |
|
| 203 | + public function orInt64(ParagonIE_Sodium_Core32_Int64 $b) { |
|
| 215 | 204 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 216 | 205 | $return->limbs = array( |
| 217 | 206 | (int) ($this->limbs[0] | $b->limbs[0]), |
@@ -226,8 +215,7 @@ discard block |
||
| 226 | 215 | * @param int $c |
| 227 | 216 | * @return ParagonIE_Sodium_Core32_Int64 |
| 228 | 217 | */ |
| 229 | - public function rotateLeft($c = 0) |
|
| 230 | - { |
|
| 218 | + public function rotateLeft($c = 0) { |
|
| 231 | 219 | ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
| 232 | 220 | |
| 233 | 221 | $return = new ParagonIE_Sodium_Core32_Int64(); |
@@ -260,8 +248,7 @@ discard block |
||
| 260 | 248 | * @param int $c |
| 261 | 249 | * @return ParagonIE_Sodium_Core32_Int64 |
| 262 | 250 | */ |
| 263 | - public function rotateRight($c = 0) |
|
| 264 | - { |
|
| 251 | + public function rotateRight($c = 0) { |
|
| 265 | 252 | ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
| 266 | 253 | |
| 267 | 254 | $return = new ParagonIE_Sodium_Core32_Int64(); |
@@ -292,8 +279,7 @@ discard block |
||
| 292 | 279 | * @return ParagonIE_Sodium_Core32_Int64 |
| 293 | 280 | * @throws TypeError |
| 294 | 281 | */ |
| 295 | - public function shiftLeft($c = 0) |
|
| 296 | - { |
|
| 282 | + public function shiftLeft($c = 0) { |
|
| 297 | 283 | ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
| 298 | 284 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 299 | 285 | $c &= 63; |
@@ -337,8 +323,7 @@ discard block |
||
| 337 | 323 | * @return ParagonIE_Sodium_Core32_Int64 |
| 338 | 324 | * @throws TypeError |
| 339 | 325 | */ |
| 340 | - public function shiftRight($c = 0) |
|
| 341 | - { |
|
| 326 | + public function shiftRight($c = 0) { |
|
| 342 | 327 | ParagonIE_Sodium_Core32_Util::declareScalarType($c, 'int', 1); |
| 343 | 328 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 344 | 329 | $c &= 63; |
@@ -397,8 +382,7 @@ discard block |
||
| 397 | 382 | * @param int $int |
| 398 | 383 | * @return ParagonIE_Sodium_Core32_Int64 |
| 399 | 384 | */ |
| 400 | - public function subInt($int) |
|
| 401 | - { |
|
| 385 | + public function subInt($int) { |
|
| 402 | 386 | ParagonIE_Sodium_Core32_Util::declareScalarType($int, 'int', 1); |
| 403 | 387 | |
| 404 | 388 | $return = new ParagonIE_Sodium_Core32_Int64(); |
@@ -418,8 +402,7 @@ discard block |
||
| 418 | 402 | * @param ParagonIE_Sodium_Core32_Int64 $b |
| 419 | 403 | * @return ParagonIE_Sodium_Core32_Int64 |
| 420 | 404 | */ |
| 421 | - public function subInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
| 422 | - { |
|
| 405 | + public function subInt64(ParagonIE_Sodium_Core32_Int64 $b) { |
|
| 423 | 406 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 424 | 407 | $carry = 0; |
| 425 | 408 | for ($i = 3; $i >= 0; --$i) { |
@@ -437,8 +420,7 @@ discard block |
||
| 437 | 420 | * @param ParagonIE_Sodium_Core32_Int64 $b |
| 438 | 421 | * @return ParagonIE_Sodium_Core32_Int64 |
| 439 | 422 | */ |
| 440 | - public function xorInt64(ParagonIE_Sodium_Core32_Int64 $b) |
|
| 441 | - { |
|
| 423 | + public function xorInt64(ParagonIE_Sodium_Core32_Int64 $b) { |
|
| 442 | 424 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 443 | 425 | $return->limbs = array( |
| 444 | 426 | (int) ($this->limbs[0] ^ $b->limbs[0]), |
@@ -454,8 +436,7 @@ discard block |
||
| 454 | 436 | * @param int $high |
| 455 | 437 | * @return self |
| 456 | 438 | */ |
| 457 | - public static function fromInts($low, $high) |
|
| 458 | - { |
|
| 439 | + public static function fromInts($low, $high) { |
|
| 459 | 440 | ParagonIE_Sodium_Core32_Util::declareScalarType($low, 'int', 1); |
| 460 | 441 | ParagonIE_Sodium_Core32_Util::declareScalarType($high, 'int', 2); |
| 461 | 442 | |
@@ -473,8 +454,7 @@ discard block |
||
| 473 | 454 | * @param string $string |
| 474 | 455 | * @return self |
| 475 | 456 | */ |
| 476 | - public static function fromString($string) |
|
| 477 | - { |
|
| 457 | + public static function fromString($string) { |
|
| 478 | 458 | ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
| 479 | 459 | $string = (string) $string; |
| 480 | 460 | if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { |
@@ -499,8 +479,7 @@ discard block |
||
| 499 | 479 | * @param string $string |
| 500 | 480 | * @return self |
| 501 | 481 | */ |
| 502 | - public static function fromReverseString($string) |
|
| 503 | - { |
|
| 482 | + public static function fromReverseString($string) { |
|
| 504 | 483 | ParagonIE_Sodium_Core32_Util::declareScalarType($string, 'string', 1); |
| 505 | 484 | $string = (string) $string; |
| 506 | 485 | if (ParagonIE_Sodium_Core32_Util::strlen($string) !== 8) { |
@@ -524,8 +503,7 @@ discard block |
||
| 524 | 503 | /** |
| 525 | 504 | * @return array<int, int> |
| 526 | 505 | */ |
| 527 | - public function toArray() |
|
| 528 | - { |
|
| 506 | + public function toArray() { |
|
| 529 | 507 | return array( |
| 530 | 508 | (int) ((($this->limbs[0] & 0xffff) << 16) | ($this->limbs[1] & 0xffff)), |
| 531 | 509 | (int) ((($this->limbs[2] & 0xffff) << 16) | ($this->limbs[3] & 0xffff)) |
@@ -535,8 +513,7 @@ discard block |
||
| 535 | 513 | /** |
| 536 | 514 | * @return ParagonIE_Sodium_Core32_Int32 |
| 537 | 515 | */ |
| 538 | - public function toInt32() |
|
| 539 | - { |
|
| 516 | + public function toInt32() { |
|
| 540 | 517 | $return = new ParagonIE_Sodium_Core32_Int32(); |
| 541 | 518 | $return->limbs[0] = (int) ($this->limbs[2]); |
| 542 | 519 | $return->limbs[1] = (int) ($this->limbs[3]); |
@@ -546,8 +523,7 @@ discard block |
||
| 546 | 523 | /** |
| 547 | 524 | * @return ParagonIE_Sodium_Core32_Int64 |
| 548 | 525 | */ |
| 549 | - public function toInt64() |
|
| 550 | - { |
|
| 526 | + public function toInt64() { |
|
| 551 | 527 | $return = new ParagonIE_Sodium_Core32_Int64(); |
| 552 | 528 | $return->limbs[0] = (int) ($this->limbs[0]); |
| 553 | 529 | $return->limbs[1] = (int) ($this->limbs[1]); |
@@ -559,8 +535,7 @@ discard block |
||
| 559 | 535 | /** |
| 560 | 536 | * @return string |
| 561 | 537 | */ |
| 562 | - public function toString() |
|
| 563 | - { |
|
| 538 | + public function toString() { |
|
| 564 | 539 | return ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[0] >> 8) & 0xff) . |
| 565 | 540 | ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[0] & 0xff) . |
| 566 | 541 | ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[1] >> 8) & 0xff) . |
@@ -574,8 +549,7 @@ discard block |
||
| 574 | 549 | /** |
| 575 | 550 | * @return string |
| 576 | 551 | */ |
| 577 | - public function toReverseString() |
|
| 578 | - { |
|
| 552 | + public function toReverseString() { |
|
| 579 | 553 | return ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[3] & 0xff) . |
| 580 | 554 | ParagonIE_Sodium_Core32_Util::intToChr(($this->limbs[3] >> 8) & 0xff) . |
| 581 | 555 | ParagonIE_Sodium_Core32_Util::intToChr($this->limbs[2] & 0xff) . |
@@ -589,8 +563,7 @@ discard block |
||
| 589 | 563 | /** |
| 590 | 564 | * @return string |
| 591 | 565 | */ |
| 592 | - public function __toString() |
|
| 593 | - { |
|
| 566 | + public function __toString() { |
|
| 594 | 567 | return $this->toString(); |
| 595 | 568 | } |
| 596 | 569 | } |
@@ -1024,10 +1024,10 @@ |
||
| 1024 | 1024 | * Update a hash context with the contents of a file, without |
| 1025 | 1025 | * loading the entire file into memory. |
| 1026 | 1026 | * |
| 1027 | - * @param resource|object $hash |
|
| 1027 | + * @param resource $hash |
|
| 1028 | 1028 | * @param resource $fp |
| 1029 | 1029 | * @param int $size |
| 1030 | - * @return mixed (resource on PHP < 7.2, object on PHP >= 7.2) |
|
| 1030 | + * @return resource (resource on PHP < 7.2, object on PHP >= 7.2) |
|
| 1031 | 1031 | * @throws Error |
| 1032 | 1032 | * @throws TypeError |
| 1033 | 1033 | * @psalm-suppress PossiblyInvalidArgument |
@@ -1,1080 +1,1080 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | if (class_exists('ParagonIE_Sodium_File', false)) { |
| 4 | - return; |
|
| 4 | + return; |
|
| 5 | 5 | } |
| 6 | 6 | /** |
| 7 | 7 | * Class ParagonIE_Sodium_File |
| 8 | 8 | */ |
| 9 | 9 | class ParagonIE_Sodium_File extends ParagonIE_Sodium_Core_Util |
| 10 | 10 | { |
| 11 | - /* PHP's default buffer size is 8192 for fread()/fwrite(). */ |
|
| 12 | - const BUFFER_SIZE = 8192; |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * Box a file (rather than a string). Uses less memory than |
|
| 16 | - * ParagonIE_Sodium_Compat::crypto_box(), but produces |
|
| 17 | - * the same result. |
|
| 18 | - * |
|
| 19 | - * @param string $inputFile Absolute path to a file on the filesystem |
|
| 20 | - * @param string $outputFile Absolute path to a file on the filesystem |
|
| 21 | - * @param string $nonce Number to be used only once |
|
| 22 | - * @param string $keyPair ECDH secret key and ECDH public key concatenated |
|
| 23 | - * |
|
| 24 | - * @return bool |
|
| 25 | - * @throws Error |
|
| 26 | - * @throws TypeError |
|
| 27 | - */ |
|
| 28 | - public static function box($inputFile, $outputFile, $nonce, $keyPair) |
|
| 29 | - { |
|
| 30 | - /* Type checks: */ |
|
| 31 | - if (!is_string($inputFile)) { |
|
| 32 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 33 | - } |
|
| 34 | - if (!is_string($outputFile)) { |
|
| 35 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 36 | - } |
|
| 37 | - if (!is_string($nonce)) { |
|
| 38 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /* Input validation: */ |
|
| 42 | - if (!is_string($keyPair)) { |
|
| 43 | - throw new TypeError('Argument 4 must be a string, ' . gettype($keyPair) . ' given.'); |
|
| 44 | - } |
|
| 45 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
| 46 | - throw new TypeError('Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
| 47 | - } |
|
| 48 | - if (self::strlen($keyPair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 49 | - throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** @var int $size */ |
|
| 53 | - $size = filesize($inputFile); |
|
| 54 | - if (!is_int($size)) { |
|
| 55 | - throw new Error('Could not obtain the file size'); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** @var resource $ifp */ |
|
| 59 | - $ifp = fopen($inputFile, 'rb'); |
|
| 60 | - if (!is_resource($ifp)) { |
|
| 61 | - throw new Error('Could not open input file for reading'); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** @var resource $ofp */ |
|
| 65 | - $ofp = fopen($outputFile, 'wb'); |
|
| 66 | - if (!is_resource($ofp)) { |
|
| 67 | - fclose($ifp); |
|
| 68 | - throw new Error('Could not open output file for writing'); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $keyPair); |
|
| 72 | - fclose($ifp); |
|
| 73 | - fclose($ofp); |
|
| 74 | - return $res; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Open a boxed file (rather than a string). Uses less memory than |
|
| 79 | - * ParagonIE_Sodium_Compat::crypto_box_open(), but produces |
|
| 80 | - * the same result. |
|
| 81 | - * |
|
| 82 | - * Warning: Does not protect against TOCTOU attacks. You should |
|
| 83 | - * just load the file into memory and use crypto_box_open() if |
|
| 84 | - * you are worried about those. |
|
| 85 | - * |
|
| 86 | - * @param string $inputFile |
|
| 87 | - * @param string $outputFile |
|
| 88 | - * @param string $nonce |
|
| 89 | - * @param string $keypair |
|
| 90 | - * @return bool |
|
| 91 | - * @throws Error |
|
| 92 | - * @throws TypeError |
|
| 93 | - */ |
|
| 94 | - public static function box_open($inputFile, $outputFile, $nonce, $keypair) |
|
| 95 | - { |
|
| 96 | - /* Type checks: */ |
|
| 97 | - if (!is_string($inputFile)) { |
|
| 98 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 99 | - } |
|
| 100 | - if (!is_string($outputFile)) { |
|
| 101 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 102 | - } |
|
| 103 | - if (!is_string($nonce)) { |
|
| 104 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 105 | - } |
|
| 106 | - if (!is_string($keypair)) { |
|
| 107 | - throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.'); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /* Input validation: */ |
|
| 111 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
| 112 | - throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
| 113 | - } |
|
| 114 | - if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 115 | - throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** @var int $size */ |
|
| 119 | - $size = filesize($inputFile); |
|
| 120 | - if (!is_int($size)) { |
|
| 121 | - throw new Error('Could not obtain the file size'); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** @var resource $ifp */ |
|
| 125 | - $ifp = fopen($inputFile, 'rb'); |
|
| 126 | - if (!is_resource($ifp)) { |
|
| 127 | - throw new Error('Could not open input file for reading'); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** @var resource $ofp */ |
|
| 131 | - $ofp = fopen($outputFile, 'wb'); |
|
| 132 | - if (!is_resource($ofp)) { |
|
| 133 | - fclose($ifp); |
|
| 134 | - throw new Error('Could not open output file for writing'); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair); |
|
| 138 | - fclose($ifp); |
|
| 139 | - fclose($ofp); |
|
| 140 | - try { |
|
| 141 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
| 142 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 143 | - } catch (Error $ex) { |
|
| 144 | - unset($ephKeypair); |
|
| 145 | - } |
|
| 146 | - return $res; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * Seal a file (rather than a string). Uses less memory than |
|
| 151 | - * ParagonIE_Sodium_Compat::crypto_box_seal(), but produces |
|
| 152 | - * the same result. |
|
| 153 | - * |
|
| 154 | - * @param string $inputFile Absolute path to a file on the filesystem |
|
| 155 | - * @param string $outputFile Absolute path to a file on the filesystem |
|
| 156 | - * @param string $publicKey ECDH public key |
|
| 157 | - * |
|
| 158 | - * @return bool |
|
| 159 | - * @throws Error |
|
| 160 | - * @throws TypeError |
|
| 161 | - */ |
|
| 162 | - public static function box_seal($inputFile, $outputFile, $publicKey) |
|
| 163 | - { |
|
| 164 | - /* Type checks: */ |
|
| 165 | - if (!is_string($inputFile)) { |
|
| 166 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 167 | - } |
|
| 168 | - if (!is_string($outputFile)) { |
|
| 169 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 170 | - } |
|
| 171 | - if (!is_string($publicKey)) { |
|
| 172 | - throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /* Input validation: */ |
|
| 176 | - if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
| 177 | - throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes'); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** @var int $size */ |
|
| 181 | - $size = filesize($inputFile); |
|
| 182 | - if (!is_int($size)) { |
|
| 183 | - throw new Error('Could not obtain the file size'); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** @var resource $ifp */ |
|
| 187 | - $ifp = fopen($inputFile, 'rb'); |
|
| 188 | - if (!is_resource($ifp)) { |
|
| 189 | - throw new Error('Could not open input file for reading'); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** @var resource $ofp */ |
|
| 193 | - $ofp = fopen($outputFile, 'wb'); |
|
| 194 | - if (!is_resource($ofp)) { |
|
| 195 | - fclose($ifp); |
|
| 196 | - throw new Error('Could not open output file for writing'); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - /** @var string $ephKeypair */ |
|
| 200 | - $ephKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair(); |
|
| 201 | - |
|
| 202 | - /** @var string $msgKeypair */ |
|
| 203 | - $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( |
|
| 204 | - ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair), |
|
| 205 | - $publicKey |
|
| 206 | - ); |
|
| 207 | - |
|
| 208 | - /** @var string $ephemeralPK */ |
|
| 209 | - $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair); |
|
| 210 | - |
|
| 211 | - /** @var string $nonce */ |
|
| 212 | - $nonce = ParagonIE_Sodium_Compat::crypto_generichash( |
|
| 213 | - $ephemeralPK . $publicKey, |
|
| 214 | - '', |
|
| 215 | - 24 |
|
| 216 | - ); |
|
| 217 | - |
|
| 218 | - /** @var int $firstWrite */ |
|
| 219 | - $firstWrite = fwrite( |
|
| 220 | - $ofp, |
|
| 221 | - $ephemeralPK, |
|
| 222 | - ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES |
|
| 223 | - ); |
|
| 224 | - if (!is_int($firstWrite)) { |
|
| 225 | - fclose($ifp); |
|
| 226 | - fclose($ofp); |
|
| 227 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 228 | - throw new Error('Could not write to output file'); |
|
| 229 | - } |
|
| 230 | - if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
| 231 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 232 | - fclose($ifp); |
|
| 233 | - fclose($ofp); |
|
| 234 | - throw new Error('Error writing public key to output file'); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair); |
|
| 238 | - fclose($ifp); |
|
| 239 | - fclose($ofp); |
|
| 240 | - try { |
|
| 241 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
| 242 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 243 | - } catch (Error $ex) { |
|
| 244 | - unset($ephKeypair); |
|
| 245 | - } |
|
| 246 | - return $res; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Open a sealed file (rather than a string). Uses less memory than |
|
| 251 | - * ParagonIE_Sodium_Compat::crypto_box_seal_open(), but produces |
|
| 252 | - * the same result. |
|
| 253 | - * |
|
| 254 | - * Warning: Does not protect against TOCTOU attacks. You should |
|
| 255 | - * just load the file into memory and use crypto_box_seal_open() if |
|
| 256 | - * you are worried about those. |
|
| 257 | - * |
|
| 258 | - * @param string $inputFile |
|
| 259 | - * @param string $outputFile |
|
| 260 | - * @param string $ecdhKeypair |
|
| 261 | - * @return bool |
|
| 262 | - * @throws Error |
|
| 263 | - * @throws TypeError |
|
| 264 | - */ |
|
| 265 | - public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair) |
|
| 266 | - { |
|
| 267 | - /* Type checks: */ |
|
| 268 | - if (!is_string($inputFile)) { |
|
| 269 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 270 | - } |
|
| 271 | - if (!is_string($outputFile)) { |
|
| 272 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 273 | - } |
|
| 274 | - if (!is_string($ecdhKeypair)) { |
|
| 275 | - throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.'); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /* Input validation: */ |
|
| 279 | - if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 280 | - throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair); |
|
| 284 | - |
|
| 285 | - /** @var int $size */ |
|
| 286 | - $size = filesize($inputFile); |
|
| 287 | - if (!is_int($size)) { |
|
| 288 | - throw new Error('Could not obtain the file size'); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /** @var resource $ifp */ |
|
| 292 | - $ifp = fopen($inputFile, 'rb'); |
|
| 293 | - if (!is_resource($ifp)) { |
|
| 294 | - throw new Error('Could not open input file for reading'); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - /** @var resource $ofp */ |
|
| 298 | - $ofp = fopen($outputFile, 'wb'); |
|
| 299 | - if (!is_resource($ofp)) { |
|
| 300 | - fclose($ifp); |
|
| 301 | - throw new Error('Could not open output file for writing'); |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - $ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES); |
|
| 305 | - if (!is_string($ephemeralPK)) { |
|
| 306 | - throw new Error('Could not read input file'); |
|
| 307 | - } |
|
| 308 | - if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
| 309 | - fclose($ifp); |
|
| 310 | - fclose($ofp); |
|
| 311 | - throw new Error('Could not read public key from sealed file'); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - $nonce = ParagonIE_Sodium_Compat::crypto_generichash( |
|
| 315 | - $ephemeralPK . $publicKey, |
|
| 316 | - '', |
|
| 317 | - 24 |
|
| 318 | - ); |
|
| 319 | - $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( |
|
| 320 | - ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair), |
|
| 321 | - $ephemeralPK |
|
| 322 | - ); |
|
| 323 | - |
|
| 324 | - $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair); |
|
| 325 | - fclose($ifp); |
|
| 326 | - fclose($ofp); |
|
| 327 | - try { |
|
| 328 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
| 329 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 330 | - } catch (Error $ex) { |
|
| 331 | - unset($ephKeypair); |
|
| 332 | - } |
|
| 333 | - return $res; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Calculate the BLAKE2b hash of a file. |
|
| 338 | - * |
|
| 339 | - * @param string $filePath Absolute path to a file on the filesystem |
|
| 340 | - * @param string|null $key BLAKE2b key |
|
| 341 | - * @param int $outputLength Length of hash output |
|
| 342 | - * |
|
| 343 | - * @return string BLAKE2b hash |
|
| 344 | - * @throws Error |
|
| 345 | - * @throws TypeError |
|
| 346 | - * @psalm-suppress FailedTypeResolution |
|
| 347 | - */ |
|
| 348 | - public static function generichash($filePath, $key = '', $outputLength = 32) |
|
| 349 | - { |
|
| 350 | - /* Type checks: */ |
|
| 351 | - if (!is_string($filePath)) { |
|
| 352 | - throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
|
| 353 | - } |
|
| 354 | - if (!is_string($key)) { |
|
| 355 | - if (is_null($key)) { |
|
| 356 | - $key = ''; |
|
| 357 | - } else { |
|
| 358 | - throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.'); |
|
| 359 | - } |
|
| 360 | - } |
|
| 361 | - if (!is_int($outputLength)) { |
|
| 362 | - if (!is_numeric($outputLength)) { |
|
| 363 | - throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.'); |
|
| 364 | - } |
|
| 365 | - $outputLength = (int) $outputLength; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /* Input validation: */ |
|
| 369 | - if (!empty($key)) { |
|
| 370 | - if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) { |
|
| 371 | - throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes'); |
|
| 372 | - } |
|
| 373 | - if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) { |
|
| 374 | - throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes'); |
|
| 375 | - } |
|
| 376 | - } |
|
| 377 | - if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) { |
|
| 378 | - throw new Error('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN'); |
|
| 379 | - } |
|
| 380 | - if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) { |
|
| 381 | - throw new Error('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX'); |
|
| 382 | - } |
|
| 383 | - |
|
| 384 | - /** @var int $size */ |
|
| 385 | - $size = filesize($filePath); |
|
| 386 | - if (!is_int($size)) { |
|
| 387 | - throw new Error('Could not obtain the file size'); |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** @var resource $fp */ |
|
| 391 | - $fp = fopen($filePath, 'rb'); |
|
| 392 | - if (!is_resource($fp)) { |
|
| 393 | - throw new Error('Could not open input file for reading'); |
|
| 394 | - } |
|
| 395 | - $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength); |
|
| 396 | - while ($size > 0) { |
|
| 397 | - $blockSize = $size > 64 |
|
| 398 | - ? 64 |
|
| 399 | - : $size; |
|
| 400 | - $read = fread($fp, $blockSize); |
|
| 401 | - if (!is_string($read)) { |
|
| 402 | - throw new Error('Could not read input file'); |
|
| 403 | - } |
|
| 404 | - ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read); |
|
| 405 | - $size -= $blockSize; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - fclose($fp); |
|
| 409 | - return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - /** |
|
| 413 | - * Encrypt a file (rather than a string). Uses less memory than |
|
| 414 | - * ParagonIE_Sodium_Compat::crypto_secretbox(), but produces |
|
| 415 | - * the same result. |
|
| 416 | - * |
|
| 417 | - * @param string $inputFile Absolute path to a file on the filesystem |
|
| 418 | - * @param string $outputFile Absolute path to a file on the filesystem |
|
| 419 | - * @param string $nonce Number to be used only once |
|
| 420 | - * @param string $key Encryption key |
|
| 421 | - * |
|
| 422 | - * @return bool |
|
| 423 | - * @throws Error |
|
| 424 | - * @throws TypeError |
|
| 425 | - */ |
|
| 426 | - public static function secretbox($inputFile, $outputFile, $nonce, $key) |
|
| 427 | - { |
|
| 428 | - /* Type checks: */ |
|
| 429 | - if (!is_string($inputFile)) { |
|
| 430 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..'); |
|
| 431 | - } |
|
| 432 | - if (!is_string($outputFile)) { |
|
| 433 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 434 | - } |
|
| 435 | - if (!is_string($nonce)) { |
|
| 436 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /* Input validation: */ |
|
| 440 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
| 441 | - throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
| 442 | - } |
|
| 443 | - if (!is_string($key)) { |
|
| 444 | - throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
| 445 | - } |
|
| 446 | - if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
| 447 | - throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes'); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** @var int $size */ |
|
| 451 | - $size = filesize($inputFile); |
|
| 452 | - if (!is_int($size)) { |
|
| 453 | - throw new Error('Could not obtain the file size'); |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** @var resource $ifp */ |
|
| 457 | - $ifp = fopen($inputFile, 'rb'); |
|
| 458 | - if (!is_resource($ifp)) { |
|
| 459 | - throw new Error('Could not open input file for reading'); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** @var resource $ofp */ |
|
| 463 | - $ofp = fopen($outputFile, 'wb'); |
|
| 464 | - if (!is_resource($ofp)) { |
|
| 465 | - fclose($ifp); |
|
| 466 | - throw new Error('Could not open output file for writing'); |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - $res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key); |
|
| 470 | - fclose($ifp); |
|
| 471 | - fclose($ofp); |
|
| 472 | - return $res; |
|
| 473 | - } |
|
| 474 | - /** |
|
| 475 | - * Seal a file (rather than a string). Uses less memory than |
|
| 476 | - * ParagonIE_Sodium_Compat::crypto_secretbox_open(), but produces |
|
| 477 | - * the same result. |
|
| 478 | - * |
|
| 479 | - * Warning: Does not protect against TOCTOU attacks. You should |
|
| 480 | - * just load the file into memory and use crypto_secretbox_open() if |
|
| 481 | - * you are worried about those. |
|
| 482 | - * |
|
| 483 | - * @param string $inputFile |
|
| 484 | - * @param string $outputFile |
|
| 485 | - * @param string $nonce |
|
| 486 | - * @param string $key |
|
| 487 | - * @return bool |
|
| 488 | - * @throws Error |
|
| 489 | - * @throws TypeError |
|
| 490 | - */ |
|
| 491 | - public static function secretbox_open($inputFile, $outputFile, $nonce, $key) |
|
| 492 | - { |
|
| 493 | - /* Type checks: */ |
|
| 494 | - if (!is_string($inputFile)) { |
|
| 495 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 496 | - } |
|
| 497 | - if (!is_string($outputFile)) { |
|
| 498 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 499 | - } |
|
| 500 | - if (!is_string($nonce)) { |
|
| 501 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 502 | - } |
|
| 503 | - if (!is_string($key)) { |
|
| 504 | - throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - /* Input validation: */ |
|
| 508 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
| 509 | - throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
| 510 | - } |
|
| 511 | - if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
| 512 | - throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes'); |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - /** @var int $size */ |
|
| 516 | - $size = filesize($inputFile); |
|
| 517 | - if (!is_int($size)) { |
|
| 518 | - throw new Error('Could not obtain the file size'); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - /** @var resource $ifp */ |
|
| 522 | - $ifp = fopen($inputFile, 'rb'); |
|
| 523 | - if (!is_resource($ifp)) { |
|
| 524 | - throw new Error('Could not open input file for reading'); |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** @var resource $ofp */ |
|
| 528 | - $ofp = fopen($outputFile, 'wb'); |
|
| 529 | - if (!is_resource($ofp)) { |
|
| 530 | - fclose($ifp); |
|
| 531 | - throw new Error('Could not open output file for writing'); |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - $res = self::secretbox_decrypt($ifp, $ofp, $size, $nonce, $key); |
|
| 535 | - fclose($ifp); |
|
| 536 | - fclose($ofp); |
|
| 537 | - try { |
|
| 538 | - ParagonIE_Sodium_Compat::memzero($key); |
|
| 539 | - } catch (Error $ex) { |
|
| 540 | - unset($key); |
|
| 541 | - } |
|
| 542 | - return $res; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Sign a file (rather than a string). Uses less memory than |
|
| 547 | - * ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces |
|
| 548 | - * the same result. |
|
| 549 | - * |
|
| 550 | - * @param string $filePath Absolute path to a file on the filesystem |
|
| 551 | - * @param string $secretKey Secret signing key |
|
| 552 | - * |
|
| 553 | - * @return string Ed25519 signature |
|
| 554 | - * @throws Error |
|
| 555 | - * @throws TypeError |
|
| 556 | - */ |
|
| 557 | - public static function sign($filePath, $secretKey) |
|
| 558 | - { |
|
| 559 | - /* Type checks: */ |
|
| 560 | - if (!is_string($filePath)) { |
|
| 561 | - throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
|
| 562 | - } |
|
| 563 | - if (!is_string($secretKey)) { |
|
| 564 | - throw new TypeError('Argument 2 must be a string, ' . gettype($secretKey) . ' given.'); |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - /* Input validation: */ |
|
| 568 | - if (self::strlen($secretKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES) { |
|
| 569 | - throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes'); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - /** @var int $size */ |
|
| 573 | - $size = filesize($filePath); |
|
| 574 | - if (!is_int($size)) { |
|
| 575 | - throw new Error('Could not obtain the file size'); |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** @var resource $fp */ |
|
| 579 | - $fp = fopen($filePath, 'rb'); |
|
| 580 | - if (!is_resource($fp)) { |
|
| 581 | - throw new Error('Could not open input file for reading'); |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - /** @var string $az */ |
|
| 585 | - $az = hash('sha512', self::substr($secretKey, 0, 32), true); |
|
| 586 | - |
|
| 587 | - $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
| 588 | - $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
| 589 | - |
|
| 590 | - /** @var resource $hs */ |
|
| 591 | - $hs = hash_init('sha512'); |
|
| 592 | - hash_update($hs, self::substr($az, 32, 32)); |
|
| 593 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
| 594 | - |
|
| 595 | - /** @var string $nonceHash */ |
|
| 596 | - $nonceHash = hash_final($hs, true); |
|
| 597 | - |
|
| 598 | - /** @var string $pk */ |
|
| 599 | - $pk = self::substr($secretKey, 32, 32); |
|
| 600 | - |
|
| 601 | - /** @var string $nonce */ |
|
| 602 | - $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
| 603 | - |
|
| 604 | - /** @var string $sig */ |
|
| 605 | - $sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes( |
|
| 606 | - ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce) |
|
| 607 | - ); |
|
| 608 | - |
|
| 609 | - /** @var resource $hs */ |
|
| 610 | - $hs = hash_init('sha512'); |
|
| 611 | - hash_update($hs, self::substr($sig, 0, 32)); |
|
| 612 | - hash_update($hs, self::substr($pk, 0, 32)); |
|
| 613 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
| 614 | - |
|
| 615 | - /** @var string $hramHash */ |
|
| 616 | - $hramHash = hash_final($hs, true); |
|
| 617 | - |
|
| 618 | - /** @var string $hram */ |
|
| 619 | - $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hramHash); |
|
| 620 | - |
|
| 621 | - /** @var string $sigAfter */ |
|
| 622 | - $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd($hram, $az, $nonce); |
|
| 623 | - |
|
| 624 | - /** @var string $sig */ |
|
| 625 | - $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
| 626 | - |
|
| 627 | - try { |
|
| 628 | - ParagonIE_Sodium_Compat::memzero($az); |
|
| 629 | - } catch (Error $ex) { |
|
| 630 | - $az = null; |
|
| 631 | - } |
|
| 632 | - fclose($fp); |
|
| 633 | - return $sig; |
|
| 634 | - } |
|
| 635 | - |
|
| 636 | - /** |
|
| 637 | - * Verify a file (rather than a string). Uses less memory than |
|
| 638 | - * ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but |
|
| 639 | - * produces the same result. |
|
| 640 | - * |
|
| 641 | - * @param string $sig Ed25519 signature |
|
| 642 | - * @param string $filePath Absolute path to a file on the filesystem |
|
| 643 | - * @param string $publicKey Signing public key |
|
| 644 | - * |
|
| 645 | - * @return bool |
|
| 646 | - * @throws Error |
|
| 647 | - * @throws Exception |
|
| 648 | - */ |
|
| 649 | - public static function verify($sig, $filePath, $publicKey) |
|
| 650 | - { |
|
| 651 | - /* Type checks: */ |
|
| 652 | - if (!is_string($sig)) { |
|
| 653 | - throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.'); |
|
| 654 | - } |
|
| 655 | - if (!is_string($filePath)) { |
|
| 656 | - throw new TypeError('Argument 2 must be a string, ' . gettype($filePath) . ' given.'); |
|
| 657 | - } |
|
| 658 | - if (!is_string($publicKey)) { |
|
| 659 | - throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - /* Input validation: */ |
|
| 663 | - if (self::strlen($sig) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES) { |
|
| 664 | - throw new TypeError('Argument 1 must be CRYPTO_SIGN_BYTES bytes'); |
|
| 665 | - } |
|
| 666 | - if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES) { |
|
| 667 | - throw new TypeError('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes'); |
|
| 668 | - } |
|
| 669 | - if (self::strlen($sig) < 64) { |
|
| 670 | - throw new Exception('Signature is too short'); |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - /* Security checks */ |
|
| 674 | - if (ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
| 675 | - throw new Exception('S < L - Invalid signature'); |
|
| 676 | - } |
|
| 677 | - if (ParagonIE_Sodium_Core_Ed25519::small_order($sig)) { |
|
| 678 | - throw new Exception('Signature is on too small of an order'); |
|
| 679 | - } |
|
| 680 | - if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
| 681 | - throw new Exception('Invalid signature'); |
|
| 682 | - } |
|
| 683 | - $d = 0; |
|
| 684 | - for ($i = 0; $i < 32; ++$i) { |
|
| 685 | - $d |= self::chrToInt($publicKey[$i]); |
|
| 686 | - } |
|
| 687 | - if ($d === 0) { |
|
| 688 | - throw new Exception('All zero public key'); |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - /** @var int $size */ |
|
| 692 | - $size = filesize($filePath); |
|
| 693 | - if (!is_int($size)) { |
|
| 694 | - throw new Error('Could not obtain the file size'); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - /** @var resource $fp */ |
|
| 698 | - $fp = fopen($filePath, 'rb'); |
|
| 699 | - if (!is_resource($fp)) { |
|
| 700 | - throw new Error('Could not open input file for reading'); |
|
| 701 | - } |
|
| 702 | - |
|
| 703 | - /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
|
| 704 | - $orig = ParagonIE_Sodium_Compat::$fastMult; |
|
| 705 | - |
|
| 706 | - // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. |
|
| 707 | - ParagonIE_Sodium_Compat::$fastMult = true; |
|
| 708 | - |
|
| 709 | - /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ |
|
| 710 | - $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey); |
|
| 711 | - |
|
| 712 | - /** @var resource $hs */ |
|
| 713 | - $hs = hash_init('sha512'); |
|
| 714 | - hash_update($hs, self::substr($sig, 0, 32)); |
|
| 715 | - hash_update($hs, self::substr($publicKey, 0, 32)); |
|
| 716 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
| 717 | - /** @var string $hDigest */ |
|
| 718 | - $hDigest = hash_final($hs, true); |
|
| 719 | - |
|
| 720 | - /** @var string $h */ |
|
| 721 | - $h = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
| 722 | - |
|
| 723 | - /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */ |
|
| 724 | - $R = ParagonIE_Sodium_Core_Ed25519::ge_double_scalarmult_vartime( |
|
| 725 | - $h, |
|
| 726 | - $A, |
|
| 727 | - self::substr($sig, 32) |
|
| 728 | - ); |
|
| 729 | - |
|
| 730 | - /** @var string $rcheck */ |
|
| 731 | - $rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes($R); |
|
| 732 | - |
|
| 733 | - // Close the file handle |
|
| 734 | - fclose($fp); |
|
| 735 | - |
|
| 736 | - // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
|
| 737 | - ParagonIE_Sodium_Compat::$fastMult = $orig; |
|
| 738 | - return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - /** |
|
| 742 | - * @param resource $ifp |
|
| 743 | - * @param resource $ofp |
|
| 744 | - * @param int $mlen |
|
| 745 | - * @param string $nonce |
|
| 746 | - * @param string $boxKeypair |
|
| 747 | - * @return bool |
|
| 748 | - */ |
|
| 749 | - protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
| 750 | - { |
|
| 751 | - return self::secretbox_encrypt( |
|
| 752 | - $ifp, |
|
| 753 | - $ofp, |
|
| 754 | - $mlen, |
|
| 755 | - $nonce, |
|
| 756 | - ParagonIE_Sodium_Crypto::box_beforenm( |
|
| 757 | - ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), |
|
| 758 | - ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) |
|
| 759 | - ) |
|
| 760 | - ); |
|
| 761 | - } |
|
| 762 | - |
|
| 763 | - |
|
| 764 | - /** |
|
| 765 | - * @param resource $ifp |
|
| 766 | - * @param resource $ofp |
|
| 767 | - * @param int $mlen |
|
| 768 | - * @param string $nonce |
|
| 769 | - * @param string $boxKeypair |
|
| 770 | - * @return bool |
|
| 771 | - */ |
|
| 772 | - protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
| 773 | - { |
|
| 774 | - return self::secretbox_decrypt( |
|
| 775 | - $ifp, |
|
| 776 | - $ofp, |
|
| 777 | - $mlen, |
|
| 778 | - $nonce, |
|
| 779 | - ParagonIE_Sodium_Crypto::box_beforenm( |
|
| 780 | - ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), |
|
| 781 | - ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) |
|
| 782 | - ) |
|
| 783 | - ); |
|
| 784 | - } |
|
| 785 | - |
|
| 786 | - /** |
|
| 787 | - * Encrypt a file |
|
| 788 | - * |
|
| 789 | - * @param resource $ifp |
|
| 790 | - * @param resource $ofp |
|
| 791 | - * @param int $mlen |
|
| 792 | - * @param string $nonce |
|
| 793 | - * @param string $key |
|
| 794 | - * @return bool |
|
| 795 | - * @throws Error |
|
| 796 | - */ |
|
| 797 | - protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
| 798 | - { |
|
| 799 | - $plaintext = fread($ifp, 32); |
|
| 800 | - if (!is_string($plaintext)) { |
|
| 801 | - throw new Error('Could not read input file'); |
|
| 802 | - } |
|
| 803 | - $first32 = ftell($ifp); |
|
| 804 | - |
|
| 805 | - /** @var string $subkey */ |
|
| 806 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
| 807 | - |
|
| 808 | - /** @var string $realNonce */ |
|
| 809 | - $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
| 810 | - |
|
| 811 | - /** @var string $block0 */ |
|
| 812 | - $block0 = str_repeat("\x00", 32); |
|
| 813 | - |
|
| 814 | - /** @var int $mlen - Length of the plaintext message */ |
|
| 815 | - $mlen0 = $mlen; |
|
| 816 | - if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
| 817 | - $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES; |
|
| 818 | - } |
|
| 819 | - $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
| 820 | - |
|
| 821 | - /** @var string $block0 */ |
|
| 822 | - $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
|
| 823 | - $block0, |
|
| 824 | - $realNonce, |
|
| 825 | - $subkey |
|
| 826 | - ); |
|
| 827 | - |
|
| 828 | - $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
| 829 | - ParagonIE_Sodium_Core_Util::substr( |
|
| 830 | - $block0, |
|
| 831 | - 0, |
|
| 832 | - ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES |
|
| 833 | - ) |
|
| 834 | - ); |
|
| 835 | - |
|
| 836 | - // Pre-write 16 blank bytes for the Poly1305 tag |
|
| 837 | - $start = ftell($ofp); |
|
| 838 | - fwrite($ofp, str_repeat("\x00", 16)); |
|
| 839 | - |
|
| 840 | - /** @var string $c */ |
|
| 841 | - $cBlock = ParagonIE_Sodium_Core_Util::substr( |
|
| 842 | - $block0, |
|
| 843 | - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES |
|
| 844 | - ); |
|
| 845 | - $state->update($cBlock); |
|
| 846 | - fwrite($ofp, $cBlock); |
|
| 847 | - $mlen -= 32; |
|
| 848 | - |
|
| 849 | - /** @var int $iter */ |
|
| 850 | - $iter = 1; |
|
| 851 | - |
|
| 852 | - /** @var int $incr */ |
|
| 853 | - $incr = self::BUFFER_SIZE >> 6; |
|
| 854 | - |
|
| 855 | - /* |
|
| 11 | + /* PHP's default buffer size is 8192 for fread()/fwrite(). */ |
|
| 12 | + const BUFFER_SIZE = 8192; |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * Box a file (rather than a string). Uses less memory than |
|
| 16 | + * ParagonIE_Sodium_Compat::crypto_box(), but produces |
|
| 17 | + * the same result. |
|
| 18 | + * |
|
| 19 | + * @param string $inputFile Absolute path to a file on the filesystem |
|
| 20 | + * @param string $outputFile Absolute path to a file on the filesystem |
|
| 21 | + * @param string $nonce Number to be used only once |
|
| 22 | + * @param string $keyPair ECDH secret key and ECDH public key concatenated |
|
| 23 | + * |
|
| 24 | + * @return bool |
|
| 25 | + * @throws Error |
|
| 26 | + * @throws TypeError |
|
| 27 | + */ |
|
| 28 | + public static function box($inputFile, $outputFile, $nonce, $keyPair) |
|
| 29 | + { |
|
| 30 | + /* Type checks: */ |
|
| 31 | + if (!is_string($inputFile)) { |
|
| 32 | + throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 33 | + } |
|
| 34 | + if (!is_string($outputFile)) { |
|
| 35 | + throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 36 | + } |
|
| 37 | + if (!is_string($nonce)) { |
|
| 38 | + throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /* Input validation: */ |
|
| 42 | + if (!is_string($keyPair)) { |
|
| 43 | + throw new TypeError('Argument 4 must be a string, ' . gettype($keyPair) . ' given.'); |
|
| 44 | + } |
|
| 45 | + if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
| 46 | + throw new TypeError('Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
| 47 | + } |
|
| 48 | + if (self::strlen($keyPair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 49 | + throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** @var int $size */ |
|
| 53 | + $size = filesize($inputFile); |
|
| 54 | + if (!is_int($size)) { |
|
| 55 | + throw new Error('Could not obtain the file size'); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** @var resource $ifp */ |
|
| 59 | + $ifp = fopen($inputFile, 'rb'); |
|
| 60 | + if (!is_resource($ifp)) { |
|
| 61 | + throw new Error('Could not open input file for reading'); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** @var resource $ofp */ |
|
| 65 | + $ofp = fopen($outputFile, 'wb'); |
|
| 66 | + if (!is_resource($ofp)) { |
|
| 67 | + fclose($ifp); |
|
| 68 | + throw new Error('Could not open output file for writing'); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $keyPair); |
|
| 72 | + fclose($ifp); |
|
| 73 | + fclose($ofp); |
|
| 74 | + return $res; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Open a boxed file (rather than a string). Uses less memory than |
|
| 79 | + * ParagonIE_Sodium_Compat::crypto_box_open(), but produces |
|
| 80 | + * the same result. |
|
| 81 | + * |
|
| 82 | + * Warning: Does not protect against TOCTOU attacks. You should |
|
| 83 | + * just load the file into memory and use crypto_box_open() if |
|
| 84 | + * you are worried about those. |
|
| 85 | + * |
|
| 86 | + * @param string $inputFile |
|
| 87 | + * @param string $outputFile |
|
| 88 | + * @param string $nonce |
|
| 89 | + * @param string $keypair |
|
| 90 | + * @return bool |
|
| 91 | + * @throws Error |
|
| 92 | + * @throws TypeError |
|
| 93 | + */ |
|
| 94 | + public static function box_open($inputFile, $outputFile, $nonce, $keypair) |
|
| 95 | + { |
|
| 96 | + /* Type checks: */ |
|
| 97 | + if (!is_string($inputFile)) { |
|
| 98 | + throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 99 | + } |
|
| 100 | + if (!is_string($outputFile)) { |
|
| 101 | + throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 102 | + } |
|
| 103 | + if (!is_string($nonce)) { |
|
| 104 | + throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 105 | + } |
|
| 106 | + if (!is_string($keypair)) { |
|
| 107 | + throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.'); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /* Input validation: */ |
|
| 111 | + if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
| 112 | + throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
| 113 | + } |
|
| 114 | + if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 115 | + throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** @var int $size */ |
|
| 119 | + $size = filesize($inputFile); |
|
| 120 | + if (!is_int($size)) { |
|
| 121 | + throw new Error('Could not obtain the file size'); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** @var resource $ifp */ |
|
| 125 | + $ifp = fopen($inputFile, 'rb'); |
|
| 126 | + if (!is_resource($ifp)) { |
|
| 127 | + throw new Error('Could not open input file for reading'); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** @var resource $ofp */ |
|
| 131 | + $ofp = fopen($outputFile, 'wb'); |
|
| 132 | + if (!is_resource($ofp)) { |
|
| 133 | + fclose($ifp); |
|
| 134 | + throw new Error('Could not open output file for writing'); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair); |
|
| 138 | + fclose($ifp); |
|
| 139 | + fclose($ofp); |
|
| 140 | + try { |
|
| 141 | + ParagonIE_Sodium_Compat::memzero($nonce); |
|
| 142 | + ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 143 | + } catch (Error $ex) { |
|
| 144 | + unset($ephKeypair); |
|
| 145 | + } |
|
| 146 | + return $res; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * Seal a file (rather than a string). Uses less memory than |
|
| 151 | + * ParagonIE_Sodium_Compat::crypto_box_seal(), but produces |
|
| 152 | + * the same result. |
|
| 153 | + * |
|
| 154 | + * @param string $inputFile Absolute path to a file on the filesystem |
|
| 155 | + * @param string $outputFile Absolute path to a file on the filesystem |
|
| 156 | + * @param string $publicKey ECDH public key |
|
| 157 | + * |
|
| 158 | + * @return bool |
|
| 159 | + * @throws Error |
|
| 160 | + * @throws TypeError |
|
| 161 | + */ |
|
| 162 | + public static function box_seal($inputFile, $outputFile, $publicKey) |
|
| 163 | + { |
|
| 164 | + /* Type checks: */ |
|
| 165 | + if (!is_string($inputFile)) { |
|
| 166 | + throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 167 | + } |
|
| 168 | + if (!is_string($outputFile)) { |
|
| 169 | + throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 170 | + } |
|
| 171 | + if (!is_string($publicKey)) { |
|
| 172 | + throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /* Input validation: */ |
|
| 176 | + if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
| 177 | + throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes'); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** @var int $size */ |
|
| 181 | + $size = filesize($inputFile); |
|
| 182 | + if (!is_int($size)) { |
|
| 183 | + throw new Error('Could not obtain the file size'); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** @var resource $ifp */ |
|
| 187 | + $ifp = fopen($inputFile, 'rb'); |
|
| 188 | + if (!is_resource($ifp)) { |
|
| 189 | + throw new Error('Could not open input file for reading'); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** @var resource $ofp */ |
|
| 193 | + $ofp = fopen($outputFile, 'wb'); |
|
| 194 | + if (!is_resource($ofp)) { |
|
| 195 | + fclose($ifp); |
|
| 196 | + throw new Error('Could not open output file for writing'); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + /** @var string $ephKeypair */ |
|
| 200 | + $ephKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair(); |
|
| 201 | + |
|
| 202 | + /** @var string $msgKeypair */ |
|
| 203 | + $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( |
|
| 204 | + ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair), |
|
| 205 | + $publicKey |
|
| 206 | + ); |
|
| 207 | + |
|
| 208 | + /** @var string $ephemeralPK */ |
|
| 209 | + $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair); |
|
| 210 | + |
|
| 211 | + /** @var string $nonce */ |
|
| 212 | + $nonce = ParagonIE_Sodium_Compat::crypto_generichash( |
|
| 213 | + $ephemeralPK . $publicKey, |
|
| 214 | + '', |
|
| 215 | + 24 |
|
| 216 | + ); |
|
| 217 | + |
|
| 218 | + /** @var int $firstWrite */ |
|
| 219 | + $firstWrite = fwrite( |
|
| 220 | + $ofp, |
|
| 221 | + $ephemeralPK, |
|
| 222 | + ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES |
|
| 223 | + ); |
|
| 224 | + if (!is_int($firstWrite)) { |
|
| 225 | + fclose($ifp); |
|
| 226 | + fclose($ofp); |
|
| 227 | + ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 228 | + throw new Error('Could not write to output file'); |
|
| 229 | + } |
|
| 230 | + if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
| 231 | + ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 232 | + fclose($ifp); |
|
| 233 | + fclose($ofp); |
|
| 234 | + throw new Error('Error writing public key to output file'); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair); |
|
| 238 | + fclose($ifp); |
|
| 239 | + fclose($ofp); |
|
| 240 | + try { |
|
| 241 | + ParagonIE_Sodium_Compat::memzero($nonce); |
|
| 242 | + ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 243 | + } catch (Error $ex) { |
|
| 244 | + unset($ephKeypair); |
|
| 245 | + } |
|
| 246 | + return $res; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Open a sealed file (rather than a string). Uses less memory than |
|
| 251 | + * ParagonIE_Sodium_Compat::crypto_box_seal_open(), but produces |
|
| 252 | + * the same result. |
|
| 253 | + * |
|
| 254 | + * Warning: Does not protect against TOCTOU attacks. You should |
|
| 255 | + * just load the file into memory and use crypto_box_seal_open() if |
|
| 256 | + * you are worried about those. |
|
| 257 | + * |
|
| 258 | + * @param string $inputFile |
|
| 259 | + * @param string $outputFile |
|
| 260 | + * @param string $ecdhKeypair |
|
| 261 | + * @return bool |
|
| 262 | + * @throws Error |
|
| 263 | + * @throws TypeError |
|
| 264 | + */ |
|
| 265 | + public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair) |
|
| 266 | + { |
|
| 267 | + /* Type checks: */ |
|
| 268 | + if (!is_string($inputFile)) { |
|
| 269 | + throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 270 | + } |
|
| 271 | + if (!is_string($outputFile)) { |
|
| 272 | + throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 273 | + } |
|
| 274 | + if (!is_string($ecdhKeypair)) { |
|
| 275 | + throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.'); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /* Input validation: */ |
|
| 279 | + if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 280 | + throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair); |
|
| 284 | + |
|
| 285 | + /** @var int $size */ |
|
| 286 | + $size = filesize($inputFile); |
|
| 287 | + if (!is_int($size)) { |
|
| 288 | + throw new Error('Could not obtain the file size'); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /** @var resource $ifp */ |
|
| 292 | + $ifp = fopen($inputFile, 'rb'); |
|
| 293 | + if (!is_resource($ifp)) { |
|
| 294 | + throw new Error('Could not open input file for reading'); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + /** @var resource $ofp */ |
|
| 298 | + $ofp = fopen($outputFile, 'wb'); |
|
| 299 | + if (!is_resource($ofp)) { |
|
| 300 | + fclose($ifp); |
|
| 301 | + throw new Error('Could not open output file for writing'); |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + $ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES); |
|
| 305 | + if (!is_string($ephemeralPK)) { |
|
| 306 | + throw new Error('Could not read input file'); |
|
| 307 | + } |
|
| 308 | + if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
| 309 | + fclose($ifp); |
|
| 310 | + fclose($ofp); |
|
| 311 | + throw new Error('Could not read public key from sealed file'); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + $nonce = ParagonIE_Sodium_Compat::crypto_generichash( |
|
| 315 | + $ephemeralPK . $publicKey, |
|
| 316 | + '', |
|
| 317 | + 24 |
|
| 318 | + ); |
|
| 319 | + $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( |
|
| 320 | + ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair), |
|
| 321 | + $ephemeralPK |
|
| 322 | + ); |
|
| 323 | + |
|
| 324 | + $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair); |
|
| 325 | + fclose($ifp); |
|
| 326 | + fclose($ofp); |
|
| 327 | + try { |
|
| 328 | + ParagonIE_Sodium_Compat::memzero($nonce); |
|
| 329 | + ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 330 | + } catch (Error $ex) { |
|
| 331 | + unset($ephKeypair); |
|
| 332 | + } |
|
| 333 | + return $res; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Calculate the BLAKE2b hash of a file. |
|
| 338 | + * |
|
| 339 | + * @param string $filePath Absolute path to a file on the filesystem |
|
| 340 | + * @param string|null $key BLAKE2b key |
|
| 341 | + * @param int $outputLength Length of hash output |
|
| 342 | + * |
|
| 343 | + * @return string BLAKE2b hash |
|
| 344 | + * @throws Error |
|
| 345 | + * @throws TypeError |
|
| 346 | + * @psalm-suppress FailedTypeResolution |
|
| 347 | + */ |
|
| 348 | + public static function generichash($filePath, $key = '', $outputLength = 32) |
|
| 349 | + { |
|
| 350 | + /* Type checks: */ |
|
| 351 | + if (!is_string($filePath)) { |
|
| 352 | + throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
|
| 353 | + } |
|
| 354 | + if (!is_string($key)) { |
|
| 355 | + if (is_null($key)) { |
|
| 356 | + $key = ''; |
|
| 357 | + } else { |
|
| 358 | + throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.'); |
|
| 359 | + } |
|
| 360 | + } |
|
| 361 | + if (!is_int($outputLength)) { |
|
| 362 | + if (!is_numeric($outputLength)) { |
|
| 363 | + throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.'); |
|
| 364 | + } |
|
| 365 | + $outputLength = (int) $outputLength; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /* Input validation: */ |
|
| 369 | + if (!empty($key)) { |
|
| 370 | + if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) { |
|
| 371 | + throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes'); |
|
| 372 | + } |
|
| 373 | + if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) { |
|
| 374 | + throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes'); |
|
| 375 | + } |
|
| 376 | + } |
|
| 377 | + if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) { |
|
| 378 | + throw new Error('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN'); |
|
| 379 | + } |
|
| 380 | + if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) { |
|
| 381 | + throw new Error('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX'); |
|
| 382 | + } |
|
| 383 | + |
|
| 384 | + /** @var int $size */ |
|
| 385 | + $size = filesize($filePath); |
|
| 386 | + if (!is_int($size)) { |
|
| 387 | + throw new Error('Could not obtain the file size'); |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** @var resource $fp */ |
|
| 391 | + $fp = fopen($filePath, 'rb'); |
|
| 392 | + if (!is_resource($fp)) { |
|
| 393 | + throw new Error('Could not open input file for reading'); |
|
| 394 | + } |
|
| 395 | + $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength); |
|
| 396 | + while ($size > 0) { |
|
| 397 | + $blockSize = $size > 64 |
|
| 398 | + ? 64 |
|
| 399 | + : $size; |
|
| 400 | + $read = fread($fp, $blockSize); |
|
| 401 | + if (!is_string($read)) { |
|
| 402 | + throw new Error('Could not read input file'); |
|
| 403 | + } |
|
| 404 | + ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read); |
|
| 405 | + $size -= $blockSize; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + fclose($fp); |
|
| 409 | + return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + /** |
|
| 413 | + * Encrypt a file (rather than a string). Uses less memory than |
|
| 414 | + * ParagonIE_Sodium_Compat::crypto_secretbox(), but produces |
|
| 415 | + * the same result. |
|
| 416 | + * |
|
| 417 | + * @param string $inputFile Absolute path to a file on the filesystem |
|
| 418 | + * @param string $outputFile Absolute path to a file on the filesystem |
|
| 419 | + * @param string $nonce Number to be used only once |
|
| 420 | + * @param string $key Encryption key |
|
| 421 | + * |
|
| 422 | + * @return bool |
|
| 423 | + * @throws Error |
|
| 424 | + * @throws TypeError |
|
| 425 | + */ |
|
| 426 | + public static function secretbox($inputFile, $outputFile, $nonce, $key) |
|
| 427 | + { |
|
| 428 | + /* Type checks: */ |
|
| 429 | + if (!is_string($inputFile)) { |
|
| 430 | + throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..'); |
|
| 431 | + } |
|
| 432 | + if (!is_string($outputFile)) { |
|
| 433 | + throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 434 | + } |
|
| 435 | + if (!is_string($nonce)) { |
|
| 436 | + throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /* Input validation: */ |
|
| 440 | + if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
| 441 | + throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
| 442 | + } |
|
| 443 | + if (!is_string($key)) { |
|
| 444 | + throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
| 445 | + } |
|
| 446 | + if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
| 447 | + throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes'); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** @var int $size */ |
|
| 451 | + $size = filesize($inputFile); |
|
| 452 | + if (!is_int($size)) { |
|
| 453 | + throw new Error('Could not obtain the file size'); |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** @var resource $ifp */ |
|
| 457 | + $ifp = fopen($inputFile, 'rb'); |
|
| 458 | + if (!is_resource($ifp)) { |
|
| 459 | + throw new Error('Could not open input file for reading'); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** @var resource $ofp */ |
|
| 463 | + $ofp = fopen($outputFile, 'wb'); |
|
| 464 | + if (!is_resource($ofp)) { |
|
| 465 | + fclose($ifp); |
|
| 466 | + throw new Error('Could not open output file for writing'); |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + $res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key); |
|
| 470 | + fclose($ifp); |
|
| 471 | + fclose($ofp); |
|
| 472 | + return $res; |
|
| 473 | + } |
|
| 474 | + /** |
|
| 475 | + * Seal a file (rather than a string). Uses less memory than |
|
| 476 | + * ParagonIE_Sodium_Compat::crypto_secretbox_open(), but produces |
|
| 477 | + * the same result. |
|
| 478 | + * |
|
| 479 | + * Warning: Does not protect against TOCTOU attacks. You should |
|
| 480 | + * just load the file into memory and use crypto_secretbox_open() if |
|
| 481 | + * you are worried about those. |
|
| 482 | + * |
|
| 483 | + * @param string $inputFile |
|
| 484 | + * @param string $outputFile |
|
| 485 | + * @param string $nonce |
|
| 486 | + * @param string $key |
|
| 487 | + * @return bool |
|
| 488 | + * @throws Error |
|
| 489 | + * @throws TypeError |
|
| 490 | + */ |
|
| 491 | + public static function secretbox_open($inputFile, $outputFile, $nonce, $key) |
|
| 492 | + { |
|
| 493 | + /* Type checks: */ |
|
| 494 | + if (!is_string($inputFile)) { |
|
| 495 | + throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 496 | + } |
|
| 497 | + if (!is_string($outputFile)) { |
|
| 498 | + throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 499 | + } |
|
| 500 | + if (!is_string($nonce)) { |
|
| 501 | + throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 502 | + } |
|
| 503 | + if (!is_string($key)) { |
|
| 504 | + throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + /* Input validation: */ |
|
| 508 | + if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
| 509 | + throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
| 510 | + } |
|
| 511 | + if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
| 512 | + throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes'); |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + /** @var int $size */ |
|
| 516 | + $size = filesize($inputFile); |
|
| 517 | + if (!is_int($size)) { |
|
| 518 | + throw new Error('Could not obtain the file size'); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + /** @var resource $ifp */ |
|
| 522 | + $ifp = fopen($inputFile, 'rb'); |
|
| 523 | + if (!is_resource($ifp)) { |
|
| 524 | + throw new Error('Could not open input file for reading'); |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** @var resource $ofp */ |
|
| 528 | + $ofp = fopen($outputFile, 'wb'); |
|
| 529 | + if (!is_resource($ofp)) { |
|
| 530 | + fclose($ifp); |
|
| 531 | + throw new Error('Could not open output file for writing'); |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + $res = self::secretbox_decrypt($ifp, $ofp, $size, $nonce, $key); |
|
| 535 | + fclose($ifp); |
|
| 536 | + fclose($ofp); |
|
| 537 | + try { |
|
| 538 | + ParagonIE_Sodium_Compat::memzero($key); |
|
| 539 | + } catch (Error $ex) { |
|
| 540 | + unset($key); |
|
| 541 | + } |
|
| 542 | + return $res; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Sign a file (rather than a string). Uses less memory than |
|
| 547 | + * ParagonIE_Sodium_Compat::crypto_sign_detached(), but produces |
|
| 548 | + * the same result. |
|
| 549 | + * |
|
| 550 | + * @param string $filePath Absolute path to a file on the filesystem |
|
| 551 | + * @param string $secretKey Secret signing key |
|
| 552 | + * |
|
| 553 | + * @return string Ed25519 signature |
|
| 554 | + * @throws Error |
|
| 555 | + * @throws TypeError |
|
| 556 | + */ |
|
| 557 | + public static function sign($filePath, $secretKey) |
|
| 558 | + { |
|
| 559 | + /* Type checks: */ |
|
| 560 | + if (!is_string($filePath)) { |
|
| 561 | + throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
|
| 562 | + } |
|
| 563 | + if (!is_string($secretKey)) { |
|
| 564 | + throw new TypeError('Argument 2 must be a string, ' . gettype($secretKey) . ' given.'); |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + /* Input validation: */ |
|
| 568 | + if (self::strlen($secretKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES) { |
|
| 569 | + throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes'); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + /** @var int $size */ |
|
| 573 | + $size = filesize($filePath); |
|
| 574 | + if (!is_int($size)) { |
|
| 575 | + throw new Error('Could not obtain the file size'); |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** @var resource $fp */ |
|
| 579 | + $fp = fopen($filePath, 'rb'); |
|
| 580 | + if (!is_resource($fp)) { |
|
| 581 | + throw new Error('Could not open input file for reading'); |
|
| 582 | + } |
|
| 583 | + |
|
| 584 | + /** @var string $az */ |
|
| 585 | + $az = hash('sha512', self::substr($secretKey, 0, 32), true); |
|
| 586 | + |
|
| 587 | + $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
| 588 | + $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
| 589 | + |
|
| 590 | + /** @var resource $hs */ |
|
| 591 | + $hs = hash_init('sha512'); |
|
| 592 | + hash_update($hs, self::substr($az, 32, 32)); |
|
| 593 | + $hs = self::updateHashWithFile($hs, $fp, $size); |
|
| 594 | + |
|
| 595 | + /** @var string $nonceHash */ |
|
| 596 | + $nonceHash = hash_final($hs, true); |
|
| 597 | + |
|
| 598 | + /** @var string $pk */ |
|
| 599 | + $pk = self::substr($secretKey, 32, 32); |
|
| 600 | + |
|
| 601 | + /** @var string $nonce */ |
|
| 602 | + $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
| 603 | + |
|
| 604 | + /** @var string $sig */ |
|
| 605 | + $sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes( |
|
| 606 | + ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce) |
|
| 607 | + ); |
|
| 608 | + |
|
| 609 | + /** @var resource $hs */ |
|
| 610 | + $hs = hash_init('sha512'); |
|
| 611 | + hash_update($hs, self::substr($sig, 0, 32)); |
|
| 612 | + hash_update($hs, self::substr($pk, 0, 32)); |
|
| 613 | + $hs = self::updateHashWithFile($hs, $fp, $size); |
|
| 614 | + |
|
| 615 | + /** @var string $hramHash */ |
|
| 616 | + $hramHash = hash_final($hs, true); |
|
| 617 | + |
|
| 618 | + /** @var string $hram */ |
|
| 619 | + $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hramHash); |
|
| 620 | + |
|
| 621 | + /** @var string $sigAfter */ |
|
| 622 | + $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd($hram, $az, $nonce); |
|
| 623 | + |
|
| 624 | + /** @var string $sig */ |
|
| 625 | + $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
| 626 | + |
|
| 627 | + try { |
|
| 628 | + ParagonIE_Sodium_Compat::memzero($az); |
|
| 629 | + } catch (Error $ex) { |
|
| 630 | + $az = null; |
|
| 631 | + } |
|
| 632 | + fclose($fp); |
|
| 633 | + return $sig; |
|
| 634 | + } |
|
| 635 | + |
|
| 636 | + /** |
|
| 637 | + * Verify a file (rather than a string). Uses less memory than |
|
| 638 | + * ParagonIE_Sodium_Compat::crypto_sign_verify_detached(), but |
|
| 639 | + * produces the same result. |
|
| 640 | + * |
|
| 641 | + * @param string $sig Ed25519 signature |
|
| 642 | + * @param string $filePath Absolute path to a file on the filesystem |
|
| 643 | + * @param string $publicKey Signing public key |
|
| 644 | + * |
|
| 645 | + * @return bool |
|
| 646 | + * @throws Error |
|
| 647 | + * @throws Exception |
|
| 648 | + */ |
|
| 649 | + public static function verify($sig, $filePath, $publicKey) |
|
| 650 | + { |
|
| 651 | + /* Type checks: */ |
|
| 652 | + if (!is_string($sig)) { |
|
| 653 | + throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.'); |
|
| 654 | + } |
|
| 655 | + if (!is_string($filePath)) { |
|
| 656 | + throw new TypeError('Argument 2 must be a string, ' . gettype($filePath) . ' given.'); |
|
| 657 | + } |
|
| 658 | + if (!is_string($publicKey)) { |
|
| 659 | + throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + /* Input validation: */ |
|
| 663 | + if (self::strlen($sig) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES) { |
|
| 664 | + throw new TypeError('Argument 1 must be CRYPTO_SIGN_BYTES bytes'); |
|
| 665 | + } |
|
| 666 | + if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES) { |
|
| 667 | + throw new TypeError('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes'); |
|
| 668 | + } |
|
| 669 | + if (self::strlen($sig) < 64) { |
|
| 670 | + throw new Exception('Signature is too short'); |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + /* Security checks */ |
|
| 674 | + if (ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
| 675 | + throw new Exception('S < L - Invalid signature'); |
|
| 676 | + } |
|
| 677 | + if (ParagonIE_Sodium_Core_Ed25519::small_order($sig)) { |
|
| 678 | + throw new Exception('Signature is on too small of an order'); |
|
| 679 | + } |
|
| 680 | + if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
| 681 | + throw new Exception('Invalid signature'); |
|
| 682 | + } |
|
| 683 | + $d = 0; |
|
| 684 | + for ($i = 0; $i < 32; ++$i) { |
|
| 685 | + $d |= self::chrToInt($publicKey[$i]); |
|
| 686 | + } |
|
| 687 | + if ($d === 0) { |
|
| 688 | + throw new Exception('All zero public key'); |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + /** @var int $size */ |
|
| 692 | + $size = filesize($filePath); |
|
| 693 | + if (!is_int($size)) { |
|
| 694 | + throw new Error('Could not obtain the file size'); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + /** @var resource $fp */ |
|
| 698 | + $fp = fopen($filePath, 'rb'); |
|
| 699 | + if (!is_resource($fp)) { |
|
| 700 | + throw new Error('Could not open input file for reading'); |
|
| 701 | + } |
|
| 702 | + |
|
| 703 | + /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
|
| 704 | + $orig = ParagonIE_Sodium_Compat::$fastMult; |
|
| 705 | + |
|
| 706 | + // Set ParagonIE_Sodium_Compat::$fastMult to true to speed up verification. |
|
| 707 | + ParagonIE_Sodium_Compat::$fastMult = true; |
|
| 708 | + |
|
| 709 | + /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ |
|
| 710 | + $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey); |
|
| 711 | + |
|
| 712 | + /** @var resource $hs */ |
|
| 713 | + $hs = hash_init('sha512'); |
|
| 714 | + hash_update($hs, self::substr($sig, 0, 32)); |
|
| 715 | + hash_update($hs, self::substr($publicKey, 0, 32)); |
|
| 716 | + $hs = self::updateHashWithFile($hs, $fp, $size); |
|
| 717 | + /** @var string $hDigest */ |
|
| 718 | + $hDigest = hash_final($hs, true); |
|
| 719 | + |
|
| 720 | + /** @var string $h */ |
|
| 721 | + $h = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
| 722 | + |
|
| 723 | + /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */ |
|
| 724 | + $R = ParagonIE_Sodium_Core_Ed25519::ge_double_scalarmult_vartime( |
|
| 725 | + $h, |
|
| 726 | + $A, |
|
| 727 | + self::substr($sig, 32) |
|
| 728 | + ); |
|
| 729 | + |
|
| 730 | + /** @var string $rcheck */ |
|
| 731 | + $rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes($R); |
|
| 732 | + |
|
| 733 | + // Close the file handle |
|
| 734 | + fclose($fp); |
|
| 735 | + |
|
| 736 | + // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
|
| 737 | + ParagonIE_Sodium_Compat::$fastMult = $orig; |
|
| 738 | + return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + /** |
|
| 742 | + * @param resource $ifp |
|
| 743 | + * @param resource $ofp |
|
| 744 | + * @param int $mlen |
|
| 745 | + * @param string $nonce |
|
| 746 | + * @param string $boxKeypair |
|
| 747 | + * @return bool |
|
| 748 | + */ |
|
| 749 | + protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
| 750 | + { |
|
| 751 | + return self::secretbox_encrypt( |
|
| 752 | + $ifp, |
|
| 753 | + $ofp, |
|
| 754 | + $mlen, |
|
| 755 | + $nonce, |
|
| 756 | + ParagonIE_Sodium_Crypto::box_beforenm( |
|
| 757 | + ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), |
|
| 758 | + ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) |
|
| 759 | + ) |
|
| 760 | + ); |
|
| 761 | + } |
|
| 762 | + |
|
| 763 | + |
|
| 764 | + /** |
|
| 765 | + * @param resource $ifp |
|
| 766 | + * @param resource $ofp |
|
| 767 | + * @param int $mlen |
|
| 768 | + * @param string $nonce |
|
| 769 | + * @param string $boxKeypair |
|
| 770 | + * @return bool |
|
| 771 | + */ |
|
| 772 | + protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
| 773 | + { |
|
| 774 | + return self::secretbox_decrypt( |
|
| 775 | + $ifp, |
|
| 776 | + $ofp, |
|
| 777 | + $mlen, |
|
| 778 | + $nonce, |
|
| 779 | + ParagonIE_Sodium_Crypto::box_beforenm( |
|
| 780 | + ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), |
|
| 781 | + ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) |
|
| 782 | + ) |
|
| 783 | + ); |
|
| 784 | + } |
|
| 785 | + |
|
| 786 | + /** |
|
| 787 | + * Encrypt a file |
|
| 788 | + * |
|
| 789 | + * @param resource $ifp |
|
| 790 | + * @param resource $ofp |
|
| 791 | + * @param int $mlen |
|
| 792 | + * @param string $nonce |
|
| 793 | + * @param string $key |
|
| 794 | + * @return bool |
|
| 795 | + * @throws Error |
|
| 796 | + */ |
|
| 797 | + protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
| 798 | + { |
|
| 799 | + $plaintext = fread($ifp, 32); |
|
| 800 | + if (!is_string($plaintext)) { |
|
| 801 | + throw new Error('Could not read input file'); |
|
| 802 | + } |
|
| 803 | + $first32 = ftell($ifp); |
|
| 804 | + |
|
| 805 | + /** @var string $subkey */ |
|
| 806 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
| 807 | + |
|
| 808 | + /** @var string $realNonce */ |
|
| 809 | + $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
| 810 | + |
|
| 811 | + /** @var string $block0 */ |
|
| 812 | + $block0 = str_repeat("\x00", 32); |
|
| 813 | + |
|
| 814 | + /** @var int $mlen - Length of the plaintext message */ |
|
| 815 | + $mlen0 = $mlen; |
|
| 816 | + if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
| 817 | + $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES; |
|
| 818 | + } |
|
| 819 | + $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
| 820 | + |
|
| 821 | + /** @var string $block0 */ |
|
| 822 | + $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
|
| 823 | + $block0, |
|
| 824 | + $realNonce, |
|
| 825 | + $subkey |
|
| 826 | + ); |
|
| 827 | + |
|
| 828 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( |
|
| 829 | + ParagonIE_Sodium_Core_Util::substr( |
|
| 830 | + $block0, |
|
| 831 | + 0, |
|
| 832 | + ParagonIE_Sodium_Crypto::onetimeauth_poly1305_KEYBYTES |
|
| 833 | + ) |
|
| 834 | + ); |
|
| 835 | + |
|
| 836 | + // Pre-write 16 blank bytes for the Poly1305 tag |
|
| 837 | + $start = ftell($ofp); |
|
| 838 | + fwrite($ofp, str_repeat("\x00", 16)); |
|
| 839 | + |
|
| 840 | + /** @var string $c */ |
|
| 841 | + $cBlock = ParagonIE_Sodium_Core_Util::substr( |
|
| 842 | + $block0, |
|
| 843 | + ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES |
|
| 844 | + ); |
|
| 845 | + $state->update($cBlock); |
|
| 846 | + fwrite($ofp, $cBlock); |
|
| 847 | + $mlen -= 32; |
|
| 848 | + |
|
| 849 | + /** @var int $iter */ |
|
| 850 | + $iter = 1; |
|
| 851 | + |
|
| 852 | + /** @var int $incr */ |
|
| 853 | + $incr = self::BUFFER_SIZE >> 6; |
|
| 854 | + |
|
| 855 | + /* |
|
| 856 | 856 | * Set the cursor to the end of the first half-block. All future bytes will |
| 857 | 857 | * generated from salsa20_xor_ic, starting from 1 (second block). |
| 858 | 858 | */ |
| 859 | - fseek($ifp, $first32, SEEK_SET); |
|
| 860 | - |
|
| 861 | - while ($mlen > 0) { |
|
| 862 | - $blockSize = $mlen > self::BUFFER_SIZE |
|
| 863 | - ? self::BUFFER_SIZE |
|
| 864 | - : $mlen; |
|
| 865 | - $plaintext = fread($ifp, $blockSize); |
|
| 866 | - if (!is_string($plaintext)) { |
|
| 867 | - throw new Error('Could not read input file'); |
|
| 868 | - } |
|
| 869 | - $cBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
| 870 | - $plaintext, |
|
| 871 | - $realNonce, |
|
| 872 | - $iter, |
|
| 873 | - $subkey |
|
| 874 | - ); |
|
| 875 | - fwrite($ofp, $cBlock, $blockSize); |
|
| 876 | - $state->update($cBlock); |
|
| 877 | - |
|
| 878 | - $mlen -= $blockSize; |
|
| 879 | - $iter += $incr; |
|
| 880 | - } |
|
| 881 | - try { |
|
| 882 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
| 883 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 884 | - } catch (Error $ex) { |
|
| 885 | - $block0 = null; |
|
| 886 | - $subkey = null; |
|
| 887 | - } |
|
| 888 | - $end = ftell($ofp); |
|
| 889 | - |
|
| 890 | - /* |
|
| 859 | + fseek($ifp, $first32, SEEK_SET); |
|
| 860 | + |
|
| 861 | + while ($mlen > 0) { |
|
| 862 | + $blockSize = $mlen > self::BUFFER_SIZE |
|
| 863 | + ? self::BUFFER_SIZE |
|
| 864 | + : $mlen; |
|
| 865 | + $plaintext = fread($ifp, $blockSize); |
|
| 866 | + if (!is_string($plaintext)) { |
|
| 867 | + throw new Error('Could not read input file'); |
|
| 868 | + } |
|
| 869 | + $cBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
| 870 | + $plaintext, |
|
| 871 | + $realNonce, |
|
| 872 | + $iter, |
|
| 873 | + $subkey |
|
| 874 | + ); |
|
| 875 | + fwrite($ofp, $cBlock, $blockSize); |
|
| 876 | + $state->update($cBlock); |
|
| 877 | + |
|
| 878 | + $mlen -= $blockSize; |
|
| 879 | + $iter += $incr; |
|
| 880 | + } |
|
| 881 | + try { |
|
| 882 | + ParagonIE_Sodium_Compat::memzero($block0); |
|
| 883 | + ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 884 | + } catch (Error $ex) { |
|
| 885 | + $block0 = null; |
|
| 886 | + $subkey = null; |
|
| 887 | + } |
|
| 888 | + $end = ftell($ofp); |
|
| 889 | + |
|
| 890 | + /* |
|
| 891 | 891 | * Write the Poly1305 authentication tag that provides integrity |
| 892 | 892 | * over the ciphertext (encrypt-then-MAC) |
| 893 | 893 | */ |
| 894 | - fseek($ofp, $start, SEEK_SET); |
|
| 895 | - fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES); |
|
| 896 | - fseek($ofp, $end, SEEK_SET); |
|
| 897 | - unset($state); |
|
| 898 | - |
|
| 899 | - return true; |
|
| 900 | - } |
|
| 901 | - |
|
| 902 | - /** |
|
| 903 | - * Decrypt a file |
|
| 904 | - * |
|
| 905 | - * @param resource $ifp |
|
| 906 | - * @param resource $ofp |
|
| 907 | - * @param int $mlen |
|
| 908 | - * @param string $nonce |
|
| 909 | - * @param string $key |
|
| 910 | - * @return bool |
|
| 911 | - * @throws Error |
|
| 912 | - * @throws Exception |
|
| 913 | - */ |
|
| 914 | - protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
| 915 | - { |
|
| 916 | - $tag = fread($ifp, 16); |
|
| 917 | - if (!is_string($tag)) { |
|
| 918 | - throw new Error('Could not read input file'); |
|
| 919 | - } |
|
| 920 | - |
|
| 921 | - /** @var string $subkey */ |
|
| 922 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
| 923 | - |
|
| 924 | - /** @var string $realNonce */ |
|
| 925 | - $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
| 926 | - |
|
| 927 | - /** @var string $block0 */ |
|
| 928 | - $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
|
| 929 | - 64, |
|
| 930 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 931 | - $subkey |
|
| 932 | - ); |
|
| 933 | - |
|
| 934 | - /* Verify the Poly1305 MAC -before- attempting to decrypt! */ |
|
| 935 | - $state = new ParagonIE_Sodium_Core_Poly1305_State(self::substr($block0, 0, 32)); |
|
| 936 | - if (!self::onetimeauth_verify($state, $ifp, $tag, $mlen)) { |
|
| 937 | - throw new Exception('Invalid MAC'); |
|
| 938 | - } |
|
| 939 | - |
|
| 940 | - /* |
|
| 894 | + fseek($ofp, $start, SEEK_SET); |
|
| 895 | + fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES); |
|
| 896 | + fseek($ofp, $end, SEEK_SET); |
|
| 897 | + unset($state); |
|
| 898 | + |
|
| 899 | + return true; |
|
| 900 | + } |
|
| 901 | + |
|
| 902 | + /** |
|
| 903 | + * Decrypt a file |
|
| 904 | + * |
|
| 905 | + * @param resource $ifp |
|
| 906 | + * @param resource $ofp |
|
| 907 | + * @param int $mlen |
|
| 908 | + * @param string $nonce |
|
| 909 | + * @param string $key |
|
| 910 | + * @return bool |
|
| 911 | + * @throws Error |
|
| 912 | + * @throws Exception |
|
| 913 | + */ |
|
| 914 | + protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
| 915 | + { |
|
| 916 | + $tag = fread($ifp, 16); |
|
| 917 | + if (!is_string($tag)) { |
|
| 918 | + throw new Error('Could not read input file'); |
|
| 919 | + } |
|
| 920 | + |
|
| 921 | + /** @var string $subkey */ |
|
| 922 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
| 923 | + |
|
| 924 | + /** @var string $realNonce */ |
|
| 925 | + $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
| 926 | + |
|
| 927 | + /** @var string $block0 */ |
|
| 928 | + $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
|
| 929 | + 64, |
|
| 930 | + ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 931 | + $subkey |
|
| 932 | + ); |
|
| 933 | + |
|
| 934 | + /* Verify the Poly1305 MAC -before- attempting to decrypt! */ |
|
| 935 | + $state = new ParagonIE_Sodium_Core_Poly1305_State(self::substr($block0, 0, 32)); |
|
| 936 | + if (!self::onetimeauth_verify($state, $ifp, $tag, $mlen)) { |
|
| 937 | + throw new Exception('Invalid MAC'); |
|
| 938 | + } |
|
| 939 | + |
|
| 940 | + /* |
|
| 941 | 941 | * Set the cursor to the end of the first half-block. All future bytes will |
| 942 | 942 | * generated from salsa20_xor_ic, starting from 1 (second block). |
| 943 | 943 | */ |
| 944 | - $first32 = fread($ifp, 32); |
|
| 945 | - if (!is_string($first32)) { |
|
| 946 | - throw new Error('Could not read input file'); |
|
| 947 | - } |
|
| 948 | - $first32len = self::strlen($first32); |
|
| 949 | - fwrite( |
|
| 950 | - $ofp, |
|
| 951 | - self::xorStrings( |
|
| 952 | - self::substr($block0, 32, $first32len), |
|
| 953 | - self::substr($first32, 0, $first32len) |
|
| 954 | - ) |
|
| 955 | - ); |
|
| 956 | - $mlen -= 32; |
|
| 957 | - |
|
| 958 | - /** @var int $iter */ |
|
| 959 | - $iter = 1; |
|
| 960 | - |
|
| 961 | - /** @var int $incr */ |
|
| 962 | - $incr = self::BUFFER_SIZE >> 6; |
|
| 963 | - |
|
| 964 | - /* Decrypts ciphertext, writes to output file. */ |
|
| 965 | - while ($mlen > 0) { |
|
| 966 | - $blockSize = $mlen > self::BUFFER_SIZE |
|
| 967 | - ? self::BUFFER_SIZE |
|
| 968 | - : $mlen; |
|
| 969 | - $ciphertext = fread($ifp, $blockSize); |
|
| 970 | - if (!is_string($ciphertext)) { |
|
| 971 | - throw new Error('Could not read input file'); |
|
| 972 | - } |
|
| 973 | - $pBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
| 974 | - $ciphertext, |
|
| 975 | - $realNonce, |
|
| 976 | - $iter, |
|
| 977 | - $subkey |
|
| 978 | - ); |
|
| 979 | - fwrite($ofp, $pBlock, $blockSize); |
|
| 980 | - $mlen -= $blockSize; |
|
| 981 | - $iter += $incr; |
|
| 982 | - } |
|
| 983 | - return true; |
|
| 984 | - } |
|
| 985 | - |
|
| 986 | - /** |
|
| 987 | - * @param ParagonIE_Sodium_Core_Poly1305_State $state |
|
| 988 | - * @param resource $ifp |
|
| 989 | - * @param string $tag |
|
| 990 | - * @param int $mlen |
|
| 991 | - * @return bool |
|
| 992 | - * @throws Error |
|
| 993 | - */ |
|
| 994 | - protected static function onetimeauth_verify(ParagonIE_Sodium_Core_Poly1305_State $state, $ifp, $tag = '', $mlen = 0) |
|
| 995 | - { |
|
| 996 | - /** @var int $pos */ |
|
| 997 | - $pos = ftell($ifp); |
|
| 998 | - |
|
| 999 | - /** @var int $iter */ |
|
| 1000 | - $iter = 1; |
|
| 1001 | - |
|
| 1002 | - /** @var int $incr */ |
|
| 1003 | - $incr = self::BUFFER_SIZE >> 6; |
|
| 1004 | - |
|
| 1005 | - while ($mlen > 0) { |
|
| 1006 | - $blockSize = $mlen > self::BUFFER_SIZE |
|
| 1007 | - ? self::BUFFER_SIZE |
|
| 1008 | - : $mlen; |
|
| 1009 | - $ciphertext = fread($ifp, $blockSize); |
|
| 1010 | - if (!is_string($ciphertext)) { |
|
| 1011 | - throw new Error('Could not read input file'); |
|
| 1012 | - } |
|
| 1013 | - $state->update($ciphertext); |
|
| 1014 | - $mlen -= $blockSize; |
|
| 1015 | - $iter += $incr; |
|
| 1016 | - } |
|
| 1017 | - $res = ParagonIE_Sodium_Core_Util::verify_16($tag, $state->finish()); |
|
| 1018 | - |
|
| 1019 | - fseek($ifp, $pos, SEEK_SET); |
|
| 1020 | - return $res; |
|
| 1021 | - } |
|
| 1022 | - |
|
| 1023 | - /** |
|
| 1024 | - * Update a hash context with the contents of a file, without |
|
| 1025 | - * loading the entire file into memory. |
|
| 1026 | - * |
|
| 1027 | - * @param resource|object $hash |
|
| 1028 | - * @param resource $fp |
|
| 1029 | - * @param int $size |
|
| 1030 | - * @return mixed (resource on PHP < 7.2, object on PHP >= 7.2) |
|
| 1031 | - * @throws Error |
|
| 1032 | - * @throws TypeError |
|
| 1033 | - * @psalm-suppress PossiblyInvalidArgument |
|
| 1034 | - * PHP 7.2 changes from a resource to an object, |
|
| 1035 | - * which causes Psalm to complain about an error. |
|
| 1036 | - */ |
|
| 1037 | - public static function updateHashWithFile($hash, $fp, $size = 0) |
|
| 1038 | - { |
|
| 1039 | - /* Type checks: */ |
|
| 1040 | - if (PHP_VERSION_ID < 70200) { |
|
| 1041 | - if (!is_resource($hash)) { |
|
| 1042 | - throw new TypeError('Argument 1 must be a resource, ' . gettype($hash) . ' given.'); |
|
| 1043 | - } |
|
| 1044 | - |
|
| 1045 | - } else { |
|
| 1046 | - if (!is_object($hash)) { |
|
| 1047 | - throw new TypeError('Argument 1 must be an object (PHP 7.2+), ' . gettype($hash) . ' given.'); |
|
| 1048 | - } |
|
| 1049 | - } |
|
| 1050 | - if (!is_resource($fp)) { |
|
| 1051 | - throw new TypeError('Argument 2 must be a resource, ' . gettype($fp) . ' given.'); |
|
| 1052 | - } |
|
| 1053 | - if (!is_int($size)) { |
|
| 1054 | - throw new TypeError('Argument 3 must be an integer, ' . gettype($size) . ' given.'); |
|
| 1055 | - } |
|
| 1056 | - |
|
| 1057 | - /** @var int $originalPosition */ |
|
| 1058 | - $originalPosition = ftell($fp); |
|
| 1059 | - |
|
| 1060 | - // Move file pointer to beginning of file |
|
| 1061 | - fseek($fp, 0, SEEK_SET); |
|
| 1062 | - for ($i = 0; $i < $size; $i += self::BUFFER_SIZE) { |
|
| 1063 | - /** @var string $message */ |
|
| 1064 | - $message = fread( |
|
| 1065 | - $fp, |
|
| 1066 | - ($size - $i) > self::BUFFER_SIZE |
|
| 1067 | - ? $size - $i |
|
| 1068 | - : self::BUFFER_SIZE |
|
| 1069 | - ); |
|
| 1070 | - if (!is_string($message)) { |
|
| 1071 | - throw new Error('Unexpected error reading from file.'); |
|
| 1072 | - } |
|
| 1073 | - /** @psalm-suppress InvalidArgument */ |
|
| 1074 | - hash_update($hash, $message); |
|
| 1075 | - } |
|
| 1076 | - // Reset file pointer's position |
|
| 1077 | - fseek($fp, $originalPosition, SEEK_SET); |
|
| 1078 | - return $hash; |
|
| 1079 | - } |
|
| 944 | + $first32 = fread($ifp, 32); |
|
| 945 | + if (!is_string($first32)) { |
|
| 946 | + throw new Error('Could not read input file'); |
|
| 947 | + } |
|
| 948 | + $first32len = self::strlen($first32); |
|
| 949 | + fwrite( |
|
| 950 | + $ofp, |
|
| 951 | + self::xorStrings( |
|
| 952 | + self::substr($block0, 32, $first32len), |
|
| 953 | + self::substr($first32, 0, $first32len) |
|
| 954 | + ) |
|
| 955 | + ); |
|
| 956 | + $mlen -= 32; |
|
| 957 | + |
|
| 958 | + /** @var int $iter */ |
|
| 959 | + $iter = 1; |
|
| 960 | + |
|
| 961 | + /** @var int $incr */ |
|
| 962 | + $incr = self::BUFFER_SIZE >> 6; |
|
| 963 | + |
|
| 964 | + /* Decrypts ciphertext, writes to output file. */ |
|
| 965 | + while ($mlen > 0) { |
|
| 966 | + $blockSize = $mlen > self::BUFFER_SIZE |
|
| 967 | + ? self::BUFFER_SIZE |
|
| 968 | + : $mlen; |
|
| 969 | + $ciphertext = fread($ifp, $blockSize); |
|
| 970 | + if (!is_string($ciphertext)) { |
|
| 971 | + throw new Error('Could not read input file'); |
|
| 972 | + } |
|
| 973 | + $pBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
|
| 974 | + $ciphertext, |
|
| 975 | + $realNonce, |
|
| 976 | + $iter, |
|
| 977 | + $subkey |
|
| 978 | + ); |
|
| 979 | + fwrite($ofp, $pBlock, $blockSize); |
|
| 980 | + $mlen -= $blockSize; |
|
| 981 | + $iter += $incr; |
|
| 982 | + } |
|
| 983 | + return true; |
|
| 984 | + } |
|
| 985 | + |
|
| 986 | + /** |
|
| 987 | + * @param ParagonIE_Sodium_Core_Poly1305_State $state |
|
| 988 | + * @param resource $ifp |
|
| 989 | + * @param string $tag |
|
| 990 | + * @param int $mlen |
|
| 991 | + * @return bool |
|
| 992 | + * @throws Error |
|
| 993 | + */ |
|
| 994 | + protected static function onetimeauth_verify(ParagonIE_Sodium_Core_Poly1305_State $state, $ifp, $tag = '', $mlen = 0) |
|
| 995 | + { |
|
| 996 | + /** @var int $pos */ |
|
| 997 | + $pos = ftell($ifp); |
|
| 998 | + |
|
| 999 | + /** @var int $iter */ |
|
| 1000 | + $iter = 1; |
|
| 1001 | + |
|
| 1002 | + /** @var int $incr */ |
|
| 1003 | + $incr = self::BUFFER_SIZE >> 6; |
|
| 1004 | + |
|
| 1005 | + while ($mlen > 0) { |
|
| 1006 | + $blockSize = $mlen > self::BUFFER_SIZE |
|
| 1007 | + ? self::BUFFER_SIZE |
|
| 1008 | + : $mlen; |
|
| 1009 | + $ciphertext = fread($ifp, $blockSize); |
|
| 1010 | + if (!is_string($ciphertext)) { |
|
| 1011 | + throw new Error('Could not read input file'); |
|
| 1012 | + } |
|
| 1013 | + $state->update($ciphertext); |
|
| 1014 | + $mlen -= $blockSize; |
|
| 1015 | + $iter += $incr; |
|
| 1016 | + } |
|
| 1017 | + $res = ParagonIE_Sodium_Core_Util::verify_16($tag, $state->finish()); |
|
| 1018 | + |
|
| 1019 | + fseek($ifp, $pos, SEEK_SET); |
|
| 1020 | + return $res; |
|
| 1021 | + } |
|
| 1022 | + |
|
| 1023 | + /** |
|
| 1024 | + * Update a hash context with the contents of a file, without |
|
| 1025 | + * loading the entire file into memory. |
|
| 1026 | + * |
|
| 1027 | + * @param resource|object $hash |
|
| 1028 | + * @param resource $fp |
|
| 1029 | + * @param int $size |
|
| 1030 | + * @return mixed (resource on PHP < 7.2, object on PHP >= 7.2) |
|
| 1031 | + * @throws Error |
|
| 1032 | + * @throws TypeError |
|
| 1033 | + * @psalm-suppress PossiblyInvalidArgument |
|
| 1034 | + * PHP 7.2 changes from a resource to an object, |
|
| 1035 | + * which causes Psalm to complain about an error. |
|
| 1036 | + */ |
|
| 1037 | + public static function updateHashWithFile($hash, $fp, $size = 0) |
|
| 1038 | + { |
|
| 1039 | + /* Type checks: */ |
|
| 1040 | + if (PHP_VERSION_ID < 70200) { |
|
| 1041 | + if (!is_resource($hash)) { |
|
| 1042 | + throw new TypeError('Argument 1 must be a resource, ' . gettype($hash) . ' given.'); |
|
| 1043 | + } |
|
| 1044 | + |
|
| 1045 | + } else { |
|
| 1046 | + if (!is_object($hash)) { |
|
| 1047 | + throw new TypeError('Argument 1 must be an object (PHP 7.2+), ' . gettype($hash) . ' given.'); |
|
| 1048 | + } |
|
| 1049 | + } |
|
| 1050 | + if (!is_resource($fp)) { |
|
| 1051 | + throw new TypeError('Argument 2 must be a resource, ' . gettype($fp) . ' given.'); |
|
| 1052 | + } |
|
| 1053 | + if (!is_int($size)) { |
|
| 1054 | + throw new TypeError('Argument 3 must be an integer, ' . gettype($size) . ' given.'); |
|
| 1055 | + } |
|
| 1056 | + |
|
| 1057 | + /** @var int $originalPosition */ |
|
| 1058 | + $originalPosition = ftell($fp); |
|
| 1059 | + |
|
| 1060 | + // Move file pointer to beginning of file |
|
| 1061 | + fseek($fp, 0, SEEK_SET); |
|
| 1062 | + for ($i = 0; $i < $size; $i += self::BUFFER_SIZE) { |
|
| 1063 | + /** @var string $message */ |
|
| 1064 | + $message = fread( |
|
| 1065 | + $fp, |
|
| 1066 | + ($size - $i) > self::BUFFER_SIZE |
|
| 1067 | + ? $size - $i |
|
| 1068 | + : self::BUFFER_SIZE |
|
| 1069 | + ); |
|
| 1070 | + if (!is_string($message)) { |
|
| 1071 | + throw new Error('Unexpected error reading from file.'); |
|
| 1072 | + } |
|
| 1073 | + /** @psalm-suppress InvalidArgument */ |
|
| 1074 | + hash_update($hash, $message); |
|
| 1075 | + } |
|
| 1076 | + // Reset file pointer's position |
|
| 1077 | + fseek($fp, $originalPosition, SEEK_SET); |
|
| 1078 | + return $hash; |
|
| 1079 | + } |
|
| 1080 | 1080 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if (class_exists('ParagonIE_Sodium_File', false)) { |
|
| 3 | +if ( class_exists( 'ParagonIE_Sodium_File', false ) ) { |
|
| 4 | 4 | return; |
| 5 | 5 | } |
| 6 | 6 | /** |
@@ -25,52 +25,52 @@ discard block |
||
| 25 | 25 | * @throws Error |
| 26 | 26 | * @throws TypeError |
| 27 | 27 | */ |
| 28 | - public static function box($inputFile, $outputFile, $nonce, $keyPair) |
|
| 28 | + public static function box( $inputFile, $outputFile, $nonce, $keyPair ) |
|
| 29 | 29 | { |
| 30 | 30 | /* Type checks: */ |
| 31 | - if (!is_string($inputFile)) { |
|
| 32 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 31 | + if ( ! is_string( $inputFile ) ) { |
|
| 32 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' ); |
|
| 33 | 33 | } |
| 34 | - if (!is_string($outputFile)) { |
|
| 35 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 34 | + if ( ! is_string( $outputFile ) ) { |
|
| 35 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' ); |
|
| 36 | 36 | } |
| 37 | - if (!is_string($nonce)) { |
|
| 38 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 37 | + if ( ! is_string( $nonce ) ) { |
|
| 38 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $nonce ) . ' given.' ); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | 41 | /* Input validation: */ |
| 42 | - if (!is_string($keyPair)) { |
|
| 43 | - throw new TypeError('Argument 4 must be a string, ' . gettype($keyPair) . ' given.'); |
|
| 42 | + if ( ! is_string( $keyPair ) ) { |
|
| 43 | + throw new TypeError( 'Argument 4 must be a string, ' . gettype( $keyPair ) . ' given.' ); |
|
| 44 | 44 | } |
| 45 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
| 46 | - throw new TypeError('Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
| 45 | + if ( self::strlen( $nonce ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES ) { |
|
| 46 | + throw new TypeError( 'Argument 3 must be CRYPTO_BOX_NONCEBYTES bytes' ); |
|
| 47 | 47 | } |
| 48 | - if (self::strlen($keyPair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 49 | - throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
| 48 | + if ( self::strlen( $keyPair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) { |
|
| 49 | + throw new TypeError( 'Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes' ); |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /** @var int $size */ |
| 53 | - $size = filesize($inputFile); |
|
| 54 | - if (!is_int($size)) { |
|
| 55 | - throw new Error('Could not obtain the file size'); |
|
| 53 | + $size = filesize( $inputFile ); |
|
| 54 | + if ( ! is_int( $size ) ) { |
|
| 55 | + throw new Error( 'Could not obtain the file size' ); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | /** @var resource $ifp */ |
| 59 | - $ifp = fopen($inputFile, 'rb'); |
|
| 60 | - if (!is_resource($ifp)) { |
|
| 61 | - throw new Error('Could not open input file for reading'); |
|
| 59 | + $ifp = fopen( $inputFile, 'rb' ); |
|
| 60 | + if ( ! is_resource( $ifp ) ) { |
|
| 61 | + throw new Error( 'Could not open input file for reading' ); |
|
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /** @var resource $ofp */ |
| 65 | - $ofp = fopen($outputFile, 'wb'); |
|
| 66 | - if (!is_resource($ofp)) { |
|
| 67 | - fclose($ifp); |
|
| 68 | - throw new Error('Could not open output file for writing'); |
|
| 65 | + $ofp = fopen( $outputFile, 'wb' ); |
|
| 66 | + if ( ! is_resource( $ofp ) ) { |
|
| 67 | + fclose( $ifp ); |
|
| 68 | + throw new Error( 'Could not open output file for writing' ); |
|
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $keyPair); |
|
| 72 | - fclose($ifp); |
|
| 73 | - fclose($ofp); |
|
| 71 | + $res = self::box_encrypt( $ifp, $ofp, $size, $nonce, $keyPair ); |
|
| 72 | + fclose( $ifp ); |
|
| 73 | + fclose( $ofp ); |
|
| 74 | 74 | return $res; |
| 75 | 75 | } |
| 76 | 76 | |
@@ -91,57 +91,57 @@ discard block |
||
| 91 | 91 | * @throws Error |
| 92 | 92 | * @throws TypeError |
| 93 | 93 | */ |
| 94 | - public static function box_open($inputFile, $outputFile, $nonce, $keypair) |
|
| 94 | + public static function box_open( $inputFile, $outputFile, $nonce, $keypair ) |
|
| 95 | 95 | { |
| 96 | 96 | /* Type checks: */ |
| 97 | - if (!is_string($inputFile)) { |
|
| 98 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 97 | + if ( ! is_string( $inputFile ) ) { |
|
| 98 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' ); |
|
| 99 | 99 | } |
| 100 | - if (!is_string($outputFile)) { |
|
| 101 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 100 | + if ( ! is_string( $outputFile ) ) { |
|
| 101 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' ); |
|
| 102 | 102 | } |
| 103 | - if (!is_string($nonce)) { |
|
| 104 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 103 | + if ( ! is_string( $nonce ) ) { |
|
| 104 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $nonce ) . ' given.' ); |
|
| 105 | 105 | } |
| 106 | - if (!is_string($keypair)) { |
|
| 107 | - throw new TypeError('Argument 4 must be a string, ' . gettype($keypair) . ' given.'); |
|
| 106 | + if ( ! is_string( $keypair ) ) { |
|
| 107 | + throw new TypeError( 'Argument 4 must be a string, ' . gettype( $keypair ) . ' given.' ); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /* Input validation: */ |
| 111 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES) { |
|
| 112 | - throw new TypeError('Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes'); |
|
| 111 | + if ( self::strlen( $nonce ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_NONCEBYTES ) { |
|
| 112 | + throw new TypeError( 'Argument 4 must be CRYPTO_BOX_NONCEBYTES bytes' ); |
|
| 113 | 113 | } |
| 114 | - if (self::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 115 | - throw new TypeError('Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
| 114 | + if ( self::strlen( $keypair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) { |
|
| 115 | + throw new TypeError( 'Argument 4 must be CRYPTO_BOX_KEYPAIRBYTES bytes' ); |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | /** @var int $size */ |
| 119 | - $size = filesize($inputFile); |
|
| 120 | - if (!is_int($size)) { |
|
| 121 | - throw new Error('Could not obtain the file size'); |
|
| 119 | + $size = filesize( $inputFile ); |
|
| 120 | + if ( ! is_int( $size ) ) { |
|
| 121 | + throw new Error( 'Could not obtain the file size' ); |
|
| 122 | 122 | } |
| 123 | 123 | |
| 124 | 124 | /** @var resource $ifp */ |
| 125 | - $ifp = fopen($inputFile, 'rb'); |
|
| 126 | - if (!is_resource($ifp)) { |
|
| 127 | - throw new Error('Could not open input file for reading'); |
|
| 125 | + $ifp = fopen( $inputFile, 'rb' ); |
|
| 126 | + if ( ! is_resource( $ifp ) ) { |
|
| 127 | + throw new Error( 'Could not open input file for reading' ); |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | /** @var resource $ofp */ |
| 131 | - $ofp = fopen($outputFile, 'wb'); |
|
| 132 | - if (!is_resource($ofp)) { |
|
| 133 | - fclose($ifp); |
|
| 134 | - throw new Error('Could not open output file for writing'); |
|
| 131 | + $ofp = fopen( $outputFile, 'wb' ); |
|
| 132 | + if ( ! is_resource( $ofp ) ) { |
|
| 133 | + fclose( $ifp ); |
|
| 134 | + throw new Error( 'Could not open output file for writing' ); |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | - $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $keypair); |
|
| 138 | - fclose($ifp); |
|
| 139 | - fclose($ofp); |
|
| 137 | + $res = self::box_decrypt( $ifp, $ofp, $size, $nonce, $keypair ); |
|
| 138 | + fclose( $ifp ); |
|
| 139 | + fclose( $ofp ); |
|
| 140 | 140 | try { |
| 141 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
| 142 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 143 | - } catch (Error $ex) { |
|
| 144 | - unset($ephKeypair); |
|
| 141 | + ParagonIE_Sodium_Compat::memzero( $nonce ); |
|
| 142 | + ParagonIE_Sodium_Compat::memzero( $ephKeypair ); |
|
| 143 | + } catch ( Error $ex ) { |
|
| 144 | + unset( $ephKeypair ); |
|
| 145 | 145 | } |
| 146 | 146 | return $res; |
| 147 | 147 | } |
@@ -159,41 +159,41 @@ discard block |
||
| 159 | 159 | * @throws Error |
| 160 | 160 | * @throws TypeError |
| 161 | 161 | */ |
| 162 | - public static function box_seal($inputFile, $outputFile, $publicKey) |
|
| 162 | + public static function box_seal( $inputFile, $outputFile, $publicKey ) |
|
| 163 | 163 | { |
| 164 | 164 | /* Type checks: */ |
| 165 | - if (!is_string($inputFile)) { |
|
| 166 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 165 | + if ( ! is_string( $inputFile ) ) { |
|
| 166 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' ); |
|
| 167 | 167 | } |
| 168 | - if (!is_string($outputFile)) { |
|
| 169 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 168 | + if ( ! is_string( $outputFile ) ) { |
|
| 169 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' ); |
|
| 170 | 170 | } |
| 171 | - if (!is_string($publicKey)) { |
|
| 172 | - throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); |
|
| 171 | + if ( ! is_string( $publicKey ) ) { |
|
| 172 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $publicKey ) . ' given.' ); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | /* Input validation: */ |
| 176 | - if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
| 177 | - throw new TypeError('Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes'); |
|
| 176 | + if ( self::strlen( $publicKey ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES ) { |
|
| 177 | + throw new TypeError( 'Argument 3 must be CRYPTO_BOX_PUBLICKEYBYTES bytes' ); |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** @var int $size */ |
| 181 | - $size = filesize($inputFile); |
|
| 182 | - if (!is_int($size)) { |
|
| 183 | - throw new Error('Could not obtain the file size'); |
|
| 181 | + $size = filesize( $inputFile ); |
|
| 182 | + if ( ! is_int( $size ) ) { |
|
| 183 | + throw new Error( 'Could not obtain the file size' ); |
|
| 184 | 184 | } |
| 185 | 185 | |
| 186 | 186 | /** @var resource $ifp */ |
| 187 | - $ifp = fopen($inputFile, 'rb'); |
|
| 188 | - if (!is_resource($ifp)) { |
|
| 189 | - throw new Error('Could not open input file for reading'); |
|
| 187 | + $ifp = fopen( $inputFile, 'rb' ); |
|
| 188 | + if ( ! is_resource( $ifp ) ) { |
|
| 189 | + throw new Error( 'Could not open input file for reading' ); |
|
| 190 | 190 | } |
| 191 | 191 | |
| 192 | 192 | /** @var resource $ofp */ |
| 193 | - $ofp = fopen($outputFile, 'wb'); |
|
| 194 | - if (!is_resource($ofp)) { |
|
| 195 | - fclose($ifp); |
|
| 196 | - throw new Error('Could not open output file for writing'); |
|
| 193 | + $ofp = fopen( $outputFile, 'wb' ); |
|
| 194 | + if ( ! is_resource( $ofp ) ) { |
|
| 195 | + fclose( $ifp ); |
|
| 196 | + throw new Error( 'Could not open output file for writing' ); |
|
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | /** @var string $ephKeypair */ |
@@ -201,12 +201,12 @@ discard block |
||
| 201 | 201 | |
| 202 | 202 | /** @var string $msgKeypair */ |
| 203 | 203 | $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( |
| 204 | - ParagonIE_Sodium_Compat::crypto_box_secretkey($ephKeypair), |
|
| 204 | + ParagonIE_Sodium_Compat::crypto_box_secretkey( $ephKeypair ), |
|
| 205 | 205 | $publicKey |
| 206 | 206 | ); |
| 207 | 207 | |
| 208 | 208 | /** @var string $ephemeralPK */ |
| 209 | - $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey($ephKeypair); |
|
| 209 | + $ephemeralPK = ParagonIE_Sodium_Compat::crypto_box_publickey( $ephKeypair ); |
|
| 210 | 210 | |
| 211 | 211 | /** @var string $nonce */ |
| 212 | 212 | $nonce = ParagonIE_Sodium_Compat::crypto_generichash( |
@@ -221,27 +221,27 @@ discard block |
||
| 221 | 221 | $ephemeralPK, |
| 222 | 222 | ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES |
| 223 | 223 | ); |
| 224 | - if (!is_int($firstWrite)) { |
|
| 225 | - fclose($ifp); |
|
| 226 | - fclose($ofp); |
|
| 227 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 228 | - throw new Error('Could not write to output file'); |
|
| 229 | - } |
|
| 230 | - if ($firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
| 231 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 232 | - fclose($ifp); |
|
| 233 | - fclose($ofp); |
|
| 234 | - throw new Error('Error writing public key to output file'); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - $res = self::box_encrypt($ifp, $ofp, $size, $nonce, $msgKeypair); |
|
| 238 | - fclose($ifp); |
|
| 239 | - fclose($ofp); |
|
| 224 | + if ( ! is_int( $firstWrite ) ) { |
|
| 225 | + fclose( $ifp ); |
|
| 226 | + fclose( $ofp ); |
|
| 227 | + ParagonIE_Sodium_Compat::memzero( $ephKeypair ); |
|
| 228 | + throw new Error( 'Could not write to output file' ); |
|
| 229 | + } |
|
| 230 | + if ( $firstWrite !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES ) { |
|
| 231 | + ParagonIE_Sodium_Compat::memzero( $ephKeypair ); |
|
| 232 | + fclose( $ifp ); |
|
| 233 | + fclose( $ofp ); |
|
| 234 | + throw new Error( 'Error writing public key to output file' ); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + $res = self::box_encrypt( $ifp, $ofp, $size, $nonce, $msgKeypair ); |
|
| 238 | + fclose( $ifp ); |
|
| 239 | + fclose( $ofp ); |
|
| 240 | 240 | try { |
| 241 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
| 242 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 243 | - } catch (Error $ex) { |
|
| 244 | - unset($ephKeypair); |
|
| 241 | + ParagonIE_Sodium_Compat::memzero( $nonce ); |
|
| 242 | + ParagonIE_Sodium_Compat::memzero( $ephKeypair ); |
|
| 243 | + } catch ( Error $ex ) { |
|
| 244 | + unset( $ephKeypair ); |
|
| 245 | 245 | } |
| 246 | 246 | return $res; |
| 247 | 247 | } |
@@ -262,53 +262,53 @@ discard block |
||
| 262 | 262 | * @throws Error |
| 263 | 263 | * @throws TypeError |
| 264 | 264 | */ |
| 265 | - public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair) |
|
| 265 | + public static function box_seal_open( $inputFile, $outputFile, $ecdhKeypair ) |
|
| 266 | 266 | { |
| 267 | 267 | /* Type checks: */ |
| 268 | - if (!is_string($inputFile)) { |
|
| 269 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 268 | + if ( ! is_string( $inputFile ) ) { |
|
| 269 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' ); |
|
| 270 | 270 | } |
| 271 | - if (!is_string($outputFile)) { |
|
| 272 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 271 | + if ( ! is_string( $outputFile ) ) { |
|
| 272 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' ); |
|
| 273 | 273 | } |
| 274 | - if (!is_string($ecdhKeypair)) { |
|
| 275 | - throw new TypeError('Argument 3 must be a string, ' . gettype($ecdhKeypair) . ' given.'); |
|
| 274 | + if ( ! is_string( $ecdhKeypair ) ) { |
|
| 275 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $ecdhKeypair ) . ' given.' ); |
|
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | /* Input validation: */ |
| 279 | - if (self::strlen($ecdhKeypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
|
| 280 | - throw new TypeError('Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes'); |
|
| 279 | + if ( self::strlen( $ecdhKeypair ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES ) { |
|
| 280 | + throw new TypeError( 'Argument 3 must be CRYPTO_BOX_KEYPAIRBYTES bytes' ); |
|
| 281 | 281 | } |
| 282 | 282 | |
| 283 | - $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey($ecdhKeypair); |
|
| 283 | + $publicKey = ParagonIE_Sodium_Compat::crypto_box_publickey( $ecdhKeypair ); |
|
| 284 | 284 | |
| 285 | 285 | /** @var int $size */ |
| 286 | - $size = filesize($inputFile); |
|
| 287 | - if (!is_int($size)) { |
|
| 288 | - throw new Error('Could not obtain the file size'); |
|
| 286 | + $size = filesize( $inputFile ); |
|
| 287 | + if ( ! is_int( $size ) ) { |
|
| 288 | + throw new Error( 'Could not obtain the file size' ); |
|
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | /** @var resource $ifp */ |
| 292 | - $ifp = fopen($inputFile, 'rb'); |
|
| 293 | - if (!is_resource($ifp)) { |
|
| 294 | - throw new Error('Could not open input file for reading'); |
|
| 292 | + $ifp = fopen( $inputFile, 'rb' ); |
|
| 293 | + if ( ! is_resource( $ifp ) ) { |
|
| 294 | + throw new Error( 'Could not open input file for reading' ); |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | /** @var resource $ofp */ |
| 298 | - $ofp = fopen($outputFile, 'wb'); |
|
| 299 | - if (!is_resource($ofp)) { |
|
| 300 | - fclose($ifp); |
|
| 301 | - throw new Error('Could not open output file for writing'); |
|
| 298 | + $ofp = fopen( $outputFile, 'wb' ); |
|
| 299 | + if ( ! is_resource( $ofp ) ) { |
|
| 300 | + fclose( $ifp ); |
|
| 301 | + throw new Error( 'Could not open output file for writing' ); |
|
| 302 | 302 | } |
| 303 | 303 | |
| 304 | - $ephemeralPK = fread($ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES); |
|
| 305 | - if (!is_string($ephemeralPK)) { |
|
| 306 | - throw new Error('Could not read input file'); |
|
| 304 | + $ephemeralPK = fread( $ifp, ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES ); |
|
| 305 | + if ( ! is_string( $ephemeralPK ) ) { |
|
| 306 | + throw new Error( 'Could not read input file' ); |
|
| 307 | 307 | } |
| 308 | - if (self::strlen($ephemeralPK) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES) { |
|
| 309 | - fclose($ifp); |
|
| 310 | - fclose($ofp); |
|
| 311 | - throw new Error('Could not read public key from sealed file'); |
|
| 308 | + if ( self::strlen( $ephemeralPK ) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_PUBLICKEYBYTES ) { |
|
| 309 | + fclose( $ifp ); |
|
| 310 | + fclose( $ofp ); |
|
| 311 | + throw new Error( 'Could not read public key from sealed file' ); |
|
| 312 | 312 | } |
| 313 | 313 | |
| 314 | 314 | $nonce = ParagonIE_Sodium_Compat::crypto_generichash( |
@@ -317,18 +317,18 @@ discard block |
||
| 317 | 317 | 24 |
| 318 | 318 | ); |
| 319 | 319 | $msgKeypair = ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey( |
| 320 | - ParagonIE_Sodium_Compat::crypto_box_secretkey($ecdhKeypair), |
|
| 320 | + ParagonIE_Sodium_Compat::crypto_box_secretkey( $ecdhKeypair ), |
|
| 321 | 321 | $ephemeralPK |
| 322 | 322 | ); |
| 323 | 323 | |
| 324 | - $res = self::box_decrypt($ifp, $ofp, $size, $nonce, $msgKeypair); |
|
| 325 | - fclose($ifp); |
|
| 326 | - fclose($ofp); |
|
| 324 | + $res = self::box_decrypt( $ifp, $ofp, $size, $nonce, $msgKeypair ); |
|
| 325 | + fclose( $ifp ); |
|
| 326 | + fclose( $ofp ); |
|
| 327 | 327 | try { |
| 328 | - ParagonIE_Sodium_Compat::memzero($nonce); |
|
| 329 | - ParagonIE_Sodium_Compat::memzero($ephKeypair); |
|
| 330 | - } catch (Error $ex) { |
|
| 331 | - unset($ephKeypair); |
|
| 328 | + ParagonIE_Sodium_Compat::memzero( $nonce ); |
|
| 329 | + ParagonIE_Sodium_Compat::memzero( $ephKeypair ); |
|
| 330 | + } catch ( Error $ex ) { |
|
| 331 | + unset( $ephKeypair ); |
|
| 332 | 332 | } |
| 333 | 333 | return $res; |
| 334 | 334 | } |
@@ -345,68 +345,68 @@ discard block |
||
| 345 | 345 | * @throws TypeError |
| 346 | 346 | * @psalm-suppress FailedTypeResolution |
| 347 | 347 | */ |
| 348 | - public static function generichash($filePath, $key = '', $outputLength = 32) |
|
| 348 | + public static function generichash( $filePath, $key = '', $outputLength = 32 ) |
|
| 349 | 349 | { |
| 350 | 350 | /* Type checks: */ |
| 351 | - if (!is_string($filePath)) { |
|
| 352 | - throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
|
| 351 | + if ( ! is_string( $filePath ) ) { |
|
| 352 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $filePath ) . ' given.' ); |
|
| 353 | 353 | } |
| 354 | - if (!is_string($key)) { |
|
| 355 | - if (is_null($key)) { |
|
| 354 | + if ( ! is_string( $key ) ) { |
|
| 355 | + if ( is_null( $key ) ) { |
|
| 356 | 356 | $key = ''; |
| 357 | 357 | } else { |
| 358 | - throw new TypeError('Argument 2 must be a string, ' . gettype($key) . ' given.'); |
|
| 358 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $key ) . ' given.' ); |
|
| 359 | 359 | } |
| 360 | 360 | } |
| 361 | - if (!is_int($outputLength)) { |
|
| 362 | - if (!is_numeric($outputLength)) { |
|
| 363 | - throw new TypeError('Argument 3 must be an integer, ' . gettype($outputLength) . ' given.'); |
|
| 361 | + if ( ! is_int( $outputLength ) ) { |
|
| 362 | + if ( ! is_numeric( $outputLength ) ) { |
|
| 363 | + throw new TypeError( 'Argument 3 must be an integer, ' . gettype( $outputLength ) . ' given.' ); |
|
| 364 | 364 | } |
| 365 | 365 | $outputLength = (int) $outputLength; |
| 366 | 366 | } |
| 367 | 367 | |
| 368 | 368 | /* Input validation: */ |
| 369 | - if (!empty($key)) { |
|
| 370 | - if (self::strlen($key) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN) { |
|
| 371 | - throw new TypeError('Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes'); |
|
| 369 | + if ( ! empty( $key ) ) { |
|
| 370 | + if ( self::strlen( $key ) < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MIN ) { |
|
| 371 | + throw new TypeError( 'Argument 2 must be at least CRYPTO_GENERICHASH_KEYBYTES_MIN bytes' ); |
|
| 372 | 372 | } |
| 373 | - if (self::strlen($key) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX) { |
|
| 374 | - throw new TypeError('Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes'); |
|
| 373 | + if ( self::strlen( $key ) > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_KEYBYTES_MAX ) { |
|
| 374 | + throw new TypeError( 'Argument 2 must be at most CRYPTO_GENERICHASH_KEYBYTES_MAX bytes' ); |
|
| 375 | 375 | } |
| 376 | 376 | } |
| 377 | - if ($outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN) { |
|
| 378 | - throw new Error('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN'); |
|
| 377 | + if ( $outputLength < ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MIN ) { |
|
| 378 | + throw new Error( 'Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MIN' ); |
|
| 379 | 379 | } |
| 380 | - if ($outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX) { |
|
| 381 | - throw new Error('Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX'); |
|
| 380 | + if ( $outputLength > ParagonIE_Sodium_Compat::CRYPTO_GENERICHASH_BYTES_MAX ) { |
|
| 381 | + throw new Error( 'Argument 3 must be at least CRYPTO_GENERICHASH_BYTES_MAX' ); |
|
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | /** @var int $size */ |
| 385 | - $size = filesize($filePath); |
|
| 386 | - if (!is_int($size)) { |
|
| 387 | - throw new Error('Could not obtain the file size'); |
|
| 385 | + $size = filesize( $filePath ); |
|
| 386 | + if ( ! is_int( $size ) ) { |
|
| 387 | + throw new Error( 'Could not obtain the file size' ); |
|
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | /** @var resource $fp */ |
| 391 | - $fp = fopen($filePath, 'rb'); |
|
| 392 | - if (!is_resource($fp)) { |
|
| 393 | - throw new Error('Could not open input file for reading'); |
|
| 391 | + $fp = fopen( $filePath, 'rb' ); |
|
| 392 | + if ( ! is_resource( $fp ) ) { |
|
| 393 | + throw new Error( 'Could not open input file for reading' ); |
|
| 394 | 394 | } |
| 395 | - $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outputLength); |
|
| 396 | - while ($size > 0) { |
|
| 395 | + $ctx = ParagonIE_Sodium_Compat::crypto_generichash_init( $key, $outputLength ); |
|
| 396 | + while ( $size > 0 ) { |
|
| 397 | 397 | $blockSize = $size > 64 |
| 398 | 398 | ? 64 |
| 399 | 399 | : $size; |
| 400 | - $read = fread($fp, $blockSize); |
|
| 401 | - if (!is_string($read)) { |
|
| 402 | - throw new Error('Could not read input file'); |
|
| 400 | + $read = fread( $fp, $blockSize ); |
|
| 401 | + if ( ! is_string( $read ) ) { |
|
| 402 | + throw new Error( 'Could not read input file' ); |
|
| 403 | 403 | } |
| 404 | - ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $read); |
|
| 404 | + ParagonIE_Sodium_Compat::crypto_generichash_update( $ctx, $read ); |
|
| 405 | 405 | $size -= $blockSize; |
| 406 | 406 | } |
| 407 | 407 | |
| 408 | - fclose($fp); |
|
| 409 | - return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength); |
|
| 408 | + fclose( $fp ); |
|
| 409 | + return ParagonIE_Sodium_Compat::crypto_generichash_final( $ctx, $outputLength ); |
|
| 410 | 410 | } |
| 411 | 411 | |
| 412 | 412 | /** |
@@ -423,52 +423,52 @@ discard block |
||
| 423 | 423 | * @throws Error |
| 424 | 424 | * @throws TypeError |
| 425 | 425 | */ |
| 426 | - public static function secretbox($inputFile, $outputFile, $nonce, $key) |
|
| 426 | + public static function secretbox( $inputFile, $outputFile, $nonce, $key ) |
|
| 427 | 427 | { |
| 428 | 428 | /* Type checks: */ |
| 429 | - if (!is_string($inputFile)) { |
|
| 430 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..'); |
|
| 429 | + if ( ! is_string( $inputFile ) ) { |
|
| 430 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given..' ); |
|
| 431 | 431 | } |
| 432 | - if (!is_string($outputFile)) { |
|
| 433 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 432 | + if ( ! is_string( $outputFile ) ) { |
|
| 433 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' ); |
|
| 434 | 434 | } |
| 435 | - if (!is_string($nonce)) { |
|
| 436 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 435 | + if ( ! is_string( $nonce ) ) { |
|
| 436 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $nonce ) . ' given.' ); |
|
| 437 | 437 | } |
| 438 | 438 | |
| 439 | 439 | /* Input validation: */ |
| 440 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
| 441 | - throw new TypeError('Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
| 440 | + if ( self::strlen( $nonce ) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES ) { |
|
| 441 | + throw new TypeError( 'Argument 3 must be CRYPTO_SECRETBOX_NONCEBYTES bytes' ); |
|
| 442 | 442 | } |
| 443 | - if (!is_string($key)) { |
|
| 444 | - throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
| 443 | + if ( ! is_string( $key ) ) { |
|
| 444 | + throw new TypeError( 'Argument 4 must be a string, ' . gettype( $key ) . ' given.' ); |
|
| 445 | 445 | } |
| 446 | - if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
| 447 | - throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes'); |
|
| 446 | + if ( self::strlen( $key ) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES ) { |
|
| 447 | + throw new TypeError( 'Argument 4 must be CRYPTO_SECRETBOX_KEYBYTES bytes' ); |
|
| 448 | 448 | } |
| 449 | 449 | |
| 450 | 450 | /** @var int $size */ |
| 451 | - $size = filesize($inputFile); |
|
| 452 | - if (!is_int($size)) { |
|
| 453 | - throw new Error('Could not obtain the file size'); |
|
| 451 | + $size = filesize( $inputFile ); |
|
| 452 | + if ( ! is_int( $size ) ) { |
|
| 453 | + throw new Error( 'Could not obtain the file size' ); |
|
| 454 | 454 | } |
| 455 | 455 | |
| 456 | 456 | /** @var resource $ifp */ |
| 457 | - $ifp = fopen($inputFile, 'rb'); |
|
| 458 | - if (!is_resource($ifp)) { |
|
| 459 | - throw new Error('Could not open input file for reading'); |
|
| 457 | + $ifp = fopen( $inputFile, 'rb' ); |
|
| 458 | + if ( ! is_resource( $ifp ) ) { |
|
| 459 | + throw new Error( 'Could not open input file for reading' ); |
|
| 460 | 460 | } |
| 461 | 461 | |
| 462 | 462 | /** @var resource $ofp */ |
| 463 | - $ofp = fopen($outputFile, 'wb'); |
|
| 464 | - if (!is_resource($ofp)) { |
|
| 465 | - fclose($ifp); |
|
| 466 | - throw new Error('Could not open output file for writing'); |
|
| 463 | + $ofp = fopen( $outputFile, 'wb' ); |
|
| 464 | + if ( ! is_resource( $ofp ) ) { |
|
| 465 | + fclose( $ifp ); |
|
| 466 | + throw new Error( 'Could not open output file for writing' ); |
|
| 467 | 467 | } |
| 468 | 468 | |
| 469 | - $res = self::secretbox_encrypt($ifp, $ofp, $size, $nonce, $key); |
|
| 470 | - fclose($ifp); |
|
| 471 | - fclose($ofp); |
|
| 469 | + $res = self::secretbox_encrypt( $ifp, $ofp, $size, $nonce, $key ); |
|
| 470 | + fclose( $ifp ); |
|
| 471 | + fclose( $ofp ); |
|
| 472 | 472 | return $res; |
| 473 | 473 | } |
| 474 | 474 | /** |
@@ -488,56 +488,56 @@ discard block |
||
| 488 | 488 | * @throws Error |
| 489 | 489 | * @throws TypeError |
| 490 | 490 | */ |
| 491 | - public static function secretbox_open($inputFile, $outputFile, $nonce, $key) |
|
| 491 | + public static function secretbox_open( $inputFile, $outputFile, $nonce, $key ) |
|
| 492 | 492 | { |
| 493 | 493 | /* Type checks: */ |
| 494 | - if (!is_string($inputFile)) { |
|
| 495 | - throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
|
| 494 | + if ( ! is_string( $inputFile ) ) { |
|
| 495 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $inputFile ) . ' given.' ); |
|
| 496 | 496 | } |
| 497 | - if (!is_string($outputFile)) { |
|
| 498 | - throw new TypeError('Argument 2 must be a string, ' . gettype($outputFile) . ' given.'); |
|
| 497 | + if ( ! is_string( $outputFile ) ) { |
|
| 498 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $outputFile ) . ' given.' ); |
|
| 499 | 499 | } |
| 500 | - if (!is_string($nonce)) { |
|
| 501 | - throw new TypeError('Argument 3 must be a string, ' . gettype($nonce) . ' given.'); |
|
| 500 | + if ( ! is_string( $nonce ) ) { |
|
| 501 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $nonce ) . ' given.' ); |
|
| 502 | 502 | } |
| 503 | - if (!is_string($key)) { |
|
| 504 | - throw new TypeError('Argument 4 must be a string, ' . gettype($key) . ' given.'); |
|
| 503 | + if ( ! is_string( $key ) ) { |
|
| 504 | + throw new TypeError( 'Argument 4 must be a string, ' . gettype( $key ) . ' given.' ); |
|
| 505 | 505 | } |
| 506 | 506 | |
| 507 | 507 | /* Input validation: */ |
| 508 | - if (self::strlen($nonce) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES) { |
|
| 509 | - throw new TypeError('Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes'); |
|
| 508 | + if ( self::strlen( $nonce ) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_NONCEBYTES ) { |
|
| 509 | + throw new TypeError( 'Argument 4 must be CRYPTO_SECRETBOX_NONCEBYTES bytes' ); |
|
| 510 | 510 | } |
| 511 | - if (self::strlen($key) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES) { |
|
| 512 | - throw new TypeError('Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes'); |
|
| 511 | + if ( self::strlen( $key ) !== ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_KEYBYTES ) { |
|
| 512 | + throw new TypeError( 'Argument 4 must be CRYPTO_SECRETBOXBOX_KEYBYTES bytes' ); |
|
| 513 | 513 | } |
| 514 | 514 | |
| 515 | 515 | /** @var int $size */ |
| 516 | - $size = filesize($inputFile); |
|
| 517 | - if (!is_int($size)) { |
|
| 518 | - throw new Error('Could not obtain the file size'); |
|
| 516 | + $size = filesize( $inputFile ); |
|
| 517 | + if ( ! is_int( $size ) ) { |
|
| 518 | + throw new Error( 'Could not obtain the file size' ); |
|
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | /** @var resource $ifp */ |
| 522 | - $ifp = fopen($inputFile, 'rb'); |
|
| 523 | - if (!is_resource($ifp)) { |
|
| 524 | - throw new Error('Could not open input file for reading'); |
|
| 522 | + $ifp = fopen( $inputFile, 'rb' ); |
|
| 523 | + if ( ! is_resource( $ifp ) ) { |
|
| 524 | + throw new Error( 'Could not open input file for reading' ); |
|
| 525 | 525 | } |
| 526 | 526 | |
| 527 | 527 | /** @var resource $ofp */ |
| 528 | - $ofp = fopen($outputFile, 'wb'); |
|
| 529 | - if (!is_resource($ofp)) { |
|
| 530 | - fclose($ifp); |
|
| 531 | - throw new Error('Could not open output file for writing'); |
|
| 528 | + $ofp = fopen( $outputFile, 'wb' ); |
|
| 529 | + if ( ! is_resource( $ofp ) ) { |
|
| 530 | + fclose( $ifp ); |
|
| 531 | + throw new Error( 'Could not open output file for writing' ); |
|
| 532 | 532 | } |
| 533 | 533 | |
| 534 | - $res = self::secretbox_decrypt($ifp, $ofp, $size, $nonce, $key); |
|
| 535 | - fclose($ifp); |
|
| 536 | - fclose($ofp); |
|
| 534 | + $res = self::secretbox_decrypt( $ifp, $ofp, $size, $nonce, $key ); |
|
| 535 | + fclose( $ifp ); |
|
| 536 | + fclose( $ofp ); |
|
| 537 | 537 | try { |
| 538 | - ParagonIE_Sodium_Compat::memzero($key); |
|
| 539 | - } catch (Error $ex) { |
|
| 540 | - unset($key); |
|
| 538 | + ParagonIE_Sodium_Compat::memzero( $key ); |
|
| 539 | + } catch ( Error $ex ) { |
|
| 540 | + unset( $key ); |
|
| 541 | 541 | } |
| 542 | 542 | return $res; |
| 543 | 543 | } |
@@ -554,82 +554,82 @@ discard block |
||
| 554 | 554 | * @throws Error |
| 555 | 555 | * @throws TypeError |
| 556 | 556 | */ |
| 557 | - public static function sign($filePath, $secretKey) |
|
| 557 | + public static function sign( $filePath, $secretKey ) |
|
| 558 | 558 | { |
| 559 | 559 | /* Type checks: */ |
| 560 | - if (!is_string($filePath)) { |
|
| 561 | - throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
|
| 560 | + if ( ! is_string( $filePath ) ) { |
|
| 561 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $filePath ) . ' given.' ); |
|
| 562 | 562 | } |
| 563 | - if (!is_string($secretKey)) { |
|
| 564 | - throw new TypeError('Argument 2 must be a string, ' . gettype($secretKey) . ' given.'); |
|
| 563 | + if ( ! is_string( $secretKey ) ) { |
|
| 564 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $secretKey ) . ' given.' ); |
|
| 565 | 565 | } |
| 566 | 566 | |
| 567 | 567 | /* Input validation: */ |
| 568 | - if (self::strlen($secretKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES) { |
|
| 569 | - throw new TypeError('Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes'); |
|
| 568 | + if ( self::strlen( $secretKey ) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_SECRETKEYBYTES ) { |
|
| 569 | + throw new TypeError( 'Argument 2 must be CRYPTO_SIGN_SECRETKEYBYTES bytes' ); |
|
| 570 | 570 | } |
| 571 | 571 | |
| 572 | 572 | /** @var int $size */ |
| 573 | - $size = filesize($filePath); |
|
| 574 | - if (!is_int($size)) { |
|
| 575 | - throw new Error('Could not obtain the file size'); |
|
| 573 | + $size = filesize( $filePath ); |
|
| 574 | + if ( ! is_int( $size ) ) { |
|
| 575 | + throw new Error( 'Could not obtain the file size' ); |
|
| 576 | 576 | } |
| 577 | 577 | |
| 578 | 578 | /** @var resource $fp */ |
| 579 | - $fp = fopen($filePath, 'rb'); |
|
| 580 | - if (!is_resource($fp)) { |
|
| 581 | - throw new Error('Could not open input file for reading'); |
|
| 579 | + $fp = fopen( $filePath, 'rb' ); |
|
| 580 | + if ( ! is_resource( $fp ) ) { |
|
| 581 | + throw new Error( 'Could not open input file for reading' ); |
|
| 582 | 582 | } |
| 583 | 583 | |
| 584 | 584 | /** @var string $az */ |
| 585 | - $az = hash('sha512', self::substr($secretKey, 0, 32), true); |
|
| 585 | + $az = hash( 'sha512', self::substr( $secretKey, 0, 32 ), true ); |
|
| 586 | 586 | |
| 587 | - $az[0] = self::intToChr(self::chrToInt($az[0]) & 248); |
|
| 588 | - $az[31] = self::intToChr((self::chrToInt($az[31]) & 63) | 64); |
|
| 587 | + $az[0] = self::intToChr( self::chrToInt( $az[0] ) & 248 ); |
|
| 588 | + $az[31] = self::intToChr( ( self::chrToInt( $az[31] ) & 63 ) | 64 ); |
|
| 589 | 589 | |
| 590 | 590 | /** @var resource $hs */ |
| 591 | - $hs = hash_init('sha512'); |
|
| 592 | - hash_update($hs, self::substr($az, 32, 32)); |
|
| 593 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
| 591 | + $hs = hash_init( 'sha512' ); |
|
| 592 | + hash_update( $hs, self::substr( $az, 32, 32 ) ); |
|
| 593 | + $hs = self::updateHashWithFile( $hs, $fp, $size ); |
|
| 594 | 594 | |
| 595 | 595 | /** @var string $nonceHash */ |
| 596 | - $nonceHash = hash_final($hs, true); |
|
| 596 | + $nonceHash = hash_final( $hs, true ); |
|
| 597 | 597 | |
| 598 | 598 | /** @var string $pk */ |
| 599 | - $pk = self::substr($secretKey, 32, 32); |
|
| 599 | + $pk = self::substr( $secretKey, 32, 32 ); |
|
| 600 | 600 | |
| 601 | 601 | /** @var string $nonce */ |
| 602 | - $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce($nonceHash) . self::substr($nonceHash, 32); |
|
| 602 | + $nonce = ParagonIE_Sodium_Core_Ed25519::sc_reduce( $nonceHash ) . self::substr( $nonceHash, 32 ); |
|
| 603 | 603 | |
| 604 | 604 | /** @var string $sig */ |
| 605 | 605 | $sig = ParagonIE_Sodium_Core_Ed25519::ge_p3_tobytes( |
| 606 | - ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base($nonce) |
|
| 606 | + ParagonIE_Sodium_Core_Ed25519::ge_scalarmult_base( $nonce ) |
|
| 607 | 607 | ); |
| 608 | 608 | |
| 609 | 609 | /** @var resource $hs */ |
| 610 | - $hs = hash_init('sha512'); |
|
| 611 | - hash_update($hs, self::substr($sig, 0, 32)); |
|
| 612 | - hash_update($hs, self::substr($pk, 0, 32)); |
|
| 613 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
| 610 | + $hs = hash_init( 'sha512' ); |
|
| 611 | + hash_update( $hs, self::substr( $sig, 0, 32 ) ); |
|
| 612 | + hash_update( $hs, self::substr( $pk, 0, 32 ) ); |
|
| 613 | + $hs = self::updateHashWithFile( $hs, $fp, $size ); |
|
| 614 | 614 | |
| 615 | 615 | /** @var string $hramHash */ |
| 616 | - $hramHash = hash_final($hs, true); |
|
| 616 | + $hramHash = hash_final( $hs, true ); |
|
| 617 | 617 | |
| 618 | 618 | /** @var string $hram */ |
| 619 | - $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hramHash); |
|
| 619 | + $hram = ParagonIE_Sodium_Core_Ed25519::sc_reduce( $hramHash ); |
|
| 620 | 620 | |
| 621 | 621 | /** @var string $sigAfter */ |
| 622 | - $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd($hram, $az, $nonce); |
|
| 622 | + $sigAfter = ParagonIE_Sodium_Core_Ed25519::sc_muladd( $hram, $az, $nonce ); |
|
| 623 | 623 | |
| 624 | 624 | /** @var string $sig */ |
| 625 | - $sig = self::substr($sig, 0, 32) . self::substr($sigAfter, 0, 32); |
|
| 625 | + $sig = self::substr( $sig, 0, 32 ) . self::substr( $sigAfter, 0, 32 ); |
|
| 626 | 626 | |
| 627 | 627 | try { |
| 628 | - ParagonIE_Sodium_Compat::memzero($az); |
|
| 629 | - } catch (Error $ex) { |
|
| 628 | + ParagonIE_Sodium_Compat::memzero( $az ); |
|
| 629 | + } catch ( Error $ex ) { |
|
| 630 | 630 | $az = null; |
| 631 | 631 | } |
| 632 | - fclose($fp); |
|
| 632 | + fclose( $fp ); |
|
| 633 | 633 | return $sig; |
| 634 | 634 | } |
| 635 | 635 | |
@@ -646,58 +646,58 @@ discard block |
||
| 646 | 646 | * @throws Error |
| 647 | 647 | * @throws Exception |
| 648 | 648 | */ |
| 649 | - public static function verify($sig, $filePath, $publicKey) |
|
| 649 | + public static function verify( $sig, $filePath, $publicKey ) |
|
| 650 | 650 | { |
| 651 | 651 | /* Type checks: */ |
| 652 | - if (!is_string($sig)) { |
|
| 653 | - throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.'); |
|
| 652 | + if ( ! is_string( $sig ) ) { |
|
| 653 | + throw new TypeError( 'Argument 1 must be a string, ' . gettype( $sig ) . ' given.' ); |
|
| 654 | 654 | } |
| 655 | - if (!is_string($filePath)) { |
|
| 656 | - throw new TypeError('Argument 2 must be a string, ' . gettype($filePath) . ' given.'); |
|
| 655 | + if ( ! is_string( $filePath ) ) { |
|
| 656 | + throw new TypeError( 'Argument 2 must be a string, ' . gettype( $filePath ) . ' given.' ); |
|
| 657 | 657 | } |
| 658 | - if (!is_string($publicKey)) { |
|
| 659 | - throw new TypeError('Argument 3 must be a string, ' . gettype($publicKey) . ' given.'); |
|
| 658 | + if ( ! is_string( $publicKey ) ) { |
|
| 659 | + throw new TypeError( 'Argument 3 must be a string, ' . gettype( $publicKey ) . ' given.' ); |
|
| 660 | 660 | } |
| 661 | 661 | |
| 662 | 662 | /* Input validation: */ |
| 663 | - if (self::strlen($sig) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES) { |
|
| 664 | - throw new TypeError('Argument 1 must be CRYPTO_SIGN_BYTES bytes'); |
|
| 663 | + if ( self::strlen( $sig ) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_BYTES ) { |
|
| 664 | + throw new TypeError( 'Argument 1 must be CRYPTO_SIGN_BYTES bytes' ); |
|
| 665 | 665 | } |
| 666 | - if (self::strlen($publicKey) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES) { |
|
| 667 | - throw new TypeError('Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes'); |
|
| 666 | + if ( self::strlen( $publicKey ) !== ParagonIE_Sodium_Compat::CRYPTO_SIGN_PUBLICKEYBYTES ) { |
|
| 667 | + throw new TypeError( 'Argument 3 must be CRYPTO_SIGN_PUBLICKEYBYTES bytes' ); |
|
| 668 | 668 | } |
| 669 | - if (self::strlen($sig) < 64) { |
|
| 670 | - throw new Exception('Signature is too short'); |
|
| 669 | + if ( self::strlen( $sig ) < 64 ) { |
|
| 670 | + throw new Exception( 'Signature is too short' ); |
|
| 671 | 671 | } |
| 672 | 672 | |
| 673 | 673 | /* Security checks */ |
| 674 | - if (ParagonIE_Sodium_Core_Ed25519::check_S_lt_L(self::substr($sig, 32, 32))) { |
|
| 675 | - throw new Exception('S < L - Invalid signature'); |
|
| 674 | + if ( ParagonIE_Sodium_Core_Ed25519::check_S_lt_L( self::substr( $sig, 32, 32 ) ) ) { |
|
| 675 | + throw new Exception( 'S < L - Invalid signature' ); |
|
| 676 | 676 | } |
| 677 | - if (ParagonIE_Sodium_Core_Ed25519::small_order($sig)) { |
|
| 678 | - throw new Exception('Signature is on too small of an order'); |
|
| 677 | + if ( ParagonIE_Sodium_Core_Ed25519::small_order( $sig ) ) { |
|
| 678 | + throw new Exception( 'Signature is on too small of an order' ); |
|
| 679 | 679 | } |
| 680 | - if ((self::chrToInt($sig[63]) & 224) !== 0) { |
|
| 681 | - throw new Exception('Invalid signature'); |
|
| 680 | + if ( ( self::chrToInt( $sig[63] ) & 224 ) !== 0 ) { |
|
| 681 | + throw new Exception( 'Invalid signature' ); |
|
| 682 | 682 | } |
| 683 | 683 | $d = 0; |
| 684 | - for ($i = 0; $i < 32; ++$i) { |
|
| 685 | - $d |= self::chrToInt($publicKey[$i]); |
|
| 684 | + for ( $i = 0; $i < 32; ++$i ) { |
|
| 685 | + $d |= self::chrToInt( $publicKey[$i] ); |
|
| 686 | 686 | } |
| 687 | - if ($d === 0) { |
|
| 688 | - throw new Exception('All zero public key'); |
|
| 687 | + if ( $d === 0 ) { |
|
| 688 | + throw new Exception( 'All zero public key' ); |
|
| 689 | 689 | } |
| 690 | 690 | |
| 691 | 691 | /** @var int $size */ |
| 692 | - $size = filesize($filePath); |
|
| 693 | - if (!is_int($size)) { |
|
| 694 | - throw new Error('Could not obtain the file size'); |
|
| 692 | + $size = filesize( $filePath ); |
|
| 693 | + if ( ! is_int( $size ) ) { |
|
| 694 | + throw new Error( 'Could not obtain the file size' ); |
|
| 695 | 695 | } |
| 696 | 696 | |
| 697 | 697 | /** @var resource $fp */ |
| 698 | - $fp = fopen($filePath, 'rb'); |
|
| 699 | - if (!is_resource($fp)) { |
|
| 700 | - throw new Error('Could not open input file for reading'); |
|
| 698 | + $fp = fopen( $filePath, 'rb' ); |
|
| 699 | + if ( ! is_resource( $fp ) ) { |
|
| 700 | + throw new Error( 'Could not open input file for reading' ); |
|
| 701 | 701 | } |
| 702 | 702 | |
| 703 | 703 | /** @var bool The original value of ParagonIE_Sodium_Compat::$fastMult */ |
@@ -707,35 +707,35 @@ discard block |
||
| 707 | 707 | ParagonIE_Sodium_Compat::$fastMult = true; |
| 708 | 708 | |
| 709 | 709 | /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P3 $A */ |
| 710 | - $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime($publicKey); |
|
| 710 | + $A = ParagonIE_Sodium_Core_Ed25519::ge_frombytes_negate_vartime( $publicKey ); |
|
| 711 | 711 | |
| 712 | 712 | /** @var resource $hs */ |
| 713 | - $hs = hash_init('sha512'); |
|
| 714 | - hash_update($hs, self::substr($sig, 0, 32)); |
|
| 715 | - hash_update($hs, self::substr($publicKey, 0, 32)); |
|
| 716 | - $hs = self::updateHashWithFile($hs, $fp, $size); |
|
| 713 | + $hs = hash_init( 'sha512' ); |
|
| 714 | + hash_update( $hs, self::substr( $sig, 0, 32 ) ); |
|
| 715 | + hash_update( $hs, self::substr( $publicKey, 0, 32 ) ); |
|
| 716 | + $hs = self::updateHashWithFile( $hs, $fp, $size ); |
|
| 717 | 717 | /** @var string $hDigest */ |
| 718 | - $hDigest = hash_final($hs, true); |
|
| 718 | + $hDigest = hash_final( $hs, true ); |
|
| 719 | 719 | |
| 720 | 720 | /** @var string $h */ |
| 721 | - $h = ParagonIE_Sodium_Core_Ed25519::sc_reduce($hDigest) . self::substr($hDigest, 32); |
|
| 721 | + $h = ParagonIE_Sodium_Core_Ed25519::sc_reduce( $hDigest ) . self::substr( $hDigest, 32 ); |
|
| 722 | 722 | |
| 723 | 723 | /** @var ParagonIE_Sodium_Core_Curve25519_Ge_P2 $R */ |
| 724 | 724 | $R = ParagonIE_Sodium_Core_Ed25519::ge_double_scalarmult_vartime( |
| 725 | 725 | $h, |
| 726 | 726 | $A, |
| 727 | - self::substr($sig, 32) |
|
| 727 | + self::substr( $sig, 32 ) |
|
| 728 | 728 | ); |
| 729 | 729 | |
| 730 | 730 | /** @var string $rcheck */ |
| 731 | - $rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes($R); |
|
| 731 | + $rcheck = ParagonIE_Sodium_Core_Ed25519::ge_tobytes( $R ); |
|
| 732 | 732 | |
| 733 | 733 | // Close the file handle |
| 734 | - fclose($fp); |
|
| 734 | + fclose( $fp ); |
|
| 735 | 735 | |
| 736 | 736 | // Reset ParagonIE_Sodium_Compat::$fastMult to what it was before. |
| 737 | 737 | ParagonIE_Sodium_Compat::$fastMult = $orig; |
| 738 | - return self::verify_32($rcheck, self::substr($sig, 0, 32)); |
|
| 738 | + return self::verify_32( $rcheck, self::substr( $sig, 0, 32 ) ); |
|
| 739 | 739 | } |
| 740 | 740 | |
| 741 | 741 | /** |
@@ -746,7 +746,7 @@ discard block |
||
| 746 | 746 | * @param string $boxKeypair |
| 747 | 747 | * @return bool |
| 748 | 748 | */ |
| 749 | - protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
| 749 | + protected static function box_encrypt( $ifp, $ofp, $mlen, $nonce, $boxKeypair ) |
|
| 750 | 750 | { |
| 751 | 751 | return self::secretbox_encrypt( |
| 752 | 752 | $ifp, |
@@ -754,8 +754,8 @@ discard block |
||
| 754 | 754 | $mlen, |
| 755 | 755 | $nonce, |
| 756 | 756 | ParagonIE_Sodium_Crypto::box_beforenm( |
| 757 | - ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), |
|
| 758 | - ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) |
|
| 757 | + ParagonIE_Sodium_Crypto::box_secretkey( $boxKeypair ), |
|
| 758 | + ParagonIE_Sodium_Crypto::box_publickey( $boxKeypair ) |
|
| 759 | 759 | ) |
| 760 | 760 | ); |
| 761 | 761 | } |
@@ -769,7 +769,7 @@ discard block |
||
| 769 | 769 | * @param string $boxKeypair |
| 770 | 770 | * @return bool |
| 771 | 771 | */ |
| 772 | - protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
| 772 | + protected static function box_decrypt( $ifp, $ofp, $mlen, $nonce, $boxKeypair ) |
|
| 773 | 773 | { |
| 774 | 774 | return self::secretbox_decrypt( |
| 775 | 775 | $ifp, |
@@ -777,8 +777,8 @@ discard block |
||
| 777 | 777 | $mlen, |
| 778 | 778 | $nonce, |
| 779 | 779 | ParagonIE_Sodium_Crypto::box_beforenm( |
| 780 | - ParagonIE_Sodium_Crypto::box_secretkey($boxKeypair), |
|
| 781 | - ParagonIE_Sodium_Crypto::box_publickey($boxKeypair) |
|
| 780 | + ParagonIE_Sodium_Crypto::box_secretkey( $boxKeypair ), |
|
| 781 | + ParagonIE_Sodium_Crypto::box_publickey( $boxKeypair ) |
|
| 782 | 782 | ) |
| 783 | 783 | ); |
| 784 | 784 | } |
@@ -794,29 +794,29 @@ discard block |
||
| 794 | 794 | * @return bool |
| 795 | 795 | * @throws Error |
| 796 | 796 | */ |
| 797 | - protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
| 797 | + protected static function secretbox_encrypt( $ifp, $ofp, $mlen, $nonce, $key ) |
|
| 798 | 798 | { |
| 799 | - $plaintext = fread($ifp, 32); |
|
| 800 | - if (!is_string($plaintext)) { |
|
| 801 | - throw new Error('Could not read input file'); |
|
| 799 | + $plaintext = fread( $ifp, 32 ); |
|
| 800 | + if ( ! is_string( $plaintext ) ) { |
|
| 801 | + throw new Error( 'Could not read input file' ); |
|
| 802 | 802 | } |
| 803 | - $first32 = ftell($ifp); |
|
| 803 | + $first32 = ftell( $ifp ); |
|
| 804 | 804 | |
| 805 | 805 | /** @var string $subkey */ |
| 806 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
| 806 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20( $nonce, $key ); |
|
| 807 | 807 | |
| 808 | 808 | /** @var string $realNonce */ |
| 809 | - $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
| 809 | + $realNonce = ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ); |
|
| 810 | 810 | |
| 811 | 811 | /** @var string $block0 */ |
| 812 | - $block0 = str_repeat("\x00", 32); |
|
| 812 | + $block0 = str_repeat( "\x00", 32 ); |
|
| 813 | 813 | |
| 814 | 814 | /** @var int $mlen - Length of the plaintext message */ |
| 815 | 815 | $mlen0 = $mlen; |
| 816 | - if ($mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES) { |
|
| 816 | + if ( $mlen0 > 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES ) { |
|
| 817 | 817 | $mlen0 = 64 - ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES; |
| 818 | 818 | } |
| 819 | - $block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
|
| 819 | + $block0 .= ParagonIE_Sodium_Core_Util::substr( $plaintext, 0, $mlen0 ); |
|
| 820 | 820 | |
| 821 | 821 | /** @var string $block0 */ |
| 822 | 822 | $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
@@ -834,16 +834,16 @@ discard block |
||
| 834 | 834 | ); |
| 835 | 835 | |
| 836 | 836 | // Pre-write 16 blank bytes for the Poly1305 tag |
| 837 | - $start = ftell($ofp); |
|
| 838 | - fwrite($ofp, str_repeat("\x00", 16)); |
|
| 837 | + $start = ftell( $ofp ); |
|
| 838 | + fwrite( $ofp, str_repeat( "\x00", 16 ) ); |
|
| 839 | 839 | |
| 840 | 840 | /** @var string $c */ |
| 841 | 841 | $cBlock = ParagonIE_Sodium_Core_Util::substr( |
| 842 | 842 | $block0, |
| 843 | 843 | ParagonIE_Sodium_Crypto::secretbox_xsalsa20poly1305_ZEROBYTES |
| 844 | 844 | ); |
| 845 | - $state->update($cBlock); |
|
| 846 | - fwrite($ofp, $cBlock); |
|
| 845 | + $state->update( $cBlock ); |
|
| 846 | + fwrite( $ofp, $cBlock ); |
|
| 847 | 847 | $mlen -= 32; |
| 848 | 848 | |
| 849 | 849 | /** @var int $iter */ |
@@ -856,15 +856,15 @@ discard block |
||
| 856 | 856 | * Set the cursor to the end of the first half-block. All future bytes will |
| 857 | 857 | * generated from salsa20_xor_ic, starting from 1 (second block). |
| 858 | 858 | */ |
| 859 | - fseek($ifp, $first32, SEEK_SET); |
|
| 859 | + fseek( $ifp, $first32, SEEK_SET ); |
|
| 860 | 860 | |
| 861 | - while ($mlen > 0) { |
|
| 861 | + while ( $mlen > 0 ) { |
|
| 862 | 862 | $blockSize = $mlen > self::BUFFER_SIZE |
| 863 | 863 | ? self::BUFFER_SIZE |
| 864 | 864 | : $mlen; |
| 865 | - $plaintext = fread($ifp, $blockSize); |
|
| 866 | - if (!is_string($plaintext)) { |
|
| 867 | - throw new Error('Could not read input file'); |
|
| 865 | + $plaintext = fread( $ifp, $blockSize ); |
|
| 866 | + if ( ! is_string( $plaintext ) ) { |
|
| 867 | + throw new Error( 'Could not read input file' ); |
|
| 868 | 868 | } |
| 869 | 869 | $cBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
| 870 | 870 | $plaintext, |
@@ -872,29 +872,29 @@ discard block |
||
| 872 | 872 | $iter, |
| 873 | 873 | $subkey |
| 874 | 874 | ); |
| 875 | - fwrite($ofp, $cBlock, $blockSize); |
|
| 876 | - $state->update($cBlock); |
|
| 875 | + fwrite( $ofp, $cBlock, $blockSize ); |
|
| 876 | + $state->update( $cBlock ); |
|
| 877 | 877 | |
| 878 | 878 | $mlen -= $blockSize; |
| 879 | 879 | $iter += $incr; |
| 880 | 880 | } |
| 881 | 881 | try { |
| 882 | - ParagonIE_Sodium_Compat::memzero($block0); |
|
| 883 | - ParagonIE_Sodium_Compat::memzero($subkey); |
|
| 884 | - } catch (Error $ex) { |
|
| 882 | + ParagonIE_Sodium_Compat::memzero( $block0 ); |
|
| 883 | + ParagonIE_Sodium_Compat::memzero( $subkey ); |
|
| 884 | + } catch ( Error $ex ) { |
|
| 885 | 885 | $block0 = null; |
| 886 | 886 | $subkey = null; |
| 887 | 887 | } |
| 888 | - $end = ftell($ofp); |
|
| 888 | + $end = ftell( $ofp ); |
|
| 889 | 889 | |
| 890 | 890 | /* |
| 891 | 891 | * Write the Poly1305 authentication tag that provides integrity |
| 892 | 892 | * over the ciphertext (encrypt-then-MAC) |
| 893 | 893 | */ |
| 894 | - fseek($ofp, $start, SEEK_SET); |
|
| 895 | - fwrite($ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES); |
|
| 896 | - fseek($ofp, $end, SEEK_SET); |
|
| 897 | - unset($state); |
|
| 894 | + fseek( $ofp, $start, SEEK_SET ); |
|
| 895 | + fwrite( $ofp, $state->finish(), ParagonIE_Sodium_Compat::CRYPTO_SECRETBOX_MACBYTES ); |
|
| 896 | + fseek( $ofp, $end, SEEK_SET ); |
|
| 897 | + unset( $state ); |
|
| 898 | 898 | |
| 899 | 899 | return true; |
| 900 | 900 | } |
@@ -911,46 +911,46 @@ discard block |
||
| 911 | 911 | * @throws Error |
| 912 | 912 | * @throws Exception |
| 913 | 913 | */ |
| 914 | - protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
| 914 | + protected static function secretbox_decrypt( $ifp, $ofp, $mlen, $nonce, $key ) |
|
| 915 | 915 | { |
| 916 | - $tag = fread($ifp, 16); |
|
| 917 | - if (!is_string($tag)) { |
|
| 918 | - throw new Error('Could not read input file'); |
|
| 916 | + $tag = fread( $ifp, 16 ); |
|
| 917 | + if ( ! is_string( $tag ) ) { |
|
| 918 | + throw new Error( 'Could not read input file' ); |
|
| 919 | 919 | } |
| 920 | 920 | |
| 921 | 921 | /** @var string $subkey */ |
| 922 | - $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
|
| 922 | + $subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20( $nonce, $key ); |
|
| 923 | 923 | |
| 924 | 924 | /** @var string $realNonce */ |
| 925 | - $realNonce = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
|
| 925 | + $realNonce = ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ); |
|
| 926 | 926 | |
| 927 | 927 | /** @var string $block0 */ |
| 928 | 928 | $block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
| 929 | 929 | 64, |
| 930 | - ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
|
| 930 | + ParagonIE_Sodium_Core_Util::substr( $nonce, 16, 8 ), |
|
| 931 | 931 | $subkey |
| 932 | 932 | ); |
| 933 | 933 | |
| 934 | 934 | /* Verify the Poly1305 MAC -before- attempting to decrypt! */ |
| 935 | - $state = new ParagonIE_Sodium_Core_Poly1305_State(self::substr($block0, 0, 32)); |
|
| 936 | - if (!self::onetimeauth_verify($state, $ifp, $tag, $mlen)) { |
|
| 937 | - throw new Exception('Invalid MAC'); |
|
| 935 | + $state = new ParagonIE_Sodium_Core_Poly1305_State( self::substr( $block0, 0, 32 ) ); |
|
| 936 | + if ( ! self::onetimeauth_verify( $state, $ifp, $tag, $mlen ) ) { |
|
| 937 | + throw new Exception( 'Invalid MAC' ); |
|
| 938 | 938 | } |
| 939 | 939 | |
| 940 | 940 | /* |
| 941 | 941 | * Set the cursor to the end of the first half-block. All future bytes will |
| 942 | 942 | * generated from salsa20_xor_ic, starting from 1 (second block). |
| 943 | 943 | */ |
| 944 | - $first32 = fread($ifp, 32); |
|
| 945 | - if (!is_string($first32)) { |
|
| 946 | - throw new Error('Could not read input file'); |
|
| 944 | + $first32 = fread( $ifp, 32 ); |
|
| 945 | + if ( ! is_string( $first32 ) ) { |
|
| 946 | + throw new Error( 'Could not read input file' ); |
|
| 947 | 947 | } |
| 948 | - $first32len = self::strlen($first32); |
|
| 948 | + $first32len = self::strlen( $first32 ); |
|
| 949 | 949 | fwrite( |
| 950 | 950 | $ofp, |
| 951 | 951 | self::xorStrings( |
| 952 | - self::substr($block0, 32, $first32len), |
|
| 953 | - self::substr($first32, 0, $first32len) |
|
| 952 | + self::substr( $block0, 32, $first32len ), |
|
| 953 | + self::substr( $first32, 0, $first32len ) |
|
| 954 | 954 | ) |
| 955 | 955 | ); |
| 956 | 956 | $mlen -= 32; |
@@ -962,13 +962,13 @@ discard block |
||
| 962 | 962 | $incr = self::BUFFER_SIZE >> 6; |
| 963 | 963 | |
| 964 | 964 | /* Decrypts ciphertext, writes to output file. */ |
| 965 | - while ($mlen > 0) { |
|
| 965 | + while ( $mlen > 0 ) { |
|
| 966 | 966 | $blockSize = $mlen > self::BUFFER_SIZE |
| 967 | 967 | ? self::BUFFER_SIZE |
| 968 | 968 | : $mlen; |
| 969 | - $ciphertext = fread($ifp, $blockSize); |
|
| 970 | - if (!is_string($ciphertext)) { |
|
| 971 | - throw new Error('Could not read input file'); |
|
| 969 | + $ciphertext = fread( $ifp, $blockSize ); |
|
| 970 | + if ( ! is_string( $ciphertext ) ) { |
|
| 971 | + throw new Error( 'Could not read input file' ); |
|
| 972 | 972 | } |
| 973 | 973 | $pBlock = ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
| 974 | 974 | $ciphertext, |
@@ -976,7 +976,7 @@ discard block |
||
| 976 | 976 | $iter, |
| 977 | 977 | $subkey |
| 978 | 978 | ); |
| 979 | - fwrite($ofp, $pBlock, $blockSize); |
|
| 979 | + fwrite( $ofp, $pBlock, $blockSize ); |
|
| 980 | 980 | $mlen -= $blockSize; |
| 981 | 981 | $iter += $incr; |
| 982 | 982 | } |
@@ -991,10 +991,10 @@ discard block |
||
| 991 | 991 | * @return bool |
| 992 | 992 | * @throws Error |
| 993 | 993 | */ |
| 994 | - protected static function onetimeauth_verify(ParagonIE_Sodium_Core_Poly1305_State $state, $ifp, $tag = '', $mlen = 0) |
|
| 994 | + protected static function onetimeauth_verify( ParagonIE_Sodium_Core_Poly1305_State $state, $ifp, $tag = '', $mlen = 0 ) |
|
| 995 | 995 | { |
| 996 | 996 | /** @var int $pos */ |
| 997 | - $pos = ftell($ifp); |
|
| 997 | + $pos = ftell( $ifp ); |
|
| 998 | 998 | |
| 999 | 999 | /** @var int $iter */ |
| 1000 | 1000 | $iter = 1; |
@@ -1002,21 +1002,21 @@ discard block |
||
| 1002 | 1002 | /** @var int $incr */ |
| 1003 | 1003 | $incr = self::BUFFER_SIZE >> 6; |
| 1004 | 1004 | |
| 1005 | - while ($mlen > 0) { |
|
| 1005 | + while ( $mlen > 0 ) { |
|
| 1006 | 1006 | $blockSize = $mlen > self::BUFFER_SIZE |
| 1007 | 1007 | ? self::BUFFER_SIZE |
| 1008 | 1008 | : $mlen; |
| 1009 | - $ciphertext = fread($ifp, $blockSize); |
|
| 1010 | - if (!is_string($ciphertext)) { |
|
| 1011 | - throw new Error('Could not read input file'); |
|
| 1009 | + $ciphertext = fread( $ifp, $blockSize ); |
|
| 1010 | + if ( ! is_string( $ciphertext ) ) { |
|
| 1011 | + throw new Error( 'Could not read input file' ); |
|
| 1012 | 1012 | } |
| 1013 | - $state->update($ciphertext); |
|
| 1013 | + $state->update( $ciphertext ); |
|
| 1014 | 1014 | $mlen -= $blockSize; |
| 1015 | 1015 | $iter += $incr; |
| 1016 | 1016 | } |
| 1017 | - $res = ParagonIE_Sodium_Core_Util::verify_16($tag, $state->finish()); |
|
| 1017 | + $res = ParagonIE_Sodium_Core_Util::verify_16( $tag, $state->finish() ); |
|
| 1018 | 1018 | |
| 1019 | - fseek($ifp, $pos, SEEK_SET); |
|
| 1019 | + fseek( $ifp, $pos, SEEK_SET ); |
|
| 1020 | 1020 | return $res; |
| 1021 | 1021 | } |
| 1022 | 1022 | |
@@ -1034,47 +1034,47 @@ discard block |
||
| 1034 | 1034 | * PHP 7.2 changes from a resource to an object, |
| 1035 | 1035 | * which causes Psalm to complain about an error. |
| 1036 | 1036 | */ |
| 1037 | - public static function updateHashWithFile($hash, $fp, $size = 0) |
|
| 1037 | + public static function updateHashWithFile( $hash, $fp, $size = 0 ) |
|
| 1038 | 1038 | { |
| 1039 | 1039 | /* Type checks: */ |
| 1040 | - if (PHP_VERSION_ID < 70200) { |
|
| 1041 | - if (!is_resource($hash)) { |
|
| 1042 | - throw new TypeError('Argument 1 must be a resource, ' . gettype($hash) . ' given.'); |
|
| 1040 | + if ( PHP_VERSION_ID < 70200 ) { |
|
| 1041 | + if ( ! is_resource( $hash ) ) { |
|
| 1042 | + throw new TypeError( 'Argument 1 must be a resource, ' . gettype( $hash ) . ' given.' ); |
|
| 1043 | 1043 | } |
| 1044 | 1044 | |
| 1045 | 1045 | } else { |
| 1046 | - if (!is_object($hash)) { |
|
| 1047 | - throw new TypeError('Argument 1 must be an object (PHP 7.2+), ' . gettype($hash) . ' given.'); |
|
| 1046 | + if ( ! is_object( $hash ) ) { |
|
| 1047 | + throw new TypeError( 'Argument 1 must be an object (PHP 7.2+), ' . gettype( $hash ) . ' given.' ); |
|
| 1048 | 1048 | } |
| 1049 | 1049 | } |
| 1050 | - if (!is_resource($fp)) { |
|
| 1051 | - throw new TypeError('Argument 2 must be a resource, ' . gettype($fp) . ' given.'); |
|
| 1050 | + if ( ! is_resource( $fp ) ) { |
|
| 1051 | + throw new TypeError( 'Argument 2 must be a resource, ' . gettype( $fp ) . ' given.' ); |
|
| 1052 | 1052 | } |
| 1053 | - if (!is_int($size)) { |
|
| 1054 | - throw new TypeError('Argument 3 must be an integer, ' . gettype($size) . ' given.'); |
|
| 1053 | + if ( ! is_int( $size ) ) { |
|
| 1054 | + throw new TypeError( 'Argument 3 must be an integer, ' . gettype( $size ) . ' given.' ); |
|
| 1055 | 1055 | } |
| 1056 | 1056 | |
| 1057 | 1057 | /** @var int $originalPosition */ |
| 1058 | - $originalPosition = ftell($fp); |
|
| 1058 | + $originalPosition = ftell( $fp ); |
|
| 1059 | 1059 | |
| 1060 | 1060 | // Move file pointer to beginning of file |
| 1061 | - fseek($fp, 0, SEEK_SET); |
|
| 1062 | - for ($i = 0; $i < $size; $i += self::BUFFER_SIZE) { |
|
| 1061 | + fseek( $fp, 0, SEEK_SET ); |
|
| 1062 | + for ( $i = 0; $i < $size; $i += self::BUFFER_SIZE ) { |
|
| 1063 | 1063 | /** @var string $message */ |
| 1064 | 1064 | $message = fread( |
| 1065 | 1065 | $fp, |
| 1066 | - ($size - $i) > self::BUFFER_SIZE |
|
| 1066 | + ( $size - $i ) > self::BUFFER_SIZE |
|
| 1067 | 1067 | ? $size - $i |
| 1068 | 1068 | : self::BUFFER_SIZE |
| 1069 | 1069 | ); |
| 1070 | - if (!is_string($message)) { |
|
| 1071 | - throw new Error('Unexpected error reading from file.'); |
|
| 1070 | + if ( ! is_string( $message ) ) { |
|
| 1071 | + throw new Error( 'Unexpected error reading from file.' ); |
|
| 1072 | 1072 | } |
| 1073 | 1073 | /** @psalm-suppress InvalidArgument */ |
| 1074 | - hash_update($hash, $message); |
|
| 1074 | + hash_update( $hash, $message ); |
|
| 1075 | 1075 | } |
| 1076 | 1076 | // Reset file pointer's position |
| 1077 | - fseek($fp, $originalPosition, SEEK_SET); |
|
| 1077 | + fseek( $fp, $originalPosition, SEEK_SET ); |
|
| 1078 | 1078 | return $hash; |
| 1079 | 1079 | } |
| 1080 | 1080 | } |
@@ -6,8 +6,7 @@ discard block |
||
| 6 | 6 | /** |
| 7 | 7 | * Class ParagonIE_Sodium_File |
| 8 | 8 | */ |
| 9 | -class ParagonIE_Sodium_File extends ParagonIE_Sodium_Core_Util |
|
| 10 | -{ |
|
| 9 | +class ParagonIE_Sodium_File extends ParagonIE_Sodium_Core_Util { |
|
| 11 | 10 | /* PHP's default buffer size is 8192 for fread()/fwrite(). */ |
| 12 | 11 | const BUFFER_SIZE = 8192; |
| 13 | 12 | |
@@ -25,8 +24,7 @@ discard block |
||
| 25 | 24 | * @throws Error |
| 26 | 25 | * @throws TypeError |
| 27 | 26 | */ |
| 28 | - public static function box($inputFile, $outputFile, $nonce, $keyPair) |
|
| 29 | - { |
|
| 27 | + public static function box($inputFile, $outputFile, $nonce, $keyPair) { |
|
| 30 | 28 | /* Type checks: */ |
| 31 | 29 | if (!is_string($inputFile)) { |
| 32 | 30 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
@@ -91,8 +89,7 @@ discard block |
||
| 91 | 89 | * @throws Error |
| 92 | 90 | * @throws TypeError |
| 93 | 91 | */ |
| 94 | - public static function box_open($inputFile, $outputFile, $nonce, $keypair) |
|
| 95 | - { |
|
| 92 | + public static function box_open($inputFile, $outputFile, $nonce, $keypair) { |
|
| 96 | 93 | /* Type checks: */ |
| 97 | 94 | if (!is_string($inputFile)) { |
| 98 | 95 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
@@ -159,8 +156,7 @@ discard block |
||
| 159 | 156 | * @throws Error |
| 160 | 157 | * @throws TypeError |
| 161 | 158 | */ |
| 162 | - public static function box_seal($inputFile, $outputFile, $publicKey) |
|
| 163 | - { |
|
| 159 | + public static function box_seal($inputFile, $outputFile, $publicKey) { |
|
| 164 | 160 | /* Type checks: */ |
| 165 | 161 | if (!is_string($inputFile)) { |
| 166 | 162 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
@@ -262,8 +258,7 @@ discard block |
||
| 262 | 258 | * @throws Error |
| 263 | 259 | * @throws TypeError |
| 264 | 260 | */ |
| 265 | - public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair) |
|
| 266 | - { |
|
| 261 | + public static function box_seal_open($inputFile, $outputFile, $ecdhKeypair) { |
|
| 267 | 262 | /* Type checks: */ |
| 268 | 263 | if (!is_string($inputFile)) { |
| 269 | 264 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
@@ -345,8 +340,7 @@ discard block |
||
| 345 | 340 | * @throws TypeError |
| 346 | 341 | * @psalm-suppress FailedTypeResolution |
| 347 | 342 | */ |
| 348 | - public static function generichash($filePath, $key = '', $outputLength = 32) |
|
| 349 | - { |
|
| 343 | + public static function generichash($filePath, $key = '', $outputLength = 32) { |
|
| 350 | 344 | /* Type checks: */ |
| 351 | 345 | if (!is_string($filePath)) { |
| 352 | 346 | throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
@@ -423,8 +417,7 @@ discard block |
||
| 423 | 417 | * @throws Error |
| 424 | 418 | * @throws TypeError |
| 425 | 419 | */ |
| 426 | - public static function secretbox($inputFile, $outputFile, $nonce, $key) |
|
| 427 | - { |
|
| 420 | + public static function secretbox($inputFile, $outputFile, $nonce, $key) { |
|
| 428 | 421 | /* Type checks: */ |
| 429 | 422 | if (!is_string($inputFile)) { |
| 430 | 423 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given..'); |
@@ -488,8 +481,7 @@ discard block |
||
| 488 | 481 | * @throws Error |
| 489 | 482 | * @throws TypeError |
| 490 | 483 | */ |
| 491 | - public static function secretbox_open($inputFile, $outputFile, $nonce, $key) |
|
| 492 | - { |
|
| 484 | + public static function secretbox_open($inputFile, $outputFile, $nonce, $key) { |
|
| 493 | 485 | /* Type checks: */ |
| 494 | 486 | if (!is_string($inputFile)) { |
| 495 | 487 | throw new TypeError('Argument 1 must be a string, ' . gettype($inputFile) . ' given.'); |
@@ -554,8 +546,7 @@ discard block |
||
| 554 | 546 | * @throws Error |
| 555 | 547 | * @throws TypeError |
| 556 | 548 | */ |
| 557 | - public static function sign($filePath, $secretKey) |
|
| 558 | - { |
|
| 549 | + public static function sign($filePath, $secretKey) { |
|
| 559 | 550 | /* Type checks: */ |
| 560 | 551 | if (!is_string($filePath)) { |
| 561 | 552 | throw new TypeError('Argument 1 must be a string, ' . gettype($filePath) . ' given.'); |
@@ -646,8 +637,7 @@ discard block |
||
| 646 | 637 | * @throws Error |
| 647 | 638 | * @throws Exception |
| 648 | 639 | */ |
| 649 | - public static function verify($sig, $filePath, $publicKey) |
|
| 650 | - { |
|
| 640 | + public static function verify($sig, $filePath, $publicKey) { |
|
| 651 | 641 | /* Type checks: */ |
| 652 | 642 | if (!is_string($sig)) { |
| 653 | 643 | throw new TypeError('Argument 1 must be a string, ' . gettype($sig) . ' given.'); |
@@ -746,8 +736,7 @@ discard block |
||
| 746 | 736 | * @param string $boxKeypair |
| 747 | 737 | * @return bool |
| 748 | 738 | */ |
| 749 | - protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
| 750 | - { |
|
| 739 | + protected static function box_encrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) { |
|
| 751 | 740 | return self::secretbox_encrypt( |
| 752 | 741 | $ifp, |
| 753 | 742 | $ofp, |
@@ -769,8 +758,7 @@ discard block |
||
| 769 | 758 | * @param string $boxKeypair |
| 770 | 759 | * @return bool |
| 771 | 760 | */ |
| 772 | - protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) |
|
| 773 | - { |
|
| 761 | + protected static function box_decrypt($ifp, $ofp, $mlen, $nonce, $boxKeypair) { |
|
| 774 | 762 | return self::secretbox_decrypt( |
| 775 | 763 | $ifp, |
| 776 | 764 | $ofp, |
@@ -794,8 +782,7 @@ discard block |
||
| 794 | 782 | * @return bool |
| 795 | 783 | * @throws Error |
| 796 | 784 | */ |
| 797 | - protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
| 798 | - { |
|
| 785 | + protected static function secretbox_encrypt($ifp, $ofp, $mlen, $nonce, $key) { |
|
| 799 | 786 | $plaintext = fread($ifp, 32); |
| 800 | 787 | if (!is_string($plaintext)) { |
| 801 | 788 | throw new Error('Could not read input file'); |
@@ -911,8 +898,7 @@ discard block |
||
| 911 | 898 | * @throws Error |
| 912 | 899 | * @throws Exception |
| 913 | 900 | */ |
| 914 | - protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key) |
|
| 915 | - { |
|
| 901 | + protected static function secretbox_decrypt($ifp, $ofp, $mlen, $nonce, $key) { |
|
| 916 | 902 | $tag = fread($ifp, 16); |
| 917 | 903 | if (!is_string($tag)) { |
| 918 | 904 | throw new Error('Could not read input file'); |
@@ -991,8 +977,7 @@ discard block |
||
| 991 | 977 | * @return bool |
| 992 | 978 | * @throws Error |
| 993 | 979 | */ |
| 994 | - protected static function onetimeauth_verify(ParagonIE_Sodium_Core_Poly1305_State $state, $ifp, $tag = '', $mlen = 0) |
|
| 995 | - { |
|
| 980 | + protected static function onetimeauth_verify(ParagonIE_Sodium_Core_Poly1305_State $state, $ifp, $tag = '', $mlen = 0) { |
|
| 996 | 981 | /** @var int $pos */ |
| 997 | 982 | $pos = ftell($ifp); |
| 998 | 983 | |
@@ -1034,8 +1019,7 @@ discard block |
||
| 1034 | 1019 | * PHP 7.2 changes from a resource to an object, |
| 1035 | 1020 | * which causes Psalm to complain about an error. |
| 1036 | 1021 | */ |
| 1037 | - public static function updateHashWithFile($hash, $fp, $size = 0) |
|
| 1038 | - { |
|
| 1022 | + public static function updateHashWithFile($hash, $fp, $size = 0) { |
|
| 1039 | 1023 | /* Type checks: */ |
| 1040 | 1024 | if (PHP_VERSION_ID < 70200) { |
| 1041 | 1025 | if (!is_resource($hash)) { |
@@ -1,7 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace ParagonIE\Sodium; |
| 3 | 3 | |
| 4 | -class File extends \ParagonIE_Sodium_File |
|
| 5 | -{ |
|
| 4 | +class File extends \ParagonIE_Sodium_File { |
|
| 6 | 5 | |
| 7 | 6 | } |
@@ -1,7 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace ParagonIE\Sodium; |
| 3 | 3 | |
| 4 | -class Crypto extends \ParagonIE_Sodium_Crypto |
|
| 5 | -{ |
|
| 4 | +class Crypto extends \ParagonIE_Sodium_Crypto { |
|
| 6 | 5 | |
| 7 | 6 | } |
@@ -1,7 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace ParagonIE\Sodium; |
| 3 | 3 | |
| 4 | -class Compat extends \ParagonIE_Sodium_Compat |
|
| 5 | -{ |
|
| 4 | +class Compat extends \ParagonIE_Sodium_Compat { |
|
| 6 | 5 | |
| 7 | 6 | } |
@@ -1,7 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace ParagonIE\Sodium\Core; |
| 3 | 3 | |
| 4 | -class Util extends \ParagonIE_Sodium_Core_Util |
|
| 5 | -{ |
|
| 4 | +class Util extends \ParagonIE_Sodium_Core_Util { |
|
| 6 | 5 | |
| 7 | 6 | } |
@@ -1,7 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace ParagonIE\Sodium\Core; |
| 3 | 3 | |
| 4 | -class Poly1305 extends \ParagonIE_Sodium_Core_Poly1305 |
|
| 5 | -{ |
|
| 4 | +class Poly1305 extends \ParagonIE_Sodium_Core_Poly1305 { |
|
| 6 | 5 | |
| 7 | 6 | } |
@@ -1,7 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace ParagonIE\Sodium\Core; |
| 3 | 3 | |
| 4 | -class Ed25519 extends \ParagonIE_Sodium_Core_Ed25519 |
|
| 5 | -{ |
|
| 4 | +class Ed25519 extends \ParagonIE_Sodium_Core_Ed25519 { |
|
| 6 | 5 | |
| 7 | 6 | } |
@@ -1,7 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | namespace ParagonIE\Sodium\Core\Poly1305; |
| 3 | 3 | |
| 4 | -class State extends \ParagonIE_Sodium_Core_Poly1305_State |
|
| 5 | -{ |
|
| 4 | +class State extends \ParagonIE_Sodium_Core_Poly1305_State { |
|
| 6 | 5 | |
| 7 | 6 | } |