Skip to content

Commit

Permalink
Add sdfdigest command
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Jun 8, 2024
1 parent 3614daf commit 368f2e5
Show file tree
Hide file tree
Showing 6 changed files with 380 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ if (ENABLE_SDF)
src/sdf/sdf_meth.c
src/sdf/sdf_ext.c
src/sdf/sdf_sansec.c)
list(APPEND tools tools/sdfinfo.c tools/sdfexport.c tools/sdfsign.c tools/sdftest.c)
list(APPEND tools tools/sdfinfo.c tools/sdfdigest.c tools/sdfexport.c tools/sdfsign.c tools/sdftest.c)
endif()


Expand Down
9 changes: 9 additions & 0 deletions include/gmssl/sdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ typedef struct {
char serial[17];
} SDF_DEVICE;

typedef struct {
void *session;
} SDF_DIGEST_CTX;

typedef struct {
SM2_Z256_POINT public_key;
void *session;
Expand All @@ -43,6 +47,11 @@ typedef struct {
int sdf_load_library(const char *so_path, const char *vendor);
int sdf_open_device(SDF_DEVICE *dev);
int sdf_print_device_info(FILE *fp, int fmt, int ind, const char *lable, SDF_DEVICE *dev);
int sdf_digest_init(SDF_DIGEST_CTX *ctx, SDF_DEVICE *dev);
int sdf_digest_update(SDF_DIGEST_CTX *ctx, const uint8_t *data, size_t datalen);
int sdf_digest_finish(SDF_DIGEST_CTX *ctx, uint8_t dgst[SM3_DIGEST_SIZE]);
int sdf_digest_reset(SDF_DIGEST_CTX *ctx);
void sdf_digest_cleanup(SDF_DIGEST_CTX *ctx);
int sdf_export_sign_public_key(SDF_DEVICE *dev, int key_index, SM2_KEY *public_key);
int sdf_load_sign_key(SDF_DEVICE *dev, SDF_SIGN_KEY *key, int key_index, const char *pass);
int sdf_sign(SDF_SIGN_KEY *key, const uint8_t dgst[32], uint8_t *sig, size_t *siglen);
Expand Down
99 changes: 99 additions & 0 deletions src/sdf/sdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,105 @@ int sdf_print_device_info(FILE *fp, int fmt, int ind, const char *lable, SDF_DEV
return 1;
}

int sdf_digest_init(SDF_DIGEST_CTX *ctx, SDF_DEVICE *dev)
{
void *hSession;
int ret;

if (!dev || !ctx) {
error_print();
return -1;
}
if (!dev->handle) {
error_print();
return -1;
}
if ((ret = SDF_OpenSession(dev->handle, &hSession)) != SDR_OK) {
error_print();
return -1;
}
if ((ret = SDF_HashInit(hSession, SGD_SM3, NULL, NULL, 0)) != SDR_OK) {
(void)SDF_CloseSession(hSession);
error_print();
return -1;
}
ctx->session = hSession;
return 1;
}

int sdf_digest_update(SDF_DIGEST_CTX *ctx, const uint8_t *data, size_t datalen)
{
int ret;

if (!ctx) {
error_print();
return -1;
}
if (!ctx->session) {
error_print();
return -1;
}
if ((ret = SDF_HashUpdate(ctx->session, (uint8_t *)data, (unsigned int)datalen)) != SDR_OK) {
error_print();
return -1;
}
return 1;
}

int sdf_digest_finish(SDF_DIGEST_CTX *ctx, uint8_t dgst[SM3_DIGEST_SIZE])
{
unsigned int dgstlen;
int ret;

if (!ctx || !dgst) {
error_print();
return -1;
}
if (!ctx->session) {
error_print();
return -1;
}
if ((ret = SDF_HashFinal(ctx->session, dgst, &dgstlen)) != SDR_OK) {
error_print();
return -1;
}
if (dgstlen != 32) {
error_print();
return -1;
}
return 1;
}

int sdf_digest_reset(SDF_DIGEST_CTX *ctx)
{
int ret;

if (!ctx) {
error_print();
return -1;
}
if (!ctx->session) {
error_print();
return -1;
}
if ((ret = SDF_HashInit(ctx->session, SGD_SM3, NULL, NULL, 0)) != SDR_OK) {
error_print();
return -1;
}
return 1;
}

void sdf_digest_cleanup(SDF_DIGEST_CTX *ctx)
{
if (ctx && ctx->session) {
int ret;
if ((ret = SDF_CloseSession(ctx->session)) != SDR_OK) {
error_print();
}
ctx->session = NULL;
}
}

int sdf_export_sign_public_key(SDF_DEVICE *dev, int key_index, SM2_KEY *sm2_key)
{
void *hSession;
Expand Down
4 changes: 4 additions & 0 deletions tools/gmssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ extern int tls13_client_main(int argc, char **argv);
extern int tls13_server_main(int argc, char **argv);
#ifdef ENABLE_SDF
extern int sdfinfo_main(int argc, char **argv);
extern int sdfdigest_main(int argc, char **argv);
extern int sdfexport_main(int argc, char **argv);
extern int sdfsign_main(int argc, char **argv);
extern int sdftest_main(int argc, char **argv);
Expand Down Expand Up @@ -129,6 +130,7 @@ static const char *options =
" cmsverify Verify CMS SignedData\n"
#ifdef ENABLE_SDF
" sdfinfo Print SDF device info\n"
" sdfdigest Generate SM3 hash with SDF device\n"
" sdfexport Export SM2 signing public key from SDF device\n"
" sdfsign Generate SM2 signature with SDF internal private key\n"
" sdftest Test vendor's SDF library and device\n"
Expand Down Expand Up @@ -283,6 +285,8 @@ int main(int argc, char **argv)
#ifdef ENABLE_SDF
} else if (!strcmp(*argv, "sdfinfo")) {
return sdfinfo_main(argc, argv);
} else if (!strcmp(*argv, "sdfdigest")) {
return sdfdigest_main(argc, argv);
} else if (!strcmp(*argv, "sdfexport")) {
return sdfexport_main(argc, argv);
} else if (!strcmp(*argv, "sdfsign")) {
Expand Down
Loading

0 comments on commit 368f2e5

Please sign in to comment.