Add more SM4 test vectors

SM4 pass openssl and other known test vectors. SM4-GCM and SM4-XTS only support the GB/T GF(2^128) encoding standard.
This commit is contained in:
Zhi Guan
2024-04-21 10:10:46 +08:00
parent a485fa0b10
commit 252c9e1765
16 changed files with 1267 additions and 691 deletions

View File

@@ -59,6 +59,113 @@ static int test_sm4_cfb(void)
return 1;
}
// FIXME: no test vectors for SM4_CFB_8, SM4_CFB_64
static int test_sm4_cfb_test_vectors(void)
{
struct {
char *label;
char *key;
size_t sbytes;
char *iv;
char *plaintext;
char *ciphertext;
} tests[] = {
{
"openssl",
"0123456789abcdeffedcba9876543210",
SM4_CFB_128,
"0123456789abcdeffedcba9876543210",
"0123456789abcdeffedcba98765432100123456789abcdeffedcba9876543210",
"693d9a535bad5bb1786f53d7253a70569ed258a85a0467cc92aab393dd978995",
},
{
"draft-ribose-cfrg-sm4-10 example-1",
"0123456789abcdeffedcba9876543210",
SM4_CFB_128,
"000102030405060708090a0b0c0d0e0f",
"aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffffaaaaaaaabbbbbbbb",
"ac3236cb861dd316e6413b4e3c7524b769d4c54ed433b9a0346009beb37b2b3f",
},
{
"draft-ribose-cfrg-sm4-10 example-2",
"fedcba98765432100123456789abcdef",
SM4_CFB_128,
"000102030405060708090a0b0c0d0e0f",
"aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffffaaaaaaaabbbbbbbb",
"5dcccd25a84ba16560d7f265887068490d9b86ff20c3bfe115ffa02ca6192cc5"
},
};
uint8_t key[16];
size_t key_len;
uint8_t iv[16];
size_t iv_len;
uint8_t *plaintext;
size_t plaintext_len;
uint8_t *ciphertext;
size_t ciphertext_len;
SM4_KEY sm4_key;
uint8_t *encrypted;
size_t encrypted_len;
uint8_t *decrypted;
size_t decrypted_len;
size_t i;
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
if ((plaintext = (uint8_t *)malloc(strlen(tests[i].plaintext)/2)) == NULL) {
error_print();
return -1;
}
if ((ciphertext = (uint8_t *)malloc(strlen(tests[i].ciphertext)/2)) == NULL) {
error_print();
return -1;
}
hex_to_bytes(tests[i].key, strlen(tests[i].key), key, &key_len);
hex_to_bytes(tests[i].iv, strlen(tests[i].iv), iv, &iv_len);
hex_to_bytes(tests[i].plaintext, strlen(tests[i].plaintext), plaintext, &plaintext_len);
hex_to_bytes(tests[i].ciphertext, strlen(tests[i].ciphertext), ciphertext, &ciphertext_len);
if ((encrypted = (uint8_t *)malloc(ciphertext_len)) == NULL) {
error_print();
return -1;
}
sm4_set_encrypt_key(&sm4_key, key);
sm4_cfb_encrypt(&sm4_key, tests[i].sbytes, iv, plaintext, plaintext_len, encrypted);
if (memcmp(encrypted, ciphertext, ciphertext_len) != 0) {
error_print();
return -1;
}
if ((decrypted = (uint8_t *)malloc(plaintext_len)) == NULL) {
error_print();
return -1;
}
//sm4_set_encrypt_key(&sm4_key, key);
hex_to_bytes(tests[i].iv, strlen(tests[i].iv), iv, &iv_len);
sm4_cfb_decrypt(&sm4_key, tests[i].sbytes, iv, ciphertext, ciphertext_len, decrypted);
if (memcmp(decrypted, plaintext, plaintext_len) != 0) {
error_print();
return -1;
}
free(plaintext);
free(ciphertext);
free(encrypted);
free(decrypted);
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_sm4_cfb_ctx(void)
{
@@ -158,6 +265,7 @@ static int test_sm4_cfb_ctx(void)
int main(void)
{
if (test_sm4_cfb() != 1) goto err;
if (test_sm4_cfb_test_vectors() != 1) goto err;
if (test_sm4_cfb_ctx() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;