Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update zuc.c #1686

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 0 additions & 67 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -183,44 +183,6 @@ set(tools
tools/tls13_server.c
)

set(tests
sm4
sm4_cbc
sm4_ctr
sm4_gcm
sm3
sm4_sm3_hmac
sm2_z256
sm2_key
sm2_sign
sm2_enc
sm9
zuc
hash_drbg
block_cipher
digest
hmac
hkdf
gf128
ghash
pkcs8
ec
asn1
hex
base64
pem
x509
x509_oid
x509_alg
x509_str
x509_ext
x509_req
x509_crl
cms
tls
tls13
)




Expand Down Expand Up @@ -361,31 +323,27 @@ if (ENABLE_SM4_CL)
add_definitions(-DMACOS) # to include <OpenCL/OpenCL.h>
endif()
list(APPEND src src/sm4_cl.c)
list(APPEND tests sm4_cl)
endif()

if (ENABLE_SM4_ECB)
message(STATUS "ENABLE_SM4_ECB is ON")
add_definitions(-DENABLE_SM4_ECB)
list(APPEND src src/sm4_ecb.c)
list(APPEND tools tools/sm4_ecb.c)
list(APPEND tests sm4_ecb)
endif()

if (ENABLE_SM4_OFB)
message(STATUS "ENABLE_SM4_OFB is ON")
add_definitions(-DENABLE_SM4_OFB)
list(APPEND src src/sm4_ofb.c)
list(APPEND tools tools/sm4_ofb.c)
list(APPEND tests sm4_ofb)
endif()

if (ENABLE_SM4_CFB)
message(STATUS "ENABLE_SM4_CFB is ON")
add_definitions(-DENABLE_SM4_CFB)
list(APPEND src src/sm4_cfb.c)
list(APPEND tools tools/sm4_cfb.c)
list(APPEND tests sm4_cfb)
endif()

if (ENABLE_SM4_CCM)
Expand All @@ -394,15 +352,13 @@ if (ENABLE_SM4_CCM)
add_definitions(-DENABLE_SM4_CCM)
list(APPEND src src/sm4_ccm.c)
list(APPEND tools tools/sm4_ccm.c)
list(APPEND tests sm4_ccm)
endif()

if (ENABLE_SM4_XTS)
message(STATUS "ENABLE_SM4_XTS is ON")
add_definitions(-DENABLE_SM4_XTS)
list(APPEND src src/sm4_xts.c)
list(APPEND tools tools/sm4_xts.c)
list(APPEND tests sm4_xts)
endif()


Expand All @@ -415,7 +371,6 @@ if (ENABLE_SM2_EXTS)
src/sm2_ring.c
src/sm2_elgamal.c
src/sm2_commit.c)
list(APPEND tests sm2_key_share sm2_blind sm2_ring sm2_elgamal sm2_commit)
endif()


Expand All @@ -427,7 +382,6 @@ if (ENABLE_SM3_XMSS)
if (ENABLE_SM3_XMSS_CROSSCHECK)
message(STATUS "ENABLE_SM3_XMSS_CROSSCHECK is ON")
add_definitions(-DENABLE_SM3_XMSS_CROSSCHECK)
list(APPEND tests sm3_xmss)
endif()
endif()

Expand All @@ -436,29 +390,25 @@ if (ENABLE_SHA1)
message(STATUS "ENABLE_SHA1 is ON")
add_definitions(-DENABLE_SHA1)
list(APPEND src src/sha1.c)
list(APPEND tests sha1)
endif()


if (ENABLE_SHA2)
message(STATUS "ENABLE_SHA2 is ON")
add_definitions(-DENABLE_SHA2)
list(APPEND src src/sha256.c src/sha512.c)
list(APPEND tests sha224 sha256 sha384 sha512)
endif()


if (ENABLE_AES)
message(STATUS "ENABLE_AES is ON")
list(APPEND src src/aes.c src/aes_modes.c)
list(APPEND tests aes)
endif()


if (ENABLE_CHACHA20)
message(STATUS "ENABLE_CHACHA20 is ON")
list(APPEND src src/chacha20.c)
list(APPEND tests chacha20)
endif()


Expand Down Expand Up @@ -492,14 +442,12 @@ if (ENABLE_SM4_CBC_MAC)
message(STATUS "ENABLE_SM4_CBC_MAC is ON")
list(APPEND src src/sm4_cbc_mac.c)
list(APPEND tools tools/sm4_cbc_mac.c)
list(APPEND tests sm4_cbc_mac)
endif()


if (ENABLE_GMT_0105_RNG)
message(STATUS "ENABLE_GMT_0105_RNG is ON")
list(APPEND src src/sm3_rng.c src/sm4_rng.c)
list(APPEND tests sm3_rng sm4_rng)
endif()


Expand Down Expand Up @@ -564,14 +512,6 @@ if (ENABLE_SDF)
list(APPEND tools tools/sdfutil.c tools/sdftest.c)
endif()


option(ENABLE_HTTP_TESTS "Enable HTTP GET/POST related tests" OFF)
if (ENABLE_HTTP_TESTS)
message(STATUS "ENABLE_HTTP_TESTS")
list(APPEND tests http http_crl)
endif()


option(BUILD_SHARED_LIBS "Build using shared libraries" ON)

if (CMAKE_C_COMPILER_ID MATCHES "MSVC")
Expand Down Expand Up @@ -632,13 +572,6 @@ if (NOT ${CMAKE_SYSTEM_NAME} STREQUAL "iOS")
target_link_libraries(gmssl-bin PRIVATE Ws2_32)
endif()

enable_testing()
foreach(name ${tests})
add_test(NAME ${name} COMMAND ${name}test)
add_executable(${name}test tests/${name}test.c)
target_link_libraries (${name}test LINK_PUBLIC gmssl)
endforeach()

install(TARGETS gmssl-bin RUNTIME DESTINATION bin)
endif()

Expand Down
17 changes: 15 additions & 2 deletions include/gmssl/sm9.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ typedef struct {

_gmssl_export int sm9_sign_master_key_generate(SM9_SIGN_MASTER_KEY *master);
_gmssl_export int sm9_sign_master_key_extract_key(SM9_SIGN_MASTER_KEY *master, const char *id, size_t idlen, SM9_SIGN_KEY *key);
_gmssl_export int tv_sm9_sign_master_key_generate(uint8_t *pri, uint8_t *pub);
_gmssl_export int tv_sm9_sign_master_key_extract_key(uint8_t *masterPri, const char *id, size_t idlen, uint8_t *userPri);

// algorthm,parameters = sm9,sm9sign
#define SM9_SIGN_MASTER_KEY_MAX_SIZE 171
Expand Down Expand Up @@ -131,10 +133,12 @@ typedef struct {
_gmssl_export int sm9_sign_init(SM9_SIGN_CTX *ctx);
_gmssl_export int sm9_sign_update(SM9_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
_gmssl_export int sm9_sign_finish(SM9_SIGN_CTX *ctx, const SM9_SIGN_KEY *key, uint8_t *sig, size_t *siglen);
_gmssl_export int tv_sm9_sign(const uint8_t *masterPub, const uint8_t *userPri, const uint8_t *data, size_t datalen, uint8_t *h, uint8_t *S);

_gmssl_export int sm9_verify_init(SM9_SIGN_CTX *ctx);
_gmssl_export int sm9_verify_update(SM9_SIGN_CTX *ctx, const uint8_t *data, size_t datalen);
_gmssl_export int sm9_verify_finish(SM9_SIGN_CTX *ctx, const uint8_t *sig, size_t siglen,
const SM9_SIGN_MASTER_KEY *mpk, const char *id, size_t idlen);
_gmssl_export int sm9_verify_finish(SM9_SIGN_CTX *ctx, const uint8_t *sig, size_t siglen, const SM9_SIGN_MASTER_KEY *mpk, const char *id, size_t idlen);
_gmssl_export int tv_sm9_verify(const uint8_t *h, const uint8_t *S, const uint8_t *masterPub, const uint8_t *data, size_t datalen, const char *id, size_t idlen);



Expand Down Expand Up @@ -162,7 +166,9 @@ typedef struct {
} SM9_ENC_KEY;

_gmssl_export int sm9_enc_master_key_generate(SM9_ENC_MASTER_KEY *master);
_gmssl_export int tv_sm9_enc_master_key_generate(uint8_t *pri, uint8_t *pub);
_gmssl_export int sm9_enc_master_key_extract_key(SM9_ENC_MASTER_KEY *master, const char *id, size_t idlen, SM9_ENC_KEY *key);
_gmssl_export int tv_sm9_enc_master_key_extract_key(uint8_t *masterPri, const char *id, size_t idlen, uint8_t *userPri);

// algorithm,parameters = sm9,sm9encrypt
#define SM9_ENC_MASTER_KEY_MAX_SIZE 105
Expand Down Expand Up @@ -206,11 +212,17 @@ SM9Cipher ::= SEQUENCE {

int sm9_kem_encrypt(const SM9_ENC_MASTER_KEY *mpk, const char *id, size_t idlen, size_t klen, uint8_t *kbuf, SM9_Z256_POINT *C);
int sm9_kem_decrypt(const SM9_ENC_KEY *key, const char *id, size_t idlen, const SM9_Z256_POINT *C, size_t klen, uint8_t *kbuf);

int sm9_do_encrypt(const SM9_ENC_MASTER_KEY *mpk, const char *id, size_t idlen,
const uint8_t *in, size_t inlen, SM9_Z256_POINT *C1, uint8_t *c2, uint8_t c3[SM3_HMAC_SIZE]);
int sm9_do_decrypt(const SM9_ENC_KEY *key, const char *id, size_t idlen,
const SM9_Z256_POINT *C1, const uint8_t *c2, size_t c2len, const uint8_t c3[SM3_HMAC_SIZE], uint8_t *out);

int tv_sm9_do_encrypt(uint8_t *masterPub, const char *id, size_t idlen,
const uint8_t *in, size_t inlen, uint8_t *c1, uint8_t *c2, uint8_t *c3);
int tv_sm9_do_decrypt(uint8_t *userPri, const char *id, size_t idlen,
const uint8_t *c1, const uint8_t *c2, size_t c2len, const uint8_t *c3, uint8_t *out);

#define SM9_MAX_PLAINTEXT_SIZE 255
#define SM9_MAX_CIPHERTEXT_SIZE 367 // calculated in test_sm9_ciphertext()
int sm9_ciphertext_to_der(const SM9_Z256_POINT *C1, const uint8_t *c2, size_t c2len,
Expand All @@ -229,6 +241,7 @@ _gmssl_export int sm9_decrypt(const SM9_ENC_KEY *key, const char *id, size_t idl
#define SM9_EXCH_KEY SM9_ENC_KEY
#define sm9_exch_master_key_generate(msk) sm9_enc_master_key_generate(msk)
int sm9_exch_master_key_extract_key(SM9_EXCH_MASTER_KEY *master, const char *id, size_t idlen, SM9_EXCH_KEY *key);
int tv_sm9_exch_master_key_extract_key(uint8_t *masterPri, const char *id, size_t idlen, uint8_t *userPri);

int sm9_exch_step_1A(const SM9_EXCH_MASTER_KEY *mpk, const char *idB, size_t idBlen, SM9_Z256_POINT *RA, sm9_z256_t rA);
int sm9_exch_step_1B(const SM9_EXCH_MASTER_KEY *mpk, const char *idA, size_t idAlen, const char *idB, size_t idBlen,
Expand Down
29 changes: 29 additions & 0 deletions src/sm9_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ int sm9_kem_decrypt(const SM9_ENC_KEY *key, const char *id, size_t idlen, const
return 1;
}

int tv_sm9_do_encrypt(uint8_t *masterPub, const char *id, size_t idlen, const uint8_t *in, size_t inlen, uint8_t *c1, uint8_t *c2, uint8_t *c3)
{
SM9_ENC_MASTER_KEY masterKey;
SM9_Z256_POINT C1;

sm9_z256_point_from_uncompressed_octets(&masterKey.Ppube, masterPub);

if(sm9_do_encrypt(&masterKey, id, idlen, in, inlen, &C1, c2, c3) != 1){
return -1;
}
sm9_z256_point_to_uncompressed_octets(&C1, c1);
return 1;
}

int sm9_do_encrypt(const SM9_ENC_MASTER_KEY *mpk, const char *id, size_t idlen,
const uint8_t *in, size_t inlen,
SM9_Z256_POINT *C1, uint8_t *c2, uint8_t c3[SM3_HMAC_SIZE])
Expand All @@ -125,6 +139,21 @@ int sm9_do_encrypt(const SM9_ENC_MASTER_KEY *mpk, const char *id, size_t idlen,
return 1;
}

int tv_sm9_do_decrypt(uint8_t *userPri, const char *id, size_t idlen, const uint8_t *c1, const uint8_t *c2, size_t c2len, const uint8_t *c3, uint8_t *out)
{
SM9_ENC_KEY encKey;
SM9_Z256_POINT C1;

sm9_z256_point_from_uncompressed_octets(&C1, c1);

sm9_z256_twist_point_from_uncompressed_octets(&encKey.de, userPri);

if(sm9_do_decrypt(&encKey, id, idlen, &C1, c2, c2len, c3, out) != 1){
return -1;
}
return sizeof(out);
}

int sm9_do_decrypt(const SM9_ENC_KEY *key, const char *id, size_t idlen,
const SM9_Z256_POINT *C1, const uint8_t *c2, size_t c2len, const uint8_t c3[SM3_HMAC_SIZE],
uint8_t *out)
Expand Down
61 changes: 61 additions & 0 deletions src/sm9_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,17 @@ int sm9_enc_key_from_der(SM9_ENC_KEY *key, const uint8_t **in, size_t *inlen)
return 1;
}

int tv_sm9_sign_master_key_generate(uint8_t *pri, uint8_t *pub)
{
SM9_SIGN_MASTER_KEY msk;
if(sm9_sign_master_key_generate(&msk) != 1){
return -1;
}
sm9_z256_to_bytes(msk.ks, pri);
sm9_z256_twist_point_to_uncompressed_octets(&msk.Ppubs, pub);
return 1;
}

int sm9_sign_master_key_generate(SM9_SIGN_MASTER_KEY *msk)
{
if (!msk) {
Expand All @@ -369,6 +380,17 @@ int sm9_sign_master_key_generate(SM9_SIGN_MASTER_KEY *msk)
return 1;
}

int tv_sm9_enc_master_key_generate(uint8_t *pri, uint8_t *pub)
{
SM9_ENC_MASTER_KEY encMasterKey;
if(sm9_enc_master_key_generate(&encMasterKey) != 1){
return -1;
}
sm9_z256_to_bytes(encMasterKey.ke, pri);
sm9_z256_point_to_uncompressed_octets(&encMasterKey.Ppube, pub);
return 1;
}

int sm9_enc_master_key_generate(SM9_ENC_MASTER_KEY *msk)
{
// k = rand(1, n-1)
Expand All @@ -381,6 +403,19 @@ int sm9_enc_master_key_generate(SM9_ENC_MASTER_KEY *msk)
return 1;
}

int tv_sm9_sign_master_key_extract_key(uint8_t *masterPri, const char *id, size_t idlen, uint8_t *userPri)
{
SM9_SIGN_MASTER_KEY masterKey;
SM9_SIGN_KEY userKey;

sm9_z256_from_bytes(masterKey.ks, masterPri);
if(sm9_sign_master_key_extract_key(&masterKey, id, idlen, &userKey) != 1){
return -1;
}
sm9_z256_point_to_uncompressed_octets(&userKey.ds, userPri);
return 1;
}

int sm9_sign_master_key_extract_key(SM9_SIGN_MASTER_KEY *msk, const char *id, size_t idlen, SM9_SIGN_KEY *key)
{
sm9_z256_t t;
Expand All @@ -405,6 +440,19 @@ int sm9_sign_master_key_extract_key(SM9_SIGN_MASTER_KEY *msk, const char *id, si
return 1;
}

int tv_sm9_enc_master_key_extract_key(uint8_t *masterPri, const char *id, size_t idlen, uint8_t *userPri)
{
SM9_ENC_MASTER_KEY masterKey;
SM9_ENC_KEY userKey;

sm9_z256_from_bytes(masterKey.ke, masterPri);
if(sm9_enc_master_key_extract_key(&masterKey, id, idlen, &userKey) != 1){
return -1;
}
sm9_z256_twist_point_to_uncompressed_octets(&userKey.de, userPri);
return 1;
}

int sm9_enc_master_key_extract_key(SM9_ENC_MASTER_KEY *msk, const char *id, size_t idlen,
SM9_ENC_KEY *key)
{
Expand All @@ -429,6 +477,19 @@ int sm9_enc_master_key_extract_key(SM9_ENC_MASTER_KEY *msk, const char *id, size
return 1;
}

int tv_sm9_exch_master_key_extract_key(uint8_t *masterPri, const char *id, size_t idlen, uint8_t *userPri)
{
SM9_EXCH_MASTER_KEY masterKey;
SM9_EXCH_KEY userKey;

sm9_z256_from_bytes(masterKey.ke, masterPri);
if(sm9_exch_master_key_extract_key(&masterKey, id, idlen, &userKey) != 1){
return -1;
}
sm9_z256_twist_point_to_uncompressed_octets(&userKey.de, userPri);
return 1;
}

int sm9_exch_master_key_extract_key(SM9_EXCH_MASTER_KEY *msk, const char *id, size_t idlen,
SM9_EXCH_KEY *key)
{
Expand Down
Loading