修改License

This commit is contained in:
Simon
2022-08-25 22:42:11 -07:00
parent 16b39da60b
commit 0b4308b948
268 changed files with 67247 additions and 67015 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,370 +7,371 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/aes.h>
#include <gmssl/hex.h>
#include <gmssl/error.h>
int test_aes(void)
{
AES_KEY aes_key;
int i;
/* test 1 */
uint8_t key128[16] = {
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
};
uint32_t rk128[4 * 11] = {
0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c,
0xa0fafe17, 0x88542cb1, 0x23a33939, 0x2a6c7605,
0xf2c295f2, 0x7a96b943, 0x5935807a, 0x7359f67f,
0x3d80477d, 0x4716fe3e, 0x1e237e44, 0x6d7a883b,
0xef44a541, 0xa8525b7f, 0xb671253b, 0xdb0bad00,
0xd4d1c6f8, 0x7c839d87, 0xcaf2b8bc, 0x11f915bc,
0x6d88a37a, 0x110b3efd, 0xdbf98641, 0xca0093fd,
0x4e54f70e, 0x5f5fc9f3, 0x84a64fb2, 0x4ea6dc4f,
0xead27321, 0xb58dbad2, 0x312bf560, 0x7f8d292f,
0xac7766f3, 0x19fadc21, 0x28d12941, 0x575c006e,
0xd014f9a8, 0xc9ee2589, 0xe13f0cc8, 0xb6630ca6,
};
/* test 2 */
uint8_t key192[24] = {
0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b,
};
uint32_t rk192[4 * 13] = {
0x8e73b0f7, 0xda0e6452, 0xc810f32b, 0x809079e5,
0x62f8ead2, 0x522c6b7b, 0xfe0c91f7, 0x2402f5a5,
0xec12068e, 0x6c827f6b, 0x0e7a95b9, 0x5c56fec2,
0x4db7b4bd, 0x69b54118, 0x85a74796, 0xe92538fd,
0xe75fad44, 0xbb095386, 0x485af057, 0x21efb14f,
0xa448f6d9, 0x4d6dce24, 0xaa326360, 0x113b30e6,
0xa25e7ed5, 0x83b1cf9a, 0x27f93943, 0x6a94f767,
0xc0a69407, 0xd19da4e1, 0xec1786eb, 0x6fa64971,
0x485f7032, 0x22cb8755, 0xe26d1352, 0x33f0b7b3,
0x40beeb28, 0x2f18a259, 0x6747d26b, 0x458c553e,
0xa7e1466c, 0x9411f1df, 0x821f750a, 0xad07d753,
0xca400538, 0x8fcc5006, 0x282d166a, 0xbc3ce7b5,
0xe98ba06f, 0x448c773c, 0x8ecc7204, 0x01002202,
};
/* test 3 */
uint8_t key256[32] = {
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4,
};
uint32_t rk256[4 * 15] = {
0x603deb10, 0x15ca71be, 0x2b73aef0, 0x857d7781,
0x1f352c07, 0x3b6108d7, 0x2d9810a3, 0x0914dff4,
0x9ba35411, 0x8e6925af, 0xa51a8b5f, 0x2067fcde,
0xa8b09c1a, 0x93d194cd, 0xbe49846e, 0xb75d5b9a,
0xd59aecb8, 0x5bf3c917, 0xfee94248, 0xde8ebe96,
0xb5a9328a, 0x2678a647, 0x98312229, 0x2f6c79b3,
0x812c81ad, 0xdadf48ba, 0x24360af2, 0xfab8b464,
0x98c5bfc9, 0xbebd198e, 0x268c3ba7, 0x09e04214,
0x68007bac, 0xb2df3316, 0x96e939e4, 0x6c518d80,
0xc814e204, 0x76a9fb8a, 0x5025c02d, 0x59c58239,
0xde136967, 0x6ccc5a71, 0xfa256395, 0x9674ee15,
0x5886ca5d, 0x2e2f31d7, 0x7e0af1fa, 0x27cf73c3,
0x749c47ab, 0x18501dda, 0xe2757e4f, 0x7401905a,
0xcafaaae3, 0xe4d59b34, 0x9adf6ace, 0xbd10190d,
0xfe4890d1, 0xe6188d0b, 0x046df344, 0x706c631e,
};
/* test 4 */
unsigned char in1[16] = {
0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34,
};
unsigned char out1[16] = {
0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb,
0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32,
};
unsigned char buf[16] = {0};
printf("aes test 1 ");
aes_set_encrypt_key(&aes_key, key128, sizeof(key128));
if (memcmp(&aes_key, rk128, sizeof(rk128)) != 0) {
printf("failed\n");
return -1;
} else {
printf("ok\n");
}
printf("aes test 2 ");
aes_set_encrypt_key(&aes_key, key192, sizeof(key192));
if (memcmp(&aes_key, rk192, sizeof(rk192)) != 0) {
printf("failed\n");
return -1;
} else {
printf("ok\n");
}
printf("aes test 3 ");
aes_set_encrypt_key(&aes_key, key256, sizeof(key256));
if (memcmp(&aes_key, rk256, sizeof(rk256)) != 0) {
printf("failed\n");
return -1;
} else {
printf("ok\n");
}
printf("aes test 4 ");
aes_set_encrypt_key(&aes_key, key128, sizeof(key128));
aes_encrypt(&aes_key, in1, buf);
if (memcmp(buf, out1, sizeof(out1)) != 0) {
printf("failed\n");
return -1;
} else {
printf("ok\n");
}
printf("aes test 5 ");
aes_set_decrypt_key(&aes_key, key128, sizeof(key128));
aes_decrypt(&aes_key, buf, buf);
if (memcmp(buf, in1, sizeof(in1)) != 0) {
printf("failed\n");
return -1;
} else {
printf("ok\n");
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int test_aes_ctr(void)
{
// NIST SP 800-38A F.5.1
char *hex_key = "2b7e151628aed2a6abf7158809cf4f3c";
char *hex_ctr = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
char *hex_msg = "6bc1bee22e409f96e93d7e117393172a"
"ae2d8a571e03ac9c9eb76fac45af8e51"
"30c81c46a35ce411e5fbc1191a0a52ef"
"f69f2445df4f9b17ad2b417be66c3710";
char *hex_out = "874d6191b620e3261bef6864990db6ce"
"9806f66b7970fdff8617187bb9fffdff"
"5ae4df3edbd5d35e5b4f09020db03eab"
"1e031dda2fbe03d1792170a0f3009cee";
AES_KEY aes_key;
uint8_t key[32];
uint8_t ctr[16];
uint8_t msg[64];
uint8_t out[64];
uint8_t buf[64];
size_t keylen, ctrlen, msglen, outlen, buflen;
hex_to_bytes(hex_key, strlen(hex_key), key, &keylen);
hex_to_bytes(hex_ctr, strlen(hex_ctr), ctr, &ctrlen);
hex_to_bytes(hex_msg, strlen(hex_msg), msg, &msglen);
hex_to_bytes(hex_out, strlen(hex_out), out, &outlen);
aes_set_encrypt_key(&aes_key, key, keylen);
aes_ctr_encrypt(&aes_key, ctr, msg, msglen, buf);
buflen = msglen;
printf("aes ctr test 1 ");
if (memcmp(buf, out, outlen) != 0) {
printf("failed\n");
format_bytes(stdout, 0, 0, "aes_ctr(msg) = ", buf, buflen);
format_bytes(stdout, 0, 0, " != ", out, outlen);
return -1;
} else {
printf("ok\n");
}
printf("aes ctr test 2 ");
hex_to_bytes(hex_ctr, strlen(hex_ctr), ctr, &ctrlen);
aes_ctr_decrypt(&aes_key, ctr, buf, buflen, buf);
if (memcmp(buf, msg, msglen) != 0) {
printf("failed\n");
format_bytes(stdout, 0, 0, "msg = ", msg, msglen);
format_bytes(stdout, 0, 0, " = ", buf, buflen);
return -1;
} else {
printf("ok\n");
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
struct {
char *K;
char *P;
char *A;
char *IV;
char *C;
char *T;
} aes_gcm_tests[] = {
// test 1
{
"00000000000000000000000000000000",
"",
"",
"000000000000000000000000",
"",
"58e2fccefa7e3061367f1d57a4e7455a",
},
// test 2
{
"00000000000000000000000000000000",
"00000000000000000000000000000000",
"",
"000000000000000000000000",
"0388dace60b6a392f328c2b971b2fe78",
"ab6e47d42cec13bdf53a67b21257bddf",
},
// test 3
{
"feffe9928665731c6d6a8f9467308308",
"d9313225f88406e5a55909c5aff5269a"
"86a7a9531534f7da2e4c303d8a318a72"
"1c3c0c95956809532fcf0e2449a6b525"
"b16aedf5aa0de657ba637b391aafd255",
"",
"cafebabefacedbaddecaf888",
"42831ec2217774244b7221b784d0d49c"
"e3aa212f2c02a4e035c17e2329aca12e"
"21d514b25466931c7d8f6a5aac84aa05"
"1ba30b396a0aac973d58e091473f5985",
"4d5c2af327cd64a62cf35abd2ba6fab4",
},
// test 4
{
"feffe9928665731c6d6a8f9467308308",
"d9313225f88406e5a55909c5aff5269a"
"86a7a9531534f7da2e4c303d8a318a72"
"1c3c0c95956809532fcf0e2449a6b525"
"b16aedf5aa0de657ba637b39",
"feedfacedeadbeeffeedfacedeadbeef"
"abaddad2",
"cafebabefacedbaddecaf888",
"42831ec2217774244b7221b784d0d49c"
"e3aa212f2c02a4e035c17e2329aca12e"
"21d514b25466931c7d8f6a5aac84aa05"
"1ba30b396a0aac973d58e091",
"5bc94fbc3221a5db94fae95ae7121a47",
},
// test 5
{
"feffe9928665731c6d6a8f9467308308",
"d9313225f88406e5a55909c5aff5269a"
"86a7a9531534f7da2e4c303d8a318a72"
"1c3c0c95956809532fcf0e2449a6b525"
"b16aedf5aa0de657ba637b39",
"feedfacedeadbeeffeedfacedeadbeef"
"abaddad2",
"cafebabefacedbad",
"61353b4c2806934a777ff51fa22a4755"
"699b2a714fcdc6f83766e5f97b6c7423"
"73806900e49f24b22b097544d4896b42"
"4989b5e1ebac0f07c23f4598",
"3612d2e79e3b0785561be14aaca2fccb",
},
// test 6
{
"feffe9928665731c6d6a8f9467308308",
"d9313225f88406e5a55909c5aff5269a"
"86a7a9531534f7da2e4c303d8a318a72"
"1c3c0c95956809532fcf0e2449a6b525"
"b16aedf5aa0de657ba637b39",
"feedfacedeadbeeffeedfacedeadbeef"
"abaddad2",
"9313225df88406e555909c5aff5269aa"
"6a7a9538534f7da1e4c303d2a318a728"
"c3c0c95156809539fcf0e2429a6b5254"
"16aedbf5a0de6a57a637b39b",
"8ce24998625615b603a033aca13fb894"
"be9112a5c3a211a8ba262a3cca7e2ca7"
"01e4a9a4fba43c90ccdcb281d48c7c6f"
"d62875d2aca417034c34aee5",
"619cc5aefffe0bfa462af43c1699d050",
},
// test 7
{
"00000000000000000000000000000000"
"0000000000000000",
"",
"",
"000000000000000000000000",
"",
"cd33b28ac773f74ba00ed1f312572435",
},
};
int test_aes_gcm(void)
{
int err = 0;
uint8_t K[32];
uint8_t P[64];
uint8_t A[32];
uint8_t IV[64];
uint8_t C[64];
uint8_t T[16];
size_t Klen, Plen, Alen, IVlen, Clen, Tlen;
AES_KEY aes_key;
uint8_t out[64];
uint8_t tag[16];
uint8_t buf[64];
int i;
for (i = 0; i < sizeof(aes_gcm_tests)/sizeof(aes_gcm_tests[0]); i++) {
hex_to_bytes(aes_gcm_tests[i].K, strlen(aes_gcm_tests[i].K), K, &Klen);
hex_to_bytes(aes_gcm_tests[i].P, strlen(aes_gcm_tests[i].P), P, &Plen);
hex_to_bytes(aes_gcm_tests[i].A, strlen(aes_gcm_tests[i].A), A, &Alen);
hex_to_bytes(aes_gcm_tests[i].IV, strlen(aes_gcm_tests[i].IV), IV, &IVlen);
hex_to_bytes(aes_gcm_tests[i].C, strlen(aes_gcm_tests[i].C), C, &Clen);
hex_to_bytes(aes_gcm_tests[i].T, strlen(aes_gcm_tests[i].T), T, &Tlen);
aes_set_encrypt_key(&aes_key, K, Klen);
aes_gcm_encrypt(&aes_key, IV, IVlen, A, Alen, P, Plen, out, Tlen, tag);
printf("aes gcm test %d ", i + 1);
if (aes_gcm_decrypt(&aes_key, IV, IVlen, A, Alen, out, Plen, tag, Tlen, buf) != 1
|| memcmp(buf, P, Plen) != 0) {
printf("failed\n");
format_print(stdout, 0, 2, "K = %s\n", aes_gcm_tests[i].K);
format_print(stdout, 0, 2, "P = %s\n", aes_gcm_tests[i].P);
format_print(stdout, 0, 2, "A = %s\n", aes_gcm_tests[i].A);
format_print(stdout, 0, 2, "IV = %s\n", aes_gcm_tests[i].IV);
format_print(stdout, 0, 2, "C = %s\n", aes_gcm_tests[i].C);
format_bytes(stdout, 0, 2, " = ", out, Plen);
format_print(stdout, 0, 2, "T = %s\n", aes_gcm_tests[i].T);
format_bytes(stdout, 0, 2, " = ", tag, Tlen);
return -1;
} else {
printf("ok\n");
}
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_aes() != 1) goto err;
if (test_aes_ctr() != 1) goto err;
if (test_aes_gcm() != 1) goto err;
printf("%s all tests passed!\n", __FILE__);
return 0;
err:
error_print();
return 1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/aes.h>
#include <gmssl/hex.h>
#include <gmssl/error.h>
int test_aes(void)
{
AES_KEY aes_key;
int i;
/* test 1 */
uint8_t key128[16] = {
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c,
};
uint32_t rk128[4 * 11] = {
0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c,
0xa0fafe17, 0x88542cb1, 0x23a33939, 0x2a6c7605,
0xf2c295f2, 0x7a96b943, 0x5935807a, 0x7359f67f,
0x3d80477d, 0x4716fe3e, 0x1e237e44, 0x6d7a883b,
0xef44a541, 0xa8525b7f, 0xb671253b, 0xdb0bad00,
0xd4d1c6f8, 0x7c839d87, 0xcaf2b8bc, 0x11f915bc,
0x6d88a37a, 0x110b3efd, 0xdbf98641, 0xca0093fd,
0x4e54f70e, 0x5f5fc9f3, 0x84a64fb2, 0x4ea6dc4f,
0xead27321, 0xb58dbad2, 0x312bf560, 0x7f8d292f,
0xac7766f3, 0x19fadc21, 0x28d12941, 0x575c006e,
0xd014f9a8, 0xc9ee2589, 0xe13f0cc8, 0xb6630ca6,
};
/* test 2 */
uint8_t key192[24] = {
0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b,
};
uint32_t rk192[4 * 13] = {
0x8e73b0f7, 0xda0e6452, 0xc810f32b, 0x809079e5,
0x62f8ead2, 0x522c6b7b, 0xfe0c91f7, 0x2402f5a5,
0xec12068e, 0x6c827f6b, 0x0e7a95b9, 0x5c56fec2,
0x4db7b4bd, 0x69b54118, 0x85a74796, 0xe92538fd,
0xe75fad44, 0xbb095386, 0x485af057, 0x21efb14f,
0xa448f6d9, 0x4d6dce24, 0xaa326360, 0x113b30e6,
0xa25e7ed5, 0x83b1cf9a, 0x27f93943, 0x6a94f767,
0xc0a69407, 0xd19da4e1, 0xec1786eb, 0x6fa64971,
0x485f7032, 0x22cb8755, 0xe26d1352, 0x33f0b7b3,
0x40beeb28, 0x2f18a259, 0x6747d26b, 0x458c553e,
0xa7e1466c, 0x9411f1df, 0x821f750a, 0xad07d753,
0xca400538, 0x8fcc5006, 0x282d166a, 0xbc3ce7b5,
0xe98ba06f, 0x448c773c, 0x8ecc7204, 0x01002202,
};
/* test 3 */
uint8_t key256[32] = {
0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4,
};
uint32_t rk256[4 * 15] = {
0x603deb10, 0x15ca71be, 0x2b73aef0, 0x857d7781,
0x1f352c07, 0x3b6108d7, 0x2d9810a3, 0x0914dff4,
0x9ba35411, 0x8e6925af, 0xa51a8b5f, 0x2067fcde,
0xa8b09c1a, 0x93d194cd, 0xbe49846e, 0xb75d5b9a,
0xd59aecb8, 0x5bf3c917, 0xfee94248, 0xde8ebe96,
0xb5a9328a, 0x2678a647, 0x98312229, 0x2f6c79b3,
0x812c81ad, 0xdadf48ba, 0x24360af2, 0xfab8b464,
0x98c5bfc9, 0xbebd198e, 0x268c3ba7, 0x09e04214,
0x68007bac, 0xb2df3316, 0x96e939e4, 0x6c518d80,
0xc814e204, 0x76a9fb8a, 0x5025c02d, 0x59c58239,
0xde136967, 0x6ccc5a71, 0xfa256395, 0x9674ee15,
0x5886ca5d, 0x2e2f31d7, 0x7e0af1fa, 0x27cf73c3,
0x749c47ab, 0x18501dda, 0xe2757e4f, 0x7401905a,
0xcafaaae3, 0xe4d59b34, 0x9adf6ace, 0xbd10190d,
0xfe4890d1, 0xe6188d0b, 0x046df344, 0x706c631e,
};
/* test 4 */
unsigned char in1[16] = {
0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d,
0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34,
};
unsigned char out1[16] = {
0x39, 0x25, 0x84, 0x1d, 0x02, 0xdc, 0x09, 0xfb,
0xdc, 0x11, 0x85, 0x97, 0x19, 0x6a, 0x0b, 0x32,
};
unsigned char buf[16] = {0};
printf("aes test 1 ");
aes_set_encrypt_key(&aes_key, key128, sizeof(key128));
if (memcmp(&aes_key, rk128, sizeof(rk128)) != 0) {
printf("failed\n");
return -1;
} else {
printf("ok\n");
}
printf("aes test 2 ");
aes_set_encrypt_key(&aes_key, key192, sizeof(key192));
if (memcmp(&aes_key, rk192, sizeof(rk192)) != 0) {
printf("failed\n");
return -1;
} else {
printf("ok\n");
}
printf("aes test 3 ");
aes_set_encrypt_key(&aes_key, key256, sizeof(key256));
if (memcmp(&aes_key, rk256, sizeof(rk256)) != 0) {
printf("failed\n");
return -1;
} else {
printf("ok\n");
}
printf("aes test 4 ");
aes_set_encrypt_key(&aes_key, key128, sizeof(key128));
aes_encrypt(&aes_key, in1, buf);
if (memcmp(buf, out1, sizeof(out1)) != 0) {
printf("failed\n");
return -1;
} else {
printf("ok\n");
}
printf("aes test 5 ");
aes_set_decrypt_key(&aes_key, key128, sizeof(key128));
aes_decrypt(&aes_key, buf, buf);
if (memcmp(buf, in1, sizeof(in1)) != 0) {
printf("failed\n");
return -1;
} else {
printf("ok\n");
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int test_aes_ctr(void)
{
// NIST SP 800-38A F.5.1
char *hex_key = "2b7e151628aed2a6abf7158809cf4f3c";
char *hex_ctr = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
char *hex_msg = "6bc1bee22e409f96e93d7e117393172a"
"ae2d8a571e03ac9c9eb76fac45af8e51"
"30c81c46a35ce411e5fbc1191a0a52ef"
"f69f2445df4f9b17ad2b417be66c3710";
char *hex_out = "874d6191b620e3261bef6864990db6ce"
"9806f66b7970fdff8617187bb9fffdff"
"5ae4df3edbd5d35e5b4f09020db03eab"
"1e031dda2fbe03d1792170a0f3009cee";
AES_KEY aes_key;
uint8_t key[32];
uint8_t ctr[16];
uint8_t msg[64];
uint8_t out[64];
uint8_t buf[64];
size_t keylen, ctrlen, msglen, outlen, buflen;
hex_to_bytes(hex_key, strlen(hex_key), key, &keylen);
hex_to_bytes(hex_ctr, strlen(hex_ctr), ctr, &ctrlen);
hex_to_bytes(hex_msg, strlen(hex_msg), msg, &msglen);
hex_to_bytes(hex_out, strlen(hex_out), out, &outlen);
aes_set_encrypt_key(&aes_key, key, keylen);
aes_ctr_encrypt(&aes_key, ctr, msg, msglen, buf);
buflen = msglen;
printf("aes ctr test 1 ");
if (memcmp(buf, out, outlen) != 0) {
printf("failed\n");
format_bytes(stdout, 0, 0, "aes_ctr(msg) = ", buf, buflen);
format_bytes(stdout, 0, 0, " != ", out, outlen);
return -1;
} else {
printf("ok\n");
}
printf("aes ctr test 2 ");
hex_to_bytes(hex_ctr, strlen(hex_ctr), ctr, &ctrlen);
aes_ctr_decrypt(&aes_key, ctr, buf, buflen, buf);
if (memcmp(buf, msg, msglen) != 0) {
printf("failed\n");
format_bytes(stdout, 0, 0, "msg = ", msg, msglen);
format_bytes(stdout, 0, 0, " = ", buf, buflen);
return -1;
} else {
printf("ok\n");
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
struct {
char *K;
char *P;
char *A;
char *IV;
char *C;
char *T;
} aes_gcm_tests[] = {
// test 1
{
"00000000000000000000000000000000",
"",
"",
"000000000000000000000000",
"",
"58e2fccefa7e3061367f1d57a4e7455a",
},
// test 2
{
"00000000000000000000000000000000",
"00000000000000000000000000000000",
"",
"000000000000000000000000",
"0388dace60b6a392f328c2b971b2fe78",
"ab6e47d42cec13bdf53a67b21257bddf",
},
// test 3
{
"feffe9928665731c6d6a8f9467308308",
"d9313225f88406e5a55909c5aff5269a"
"86a7a9531534f7da2e4c303d8a318a72"
"1c3c0c95956809532fcf0e2449a6b525"
"b16aedf5aa0de657ba637b391aafd255",
"",
"cafebabefacedbaddecaf888",
"42831ec2217774244b7221b784d0d49c"
"e3aa212f2c02a4e035c17e2329aca12e"
"21d514b25466931c7d8f6a5aac84aa05"
"1ba30b396a0aac973d58e091473f5985",
"4d5c2af327cd64a62cf35abd2ba6fab4",
},
// test 4
{
"feffe9928665731c6d6a8f9467308308",
"d9313225f88406e5a55909c5aff5269a"
"86a7a9531534f7da2e4c303d8a318a72"
"1c3c0c95956809532fcf0e2449a6b525"
"b16aedf5aa0de657ba637b39",
"feedfacedeadbeeffeedfacedeadbeef"
"abaddad2",
"cafebabefacedbaddecaf888",
"42831ec2217774244b7221b784d0d49c"
"e3aa212f2c02a4e035c17e2329aca12e"
"21d514b25466931c7d8f6a5aac84aa05"
"1ba30b396a0aac973d58e091",
"5bc94fbc3221a5db94fae95ae7121a47",
},
// test 5
{
"feffe9928665731c6d6a8f9467308308",
"d9313225f88406e5a55909c5aff5269a"
"86a7a9531534f7da2e4c303d8a318a72"
"1c3c0c95956809532fcf0e2449a6b525"
"b16aedf5aa0de657ba637b39",
"feedfacedeadbeeffeedfacedeadbeef"
"abaddad2",
"cafebabefacedbad",
"61353b4c2806934a777ff51fa22a4755"
"699b2a714fcdc6f83766e5f97b6c7423"
"73806900e49f24b22b097544d4896b42"
"4989b5e1ebac0f07c23f4598",
"3612d2e79e3b0785561be14aaca2fccb",
},
// test 6
{
"feffe9928665731c6d6a8f9467308308",
"d9313225f88406e5a55909c5aff5269a"
"86a7a9531534f7da2e4c303d8a318a72"
"1c3c0c95956809532fcf0e2449a6b525"
"b16aedf5aa0de657ba637b39",
"feedfacedeadbeeffeedfacedeadbeef"
"abaddad2",
"9313225df88406e555909c5aff5269aa"
"6a7a9538534f7da1e4c303d2a318a728"
"c3c0c95156809539fcf0e2429a6b5254"
"16aedbf5a0de6a57a637b39b",
"8ce24998625615b603a033aca13fb894"
"be9112a5c3a211a8ba262a3cca7e2ca7"
"01e4a9a4fba43c90ccdcb281d48c7c6f"
"d62875d2aca417034c34aee5",
"619cc5aefffe0bfa462af43c1699d050",
},
// test 7
{
"00000000000000000000000000000000"
"0000000000000000",
"",
"",
"000000000000000000000000",
"",
"cd33b28ac773f74ba00ed1f312572435",
},
};
int test_aes_gcm(void)
{
int err = 0;
uint8_t K[32];
uint8_t P[64];
uint8_t A[32];
uint8_t IV[64];
uint8_t C[64];
uint8_t T[16];
size_t Klen, Plen, Alen, IVlen, Clen, Tlen;
AES_KEY aes_key;
uint8_t out[64];
uint8_t tag[16];
uint8_t buf[64];
int i;
for (i = 0; i < sizeof(aes_gcm_tests)/sizeof(aes_gcm_tests[0]); i++) {
hex_to_bytes(aes_gcm_tests[i].K, strlen(aes_gcm_tests[i].K), K, &Klen);
hex_to_bytes(aes_gcm_tests[i].P, strlen(aes_gcm_tests[i].P), P, &Plen);
hex_to_bytes(aes_gcm_tests[i].A, strlen(aes_gcm_tests[i].A), A, &Alen);
hex_to_bytes(aes_gcm_tests[i].IV, strlen(aes_gcm_tests[i].IV), IV, &IVlen);
hex_to_bytes(aes_gcm_tests[i].C, strlen(aes_gcm_tests[i].C), C, &Clen);
hex_to_bytes(aes_gcm_tests[i].T, strlen(aes_gcm_tests[i].T), T, &Tlen);
aes_set_encrypt_key(&aes_key, K, Klen);
aes_gcm_encrypt(&aes_key, IV, IVlen, A, Alen, P, Plen, out, Tlen, tag);
printf("aes gcm test %d ", i + 1);
if (aes_gcm_decrypt(&aes_key, IV, IVlen, A, Alen, out, Plen, tag, Tlen, buf) != 1
|| memcmp(buf, P, Plen) != 0) {
printf("failed\n");
format_print(stdout, 0, 2, "K = %s\n", aes_gcm_tests[i].K);
format_print(stdout, 0, 2, "P = %s\n", aes_gcm_tests[i].P);
format_print(stdout, 0, 2, "A = %s\n", aes_gcm_tests[i].A);
format_print(stdout, 0, 2, "IV = %s\n", aes_gcm_tests[i].IV);
format_print(stdout, 0, 2, "C = %s\n", aes_gcm_tests[i].C);
format_bytes(stdout, 0, 2, " = ", out, Plen);
format_print(stdout, 0, 2, "T = %s\n", aes_gcm_tests[i].T);
format_bytes(stdout, 0, 2, " = ", tag, Tlen);
return -1;
} else {
printf("ok\n");
}
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_aes() != 1) goto err;
if (test_aes_ctr() != 1) goto err;
if (test_aes_gcm() != 1) goto err;
printf("%s all tests passed!\n", __FILE__);
return 0;
err:
error_print();
return 1;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,66 +7,67 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/base64.h>
#include <gmssl/error.h>
static int test_base64(void)
{
uint8_t bin1[50];
uint8_t bin2[100];
uint8_t bin3[200];
uint8_t buf1[8000] = {0};
uint8_t buf2[8000] = {0};
BASE64_CTX ctx;
uint8_t *p;
int len;
memset(bin1, 0x01, sizeof(bin1));
memset(bin2, 0xA5, sizeof(bin2));
memset(bin3, 0xff, sizeof(bin3));
p = buf1;
base64_encode_init(&ctx);
base64_encode_update(&ctx, bin1, sizeof(bin1), p, &len); p += len;
base64_encode_update(&ctx, bin2, sizeof(bin2), p, &len); p += len;
base64_encode_update(&ctx, bin3, sizeof(bin3), p, &len); p += len;
base64_encode_finish(&ctx, p, &len); p += len;
len = (int)(p - buf1);
p = buf2;
base64_decode_init(&ctx);
base64_decode_update(&ctx, buf1, len, p, &len); p += len;
base64_decode_finish(&ctx, p, &len); p += len;
len = (int)(p - buf2);
printf("base64 test ");
if (len != sizeof(bin1) + sizeof(bin2) + sizeof(bin3)
|| memcmp(buf2, bin1, sizeof(bin1)) != 0
|| memcmp(buf2 + sizeof(bin1), bin2, sizeof(bin2)) != 0
|| memcmp(buf2 + sizeof(bin1) + sizeof(bin2), bin3, sizeof(bin3)) != 0) {
printf("failed\n");
return -1;
} else {
printf("ok\n");
}
return 1;
}
int main(void)
{
if (test_base64() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/base64.h>
#include <gmssl/error.h>
static int test_base64(void)
{
uint8_t bin1[50];
uint8_t bin2[100];
uint8_t bin3[200];
uint8_t buf1[8000] = {0};
uint8_t buf2[8000] = {0};
BASE64_CTX ctx;
uint8_t *p;
int len;
memset(bin1, 0x01, sizeof(bin1));
memset(bin2, 0xA5, sizeof(bin2));
memset(bin3, 0xff, sizeof(bin3));
p = buf1;
base64_encode_init(&ctx);
base64_encode_update(&ctx, bin1, sizeof(bin1), p, &len); p += len;
base64_encode_update(&ctx, bin2, sizeof(bin2), p, &len); p += len;
base64_encode_update(&ctx, bin3, sizeof(bin3), p, &len); p += len;
base64_encode_finish(&ctx, p, &len); p += len;
len = (int)(p - buf1);
p = buf2;
base64_decode_init(&ctx);
base64_decode_update(&ctx, buf1, len, p, &len); p += len;
base64_decode_finish(&ctx, p, &len); p += len;
len = (int)(p - buf2);
printf("base64 test ");
if (len != sizeof(bin1) + sizeof(bin2) + sizeof(bin3)
|| memcmp(buf2, bin1, sizeof(bin1)) != 0
|| memcmp(buf2 + sizeof(bin1), bin2, sizeof(bin2)) != 0
|| memcmp(buf2 + sizeof(bin1) + sizeof(bin2), bin3, sizeof(bin3)) != 0) {
printf("failed\n");
return -1;
} else {
printf("ok\n");
}
return 1;
}
int main(void)
{
if (test_base64() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,13 +7,14 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/block_cipher.h>
int main(void)
{
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/block_cipher.h>
int main(void)
{
return 0;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,53 +7,54 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmssl/chacha20.h>
int main(void)
{
int err = 0;
int i;
const unsigned char key[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
};
const unsigned char nonce[] = {
0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a,
0x00, 0x00, 0x00, 0x00,
};
uint32_t counter = 1;
const unsigned char testdata[] = {
0x10, 0xf1, 0xe7, 0xe4, 0xd1, 0x3b, 0x59, 0x15,
0x50, 0x0f, 0xdd, 0x1f, 0xa3, 0x20, 0x71, 0xc4,
0xc7, 0xd1, 0xf4, 0xc7, 0x33, 0xc0, 0x68, 0x03,
0x04, 0x22, 0xaa, 0x9a, 0xc3, 0xd4, 0x6c, 0x4e,
0xd2, 0x82, 0x64, 0x46, 0x07, 0x9f, 0xaa, 0x09,
0x14, 0xc2, 0xd7, 0x05, 0xd9, 0x8b, 0x02, 0xa2,
0xb5, 0x12, 0x9c, 0xd1, 0xde, 0x16, 0x4e, 0xb9,
0xcb, 0xd0, 0x83, 0xe8, 0xa2, 0x50, 0x3c, 0x4e,
};
unsigned char buf[64];
CHACHA20_STATE state;
chacha20_init(&state, key, nonce, counter);
chacha20_generate_keystream(&state, 1, buf);
printf("chacha20 test ");
if (memcmp(buf, testdata, sizeof(testdata)) != 0) {
printf("failed\n");
err++;
} else {
printf("ok\n");
}
return err;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gmssl/chacha20.h>
int main(void)
{
int err = 0;
int i;
const unsigned char key[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
};
const unsigned char nonce[] = {
0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a,
0x00, 0x00, 0x00, 0x00,
};
uint32_t counter = 1;
const unsigned char testdata[] = {
0x10, 0xf1, 0xe7, 0xe4, 0xd1, 0x3b, 0x59, 0x15,
0x50, 0x0f, 0xdd, 0x1f, 0xa3, 0x20, 0x71, 0xc4,
0xc7, 0xd1, 0xf4, 0xc7, 0x33, 0xc0, 0x68, 0x03,
0x04, 0x22, 0xaa, 0x9a, 0xc3, 0xd4, 0x6c, 0x4e,
0xd2, 0x82, 0x64, 0x46, 0x07, 0x9f, 0xaa, 0x09,
0x14, 0xc2, 0xd7, 0x05, 0xd9, 0x8b, 0x02, 0xa2,
0xb5, 0x12, 0x9c, 0xd1, 0xde, 0x16, 0x4e, 0xb9,
0xcb, 0xd0, 0x83, 0xe8, 0xa2, 0x50, 0x3c, 0x4e,
};
unsigned char buf[64];
CHACHA20_STATE state;
chacha20_init(&state, key, nonce, counter);
chacha20_generate_keystream(&state, 1, buf);
printf("chacha20 test ");
if (memcmp(buf, testdata, sizeof(testdata)) != 0) {
printf("failed\n");
err++;
} else {
printf("ok\n");
}
return err;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,17 +7,18 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/des.h>
int main(void)
{
int err = 0;
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/des.h>
int main(void)
{
int err = 0;
return err;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,41 +7,42 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/digest.h>
const char *digests[] = {
// "md5",
"sha1",
"sm3",
"sha224",
"sha256",
"sha384",
"sha512",
"sha512-224",
"sha512-256",
};
int main(void)
{
uint8_t dgst[64];
size_t dgstlen;
size_t i, j;
for (i = 0; i < sizeof(digests)/sizeof(digests[0]); i++) {
const DIGEST *algor = digest_from_name(digests[i]);
digest(algor, (uint8_t *)"abc", 3, dgst, &dgstlen);
printf("%s (%zu) ", digests[i], dgstlen);
for (j = 0; j < dgstlen; j++) {
printf("%02x", dgst[j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/digest.h>
const char *digests[] = {
// "md5",
"sha1",
"sm3",
"sha224",
"sha256",
"sha384",
"sha512",
"sha512-224",
"sha512-256",
};
int main(void)
{
uint8_t dgst[64];
size_t dgstlen;
size_t i, j;
for (i = 0; i < sizeof(digests)/sizeof(digests[0]); i++) {
const DIGEST *algor = digest_from_name(digests[i]);
digest(algor, (uint8_t *)"abc", 3, dgst, &dgstlen);
printf("%s (%zu) ", digests[i], dgstlen);
for (j = 0; j < dgstlen; j++) {
printf("%02x", dgst[j]);
}
printf("\n");
}
return 0;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,115 +7,116 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/sm2.h>
#include <gmssl/ec.h>
#include <gmssl/error.h>
static int test_ec_named_curve(void)
{
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
char *curves[] = {
"sm2p256v1",
"prime192v1",
"prime256v1",
"secp256k1",
"secp384r1",
"secp521r1",
};
int oid;
int i;
for (i = 0; i < sizeof(curves)/sizeof(curves[0]); i++) {
if ((oid = ec_named_curve_from_name(curves[i])) == OID_undef) {
error_print();
return -1;
}
if (ec_named_curve_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
}
for (i = 0; i < sizeof(curves)/sizeof(curves[0]); i++) {
if (ec_named_curve_from_der(&oid, &cp, &len) != 1) {
error_print();
return -1;
}
if (oid != ec_named_curve_from_name(curves[i])) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", ec_named_curve_name(oid));
}
(void)asn1_length_is_zero(len);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_ec_point_print(void)
{
SM2_KEY sm2_key;
uint8_t buf[256];
uint8_t *p = buf;
size_t len = 0;
if (sm2_key_generate(&sm2_key) != 1) {
error_print();
return -1;
}
if (sm2_point_to_der(&(sm2_key.public_key), &p, &len) != 1) {
error_print();
return -1;
}
ec_point_print(stderr, 0, 4, "ECPoint", buf, len);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_ec_private_key_print(void)
{
SM2_KEY sm2_key;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
if (sm2_key_generate(&sm2_key) != 1) {
error_print();
return -1;
}
if (sm2_private_key_to_der(&sm2_key, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
ec_private_key_print(stderr, 0, 4, "ECPrivateKey", d, dlen);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_ec_named_curve() != 1) goto err;
if (test_ec_point_print() != 1) goto err;
if (test_ec_private_key_print() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/sm2.h>
#include <gmssl/ec.h>
#include <gmssl/error.h>
static int test_ec_named_curve(void)
{
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
char *curves[] = {
"sm2p256v1",
"prime192v1",
"prime256v1",
"secp256k1",
"secp384r1",
"secp521r1",
};
int oid;
int i;
for (i = 0; i < sizeof(curves)/sizeof(curves[0]); i++) {
if ((oid = ec_named_curve_from_name(curves[i])) == OID_undef) {
error_print();
return -1;
}
if (ec_named_curve_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
}
for (i = 0; i < sizeof(curves)/sizeof(curves[0]); i++) {
if (ec_named_curve_from_der(&oid, &cp, &len) != 1) {
error_print();
return -1;
}
if (oid != ec_named_curve_from_name(curves[i])) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", ec_named_curve_name(oid));
}
(void)asn1_length_is_zero(len);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_ec_point_print(void)
{
SM2_KEY sm2_key;
uint8_t buf[256];
uint8_t *p = buf;
size_t len = 0;
if (sm2_key_generate(&sm2_key) != 1) {
error_print();
return -1;
}
if (sm2_point_to_der(&(sm2_key.public_key), &p, &len) != 1) {
error_print();
return -1;
}
ec_point_print(stderr, 0, 4, "ECPoint", buf, len);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_ec_private_key_print(void)
{
SM2_KEY sm2_key;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
if (sm2_key_generate(&sm2_key) != 1) {
error_print();
return -1;
}
if (sm2_private_key_to_der(&sm2_key, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
ec_private_key_print(stderr, 0, 4, "ECPrivateKey", d, dlen);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_ec_named_curve() != 1) goto err;
if (test_ec_point_print() != 1) goto err;
if (test_ec_private_key_print() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,187 +7,188 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/gcm.h>
#include <gmssl/hex.h>
#include <gmssl/rand.h>
#include <gmssl/block_cipher.h>
#include <gmssl/error.h>
struct {
char *H;
char *A;
char *C;
char *T;
} ghash_tests[] = {
// test 1
{
"66e94bd4ef8a2c3b884cfa59ca342b2e",
"",
"",
"00000000000000000000000000000000",
},
// test 2
{
"66e94bd4ef8a2c3b884cfa59ca342b2e",
"",
"0388dace60b6a392f328c2b971b2fe78",
"f38cbb1ad69223dcc3457ae5b6b0f885",
},
// test 3
{
"b83b533708bf535d0aa6e52980d53b78",
"",
"42831ec2217774244b7221b784d0d49c"
"e3aa212f2c02a4e035c17e2329aca12e"
"21d514b25466931c7d8f6a5aac84aa05"
"1ba30b396a0aac973d58e091473f5985",
"7f1b32b81b820d02614f8895ac1d4eac",
},
// test 4
{
"b83b533708bf535d0aa6e52980d53b78",
"feedfacedeadbeeffeedfacedeadbeef"
"abaddad2",
"42831ec2217774244b7221b784d0d49c"
"e3aa212f2c02a4e035c17e2329aca12e"
"21d514b25466931c7d8f6a5aac84aa05"
"1ba30b396a0aac973d58e091",
"698e57f70e6ecc7fd9463b7260a9ae5f",
},
// test 5
{
"b83b533708bf535d0aa6e52980d53b78",
"feedfacedeadbeeffeedfacedeadbeef"
"abaddad2",
"61353b4c2806934a777ff51fa22a4755"
"699b2a714fcdc6f83766e5f97b6c7423"
"73806900e49f24b22b097544d4896b42"
"4989b5e1ebac0f07c23f4598",
"df586bb4c249b92cb6922877e444d37b",
},
// test 6
{
"b83b533708bf535d0aa6e52980d53b78",
"feedfacedeadbeeffeedfacedeadbeef"
"abaddad2",
"8ce24998625615b603a033aca13fb894"
"be9112a5c3a211a8ba262a3cca7e2ca7"
"01e4a9a4fba43c90ccdcb281d48c7c6f"
"d62875d2aca417034c34aee5",
"1c5afe9760d3932f3c9a878aac3dc3de",
},
};
int test_ghash(void)
{
uint8_t H[16];
uint8_t A[32];
uint8_t C[64];
uint8_t T[16];
uint8_t out[16];
size_t Hlen, Alen, Clen, Tlen;
int i;
for (i = 0; i < sizeof(ghash_tests)/sizeof(ghash_tests[0]); i++) {
hex_to_bytes(ghash_tests[i].H, strlen(ghash_tests[i].H), H, &Hlen);
hex_to_bytes(ghash_tests[i].A, strlen(ghash_tests[i].A), A, &Alen);
hex_to_bytes(ghash_tests[i].C, strlen(ghash_tests[i].C), C, &Clen);
hex_to_bytes(ghash_tests[i].T, strlen(ghash_tests[i].T), T, &Tlen);
ghash(H, A, Alen, C, Clen, out);
if (memcmp(out, T, Tlen) != 0) {
format_print(stderr, 0, 0, "test %d failed\n", i + 1);
format_print(stderr, 0, 2, "H = %s\n", ghash_tests[i].H);
format_print(stderr, 0, 2, "A = %s\n", ghash_tests[i].A);
format_print(stderr, 0, 2, "C = %s\n", ghash_tests[i].C);
format_bytes(stderr, 0, 2, "GHASH(H,A,C) = ", out, 16);
format_print(stderr, 0, 2, " = %s\n\n", ghash_tests[i].T);
}
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int test_gcm(void)
{
BLOCK_CIPHER_KEY block_key;
uint8_t key[16];
uint8_t iv[12];
uint8_t aad[64];
uint8_t in[100];
uint8_t out[sizeof(in)];
uint8_t buf[sizeof(in)];
uint8_t tag[16];
rand_bytes(key, sizeof(key));
rand_bytes(iv, sizeof(iv));
rand_bytes(aad, sizeof(aad));
rand_bytes(in, sizeof(in));
memset(out, 0, sizeof(out));
memset(buf, 0, sizeof(buf));
memset(tag, 0, sizeof(tag));
if (block_cipher_set_encrypt_key(&block_key, BLOCK_CIPHER_aes128(), key) != 1) {
error_print();
return -1;
}
if (gcm_encrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), in, sizeof(in), out, sizeof(tag), tag) != 1) {
error_print();
return -1;
}
if (gcm_decrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), out, sizeof(out), tag, sizeof(tag), buf) != 1) {
error_print();
return -1;
}
if (memcmp(buf, in, sizeof(in)) != 0) {
error_print();
return -1;
}
memset(out, 0, sizeof(out));
memset(buf, 0, sizeof(buf));
memset(tag, 0, sizeof(tag));
if (block_cipher_set_encrypt_key(&block_key, BLOCK_CIPHER_sm4(), key) != 1) {
error_print();
return -1;
}
if (gcm_encrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), in, sizeof(in), out, sizeof(tag), tag) != 1) {
error_print();
return -1;
}
if (gcm_decrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), out, sizeof(out), tag, sizeof(tag), buf) != 1) {
error_print();
return -1;
}
if (memcmp(buf, in, sizeof(in)) != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(int argc, char **argv)
{
if (test_ghash() != 1) goto err;
if (test_gcm() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/gcm.h>
#include <gmssl/hex.h>
#include <gmssl/rand.h>
#include <gmssl/block_cipher.h>
#include <gmssl/error.h>
struct {
char *H;
char *A;
char *C;
char *T;
} ghash_tests[] = {
// test 1
{
"66e94bd4ef8a2c3b884cfa59ca342b2e",
"",
"",
"00000000000000000000000000000000",
},
// test 2
{
"66e94bd4ef8a2c3b884cfa59ca342b2e",
"",
"0388dace60b6a392f328c2b971b2fe78",
"f38cbb1ad69223dcc3457ae5b6b0f885",
},
// test 3
{
"b83b533708bf535d0aa6e52980d53b78",
"",
"42831ec2217774244b7221b784d0d49c"
"e3aa212f2c02a4e035c17e2329aca12e"
"21d514b25466931c7d8f6a5aac84aa05"
"1ba30b396a0aac973d58e091473f5985",
"7f1b32b81b820d02614f8895ac1d4eac",
},
// test 4
{
"b83b533708bf535d0aa6e52980d53b78",
"feedfacedeadbeeffeedfacedeadbeef"
"abaddad2",
"42831ec2217774244b7221b784d0d49c"
"e3aa212f2c02a4e035c17e2329aca12e"
"21d514b25466931c7d8f6a5aac84aa05"
"1ba30b396a0aac973d58e091",
"698e57f70e6ecc7fd9463b7260a9ae5f",
},
// test 5
{
"b83b533708bf535d0aa6e52980d53b78",
"feedfacedeadbeeffeedfacedeadbeef"
"abaddad2",
"61353b4c2806934a777ff51fa22a4755"
"699b2a714fcdc6f83766e5f97b6c7423"
"73806900e49f24b22b097544d4896b42"
"4989b5e1ebac0f07c23f4598",
"df586bb4c249b92cb6922877e444d37b",
},
// test 6
{
"b83b533708bf535d0aa6e52980d53b78",
"feedfacedeadbeeffeedfacedeadbeef"
"abaddad2",
"8ce24998625615b603a033aca13fb894"
"be9112a5c3a211a8ba262a3cca7e2ca7"
"01e4a9a4fba43c90ccdcb281d48c7c6f"
"d62875d2aca417034c34aee5",
"1c5afe9760d3932f3c9a878aac3dc3de",
},
};
int test_ghash(void)
{
uint8_t H[16];
uint8_t A[32];
uint8_t C[64];
uint8_t T[16];
uint8_t out[16];
size_t Hlen, Alen, Clen, Tlen;
int i;
for (i = 0; i < sizeof(ghash_tests)/sizeof(ghash_tests[0]); i++) {
hex_to_bytes(ghash_tests[i].H, strlen(ghash_tests[i].H), H, &Hlen);
hex_to_bytes(ghash_tests[i].A, strlen(ghash_tests[i].A), A, &Alen);
hex_to_bytes(ghash_tests[i].C, strlen(ghash_tests[i].C), C, &Clen);
hex_to_bytes(ghash_tests[i].T, strlen(ghash_tests[i].T), T, &Tlen);
ghash(H, A, Alen, C, Clen, out);
if (memcmp(out, T, Tlen) != 0) {
format_print(stderr, 0, 0, "test %d failed\n", i + 1);
format_print(stderr, 0, 2, "H = %s\n", ghash_tests[i].H);
format_print(stderr, 0, 2, "A = %s\n", ghash_tests[i].A);
format_print(stderr, 0, 2, "C = %s\n", ghash_tests[i].C);
format_bytes(stderr, 0, 2, "GHASH(H,A,C) = ", out, 16);
format_print(stderr, 0, 2, " = %s\n\n", ghash_tests[i].T);
}
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int test_gcm(void)
{
BLOCK_CIPHER_KEY block_key;
uint8_t key[16];
uint8_t iv[12];
uint8_t aad[64];
uint8_t in[100];
uint8_t out[sizeof(in)];
uint8_t buf[sizeof(in)];
uint8_t tag[16];
rand_bytes(key, sizeof(key));
rand_bytes(iv, sizeof(iv));
rand_bytes(aad, sizeof(aad));
rand_bytes(in, sizeof(in));
memset(out, 0, sizeof(out));
memset(buf, 0, sizeof(buf));
memset(tag, 0, sizeof(tag));
if (block_cipher_set_encrypt_key(&block_key, BLOCK_CIPHER_aes128(), key) != 1) {
error_print();
return -1;
}
if (gcm_encrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), in, sizeof(in), out, sizeof(tag), tag) != 1) {
error_print();
return -1;
}
if (gcm_decrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), out, sizeof(out), tag, sizeof(tag), buf) != 1) {
error_print();
return -1;
}
if (memcmp(buf, in, sizeof(in)) != 0) {
error_print();
return -1;
}
memset(out, 0, sizeof(out));
memset(buf, 0, sizeof(buf));
memset(tag, 0, sizeof(tag));
if (block_cipher_set_encrypt_key(&block_key, BLOCK_CIPHER_sm4(), key) != 1) {
error_print();
return -1;
}
if (gcm_encrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), in, sizeof(in), out, sizeof(tag), tag) != 1) {
error_print();
return -1;
}
if (gcm_decrypt(&block_key, iv, sizeof(iv), aad, sizeof(aad), out, sizeof(out), tag, sizeof(tag), buf) != 1) {
error_print();
return -1;
}
if (memcmp(buf, in, sizeof(in)) != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(int argc, char **argv)
{
if (test_ghash() != 1) goto err;
if (test_gcm() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,102 +7,103 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <gmssl/hex.h>
#include <gmssl/gf128.h>
#include <gmssl/error.h>
int test_gf128_from_hex(void)
{
char *tests[] = {
"00000000000000000000000000000000",
"00000000000000000000000000000001",
"10000000000000000000000000000000",
"de300f9301a499a965f8bf677e99e80d",
"14b267838ec9ef1bb7b5ce8c19e34bc6",
};
gf128_t a;
int i;
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
a = gf128_from_hex(tests[i]);
if (gf128_equ_hex(a, tests[i]) != 1) {
error_print();
return -1;
}
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int test_gf128_mul2(void)
{
char *tests[] = {
"00000000000000000000000000000001",
"de300f9301a499a965f8bf677e99e80d",
};
char *results[] = {
"e1000000000000000000000000000000",
"8e1807c980d24cd4b2fc5fb3bf4cf406",
};
gf128_t a;
int i;
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
a = gf128_from_hex(tests[i]);
a = gf128_mul2(a);
if (gf128_equ_hex(a, results[i]) != 1) {
error_print();
return -1;
}
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int test_gf128_mul(void)
{
char *hex_a = "de300f9301a499a965f8bf677e99e80d";
char *hex_b = "14b267838ec9ef1bb7b5ce8c19e34bc6";
char *hex_add_a_b = "ca8268108f6d76b2d24d71eb677aa3cb";
char *hex_mul_a_b = "7d87dda57a20b0c51d9743071ab14010";
gf128_t a, b, r;
a = gf128_from_hex(hex_a);
b = gf128_from_hex(hex_b);
r = gf128_add(a, b);
if (gf128_equ_hex(r, hex_add_a_b) != 1) {
error_print();
return -1;
}
r = gf128_mul(a, b);
if (gf128_equ_hex(r, hex_mul_a_b) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_gf128_from_hex() != 1) goto err;
if (test_gf128_mul2() != 1) goto err;
if (test_gf128_mul() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <gmssl/hex.h>
#include <gmssl/gf128.h>
#include <gmssl/error.h>
int test_gf128_from_hex(void)
{
char *tests[] = {
"00000000000000000000000000000000",
"00000000000000000000000000000001",
"10000000000000000000000000000000",
"de300f9301a499a965f8bf677e99e80d",
"14b267838ec9ef1bb7b5ce8c19e34bc6",
};
gf128_t a;
int i;
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
a = gf128_from_hex(tests[i]);
if (gf128_equ_hex(a, tests[i]) != 1) {
error_print();
return -1;
}
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int test_gf128_mul2(void)
{
char *tests[] = {
"00000000000000000000000000000001",
"de300f9301a499a965f8bf677e99e80d",
};
char *results[] = {
"e1000000000000000000000000000000",
"8e1807c980d24cd4b2fc5fb3bf4cf406",
};
gf128_t a;
int i;
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
a = gf128_from_hex(tests[i]);
a = gf128_mul2(a);
if (gf128_equ_hex(a, results[i]) != 1) {
error_print();
return -1;
}
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int test_gf128_mul(void)
{
char *hex_a = "de300f9301a499a965f8bf677e99e80d";
char *hex_b = "14b267838ec9ef1bb7b5ce8c19e34bc6";
char *hex_add_a_b = "ca8268108f6d76b2d24d71eb677aa3cb";
char *hex_mul_a_b = "7d87dda57a20b0c51d9743071ab14010";
gf128_t a, b, r;
a = gf128_from_hex(hex_a);
b = gf128_from_hex(hex_b);
r = gf128_add(a, b);
if (gf128_equ_hex(r, hex_add_a_b) != 1) {
error_print();
return -1;
}
r = gf128_mul(a, b);
if (gf128_equ_hex(r, hex_mul_a_b) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_gf128_from_hex() != 1) goto err;
if (test_gf128_mul2() != 1) goto err;
if (test_gf128_mul() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,86 +7,87 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/hex.h>
#include <gmssl/digest.h>
#include <gmssl/hash_drbg.h>
#include <gmssl/error.h>
#define EntropyInput "212956390783381dbfc6362dd0da9a09"
#define Nonce "5280987fc5e27a49"
#define PersonalizationString ""
#define AdditionalInput ""
#define V0 "02b84eba8121ca090b6b66d3371609eaf76405a5c2807d80035c1a13dfed5aa18e536af599a7b3c68b2c56240ed11997f4048910d84604"
#define C0 "a677e4921587563eebe55d1b25e59c3f3d200bc61aaee665e7a6858c2857c45dba4bce8182252962ae86de491046a5e3450eec44938a0a"
#define AdditionalInput1 ""
#define EntropyInputPR1 "2edb396eeb8960f77943c2a59075a786"
#define V1 "f9afadfbbf2c3d1004f9baca38be247342e5fbb83281915d5de18beb963712a344e89bb0e6b925a7bbc32eadb8b441efc1fa0c649df42a"
#define C1 "1d41cbbd634909e4761c232fcfd6a6c2edf0a7f4d3d3c164f74a88955f355efce2d86c1e9fa897b7005ef9d4d3a51bf4fc0b805ab896c9"
#define PR1 "2edb396eeb8960f77943c2a59075a786"
#define PR2 "30b565b63a5012676940d3ef17d9e996"
int main(void)
{
HASH_DRBG drbg;
uint8_t entropy[sizeof(EntropyInput)/2];
uint8_t nonce[sizeof(Nonce)/2];
uint8_t personalstr[sizeof(PersonalizationString)/2];
uint8_t v[sizeof(V0)/2];
uint8_t c[sizeof(C0)/2];
uint8_t entropy_pr1[sizeof(EntropyInputPR1)/2];
uint8_t pr1[sizeof(PR1)/2];
uint8_t pr2[sizeof(PR2)/2];
size_t entropy_len, nonce_len, personalstr_len, vlen, clen;
size_t entropy_pr1len;
size_t pr1_len, pr2_len;
unsigned char out[640/8];
int i;
hex_to_bytes(EntropyInput, strlen(EntropyInput), entropy, &entropy_len);
hex_to_bytes(Nonce, strlen(Nonce), nonce, &nonce_len);
hex_to_bytes(PersonalizationString, strlen(PersonalizationString), personalstr, &personalstr_len);
hex_to_bytes(V0, strlen(V0), v, &vlen);
hex_to_bytes(C0, strlen(C0), c, &clen);
hex_to_bytes(EntropyInputPR1, strlen(EntropyInputPR1), entropy_pr1, &entropy_pr1len);
hex_to_bytes(PR1, strlen(PR1), pr1, &pr1_len);
hex_to_bytes(PR2, strlen(PR2), pr2, &pr2_len);
hash_drbg_init(&drbg, DIGEST_sha1(),
entropy, entropy_len,
nonce, nonce_len,
personalstr, personalstr_len);
printf("sha1_drbg test 1 ");
if (drbg.seedlen != vlen
|| memcmp(drbg.V, v, vlen) != 0
|| memcmp(drbg.C, c, clen) != 0
|| drbg.reseed_counter != 1) {
printf("failed\n");
return 1;
} else {
printf("ok\n");
}
hash_drbg_reseed(&drbg, pr1, pr1_len, NULL, 0);
hash_drbg_generate(&drbg, NULL, 0, 640/8, out);
hash_drbg_reseed(&drbg, pr2, pr2_len, NULL, 0);
hash_drbg_generate(&drbg, NULL, 0, 640/8, out);
for (i = 0; i < sizeof(out); i++) {
printf("%02x", out[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/hex.h>
#include <gmssl/digest.h>
#include <gmssl/hash_drbg.h>
#include <gmssl/error.h>
#define EntropyInput "212956390783381dbfc6362dd0da9a09"
#define Nonce "5280987fc5e27a49"
#define PersonalizationString ""
#define AdditionalInput ""
#define V0 "02b84eba8121ca090b6b66d3371609eaf76405a5c2807d80035c1a13dfed5aa18e536af599a7b3c68b2c56240ed11997f4048910d84604"
#define C0 "a677e4921587563eebe55d1b25e59c3f3d200bc61aaee665e7a6858c2857c45dba4bce8182252962ae86de491046a5e3450eec44938a0a"
#define AdditionalInput1 ""
#define EntropyInputPR1 "2edb396eeb8960f77943c2a59075a786"
#define V1 "f9afadfbbf2c3d1004f9baca38be247342e5fbb83281915d5de18beb963712a344e89bb0e6b925a7bbc32eadb8b441efc1fa0c649df42a"
#define C1 "1d41cbbd634909e4761c232fcfd6a6c2edf0a7f4d3d3c164f74a88955f355efce2d86c1e9fa897b7005ef9d4d3a51bf4fc0b805ab896c9"
#define PR1 "2edb396eeb8960f77943c2a59075a786"
#define PR2 "30b565b63a5012676940d3ef17d9e996"
int main(void)
{
HASH_DRBG drbg;
uint8_t entropy[sizeof(EntropyInput)/2];
uint8_t nonce[sizeof(Nonce)/2];
uint8_t personalstr[sizeof(PersonalizationString)/2];
uint8_t v[sizeof(V0)/2];
uint8_t c[sizeof(C0)/2];
uint8_t entropy_pr1[sizeof(EntropyInputPR1)/2];
uint8_t pr1[sizeof(PR1)/2];
uint8_t pr2[sizeof(PR2)/2];
size_t entropy_len, nonce_len, personalstr_len, vlen, clen;
size_t entropy_pr1len;
size_t pr1_len, pr2_len;
unsigned char out[640/8];
int i;
hex_to_bytes(EntropyInput, strlen(EntropyInput), entropy, &entropy_len);
hex_to_bytes(Nonce, strlen(Nonce), nonce, &nonce_len);
hex_to_bytes(PersonalizationString, strlen(PersonalizationString), personalstr, &personalstr_len);
hex_to_bytes(V0, strlen(V0), v, &vlen);
hex_to_bytes(C0, strlen(C0), c, &clen);
hex_to_bytes(EntropyInputPR1, strlen(EntropyInputPR1), entropy_pr1, &entropy_pr1len);
hex_to_bytes(PR1, strlen(PR1), pr1, &pr1_len);
hex_to_bytes(PR2, strlen(PR2), pr2, &pr2_len);
hash_drbg_init(&drbg, DIGEST_sha1(),
entropy, entropy_len,
nonce, nonce_len,
personalstr, personalstr_len);
printf("sha1_drbg test 1 ");
if (drbg.seedlen != vlen
|| memcmp(drbg.V, v, vlen) != 0
|| memcmp(drbg.C, c, clen) != 0
|| drbg.reseed_counter != 1) {
printf("failed\n");
return 1;
} else {
printf("ok\n");
}
hash_drbg_reseed(&drbg, pr1, pr1_len, NULL, 0);
hash_drbg_generate(&drbg, NULL, 0, 640/8, out);
hash_drbg_reseed(&drbg, pr2, pr2_len, NULL, 0);
hash_drbg_generate(&drbg, NULL, 0, 640/8, out);
for (i = 0; i < sizeof(out); i++) {
printf("%02x", out[i]);
}
printf("\n");
return 0;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,13 +7,14 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/hex.h>
int main(void)
{
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/hex.h>
int main(void)
{
return 0;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,210 +7,211 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/hex.h>
#include <gmssl/hkdf.h>
#include <gmssl/error.h>
static struct {
char *algor;
char *ikm;
char *salt;
char *info;
int L;
char *prk;
char *okm;
} hkdf_tests[] = {
{
// test 1
"sha256",
"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
"000102030405060708090a0b0c",
"f0f1f2f3f4f5f6f7f8f9",
42,
"077709362c2e32df0ddc3f0dc47bba63"
"90b6c73bb50f9c3122ec844ad7c2b3e5",
"3cb25f25faacd57a90434f64d0362f2a"
"2d2d0a90cf1a5a4c5db02d56ecc4c5bf"
"34007208d5b887185865",
},
{
// test 2
"sha256",
"000102030405060708090a0b0c0d0e0f"
"101112131415161718191a1b1c1d1e1f"
"202122232425262728292a2b2c2d2e2f"
"303132333435363738393a3b3c3d3e3f"
"404142434445464748494a4b4c4d4e4f",
"606162636465666768696a6b6c6d6e6f"
"707172737475767778797a7b7c7d7e7f"
"808182838485868788898a8b8c8d8e8f"
"909192939495969798999a9b9c9d9e9f"
"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
82,
"06a6b88c5853361a06104c9ceb35b45c"
"ef760014904671014a193f40c15fc244",
"b11e398dc80327a1c8e7f78c596a4934"
"4f012eda2d4efad8a050cc4c19afa97c"
"59045a99cac7827271cb41c65e590e09"
"da3275600c2f09b8367793a9aca3db71"
"cc30c58179ec3e87c14c01d5c1f3434f"
"1d87",
},
{
// test 3
"sha256",
"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
"",
"",
42,
"19ef24a32c717b167f33a91d6f648bdf"
"96596776afdb6377ac434c1c293ccb04",
"8da4e775a563c18f715f802a063c5a31"
"b8a11f5c5ee1879ec3454e5f3c738d2d"
"9d201395faa4b61a96c8",
},
{
// test 4
"sha1",
"0b0b0b0b0b0b0b0b0b0b0b",
"000102030405060708090a0b0c",
"f0f1f2f3f4f5f6f7f8f9",
42,
"9b6c18c432a7bf8f0e71c8eb88f4b30baa2ba243",
"085a01ea1b10f36933068b56efa5ad81"
"a4f14b822f5b091568a9cdd4f155fda2"
"c22e422478d305f3f896",
},
{
// test 5
"sha1",
"000102030405060708090a0b0c0d0e0f"
"101112131415161718191a1b1c1d1e1f"
"202122232425262728292a2b2c2d2e2f"
"303132333435363738393a3b3c3d3e3f"
"404142434445464748494a4b4c4d4e4f",
"606162636465666768696a6b6c6d6e6f"
"707172737475767778797a7b7c7d7e7f"
"808182838485868788898a8b8c8d8e8f"
"909192939495969798999a9b9c9d9e9f"
"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
82,
"8adae09a2a307059478d309b26c4115a224cfaf6",
"0bd770a74d1160f7c9f12cd5912a06eb"
"ff6adcae899d92191fe4305673ba2ffe"
"8fa3f1a4e5ad79f3f334b3b202b2173c"
"486ea37ce3d397ed034c7f9dfeb15c5e"
"927336d0441f4c4300e2cff0d0900b52"
"d3b4",
},
{
// test 6
"sha1",
"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
"",
"",
42,
"da8c8a73c7fa77288ec6f5e7c297786aa0d32d01",
"0ac1af7002b3d761d1e55298da9d0506"
"b9ae52057220a306e07b6b87e8df21d0"
"ea00033de03984d34918"
},
{
// test 7
"sha1",
"0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
"",
"",
42,
"2adccada18779e7c2077ad2eb19d3f3e731385dd",
"2c91117204d745f3500d636a62f64f0a"
"b3bae548aa53d423b0d1f27ebba6f5e5"
"673a081d70cce7acfc48",
},
};
int test_hkdf(void)
{
int i;
const DIGEST *digest;
uint8_t ikm[512];
uint8_t salt[512];
uint8_t info[512];
uint8_t prk[512];
uint8_t okm[512];
size_t ikmlen, saltlen, infolen, prklen, okmlen;
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);
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;
prklen = strlen(hkdf_tests[i].prk)/2;
okmlen = strlen(hkdf_tests[i].okm)/2;
L = hkdf_tests[i].L;
printf("test %d\n", i + 1);
format_print(stdout, 0, 0, "Hash = %s\n", digest_name(digest));
format_bytes(stdout, 0, 0, "IKM = ", ikm, ikmlen);
format_bytes(stdout, 0, 0, "salt = ", salt, saltlen);
format_bytes(stdout, 0, 0, "info = ", info, infolen);
format_print(stdout, 0, 0, "L = %zu\n", L);
if (hkdf_extract(digest, salt, saltlen, ikm, ikmlen, buf, &buflen) != 1) {
error_print();
return -1;
}
format_bytes(stdout, 0, 0, "PRK = ", buf, buflen);
format_bytes(stdout, 0, 0, " = ", prk, prklen);
if (buflen != prklen || memcmp(buf, prk, prklen) != 0) {
error_print();
return -1;
}
if (hkdf_expand(digest, prk, prklen, info, infolen, L, buf) != 1) {
error_print();
return -1;
}
format_bytes(stdout, 0, 0, "OKM = ", buf, L);
format_bytes(stdout, 0, 0, " = ", okm, okmlen);
if (L != okmlen || memcmp(buf, okm, okmlen) != 0) {
error_print();
return -1;
}
printf("\n");
}
return 0;
}
int main(void)
{
int err = 0;
err += test_hkdf();
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/hex.h>
#include <gmssl/hkdf.h>
#include <gmssl/error.h>
static struct {
char *algor;
char *ikm;
char *salt;
char *info;
int L;
char *prk;
char *okm;
} hkdf_tests[] = {
{
// test 1
"sha256",
"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
"000102030405060708090a0b0c",
"f0f1f2f3f4f5f6f7f8f9",
42,
"077709362c2e32df0ddc3f0dc47bba63"
"90b6c73bb50f9c3122ec844ad7c2b3e5",
"3cb25f25faacd57a90434f64d0362f2a"
"2d2d0a90cf1a5a4c5db02d56ecc4c5bf"
"34007208d5b887185865",
},
{
// test 2
"sha256",
"000102030405060708090a0b0c0d0e0f"
"101112131415161718191a1b1c1d1e1f"
"202122232425262728292a2b2c2d2e2f"
"303132333435363738393a3b3c3d3e3f"
"404142434445464748494a4b4c4d4e4f",
"606162636465666768696a6b6c6d6e6f"
"707172737475767778797a7b7c7d7e7f"
"808182838485868788898a8b8c8d8e8f"
"909192939495969798999a9b9c9d9e9f"
"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
82,
"06a6b88c5853361a06104c9ceb35b45c"
"ef760014904671014a193f40c15fc244",
"b11e398dc80327a1c8e7f78c596a4934"
"4f012eda2d4efad8a050cc4c19afa97c"
"59045a99cac7827271cb41c65e590e09"
"da3275600c2f09b8367793a9aca3db71"
"cc30c58179ec3e87c14c01d5c1f3434f"
"1d87",
},
{
// test 3
"sha256",
"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
"",
"",
42,
"19ef24a32c717b167f33a91d6f648bdf"
"96596776afdb6377ac434c1c293ccb04",
"8da4e775a563c18f715f802a063c5a31"
"b8a11f5c5ee1879ec3454e5f3c738d2d"
"9d201395faa4b61a96c8",
},
{
// test 4
"sha1",
"0b0b0b0b0b0b0b0b0b0b0b",
"000102030405060708090a0b0c",
"f0f1f2f3f4f5f6f7f8f9",
42,
"9b6c18c432a7bf8f0e71c8eb88f4b30baa2ba243",
"085a01ea1b10f36933068b56efa5ad81"
"a4f14b822f5b091568a9cdd4f155fda2"
"c22e422478d305f3f896",
},
{
// test 5
"sha1",
"000102030405060708090a0b0c0d0e0f"
"101112131415161718191a1b1c1d1e1f"
"202122232425262728292a2b2c2d2e2f"
"303132333435363738393a3b3c3d3e3f"
"404142434445464748494a4b4c4d4e4f",
"606162636465666768696a6b6c6d6e6f"
"707172737475767778797a7b7c7d7e7f"
"808182838485868788898a8b8c8d8e8f"
"909192939495969798999a9b9c9d9e9f"
"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf",
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff",
82,
"8adae09a2a307059478d309b26c4115a224cfaf6",
"0bd770a74d1160f7c9f12cd5912a06eb"
"ff6adcae899d92191fe4305673ba2ffe"
"8fa3f1a4e5ad79f3f334b3b202b2173c"
"486ea37ce3d397ed034c7f9dfeb15c5e"
"927336d0441f4c4300e2cff0d0900b52"
"d3b4",
},
{
// test 6
"sha1",
"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b",
"",
"",
42,
"da8c8a73c7fa77288ec6f5e7c297786aa0d32d01",
"0ac1af7002b3d761d1e55298da9d0506"
"b9ae52057220a306e07b6b87e8df21d0"
"ea00033de03984d34918"
},
{
// test 7
"sha1",
"0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c",
"",
"",
42,
"2adccada18779e7c2077ad2eb19d3f3e731385dd",
"2c91117204d745f3500d636a62f64f0a"
"b3bae548aa53d423b0d1f27ebba6f5e5"
"673a081d70cce7acfc48",
},
};
int test_hkdf(void)
{
int i;
const DIGEST *digest;
uint8_t ikm[512];
uint8_t salt[512];
uint8_t info[512];
uint8_t prk[512];
uint8_t okm[512];
size_t ikmlen, saltlen, infolen, prklen, okmlen;
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);
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;
prklen = strlen(hkdf_tests[i].prk)/2;
okmlen = strlen(hkdf_tests[i].okm)/2;
L = hkdf_tests[i].L;
printf("test %d\n", i + 1);
format_print(stdout, 0, 0, "Hash = %s\n", digest_name(digest));
format_bytes(stdout, 0, 0, "IKM = ", ikm, ikmlen);
format_bytes(stdout, 0, 0, "salt = ", salt, saltlen);
format_bytes(stdout, 0, 0, "info = ", info, infolen);
format_print(stdout, 0, 0, "L = %zu\n", L);
if (hkdf_extract(digest, salt, saltlen, ikm, ikmlen, buf, &buflen) != 1) {
error_print();
return -1;
}
format_bytes(stdout, 0, 0, "PRK = ", buf, buflen);
format_bytes(stdout, 0, 0, " = ", prk, prklen);
if (buflen != prklen || memcmp(buf, prk, prklen) != 0) {
error_print();
return -1;
}
if (hkdf_expand(digest, prk, prklen, info, infolen, L, buf) != 1) {
error_print();
return -1;
}
format_bytes(stdout, 0, 0, "OKM = ", buf, L);
format_bytes(stdout, 0, 0, " = ", okm, okmlen);
if (L != okmlen || memcmp(buf, okm, okmlen) != 0) {
error_print();
return -1;
}
printf("\n");
}
return 0;
}
int main(void)
{
int err = 0;
err += test_hkdf();
return err;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,114 +7,115 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/hex.h>
#include <gmssl/hmac.h>
// FIXME: md5, sha1, sm3 test vectors
struct {
char *key;
char *data;
char *hmac_sha224;
char *hmac_sha256;
char *hmac_sha384;
char *hmac_sha512;
} hmac_tests[] = {
// rfc 4231 test vectors
{
"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"
"0b0b0b0b",
"4869205468657265",
"896fb1128abbdf196832107cd49df33f"
"47b4b1169912ba4f53684b22",
"b0344c61d8db38535ca8afceaf0bf12b"
"881dc200c9833da726e9376c2e32cff7",
"afd03944d84895626b0825f4ab46907f"
"15f9dadbe4101ec682aa034c7cebc59c"
"faea9ea9076ede7f4af152e8b2fa9cb6",
"87aa7cdea5ef619d4ff0b4241a1d6cb0"
"2379f4e2ce4ec2787ad0b30545e17cde"
"daa833b7d6b8a702038b274eaea3f4e4"
"be9d914eeb61f1702e696c203a126854",
},
{
"4a656665",
"7768617420646f2079612077616e7420"
"666f72206e6f7468696e673f",
"a30e01098bc6dbbf45690f3a7e9e6d0f"
"8bbea2a39e6148008fd05e44",
"5bdcc146bf60754e6a042426089575c7"
"5a003f089d2739839dec58b964ec3843",
"af45d2e376484031617f78d2b58a6b1b"
"9c7ef464f5a01b47e42ec3736322445e"
"8e2240ca5e69e2c78b3239ecfab21649",
"164b7a7bfcf819e2e395fbe73b56e0a3"
"87bd64222e831fd610270cd7ea250554"
"9758bf75c05a994a6d034f65f8f0e6fd"
"caeab1a34d4a6b4b636e070a38bce737",
},
{
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaa",
"dddddddddddddddddddddddddddddddd"
"dddddddddddddddddddddddddddddddd"
"dddddddddddddddddddddddddddddddd"
"dddd",
"7fb3cb3588c6c1f6ffa9694d7d6ad264"
"9365b0c1f65d69d1ec8333ea",
"773ea91e36800e46854db8ebd09181a7"
"2959098b3ef8c122d9635514ced565fe",
"88062608d3e6ad8a0aa2ace014c8a86f"
"0aa635d947ac9febe83ef4e55966144b"
"2a5ab39dc13814b94e3ab6e101a34f27",
"fa73b0089d56a284efb0f0756c890be9"
"b1b5dbdd8ee81a3655f83e33b2279d39"
"bf3e848279a722c806b485a47e67c807"
"b946a337bee8942674278859e13292fb",
},
};
int test_hmac(const DIGEST *digest, const char *key_hex, const char *data_hex, const char *hmac_hex)
{
HMAC_CTX ctx;
uint8_t key[strlen(key_hex)/2];
uint8_t data[strlen(data_hex)/2];
uint8_t hmac[strlen(hmac_hex)/2];
uint8_t buf[64];
size_t len;
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));
hmac_finish(&ctx, buf, &len);
if (len != sizeof(hmac) || memcmp(buf, hmac, sizeof(hmac)) != 0) {
printf("failed\n");
return 0;
}
printf("ok\n");
return 1;
}
int main(void)
{
int i;
for (i = 0; i < sizeof(hmac_tests)/sizeof(hmac_tests[0]); i++) {
test_hmac(DIGEST_sha224(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha224);
test_hmac(DIGEST_sha256(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha256);
test_hmac(DIGEST_sha384(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha384);
test_hmac(DIGEST_sha512(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha512);
};
return 0;
};
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/hex.h>
#include <gmssl/hmac.h>
// FIXME: md5, sha1, sm3 test vectors
struct {
char *key;
char *data;
char *hmac_sha224;
char *hmac_sha256;
char *hmac_sha384;
char *hmac_sha512;
} hmac_tests[] = {
// rfc 4231 test vectors
{
"0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"
"0b0b0b0b",
"4869205468657265",
"896fb1128abbdf196832107cd49df33f"
"47b4b1169912ba4f53684b22",
"b0344c61d8db38535ca8afceaf0bf12b"
"881dc200c9833da726e9376c2e32cff7",
"afd03944d84895626b0825f4ab46907f"
"15f9dadbe4101ec682aa034c7cebc59c"
"faea9ea9076ede7f4af152e8b2fa9cb6",
"87aa7cdea5ef619d4ff0b4241a1d6cb0"
"2379f4e2ce4ec2787ad0b30545e17cde"
"daa833b7d6b8a702038b274eaea3f4e4"
"be9d914eeb61f1702e696c203a126854",
},
{
"4a656665",
"7768617420646f2079612077616e7420"
"666f72206e6f7468696e673f",
"a30e01098bc6dbbf45690f3a7e9e6d0f"
"8bbea2a39e6148008fd05e44",
"5bdcc146bf60754e6a042426089575c7"
"5a003f089d2739839dec58b964ec3843",
"af45d2e376484031617f78d2b58a6b1b"
"9c7ef464f5a01b47e42ec3736322445e"
"8e2240ca5e69e2c78b3239ecfab21649",
"164b7a7bfcf819e2e395fbe73b56e0a3"
"87bd64222e831fd610270cd7ea250554"
"9758bf75c05a994a6d034f65f8f0e6fd"
"caeab1a34d4a6b4b636e070a38bce737",
},
{
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
"aaaaaaaa",
"dddddddddddddddddddddddddddddddd"
"dddddddddddddddddddddddddddddddd"
"dddddddddddddddddddddddddddddddd"
"dddd",
"7fb3cb3588c6c1f6ffa9694d7d6ad264"
"9365b0c1f65d69d1ec8333ea",
"773ea91e36800e46854db8ebd09181a7"
"2959098b3ef8c122d9635514ced565fe",
"88062608d3e6ad8a0aa2ace014c8a86f"
"0aa635d947ac9febe83ef4e55966144b"
"2a5ab39dc13814b94e3ab6e101a34f27",
"fa73b0089d56a284efb0f0756c890be9"
"b1b5dbdd8ee81a3655f83e33b2279d39"
"bf3e848279a722c806b485a47e67c807"
"b946a337bee8942674278859e13292fb",
},
};
int test_hmac(const DIGEST *digest, const char *key_hex, const char *data_hex, const char *hmac_hex)
{
HMAC_CTX ctx;
uint8_t key[strlen(key_hex)/2];
uint8_t data[strlen(data_hex)/2];
uint8_t hmac[strlen(hmac_hex)/2];
uint8_t buf[64];
size_t len;
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));
hmac_finish(&ctx, buf, &len);
if (len != sizeof(hmac) || memcmp(buf, hmac, sizeof(hmac)) != 0) {
printf("failed\n");
return 0;
}
printf("ok\n");
return 1;
}
int main(void)
{
int i;
for (i = 0; i < sizeof(hmac_tests)/sizeof(hmac_tests[0]); i++) {
test_hmac(DIGEST_sha224(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha224);
test_hmac(DIGEST_sha256(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha256);
test_hmac(DIGEST_sha384(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha384);
test_hmac(DIGEST_sha512(), hmac_tests[i].key, hmac_tests[i].data, hmac_tests[i].hmac_sha512);
};
return 0;
};

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,63 +7,64 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/md5.h>
#include <gmssl/hex.h>
static char *teststr[] = {
"",
"a",
"abc",
"message digest",
"abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
};
static char *dgsthex[] = {
"d41d8cd98f00b204e9800998ecf8427e",
"0cc175b9c0f1b6a831c399e269772661",
"900150983cd24fb0d6963f7d28e17f72",
"f96b697d7cb7938d525a2f31aaf161d0",
"c3fcd3d76192e4007dfb496cca67e13b",
"d174ab98d277d9f5a5611c2c9f419d9f",
"57edf4a22be3c955ac49da2e2107b67a",
};
int main(int argc, char **argv)
{
int err = 0;
char *p;
uint8_t dgst[16];
uint8_t dgstbuf[16];
size_t dgstbuflen;
size_t i;
for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) {
hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstbuflen);
md5_digest((uint8_t *)teststr[i], strlen(teststr[i]), dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
int n;
printf("error calculating MD5 on %s\n", teststr[i]);
printf(" digest(corret) = ");
for (n = 0; n < sizeof(dgst); n++) {
printf("%02X", dgst[n]);
}
printf("\n");
printf(" digest(error) = %s\n", dgsthex[i]);
err++;
} else {
printf("md5 test %lu ok\n", i+1);
}
}
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/md5.h>
#include <gmssl/hex.h>
static char *teststr[] = {
"",
"a",
"abc",
"message digest",
"abcdefghijklmnopqrstuvwxyz",
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
};
static char *dgsthex[] = {
"d41d8cd98f00b204e9800998ecf8427e",
"0cc175b9c0f1b6a831c399e269772661",
"900150983cd24fb0d6963f7d28e17f72",
"f96b697d7cb7938d525a2f31aaf161d0",
"c3fcd3d76192e4007dfb496cca67e13b",
"d174ab98d277d9f5a5611c2c9f419d9f",
"57edf4a22be3c955ac49da2e2107b67a",
};
int main(int argc, char **argv)
{
int err = 0;
char *p;
uint8_t dgst[16];
uint8_t dgstbuf[16];
size_t dgstbuflen;
size_t i;
for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) {
hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstbuflen);
md5_digest((uint8_t *)teststr[i], strlen(teststr[i]), dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
int n;
printf("error calculating MD5 on %s\n", teststr[i]);
printf(" digest(corret) = ");
for (n = 0; n < sizeof(dgst); n++) {
printf("%02X", dgst[n]);
}
printf("\n");
printf(" digest(error) = %s\n", dgsthex[i]);
err++;
} else {
printf("md5 test %lu ok\n", i+1);
}
}
return err;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,130 +7,131 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/hex.h>
#include <gmssl/pbkdf2.h>
#include <gmssl/error.h>
struct {
char *pass;
char *salt;
int iter;
int dklen;
char *dk;
} pbkdf2_hmac_sha1_tests[] = {
// rfc 6070 test vectors for pbkdf2-hmac-sha1
{
"password",
"salt",
1,
20,
"0c60c80f961f0e71f3a9b524af6012062fe037a6",
},
{
"password",
"salt",
2,
20,
"ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957",
},
{
"password",
"salt",
4096,
20,
"4b007901b765489abead49d926f721d065a429c1",
},
/*
{
"password",
"salt",
16777216, // very slow
20,
"eefe3d61cd4da4e4e9945b3d6ba2158c2634e984",
},
*/
{
"passwordPASSWORDpassword",
"saltSALTsaltSALTsaltSALTsaltSALTsalt",
4096,
25,
"3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038",
},
};
/*
void test(void)
{
HMAC_CTX ctx;
uint8_t iter[4] = {0, 0, 0, 1};
uint8_t mac[20];
size_t len;
int i;
hmac_init(&ctx, DIGEST_sha1(), (uint8_t *)"password", 8);
hmac_update(&ctx, (uint8_t *)"salt", 4);
hmac_update(&ctx, iter, 4);
hmac_finish(&ctx, mac, &len);
for (i = 1; i < 4096; i++) {
uint8_t buf[20];
memset(&ctx, 0, sizeof(HMAC_CTX));
hmac_init(&ctx, DIGEST_sha1(), (uint8_t *)"password", 8);
hmac_update(&ctx, mac, len);
hmac_finish(&ctx, buf, &len);
int j;
for (j = 0; j < len; j++) {
mac[j] ^= buf[j];
}
}
for (i = 0; i < len; i++) {
printf("%02x", mac[i]);
}
printf("\n");
}
*/
static int test_pbkdf2_genkey(void)
{
int i;
uint8_t key[64];
uint8_t buf[64];
size_t len;
for (i = 0; i < sizeof(pbkdf2_hmac_sha1_tests)/sizeof(pbkdf2_hmac_sha1_tests[0]); i++) {
hex_to_bytes(pbkdf2_hmac_sha1_tests[i].dk, strlen(pbkdf2_hmac_sha1_tests[i].dk), buf, &len);
if (pbkdf2_genkey(DIGEST_sha1(),
pbkdf2_hmac_sha1_tests[i].pass, strlen(pbkdf2_hmac_sha1_tests[i].pass),
(uint8_t *)pbkdf2_hmac_sha1_tests[i].salt, strlen(pbkdf2_hmac_sha1_tests[i].salt),
pbkdf2_hmac_sha1_tests[i].iter, pbkdf2_hmac_sha1_tests[i].dklen, key) != 1) {
error_print();
return -1;
}
if (memcmp(key, buf, pbkdf2_hmac_sha1_tests[i].dklen) != 0) {
fprintf(stderr, "test_pbkdf2_genkey test %d failed\n", i);
return -1;
} else {
fprintf(stderr, "test_pbkdf2_genkey test %d ok\n", i);
}
}
printf("%s() ok\n", __FUNCTION__);
return 0;
}
int main(int argc, char **argv)
{
int err = 0;
err += test_pbkdf2_genkey();
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/hex.h>
#include <gmssl/pbkdf2.h>
#include <gmssl/error.h>
struct {
char *pass;
char *salt;
int iter;
int dklen;
char *dk;
} pbkdf2_hmac_sha1_tests[] = {
// rfc 6070 test vectors for pbkdf2-hmac-sha1
{
"password",
"salt",
1,
20,
"0c60c80f961f0e71f3a9b524af6012062fe037a6",
},
{
"password",
"salt",
2,
20,
"ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957",
},
{
"password",
"salt",
4096,
20,
"4b007901b765489abead49d926f721d065a429c1",
},
/*
{
"password",
"salt",
16777216, // very slow
20,
"eefe3d61cd4da4e4e9945b3d6ba2158c2634e984",
},
*/
{
"passwordPASSWORDpassword",
"saltSALTsaltSALTsaltSALTsaltSALTsalt",
4096,
25,
"3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038",
},
};
/*
void test(void)
{
HMAC_CTX ctx;
uint8_t iter[4] = {0, 0, 0, 1};
uint8_t mac[20];
size_t len;
int i;
hmac_init(&ctx, DIGEST_sha1(), (uint8_t *)"password", 8);
hmac_update(&ctx, (uint8_t *)"salt", 4);
hmac_update(&ctx, iter, 4);
hmac_finish(&ctx, mac, &len);
for (i = 1; i < 4096; i++) {
uint8_t buf[20];
memset(&ctx, 0, sizeof(HMAC_CTX));
hmac_init(&ctx, DIGEST_sha1(), (uint8_t *)"password", 8);
hmac_update(&ctx, mac, len);
hmac_finish(&ctx, buf, &len);
int j;
for (j = 0; j < len; j++) {
mac[j] ^= buf[j];
}
}
for (i = 0; i < len; i++) {
printf("%02x", mac[i]);
}
printf("\n");
}
*/
static int test_pbkdf2_genkey(void)
{
int i;
uint8_t key[64];
uint8_t buf[64];
size_t len;
for (i = 0; i < sizeof(pbkdf2_hmac_sha1_tests)/sizeof(pbkdf2_hmac_sha1_tests[0]); i++) {
hex_to_bytes(pbkdf2_hmac_sha1_tests[i].dk, strlen(pbkdf2_hmac_sha1_tests[i].dk), buf, &len);
if (pbkdf2_genkey(DIGEST_sha1(),
pbkdf2_hmac_sha1_tests[i].pass, strlen(pbkdf2_hmac_sha1_tests[i].pass),
(uint8_t *)pbkdf2_hmac_sha1_tests[i].salt, strlen(pbkdf2_hmac_sha1_tests[i].salt),
pbkdf2_hmac_sha1_tests[i].iter, pbkdf2_hmac_sha1_tests[i].dklen, key) != 1) {
error_print();
return -1;
}
if (memcmp(key, buf, pbkdf2_hmac_sha1_tests[i].dklen) != 0) {
fprintf(stderr, "test_pbkdf2_genkey test %d failed\n", i);
return -1;
} else {
fprintf(stderr, "test_pbkdf2_genkey test %d ok\n", i);
}
}
printf("%s() ok\n", __FUNCTION__);
return 0;
}
int main(int argc, char **argv)
{
int err = 0;
err += test_pbkdf2_genkey();
return err;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,13 +7,14 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/pem.h>
int main(void)
{
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/pem.h>
int main(void)
{
return 0;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,413 +7,414 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/sm2.h>
#include <gmssl/asn1.h>
#include <gmssl/pkcs8.h>
#include <gmssl/error.h>
static int test_pbkdf2_params(void)
{
uint8_t salt[8] = {0};
size_t saltlen;
int iter = 65536;
int keylen = 16;
int prf = OID_hmac_sm3;
uint8_t buf[128];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
const uint8_t *psalt;
if (pbkdf2_params_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pbkdf2_params_print(stderr, 0, 0, "PBKDF2-params", d, dlen);
p = buf;
cp = buf;
len = 0;
keylen = -1;
prf = -1;
if (pbkdf2_params_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pbkdf2_params_print(stderr, 0, 0, "PBKDF2-params", d, dlen);
p = buf;
cp = buf;
len = 0;
keylen = -1;
prf = -1;
if (pbkdf2_params_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1
|| pbkdf2_params_from_der(&psalt, &saltlen, &iter, &keylen, &prf, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 0, "PBKDF2-params\n");
format_bytes(stderr, 0, 4, "salt", psalt, saltlen);
format_print(stderr, 0, 4, "iterationCount: %d\n", iter);
format_print(stderr, 0, 4, "keyLength: %d\n", keylen);
format_print(stderr, 0, 4, "prf: %d\n", prf);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pbkdf2_algor(void)
{
uint8_t salt[8] = {0};
size_t saltlen;
int iter = 65536;
int keylen = 16;
int prf = OID_hmac_sm3;
uint8_t buf[128];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
const uint8_t *psalt;
if (pbkdf2_algor_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pbkdf2_algor_print(stderr, 0, 0, "PBKDF2", d, dlen);
p = buf;
cp = buf;
len = 0;
if (pbkdf2_algor_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 0, "PBKDF2\n");
format_bytes(stderr, 0, 4, "salt", psalt, saltlen);
format_print(stderr, 0, 4, "iterationCount: %d\n", iter);
format_print(stderr, 0, 4, "keyLength: %d\n", keylen);
format_print(stderr, 0, 4, "prf: %d\n", prf);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pbes2_enc_algor(void)
{
int cipher = OID_sm4_cbc;
uint8_t iv[16] = {1};
uint8_t buf[128];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
const uint8_t *piv;
size_t ivlen;
if (pbes2_enc_algor_to_der(cipher, iv, sizeof(iv), &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pbes2_enc_algor_print(stderr, 0, 0, "PBES2-Enc", d, dlen);
p = buf;
cp = buf;
len = 0;
if (pbes2_enc_algor_to_der(cipher, iv, sizeof(iv), &p, &len) != 1
|| pbes2_enc_algor_from_der(&cipher, &piv, &ivlen, &cp, &len) != 1
|| asn1_check(cipher == OID_sm4_cbc) != 1
|| asn1_check(ivlen == sizeof(iv)) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pbes2_params(void)
{
uint8_t salt[8] = {0};
size_t saltlen;
int iter = 65536;
int keylen = -1;
int prf = OID_hmac_sm3;
int cipher = OID_sm4_cbc;
uint8_t iv[16];
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
const uint8_t *psalt;
const uint8_t *piv;
size_t ivlen;
if (pbes2_params_to_der(salt, sizeof(salt), iter, keylen, prf, cipher, iv, sizeof(iv), &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pbes2_params_print(stderr, 0, 0, "PBES2-params", d, dlen);
p = buf;
cp = buf;
len = 0;
if (pbes2_params_to_der(salt, sizeof(salt), iter, keylen, prf, cipher, iv, sizeof(iv), &p, &len) != 1
|| pbes2_params_from_der(&psalt, &saltlen, &iter, &keylen, &prf, &cipher, &piv, &ivlen, &cp, &len) != 1
|| asn1_check(saltlen == sizeof(salt)) != 1
|| asn1_check(iter == 65536) != 1
|| asn1_check(keylen == -1) != 1
|| asn1_check(prf == OID_hmac_sm3) != 1
|| asn1_check(cipher == OID_sm4_cbc) != 1
|| asn1_check(ivlen == sizeof(iv)) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pbes2_algor(void)
{
uint8_t salt[8] = {0};
size_t saltlen;
int iter = 65536;
int keylen = -1;
int prf = OID_hmac_sm3;
int cipher = OID_sm4_cbc;
uint8_t iv[16];
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
const uint8_t *psalt;
const uint8_t *piv;
size_t ivlen;
if (pbes2_algor_to_der(salt, sizeof(salt), iter, keylen, prf, cipher, iv, sizeof(iv), &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pbes2_algor_print(stderr, 0, 0, "PBES2", d, dlen);
p = buf;
cp = buf;
len = 0;
if (pbes2_algor_to_der(salt, sizeof(salt), iter, keylen, prf, cipher, iv, sizeof(iv), &p, &len) != 1
|| pbes2_algor_from_der(&psalt, &saltlen, &iter, &keylen, &prf, &cipher, &piv, &ivlen, &cp, &len) != 1
|| asn1_check(saltlen == sizeof(salt)) != 1
|| asn1_check(iter == 65536) != 1
|| asn1_check(keylen == -1) != 1
|| asn1_check(prf == OID_hmac_sm3) != 1
|| asn1_check(cipher == OID_sm4_cbc) != 1
|| asn1_check(ivlen == sizeof(iv)) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pkcs8_enced_private_key_info(void)
{
uint8_t salt[8] = { 1,0 };
int iter = 65536;
int keylen = -1;
int prf = OID_hmac_sm3;
int cipher = OID_sm4_cbc;
uint8_t iv[16] = { 2,0 };
uint8_t enced[128] = { 3,0 };
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
const uint8_t *psalt;
size_t saltlen;
const uint8_t *piv;
size_t ivlen;
const uint8_t *penced;
size_t encedlen;
if (pkcs8_enced_private_key_info_to_der(
salt, sizeof(salt), iter, keylen, prf,
cipher, iv, sizeof(iv),
enced, sizeof(enced), &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pkcs8_enced_private_key_info_print(stderr, 0, 0, "EncryptedPrivateKeyInfo", d, dlen);
p = buf;
cp = buf;
len = 0;
if (pkcs8_enced_private_key_info_to_der(
salt, sizeof(salt), iter, keylen, prf,
cipher, iv, sizeof(iv),
enced, sizeof(enced), &p, &len) != 1
|| pkcs8_enced_private_key_info_from_der(
&psalt, &saltlen, &iter, &keylen, &prf,
&cipher, &piv, &ivlen,
&penced, &encedlen, &cp, &len) != 1
|| asn1_check(saltlen == sizeof(salt)) != 1
|| asn1_check(keylen == -1) != 1
|| asn1_check(prf == OID_hmac_sm3) != 1
|| asn1_check(cipher == OID_sm4_cbc) != 1
|| asn1_check(ivlen == sizeof(iv)) != 1
|| asn1_check(encedlen == sizeof(enced)) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pkcs8(void)
{
int err = 0;
SM2_KEY sm2_key;
SM2_KEY sm2_buf;
const uint8_t *attrs;
size_t attrslen;
uint8_t buf[1024];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
sm2_key_generate(&sm2_key);
memcpy(&sm2_buf, &sm2_key, sizeof(sm2_key));
sm2_key_print(stderr, 0, 0, "SM2_KEY", &sm2_key);
if (sm2_private_key_info_encrypt_to_der(&sm2_key, "password", &p, &len) != 1) {
error_print();
return -1;
}
{
const uint8_t *a = buf;
size_t alen = len;
const uint8_t *d;
size_t dlen;
if (asn1_sequence_from_der(&d, &dlen, &a, &alen) != 1
|| asn1_length_is_zero(alen) != 1) {
error_print();
return -1;
}
pkcs8_enced_private_key_info_print(stderr, 0, 0, "test_pkcs8: 392", d, dlen);
fprintf(stderr, "\n");
}
memset(&sm2_key, 0, sizeof(sm2_key));
if (sm2_private_key_info_decrypt_from_der(&sm2_key, &attrs, &attrslen, "password", &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
fprintf(stderr, "\n");
sm2_key_print(stderr, 0, 0, "SM2_KEY", &sm2_key);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pkcs8_pem(void)
{
int err = 0;
char *file = "test_pkcs8_pem.pem";
char *pass = "password";
SM2_KEY sm2_key;
SM2_KEY sm2_buf;
const uint8_t *attrs;
size_t attrs_len;
FILE *fp;
sm2_key_generate(&sm2_key);
memcpy(&sm2_buf, &sm2_key, sizeof(sm2_key));
sm2_key_print(stderr, 0, 0, "SM2_KEY", &sm2_key);
if (!(fp = fopen(file, "w"))
|| sm2_private_key_info_encrypt_to_pem(&sm2_key, pass, fp) != 1) {
error_print();
return -1;
}
fclose(fp);
memset(&sm2_key, 0, sizeof(sm2_key));
if (!(fp = fopen(file, "r"))
|| sm2_private_key_info_decrypt_from_pem(&sm2_key, pass, fp) != 1) {
error_print();
return -1;
}
sm2_key_print(stderr, 0, 0, "SM2_KEY", &sm2_key);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
int main(void)
{
int err = 0;
/*
err += test_pbkdf2_params();
err += test_pbkdf2_algor();
err += test_pbes2_enc_algor();
err += test_pbes2_params();
err += test_pbes2_algor();
err += test_pkcs8_enced_private_key_info();
*/
err += test_pkcs8();
// err += test_pkcs8_pem();
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/sm2.h>
#include <gmssl/asn1.h>
#include <gmssl/pkcs8.h>
#include <gmssl/error.h>
static int test_pbkdf2_params(void)
{
uint8_t salt[8] = {0};
size_t saltlen;
int iter = 65536;
int keylen = 16;
int prf = OID_hmac_sm3;
uint8_t buf[128];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
const uint8_t *psalt;
if (pbkdf2_params_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pbkdf2_params_print(stderr, 0, 0, "PBKDF2-params", d, dlen);
p = buf;
cp = buf;
len = 0;
keylen = -1;
prf = -1;
if (pbkdf2_params_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pbkdf2_params_print(stderr, 0, 0, "PBKDF2-params", d, dlen);
p = buf;
cp = buf;
len = 0;
keylen = -1;
prf = -1;
if (pbkdf2_params_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1
|| pbkdf2_params_from_der(&psalt, &saltlen, &iter, &keylen, &prf, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 0, "PBKDF2-params\n");
format_bytes(stderr, 0, 4, "salt", psalt, saltlen);
format_print(stderr, 0, 4, "iterationCount: %d\n", iter);
format_print(stderr, 0, 4, "keyLength: %d\n", keylen);
format_print(stderr, 0, 4, "prf: %d\n", prf);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pbkdf2_algor(void)
{
uint8_t salt[8] = {0};
size_t saltlen;
int iter = 65536;
int keylen = 16;
int prf = OID_hmac_sm3;
uint8_t buf[128];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
const uint8_t *psalt;
if (pbkdf2_algor_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pbkdf2_algor_print(stderr, 0, 0, "PBKDF2", d, dlen);
p = buf;
cp = buf;
len = 0;
if (pbkdf2_algor_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 0, "PBKDF2\n");
format_bytes(stderr, 0, 4, "salt", psalt, saltlen);
format_print(stderr, 0, 4, "iterationCount: %d\n", iter);
format_print(stderr, 0, 4, "keyLength: %d\n", keylen);
format_print(stderr, 0, 4, "prf: %d\n", prf);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pbes2_enc_algor(void)
{
int cipher = OID_sm4_cbc;
uint8_t iv[16] = {1};
uint8_t buf[128];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
const uint8_t *piv;
size_t ivlen;
if (pbes2_enc_algor_to_der(cipher, iv, sizeof(iv), &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pbes2_enc_algor_print(stderr, 0, 0, "PBES2-Enc", d, dlen);
p = buf;
cp = buf;
len = 0;
if (pbes2_enc_algor_to_der(cipher, iv, sizeof(iv), &p, &len) != 1
|| pbes2_enc_algor_from_der(&cipher, &piv, &ivlen, &cp, &len) != 1
|| asn1_check(cipher == OID_sm4_cbc) != 1
|| asn1_check(ivlen == sizeof(iv)) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pbes2_params(void)
{
uint8_t salt[8] = {0};
size_t saltlen;
int iter = 65536;
int keylen = -1;
int prf = OID_hmac_sm3;
int cipher = OID_sm4_cbc;
uint8_t iv[16];
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
const uint8_t *psalt;
const uint8_t *piv;
size_t ivlen;
if (pbes2_params_to_der(salt, sizeof(salt), iter, keylen, prf, cipher, iv, sizeof(iv), &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pbes2_params_print(stderr, 0, 0, "PBES2-params", d, dlen);
p = buf;
cp = buf;
len = 0;
if (pbes2_params_to_der(salt, sizeof(salt), iter, keylen, prf, cipher, iv, sizeof(iv), &p, &len) != 1
|| pbes2_params_from_der(&psalt, &saltlen, &iter, &keylen, &prf, &cipher, &piv, &ivlen, &cp, &len) != 1
|| asn1_check(saltlen == sizeof(salt)) != 1
|| asn1_check(iter == 65536) != 1
|| asn1_check(keylen == -1) != 1
|| asn1_check(prf == OID_hmac_sm3) != 1
|| asn1_check(cipher == OID_sm4_cbc) != 1
|| asn1_check(ivlen == sizeof(iv)) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pbes2_algor(void)
{
uint8_t salt[8] = {0};
size_t saltlen;
int iter = 65536;
int keylen = -1;
int prf = OID_hmac_sm3;
int cipher = OID_sm4_cbc;
uint8_t iv[16];
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
const uint8_t *psalt;
const uint8_t *piv;
size_t ivlen;
if (pbes2_algor_to_der(salt, sizeof(salt), iter, keylen, prf, cipher, iv, sizeof(iv), &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pbes2_algor_print(stderr, 0, 0, "PBES2", d, dlen);
p = buf;
cp = buf;
len = 0;
if (pbes2_algor_to_der(salt, sizeof(salt), iter, keylen, prf, cipher, iv, sizeof(iv), &p, &len) != 1
|| pbes2_algor_from_der(&psalt, &saltlen, &iter, &keylen, &prf, &cipher, &piv, &ivlen, &cp, &len) != 1
|| asn1_check(saltlen == sizeof(salt)) != 1
|| asn1_check(iter == 65536) != 1
|| asn1_check(keylen == -1) != 1
|| asn1_check(prf == OID_hmac_sm3) != 1
|| asn1_check(cipher == OID_sm4_cbc) != 1
|| asn1_check(ivlen == sizeof(iv)) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pkcs8_enced_private_key_info(void)
{
uint8_t salt[8] = { 1,0 };
int iter = 65536;
int keylen = -1;
int prf = OID_hmac_sm3;
int cipher = OID_sm4_cbc;
uint8_t iv[16] = { 2,0 };
uint8_t enced[128] = { 3,0 };
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
const uint8_t *psalt;
size_t saltlen;
const uint8_t *piv;
size_t ivlen;
const uint8_t *penced;
size_t encedlen;
if (pkcs8_enced_private_key_info_to_der(
salt, sizeof(salt), iter, keylen, prf,
cipher, iv, sizeof(iv),
enced, sizeof(enced), &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
pkcs8_enced_private_key_info_print(stderr, 0, 0, "EncryptedPrivateKeyInfo", d, dlen);
p = buf;
cp = buf;
len = 0;
if (pkcs8_enced_private_key_info_to_der(
salt, sizeof(salt), iter, keylen, prf,
cipher, iv, sizeof(iv),
enced, sizeof(enced), &p, &len) != 1
|| pkcs8_enced_private_key_info_from_der(
&psalt, &saltlen, &iter, &keylen, &prf,
&cipher, &piv, &ivlen,
&penced, &encedlen, &cp, &len) != 1
|| asn1_check(saltlen == sizeof(salt)) != 1
|| asn1_check(keylen == -1) != 1
|| asn1_check(prf == OID_hmac_sm3) != 1
|| asn1_check(cipher == OID_sm4_cbc) != 1
|| asn1_check(ivlen == sizeof(iv)) != 1
|| asn1_check(encedlen == sizeof(enced)) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pkcs8(void)
{
int err = 0;
SM2_KEY sm2_key;
SM2_KEY sm2_buf;
const uint8_t *attrs;
size_t attrslen;
uint8_t buf[1024];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
sm2_key_generate(&sm2_key);
memcpy(&sm2_buf, &sm2_key, sizeof(sm2_key));
sm2_key_print(stderr, 0, 0, "SM2_KEY", &sm2_key);
if (sm2_private_key_info_encrypt_to_der(&sm2_key, "password", &p, &len) != 1) {
error_print();
return -1;
}
{
const uint8_t *a = buf;
size_t alen = len;
const uint8_t *d;
size_t dlen;
if (asn1_sequence_from_der(&d, &dlen, &a, &alen) != 1
|| asn1_length_is_zero(alen) != 1) {
error_print();
return -1;
}
pkcs8_enced_private_key_info_print(stderr, 0, 0, "test_pkcs8: 392", d, dlen);
fprintf(stderr, "\n");
}
memset(&sm2_key, 0, sizeof(sm2_key));
if (sm2_private_key_info_decrypt_from_der(&sm2_key, &attrs, &attrslen, "password", &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
fprintf(stderr, "\n");
sm2_key_print(stderr, 0, 0, "SM2_KEY", &sm2_key);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_pkcs8_pem(void)
{
int err = 0;
char *file = "test_pkcs8_pem.pem";
char *pass = "password";
SM2_KEY sm2_key;
SM2_KEY sm2_buf;
const uint8_t *attrs;
size_t attrs_len;
FILE *fp;
sm2_key_generate(&sm2_key);
memcpy(&sm2_buf, &sm2_key, sizeof(sm2_key));
sm2_key_print(stderr, 0, 0, "SM2_KEY", &sm2_key);
if (!(fp = fopen(file, "w"))
|| sm2_private_key_info_encrypt_to_pem(&sm2_key, pass, fp) != 1) {
error_print();
return -1;
}
fclose(fp);
memset(&sm2_key, 0, sizeof(sm2_key));
if (!(fp = fopen(file, "r"))
|| sm2_private_key_info_decrypt_from_pem(&sm2_key, pass, fp) != 1) {
error_print();
return -1;
}
sm2_key_print(stderr, 0, 0, "SM2_KEY", &sm2_key);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
int main(void)
{
int err = 0;
/*
err += test_pbkdf2_params();
err += test_pbkdf2_algor();
err += test_pbes2_enc_algor();
err += test_pbes2_params();
err += test_pbes2_algor();
err += test_pkcs8_enced_private_key_info();
*/
err += test_pkcs8();
// err += test_pkcs8_pem();
return err;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,383 +7,384 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/rc4.h>
/* tests from RFC 6229 Test Vectors for the Stream Cipher RC4 */
unsigned char key1[] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
};
unsigned char key2[] = {
0x1a, 0xda, 0x31, 0xd5, 0xcf, 0x68, 0x82, 0x21,
0xc1, 0x09, 0x16, 0x39, 0x08, 0xeb, 0xe5, 0x1d,
0xeb, 0xb4, 0x62, 0x27, 0xc6, 0xcc, 0x8b, 0x37,
0x64, 0x19, 0x10, 0x83, 0x32, 0x22, 0x77, 0x2a,
};
int keybits[] = {
40,
56,
64,
80,
128,
192,
256,
};
int testindex[] = {
0,
16,
240,
256,
496,
512,
752,
768,
1008,
1024,
1520,
1536,
2032,
2048,
3056,
3072,
4080,
4096,
};
unsigned char testdata1[][16 * 18] = {
{
0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27, 0xcc, 0xc3, 0x52, 0x4a, 0x0a, 0x11, 0x18, 0xa8,
0x69, 0x82, 0x94, 0x4f, 0x18, 0xfc, 0x82, 0xd5, 0x89, 0xc4, 0x03, 0xa4, 0x7a, 0x0d, 0x09, 0x19,
0x28, 0xcb, 0x11, 0x32, 0xc9, 0x6c, 0xe2, 0x86, 0x42, 0x1d, 0xca, 0xad, 0xb8, 0xb6, 0x9e, 0xae,
0x1c, 0xfc, 0xf6, 0x2b, 0x03, 0xed, 0xdb, 0x64, 0x1d, 0x77, 0xdf, 0xcf, 0x7f, 0x8d, 0x8c, 0x93,
0x42, 0xb7, 0xd0, 0xcd, 0xd9, 0x18, 0xa8, 0xa3, 0x3d, 0xd5, 0x17, 0x81, 0xc8, 0x1f, 0x40, 0x41,
0x64, 0x59, 0x84, 0x44, 0x32, 0xa7, 0xda, 0x92, 0x3c, 0xfb, 0x3e, 0xb4, 0x98, 0x06, 0x61, 0xf6,
0xec, 0x10, 0x32, 0x7b, 0xde, 0x2b, 0xee, 0xfd, 0x18, 0xf9, 0x27, 0x76, 0x80, 0x45, 0x7e, 0x22,
0xeb, 0x62, 0x63, 0x8d, 0x4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, 0xf3, 0xa2, 0x37, 0xcf, 0x9d, 0xe7, 0x35, 0x59,
0x9e, 0xa3, 0x6c, 0x52, 0x55, 0x31, 0xb8, 0x80, 0xba, 0x12, 0x43, 0x34, 0xf5, 0x7b, 0x0b, 0x70,
0xd5, 0xa3, 0x9e, 0x3d, 0xfc, 0xc5, 0x02, 0x80, 0xba, 0xc4, 0xa6, 0xb5, 0xaa, 0x0d, 0xca, 0x7d,
0x37, 0x0b, 0x1c, 0x1f, 0xe6, 0x55, 0x91, 0x6d, 0x97, 0xfd, 0x0d, 0x47, 0xca, 0x1d, 0x72, 0xb8,
},
};
int main(void)
{
int err = 0;
RC4_STATE state;
unsigned char buf[4096 + 16];
size_t i, j;
for (i = 0; i < sizeof(keybits)/sizeof(keybits[0]); i++) {
int e = 0;
rc4_init(&state, key1, keybits[i]/8);
rc4_generate_keystream(&state, sizeof(buf), buf);
for (j = 0; j < sizeof(testindex)/sizeof(testindex[0]); j++) {
if (memcmp(buf + testindex[j], &testdata1[i][j * 16], 16) != 0) {
e++;
}
}
fprintf(stderr, "rc4 test 1.%zu %s\n", i+1, e ? "failed" : "ok");
if (e) {
err++;
}
}
for (i = 0; i < sizeof(keybits)/sizeof(keybits[0]); i++) {
int e = 0;
rc4_init(&state, key2 + sizeof(key2) - keybits[i]/8, keybits[i]/8);
rc4_generate_keystream(&state, sizeof(buf), buf);
for (j = 0; j < sizeof(testindex)/sizeof(testindex[0]); j++) {
if (memcmp(buf + testindex[j], &testdata2[i][j * 16], 16) != 0) {
e++;
}
}
fprintf(stderr, "rc4 test 2.%zu %s\n", i+1, e ? "failed" : "ok");
if (e) {
err++;
}
}
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/rc4.h>
/* tests from RFC 6229 Test Vectors for the Stream Cipher RC4 */
unsigned char key1[] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
};
unsigned char key2[] = {
0x1a, 0xda, 0x31, 0xd5, 0xcf, 0x68, 0x82, 0x21,
0xc1, 0x09, 0x16, 0x39, 0x08, 0xeb, 0xe5, 0x1d,
0xeb, 0xb4, 0x62, 0x27, 0xc6, 0xcc, 0x8b, 0x37,
0x64, 0x19, 0x10, 0x83, 0x32, 0x22, 0x77, 0x2a,
};
int keybits[] = {
40,
56,
64,
80,
128,
192,
256,
};
int testindex[] = {
0,
16,
240,
256,
496,
512,
752,
768,
1008,
1024,
1520,
1536,
2032,
2048,
3056,
3072,
4080,
4096,
};
unsigned char testdata1[][16 * 18] = {
{
0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27, 0xcc, 0xc3, 0x52, 0x4a, 0x0a, 0x11, 0x18, 0xa8,
0x69, 0x82, 0x94, 0x4f, 0x18, 0xfc, 0x82, 0xd5, 0x89, 0xc4, 0x03, 0xa4, 0x7a, 0x0d, 0x09, 0x19,
0x28, 0xcb, 0x11, 0x32, 0xc9, 0x6c, 0xe2, 0x86, 0x42, 0x1d, 0xca, 0xad, 0xb8, 0xb6, 0x9e, 0xae,
0x1c, 0xfc, 0xf6, 0x2b, 0x03, 0xed, 0xdb, 0x64, 0x1d, 0x77, 0xdf, 0xcf, 0x7f, 0x8d, 0x8c, 0x93,
0x42, 0xb7, 0xd0, 0xcd, 0xd9, 0x18, 0xa8, 0xa3, 0x3d, 0xd5, 0x17, 0x81, 0xc8, 0x1f, 0x40, 0x41,
0x64, 0x59, 0x84, 0x44, 0x32, 0xa7, 0xda, 0x92, 0x3c, 0xfb, 0x3e, 0xb4, 0x98, 0x06, 0x61, 0xf6,
0xec, 0x10, 0x32, 0x7b, 0xde, 0x2b, 0xee, 0xfd, 0x18, 0xf9, 0x27, 0x76, 0x80, 0x45, 0x7e, 0x22,
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x66, 0xd2, 0x8e, 0xd1, 0x43, 0xf3, 0xa2, 0x37, 0xcf, 0x9d, 0xe7, 0x35, 0x59,
0x9e, 0xa3, 0x6c, 0x52, 0x55, 0x31, 0xb8, 0x80, 0xba, 0x12, 0x43, 0x34, 0xf5, 0x7b, 0x0b, 0x70,
0xd5, 0xa3, 0x9e, 0x3d, 0xfc, 0xc5, 0x02, 0x80, 0xba, 0xc4, 0xa6, 0xb5, 0xaa, 0x0d, 0xca, 0x7d,
0x37, 0x0b, 0x1c, 0x1f, 0xe6, 0x55, 0x91, 0x6d, 0x97, 0xfd, 0x0d, 0x47, 0xca, 0x1d, 0x72, 0xb8,
},
};
int main(void)
{
int err = 0;
RC4_STATE state;
unsigned char buf[4096 + 16];
size_t i, j;
for (i = 0; i < sizeof(keybits)/sizeof(keybits[0]); i++) {
int e = 0;
rc4_init(&state, key1, keybits[i]/8);
rc4_generate_keystream(&state, sizeof(buf), buf);
for (j = 0; j < sizeof(testindex)/sizeof(testindex[0]); j++) {
if (memcmp(buf + testindex[j], &testdata1[i][j * 16], 16) != 0) {
e++;
}
}
fprintf(stderr, "rc4 test 1.%zu %s\n", i+1, e ? "failed" : "ok");
if (e) {
err++;
}
}
for (i = 0; i < sizeof(keybits)/sizeof(keybits[0]); i++) {
int e = 0;
rc4_init(&state, key2 + sizeof(key2) - keybits[i]/8, keybits[i]/8);
rc4_generate_keystream(&state, sizeof(buf), buf);
for (j = 0; j < sizeof(testindex)/sizeof(testindex[0]); j++) {
if (memcmp(buf + testindex[j], &testdata2[i][j * 16], 16) != 0) {
e++;
}
}
fprintf(stderr, "rc4 test 2.%zu %s\n", i+1, e ? "failed" : "ok");
if (e) {
err++;
}
}
return err;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,66 +7,67 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/sha1.h>
#include <gmssl/hex.h>
static char *teststr[] = {
"abc",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
"a",
"0123456701234567012345670123456701234567012345670123456701234567",
};
static size_t testcnt[] = {
1,
1,
1000000,
10,
};
static char *dgsthex[] = {
"A9993E364706816ABA3E25717850C26C9CD0D89D",
"84983E441C3BD26EBAAE4AA1F95129E5E54670F1",
"34AA973CD4C4DAA4F61EEB2BDBAD27316534016F",
"DEA356A2CDDD90C7A7ECEDC5EBB563934F460452",
};
int main(void)
{
int err = 0;
SHA1_CTX ctx;
uint8_t dgst[20];
uint8_t dgstbuf[20];
size_t dgstlen;
size_t i, j;
for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) {
hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstlen);
sha1_init(&ctx);
for (j = 0; j < testcnt[i]; j++) {
sha1_update(&ctx, (uint8_t *)teststr[i], strlen(teststr[i]));
}
sha1_finish(&ctx, dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
printf("sha1 test %lu failed\n", i+1);
printf("%s\n", dgsthex[i]);
for (j = 0; j < sizeof(dgst); j++) {
printf("%02X", dgst[j]);
}
printf("\n");
err++;
} else {
printf("sha1 test %lu ok\n", i+1);
}
}
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/sha1.h>
#include <gmssl/hex.h>
static char *teststr[] = {
"abc",
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
"a",
"0123456701234567012345670123456701234567012345670123456701234567",
};
static size_t testcnt[] = {
1,
1,
1000000,
10,
};
static char *dgsthex[] = {
"A9993E364706816ABA3E25717850C26C9CD0D89D",
"84983E441C3BD26EBAAE4AA1F95129E5E54670F1",
"34AA973CD4C4DAA4F61EEB2BDBAD27316534016F",
"DEA356A2CDDD90C7A7ECEDC5EBB563934F460452",
};
int main(void)
{
int err = 0;
SHA1_CTX ctx;
uint8_t dgst[20];
uint8_t dgstbuf[20];
size_t dgstlen;
size_t i, j;
for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) {
hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstlen);
sha1_init(&ctx);
for (j = 0; j < testcnt[i]; j++) {
sha1_update(&ctx, (uint8_t *)teststr[i], strlen(teststr[i]));
}
sha1_finish(&ctx, dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
printf("sha1 test %lu failed\n", i+1);
printf("%s\n", dgsthex[i]);
for (j = 0; j < sizeof(dgst); j++) {
printf("%02X", dgst[j]);
}
printf("\n");
err++;
} else {
printf("sha1 test %lu ok\n", i+1);
}
}
return err;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,88 +7,89 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/hex.h>
#include <gmssl/sha2.h>
#define TEST1 "abc"
#define TEST2 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
#define TEST3 "a"
#define TEST4 "0123456701234567012345670123456701234567012345670123456701234567"
#define TEST5 "\x07"
#define TEST6 "\x18\x80\x40\x05\xdd\x4f\xbd\x15\x56\x29\x9d\x6f\x9d\x93\xdf\x62"
#define TEST7 \
"\x55\xb2\x10\x07\x9c\x61\xb5\x3a\xdd\x52\x06\x22\xd1\xac\x97\xd5" \
"\xcd\xbe\x8c\xb3\x3a\xa0\xae\x34\x45\x17\xbe\xe4\xd7\xba\x09\xab" \
"\xc8\x53\x3c\x52\x50\x88\x7a\x43\xbe\xbb\xac\x90\x6c\x2e\x18\x37" \
"\xf2\x6b\x36\xa5\x9a\xe3\xbe\x78\x14\xd5\x06\x89\x6b\x71\x8b\x2a" \
"\x38\x3e\xcd\xac\x16\xb9\x61\x25\x55\x3f\x41\x6f\xf3\x2c\x66\x74" \
"\xc7\x45\x99\xa9\x00\x53\x86\xd9\xce\x11\x12\x24\x5f\x48\xee\x47" \
"\x0d\x39\x6c\x1e\xd6\x3b\x92\x67\x0c\xa5\x6e\xc8\x4d\xee\xa8\x14" \
"\xb6\x13\x5e\xca\x54\x39\x2b\xde\xdb\x94\x89\xbc\x9b\x87\x5a\x8b" \
"\xaf\x0d\xc1\xae\x78\x57\x36\x91\x4a\xb7\xda\xa2\x64\xbc\x07\x9d" \
"\x26\x9f\x2c\x0d\x7e\xdd\xd8\x10\xa4\x26\x14\x5a\x07\x76\xf6\x7c" \
"\x87\x82\x73"
#define DGST1 "23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7"
#define DGST2 "75388B16512776CC5DBA5DA1FD890150B0C6455CB4F58B1952522525"
#define DGST3 "20794655980C91D8BBB4C1EA97618A4BF03F42581948B2EE4EE7AD67"
#define DGST4 "567F69F168CD7844E65259CE658FE7AADFA25216E68ECA0EB7AB8262"
#define DGST5 "00ECD5F138422B8AD74C9799FD826C531BAD2FCABC7450BEE2AA8C2A"
#define DGST6 "DF90D78AA78821C99B40BA4C966921ACCD8FFB1E98AC388E56191DB1"
#define DGST7 "0B31894EC8937AD9B91BDFBCBA294D9ADEFAA18E09305E9F20D5C3A4"
struct {
char *data;
size_t length;
size_t count;
char *dgsthex;
} tests[7] = {
{TEST1, sizeof(TEST1) - 1, 1, DGST1},
{TEST2, sizeof(TEST2) - 1, 1, DGST2},
{TEST3, sizeof(TEST3) - 1, 1000000, DGST3},
{TEST4, sizeof(TEST4) - 1, 10, DGST4},
{TEST5, sizeof(TEST5) - 1, 1, DGST5},
{TEST6, sizeof(TEST6) - 1, 1, DGST6},
{TEST7, sizeof(TEST7) - 1, 1, DGST7},
};
int main(int argc, char **argv)
{
int err = 0;
SHA224_CTX ctx;
uint8_t dgst[SHA224_DIGEST_SIZE];
uint8_t dgstbuf[SHA224_DIGEST_SIZE];
size_t dgstlen;
size_t i, j;
for (i = 0; i < 7; i++) {
hex_to_bytes(tests[i].dgsthex, strlen(tests[i].dgsthex), dgstbuf, &dgstlen);
sha224_init(&ctx);
for (j = 0; j < tests[i].count; j++) {
sha224_update(&ctx, (uint8_t *)tests[i].data, tests[i].length);
}
sha224_finish(&ctx, dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
printf("sha224 test %lu failed\n", i+1);
printf("%s\n", tests[i].dgsthex);
for (j = 0; j < sizeof(dgst); j++) {
printf("%02X", dgst[j]);
}
printf("\n");
err++;
} else {
printf("sha224 test %lu ok\n", i+1);
}
}
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/hex.h>
#include <gmssl/sha2.h>
#define TEST1 "abc"
#define TEST2 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
#define TEST3 "a"
#define TEST4 "0123456701234567012345670123456701234567012345670123456701234567"
#define TEST5 "\x07"
#define TEST6 "\x18\x80\x40\x05\xdd\x4f\xbd\x15\x56\x29\x9d\x6f\x9d\x93\xdf\x62"
#define TEST7 \
"\x55\xb2\x10\x07\x9c\x61\xb5\x3a\xdd\x52\x06\x22\xd1\xac\x97\xd5" \
"\xcd\xbe\x8c\xb3\x3a\xa0\xae\x34\x45\x17\xbe\xe4\xd7\xba\x09\xab" \
"\xc8\x53\x3c\x52\x50\x88\x7a\x43\xbe\xbb\xac\x90\x6c\x2e\x18\x37" \
"\xf2\x6b\x36\xa5\x9a\xe3\xbe\x78\x14\xd5\x06\x89\x6b\x71\x8b\x2a" \
"\x38\x3e\xcd\xac\x16\xb9\x61\x25\x55\x3f\x41\x6f\xf3\x2c\x66\x74" \
"\xc7\x45\x99\xa9\x00\x53\x86\xd9\xce\x11\x12\x24\x5f\x48\xee\x47" \
"\x0d\x39\x6c\x1e\xd6\x3b\x92\x67\x0c\xa5\x6e\xc8\x4d\xee\xa8\x14" \
"\xb6\x13\x5e\xca\x54\x39\x2b\xde\xdb\x94\x89\xbc\x9b\x87\x5a\x8b" \
"\xaf\x0d\xc1\xae\x78\x57\x36\x91\x4a\xb7\xda\xa2\x64\xbc\x07\x9d" \
"\x26\x9f\x2c\x0d\x7e\xdd\xd8\x10\xa4\x26\x14\x5a\x07\x76\xf6\x7c" \
"\x87\x82\x73"
#define DGST1 "23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7"
#define DGST2 "75388B16512776CC5DBA5DA1FD890150B0C6455CB4F58B1952522525"
#define DGST3 "20794655980C91D8BBB4C1EA97618A4BF03F42581948B2EE4EE7AD67"
#define DGST4 "567F69F168CD7844E65259CE658FE7AADFA25216E68ECA0EB7AB8262"
#define DGST5 "00ECD5F138422B8AD74C9799FD826C531BAD2FCABC7450BEE2AA8C2A"
#define DGST6 "DF90D78AA78821C99B40BA4C966921ACCD8FFB1E98AC388E56191DB1"
#define DGST7 "0B31894EC8937AD9B91BDFBCBA294D9ADEFAA18E09305E9F20D5C3A4"
struct {
char *data;
size_t length;
size_t count;
char *dgsthex;
} tests[7] = {
{TEST1, sizeof(TEST1) - 1, 1, DGST1},
{TEST2, sizeof(TEST2) - 1, 1, DGST2},
{TEST3, sizeof(TEST3) - 1, 1000000, DGST3},
{TEST4, sizeof(TEST4) - 1, 10, DGST4},
{TEST5, sizeof(TEST5) - 1, 1, DGST5},
{TEST6, sizeof(TEST6) - 1, 1, DGST6},
{TEST7, sizeof(TEST7) - 1, 1, DGST7},
};
int main(int argc, char **argv)
{
int err = 0;
SHA224_CTX ctx;
uint8_t dgst[SHA224_DIGEST_SIZE];
uint8_t dgstbuf[SHA224_DIGEST_SIZE];
size_t dgstlen;
size_t i, j;
for (i = 0; i < 7; i++) {
hex_to_bytes(tests[i].dgsthex, strlen(tests[i].dgsthex), dgstbuf, &dgstlen);
sha224_init(&ctx);
for (j = 0; j < tests[i].count; j++) {
sha224_update(&ctx, (uint8_t *)tests[i].data, tests[i].length);
}
sha224_finish(&ctx, dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
printf("sha224 test %lu failed\n", i+1);
printf("%s\n", tests[i].dgsthex);
for (j = 0; j < sizeof(dgst); j++) {
printf("%02X", dgst[j]);
}
printf("\n");
err++;
} else {
printf("sha224 test %lu ok\n", i+1);
}
}
return err;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,87 +7,88 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/hex.h>
#include <gmssl/sha2.h>
#define TEST1 "abc"
#define TEST2 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
#define TEST3 "a"
#define TEST4 "0123456701234567012345670123456701234567012345670123456701234567"
#define TEST5 "\x19"
#define TEST6 "\xe3\xd7\x25\x70\xdc\xdd\x78\x7c\xe3\x88\x7a\xb2\xcd\x68\x46\x52"
#define TEST7 "\x83\x26\x75\x4e\x22\x77\x37\x2f\x4f\xc1\x2b\x20\x52\x7a\xfe\xf0" \
"\x4d\x8a\x05\x69\x71\xb1\x1a\xd5\x71\x23\xa7\xc1\x37\x76\x00\x00" \
"\xd7\xbe\xf6\xf3\xc1\xf7\xa9\x08\x3a\xa3\x9d\x81\x0d\xb3\x10\x77" \
"\x7d\xab\x8b\x1e\x7f\x02\xb8\x4a\x26\xc7\x73\x32\x5f\x8b\x23\x74" \
"\xde\x7a\x4b\x5a\x58\xcb\x5c\x5c\xf3\x5b\xce\xe6\xfb\x94\x6e\x5b" \
"\xd6\x94\xfa\x59\x3a\x8b\xeb\x3f\x9d\x65\x92\xec\xed\xaa\x66\xca" \
"\x82\xa2\x9d\x0c\x51\xbc\xf9\x33\x62\x30\xe5\xd7\x84\xe4\xc0\xa4" \
"\x3f\x8d\x79\xa3\x0a\x16\x5c\xba\xbe\x45\x2b\x77\x4b\x9c\x71\x09" \
"\xa9\x7d\x13\x8f\x12\x92\x28\x96\x6f\x6c\x0a\xdc\x10\x6a\xad\x5a" \
"\x9f\xdd\x30\x82\x57\x69\xb2\xc6\x71\xaf\x67\x59\xdf\x28\xeb\x39" \
"\x3d\x54\xd6"
#define DGST1 "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD"
#define DGST2 "248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1"
#define DGST3 "CDC76E5C9914FB9281A1C7E284D73E67F1809A48A497200E046D39CCC7112CD0"
#define DGST4 "594847328451BDFA85056225462CC1D867D877FB388DF0CE35F25AB5562BFBB5"
#define DGST5 "68AA2E2EE5DFF96E3355E6C7EE373E3D6A4E17F75F9518D843709C0C9BC3E3D4"
#define DGST6 "175EE69B02BA9B58E2B0A5FD13819CEA573F3940A94F825128CF4209BEABB4E8"
#define DGST7 "97DBCA7DF46D62C8A422C941DD7E835B8AD3361763F7E9B2D95F4F0DA6E1CCBC"
struct {
char *data;
size_t length;
size_t count;
char *dgsthex;
} tests[7] = {
{TEST1, sizeof(TEST1) - 1, 1, DGST1},
{TEST2, sizeof(TEST2) - 1, 1, DGST2},
{TEST3, sizeof(TEST3) - 1, 1000000, DGST3},
{TEST4, sizeof(TEST4) - 1, 10, DGST4},
{TEST5, sizeof(TEST5) - 1, 1, DGST5},
{TEST6, sizeof(TEST6) - 1, 1, DGST6},
{TEST7, sizeof(TEST7) - 1, 1, DGST7},
};
int main(int argc, char **argv)
{
int err = 0;
SHA256_CTX ctx;
uint8_t dgst[SHA256_DIGEST_SIZE];
uint8_t dgstbuf[SHA256_DIGEST_SIZE];
size_t dgstlen;
size_t i, j;
for (i = 0; i < 7; i++) {
hex_to_bytes(tests[i].dgsthex, strlen(tests[i].dgsthex), dgstbuf, &dgstlen);
sha256_init(&ctx);
for (j = 0; j < tests[i].count; j++) {
sha256_update(&ctx, (uint8_t *)tests[i].data, tests[i].length);
}
sha256_finish(&ctx, dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
printf("sha256 test %lu failed\n", i+1);
printf("%s\n", tests[i].dgsthex);
for (j = 0; j < sizeof(dgst); j++) {
printf("%02X", dgst[j]);
}
printf("\n");
err++;
} else {
printf("sha256 test %lu ok\n", i+1);
}
}
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/hex.h>
#include <gmssl/sha2.h>
#define TEST1 "abc"
#define TEST2 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
#define TEST3 "a"
#define TEST4 "0123456701234567012345670123456701234567012345670123456701234567"
#define TEST5 "\x19"
#define TEST6 "\xe3\xd7\x25\x70\xdc\xdd\x78\x7c\xe3\x88\x7a\xb2\xcd\x68\x46\x52"
#define TEST7 "\x83\x26\x75\x4e\x22\x77\x37\x2f\x4f\xc1\x2b\x20\x52\x7a\xfe\xf0" \
"\x4d\x8a\x05\x69\x71\xb1\x1a\xd5\x71\x23\xa7\xc1\x37\x76\x00\x00" \
"\xd7\xbe\xf6\xf3\xc1\xf7\xa9\x08\x3a\xa3\x9d\x81\x0d\xb3\x10\x77" \
"\x7d\xab\x8b\x1e\x7f\x02\xb8\x4a\x26\xc7\x73\x32\x5f\x8b\x23\x74" \
"\xde\x7a\x4b\x5a\x58\xcb\x5c\x5c\xf3\x5b\xce\xe6\xfb\x94\x6e\x5b" \
"\xd6\x94\xfa\x59\x3a\x8b\xeb\x3f\x9d\x65\x92\xec\xed\xaa\x66\xca" \
"\x82\xa2\x9d\x0c\x51\xbc\xf9\x33\x62\x30\xe5\xd7\x84\xe4\xc0\xa4" \
"\x3f\x8d\x79\xa3\x0a\x16\x5c\xba\xbe\x45\x2b\x77\x4b\x9c\x71\x09" \
"\xa9\x7d\x13\x8f\x12\x92\x28\x96\x6f\x6c\x0a\xdc\x10\x6a\xad\x5a" \
"\x9f\xdd\x30\x82\x57\x69\xb2\xc6\x71\xaf\x67\x59\xdf\x28\xeb\x39" \
"\x3d\x54\xd6"
#define DGST1 "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD"
#define DGST2 "248D6A61D20638B8E5C026930C3E6039A33CE45964FF2167F6ECEDD419DB06C1"
#define DGST3 "CDC76E5C9914FB9281A1C7E284D73E67F1809A48A497200E046D39CCC7112CD0"
#define DGST4 "594847328451BDFA85056225462CC1D867D877FB388DF0CE35F25AB5562BFBB5"
#define DGST5 "68AA2E2EE5DFF96E3355E6C7EE373E3D6A4E17F75F9518D843709C0C9BC3E3D4"
#define DGST6 "175EE69B02BA9B58E2B0A5FD13819CEA573F3940A94F825128CF4209BEABB4E8"
#define DGST7 "97DBCA7DF46D62C8A422C941DD7E835B8AD3361763F7E9B2D95F4F0DA6E1CCBC"
struct {
char *data;
size_t length;
size_t count;
char *dgsthex;
} tests[7] = {
{TEST1, sizeof(TEST1) - 1, 1, DGST1},
{TEST2, sizeof(TEST2) - 1, 1, DGST2},
{TEST3, sizeof(TEST3) - 1, 1000000, DGST3},
{TEST4, sizeof(TEST4) - 1, 10, DGST4},
{TEST5, sizeof(TEST5) - 1, 1, DGST5},
{TEST6, sizeof(TEST6) - 1, 1, DGST6},
{TEST7, sizeof(TEST7) - 1, 1, DGST7},
};
int main(int argc, char **argv)
{
int err = 0;
SHA256_CTX ctx;
uint8_t dgst[SHA256_DIGEST_SIZE];
uint8_t dgstbuf[SHA256_DIGEST_SIZE];
size_t dgstlen;
size_t i, j;
for (i = 0; i < 7; i++) {
hex_to_bytes(tests[i].dgsthex, strlen(tests[i].dgsthex), dgstbuf, &dgstlen);
sha256_init(&ctx);
for (j = 0; j < tests[i].count; j++) {
sha256_update(&ctx, (uint8_t *)tests[i].data, tests[i].length);
}
sha256_finish(&ctx, dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
printf("sha256 test %lu failed\n", i+1);
printf("%s\n", tests[i].dgsthex);
for (j = 0; j < sizeof(dgst); j++) {
printf("%02X", dgst[j]);
}
printf("\n");
err++;
} else {
printf("sha256 test %lu ok\n", i+1);
}
}
return err;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,93 +7,94 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/hex.h>
#include <gmssl/sha2.h>
#define TEST1 "abc"
#define TEST2 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" \
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
#define TEST3 "a"
#define TEST4 "0123456701234567012345670123456701234567012345670123456701234567"
#define TEST5 "\xb9"
#define TEST6 "\xa4\x1c\x49\x77\x79\xc0\x37\x5f\xf1\x0a\x7f\x4e\x08\x59\x17\x39"
#define TEST7 "\x39\x96\x69\xe2\x8f\x6b\x9c\x6d\xbc\xbb\x69\x12\xec\x10\xff\xcf" \
"\x74\x79\x03\x49\xb7\xdc\x8f\xbe\x4a\x8e\x7b\x3b\x56\x21\xdb\x0f" \
"\x3e\x7d\xc8\x7f\x82\x32\x64\xbb\xe4\x0d\x18\x11\xc9\xea\x20\x61" \
"\xe1\xc8\x4a\xd1\x0a\x23\xfa\xc1\x72\x7e\x72\x02\xfc\x3f\x50\x42" \
"\xe6\xbf\x58\xcb\xa8\xa2\x74\x6e\x1f\x64\xf9\xb9\xea\x35\x2c\x71" \
"\x15\x07\x05\x3c\xf4\xe5\x33\x9d\x52\x86\x5f\x25\xcc\x22\xb5\xe8" \
"\x77\x84\xa1\x2f\xc9\x61\xd6\x6c\xb6\xe8\x95\x73\x19\x9a\x2c\xe6" \
"\x56\x5c\xbd\xf1\x3d\xca\x40\x38\x32\xcf\xcb\x0e\x8b\x72\x11\xe8" \
"\x3a\xf3\x2a\x11\xac\x17\x92\x9f\xf1\xc0\x73\xa5\x1c\xc0\x27\xaa" \
"\xed\xef\xf8\x5a\xad\x7c\x2b\x7c\x5a\x80\x3e\x24\x04\xd9\x6d\x2a" \
"\x77\x35\x7b\xda\x1a\x6d\xae\xed\x17\x15\x1c\xb9\xbc\x51\x25\xa4" \
"\x22\xe9\x41\xde\x0c\xa0\xfc\x50\x11\xc2\x3e\xcf\xfe\xfd\xd0\x96" \
"\x76\x71\x1c\xf3\xdb\x0a\x34\x40\x72\x0e\x16\x15\xc1\xf2\x2f\xbc" \
"\x3c\x72\x1d\xe5\x21\xe1\xb9\x9b\xa1\xbd\x55\x77\x40\x86\x42\x14" \
"\x7e\xd0\x96"
#define DGST1 "CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7"
#define DGST2 "09330C33F71147E83D192FC782CD1B4753111B173B3B05D22FA08086E3B0F712FCC7C71A557E2DB966C3E9FA91746039"
#define DGST3 "9D0E1809716474CB086E834E310A4A1CED149E9C00F248527972CEC5704C2A5B07B8B3DC38ECC4EBAE97DDD87F3D8985"
#define DGST4 "2FC64A4F500DDB6828F6A3430B8DD72A368EB7F3A8322A70BC84275B9C0B3AB00D27A5CC3C2D224AA6B61A0D79FB4596"
#define DGST5 "BC8089A19007C0B14195F4ECC74094FEC64F01F90929282C2FB392881578208AD466828B1C6C283D2722CF0AD1AB6938"
#define DGST6 "C9A68443A005812256B8EC76B00516F0DBB74FAB26D665913F194B6FFB0E91EA9967566B58109CBC675CC208E4C823F7"
#define DGST7 "4F440DB1E6EDD2899FA335F09515AA025EE177A79F4B4AAF38E42B5C4DE660F5DE8FB2A5B2FBD2A3CBFFD20CFF1288C0"
struct {
char *data;
size_t length;
size_t count;
char *dgsthex;
} tests[7] = {
{TEST1, sizeof(TEST1) - 1, 1, DGST1},
{TEST2, sizeof(TEST2) - 1, 1, DGST2},
{TEST3, sizeof(TEST3) - 1, 1000000, DGST3},
{TEST4, sizeof(TEST4) - 1, 10, DGST4},
{TEST5, sizeof(TEST5) - 1, 1, DGST5},
{TEST6, sizeof(TEST6) - 1, 1, DGST6},
{TEST7, sizeof(TEST7) - 1, 1, DGST7},
};
int main(void)
{
int err = 0;
SHA384_CTX ctx;
uint8_t dgst[SHA384_DIGEST_SIZE];
uint8_t dgstbuf[SHA384_DIGEST_SIZE];
size_t dgstlen;
size_t i, j;
for (i = 0; i < 7; i++) {
hex_to_bytes(tests[i].dgsthex, strlen(tests[i].dgsthex), dgstbuf, &dgstlen);
sha384_init(&ctx);
for (j = 0; j < tests[i].count; j++) {
sha384_update(&ctx, (uint8_t *)tests[i].data, tests[i].length);
}
sha384_finish(&ctx, dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
printf("sha384 test %lu failed\n", i+1);
printf("%s\n", tests[i].dgsthex);
for (j = 0; j < sizeof(dgst); j++) {
printf("%02x", dgst[j]);
}
printf("\n");
err++;
} else {
printf("sha384 test %lu ok\n", i+1);
}
}
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/hex.h>
#include <gmssl/sha2.h>
#define TEST1 "abc"
#define TEST2 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" \
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
#define TEST3 "a"
#define TEST4 "0123456701234567012345670123456701234567012345670123456701234567"
#define TEST5 "\xb9"
#define TEST6 "\xa4\x1c\x49\x77\x79\xc0\x37\x5f\xf1\x0a\x7f\x4e\x08\x59\x17\x39"
#define TEST7 "\x39\x96\x69\xe2\x8f\x6b\x9c\x6d\xbc\xbb\x69\x12\xec\x10\xff\xcf" \
"\x74\x79\x03\x49\xb7\xdc\x8f\xbe\x4a\x8e\x7b\x3b\x56\x21\xdb\x0f" \
"\x3e\x7d\xc8\x7f\x82\x32\x64\xbb\xe4\x0d\x18\x11\xc9\xea\x20\x61" \
"\xe1\xc8\x4a\xd1\x0a\x23\xfa\xc1\x72\x7e\x72\x02\xfc\x3f\x50\x42" \
"\xe6\xbf\x58\xcb\xa8\xa2\x74\x6e\x1f\x64\xf9\xb9\xea\x35\x2c\x71" \
"\x15\x07\x05\x3c\xf4\xe5\x33\x9d\x52\x86\x5f\x25\xcc\x22\xb5\xe8" \
"\x77\x84\xa1\x2f\xc9\x61\xd6\x6c\xb6\xe8\x95\x73\x19\x9a\x2c\xe6" \
"\x56\x5c\xbd\xf1\x3d\xca\x40\x38\x32\xcf\xcb\x0e\x8b\x72\x11\xe8" \
"\x3a\xf3\x2a\x11\xac\x17\x92\x9f\xf1\xc0\x73\xa5\x1c\xc0\x27\xaa" \
"\xed\xef\xf8\x5a\xad\x7c\x2b\x7c\x5a\x80\x3e\x24\x04\xd9\x6d\x2a" \
"\x77\x35\x7b\xda\x1a\x6d\xae\xed\x17\x15\x1c\xb9\xbc\x51\x25\xa4" \
"\x22\xe9\x41\xde\x0c\xa0\xfc\x50\x11\xc2\x3e\xcf\xfe\xfd\xd0\x96" \
"\x76\x71\x1c\xf3\xdb\x0a\x34\x40\x72\x0e\x16\x15\xc1\xf2\x2f\xbc" \
"\x3c\x72\x1d\xe5\x21\xe1\xb9\x9b\xa1\xbd\x55\x77\x40\x86\x42\x14" \
"\x7e\xd0\x96"
#define DGST1 "CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7"
#define DGST2 "09330C33F71147E83D192FC782CD1B4753111B173B3B05D22FA08086E3B0F712FCC7C71A557E2DB966C3E9FA91746039"
#define DGST3 "9D0E1809716474CB086E834E310A4A1CED149E9C00F248527972CEC5704C2A5B07B8B3DC38ECC4EBAE97DDD87F3D8985"
#define DGST4 "2FC64A4F500DDB6828F6A3430B8DD72A368EB7F3A8322A70BC84275B9C0B3AB00D27A5CC3C2D224AA6B61A0D79FB4596"
#define DGST5 "BC8089A19007C0B14195F4ECC74094FEC64F01F90929282C2FB392881578208AD466828B1C6C283D2722CF0AD1AB6938"
#define DGST6 "C9A68443A005812256B8EC76B00516F0DBB74FAB26D665913F194B6FFB0E91EA9967566B58109CBC675CC208E4C823F7"
#define DGST7 "4F440DB1E6EDD2899FA335F09515AA025EE177A79F4B4AAF38E42B5C4DE660F5DE8FB2A5B2FBD2A3CBFFD20CFF1288C0"
struct {
char *data;
size_t length;
size_t count;
char *dgsthex;
} tests[7] = {
{TEST1, sizeof(TEST1) - 1, 1, DGST1},
{TEST2, sizeof(TEST2) - 1, 1, DGST2},
{TEST3, sizeof(TEST3) - 1, 1000000, DGST3},
{TEST4, sizeof(TEST4) - 1, 10, DGST4},
{TEST5, sizeof(TEST5) - 1, 1, DGST5},
{TEST6, sizeof(TEST6) - 1, 1, DGST6},
{TEST7, sizeof(TEST7) - 1, 1, DGST7},
};
int main(void)
{
int err = 0;
SHA384_CTX ctx;
uint8_t dgst[SHA384_DIGEST_SIZE];
uint8_t dgstbuf[SHA384_DIGEST_SIZE];
size_t dgstlen;
size_t i, j;
for (i = 0; i < 7; i++) {
hex_to_bytes(tests[i].dgsthex, strlen(tests[i].dgsthex), dgstbuf, &dgstlen);
sha384_init(&ctx);
for (j = 0; j < tests[i].count; j++) {
sha384_update(&ctx, (uint8_t *)tests[i].data, tests[i].length);
}
sha384_finish(&ctx, dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
printf("sha384 test %lu failed\n", i+1);
printf("%s\n", tests[i].dgsthex);
for (j = 0; j < sizeof(dgst); j++) {
printf("%02x", dgst[j]);
}
printf("\n");
err++;
} else {
printf("sha384 test %lu ok\n", i+1);
}
}
return err;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,94 +7,95 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/hex.h>
#include <gmssl/sha2.h>
#define TEST1 "abc"
#define TEST2 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" \
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
#define TEST3 "a"
#define TEST4 "0123456701234567012345670123456701234567012345670123456701234567"
#define TEST5 "\xD0"
#define TEST6 "\x8d\x4e\x3c\x0e\x38\x89\x19\x14\x91\x81\x6e\x9d\x98\xbf\xf0\xa0"
#define TEST7 \
"\xa5\x5f\x20\xc4\x11\xaa\xd1\x32\x80\x7a\x50\x2d\x65\x82\x4e\x31" \
"\xa2\x30\x54\x32\xaa\x3d\x06\xd3\xe2\x82\xa8\xd8\x4e\x0d\xe1\xde" \
"\x69\x74\xbf\x49\x54\x69\xfc\x7f\x33\x8f\x80\x54\xd5\x8c\x26\xc4" \
"\x93\x60\xc3\xe8\x7a\xf5\x65\x23\xac\xf6\xd8\x9d\x03\xe5\x6f\xf2" \
"\xf8\x68\x00\x2b\xc3\xe4\x31\xed\xc4\x4d\xf2\xf0\x22\x3d\x4b\xb3" \
"\xb2\x43\x58\x6e\x1a\x7d\x92\x49\x36\x69\x4f\xcb\xba\xf8\x8d\x95" \
"\x19\xe4\xeb\x50\xa6\x44\xf8\xe4\xf9\x5e\xb0\xea\x95\xbc\x44\x65" \
"\xc8\x82\x1a\xac\xd2\xfe\x15\xab\x49\x81\x16\x4b\xbb\x6d\xc3\x2f" \
"\x96\x90\x87\xa1\x45\xb0\xd9\xcc\x9c\x67\xc2\x2b\x76\x32\x99\x41" \
"\x9c\xc4\x12\x8b\xe9\xa0\x77\xb3\xac\xe6\x34\x06\x4e\x6d\x99\x28" \
"\x35\x13\xdc\x06\xe7\x51\x5d\x0d\x73\x13\x2e\x9a\x0d\xc6\xd3\xb1" \
"\xf8\xb2\x46\xf1\xa9\x8a\x3f\xc7\x29\x41\xb1\xe3\xbb\x20\x98\xe8" \
"\xbf\x16\xf2\x68\xd6\x4f\x0b\x0f\x47\x07\xfe\x1e\xa1\xa1\x79\x1b" \
"\xa2\xf3\xc0\xc7\x58\xe5\xf5\x51\x86\x3a\x96\xc9\x49\xad\x47\xd7" \
"\xfb\x40\xd2"
#define DGST1 "DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F"
#define DGST2 "8E959B75DAE313DA8CF4F72814FC143F8F7779C6EB9F7FA17299AEADB6889018501D289E4900F7E4331B99DEC4B5433AC7D329EEB6DD26545E96E55B874BE909"
#define DGST3 "E718483D0CE769644E2E42C7BC15B4638E1F98B13B2044285632A803AFA973EBDE0FF244877EA60A4CB0432CE577C31BEB009C5C2C49AA2E4EADB217AD8CC09B"
#define DGST4 "89D05BA632C699C31231DED4FFC127D5A894DAD412C0E024DB872D1ABD2BA8141A0F85072A9BE1E2AA04CF33C765CB510813A39CD5A84C4ACAA64D3F3FB7BAE9"
#define DGST5 "9992202938E882E73E20F6B69E68A0A7149090423D93C81BAB3F21678D4ACEEEE50E4E8CAFADA4C85A54EA8306826C4AD6E74CECE9631BFA8A549B4AB3FBBA15"
#define DGST6 "CB0B67A4B8712CD73C9AABC0B199E9269B20844AFB75ACBDD1C153C9828924C3DDEDAAFE669C5FDD0BC66F630F6773988213EB1B16F517AD0DE4B2F0C95C90F8"
#define DGST7 "C665BEFB36DA189D78822D10528CBF3B12B3EEF726039909C1A16A270D48719377966B957A878E720584779A62825C18DA26415E49A7176A894E7510FD1451F5"
struct {
char *data;
size_t length;
size_t count;
char *dgsthex;
} tests[7] = {
{TEST1, sizeof(TEST1) - 1, 1, DGST1},
{TEST2, sizeof(TEST2) - 1, 1, DGST2},
{TEST3, sizeof(TEST3) - 1, 1000000, DGST3},
{TEST4, sizeof(TEST4) - 1, 10, DGST4},
{TEST5, sizeof(TEST5) - 1, 1, DGST5},
{TEST6, sizeof(TEST6) - 1, 1, DGST6},
{TEST7, sizeof(TEST7) - 1, 1, DGST7},
};
int main(void)
{
int err = 0;
SHA512_CTX ctx;
uint8_t dgst[SHA512_DIGEST_SIZE];
uint8_t dgstbuf[SHA512_DIGEST_SIZE];
size_t dgstlen;
size_t i, j;
for (i = 0; i < 7; i++) {
hex_to_bytes(tests[i].dgsthex, strlen(tests[i].dgsthex), dgstbuf, &dgstlen);
sha512_init(&ctx);
for (j = 0; j < tests[i].count; j++) {
sha512_update(&ctx, (uint8_t *)tests[i].data, tests[i].length);
}
sha512_finish(&ctx, dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
printf("sha512 test %lu failed\n", i+1);
printf("%s\n", tests[i].dgsthex);
for (j = 0; j < sizeof(dgst); j++) {
printf("%02x", dgst[j]);
}
printf("\n");
err++;
} else {
printf("sha512 test %lu ok\n", i+1);
}
}
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/hex.h>
#include <gmssl/sha2.h>
#define TEST1 "abc"
#define TEST2 "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" \
"hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"
#define TEST3 "a"
#define TEST4 "0123456701234567012345670123456701234567012345670123456701234567"
#define TEST5 "\xD0"
#define TEST6 "\x8d\x4e\x3c\x0e\x38\x89\x19\x14\x91\x81\x6e\x9d\x98\xbf\xf0\xa0"
#define TEST7 \
"\xa5\x5f\x20\xc4\x11\xaa\xd1\x32\x80\x7a\x50\x2d\x65\x82\x4e\x31" \
"\xa2\x30\x54\x32\xaa\x3d\x06\xd3\xe2\x82\xa8\xd8\x4e\x0d\xe1\xde" \
"\x69\x74\xbf\x49\x54\x69\xfc\x7f\x33\x8f\x80\x54\xd5\x8c\x26\xc4" \
"\x93\x60\xc3\xe8\x7a\xf5\x65\x23\xac\xf6\xd8\x9d\x03\xe5\x6f\xf2" \
"\xf8\x68\x00\x2b\xc3\xe4\x31\xed\xc4\x4d\xf2\xf0\x22\x3d\x4b\xb3" \
"\xb2\x43\x58\x6e\x1a\x7d\x92\x49\x36\x69\x4f\xcb\xba\xf8\x8d\x95" \
"\x19\xe4\xeb\x50\xa6\x44\xf8\xe4\xf9\x5e\xb0\xea\x95\xbc\x44\x65" \
"\xc8\x82\x1a\xac\xd2\xfe\x15\xab\x49\x81\x16\x4b\xbb\x6d\xc3\x2f" \
"\x96\x90\x87\xa1\x45\xb0\xd9\xcc\x9c\x67\xc2\x2b\x76\x32\x99\x41" \
"\x9c\xc4\x12\x8b\xe9\xa0\x77\xb3\xac\xe6\x34\x06\x4e\x6d\x99\x28" \
"\x35\x13\xdc\x06\xe7\x51\x5d\x0d\x73\x13\x2e\x9a\x0d\xc6\xd3\xb1" \
"\xf8\xb2\x46\xf1\xa9\x8a\x3f\xc7\x29\x41\xb1\xe3\xbb\x20\x98\xe8" \
"\xbf\x16\xf2\x68\xd6\x4f\x0b\x0f\x47\x07\xfe\x1e\xa1\xa1\x79\x1b" \
"\xa2\xf3\xc0\xc7\x58\xe5\xf5\x51\x86\x3a\x96\xc9\x49\xad\x47\xd7" \
"\xfb\x40\xd2"
#define DGST1 "DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F"
#define DGST2 "8E959B75DAE313DA8CF4F72814FC143F8F7779C6EB9F7FA17299AEADB6889018501D289E4900F7E4331B99DEC4B5433AC7D329EEB6DD26545E96E55B874BE909"
#define DGST3 "E718483D0CE769644E2E42C7BC15B4638E1F98B13B2044285632A803AFA973EBDE0FF244877EA60A4CB0432CE577C31BEB009C5C2C49AA2E4EADB217AD8CC09B"
#define DGST4 "89D05BA632C699C31231DED4FFC127D5A894DAD412C0E024DB872D1ABD2BA8141A0F85072A9BE1E2AA04CF33C765CB510813A39CD5A84C4ACAA64D3F3FB7BAE9"
#define DGST5 "9992202938E882E73E20F6B69E68A0A7149090423D93C81BAB3F21678D4ACEEEE50E4E8CAFADA4C85A54EA8306826C4AD6E74CECE9631BFA8A549B4AB3FBBA15"
#define DGST6 "CB0B67A4B8712CD73C9AABC0B199E9269B20844AFB75ACBDD1C153C9828924C3DDEDAAFE669C5FDD0BC66F630F6773988213EB1B16F517AD0DE4B2F0C95C90F8"
#define DGST7 "C665BEFB36DA189D78822D10528CBF3B12B3EEF726039909C1A16A270D48719377966B957A878E720584779A62825C18DA26415E49A7176A894E7510FD1451F5"
struct {
char *data;
size_t length;
size_t count;
char *dgsthex;
} tests[7] = {
{TEST1, sizeof(TEST1) - 1, 1, DGST1},
{TEST2, sizeof(TEST2) - 1, 1, DGST2},
{TEST3, sizeof(TEST3) - 1, 1000000, DGST3},
{TEST4, sizeof(TEST4) - 1, 10, DGST4},
{TEST5, sizeof(TEST5) - 1, 1, DGST5},
{TEST6, sizeof(TEST6) - 1, 1, DGST6},
{TEST7, sizeof(TEST7) - 1, 1, DGST7},
};
int main(void)
{
int err = 0;
SHA512_CTX ctx;
uint8_t dgst[SHA512_DIGEST_SIZE];
uint8_t dgstbuf[SHA512_DIGEST_SIZE];
size_t dgstlen;
size_t i, j;
for (i = 0; i < 7; i++) {
hex_to_bytes(tests[i].dgsthex, strlen(tests[i].dgsthex), dgstbuf, &dgstlen);
sha512_init(&ctx);
for (j = 0; j < tests[i].count; j++) {
sha512_update(&ctx, (uint8_t *)tests[i].data, tests[i].length);
}
sha512_finish(&ctx, dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
printf("sha512 test %lu failed\n", i+1);
printf("%s\n", tests[i].dgsthex);
for (j = 0; j < sizeof(dgst); j++) {
printf("%02x", dgst[j]);
}
printf("\n");
err++;
} else {
printf("sha512 test %lu ok\n", i+1);
}
}
return err;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,178 +7,179 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/sm3.h>
#include <gmssl/hex.h>
#include <gmssl/error.h>
static int test_sm3(void)
{
const char *testhex[] = {
/* 0 "abc" */
"616263",
/* 1 "abcd" 16 times */
"6162636461626364616263646162636461626364616263646162636461626364"
"6162636461626364616263646162636461626364616263646162636461626364",
/* 2 p.57 ZA */
"0090"
"414C494345313233405941484F4F2E434F4D"
"787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498"
"63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A"
"421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D"
"0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2"
"0AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A"
"7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857",
/* 3 p.59 ZA */
"0090"
"414C494345313233405941484F4F2E434F4D"
"000000000000000000000000000000000000000000000000000000000000000000"
"00E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B"
"00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD"
"013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E"
"0165961645281A8626607B917F657D7E9382F1EA5CD931F40F6627F357542653B2"
"01686522130D590FB8DE635D8FCA715CC6BF3D05BEF3F75DA5D543454448166612",
/* 4 p.72 ZA */
"0090"
"414C494345313233405941484F4F2E434F4D"
"787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498"
"63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A"
"421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D"
"0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2"
"3099093BF3C137D8FCBBCDF4A2AE50F3B0F216C3122D79425FE03A45DBFE1655"
"3DF79E8DAC1CF0ECBAA2F2B49D51A4B387F2EFAF482339086A27A8E05BAED98B",
/* 5 p.72 ZB */
"0088"
"42494C4C343536405941484F4F2E434F4D"
"787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498"
"63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A"
"421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D"
"0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2"
"245493D446C38D8CC0F118374690E7DF633A8A4BFB3329B5ECE604B2B4F37F43"
"53C0869F4B9E17773DE68FEC45E14904E0DEA45BF6CECF9918C85EA047C60A4C",
/* 6 p.75 ZA */
"0090"
"414C494345313233405941484F4F2E434F4D"
"000000000000000000000000000000000000000000000000000000000000000000"
"00E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B"
"00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD"
"013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E"
"008E3BDB2E11F9193388F1F901CCC857BF49CFC065FB38B9069CAAE6D5AFC3592F"
"004555122AAC0075F42E0A8BBD2C0665C789120DF19D77B4E3EE4712F598040415",
/* 7 p.76 ZB */
"0088"
"42494C4C343536405941484F4F2E434F4D"
"000000000000000000000000000000000000000000000000000000000000000000"
"00E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B"
"00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD"
"013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E"
"0034297DD83AB14D5B393B6712F32B2F2E938D4690B095424B89DA880C52D4A7D9"
"0199BBF11AC95A0EA34BBD00CA50B93EC24ACB68335D20BA5DCFE3B33BDBD2B62D",
/* 8 TopsecCA cert ZA */
"0080"
"31323334353637383132333435363738"
"FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC"
"28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93"
"32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7"
"BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0"
"D69C2F1EEC3BFB6B95B30C28085C77B125D77A9C39525D8190768F37D6B205B5"
"89DCD316BBE7D89A9DC21917F17799E698531F5E6E3E10BD31370B259C3F81C3",
/* 9 */
"4D38D2958CA7FD2CFAE3AF04486959CF92C8EF48E8B83A05C112E739D5F181D0"
"3082020CA003020102020900"
"AF28725D98D33143300C06082A811CCF"
"550183750500307D310B300906035504"
"060C02636E310B300906035504080C02"
"626A310B300906035504070C02626A31"
"0F300D060355040A0C06746F70736563"
"310F300D060355040B0C06746F707365"
"633111300F06035504030C08546F7073"
"65634341311F301D06092A864886F70D"
"0109010C10626A40746F707365632E63"
"6F6D2E636E301E170D31323036323430"
"37353433395A170D3332303632303037"
"353433395A307D310B30090603550406"
"0C02636E310B300906035504080C0262"
"6A310B300906035504070C02626A310F"
"300D060355040A0C06746F7073656331"
"0F300D060355040B0C06746F70736563"
"3111300F06035504030C08546F707365"
"634341311F301D06092A864886F70D01"
"09010C10626A40746F707365632E636F"
"6D2E636E3059301306072A8648CE3D02"
"0106082A811CCF5501822D03420004D6"
"9C2F1EEC3BFB6B95B30C28085C77B125"
"D77A9C39525D8190768F37D6B205B589"
"DCD316BBE7D89A9DC21917F17799E698"
"531F5E6E3E10BD31370B259C3F81C3A3"
"733071300F0603551D130101FF040530"
"030101FF301D0603551D0E041604148E"
"5D90347858BAAAD870D8BDFBA6A85E7B"
"563B64301F0603551D23041830168014"
"8E5D90347858BAAAD870D8BDFBA6A85E"
"7B563B64300B0603551D0F0404030201"
"06301106096086480186F84201010404"
"03020057",
};
const char *dgsthex[] = {
"66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0",
"debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732",
"F4A38489E32B45B6F876E3AC2168CA392362DC8F23459C1D1146FC3DBFB7BC9A",
"26352AF82EC19F207BBC6F9474E11E90CE0F7DDACE03B27F801817E897A81FD5",
"E4D1D0C3CA4C7F11BC8FF8CB3F4C02A78F108FA098E51A668487240F75E20F31",
"6B4B6D0E276691BD4A11BF72F4FB501AE309FDACB72FA6CC336E6656119ABD67",
"329c2f6030cc7e0ca3af6c97b76243ca250338ad3d3dc3a8b322d1cfdf98c2b7",
/*"ECF0080215977B2E5D6D61B98A99442F03E8803DC39E349F8DCA5621A9ACDF2B",*/
"557BAD30E183559AEEC3B2256E1C7C11F870D22B165D015ACF9465B09B87B527",
"4D38D2958CA7FD2CFAE3AF04486959CF92C8EF48E8B83A05C112E739D5F181D0",
"C3B02E500A8B60B77DEDCF6F4C11BEF8D56E5CDE708C72065654FD7B2167915A",
};
int err = 0;
char *p;
uint8_t testbuf[sizeof(testhex)/2 + 1000];
uint8_t dgstbuf[32];
size_t testbuflen, dgstbuflen;
uint8_t dgst[32];
size_t i;
for (i = 0; i < sizeof(testhex)/sizeof(testhex[0]); i++) {
hex_to_bytes(testhex[i], strlen(testhex[i]), testbuf, &testbuflen);
hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstbuflen);
sm3_digest(testbuf, testbuflen, dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
int n;
fprintf(stderr, "sm3 test %lu failed\n", i+1);
fprintf(stderr, "error calculating SM3 on %s\n", testhex[i]);
fprintf(stderr, " digest(corret) = ");
for (n = 0; n < sizeof(dgst); n++) {
fprintf(stderr, "%02X", dgst[n]);
}
fprintf(stderr, "\n");
fprintf(stderr, " digest(error) = %s\n", dgsthex[i]);
return -1;
}
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_sm3() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return 1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <gmssl/sm3.h>
#include <gmssl/hex.h>
#include <gmssl/error.h>
static int test_sm3(void)
{
const char *testhex[] = {
/* 0 "abc" */
"616263",
/* 1 "abcd" 16 times */
"6162636461626364616263646162636461626364616263646162636461626364"
"6162636461626364616263646162636461626364616263646162636461626364",
/* 2 p.57 ZA */
"0090"
"414C494345313233405941484F4F2E434F4D"
"787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498"
"63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A"
"421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D"
"0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2"
"0AE4C7798AA0F119471BEE11825BE46202BB79E2A5844495E97C04FF4DF2548A"
"7C0240F88F1CD4E16352A73C17B7F16F07353E53A176D684A9FE0C6BB798E857",
/* 3 p.59 ZA */
"0090"
"414C494345313233405941484F4F2E434F4D"
"000000000000000000000000000000000000000000000000000000000000000000"
"00E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B"
"00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD"
"013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E"
"0165961645281A8626607B917F657D7E9382F1EA5CD931F40F6627F357542653B2"
"01686522130D590FB8DE635D8FCA715CC6BF3D05BEF3F75DA5D543454448166612",
/* 4 p.72 ZA */
"0090"
"414C494345313233405941484F4F2E434F4D"
"787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498"
"63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A"
"421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D"
"0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2"
"3099093BF3C137D8FCBBCDF4A2AE50F3B0F216C3122D79425FE03A45DBFE1655"
"3DF79E8DAC1CF0ECBAA2F2B49D51A4B387F2EFAF482339086A27A8E05BAED98B",
/* 5 p.72 ZB */
"0088"
"42494C4C343536405941484F4F2E434F4D"
"787968B4FA32C3FD2417842E73BBFEFF2F3C848B6831D7E0EC65228B3937E498"
"63E4C6D3B23B0C849CF84241484BFE48F61D59A5B16BA06E6E12D1DA27C5249A"
"421DEBD61B62EAB6746434EBC3CC315E32220B3BADD50BDC4C4E6C147FEDD43D"
"0680512BCBB42C07D47349D2153B70C4E5D7FDFCBFA36EA1A85841B9E46E09A2"
"245493D446C38D8CC0F118374690E7DF633A8A4BFB3329B5ECE604B2B4F37F43"
"53C0869F4B9E17773DE68FEC45E14904E0DEA45BF6CECF9918C85EA047C60A4C",
/* 6 p.75 ZA */
"0090"
"414C494345313233405941484F4F2E434F4D"
"000000000000000000000000000000000000000000000000000000000000000000"
"00E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B"
"00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD"
"013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E"
"008E3BDB2E11F9193388F1F901CCC857BF49CFC065FB38B9069CAAE6D5AFC3592F"
"004555122AAC0075F42E0A8BBD2C0665C789120DF19D77B4E3EE4712F598040415",
/* 7 p.76 ZB */
"0088"
"42494C4C343536405941484F4F2E434F4D"
"000000000000000000000000000000000000000000000000000000000000000000"
"00E78BCD09746C202378A7E72B12BCE00266B9627ECB0B5A25367AD1AD4CC6242B"
"00CDB9CA7F1E6B0441F658343F4B10297C0EF9B6491082400A62E7A7485735FADD"
"013DE74DA65951C4D76DC89220D5F7777A611B1C38BAE260B175951DC8060C2B3E"
"0034297DD83AB14D5B393B6712F32B2F2E938D4690B095424B89DA880C52D4A7D9"
"0199BBF11AC95A0EA34BBD00CA50B93EC24ACB68335D20BA5DCFE3B33BDBD2B62D",
/* 8 TopsecCA cert ZA */
"0080"
"31323334353637383132333435363738"
"FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC"
"28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93"
"32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7"
"BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0"
"D69C2F1EEC3BFB6B95B30C28085C77B125D77A9C39525D8190768F37D6B205B5"
"89DCD316BBE7D89A9DC21917F17799E698531F5E6E3E10BD31370B259C3F81C3",
/* 9 */
"4D38D2958CA7FD2CFAE3AF04486959CF92C8EF48E8B83A05C112E739D5F181D0"
"3082020CA003020102020900"
"AF28725D98D33143300C06082A811CCF"
"550183750500307D310B300906035504"
"060C02636E310B300906035504080C02"
"626A310B300906035504070C02626A31"
"0F300D060355040A0C06746F70736563"
"310F300D060355040B0C06746F707365"
"633111300F06035504030C08546F7073"
"65634341311F301D06092A864886F70D"
"0109010C10626A40746F707365632E63"
"6F6D2E636E301E170D31323036323430"
"37353433395A170D3332303632303037"
"353433395A307D310B30090603550406"
"0C02636E310B300906035504080C0262"
"6A310B300906035504070C02626A310F"
"300D060355040A0C06746F7073656331"
"0F300D060355040B0C06746F70736563"
"3111300F06035504030C08546F707365"
"634341311F301D06092A864886F70D01"
"09010C10626A40746F707365632E636F"
"6D2E636E3059301306072A8648CE3D02"
"0106082A811CCF5501822D03420004D6"
"9C2F1EEC3BFB6B95B30C28085C77B125"
"D77A9C39525D8190768F37D6B205B589"
"DCD316BBE7D89A9DC21917F17799E698"
"531F5E6E3E10BD31370B259C3F81C3A3"
"733071300F0603551D130101FF040530"
"030101FF301D0603551D0E041604148E"
"5D90347858BAAAD870D8BDFBA6A85E7B"
"563B64301F0603551D23041830168014"
"8E5D90347858BAAAD870D8BDFBA6A85E"
"7B563B64300B0603551D0F0404030201"
"06301106096086480186F84201010404"
"03020057",
};
const char *dgsthex[] = {
"66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0",
"debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732",
"F4A38489E32B45B6F876E3AC2168CA392362DC8F23459C1D1146FC3DBFB7BC9A",
"26352AF82EC19F207BBC6F9474E11E90CE0F7DDACE03B27F801817E897A81FD5",
"E4D1D0C3CA4C7F11BC8FF8CB3F4C02A78F108FA098E51A668487240F75E20F31",
"6B4B6D0E276691BD4A11BF72F4FB501AE309FDACB72FA6CC336E6656119ABD67",
"329c2f6030cc7e0ca3af6c97b76243ca250338ad3d3dc3a8b322d1cfdf98c2b7",
/*"ECF0080215977B2E5D6D61B98A99442F03E8803DC39E349F8DCA5621A9ACDF2B",*/
"557BAD30E183559AEEC3B2256E1C7C11F870D22B165D015ACF9465B09B87B527",
"4D38D2958CA7FD2CFAE3AF04486959CF92C8EF48E8B83A05C112E739D5F181D0",
"C3B02E500A8B60B77DEDCF6F4C11BEF8D56E5CDE708C72065654FD7B2167915A",
};
int err = 0;
char *p;
uint8_t testbuf[sizeof(testhex)/2 + 1000];
uint8_t dgstbuf[32];
size_t testbuflen, dgstbuflen;
uint8_t dgst[32];
size_t i;
for (i = 0; i < sizeof(testhex)/sizeof(testhex[0]); i++) {
hex_to_bytes(testhex[i], strlen(testhex[i]), testbuf, &testbuflen);
hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstbuflen);
sm3_digest(testbuf, testbuflen, dgst);
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
int n;
fprintf(stderr, "sm3 test %lu failed\n", i+1);
fprintf(stderr, "error calculating SM3 on %s\n", testhex[i]);
fprintf(stderr, " digest(corret) = ");
for (n = 0; n < sizeof(dgst); n++) {
fprintf(stderr, "%02X", dgst[n]);
}
fprintf(stderr, "\n");
fprintf(stderr, " digest(error) = %s\n", dgsthex[i]);
return -1;
}
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_sm3() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return 1;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,75 +7,76 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
#include <gmssl/tls.h>
#include <gmssl/sm3.h>
#include <gmssl/sm4.h>
static int test_tls13_gcm(void)
{
BLOCK_CIPHER_KEY block_key;
uint8_t key[16];
uint8_t iv[12];
uint8_t seq_num[8] = {0,0,0,0,0,0,0,1};
int record_type = TLS_record_handshake;
uint8_t in[40];
size_t padding_len = 8;
uint8_t out[256];
size_t outlen;
uint8_t buf[256];
size_t buflen;
rand_bytes(key, sizeof(key));
rand_bytes(iv, sizeof(iv));
rand_bytes(in, sizeof(in));
memset(out, 1, sizeof(out));
outlen = 0;
memset(buf, 1, sizeof(buf));
buflen = 0;
if (block_cipher_set_encrypt_key(&block_key, BLOCK_CIPHER_sm4(), key) != 1) {
error_print();
return -1;
}
if (tls13_gcm_encrypt(&block_key, iv, seq_num, record_type, in, sizeof(in), padding_len, out, &outlen) != 1) {
error_print();
return -1;
}
if (tls13_gcm_decrypt(&block_key, iv, seq_num, out, outlen, &record_type, buf, &buflen) != 1) {
error_print();
return -1;
}
if (buflen != sizeof(in)) {
error_print();
return -1;
}
if (memcmp(in, buf, buflen) != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_tls13_gcm() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
#include <gmssl/tls.h>
#include <gmssl/sm3.h>
#include <gmssl/sm4.h>
static int test_tls13_gcm(void)
{
BLOCK_CIPHER_KEY block_key;
uint8_t key[16];
uint8_t iv[12];
uint8_t seq_num[8] = {0,0,0,0,0,0,0,1};
int record_type = TLS_record_handshake;
uint8_t in[40];
size_t padding_len = 8;
uint8_t out[256];
size_t outlen;
uint8_t buf[256];
size_t buflen;
rand_bytes(key, sizeof(key));
rand_bytes(iv, sizeof(iv));
rand_bytes(in, sizeof(in));
memset(out, 1, sizeof(out));
outlen = 0;
memset(buf, 1, sizeof(buf));
buflen = 0;
if (block_cipher_set_encrypt_key(&block_key, BLOCK_CIPHER_sm4(), key) != 1) {
error_print();
return -1;
}
if (tls13_gcm_encrypt(&block_key, iv, seq_num, record_type, in, sizeof(in), padding_len, out, &outlen) != 1) {
error_print();
return -1;
}
if (tls13_gcm_decrypt(&block_key, iv, seq_num, out, outlen, &record_type, buf, &buflen) != 1) {
error_print();
return -1;
}
if (buflen != sizeof(in)) {
error_print();
return -1;
}
if (memcmp(in, buf, buflen) != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_tls13_gcm() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,340 +7,341 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
#include <gmssl/tls.h>
#include <gmssl/sm3.h>
#include <gmssl/sm4.h>
static int test_tls_encode(void)
{
uint8_t a1 = 200;
uint16_t a2 = 30000;
uint24_t a3 = 4000000;
uint32_t a4 = 4000000000;
uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
uint8_t r1;
uint16_t r2;
uint24_t r3;
uint32_t r4;
const uint8_t *pdata;
size_t datalen;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
tls_uint8_to_bytes(a1, &p, &len);
tls_uint16_to_bytes(a2, &p, &len);
tls_uint24_to_bytes(a3, &p, &len);
tls_uint32_to_bytes(a4, &p, &len);
tls_uint8array_to_bytes(data, 5, &p, &len);
tls_uint16array_to_bytes(data, 6, &p, &len);
tls_uint24array_to_bytes(data, 7, &p, &len);
if (tls_uint8_from_bytes(&r1, &cp, &len) != 1 || r1 != a1
|| tls_uint16_from_bytes(&r2, &cp, &len) != 1 || r2 != a2
|| tls_uint24_from_bytes(&r3, &cp, &len) != 1 || r3 != a3
|| tls_uint32_from_bytes(&r4, &cp, &len) != 1 || r4 != a4
|| tls_uint8array_from_bytes(&pdata, &datalen, &cp, &len) != 1 || datalen != 5 || memcmp(pdata, data, 5) != 0
|| tls_uint16array_from_bytes(&pdata, &datalen, &cp, &len) != 1 || datalen != 6 || memcmp(pdata, data, 6) != 0
|| tls_uint24array_from_bytes(&pdata, &datalen, &cp, &len) != 1 || datalen != 7 || memcmp(pdata, data, 7) != 0
|| len > 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_cbc(void)
{
uint8_t key[32] = {0};
SM3_HMAC_CTX hmac_ctx;
SM4_KEY sm4_key;
uint8_t seq_num[8] = { 0,0,0,0,0,0,0,1 };
uint8_t header[5];
uint8_t in[] = "hello world";
uint8_t out[256];
uint8_t buf[256] = {0};
size_t len;
size_t buflen;
header[0] = TLS_record_handshake;
header[1] = TLS_protocol_tls12 >> 8;
header[2] = TLS_protocol_tls12 & 0xff;
header[3] = sizeof(in) >> 8;
header[4] = sizeof(in) & 0xff;
sm3_hmac_init(&hmac_ctx, key, 32);
sm4_set_encrypt_key(&sm4_key, key);
tls_cbc_encrypt(&hmac_ctx, &sm4_key, seq_num, header, in, sizeof(in), out, &len);
sm3_hmac_init(&hmac_ctx, key, 32);
sm4_set_decrypt_key(&sm4_key, key);
tls_cbc_decrypt(&hmac_ctx, &sm4_key, seq_num, header, out, len, buf, &buflen);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_random(void)
{
uint8_t random[32];
tls_random_generate(random);
tls_random_print(stdout, random, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_client_hello(void)
{
uint8_t record[512];
size_t recordlen = 0;
int version = TLS_protocol_tlcp;
uint8_t random[32];
int cipher_suites[] = {
TLS_cipher_ecc_sm4_cbc_sm3,
TLS_cipher_ecc_sm4_gcm_sm3,
TLS_cipher_ecdhe_sm4_cbc_sm3,
TLS_cipher_ecdhe_sm4_gcm_sm3,
TLS_cipher_ibsdh_sm4_cbc_sm3,
TLS_cipher_ibsdh_sm4_gcm_sm3,
TLS_cipher_ibc_sm4_cbc_sm3,
TLS_cipher_ibc_sm4_gcm_sm3,
TLS_cipher_rsa_sm4_cbc_sm3,
TLS_cipher_rsa_sm4_gcm_sm3,
TLS_cipher_rsa_sm4_cbc_sha256,
TLS_cipher_rsa_sm4_gcm_sha256,
};
int comp_meths[] = {0};
tls_record_set_protocol(record, TLS_protocol_tlcp);
if (tls_record_set_handshake_client_hello(record, &recordlen,
version,
random,
NULL, 0,
cipher_suites, sizeof(cipher_suites)/sizeof(cipher_suites[0]),
NULL, 0) != 1) {
error_print();
return -1;
}
tls_client_hello_print(stdout, record + 5 + 4, recordlen - 5 -4, 0, 4);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_server_hello(void)
{
uint8_t record[512];
size_t recordlen = 0;
uint8_t random[32];
uint16_t cipher_suite = TLS_cipher_ecdhe_sm4_cbc_sm3;
tls_record_set_protocol(record, TLS_protocol_tlcp);
if (tls_record_set_handshake_server_hello(record, &recordlen,
TLS_protocol_tlcp,
random,
NULL, 0,
cipher_suite,
NULL, 0) != 1) {
error_print();
return -1;
}
tls_server_hello_print(stdout, record + 5 + 4, recordlen - 5 -4, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_certificate(void)
{
uint8_t record[1024];
size_t recordlen = 0;
FILE *fp = NULL;
// 测试函数不要有外部的依赖
// TODO: 输出一些握手过程的record字节数组和handshake字节数组作为后续测试的测试数据
/*
if (!(fp = fopen("cacert.pem", "r"))) {
error_print();
return -1;
}
if (tls_record_set_handshake_certificate_from_pem(record, &recordlen, fp) != 1) {
error_print();
return -1;
}
tls_certificate_print(stdout, record + 9, recordlen - 9, 0, 0);
*/
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_server_key_exchange(void)
{
uint8_t record[1024];
size_t recordlen = 0;
uint8_t sig[SM2_MAX_SIGNATURE_SIZE] = {0xAA, 0xBB};
const uint8_t *psig;
size_t siglen;
tls_record_set_protocol(record, TLS_protocol_tlcp);
if (tlcp_record_set_handshake_server_key_exchange_pke(record, &recordlen, sig, sizeof(sig)) != 1) {
error_print();
return -1;
}
if (tlcp_record_get_handshake_server_key_exchange_pke(record, &psig, &siglen) != 1) {
error_print();
return -1;
}
format_bytes(stdout, 0, 0, "server_key_exchange siganture", psig, siglen);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_certificate_verify(void)
{
uint8_t record[1024];
size_t recordlen = 0;
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
const uint8_t *psig;
size_t siglen;
tls_record_set_protocol(record, TLS_protocol_tls12);
if (tls_record_set_handshake_certificate_verify(record, &recordlen, sig, sizeof(sig)) != 1) {
error_print();
return -1;
}
if (tls_record_get_handshake_certificate_verify(record, &psig, &siglen) != 1) {
error_print();
return -1;
}
tls_certificate_verify_print(stdout, psig, siglen, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_finished(void)
{
uint8_t record[1024];
size_t recordlen = 0;
uint8_t verify_data[12];
const uint8_t *verify_data_ptr;
size_t verify_data_len;
if (tls_record_set_handshake_finished(record, &recordlen, verify_data, sizeof(verify_data)) != 1) {
error_print();
return -1;
}
if (tls_record_get_handshake_finished(record, &verify_data_ptr, &verify_data_len) != 1) {
error_print();
return -1;
}
tls_finished_print(stdout, verify_data_ptr, verify_data_len, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_alert(void)
{
uint8_t record[1024];
size_t recordlen = 0;
int level;
int reason;
if (tls_record_set_alert(record, &recordlen, TLS_alert_level_fatal, TLS_alert_close_notify) != 1) {
error_print();
return -1;
}
if (tls_record_get_alert(record, &level, &reason) != 1) {
error_print();
return -1;
}
tls_alert_print(stdout, record + 5, recordlen - 5, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_change_cipher_spec(void)
{
uint8_t record[1024];
size_t recordlen = 0;
if (tls_record_set_change_cipher_spec(record, &recordlen) != 1) {
error_print();
return -1;
}
if (tls_record_get_change_cipher_spec(record) != 1) {
error_print();
return -1;
}
tls_change_cipher_spec_print(stdout, record + 5, recordlen - 5, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_application_data(void)
{
uint8_t record[1024];
size_t recordlen = 0;
uint8_t data[88];
const uint8_t *p;
size_t len;
if (tls_record_set_application_data(record, &recordlen, data, sizeof(data)) != 1) {
error_print();
return -1;
}
if (tls_record_get_application_data(record, &p, &len) != 1) {
error_print();
return -1;
}
tls_application_data_print(stdout, p, len, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_tls_encode() != 1) goto err;
if (test_tls_cbc() != 1) goto err;
if (test_tls_random() != 1) goto err;
if (test_tls_client_hello() != 1) goto err;
if (test_tls_server_hello() != 1) goto err;
if (test_tls_certificate() != 1) goto err;
if (test_tls_server_key_exchange() != 1) goto err;
if (test_tls_certificate_verify() != 1) goto err;
if (test_tls_finished() != 1) goto err;
if (test_tls_alert() != 1) goto err;
if (test_tls_change_cipher_spec() != 1) goto err;
if (test_tls_application_data() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
#include <gmssl/tls.h>
#include <gmssl/sm3.h>
#include <gmssl/sm4.h>
static int test_tls_encode(void)
{
uint8_t a1 = 200;
uint16_t a2 = 30000;
uint24_t a3 = 4000000;
uint32_t a4 = 4000000000;
uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8};
uint8_t r1;
uint16_t r2;
uint24_t r3;
uint32_t r4;
const uint8_t *pdata;
size_t datalen;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
tls_uint8_to_bytes(a1, &p, &len);
tls_uint16_to_bytes(a2, &p, &len);
tls_uint24_to_bytes(a3, &p, &len);
tls_uint32_to_bytes(a4, &p, &len);
tls_uint8array_to_bytes(data, 5, &p, &len);
tls_uint16array_to_bytes(data, 6, &p, &len);
tls_uint24array_to_bytes(data, 7, &p, &len);
if (tls_uint8_from_bytes(&r1, &cp, &len) != 1 || r1 != a1
|| tls_uint16_from_bytes(&r2, &cp, &len) != 1 || r2 != a2
|| tls_uint24_from_bytes(&r3, &cp, &len) != 1 || r3 != a3
|| tls_uint32_from_bytes(&r4, &cp, &len) != 1 || r4 != a4
|| tls_uint8array_from_bytes(&pdata, &datalen, &cp, &len) != 1 || datalen != 5 || memcmp(pdata, data, 5) != 0
|| tls_uint16array_from_bytes(&pdata, &datalen, &cp, &len) != 1 || datalen != 6 || memcmp(pdata, data, 6) != 0
|| tls_uint24array_from_bytes(&pdata, &datalen, &cp, &len) != 1 || datalen != 7 || memcmp(pdata, data, 7) != 0
|| len > 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_cbc(void)
{
uint8_t key[32] = {0};
SM3_HMAC_CTX hmac_ctx;
SM4_KEY sm4_key;
uint8_t seq_num[8] = { 0,0,0,0,0,0,0,1 };
uint8_t header[5];
uint8_t in[] = "hello world";
uint8_t out[256];
uint8_t buf[256] = {0};
size_t len;
size_t buflen;
header[0] = TLS_record_handshake;
header[1] = TLS_protocol_tls12 >> 8;
header[2] = TLS_protocol_tls12 & 0xff;
header[3] = sizeof(in) >> 8;
header[4] = sizeof(in) & 0xff;
sm3_hmac_init(&hmac_ctx, key, 32);
sm4_set_encrypt_key(&sm4_key, key);
tls_cbc_encrypt(&hmac_ctx, &sm4_key, seq_num, header, in, sizeof(in), out, &len);
sm3_hmac_init(&hmac_ctx, key, 32);
sm4_set_decrypt_key(&sm4_key, key);
tls_cbc_decrypt(&hmac_ctx, &sm4_key, seq_num, header, out, len, buf, &buflen);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_random(void)
{
uint8_t random[32];
tls_random_generate(random);
tls_random_print(stdout, random, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_client_hello(void)
{
uint8_t record[512];
size_t recordlen = 0;
int version = TLS_protocol_tlcp;
uint8_t random[32];
int cipher_suites[] = {
TLS_cipher_ecc_sm4_cbc_sm3,
TLS_cipher_ecc_sm4_gcm_sm3,
TLS_cipher_ecdhe_sm4_cbc_sm3,
TLS_cipher_ecdhe_sm4_gcm_sm3,
TLS_cipher_ibsdh_sm4_cbc_sm3,
TLS_cipher_ibsdh_sm4_gcm_sm3,
TLS_cipher_ibc_sm4_cbc_sm3,
TLS_cipher_ibc_sm4_gcm_sm3,
TLS_cipher_rsa_sm4_cbc_sm3,
TLS_cipher_rsa_sm4_gcm_sm3,
TLS_cipher_rsa_sm4_cbc_sha256,
TLS_cipher_rsa_sm4_gcm_sha256,
};
int comp_meths[] = {0};
tls_record_set_protocol(record, TLS_protocol_tlcp);
if (tls_record_set_handshake_client_hello(record, &recordlen,
version,
random,
NULL, 0,
cipher_suites, sizeof(cipher_suites)/sizeof(cipher_suites[0]),
NULL, 0) != 1) {
error_print();
return -1;
}
tls_client_hello_print(stdout, record + 5 + 4, recordlen - 5 -4, 0, 4);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_server_hello(void)
{
uint8_t record[512];
size_t recordlen = 0;
uint8_t random[32];
uint16_t cipher_suite = TLS_cipher_ecdhe_sm4_cbc_sm3;
tls_record_set_protocol(record, TLS_protocol_tlcp);
if (tls_record_set_handshake_server_hello(record, &recordlen,
TLS_protocol_tlcp,
random,
NULL, 0,
cipher_suite,
NULL, 0) != 1) {
error_print();
return -1;
}
tls_server_hello_print(stdout, record + 5 + 4, recordlen - 5 -4, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_certificate(void)
{
uint8_t record[1024];
size_t recordlen = 0;
FILE *fp = NULL;
// 测试函数不要有外部的依赖
// TODO: 输出一些握手过程的record字节数组和handshake字节数组作为后续测试的测试数据
/*
if (!(fp = fopen("cacert.pem", "r"))) {
error_print();
return -1;
}
if (tls_record_set_handshake_certificate_from_pem(record, &recordlen, fp) != 1) {
error_print();
return -1;
}
tls_certificate_print(stdout, record + 9, recordlen - 9, 0, 0);
*/
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_server_key_exchange(void)
{
uint8_t record[1024];
size_t recordlen = 0;
uint8_t sig[SM2_MAX_SIGNATURE_SIZE] = {0xAA, 0xBB};
const uint8_t *psig;
size_t siglen;
tls_record_set_protocol(record, TLS_protocol_tlcp);
if (tlcp_record_set_handshake_server_key_exchange_pke(record, &recordlen, sig, sizeof(sig)) != 1) {
error_print();
return -1;
}
if (tlcp_record_get_handshake_server_key_exchange_pke(record, &psig, &siglen) != 1) {
error_print();
return -1;
}
format_bytes(stdout, 0, 0, "server_key_exchange siganture", psig, siglen);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_certificate_verify(void)
{
uint8_t record[1024];
size_t recordlen = 0;
uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
const uint8_t *psig;
size_t siglen;
tls_record_set_protocol(record, TLS_protocol_tls12);
if (tls_record_set_handshake_certificate_verify(record, &recordlen, sig, sizeof(sig)) != 1) {
error_print();
return -1;
}
if (tls_record_get_handshake_certificate_verify(record, &psig, &siglen) != 1) {
error_print();
return -1;
}
tls_certificate_verify_print(stdout, psig, siglen, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_finished(void)
{
uint8_t record[1024];
size_t recordlen = 0;
uint8_t verify_data[12];
const uint8_t *verify_data_ptr;
size_t verify_data_len;
if (tls_record_set_handshake_finished(record, &recordlen, verify_data, sizeof(verify_data)) != 1) {
error_print();
return -1;
}
if (tls_record_get_handshake_finished(record, &verify_data_ptr, &verify_data_len) != 1) {
error_print();
return -1;
}
tls_finished_print(stdout, verify_data_ptr, verify_data_len, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_alert(void)
{
uint8_t record[1024];
size_t recordlen = 0;
int level;
int reason;
if (tls_record_set_alert(record, &recordlen, TLS_alert_level_fatal, TLS_alert_close_notify) != 1) {
error_print();
return -1;
}
if (tls_record_get_alert(record, &level, &reason) != 1) {
error_print();
return -1;
}
tls_alert_print(stdout, record + 5, recordlen - 5, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_change_cipher_spec(void)
{
uint8_t record[1024];
size_t recordlen = 0;
if (tls_record_set_change_cipher_spec(record, &recordlen) != 1) {
error_print();
return -1;
}
if (tls_record_get_change_cipher_spec(record) != 1) {
error_print();
return -1;
}
tls_change_cipher_spec_print(stdout, record + 5, recordlen - 5, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_tls_application_data(void)
{
uint8_t record[1024];
size_t recordlen = 0;
uint8_t data[88];
const uint8_t *p;
size_t len;
if (tls_record_set_application_data(record, &recordlen, data, sizeof(data)) != 1) {
error_print();
return -1;
}
if (tls_record_get_application_data(record, &p, &len) != 1) {
error_print();
return -1;
}
tls_application_data_print(stdout, p, len, 0, 0);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_tls_encode() != 1) goto err;
if (test_tls_cbc() != 1) goto err;
if (test_tls_random() != 1) goto err;
if (test_tls_client_hello() != 1) goto err;
if (test_tls_server_hello() != 1) goto err;
if (test_tls_certificate() != 1) goto err;
if (test_tls_server_key_exchange() != 1) goto err;
if (test_tls_certificate_verify() != 1) goto err;
if (test_tls_finished() != 1) goto err;
if (test_tls_alert() != 1) goto err;
if (test_tls_change_cipher_spec() != 1) goto err;
if (test_tls_application_data() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return -1;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,192 +7,193 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509_alg.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int test_x509_digest_algor(void)
{
char *names[] = {
"sm3",
"md5",
"sha1",
"sha224",
"sha256",
"sha384",
"sha512",
};
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int oid;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_digest_algor_from_name(names[i]);
if (x509_digest_algor_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_digest_algor_from_der(&oid, &cp, &len) != 1) {
error_print();
return -1;
}
if (oid != x509_digest_algor_from_name(names[i])) {
error_print();
return 1;
}
format_print(stderr, 0, 4, "%s\n", x509_digest_algor_name(oid));
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_encryption_algor(void)
{
char *names[] = {
"sm4-cbc",
"aes128-cbc",
"aes192-cbc",
"aes256-cbc",
};
uint8_t iv[16] = {0};
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int oid;
const uint8_t *params;
size_t paramslen;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_encryption_algor_from_name(names[i]);
if (x509_encryption_algor_to_der(oid, iv, sizeof(iv), &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_encryption_algor_from_der(&oid, &params, &paramslen, &cp, &len) != 1
|| asn1_check(params != NULL) != 1
|| asn1_check(paramslen == sizeof(iv)) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_encryption_algor_name(oid));
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_signature_algor(void)
{
char *names[] = {
"sm2sign-with-sm3",
"rsasign-with-sm3",
"ecdsa-with-sha1",
"ecdsa-with-sha224",
"ecdsa-with-sha256",
"ecdsa-with-sha384",
"ecdsa-with-sha512",
"sha1WithRSAEncryption",
"sha224WithRSAEncryption",
"sha256WithRSAEncryption",
"sha384WithRSAEncryption",
"sha512WithRSAEncryption",
};
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int oid;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_signature_algor_from_name(names[i]);
if (x509_signature_algor_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_signature_algor_from_der(&oid, &cp, &len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_signature_algor_name(oid));
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_public_key_encryption_algor(void)
{
char *names[] = {
"sm2encrypt",
// "rsaesOAEP",
// "rsaEncryption",
};
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int oid;
const uint8_t *params;
size_t paramslen;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_public_key_encryption_algor_from_name(names[i]);
if (x509_public_key_encryption_algor_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_public_key_encryption_algor_from_der(&oid, &params, &paramslen, &cp, &len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_public_key_encryption_algor_name(oid));
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_x509_digest_algor() != 1) goto err;
if (test_x509_encryption_algor() != 1) goto err;
if (test_x509_signature_algor() != 1) goto err;
if (test_x509_public_key_encryption_algor() != 1) goto err;
printf("%s all tests passed!\n", __FILE__);
return 0;
err:
error_print();
return 1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509_alg.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int test_x509_digest_algor(void)
{
char *names[] = {
"sm3",
"md5",
"sha1",
"sha224",
"sha256",
"sha384",
"sha512",
};
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int oid;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_digest_algor_from_name(names[i]);
if (x509_digest_algor_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_digest_algor_from_der(&oid, &cp, &len) != 1) {
error_print();
return -1;
}
if (oid != x509_digest_algor_from_name(names[i])) {
error_print();
return 1;
}
format_print(stderr, 0, 4, "%s\n", x509_digest_algor_name(oid));
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_encryption_algor(void)
{
char *names[] = {
"sm4-cbc",
"aes128-cbc",
"aes192-cbc",
"aes256-cbc",
};
uint8_t iv[16] = {0};
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int oid;
const uint8_t *params;
size_t paramslen;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_encryption_algor_from_name(names[i]);
if (x509_encryption_algor_to_der(oid, iv, sizeof(iv), &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_encryption_algor_from_der(&oid, &params, &paramslen, &cp, &len) != 1
|| asn1_check(params != NULL) != 1
|| asn1_check(paramslen == sizeof(iv)) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_encryption_algor_name(oid));
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_signature_algor(void)
{
char *names[] = {
"sm2sign-with-sm3",
"rsasign-with-sm3",
"ecdsa-with-sha1",
"ecdsa-with-sha224",
"ecdsa-with-sha256",
"ecdsa-with-sha384",
"ecdsa-with-sha512",
"sha1WithRSAEncryption",
"sha224WithRSAEncryption",
"sha256WithRSAEncryption",
"sha384WithRSAEncryption",
"sha512WithRSAEncryption",
};
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int oid;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_signature_algor_from_name(names[i]);
if (x509_signature_algor_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_signature_algor_from_der(&oid, &cp, &len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_signature_algor_name(oid));
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_public_key_encryption_algor(void)
{
char *names[] = {
"sm2encrypt",
// "rsaesOAEP",
// "rsaEncryption",
};
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int oid;
const uint8_t *params;
size_t paramslen;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_public_key_encryption_algor_from_name(names[i]);
if (x509_public_key_encryption_algor_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_public_key_encryption_algor_from_der(&oid, &params, &paramslen, &cp, &len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_public_key_encryption_algor_name(oid));
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_x509_digest_algor() != 1) goto err;
if (test_x509_encryption_algor() != 1) goto err;
if (test_x509_signature_algor() != 1) goto err;
if (test_x509_public_key_encryption_algor() != 1) goto err;
printf("%s all tests passed!\n", __FILE__);
return 0;
err:
error_print();
return 1;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,146 +7,147 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509_alg.h>
#include <gmssl/x509_oid.h>
#include <gmssl/x509_crl.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int test_x509_crl_reason(void)
{
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int reason;
int i;
for (i = 0; i < 11; i++) {
if (x509_crl_reason_to_der(i, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
for (i = 0; i < 11; i++) {
if (x509_crl_reason_from_der(&reason, &cp, &len) != 1
|| asn1_check(reason == i) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s (%d)\n", x509_crl_reason_name(reason), reason);
}
(void)asn1_length_is_zero(len);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_crl_entry_ext(void)
{
int exts[] = {
OID_ce_crl_reasons,
OID_ce_invalidity_date,
OID_ce_certificate_issuer,
};
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int oid;
int i;
for (i = 0; i < sizeof(exts)/sizeof(exts[0]); i++) {
if (x509_crl_entry_ext_id_to_der(exts[i], &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
for (i = 0; i < sizeof(exts)/sizeof(exts[0]); i++) {
if (x509_crl_entry_ext_id_from_der(&oid, &cp, &len) != 1
|| asn1_check(oid == exts[i]) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_crl_entry_ext_id_name(oid));
}
(void)asn1_length_is_zero(len);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_crl_entry_exts(void)
{
uint8_t exts[256];
size_t extslen = 0;
int reason = X509_cr_key_compromise;
time_t tv;
uint8_t issuer[256];
size_t issuer_len = 0;
int critical = 1;
uint8_t buf[512];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
time(&tv);
if (x509_crl_entry_exts_add_reason(exts, &extslen, sizeof(exts), critical, reason) != 1
|| x509_crl_entry_exts_add_invalidity_date(exts, &extslen, sizeof(exts), critical, tv) != 1
|| x509_crl_entry_exts_add_certificate_issuer(exts, &extslen, sizeof(exts), critical, issuer, issuer_len) != 1
|| x509_crl_entry_exts_to_der(exts, extslen, &p, &len) != 1) {
error_print();
return -1;
}
x509_crl_entry_exts_print(stderr, 0, 0, "CRLEntryExtensions", exts, extslen);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_revoked_cert(void)
{
uint8_t serial[20] = { 0x01,0x02 };
time_t revoke_date;
uint8_t buf[512];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
time(&revoke_date);
if (x509_revoked_cert_to_der(serial, sizeof(serial), revoke_date, NULL, 0, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
x509_revoked_cert_print(stderr, 0, 0, "RevokedCertificate", d, dlen);
return 1;
}
int main(void)
{
if (test_x509_crl_reason() != 1) goto err;
if (test_x509_crl_entry_ext() != 1) goto err;
if (test_x509_crl_entry_exts() != 1) goto err;
if (test_x509_revoked_cert() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return 1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509_alg.h>
#include <gmssl/x509_oid.h>
#include <gmssl/x509_crl.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int test_x509_crl_reason(void)
{
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int reason;
int i;
for (i = 0; i < 11; i++) {
if (x509_crl_reason_to_der(i, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
for (i = 0; i < 11; i++) {
if (x509_crl_reason_from_der(&reason, &cp, &len) != 1
|| asn1_check(reason == i) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s (%d)\n", x509_crl_reason_name(reason), reason);
}
(void)asn1_length_is_zero(len);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_crl_entry_ext(void)
{
int exts[] = {
OID_ce_crl_reasons,
OID_ce_invalidity_date,
OID_ce_certificate_issuer,
};
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int oid;
int i;
for (i = 0; i < sizeof(exts)/sizeof(exts[0]); i++) {
if (x509_crl_entry_ext_id_to_der(exts[i], &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
for (i = 0; i < sizeof(exts)/sizeof(exts[0]); i++) {
if (x509_crl_entry_ext_id_from_der(&oid, &cp, &len) != 1
|| asn1_check(oid == exts[i]) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_crl_entry_ext_id_name(oid));
}
(void)asn1_length_is_zero(len);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_crl_entry_exts(void)
{
uint8_t exts[256];
size_t extslen = 0;
int reason = X509_cr_key_compromise;
time_t tv;
uint8_t issuer[256];
size_t issuer_len = 0;
int critical = 1;
uint8_t buf[512];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
time(&tv);
if (x509_crl_entry_exts_add_reason(exts, &extslen, sizeof(exts), critical, reason) != 1
|| x509_crl_entry_exts_add_invalidity_date(exts, &extslen, sizeof(exts), critical, tv) != 1
|| x509_crl_entry_exts_add_certificate_issuer(exts, &extslen, sizeof(exts), critical, issuer, issuer_len) != 1
|| x509_crl_entry_exts_to_der(exts, extslen, &p, &len) != 1) {
error_print();
return -1;
}
x509_crl_entry_exts_print(stderr, 0, 0, "CRLEntryExtensions", exts, extslen);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_revoked_cert(void)
{
uint8_t serial[20] = { 0x01,0x02 };
time_t revoke_date;
uint8_t buf[512];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
time(&revoke_date);
if (x509_revoked_cert_to_der(serial, sizeof(serial), revoke_date, NULL, 0, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
x509_revoked_cert_print(stderr, 0, 0, "RevokedCertificate", d, dlen);
return 1;
}
int main(void)
{
if (test_x509_crl_reason() != 1) goto err;
if (test_x509_crl_entry_ext() != 1) goto err;
if (test_x509_crl_entry_exts() != 1) goto err;
if (test_x509_revoked_cert() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return 1;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,281 +7,282 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509_oid.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int test_x509_name_type()
{
char *names[] = {
"name",
"surname",
"givenName",
"initials",
"generationQualifier",
"commonName",
"localityName",
"stateOrProvinceName",
"organizationName",
"organizationalUnitName",
"title",
"dnQualifier",
"countryName",
"serialNumber",
"pseudonym",
"domainComponent",
};
int oid;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_name_type_from_name(names[i]);
if (asn1_check(oid != OID_undef) != 1
|| x509_name_type_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_name_type_from_der(&oid, &cp, &len) != 1) {
error_print();
return -1;
}
if (oid != x509_name_type_from_name(names[i])) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_name_type_name(oid));
}
if (len != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_ext_id()
{
char *names[] = {
"AuthorityKeyIdentifier",
"SubjectKeyIdentifier",
"KeyUsage",
"CertificatePolicies",
"PolicyMappings",
"SubjectAltName",
"IssuerAltName",
"SubjectDirectoryAttributes",
"BasicConstraints",
"NameConstraints",
"PolicyConstraints",
"ExtKeyUsage",
"CRLDistributionPoints",
"InhibitAnyPolicy",
"FreshestCRL",
};
int oid;
uint32_t nodes[32];
size_t nodes_cnt;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_ext_id_from_name(names[i]);
if (asn1_check(oid != OID_undef) != 1
|| x509_ext_id_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "ExtnID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_ext_id_from_der(&oid, nodes, &nodes_cnt, &cp, &len) != 1) {
error_print();
return -1;
}
if (oid != x509_ext_id_from_name(names[i])) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_ext_id_name(oid));
}
if (len != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_qualifier_id(void)
{
char *names[] = {
"CPS",
"userNotice",
};
int oid;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_qualifier_id_from_name(names[i]);
if (asn1_check(oid != OID_undef) != 1
|| x509_qualifier_id_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_qualifier_id_from_der(&oid, &cp, &len) != 1) {
error_print();
return -1;
}
if (asn1_check(oid == x509_qualifier_id_from_name(names[i])) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_qualifier_id_name(oid));
}
if (len != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_cert_policy_id(void)
{
char *names[] = {
"anyPolicy",
};
int oid;
uint32_t nodes[32];
size_t nodes_cnt;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_cert_policy_id_from_name(names[i]);
if (asn1_check(oid != OID_undef) != 1
|| x509_cert_policy_id_to_der(oid, NULL, 0, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_cert_policy_id_from_der(&oid, nodes, &nodes_cnt, &cp, &len) != 1) {
error_print();
return -1;
}
if (oid != x509_cert_policy_id_from_name(names[i])) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_cert_policy_id_name(oid));
}
if (len != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_key_purpose(void)
{
char *names[] = {
"serverAuth",
"clientAuth",
"codeSigning",
"emailProtection",
"timeStamping",
"OCSPSigning",
};
int oid;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_key_purpose_from_name(names[i]);
if (asn1_check(oid != OID_undef) != 1
|| x509_key_purpose_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_key_purpose_from_der(&oid, &cp, &len) != 1) {
error_print();
return -1;
}
if (oid != x509_key_purpose_from_name(names[i])) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_key_purpose_name(oid));
}
if (len != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_x509_name_type() != 1) goto err;
if (test_x509_ext_id() != 1) goto err;
if (test_x509_qualifier_id() != 1) goto err;
if (test_x509_cert_policy_id() != 1) goto err;
if (test_x509_key_purpose() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return 1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509_oid.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int test_x509_name_type()
{
char *names[] = {
"name",
"surname",
"givenName",
"initials",
"generationQualifier",
"commonName",
"localityName",
"stateOrProvinceName",
"organizationName",
"organizationalUnitName",
"title",
"dnQualifier",
"countryName",
"serialNumber",
"pseudonym",
"domainComponent",
};
int oid;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_name_type_from_name(names[i]);
if (asn1_check(oid != OID_undef) != 1
|| x509_name_type_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_name_type_from_der(&oid, &cp, &len) != 1) {
error_print();
return -1;
}
if (oid != x509_name_type_from_name(names[i])) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_name_type_name(oid));
}
if (len != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_ext_id()
{
char *names[] = {
"AuthorityKeyIdentifier",
"SubjectKeyIdentifier",
"KeyUsage",
"CertificatePolicies",
"PolicyMappings",
"SubjectAltName",
"IssuerAltName",
"SubjectDirectoryAttributes",
"BasicConstraints",
"NameConstraints",
"PolicyConstraints",
"ExtKeyUsage",
"CRLDistributionPoints",
"InhibitAnyPolicy",
"FreshestCRL",
};
int oid;
uint32_t nodes[32];
size_t nodes_cnt;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_ext_id_from_name(names[i]);
if (asn1_check(oid != OID_undef) != 1
|| x509_ext_id_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "ExtnID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_ext_id_from_der(&oid, nodes, &nodes_cnt, &cp, &len) != 1) {
error_print();
return -1;
}
if (oid != x509_ext_id_from_name(names[i])) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_ext_id_name(oid));
}
if (len != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_qualifier_id(void)
{
char *names[] = {
"CPS",
"userNotice",
};
int oid;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_qualifier_id_from_name(names[i]);
if (asn1_check(oid != OID_undef) != 1
|| x509_qualifier_id_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_qualifier_id_from_der(&oid, &cp, &len) != 1) {
error_print();
return -1;
}
if (asn1_check(oid == x509_qualifier_id_from_name(names[i])) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_qualifier_id_name(oid));
}
if (len != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_cert_policy_id(void)
{
char *names[] = {
"anyPolicy",
};
int oid;
uint32_t nodes[32];
size_t nodes_cnt;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_cert_policy_id_from_name(names[i]);
if (asn1_check(oid != OID_undef) != 1
|| x509_cert_policy_id_to_der(oid, NULL, 0, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_cert_policy_id_from_der(&oid, nodes, &nodes_cnt, &cp, &len) != 1) {
error_print();
return -1;
}
if (oid != x509_cert_policy_id_from_name(names[i])) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_cert_policy_id_name(oid));
}
if (len != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_key_purpose(void)
{
char *names[] = {
"serverAuth",
"clientAuth",
"codeSigning",
"emailProtection",
"timeStamping",
"OCSPSigning",
};
int oid;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int i;
format_print(stderr, 0, 0, "DER\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
oid = x509_key_purpose_from_name(names[i]);
if (asn1_check(oid != OID_undef) != 1
|| x509_key_purpose_to_der(oid, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
format_print(stderr, 0, 0, "OID\n");
for (i = 0; i < sizeof(names)/sizeof(names[0]); i++) {
if (x509_key_purpose_from_der(&oid, &cp, &len) != 1) {
error_print();
return -1;
}
if (oid != x509_key_purpose_from_name(names[i])) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_key_purpose_name(oid));
}
if (len != 0) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_x509_name_type() != 1) goto err;
if (test_x509_ext_id() != 1) goto err;
if (test_x509_qualifier_id() != 1) goto err;
if (test_x509_cert_policy_id() != 1) goto err;
if (test_x509_key_purpose() != 1) goto err;
printf("%s all tests passed\n", __FILE__);
return 0;
err:
error_print();
return 1;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,212 +7,213 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509_alg.h>
#include <gmssl/x509_oid.h>
#include <gmssl/x509_req.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int test_x509_request_info(void)
{
uint8_t subject[256];
size_t subject_len;
SM2_KEY sm2_key;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
int version;
const uint8_t *subj;
size_t subj_len;
SM2_KEY pub_key;
const uint8_t *attrs;
size_t attrs_len;
if (sm2_key_generate(&sm2_key) != 1
|| x509_name_set(subject, &subject_len, sizeof(subject), "CN", "Beijing", "Haidian", "PKU", "CS", "CA") != 1
|| x509_request_info_to_der(X509_version_v1, subject, subject_len, &sm2_key, NULL, 0, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
x509_request_info_print(stderr, 0, 0, "CertificationRequestInfo", d, dlen);
p = buf;
cp = buf;
len = 0;
if (x509_request_info_to_der(X509_version_v1, subject, subject_len, &sm2_key, NULL, 0, &p, &len) != 1
|| x509_request_info_from_der(&version, &subj, &subj_len, &pub_key, &attrs, &attrs_len, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 0, "CertificationRequestInfo\n");
format_print(stderr, 0, 4, "version: %d\n", version);
x509_name_print(stderr, 0, 4, "subject", subj, subj_len);
sm2_public_key_print(stderr, 0, 4, "publicKey", &pub_key);
format_bytes(stderr, 0, 4, "attributes", attrs, attrs_len);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_request(void)
{
uint8_t subject[256];
size_t subject_len;
SM2_KEY sm2_key;
uint8_t signature[128] = { 0x01, 0x02 };
uint8_t buf[512];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
int version;
const uint8_t *subj;
size_t subj_len;
SM2_KEY pub_key;
const uint8_t *attrs;
size_t attrs_len;
int sig_alg;
const uint8_t *sig;
size_t siglen;
if (sm2_key_generate(&sm2_key) != 1
|| x509_name_set(subject, &subject_len, sizeof(subject), "CN", "Beijing", "Haidian", "PKU", "CS", "CA") != 1
|| x509_request_to_der(X509_version_v1, subject, subject_len, &sm2_key, NULL, 0,
OID_sm2sign_with_sm3, signature, sizeof(signature), &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
x509_request_print(stderr, 0, 0, "CertificationRequest", d, dlen);
p = buf;
cp = buf;
len = 0;
if (x509_request_to_der(X509_version_v1, subject, subject_len, &sm2_key, NULL, 0,
OID_sm2sign_with_sm3, signature, sizeof(signature), &p, &len) != 1
|| x509_request_from_der(&version, &subj, &subj_len, &pub_key, &attrs, &attrs_len,
&sig_alg, &sig, &siglen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 0, "CertificationRequest\n");
format_print(stderr, 0, 4, "version: %d\n", version);
x509_name_print(stderr, 0, 4, "subject", subj, subj_len);
sm2_public_key_print(stderr, 0, 4, "publicKey", &pub_key);
format_bytes(stderr, 0, 4, "attributes", attrs, attrs_len);
format_print(stderr, 0, 4, "signatureAlgor: %s\n", x509_signature_algor_name(sig_alg));
format_bytes(stderr, 0, 4, "signature", sig, siglen);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_req(void)
{
uint8_t subject[256];
size_t subject_len;
SM2_KEY sm2_key;
uint8_t req[512];
size_t reqlen = 0;
int version;
const uint8_t *subj;
size_t subj_len;
SM2_KEY pub_key;
const uint8_t *attrs;
size_t attrs_len;
if (sm2_key_generate(&sm2_key) != 1
|| x509_name_set(subject, &subject_len, sizeof(subject), "CN", "Beijing", "Haidian", "PKU", "CS", "CA") != 1
|| x509_req_sign(req, &reqlen, sizeof(req),
X509_version_v1, subject, subject_len, &sm2_key, NULL, 0,
OID_sm2sign_with_sm3, &sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
error_print();
return -1;
}
x509_req_print(stderr, 0, 0, "CertificationRequest", req, reqlen);
FILE *fp;
if ((fp = fopen("req.pem", "w")) == NULL) {
error_print();
return -1;
}
if (x509_req_to_pem(req, reqlen, fp) != 1) {
error_print();
return -1;
}
fclose(fp);
x509_req_to_pem(req, reqlen, stderr);
memset(req, 0, sizeof(req));
if ((fp = fopen("req.pem", "r")) == NULL) {
error_print();
return -1;
}
if (x509_req_from_pem(req, &reqlen, sizeof(req), fp) != 1) {
error_print();
return -1;
}
if (x509_req_verify(req, reqlen, &sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 0, "x509_req_verify() success\n");
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_x509_request_info() != 1) goto err;
if (test_x509_request() != 1) goto err;
if (test_x509_req() != 1) goto err;
printf("%s all tests passed!\n", __FILE__);
return 0;
err:
error_print();
return 1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509_alg.h>
#include <gmssl/x509_oid.h>
#include <gmssl/x509_req.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int test_x509_request_info(void)
{
uint8_t subject[256];
size_t subject_len;
SM2_KEY sm2_key;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
int version;
const uint8_t *subj;
size_t subj_len;
SM2_KEY pub_key;
const uint8_t *attrs;
size_t attrs_len;
if (sm2_key_generate(&sm2_key) != 1
|| x509_name_set(subject, &subject_len, sizeof(subject), "CN", "Beijing", "Haidian", "PKU", "CS", "CA") != 1
|| x509_request_info_to_der(X509_version_v1, subject, subject_len, &sm2_key, NULL, 0, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
x509_request_info_print(stderr, 0, 0, "CertificationRequestInfo", d, dlen);
p = buf;
cp = buf;
len = 0;
if (x509_request_info_to_der(X509_version_v1, subject, subject_len, &sm2_key, NULL, 0, &p, &len) != 1
|| x509_request_info_from_der(&version, &subj, &subj_len, &pub_key, &attrs, &attrs_len, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 0, "CertificationRequestInfo\n");
format_print(stderr, 0, 4, "version: %d\n", version);
x509_name_print(stderr, 0, 4, "subject", subj, subj_len);
sm2_public_key_print(stderr, 0, 4, "publicKey", &pub_key);
format_bytes(stderr, 0, 4, "attributes", attrs, attrs_len);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_request(void)
{
uint8_t subject[256];
size_t subject_len;
SM2_KEY sm2_key;
uint8_t signature[128] = { 0x01, 0x02 };
uint8_t buf[512];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
int version;
const uint8_t *subj;
size_t subj_len;
SM2_KEY pub_key;
const uint8_t *attrs;
size_t attrs_len;
int sig_alg;
const uint8_t *sig;
size_t siglen;
if (sm2_key_generate(&sm2_key) != 1
|| x509_name_set(subject, &subject_len, sizeof(subject), "CN", "Beijing", "Haidian", "PKU", "CS", "CA") != 1
|| x509_request_to_der(X509_version_v1, subject, subject_len, &sm2_key, NULL, 0,
OID_sm2sign_with_sm3, signature, sizeof(signature), &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
x509_request_print(stderr, 0, 0, "CertificationRequest", d, dlen);
p = buf;
cp = buf;
len = 0;
if (x509_request_to_der(X509_version_v1, subject, subject_len, &sm2_key, NULL, 0,
OID_sm2sign_with_sm3, signature, sizeof(signature), &p, &len) != 1
|| x509_request_from_der(&version, &subj, &subj_len, &pub_key, &attrs, &attrs_len,
&sig_alg, &sig, &siglen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 0, "CertificationRequest\n");
format_print(stderr, 0, 4, "version: %d\n", version);
x509_name_print(stderr, 0, 4, "subject", subj, subj_len);
sm2_public_key_print(stderr, 0, 4, "publicKey", &pub_key);
format_bytes(stderr, 0, 4, "attributes", attrs, attrs_len);
format_print(stderr, 0, 4, "signatureAlgor: %s\n", x509_signature_algor_name(sig_alg));
format_bytes(stderr, 0, 4, "signature", sig, siglen);
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_req(void)
{
uint8_t subject[256];
size_t subject_len;
SM2_KEY sm2_key;
uint8_t req[512];
size_t reqlen = 0;
int version;
const uint8_t *subj;
size_t subj_len;
SM2_KEY pub_key;
const uint8_t *attrs;
size_t attrs_len;
if (sm2_key_generate(&sm2_key) != 1
|| x509_name_set(subject, &subject_len, sizeof(subject), "CN", "Beijing", "Haidian", "PKU", "CS", "CA") != 1
|| x509_req_sign(req, &reqlen, sizeof(req),
X509_version_v1, subject, subject_len, &sm2_key, NULL, 0,
OID_sm2sign_with_sm3, &sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
error_print();
return -1;
}
x509_req_print(stderr, 0, 0, "CertificationRequest", req, reqlen);
FILE *fp;
if ((fp = fopen("req.pem", "w")) == NULL) {
error_print();
return -1;
}
if (x509_req_to_pem(req, reqlen, fp) != 1) {
error_print();
return -1;
}
fclose(fp);
x509_req_to_pem(req, reqlen, stderr);
memset(req, 0, sizeof(req));
if ((fp = fopen("req.pem", "r")) == NULL) {
error_print();
return -1;
}
if (x509_req_from_pem(req, &reqlen, sizeof(req), fp) != 1) {
error_print();
return -1;
}
if (x509_req_verify(req, reqlen, &sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 0, "x509_req_verify() success\n");
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_x509_request_info() != 1) goto err;
if (test_x509_request() != 1) goto err;
if (test_x509_req() != 1) goto err;
printf("%s all tests passed!\n", __FILE__);
return 0;
err:
error_print();
return 1;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,73 +7,74 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509_str.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int test_x509_directory_name(void)
{
uint8_t str[] = { 'a', 'b', 'c', 0 };
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int tag;
const uint8_t *d;
size_t dlen;
if (x509_directory_name_check_ex(ASN1_TAG_UTF8String, str, 3, 1, 10) != 1 // str,4 will fail
|| x509_directory_name_to_der(ASN1_TAG_UTF8String, str, 3, &p, &len) != 1
|| x509_directory_name_from_der(&tag, &d, &dlen, &cp, &len) != 1
|| asn1_check(tag == ASN1_TAG_UTF8String) != 1
|| asn1_check(dlen == 3) != 1
|| asn1_check(memcmp(str, d, dlen) == 0) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_display_text(void)
{
uint8_t str[] = { 'a', 'b', 'c', 0 };
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int tag;
const uint8_t *d;
size_t dlen;
if (x509_display_text_check(ASN1_TAG_UTF8String, str, 3) != 1 // str,4 will fail
|| x509_display_text_to_der(ASN1_TAG_UTF8String, str, 3, &p, &len) != 1
|| x509_display_text_from_der(&tag, &d, &dlen, &cp, &len) != 1
|| asn1_check(tag == ASN1_TAG_UTF8String) != 1
|| asn1_check(dlen == 3) != 1
|| asn1_check(memcmp(str, d, dlen) == 0) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_x509_directory_name() != 1) goto err;
if (test_x509_display_text() != 1) goto err;
printf("%s all tests passed!\n", __FILE__);
return 0;
err:
error_print();
return 1;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509_str.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int test_x509_directory_name(void)
{
uint8_t str[] = { 'a', 'b', 'c', 0 };
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int tag;
const uint8_t *d;
size_t dlen;
if (x509_directory_name_check_ex(ASN1_TAG_UTF8String, str, 3, 1, 10) != 1 // str,4 will fail
|| x509_directory_name_to_der(ASN1_TAG_UTF8String, str, 3, &p, &len) != 1
|| x509_directory_name_from_der(&tag, &d, &dlen, &cp, &len) != 1
|| asn1_check(tag == ASN1_TAG_UTF8String) != 1
|| asn1_check(dlen == 3) != 1
|| asn1_check(memcmp(str, d, dlen) == 0) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
static int test_x509_display_text(void)
{
uint8_t str[] = { 'a', 'b', 'c', 0 };
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
int tag;
const uint8_t *d;
size_t dlen;
if (x509_display_text_check(ASN1_TAG_UTF8String, str, 3) != 1 // str,4 will fail
|| x509_display_text_to_der(ASN1_TAG_UTF8String, str, 3, &p, &len) != 1
|| x509_display_text_from_der(&tag, &d, &dlen, &cp, &len) != 1
|| asn1_check(tag == ASN1_TAG_UTF8String) != 1
|| asn1_check(dlen == 3) != 1
|| asn1_check(memcmp(str, d, dlen) == 0) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
printf("%s() ok\n", __FUNCTION__);
return 1;
}
int main(void)
{
if (test_x509_directory_name() != 1) goto err;
if (test_x509_display_text() != 1) goto err;
printf("%s all tests passed!\n", __FILE__);
return 0;
err:
error_print();
return 1;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,393 +7,394 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509_alg.h>
#include <gmssl/x509_oid.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int test_x509_version(void)
{
int tests[] = {
X509_version_v1,
X509_version_v2,
X509_version_v3,
-1,
};
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
size_t i;
format_print(stderr, 0, 0, "Version\n");
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
if (x509_explicit_version_to_der(i, tests[i], &p, &len) < 0) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
int ver;
if (x509_explicit_version_from_der(i, &ver, &cp, &len) < 0
|| asn1_check(ver == tests[i]) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_version_name(ver));
}
(void)asn1_length_is_zero(len);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_x509_validity(void)
{
time_t not_before, not_before_;
time_t not_after, not_after_;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
size_t i;
time(&not_before);
format_print(stderr, 0, 0, "Validity\n");
if (x509_validity_add_days(&not_after, not_before, 365) != 1
|| x509_validity_to_der(not_before, not_after, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
if (x509_validity_from_der(&not_before_, &not_after_, &cp, &len) != 1
|| asn1_check(not_before == not_before_) != 1
|| asn1_check(not_after == not_after_) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return 1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_x509_attr_type_and_value(void)
{
int oid;
int tag;
const uint8_t *d;
size_t dlen;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
format_print(stderr, 0, 0, "AttributeTypeAndValue\n");
if (x509_attr_type_and_value_to_der(OID_at_locality_name, ASN1_TAG_PrintableString, (uint8_t *)"Haidian", strlen("Haidian"), &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
if (x509_attr_type_and_value_from_der(&oid, &tag, &d, &dlen, &cp, &len) != 1
|| asn1_check(oid == OID_at_locality_name) != 1
|| asn1_check(tag == ASN1_TAG_PrintableString) != 1
|| asn1_check(dlen == strlen("Haidian")) != 1
|| asn1_check(memcmp("Haidian", d, dlen) == 0) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s : %s ", x509_name_type_name(oid), asn1_tag_name(tag));
format_string(stderr, 0, 0, "", d, dlen);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_x509_rdn(void)
{
int oid;
int tag;
const uint8_t *d;
size_t dlen;
const uint8_t *more;
size_t morelen;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
format_print(stderr, 0, 0, "RDN\n");
if (x509_rdn_to_der(OID_at_locality_name, ASN1_TAG_PrintableString,
(uint8_t *)"Haidian", strlen("Haidian"), NULL, 0, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
if (x509_rdn_from_der(&oid, &tag, &d, &dlen, &more, &morelen, &cp, &len) != 1
|| asn1_check(oid == OID_at_locality_name) != 1
|| asn1_check(tag == ASN1_TAG_PrintableString) != 1
|| asn1_check(dlen == strlen("Haidian")) != 1
|| asn1_check(memcmp("Haidian", d, dlen) == 0) != 1
|| asn1_check(more == NULL) != 1
|| asn1_check(morelen == 0) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s : %s ", x509_name_type_name(oid), asn1_tag_name(tag));
format_string(stderr, 0, 0, "", d, dlen);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_x509_name(void)
{
int err = 0;
uint8_t name[512];
size_t namelen = 0;
uint8_t buf[1024];
const uint8_t *cp = buf;
uint8_t *p = buf;
size_t len = 0;
if (x509_name_add_country_name(name, &namelen, sizeof(name), "CN") != 1
|| format_bytes(stderr, 0, 4, "", name, namelen) > 2
|| x509_name_add_locality_name(name, &namelen, sizeof(name), ASN1_TAG_PrintableString, (uint8_t *)"Haidian", strlen("Haidian")) != 1
|| format_bytes(stderr, 0, 4, "", name, namelen) > 2
|| x509_name_add_state_or_province_name(name, &namelen, sizeof(name), ASN1_TAG_PrintableString, (uint8_t *)"Beijing", strlen("Beijing")) != 1
|| format_bytes(stderr, 0, 4, "", name, namelen) > 2
|| x509_name_add_organization_name(name, &namelen, sizeof(name), ASN1_TAG_PrintableString, (uint8_t *)"PKU", strlen("PKU")) != 1
|| format_bytes(stderr, 0, 4, "", name, namelen) > 2
|| x509_name_add_organizational_unit_name(name, &namelen, sizeof(name), ASN1_TAG_PrintableString, (uint8_t *)"CS", strlen("CS")) != 1
|| format_bytes(stderr, 0, 4, "", name, namelen) > 2
|| x509_name_add_common_name(name, &namelen, sizeof(name), ASN1_TAG_PrintableString, (uint8_t *)"CA", strlen("CA")) != 1
|| format_bytes(stderr, 0, 4, "", name, namelen) > 2
) {
error_print();
return 1;
}
format_bytes(stdout, 0, 0, "der ", name, namelen);
x509_name_print(stdout, 0, 0, "Name", name, namelen);
return 0;
}
static int test_x509_public_key_info(void)
{
int err = 0;
SM2_KEY sm2_key;
SM2_KEY pub_key;
uint8_t buf[256];
const uint8_t *cp = buf;
uint8_t *p = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
if (sm2_key_generate(&sm2_key) != 1
|| x509_public_key_info_to_der(&sm2_key, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return 1;
}
x509_public_key_info_print(stdout, 0, 0, "PublicKeyInfo", d, dlen);
if (sm2_key_generate(&sm2_key) != 1
|| x509_public_key_info_to_der(&sm2_key, &p, &len) != 1
|| x509_public_key_info_from_der(&pub_key, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return 1;
}
sm2_public_key_print(stdout, 0, 8, "ECPublicKey", &pub_key);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int set_x509_name(uint8_t *name, size_t *namelen, size_t maxlen)
{
*namelen = 0;
if (x509_name_add_country_name(name, namelen, maxlen, "CN") != 1
|| x509_name_add_locality_name(name, namelen, maxlen, ASN1_TAG_PrintableString, (uint8_t *)"Haidian", strlen("Haidian")) != 1
|| x509_name_add_state_or_province_name(name, namelen, maxlen, ASN1_TAG_PrintableString, (uint8_t *)"Beijing", strlen("Beijing")) != 1
|| x509_name_add_organization_name(name, namelen, maxlen, ASN1_TAG_PrintableString, (uint8_t *)"PKU", strlen("PKU")) != 1
|| x509_name_add_organizational_unit_name(name, namelen, maxlen, ASN1_TAG_PrintableString, (uint8_t *)"CS", strlen("CS")) != 1
|| x509_name_add_common_name(name, namelen, maxlen, ASN1_TAG_PrintableString, (uint8_t *)"CA", strlen("CA")) != 1) {
error_print();
return -1;
}
return 1;
}
static int test_x509_tbs_cert(void)
{
uint8_t serial[20] = { 0x01, 0x00 };
size_t serial_len;
uint8_t issuer[256];
size_t issuer_len = 0;
time_t not_before, not_after;
uint8_t subject[256];
size_t subject_len = 0;
SM2_KEY sm2_key;
uint8_t buf[1024] = {0};
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
set_x509_name(issuer, &issuer_len, sizeof(issuer));
time(&not_before);
x509_validity_add_days(&not_after, not_before, 365);
set_x509_name(subject, &subject_len, sizeof(subject));
sm2_key_generate(&sm2_key);
if (x509_tbs_cert_to_der(
X509_version_v3,
serial, sizeof(serial),
OID_sm2sign_with_sm3,
issuer, issuer_len,
not_before, not_after,
subject, subject_len,
&sm2_key,
NULL, 0,
NULL, 0,
NULL, 0,
&p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 0, "tbs_cert", buf, len);
if (asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
x509_tbs_cert_print(stderr, 0, 4, "TBSCertificate", d, dlen);
return 0;
}
static int test_x509_cert_get(const uint8_t *cert, size_t certlen)
{
const uint8_t *serial;
size_t serial_len;
const uint8_t *issuer;
size_t issuer_len;
const uint8_t *subject;
size_t subject_len;
SM2_KEY public_key;
if (x509_cert_get_issuer_and_serial_number(cert, certlen, &issuer, &issuer_len, &serial, &serial_len) != 1
|| x509_cert_get_subject(cert, certlen, &subject, &subject_len) != 1
|| x509_cert_get_subject_public_key(cert, certlen, &public_key) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "SerialNumber", serial, serial_len);
x509_name_print(stderr, 0, 4, "Issuer", issuer, issuer_len);
x509_name_print(stderr, 0, 4, "Subject", subject, subject_len);
sm2_public_key_print(stderr, 0, 4, "SubjectPublicKey", &public_key);
return 0;
}
static int test_x509_cert(void)
{
uint8_t serial[20] = { 0x01, 0x00 };
size_t serial_len;
uint8_t issuer[256];
size_t issuer_len = 0;
time_t not_before, not_after;
uint8_t subject[256];
size_t subject_len = 0;
SM2_KEY sm2_key;
uint8_t cert[1024] = {0};
uint8_t *p = cert;
const uint8_t *cp = cert;
size_t certlen = 0;
set_x509_name(issuer, &issuer_len, sizeof(issuer));
time(&not_before);
x509_validity_add_days(&not_after, not_before, 365);
set_x509_name(subject, &subject_len, sizeof(subject));
sm2_key_generate(&sm2_key);
if (x509_cert_sign(
cert, &certlen, sizeof(cert),
X509_version_v3,
serial, sizeof(serial),
OID_sm2sign_with_sm3,
issuer, issuer_len,
not_before, not_after,
subject, subject_len,
&sm2_key,
NULL, 0,
NULL, 0,
NULL, 0,
&sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "cert", cert, certlen);
x509_cert_print(stderr, 0, 4, "Certificate", cert, certlen);
if (x509_cert_verify(cert, certlen, &sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
error_print();
return -1;
}
printf("x509_cert_verify() success\n");
test_x509_cert_get(cert, certlen);
FILE *fp;
if (!(fp = fopen("cert.pem", "w"))) {
error_print();
return -1;
}
x509_cert_to_pem(cert, certlen, fp);
x509_cert_to_pem(cert, certlen, stderr);
fclose(fp);
if (!(fp = fopen("cert.pem", "r"))) {
error_print();
return -1;
}
memset(cert, 0, sizeof(cert));
if (x509_cert_from_pem(cert, &certlen, sizeof(cert), fp) != 1) {
error_print();
return -1;
}
x509_cert_print(stderr, 0, 4, "Certificate", cert, certlen);
return 0;
}
int main(void)
{
int err = 0;
err += test_x509_version();
err += test_x509_validity();
err += test_x509_attr_type_and_value();
err += test_x509_rdn();
err += test_x509_name();
err += test_x509_public_key_info();
err += test_x509_tbs_cert();
err += test_x509_cert();
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/oid.h>
#include <gmssl/x509_alg.h>
#include <gmssl/x509_oid.h>
#include <gmssl/x509.h>
#include <gmssl/rand.h>
#include <gmssl/error.h>
static int test_x509_version(void)
{
int tests[] = {
X509_version_v1,
X509_version_v2,
X509_version_v3,
-1,
};
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
size_t i;
format_print(stderr, 0, 0, "Version\n");
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
if (x509_explicit_version_to_der(i, tests[i], &p, &len) < 0) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
}
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
int ver;
if (x509_explicit_version_from_der(i, &ver, &cp, &len) < 0
|| asn1_check(ver == tests[i]) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s\n", x509_version_name(ver));
}
(void)asn1_length_is_zero(len);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_x509_validity(void)
{
time_t not_before, not_before_;
time_t not_after, not_after_;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
size_t i;
time(&not_before);
format_print(stderr, 0, 0, "Validity\n");
if (x509_validity_add_days(&not_after, not_before, 365) != 1
|| x509_validity_to_der(not_before, not_after, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
if (x509_validity_from_der(&not_before_, &not_after_, &cp, &len) != 1
|| asn1_check(not_before == not_before_) != 1
|| asn1_check(not_after == not_after_) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return 1;
}
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_x509_attr_type_and_value(void)
{
int oid;
int tag;
const uint8_t *d;
size_t dlen;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
format_print(stderr, 0, 0, "AttributeTypeAndValue\n");
if (x509_attr_type_and_value_to_der(OID_at_locality_name, ASN1_TAG_PrintableString, (uint8_t *)"Haidian", strlen("Haidian"), &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
if (x509_attr_type_and_value_from_der(&oid, &tag, &d, &dlen, &cp, &len) != 1
|| asn1_check(oid == OID_at_locality_name) != 1
|| asn1_check(tag == ASN1_TAG_PrintableString) != 1
|| asn1_check(dlen == strlen("Haidian")) != 1
|| asn1_check(memcmp("Haidian", d, dlen) == 0) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s : %s ", x509_name_type_name(oid), asn1_tag_name(tag));
format_string(stderr, 0, 0, "", d, dlen);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_x509_rdn(void)
{
int oid;
int tag;
const uint8_t *d;
size_t dlen;
const uint8_t *more;
size_t morelen;
uint8_t buf[256];
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
format_print(stderr, 0, 0, "RDN\n");
if (x509_rdn_to_der(OID_at_locality_name, ASN1_TAG_PrintableString,
(uint8_t *)"Haidian", strlen("Haidian"), NULL, 0, &p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "", buf, len);
if (x509_rdn_from_der(&oid, &tag, &d, &dlen, &more, &morelen, &cp, &len) != 1
|| asn1_check(oid == OID_at_locality_name) != 1
|| asn1_check(tag == ASN1_TAG_PrintableString) != 1
|| asn1_check(dlen == strlen("Haidian")) != 1
|| asn1_check(memcmp("Haidian", d, dlen) == 0) != 1
|| asn1_check(more == NULL) != 1
|| asn1_check(morelen == 0) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
format_print(stderr, 0, 4, "%s : %s ", x509_name_type_name(oid), asn1_tag_name(tag));
format_string(stderr, 0, 0, "", d, dlen);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int test_x509_name(void)
{
int err = 0;
uint8_t name[512];
size_t namelen = 0;
uint8_t buf[1024];
const uint8_t *cp = buf;
uint8_t *p = buf;
size_t len = 0;
if (x509_name_add_country_name(name, &namelen, sizeof(name), "CN") != 1
|| format_bytes(stderr, 0, 4, "", name, namelen) > 2
|| x509_name_add_locality_name(name, &namelen, sizeof(name), ASN1_TAG_PrintableString, (uint8_t *)"Haidian", strlen("Haidian")) != 1
|| format_bytes(stderr, 0, 4, "", name, namelen) > 2
|| x509_name_add_state_or_province_name(name, &namelen, sizeof(name), ASN1_TAG_PrintableString, (uint8_t *)"Beijing", strlen("Beijing")) != 1
|| format_bytes(stderr, 0, 4, "", name, namelen) > 2
|| x509_name_add_organization_name(name, &namelen, sizeof(name), ASN1_TAG_PrintableString, (uint8_t *)"PKU", strlen("PKU")) != 1
|| format_bytes(stderr, 0, 4, "", name, namelen) > 2
|| x509_name_add_organizational_unit_name(name, &namelen, sizeof(name), ASN1_TAG_PrintableString, (uint8_t *)"CS", strlen("CS")) != 1
|| format_bytes(stderr, 0, 4, "", name, namelen) > 2
|| x509_name_add_common_name(name, &namelen, sizeof(name), ASN1_TAG_PrintableString, (uint8_t *)"CA", strlen("CA")) != 1
|| format_bytes(stderr, 0, 4, "", name, namelen) > 2
) {
error_print();
return 1;
}
format_bytes(stdout, 0, 0, "der ", name, namelen);
x509_name_print(stdout, 0, 0, "Name", name, namelen);
return 0;
}
static int test_x509_public_key_info(void)
{
int err = 0;
SM2_KEY sm2_key;
SM2_KEY pub_key;
uint8_t buf[256];
const uint8_t *cp = buf;
uint8_t *p = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
if (sm2_key_generate(&sm2_key) != 1
|| x509_public_key_info_to_der(&sm2_key, &p, &len) != 1
|| asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return 1;
}
x509_public_key_info_print(stdout, 0, 0, "PublicKeyInfo", d, dlen);
if (sm2_key_generate(&sm2_key) != 1
|| x509_public_key_info_to_der(&sm2_key, &p, &len) != 1
|| x509_public_key_info_from_der(&pub_key, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return 1;
}
sm2_public_key_print(stdout, 0, 8, "ECPublicKey", &pub_key);
printf("%s() ok\n", __FUNCTION__);
return 0;
}
static int set_x509_name(uint8_t *name, size_t *namelen, size_t maxlen)
{
*namelen = 0;
if (x509_name_add_country_name(name, namelen, maxlen, "CN") != 1
|| x509_name_add_locality_name(name, namelen, maxlen, ASN1_TAG_PrintableString, (uint8_t *)"Haidian", strlen("Haidian")) != 1
|| x509_name_add_state_or_province_name(name, namelen, maxlen, ASN1_TAG_PrintableString, (uint8_t *)"Beijing", strlen("Beijing")) != 1
|| x509_name_add_organization_name(name, namelen, maxlen, ASN1_TAG_PrintableString, (uint8_t *)"PKU", strlen("PKU")) != 1
|| x509_name_add_organizational_unit_name(name, namelen, maxlen, ASN1_TAG_PrintableString, (uint8_t *)"CS", strlen("CS")) != 1
|| x509_name_add_common_name(name, namelen, maxlen, ASN1_TAG_PrintableString, (uint8_t *)"CA", strlen("CA")) != 1) {
error_print();
return -1;
}
return 1;
}
static int test_x509_tbs_cert(void)
{
uint8_t serial[20] = { 0x01, 0x00 };
size_t serial_len;
uint8_t issuer[256];
size_t issuer_len = 0;
time_t not_before, not_after;
uint8_t subject[256];
size_t subject_len = 0;
SM2_KEY sm2_key;
uint8_t buf[1024] = {0};
uint8_t *p = buf;
const uint8_t *cp = buf;
size_t len = 0;
const uint8_t *d;
size_t dlen;
set_x509_name(issuer, &issuer_len, sizeof(issuer));
time(&not_before);
x509_validity_add_days(&not_after, not_before, 365);
set_x509_name(subject, &subject_len, sizeof(subject));
sm2_key_generate(&sm2_key);
if (x509_tbs_cert_to_der(
X509_version_v3,
serial, sizeof(serial),
OID_sm2sign_with_sm3,
issuer, issuer_len,
not_before, not_after,
subject, subject_len,
&sm2_key,
NULL, 0,
NULL, 0,
NULL, 0,
&p, &len) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 0, "tbs_cert", buf, len);
if (asn1_sequence_from_der(&d, &dlen, &cp, &len) != 1
|| asn1_length_is_zero(len) != 1) {
error_print();
return -1;
}
x509_tbs_cert_print(stderr, 0, 4, "TBSCertificate", d, dlen);
return 0;
}
static int test_x509_cert_get(const uint8_t *cert, size_t certlen)
{
const uint8_t *serial;
size_t serial_len;
const uint8_t *issuer;
size_t issuer_len;
const uint8_t *subject;
size_t subject_len;
SM2_KEY public_key;
if (x509_cert_get_issuer_and_serial_number(cert, certlen, &issuer, &issuer_len, &serial, &serial_len) != 1
|| x509_cert_get_subject(cert, certlen, &subject, &subject_len) != 1
|| x509_cert_get_subject_public_key(cert, certlen, &public_key) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "SerialNumber", serial, serial_len);
x509_name_print(stderr, 0, 4, "Issuer", issuer, issuer_len);
x509_name_print(stderr, 0, 4, "Subject", subject, subject_len);
sm2_public_key_print(stderr, 0, 4, "SubjectPublicKey", &public_key);
return 0;
}
static int test_x509_cert(void)
{
uint8_t serial[20] = { 0x01, 0x00 };
size_t serial_len;
uint8_t issuer[256];
size_t issuer_len = 0;
time_t not_before, not_after;
uint8_t subject[256];
size_t subject_len = 0;
SM2_KEY sm2_key;
uint8_t cert[1024] = {0};
uint8_t *p = cert;
const uint8_t *cp = cert;
size_t certlen = 0;
set_x509_name(issuer, &issuer_len, sizeof(issuer));
time(&not_before);
x509_validity_add_days(&not_after, not_before, 365);
set_x509_name(subject, &subject_len, sizeof(subject));
sm2_key_generate(&sm2_key);
if (x509_cert_sign(
cert, &certlen, sizeof(cert),
X509_version_v3,
serial, sizeof(serial),
OID_sm2sign_with_sm3,
issuer, issuer_len,
not_before, not_after,
subject, subject_len,
&sm2_key,
NULL, 0,
NULL, 0,
NULL, 0,
&sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
error_print();
return -1;
}
format_bytes(stderr, 0, 4, "cert", cert, certlen);
x509_cert_print(stderr, 0, 4, "Certificate", cert, certlen);
if (x509_cert_verify(cert, certlen, &sm2_key, SM2_DEFAULT_ID, strlen(SM2_DEFAULT_ID)) != 1) {
error_print();
return -1;
}
printf("x509_cert_verify() success\n");
test_x509_cert_get(cert, certlen);
FILE *fp;
if (!(fp = fopen("cert.pem", "w"))) {
error_print();
return -1;
}
x509_cert_to_pem(cert, certlen, fp);
x509_cert_to_pem(cert, certlen, stderr);
fclose(fp);
if (!(fp = fopen("cert.pem", "r"))) {
error_print();
return -1;
}
memset(cert, 0, sizeof(cert));
if (x509_cert_from_pem(cert, &certlen, sizeof(cert), fp) != 1) {
error_print();
return -1;
}
x509_cert_print(stderr, 0, 4, "Certificate", cert, certlen);
return 0;
}
int main(void)
{
int err = 0;
err += test_x509_version();
err += test_x509_validity();
err += test_x509_attr_type_and_value();
err += test_x509_rdn();
err += test_x509_name();
err += test_x509_public_key_info();
err += test_x509_tbs_cert();
err += test_x509_cert();
return err;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2022 The GmSSL Project. All Rights Reserved.
* Copyright 2014-2022 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.
@@ -7,477 +7,478 @@
* http://www.apache.org/licenses/LICENSE-2.0
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/zuc.h>
static void bswap_buf(uint32_t *buf, size_t nwords)
{
size_t i;
for (i = 0; i < nwords; i++) {
uint32_t a = buf[i];
buf[i] = (a >> 24) | ((a >> 8) & 0xff00) |
((a << 8) & 0xff0000) | (a << 24);
}
}
int zuc_test(void)
{
int err = 0;
int i;
unsigned char key[][16] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
{0x3d,0x4c,0x4b,0xe9,0x6a,0x82,0xfd,0xae,0xb5,0x8f,0x64,0x1d,0xb1,0x7b,0x45,0x5b},
};
unsigned char iv[][16] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
{0x84,0x31,0x9a,0xa8,0xde,0x69,0x15,0xca,0x1f,0x6b,0xda,0x6b,0xfb,0xd8,0xc7,0x66},
};
uint32_t ciphertext[][2] = {
{0x27bede74, 0x018082da},
{0x0657cfa0, 0x7096398b},
{0x14f1c272, 0x3279c419},
};
for (i = 0; i < 3; i++) {
ZUC_STATE zuc = {{0}};
uint32_t buf[3] = {0};
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);
err++;
} else {
fprintf(stderr, "zuc test %d ok\n", i);
}
}
return err;
}
/* test vector from GM/T 0001.2-2012 */
static int zuc_eea_test(void)
{
int err = 0;
unsigned char key[][16] = {
{0x17, 0x3d, 0x14, 0xba, 0x50, 0x03, 0x73, 0x1d,
0x7a, 0x60, 0x04, 0x94, 0x70, 0xf0, 0x0a, 0x29},
{0xe5, 0xbd, 0x3e, 0xa0, 0xeb, 0x55, 0xad, 0xe8,
0x66, 0xc6, 0xac, 0x58, 0xbd, 0x54, 0x30, 0x2a},
{0xe1, 0x3f, 0xed, 0x21, 0xb4, 0x6e, 0x4e, 0x7e,
0xc3, 0x12, 0x53, 0xb2, 0xbb, 0x17, 0xb3, 0xe0},
};
ZUC_UINT32 count[] = {0x66035492, 0x56823, 0x2738cdaa};
ZUC_UINT5 bearer[] = {0x0f, 0x18, 0x1a};
ZUC_BIT direction[] = {0, 1, 0};
ZUC_UINT32 ibs0[] = {
0x6cf65340, 0x735552ab, 0x0c9752fa, 0x6f9025fe,
0x0bd675d9, 0x005875b2, 0x00000000,
};
ZUC_UINT32 ibs1[] = {
0x14a8ef69, 0x3d678507, 0xbbe7270a, 0x7f67ff50,
0x06c3525b, 0x9807e467, 0xc4e56000, 0xba338f5d,
0x42955903, 0x67518222, 0x46c80d3b, 0x38f07f4b,
0xe2d8ff58, 0x05f51322, 0x29bde93b, 0xbbdcaf38,
0x2bf1ee97, 0x2fbf9977, 0xbada8945, 0x847a2a6c,
0x9ad34a66, 0x7554e04d, 0x1f7fa2c3, 0x3241bd8f,
0x01ba220d,
};
ZUC_UINT32 ibs2[] = {
0x8d74e20d, 0x54894e06, 0xd3cb13cb, 0x3933065e,
0x8674be62, 0xadb1c72b, 0x3a646965, 0xab63cb7b,
0x7854dfdc, 0x27e84929, 0xf49c64b8, 0x72a490b1,
0x3f957b64, 0x827e71f4, 0x1fbd4269, 0xa42c97f8,
0x24537027, 0xf86e9f4a, 0xd82d1df4, 0x51690fdd,
0x98b6d03f, 0x3a0ebe3a, 0x312d6b84, 0x0ba5a182,
0x0b2a2c97, 0x09c090d2, 0x45ed267c, 0xf845ae41,
0xfa975d33, 0x33ac3009, 0xfd40eba9, 0xeb5b8857,
0x14b768b6, 0x97138baf, 0x21380eca, 0x49f644d4,
0x8689e421, 0x5760b906, 0x739f0d2b, 0x3f091133,
0xca15d981, 0xcbe401ba, 0xf72d05ac, 0xe05cccb2,
0xd297f4ef, 0x6a5f58d9, 0x1246cfa7, 0x7215b892,
0xab441d52, 0x78452795, 0xccb7f5d7, 0x9057a1c4,
0xf77f80d4, 0x6db2033c, 0xb79bedf8, 0xe60551ce,
0x10c667f6, 0x2a97abaf, 0xabbcd677, 0x2018df96,
0xa282ea73, 0x7ce2cb33, 0x1211f60d, 0x5354ce78,
0xf9918d9c, 0x206ca042, 0xc9b62387, 0xdd709604,
0xa50af16d, 0x8d35a890, 0x6be484cf, 0x2e74a928,
0x99403643, 0x53249b27, 0xb4c9ae29, 0xeddfc7da,
0x6418791a, 0x4e7baa06, 0x60fa6451, 0x1f2d685c,
0xc3a5ff70, 0xe0d2b742, 0x92e3b8a0, 0xcd6b04b1,
0xc790b8ea, 0xd2703708, 0x540dea2f, 0xc09c3da7,
0x70f65449, 0xc84d817a, 0x4f551055, 0xe19ab850,
0x18a0028b, 0x71a144d9, 0x6791e9a3, 0x57793350,
0x4eee0060, 0x340c69d2, 0x74e1bf9d, 0x805dcbcc,
0x1a6faa97, 0x6800b6ff, 0x2b671dc4, 0x63652fa8,
0xa33ee509, 0x74c1c21b, 0xe01eabb2, 0x16743026,
0x9d72ee51, 0x1c9dde30, 0x797c9a25, 0xd86ce74f,
0x5b961be5, 0xfdfb6807, 0x814039e7, 0x137636bd,
0x1d7fa9e0, 0x9efd2007, 0x505906a5, 0xac45dfde,
0xed7757bb, 0xee745749, 0xc2963335, 0x0bee0ea6,
0xf409df45, 0x80160000,
};
ZUC_UINT32 obs0[] = {
0xa6c85fc6, 0x6afb8533, 0xaafc2518, 0xdfe78494,
0x0ee1e4b0, 0x30238cc8, 0x00000000,
};
ZUC_UINT32 obs1[] = {
0x131d43e0, 0xdea1be5c, 0x5a1bfd97, 0x1d852cbf,
0x712d7b4f, 0x57961fea, 0x3208afa8, 0xbca433f4,
0x56ad09c7, 0x417e58bc, 0x69cf8866, 0xd1353f74,
0x865e8078, 0x1d202dfb, 0x3ecff7fc, 0xbc3b190f,
0xe82a204e, 0xd0e350fc, 0x0f6f2613, 0xb2f2bca6,
0xdf5a473a, 0x57a4a00d, 0x985ebad8, 0x80d6f238,
0x64a07b01,
};
ZUC_UINT32 obs2[] = {
0x94eaa4aa, 0x30a57137, 0xddf09b97, 0xb25618a2,
0x0a13e2f1, 0x0fa5bf81, 0x61a879cc, 0x2ae797a6,
0xb4cf2d9d, 0xf31debb9, 0x905ccfec, 0x97de605d,
0x21c61ab8, 0x531b7f3c, 0x9da5f039, 0x31f8a064,
0x2de48211, 0xf5f52ffe, 0xa10f392a, 0x04766998,
0x5da454a2, 0x8f080961, 0xa6c2b62d, 0xaa17f33c,
0xd60a4971, 0xf48d2d90, 0x9394a55f, 0x48117ace,
0x43d708e6, 0xb77d3dc4, 0x6d8bc017, 0xd4d1abb7,
0x7b7428c0, 0x42b06f2f, 0x99d8d07c, 0x9879d996,
0x00127a31, 0x985f1099, 0xbbd7d6c1, 0x519ede8f,
0x5eeb4a61, 0x0b349ac0, 0x1ea23506, 0x91756bd1,
0x05c974a5, 0x3eddb35d, 0x1d4100b0, 0x12e522ab,
0x41f4c5f2, 0xfde76b59, 0xcb8b96d8, 0x85cfe408,
0x0d1328a0, 0xd636cc0e, 0xdc05800b, 0x76acca8f,
0xef672084, 0xd1f52a8b, 0xbd8e0993, 0x320992c7,
0xffbae17c, 0x408441e0, 0xee883fc8, 0xa8b05e22,
0xf5ff7f8d, 0x1b48c74c, 0x468c467a, 0x028f09fd,
0x7ce91109, 0xa570a2d5, 0xc4d5f4fa, 0x18c5dd3e,
0x4562afe2, 0x4ef77190, 0x1f59af64, 0x5898acef,
0x088abae0, 0x7e92d52e, 0xb2de5504, 0x5bb1b7c4,
0x164ef2d7, 0xa6cac15e, 0xeb926d7e, 0xa2f08b66,
0xe1f759f3, 0xaee44614, 0x725aa3c7, 0x482b3084,
0x4c143ff8, 0x7b53f1e5, 0x83c50125, 0x7dddd096,
0xb81268da, 0xa303f172, 0x34c23335, 0x41f0bb8e,
0x190648c5, 0x807c866d, 0x71932286, 0x09adb948,
0x686f7de2, 0x94a802cc, 0x38f7fe52, 0x08f5ea31,
0x96d0167b, 0x9bdd02f0, 0xd2a5221c, 0xa508f893,
0xaf5c4b4b, 0xb9f4f520, 0xfd84289b, 0x3dbe7e61,
0x497a7e2a, 0x584037ea, 0x637b6981, 0x127174af,
0x57b471df, 0x4b2768fd, 0x79c1540f, 0xb3edf2ea,
0x22cb69be, 0xc0cf8d93, 0x3d9c6fdd, 0x645e8505,
0x91cca3d6, 0x2c0cc000,
};
ZUC_UINT32 *ibs[] = {ibs0, ibs1, ibs2};
ZUC_UINT32 *obs[] = {obs0, obs1, obs2};
size_t bits[] = {0xc1, 0x320, 0xfb3};
ZUC_UINT32 buf[sizeof(obs2)/4];
size_t i;
for (i = 0; i < sizeof(key)/sizeof(key[i]); i++) {
zuc_eea_encrypt(ibs[i], buf, bits[i], key[i], count[i], bearer[i], direction[i]);
if (memcmp(buf, obs[i], (bits[i] + 31)/32) != 0) {
printf("zuc eea test %zu failed\n", i);
err++;
} else {
printf("zuc eea test %zu ok\n", i);
}
}
return err;
}
/* test vector from GM/T 0001.3-2012 */
static int zuc_eia_test(void)
{
int err = 0;
unsigned char key[][16] = {
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0xc9, 0xe6, 0xce, 0xc4, 0x60, 0x7c, 0x72, 0xdb,
0x00, 0x0a, 0xef, 0xa8, 0x83, 0x85, 0xab, 0x0a},
{0x6b, 0x8b, 0x08, 0xee, 0x79, 0xe0, 0xb5, 0x98,
0x2d, 0x6d, 0x12, 0x8e, 0xa9, 0xf2, 0x20, 0xcb},
};
ZUC_UINT32 count[] = {0, 0xa94059daU, 0x561eb2ddU};
ZUC_UINT5 bearer[] = {0, 0x0a, 0x1c};
ZUC_BIT direction[] = {0, 1, 0};
ZUC_UINT32 mesg0[] = {0};
ZUC_UINT32 mesg1[] = {
0x983b41d4, 0x7d780c9e, 0x1ad11d7e, 0xb70391b1,
0xde0b35da, 0x2dc62f83, 0xe7b78d63, 0x06ca0ea0,
0x7e941b7b, 0xe91348f9, 0xfcb170e2, 0x217fecd9,
0x7f9f68ad, 0xb16e5d7d, 0x21e569d2, 0x80ed775c,
0xebde3f40, 0x93c53881, 0x00000000,
};
ZUC_UINT32 mesg2[] = {
0x5bad7247, 0x10ba1c56, 0xd5a315f8, 0xd40f6e09,
0x3780be8e, 0x8de07b69, 0x92432018, 0xe08ed96a,
0x5734af8b, 0xad8a575d, 0x3a1f162f, 0x85045cc7,
0x70925571, 0xd9f5b94e, 0x454a77c1, 0x6e72936b,
0xf016ae15, 0x7499f054, 0x3b5d52ca, 0xa6dbeab6,
0x97d2bb73, 0xe41b8075, 0xdce79b4b, 0x86044f66,
0x1d4485a5, 0x43dd7860, 0x6e0419e8, 0x059859d3,
0xcb2b67ce, 0x0977603f, 0x81ff839e, 0x33185954,
0x4cfbc8d0, 0x0fef1a4c, 0x8510fb54, 0x7d6b06c6,
0x11ef44f1, 0xbce107cf, 0xa45a06aa, 0xb360152b,
0x28dc1ebe, 0x6f7fe09b, 0x0516f9a5, 0xb02a1bd8,
0x4bb0181e, 0x2e89e19b, 0xd8125930, 0xd178682f,
0x3862dc51, 0xb636f04e, 0x720c47c3, 0xce51ad70,
0xd94b9b22, 0x55fbae90, 0x6549f499, 0xf8c6d399,
0x47ed5e5d, 0xf8e2def1, 0x13253e7b, 0x08d0a76b,
0x6bfc68c8, 0x12f375c7, 0x9b8fe5fd, 0x85976aa6,
0xd46b4a23, 0x39d8ae51, 0x47f680fb, 0xe70f978b,
0x38effd7b, 0x2f7866a2, 0x2554e193, 0xa94e98a6,
0x8b74bd25, 0xbb2b3f5f, 0xb0a5fd59, 0x887f9ab6,
0x8159b717, 0x8d5b7b67, 0x7cb546bf, 0x41eadca2,
0x16fc1085, 0x0128f8bd, 0xef5c8d89, 0xf96afa4f,
0xa8b54885, 0x565ed838, 0xa950fee5, 0xf1c3b0a4,
0xf6fb71e5, 0x4dfd169e, 0x82cecc72, 0x66c850e6,
0x7c5ef0ba, 0x960f5214, 0x060e71eb, 0x172a75fc,
0x1486835c, 0xbea65344, 0x65b055c9, 0x6a72e410,
0x52241823, 0x25d83041, 0x4b40214d, 0xaa8091d2,
0xe0fb010a, 0xe15c6de9, 0x0850973b, 0xdf1e423b,
0xe148a237, 0xb87a0c9f, 0x34d4b476, 0x05b803d7,
0x43a86a90, 0x399a4af3, 0x96d3a120, 0x0a62f3d9,
0x507962e8, 0xe5bee6d3, 0xda2bb3f7, 0x237664ac,
0x7a292823, 0x900bc635, 0x03b29e80, 0xd63f6067,
0xbf8e1716, 0xac25beba, 0x350deb62, 0xa99fe031,
0x85eb4f69, 0x937ecd38, 0x7941fda5, 0x44ba67db,
0x09117749, 0x38b01827, 0xbcc69c92, 0xb3f772a9,
0xd2859ef0, 0x03398b1f, 0x6bbad7b5, 0x74f7989a,
0x1d10b2df, 0x798e0dbf, 0x30d65874, 0x64d24878,
0xcd00c0ea, 0xee8a1a0c, 0xc753a279, 0x79e11b41,
0xdb1de3d5, 0x038afaf4, 0x9f5c682c, 0x3748d8a3,
0xa9ec54e6, 0xa371275f, 0x1683510f, 0x8e4f9093,
0x8f9ab6e1, 0x34c2cfdf, 0x4841cba8, 0x8e0cff2b,
0x0bcc8e6a, 0xdcb71109, 0xb5198fec, 0xf1bb7e5c,
0x531aca50, 0xa56a8a3b, 0x6de59862, 0xd41fa113,
0xd9cd9578, 0x08f08571, 0xd9a4bb79, 0x2af271f6,
0xcc6dbb8d, 0xc7ec36e3, 0x6be1ed30, 0x8164c31c,
0x7c0afc54, 0x1c000000,
};
ZUC_UINT32 *mesg[] = {mesg0, mesg1, mesg2};
size_t bits[] = {1, 0x241, 0x1626};
ZUC_UINT32 mac[] = {0xc8a9595eU, 0xfae8ff0bU, 0x0ca12792U};
size_t i;
bswap_buf(mesg0, sizeof(mesg0)/sizeof(mesg0[0]));
bswap_buf(mesg1, sizeof(mesg1)/sizeof(mesg1[0]));
bswap_buf(mesg2, sizeof(mesg2)/sizeof(mesg2[0]));
for (i = 0; i < sizeof(key)/sizeof(key[0]); i++) {
ZUC_UINT32 T;
T = zuc_eia_generate_mac(mesg[i], bits[i], key[i],
count[i], bearer[i], direction[i]);
if (T != mac[i]) {
printf("zuc eia test %zu failed\n", i);
err++;
} else {
printf("zuc eia test %zu ok\n", i);
}
}
return err;
}
/* from ZUC256 draft */
int zuc256_test(void)
{
int err = 0;
int i;
unsigned char key[][32] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}
};
unsigned char iv[][23] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff},
};
uint32_t ciphertext[][20] = {
{0x58d03ad6,0x2e032ce2,0xdafc683a,0x39bdcb03,0x52a2bc67,
0xf1b7de74,0x163ce3a1,0x01ef5558,0x9639d75b,0x95fa681b,
0x7f090df7,0x56391ccc,0x903b7612,0x744d544c,0x17bc3fad,
0x8b163b08,0x21787c0b,0x97775bb8,0x4943c6bb,0xe8ad8afd},
{0x3356cbae,0xd1a1c18b,0x6baa4ffe,0x343f777c,0x9e15128f,
0x251ab65b,0x949f7b26,0xef7157f2,0x96dd2fa9,0xdf95e3ee,
0x7a5be02e,0xc32ba585,0x505af316,0xc2f9ded2,0x7cdbd935,
0xe441ce11,0x15fd0a80,0xbb7aef67,0x68989416,0xb8fac8c2}
};
for (i = 0; i < sizeof(key)/sizeof(key[0]); i++) {
ZUC_STATE zuc_key;
uint32_t buf[20] = {0};
zuc256_init(&zuc_key, key[i], iv[i]);
zuc_generate_keystream(&zuc_key, 20, buf);
if (memcmp(buf, ciphertext[i], 20) != 0) {
printf("zuc256 test %d failed\n", i);
err++;
} else {
printf("zuc256 test %d ok\n", i);
}
}
return err;
}
int zuc256_mac_test(void)
{
int err = 0;
unsigned char key[][32] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}
};
unsigned char iv[][23] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff},
};
unsigned char msg[][50] = {
/* 400 zero bits */
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
/* 4000 bits */
{0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
/* 400 zero bits */
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
/* 4000 bits */
{0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
};
unsigned int msg_num[] = {
1,
10,
1,
10
};
unsigned int tag32[][1] = {
{0x9b972a74},
{0x8754f5cf},
{0x1f3079b4},
{0x5c7c8b88},
};
unsigned int tag64[][2] = {
{0x673e5499,0x0034d38c},
{0x130dc225,0xe72240cc},
{0x8c71394d,0x39957725},
{0xea1dee54,0x4bb6223b},
};
unsigned int tag128[][4] = {
{0xd85e54bb,0xcb960096,0x7084c952,0xa1654b26},
{0xdf1e8307,0xb31cc62b,0xeca1ac6f,0x8190c22f},
{0xa35bb274,0xb567c48b,0x28319f11,0x1af34fbd},
{0x3a83b554,0xbe408ca5,0x494124ed,0x9d473205},
};
int i, j;
bswap_buf((uint32_t *)tag32, sizeof(tag32)/4);
bswap_buf((uint32_t *)tag64, sizeof(tag64)/4);
bswap_buf((uint32_t *)tag128, sizeof(tag128)/4);
for (i = 0; i < 4; i++) {
ZUC256_MAC_CTX ctx;
unsigned char mac[16];
zuc256_mac_init(&ctx, key[i], iv[i], 32);
for (j = 0; j < msg_num[i]; j++) {
zuc256_mac_update(&ctx, msg[i], 50);
}
zuc256_mac_finish(&ctx, NULL, 0, mac);
if (memcmp(mac, tag32[i], 4) != 0) {
printf("zuc256 mac test %d 32-bit failed\n", i);
err++;
} else {
printf("zuc256 mac test %d 32-bit ok\n", i);
}
zuc256_mac_init(&ctx, key[i], iv[i], 64);
for (j = 0; j < msg_num[i]; j++) {
zuc256_mac_update(&ctx, msg[i], 50);
}
zuc256_mac_finish(&ctx, NULL, 0, mac);
if (memcmp(mac, tag64[i], 8) != 0) {
printf("zuc256 mac test %d 64-bit failed\n", i);
err++;
} else {
printf("zuc256 mac test %d 64-bit ok\n", i);
}
zuc256_mac_init(&ctx, key[i], iv[i], 128);
for (j = 0; j < msg_num[i]; j++) {
zuc256_mac_update(&ctx, msg[i], 50);
}
zuc256_mac_finish(&ctx, NULL, 0, mac);
if (memcmp(mac, tag128[i], 16) != 0) {
printf("zuc256 mac test %d 128-bit failed\n", i);
err++;
} else {
printf("zuc256 mac test %d 128-bit ok\n", i);
}
}
return err;
}
int main(void)
{
int err = 0;
err += zuc_test();
err += zuc_eea_test();
err += zuc_eia_test();
err += zuc256_test();
err += zuc256_mac_test();
return err;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/zuc.h>
static void bswap_buf(uint32_t *buf, size_t nwords)
{
size_t i;
for (i = 0; i < nwords; i++) {
uint32_t a = buf[i];
buf[i] = (a >> 24) | ((a >> 8) & 0xff00) |
((a << 8) & 0xff0000) | (a << 24);
}
}
int zuc_test(void)
{
int err = 0;
int i;
unsigned char key[][16] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
{0x3d,0x4c,0x4b,0xe9,0x6a,0x82,0xfd,0xae,0xb5,0x8f,0x64,0x1d,0xb1,0x7b,0x45,0x5b},
};
unsigned char iv[][16] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
{0x84,0x31,0x9a,0xa8,0xde,0x69,0x15,0xca,0x1f,0x6b,0xda,0x6b,0xfb,0xd8,0xc7,0x66},
};
uint32_t ciphertext[][2] = {
{0x27bede74, 0x018082da},
{0x0657cfa0, 0x7096398b},
{0x14f1c272, 0x3279c419},
};
for (i = 0; i < 3; i++) {
ZUC_STATE zuc = {{0}};
uint32_t buf[3] = {0};
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);
err++;
} else {
fprintf(stderr, "zuc test %d ok\n", i);
}
}
return err;
}
/* test vector from GM/T 0001.2-2012 */
static int zuc_eea_test(void)
{
int err = 0;
unsigned char key[][16] = {
{0x17, 0x3d, 0x14, 0xba, 0x50, 0x03, 0x73, 0x1d,
0x7a, 0x60, 0x04, 0x94, 0x70, 0xf0, 0x0a, 0x29},
{0xe5, 0xbd, 0x3e, 0xa0, 0xeb, 0x55, 0xad, 0xe8,
0x66, 0xc6, 0xac, 0x58, 0xbd, 0x54, 0x30, 0x2a},
{0xe1, 0x3f, 0xed, 0x21, 0xb4, 0x6e, 0x4e, 0x7e,
0xc3, 0x12, 0x53, 0xb2, 0xbb, 0x17, 0xb3, 0xe0},
};
ZUC_UINT32 count[] = {0x66035492, 0x56823, 0x2738cdaa};
ZUC_UINT5 bearer[] = {0x0f, 0x18, 0x1a};
ZUC_BIT direction[] = {0, 1, 0};
ZUC_UINT32 ibs0[] = {
0x6cf65340, 0x735552ab, 0x0c9752fa, 0x6f9025fe,
0x0bd675d9, 0x005875b2, 0x00000000,
};
ZUC_UINT32 ibs1[] = {
0x14a8ef69, 0x3d678507, 0xbbe7270a, 0x7f67ff50,
0x06c3525b, 0x9807e467, 0xc4e56000, 0xba338f5d,
0x42955903, 0x67518222, 0x46c80d3b, 0x38f07f4b,
0xe2d8ff58, 0x05f51322, 0x29bde93b, 0xbbdcaf38,
0x2bf1ee97, 0x2fbf9977, 0xbada8945, 0x847a2a6c,
0x9ad34a66, 0x7554e04d, 0x1f7fa2c3, 0x3241bd8f,
0x01ba220d,
};
ZUC_UINT32 ibs2[] = {
0x8d74e20d, 0x54894e06, 0xd3cb13cb, 0x3933065e,
0x8674be62, 0xadb1c72b, 0x3a646965, 0xab63cb7b,
0x7854dfdc, 0x27e84929, 0xf49c64b8, 0x72a490b1,
0x3f957b64, 0x827e71f4, 0x1fbd4269, 0xa42c97f8,
0x24537027, 0xf86e9f4a, 0xd82d1df4, 0x51690fdd,
0x98b6d03f, 0x3a0ebe3a, 0x312d6b84, 0x0ba5a182,
0x0b2a2c97, 0x09c090d2, 0x45ed267c, 0xf845ae41,
0xfa975d33, 0x33ac3009, 0xfd40eba9, 0xeb5b8857,
0x14b768b6, 0x97138baf, 0x21380eca, 0x49f644d4,
0x8689e421, 0x5760b906, 0x739f0d2b, 0x3f091133,
0xca15d981, 0xcbe401ba, 0xf72d05ac, 0xe05cccb2,
0xd297f4ef, 0x6a5f58d9, 0x1246cfa7, 0x7215b892,
0xab441d52, 0x78452795, 0xccb7f5d7, 0x9057a1c4,
0xf77f80d4, 0x6db2033c, 0xb79bedf8, 0xe60551ce,
0x10c667f6, 0x2a97abaf, 0xabbcd677, 0x2018df96,
0xa282ea73, 0x7ce2cb33, 0x1211f60d, 0x5354ce78,
0xf9918d9c, 0x206ca042, 0xc9b62387, 0xdd709604,
0xa50af16d, 0x8d35a890, 0x6be484cf, 0x2e74a928,
0x99403643, 0x53249b27, 0xb4c9ae29, 0xeddfc7da,
0x6418791a, 0x4e7baa06, 0x60fa6451, 0x1f2d685c,
0xc3a5ff70, 0xe0d2b742, 0x92e3b8a0, 0xcd6b04b1,
0xc790b8ea, 0xd2703708, 0x540dea2f, 0xc09c3da7,
0x70f65449, 0xc84d817a, 0x4f551055, 0xe19ab850,
0x18a0028b, 0x71a144d9, 0x6791e9a3, 0x57793350,
0x4eee0060, 0x340c69d2, 0x74e1bf9d, 0x805dcbcc,
0x1a6faa97, 0x6800b6ff, 0x2b671dc4, 0x63652fa8,
0xa33ee509, 0x74c1c21b, 0xe01eabb2, 0x16743026,
0x9d72ee51, 0x1c9dde30, 0x797c9a25, 0xd86ce74f,
0x5b961be5, 0xfdfb6807, 0x814039e7, 0x137636bd,
0x1d7fa9e0, 0x9efd2007, 0x505906a5, 0xac45dfde,
0xed7757bb, 0xee745749, 0xc2963335, 0x0bee0ea6,
0xf409df45, 0x80160000,
};
ZUC_UINT32 obs0[] = {
0xa6c85fc6, 0x6afb8533, 0xaafc2518, 0xdfe78494,
0x0ee1e4b0, 0x30238cc8, 0x00000000,
};
ZUC_UINT32 obs1[] = {
0x131d43e0, 0xdea1be5c, 0x5a1bfd97, 0x1d852cbf,
0x712d7b4f, 0x57961fea, 0x3208afa8, 0xbca433f4,
0x56ad09c7, 0x417e58bc, 0x69cf8866, 0xd1353f74,
0x865e8078, 0x1d202dfb, 0x3ecff7fc, 0xbc3b190f,
0xe82a204e, 0xd0e350fc, 0x0f6f2613, 0xb2f2bca6,
0xdf5a473a, 0x57a4a00d, 0x985ebad8, 0x80d6f238,
0x64a07b01,
};
ZUC_UINT32 obs2[] = {
0x94eaa4aa, 0x30a57137, 0xddf09b97, 0xb25618a2,
0x0a13e2f1, 0x0fa5bf81, 0x61a879cc, 0x2ae797a6,
0xb4cf2d9d, 0xf31debb9, 0x905ccfec, 0x97de605d,
0x21c61ab8, 0x531b7f3c, 0x9da5f039, 0x31f8a064,
0x2de48211, 0xf5f52ffe, 0xa10f392a, 0x04766998,
0x5da454a2, 0x8f080961, 0xa6c2b62d, 0xaa17f33c,
0xd60a4971, 0xf48d2d90, 0x9394a55f, 0x48117ace,
0x43d708e6, 0xb77d3dc4, 0x6d8bc017, 0xd4d1abb7,
0x7b7428c0, 0x42b06f2f, 0x99d8d07c, 0x9879d996,
0x00127a31, 0x985f1099, 0xbbd7d6c1, 0x519ede8f,
0x5eeb4a61, 0x0b349ac0, 0x1ea23506, 0x91756bd1,
0x05c974a5, 0x3eddb35d, 0x1d4100b0, 0x12e522ab,
0x41f4c5f2, 0xfde76b59, 0xcb8b96d8, 0x85cfe408,
0x0d1328a0, 0xd636cc0e, 0xdc05800b, 0x76acca8f,
0xef672084, 0xd1f52a8b, 0xbd8e0993, 0x320992c7,
0xffbae17c, 0x408441e0, 0xee883fc8, 0xa8b05e22,
0xf5ff7f8d, 0x1b48c74c, 0x468c467a, 0x028f09fd,
0x7ce91109, 0xa570a2d5, 0xc4d5f4fa, 0x18c5dd3e,
0x4562afe2, 0x4ef77190, 0x1f59af64, 0x5898acef,
0x088abae0, 0x7e92d52e, 0xb2de5504, 0x5bb1b7c4,
0x164ef2d7, 0xa6cac15e, 0xeb926d7e, 0xa2f08b66,
0xe1f759f3, 0xaee44614, 0x725aa3c7, 0x482b3084,
0x4c143ff8, 0x7b53f1e5, 0x83c50125, 0x7dddd096,
0xb81268da, 0xa303f172, 0x34c23335, 0x41f0bb8e,
0x190648c5, 0x807c866d, 0x71932286, 0x09adb948,
0x686f7de2, 0x94a802cc, 0x38f7fe52, 0x08f5ea31,
0x96d0167b, 0x9bdd02f0, 0xd2a5221c, 0xa508f893,
0xaf5c4b4b, 0xb9f4f520, 0xfd84289b, 0x3dbe7e61,
0x497a7e2a, 0x584037ea, 0x637b6981, 0x127174af,
0x57b471df, 0x4b2768fd, 0x79c1540f, 0xb3edf2ea,
0x22cb69be, 0xc0cf8d93, 0x3d9c6fdd, 0x645e8505,
0x91cca3d6, 0x2c0cc000,
};
ZUC_UINT32 *ibs[] = {ibs0, ibs1, ibs2};
ZUC_UINT32 *obs[] = {obs0, obs1, obs2};
size_t bits[] = {0xc1, 0x320, 0xfb3};
ZUC_UINT32 buf[sizeof(obs2)/4];
size_t i;
for (i = 0; i < sizeof(key)/sizeof(key[i]); i++) {
zuc_eea_encrypt(ibs[i], buf, bits[i], key[i], count[i], bearer[i], direction[i]);
if (memcmp(buf, obs[i], (bits[i] + 31)/32) != 0) {
printf("zuc eea test %zu failed\n", i);
err++;
} else {
printf("zuc eea test %zu ok\n", i);
}
}
return err;
}
/* test vector from GM/T 0001.3-2012 */
static int zuc_eia_test(void)
{
int err = 0;
unsigned char key[][16] = {
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
{0xc9, 0xe6, 0xce, 0xc4, 0x60, 0x7c, 0x72, 0xdb,
0x00, 0x0a, 0xef, 0xa8, 0x83, 0x85, 0xab, 0x0a},
{0x6b, 0x8b, 0x08, 0xee, 0x79, 0xe0, 0xb5, 0x98,
0x2d, 0x6d, 0x12, 0x8e, 0xa9, 0xf2, 0x20, 0xcb},
};
ZUC_UINT32 count[] = {0, 0xa94059daU, 0x561eb2ddU};
ZUC_UINT5 bearer[] = {0, 0x0a, 0x1c};
ZUC_BIT direction[] = {0, 1, 0};
ZUC_UINT32 mesg0[] = {0};
ZUC_UINT32 mesg1[] = {
0x983b41d4, 0x7d780c9e, 0x1ad11d7e, 0xb70391b1,
0xde0b35da, 0x2dc62f83, 0xe7b78d63, 0x06ca0ea0,
0x7e941b7b, 0xe91348f9, 0xfcb170e2, 0x217fecd9,
0x7f9f68ad, 0xb16e5d7d, 0x21e569d2, 0x80ed775c,
0xebde3f40, 0x93c53881, 0x00000000,
};
ZUC_UINT32 mesg2[] = {
0x5bad7247, 0x10ba1c56, 0xd5a315f8, 0xd40f6e09,
0x3780be8e, 0x8de07b69, 0x92432018, 0xe08ed96a,
0x5734af8b, 0xad8a575d, 0x3a1f162f, 0x85045cc7,
0x70925571, 0xd9f5b94e, 0x454a77c1, 0x6e72936b,
0xf016ae15, 0x7499f054, 0x3b5d52ca, 0xa6dbeab6,
0x97d2bb73, 0xe41b8075, 0xdce79b4b, 0x86044f66,
0x1d4485a5, 0x43dd7860, 0x6e0419e8, 0x059859d3,
0xcb2b67ce, 0x0977603f, 0x81ff839e, 0x33185954,
0x4cfbc8d0, 0x0fef1a4c, 0x8510fb54, 0x7d6b06c6,
0x11ef44f1, 0xbce107cf, 0xa45a06aa, 0xb360152b,
0x28dc1ebe, 0x6f7fe09b, 0x0516f9a5, 0xb02a1bd8,
0x4bb0181e, 0x2e89e19b, 0xd8125930, 0xd178682f,
0x3862dc51, 0xb636f04e, 0x720c47c3, 0xce51ad70,
0xd94b9b22, 0x55fbae90, 0x6549f499, 0xf8c6d399,
0x47ed5e5d, 0xf8e2def1, 0x13253e7b, 0x08d0a76b,
0x6bfc68c8, 0x12f375c7, 0x9b8fe5fd, 0x85976aa6,
0xd46b4a23, 0x39d8ae51, 0x47f680fb, 0xe70f978b,
0x38effd7b, 0x2f7866a2, 0x2554e193, 0xa94e98a6,
0x8b74bd25, 0xbb2b3f5f, 0xb0a5fd59, 0x887f9ab6,
0x8159b717, 0x8d5b7b67, 0x7cb546bf, 0x41eadca2,
0x16fc1085, 0x0128f8bd, 0xef5c8d89, 0xf96afa4f,
0xa8b54885, 0x565ed838, 0xa950fee5, 0xf1c3b0a4,
0xf6fb71e5, 0x4dfd169e, 0x82cecc72, 0x66c850e6,
0x7c5ef0ba, 0x960f5214, 0x060e71eb, 0x172a75fc,
0x1486835c, 0xbea65344, 0x65b055c9, 0x6a72e410,
0x52241823, 0x25d83041, 0x4b40214d, 0xaa8091d2,
0xe0fb010a, 0xe15c6de9, 0x0850973b, 0xdf1e423b,
0xe148a237, 0xb87a0c9f, 0x34d4b476, 0x05b803d7,
0x43a86a90, 0x399a4af3, 0x96d3a120, 0x0a62f3d9,
0x507962e8, 0xe5bee6d3, 0xda2bb3f7, 0x237664ac,
0x7a292823, 0x900bc635, 0x03b29e80, 0xd63f6067,
0xbf8e1716, 0xac25beba, 0x350deb62, 0xa99fe031,
0x85eb4f69, 0x937ecd38, 0x7941fda5, 0x44ba67db,
0x09117749, 0x38b01827, 0xbcc69c92, 0xb3f772a9,
0xd2859ef0, 0x03398b1f, 0x6bbad7b5, 0x74f7989a,
0x1d10b2df, 0x798e0dbf, 0x30d65874, 0x64d24878,
0xcd00c0ea, 0xee8a1a0c, 0xc753a279, 0x79e11b41,
0xdb1de3d5, 0x038afaf4, 0x9f5c682c, 0x3748d8a3,
0xa9ec54e6, 0xa371275f, 0x1683510f, 0x8e4f9093,
0x8f9ab6e1, 0x34c2cfdf, 0x4841cba8, 0x8e0cff2b,
0x0bcc8e6a, 0xdcb71109, 0xb5198fec, 0xf1bb7e5c,
0x531aca50, 0xa56a8a3b, 0x6de59862, 0xd41fa113,
0xd9cd9578, 0x08f08571, 0xd9a4bb79, 0x2af271f6,
0xcc6dbb8d, 0xc7ec36e3, 0x6be1ed30, 0x8164c31c,
0x7c0afc54, 0x1c000000,
};
ZUC_UINT32 *mesg[] = {mesg0, mesg1, mesg2};
size_t bits[] = {1, 0x241, 0x1626};
ZUC_UINT32 mac[] = {0xc8a9595eU, 0xfae8ff0bU, 0x0ca12792U};
size_t i;
bswap_buf(mesg0, sizeof(mesg0)/sizeof(mesg0[0]));
bswap_buf(mesg1, sizeof(mesg1)/sizeof(mesg1[0]));
bswap_buf(mesg2, sizeof(mesg2)/sizeof(mesg2[0]));
for (i = 0; i < sizeof(key)/sizeof(key[0]); i++) {
ZUC_UINT32 T;
T = zuc_eia_generate_mac(mesg[i], bits[i], key[i],
count[i], bearer[i], direction[i]);
if (T != mac[i]) {
printf("zuc eia test %zu failed\n", i);
err++;
} else {
printf("zuc eia test %zu ok\n", i);
}
}
return err;
}
/* from ZUC256 draft */
int zuc256_test(void)
{
int err = 0;
int i;
unsigned char key[][32] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}
};
unsigned char iv[][23] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff},
};
uint32_t ciphertext[][20] = {
{0x58d03ad6,0x2e032ce2,0xdafc683a,0x39bdcb03,0x52a2bc67,
0xf1b7de74,0x163ce3a1,0x01ef5558,0x9639d75b,0x95fa681b,
0x7f090df7,0x56391ccc,0x903b7612,0x744d544c,0x17bc3fad,
0x8b163b08,0x21787c0b,0x97775bb8,0x4943c6bb,0xe8ad8afd},
{0x3356cbae,0xd1a1c18b,0x6baa4ffe,0x343f777c,0x9e15128f,
0x251ab65b,0x949f7b26,0xef7157f2,0x96dd2fa9,0xdf95e3ee,
0x7a5be02e,0xc32ba585,0x505af316,0xc2f9ded2,0x7cdbd935,
0xe441ce11,0x15fd0a80,0xbb7aef67,0x68989416,0xb8fac8c2}
};
for (i = 0; i < sizeof(key)/sizeof(key[0]); i++) {
ZUC_STATE zuc_key;
uint32_t buf[20] = {0};
zuc256_init(&zuc_key, key[i], iv[i]);
zuc_generate_keystream(&zuc_key, 20, buf);
if (memcmp(buf, ciphertext[i], 20) != 0) {
printf("zuc256 test %d failed\n", i);
err++;
} else {
printf("zuc256 test %d ok\n", i);
}
}
return err;
}
int zuc256_mac_test(void)
{
int err = 0;
unsigned char key[][32] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}
};
unsigned char iv[][23] = {
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff},
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff},
};
unsigned char msg[][50] = {
/* 400 zero bits */
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
/* 4000 bits */
{0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
/* 400 zero bits */
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
/* 4000 bits */
{0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
};
unsigned int msg_num[] = {
1,
10,
1,
10
};
unsigned int tag32[][1] = {
{0x9b972a74},
{0x8754f5cf},
{0x1f3079b4},
{0x5c7c8b88},
};
unsigned int tag64[][2] = {
{0x673e5499,0x0034d38c},
{0x130dc225,0xe72240cc},
{0x8c71394d,0x39957725},
{0xea1dee54,0x4bb6223b},
};
unsigned int tag128[][4] = {
{0xd85e54bb,0xcb960096,0x7084c952,0xa1654b26},
{0xdf1e8307,0xb31cc62b,0xeca1ac6f,0x8190c22f},
{0xa35bb274,0xb567c48b,0x28319f11,0x1af34fbd},
{0x3a83b554,0xbe408ca5,0x494124ed,0x9d473205},
};
int i, j;
bswap_buf((uint32_t *)tag32, sizeof(tag32)/4);
bswap_buf((uint32_t *)tag64, sizeof(tag64)/4);
bswap_buf((uint32_t *)tag128, sizeof(tag128)/4);
for (i = 0; i < 4; i++) {
ZUC256_MAC_CTX ctx;
unsigned char mac[16];
zuc256_mac_init(&ctx, key[i], iv[i], 32);
for (j = 0; j < msg_num[i]; j++) {
zuc256_mac_update(&ctx, msg[i], 50);
}
zuc256_mac_finish(&ctx, NULL, 0, mac);
if (memcmp(mac, tag32[i], 4) != 0) {
printf("zuc256 mac test %d 32-bit failed\n", i);
err++;
} else {
printf("zuc256 mac test %d 32-bit ok\n", i);
}
zuc256_mac_init(&ctx, key[i], iv[i], 64);
for (j = 0; j < msg_num[i]; j++) {
zuc256_mac_update(&ctx, msg[i], 50);
}
zuc256_mac_finish(&ctx, NULL, 0, mac);
if (memcmp(mac, tag64[i], 8) != 0) {
printf("zuc256 mac test %d 64-bit failed\n", i);
err++;
} else {
printf("zuc256 mac test %d 64-bit ok\n", i);
}
zuc256_mac_init(&ctx, key[i], iv[i], 128);
for (j = 0; j < msg_num[i]; j++) {
zuc256_mac_update(&ctx, msg[i], 50);
}
zuc256_mac_finish(&ctx, NULL, 0, mac);
if (memcmp(mac, tag128[i], 16) != 0) {
printf("zuc256 mac test %d 128-bit failed\n", i);
err++;
} else {
printf("zuc256 mac test %d 128-bit ok\n", i);
}
}
return err;
}
int main(void)
{
int err = 0;
err += zuc_test();
err += zuc_eea_test();
err += zuc_eia_test();
err += zuc256_test();
err += zuc256_mac_test();
return err;
}