Update SM4 TBOX version

This commit is contained in:
Zhi Guan
2024-04-28 17:11:57 +08:00
parent af3175f6fa
commit 060930093f
5 changed files with 624 additions and 103 deletions

View File

@@ -91,6 +91,48 @@ static int test_sm4(void)
return 1;
}
static int test_sm4_encrypt_blocks(void)
{
const uint8_t key[16] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
};
const uint8_t plaintext[16 * 4] = {
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
};
const uint8_t ciphertext[16 * 4] = {
0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
};
SM4_KEY sm4_key;
uint8_t encrypted[16 * 4];
sm4_set_encrypt_key(&sm4_key, key);
sm4_encrypt_blocks(&sm4_key, plaintext, 4, encrypted);
if (memcmp(encrypted, ciphertext, sizeof(ciphertext)) != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_sm4_encrypt_speed(void)
{
SM4_KEY sm4_key;
@@ -102,6 +144,9 @@ static int test_sm4_encrypt_speed(void)
int i;
sm4_set_encrypt_key(&sm4_key, key);
for (i = 0; i < nbytes/sizeof(buf); i++) {
sm4_encrypt(&sm4_key, buf, buf);
}
begin = clock();
for (i = 0; i < nbytes/sizeof(buf); i++) {
@@ -115,11 +160,44 @@ static int test_sm4_encrypt_speed(void)
return 1;
}
static int test_sm4_encrypt_blocks_speed(void)
{
SM4_KEY sm4_key;
uint8_t key[16] = {0};
//uint32_t buf[1024];
uint8_t buf[4096 + 100] __attribute__((aligned(16)));
clock_t begin, end;
double seconds;
int i;
sm4_set_encrypt_key(&sm4_key, key);
for (i = 0; i < 4096; i++) {
// fprintf(stderr, ".");
sm4_encrypt_blocks(&sm4_key, (uint8_t *)buf, sizeof(buf)/16, (uint8_t *)buf);
}
//fprintf(stderr, "start\n");
begin = clock();
for (i = 0; i < 4096; i++) {
sm4_encrypt_blocks(&sm4_key, (uint8_t *)buf, sizeof(buf)/16, (uint8_t *)buf);
// fprintf(stderr, ".");
}
end = clock();
seconds = (double)(end - begin)/ CLOCKS_PER_SEC;
fprintf(stderr, "sm4_encrypt_blocks: %f MiB per second\n", 16/seconds);
return 1;
}
int main(void)
{
if (test_sm4() != 1) goto err;
if (test_sm4_encrypt_blocks() != 1) goto err;
#if ENABLE_TEST_SPEED
if (test_sm4_encrypt_speed() != 1) goto err;
if (test_sm4_encrypt_blocks_speed() != 1) goto err;
#endif
printf("%s all tests passed\n", __FILE__);
return 0;