| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
if (class_exists('ParagonIE_Sodium_Crypto', false)) { |
| 5 |
return; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
abstract class ParagonIE_Sodium_Crypto |
| 17 |
{ |
| 18 |
const aead_chacha20poly1305_KEYBYTES = 32; |
| 19 |
const aead_chacha20poly1305_NSECBYTES = 0; |
| 20 |
const aead_chacha20poly1305_NPUBBYTES = 8; |
| 21 |
const aead_chacha20poly1305_ABYTES = 16; |
| 22 |
|
| 23 |
const aead_chacha20poly1305_IETF_KEYBYTES = 32; |
| 24 |
const aead_chacha20poly1305_IETF_NSECBYTES = 0; |
| 25 |
const aead_chacha20poly1305_IETF_NPUBBYTES = 12; |
| 26 |
const aead_chacha20poly1305_IETF_ABYTES = 16; |
| 27 |
|
| 28 |
const aead_xchacha20poly1305_IETF_KEYBYTES = 32; |
| 29 |
const aead_xchacha20poly1305_IETF_NSECBYTES = 0; |
| 30 |
const aead_xchacha20poly1305_IETF_NPUBBYTES = 24; |
| 31 |
const aead_xchacha20poly1305_IETF_ABYTES = 16; |
| 32 |
|
| 33 |
const box_curve25519xsalsa20poly1305_SEEDBYTES = 32; |
| 34 |
const box_curve25519xsalsa20poly1305_PUBLICKEYBYTES = 32; |
| 35 |
const box_curve25519xsalsa20poly1305_SECRETKEYBYTES = 32; |
| 36 |
const box_curve25519xsalsa20poly1305_BEFORENMBYTES = 32; |
| 37 |
const box_curve25519xsalsa20poly1305_NONCEBYTES = 24; |
| 38 |
const box_curve25519xsalsa20poly1305_MACBYTES = 16; |
| 39 |
const box_curve25519xsalsa20poly1305_BOXZEROBYTES = 16; |
| 40 |
const box_curve25519xsalsa20poly1305_ZEROBYTES = 32; |
| 41 |
|
| 42 |
const onetimeauth_poly1305_BYTES = 16; |
| 43 |
const onetimeauth_poly1305_KEYBYTES = 32; |
| 44 |
|
| 45 |
const secretbox_xsalsa20poly1305_KEYBYTES = 32; |
| 46 |
const secretbox_xsalsa20poly1305_NONCEBYTES = 24; |
| 47 |
const secretbox_xsalsa20poly1305_MACBYTES = 16; |
| 48 |
const secretbox_xsalsa20poly1305_BOXZEROBYTES = 16; |
| 49 |
const secretbox_xsalsa20poly1305_ZEROBYTES = 32; |
| 50 |
|
| 51 |
const secretbox_xchacha20poly1305_KEYBYTES = 32; |
| 52 |
const secretbox_xchacha20poly1305_NONCEBYTES = 24; |
| 53 |
const secretbox_xchacha20poly1305_MACBYTES = 16; |
| 54 |
const secretbox_xchacha20poly1305_BOXZEROBYTES = 16; |
| 55 |
const secretbox_xchacha20poly1305_ZEROBYTES = 32; |
| 56 |
|
| 57 |
const stream_salsa20_KEYBYTES = 32; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static function aead_chacha20poly1305_decrypt( |
| 73 |
string $message = '', |
| 74 |
string $ad = '', |
| 75 |
string $nonce = '', |
| 76 |
#[SensitiveParameter] |
| 77 |
string $key = '' |
| 78 |
): string { |
| 79 |
|
| 80 |
$len = ParagonIE_Sodium_Core_Util::strlen($message); |
| 81 |
|
| 82 |
|
| 83 |
$clen = $len - self::aead_chacha20poly1305_ABYTES; |
| 84 |
|
| 85 |
|
| 86 |
$adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
| 87 |
|
| 88 |
|
| 89 |
$mac = ParagonIE_Sodium_Core_Util::substr( |
| 90 |
$message, |
| 91 |
$clen, |
| 92 |
self::aead_chacha20poly1305_ABYTES |
| 93 |
); |
| 94 |
|
| 95 |
|
| 96 |
$ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 0, $clen); |
| 97 |
|
| 98 |
|
| 99 |
$block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
| 100 |
32, |
| 101 |
$nonce, |
| 102 |
$key |
| 103 |
); |
| 104 |
|
| 105 |
|
| 106 |
$state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
| 107 |
try { |
| 108 |
ParagonIE_Sodium_Compat::memzero($block0); |
| 109 |
} catch (SodiumException) { |
| 110 |
$block0 = null; |
| 111 |
} |
| 112 |
$state->update($ad); |
| 113 |
$state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
| 114 |
$state->update($ciphertext); |
| 115 |
$state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
| 116 |
$computed_mac = $state->finish(); |
| 117 |
|
| 118 |
|
| 119 |
if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) { |
| 120 |
throw new SodiumException('Invalid MAC'); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
return ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
| 125 |
$ciphertext, |
| 126 |
$nonce, |
| 127 |
$key, |
| 128 |
ParagonIE_Sodium_Core_Util::store64_le(1) |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public static function aead_chacha20poly1305_encrypt( |
| 146 |
#[SensitiveParameter] |
| 147 |
string $message = '', |
| 148 |
string $ad = '', |
| 149 |
string $nonce = '', |
| 150 |
#[SensitiveParameter] |
| 151 |
string $key = '' |
| 152 |
): string { |
| 153 |
|
| 154 |
$len = ParagonIE_Sodium_Core_Util::strlen($message); |
| 155 |
|
| 156 |
|
| 157 |
$adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
| 158 |
|
| 159 |
|
| 160 |
$block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
| 161 |
32, |
| 162 |
$nonce, |
| 163 |
$key |
| 164 |
); |
| 165 |
$state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
| 166 |
try { |
| 167 |
ParagonIE_Sodium_Compat::memzero($block0); |
| 168 |
} catch (SodiumException) { |
| 169 |
$block0 = null; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
$ciphertext = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
| 174 |
$message, |
| 175 |
$nonce, |
| 176 |
$key, |
| 177 |
ParagonIE_Sodium_Core_Util::store64_le(1) |
| 178 |
); |
| 179 |
|
| 180 |
$state->update($ad); |
| 181 |
$state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
| 182 |
$state->update($ciphertext); |
| 183 |
$state->update(ParagonIE_Sodium_Core_Util::store64_le($len)); |
| 184 |
return $ciphertext . $state->finish(); |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public static function aead_chacha20poly1305_ietf_decrypt( |
| 201 |
string $message = '', |
| 202 |
string $ad = '', |
| 203 |
string $nonce = '', |
| 204 |
#[SensitiveParameter] |
| 205 |
string $key = '' |
| 206 |
): string { |
| 207 |
|
| 208 |
$adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
| 209 |
|
| 210 |
|
| 211 |
$len = ParagonIE_Sodium_Core_Util::strlen($message); |
| 212 |
|
| 213 |
|
| 214 |
$clen = $len - self::aead_chacha20poly1305_IETF_ABYTES; |
| 215 |
|
| 216 |
|
| 217 |
$block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream( |
| 218 |
32, |
| 219 |
$nonce, |
| 220 |
$key |
| 221 |
); |
| 222 |
|
| 223 |
|
| 224 |
$mac = ParagonIE_Sodium_Core_Util::substr( |
| 225 |
$message, |
| 226 |
$len - self::aead_chacha20poly1305_IETF_ABYTES, |
| 227 |
self::aead_chacha20poly1305_IETF_ABYTES |
| 228 |
); |
| 229 |
|
| 230 |
|
| 231 |
$ciphertext = ParagonIE_Sodium_Core_Util::substr( |
| 232 |
$message, |
| 233 |
0, |
| 234 |
$len - self::aead_chacha20poly1305_IETF_ABYTES |
| 235 |
); |
| 236 |
|
| 237 |
|
| 238 |
$state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
| 239 |
try { |
| 240 |
ParagonIE_Sodium_Compat::memzero($block0); |
| 241 |
} catch (SodiumException) { |
| 242 |
$block0 = null; |
| 243 |
} |
| 244 |
$state->update($ad); |
| 245 |
$state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf))); |
| 246 |
$state->update($ciphertext); |
| 247 |
$state->update(str_repeat("\x00", (0x10 - $clen) & 0xf)); |
| 248 |
$state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
| 249 |
$state->update(ParagonIE_Sodium_Core_Util::store64_le($clen)); |
| 250 |
$computed_mac = $state->finish(); |
| 251 |
|
| 252 |
|
| 253 |
if (!ParagonIE_Sodium_Core_Util::verify_16($computed_mac, $mac)) { |
| 254 |
throw new SodiumException('Invalid MAC'); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
return ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
| 259 |
$ciphertext, |
| 260 |
$nonce, |
| 261 |
$key, |
| 262 |
ParagonIE_Sodium_Core_Util::store64_le(1) |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public static function aead_chacha20poly1305_ietf_encrypt( |
| 280 |
#[SensitiveParameter] |
| 281 |
string $message = '', |
| 282 |
string $ad = '', |
| 283 |
string $nonce = '', |
| 284 |
#[SensitiveParameter] |
| 285 |
string $key = '' |
| 286 |
): string { |
| 287 |
|
| 288 |
$len = ParagonIE_Sodium_Core_Util::strlen($message); |
| 289 |
|
| 290 |
|
| 291 |
$adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
| 292 |
|
| 293 |
|
| 294 |
$block0 = ParagonIE_Sodium_Core_ChaCha20::ietfStream( |
| 295 |
32, |
| 296 |
$nonce, |
| 297 |
$key |
| 298 |
); |
| 299 |
$state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
| 300 |
try { |
| 301 |
ParagonIE_Sodium_Compat::memzero($block0); |
| 302 |
} catch (SodiumException) { |
| 303 |
$block0 = null; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
$ciphertext = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
| 308 |
$message, |
| 309 |
$nonce, |
| 310 |
$key, |
| 311 |
ParagonIE_Sodium_Core_Util::store64_le(1) |
| 312 |
); |
| 313 |
|
| 314 |
$state->update($ad); |
| 315 |
$state->update(str_repeat("\x00", ((0x10 - $adlen) & 0xf))); |
| 316 |
$state->update($ciphertext); |
| 317 |
$state->update(str_repeat("\x00", ((0x10 - $len) & 0xf))); |
| 318 |
$state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
| 319 |
$state->update(ParagonIE_Sodium_Core_Util::store64_le($len)); |
| 320 |
return $ciphertext . $state->finish(); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
public static function aead_xchacha20poly1305_ietf_decrypt( |
| 337 |
string $message = '', |
| 338 |
string $ad = '', |
| 339 |
string $nonce = '', |
| 340 |
#[SensitiveParameter] |
| 341 |
string $key = '' |
| 342 |
): string { |
| 343 |
$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
| 344 |
ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
| 345 |
$key |
| 346 |
); |
| 347 |
$nonceLast = "\x00\x00\x00\x00" . |
| 348 |
ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
| 349 |
|
| 350 |
return self::aead_chacha20poly1305_ietf_decrypt($message, $ad, $nonceLast, $subkey); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public static function aead_xchacha20poly1305_ietf_encrypt( |
| 367 |
#[SensitiveParameter] |
| 368 |
string $message = '', |
| 369 |
string $ad = '', |
| 370 |
string $nonce = '', |
| 371 |
#[SensitiveParameter] |
| 372 |
string $key = '' |
| 373 |
): string { |
| 374 |
$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
| 375 |
ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
| 376 |
$key |
| 377 |
); |
| 378 |
$nonceLast = "\x00\x00\x00\x00" . |
| 379 |
ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
| 380 |
|
| 381 |
return self::aead_chacha20poly1305_ietf_encrypt($message, $ad, $nonceLast, $subkey); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
public static function auth( |
| 395 |
string $message, |
| 396 |
#[SensitiveParameter] |
| 397 |
string $key |
| 398 |
): string { |
| 399 |
return ParagonIE_Sodium_Core_Util::substr( |
| 400 |
hash_hmac('sha512', $message, $key, true), |
| 401 |
0, |
| 402 |
32 |
| 403 |
); |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
public static function auth_verify( |
| 419 |
string $mac, |
| 420 |
string $message, |
| 421 |
#[SensitiveParameter] |
| 422 |
string $key |
| 423 |
): bool { |
| 424 |
return ParagonIE_Sodium_Core_Util::hashEquals( |
| 425 |
$mac, |
| 426 |
self::auth($message, $key) |
| 427 |
); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public static function box( |
| 443 |
#[SensitiveParameter] |
| 444 |
string $plaintext, |
| 445 |
string $nonce, |
| 446 |
#[SensitiveParameter] |
| 447 |
string $keypair |
| 448 |
): string { |
| 449 |
return self::secretbox( |
| 450 |
$plaintext, |
| 451 |
$nonce, |
| 452 |
self::box_beforenm( |
| 453 |
self::box_secretkey($keypair), |
| 454 |
self::box_publickey($keypair) |
| 455 |
) |
| 456 |
); |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
public static function box_seal( |
| 471 |
#[SensitiveParameter] |
| 472 |
string $message, |
| 473 |
string $publicKey |
| 474 |
): string { |
| 475 |
|
| 476 |
$ephemeralKeypair = self::box_keypair(); |
| 477 |
|
| 478 |
|
| 479 |
$ephemeralSK = self::box_secretkey($ephemeralKeypair); |
| 480 |
|
| 481 |
|
| 482 |
$ephemeralPK = self::box_publickey($ephemeralKeypair); |
| 483 |
|
| 484 |
|
| 485 |
$nonce = self::generichash( |
| 486 |
$ephemeralPK . $publicKey, |
| 487 |
'', |
| 488 |
24 |
| 489 |
); |
| 490 |
|
| 491 |
|
| 492 |
$keypair = self::box_keypair_from_secretkey_and_publickey($ephemeralSK, $publicKey); |
| 493 |
|
| 494 |
|
| 495 |
$ciphertext = self::box($message, $nonce, $keypair); |
| 496 |
try { |
| 497 |
ParagonIE_Sodium_Compat::memzero($ephemeralKeypair); |
| 498 |
ParagonIE_Sodium_Compat::memzero($ephemeralSK); |
| 499 |
ParagonIE_Sodium_Compat::memzero($nonce); |
| 500 |
} catch (SodiumException) { |
| 501 |
$ephemeralKeypair = null; |
| 502 |
$ephemeralSK = null; |
| 503 |
$nonce = null; |
| 504 |
} |
| 505 |
return $ephemeralPK . $ciphertext; |
| 506 |
} |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
public static function box_seal_open( |
| 520 |
string $message, |
| 521 |
#[SensitiveParameter] |
| 522 |
string $keypair |
| 523 |
): string { |
| 524 |
|
| 525 |
$ephemeralPK = ParagonIE_Sodium_Core_Util::substr($message, 0, 32); |
| 526 |
|
| 527 |
|
| 528 |
$ciphertext = ParagonIE_Sodium_Core_Util::substr($message, 32); |
| 529 |
|
| 530 |
|
| 531 |
$secretKey = self::box_secretkey($keypair); |
| 532 |
|
| 533 |
|
| 534 |
$publicKey = self::box_publickey($keypair); |
| 535 |
|
| 536 |
|
| 537 |
$nonce = self::generichash( |
| 538 |
$ephemeralPK . $publicKey, |
| 539 |
'', |
| 540 |
24 |
| 541 |
); |
| 542 |
|
| 543 |
|
| 544 |
$keypair = self::box_keypair_from_secretkey_and_publickey($secretKey, $ephemeralPK); |
| 545 |
|
| 546 |
|
| 547 |
$m = self::box_open($ciphertext, $nonce, $keypair); |
| 548 |
try { |
| 549 |
ParagonIE_Sodium_Compat::memzero($secretKey); |
| 550 |
ParagonIE_Sodium_Compat::memzero($ephemeralPK); |
| 551 |
ParagonIE_Sodium_Compat::memzero($nonce); |
| 552 |
} catch (SodiumException) { |
| 553 |
$secretKey = null; |
| 554 |
$ephemeralPK = null; |
| 555 |
$nonce = null; |
| 556 |
} |
| 557 |
return $m; |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
public static function box_beforenm( |
| 572 |
#[SensitiveParameter] |
| 573 |
string $sk, |
| 574 |
string $pk |
| 575 |
): string |
| 576 |
{ |
| 577 |
return ParagonIE_Sodium_Core_HSalsa20::hsalsa20( |
| 578 |
str_repeat("\x00", 16), |
| 579 |
self::scalarmult($sk, $pk) |
| 580 |
); |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
public static function box_keypair(): string |
| 592 |
{ |
| 593 |
$sKey = random_bytes(32); |
| 594 |
$pKey = self::scalarmult_base($sKey); |
| 595 |
return $sKey . $pKey; |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
public static function box_seed_keypair( |
| 605 |
#[SensitiveParameter] |
| 606 |
string $seed |
| 607 |
): string |
| 608 |
{ |
| 609 |
$sKey = ParagonIE_Sodium_Core_Util::substr( |
| 610 |
hash('sha512', $seed, true), |
| 611 |
0, |
| 612 |
32 |
| 613 |
); |
| 614 |
$pKey = self::scalarmult_base($sKey); |
| 615 |
return $sKey . $pKey; |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
public static function box_keypair_from_secretkey_and_publickey( |
| 627 |
#[SensitiveParameter] |
| 628 |
string $sKey, |
| 629 |
string $pKey |
| 630 |
): string { |
| 631 |
return ParagonIE_Sodium_Core_Util::substr($sKey, 0, 32) . |
| 632 |
ParagonIE_Sodium_Core_Util::substr($pKey, 0, 32); |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
public static function box_secretkey( |
| 644 |
#[SensitiveParameter] |
| 645 |
string $keypair |
| 646 |
): string { |
| 647 |
if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== 64) { |
| 648 |
throw new RangeException( |
| 649 |
'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' |
| 650 |
); |
| 651 |
} |
| 652 |
return ParagonIE_Sodium_Core_Util::substr($keypair, 0, 32); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
public static function box_publickey( |
| 664 |
#[SensitiveParameter] |
| 665 |
string $keypair |
| 666 |
): string { |
| 667 |
if (ParagonIE_Sodium_Core_Util::strlen($keypair) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES) { |
| 668 |
throw new RangeException( |
| 669 |
'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_KEYPAIRBYTES bytes long.' |
| 670 |
); |
| 671 |
} |
| 672 |
return ParagonIE_Sodium_Core_Util::substr($keypair, 32, 32); |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
public static function box_publickey_from_secretkey( |
| 685 |
#[SensitiveParameter] |
| 686 |
string $sKey |
| 687 |
): string { |
| 688 |
if (ParagonIE_Sodium_Core_Util::strlen($sKey) !== ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES) { |
| 689 |
throw new RangeException( |
| 690 |
'Must be ParagonIE_Sodium_Compat::CRYPTO_BOX_SECRETKEYBYTES bytes long.' |
| 691 |
); |
| 692 |
} |
| 693 |
return self::scalarmult_base($sKey); |
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
public static function box_open( |
| 709 |
string $ciphertext, |
| 710 |
string $nonce, |
| 711 |
#[SensitiveParameter] |
| 712 |
string $keypair |
| 713 |
): string { |
| 714 |
return self::secretbox_open( |
| 715 |
$ciphertext, |
| 716 |
$nonce, |
| 717 |
self::box_beforenm( |
| 718 |
self::box_secretkey($keypair), |
| 719 |
self::box_publickey($keypair) |
| 720 |
) |
| 721 |
); |
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
public static function generichash( |
| 738 |
string $message, |
| 739 |
#[SensitiveParameter] |
| 740 |
?string $key = '', |
| 741 |
int $outlen = 32 |
| 742 |
): string { |
| 743 |
|
| 744 |
ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
| 745 |
|
| 746 |
$k = null; |
| 747 |
|
| 748 |
if (!empty($key)) { |
| 749 |
|
| 750 |
$k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
| 751 |
if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
| 752 |
throw new RangeException('Invalid key size'); |
| 753 |
} |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
$in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
| 758 |
|
| 759 |
|
| 760 |
$ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outlen); |
| 761 |
ParagonIE_Sodium_Core_BLAKE2b::update($ctx, $in, $in->count()); |
| 762 |
|
| 763 |
|
| 764 |
$out = new SplFixedArray($outlen); |
| 765 |
$out = ParagonIE_Sodium_Core_BLAKE2b::finish($ctx, $out); |
| 766 |
|
| 767 |
|
| 768 |
$outArray = $out->toArray(); |
| 769 |
return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
public static function generichash_final(string $ctx, int $outlen = 32): string |
| 784 |
{ |
| 785 |
$out = new SplFixedArray($outlen); |
| 786 |
$context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
| 787 |
$out = ParagonIE_Sodium_Core_BLAKE2b::finish($context, $out); |
| 788 |
$outArray = $out->toArray(); |
| 789 |
return ParagonIE_Sodium_Core_Util::intArrayToString($outArray); |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
public static function generichash_init( |
| 805 |
#[SensitiveParameter] |
| 806 |
string $key = '', |
| 807 |
int $outputLength = 32 |
| 808 |
): string { |
| 809 |
|
| 810 |
ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
| 811 |
|
| 812 |
$k = null; |
| 813 |
if (!empty($key)) { |
| 814 |
$k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
| 815 |
if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
| 816 |
throw new RangeException('Invalid key size'); |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
$ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength); |
| 822 |
|
| 823 |
return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
public static function generichash_init_salt_personal( |
| 841 |
#[SensitiveParameter] |
| 842 |
string $key = '', |
| 843 |
int $outputLength = 32, |
| 844 |
string $salt = '', |
| 845 |
string $personal = '' |
| 846 |
): string { |
| 847 |
|
| 848 |
ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
| 849 |
|
| 850 |
$k = null; |
| 851 |
if (!empty($key)) { |
| 852 |
$k = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($key); |
| 853 |
if ($k->count() > ParagonIE_Sodium_Core_BLAKE2b::KEYBYTES) { |
| 854 |
throw new RangeException('Invalid key size'); |
| 855 |
} |
| 856 |
} |
| 857 |
if (!empty($salt)) { |
| 858 |
$s = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($salt); |
| 859 |
} else { |
| 860 |
$s = null; |
| 861 |
} |
| 862 |
if (!empty($salt)) { |
| 863 |
$p = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($personal); |
| 864 |
} else { |
| 865 |
$p = null; |
| 866 |
} |
| 867 |
$ctx = ParagonIE_Sodium_Core_BLAKE2b::init($k, $outputLength, $s, $p); |
| 868 |
return ParagonIE_Sodium_Core_BLAKE2b::contextToString($ctx); |
| 869 |
} |
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
public static function generichash_update(string $ctx, string $message): string |
| 883 |
{ |
| 884 |
|
| 885 |
ParagonIE_Sodium_Core_BLAKE2b::pseudoConstructor(); |
| 886 |
$context = ParagonIE_Sodium_Core_BLAKE2b::stringToContext($ctx); |
| 887 |
$in = ParagonIE_Sodium_Core_BLAKE2b::stringToSplFixedArray($message); |
| 888 |
ParagonIE_Sodium_Core_BLAKE2b::update($context, $in, $in->count()); |
| 889 |
return ParagonIE_Sodium_Core_BLAKE2b::contextToString($context); |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
public static function keyExchange( |
| 906 |
#[SensitiveParameter] |
| 907 |
string $my_sk, |
| 908 |
string $their_pk, |
| 909 |
string $client_pk, |
| 910 |
string $server_pk |
| 911 |
): string { |
| 912 |
return ParagonIE_Sodium_Compat::crypto_generichash( |
| 913 |
ParagonIE_Sodium_Compat::crypto_scalarmult($my_sk, $their_pk) . |
| 914 |
$client_pk . |
| 915 |
$server_pk |
| 916 |
); |
| 917 |
} |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
public static function scalarmult( |
| 932 |
#[SensitiveParameter] |
| 933 |
string $sKey, |
| 934 |
string $pKey |
| 935 |
): string { |
| 936 |
$q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10($sKey, $pKey); |
| 937 |
self::scalarmult_throw_if_zero($q); |
| 938 |
return $q; |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
public static function scalarmult_base( |
| 952 |
#[SensitiveParameter] |
| 953 |
string $secret |
| 954 |
): string { |
| 955 |
$q = ParagonIE_Sodium_Core_X25519::crypto_scalarmult_curve25519_ref10_base($secret); |
| 956 |
self::scalarmult_throw_if_zero($q); |
| 957 |
return $q; |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
protected static function scalarmult_throw_if_zero( |
| 969 |
#[SensitiveParameter] |
| 970 |
string $q |
| 971 |
): void { |
| 972 |
$d = 0; |
| 973 |
for ($i = 0; $i < self::box_curve25519xsalsa20poly1305_SECRETKEYBYTES; ++$i) { |
| 974 |
$d |= ParagonIE_Sodium_Core_Util::chrToInt($q[$i]); |
| 975 |
} |
| 976 |
|
| 977 |
|
| 978 |
if (-(1 & (($d - 1) >> 8))) { |
| 979 |
throw new SodiumException('Zero public key is not allowed'); |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
public static function secretbox( |
| 996 |
#[SensitiveParameter] |
| 997 |
string $plaintext, |
| 998 |
string $nonce, |
| 999 |
#[SensitiveParameter] |
| 1000 |
string $key |
| 1001 |
): string { |
| 1002 |
$subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
| 1003 |
$block0 = str_repeat("\x00", 32); |
| 1004 |
|
| 1005 |
|
| 1006 |
$mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
| 1007 |
$mlen0 = min($mlen, 64 - self::secretbox_xsalsa20poly1305_ZEROBYTES); |
| 1008 |
$block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
| 1009 |
|
| 1010 |
|
| 1011 |
$block0 = ParagonIE_Sodium_Core_Salsa20::salsa20_xor( |
| 1012 |
$block0, |
| 1013 |
ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
| 1014 |
$subkey |
| 1015 |
); |
| 1016 |
|
| 1017 |
|
| 1018 |
$c = ParagonIE_Sodium_Core_Util::substr( |
| 1019 |
$block0, |
| 1020 |
self::secretbox_xsalsa20poly1305_ZEROBYTES |
| 1021 |
); |
| 1022 |
if ($mlen > $mlen0) { |
| 1023 |
$c .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
| 1024 |
ParagonIE_Sodium_Core_Util::substr( |
| 1025 |
$plaintext, |
| 1026 |
self::secretbox_xsalsa20poly1305_ZEROBYTES |
| 1027 |
), |
| 1028 |
ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
| 1029 |
1, |
| 1030 |
$subkey |
| 1031 |
); |
| 1032 |
} |
| 1033 |
$state = new ParagonIE_Sodium_Core_Poly1305_State( |
| 1034 |
ParagonIE_Sodium_Core_Util::substr( |
| 1035 |
$block0, |
| 1036 |
0, |
| 1037 |
self::onetimeauth_poly1305_KEYBYTES |
| 1038 |
) |
| 1039 |
); |
| 1040 |
try { |
| 1041 |
ParagonIE_Sodium_Compat::memzero($block0); |
| 1042 |
ParagonIE_Sodium_Compat::memzero($subkey); |
| 1043 |
} catch (SodiumException) { |
| 1044 |
$block0 = null; |
| 1045 |
$subkey = null; |
| 1046 |
} |
| 1047 |
|
| 1048 |
$state->update($c); |
| 1049 |
|
| 1050 |
|
| 1051 |
$c = $state->finish() . $c; |
| 1052 |
unset($state); |
| 1053 |
|
| 1054 |
return $c; |
| 1055 |
} |
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
public static function secretbox_open( |
| 1070 |
string $ciphertext, |
| 1071 |
string $nonce, |
| 1072 |
#[SensitiveParameter] |
| 1073 |
string $key |
| 1074 |
): string { |
| 1075 |
$mac = ParagonIE_Sodium_Core_Util::substr( |
| 1076 |
$ciphertext, |
| 1077 |
0, |
| 1078 |
self::secretbox_xsalsa20poly1305_MACBYTES |
| 1079 |
); |
| 1080 |
|
| 1081 |
$c = ParagonIE_Sodium_Core_Util::substr( |
| 1082 |
$ciphertext, |
| 1083 |
self::secretbox_xsalsa20poly1305_MACBYTES |
| 1084 |
); |
| 1085 |
|
| 1086 |
$clen = ParagonIE_Sodium_Core_Util::strlen($c); |
| 1087 |
|
| 1088 |
$subkey = ParagonIE_Sodium_Core_HSalsa20::hsalsa20($nonce, $key); |
| 1089 |
|
| 1090 |
$block0 = ParagonIE_Sodium_Core_Salsa20::salsa20( |
| 1091 |
64, |
| 1092 |
ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
| 1093 |
$subkey |
| 1094 |
); |
| 1095 |
$verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
| 1096 |
$mac, |
| 1097 |
$c, |
| 1098 |
ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
| 1099 |
); |
| 1100 |
if (!$verified) { |
| 1101 |
try { |
| 1102 |
ParagonIE_Sodium_Compat::memzero($subkey); |
| 1103 |
} catch (SodiumException) { |
| 1104 |
$subkey = null; |
| 1105 |
} |
| 1106 |
throw new SodiumException('Invalid MAC'); |
| 1107 |
} |
| 1108 |
|
| 1109 |
|
| 1110 |
$m = ParagonIE_Sodium_Core_Util::xorStrings( |
| 1111 |
ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xsalsa20poly1305_ZEROBYTES), |
| 1112 |
ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xsalsa20poly1305_ZEROBYTES) |
| 1113 |
); |
| 1114 |
if ($clen > self::secretbox_xsalsa20poly1305_ZEROBYTES) { |
| 1115 |
|
| 1116 |
$m .= ParagonIE_Sodium_Core_Salsa20::salsa20_xor_ic( |
| 1117 |
ParagonIE_Sodium_Core_Util::substr( |
| 1118 |
$c, |
| 1119 |
self::secretbox_xsalsa20poly1305_ZEROBYTES |
| 1120 |
), |
| 1121 |
ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
| 1122 |
1, |
| 1123 |
$subkey |
| 1124 |
); |
| 1125 |
} |
| 1126 |
return $m; |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
public static function secretbox_xchacha20poly1305( |
| 1142 |
#[SensitiveParameter] |
| 1143 |
string $plaintext, |
| 1144 |
string $nonce, |
| 1145 |
#[SensitiveParameter] |
| 1146 |
string $key |
| 1147 |
): string { |
| 1148 |
$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
| 1149 |
ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
| 1150 |
$key |
| 1151 |
); |
| 1152 |
$nonceLast = ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8); |
| 1153 |
$block0 = str_repeat("\x00", 32); |
| 1154 |
|
| 1155 |
|
| 1156 |
$mlen = ParagonIE_Sodium_Core_Util::strlen($plaintext); |
| 1157 |
$mlen0 = $mlen; |
| 1158 |
if ($mlen0 > 64 - self::secretbox_xchacha20poly1305_ZEROBYTES) { |
| 1159 |
$mlen0 = 64 - self::secretbox_xchacha20poly1305_ZEROBYTES; |
| 1160 |
} |
| 1161 |
$block0 .= ParagonIE_Sodium_Core_Util::substr($plaintext, 0, $mlen0); |
| 1162 |
|
| 1163 |
|
| 1164 |
$block0 = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
| 1165 |
$block0, |
| 1166 |
$nonceLast, |
| 1167 |
$subkey |
| 1168 |
); |
| 1169 |
|
| 1170 |
|
| 1171 |
$c = ParagonIE_Sodium_Core_Util::substr( |
| 1172 |
$block0, |
| 1173 |
self::secretbox_xchacha20poly1305_ZEROBYTES |
| 1174 |
); |
| 1175 |
if ($mlen > $mlen0) { |
| 1176 |
$c .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
| 1177 |
ParagonIE_Sodium_Core_Util::substr( |
| 1178 |
$plaintext, |
| 1179 |
self::secretbox_xchacha20poly1305_ZEROBYTES |
| 1180 |
), |
| 1181 |
$nonceLast, |
| 1182 |
$subkey, |
| 1183 |
ParagonIE_Sodium_Core_Util::store64_le(1) |
| 1184 |
); |
| 1185 |
} |
| 1186 |
$state = new ParagonIE_Sodium_Core_Poly1305_State( |
| 1187 |
ParagonIE_Sodium_Core_Util::substr( |
| 1188 |
$block0, |
| 1189 |
0, |
| 1190 |
self::onetimeauth_poly1305_KEYBYTES |
| 1191 |
) |
| 1192 |
); |
| 1193 |
try { |
| 1194 |
ParagonIE_Sodium_Compat::memzero($block0); |
| 1195 |
ParagonIE_Sodium_Compat::memzero($subkey); |
| 1196 |
} catch (SodiumException) { |
| 1197 |
$block0 = null; |
| 1198 |
$subkey = null; |
| 1199 |
} |
| 1200 |
|
| 1201 |
$state->update($c); |
| 1202 |
|
| 1203 |
|
| 1204 |
$c = $state->finish() . $c; |
| 1205 |
unset($state); |
| 1206 |
|
| 1207 |
return $c; |
| 1208 |
} |
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
public static function secretbox_xchacha20poly1305_open( |
| 1223 |
string $ciphertext, |
| 1224 |
string $nonce, |
| 1225 |
#[SensitiveParameter] |
| 1226 |
string $key |
| 1227 |
): string { |
| 1228 |
$mac = ParagonIE_Sodium_Core_Util::substr( |
| 1229 |
$ciphertext, |
| 1230 |
0, |
| 1231 |
self::secretbox_xchacha20poly1305_MACBYTES |
| 1232 |
); |
| 1233 |
|
| 1234 |
$c = ParagonIE_Sodium_Core_Util::substr( |
| 1235 |
$ciphertext, |
| 1236 |
self::secretbox_xchacha20poly1305_MACBYTES |
| 1237 |
); |
| 1238 |
|
| 1239 |
$clen = ParagonIE_Sodium_Core_Util::strlen($c); |
| 1240 |
|
| 1241 |
$subkey = ParagonIE_Sodium_Core_HChaCha20::hchacha20( |
| 1242 |
ParagonIE_Sodium_Core_Util::substr($nonce, 0, 16), |
| 1243 |
$key |
| 1244 |
); |
| 1245 |
|
| 1246 |
$block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
| 1247 |
64, |
| 1248 |
ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
| 1249 |
$subkey |
| 1250 |
); |
| 1251 |
$verified = ParagonIE_Sodium_Core_Poly1305::onetimeauth_verify( |
| 1252 |
$mac, |
| 1253 |
$c, |
| 1254 |
ParagonIE_Sodium_Core_Util::substr($block0, 0, 32) |
| 1255 |
); |
| 1256 |
|
| 1257 |
if (!$verified) { |
| 1258 |
try { |
| 1259 |
ParagonIE_Sodium_Compat::memzero($subkey); |
| 1260 |
} catch (SodiumException) { |
| 1261 |
$subkey = null; |
| 1262 |
} |
| 1263 |
throw new SodiumException('Invalid MAC'); |
| 1264 |
} |
| 1265 |
|
| 1266 |
|
| 1267 |
$m = ParagonIE_Sodium_Core_Util::xorStrings( |
| 1268 |
ParagonIE_Sodium_Core_Util::substr($block0, self::secretbox_xchacha20poly1305_ZEROBYTES), |
| 1269 |
ParagonIE_Sodium_Core_Util::substr($c, 0, self::secretbox_xchacha20poly1305_ZEROBYTES) |
| 1270 |
); |
| 1271 |
|
| 1272 |
if ($clen > self::secretbox_xchacha20poly1305_ZEROBYTES) { |
| 1273 |
|
| 1274 |
$m .= ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
| 1275 |
ParagonIE_Sodium_Core_Util::substr( |
| 1276 |
$c, |
| 1277 |
self::secretbox_xchacha20poly1305_ZEROBYTES |
| 1278 |
), |
| 1279 |
ParagonIE_Sodium_Core_Util::substr($nonce, 16, 8), |
| 1280 |
$subkey, |
| 1281 |
ParagonIE_Sodium_Core_Util::store64_le(1) |
| 1282 |
); |
| 1283 |
} |
| 1284 |
return $m; |
| 1285 |
} |
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
public static function secretstream_xchacha20poly1305_init_push( |
| 1294 |
#[SensitiveParameter] |
| 1295 |
string $key |
| 1296 |
): array { |
| 1297 |
|
| 1298 |
$out = random_bytes(24); |
| 1299 |
|
| 1300 |
|
| 1301 |
$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
| 1302 |
ParagonIE_Sodium_Core_Util::substr($out, 0, 16), |
| 1303 |
$key |
| 1304 |
); |
| 1305 |
$state = new ParagonIE_Sodium_Core_SecretStream_State( |
| 1306 |
$subkey, |
| 1307 |
ParagonIE_Sodium_Core_Util::substr($out, 16, 8) . str_repeat("\0", 4) |
| 1308 |
); |
| 1309 |
|
| 1310 |
|
| 1311 |
$state->counterReset(); |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
return array( |
| 1317 |
$state->toString(), |
| 1318 |
$out |
| 1319 |
); |
| 1320 |
} |
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
public static function secretstream_xchacha20poly1305_init_pull( |
| 1329 |
#[SensitiveParameter] |
| 1330 |
string $key, |
| 1331 |
string $header |
| 1332 |
): string { |
| 1333 |
|
| 1334 |
$subkey = ParagonIE_Sodium_Core_HChaCha20::hChaCha20( |
| 1335 |
ParagonIE_Sodium_Core_Util::substr($header, 0, 16), |
| 1336 |
$key |
| 1337 |
); |
| 1338 |
$state = new ParagonIE_Sodium_Core_SecretStream_State( |
| 1339 |
$subkey, |
| 1340 |
ParagonIE_Sodium_Core_Util::substr($header, 16) |
| 1341 |
); |
| 1342 |
$state->counterReset(); |
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
|
| 1347 |
return $state->toString(); |
| 1348 |
} |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
public static function secretstream_xchacha20poly1305_push( |
| 1359 |
#[SensitiveParameter] |
| 1360 |
string &$state, |
| 1361 |
#[SensitiveParameter] |
| 1362 |
string $msg, |
| 1363 |
string $aad = '', |
| 1364 |
int $tag = 0 |
| 1365 |
): string { |
| 1366 |
$st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
| 1367 |
|
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
|
| 1373 |
$msglen = ParagonIE_Sodium_Core_Util::strlen($msg); |
| 1374 |
$aadlen = ParagonIE_Sodium_Core_Util::strlen($aad); |
| 1375 |
|
| 1376 |
if ((($msglen + 63) >> 6) > 0xfffffffe) { |
| 1377 |
throw new SodiumException( |
| 1378 |
'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes' |
| 1379 |
); |
| 1380 |
} |
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
$auth = new ParagonIE_Sodium_Core_Poly1305_State( |
| 1393 |
ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey()) |
| 1394 |
); |
| 1395 |
|
| 1396 |
|
| 1397 |
$auth->update($aad); |
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
$auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf))); |
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
$block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
| 1408 |
ParagonIE_Sodium_Core_Util::intToChr($tag) . str_repeat("\0", 63), |
| 1409 |
$st->getCombinedNonce(), |
| 1410 |
$st->getKey(), |
| 1411 |
ParagonIE_Sodium_Core_Util::store64_le(1) |
| 1412 |
); |
| 1413 |
|
| 1414 |
|
| 1415 |
$auth->update($block); |
| 1416 |
|
| 1417 |
|
| 1418 |
$out = $block[0]; |
| 1419 |
|
| 1420 |
|
| 1421 |
$cipher = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
| 1422 |
$msg, |
| 1423 |
$st->getCombinedNonce(), |
| 1424 |
$st->getKey(), |
| 1425 |
ParagonIE_Sodium_Core_Util::store64_le(2) |
| 1426 |
); |
| 1427 |
|
| 1428 |
|
| 1429 |
$auth->update($cipher); |
| 1430 |
|
| 1431 |
$out .= $cipher; |
| 1432 |
unset($cipher); |
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
$auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf))); |
| 1437 |
|
| 1438 |
|
| 1439 |
$slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen); |
| 1440 |
|
| 1441 |
|
| 1442 |
$auth->update($slen); |
| 1443 |
|
| 1444 |
|
| 1445 |
$slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen); |
| 1446 |
|
| 1447 |
|
| 1448 |
$auth->update($slen); |
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
$mac = $auth->finish(); |
| 1453 |
$out .= $mac; |
| 1454 |
|
| 1455 |
|
| 1456 |
unset($auth); |
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
$st->xorNonce($mac); |
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
$st->incrementCounter(); |
| 1466 |
|
| 1467 |
$state = $st->toString(); |
| 1468 |
|
| 1469 |
|
| 1470 |
$rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0; |
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
if ($rekey || $st->needsRekey()) { |
| 1477 |
|
| 1478 |
self::secretstream_xchacha20poly1305_rekey($state); |
| 1479 |
} |
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
return $out; |
| 1484 |
} |
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
public static function secretstream_xchacha20poly1305_pull( |
| 1494 |
#[SensitiveParameter] |
| 1495 |
string &$state, |
| 1496 |
string $cipher, |
| 1497 |
string $aad = '' |
| 1498 |
): bool|array { |
| 1499 |
$st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
| 1500 |
|
| 1501 |
$cipherlen = ParagonIE_Sodium_Core_Util::strlen($cipher); |
| 1502 |
|
| 1503 |
$msglen = $cipherlen - ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES; |
| 1504 |
$aadlen = ParagonIE_Sodium_Core_Util::strlen($aad); |
| 1505 |
|
| 1506 |
|
| 1507 |
|
| 1508 |
|
| 1509 |
if ((($msglen + 63) >> 6) > 0xfffffffe) { |
| 1510 |
throw new SodiumException( |
| 1511 |
'message cannot be larger than SODIUM_CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX bytes' |
| 1512 |
); |
| 1513 |
} |
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
$auth = new ParagonIE_Sodium_Core_Poly1305_State( |
| 1519 |
ParagonIE_Sodium_Core_ChaCha20::ietfStream(32, $st->getCombinedNonce(), $st->getKey()) |
| 1520 |
); |
| 1521 |
|
| 1522 |
|
| 1523 |
$auth->update($aad); |
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
$auth->update(str_repeat("\0", ((0x10 - $aadlen) & 0xf))); |
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
$block = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
| 1535 |
$cipher[0] . str_repeat("\0", 63), |
| 1536 |
$st->getCombinedNonce(), |
| 1537 |
$st->getKey(), |
| 1538 |
ParagonIE_Sodium_Core_Util::store64_le(1) |
| 1539 |
); |
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
$tag = ParagonIE_Sodium_Core_Util::chrToInt($block[0]); |
| 1544 |
$block[0] = $cipher[0]; |
| 1545 |
$auth->update($block); |
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
$auth->update(ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen)); |
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
$auth->update(str_repeat("\0", ((0x10 - 64 + $msglen) & 0xf))); |
| 1555 |
|
| 1556 |
|
| 1557 |
|
| 1558 |
$slen = ParagonIE_Sodium_Core_Util::store64_le($aadlen); |
| 1559 |
$auth->update($slen); |
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
$slen = ParagonIE_Sodium_Core_Util::store64_le(64 + $msglen); |
| 1564 |
$auth->update($slen); |
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
$mac = $auth->finish(); |
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
$stored = ParagonIE_Sodium_Core_Util::substr($cipher, $msglen + 1, 16); |
| 1577 |
if (!ParagonIE_Sodium_Core_Util::hashEquals($mac, $stored)) { |
| 1578 |
return false; |
| 1579 |
} |
| 1580 |
|
| 1581 |
|
| 1582 |
$out = ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
| 1583 |
ParagonIE_Sodium_Core_Util::substr($cipher, 1, $msglen), |
| 1584 |
$st->getCombinedNonce(), |
| 1585 |
$st->getKey(), |
| 1586 |
ParagonIE_Sodium_Core_Util::store64_le(2) |
| 1587 |
); |
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
$st->xorNonce($mac); |
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
$st->incrementCounter(); |
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
$state = $st->toString(); |
| 1605 |
|
| 1606 |
|
| 1607 |
$rekey = ($tag & ParagonIE_Sodium_Compat::CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY) !== 0; |
| 1608 |
if ($rekey || $st->needsRekey()) { |
| 1609 |
|
| 1610 |
self::secretstream_xchacha20poly1305_rekey($state); |
| 1611 |
} |
| 1612 |
return array($out, $tag); |
| 1613 |
} |
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
public static function secretstream_xchacha20poly1305_rekey( |
| 1621 |
#[SensitiveParameter] |
| 1622 |
string &$state |
| 1623 |
): void { |
| 1624 |
$st = ParagonIE_Sodium_Core_SecretStream_State::fromString($state); |
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
$new_key_and_inonce = $st->getKey(); |
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
$new_key_and_inonce .= ParagonIE_Sodium_Core_Util::substR($st->getNonce(), 0, 8); |
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
$st->rekey(ParagonIE_Sodium_Core_ChaCha20::ietfStreamXorIc( |
| 1644 |
$new_key_and_inonce, |
| 1645 |
$st->getCombinedNonce(), |
| 1646 |
$st->getKey(), |
| 1647 |
ParagonIE_Sodium_Core_Util::store64_le(0) |
| 1648 |
)); |
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
|
| 1656 |
|
| 1657 |
|
| 1658 |
$st->counterReset(); |
| 1659 |
|
| 1660 |
$state = $st->toString(); |
| 1661 |
} |
| 1662 |
|
| 1663 |
|
| 1664 |
|
| 1665 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
public static function sign_detached( |
| 1675 |
string $message, |
| 1676 |
#[SensitiveParameter] |
| 1677 |
string $sk |
| 1678 |
): string { |
| 1679 |
return ParagonIE_Sodium_Core_Ed25519::sign_detached($message, $sk); |
| 1680 |
} |
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
public static function sign( |
| 1694 |
string $message, |
| 1695 |
#[SensitiveParameter] |
| 1696 |
string $sk |
| 1697 |
): string { |
| 1698 |
return ParagonIE_Sodium_Core_Ed25519::sign($message, $sk); |
| 1699 |
} |
| 1700 |
|
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
|
| 1708 |
|
| 1709 |
|
| 1710 |
|
| 1711 |
|
| 1712 |
public static function sign_open( |
| 1713 |
string $signedMessage, |
| 1714 |
#[SensitiveParameter] |
| 1715 |
string $pk |
| 1716 |
): string { |
| 1717 |
return ParagonIE_Sodium_Core_Ed25519::sign_open($signedMessage, $pk); |
| 1718 |
} |
| 1719 |
|
| 1720 |
|
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
|
| 1725 |
|
| 1726 |
|
| 1727 |
|
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
public static function sign_verify_detached( |
| 1733 |
string $signature, |
| 1734 |
string $message, |
| 1735 |
string $pk |
| 1736 |
): bool { |
| 1737 |
return ParagonIE_Sodium_Core_Ed25519::verify_detached($signature, $message, $pk); |
| 1738 |
} |
| 1739 |
} |