-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CryptoSodium\Aead; | ||
|
||
use PetrKnap\CryptoSodium\CiphertextWithNonce; | ||
use PetrKnap\CryptoSodium\CryptoSodiumTrait; | ||
use PetrKnap\CryptoSodium\DataEraser; | ||
use PetrKnap\CryptoSodium\Exception; | ||
use PetrKnap\CryptoSodium\KeyGenerator; | ||
use PetrKnap\Shorts\Exception\MissingRequirement; | ||
use SensitiveParameter; | ||
|
||
/** | ||
* @see sodium_crypto_aead_aes256gcm_encrypt() | ||
*/ | ||
class Aes256Gcm implements KeyGenerator, DataEraser | ||
{ | ||
use CryptoSodiumTrait; | ||
|
||
public const HEADER_BYTES = SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES; | ||
|
||
public function __construct() | ||
{ | ||
if (!sodium_crypto_aead_aes256gcm_is_available()) { | ||
throw new MissingRequirement(self::class, 'available', 'aes256gcm'); | ||
} | ||
} | ||
|
||
public function generateKey(): string | ||
{ | ||
return sodium_crypto_aead_aes256gcm_keygen(); | ||
} | ||
|
||
/** | ||
* @throws Exception\CouldNotEncryptData | ||
*/ | ||
public function encrypt( | ||
string $message, | ||
#[SensitiveParameter] | ||
string &$key, | ||
?string $nonce = null, | ||
?string $additionalData = null, | ||
): CiphertextWithNonce { | ||
return $this->wrapEncryption(static function (string $message, string $nonce) use (&$key, $additionalData): string { | ||
$additionalData ??= ''; | ||
return sodium_crypto_aead_aes256gcm_encrypt($message, $additionalData, $nonce, $key); | ||
}, $message, $nonce, self::HEADER_BYTES); | ||
} | ||
|
||
/** | ||
* @throws Exception\CouldNotDecryptData | ||
*/ | ||
public function decrypt( | ||
CiphertextWithNonce|string $ciphertext, | ||
#[SensitiveParameter] | ||
string &$key, | ||
?string $nonce = null, | ||
?string $additionalData = null, | ||
): string { | ||
return $this->wrapDecryption(static function (string $ciphertext, string $nonce) use (&$key, $additionalData): string { | ||
$additionalData ??= ''; | ||
$message = sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $additionalData, $nonce, $key); | ||
if ($message === false) { | ||
throw new Exception\CouldNotDecryptData('sodium_crypto_aead_aes256gcm_decrypt', $ciphertext); | ||
} | ||
return $message; | ||
}, $ciphertext, $nonce, self::HEADER_BYTES); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CryptoSodium\Aead; | ||
|
||
use PetrKnap\CryptoSodium\CiphertextWithNonce; | ||
use PetrKnap\CryptoSodium\CryptoSodiumTestCase; | ||
|
||
final class Aes256GcmTest extends CryptoSodiumTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->instance = new Aes256Gcm(); | ||
$key = base64_decode('9+PiVpFGyLSks1UJlpgGL6VuxJ86zzaizKB5e0C3IE8='); | ||
$additionalData = 'AD'; | ||
$this->ciphertextWithNonce = new CiphertextWithNonce( | ||
ciphertext: base64_decode('sycH81OWv2uCxygkUDpBq+Jy7SBl6zSuWjh4zTQ='), | ||
nonce: base64_decode('FJbSk+jybJUfvxrn'), | ||
); | ||
$this->encryptArgsSet = [ | ||
[self::MESSAGE, $key, $this->ciphertextWithNonce->nonce, $additionalData], | ||
]; | ||
$this->decryptArgsSet = [ | ||
[$this->ciphertextWithNonce, $key, null, $additionalData], | ||
[$this->ciphertextWithNonce->toString(), $key, null, $additionalData], | ||
[$this->ciphertextWithNonce->ciphertext, $key, $this->ciphertextWithNonce->nonce, $additionalData], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters