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

Commit b3a4175

Browse files
authored
Native EC Key Generator and AES GCM dependency updated (#151)
* AES GCM algs updated and ECKey Generator modified
1 parent 923b6e9 commit b3a4175

5 files changed

Lines changed: 60 additions & 48 deletions

File tree

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ language: php
33
sudo: true
44

55
matrix:
6+
allow_failures:
7+
- php: nightly
68
fast_finish: true
79
include:
810
- php: 5.6
911
env: deps=low
1012
- php: 5.6
11-
env: WITH_CRYPTO=true
1213
- php: 7.0
13-
env: deps=low
1414
- php: 7.0
15-
env: WITH_CRYPTO=true
15+
env: deps=low
1616
- php: 7.1
1717
- php: hhvm
1818
- php: hhvm

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"lib-openssl": "*",
3030
"spomky-labs/base64url": "^1.0",
3131
"spomky-labs/aes-key-wrap": "^3.0",
32-
"spomky-labs/php-aes-gcm": "^1.0",
32+
"spomky-labs/php-aes-gcm": "^1.2",
3333
"beberlei/assert": "^2.4",
3434
"symfony/polyfill-mbstring": "^1.1",
3535
"symfony/polyfill-php70": "^1.1",

src/Algorithm/ContentEncryption/AESGCM.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,6 @@ public function encryptContent($data, $cek, $iv, $aad, $encoded_protected_header
2727
$calculated_aad .= '.'.$aad;
2828
}
2929

30-
if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
31-
return openssl_encrypt($data, $this->getMode($cek), $cek, OPENSSL_RAW_DATA, $iv, $tag, $calculated_aad, 16);
32-
} elseif (class_exists('\Crypto\Cipher')) {
33-
$cipher = Cipher::aes(Cipher::MODE_GCM, $this->getKeySize());
34-
$calculated_aad = $encoded_protected_header;
35-
if (null !== $aad) {
36-
$calculated_aad .= '.'.$aad;
37-
}
38-
39-
$cipher->setAAD($calculated_aad);
40-
$cyphertext = $cipher->encrypt($data, $cek, $iv);
41-
$tag = $cipher->getTag();
42-
43-
return $cyphertext;
44-
}
45-
4630
list($cyphertext, $tag) = GCM::encrypt($cek, $iv, $data, $calculated_aad);
4731

4832
return $cyphertext;

src/Algorithm/KeyEncryption/AESGCMKW.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,8 @@ public function wrapKey(JWKInterface $key, $cek, array $complete_headers, array
3232
$iv = random_bytes(96 / 8);
3333
$additional_headers['iv'] = Base64Url::encode($iv);
3434

35-
if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
36-
$tag = null;
37-
$encrypted_cek = openssl_encrypt($cek, $this->getMode($kek), $kek, OPENSSL_RAW_DATA, $iv, $tag, null, 16);
38-
$additional_headers['tag'] = Base64Url::encode($tag);
39-
} elseif (class_exists('\Crypto\Cipher')) {
40-
$cipher = Cipher::aes(Cipher::MODE_GCM, $this->getKeySize());
41-
$cipher->setAAD(null);
42-
$encrypted_cek = $cipher->encrypt($cek, $kek, $iv);
43-
44-
$additional_headers['tag'] = Base64Url::encode($cipher->getTag());
45-
} else {
46-
list($encrypted_cek, $tag) = AESGCM::encrypt($kek, $iv, $cek, null);
47-
$additional_headers['tag'] = Base64Url::encode($tag);
48-
}
35+
list($encrypted_cek, $tag) = AESGCM::encrypt($kek, $iv, $cek, null);
36+
$additional_headers['tag'] = Base64Url::encode($tag);
4937

5038
return $encrypted_cek;
5139
}

src/Factory/JWKFactory.php

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Assert\Assertion;
1515
use Base64Url\Base64Url;
16+
use Jose\KeyConverter\ECKey;
1617
use Jose\KeyConverter\KeyConverter;
1718
use Jose\KeyConverter\RSAKey;
1819
use Jose\Object\JKUJWKSet;
@@ -119,20 +120,38 @@ public static function createECKey(array $values)
119120
{
120121
Assertion::keyExists($values, 'crv', 'The curve is not set.');
121122
$curve = $values['crv'];
122-
$curve_name = self::getNistName($curve);
123-
$generator = CurveFactory::getGeneratorByName($curve_name);
124-
$private_key = $generator->createPrivateKey();
125-
126-
$values = array_merge(
127-
$values,
128-
[
129-
'kty' => 'EC',
130-
'crv' => $curve,
131-
'x' => self::encodeValue($private_key->getPublicKey()->getPoint()->getX()),
132-
'y' => self::encodeValue($private_key->getPublicKey()->getPoint()->getY()),
133-
'd' => self::encodeValue($private_key->getSecret()),
134-
]
135-
);
123+
if (function_exists('openssl_get_curve_names')) {
124+
$args = [
125+
'curve_name' => self::getOpensslName($curve),
126+
'private_key_type' => OPENSSL_KEYTYPE_EC,
127+
];
128+
$key = openssl_pkey_new($args);
129+
$res = openssl_pkey_export($key, $out);
130+
Assertion::true($res, 'Unable to create the key');
131+
132+
$rsa = new ECKey($out);
133+
$values = array_merge(
134+
$values,
135+
$rsa->toArray()
136+
);
137+
138+
return new JWK($values);
139+
} else {
140+
$curve_name = self::getNistName($curve);
141+
$generator = CurveFactory::getGeneratorByName($curve_name);
142+
$private_key = $generator->createPrivateKey();
143+
144+
$values = array_merge(
145+
$values,
146+
[
147+
'kty' => 'EC',
148+
'crv' => $curve,
149+
'x' => self::encodeValue($private_key->getPublicKey()->getPoint()->getX()),
150+
'y' => self::encodeValue($private_key->getPublicKey()->getPoint()->getY()),
151+
'd' => self::encodeValue($private_key->getSecret()),
152+
]
153+
);
154+
}
136155

137156
return new JWK($values);
138157
}
@@ -233,6 +252,27 @@ private static function convertDecToBin($value)
233252
return hex2bin($adapter->decHex($value));
234253
}
235254

255+
/**
256+
* @param string $curve
257+
*
258+
* @throws \InvalidArgumentException
259+
*
260+
* @return string
261+
*/
262+
private static function getOpensslName($curve)
263+
{
264+
switch ($curve) {
265+
case 'P-256':
266+
return 'prime256v1';
267+
case 'P-384':
268+
return 'secp384r1';
269+
case 'P-521':
270+
return 'secp521r1';
271+
default:
272+
throw new \InvalidArgumentException(sprintf('The curve "%s" is not supported.', $curve));
273+
}
274+
}
275+
236276
/**
237277
* @param string $curve
238278
*

0 commit comments

Comments
 (0)