Remove sm3_digest.c

This commit is contained in:
Zhi Guan
2026-06-27 12:13:30 +08:00
parent c924503a05
commit e680c45b69
6 changed files with 18 additions and 134 deletions

View File

@@ -13,6 +13,7 @@
#include <string.h>
#include <stdlib.h>
#include <gmssl/sm2.h>
#include <gmssl/sm3.h>
#include <gmssl/hex.h>
#include <gmssl/error.h>
@@ -74,7 +75,7 @@ int sm3_main(int argc, char **argv)
FILE *outfp = stdout;
uint8_t id_bin[64];
size_t id_bin_len;
SM3_DIGEST_CTX sm3_ctx;
SM3_CTX sm3_ctx;
uint8_t dgst[32];
int i;
@@ -170,10 +171,7 @@ bad:
goto end;
}
if (sm3_digest_init(&sm3_ctx, NULL, 0) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
sm3_init(&sm3_ctx);
if (pubkeyfile) {
SM2_KEY sm2_key;
@@ -193,26 +191,17 @@ bad:
sm2_compute_z(z, &sm2_key.public_key, id, strlen(id));
}
if (sm3_digest_update(&sm3_ctx, z, sizeof(z)) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
sm3_update(&sm3_ctx, z, sizeof(z));
}
if (in_str) {
if (sm3_digest_update(&sm3_ctx, (uint8_t *)in_str, strlen(in_str)) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
sm3_update(&sm3_ctx, (uint8_t *)in_str, strlen(in_str));
} else {
uint8_t buf[4096];
size_t len;
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
if (sm3_digest_update(&sm3_ctx, buf, len) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
sm3_update(&sm3_ctx, buf, len);
}
if (ferror(infp)) {
fprintf(stderr, "%s: read failure\n", prog);
@@ -220,10 +209,7 @@ bad:
}
memset(buf, 0, sizeof(buf));
}
if (sm3_digest_finish(&sm3_ctx, dgst) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
sm3_finish(&sm3_ctx, dgst);
memset(&sm3_ctx, 0, sizeof(sm3_ctx));
if (outformat > 1) {

View File

@@ -64,7 +64,7 @@ int sm3_hmac_main(int argc, char **argv)
size_t keylen;
FILE *infp = stdin;
FILE *outfp = stdout;
SM3_DIGEST_CTX ctx;
SM3_HMAC_CTX ctx;
uint8_t mac[SM3_HMAC_SIZE];
size_t i;
@@ -93,6 +93,10 @@ int sm3_hmac_main(int argc, char **argv)
fprintf(stderr, "%s: invalid HEX digits\n", prog);
goto end;
}
if (keylen < 12) {
fprintf(stderr, "%s: key should be at least 24 digits (12 bytes)\n", prog);
goto end;
}
} else if (!strcmp(*argv, "-hex")) {
if (outformat == 2) {
fprintf(stderr, "%s: `-hex` and `-bin` should not be used together\n", prog);
@@ -143,24 +147,15 @@ bad:
goto end;
}
if (sm3_digest_init(&ctx, key, keylen) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
sm3_hmac_init(&ctx, key, keylen);
if (in_str) {
if (sm3_digest_update(&ctx, (uint8_t *)in_str, strlen(in_str)) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
sm3_hmac_update(&ctx, (uint8_t *)in_str, strlen(in_str));
} else {
uint8_t buf[4096];
size_t len;
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
if (sm3_digest_update(&ctx, buf, len) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
sm3_hmac_update(&ctx, buf, len);
}
if (ferror(infp)) {
fprintf(stderr, "%s: read failure\n", prog);
@@ -168,10 +163,7 @@ bad:
}
memset(buf, 0, sizeof(buf));
}
if (sm3_digest_finish(&ctx, mac) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
sm3_hmac_finish(&ctx, mac);
if (outformat > 1) {
if (fwrite(mac, 1, sizeof(mac), outfp) != sizeof(mac)) {