Skip to content

Commit

Permalink
Native Scalar Multiplication support (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky authored Apr 22, 2019
1 parent 7d1ec31 commit 44e5d2f
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 109 deletions.
1 change: 0 additions & 1 deletion performance/JWE/ECDHESA128KWBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ public function dataRecipientPublicKeys(): array
'crv' => 'X25519',
'kty' => 'OKP',
'x' => 'LD7PfRPxq03bd0WJyf_1z-LQevmrbcYx7jJafep3gmk',
'd' => 'pSdgXFRYMvOa7giAm3Rrf5Mf8GnvLz7HtZKu_KN06KY',
],
],
];
Expand Down
1 change: 0 additions & 1 deletion performance/JWE/ECDHESA192KWBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ public function dataRecipientPublicKeys(): array
'crv' => 'X25519',
'kty' => 'OKP',
'x' => 'LD7PfRPxq03bd0WJyf_1z-LQevmrbcYx7jJafep3gmk',
'd' => 'pSdgXFRYMvOa7giAm3Rrf5Mf8GnvLz7HtZKu_KN06KY',
],
],
];
Expand Down
1 change: 0 additions & 1 deletion performance/JWE/ECDHESA256KWBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ public function dataRecipientPublicKeys(): array
'crv' => 'X25519',
'kty' => 'OKP',
'x' => 'LD7PfRPxq03bd0WJyf_1z-LQevmrbcYx7jJafep3gmk',
'd' => 'pSdgXFRYMvOa7giAm3Rrf5Mf8GnvLz7HtZKu_KN06KY',
],
],
];
Expand Down
221 changes: 115 additions & 106 deletions src/Component/Core/Util/ECKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Jose\Component\Core\Util\Ecc\Curve;
use Jose\Component\Core\Util\Ecc\NistCurve;
use RuntimeException;
use Throwable;

/**
* @internal
Expand All @@ -29,9 +30,9 @@ public static function convertToPEM(JWK $jwk): string
{
if ($jwk->has('d')) {
return self::convertPrivateKeyToPEM($jwk);
} else {
return self::convertPublicKeyToPEM($jwk);
}

return self::convertPublicKeyToPEM($jwk);
}

public static function convertPublicKeyToPEM(JWK $jwk): string
Expand All @@ -50,11 +51,11 @@ public static function convertPublicKeyToPEM(JWK $jwk): string

break;
default:
throw new \InvalidArgumentException('Unsupported curve.');
throw new InvalidArgumentException('Unsupported curve.');
}
$der .= self::getKey($jwk);
$pem = '-----BEGIN PUBLIC KEY-----'.PHP_EOL;
$pem .= \chunk_split(\base64_encode($der), 64, PHP_EOL);
$pem .= chunk_split(base64_encode($der), 64, PHP_EOL);
$pem .= '-----END PUBLIC KEY-----'.PHP_EOL;

return $pem;
Expand All @@ -76,11 +77,11 @@ public static function convertPrivateKeyToPEM(JWK $jwk): string

break;
default:
throw new \InvalidArgumentException('Unsupported curve.');
throw new InvalidArgumentException('Unsupported curve.');
}
$der .= self::getKey($jwk);
$pem = '-----BEGIN EC PRIVATE KEY-----'.PHP_EOL;
$pem .= \chunk_split(\base64_encode($der), 64, PHP_EOL);
$pem .= chunk_split(base64_encode($der), 64, PHP_EOL);
$pem .= '-----END EC PRIVATE KEY-----'.PHP_EOL;

return $pem;
Expand All @@ -96,27 +97,40 @@ public static function createECKey(string $curve, array $values = []): JWK
{
try {
$jwk = self::createECKeyUsingOpenSSL($curve);
} catch (\Exception $e) {
} catch (Throwable $e) {
$jwk = self::createECKeyUsingPurePhp($curve);
}
$values = \array_merge($values, $jwk);
$values = array_merge($values, $jwk);

return new JWK($values);
}

private static function getNistCurve(string $curve): Curve
{
switch ($curve) {
case 'P-256':
return NistCurve::curve256();
case 'P-384':
return NistCurve::curve384();
case 'P-521':
return NistCurve::curve521();
default:
throw new InvalidArgumentException(sprintf('The curve "%s" is not supported.', $curve));
}
}

private static function createECKeyUsingPurePhp(string $curve): array
{
$nistCurve = static::getCurve($curve);
$componentSize = (int) ceil($nistCurve->getSize() / 8);
$nistCurve = self::getNistCurve($curve);
$privateKey = $nistCurve->createPrivateKey();
$publicKey = $nistCurve->createPublicKey($privateKey);

return [
'kty' => 'EC',
'crv' => $curve,
'd' => Base64Url::encode(str_pad(gmp_export($privateKey->getSecret()), $componentSize, "\0", STR_PAD_LEFT)),
'x' => Base64Url::encode(str_pad(gmp_export($publicKey->getPoint()->getX()), $componentSize, "\0", STR_PAD_LEFT)),
'y' => Base64Url::encode(str_pad(gmp_export($publicKey->getPoint()->getY()), $componentSize, "\0", STR_PAD_LEFT)),
'x' => Base64Url::encode(str_pad(gmp_export($publicKey->getPoint()->getX()), (int) ceil($nistCurve->getSize() / 8), "\0", STR_PAD_LEFT)),
'y' => Base64Url::encode(str_pad(gmp_export($publicKey->getPoint()->getY()), (int) ceil($nistCurve->getSize() / 8), "\0", STR_PAD_LEFT)),
'd' => Base64Url::encode(str_pad(gmp_export($privateKey->getSecret()), (int) ceil($nistCurve->getSize() / 8), "\0", STR_PAD_LEFT)),
];
}

Expand All @@ -126,40 +140,29 @@ private static function createECKeyUsingOpenSSL(string $curve): array
'curve_name' => self::getOpensslCurveName($curve),
'private_key_type' => OPENSSL_KEYTYPE_EC,
]);
$res = openssl_pkey_export($key, $out);
if (false === $res) {
if (false === $key) {
throw new RuntimeException('Unable to create the key');
}
$result = openssl_pkey_export($key, $out);
if (false === $result) {
throw new RuntimeException('Unable to create the key');
}
$res = openssl_pkey_get_private($out);

if (false === $res) {
throw new RuntimeException('Unable to create the key');
}
$details = openssl_pkey_get_details($res);

$nistCurve = static::getCurve($curve);
$componentSize = (int) ceil($nistCurve->getSize() / 8);
$nistCurve = self::getNistCurve($curve);

return [
'kty' => 'EC',
'crv' => $curve,
'x' => Base64Url::encode(str_pad($details['ec']['x'], $componentSize, "\0", STR_PAD_LEFT)),
'y' => Base64Url::encode(str_pad($details['ec']['y'], $componentSize, "\0", STR_PAD_LEFT)),
'd' => Base64Url::encode(str_pad($details['ec']['d'], $componentSize, "\0", STR_PAD_LEFT)),
'd' => Base64Url::encode(str_pad($details['ec']['d'], (int) ceil($nistCurve->getSize() / 8), "\0", STR_PAD_LEFT)),
'x' => Base64Url::encode(str_pad($details['ec']['x'], (int) ceil($nistCurve->getSize() / 8), "\0", STR_PAD_LEFT)),
'y' => Base64Url::encode(str_pad($details['ec']['y'], (int) ceil($nistCurve->getSize() / 8), "\0", STR_PAD_LEFT)),
];
}

private static function getCurve(string $curve): Curve
{
switch ($curve) {
case 'P-256':
return NistCurve::curve256();
case 'P-384':
return NistCurve::curve384();
case 'P-521':
return NistCurve::curve521();
default:
throw new InvalidArgumentException(\sprintf('The curve "%s" is not supported.', $curve));
}
}

private static function getOpensslCurveName(string $curve): string
{
switch ($curve) {
Expand All @@ -170,114 +173,120 @@ private static function getOpensslCurveName(string $curve): string
case 'P-521':
return 'secp521r1';
default:
throw new InvalidArgumentException(\sprintf('The curve "%s" is not supported.', $curve));
throw new InvalidArgumentException(sprintf('The curve "%s" is not supported.', $curve));
}
}

private static function p256PublicKey(): string
{
return \pack('H*',
return pack(
'H*',
'3059' // SEQUENCE, length 89
.'3013' // SEQUENCE, length 19
.'0607' // OID, length 7
.'2a8648ce3d0201' // 1.2.840.10045.2.1 = EC Public Key
.'0608' // OID, length 8
.'2a8648ce3d030107' // 1.2.840.10045.3.1.7 = P-256 Curve
.'0342' // BIT STRING, length 66
.'00' // prepend with NUL - pubkey will follow
.'3013' // SEQUENCE, length 19
.'0607' // OID, length 7
.'2a8648ce3d0201' // 1.2.840.10045.2.1 = EC Public Key
.'0608' // OID, length 8
.'2a8648ce3d030107' // 1.2.840.10045.3.1.7 = P-256 Curve
.'0342' // BIT STRING, length 66
.'00' // prepend with NUL - pubkey will follow
);
}

private static function p384PublicKey(): string
{
return \pack('H*',
return pack(
'H*',
'3076' // SEQUENCE, length 118
.'3010' // SEQUENCE, length 16
.'0607' // OID, length 7
.'2a8648ce3d0201' // 1.2.840.10045.2.1 = EC Public Key
.'0605' // OID, length 5
.'2b81040022' // 1.3.132.0.34 = P-384 Curve
.'0362' // BIT STRING, length 98
.'00' // prepend with NUL - pubkey will follow
.'3010' // SEQUENCE, length 16
.'0607' // OID, length 7
.'2a8648ce3d0201' // 1.2.840.10045.2.1 = EC Public Key
.'0605' // OID, length 5
.'2b81040022' // 1.3.132.0.34 = P-384 Curve
.'0362' // BIT STRING, length 98
.'00' // prepend with NUL - pubkey will follow
);
}

private static function p521PublicKey(): string
{
return \pack('H*',
return pack(
'H*',
'30819b' // SEQUENCE, length 154
.'3010' // SEQUENCE, length 16
.'0607' // OID, length 7
.'2a8648ce3d0201' // 1.2.840.10045.2.1 = EC Public Key
.'0605' // OID, length 5
.'2b81040023' // 1.3.132.0.35 = P-521 Curve
.'038186' // BIT STRING, length 134
.'00' // prepend with NUL - pubkey will follow
.'3010' // SEQUENCE, length 16
.'0607' // OID, length 7
.'2a8648ce3d0201' // 1.2.840.10045.2.1 = EC Public Key
.'0605' // OID, length 5
.'2b81040023' // 1.3.132.0.35 = P-521 Curve
.'038186' // BIT STRING, length 134
.'00' // prepend with NUL - pubkey will follow
);
}

private static function p256PrivateKey(JWK $jwk): string
{
$d = \unpack('H*', Base64Url::decode($jwk->get('d')))[1];
$dl = \mb_strlen($d, '8bit') / 2;

return \pack('H*',
'30'.\dechex(87 + $dl) // SEQUENCE, length 87+length($d)
.'020101' // INTEGER, 1
.'04'.\dechex($dl) // OCTET STRING, length($d)
.$d
.'a00a' // TAGGED OBJECT #0, length 10
.'0608' // OID, length 8
.'2a8648ce3d030107' // 1.3.132.0.34 = P-384 Curve
.'a144' // TAGGED OBJECT #1, length 68
.'0342' // BIT STRING, length 66
.'00' // prepend with NUL - pubkey will follow
$d = unpack('H*', str_pad(Base64Url::decode($jwk->get('d')), 32, "\0", STR_PAD_LEFT))[1];

return pack(
'H*',
'3077' // SEQUENCE, length 87+length($d)=32
.'020101' // INTEGER, 1
.'0420' // OCTET STRING, length($d) = 32
.$d
.'a00a' // TAGGED OBJECT #0, length 10
.'0608' // OID, length 8
.'2a8648ce3d030107' // 1.3.132.0.34 = P-384 Curve
.'a144' // TAGGED OBJECT #1, length 68
.'0342' // BIT STRING, length 66
.'00' // prepend with NUL - pubkey will follow
);
}

private static function p384PrivateKey(JWK $jwk): string
{
$d = \unpack('H*', Base64Url::decode($jwk->get('d')))[1];
$dl = \mb_strlen($d, '8bit') / 2;

return \pack('H*',
'3081'.\dechex(116 + $dl) // SEQUENCE, length 116 + length($d)
.'020101' // INTEGER, 1
.'04'.\dechex($dl) // OCTET STRING, length($d)
.$d
.'a007' // TAGGED OBJECT #0, length 7
.'0605' // OID, length 5
.'2b81040022' // 1.3.132.0.34 = P-384 Curve
.'a164' // TAGGED OBJECT #1, length 100
.'0362' // BIT STRING, length 98
.'00' // prepend with NUL - pubkey will follow
$d = unpack('H*', str_pad(Base64Url::decode($jwk->get('d')), 48, "\0", STR_PAD_LEFT))[1];

return pack(
'H*',
'3081a4' // SEQUENCE, length 116 + length($d)=48
.'020101' // INTEGER, 1
.'0430' // OCTET STRING, length($d) = 30
.$d
.'a007' // TAGGED OBJECT #0, length 7
.'0605' // OID, length 5
.'2b81040022' // 1.3.132.0.34 = P-384 Curve
.'a164' // TAGGED OBJECT #1, length 100
.'0362' // BIT STRING, length 98
.'00' // prepend with NUL - pubkey will follow
);
}

private static function p521PrivateKey(JWK $jwk): string
{
$d = \unpack('H*', Base64Url::decode($jwk->get('d')))[1];
$dl = \mb_strlen($d, '8bit') / 2;

return \pack('H*',
'3081'.\dechex(154 + $dl) // SEQUENCE, length 154+length(d)
.'020101' // INTEGER, 1
.'04'.\dechex($dl) // OCTET STRING, length(d)
.$d
.'a007' // TAGGED OBJECT #0, length 7
.'0605' // OID, length 5
.'2b81040023' // 1.3.132.0.35 = P-521 Curve
.'a18189' // TAGGED OBJECT #1, length 137
.'038186' // BIT STRING, length 134
.'00' // prepend with NUL - pubkey will follow
$d = unpack('H*', str_pad(Base64Url::decode($jwk->get('d')), 66, "\0", STR_PAD_LEFT))[1];

return pack(
'H*',
'3081dc' // SEQUENCE, length 154 + length($d)=66
.'020101' // INTEGER, 1
.'0442' // OCTET STRING, length(d) = 66
.$d
.'a007' // TAGGED OBJECT #0, length 7
.'0605' // OID, length 5
.'2b81040023' // 1.3.132.0.35 = P-521 Curve
.'a18189' // TAGGED OBJECT #1, length 137
.'038186' // BIT STRING, length 134
.'00' // prepend with NUL - pubkey will follow
);
}

private static function getKey(JWK $jwk): string
{
$nistCurve = self::getNistCurve($jwk->get('crv'));
$length = (int) ceil($nistCurve->getSize() / 8);

return
\pack('H*', '04')
.Base64Url::decode($jwk->get('x'))
.Base64Url::decode($jwk->get('y'));
"\04"
.str_pad(Base64Url::decode($jwk->get('x')), $length, "\0", STR_PAD_LEFT)
.str_pad(Base64Url::decode($jwk->get('y')), $length, "\0", STR_PAD_LEFT);
}
}
10 changes: 10 additions & 0 deletions src/EncryptionAlgorithm/KeyEncryption/ECDHES/ECDHES.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public function calculateAgreementKey(JWK $private_key, JWK $public_key): string
case 'P-384':
case 'P-521':
$curve = $this->getCurve($public_key->get('crv'));
if (\function_exists('openssl_pkey_derive')) {
try {
$publicPem = ECKey::convertPublicKeyToPEM($public_key);
$privatePem = ECKey::convertPrivateKeyToPEM($private_key);

return openssl_pkey_derive($publicPem, $privatePem, $curve->getSize());
} catch (\Throwable $throwable) {
//Does nothing. Will fallback to the pure PHP function
}
}

$rec_x = $this->convertBase64ToGmp($public_key->get('x'));
$rec_y = $this->convertBase64ToGmp($public_key->get('y'));
Expand Down

0 comments on commit 44e5d2f

Please sign in to comment.