@@ -69,6 +69,7 @@ NON_EMPTY_TRANSLATION_UNIT
6969# include <openssl/pem.h>
7070# include <openssl/sm2.h>
7171# include <openssl/objects.h>
72+ # include "../crypto/ec/ec_lcl.h"
7273# include "apps.h"
7374
7475static OPT_PAIR conv_forms [] = {
@@ -129,7 +130,7 @@ int sm2_main(int argc, char **argv)
129130 ENGINE * e = NULL ;
130131 EC_KEY * eckey = NULL ;
131132 const EC_GROUP * group ;
132- const EVP_CIPHER * enc = NULL ;
133+ const EVP_CIPHER * enc = EVP_sms4_cbc () ;
133134 point_conversion_form_t form = POINT_CONVERSION_UNCOMPRESSED ;
134135 char * infile = NULL , * outfile = NULL , * prog ;
135136 char * passin = NULL , * passout = NULL , * passinarg = NULL , * passoutarg = NULL ;
@@ -292,6 +293,10 @@ int sm2_main(int argc, char **argv)
292293 unsigned char buf [256 ] = {0 };
293294 unsigned char * key = NULL ;
294295 long keylen ;
296+ if (!(eckey = EC_KEY_new_by_curve_name (NID_sm2p256v1 ))) {
297+ ERR_print_errors (bio_err );
298+ goto end ;
299+ }
295300 if (BIO_read (in , buf , sizeof (buf ) - 1 ) <= 0 ) {
296301 ERR_print_errors (bio_err );
297302 OPENSSL_cleanse (buf , sizeof (buf ));
@@ -304,16 +309,25 @@ int sm2_main(int argc, char **argv)
304309 }
305310 OPENSSL_cleanse (buf , sizeof (buf ));
306311 if (keylen != 32 ) {
307- BIO_printf (bio_err , "invalid private key in hex format\n" );
312+ BIO_printf (bio_err , "Invalid private key in hex format\n" );
313+ BIO_printf (bio_err , "Key length is %ld, not 32 byte\n" , keylen );
308314 OPENSSL_cleanse (key , keylen );
309315 goto end ;
310316 }
311- if (!EC_KEY_oct2key (eckey , key , sizeof ( key ), NULL )) {
317+ if (!EC_KEY_oct2priv (eckey , key , keylen )) {
312318 ERR_print_errors (bio_err );
313319 OPENSSL_cleanse (key , keylen );
314320 goto end ;
315321 }
316322 OPENSSL_cleanse (key , keylen );
323+
324+ if (eckey -> group -> meth -> keygenpub == NULL
325+ || eckey -> group -> meth -> keygenpub (eckey ) == 0 ) {
326+ BIO_printf (bio_err , "Generate public key from private key failed\n" );
327+ ERR_print_errors (bio_err );
328+ goto end ;
329+ }
330+
317331 } else if (informat == FORMAT_ENGINE ) {
318332 EVP_PKEY * pkey ;
319333 if (pubin )
@@ -378,6 +392,24 @@ int sm2_main(int argc, char **argv)
378392 }
379393 }
380394
395+ if (genzid ) {
396+ unsigned char z [64 ];
397+ size_t zlen = sizeof (z );
398+ if (!id ) {
399+ id = SM2_DEFAULT_ID ;
400+ BIO_printf (bio_err , "use default identity '%s'\n" , id );
401+ }
402+ if (!SM2_compute_id_digest (EVP_sm3 (), id , strlen (id ), z , & zlen , eckey )) {
403+ goto end ;
404+ }
405+ BIO_printf (out , "id: %s\n" , id );
406+ BIO_puts (out , "Z:\n" );
407+ ASN1_buf_print (out , z , zlen , 4 );
408+
409+
410+ BIO_printf (out , "\n" );
411+ }
412+
381413 if (noout ) {
382414 ret = 0 ;
383415 goto end ;
@@ -400,26 +432,12 @@ int sm2_main(int argc, char **argv)
400432 i = PEM_write_bio_EC_PUBKEY (out , eckey );
401433 else {
402434 assert (private );
435+ //FIXME: use PKCS#8
403436 i = PEM_write_bio_ECPrivateKey (out , eckey , enc ,
404437 NULL , 0 , NULL , passout );
438+
405439 }
406440 }
407- if (genzid ) {
408- unsigned char z [64 ];
409- size_t zlen = sizeof (z );
410- if (!id ) {
411- id = SM2_DEFAULT_ID ;
412- BIO_printf (bio_err , "use default identity '%s'\n" , id );
413- }
414- if (!SM2_compute_id_digest (EVP_sm3 (), id , strlen (id ), z , & zlen , eckey )) {
415- goto end ;
416- }
417- BIO_printf (out , "Z = " );
418- for (i = 0 ; i < zlen ; i ++ ) {
419- BIO_printf (out , "%02X" , z [i ]);
420- }
421- BIO_printf (out , "\n" );
422- }
423441
424442 if (!i ) {
425443 BIO_printf (bio_err , "unable to write private key\n" );
0 commit comments