Fix SM4 API bugs and change behavior

This commit is contained in:
Zhi Guan
2026-06-13 22:14:56 +08:00
parent 8fded4abc7
commit c12edeb7b1
29 changed files with 1281 additions and 212 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2024 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
@@ -260,11 +260,69 @@ static int test_sm4_cfb_ctx(void)
return 1;
}
static int test_sm4_cfb_args(void)
{
SM4_CFB_CTX ctx;
uint8_t key[16] = {0};
uint8_t iv[16] = {0};
uint8_t in[16] = {0};
uint8_t out[16];
size_t outlen;
if (sm4_cfb_encrypt_init(NULL, SM4_CFB_128, key, iv) != -1
|| sm4_cfb_encrypt_init(&ctx, 0, key, iv) != -1
|| sm4_cfb_encrypt_init(&ctx, SM4_BLOCK_SIZE + 1, key, iv) != -1
|| sm4_cfb_encrypt_init(&ctx, SM4_CFB_128, NULL, iv) != -1
|| sm4_cfb_encrypt_init(&ctx, SM4_CFB_128, key, NULL) != -1
|| sm4_cfb_decrypt_init(NULL, SM4_CFB_128, key, iv) != -1
|| sm4_cfb_decrypt_init(&ctx, 0, key, iv) != -1
|| sm4_cfb_decrypt_init(&ctx, SM4_BLOCK_SIZE + 1, key, iv) != -1
|| sm4_cfb_decrypt_init(&ctx, SM4_CFB_128, NULL, iv) != -1
|| sm4_cfb_decrypt_init(&ctx, SM4_CFB_128, key, NULL) != -1) {
error_print();
return -1;
}
if (sm4_cfb_encrypt_init(&ctx, SM4_CFB_128, key, iv) != 1
|| sm4_cfb_encrypt_update(NULL, in, sizeof(in), out, &outlen) != -1
|| sm4_cfb_encrypt_update(&ctx, NULL, 1, out, &outlen) != -1
|| sm4_cfb_encrypt_update(&ctx, in, sizeof(in), NULL, &outlen) != -1
|| sm4_cfb_encrypt_update(&ctx, in, sizeof(in), out, NULL) != -1
|| sm4_cfb_encrypt_update(&ctx, NULL, 0, out, &outlen) != 1
|| outlen != 0
|| sm4_cfb_encrypt_update(&ctx, NULL, 0, NULL, &outlen) != -1
|| sm4_cfb_encrypt_finish(NULL, out, &outlen) != -1
|| sm4_cfb_encrypt_finish(&ctx, NULL, &outlen) != -1
|| sm4_cfb_encrypt_finish(&ctx, out, NULL) != -1) {
error_print();
return -1;
}
if (sm4_cfb_decrypt_init(&ctx, SM4_CFB_128, key, iv) != 1
|| sm4_cfb_decrypt_update(NULL, in, sizeof(in), out, &outlen) != -1
|| sm4_cfb_decrypt_update(&ctx, NULL, 1, out, &outlen) != -1
|| sm4_cfb_decrypt_update(&ctx, in, sizeof(in), NULL, &outlen) != -1
|| sm4_cfb_decrypt_update(&ctx, in, sizeof(in), out, NULL) != -1
|| sm4_cfb_decrypt_update(&ctx, NULL, 0, out, &outlen) != 1
|| outlen != 0
|| sm4_cfb_decrypt_update(&ctx, NULL, 0, NULL, &outlen) != -1
|| sm4_cfb_decrypt_finish(NULL, out, &outlen) != -1
|| sm4_cfb_decrypt_finish(&ctx, NULL, &outlen) != -1
|| sm4_cfb_decrypt_finish(&ctx, out, NULL) != -1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
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;
if (test_sm4_cfb_args() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err: