From 2dab02f76a5f95c37a4da6509de3101b18f13d0f Mon Sep 17 00:00:00 2001 From: Zhi Guan Date: Sat, 6 Jan 2024 20:27:52 +0800 Subject: [PATCH] Move sm2 ctx functions to a standalone source file To support UADK implementations --- CMakeLists.txt | 1 + src/sm2_ctx.c | 137 +++++++++++++++++++++++++++++++++++++++++++++++++ src/sm2_lib.c | 115 ----------------------------------------- 3 files changed, 138 insertions(+), 115 deletions(-) create mode 100644 src/sm2_ctx.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 41c02181..df814629 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,7 @@ set(src src/sm2_alg.c src/sm2_key.c src/sm2_lib.c + src/sm2_ctx.c src/sm9_alg.c src/sm9_key.c src/sm9_lib.c diff --git a/src/sm2_ctx.c b/src/sm2_ctx.c new file mode 100644 index 00000000..7aef5378 --- /dev/null +++ b/src/sm2_ctx.c @@ -0,0 +1,137 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include + + +int sm2_sign_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen) +{ + if (!ctx || !key) { + error_print(); + return -1; + } + ctx->key = *key; + sm3_init(&ctx->sm3_ctx); + + if (id) { + uint8_t z[SM3_DIGEST_SIZE]; + if (idlen <= 0 || idlen > SM2_MAX_ID_LENGTH) { + error_print(); + return -1; + } + sm2_compute_z(z, &key->public_key, id, idlen); + sm3_update(&ctx->sm3_ctx, z, sizeof(z)); + } + return 1; +} + +int sm2_sign_update(SM2_SIGN_CTX *ctx, const uint8_t *data, size_t datalen) +{ + if (!ctx) { + error_print(); + return -1; + } + if (data && datalen > 0) { + sm3_update(&ctx->sm3_ctx, data, datalen); + } + return 1; +} + +int sm2_sign_finish(SM2_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen) +{ + uint8_t dgst[SM3_DIGEST_SIZE]; + + if (!ctx || !sig || !siglen) { + error_print(); + return -1; + } + sm3_finish(&ctx->sm3_ctx, dgst); + if (sm2_sign(&ctx->key, dgst, sig, siglen) != 1) { + error_print(); + return -1; + } + return 1; +} + +int sm2_sign_finish_fixlen(SM2_SIGN_CTX *ctx, size_t siglen, uint8_t *sig) +{ + uint8_t dgst[SM3_DIGEST_SIZE]; + + if (!ctx || !sig || !siglen) { + error_print(); + return -1; + } + sm3_finish(&ctx->sm3_ctx, dgst); + if (sm2_sign_fixlen(&ctx->key, dgst, siglen, sig) != 1) { + error_print(); + return -1; + } + return 1; +} + +int sm2_verify_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen) +{ + if (!ctx || !key) { + error_print(); + return -1; + } + memset(ctx, 0, sizeof(*ctx)); + ctx->key.public_key = key->public_key; + sm3_init(&ctx->sm3_ctx); + + if (id) { + uint8_t z[SM3_DIGEST_SIZE]; + if (idlen <= 0 || idlen > SM2_MAX_ID_LENGTH) { + error_print(); + return -1; + } + sm2_compute_z(z, &key->public_key, id, idlen); + sm3_update(&ctx->sm3_ctx, z, sizeof(z)); + } + return 1; +} + +int sm2_verify_update(SM2_SIGN_CTX *ctx, const uint8_t *data, size_t datalen) +{ + if (!ctx) { + error_print(); + return -1; + } + if (data && datalen > 0) { + sm3_update(&ctx->sm3_ctx, data, datalen); + } + return 1; +} + +int sm2_verify_finish(SM2_SIGN_CTX *ctx, const uint8_t *sig, size_t siglen) +{ + uint8_t dgst[SM3_DIGEST_SIZE]; + + if (!ctx || !sig) { + error_print(); + return -1; + } + sm3_finish(&ctx->sm3_ctx, dgst); + if (sm2_verify(&ctx->key, dgst, sig, siglen) != 1) { + error_print(); + return -1; + } + return 1; +} + diff --git a/src/sm2_lib.c b/src/sm2_lib.c index 10cd8f59..c5e22e16 100644 --- a/src/sm2_lib.c +++ b/src/sm2_lib.c @@ -332,121 +332,6 @@ int sm2_compute_z(uint8_t z[32], const SM2_POINT *pub, const char *id, size_t id return 1; } -int sm2_sign_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen) -{ - if (!ctx || !key) { - error_print(); - return -1; - } - ctx->key = *key; - sm3_init(&ctx->sm3_ctx); - - if (id) { - uint8_t z[SM3_DIGEST_SIZE]; - if (idlen <= 0 || idlen > SM2_MAX_ID_LENGTH) { - error_print(); - return -1; - } - sm2_compute_z(z, &key->public_key, id, idlen); - sm3_update(&ctx->sm3_ctx, z, sizeof(z)); - } - return 1; -} - -int sm2_sign_update(SM2_SIGN_CTX *ctx, const uint8_t *data, size_t datalen) -{ - if (!ctx) { - error_print(); - return -1; - } - if (data && datalen > 0) { - sm3_update(&ctx->sm3_ctx, data, datalen); - } - return 1; -} - -int sm2_sign_finish(SM2_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen) -{ - uint8_t dgst[SM3_DIGEST_SIZE]; - - if (!ctx || !sig || !siglen) { - error_print(); - return -1; - } - sm3_finish(&ctx->sm3_ctx, dgst); - if (sm2_sign(&ctx->key, dgst, sig, siglen) != 1) { - error_print(); - return -1; - } - return 1; -} - -int sm2_sign_finish_fixlen(SM2_SIGN_CTX *ctx, size_t siglen, uint8_t *sig) -{ - uint8_t dgst[SM3_DIGEST_SIZE]; - - if (!ctx || !sig || !siglen) { - error_print(); - return -1; - } - sm3_finish(&ctx->sm3_ctx, dgst); - if (sm2_sign_fixlen(&ctx->key, dgst, siglen, sig) != 1) { - error_print(); - return -1; - } - return 1; -} - -int sm2_verify_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen) -{ - if (!ctx || !key) { - error_print(); - return -1; - } - memset(ctx, 0, sizeof(*ctx)); - ctx->key.public_key = key->public_key; - sm3_init(&ctx->sm3_ctx); - - if (id) { - uint8_t z[SM3_DIGEST_SIZE]; - if (idlen <= 0 || idlen > SM2_MAX_ID_LENGTH) { - error_print(); - return -1; - } - sm2_compute_z(z, &key->public_key, id, idlen); - sm3_update(&ctx->sm3_ctx, z, sizeof(z)); - } - return 1; -} - -int sm2_verify_update(SM2_SIGN_CTX *ctx, const uint8_t *data, size_t datalen) -{ - if (!ctx) { - error_print(); - return -1; - } - if (data && datalen > 0) { - sm3_update(&ctx->sm3_ctx, data, datalen); - } - return 1; -} - -int sm2_verify_finish(SM2_SIGN_CTX *ctx, const uint8_t *sig, size_t siglen) -{ - uint8_t dgst[SM3_DIGEST_SIZE]; - - if (!ctx || !sig) { - error_print(); - return -1; - } - sm3_finish(&ctx->sm3_ctx, dgst); - if (sm2_verify(&ctx->key, dgst, sig, siglen) != 1) { - error_print(); - return -1; - } - return 1; -} - int sm2_kdf(const uint8_t *in, size_t inlen, size_t outlen, uint8_t *out) { SM3_CTX ctx;