Let sm4_cbc_encrypt_blocks update iv

If in == out, then after encryptions the input (i.e. iv) is changed
This commit is contained in:
Zhi Guan
2024-05-13 21:44:06 +08:00
parent 3b6c2a3e9b
commit 7f3072e917
5 changed files with 49 additions and 22 deletions

View File

@@ -21,15 +21,19 @@
static int test_sm4_cbc(void)
{
SM4_KEY sm4_key;
uint8_t key[16] = {0};
uint8_t iv[16] = {0};
const uint8_t key[16] = {0};
const uint8_t civ[16] = {0};
uint8_t iv[16];
uint8_t buf1[32] = {0};
uint8_t buf2[32] = {0};
uint8_t buf3[32] = {0};
sm4_set_encrypt_key(&sm4_key, key);
memcpy(iv, civ, 16);
sm4_cbc_encrypt_blocks(&sm4_key, iv, buf1, 2, buf2);
sm4_set_decrypt_key(&sm4_key, key);
memcpy(iv, civ, 16);
sm4_cbc_decrypt_blocks(&sm4_key, iv, buf2, 2, buf3);
if (memcmp(buf1, buf3, sizeof(buf3)) != 0) {
@@ -107,6 +111,7 @@ static int test_sm4_cbc_test_vectors(void)
}
sm4_set_encrypt_key(&sm4_key, key);
hex_to_bytes(tests[i].iv, strlen(tests[i].iv), iv, &iv_len);
sm4_cbc_encrypt_blocks(&sm4_key, iv, plaintext, plaintext_len/16, encrypted);
if (memcmp(encrypted, ciphertext, ciphertext_len) != 0) {