Add sdfencrypt command

This commit is contained in:
Zhi Guan
2024-06-09 11:17:54 +08:00
parent 368f2e5bdc
commit 9784bbc380
6 changed files with 763 additions and 10 deletions

View File

@@ -16,19 +16,22 @@
#include <gmssl/sdf.h>
static const char *usage = "-lib so_path -key num [-out file]";
static const char *usage = "-lib so_path {-sign|-encrypt} -key num [-out file]";
static const char *options =
"\n"
"Options\n"
"\n"
" -lib so_path Vendor's SDF dynamic library\n"
" -sign Export signing public key\n"
" -encrypt Export encryption public key\n"
" -key num Private key index number\n"
" -out file | stdout Output public key in PEM format\n"
"\n"
"Examples\n"
"\n"
" $ gmssl sdfexport -key 1 -out sm2signpub.pem\n"
" $ gmssl sdfexport -sign -key 1 -out sm2signpub.pem\n"
" $ gmssl sdfexport -encrypt -key 1 -out sm2signpub.pem\n"
"\n";
@@ -62,6 +65,18 @@ int sdfexport_main(int argc, char **argv)
} else if (!strcmp(*argv, "-lib")) {
if (--argc < 1) goto bad;
lib = *(++argv);
} else if (!strcmp(*argv, "-sign")) {
if (enc_public_key) {
fprintf(stderr, "gmssl %s: '-sign' and '-encrypt' should not used together\n", prog);
goto end;
}
sign_public_key = 1;
} else if (!strcmp(*argv, "-encrypt")) {
if (sign_public_key) {
fprintf(stderr, "gmssl %s: '-sign' and '-encrypt' should not used together\n", prog);
goto end;
}
enc_public_key = 1;
} else if (!strcmp(*argv, "-key")) {
if (--argc < 1) goto bad;
index = atoi(*(++argv));
@@ -92,6 +107,10 @@ bad:
fprintf(stderr, "gmssl %s: option '-lib' required\n", prog);
goto end;
}
if (!sign_public_key && !enc_public_key) {
fprintf(stderr, "gmssl %s: '-sign' or '-encrypt' option required\n", prog);
goto end;
}
if (index < 0) {
fprintf(stderr, "gmssl %s: '-index' option required\n", prog);
goto end;
@@ -106,9 +125,16 @@ bad:
goto end;
}
if (sdf_export_sign_public_key(&dev, index, &sm2_key) != 1) {
fprintf(stderr, "%s: load sign key failed\n", prog);
goto end;
if (sign_public_key) {
if (sdf_export_sign_public_key(&dev, index, &sm2_key) != 1) {
fprintf(stderr, "%s: load sign key failed\n", prog);
goto end;
}
} else {
if (sdf_export_encrypt_public_key(&dev, index, &sm2_key) != 1) {
fprintf(stderr, "%s: load sign key failed\n", prog);
goto end;
}
}
if (sm2_public_key_info_to_pem(&sm2_key, outfp) != 1) {
fprintf(stderr, "gmssl %s: output public key to PEM failed\n", prog);