Skip to content

Commit

Permalink
Support SM3 third-party implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Jan 4, 2024
1 parent 92818b3 commit b0c5208
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 13 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ set(src
src/sm3.c
src/sm3_hmac.c
src/sm3_kdf.c
src/sm3_digest.c
src/sm2_alg.c
src/sm2_key.c
src/sm2_lib.c
Expand Down
40 changes: 40 additions & 0 deletions include/gmssl/sm3_digest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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
*/


#ifndef GMSSL_SM3_DIGEST_H
#define GMSSL_SM3_DIGEST_H

#include <string.h>
#include <stdint.h>
#include <gmssl/sm3.h>

#ifdef __cplusplus
extern "C" {
#endif


typedef struct {
union {
SM3_CTX sm3_ctx;
SM3_HMAC_CTX hmac_ctx;
void *handle;
};
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
#endif
80 changes: 80 additions & 0 deletions src/sm3_digest.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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_digest.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;
}
30 changes: 23 additions & 7 deletions tools/sm3.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <string.h>
#include <stdlib.h>
#include <gmssl/sm2.h>
#include <gmssl/sm3.h>
#include <gmssl/sm3_digest.h>
#include <gmssl/hex.h>
#include <gmssl/error.h>

Expand Down Expand Up @@ -75,7 +75,7 @@ int sm3_main(int argc, char **argv)
FILE *outfp = stdout;
uint8_t id_bin[64];
size_t id_bin_len;
SM3_CTX sm3_ctx;
SM3_DIGEST_CTX sm3_ctx;
uint8_t dgst[32];
int i;

Expand Down Expand Up @@ -171,7 +171,10 @@ int sm3_main(int argc, char **argv)
goto end;
}

sm3_init(&sm3_ctx);
if (sm3_digest_init(&sm3_ctx, NULL, 0) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}

if (pubkeyfile) {
SM2_KEY sm2_key;
Expand All @@ -191,20 +194,33 @@ int sm3_main(int argc, char **argv)
sm2_compute_z(z, (SM2_POINT *)&sm2_key, id, strlen(id));
}

sm3_update(&sm3_ctx, z, sizeof(z));
if (sm3_digest_update(&sm3_ctx, z, sizeof(z)) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
}

if (in_str) {
sm3_update(&sm3_ctx, (uint8_t *)in_str, strlen(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;
}

} else {
uint8_t buf[4096];
size_t len;
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
sm3_update(&sm3_ctx, buf, len);
if (sm3_digest_update(&sm3_ctx, buf, len) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
}
memset(buf, 0, sizeof(buf));
}
sm3_finish(&sm3_ctx, dgst);
if (sm3_digest_finish(&sm3_ctx, dgst) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
memset(&sm3_ctx, 0, sizeof(sm3_ctx));

if (outformat > 1) {
Expand Down
25 changes: 19 additions & 6 deletions tools/sm3hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <string.h>
#include <gmssl/mem.h>
#include <gmssl/hex.h>
#include <gmssl/sm3.h>
#include <gmssl/sm3_digest.h>


static const char *usage = "-key hex [-in file | -in_str str] [-bin|-hex] [-out file]";
Expand Down Expand Up @@ -63,7 +63,7 @@ int sm3hmac_main(int argc, char **argv)
size_t keylen;
FILE *infp = stdin;
FILE *outfp = stdout;
SM3_HMAC_CTX ctx;
SM3_DIGEST_CTX ctx;
uint8_t mac[SM3_HMAC_SIZE];
size_t i;

Expand Down Expand Up @@ -142,18 +142,31 @@ int sm3hmac_main(int argc, char **argv)
goto end;
}

sm3_hmac_init(&ctx, key, keylen);
if (sm3_digest_init(&ctx, key, keylen) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}

if (in_str) {
sm3_hmac_update(&ctx, (uint8_t *)in_str, strlen(in_str));
if (sm3_digest_update(&ctx, (uint8_t *)in_str, strlen(in_str)) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
} else {
uint8_t buf[4096];
size_t len;
while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
sm3_hmac_update(&ctx, buf, len);
if (sm3_digest_update(&ctx, buf, len) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}
}
memset(buf, 0, sizeof(buf));
}
sm3_hmac_finish(&ctx, mac);
if (sm3_digest_finish(&ctx, mac) != 1) {
fprintf(stderr, "%s: inner error\n", prog);
goto end;
}

if (outformat > 1) {
if (fwrite(mac, 1, sizeof(mac), outfp) != sizeof(mac)) {
Expand Down

0 comments on commit b0c5208

Please sign in to comment.