Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #93 from Spomky-Labs/analysis-8bQeV5
Browse files Browse the repository at this point in the history
Applied fixes from StyleCI
  • Loading branch information
Spomky committed Feb 21, 2016
2 parents f07d4ed + b5b96f8 commit 620393d
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 233 deletions.
8 changes: 4 additions & 4 deletions src/Factory/JWKFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public static function createRandomECPrivateKey($curve, array $additional_values
$privKey = $generator->createPrivateKey();

$values = [
"kty" =>"EC",
'kty' => 'EC',
'crv' => $curve,
'x' => Base64Url::encode(self::convertDecToBin($privKey->getPoint()->getX())),
'y' => Base64Url::encode(self::convertDecToBin($privKey->getPoint()->getY())),
'd' => Base64Url::encode(self::convertDecToBin($privKey->getSecret())),
'x' => Base64Url::encode(self::convertDecToBin($privKey->getPoint()->getX())),
'y' => Base64Url::encode(self::convertDecToBin($privKey->getPoint()->getY())),
'd' => Base64Url::encode(self::convertDecToBin($privKey->getSecret())),
];
$values = array_merge($values, $additional_values);

Expand Down
15 changes: 8 additions & 7 deletions src/Util/ConcatKDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/

namespace Jose\Util;

use Base64Url\Base64Url;

/**
Expand All @@ -24,18 +25,18 @@ final class ConcatKDF
/**
* Key Derivation Function.
*
* @param string $Z Shared secret
* @param string $algorithm Encryption algorithm
* @param int $encryption_key_size Size of the encryption key
* @param string $apu Agreement PartyUInfo (information about the producer)
* @param string $apv Agreement PartyVInfo (information about the recipient)
* @param string $Z Shared secret
* @param string $algorithm Encryption algorithm
* @param int $encryption_key_size Size of the encryption key
* @param string $apu Agreement PartyUInfo (information about the producer)
* @param string $apv Agreement PartyVInfo (information about the recipient)
*
* @return string
*/
public static function generate($Z, $algorithm, $encryption_key_size, $apu = '', $apv = '')
{
$apu = !empty($apu)?Base64Url::decode($apu):'';
$apv = !empty($apv)?Base64Url::decode($apv):'';
$apu = !empty($apu) ? Base64Url::decode($apu) : '';
$apv = !empty($apv) ? Base64Url::decode($apv) : '';
$encryption_segments = [
self::toInt32Bits(1), // Round number 1
$Z, // Z (shared secret)
Expand Down
4 changes: 2 additions & 2 deletions src/Util/GCM.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private static function getHash($H, $X)
{
$Y = [];
$Y[0] = StringUtil::addPadding('', 16, "\0");
$num_blocks = (int)(StringUtil::getStringLength($X) / 16);
$num_blocks = (int) (StringUtil::getStringLength($X) / 16);
for ($i = 1; $i <= $num_blocks; $i++) {
$Y[$i] = self::getProduct(self::getBitXor($Y[$i - 1], StringUtil::getSubString($X, ($i - 1) * 16, 16)), $H);
}
Expand All @@ -285,7 +285,7 @@ private static function getGCTR($K, $ICB, $X)
$iv = str_repeat(chr(0), 16); // initialize to 16 byte string of "0"s
mcrypt_generic_init($cipher, $K, $iv);

$n = (int)ceil(self::getLength($X) / 128);
$n = (int) ceil(self::getLength($X) / 128);
$CB = [];
$Y = [];
$CB[1] = $ICB;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ public function testA128KWAndA128GCMEncryptionProtectedContentOnly()
$expected_payload = "You can trust us to stick with you through thick and thin\xe2\x80\x93to the bitter end. And you can trust us to keep any secret of yours\xe2\x80\x93closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.";

$private_key = new JWK([
"kty" => "oct",
"kid" => "81b20965-8332-43d9-a468-82160ad91ac8",
"use" => "enc",
"alg" => "A128KW",
"k" => "GZy6sIZ6wl9NJOKB-jnmVQ",
'kty' => 'oct',
'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
'use' => 'enc',
'alg' => 'A128KW',
'k' => 'GZy6sIZ6wl9NJOKB-jnmVQ',
]);

$protected_headers = [
];

$headers = [
"enc" => "A128GCM",
"alg" => "A128KW",
"kid" => "81b20965-8332-43d9-a468-82160ad91ac8",
'enc' => 'A128GCM',
'alg' => 'A128KW',
'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
];

$expected_flattened_json = '{"unprotected":{"alg":"A128KW","kid":"81b20965-8332-43d9-a468-82160ad91ac8","enc":"A128GCM"},"encrypted_key":"244YHfO_W7RMpQW81UjQrZcq5LSyqiPv","iv":"YihBoVOGsR1l7jCD","ciphertext":"qtPIMMaOBRgASL10dNQhOa7Gqrk7Eal1vwht7R4TT1uq-arsVCPaIeFwQfzrSS6oEUWbBtxEasE0vC6r7sphyVziMCVJEuRJyoAHFSP3eqQPb4Ic1SDSqyXjw_L3svybhHYUGyQuTmUQEDjgjJfBOifwHIsDsRPeBz1NomqeifVPq5GTCWFo5k_MNIQURR2Wj0AHC2k7JZfu2iWjUHLF8ExFZLZ4nlmsvJu_mvifMYiikfNfsZAudISOa6O73yPZtL04k_1FI7WDfrb2w7OqKLWDXzlpcxohPVOLQwpA3mFNRKdY-bQz4Z4KX9lfz1cne31N4-8BKmojpw-OdQjKdLOGkC445Fb_K1tlDQXw2sBF","tag":"e2m0Vm7JvjK2VpCKXS-kyg"}';
Expand All @@ -72,7 +72,6 @@ public function testA128KWAndA128GCMEncryptionProtectedContentOnly()
$this->assertEquals($expected_tag, Base64Url::encode($loaded_flattened_json->getTag()));
$this->assertEquals($expected_cek, Base64Url::encode($loaded_flattened_json->getContentEncryptionKey()));


$this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_json->getCiphertext()));
$this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
$this->assertEquals($expected_iv, Base64Url::encode($loaded_json->getIV()));
Expand All @@ -81,7 +80,6 @@ public function testA128KWAndA128GCMEncryptionProtectedContentOnly()
$this->assertEquals($expected_tag, Base64Url::encode($loaded_json->getTag()));
$this->assertEquals($expected_cek, Base64Url::encode($loaded_json->getContentEncryptionKey()));


$this->assertEquals($expected_payload, $loaded_flattened_json->getPayload());
$this->assertEquals($expected_payload, $loaded_json->getPayload());
}
Expand Down
19 changes: 8 additions & 11 deletions tests/RFC7520/A128KWAndA128GCMEncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public function testA128KWAndA128GCMEncryption()
$expected_payload = "You can trust us to stick with you through thick and thin\xe2\x80\x93to the bitter end. And you can trust us to keep any secret of yours\xe2\x80\x93closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.";

$private_key = new JWK([
"kty" => "oct",
"kid" => "81b20965-8332-43d9-a468-82160ad91ac8",
"use" => "enc",
"alg" => "A128KW",
"k" => "GZy6sIZ6wl9NJOKB-jnmVQ",
'kty' => 'oct',
'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
'use' => 'enc',
'alg' => 'A128KW',
'k' => 'GZy6sIZ6wl9NJOKB-jnmVQ',
]);

$protected_headers = [
"alg" => "A128KW",
"kid" => "81b20965-8332-43d9-a468-82160ad91ac8",
"enc" => "A128GCM",
'alg' => 'A128KW',
'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
'enc' => 'A128GCM',
];

$expected_compact_json = 'eyJhbGciOiJBMTI4S1ciLCJraWQiOiI4MWIyMDk2NS04MzMyLTQzZDktYTQ2OC04MjE2MGFkOTFhYzgiLCJlbmMiOiJBMTI4R0NNIn0.CBI6oDw8MydIx1IBntf_lQcw2MmJKIQx.Qx0pmsDa8KnJc9Jo.AwliP-KmWgsZ37BvzCefNen6VTbRK3QMA4TkvRkH0tP1bTdhtFJgJxeVmJkLD61A1hnWGetdg11c9ADsnWgL56NyxwSYjU1ZEHcGkd3EkU0vjHi9gTlb90qSYFfeF0LwkcTtjbYKCsiNJQkcIp1yeM03OmuiYSoYJVSpf7ej6zaYcMv3WwdxDFl8REwOhNImk2Xld2JXq6BR53TSFkyT7PwVLuq-1GwtGHlQeg7gDT6xW0JqHDPn_H-puQsmthc9Zg0ojmJfqqFvETUxLAF-KjcBTS5dNy6egwkYtOt8EIHK-oEsKYtZRaa8Z7MOZ7UGxGIMvEmxrGCPeJa14slv2-gaqK0kEThkaSqdYw0FkQZF.ER7MWJZ1FBI_NKvn7Zb1Lw';
Expand Down Expand Up @@ -72,23 +72,20 @@ public function testA128KWAndA128GCMEncryption()
$this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_compact_json->getRecipient(0)->getEncryptedKey()));
$this->assertEquals($expected_tag, Base64Url::encode($loaded_compact_json->getTag()));


$this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_flattened_json->getCiphertext()));
$this->assertEquals($protected_headers, $loaded_flattened_json->getSharedProtectedHeaders());
$this->assertEquals($expected_cek, Base64Url::encode($loaded_flattened_json->getContentEncryptionKey()));
$this->assertEquals($expected_iv, Base64Url::encode($loaded_flattened_json->getIV()));
$this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_flattened_json->getRecipient(0)->getEncryptedKey()));
$this->assertEquals($expected_tag, Base64Url::encode($loaded_flattened_json->getTag()));


$this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_json->getCiphertext()));
$this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
$this->assertEquals($expected_cek, Base64Url::encode($loaded_json->getContentEncryptionKey()));
$this->assertEquals($expected_iv, Base64Url::encode($loaded_json->getIV()));
$this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_json->getRecipient(0)->getEncryptedKey()));
$this->assertEquals($expected_tag, Base64Url::encode($loaded_json->getTag()));


$this->assertEquals($expected_payload, $loaded_compact_json->getPayload());
$this->assertEquals($expected_payload, $loaded_flattened_json->getPayload());
$this->assertEquals($expected_payload, $loaded_json->getPayload());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ public function testA128KWAndA128GCMEncryptionWithAdditionalAuthenticatedData()
$expected_payload = "You can trust us to stick with you through thick and thin\xe2\x80\x93to the bitter end. And you can trust us to keep any secret of yours\xe2\x80\x93closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.";

$private_key = new JWK([
"kty" => "oct",
"kid" => "81b20965-8332-43d9-a468-82160ad91ac8",
"use" => "enc",
"alg" => "A128KW",
"k" => "GZy6sIZ6wl9NJOKB-jnmVQ",
'kty' => 'oct',
'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
'use' => 'enc',
'alg' => 'A128KW',
'k' => 'GZy6sIZ6wl9NJOKB-jnmVQ',
]);

$protected_headers = [
"alg" => "A128KW",
"kid" => "81b20965-8332-43d9-a468-82160ad91ac8",
"enc" => "A128GCM",
'alg' => 'A128KW',
'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
'enc' => 'A128GCM',
];

$expected_flattened_json = '{"protected":"eyJhbGciOiJBMTI4S1ciLCJraWQiOiI4MWIyMDk2NS04MzMyLTQzZDktYTQ2OC04MjE2MGFkOTFhYzgiLCJlbmMiOiJBMTI4R0NNIn0","encrypted_key":"4YiiQ_ZzH76TaIkJmYfRFgOV9MIpnx4X","aad":"WyJ2Y2FyZCIsW1sidmVyc2lvbiIse30sInRleHQiLCI0LjAiXSxbImZuIix7fSwidGV4dCIsIk1lcmlhZG9jIEJyYW5keWJ1Y2siXSxbIm4iLHt9LCJ0ZXh0IixbIkJyYW5keWJ1Y2siLCJNZXJpYWRvYyIsIk1yLiIsIiJdXSxbImJkYXkiLHt9LCJ0ZXh0IiwiVEEgMjk4MiJdLFsiZ2VuZGVyIix7fSwidGV4dCIsIk0iXV1d","iv":"veCx9ece2orS7c_N","ciphertext":"Z_3cbr0k3bVM6N3oSNmHz7Lyf3iPppGf3Pj17wNZqteJ0Ui8p74SchQP8xygM1oFRWCNzeIa6s6BcEtp8qEFiqTUEyiNkOWDNoF14T_4NFqF-p2Mx8zkbKxI7oPK8KNarFbyxIDvICNqBLba-v3uzXBdB89fzOI-Lv4PjOFAQGHrgv1rjXAmKbgkft9cB4WeyZw8MldbBhc-V_KWZslrsLNygon_JJWd_ek6LQn5NRehvApqf9ZrxB4aq3FXBxOxCys35PhCdaggy2kfUfl2OkwKnWUbgXVD1C6HxLIlqHhCwXDG59weHrRDQeHyMRoBljoV3X_bUTJDnKBFOod7nLz-cj48JMx3SnCZTpbQAkFV","tag":"vOaH_Rajnpy_3hOtqvZHRA"}';
Expand All @@ -62,7 +62,6 @@ public function testA128KWAndA128GCMEncryptionWithAdditionalAuthenticatedData()
$loaded_json = Loader::load($expected_json);
$this->assertEquals(0, $decrypter->decryptUsingKey($loaded_json, $private_key));


$this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_flattened_json->getCiphertext()));
$this->assertEquals($protected_headers, $loaded_flattened_json->getSharedProtectedHeaders());
$this->assertEquals($expected_iv, Base64Url::encode($loaded_flattened_json->getIV()));
Expand All @@ -71,7 +70,6 @@ public function testA128KWAndA128GCMEncryptionWithAdditionalAuthenticatedData()
$this->assertEquals($expected_aad, $loaded_flattened_json->getAAD());
$this->assertEquals($expected_cek, Base64Url::encode($loaded_flattened_json->getContentEncryptionKey()));


$this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_json->getCiphertext()));
$this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
$this->assertEquals($expected_iv, Base64Url::encode($loaded_json->getIV()));
Expand All @@ -80,7 +78,6 @@ public function testA128KWAndA128GCMEncryptionWithAdditionalAuthenticatedData()
$this->assertEquals($expected_aad, $loaded_json->getAAD());
$this->assertEquals($expected_cek, Base64Url::encode($loaded_json->getContentEncryptionKey()));


$this->assertEquals($expected_payload, $loaded_flattened_json->getPayload());
$this->assertEquals($expected_payload, $loaded_json->getPayload());
}
Expand Down
21 changes: 9 additions & 12 deletions tests/RFC7520/A128KWAndA128GCMEncryptionWithCompressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ public function testA128KWAndA128GCMEncryptionWithCompression()
$expected_payload = "You can trust us to stick with you through thick and thin\xe2\x80\x93to the bitter end. And you can trust us to keep any secret of yours\xe2\x80\x93closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.";

$private_key = new JWK([
"kty" => "oct",
"kid" => "81b20965-8332-43d9-a468-82160ad91ac8",
"use" => "enc",
"alg" => "A128KW",
"k" => "GZy6sIZ6wl9NJOKB-jnmVQ",
'kty' => 'oct',
'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
'use' => 'enc',
'alg' => 'A128KW',
'k' => 'GZy6sIZ6wl9NJOKB-jnmVQ',
]);

$protected_headers = [
"alg" => "A128KW",
"kid" => "81b20965-8332-43d9-a468-82160ad91ac8",
"enc" => "A128GCM",
"zip" => "DEF",
'alg' => 'A128KW',
'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
'enc' => 'A128GCM',
'zip' => 'DEF',
];

$expected_compact_json = 'eyJhbGciOiJBMTI4S1ciLCJraWQiOiI4MWIyMDk2NS04MzMyLTQzZDktYTQ2OC04MjE2MGFkOTFhYzgiLCJlbmMiOiJBMTI4R0NNIiwiemlwIjoiREVGIn0.5vUT2WOtQxKWcekM_IzVQwkGgzlFDwPi.p9pUq6XHY0jfEZIl.HbDtOsdai1oYziSx25KEeTxmwnh8L8jKMFNc1k3zmMI6VB8hry57tDZ61jXyezSPt0fdLVfe6Jf5y5-JaCap_JQBcb5opbmT60uWGml8blyiMQmOn9J--XhhlYg0m-BHaqfDO5iTOWxPxFMUedx7WCy8mxgDHj0aBMG6152PsM-w5E_o2B3jDbrYBKhpYA7qi3AyijnCJ7BP9rr3U8kxExCpG3mK420TjOw.VILuUwuIxaLVmh5X-T7kmA';
Expand Down Expand Up @@ -73,23 +73,20 @@ public function testA128KWAndA128GCMEncryptionWithCompression()
$this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_compact_json->getRecipient(0)->getEncryptedKey()));
$this->assertEquals($expected_tag, Base64Url::encode($loaded_compact_json->getTag()));


$this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_flattened_json->getCiphertext()));
$this->assertEquals($protected_headers, $loaded_flattened_json->getSharedProtectedHeaders());
$this->assertEquals($expected_cek, Base64Url::encode($loaded_flattened_json->getContentEncryptionKey()));
$this->assertEquals($expected_iv, Base64Url::encode($loaded_flattened_json->getIV()));
$this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_flattened_json->getRecipient(0)->getEncryptedKey()));
$this->assertEquals($expected_tag, Base64Url::encode($loaded_flattened_json->getTag()));


$this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_json->getCiphertext()));
$this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
$this->assertEquals($expected_cek, Base64Url::encode($loaded_json->getContentEncryptionKey()));
$this->assertEquals($expected_iv, Base64Url::encode($loaded_json->getIV()));
$this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_json->getRecipient(0)->getEncryptedKey()));
$this->assertEquals($expected_tag, Base64Url::encode($loaded_json->getTag()));


$this->assertEquals($expected_payload, $loaded_compact_json->getPayload());
$this->assertEquals($expected_payload, $loaded_flattened_json->getPayload());
$this->assertEquals($expected_payload, $loaded_json->getPayload());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ public function testA128KWAndA128GCMEncryptionWithSpecificProtectedHeaderValues(
$expected_payload = "You can trust us to stick with you through thick and thin\xe2\x80\x93to the bitter end. And you can trust us to keep any secret of yours\xe2\x80\x93closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo.";

$private_key = new JWK([
"kty" => "oct",
"kid" => "81b20965-8332-43d9-a468-82160ad91ac8",
"use" => "enc",
"alg" => "A128KW",
"k" => "GZy6sIZ6wl9NJOKB-jnmVQ",
'kty' => 'oct',
'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
'use' => 'enc',
'alg' => 'A128KW',
'k' => 'GZy6sIZ6wl9NJOKB-jnmVQ',
]);

$protected_headers = [
"enc" => "A128GCM",
'enc' => 'A128GCM',
];

$headers = [
"alg" => "A128KW",
"kid" => "81b20965-8332-43d9-a468-82160ad91ac8",
'alg' => 'A128KW',
'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
];

$expected_flattened_json = '{"protected":"eyJlbmMiOiJBMTI4R0NNIn0","unprotected":{"alg":"A128KW","kid":"81b20965-8332-43d9-a468-82160ad91ac8"},"encrypted_key":"jJIcM9J-hbx3wnqhf5FlkEYos0sHsF0H","iv":"WgEJsDS9bkoXQ3nR","ciphertext":"lIbCyRmRJxnB2yLQOTqjCDKV3H30ossOw3uD9DPsqLL2DM3swKkjOwQyZtWsFLYMj5YeLht_StAn21tHmQJuuNt64T8D4t6C7kC9OCCJ1IHAolUv4MyOt80MoPb8fZYbNKqplzYJgIL58g8N2v46OgyG637d6uuKPwhAnTGm_zWhqc_srOvgiLkzyFXPq1hBAURbc3-8BqeRb48iR1-_5g5UjWVD3lgiLCN_P7AW8mIiFvUNXBPJK3nOWL4teUPS8yHLbWeL83olU4UAgL48x-8dDkH23JykibVSQju-f7e-1xreHWXzWLHs1NqBbre0dEwK3HX_xM0LjUz77Krppgegoutpf5qaKg3l-_xMINmf","tag":"fNYLqpUe84KD45lvDiaBAQ"}';
Expand All @@ -72,7 +72,6 @@ public function testA128KWAndA128GCMEncryptionWithSpecificProtectedHeaderValues(
$this->assertEquals($expected_tag, Base64Url::encode($loaded_flattened_json->getTag()));
$this->assertEquals($expected_cek, Base64Url::encode($loaded_flattened_json->getContentEncryptionKey()));


$this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_json->getCiphertext()));
$this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
$this->assertEquals($expected_iv, Base64Url::encode($loaded_json->getIV()));
Expand All @@ -81,7 +80,6 @@ public function testA128KWAndA128GCMEncryptionWithSpecificProtectedHeaderValues(
$this->assertEquals($expected_tag, Base64Url::encode($loaded_json->getTag()));
$this->assertEquals($expected_cek, Base64Url::encode($loaded_json->getContentEncryptionKey()));


$this->assertEquals($expected_payload, $loaded_flattened_json->getPayload());
$this->assertEquals($expected_payload, $loaded_json->getPayload());
}
Expand Down
Loading

0 comments on commit 620393d

Please sign in to comment.