Skip to content

Commit

Permalink
Adjust SM2 API and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzhi committed Apr 19, 2024
1 parent 725817a commit ab7c9a7
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 277 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ set(src
src/digest.c
src/hmac.c
src/hkdf.c
# src/pbkdf2.c
src/gf128.c
src/ghash.c
src/sm4_cbc_sm3_hmac.c
Expand Down Expand Up @@ -145,7 +144,6 @@ set(tests
digest
hmac
hkdf
pbkdf2
gf128
ghash
pkcs8
Expand Down
13 changes: 5 additions & 8 deletions include/gmssl/sm2_z256.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*/



#ifndef GMSSL_SM2_Z256_H
#define GMSSL_SM2_Z256_H

Expand Down Expand Up @@ -83,14 +82,14 @@ typedef struct {
} SM2_Z256_POINT;

void sm2_z256_point_set_infinity(SM2_Z256_POINT *P);
int sm2_z256_point_from_bytes(SM2_Z256_POINT *P, const uint8_t in[64]);
void sm2_z256_point_to_bytes(const SM2_Z256_POINT *P, uint8_t out[64]);

int sm2_z256_point_is_at_infinity(const SM2_Z256_POINT *P);
int sm2_z256_point_to_bytes(const SM2_Z256_POINT *P, uint8_t out[64]);
int sm2_z256_point_from_bytes(SM2_Z256_POINT *P, const uint8_t in[64]);
int sm2_z256_point_from_hex(SM2_Z256_POINT *P, const char *hex);
int sm2_z256_point_equ_hex(const SM2_Z256_POINT *P, const char *hex);
int sm2_z256_point_is_on_curve(const SM2_Z256_POINT *P);
int sm2_z256_point_equ(const SM2_Z256_POINT *P, const SM2_Z256_POINT *Q); // equivalent jacobian points
void sm2_z256_point_get_xy(const SM2_Z256_POINT *P, uint64_t x[4], uint64_t y[4]);

int sm2_z256_point_get_xy(const SM2_Z256_POINT *P, uint64_t x[4], uint64_t y[4]);

void sm2_z256_point_dbl(SM2_Z256_POINT *R, const SM2_Z256_POINT *A);
void sm2_z256_point_add(SM2_Z256_POINT *r, const SM2_Z256_POINT *a, const SM2_Z256_POINT *b);
Expand Down Expand Up @@ -119,8 +118,6 @@ const uint64_t *sm2_z256_order(void);
const uint64_t *sm2_z256_order_minus_one(void);
const uint64_t *sm2_z256_one(void);

void sm2_z256_point_from_hex(SM2_Z256_POINT *P, const char *hex);
int sm2_z256_point_equ_hex(const SM2_Z256_POINT *P, const char *hex);

enum {
SM2_point_at_infinity = 0x00,
Expand Down
190 changes: 0 additions & 190 deletions src/pbkdf2.c

This file was deleted.

32 changes: 29 additions & 3 deletions src/sm2_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/mem.h>
#include <gmssl/sm2.h>
#include <gmssl/sm2_z256.h>
#include <gmssl/sm3.h>
#include <gmssl/mem.h>
#include <gmssl/asn1.h>
#include <gmssl/error.h>
#include <gmssl/endian.h>
Expand All @@ -31,6 +30,34 @@ static int all_zero(const uint8_t *buf, size_t len)
return 1;
}

int sm2_kdf(const uint8_t *in, size_t inlen, size_t outlen, uint8_t *out)
{
SM3_CTX ctx;
uint8_t counter_be[4];
uint8_t dgst[SM3_DIGEST_SIZE];
uint32_t counter = 1;
size_t len;

while (outlen) {
PUTU32(counter_be, counter);
counter++;

sm3_init(&ctx);
sm3_update(&ctx, in, inlen);
sm3_update(&ctx, counter_be, sizeof(counter_be));
sm3_finish(&ctx, dgst);

len = outlen < SM3_DIGEST_SIZE ? outlen : SM3_DIGEST_SIZE;
memcpy(out, dgst, len);
out += len;
outlen -= len;
}

memset(&ctx, 0, sizeof(SM3_CTX));
memset(dgst, 0, sizeof(dgst));
return 1;
}

int sm2_do_encrypt_pre_compute(sm2_z256_t k, uint8_t C1[64])
{
SM2_Z256_POINT P;
Expand All @@ -50,7 +77,6 @@ int sm2_do_encrypt_pre_compute(sm2_z256_t k, uint8_t C1[64])
return 1;
}


// key->public_key will not be point_at_infinity when decoded from_bytes/octets/der
int sm2_do_encrypt(const SM2_KEY *key, const uint8_t *in, size_t inlen, SM2_CIPHERTEXT *out)
{
Expand Down
30 changes: 0 additions & 30 deletions src/sm2_sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include <stdlib.h>
#include <gmssl/mem.h>
#include <gmssl/sm2.h>
#include <gmssl/sm2_z256.h>
#include <gmssl/sm3.h>
#include <gmssl/asn1.h>
#include <gmssl/error.h>
Expand Down Expand Up @@ -391,35 +390,6 @@ int sm2_compute_z(uint8_t z[32], const SM2_Z256_POINT *pub, const char *id, size
return 1;
}

int sm2_kdf(const uint8_t *in, size_t inlen, size_t outlen, uint8_t *out)
{
SM3_CTX ctx;
uint8_t counter_be[4];
uint8_t dgst[SM3_DIGEST_SIZE];
uint32_t counter = 1;
size_t len;

while (outlen) {
PUTU32(counter_be, counter);
counter++;

sm3_init(&ctx);
sm3_update(&ctx, in, inlen);
sm3_update(&ctx, counter_be, sizeof(counter_be));
sm3_finish(&ctx, dgst);

len = outlen < SM3_DIGEST_SIZE ? outlen : SM3_DIGEST_SIZE;
memcpy(out, dgst, len);
out += len;
outlen -= len;
}

memset(&ctx, 0, sizeof(SM3_CTX));
memset(dgst, 0, sizeof(dgst));
return 1;
}


int sm2_sign_init(SM2_SIGN_CTX *ctx, const SM2_KEY *key, const char *id, size_t idlen)
{
size_t i;
Expand Down
Loading

0 comments on commit ab7c9a7

Please sign in to comment.