[php-src] PR #11499: feat: ext-openssl Explicit EC Parameters support
| From: | Eno-CN | Date: | Wed, 21 Jun 2023 15:33:13 +0000 |
| Subject: | [php-src] PR #11499: feat: ext-openssl Explicit EC Parameters support | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-22778@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/11499
Author: Eno-CN
# Changes
## Add Explicit EC Parameters
> By specifying explicit EC parameters, new curves can be created instead of just using the
> built-in curves that can be generated by "curve name".
[Common EC
Parameters](https://github.com/openssl/openssl/blob/openssl-3.0.7/doc/man7/EVP_PKEY-EC.pod#common-ec-parameters)
The followings are explicit EC parameters that I added :
- ### ~~
field_type (optional)~~
~~The value should be either prime-field**[default value]** or
characteristic-two-field, which correspond to prime field _Fp_ and binary field
_F2^m_.~~ **only prime field supported temporarily**
- ### p
For a curve over _Fp_ , p is the prime for the field.
~~For a curve over _F2^m_ , prepresents the irreducible polynomial - each bit
represents a term in thepolynomial.
Therefore, there will either be three or five bits set dependent on whether the polynomial is a
trinomial or a pentanomial.~~
- ### a
See the following param b.
- ### b
a and b represents the coefficients of the curve
For _Fp_ : **_y^2 mod p = x^3 +ax + b mod p_**
~~For _F2^m_ : **_y^2 + xy = x^3 + ax^2 + b_**~~
- ### generator
The generator is a well defined basepoint _G_ on the curve chosen for cryptographic
operations.
It can be generated by the next params g_x and g_y.
The encoding conforms with Sec. 2.3.3 of the SECG SEC 1 ("Elliptic Curve
Cryptography") standard.
- ### g_x and g_y
Set _(g_x,g_y)_ to define the basepoint _G_ on curve which defined by
params p, a and b.
They are set to generate generator or ignored whether generator is
existed.
- ### order
Integers used for point multiplications will be _[0,order - 1]_.
**NOTICE** : For **SM2**, integers used for the point multiplications will be
_[1,order - 1]_.
- ### seed (optional)
seed is an optional value that is for information purposes only.
It represents the random number seed used to generate the coefficient b from a
random number.
- ### cofactor (optional)
cofactor is an optional value.
order multiplied by the cofactor gives the number of points on the
curve.
## Optimize the code for generating EC _public/private keys_
Adjusted the code style to be consistent with the overall code style of openssl.c and
enhanced the code readability.
> The point (x,y) defines the
> public key.
> d defines the private key.
> A private key can generate a public key.
## Use _EVP_PKEY_generate()_ to generate keypair instead of _EVP_PKEY_keygen()_
_EVP_PKEY_keygen()_ do exactly the same thing as _EVP_PKEY_generate()_, after checking that the
corresponding _EVP_PKEY_keygen_init()_ was used to initialize ctx.
_EVP_PKEY_keygen()_ is an older function that is kept for backward compatibility.
It is safe to use _EVP_PKEY_generate()_ instead.
## Fix sm2 compatibility bugs on _openssl_pkey_get_details_
#9422 - openssl ext sm2 compatibility
[Reference
1](https://github.com/openssl/openssl/blob/openssl-3.1.1/doc/man7/migration_guide.pod?plain=1#L2255-L2263)
[Reference
2](https://github.com/openssl/openssl/blob/openssl-3.0.0/doc/man3/EVP_PKEY_is_a.pod?plain=1#L36-L41)
# Examples
## EC - generate keypair with curve_name
``` php
<?php
/*
* Custom parameters x, y, and d are not supported with SM2 in OpenSSL 3.x.
* Directly creating EVP_PKEY_CTX using EVP_PKEY_CTX_new_from_name(NULL, "SM2", NULL)
* will result in generating incorrect private keys (which cannot be correctly recognized
* by existing external applications based on the SM2 algorithm).
*/
$curve_name = 'SM2';
$pkey = openssl_pkey_new(array(
'ec'=> array(
'curve_name' => $curve_name,
)
));
$details = openssl_pkey_get_details($pkey);
var_dump($details);
$pubkey = $details['key'];
openssl_pkey_export($pkey, $prikey);
echo 'Private Key:', PHP_EOL, $prikey, PHP_EOL;
echo 'Public Key:', PHP_EOL, $pubkey, PHP_EOL;
```
## EC - generate keypair with custom params (ECDSA curve)
``` php
<?php
$d = hex2bin('8D0AC65AAEA0D6B96254C65817D4A143A9E7A03876F1A37D'); // private key binary
$x = hex2bin('98E07AAD50C31F9189EBE6B8B5C70E5DEE59D7A8BC344CC6'); // public key x binary
$y = hex2bin('6109D3D96E52D0867B9D05D72D07BE5876A3D973E0E96792'); // public key y binary
$p = hex2bin('BDB6F4FE3E8B1D9E0DA8C0D46F4C318CEFE4AFE3B6B8551F');
$a = hex2bin('BB8E5E8FBC115E139FE6A814FE48AAA6F0ADA1AA5DF91985');
$b = hex2bin('1854BEBDC31B21B7AEFC80AB0ECD10D5B1B3308E6DBF11C1');
$g_x = hex2bin('4AD5F7048DE709AD51236DE65E4D4B482C836DC6E4106640');
$g_y = hex2bin('02BB3A02D4AAADACAE24817A4CA3A1B014B5270432DB27D2');
$order = hex2bin('BDB6F4FE3E8B1D9E0DA8C0D40FC962195DFAE76F56564677');
$pkey = openssl_pkey_new(array(
'ec'=> array(
'p' => $p,
'a' => $a,
'b' => $b,
'order' => $order,
'g_x' => $g_x,
'g_y' => $g_y,
//'d' => $d, // import the private key to generate keypairs
)
));
$details = openssl_pkey_get_details($pkey);
var_dump($details);
$pubkey = $details['key'];
openssl_pkey_export($pkey, $prikey);
echo 'Private Key:', PHP_EOL, $prikey, PHP_EOL;
echo 'Public Key:', PHP_EOL, $pubkey, PHP_EOL;
```
## EC - generate keypair with custom params (SM2 curve)
``` php
<?php
$p = hex2bin('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF');
$a = hex2bin('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC');
$b = hex2bin('28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93');
$g_x = hex2bin('32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7');
$g_y = hex2bin('BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0');
$order = hex2bin('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123');
/*
* Custom parameters x, y, and d are not supported with SM2 in OpenSSL 3.x.
* Directly creating EVP_PKEY_CTX using EVP_PKEY_CTX_new_from_name(NULL, "SM2", NULL)
* will result in generating incorrect private keys (which cannot be correctly recognized
* by existing external applications based on the SM2 algorithm).
*/
$pkey = openssl_pkey_new(array(
'ec'=> array(
'p' => $p,
'a' => $a,
'b' => $b,
'order' => $order,
'g_x' => $g_x,
'g_y' => $g_y,
)
));
/*
* It is not entirely the same as generating keys through the SM2 curve naming method.
* So the generated key will be in PKCS8 format to store algorithm information.
*/
$details = openssl_pkey_get_details($pkey);
var_dump($details);
$pubkey = $details['key'];
openssl_pkey_export($pkey, $prikey);
echo 'Private Key:', PHP_EOL, $prikey, PHP_EOL;
echo 'Public Key:', PHP_EOL, $pubkey, PHP_EOL;
```
# Reference
> [1] [Examples - Explicit parameters to define
> curve](https://github.com/openssl/openssl/blob/openssl-3.0.7/test/endecode_test.c#L1048)
> [2] [Common EC
> Parameters](https://github.com/openssl/openssl/blob/openssl-3.0.7/doc/man7/EVP_PKEY-EC.pod#common-ec-parameters)
> [3] [SM2 Key
> Changes](https://github.com/openssl/openssl/blob/openssl-3.0.7/doc/man7/migration_guide.pod#sm2-key-changes)
> [4] [Examples - Custom params not supported with
> SM2](https://github.com/openssl/openssl/blob/openssl-3.0.7/test/ectest.c#L2722)
> [5] [Creating an ECC keypair using raw key
> data](https://github.com/openssl/openssl/blob/openssl-3.0.7/doc/man3/EVP_PKEY_fromdata.pod#creating-an-ecc-keypair-using-raw-key-data)
> [6] [EVP_PKEY_keygen DESCRIPTION
> ](https://github.com/openssl/openssl/blob/openssl-3.0.7/doc/man3/EVP_PKEY_keygen.pod#description)
> [7] [EVP_PKEY_CTX_new On Key
> Types](https://github.com/openssl/openssl/blob/openssl-3.0.7/doc/man3/EVP_PKEY_CTX_new.pod#on-key-types)