43 if (CipherEnum::CIPHER_CHACHA20_OPENSSH() !== $cipher) {
44 throw new \InvalidArgumentException(
'Unsupported cipher');
47 if (!($padding instanceof
None)) {
48 throw new \InvalidArgumentException(
49 'ChaCha20 does not need any padding ' .
50 '(hint: use fpoirotte\Cryptal\Padding\None)' 55 throw new \InvalidArgumentException(
'Invalid tag length');
58 if (64 !== strlen($key)) {
59 throw new \InvalidArgumentException(
'Invalid key length');
63 $this->main =
new ChaCha20($cipher, $mode, $padding, substr($key, 0, 32), 0);
64 $this->header =
new ChaCha20($cipher, $mode, $padding, substr($key, 32), 0);
66 $this->cipher = $cipher;
69 public function encrypt($iv, $data, &$tag = null, $aad =
'')
72 throw new \InvalidArgumentException(
'Invalid Initialization Vector');
75 $polyKey = $this->main->basicXcrypt(str_repeat(
"\x00", 32), $iv, 0);
76 $aad = $this->header->basicXcrypt($aad, $iv, 0);
77 $res = $this->main->basicXcrypt($data, $iv, 1);
82 MacEnum::MAC_POLY1305(),
83 CipherEnum::CIPHER_CHACHA20(),
92 public function decrypt($iv, $data, $tag = null, $aad =
'')
95 throw new \InvalidArgumentException(
'Invalid Initialization Vector');
98 $aad = $this->header->basicXcrypt($aad, $iv, 0);
99 $polyKey = $this->main->basicXcrypt(str_repeat(
"\x00", 32), $iv, 0);
104 MacEnum::MAC_POLY1305(),
105 CipherEnum::CIPHER_CHACHA20(),
111 if ($tag !== $outTag) {
112 throw new \InvalidArgumentException(
'Invalid tag');
115 return $this->main->basicXcrypt($data, $iv, 1);
encrypt($iv, $data, &$tag=null, $aad= '')
decrypt($iv, $data, $tag=null, $aad= '')
static mac(MacEnum $macAlgorithm, SubAlgorithmAbstractEnum $innerAlgorithm, $key, $data, $nonce= '', $raw=false)
$tagLength
Tag length in bytes; 16 when AEAD is enabled, 0 otherwise.
__construct(CipherEnum $cipher, ModeEnum $mode, PaddingInterface $padding, $key, $tagLength=self::DEFAULT_TAG_LENGTH)