Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
namespace Defuse\Crypto;

use \Defuse\Crypto\Exception as Ex;
use \Defuse\Crypto\Crypto;

final class Core
{
Expand Down
45 changes: 43 additions & 2 deletions src/Key.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,25 @@ final class Key
private $key_bytes = null;
private $config = null;

/**
* Creates a new random Key object for use with this library.
*
* @return \Defuse\Crypto\Key
*/
public static function CreateNewRandomKey()
{
$config = self::GetKeyVersionConfigFromKeyHeader(self::KEY_CURRENT_VERSION);
$bytes = Core::secureRandom($config->keyByteSize());
return new Key(self::KEY_CURRENT_VERSION, $bytes);
}

/**
* Loads a Key object from an ASCII-safe string
*
* @param string $savedKeyString
* @return \Defuse\Crypto\Key
* @throws Ex\CannotPerformOperationException
*/
public static function LoadFromAsciiSafeString($savedKeyString)
{
try {
Expand Down Expand Up @@ -195,13 +207,26 @@ public static function LoadFromAsciiSafeString($savedKeyString)
return new Key($version_header, $key_bytes);
}

/**
* Private constructor -> cannot be instantiated directly:
*
* $key = new Key("\xDE\xF0\x02\x00", "some_key_string"); // errors
*
* @param string $version_header
* @param string $bytes
*/
private function __construct($version_header, $bytes)
{
$this->key_version_header = $version_header;
$this->key_bytes = $bytes;
$this->config = self::GetKeyVersionConfigFromKeyHeader($this->key_version_header);
}

/**
* Encodes the key as an ASCII string, with a checksum, for storing.
*
* @return string
*/
public function saveToAsciiSafeString()
{
return Encoding::binToHex(
Expand All @@ -221,6 +246,12 @@ public function isSafeForCipherTextVersion($major, $minor)
return $major == 2 && $minor == 0;
}

/**
* Get the raw bytes of the encryption key
*
* @return string
* @throws CannotPerformOperationException
*/
public function getRawBytes()
{
if (is_null($this->key_bytes) || Core::ourStrlen($this->key_bytes) < self::MIN_SAFE_KEY_BYTE_SIZE) {
Expand All @@ -231,6 +262,13 @@ public function getRawBytes()
return $this->key_bytes;
}

/**
* Parse a key header, get the configuration
*
* @param string $key_header
* @return \Defuse\Crypto\KeyConfig
* @throws Ex\CannotPerformOperationException
*/
private static function GetKeyVersionConfigFromKeyHeader($key_header) {
if ($key_header === self::KEY_CURRENT_VERSION) {
return new KeyConfig([
Expand All @@ -244,8 +282,11 @@ private static function GetKeyVersionConfigFromKeyHeader($key_header) {
);
}

/*
* NEVER use this, exept for testing.
/**
* NEVER use this, except for testing.
*
* @param string $bytes
* @return \Defuse\Crypto\Key
*/
public static function LoadFromRawBytesForTestingPurposesOnlyInsecure($bytes)
{
Expand Down
22 changes: 17 additions & 5 deletions src/RuntimeTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class RuntimeTests extends Crypto
{
/*
/**
* Runs tests.
* Raises Ex\CannotPerformOperationException or Ex\CryptoTestFailedException if
* one of the tests fail. If any tests fails, your system is not capable of
Expand Down Expand Up @@ -74,6 +74,13 @@ public static function runtimeTest()
$test_state = 1;
}

/**
* Run-time test: string encryption and decryption
*
* @param \Defuse\Crypto\Config $config
*
* @throws Ex\CryptoTestFailedException
*/
private static function testEncryptDecrypt($config)
{
$key = Crypto::createNewRandomKey();
Expand Down Expand Up @@ -129,7 +136,9 @@ private static function testEncryptDecrypt($config)
}

/**
* Run-time testing
* Run-time testing: HKDF
*
* @param \Defuse\Crypto\Config $config
*
* @throws Ex\CryptoTestFailedException
*/
Expand Down Expand Up @@ -171,7 +180,7 @@ private static function HKDFTestVector($config)
}

/**
* Run-Time tests
* Run-Time testing: HMAC
*
* @throws Ex\CryptoTestFailedException
*/
Expand All @@ -190,7 +199,9 @@ private static function HMACTestVector($config)
}

/**
* Run-time tests
* Run-time testing: AES-256-CTR
*
* @param \Defuse\Crypto\Config $config
*
* @throws Ex\CryptoTestFailedException
*/
Expand Down Expand Up @@ -219,12 +230,14 @@ private static function AESTestVector($config)

$computed_ciphertext = Crypto::plainEncrypt($plaintext, $key, $iv, $config);
if ($computed_ciphertext !== $ciphertext) {
/*
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can revert this if you still need it for testing.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can actually delete all those commented-out lines.

echo str_repeat("\n", 30);
var_dump($config);
echo \bin2hex($computed_ciphertext);
echo "\n---\n";
echo \bin2hex($ciphertext);
echo str_repeat("\n", 30);
*/
throw new Ex\CryptoTestFailedException();
}

Expand All @@ -233,5 +246,4 @@ private static function AESTestVector($config)
throw new Ex\CryptoTestFailedException();
}
}

}