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

@@ -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)

View File

@@ -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

View File

@@ -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);

View File

@@ -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 <string.h>
#include <gmssl/sm3.h>
#include <gmssl/error.h>
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;
}

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)) {