Skip to content

Commit

Permalink
Update SM2
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Mar 19, 2024
1 parent 33baa3d commit 31440f9
Show file tree
Hide file tree
Showing 10 changed files with 484 additions and 2,958 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ set(src
src/sm3_hmac.c
src/sm3_kdf.c
src/sm3_digest.c
#src/sm2_alg.c
src/sm2_point.c
src/sm2_z256.c
src/sm2_z256_table.c
src/sm2_z256_key.c
Expand Down
12 changes: 5 additions & 7 deletions include/gmssl/sm2.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <stdlib.h>
#include <gmssl/api.h>
#include <gmssl/sm3.h>
#include <gmssl/sm2_z256.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -163,13 +162,9 @@ typedef struct {
} SM2_SIGNATURE;

int sm2_do_sign(const SM2_KEY *key, const uint8_t dgst[32], SM2_SIGNATURE *sig);
int sm2_do_sign_fast(const uint64_t d[4], const uint8_t dgst[32], SM2_SIGNATURE *sig);
int sm2_do_verify(const SM2_KEY *key, const uint8_t dgst[32], const SM2_SIGNATURE *sig);

int sm2_do_sign_pre_compute(uint64_t k[4], uint64_t x1[4]);

int sm2_do_sign_fast_ex(const uint64_t d[4], const uint64_t k[4], const uint64_t x1[4], const uint8_t dgst[32], SM2_SIGNATURE *sig);
int sm2_do_verify_fast(const SM2_Z256_POINT *P, const uint8_t dgst[32], const SM2_SIGNATURE *sig);


#define SM2_MIN_SIGNATURE_SIZE 8
Expand Down Expand Up @@ -203,20 +198,23 @@ typedef struct {
uint64_t x1[4];
} SM2_SIGN_PRE_COMP;


typedef struct {
SM3_CTX sm3_ctx;
SM2_KEY key;
// FIXME: change `key` to SM2_Z256_POINT and uint64_t[4], inner type, faster sign/verify

SM2_Z256_POINT public_key; // z256 only
uint64_t public_key[3][8]; // enough to hold point in Jacobian format

uint64_t sign_key[8]; // u64[8] to support SM2_BN
SM3_CTX inited_sm3_ctx;

SM2_SIGN_PRE_COMP pre_comp[32];
unsigned int num_pre_comp;

} SM2_SIGN_CTX;



_gmssl_export int sm2_sign_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen);
_gmssl_export int sm2_sign_update(SM2_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
_gmssl_export int sm2_sign_finish(SM2_SIGN_CTX *ctx, uint8_t *sig, size_t *siglen);
Expand Down
8 changes: 8 additions & 0 deletions include/gmssl/sm2_z256.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <gmssl/sm2.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -134,6 +135,13 @@ int sm2_z256_point_from_hash(SM2_Z256_POINT *R, const uint8_t *data, size_t data

int sm2_z256_point_from_octets(SM2_Z256_POINT *P, const uint8_t *in, size_t inlen);

// 这些函数的问题是依赖于sm2.h 这些接口的,最好是不要有这些依赖
// 这些接口和底层的SM2曲线实现是相关的
int sm2_do_sign_fast(const uint64_t d[4], const uint8_t dgst[32], SM2_SIGNATURE *sig);
int sm2_do_sign_pre_compute(uint64_t k[4], uint64_t x1[4]);
int sm2_do_sign_fast_ex(const uint64_t d[4], const uint64_t k[4], const uint64_t x1[4], const uint8_t dgst[32], SM2_SIGNATURE *sig);
int sm2_do_verify_fast(const SM2_Z256_POINT *P, const uint8_t dgst[32], const SM2_SIGNATURE *sig);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 31440f9

Please sign in to comment.