Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent Morselli committed Jan 26, 2016
1 parent 7cd5dd0 commit 7a58b62
Show file tree
Hide file tree
Showing 76 changed files with 2,834 additions and 2,144 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,20 @@
"spomky-labs/base64url": "^1.0",
"spomky-labs/pbkdf2": "^1.0",
"spomky-labs/aes-key-wrap": "^2.0",
"mdanter/ecc": "0.3",
"phpseclib/phpseclib": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^4.5|^5.0",
"satooshi/php-coveralls": "^1.0"
"satooshi/php-coveralls": "^1.0",
"mdanter/ecc": "0.3"
},
"suggest":{
"ext-crypto": "Required to use AES GCM based algorithms."
"ext-crypto": "Required to use AES GCM based algorithms.",
"mdanter/ecc": "Required to use ECC based algorithms (use v0.3 only)."
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "2.0.x-dev"
}
}
}
14 changes: 8 additions & 6 deletions src/Algorithm/ContentEncryption/AESCBCHS.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@

namespace Jose\Algorithm\ContentEncryption;

use Jose\Algorithm\ContentEncryptionAlgorithmInterface;
use Jose\Util\StringUtil;
/**
*
*/
abstract class AESCBCHS implements ContentEncryptionInterface
abstract class AESCBCHS implements ContentEncryptionAlgorithmInterface
{
/**
* {@inheritdoc}
*/
public function encryptContent($data, $cek, $iv, $aad, $encoded_protected_header, &$tag)
{
$k = substr($cek, strlen($cek) / 2);
$k = StringUtil::substr($cek, StringUtil::strlen($cek) / 2);

$cyphertext = AESOpenSSL::encrypt($data, $k, $iv);

Expand All @@ -47,7 +49,7 @@ public function decryptContent($data, $cek, $iv, $aad, $encoded_protected_header
return;
}

$k = substr($cek, strlen($cek) / 2);
$k = StringUtil::substr($cek, StringUtil::strlen($cek) / 2);

return AESOpenSSL::decrypt($data, $k, $iv);
}
Expand All @@ -67,8 +69,8 @@ protected function calculateAuthenticationTag($encrypted_data, $cek, $iv, $aad,
if (null !== $aad) {
$calculated_aad .= '.'.$aad;
}
$mac_key = substr($cek, 0, strlen($cek) / 2);
$auth_data_length = strlen($encoded_header);
$mac_key = StringUtil::substr($cek, 0, StringUtil::strlen($cek) / 2);
$auth_data_length = StringUtil::strlen($encoded_header);

$secured_input = implode('', [
$calculated_aad,
Expand All @@ -78,7 +80,7 @@ protected function calculateAuthenticationTag($encrypted_data, $cek, $iv, $aad,
]);
$hash = hash_hmac($this->getHashAlgorithm(), $secured_input, $mac_key, true);

return substr($hash, 0, strlen($hash) / 2);
return StringUtil::substr($hash, 0, StringUtil::strlen($hash) / 2);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Algorithm/ContentEncryption/AESGCM.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
namespace Jose\Algorithm\ContentEncryption;

use Crypto\Cipher;
use Jose\Algorithm\ContentEncryptionAlgorithmInterface;

/**
*
*/
abstract class AESGCM implements ContentEncryptionInterface
abstract class AESGCM implements ContentEncryptionAlgorithmInterface
{
/**
* {@inheritdoc}
Expand Down
22 changes: 21 additions & 1 deletion src/Algorithm/ContentEncryption/AESOpenSSL.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,43 @@
*/

namespace Jose\Algorithm\ContentEncryption;
use Jose\Util\StringUtil;

/**
*/
final class AESOpenSSL implements AESInterface
{
/**
* @param string $data
* @param string $k
* @param string $iv
*
* @return string
*/
public static function encrypt($data, $k, $iv)
{
return openssl_encrypt($data, self::getMode($k), $k, OPENSSL_RAW_DATA, $iv);
}

/**
* @param string $data
* @param string $k
* @param string $iv
*
* @return string
*/
public static function decrypt($data, $k, $iv)
{
return openssl_decrypt($data, self::getMode($k), $k, OPENSSL_RAW_DATA, $iv);
}

/**
* @param string $k
*
* @return string
*/
private static function getMode($k)
{
return 'aes-'.(8 * strlen($k)).'-cbc';
return 'aes-'.(8 * StringUtil::strlen($k)).'-cbc';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
* of the MIT license. See the LICENSE file for details.
*/

namespace Jose\Algorithm\ContentEncryption;
namespace Jose\Algorithm;

use Jose\Algorithm\EncryptionInterface;

interface ContentEncryptionInterface extends EncryptionInterface
interface ContentEncryptionAlgorithmInterface extends JWAInterface
{
/**
* Encrypt data.
Expand Down
16 changes: 0 additions & 16 deletions src/Algorithm/EncryptionInterface.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/Algorithm/KeyEncryption/AESGCMKW.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function decryptKey(JWKInterface $key, $encryted_cek, array $header)
*/
protected function checkKey(JWKInterface $key)
{
if (!$key->has('kty') || 'oct' !== $key->get('kty') || !$key->has('k')) {
if ('oct' !== $key->get('kty') || !$key->has('k')) {
throw new \InvalidArgumentException('The key is not valid');
}
}
Expand Down
19 changes: 14 additions & 5 deletions src/Algorithm/KeyEncryption/AESKW.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

use Base64Url\Base64Url;
use Jose\Object\JWKInterface;
use Jose\Util\StringUtil;

/**
* Class AESKW.
*/
abstract class AESKW implements KeyEncryptionInterface
abstract class AESKW implements KeyWrappingInterface
{
/**
* @param \Jose\Object\JWKInterface $key
Expand All @@ -26,7 +27,7 @@ abstract class AESKW implements KeyEncryptionInterface
*
* @return mixed
*/
public function encryptKey(JWKInterface $key, $cek, array &$header)
public function wrapKey(JWKInterface $key, $cek, array &$header)
{
$this->checkKey($key);
$wrapper = $this->getWrapper();
Expand All @@ -41,23 +42,31 @@ public function encryptKey(JWKInterface $key, $cek, array &$header)
*
* @return mixed
*/
public function decryptKey(JWKInterface $key, $encryted_cek, array $header)
public function unwrapKey(JWKInterface $key, $encryted_cek, array $header)
{
$this->checkKey($key);
$wrapper = $this->getWrapper();

return $wrapper->unwrap(Base64Url::decode($key->get('k')), $encryted_cek);
}

/**
* {@inheritdoc}
*/
public function getKeyManagementMode()
{
return self::MODE_WRAP;
}

/**
* @param \Jose\Object\JWKInterface $key
*/
protected function checkKey(JWKInterface $key)
{
if (!$key->has('kty') || 'oct' !== $key->get('kty') || !$key->has('k')) {
if ('oct' !== $key->get('kty') || !$key->has('k')) {
throw new \InvalidArgumentException('The key is not valid');
}
if ($this->getKeySize() !== strlen(Base64Url::decode($key->get('k')))) {
if ($this->getKeySize() !== StringUtil::strlen(Base64Url::decode($key->get('k')))) {
throw new \InvalidArgumentException('The key size is not valid');
}
}
Expand Down
12 changes: 10 additions & 2 deletions src/Algorithm/KeyEncryption/Dir.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ final class Dir implements DirectEncryptionInterface
/**
* {@inheritdoc}
*/
public function getCEK(JWKInterface $key, array $header)
public function getCEK(JWKInterface $key)
{
if (!$key->has('kty') || 'dir' !== $key->get('kty') || !$key->has('dir')) {
if ('dir' !== $key->get('kty') || !$key->has('dir')) {
throw new \InvalidArgumentException('The key is not valid');
}

Expand All @@ -35,4 +35,12 @@ public function getAlgorithmName()
{
return 'dir';
}

/**
* {@inheritdoc}
*/
public function getKeyManagementMode()
{
return self::MODE_DIRECT;
}
}
7 changes: 3 additions & 4 deletions src/Algorithm/KeyEncryption/DirectEncryptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@

namespace Jose\Algorithm\KeyEncryption;

use Jose\Algorithm\EncryptionInterface;
use Jose\Algorithm\KeyEncryptionAlgorithmInterface;
use Jose\Object\JWKInterface;

/**
*
*/
interface DirectEncryptionInterface extends EncryptionInterface
interface DirectEncryptionInterface extends KeyEncryptionAlgorithmInterface
{
/**
* @param \Jose\Object\JWKInterface $key The key used to get the CEK
* @param array $header The complete header of the JWT
*
* @throws \Exception If key does not support the algorithm or if the key usage does not authorize the operation
*
* @return string The CEK
*/
public function getCEK(JWKInterface $key, array $header);
public function getCEK(JWKInterface $key);
}
10 changes: 9 additions & 1 deletion src/Algorithm/KeyEncryption/ECDHES.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ public function getAlgorithmName()
return 'ECDH-ES';
}

/**
* {@inheritdoc}
*/
public function getKeyManagementMode()
{
return self::MODE_AGREEMENT;
}

/**
* @param array $complete_header
*
Expand All @@ -126,7 +134,7 @@ private function getPublicKey(array $complete_header)
*/
private function checkKey(JWKInterface $key, $is_private)
{
if (!$key->has('kty') || 'EC' !== $key->get('kty')) {
if ('EC' !== $key->get('kty')) {
throw new \InvalidArgumentException('The key type must be "EC"');
}
if (!$key->has('x') || !$key->has('y') || !$key->has('crv')) {
Expand Down
8 changes: 8 additions & 0 deletions src/Algorithm/KeyEncryption/ECDHESAESKW.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public function unwrapAgreementKey(JWKInterface $receiver_key, $encrypted_cek, $
return $wrapper->unwrap($agreement_key, $encrypted_cek);
}

/**
* {@inheritdoc}
*/
public function getKeyManagementMode()
{
return self::MODE_WRAP;
}

/**
* @return \AESKW\A128KW|\AESKW\A192KW|\AESKW\A256KW
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Algorithm/KeyEncryption/KeyAgreementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

namespace Jose\Algorithm\KeyEncryption;

use Jose\Algorithm\EncryptionInterface;
use Jose\Algorithm\KeyEncryptionAlgorithmInterface;
use Jose\Object\JWKInterface;

/**
*
*/
interface KeyAgreementInterface extends EncryptionInterface
interface KeyAgreementInterface extends KeyEncryptionAlgorithmInterface
{
/**
* @param int $encryption_key_length Size of the key expected for the algorithm used for data encryption
Expand Down
4 changes: 2 additions & 2 deletions src/Algorithm/KeyEncryption/KeyAgreementWrappingInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

namespace Jose\Algorithm\KeyEncryption;

use Jose\Algorithm\EncryptionInterface;
use Jose\Algorithm\KeyEncryptionAlgorithmInterface;
use Jose\Object\JWKInterface;

/**
*
*/
interface KeyAgreementWrappingInterface extends EncryptionInterface
interface KeyAgreementWrappingInterface extends KeyEncryptionAlgorithmInterface
{
/**
* Wrap the agreement key.
Expand Down
4 changes: 2 additions & 2 deletions src/Algorithm/KeyEncryption/KeyEncryptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

namespace Jose\Algorithm\KeyEncryption;

use Jose\Algorithm\EncryptionInterface;
use Jose\Algorithm\KeyEncryptionAlgorithmInterface;
use Jose\Object\JWKInterface;

/**
*
*/
interface KeyEncryptionInterface extends EncryptionInterface
interface KeyEncryptionInterface extends KeyEncryptionAlgorithmInterface
{
/**
* Encrypt the CEK.
Expand Down
Loading

0 comments on commit 7a58b62

Please sign in to comment.