Skip to content

Commit

Permalink
emu test code formatted
Browse files Browse the repository at this point in the history
  • Loading branch information
benzfield committed Nov 29, 2023
1 parent a3c1863 commit 55b778d
Showing 1 changed file with 163 additions and 78 deletions.
241 changes: 163 additions & 78 deletions emu/test/emu_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class EmulatorTest : public testing::Test {
}
};

#if 1
TEST_F(EmulatorTest, SDF_OpenSession_10times) {
void *device_handle = nullptr;
int rv = SDF_OpenDevice(&device_handle);
Expand Down Expand Up @@ -90,7 +89,9 @@ TEST_F(EmulatorTest, SDF_GenerateRandom) {

unsigned int randomLength = 16;
auto randomBuf = absl::make_unique<uint8_t[]>(randomLength);
rv = SDF_GenerateRandom(session_handle, randomLength, randomBuf.get());
rv = SDF_GenerateRandom(session_handle,
randomLength,
randomBuf.get());
EXPECT_EQ(rv, SDR_OK);

rv = SDF_CloseSession(session_handle);
Expand All @@ -100,8 +101,6 @@ TEST_F(EmulatorTest, SDF_GenerateRandom) {
EXPECT_EQ(rv, SDR_OK);
}

#endif

TEST_F(EmulatorTest, SDF_GenerateKeyWithKEK) {
void *device_handle = nullptr;
int rv = SDF_OpenDevice(&device_handle);
Expand All @@ -116,16 +115,26 @@ TEST_F(EmulatorTest, SDF_GenerateKeyWithKEK) {
void *keyHandle = nullptr;

// 生成key
rv = SDF_GenerateKeyWithKEK(session_handle, 128, SGD_SM4_ECB, 1, (unsigned char *)keyBuf.get(),
&keyLength, &keyHandle);
rv = SDF_GenerateKeyWithKEK(session_handle,
128,
SGD_SM4_ECB,
1,
(unsigned char *)keyBuf.get(),
&keyLength,
&keyHandle);
EXPECT_EQ(rv, SDR_OK);

// 销毁生成的key
rv = SDF_DestroyKey(session_handle, keyHandle);
EXPECT_EQ(rv, SDR_OK);

// 导入key
rv = SDF_ImportKeyWithKEK(session_handle, SGD_SM4_ECB, 1, keyBuf.get(), keyLength, &keyHandle);
rv = SDF_ImportKeyWithKEK(session_handle,
SGD_SM4_ECB,
1,
keyBuf.get(),
keyLength,
&keyHandle);
EXPECT_EQ(rv, SDR_OK);

// 销毁导入的key
Expand All @@ -149,27 +158,45 @@ TEST_F(EmulatorTest, ImportKey_Encrypt_Decrypt_SM4_ECB) {
EXPECT_EQ(rv, SDR_OK);

void *keyHandle = nullptr;
unsigned char pucKey[16] = {0x1, 0x2, 0x3, 0x4, 0x1, 0x2, 0x3, 0x4,
0x1, 0x2, 0x3, 0x4, 0x1, 0x2, 0x3, 0x4};
rv = SDF_ImportKey(session_handle, pucKey, sizeof(pucKey), &keyHandle);
EXPECT_EQ(rv, SDR_OK);

unsigned char plain[16] = {0x1, 0x2, 0x3, 0x4, 0x1, 0x2, 0x3, 0x4,
0x1, 0x2, 0x3, 0x4, 0x1, 0x2, 0x3, 0x4};
unsigned char pucKey[16] = {
0x1, 0x2, 0x3, 0x4, 0x1, 0x2, 0x3, 0x4,
0x1, 0x2, 0x3, 0x4, 0x1, 0x2, 0x3, 0x4};
rv = SDF_ImportKey(session_handle,
pucKey,
sizeof(pucKey),
&keyHandle);
EXPECT_EQ(rv, SDR_OK);

unsigned char plain[16] = {
0x1, 0x2, 0x3, 0x4, 0x1, 0x2, 0x3, 0x4,
0x1, 0x2, 0x3, 0x4, 0x1, 0x2, 0x3, 0x4};
unsigned int plainLen = sizeof(plain);
auto cipherBuf = absl::make_unique<uint8_t[]>(plainLen);
unsigned cipherLen = plainLen;
unsigned char expectResult[] = {0x36, 0x72, 0xfe, 0x3d, 0xd2, 0x5c, 0xd0, 0x85,
0x04, 0x07, 0x22, 0x9a, 0xbc, 0x55, 0x81, 0xa8};
rv = SDF_Encrypt(session_handle, keyHandle, SGD_SM4_ECB, NULL,
plain, plainLen, cipherBuf.get(), &cipherLen);
unsigned char expectResult[] = {
0x36, 0x72, 0xfe, 0x3d, 0xd2, 0x5c, 0xd0, 0x85,
0x04, 0x07, 0x22, 0x9a, 0xbc, 0x55, 0x81, 0xa8};
rv = SDF_Encrypt(session_handle,
keyHandle,
SGD_SM4_ECB,
NULL,
plain,
plainLen,
cipherBuf.get(),
&cipherLen);
EXPECT_EQ(rv, SDR_OK);
EXPECT_EQ(0, memcmp(cipherBuf.get(), expectResult, cipherLen));

unsigned int decPlainLen = plainLen;
auto decPlainBuf = absl::make_unique<uint8_t[]>(decPlainLen);
rv = SDF_Decrypt(session_handle, keyHandle, SGD_SM4_ECB, NULL,
cipherBuf.get(), cipherLen, decPlainBuf.get(), &decPlainLen);
rv = SDF_Decrypt(session_handle,
keyHandle,
SGD_SM4_ECB,
NULL,
cipherBuf.get(),
cipherLen,
decPlainBuf.get(),
&decPlainLen);
EXPECT_EQ(rv, SDR_OK);
EXPECT_EQ(plainLen, decPlainLen);
EXPECT_EQ(0, memcmp(decPlainBuf.get(), plain, decPlainLen));
Expand All @@ -194,39 +221,59 @@ TEST_F(EmulatorTest, ImportKey_Encrypt_Decrypt_SM4_CBC) {
EXPECT_EQ(rv, SDR_OK);

void *keyHandle = nullptr;
unsigned char pucKey[16] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00};
rv = SDF_ImportKey(session_handle, pucKey, sizeof(pucKey), &keyHandle);
EXPECT_EQ(rv, SDR_OK);

unsigned char iv[16] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,0x00};
unsigned char plain[32] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
unsigned char pucKey[16] = {
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00};
rv = SDF_ImportKey(session_handle,
pucKey,
sizeof(pucKey),
&keyHandle);
EXPECT_EQ(rv, SDR_OK);

unsigned char iv[16] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,0x00};
unsigned char plain[32] = {
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
unsigned int plainLen = sizeof(plain);
auto cipherBuf = absl::make_unique<uint8_t[]>(plainLen);
unsigned cipherLen = plainLen;
unsigned char expectResult[] = {0x9b, 0x4d, 0x67, 0x92, 0x65, 0x30, 0x5c, 0x47,
0x01, 0x1e, 0x6d, 0x5c, 0xf1, 0x4c, 0x50, 0xf8,
0x96, 0x96, 0x2d, 0xfd, 0xe4, 0x1a, 0x96, 0xb4,
0xd0, 0xf4, 0x92, 0x6b, 0xb1, 0x23, 0x8c, 0x6d};
unsigned char expectIV[] = {0x96, 0x96, 0x2d, 0xfd, 0xe4, 0x1a, 0x96, 0xb4,
0xd0, 0xf4, 0x92, 0x6b, 0xb1, 0x23, 0x8c, 0x6d};
unsigned char expectResult[] = {
0x9b, 0x4d, 0x67, 0x92, 0x65, 0x30, 0x5c, 0x47,
0x01, 0x1e, 0x6d, 0x5c, 0xf1, 0x4c, 0x50, 0xf8,
0x96, 0x96, 0x2d, 0xfd, 0xe4, 0x1a, 0x96, 0xb4,
0xd0, 0xf4, 0x92, 0x6b, 0xb1, 0x23, 0x8c, 0x6d};
unsigned char expectIV[] = {
0x96, 0x96, 0x2d, 0xfd, 0xe4, 0x1a, 0x96, 0xb4,
0xd0, 0xf4, 0x92, 0x6b, 0xb1, 0x23, 0x8c, 0x6d};
unsigned char iv_init[16] = {0};
memcpy(iv_init, iv, 16);
rv = SDF_Encrypt(session_handle, keyHandle, SGD_SM4_CBC, iv,
plain, plainLen, cipherBuf.get(), &cipherLen);
rv = SDF_Encrypt(session_handle,
keyHandle,
SGD_SM4_CBC,
iv,
plain,
plainLen,
cipherBuf.get(),
&cipherLen);
EXPECT_EQ(rv, SDR_OK);
EXPECT_EQ(0, memcmp(cipherBuf.get(), expectResult, cipherLen));
EXPECT_EQ(0, memcmp(iv, expectIV, 16));

unsigned int decPlainLen = plainLen;
auto decPlainBuf = absl::make_unique<uint8_t[]>(decPlainLen);
memcpy(iv, iv_init, 16);
rv = SDF_Decrypt(session_handle, keyHandle, SGD_SM4_CBC, iv,
cipherBuf.get(), cipherLen, decPlainBuf.get(), &decPlainLen);
rv = SDF_Decrypt(session_handle,
keyHandle,
SGD_SM4_CBC,
iv,
cipherBuf.get(),
cipherLen,
decPlainBuf.get(),
&decPlainLen);
EXPECT_EQ(rv, SDR_OK);
EXPECT_EQ(plainLen, decPlainLen);
EXPECT_EQ(0, memcmp(decPlainBuf.get(), plain, decPlainLen));
Expand All @@ -251,8 +298,9 @@ TEST_F(EmulatorTest, SDF_Hash_Init_Update_Final) {
rv = SDF_OpenSession(device_handle, &session_handle);
EXPECT_EQ(rv, SDR_OK);

unsigned char message[16] = {0x1, 0x2, 0x3, 0x4, 0x1, 0x2, 0x3, 0x4,
0x1, 0x2, 0x3, 0x4, 0x1, 0x2, 0x3, 0x4};
unsigned char message[16] = {
0x1, 0x2, 0x3, 0x4, 0x1, 0x2, 0x3, 0x4,
0x1, 0x2, 0x3, 0x4, 0x1, 0x2, 0x3, 0x4};

ECCrefPublicKey eccRefPubKey;
eccRefPubKey.bits = 256;
Expand All @@ -270,17 +318,25 @@ TEST_F(EmulatorTest, SDF_Hash_Init_Update_Final) {
memcpy(eccRefPubKey.y, y.c_str(), y.length());
std::string id = "1234567812345678";

rv = SDF_HashInit(session_handle, SGD_SM3, &eccRefPubKey, (unsigned char *)id.c_str(), 16);
rv = SDF_HashInit(session_handle,
SGD_SM3,
&eccRefPubKey,
(unsigned char *)id.c_str(),
16);
EXPECT_EQ(rv, SDR_OK);

rv = SDF_HashUpdate(session_handle, message, sizeof(message));
rv = SDF_HashUpdate(session_handle,
message,
sizeof(message));
EXPECT_EQ(rv, SDR_OK);

unsigned char sm3Hash[32] = {0};
unsigned int sm3HashLen = 32;
unsigned char expectResult[] =
{0xda, 0x7b, 0x52, 0xe3, 0x54, 0xa8, 0xce, 0x84, 0x0f, 0xc2, 0x97, 0xd0, 0xad, 0x8a, 0x2b, 0xc8,
0x12, 0x71, 0xeb, 0x49, 0xb2, 0xf9, 0x76, 0x35, 0x28, 0x41, 0x69, 0x7c, 0x87, 0x9b, 0x7c, 0x9d};
unsigned char expectResult[] ={
0xda, 0x7b, 0x52, 0xe3, 0x54, 0xa8, 0xce, 0x84,
0x0f, 0xc2, 0x97, 0xd0, 0xad, 0x8a, 0x2b, 0xc8,
0x12, 0x71, 0xeb, 0x49, 0xb2, 0xf9, 0x76, 0x35,
0x28, 0x41, 0x69, 0x7c, 0x87, 0x9b, 0x7c, 0x9d};
rv = SDF_HashFinal(session_handle, sm3Hash, &sm3HashLen);
EXPECT_EQ(rv, SDR_OK);
EXPECT_EQ(0, memcmp(sm3Hash, expectResult, 32));
Expand All @@ -292,9 +348,6 @@ TEST_F(EmulatorTest, SDF_Hash_Init_Update_Final) {
EXPECT_EQ(rv, SDR_OK);
}

//*********************************************/
// SM2 测试
//*********************************************/
TEST_F(EmulatorTest, SDF_ExternalEncDec_ECC) {
void *device_handle = nullptr;
int rv = SDF_OpenDevice(&device_handle);
Expand Down Expand Up @@ -431,9 +484,6 @@ TEST_F(EmulatorTest, SDF_ExternalSigVer_ECC) {
&st_sign);
EXPECT_EQ(rv, SDR_OK);
}
//*********************************************/
// SM2 测试 end
//*********************************************/

TEST_F(EmulatorTest, SDF_GenerateKeyPair_ECC) {
void *device_handle = nullptr;
Expand All @@ -446,7 +496,11 @@ TEST_F(EmulatorTest, SDF_GenerateKeyPair_ECC) {

ECCrefPublicKey eccPublicKey;
ECCrefPrivateKey eccPrivateKey;
rv = SDF_GenerateKeyPair_ECC(session_handle, SGD_SM2_1, 256, &eccPublicKey, &eccPrivateKey);
rv = SDF_GenerateKeyPair_ECC(session_handle,
SGD_SM2_1,
256,
&eccPublicKey,
&eccPrivateKey);
EXPECT_EQ(rv, SDR_OK);

unsigned char plain[32] = {
Expand All @@ -456,9 +510,19 @@ TEST_F(EmulatorTest, SDF_GenerateKeyPair_ECC) {
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01
};
ECCSignature sign;
rv = SDF_ExternalSign_ECC(session_handle, SGD_SM2_1, &eccPrivateKey, plain, sizeof(plain), &sign);
rv = SDF_ExternalSign_ECC(session_handle,
SGD_SM2_1,
&eccPrivateKey,
plain,
sizeof(plain),
&sign);
EXPECT_EQ(rv, SDR_OK);
rv = SDF_ExternalVerify_ECC(session_handle, SGD_SM2_1, &eccPublicKey, plain, sizeof(plain), &sign);
rv = SDF_ExternalVerify_ECC(session_handle,
SGD_SM2_1,
&eccPublicKey,
plain,
sizeof(plain),
&sign);
EXPECT_EQ(rv, SDR_OK);

rv = SDF_CloseSession(session_handle);
Expand All @@ -478,40 +542,61 @@ TEST_F(EmulatorTest, SDF_CalculateMAC_SGD_SM4_MAC) {
EXPECT_EQ(rv, SDR_OK);

void *keyHandle = nullptr;
unsigned char pucKey[16] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
rv = SDF_ImportKey(session_handle, pucKey, sizeof(pucKey), &keyHandle);
EXPECT_EQ(rv, SDR_OK);

unsigned char iv[16] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
unsigned char message[16] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
unsigned char pucKey[16] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
rv = SDF_ImportKey(session_handle,
pucKey,
sizeof(pucKey),
&keyHandle);
EXPECT_EQ(rv, SDR_OK);

unsigned char iv[16] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
unsigned char message[16] = {
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
unsigned int messageLen = sizeof(message);
auto macBuf = absl::make_unique<uint8_t[]>(messageLen);
unsigned int macLen = messageLen;
unsigned char expectResult[] = {0xdf, 0x4e, 0x4e, 0xfc, 0x6d, 0xbb, 0x1c, 0xfb,
0x3c, 0xe8, 0xbb, 0x0c, 0x8b, 0x1e, 0x03, 0xb5};
unsigned char expectResult[] = {
0xdf, 0x4e, 0x4e, 0xfc, 0x6d, 0xbb, 0x1c, 0xfb,
0x3c, 0xe8, 0xbb, 0x0c, 0x8b, 0x1e, 0x03, 0xb5};
unsigned char iv_init[16] = {0};
memcpy(iv_init, iv, 16);
rv = SDF_CalculateMAC(session_handle, keyHandle, SGD_SM4_MAC, iv,
message, messageLen, macBuf.get(), &macLen);
rv = SDF_CalculateMAC(session_handle,
keyHandle,
SGD_SM4_MAC,
iv,
message,
messageLen,
macBuf.get(),
&macLen);
EXPECT_EQ(rv, SDR_OK);
EXPECT_EQ(0, memcmp(macBuf.get(), expectResult, macLen));
EXPECT_EQ(0, memcmp(iv, expectResult, 16));

unsigned char message2[32] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
unsigned char message2[32] = {
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
unsigned int messageLen2 = sizeof(message2);
auto macBuf2 = absl::make_unique<uint8_t[]>(messageLen2);
unsigned int macLen2 = messageLen2;
unsigned char expectResult2[] = {0x19, 0x4c, 0xb6, 0xe5, 0xd7, 0xbf, 0x8a, 0x10,
0x3c, 0x62, 0x36, 0x83, 0xd9, 0xa0, 0x90, 0x21};
unsigned char expectResult2[] = {
0x19, 0x4c, 0xb6, 0xe5, 0xd7, 0xbf, 0x8a, 0x10,
0x3c, 0x62, 0x36, 0x83, 0xd9, 0xa0, 0x90, 0x21};
memcpy(iv, iv_init, 16);
rv = SDF_CalculateMAC(session_handle, keyHandle, SGD_SM4_MAC, iv,
message2, messageLen2, macBuf2.get(), &macLen2);
rv = SDF_CalculateMAC(session_handle,
keyHandle,
SGD_SM4_MAC,
iv,
message2,
messageLen2,
macBuf2.get(),
&macLen2);
EXPECT_EQ(rv, SDR_OK);
EXPECT_EQ(0, memcmp(macBuf2.get(), expectResult2, macLen2));
EXPECT_EQ(0, memcmp(iv, expectResult2, 16));
Expand Down

0 comments on commit 55b778d

Please sign in to comment.