mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-06 16:36:16 +08:00
x509_key all tests passed
This commit is contained in:
@@ -367,6 +367,8 @@ static int test_cms_signer_info_sign(void)
|
||||
const uint8_t *d;
|
||||
size_t dlen;
|
||||
|
||||
int algor = OID_ec_public_key;
|
||||
int algor_param = OID_sm2;
|
||||
X509_KEY x509_key;
|
||||
uint8_t serial_buf[20];
|
||||
uint8_t name[256];
|
||||
@@ -386,7 +388,7 @@ static int test_cms_signer_info_sign(void)
|
||||
const uint8_t *unauth_attrs;
|
||||
size_t serial_len, issuer_len, auth_attrs_len, unauth_attrs_len;
|
||||
|
||||
if (x509_key_generate(&x509_key, OID_ec_public_key, OID_sm2) != 1
|
||||
if (x509_key_generate(&x509_key, algor, &algor_param, sizeof(algor_param)) != 1
|
||||
|| rand_bytes(serial_buf, sizeof(serial_buf)) != 1
|
||||
|| x509_name_set(name, &namelen, sizeof(name), "CN", "Beijing", "Haidian", "PKU", "CS", "Alice") != 1
|
||||
|| time(¬_before) == -1
|
||||
@@ -456,13 +458,16 @@ static int test_cms_signer_infos(void)
|
||||
size_t signer_infos_len = 0;
|
||||
|
||||
SM3_CTX sm3_ctx;
|
||||
|
||||
int algor = OID_ec_public_key;
|
||||
int algor_param = OID_sm2;
|
||||
X509_KEY x509_key;
|
||||
|
||||
uint8_t issuer_buf[256];
|
||||
size_t issuer_len;
|
||||
uint8_t serial_buf[20];
|
||||
|
||||
if (x509_key_generate(&x509_key, OID_ec_public_key, OID_sm2) != 1) {
|
||||
if (x509_key_generate(&x509_key, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -563,7 +568,7 @@ static int test_cms_signed_data(void)
|
||||
const uint8_t *d;
|
||||
size_t dlen;
|
||||
|
||||
if (x509_key_generate(&x509_key, algor, algor_param) != 1) {
|
||||
if (x509_key_generate(&x509_key, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -683,7 +688,7 @@ static int test_cms_recipient_info(void)
|
||||
uint8_t out[sizeof(in)];
|
||||
size_t outlen;
|
||||
|
||||
if (x509_key_generate(&x509_key, algor, algor_param) != 1) {
|
||||
if (x509_key_generate(&x509_key, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -795,7 +800,7 @@ int test_cms_enveloped_data(void)
|
||||
p = certs;
|
||||
certslen = 0;
|
||||
|
||||
if (x509_key_generate(&x509_key1, algor, algor_param) != 1) {
|
||||
if (x509_key_generate(&x509_key1, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -816,7 +821,7 @@ int test_cms_enveloped_data(void)
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (x509_key_generate(&x509_key2, algor, algor_param) != 1) {
|
||||
if (x509_key_generate(&x509_key2, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -938,7 +943,7 @@ static int test_cms_key_agreement_info(void)
|
||||
size_t idlen;
|
||||
|
||||
p = cert;
|
||||
if (x509_key_generate(&x509_key, algor, algor_param) != 1) {
|
||||
if (x509_key_generate(&x509_key, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -639,6 +639,8 @@ static int test_sphincs_sign_update(void)
|
||||
SPHINCS_SIGN_CTX ctx;
|
||||
SPHINCS_SIGNATURE sig;
|
||||
uint8_t msg[100] = { 1,2,3 };
|
||||
uint8_t sigbuf[SPHINCS_SIGNATURE_SIZE];
|
||||
size_t siglen;
|
||||
|
||||
if (sphincs_key_generate(&key) != 1) {
|
||||
error_print();
|
||||
@@ -678,6 +680,51 @@ static int test_sphincs_sign_update(void)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// sign/verify sigbuf
|
||||
|
||||
if (sphincs_sign_init(&ctx, &key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sphincs_sign_prepare(&ctx, msg, sizeof(msg)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sphincs_sign_update(&ctx, msg, sizeof(msg)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sphincs_sign_finish(&ctx, sigbuf, &siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// verify
|
||||
|
||||
if (sphincs_verify_init(&ctx, &key, sigbuf, siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sphincs_verify_update(&ctx, msg, sizeof(msg)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (sphincs_verify_finish(&ctx) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -787,6 +787,8 @@ static int test_x509_cert_with_exts(void)
|
||||
uint8_t name[256];
|
||||
size_t namelen;
|
||||
time_t not_before, not_after;
|
||||
int algor = OID_ec_public_key;
|
||||
int algor_param = OID_sm2;
|
||||
X509_KEY x509_key;
|
||||
uint8_t uniq_id[32];
|
||||
uint8_t exts[512];
|
||||
@@ -800,7 +802,7 @@ static int test_x509_cert_with_exts(void)
|
||||
x509_validity_add_days(¬_after, not_before, 365);
|
||||
|
||||
|
||||
if (x509_key_generate(&x509_key, OID_ec_public_key, OID_sm2) != 1) {
|
||||
if (x509_key_generate(&x509_key, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -15,99 +15,31 @@
|
||||
#include <gmssl/x509.h>
|
||||
#include <gmssl/rand.h>
|
||||
#include <gmssl/error.h>
|
||||
#include <gmssl/x509_alg.h>
|
||||
#include <gmssl/x509_key.h>
|
||||
|
||||
|
||||
int lms_types[] = {
|
||||
LMS_HASH256_M32_H5,
|
||||
LMS_HASH256_M32_H5,
|
||||
LMS_HASH256_M32_H5,
|
||||
};
|
||||
|
||||
static int test_x509_algor_param_from_lms_types(void)
|
||||
{
|
||||
int lms_types1[] = {
|
||||
LMS_HASH256_M32_H5,
|
||||
LMS_HASH256_M32_H10,
|
||||
LMS_HASH256_M32_H15,
|
||||
LMS_HASH256_M32_H20,
|
||||
LMS_HASH256_M32_H25,
|
||||
};
|
||||
int hss_algor_param1 = 624485;
|
||||
|
||||
int lms_types2[] = {
|
||||
LMS_HASH256_M32_H5,
|
||||
LMS_HASH256_M32_H5,
|
||||
LMS_HASH256_M32_H5,
|
||||
};
|
||||
int hss_algor_param2 = 1365;
|
||||
|
||||
int algor_param1;
|
||||
int algor_param2;
|
||||
int lms_types[5];
|
||||
size_t num;
|
||||
|
||||
if (x509_algor_param_from_lms_types(&algor_param1, lms_types1, sizeof(lms_types1)/sizeof(lms_types1[0])) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (algor_param1 != hss_algor_param1) {
|
||||
format_print(stderr, 0, 4, "hss_algor_param: %d\n", algor_param1);
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_algor_param_to_lms_types(algor_param1, lms_types, &num) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (num != sizeof(lms_types1)/sizeof(lms_types1[0])) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(lms_types, lms_types1, sizeof(lms_types1)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
if (x509_algor_param_from_lms_types(&algor_param2, lms_types2, sizeof(lms_types2)/sizeof(lms_types2[0])) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (algor_param2 != hss_algor_param2) {
|
||||
format_print(stderr, 0, 4, "hss_algor_param: %d\n", algor_param2);
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_algor_param_to_lms_types(algor_param2, lms_types, &num) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (num != sizeof(lms_types2)/sizeof(lms_types2[0])) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(lms_types, lms_types2, sizeof(lms_types2)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
X509_KEY x509_keys[7];
|
||||
|
||||
|
||||
// 这个也要挪到外面,才能判断某个x509_key的类型
|
||||
struct {
|
||||
int algor;
|
||||
int algor_param;
|
||||
} tests[] = {
|
||||
{ OID_ec_public_key, OID_sm2 },
|
||||
{ OID_ec_public_key, OID_secp256r1 },
|
||||
{ OID_lms_hashsig, LMS_HASH256_M32_H5 },
|
||||
{ OID_hss_lms_hashsig, 1365 },
|
||||
{ OID_xmss_hashsig, XMSS_HASH256_10_256 },
|
||||
{ OID_xmssmt_hashsig, XMSSMT_HASH256_20_4_256 },
|
||||
{ OID_sphincs_hashsig, OID_undef },
|
||||
};
|
||||
struct {
|
||||
int algor;
|
||||
int algor_param;
|
||||
} tests[] = {
|
||||
{ OID_ec_public_key, OID_sm2 },
|
||||
{ OID_ec_public_key, OID_secp256r1 },
|
||||
{ OID_lms_hashsig, LMS_HASH256_M32_H5 },
|
||||
{ OID_hss_lms_hashsig, OID_undef }, // use lms_types[]
|
||||
{ OID_xmss_hashsig, XMSS_HASH256_10_256 },
|
||||
{ OID_xmssmt_hashsig, XMSSMT_HASH256_20_4_256 },
|
||||
{ OID_sphincs_hashsig, OID_undef },
|
||||
{ OID_kyber_kem, OID_undef },
|
||||
};
|
||||
|
||||
X509_KEY x509_keys[sizeof(tests)/sizeof(tests[0])];
|
||||
|
||||
|
||||
static int test_x509_key_generate(void)
|
||||
@@ -115,15 +47,29 @@ static int test_x509_key_generate(void)
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
if (x509_key_generate(&x509_keys[i], tests[i].algor, tests[i].algor_param) != 1) {
|
||||
void *param = NULL;
|
||||
size_t paramlen = 0;
|
||||
|
||||
switch (tests[i].algor) {
|
||||
case OID_hss_lms_hashsig:
|
||||
param = lms_types;
|
||||
paramlen = sizeof(lms_types);
|
||||
break;
|
||||
case OID_sphincs_hashsig:
|
||||
case OID_kyber_kem:
|
||||
param = NULL;
|
||||
paramlen = 0;
|
||||
break;
|
||||
default:
|
||||
param = &tests[i].algor_param;
|
||||
paramlen = sizeof(tests[i].algor_param);
|
||||
}
|
||||
if (x509_key_generate(&x509_keys[i], tests[i].algor, param, paramlen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// 这个也没有实现啊!
|
||||
// x509_private_key_print(stderr, 0, 4, "private_key", &x509_keys[i]);
|
||||
x509_public_key_print(stderr, 0, 4, "private_key", &x509_keys[i]);
|
||||
|
||||
//x509_private_key_print(stderr, 0, 4, "private_key", &x509_keys[i]);
|
||||
//x509_public_key_print(stderr, 0, 4, "private_key", &x509_keys[i]);
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
@@ -132,31 +78,26 @@ static int test_x509_key_generate(void)
|
||||
|
||||
static int test_x509_public_key_to_bytes(void)
|
||||
{
|
||||
int i;
|
||||
uint8_t buf[128];
|
||||
X509_KEY key;
|
||||
uint8_t buf[1568]; // kyber-1024
|
||||
uint8_t *p;
|
||||
size_t len;
|
||||
|
||||
uint8_t dgst[32];
|
||||
int i;
|
||||
|
||||
X509_KEY key;
|
||||
|
||||
for (i = 0; i < 7; i++) {
|
||||
//format_print(stderr, 0, 4, "public_key_to_bytes size\n");
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
p = buf;
|
||||
len = 0;
|
||||
if (x509_public_key_to_bytes(&x509_keys[i], &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "public_key_bytes: %zu\n", len);
|
||||
|
||||
//format_print(stderr, 0, 4, "%s: %zu\n", x509_public_key_algor_name(tests[i].algor), len);
|
||||
if (x509_public_key_digest(&x509_keys[i], dgst) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_bytes(stderr, 0, 4, "dgst", dgst, 32);
|
||||
|
||||
// 居然没有public_key_from_bytes
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
@@ -166,27 +107,27 @@ static int test_x509_public_key_to_bytes(void)
|
||||
static int test_x509_public_key_info_to_der(void)
|
||||
{
|
||||
X509_KEY key;
|
||||
uint8_t buf[50240];
|
||||
uint8_t buf[2048];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < sizeof(x509_keys)/sizeof(x509_keys[0]); i++) {
|
||||
//format_print(stderr, 0, 4, "public_key_info_to_bytes size\n");
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
const uint8_t *cp = buf;
|
||||
uint8_t *p = buf;
|
||||
size_t len = 0;
|
||||
|
||||
fprintf(stderr, "%d: algor = %d param = %d\n", i, x509_keys[i].algor, x509_keys[i].algor_param);
|
||||
|
||||
if (x509_public_key_info_to_der(&x509_keys[i], &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
format_print(stderr, 0, 4, "public_key_der_size: %zu\n", len);
|
||||
//format_print(stderr, 0, 8, "%s: %zu\n", x509_public_key_algor_name(tests[i].algor), len);
|
||||
|
||||
if (x509_public_key_info_from_der(&key, &cp, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (len) {
|
||||
fprintf(stderr, "len = %zu\n", len);
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -204,21 +145,79 @@ static int test_x509_public_key_info_to_der(void)
|
||||
static int test_ec_private_key_to_der(void)
|
||||
{
|
||||
X509_KEY key;
|
||||
uint8_t buf[1024];
|
||||
uint8_t buf[512];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 2; i++) {
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]) && tests[i].algor == OID_ec_public_key; i++) {
|
||||
const uint8_t *cp = buf;
|
||||
uint8_t *p = buf;
|
||||
size_t len = 0;
|
||||
// 目前底层的asn1功能不支持这两个不编码,需要仔细看看是怎么回事,explicit的编码是如何实现的
|
||||
int encode_params = 1; // X509_ENCODE_EC_PRIVATE_KEY_PARAMS;
|
||||
int encode_pubkey = 1; //X509_ENCODE_EC_PRIVATE_KEY_PUBKEY;
|
||||
int encode_params;
|
||||
int encode_pubkey;
|
||||
|
||||
// test 1
|
||||
encode_params = 0;
|
||||
encode_pubkey = 0;
|
||||
if (ec_private_key_to_der(&x509_keys[i], encode_params, encode_pubkey, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
//format_print(stderr, 0, 0, "ECPrivateKey encode_params = %d, encode_pubkey = %d\n", encode_params, encode_pubkey);
|
||||
//format_bytes(stderr, 0, 0, "ECPrivateKey", buf, len);
|
||||
if (ec_private_key_from_der(&key, tests[i].algor_param, &cp, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// test 2
|
||||
encode_params = 0;
|
||||
encode_pubkey = 1;
|
||||
if (ec_private_key_to_der(&x509_keys[i], encode_params, encode_pubkey, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
//format_print(stderr, 0, 0, "ECPrivateKey encode_params = %d, encode_pubkey = %d\n", encode_params, encode_pubkey);
|
||||
//format_bytes(stderr, 0, 0, "ECPrivateKey", buf, len);
|
||||
if (ec_private_key_from_der(&key, tests[i].algor_param, &cp, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// test 3
|
||||
encode_params = 1;
|
||||
encode_pubkey = 0;
|
||||
if (ec_private_key_to_der(&x509_keys[i], encode_params, encode_pubkey, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
//format_print(stderr, 0, 0, "ECPrivateKey encode_params = %d, encode_pubkey = %d\n", encode_params, encode_pubkey);
|
||||
//format_bytes(stderr, 0, 0, "ECPrivateKey", buf, len);
|
||||
if (ec_private_key_from_der(&key, tests[i].algor_param, &cp, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// test 4
|
||||
encode_params = 1;
|
||||
encode_pubkey = 1;
|
||||
if (ec_private_key_to_der(&x509_keys[i], encode_params, encode_pubkey, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
//format_print(stderr, 0, 0, "ECPrivateKey encode_params = %d, encode_pubkey = %d\n", encode_params, encode_pubkey);
|
||||
//format_bytes(stderr, 0, 0, "ECPrivateKey", buf, len);
|
||||
if (ec_private_key_from_der(&key, tests[i].algor_param, &cp, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
@@ -236,43 +235,36 @@ static int test_ec_private_key_to_der(void)
|
||||
static int test_x509_private_key_info_to_der(void)
|
||||
{
|
||||
X509_KEY key;
|
||||
uint8_t buf[1024];
|
||||
uint8_t buf[512];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
if (tests[i].algor == OID_ec_public_key) {
|
||||
const uint8_t *cp = buf;
|
||||
uint8_t *p = buf;
|
||||
size_t len = 0;
|
||||
const uint8_t *attrs;
|
||||
size_t attrslen;
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]) && tests[i].algor == OID_ec_public_key; i++) {
|
||||
const uint8_t *cp = buf;
|
||||
uint8_t *p = buf;
|
||||
size_t len = 0;
|
||||
const uint8_t *attrs;
|
||||
size_t attrslen;
|
||||
|
||||
if (x509_private_key_info_to_der(&x509_keys[i], &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_private_key_info_from_der(&key, &attrs, &attrslen, &cp, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (len) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_private_key_info_to_der(&x509_keys[i], &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_private_key_info_from_der(&key, &attrs, &attrslen, &cp, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (len) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
static int test_x509_private_key_info_encrypt_to_der(void)
|
||||
@@ -282,30 +274,28 @@ static int test_x509_private_key_info_encrypt_to_der(void)
|
||||
uint8_t buf[1024];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
if (tests[i].algor == OID_ec_public_key) {
|
||||
const uint8_t *cp = buf;
|
||||
uint8_t *p = buf;
|
||||
size_t len = 0;
|
||||
const uint8_t *attrs;
|
||||
size_t attrslen;
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]) && tests[i].algor == OID_ec_public_key; i++) {
|
||||
const uint8_t *cp = buf;
|
||||
uint8_t *p = buf;
|
||||
size_t len = 0;
|
||||
const uint8_t *attrs;
|
||||
size_t attrslen;
|
||||
|
||||
if (x509_private_key_info_encrypt_to_der(&x509_keys[i], pass, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_private_key_info_decrypt_from_der(&key, &attrs, &attrslen, pass, &cp, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (len) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_private_key_info_encrypt_to_der(&x509_keys[i], pass, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_private_key_info_decrypt_from_der(&key, &attrs, &attrslen, pass, &cp, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (len) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -313,7 +303,6 @@ static int test_x509_private_key_info_encrypt_to_der(void)
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int test_x509_private_key_info_encrypt_to_pem(void)
|
||||
{
|
||||
const char *pass = "P@ssw0rd";
|
||||
@@ -323,41 +312,186 @@ static int test_x509_private_key_info_encrypt_to_pem(void)
|
||||
int i;
|
||||
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]) && tests[i].algor == OID_ec_public_key; i++) {
|
||||
const uint8_t *cp = buf;
|
||||
uint8_t *p = buf;
|
||||
size_t len = 0;
|
||||
const uint8_t *attrs;
|
||||
size_t attrslen;
|
||||
|
||||
if (!(fp = fopen("test_x509_private_key_info_encrypt_to_pem.pem", "w"))) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_private_key_info_encrypt_to_pem(&x509_keys[i], pass, fp) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
if (!(fp = fopen("test_x509_private_key_info_encrypt_to_pem.pem", "r"))) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_private_key_info_decrypt_from_pem(&key, &attrs, &attrslen, pass, fp) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
fclose(fp);
|
||||
if (len) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_x509_sign(void)
|
||||
{
|
||||
size_t i;
|
||||
X509_SIGN_CTX sign_ctx;
|
||||
void *args = NULL;
|
||||
size_t argslen = 0;
|
||||
uint8_t msg[66];
|
||||
uint8_t sig[40969];
|
||||
size_t siglen;
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
if (tests[i].algor == OID_ec_public_key) {
|
||||
const uint8_t *cp = buf;
|
||||
uint8_t *p = buf;
|
||||
size_t len = 0;
|
||||
const uint8_t *attrs;
|
||||
size_t attrslen;
|
||||
if (tests[i].algor == OID_kyber_kem) {
|
||||
continue;
|
||||
}
|
||||
//format_print(stderr, 0, 4, "%s\n", x509_public_key_algor_name(tests[i].algor));
|
||||
if (x509_sign_init(&sign_ctx, &x509_keys[i], args, argslen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_sign(&sign_ctx, msg, sizeof(msg), sig, &siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_verify_init(&sign_ctx, &x509_keys[i], args, argslen, sig, siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_verify(&sign_ctx, msg, sizeof(msg)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!(fp = fopen("test_x509_private_key_info_encrypt_to_pem.pem", "w"))) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_private_key_info_encrypt_to_pem(&x509_keys[i], pass, fp) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
fclose(fp);
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!(fp = fopen("test_x509_private_key_info_encrypt_to_pem.pem", "r"))) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_private_key_info_decrypt_from_pem(&key, &attrs, &attrslen, pass, fp) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
fclose(fp);
|
||||
if (len) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_public_key_equ(&key, &x509_keys[i]) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
static int test_x509_key_exchange(void)
|
||||
{
|
||||
X509_KEY key;
|
||||
uint8_t point1[65];
|
||||
uint8_t point2[65];
|
||||
uint8_t share1[32];
|
||||
uint8_t share2[32];
|
||||
uint8_t *p;
|
||||
size_t len;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
if (tests[i].algor != OID_ec_public_key) {
|
||||
continue;
|
||||
}
|
||||
if (x509_key_generate(&key, tests[i].algor, &tests[i].algor_param, sizeof(tests[i].algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// export public key 1
|
||||
p = point1;
|
||||
len = 0;
|
||||
if (x509_public_key_to_bytes(&key, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (len != sizeof(point1)) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// export public key 2
|
||||
p = point2;
|
||||
len = 0;
|
||||
if (x509_public_key_to_bytes(&x509_keys[i], &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (len != sizeof(point2)) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// key exchange 1
|
||||
if (x509_key_exchange(&key, point2, sizeof(point2), share1, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (len != sizeof(share1)) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// key exchange 2
|
||||
if (x509_key_exchange(&x509_keys[i], point1, sizeof(point1), share2, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (len != sizeof(share2)) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// share secrets equal
|
||||
if (memcmp(share1, share2, sizeof(share1)) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_x509_kem(void)
|
||||
{
|
||||
uint8_t ciphertext[sizeof(KYBER_CIPHERTEXT)];
|
||||
size_t ciphertext_len;
|
||||
uint8_t secret1[32];
|
||||
uint8_t secret2[32];
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
if (tests[i].algor != OID_kyber_kem) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (x509_key_encapsulate(&x509_keys[i], ciphertext, &ciphertext_len, secret1) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (ciphertext_len != sizeof(ciphertext)) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_key_decapsulate(&x509_keys[i], ciphertext, ciphertext_len, secret2) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(secret1, secret2, 32) != 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -366,42 +500,18 @@ static int test_x509_private_key_info_encrypt_to_pem(void)
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 首先某些头文件中的函数并没有实现,很奇怪!
|
||||
|
||||
// 然后是某些数据长度(公钥、签名之类)还没有计算具体的值,因此没有办法准备最大的缓冲去
|
||||
|
||||
// 应该把密钥生成放到最外面,这样只需要生成一次就可以了
|
||||
|
||||
// X509_KEY还不支持Kyber
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if (test_x509_key_generate() != 1) goto err;
|
||||
if (test_x509_algor_param_from_lms_types() != 1) goto err;
|
||||
if (test_x509_public_key_to_bytes() != 1) goto err;
|
||||
if (test_x509_public_key_info_to_der() != 1) goto err;
|
||||
if (test_ec_private_key_to_der() != 1) goto err;
|
||||
if (test_x509_private_key_info_to_der() != 1) goto err;
|
||||
if (test_x509_private_key_info_encrypt_to_der() != 1) goto err;
|
||||
if (test_x509_private_key_info_encrypt_to_pem() != 1) goto err;
|
||||
if (test_x509_sign() != 1) goto err;
|
||||
if (test_x509_key_exchange() != 1) goto err;
|
||||
if (test_x509_kem() != 1) goto err;
|
||||
|
||||
printf("%s all tests passed!\n", __FILE__);
|
||||
return 0;
|
||||
|
||||
@@ -43,7 +43,7 @@ static int test_x509_request_info(void)
|
||||
X509_KEY pub_key;
|
||||
const uint8_t *attrs;
|
||||
|
||||
if (x509_key_generate(&x509_key, algor, algor_param) != 1) {
|
||||
if (x509_key_generate(&x509_key, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -152,7 +152,7 @@ static int test_x509_req(void)
|
||||
uint8_t *p = req;
|
||||
size_t reqlen = 0;
|
||||
|
||||
if (x509_key_generate(&x509_key, algor, algor_param) != 1) {
|
||||
if (x509_key_generate(&x509_key, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -230,7 +230,7 @@ static int test_x509_public_key_info(void)
|
||||
const uint8_t *d;
|
||||
size_t dlen;
|
||||
|
||||
if (x509_key_generate(&x509_key, algor, algor_param) != 1) {
|
||||
if (x509_key_generate(&x509_key, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -242,7 +242,7 @@ static int test_x509_public_key_info(void)
|
||||
}
|
||||
x509_public_key_info_print(stdout, 0, 4, "PublicKeyInfo", d, dlen);
|
||||
|
||||
if (x509_key_generate(&x509_key, algor, algor_param) != 1) {
|
||||
if (x509_key_generate(&x509_key, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -296,7 +296,7 @@ static int test_x509_tbs_cert(void)
|
||||
x509_validity_add_days(¬_after, not_before, 365);
|
||||
set_x509_name(subject, &subject_len, sizeof(subject));
|
||||
|
||||
if (x509_key_generate(&x509_key, algor, algor_param) != 1) {
|
||||
if (x509_key_generate(&x509_key, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -374,7 +374,7 @@ static int test_x509_cert(void)
|
||||
x509_validity_add_days(¬_after, not_before, 365);
|
||||
set_x509_name(subject, &subject_len, sizeof(subject));
|
||||
|
||||
if (x509_key_generate(&x509_key, algor, algor_param) != 1) {
|
||||
if (x509_key_generate(&x509_key, algor, &algor_param, sizeof(algor_param)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -549,12 +549,12 @@ static int test_xmss_sign(void)
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int test_xmss_sign_init(void)
|
||||
static int test_xmss_sign_update(void)
|
||||
{
|
||||
uint32_t xmss_type = XMSS_HASH256_10_256;
|
||||
XMSS_KEY key;
|
||||
XMSS_SIGN_CTX sign_ctx;
|
||||
XMSS_SIGNATURE signature;
|
||||
uint8_t sig[XMSS_SIGNATURE_MAX_SIZE];
|
||||
size_t siglen;
|
||||
uint8_t msg[100] = {0};
|
||||
@@ -573,12 +573,14 @@ static int test_xmss_sign_init(void)
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (xmss_sign_finish(&sign_ctx, sig, &siglen) != 1) {
|
||||
if (xmss_sign_finish_ex(&sign_ctx, &signature) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
xmss_signature_print_ex(stderr, (int)xmss_type, 4, "xmss_signature", &signature);
|
||||
|
||||
if (xmss_verify_init(&sign_ctx, &key, sig, siglen) != 1) {
|
||||
|
||||
if (xmss_verify_init_ex(&sign_ctx, &key, &signature) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
@@ -1057,7 +1059,8 @@ static int test_xmssmt_sign_update(void)
|
||||
XMSSMT_SIGN_CTX ctx;
|
||||
XMSSMT_SIGNATURE sig;
|
||||
uint8_t msg[100] = {0};
|
||||
|
||||
uint8_t sigbuf[sizeof(XMSSMT_SIGNATURE) *2 ];
|
||||
size_t siglen;
|
||||
|
||||
if (xmssmt_key_generate(&key, xmssmt_type) != 1) {
|
||||
error_print();
|
||||
@@ -1092,6 +1095,44 @@ static int test_xmssmt_sign_update(void)
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
// sigbuf
|
||||
|
||||
if (xmssmt_sign_init(&ctx, &key) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (xmssmt_sign_update(&ctx, msg, sizeof(msg)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (xmssmt_sign_finish(&ctx, sigbuf, &siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(&ctx, 0, sizeof(ctx));
|
||||
if (xmssmt_verify_init(&ctx, &key, sigbuf, siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (xmssmt_verify_update(&ctx, msg, sizeof(msg)) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (xmssmt_verify_finish(&ctx) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
printf("%s() ok\n", __FUNCTION__);
|
||||
return 1;
|
||||
}
|
||||
@@ -1114,7 +1155,7 @@ int main(void)
|
||||
//if (test_xmss_private_key_to_bytes() != 1) goto err;
|
||||
if (test_xmss_signature_size() != 1) goto err;
|
||||
if (test_xmss_sign() != 1) goto err;
|
||||
if (test_xmss_sign_init() != 1) goto err;
|
||||
if (test_xmss_sign_update() != 1) goto err;
|
||||
if (test_xmssmt_key_generate() != 1) goto err;
|
||||
if (test_xmssmt_index_to_bytes() != 1) goto err;
|
||||
if (test_xmssmt_signature_to_bytes() != 1) goto err;
|
||||
|
||||
Reference in New Issue
Block a user