| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
if (class_exists('ParagonIE_Sodium_Core_ChaCha20', false)) { |
| 5 |
return; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class ParagonIE_Sodium_Core_ChaCha20 extends ParagonIE_Sodium_Core_Util |
| 12 |
{ |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public static function rotate(int $v, int $n): int |
| 23 |
{ |
| 24 |
$v &= 0xffffffff; |
| 25 |
$n &= 31; |
| 26 |
return ( |
| 27 |
0xffffffff & ( |
| 28 |
($v << $n) |
| 29 |
| |
| 30 |
($v >> (32 - $n)) |
| 31 |
) |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
protected static function quarterRound(int $a, int $b, int $c, int $d): array |
| 47 |
{ |
| 48 |
|
| 49 |
|
| 50 |
$a = ($a + $b) & 0xffffffff; |
| 51 |
$d = self::rotate($d ^ $a, 16); |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
$c = ($c + $d) & 0xffffffff; |
| 56 |
$b = self::rotate($b ^ $c, 12); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
$a = ($a + $b) & 0xffffffff; |
| 61 |
$d = self::rotate($d ^ $a, 8); |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
$c = ($c + $d) & 0xffffffff; |
| 66 |
$b = self::rotate($b ^ $c, 7); |
| 67 |
return array($a, $b, $c, $d); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public static function encryptBytes( |
| 81 |
ParagonIE_Sodium_Core_ChaCha20_Ctx $ctx, |
| 82 |
#[SensitiveParameter] |
| 83 |
string $message = '' |
| 84 |
): string { |
| 85 |
$bytes = self::strlen($message); |
| 86 |
|
| 87 |
$j0 = (int) $ctx[0]; |
| 88 |
$j1 = (int) $ctx[1]; |
| 89 |
$j2 = (int) $ctx[2]; |
| 90 |
$j3 = (int) $ctx[3]; |
| 91 |
$j4 = (int) $ctx[4]; |
| 92 |
$j5 = (int) $ctx[5]; |
| 93 |
$j6 = (int) $ctx[6]; |
| 94 |
$j7 = (int) $ctx[7]; |
| 95 |
$j8 = (int) $ctx[8]; |
| 96 |
$j9 = (int) $ctx[9]; |
| 97 |
$j10 = (int) $ctx[10]; |
| 98 |
$j11 = (int) $ctx[11]; |
| 99 |
$j12 = (int) $ctx[12]; |
| 100 |
$j13 = (int) $ctx[13]; |
| 101 |
$j14 = (int) $ctx[14]; |
| 102 |
$j15 = (int) $ctx[15]; |
| 103 |
|
| 104 |
$c = ''; |
| 105 |
for (;;) { |
| 106 |
if ($bytes < 64) { |
| 107 |
$message .= str_repeat("\x00", 64 - $bytes); |
| 108 |
} |
| 109 |
|
| 110 |
$x0 = $j0; |
| 111 |
$x1 = $j1; |
| 112 |
$x2 = $j2; |
| 113 |
$x3 = $j3; |
| 114 |
$x4 = $j4; |
| 115 |
$x5 = $j5; |
| 116 |
$x6 = $j6; |
| 117 |
$x7 = $j7; |
| 118 |
$x8 = $j8; |
| 119 |
$x9 = $j9; |
| 120 |
$x10 = $j10; |
| 121 |
$x11 = $j11; |
| 122 |
$x12 = $j12; |
| 123 |
$x13 = $j13; |
| 124 |
$x14 = $j14; |
| 125 |
$x15 = $j15; |
| 126 |
|
| 127 |
|
| 128 |
for ($i = 20; $i > 0; $i -= 2) { |
| 129 |
[$x0, $x4, $x8, $x12] = self::quarterRound($x0, $x4, $x8, $x12); |
| 130 |
[$x1, $x5, $x9, $x13] = self::quarterRound($x1, $x5, $x9, $x13); |
| 131 |
[$x2, $x6, $x10, $x14] = self::quarterRound($x2, $x6, $x10, $x14); |
| 132 |
[$x3, $x7, $x11, $x15] = self::quarterRound($x3, $x7, $x11, $x15); |
| 133 |
|
| 134 |
[$x0, $x5, $x10, $x15] = self::quarterRound($x0, $x5, $x10, $x15); |
| 135 |
[$x1, $x6, $x11, $x12] = self::quarterRound($x1, $x6, $x11, $x12); |
| 136 |
[$x2, $x7, $x8, $x13] = self::quarterRound($x2, $x7, $x8, $x13); |
| 137 |
[$x3, $x4, $x9, $x14] = self::quarterRound($x3, $x4, $x9, $x14); |
| 138 |
} |
| 139 |
$x0 = ($x0 & 0xffffffff) + $j0; |
| 140 |
$x1 = ($x1 & 0xffffffff) + $j1; |
| 141 |
$x2 = ($x2 & 0xffffffff) + $j2; |
| 142 |
$x3 = ($x3 & 0xffffffff) + $j3; |
| 143 |
$x4 = ($x4 & 0xffffffff) + $j4; |
| 144 |
$x5 = ($x5 & 0xffffffff) + $j5; |
| 145 |
$x6 = ($x6 & 0xffffffff) + $j6; |
| 146 |
$x7 = ($x7 & 0xffffffff) + $j7; |
| 147 |
$x8 = ($x8 & 0xffffffff) + $j8; |
| 148 |
$x9 = ($x9 & 0xffffffff) + $j9; |
| 149 |
$x10 = ($x10 & 0xffffffff) + $j10; |
| 150 |
$x11 = ($x11 & 0xffffffff) + $j11; |
| 151 |
$x12 = ($x12 & 0xffffffff) + $j12; |
| 152 |
$x13 = ($x13 & 0xffffffff) + $j13; |
| 153 |
$x14 = ($x14 & 0xffffffff) + $j14; |
| 154 |
$x15 = ($x15 & 0xffffffff) + $j15; |
| 155 |
|
| 156 |
$x0 ^= self::load_4(self::substr($message, 0, 4)); |
| 157 |
$x1 ^= self::load_4(self::substr($message, 4, 4)); |
| 158 |
$x2 ^= self::load_4(self::substr($message, 8, 4)); |
| 159 |
$x3 ^= self::load_4(self::substr($message, 12, 4)); |
| 160 |
$x4 ^= self::load_4(self::substr($message, 16, 4)); |
| 161 |
$x5 ^= self::load_4(self::substr($message, 20, 4)); |
| 162 |
$x6 ^= self::load_4(self::substr($message, 24, 4)); |
| 163 |
$x7 ^= self::load_4(self::substr($message, 28, 4)); |
| 164 |
$x8 ^= self::load_4(self::substr($message, 32, 4)); |
| 165 |
$x9 ^= self::load_4(self::substr($message, 36, 4)); |
| 166 |
$x10 ^= self::load_4(self::substr($message, 40, 4)); |
| 167 |
$x11 ^= self::load_4(self::substr($message, 44, 4)); |
| 168 |
$x12 ^= self::load_4(self::substr($message, 48, 4)); |
| 169 |
$x13 ^= self::load_4(self::substr($message, 52, 4)); |
| 170 |
$x14 ^= self::load_4(self::substr($message, 56, 4)); |
| 171 |
$x15 ^= self::load_4(self::substr($message, 60, 4)); |
| 172 |
|
| 173 |
++$j12; |
| 174 |
if ($j12 & 0xf0000000) { |
| 175 |
throw new SodiumException('Overflow'); |
| 176 |
} |
| 177 |
|
| 178 |
$block = self::store32_le(($x0 & 0xffffffff)) . |
| 179 |
self::store32_le(($x1 & 0xffffffff)) . |
| 180 |
self::store32_le(($x2 & 0xffffffff)) . |
| 181 |
self::store32_le(($x3 & 0xffffffff)) . |
| 182 |
self::store32_le(($x4 & 0xffffffff)) . |
| 183 |
self::store32_le(($x5 & 0xffffffff)) . |
| 184 |
self::store32_le(($x6 & 0xffffffff)) . |
| 185 |
self::store32_le(($x7 & 0xffffffff)) . |
| 186 |
self::store32_le(($x8 & 0xffffffff)) . |
| 187 |
self::store32_le(($x9 & 0xffffffff)) . |
| 188 |
self::store32_le(($x10 & 0xffffffff)) . |
| 189 |
self::store32_le(($x11 & 0xffffffff)) . |
| 190 |
self::store32_le(($x12 & 0xffffffff)) . |
| 191 |
self::store32_le(($x13 & 0xffffffff)) . |
| 192 |
self::store32_le(($x14 & 0xffffffff)) . |
| 193 |
self::store32_le(($x15 & 0xffffffff)); |
| 194 |
|
| 195 |
|
| 196 |
if ($bytes < 64) { |
| 197 |
$c .= self::substr($block, 0, $bytes); |
| 198 |
break; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
$c .= $block; |
| 203 |
$bytes -= 64; |
| 204 |
if ($bytes <= 0) { |
| 205 |
break; |
| 206 |
} |
| 207 |
$message = self::substr($message, 64); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
$ctx[12] = $j12; |
| 212 |
$ctx[13] = $j13; |
| 213 |
return $c; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public static function stream( |
| 227 |
int $len, |
| 228 |
string $nonce, |
| 229 |
#[SensitiveParameter] |
| 230 |
string $key |
| 231 |
): string { |
| 232 |
return self::encryptBytes( |
| 233 |
new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce), |
| 234 |
str_repeat("\x00", $len) |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public static function ietfStream( |
| 249 |
int $len, |
| 250 |
string $nonce, |
| 251 |
#[SensitiveParameter] |
| 252 |
string $key |
| 253 |
): string { |
| 254 |
return self::encryptBytes( |
| 255 |
new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce), |
| 256 |
str_repeat("\x00", $len) |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
public static function ietfStreamXorIc( |
| 272 |
#[SensitiveParameter] |
| 273 |
string $message, |
| 274 |
string $nonce, |
| 275 |
#[SensitiveParameter] |
| 276 |
string $key, |
| 277 |
string $ic = '' |
| 278 |
): string { |
| 279 |
return self::encryptBytes( |
| 280 |
new ParagonIE_Sodium_Core_ChaCha20_IetfCtx($key, $nonce, $ic), |
| 281 |
$message |
| 282 |
); |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
public static function streamXorIc( |
| 297 |
#[SensitiveParameter] |
| 298 |
string $message, |
| 299 |
string $nonce, |
| 300 |
#[SensitiveParameter] |
| 301 |
string $key, |
| 302 |
string $ic = '' |
| 303 |
): string { |
| 304 |
return self::encryptBytes( |
| 305 |
new ParagonIE_Sodium_Core_ChaCha20_Ctx($key, $nonce, $ic), |
| 306 |
$message |
| 307 |
); |
| 308 |
} |
| 309 |
} |