Skip to content

Commit

Permalink
More sanity checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-scott committed Jan 14, 2016
1 parent 4f90faa commit d990caa
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Alerts/InvalidSignature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
namespace ParagonIE\Halite\Alerts;

class InvalidSignature extends HaliteAlert
{

}
6 changes: 6 additions & 0 deletions src/Asymmetric/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace ParagonIE\Halite\Asymmetric;

use \ParagonIE\Halite\Alerts as CryptoException;
use \ParagonIE\Halite\Util as CryptoUtil;
use \ParagonIE\Halite\Contract;
use \ParagonIE\Halite\Symmetric\Crypto as SymmetricCrypto;
use \ParagonIE\Halite\Symmetric\EncryptionKey;
Expand Down Expand Up @@ -275,6 +276,11 @@ public static function verify(
if (!$raw) {
$signature = \Sodium\hex2bin($signature);
}
if (CryptoUtil::safeStrlen($signature) !== \Sodium\CRYPTO_SIGN_BYTES) {
throw new CryptoException\InvalidSignature(
'Signature is not the correct length; is it encoded?'
);
}

return \Sodium\crypto_sign_verify_detached(
$signature,
Expand Down
8 changes: 8 additions & 0 deletions src/Asymmetric/EncryptionPublicKey.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace ParagonIE\Halite\Asymmetric;

use \ParagonIE\Halite\Util as CryptoUtil;
use \ParagonIE\Halite\Alerts as CryptoException;

final class EncryptionPublicKey extends PublicKey
{
/**
Expand All @@ -9,6 +12,11 @@ final class EncryptionPublicKey extends PublicKey
*/
public function __construct($keyMaterial = '', ...$args)
{
if (CryptoUtil::safeStrlen($keyMaterial) !== \Sodium\CRYPTO_BOX_PUBLICKEYBYTES) {
throw new CryptoException\InvalidKey(
'Encryption public key must be CRYPTO_BOX_PUBLICKEYBYTES bytes long'
);
}
parent::__construct($keyMaterial, false);
}
}
8 changes: 8 additions & 0 deletions src/Asymmetric/EncryptionSecretKey.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace ParagonIE\Halite\Asymmetric;

use \ParagonIE\Halite\Util as CryptoUtil;
use \ParagonIE\Halite\Alerts as CryptoException;

final class EncryptionSecretKey extends SecretKey
{
/**
Expand All @@ -9,6 +12,11 @@ final class EncryptionSecretKey extends SecretKey
*/
public function __construct($keyMaterial = '', ...$args)
{
if (CryptoUtil::safeStrlen($keyMaterial) !== \Sodium\CRYPTO_BOX_SECRETKEYBYTES) {
throw new CryptoException\InvalidKey(
'Encryption secret key must be CRYPTO_BOX_SECRETKEYBYTES bytes long'
);
}
parent::__construct($keyMaterial, false);
}

Expand Down
8 changes: 8 additions & 0 deletions src/Asymmetric/SignaturePublicKey.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace ParagonIE\Halite\Asymmetric;

use \ParagonIE\Halite\Util as CryptoUtil;
use \ParagonIE\Halite\Alerts as CryptoException;

final class SignaturePublicKey extends PublicKey
{
/**
Expand All @@ -9,6 +12,11 @@ final class SignaturePublicKey extends PublicKey
*/
public function __construct($keyMaterial = '', ...$args)
{
if (CryptoUtil::safeStrlen($keyMaterial) !== \Sodium\CRYPTO_SIGN_PUBLICKEYBYTES) {
throw new CryptoException\InvalidKey(
'Signature public key must be CRYPTO_SIGN_PUBLICKEYBYTES bytes long'
);
}
parent::__construct($keyMaterial, true);
}
}
8 changes: 8 additions & 0 deletions src/Asymmetric/SignatureSecretKey.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php
namespace ParagonIE\Halite\Asymmetric;

use \ParagonIE\Halite\Util as CryptoUtil;
use \ParagonIE\Halite\Alerts as CryptoException;

final class SignatureSecretKey extends SecretKey
{
/**
Expand All @@ -9,6 +12,11 @@ final class SignatureSecretKey extends SecretKey
*/
public function __construct($keyMaterial = '', ...$args)
{
if (CryptoUtil::safeStrlen($keyMaterial) !== \Sodium\CRYPTO_SIGN_SECRETKEYBYTES) {
throw new CryptoException\InvalidKey(
'Signature secret key must be CRYPTO_SIGN_SECRETKEYBYTES bytes long'
);
}
parent::__construct($keyMaterial, true);
}

Expand Down
8 changes: 8 additions & 0 deletions src/Symmetric/AuthenticationKey.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
<?php
namespace ParagonIE\Halite\Symmetric;

use \ParagonIE\Halite\Util as CryptoUtil;
use \ParagonIE\Halite\Alerts as CryptoException;

final class AuthenticationKey extends SecretKey
{
/**
* @param string $keyMaterial - The actual key data
*/
public function __construct($keyMaterial = '', ...$args)
{
if (CryptoUtil::safeStrlen($keyMaterial) !== \Sodium\CRYPTO_AUTH_KEYBYTES) {
throw new CryptoException\InvalidKey(
'Authentication key must be CRYPTO_AUTH_KEYBYTES bytes long'
);
}
parent::__construct($keyMaterial, true);
}
}
5 changes: 5 additions & 0 deletions src/Symmetric/Crypto.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ protected static function verifyMAC(
$message,
$aKey
) {
if (CryptoUtil::safeStrlen($mac) !== \Sodium\CRYPTO_AUTH_BYTES) {
throw new CryptoException\InvalidSignature(
'Message Authentication Code is not the correct length; is it encoded?'
);
}
return \Sodium\crypto_auth_verify(
$mac,
$message,
Expand Down
8 changes: 8 additions & 0 deletions src/Symmetric/EncryptionKey.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
<?php
namespace ParagonIE\Halite\Symmetric;

use \ParagonIE\Halite\Util as CryptoUtil;
use \ParagonIE\Halite\Alerts as CryptoException;

final class EncryptionKey extends SecretKey
{
/**
* @param string $keyMaterial - The actual key data
*/
public function __construct($keyMaterial = '', ...$args)
{
if (CryptoUtil::safeStrlen($keyMaterial) !== \Sodium\CRYPTO_STREAM_KEYBYTES) {
throw new CryptoException\InvalidKey(
'Encryption key must be CRYPTO_STREAM_KEYBYTES bytes long'
);
}
parent::__construct($keyMaterial, false);
}
}

0 comments on commit d990caa

Please sign in to comment.