mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-06-19 11:23:38 +08:00
Add SM4-GCM tests from Wycheproof
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
#include <gmssl/sm4.h>
|
||||
#include <gmssl/error.h>
|
||||
#include <gmssl/rand.h>
|
||||
#include "sm4_gcmtest.h"
|
||||
|
||||
|
||||
static int test_sm4_gcm(void)
|
||||
@@ -313,6 +314,81 @@ static int test_sm4_gcm_ctx(void)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int test_sm4_gcm_has_flag(const TEST_SM4_GCM_VECTOR *tv, const char *flag)
|
||||
{
|
||||
return strstr(tv->flags, flag) != NULL;
|
||||
}
|
||||
|
||||
static int test_sm4_gcm_wycheproof(void)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(test_sm4_gcm_vectors)/sizeof(test_sm4_gcm_vectors[0]); i++) {
|
||||
const TEST_SM4_GCM_VECTOR *tv = &test_sm4_gcm_vectors[i];
|
||||
SM4_KEY sm4_key;
|
||||
uint8_t key[16];
|
||||
uint8_t iv[257];
|
||||
uint8_t aad[513];
|
||||
uint8_t msg[513];
|
||||
uint8_t ct[513];
|
||||
uint8_t tag[16];
|
||||
uint8_t out[513];
|
||||
uint8_t dec[513];
|
||||
uint8_t mac[16];
|
||||
size_t keylen, ivlen, aadlen, msglen, ctlen, taglen;
|
||||
int enc_ret, dec_ret;
|
||||
|
||||
if (hex_to_bytes(tv->key, strlen(tv->key), key, &keylen) != 1
|
||||
|| hex_to_bytes(tv->iv, strlen(tv->iv), iv, &ivlen) != 1
|
||||
|| hex_to_bytes(tv->aad, strlen(tv->aad), aad, &aadlen) != 1
|
||||
|| hex_to_bytes(tv->msg, strlen(tv->msg), msg, &msglen) != 1
|
||||
|| hex_to_bytes(tv->ct, strlen(tv->ct), ct, &ctlen) != 1
|
||||
|| hex_to_bytes(tv->tag, strlen(tv->tag), tag, &taglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (keylen != SM4_KEY_SIZE) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (taglen > sizeof(mac)
|
||||
|| msglen > sizeof(out)
|
||||
|| ctlen > sizeof(dec)) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
sm4_set_encrypt_key(&sm4_key, key);
|
||||
enc_ret = sm4_gcm_encrypt(&sm4_key, iv, ivlen, aad, aadlen, msg, msglen, out, taglen, mac);
|
||||
dec_ret = sm4_gcm_decrypt(&sm4_key, iv, ivlen, aad, aadlen, ct, ctlen, tag, taglen, dec);
|
||||
|
||||
if (tv->result == TEST_RESULT_VALID) {
|
||||
if (test_sm4_gcm_has_flag(tv, "LongIv") && ivlen > SM4_GCM_MAX_IV_SIZE
|
||||
&& enc_ret != 1 && dec_ret != 1) {
|
||||
continue;
|
||||
}
|
||||
if (enc_ret != 1 || dec_ret != 1
|
||||
|| ctlen != msglen
|
||||
|| memcmp(out, ct, ctlen) != 0
|
||||
|| memcmp(mac, tag, taglen) != 0
|
||||
|| memcmp(dec, msg, msglen) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (dec_ret == 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
fprintf(stderr, " error output above is part of the negative test\n");
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int speed_sm4_gcm_encrypt(void)
|
||||
{
|
||||
SM4_KEY sm4_key;
|
||||
@@ -348,6 +424,7 @@ int main(void)
|
||||
if (test_sm4_gcm_gbt36624_1() != 1) goto err;
|
||||
if (test_sm4_gcm_gbt36624_2() != 1) goto err;
|
||||
if (test_sm4_gcm_ctx() != 1) goto err;
|
||||
if (test_sm4_gcm_wycheproof() != 1) goto err;
|
||||
#if ENABLE_TEST_SPEED
|
||||
if (speed_sm4_gcm_encrypt() != 1) goto err;
|
||||
#endif
|
||||
|
||||
1291
tests/sm4_gcmtest.h
Normal file
1291
tests/sm4_gcmtest.h
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user