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

@@ -55,6 +55,107 @@ static int test_sm4_ofb(void)
return 1;
}
static int test_sm4_ofb_test_vectors(void)
{
struct {
char *label;
char *key;
char *iv;
char *plaintext;
char *ciphertext;
} tests[] = {
{
"sm4-ofb from openssl",
"0123456789abcdeffedcba9876543210",
"0123456789abcdeffedcba9876543210",
"0123456789abcdeffedcba98765432100123456789abcdeffedcba9876543210",
"693d9a535bad5bb1786f53d7253a7056f2075d28b5235f58d50027e4177d2bce",
},
{
"sm4-ofb-example-1 from draft-ribose-cfrg-sm4-10",
"0123456789abcdeffedcba9876543210",
"000102030405060708090a0b0c0d0e0f",
"aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffffaaaaaaaabbbbbbbb",
"ac3236cb861dd316e6413b4e3c7524b71d01aca2487ca582cbf5463e6698539b",
},
{
"sm4-ofb-example-2 from draft-ribose-cfrg-sm4-10",
"fedcba98765432100123456789abcdef",
"000102030405060708090a0b0c0d0e0f",
"aaaaaaaabbbbbbbbccccccccddddddddeeeeeeeeffffffffaaaaaaaabbbbbbbb",
"5dcccd25a84ba16560d7f2658870684933fa16bd5cd9c856cacaa1e101897a97",
},
};
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;
}
if ((decrypted = (uint8_t *)malloc(plaintext_len)) == NULL) {
error_print();
return -1;
}
sm4_set_encrypt_key(&sm4_key, key);
sm4_ofb_encrypt(&sm4_key, iv, plaintext, plaintext_len, encrypted);
if (memcmp(encrypted, ciphertext, ciphertext_len) != 0) {
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_ofb_encrypt(&sm4_key, 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_ofb_ctx(void)
{
SM4_OFB_CTX ctx;
@@ -150,6 +251,7 @@ static int test_sm4_ofb_ctx(void)
int main(void)
{
if (test_sm4_ofb() != 1) goto err;
if (test_sm4_ofb_test_vectors() != 1) goto err;
if (test_sm4_ofb_ctx() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;