| @@ 140-178 (lines=39) @@ | ||
| 137 | * @param string $key |
|
| 138 | * @return string |
|
| 139 | */ |
|
| 140 | public static function aead_chacha20poly1305_encrypt( |
|
| 141 | $message = '', |
|
| 142 | $ad = '', |
|
| 143 | $nonce = '', |
|
| 144 | $key = '' |
|
| 145 | ) { |
|
| 146 | /** @var int $len - Length of the plaintext message */ |
|
| 147 | $len = ParagonIE_Sodium_Core_Util::strlen($message); |
|
| 148 | ||
| 149 | /** @var int $adlen - Length of the associated data */ |
|
| 150 | $adlen = ParagonIE_Sodium_Core_Util::strlen($ad); |
|
| 151 | ||
| 152 | /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
| 153 | $block0 = ParagonIE_Sodium_Core_ChaCha20::stream( |
|
| 154 | 32, |
|
| 155 | $nonce, |
|
| 156 | $key |
|
| 157 | ); |
|
| 158 | $state = new ParagonIE_Sodium_Core_Poly1305_State($block0); |
|
| 159 | try { |
|
| 160 | ParagonIE_Sodium_Compat::memzero($block0); |
|
| 161 | } catch (Error $ex) { |
|
| 162 | $block0 = null; |
|
| 163 | } |
|
| 164 | ||
| 165 | /** @var string $ciphertext - Raw encrypted data */ |
|
| 166 | $ciphertext = ParagonIE_Sodium_Core_ChaCha20::streamXorIc( |
|
| 167 | $message, |
|
| 168 | $nonce, |
|
| 169 | $key, |
|
| 170 | ParagonIE_Sodium_Core_Util::store64_le(1) |
|
| 171 | ); |
|
| 172 | ||
| 173 | $state->update($ad); |
|
| 174 | $state->update(ParagonIE_Sodium_Core_Util::store64_le($adlen)); |
|
| 175 | $state->update($ciphertext); |
|
| 176 | $state->update(ParagonIE_Sodium_Core_Util::store64_le($len)); |
|
| 177 | return $ciphertext . $state->finish(); |
|
| 178 | } |
|
| 179 | ||
| 180 | /** |
|
| 181 | * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) |
|
| @@ 140-178 (lines=39) @@ | ||
| 137 | * @param string $key |
|
| 138 | * @return string |
|
| 139 | */ |
|
| 140 | public static function aead_chacha20poly1305_encrypt( |
|
| 141 | $message = '', |
|
| 142 | $ad = '', |
|
| 143 | $nonce = '', |
|
| 144 | $key = '' |
|
| 145 | ) { |
|
| 146 | /** @var int $len - Length of the plaintext message */ |
|
| 147 | $len = ParagonIE_Sodium_Core32_Util::strlen($message); |
|
| 148 | ||
| 149 | /** @var int $adlen - Length of the associated data */ |
|
| 150 | $adlen = ParagonIE_Sodium_Core32_Util::strlen($ad); |
|
| 151 | ||
| 152 | /** @var string The first block of the chacha20 keystream, used as a poly1305 key */ |
|
| 153 | $block0 = ParagonIE_Sodium_Core32_ChaCha20::stream( |
|
| 154 | 32, |
|
| 155 | $nonce, |
|
| 156 | $key |
|
| 157 | ); |
|
| 158 | $state = new ParagonIE_Sodium_Core32_Poly1305_State($block0); |
|
| 159 | try { |
|
| 160 | ParagonIE_Sodium_Compat::memzero($block0); |
|
| 161 | } catch (Error $ex) { |
|
| 162 | $block0 = null; |
|
| 163 | } |
|
| 164 | ||
| 165 | /** @var string $ciphertext - Raw encrypted data */ |
|
| 166 | $ciphertext = ParagonIE_Sodium_Core32_ChaCha20::streamXorIc( |
|
| 167 | $message, |
|
| 168 | $nonce, |
|
| 169 | $key, |
|
| 170 | ParagonIE_Sodium_Core32_Util::store64_le(1) |
|
| 171 | ); |
|
| 172 | ||
| 173 | $state->update($ad); |
|
| 174 | $state->update(ParagonIE_Sodium_Core32_Util::store64_le($adlen)); |
|
| 175 | $state->update($ciphertext); |
|
| 176 | $state->update(ParagonIE_Sodium_Core32_Util::store64_le($len)); |
|
| 177 | return $ciphertext . $state->finish(); |
|
| 178 | } |
|
| 179 | ||
| 180 | /** |
|
| 181 | * AEAD Decryption with ChaCha20-Poly1305, IETF mode (96-bit nonce) |
|