mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-09-23 05:44:07 +08:00
Add SM4 commands in gmssl CLI
This commit is contained in:
@@ -18,7 +18,7 @@
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static const char *usage = "-key hex -iv hex [-in file] [-out file]";
|
||||
static const char *usage = "[-encrypt|-decrypt] -key hex -iv hex [-in file] [-out file]";
|
||||
|
||||
static const char *options =
|
||||
"Options\n"
|
||||
@@ -43,25 +43,22 @@ int sm4_ctr_main(int argc, char **argv)
|
||||
char *ivhex = NULL;
|
||||
char *infile = NULL;
|
||||
char *outfile = NULL;
|
||||
|
||||
uint8_t key[16];
|
||||
size_t keylen;
|
||||
uint8_t iv[16];
|
||||
size_t ivlen;
|
||||
FILE *infp = stdin;
|
||||
FILE *outfp = stdout;
|
||||
|
||||
SM4_CTR_CTX ctx;
|
||||
uint8_t inbuf[4096];
|
||||
uint8_t buf[4096];
|
||||
size_t inlen;
|
||||
uint8_t outbuf[4096]; // should sizeof(outbuf) > sizeof(inbuf) ?
|
||||
size_t outlen;
|
||||
|
||||
argc--;
|
||||
argv++;
|
||||
|
||||
if (argc < 1) {
|
||||
fprintf(stderr, "usage: %s %s\n", prog, usage);
|
||||
fprintf(stderr, "usage: gmssl %s %s\n", prog, usage);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -71,51 +68,51 @@ int sm4_ctr_main(int argc, char **argv)
|
||||
printf("%s\n", options);
|
||||
ret = 0;
|
||||
goto end;
|
||||
} else if (!strcmp(*argv, "-encrypt")) {
|
||||
// ignore this option
|
||||
} else if (!strcmp(*argv, "-decrypt")) {
|
||||
// ignore this option
|
||||
} else if (!strcmp(*argv, "-key")) {
|
||||
if (--argc < 1) goto bad;
|
||||
keyhex = *(++argv);
|
||||
if (strlen(keyhex) != sizeof(key) * 2) {
|
||||
fprintf(stderr, "%s: invalid key length\n", prog);
|
||||
fprintf(stderr, "gmssl %s: invalid key length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(keyhex, strlen(keyhex), key, &keylen) != 1) {
|
||||
fprintf(stderr, "%s: invalid key hex digits\n", prog);
|
||||
fprintf(stderr, "gmssl %s: invalid key hex digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-iv")) {
|
||||
if (--argc < 1) goto bad;
|
||||
ivhex = *(++argv);
|
||||
if (strlen(ivhex) != sizeof(iv) * 2) {
|
||||
fprintf(stderr, "%s: invalid IV length\n", prog);
|
||||
fprintf(stderr, "gmssl %s: invalid IV length\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (hex_to_bytes(ivhex, strlen(ivhex), iv, &ivlen) != 1) {
|
||||
fprintf(stderr, "%s: invalid IV hex digits\n", prog);
|
||||
fprintf(stderr, "gmssl %s: invalid IV hex digits\n", prog);
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-encrypt")) {
|
||||
// ignore this option
|
||||
} else if (!strcmp(*argv, "-decrypt")) {
|
||||
// ignore this option
|
||||
} else if (!strcmp(*argv, "-in")) {
|
||||
if (--argc < 1) goto bad;
|
||||
infile = *(++argv);
|
||||
if (!(infp = fopen(infile, "rb"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
fprintf(stderr, "gmssl %s: open '%s' failure : %s\n", prog, infile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else if (!strcmp(*argv, "-out")) {
|
||||
if (--argc < 1) goto bad;
|
||||
outfile = *(++argv);
|
||||
if (!(outfp = fopen(outfile, "wb"))) {
|
||||
fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
fprintf(stderr, "gmssl %s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "%s: illegal option `%s`\n", prog, *argv);
|
||||
fprintf(stderr, "gmssl %s: illegal option `%s`\n", prog, *argv);
|
||||
goto end;
|
||||
bad:
|
||||
fprintf(stderr, "%s: `%s` option value missing\n", prog, *argv);
|
||||
fprintf(stderr, "gmssl %s: `%s` option value missing\n", prog, *argv);
|
||||
goto end;
|
||||
}
|
||||
|
||||
@@ -124,11 +121,11 @@ bad:
|
||||
}
|
||||
|
||||
if (!keyhex) {
|
||||
fprintf(stderr, "%s: option `-key` missing\n", prog);
|
||||
fprintf(stderr, "gmssl %s: option `-key` missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
if (!ivhex) {
|
||||
fprintf(stderr, "%s: option `-iv` missing\n", prog);
|
||||
fprintf(stderr, "gmssl %s: option `-iv` missing\n", prog);
|
||||
goto end;
|
||||
}
|
||||
|
||||
@@ -137,23 +134,23 @@ bad:
|
||||
goto end;
|
||||
}
|
||||
|
||||
while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
|
||||
if (sm4_ctr_encrypt_update(&ctx, inbuf, inlen, outbuf, &outlen) != 1) {
|
||||
while ((inlen = fread(buf, 1, sizeof(buf), infp)) > 0) {
|
||||
if (sm4_ctr_encrypt_update(&ctx, buf, inlen, buf, &outlen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
if (fwrite(buf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "gmssl %s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
|
||||
if (sm4_ctr_encrypt_finish(&ctx, outbuf, &outlen) != 1) {
|
||||
if (sm4_ctr_encrypt_finish(&ctx, buf, &outlen) != 1) {
|
||||
error_print();
|
||||
goto end;
|
||||
}
|
||||
if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
|
||||
if (fwrite(buf, 1, outlen, outfp) != outlen) {
|
||||
fprintf(stderr, "gmssl %s: output failure : %s\n", prog, strerror(errno));
|
||||
goto end;
|
||||
}
|
||||
|
||||
@@ -162,10 +159,9 @@ bad:
|
||||
end:
|
||||
gmssl_secure_clear(key, sizeof(key));
|
||||
gmssl_secure_clear(iv, sizeof(iv));
|
||||
gmssl_secure_clear(&ctx, sizeof(ctx));
|
||||
gmssl_secure_clear(buf, sizeof(buf));
|
||||
if (infile && infp) fclose(infp);
|
||||
if (outfile && outfp) fclose(outfp);
|
||||
gmssl_secure_clear(&ctx, sizeof(ctx));
|
||||
gmssl_secure_clear(inbuf, sizeof(inbuf));
|
||||
gmssl_secure_clear(outbuf, sizeof(outbuf));
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user