| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
if (class_exists('ParagonIE_Sodium_Core_XChaCha20', false)) { |
| 5 |
return; |
| 6 |
} |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class ParagonIE_Sodium_Core_XChaCha20 extends ParagonIE_Sodium_Core_HChaCha20 |
| 12 |
{ |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public static function stream( |
| 24 |
int $len, |
| 25 |
string $nonce, |
| 26 |
#[SensitiveParameter] |
| 27 |
string $key |
| 28 |
): string { |
| 29 |
if (self::strlen($nonce) !== 24) { |
| 30 |
throw new SodiumException('Nonce must be 24 bytes long'); |
| 31 |
} |
| 32 |
return self::encryptBytes( |
| 33 |
new ParagonIE_Sodium_Core_ChaCha20_Ctx( |
| 34 |
self::hChaCha20( |
| 35 |
self::substr($nonce, 0, 16), |
| 36 |
$key |
| 37 |
), |
| 38 |
self::substr($nonce, 16, 8) |
| 39 |
), |
| 40 |
str_repeat("\x00", $len) |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public static function ietfStream( |
| 55 |
int $len, |
| 56 |
string $nonce, |
| 57 |
#[SensitiveParameter] |
| 58 |
string $key |
| 59 |
): string { |
| 60 |
if (self::strlen($nonce) !== 24) { |
| 61 |
throw new SodiumException('Nonce must be 24 bytes long'); |
| 62 |
} |
| 63 |
return self::encryptBytes( |
| 64 |
new ParagonIE_Sodium_Core_ChaCha20_IetfCtx( |
| 65 |
self::hChaCha20( |
| 66 |
self::substr($nonce, 0, 16), |
| 67 |
$key |
| 68 |
), |
| 69 |
"\x00\x00\x00\x00" . self::substr($nonce, 16, 8) |
| 70 |
), |
| 71 |
str_repeat("\x00", $len) |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public static function streamXorIc( |
| 87 |
#[SensitiveParameter] |
| 88 |
string $message, |
| 89 |
string $nonce, |
| 90 |
#[SensitiveParameter] |
| 91 |
string $key, |
| 92 |
string $ic = '' |
| 93 |
): string { |
| 94 |
if (self::strlen($nonce) !== 24) { |
| 95 |
throw new SodiumException('Nonce must be 24 bytes long'); |
| 96 |
} |
| 97 |
return self::encryptBytes( |
| 98 |
new ParagonIE_Sodium_Core_ChaCha20_Ctx( |
| 99 |
self::hChaCha20(self::substr($nonce, 0, 16), $key), |
| 100 |
self::substr($nonce, 16, 8), |
| 101 |
$ic |
| 102 |
), |
| 103 |
$message |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public static function ietfStreamXorIc( |
| 119 |
#[SensitiveParameter] |
| 120 |
string $message, |
| 121 |
string $nonce, |
| 122 |
string $key, |
| 123 |
string $ic = '' |
| 124 |
): string { |
| 125 |
if (self::strlen($nonce) !== 24) { |
| 126 |
throw new SodiumException('Nonce must be 24 bytes long'); |
| 127 |
} |
| 128 |
return self::encryptBytes( |
| 129 |
new ParagonIE_Sodium_Core_ChaCha20_IetfCtx( |
| 130 |
self::hChaCha20(self::substr($nonce, 0, 16), $key), |
| 131 |
"\x00\x00\x00\x00" . self::substr($nonce, 16, 8), |
| 132 |
$ic |
| 133 |
), |
| 134 |
$message |
| 135 |
); |
| 136 |
} |
| 137 |
} |