Skip to content

Commit

Permalink
TKSS-1002: Provide functions for getting SM2 constants
Browse files Browse the repository at this point in the history
  • Loading branch information
johnshajiang committed Dec 20, 2024
1 parent 564f529 commit e8181f2
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 53 deletions.
29 changes: 29 additions & 0 deletions kona-crypto/src/main/jni/include/kona/kona_sm2.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,35 @@

#include <openssl/ec.h>

typedef struct {
const uint8_t* id;
size_t id_len;
} SM2_ID;

const SM2_ID* sm2_id();

typedef struct {
const uint8_t* field;
size_t field_len;

const uint8_t* order;
size_t order_len;

const uint8_t* a;
size_t a_len;

const uint8_t* b;
size_t b_len;

const uint8_t* gen_x;
size_t gen_x_len;

const uint8_t* gen_y;
size_t gen_y_len;
} SM2_CURVE;

const SM2_CURVE* sm2_curve();

const EC_GROUP* sm2_group();

BIGNUM* sm2_pri_key(const uint8_t* pri_key_bytes);
Expand Down
85 changes: 85 additions & 0 deletions kona-crypto/src/main/jni/kona_sm2_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,91 @@
#include "kona/kona_common.h"
#include "kona/kona_sm2.h"

const uint8_t ID[] = {
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38
};

const SM2_ID* sm2_id() {
static const SM2_ID* sm2_id = NULL;

if (sm2_id == NULL) {
SM2_ID* id = OPENSSL_malloc(sizeof(SM2_ID));
id->id = ID;
id->id_len = sizeof(ID);

sm2_id = id;
}

return sm2_id;
}

const uint8_t FIELD[] = {
0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

const uint8_t ORDER[] = {
0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x72, 0x03, 0xDF, 0x6B, 0x21, 0xC6, 0x05, 0x2B,
0x53, 0xBB, 0xF4, 0x09, 0x39, 0xD5, 0x41, 0x23
};

const uint8_t A[] = {
0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC
};

const uint8_t B[] = {
0x28, 0xE9, 0xFA, 0x9E, 0x9D, 0x9F, 0x5E, 0x34,
0x4D, 0x5A, 0x9E, 0x4B, 0xCF, 0x65, 0x09, 0xA7,
0xF3, 0x97, 0x89, 0xF5, 0x15, 0xAB, 0x8F, 0x92,
0xDD, 0xBC, 0xBD, 0x41, 0x4D, 0x94, 0x0E, 0x93
};

const uint8_t GEN_X[] = {
0x32, 0xC4, 0xAE, 0x2C, 0x1F, 0x19, 0x81, 0x19,
0x5F, 0x99, 0x04, 0x46, 0x6A, 0x39, 0xC9, 0x94,
0x8F, 0xE3, 0x0B, 0xBF, 0xF2, 0x66, 0x0B, 0xE1,
0x71, 0x5A, 0x45, 0x89, 0x33, 0x4C, 0x74, 0xC7
};

const uint8_t GEN_Y[] = {
0xBC, 0x37, 0x36, 0xA2, 0xF4, 0xF6, 0x77, 0x9C,
0x59, 0xBD, 0xCE, 0xE3, 0x6B, 0x69, 0x21, 0x53,
0xD0, 0xA9, 0x87, 0x7C, 0xC6, 0x2A, 0x47, 0x40,
0x02, 0xDF, 0x32, 0xE5, 0x21, 0x39, 0xF0, 0xA0
};

const SM2_CURVE* sm2_curve() {
static const SM2_CURVE* sm2_curve = NULL;

if (sm2_curve == NULL) {
SM2_CURVE* curve = OPENSSL_malloc(sizeof(SM2_CURVE));
curve->field = FIELD;
curve->field_len = sizeof(FIELD);
curve->order = ORDER;
curve->order_len = sizeof(ORDER);
curve->a = A;
curve->a_len = sizeof(A);
curve->b = B;
curve->b_len = sizeof(B);
curve->gen_x = GEN_X;
curve->gen_x_len = sizeof(GEN_X);
curve->gen_y = GEN_Y;
curve->gen_y_len = sizeof(GEN_Y);

sm2_curve = curve;
}

return sm2_curve;
}

const EC_GROUP* sm2_group() {
static const EC_GROUP* sm2_group = NULL;

Expand Down
62 changes: 9 additions & 53 deletions kona-crypto/src/main/jni/kona_sm2_keyagreement.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,53 +33,6 @@
#include "kona/kona_sm2.h"
#include "kona/kona_sm3.h"

const uint8_t FIELD[] = {
0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

const uint8_t ORDER[] = {
0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0x72, 0x03, 0xDF, 0x6B, 0x21, 0xC6, 0x05, 0x2B,
0x53, 0xBB, 0xF4, 0x09, 0x39, 0xD5, 0x41, 0x23
};

const uint8_t A[] = {
0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFC
};

const uint8_t B[] = {
0x28, 0xE9, 0xFA, 0x9E, 0x9D, 0x9F, 0x5E, 0x34,
0x4D, 0x5A, 0x9E, 0x4B, 0xCF, 0x65, 0x09, 0xA7,
0xF3, 0x97, 0x89, 0xF5, 0x15, 0xAB, 0x8F, 0x92,
0xDD, 0xBC, 0xBD, 0x41, 0x4D, 0x94, 0x0E, 0x93
};

const uint8_t GEN_X[] = {
0x32, 0xC4, 0xAE, 0x2C, 0x1F, 0x19, 0x81, 0x19,
0x5F, 0x99, 0x04, 0x46, 0x6A, 0x39, 0xC9, 0x94,
0x8F, 0xE3, 0x0B, 0xBF, 0xF2, 0x66, 0x0B, 0xE1,
0x71, 0x5A, 0x45, 0x89, 0x33, 0x4C, 0x74, 0xC7
};

const uint8_t GEN_Y[] = {
0xBC, 0x37, 0x36, 0xA2, 0xF4, 0xF6, 0x77, 0x9C,
0x59, 0xBD, 0xCE, 0xE3, 0x6B, 0x69, 0x21, 0x53,
0xD0, 0xA9, 0x87, 0x7C, 0xC6, 0x2A, 0x47, 0x40,
0x02, 0xDF, 0x32, 0xE5, 0x21, 0x39, 0xF0, 0xA0
};

const uint8_t ID[] = {
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38
};

SM2_KEYEX_CTX* sm2_create_keyex_ctx() {
EVP_MD_CTX* sm3_ctx = sm3_create_ctx();
if (sm3_ctx == NULL) {
Expand Down Expand Up @@ -114,8 +67,11 @@ void sm2_free_keyex_ctx(SM2_KEYEX_CTX* ctx) {
int z(uint8_t* out, SM2_KEYEX_CTX* ctx,
const uint8_t* id, const size_t id_len,
const EC_GROUP* group, const EC_POINT* point) {
const uint8_t* id_to_use = id ? id : ID;
size_t id_bytes_len = id ? id_len : sizeof(ID);
const SM2_ID* default_id = sm2_id();
const SM2_CURVE* curve = sm2_curve();

const uint8_t* id_to_use = id ? id : default_id->id;
size_t id_bytes_len = id ? id_len : default_id->id_len;
int id_bits_len = id_bytes_len << 3;

uint8_t id_len_high = (id_bits_len >> 8) & 0xFF;
Expand All @@ -124,11 +80,11 @@ int z(uint8_t* out, SM2_KEYEX_CTX* ctx,
!EVP_DigestUpdate(ctx->sm3_ctx, &id_len_low, 1) ||
!EVP_DigestUpdate(ctx->sm3_ctx, id_to_use, id_bytes_len) ||

!EVP_DigestUpdate(ctx->sm3_ctx, A, sizeof(A)) ||
!EVP_DigestUpdate(ctx->sm3_ctx, B, sizeof(B)) ||
!EVP_DigestUpdate(ctx->sm3_ctx, curve->a, curve->a_len) ||
!EVP_DigestUpdate(ctx->sm3_ctx, curve->b, curve->b_len) ||

!EVP_DigestUpdate(ctx->sm3_ctx, GEN_X, sizeof(GEN_X)) ||
!EVP_DigestUpdate(ctx->sm3_ctx, GEN_Y, sizeof(GEN_Y))) {
!EVP_DigestUpdate(ctx->sm3_ctx, curve->gen_x, curve->gen_x_len) ||
!EVP_DigestUpdate(ctx->sm3_ctx, curve->gen_y, curve->gen_y_len)) {
return OPENSSL_FAILURE;
}

Expand Down
Binary file modified kona-crypto/src/main/resources/libKonaCrypto-linux-aarch64.so
Binary file not shown.
Binary file modified kona-crypto/src/main/resources/libKonaCrypto-linux-x86_64.so
Binary file not shown.

0 comments on commit e8181f2

Please sign in to comment.