xref: /PHP-8.3/ext/openssl/tests/ecc_sm2.phpt (revision 0dadd661)
1--TEST--
2openssl_*() with OPENSSL_KEYTYPE_EC for SM2
3--EXTENSIONS--
4openssl
5--SKIPIF--
6<?php
7if (!defined("OPENSSL_KEYTYPE_EC")) die("skip EC disabled");
8if (!in_array('SM2', openssl_get_curve_names())) die("skip SM2 disabled");
9?>
10--FILE--
11<?php
12// EC - generate SM2 keypair with curve_name
13echo "Testing openssl_pkey_new with ec curve_name SM2\n";
14$ec = openssl_pkey_new(array(
15    'ec'=> array(
16        'curve_name' => 'SM2',
17    )
18));
19
20var_dump($ec);
21$details = openssl_pkey_get_details($ec);
22var_dump($details["bits"]);
23var_dump(strlen($details["key"]));
24var_dump($details["ec"]["curve_name"]);
25var_dump($details["type"] == OPENSSL_KEYTYPE_EC);
26
27// EC - generate SM2 keypair with curve_name
28echo "Testing openssl_pkey_get_public from SM2 pem pubkey\n";
29$public_key = openssl_pkey_get_public($details["key"]);
30var_dump($public_key);
31$details_public_key = openssl_pkey_get_details($public_key);
32var_dump(strlen($details["key"]));
33var_dump($details_public_key["ec"]["curve_name"]);
34var_dump($details["type"] == OPENSSL_KEYTYPE_EC);
35var_dump($details_public_key["ec"]["x"] === $details["ec"]["x"]);
36var_dump($details_public_key["ec"]["y"] === $details["ec"]["y"]);
37
38// EC - generate keypair with explicit parameters (SM2 curve)
39echo "Testing openssl_pkey_new with ec explicit parameters (SM2 curve)\n";
40$p = hex2bin('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF');
41$a = hex2bin('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC');
42$b = hex2bin('28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93');
43$g_x = hex2bin('32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7');
44$g_y = hex2bin('BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0');
45$order = hex2bin('FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123');
46
47$ec = openssl_pkey_new(array(
48    'ec'=> array(
49        'p' => $p,
50        'a' => $a,
51        'b' => $b,
52        'order' => $order,
53        'g_x' => $g_x,
54        'g_y' => $g_y
55    )
56));
57
58$details = openssl_pkey_get_details($ec);
59var_dump($details['bits']);
60var_dump(strlen($details['key']));
61var_dump($details['type'] == OPENSSL_KEYTYPE_EC);
62$public_key = openssl_pkey_get_public($details["key"]);
63$details_public_key = openssl_pkey_get_details($public_key);
64var_dump(strlen($details["key"]));
65var_dump($details["type"] == OPENSSL_KEYTYPE_EC);
66var_dump($details_public_key["ec"]["x"] === $details["ec"]["x"]);
67var_dump($details_public_key["ec"]["y"] === $details["ec"]["y"]);
68?>
69--EXPECTF--
70Testing openssl_pkey_new with ec curve_name SM2
71object(OpenSSLAsymmetricKey)#%d (0) {
72}
73int(256)
74int(178)
75string(3) "SM2"
76bool(true)
77Testing openssl_pkey_get_public from SM2 pem pubkey
78object(OpenSSLAsymmetricKey)#%d (0) {
79}
80int(178)
81string(3) "SM2"
82bool(true)
83bool(true)
84bool(true)
85Testing openssl_pkey_new with ec explicit parameters (SM2 curve)
86int(256)
87int(475)
88bool(true)
89int(475)
90bool(true)
91bool(true)
92bool(true)
93