From e680c45b69cae3919ee61461494585611bf0fbdf Mon Sep 17 00:00:00 2001 From: Zhi Guan Date: Sat, 27 Jun 2026 12:13:30 +0800 Subject: [PATCH] Remove sm3_digest.c --- CMakeLists.txt | 3 +- include/gmssl/sm3.h | 13 ------- include/gmssl/version.h | 2 +- src/sm3_digest.c | 80 ----------------------------------------- tools/sm3.c | 28 ++++----------- tools/sm3_hmac.c | 26 +++++--------- 6 files changed, 18 insertions(+), 134 deletions(-) delete mode 100644 src/sm3_digest.c diff --git a/CMakeLists.txt b/CMakeLists.txt index b8d690bf..05abcab8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -184,7 +184,6 @@ set(src src/sm3_hmac.c src/sm3_kdf.c src/sm3_pbkdf2.c - src/sm3_digest.c src/sm2_z256.c src/sm2_z256_table.c src/sm2_key.c @@ -1020,7 +1019,7 @@ endif() # set(CPACK_PACKAGE_NAME "GmSSL") set(CPACK_PACKAGE_VENDOR "GmSSL develop team") -set(CPACK_PACKAGE_VERSION "3.3.0-dev.1181") +set(CPACK_PACKAGE_VERSION "3.3.0-dev.1182") set(CPACK_PACKAGE_DESCRIPTION_FILE ${PROJECT_SOURCE_DIR}/README.md) set(CPACK_NSIS_MODIFY_PATH ON) include(CPack) diff --git a/include/gmssl/sm3.h b/include/gmssl/sm3.h index 35ae3ffd..00a7a578 100644 --- a/include/gmssl/sm3.h +++ b/include/gmssl/sm3.h @@ -70,19 +70,6 @@ int sm3_pbkdf2(const char *pass, size_t passlen, size_t outlen, uint8_t *out); -typedef struct { - union { - SM3_CTX sm3_ctx; - SM3_HMAC_CTX hmac_ctx; - }; - int state; -} SM3_DIGEST_CTX; - -int sm3_digest_init(SM3_DIGEST_CTX *ctx, const uint8_t *key, size_t keylen); -int sm3_digest_update(SM3_DIGEST_CTX *ctx, const uint8_t *data, size_t datalen); -int sm3_digest_finish(SM3_DIGEST_CTX *ctx, uint8_t dgst[SM3_DIGEST_SIZE]); - - #ifdef __cplusplus } #endif diff --git a/include/gmssl/version.h b/include/gmssl/version.h index f29f5d02..fdf8ee61 100644 --- a/include/gmssl/version.h +++ b/include/gmssl/version.h @@ -18,7 +18,7 @@ extern "C" { #define GMSSL_VERSION_NUM 30300 -#define GMSSL_VERSION_STR "GmSSL 3.3.0-dev.1181" +#define GMSSL_VERSION_STR "GmSSL 3.3.0-dev.1182" int gmssl_version_num(void); const char *gmssl_version_str(void); diff --git a/src/sm3_digest.c b/src/sm3_digest.c deleted file mode 100644 index 828600d0..00000000 --- a/src/sm3_digest.c +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2014-2024 The GmSSL Project. All Rights Reserved. - * - * Licensed under the Apache License, Version 2.0 (the License); you may - * not use this file except in compliance with the License. - * - * http://www.apache.org/licenses/LICENSE-2.0 - */ - - -#include -#include -#include - - -int sm3_digest_init(SM3_DIGEST_CTX *ctx, const uint8_t *key, size_t keylen) -{ - if (!ctx) { - error_print(); - return -1; - } - - memset(ctx, 0, sizeof(*ctx)); - - if (!key) { - sm3_init(&ctx->sm3_ctx); - ctx->state = 1; - } else { - if (keylen < 12 || keylen > 64) { - error_print(); - return -1; - } - sm3_hmac_init(&ctx->hmac_ctx, key, keylen); - ctx->state = 2; - } - - return 1; -} - -int sm3_digest_update(SM3_DIGEST_CTX *ctx, const uint8_t *data, size_t datalen) -{ - if (!ctx) { - error_print(); - return -1; - } - if (!data || !datalen) { - error_print(); - return -1; - } - - if (ctx->state == 1) { - sm3_update(&ctx->sm3_ctx, data, datalen); - } else if (ctx->state == 2) { - sm3_hmac_update(&ctx->hmac_ctx, data, datalen); - } else { - error_print(); - return -1; - } - return 1; -} - -int sm3_digest_finish(SM3_DIGEST_CTX *ctx, uint8_t dgst[SM3_DIGEST_SIZE]) -{ - if (!ctx || !dgst) { - error_print(); - return -1; - } - - if (ctx->state == 1) { - sm3_finish(&ctx->sm3_ctx, dgst); - } else if (ctx->state == 2) { - sm3_hmac_finish(&ctx->hmac_ctx, dgst); - } else { - error_print(); - return -1; - } - - memset(ctx, 0, sizeof(*ctx)); - return 1; -} diff --git a/tools/sm3.c b/tools/sm3.c index 72fbffc3..2ac47be6 100644 --- a/tools/sm3.c +++ b/tools/sm3.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -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) { diff --git a/tools/sm3_hmac.c b/tools/sm3_hmac.c index 49152152..f966ee5f 100644 --- a/tools/sm3_hmac.c +++ b/tools/sm3_hmac.c @@ -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)) {