Update tests use v3 API

This commit is contained in:
Zhi Guan
2022-02-27 19:11:51 +08:00
parent cadbb542e3
commit b885a07ea2
6 changed files with 29 additions and 29 deletions

View File

@@ -194,15 +194,16 @@ int test_hkdf(void)
size_t L;
uint8_t buf[512];
size_t buflen;
size_t len;
for (i = 0; i < sizeof(hkdf_tests)/sizeof(hkdf_tests[0]); i++) {
digest = digest_from_name(hkdf_tests[i].algor);
hex2bin(hkdf_tests[i].ikm, strlen(hkdf_tests[i].ikm), ikm);
hex2bin(hkdf_tests[i].salt, strlen(hkdf_tests[i].salt), salt);
hex2bin(hkdf_tests[i].info, strlen(hkdf_tests[i].info), info);
hex2bin(hkdf_tests[i].prk, strlen(hkdf_tests[i].prk), prk);
hex2bin(hkdf_tests[i].okm, strlen(hkdf_tests[i].okm), okm);
hex_to_bytes(hkdf_tests[i].ikm, strlen(hkdf_tests[i].ikm), ikm, &len);
hex_to_bytes(hkdf_tests[i].salt, strlen(hkdf_tests[i].salt), salt, &len);
hex_to_bytes(hkdf_tests[i].info, strlen(hkdf_tests[i].info), info, &len);
hex_to_bytes(hkdf_tests[i].prk, strlen(hkdf_tests[i].prk), prk, &len);
hex_to_bytes(hkdf_tests[i].okm, strlen(hkdf_tests[i].okm), okm, &len);
ikmlen = strlen(hkdf_tests[i].ikm)/2;
saltlen = strlen(hkdf_tests[i].salt)/2;
infolen = strlen(hkdf_tests[i].info)/2;

View File

@@ -128,9 +128,9 @@ int test_hmac(const DIGEST *digest, const char *key_hex, const char *data_hex, c
uint8_t buf[64];
size_t len;
hex2bin(key_hex, strlen(key_hex), key);
hex2bin(data_hex, strlen(data_hex), data);
hex2bin(hmac_hex, strlen(hmac_hex), hmac);
hex_to_bytes(key_hex, strlen(key_hex), key, &len);
hex_to_bytes(data_hex, strlen(data_hex), data, &len);
hex_to_bytes(hmac_hex, strlen(hmac_hex), hmac, &len);
hmac_init(&ctx, digest, key, sizeof(key));
hmac_update(&ctx, data, sizeof(data));

View File

@@ -65,7 +65,7 @@ static int test_sm2_point_octets(void)
// compress
for (i = 0; i < 8; i++) {
uint8_t buf[33];
sm2_keygen(&sm2_key);
sm2_key_generate(&sm2_key);
sm2_point_to_compressed_octets(&sm2_key.public_key, buf);
if (sm2_point_from_octets(&point, buf, sizeof(buf)) != 1) {
error_print();
@@ -82,7 +82,7 @@ static int test_sm2_point_octets(void)
// uncompress
for (i = 0; i < 8; i++) {
uint8_t buf[65];
sm2_keygen(&sm2_key);
sm2_key_generate(&sm2_key);
sm2_point_to_uncompressed_octets(&sm2_key.public_key, buf);
if (sm2_point_from_octets(&point, buf, sizeof(buf)) != 1) {
error_print();
@@ -111,7 +111,7 @@ static int test_sm2_private_key(void)
size_t len = 0;
sm2_keygen(&sm2_key);
sm2_key_generate(&sm2_key);
if (sm2_private_key_to_der(&sm2_key, &p, &len) != 1) {
error_print();
@@ -145,7 +145,7 @@ static int test_sm2_public_key_info(void)
const uint8_t *cp = buf;
size_t len = 0;
sm2_keygen(&sm2_key);
sm2_key_generate(&sm2_key);
if (sm2_public_key_info_to_der(&sm2_key, &p, &len) != 1) {
error_print();

View File

@@ -75,8 +75,8 @@ static int test_sm2_point(void)
sm2_point_mul_generator(&P, k);
printf("k * G :\n");
sm2_point_print(stdout, &P, 0, 2);
sm2_point_print(stdout, 0, 4, "k * G", &P);
sm2_point_to_compressed_octets(&P, buf);
for (i = 0; i < 33; i++) printf("%02x", buf[i]); printf("\n");
@@ -110,12 +110,11 @@ static int test_sm2_do_encrypt(void)
size_t plainlen = 0;
int r = 0;
sm2_keygen(&key);
sm2_key_generate(&key);
sm2_do_encrypt(&key, plaintext, sizeof(plaintext), ciphertext);
printf("ciphertext:\n");
sm2_ciphertext_print(stdout, ciphertext, 0, 2);
//sm2_ciphertext_print(stdout, 0, 4, "ciphertext", ciphertext);
sm2_do_decrypt(&key, ciphertext, plainbuf, &plainlen);
@@ -133,17 +132,16 @@ static int test_sm2_sign(void)
int i;
int r;
sm2_keygen(&key);
sm2_key_print(stdout, &key, 0, 0);
sm2_key_generate(&key);
sm2_key_print(stdout, 0, 4, "sm2_key", &key);
sm2_sign_init(&ctx, &key, SM2_DEFAULT_ID);
sm2_sign_init(&ctx, &key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID));
sm2_sign_update(&ctx, msg, sizeof(msg));
sm2_sign_finish(&ctx, sig, &siglen);
printf("signature:\n");
sm2_print_signature(stdout, sig, siglen, 0, 2);
sm2_signature_print(stdout, 0, 4, "signature", sig, siglen);
sm2_verify_init(&ctx, &key, SM2_DEFAULT_ID);
sm2_verify_init(&ctx, &key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID));
sm2_verify_update(&ctx, msg, sizeof(msg));
r = sm2_verify_finish(&ctx, sig, siglen);
printf("verify %s\n", r > 0 ? "success" : "failed");
@@ -153,7 +151,7 @@ static int test_sm2_sign(void)
int main(void)
{
sm2_algo_selftest();
sm2_selftest();
//test_sm2_point();
//test_sm2_sign();

View File

@@ -54,7 +54,7 @@
#include <gmssl/rand.h>
#include <gmssl/error.h>
#if 0
static int test_x509_validity(void)
{
int err = 0;
@@ -328,3 +328,4 @@ int main(void)
//test_x509_extensions();
return 1;
}
#endif

View File

@@ -85,9 +85,9 @@ int zuc_test(void)
};
for (i = 0; i < 3; i++) {
ZUC_KEY zuc = {{0}};
ZUC_STATE zuc = {{0}};
uint32_t buf[3] = {0};
zuc_set_key(&zuc, key[i], iv[i]);
zuc_init(&zuc, key[i], iv[i]);
zuc_generate_keystream(&zuc, 2, buf);
if (buf[0] != ciphertext[i][0] || buf[1] != ciphertext[i][1]) {
fprintf(stderr, "error generating ZUC key stream on test vector %d\n", i);
@@ -358,10 +358,10 @@ int zuc256_test(void)
};
for (i = 0; i < sizeof(key)/sizeof(key[0]); i++) {
ZUC_KEY zuc_key;
ZUC_STATE zuc_key;
uint32_t buf[20] = {0};
zuc256_set_key(&zuc_key, key[i], iv[i]);
zuc256_init(&zuc_key, key[i], iv[i]);
zuc_generate_keystream(&zuc_key, 20, buf);
if (memcmp(buf, ciphertext[i], 20) != 0) {