| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 3 if (class_exists('ParagonIE_Sodium_Core32_ChaCha20', false)) { 4 return; 5 } 6 7 /** 8 * Class ParagonIE_Sodium_Core32_ChaCha20 9 */ 10 class ParagonIE_Sodium_Core32_ChaCha20 extends ParagonIE_Sodium_Core32_Util 11 { 12 /** 13 * The ChaCha20 quarter round function. Works on four 32-bit integers. 14 * 15 * @internal You should not use this directly from another application 16 * 17 * @param ParagonIE_Sodium_Core32_Int32 $a 18 * @param ParagonIE_Sodium_Core32_Int32 $b 19 * @param ParagonIE_Sodium_Core32_Int32 $c 20 * @param ParagonIE_Sodium_Core32_Int32 $d 21 * @return array<int, ParagonIE_Sodium_Core32_Int32> 22 * @throws SodiumException 23 * @throws TypeError 24 */ 25 protected static function quarterRound( 26 ParagonIE_Sodium_Core32_Int32 $a, 27 ParagonIE_Sodium_Core32_Int32 $b, 28 ParagonIE_Sodium_Core32_Int32 $c, 29 ParagonIE_Sodium_Core32_Int32 $d 30 ) { 31 /** @var ParagonIE_Sodium_Core32_Int32 $a */ 32 /** @var ParagonIE_Sodium_Core32_Int32 $b */ 33 /** @var ParagonIE_Sodium_Core32_Int32 $c */ 34 /** @var ParagonIE_Sodium_Core32_Int32 $d */ 35 36 # a = PLUS(a,b); d = ROTATE(XOR(d,a),16); 37 $a = $a->addInt32($b); 38 $d = $d->xorInt32($a)->rotateLeft(16); 39 40 # c = PLUS(c,d); b = ROTATE(XOR(b,c),12); 41 $c = $c->addInt32($d); 42 $b = $b->xorInt32($c)->rotateLeft(12); 43 44 # a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); 45 $a = $a->addInt32($b); 46 $d = $d->xorInt32($a)->rotateLeft(8); 47 48 # c = PLUS(c,d); b = ROTATE(XOR(b,c), 7); 49 $c = $c->addInt32($d); 50 $b = $b->xorInt32($c)->rotateLeft(7); 51 52 return array($a, $b, $c, $d); 53 } 54 55 /** 56 * @internal You should not use this directly from another application 57 * 58 * @param ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx 59 * @param string $message 60 * 61 * @return string 62 * @throws SodiumException 63 * @throws TypeError 64 */ 65 public static function encryptBytes( 66 ParagonIE_Sodium_Core32_ChaCha20_Ctx $ctx, 67 $message = '' 68 ) { 69 $bytes = self::strlen($message); 70 71 /** @var ParagonIE_Sodium_Core32_Int32 $x0 */ 72 /** @var ParagonIE_Sodium_Core32_Int32 $x1 */ 73 /** @var ParagonIE_Sodium_Core32_Int32 $x2 */ 74 /** @var ParagonIE_Sodium_Core32_Int32 $x3 */ 75 /** @var ParagonIE_Sodium_Core32_Int32 $x4 */ 76 /** @var ParagonIE_Sodium_Core32_Int32 $x5 */ 77 /** @var ParagonIE_Sodium_Core32_Int32 $x6 */ 78 /** @var ParagonIE_Sodium_Core32_Int32 $x7 */ 79 /** @var ParagonIE_Sodium_Core32_Int32 $x8 */ 80 /** @var ParagonIE_Sodium_Core32_Int32 $x9 */ 81 /** @var ParagonIE_Sodium_Core32_Int32 $x10 */ 82 /** @var ParagonIE_Sodium_Core32_Int32 $x11 */ 83 /** @var ParagonIE_Sodium_Core32_Int32 $x12 */ 84 /** @var ParagonIE_Sodium_Core32_Int32 $x13 */ 85 /** @var ParagonIE_Sodium_Core32_Int32 $x14 */ 86 /** @var ParagonIE_Sodium_Core32_Int32 $x15 */ 87 88 /* 89 j0 = ctx->input[0]; 90 j1 = ctx->input[1]; 91 j2 = ctx->input[2]; 92 j3 = ctx->input[3]; 93 j4 = ctx->input[4]; 94 j5 = ctx->input[5]; 95 j6 = ctx->input[6]; 96 j7 = ctx->input[7]; 97 j8 = ctx->input[8]; 98 j9 = ctx->input[9]; 99 j10 = ctx->input[10]; 100 j11 = ctx->input[11]; 101 j12 = ctx->input[12]; 102 j13 = ctx->input[13]; 103 j14 = ctx->input[14]; 104 j15 = ctx->input[15]; 105 */ 106 /** @var ParagonIE_Sodium_Core32_Int32 $j0 */ 107 $j0 = $ctx[0]; 108 /** @var ParagonIE_Sodium_Core32_Int32 $j1 */ 109 $j1 = $ctx[1]; 110 /** @var ParagonIE_Sodium_Core32_Int32 $j2 */ 111 $j2 = $ctx[2]; 112 /** @var ParagonIE_Sodium_Core32_Int32 $j3 */ 113 $j3 = $ctx[3]; 114 /** @var ParagonIE_Sodium_Core32_Int32 $j4 */ 115 $j4 = $ctx[4]; 116 /** @var ParagonIE_Sodium_Core32_Int32 $j5 */ 117 $j5 = $ctx[5]; 118 /** @var ParagonIE_Sodium_Core32_Int32 $j6 */ 119 $j6 = $ctx[6]; 120 /** @var ParagonIE_Sodium_Core32_Int32 $j7 */ 121 $j7 = $ctx[7]; 122 /** @var ParagonIE_Sodium_Core32_Int32 $j8 */ 123 $j8 = $ctx[8]; 124 /** @var ParagonIE_Sodium_Core32_Int32 $j9 */ 125 $j9 = $ctx[9]; 126 /** @var ParagonIE_Sodium_Core32_Int32 $j10 */ 127 $j10 = $ctx[10]; 128 /** @var ParagonIE_Sodium_Core32_Int32 $j11 */ 129 $j11 = $ctx[11]; 130 /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ 131 $j12 = $ctx[12]; 132 /** @var ParagonIE_Sodium_Core32_Int32 $j13 */ 133 $j13 = $ctx[13]; 134 /** @var ParagonIE_Sodium_Core32_Int32 $j14 */ 135 $j14 = $ctx[14]; 136 /** @var ParagonIE_Sodium_Core32_Int32 $j15 */ 137 $j15 = $ctx[15]; 138 139 $c = ''; 140 for (;;) { 141 if ($ctx->isExhausted()) { 142 throw new SodiumException('Overflow'); 143 } 144 if ($bytes < 64) { 145 $message .= str_repeat("\x00", 64 - $bytes); 146 } 147 148 $x0 = clone $j0; 149 $x1 = clone $j1; 150 $x2 = clone $j2; 151 $x3 = clone $j3; 152 $x4 = clone $j4; 153 $x5 = clone $j5; 154 $x6 = clone $j6; 155 $x7 = clone $j7; 156 $x8 = clone $j8; 157 $x9 = clone $j9; 158 $x10 = clone $j10; 159 $x11 = clone $j11; 160 $x12 = clone $j12; 161 $x13 = clone $j13; 162 $x14 = clone $j14; 163 $x15 = clone $j15; 164 165 # for (i = 20; i > 0; i -= 2) { 166 for ($i = 20; $i > 0; $i -= 2) { 167 # QUARTERROUND( x0, x4, x8, x12) 168 list($x0, $x4, $x8, $x12) = self::quarterRound($x0, $x4, $x8, $x12); 169 170 # QUARTERROUND( x1, x5, x9, x13) 171 list($x1, $x5, $x9, $x13) = self::quarterRound($x1, $x5, $x9, $x13); 172 173 # QUARTERROUND( x2, x6, x10, x14) 174 list($x2, $x6, $x10, $x14) = self::quarterRound($x2, $x6, $x10, $x14); 175 176 # QUARTERROUND( x3, x7, x11, x15) 177 list($x3, $x7, $x11, $x15) = self::quarterRound($x3, $x7, $x11, $x15); 178 179 # QUARTERROUND( x0, x5, x10, x15) 180 list($x0, $x5, $x10, $x15) = self::quarterRound($x0, $x5, $x10, $x15); 181 182 # QUARTERROUND( x1, x6, x11, x12) 183 list($x1, $x6, $x11, $x12) = self::quarterRound($x1, $x6, $x11, $x12); 184 185 # QUARTERROUND( x2, x7, x8, x13) 186 list($x2, $x7, $x8, $x13) = self::quarterRound($x2, $x7, $x8, $x13); 187 188 # QUARTERROUND( x3, x4, x9, x14) 189 list($x3, $x4, $x9, $x14) = self::quarterRound($x3, $x4, $x9, $x14); 190 } 191 /* 192 x0 = PLUS(x0, j0); 193 x1 = PLUS(x1, j1); 194 x2 = PLUS(x2, j2); 195 x3 = PLUS(x3, j3); 196 x4 = PLUS(x4, j4); 197 x5 = PLUS(x5, j5); 198 x6 = PLUS(x6, j6); 199 x7 = PLUS(x7, j7); 200 x8 = PLUS(x8, j8); 201 x9 = PLUS(x9, j9); 202 x10 = PLUS(x10, j10); 203 x11 = PLUS(x11, j11); 204 x12 = PLUS(x12, j12); 205 x13 = PLUS(x13, j13); 206 x14 = PLUS(x14, j14); 207 x15 = PLUS(x15, j15); 208 */ 209 $x0 = $x0->addInt32($j0); 210 $x1 = $x1->addInt32($j1); 211 $x2 = $x2->addInt32($j2); 212 $x3 = $x3->addInt32($j3); 213 $x4 = $x4->addInt32($j4); 214 $x5 = $x5->addInt32($j5); 215 $x6 = $x6->addInt32($j6); 216 $x7 = $x7->addInt32($j7); 217 $x8 = $x8->addInt32($j8); 218 $x9 = $x9->addInt32($j9); 219 $x10 = $x10->addInt32($j10); 220 $x11 = $x11->addInt32($j11); 221 $x12 = $x12->addInt32($j12); 222 $x13 = $x13->addInt32($j13); 223 $x14 = $x14->addInt32($j14); 224 $x15 = $x15->addInt32($j15); 225 226 /* 227 x0 = XOR(x0, LOAD32_LE(m + 0)); 228 x1 = XOR(x1, LOAD32_LE(m + 4)); 229 x2 = XOR(x2, LOAD32_LE(m + 8)); 230 x3 = XOR(x3, LOAD32_LE(m + 12)); 231 x4 = XOR(x4, LOAD32_LE(m + 16)); 232 x5 = XOR(x5, LOAD32_LE(m + 20)); 233 x6 = XOR(x6, LOAD32_LE(m + 24)); 234 x7 = XOR(x7, LOAD32_LE(m + 28)); 235 x8 = XOR(x8, LOAD32_LE(m + 32)); 236 x9 = XOR(x9, LOAD32_LE(m + 36)); 237 x10 = XOR(x10, LOAD32_LE(m + 40)); 238 x11 = XOR(x11, LOAD32_LE(m + 44)); 239 x12 = XOR(x12, LOAD32_LE(m + 48)); 240 x13 = XOR(x13, LOAD32_LE(m + 52)); 241 x14 = XOR(x14, LOAD32_LE(m + 56)); 242 x15 = XOR(x15, LOAD32_LE(m + 60)); 243 */ 244 $x0 = $x0->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 0, 4))); 245 $x1 = $x1->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 4, 4))); 246 $x2 = $x2->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 8, 4))); 247 $x3 = $x3->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 12, 4))); 248 $x4 = $x4->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 16, 4))); 249 $x5 = $x5->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 20, 4))); 250 $x6 = $x6->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 24, 4))); 251 $x7 = $x7->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 28, 4))); 252 $x8 = $x8->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 32, 4))); 253 $x9 = $x9->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 36, 4))); 254 $x10 = $x10->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 40, 4))); 255 $x11 = $x11->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 44, 4))); 256 $x12 = $x12->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 48, 4))); 257 $x13 = $x13->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 52, 4))); 258 $x14 = $x14->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 56, 4))); 259 $x15 = $x15->xorInt32(ParagonIE_Sodium_Core32_Int32::fromReverseString(self::substr($message, 60, 4))); 260 261 /* 262 j12 = PLUSONE(j12); 263 if (!j12) { 264 j13 = PLUSONE(j13); 265 } 266 */ 267 /** @var ParagonIE_Sodium_Core32_Int32 $j12 */ 268 $j12 = $j12->addInt(1); 269 if ($j12->limbs[0] === 0 && $j12->limbs[1] === 0) { 270 if ($ctx instanceof ParagonIE_Sodium_Core32_ChaCha20_IetfCtx) { 271 $ctx->markExhausted(); 272 } else { 273 $j13 = $j13->addInt(1); 274 if ($j13->limbs[0] === 0 && $j13->limbs[1] === 0) { 275 $ctx->markExhausted(); 276 } 277 } 278 } 279 280 /* 281 STORE32_LE(c + 0, x0); 282 STORE32_LE(c + 4, x1); 283 STORE32_LE(c + 8, x2); 284 STORE32_LE(c + 12, x3); 285 STORE32_LE(c + 16, x4); 286 STORE32_LE(c + 20, x5); 287 STORE32_LE(c + 24, x6); 288 STORE32_LE(c + 28, x7); 289 STORE32_LE(c + 32, x8); 290 STORE32_LE(c + 36, x9); 291 STORE32_LE(c + 40, x10); 292 STORE32_LE(c + 44, x11); 293 STORE32_LE(c + 48, x12); 294 STORE32_LE(c + 52, x13); 295 STORE32_LE(c + 56, x14); 296 STORE32_LE(c + 60, x15); 297 */ 298 299 $block = $x0->toReverseString() . 300 $x1->toReverseString() . 301 $x2->toReverseString() . 302 $x3->toReverseString() . 303 $x4->toReverseString() . 304 $x5->toReverseString() . 305 $x6->toReverseString() . 306 $x7->toReverseString() . 307 $x8->toReverseString() . 308 $x9->toReverseString() . 309 $x10->toReverseString() . 310 $x11->toReverseString() . 311 $x12->toReverseString() . 312 $x13->toReverseString() . 313 $x14->toReverseString() . 314 $x15->toReverseString(); 315 316 /* Partial block */ 317 if ($bytes < 64) { 318 $c .= self::substr($block, 0, $bytes); 319 break; 320 } 321 322 /* Full block */ 323 $c .= $block; 324 $bytes -= 64; 325 if ($bytes <= 0) { 326 break; 327 } 328 $message = self::substr($message, 64); 329 } 330 /* end for(;;) loop */ 331 332 $ctx[12] = $j12; 333 $ctx[13] = $j13; 334 return $c; 335 } 336 337 /** 338 * @internal You should not use this directly from another application 339 * 340 * @param int $len 341 * @param string $nonce 342 * @param string $key 343 * @return string 344 * @throws SodiumException 345 * @throws TypeError 346 */ 347 public static function stream($len = 64, $nonce = '', $key = '') 348 { 349 return self::encryptBytes( 350 new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce), 351 str_repeat("\x00", $len) 352 ); 353 } 354 355 /** 356 * @internal You should not use this directly from another application 357 * 358 * @param int $len 359 * @param string $nonce 360 * @param string $key 361 * @return string 362 * @throws SodiumException 363 * @throws TypeError 364 */ 365 public static function ietfStream($len, $nonce = '', $key = '') 366 { 367 return self::encryptBytes( 368 new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce), 369 str_repeat("\x00", $len) 370 ); 371 } 372 373 /** 374 * @internal You should not use this directly from another application 375 * 376 * @param string $message 377 * @param string $nonce 378 * @param string $key 379 * @param string $ic 380 * @return string 381 * @throws SodiumException 382 * @throws TypeError 383 */ 384 public static function ietfStreamXorIc($message, $nonce = '', $key = '', $ic = '') 385 { 386 return self::encryptBytes( 387 new ParagonIE_Sodium_Core32_ChaCha20_IetfCtx($key, $nonce, $ic), 388 $message 389 ); 390 } 391 392 /** 393 * @internal You should not use this directly from another application 394 * 395 * @param string $message 396 * @param string $nonce 397 * @param string $key 398 * @param string $ic 399 * @return string 400 * @throws SodiumException 401 * @throws TypeError 402 */ 403 public static function streamXorIc($message, $nonce = '', $key = '', $ic = '') 404 { 405 return self::encryptBytes( 406 new ParagonIE_Sodium_Core32_ChaCha20_Ctx($key, $nonce, $ic), 407 $message 408 ); 409 } 410 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Wed Nov 11 08:20:02 2026 | Cross-referenced by PHPXref |