mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-11-14 08:35:04 +08:00
Init commit of gmssl-v3
This commit is contained in:
187
tests/aestest.c
Normal file
187
tests/aestest.c
Normal file
@@ -0,0 +1,187 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/aes.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int err = 0;
|
||||
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");
|
||||
err++;
|
||||
} 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");
|
||||
err++;
|
||||
} 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");
|
||||
err++;
|
||||
} 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");
|
||||
err++;
|
||||
} 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");
|
||||
err++;
|
||||
} else {
|
||||
printf("ok\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
568
tests/asn1test.c
Normal file
568
tests/asn1test.c
Normal file
@@ -0,0 +1,568 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <gmssl/asn1.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
#define BOOL_1 0
|
||||
#define BOOL_2 1
|
||||
|
||||
#define INT_1 "\x00"
|
||||
#define INT_2 "\x7f"
|
||||
#define INT_3 "\x80"
|
||||
#define INT_4 "\xff\xf0"
|
||||
|
||||
#define BITS_1 "\xff\xf0"
|
||||
#define BITS_1_LEN 12
|
||||
|
||||
#define OCTETS_1 "\x12\x34\x45\x56"
|
||||
|
||||
static void print_buf(const uint8_t *a, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < len; i++) {
|
||||
printf("%02x ", a[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void print_integer(const uint8_t *a, size_t alen)
|
||||
{
|
||||
size_t i;
|
||||
printf("integer = ");
|
||||
for (i = 0; i < alen; i++) {
|
||||
printf("%02x", a[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void print_bits(const uint8_t *bits, size_t nbits)
|
||||
{
|
||||
size_t i;
|
||||
printf("bits (%zu) = ", nbits);
|
||||
for (i = 0; i < (nbits + 7)/8; i++) {
|
||||
printf("%02x", bits[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void print_octets(const uint8_t *o, size_t olen)
|
||||
{
|
||||
size_t i;
|
||||
printf("octets (%zu) = ", olen);
|
||||
for (i = 0; i < olen; i++) {
|
||||
printf("%02x", o[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static int test_asn1_tag(void)
|
||||
{
|
||||
int i = 0;
|
||||
for (i = 0; i < 32; i++) {
|
||||
printf("%s\n", asn1_tag_name(i));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_asn1_length(void)
|
||||
{
|
||||
int err = 0;
|
||||
size_t tests[] = {5, 127, 128, 256, 65537, 1<<23, (size_t)1<<31, };
|
||||
size_t val;
|
||||
uint8_t buf[1024] = {0};
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
size_t left = ((size_t)1 << 32);
|
||||
size_t i;
|
||||
int rv;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
printf("%zu ", tests[i]);
|
||||
}
|
||||
printf("\n");
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
asn1_length_to_der(tests[i], &p, &len);
|
||||
print_buf(buf, len);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
rv = asn1_length_from_der(&val, &cp, &left);
|
||||
assert(rv > 0);
|
||||
if (val != tests[i]) {
|
||||
error_print("error decoding %zu-th length: get %zu, should be %zu", i, val, tests[i]);
|
||||
err++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_asn1_boolean(void)
|
||||
{
|
||||
int err = 0;
|
||||
int tests[] = {0, 1};
|
||||
int val;
|
||||
uint8_t buf[1024] = {0};
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
size_t left = sizeof(buf);
|
||||
size_t i;
|
||||
int rv;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
printf("%d ", tests[i]);
|
||||
}
|
||||
printf("\n");
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
asn1_boolean_to_der(tests[i], &p, &len);
|
||||
print_buf(buf, len);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
rv = asn1_boolean_from_der(&val, &cp, &left);
|
||||
assert(rv > 0);
|
||||
if (val != tests[i]) {
|
||||
error_print("error decoding %zu-th: get %d, should be %d", i, val, tests[i]);
|
||||
err++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_asn1_integer(void)
|
||||
{
|
||||
int err = 0;
|
||||
int tests[] = {1, 127, 128, 65535, 65537, 1<<23, 1<<30, /* 0, -1 */ };
|
||||
int val;
|
||||
uint8_t buf[1024] = {0};
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
size_t left = sizeof(buf);
|
||||
size_t i;
|
||||
int rv;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
printf("%d ", tests[i]);
|
||||
}
|
||||
printf("\n");
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
asn1_int_to_der(tests[i], &p, &len);
|
||||
print_buf(buf, len);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
rv = asn1_int_from_der(&val, &cp, &left);
|
||||
assert(rv > 0);
|
||||
if (val != tests[i]) {
|
||||
error_print("error decoding %zu-th: get %d, should be %d", i, val, tests[i]);
|
||||
err++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_asn1_bit_string(void)
|
||||
{
|
||||
int err = 0;
|
||||
int tests[] = {1, 0xfe, 0xff, 0xffff, 0xfffff };
|
||||
int val;
|
||||
uint8_t buf[1024] = {0};
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
size_t left = sizeof(buf);
|
||||
size_t i;
|
||||
int rv;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
printf("%d ", tests[i]);
|
||||
}
|
||||
printf("\n");
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
asn1_bits_to_der(tests[i], &p, &len);
|
||||
print_buf(buf, len);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
rv = asn1_bits_from_der(&val, &cp, &left);
|
||||
assert(rv > 0);
|
||||
if (val != tests[i]) {
|
||||
error_print("error decoding %zu-th: get %d, should be %d", i, val, tests[i]);
|
||||
err++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_asn1_null(void)
|
||||
{
|
||||
int err = 0;
|
||||
int tests[6];
|
||||
uint8_t buf[1024] = {0};
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
size_t left = sizeof(buf);
|
||||
size_t i;
|
||||
int rv;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
printf("null ");
|
||||
}
|
||||
printf("\n");
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
asn1_null_to_der(&p, &len);
|
||||
print_buf(buf, len);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
rv = asn1_null_from_der(&cp, &left);
|
||||
assert(rv > 0);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_asn1_object_identifier(void)
|
||||
{
|
||||
int err = 0;
|
||||
int tests[] = {1, 2, 3, 4, 5, 6};
|
||||
int val;
|
||||
uint32_t nodes[32];
|
||||
size_t nodes_count;
|
||||
uint8_t buf[1024] = {0};
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
size_t left = sizeof(buf);
|
||||
size_t i;
|
||||
int rv;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
printf("%d ", tests[i]);
|
||||
}
|
||||
printf("\n");
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
asn1_object_identifier_to_der(tests[i], NULL, 0, &p, &len);
|
||||
print_buf(buf, len);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
rv = asn1_object_identifier_from_der(&val, nodes, &nodes_count, &cp, &left);
|
||||
assert(rv > 0);
|
||||
if (val != tests[i]) {
|
||||
error_print("error decoding %zu-th: get %d, should be %d", i, val, tests[i]);
|
||||
err++;
|
||||
}
|
||||
printf("%s\n", asn1_object_identifier_name(val));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_asn1_printable_string(void)
|
||||
{
|
||||
int err = 0;
|
||||
char *tests[] = {"hello", "world", "Just do it!"};
|
||||
const char *val;
|
||||
size_t vallen;
|
||||
uint32_t nodes[32];
|
||||
size_t nodes_count;
|
||||
uint8_t buf[1024] = {0};
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
size_t left = sizeof(buf);
|
||||
size_t i;
|
||||
int rv;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
printf("%s\n", tests[i]);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
asn1_printable_string_to_der(tests[i], &p, &len);
|
||||
print_buf(buf, len);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
char str[256] = {0};
|
||||
rv = asn1_printable_string_from_der(&val, &vallen, &cp, &left);
|
||||
assert(rv > 0);
|
||||
memcpy(str, val, vallen);
|
||||
|
||||
if (strcmp(str, tests[i]) != 0) {
|
||||
error_print("error decoding %zu-th: get %s, should be %s", i, str, tests[i]);
|
||||
err++;
|
||||
}
|
||||
printf("%s\n", str);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_asn1_utf8_string(void)
|
||||
{
|
||||
int err = 0;
|
||||
char *tests[] = {"hello", "world", "Just do it!"};
|
||||
const char *val;
|
||||
size_t vallen;
|
||||
uint32_t nodes[32];
|
||||
size_t nodes_count;
|
||||
uint8_t buf[1024] = {0};
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
size_t left = sizeof(buf);
|
||||
size_t i;
|
||||
int rv;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
printf("%s\n", tests[i]);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
asn1_utf8_string_to_der(tests[i], &p, &len);
|
||||
print_buf(buf, len);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
char str[256] = {0};
|
||||
rv = asn1_utf8_string_from_der(&val, &vallen, &cp, &left);
|
||||
assert(rv > 0);
|
||||
memcpy(str, val, vallen);
|
||||
|
||||
if (strcmp(str, tests[i]) != 0) {
|
||||
error_print("error decoding %zu-th: get %s, should be %s", i, str, tests[i]);
|
||||
err++;
|
||||
}
|
||||
printf("%s\n", str);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_asn1_ia5_string(void)
|
||||
{
|
||||
int err = 0;
|
||||
char *tests[] = {"hello", "world", "Just do it!"};
|
||||
const char *val;
|
||||
size_t vallen;
|
||||
uint32_t nodes[32];
|
||||
size_t nodes_count;
|
||||
uint8_t buf[1024] = {0};
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
size_t left = sizeof(buf);
|
||||
size_t i;
|
||||
int rv;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
printf("%s\n", tests[i]);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
asn1_ia5_string_to_der(tests[i], &p, &len);
|
||||
print_buf(buf, len);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
char str[256] = {0};
|
||||
rv = asn1_ia5_string_from_der(&val, &vallen, &cp, &left);
|
||||
assert(rv > 0);
|
||||
memcpy(str, val, vallen);
|
||||
|
||||
if (strcmp(str, tests[i]) != 0) {
|
||||
error_print("error decoding %zu-th: get %s, should be %s", i, str, tests[i]);
|
||||
err++;
|
||||
}
|
||||
printf("%s\n", str);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_time(void)
|
||||
{
|
||||
time_t tval = 0;
|
||||
printf("%s", ctime(&tval));
|
||||
time(&tval);
|
||||
printf("%s", ctime(&tval));
|
||||
printf("%016llx\n", (uint64_t)tval);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_asn1_utc_time(void)
|
||||
{
|
||||
int err = 0;
|
||||
time_t tests[] = {0, 0, 1<<30 };
|
||||
time_t val;
|
||||
uint8_t buf[1024] = {0};
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
size_t left = sizeof(buf);
|
||||
size_t i;
|
||||
int rv;
|
||||
|
||||
time(&tests[0]);
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
printf("%s", ctime(&tests[i]));
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
asn1_utc_time_to_der(tests[i], &p, &len);
|
||||
print_buf(buf, len);
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
rv = asn1_utc_time_from_der(&val, &cp, &left);
|
||||
assert(rv > 0);
|
||||
if (val != tests[i]) {
|
||||
error_print("error decoding %zu-th: get %zu, should be %zu", i, val, tests[i]);
|
||||
err++;
|
||||
}
|
||||
printf("%s", ctime(&val));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_asn1_generalized_time(void)
|
||||
{
|
||||
int err = 0;
|
||||
time_t tests[] = {0, 1<<30};
|
||||
time_t val;
|
||||
uint8_t buf[1024] = {0};
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
size_t left = sizeof(buf);
|
||||
size_t i;
|
||||
int rv;
|
||||
|
||||
time(&tests[0]);
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
printf("%s", ctime(&tests[i]));
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
asn1_generalized_time_to_der(tests[i], &p, &len);
|
||||
print_buf(buf, len);
|
||||
}
|
||||
|
||||
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
rv = asn1_generalized_time_from_der(&val, &cp, &left);
|
||||
assert(rv > 0);
|
||||
if (val != tests[i]) {
|
||||
error_print("error decoding %zu-th: get %zu, should be %zu", i, val, tests[i]);
|
||||
err++;
|
||||
}
|
||||
printf("%s", ctime(&val));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int err = 0;
|
||||
err += test_asn1_tag();
|
||||
err += test_asn1_length();
|
||||
err += test_asn1_boolean();
|
||||
err += test_asn1_integer();
|
||||
//err += test_asn1_bit_string();
|
||||
err += test_asn1_null();
|
||||
err += test_asn1_object_identifier();
|
||||
err += test_asn1_printable_string();
|
||||
err += test_asn1_utf8_string();
|
||||
err += test_asn1_ia5_string();
|
||||
err += test_asn1_utc_time();
|
||||
err += test_asn1_generalized_time();
|
||||
|
||||
return err;
|
||||
}
|
||||
110
tests/base64test.c
Normal file
110
tests/base64test.c
Normal file
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/base64.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
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};
|
||||
|
||||
int err = 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);
|
||||
printf("%s\n", 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("len = %d\n", len);
|
||||
print_der(buf2, len);
|
||||
printf("\n");
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_base64();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
94
tests/block_ciphertest.c
Normal file
94
tests/block_ciphertest.c
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/block_cipher.h>
|
||||
|
||||
|
||||
int block_cipher_sm4_test(void)
|
||||
{
|
||||
static char *iv_hex[] = {
|
||||
"A9993E364706816ABA3E25717850C26C9CD0D89D",
|
||||
"A9993E364706816ABA3E25717850C26C9CD0D89D",
|
||||
};
|
||||
|
||||
// 提供256比特的密钥长度
|
||||
static char *key_hex[] = {
|
||||
};
|
||||
|
||||
static char *plaintext_hex[] = {
|
||||
};
|
||||
|
||||
static char *ciphertext_hex[] = {
|
||||
};
|
||||
|
||||
const BLOCK_CIPEHR *cipher;
|
||||
BLOCK_CIPHER_KEY cipher_key;
|
||||
uint8_t key[32];
|
||||
uint8_t iv[16];
|
||||
uint8_t plaintext[16 * 3];
|
||||
uint8_t ciphertext[16 * 4];
|
||||
uint8_t buf[16 * 4];
|
||||
|
||||
for (i = 0; i < NUM_TESTS; i++) {
|
||||
hex2bin(key_hex, strlen(key_hex), key);
|
||||
hex2bin(iv_hex, strlen(iv_hex), iv);
|
||||
hex2bin(plaintext_hex, strlen(plaintext_hex), plaintext);
|
||||
hex2bin(ciphertext_hex, strlen(ciphertext_hex), ciphertext);
|
||||
|
||||
block_cipher_set_encrypt_key(&cipher_key, cipher, key, 16, iv);
|
||||
block_cipher_cbc_encrypt(&cipher_key, iv, plaintext, 3, buf);
|
||||
|
||||
if (memcmp(buf, 16 * 3, ciphertext) != 0) {
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
94
tests/chacha20test.c
Normal file
94
tests/chacha20test.c
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <gmssl/chacha20.h>
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int e = 0, 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_set_key(&state, key, nonce, counter);
|
||||
chacha20_generate_keystream(&state, 1, buf);
|
||||
|
||||
if (memcmp(buf, testdata, sizeof(testdata)) != 0) {
|
||||
printf("chacha20 test 1 failed\n");
|
||||
} else {
|
||||
printf("chacha20 test 1 ok\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
107
tests/cmstest.c
Normal file
107
tests/cmstest.c
Normal file
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#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/cms.h>
|
||||
|
||||
static int test_cms_data(void)
|
||||
{
|
||||
uint8_t data[20];
|
||||
uint8_t content_info[512];
|
||||
size_t content_info_len = sizeof(content_info);
|
||||
size_t len = 0;
|
||||
int i;
|
||||
|
||||
memset(data, 'A', sizeof(data));
|
||||
|
||||
cms_content_info_set_data(content_info, &content_info_len, data, sizeof(data));
|
||||
cms_content_info_print(stdout, content_info, content_info_len, 0, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_cms_sign(void)
|
||||
{
|
||||
SM2_KEY sign_key;
|
||||
X509_CERTIFICATE sign_cert;
|
||||
uint8_t data[20];
|
||||
uint8_t content_info[1024];
|
||||
size_t content_info_len = 0;
|
||||
|
||||
FILE *key_fp = fopen("sign_key.pem", "r");
|
||||
FILE *cert_fp = fopen("sign_cert.pem", "r");
|
||||
|
||||
if (sm2_private_key_from_pem(&sign_key, key_fp) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (x509_certificate_from_pem(&sign_cert, cert_fp) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (cms_sign(&sign_key, &sign_cert, 1, CMS_data, data, 20, NULL, NULL, 0, content_info, &content_info_len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
cms_content_info_print(stdout, content_info, content_info_len, 0, 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_cms_data();
|
||||
test_cms_sign();
|
||||
return 0;
|
||||
}
|
||||
60
tests/destest.c
Normal file
60
tests/destest.c
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/des.h>
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
85
tests/digesttest.c
Normal file
85
tests/digesttest.c
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#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;
|
||||
}
|
||||
118
tests/gcmtest.c
Normal file
118
tests/gcmtest.c
Normal file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/gcm.h>
|
||||
|
||||
|
||||
struct {
|
||||
char *K;
|
||||
char *P;
|
||||
char *IV;
|
||||
char *C;
|
||||
char *T;
|
||||
} gcm_tests[] = {
|
||||
/* test 1 */
|
||||
{
|
||||
"00000000000000000000000000000000",
|
||||
"",
|
||||
"000000000000000000000000",
|
||||
"",
|
||||
"58e2fccefa7e3061367f1d57a4e7455a",
|
||||
},
|
||||
/* test 2 */
|
||||
{
|
||||
"00000000000000000000000000000000",
|
||||
"00000000000000000000000000000000",
|
||||
"000000000000000000000000",
|
||||
"0388dace60b6a392f328c2b971b2fe78",
|
||||
"ab6e47d42cec13bdf53a67b21257bddf",
|
||||
},
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int err = 0;
|
||||
GCM_CTX ctx;
|
||||
uint8_t *key = NULL;
|
||||
uint8_t *iv = NULL;
|
||||
uint8_t *p = NULL;
|
||||
uint8_t *c = NULL;
|
||||
uint8_t *tag = NULL;
|
||||
size_t keylen, ivlen, plen, clen, taglen, len, i;
|
||||
|
||||
for (i = 0; i < sizeof(gcm_tests)/sizeof(gcm_tests[0]); i++) {
|
||||
|
||||
key = OPENSSL_hexstr2buf(gcm_tests[i].K, &keylen);
|
||||
iv = OPENSSL_hexstr2buf(gcm_tests[i].IV, &ivlen);
|
||||
p = OPENSSL_hexstr2buf(gcm_tests[i].P, &plen);
|
||||
c = OPENSSL_hexstr2buf(gcm_tests[i].C, &clen);
|
||||
tag = OPENSSL_hexstr2buf(gcm_tests[i].T, &taglen);
|
||||
buf = malloc(plen);
|
||||
|
||||
gcm_init(&ctx, taglen, key, keylen, iv, ivlen, aad, aadlen);
|
||||
gcm_update(&ctx, p, plen, buf, &len);
|
||||
if (!gcm_finish_verify(&ctx, tag, taglen)) {
|
||||
printf("gcm test %zu failed\n", i+1);
|
||||
err++;
|
||||
} else {
|
||||
printf("gcm test %zu ok\n", i+1);
|
||||
}
|
||||
|
||||
free(key);
|
||||
free(iv);
|
||||
free(p);
|
||||
free(c);
|
||||
free(tag);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
93
tests/gf128test.c
Normal file
93
tests/gf128test.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include "internal/gf128.h"
|
||||
|
||||
|
||||
static gf128_t gf128_from_hex(const char *s)
|
||||
{
|
||||
uint8_t bin[16];
|
||||
assert(strlen(s) == 32);
|
||||
hex2bin(s, strlen(s), bin);
|
||||
return gf128_from_bytes(bin);
|
||||
}
|
||||
|
||||
static int gf128_equ_hex(gf128_t a, const char *s)
|
||||
{
|
||||
uint8_t bin1[16];
|
||||
uint8_t bin2[16];
|
||||
assert(strlen(s) == 32);
|
||||
hex2bin(s, strlen(s), bin1);
|
||||
gf128_to_bytes(a, bin2);
|
||||
return memcmp(bin1, bin2, sizeof(bin1)) == 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
gf128_t zero = gf128_from_hex("00000000000000000000000000000000");
|
||||
gf128_t one = gf128_from_hex("00000000000000000000000000000001");
|
||||
gf128_t ones = gf128_from_hex("11111111111111111111111111111111");
|
||||
gf128_t a = gf128_from_hex("de300f9301a499a965f8bf677e99e80d");
|
||||
gf128_t b = gf128_from_hex("14b267838ec9ef1bb7b5ce8c19e34bc6");
|
||||
gf128_t r;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
131
tests/hash_drbgtest.c
Normal file
131
tests/hash_drbgtest.c
Normal file
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/digest.h>
|
||||
#include <gmssl/hash_drbg.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"
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
HASH_DRBG drbg;
|
||||
|
||||
unsigned char *entropy = NULL;
|
||||
unsigned char *nonce = NULL;
|
||||
unsigned char *personalstr = NULL;
|
||||
unsigned char *v = NULL;
|
||||
unsigned char *c = NULL;
|
||||
size_t entropy_len, nonce_len, personalstr_len, vlen, clen;
|
||||
|
||||
|
||||
unsigned char *entropy_pr1 = NULL;
|
||||
size_t entropy_pr1len;
|
||||
|
||||
unsigned char out[640/8];
|
||||
|
||||
entropy = OPENSSL_hexstr2buf(EntropyInput, &entropy_len);
|
||||
nonce = OPENSSL_hexstr2buf(Nonce, &nonce_len);
|
||||
personalstr = OPENSSL_hexstr2buf(PersonalizationString, &personalstr_len);
|
||||
v = OPENSSL_hexstr2buf(V0, &vlen);
|
||||
c = OPENSSL_hexstr2buf(C0, &clen);
|
||||
|
||||
entropy_pr1 = OPENSSL_hexstr2buf(EntropyInputPR1, &entropy_pr1len);
|
||||
|
||||
|
||||
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");
|
||||
} else {
|
||||
printf("ok\n");
|
||||
}
|
||||
|
||||
unsigned char *pr1 = NULL;
|
||||
unsigned char *pr2 = NULL;
|
||||
size_t pr1_len, pr2_len;
|
||||
|
||||
pr1 = OPENSSL_hexstr2buf("2edb396eeb8960f77943c2a59075a786", &pr1_len);
|
||||
pr2 = OPENSSL_hexstr2buf("30b565b63a5012676940d3ef17d9e996", &pr2_len);
|
||||
|
||||
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);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < sizeof(out); i++) {
|
||||
printf("%02x", out[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
252
tests/hkdftest.c
Normal file
252
tests/hkdftest.c
Normal file
@@ -0,0 +1,252 @@
|
||||
/*
|
||||
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#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;
|
||||
|
||||
for (i = 0; i < sizeof(hkdf_tests)/sizeof(hkdf_tests[0]); i++) {
|
||||
|
||||
digest = digest_from_name(hkdf_tests[i].algor);
|
||||
hex2bin(hkdf_tests[i].ikm, strlen(hkdf_tests[i].ikm), ikm);
|
||||
hex2bin(hkdf_tests[i].salt, strlen(hkdf_tests[i].salt), salt);
|
||||
hex2bin(hkdf_tests[i].info, strlen(hkdf_tests[i].info), info);
|
||||
hex2bin(hkdf_tests[i].prk, strlen(hkdf_tests[i].prk), prk);
|
||||
hex2bin(hkdf_tests[i].okm, strlen(hkdf_tests[i].okm), okm);
|
||||
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 1;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_hkdf();
|
||||
return 0;
|
||||
}
|
||||
158
tests/hmactest.c
Normal file
158
tests/hmactest.c
Normal file
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2018 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#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;
|
||||
|
||||
hex2bin(key_hex, strlen(key_hex), key);
|
||||
hex2bin(data_hex, strlen(data_hex), data);
|
||||
hex2bin(hmac_hex, strlen(hmac_hex), hmac);
|
||||
|
||||
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;
|
||||
};
|
||||
113
tests/md5test.c
Normal file
113
tests/md5test.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.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;
|
||||
unsigned char *dgstbuf = NULL;
|
||||
size_t dgstbuflen;
|
||||
unsigned char dgst[16];
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) {
|
||||
if (!(dgstbuf = OPENSSL_hexstr2buf(dgsthex[i], &dgstbuflen))) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
md5_digest((unsigned char *)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);
|
||||
}
|
||||
|
||||
free(dgstbuf);
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
62
tests/oidtest.c
Normal file
62
tests/oidtest.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/oid.h>
|
||||
#include <gmssl/asn1.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// test_asn1_oid();
|
||||
|
||||
test_asn1_object_identifier();
|
||||
return 1;
|
||||
}
|
||||
160
tests/pbkdf2test.c
Normal file
160
tests/pbkdf2test.c
Normal file
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/pbkdf2.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,
|
||||
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");
|
||||
}
|
||||
|
||||
int main(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++) {
|
||||
hex2bin(pbkdf2_hmac_sha1_tests[i].dk, strlen(pbkdf2_hmac_sha1_tests[i].dk), buf);
|
||||
|
||||
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);
|
||||
|
||||
if (memcmp(key, buf, pbkdf2_hmac_sha1_tests[i].dklen) != 0) {
|
||||
printf("%d failed\n", i);
|
||||
} else {
|
||||
printf("%d ok\n", i);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
318
tests/pkcs8test.c
Normal file
318
tests/pkcs8test.c
Normal file
@@ -0,0 +1,318 @@
|
||||
/*
|
||||
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
sm2_enced_private_key_info_from_der * 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/oid.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/pkcs8.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static int test_pbkdf2_params(void)
|
||||
{
|
||||
int err = 0;
|
||||
uint8_t salt[8] = {0};
|
||||
const uint8_t *psalt;
|
||||
size_t saltlen;
|
||||
int iter = 65536;
|
||||
int keylen;
|
||||
int prf = OID_hmac_sm3;
|
||||
uint8_t buf[128];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
keylen = -1;
|
||||
if (pbkdf2_params_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
if (pbkdf2_params_from_der(&psalt, &saltlen, &iter, &keylen, &prf, &cp, &len) != 1
|
||||
|| len > 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
|
||||
keylen = 16;
|
||||
if (pbkdf2_params_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
if (pbkdf2_params_from_der(&psalt, &saltlen, &iter, &keylen, &prf, &cp, &len) != 1
|
||||
|| len > 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
printf("%s : ok\n", __func__);
|
||||
end:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_pbkdf2_algor(void)
|
||||
{
|
||||
int err = 0;
|
||||
uint8_t salt[8] = {0};
|
||||
const uint8_t *psalt;
|
||||
size_t saltlen;
|
||||
int iter = 65536;
|
||||
int keylen;
|
||||
int prf = OID_hmac_sm3;
|
||||
uint8_t buf[128];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
keylen = -1;
|
||||
if (pbkdf2_algor_to_der(salt, sizeof(salt), iter, keylen, prf, &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
if (pbkdf2_algor_from_der(&psalt, &saltlen, &iter, &keylen, &prf, &cp, &len) != 1
|
||||
|| len > 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
printf("%s : ok\n", __func__);
|
||||
end:
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
static int test_pbes2_enc_algor(void)
|
||||
{
|
||||
int err = 0;
|
||||
int cipher = OID_sm4_cbc;
|
||||
uint8_t iv[16] = {1};
|
||||
const uint8_t *piv;
|
||||
size_t ivlen;
|
||||
uint8_t buf[128];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
if (pbes2_enc_algor_to_der(OID_sm4_cbc, iv, sizeof(iv), &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
if (pbes2_enc_algor_from_der(&cipher, &piv, &ivlen, &cp, &len) != 1
|
||||
|| len > 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
printf("%s : ok\n", __func__);
|
||||
end:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_pbes2_params(void)
|
||||
{
|
||||
int err = 0;
|
||||
uint8_t salt[8] = {0};
|
||||
const uint8_t *psalt;
|
||||
size_t saltlen;
|
||||
int iter = 65536;
|
||||
int prf = OID_hmac_sm3;
|
||||
int cipher = OID_sm4_cbc;
|
||||
uint8_t iv[16];
|
||||
const uint8_t *piv;
|
||||
size_t ivlen;
|
||||
uint8_t buf[256];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
if (pbes2_params_to_der(salt, sizeof(salt), iter, prf, cipher, iv, sizeof(iv), &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
if (pbes2_params_from_der(&psalt, &saltlen, &iter, &prf, &cipher, &piv, &ivlen, &cp, &len) != 1
|
||||
|| len > 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
printf("%s : ok\n", __func__);
|
||||
end:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_pbes2_algor(void)
|
||||
{
|
||||
int err = 0;
|
||||
uint8_t salt[8] = {0};
|
||||
const uint8_t *psalt;
|
||||
size_t saltlen;
|
||||
int iter = 65536;
|
||||
int prf = OID_hmac_sm3;
|
||||
int cipher = OID_sm4_cbc;
|
||||
uint8_t iv[16];
|
||||
const uint8_t *piv;
|
||||
size_t ivlen;
|
||||
uint8_t buf[256];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
if (pbes2_algor_to_der(salt, sizeof(salt), iter, prf, cipher, iv, sizeof(iv), &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
if (pbes2_algor_from_der(&psalt, &saltlen, &iter, &prf, &cipher, &piv, &ivlen, &cp, &len) != 1
|
||||
|| len > 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
printf("%s : ok\n", __func__);
|
||||
end:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_pkcs8_enced_private_key_info(void)
|
||||
{
|
||||
int err = 0;
|
||||
uint8_t salt[8] = {0};
|
||||
const uint8_t *psalt;
|
||||
size_t saltlen;
|
||||
int iter = 65536;
|
||||
int prf = OID_hmac_sm3;
|
||||
int cipher = OID_sm4_cbc;
|
||||
uint8_t iv[16];
|
||||
const uint8_t *piv;
|
||||
size_t ivlen;
|
||||
uint8_t enced[128];
|
||||
const uint8_t *penced;
|
||||
size_t encedlen;
|
||||
uint8_t buf[256];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
if (pkcs8_enced_private_key_info_to_der(salt, sizeof(salt), iter, prf, cipher, iv, sizeof(iv),
|
||||
enced, sizeof(enced), &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
if (pkcs8_enced_private_key_info_from_der(&psalt, &saltlen, &iter, &prf, &cipher, &piv, &ivlen,
|
||||
&penced, &encedlen, &cp, &len) != 1
|
||||
|| len > 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
printf("%s : ok\n", __func__);
|
||||
end:
|
||||
return err;
|
||||
}
|
||||
|
||||
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_keygen(&sm2_key);
|
||||
memcpy(&sm2_buf, &sm2_key, sizeof(sm2_key));
|
||||
//sm2_key_print(stdout, &sm2_key, 0, 0);
|
||||
|
||||
if (sm2_enced_private_key_info_to_der(&sm2_key, "passowrd", &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
|
||||
memset(&sm2_key, 0, sizeof(sm2_key));
|
||||
if (sm2_enced_private_key_info_from_der(&sm2_key, &attrs, &attrslen, "password", &cp, &len) != 1
|
||||
|| len > 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
//sm2_key_print(stdout, &sm2_key, 0, 0);
|
||||
|
||||
if (memcmp(&sm2_key, &sm2_buf, sizeof(sm2_key)) != 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
printf("%s : ok\n", __func__);
|
||||
end:
|
||||
return err;
|
||||
}
|
||||
|
||||
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();
|
||||
return err;
|
||||
}
|
||||
427
tests/rc4test.c
Normal file
427
tests/rc4test.c
Normal file
@@ -0,0 +1,427 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#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, 0x4f, 0x0b, 0xa1, 0xfe, 0x9f, 0xca, 0x20, 0xe0, 0x5b, 0xf8, 0xff, 0x2b,
|
||||
0x45, 0x12, 0x90, 0x48, 0xe6, 0xa0, 0xed, 0x0b, 0x56, 0xb4, 0x90, 0x33, 0x8f, 0x07, 0x8d, 0xa5,
|
||||
0x30, 0xab, 0xbc, 0xc7, 0xc2, 0x0b, 0x01, 0x60, 0x9f, 0x23, 0xee, 0x2d, 0x5f, 0x6b, 0xb7, 0xdf,
|
||||
0x32, 0x94, 0xf7, 0x44, 0xd8, 0xf9, 0x79, 0x05, 0x07, 0xe7, 0x0f, 0x62, 0xe5, 0xbb, 0xce, 0xea,
|
||||
0xd8, 0x72, 0x9d, 0xb4, 0x18, 0x82, 0x25, 0x9b, 0xee, 0x4f, 0x82, 0x53, 0x25, 0xf5, 0xa1, 0x30,
|
||||
0x1e, 0xb1, 0x4a, 0x0c, 0x13, 0xb3, 0xbf, 0x47, 0xfa, 0x2a, 0x0b, 0xa9, 0x3a, 0xd4, 0x5b, 0x8b,
|
||||
0xcc, 0x58, 0x2f, 0x8b, 0xa9, 0xf2, 0x65, 0xe2, 0xb1, 0xbe, 0x91, 0x12, 0xe9, 0x75, 0xd2, 0xd7,
|
||||
0xf2, 0xe3, 0x0f, 0x9b, 0xd1, 0x02, 0xec, 0xbf, 0x75, 0xaa, 0xad, 0xe9, 0xbc, 0x35, 0xc4, 0x3c,
|
||||
0xec, 0x0e, 0x11, 0xc4, 0x79, 0xdc, 0x32, 0x9d, 0xc8, 0xda, 0x79, 0x68, 0xfe, 0x96, 0x56, 0x81,
|
||||
0x06, 0x83, 0x26, 0xa2, 0x11, 0x84, 0x16, 0xd2, 0x1f, 0x9d, 0x04, 0xb2, 0xcd, 0x1c, 0xa0, 0x50,
|
||||
0xff, 0x25, 0xb5, 0x89, 0x95, 0x99, 0x67, 0x07, 0xe5, 0x1f, 0xbd, 0xf0, 0x8b, 0x34, 0xd8, 0x75,
|
||||
},
|
||||
{
|
||||
0x29, 0x3f, 0x02, 0xd4, 0x7f, 0x37, 0xc9, 0xb6, 0x33, 0xf2, 0xaf, 0x52, 0x85, 0xfe, 0xb4, 0x6b,
|
||||
0xe6, 0x20, 0xf1, 0x39, 0x0d, 0x19, 0xbd, 0x84, 0xe2, 0xe0, 0xfd, 0x75, 0x20, 0x31, 0xaf, 0xc1,
|
||||
0x91, 0x4f, 0x02, 0x53, 0x1c, 0x92, 0x18, 0x81, 0x0d, 0xf6, 0x0f, 0x67, 0xe3, 0x38, 0x15, 0x4c,
|
||||
0xd0, 0xfd, 0xb5, 0x83, 0x07, 0x3c, 0xe8, 0x5a, 0xb8, 0x39, 0x17, 0x74, 0x0e, 0xc0, 0x11, 0xd5,
|
||||
0x75, 0xf8, 0x14, 0x11, 0xe8, 0x71, 0xcf, 0xfa, 0x70, 0xb9, 0x0c, 0x74, 0xc5, 0x92, 0xe4, 0x54,
|
||||
0x0b, 0xb8, 0x72, 0x02, 0x93, 0x8d, 0xad, 0x60, 0x9e, 0x87, 0xa5, 0xa1, 0xb0, 0x79, 0xe5, 0xe4,
|
||||
0xc2, 0x91, 0x12, 0x46, 0xb6, 0x12, 0xe7, 0xe7, 0xb9, 0x03, 0xdf, 0xed, 0xa1, 0xda, 0xd8, 0x66,
|
||||
0x32, 0x82, 0x8f, 0x91, 0x50, 0x2b, 0x62, 0x91, 0x36, 0x8d, 0xe8, 0x08, 0x1d, 0xe3, 0x6f, 0xc2,
|
||||
0xf3, 0xb9, 0xa7, 0xe3, 0xb2, 0x97, 0xbf, 0x9a, 0xd8, 0x04, 0x51, 0x2f, 0x90, 0x63, 0xef, 0xf1,
|
||||
0x8e, 0xcb, 0x67, 0xa9, 0xba, 0x1f, 0x55, 0xa5, 0xa0, 0x67, 0xe2, 0xb0, 0x26, 0xa3, 0x67, 0x6f,
|
||||
0xd2, 0xaa, 0x90, 0x2b, 0xd4, 0x2d, 0x0d, 0x7c, 0xfd, 0x34, 0x0c, 0xd4, 0x58, 0x10, 0x52, 0x9f,
|
||||
0x78, 0xb2, 0x72, 0xc9, 0x6e, 0x42, 0xea, 0xb4, 0xc6, 0x0b, 0xd9, 0x14, 0xe3, 0x9d, 0x06, 0xe3,
|
||||
0xf4, 0x33, 0x2f, 0xd3, 0x1a, 0x07, 0x93, 0x96, 0xee, 0x3c, 0xee, 0x3f, 0x2a, 0x4f, 0xf0, 0x49,
|
||||
0x05, 0x45, 0x97, 0x81, 0xd4, 0x1f, 0xda, 0x7f, 0x30, 0xc1, 0xbe, 0x7e, 0x12, 0x46, 0xc6, 0x23,
|
||||
0xad, 0xfd, 0x38, 0x68, 0xb8, 0xe5, 0x14, 0x85, 0xd5, 0xe6, 0x10, 0x01, 0x7e, 0x3d, 0xd6, 0x09,
|
||||
0xad, 0x26, 0x58, 0x1c, 0x0c, 0x5b, 0xe4, 0x5f, 0x4c, 0xea, 0x01, 0xdb, 0x2f, 0x38, 0x05, 0xd5,
|
||||
0xf3, 0x17, 0x2c, 0xef, 0xfc, 0x3b, 0x3d, 0x99, 0x7c, 0x85, 0xcc, 0xd5, 0xaf, 0x1a, 0x95, 0x0c,
|
||||
0xe7, 0x4b, 0x0b, 0x97, 0x31, 0x22, 0x7f, 0xd3, 0x7c, 0x0e, 0xc0, 0x8a, 0x47, 0xdd, 0xd8, 0xb8,
|
||||
},
|
||||
{
|
||||
0x97, 0xab, 0x8a, 0x1b, 0xf0, 0xaf, 0xb9, 0x61, 0x32, 0xf2, 0xf6, 0x72, 0x58, 0xda, 0x15, 0xa8,
|
||||
0x82, 0x63, 0xef, 0xdb, 0x45, 0xc4, 0xa1, 0x86, 0x84, 0xef, 0x87, 0xe6, 0xb1, 0x9e, 0x5b, 0x09,
|
||||
0x96, 0x36, 0xeb, 0xc9, 0x84, 0x19, 0x26, 0xf4, 0xf7, 0xd1, 0xf3, 0x62, 0xbd, 0xdf, 0x6e, 0x18,
|
||||
0xd0, 0xa9, 0x90, 0xff, 0x2c, 0x05, 0xfe, 0xf5, 0xb9, 0x03, 0x73, 0xc9, 0xff, 0x4b, 0x87, 0x0a,
|
||||
0x73, 0x23, 0x9f, 0x1d, 0xb7, 0xf4, 0x1d, 0x80, 0xb6, 0x43, 0xc0, 0xc5, 0x25, 0x18, 0xec, 0x63,
|
||||
0x16, 0x3b, 0x31, 0x99, 0x23, 0xa6, 0xbd, 0xb4, 0x52, 0x7c, 0x62, 0x61, 0x26, 0x70, 0x3c, 0x0f,
|
||||
0x49, 0xd6, 0xc8, 0xaf, 0x0f, 0x97, 0x14, 0x4a, 0x87, 0xdf, 0x21, 0xd9, 0x14, 0x72, 0xf9, 0x66,
|
||||
0x44, 0x17, 0x3a, 0x10, 0x3b, 0x66, 0x16, 0xc5, 0xd5, 0xad, 0x1c, 0xee, 0x40, 0xc8, 0x63, 0xd0,
|
||||
0x27, 0x3c, 0x9c, 0x4b, 0x27, 0xf3, 0x22, 0xe4, 0xe7, 0x16, 0xef, 0x53, 0xa4, 0x7d, 0xe7, 0xa4,
|
||||
0xc6, 0xd0, 0xe7, 0xb2, 0x26, 0x25, 0x9f, 0xa9, 0x02, 0x34, 0x90, 0xb2, 0x61, 0x67, 0xad, 0x1d,
|
||||
0x1f, 0xe8, 0x98, 0x67, 0x13, 0xf0, 0x7c, 0x3d, 0x9a, 0xe1, 0xc1, 0x63, 0xff, 0x8c, 0xf9, 0xd3,
|
||||
0x83, 0x69, 0xe1, 0xa9, 0x65, 0x61, 0x0b, 0xe8, 0x87, 0xfb, 0xd0, 0xc7, 0x91, 0x62, 0xaa, 0xfb,
|
||||
0x0a, 0x01, 0x27, 0xab, 0xb4, 0x44, 0x84, 0xb9, 0xfb, 0xef, 0x5a, 0xbc, 0xae, 0x1b, 0x57, 0x9f,
|
||||
0xc2, 0xcd, 0xad, 0xc6, 0x40, 0x2e, 0x8e, 0xe8, 0x66, 0xe1, 0xf3, 0x7b, 0xdb, 0x47, 0xe4, 0x2c,
|
||||
0x26, 0xb5, 0x1e, 0xa3, 0x7d, 0xf8, 0xe1, 0xd6, 0xf7, 0x6f, 0xc3, 0xb6, 0x6a, 0x74, 0x29, 0xb3,
|
||||
0xbc, 0x76, 0x83, 0x20, 0x5d, 0x4f, 0x44, 0x3d, 0xc1, 0xf2, 0x9d, 0xda, 0x33, 0x15, 0xc8, 0x7b,
|
||||
0xd5, 0xfa, 0x5a, 0x34, 0x69, 0xd2, 0x9a, 0xaa, 0xf8, 0x3d, 0x23, 0x58, 0x9d, 0xb8, 0xc8, 0x5b,
|
||||
0x3f, 0xb4, 0x6e, 0x2c, 0x8f, 0x0f, 0x06, 0x8e, 0xdc, 0xe8, 0xcd, 0xcd, 0x7d, 0xfc, 0x58, 0x62,
|
||||
},
|
||||
{
|
||||
0xed, 0xe3, 0xb0, 0x46, 0x43, 0xe5, 0x86, 0xcc, 0x90, 0x7d, 0xc2, 0x18, 0x51, 0x70, 0x99, 0x02,
|
||||
0x03, 0x51, 0x6b, 0xa7, 0x8f, 0x41, 0x3b, 0xeb, 0x22, 0x3a, 0xa5, 0xd4, 0xd2, 0xdf, 0x67, 0x11,
|
||||
0x3c, 0xfd, 0x6c, 0xb5, 0x8e, 0xe0, 0xfd, 0xde, 0x64, 0x01, 0x76, 0xad, 0x00, 0x00, 0x04, 0x4d,
|
||||
0x48, 0x53, 0x2b, 0x21, 0xfb, 0x60, 0x79, 0xc9, 0x11, 0x4c, 0x0f, 0xfd, 0x9c, 0x04, 0xa1, 0xad,
|
||||
0x3e, 0x8c, 0xea, 0x98, 0x01, 0x71, 0x09, 0x97, 0x90, 0x84, 0xb1, 0xef, 0x92, 0xf9, 0x9d, 0x86,
|
||||
0xe2, 0x0f, 0xb4, 0x9b, 0xdb, 0x33, 0x7e, 0xe4, 0x8b, 0x8d, 0x8d, 0xc0, 0xf4, 0xaf, 0xef, 0xfe,
|
||||
0x5c, 0x25, 0x21, 0xea, 0xcd, 0x79, 0x66, 0xf1, 0x5e, 0x05, 0x65, 0x44, 0xbe, 0xa0, 0xd3, 0x15,
|
||||
0xe0, 0x67, 0xa7, 0x03, 0x19, 0x31, 0xa2, 0x46, 0xa6, 0xc3, 0x87, 0x5d, 0x2f, 0x67, 0x8a, 0xcb,
|
||||
0xa6, 0x4f, 0x70, 0xaf, 0x88, 0xae, 0x56, 0xb6, 0xf8, 0x75, 0x81, 0xc0, 0xe2, 0x3e, 0x6b, 0x08,
|
||||
0xf4, 0x49, 0x03, 0x1d, 0xe3, 0x12, 0x81, 0x4e, 0xc6, 0xf3, 0x19, 0x29, 0x1f, 0x4a, 0x05, 0x16,
|
||||
0xbd, 0xae, 0x85, 0x92, 0x4b, 0x3c, 0xb1, 0xd0, 0xa2, 0xe3, 0x3a, 0x30, 0xc6, 0xd7, 0x95, 0x99,
|
||||
0x8a, 0x0f, 0xed, 0xdb, 0xac, 0x86, 0x5a, 0x09, 0xbc, 0xd1, 0x27, 0xfb, 0x56, 0x2e, 0xd6, 0x0a,
|
||||
0xb5, 0x5a, 0x0a, 0x5b, 0x51, 0xa1, 0x2a, 0x8b, 0xe3, 0x48, 0x99, 0xc3, 0xe0, 0x47, 0x51, 0x1a,
|
||||
0xd9, 0xa0, 0x9c, 0xea, 0x3c, 0xe7, 0x5f, 0xe3, 0x96, 0x98, 0x07, 0x03, 0x17, 0xa7, 0x13, 0x39,
|
||||
0x55, 0x22, 0x25, 0xed, 0x11, 0x77, 0xf4, 0x45, 0x84, 0xac, 0x8c, 0xfa, 0x6c, 0x4e, 0xb5, 0xfc,
|
||||
0x7e, 0x82, 0xcb, 0xab, 0xfc, 0x95, 0x38, 0x1b, 0x08, 0x09, 0x98, 0x44, 0x21, 0x29, 0xc2, 0xf8,
|
||||
0x1f, 0x13, 0x5e, 0xd1, 0x4c, 0xe6, 0x0a, 0x91, 0x36, 0x9d, 0x23, 0x22, 0xbe, 0xf2, 0x5e, 0x3c,
|
||||
0x08, 0xb6, 0xbe, 0x45, 0x12, 0x4a, 0x43, 0xe2, 0xeb, 0x77, 0x95, 0x3f, 0x84, 0xdc, 0x85, 0x53,
|
||||
},
|
||||
{
|
||||
0x9a, 0xc7, 0xcc, 0x9a, 0x60, 0x9d, 0x1e, 0xf7, 0xb2, 0x93, 0x28, 0x99, 0xcd, 0xe4, 0x1b, 0x97,
|
||||
0x52, 0x48, 0xc4, 0x95, 0x90, 0x14, 0x12, 0x6a, 0x6e, 0x8a, 0x84, 0xf1, 0x1d, 0x1a, 0x9e, 0x1c,
|
||||
0x06, 0x59, 0x02, 0xe4, 0xb6, 0x20, 0xf6, 0xcc, 0x36, 0xc8, 0x58, 0x9f, 0x66, 0x43, 0x2f, 0x2b,
|
||||
0xd3, 0x9d, 0x56, 0x6b, 0xc6, 0xbc, 0xe3, 0x01, 0x07, 0x68, 0x15, 0x15, 0x49, 0xf3, 0x87, 0x3f,
|
||||
0xb6, 0xd1, 0xe6, 0xc4, 0xa5, 0xe4, 0x77, 0x1c, 0xad, 0x79, 0x53, 0x8d, 0xf2, 0x95, 0xfb, 0x11,
|
||||
0xc6, 0x8c, 0x1d, 0x5c, 0x55, 0x9a, 0x97, 0x41, 0x23, 0xdf, 0x1d, 0xbc, 0x52, 0xa4, 0x3b, 0x89,
|
||||
0xc5, 0xec, 0xf8, 0x8d, 0xe8, 0x97, 0xfd, 0x57, 0xfe, 0xd3, 0x01, 0x70, 0x1b, 0x82, 0xa2, 0x59,
|
||||
0xec, 0xcb, 0xe1, 0x3d, 0xe1, 0xfc, 0xc9, 0x1c, 0x11, 0xa0, 0xb2, 0x6c, 0x0b, 0xc8, 0xfa, 0x4d,
|
||||
0xe7, 0xa7, 0x25, 0x74, 0xf8, 0x78, 0x2a, 0xe2, 0x6a, 0xab, 0xcf, 0x9e, 0xbc, 0xd6, 0x60, 0x65,
|
||||
0xbd, 0xf0, 0x32, 0x4e, 0x60, 0x83, 0xdc, 0xc6, 0xd3, 0xce, 0xdd, 0x3c, 0xa8, 0xc5, 0x3c, 0x16,
|
||||
0xb4, 0x01, 0x10, 0xc4, 0x19, 0x0b, 0x56, 0x22, 0xa9, 0x61, 0x16, 0xb0, 0x01, 0x7e, 0xd2, 0x97,
|
||||
0xff, 0xa0, 0xb5, 0x14, 0x64, 0x7e, 0xc0, 0x4f, 0x63, 0x06, 0xb8, 0x92, 0xae, 0x66, 0x11, 0x81,
|
||||
0xd0, 0x3d, 0x1b, 0xc0, 0x3c, 0xd3, 0x3d, 0x70, 0xdf, 0xf9, 0xfa, 0x5d, 0x71, 0x96, 0x3e, 0xbd,
|
||||
0x8a, 0x44, 0x12, 0x64, 0x11, 0xea, 0xa7, 0x8b, 0xd5, 0x1e, 0x8d, 0x87, 0xa8, 0x87, 0x9b, 0xf5,
|
||||
0xfa, 0xbe, 0xb7, 0x60, 0x28, 0xad, 0xe2, 0xd0, 0xe4, 0x87, 0x22, 0xe4, 0x6c, 0x46, 0x15, 0xa3,
|
||||
0xc0, 0x5d, 0x88, 0xab, 0xd5, 0x03, 0x57, 0xf9, 0x35, 0xa6, 0x3c, 0x59, 0xee, 0x53, 0x76, 0x23,
|
||||
0xff, 0x38, 0x26, 0x5c, 0x16, 0x42, 0xc1, 0xab, 0xe8, 0xd3, 0xc2, 0xfe, 0x5e, 0x57, 0x2b, 0xf8,
|
||||
0xa3, 0x6a, 0x4c, 0x30, 0x1a, 0xe8, 0xac, 0x13, 0x61, 0x0c, 0xcb, 0xc1, 0x22, 0x56, 0xca, 0xcc,
|
||||
},
|
||||
{
|
||||
0x05, 0x95, 0xe5, 0x7f, 0xe5, 0xf0, 0xbb, 0x3c, 0x70, 0x6e, 0xda, 0xc8, 0xa4, 0xb2, 0xdb, 0x11,
|
||||
0xdf, 0xde, 0x31, 0x34, 0x4a, 0x1a, 0xf7, 0x69, 0xc7, 0x4f, 0x07, 0x0a, 0xee, 0x9e, 0x23, 0x26,
|
||||
0xb0, 0x6b, 0x9b, 0x1e, 0x19, 0x5d, 0x13, 0xd8, 0xf4, 0xa7, 0x99, 0x5c, 0x45, 0x53, 0xac, 0x05,
|
||||
0x6b, 0xd2, 0x37, 0x8e, 0xc3, 0x41, 0xc9, 0xa4, 0x2f, 0x37, 0xba, 0x79, 0xf8, 0x8a, 0x32, 0xff,
|
||||
0xe7, 0x0b, 0xce, 0x1d, 0xf7, 0x64, 0x5a, 0xdb, 0x5d, 0x2c, 0x41, 0x30, 0x21, 0x5c, 0x35, 0x22,
|
||||
0x9a, 0x57, 0x30, 0xc7, 0xfc, 0xb4, 0xc9, 0xaf, 0x51, 0xff, 0xda, 0x89, 0xc7, 0xf1, 0xad, 0x22,
|
||||
0x04, 0x85, 0x05, 0x5f, 0xd4, 0xf6, 0xf0, 0xd9, 0x63, 0xef, 0x5a, 0xb9, 0xa5, 0x47, 0x69, 0x82,
|
||||
0x59, 0x1f, 0xc6, 0x6b, 0xcd, 0xa1, 0x0e, 0x45, 0x2b, 0x03, 0xd4, 0x55, 0x1f, 0x6b, 0x62, 0xac,
|
||||
0x27, 0x53, 0xcc, 0x83, 0x98, 0x8a, 0xfa, 0x3e, 0x16, 0x88, 0xa1, 0xd3, 0xb4, 0x2c, 0x9a, 0x02,
|
||||
0x93, 0x61, 0x0d, 0x52, 0x3d, 0x1d, 0x3f, 0x00, 0x62, 0xb3, 0xc2, 0xa3, 0xbb, 0xc7, 0xc7, 0xf0,
|
||||
0x96, 0xc2, 0x48, 0x61, 0x0a, 0xad, 0xed, 0xfe, 0xaf, 0x89, 0x78, 0xc0, 0x3d, 0xe8, 0x20, 0x5a,
|
||||
0x0e, 0x31, 0x7b, 0x3d, 0x1c, 0x73, 0xb9, 0xe9, 0xa4, 0x68, 0x8f, 0x29, 0x6d, 0x13, 0x3a, 0x19,
|
||||
0xbd, 0xf0, 0xe6, 0xc3, 0xcc, 0xa5, 0xb5, 0xb9, 0xd5, 0x33, 0xb6, 0x9c, 0x56, 0xad, 0xa1, 0x20,
|
||||
0x88, 0xa2, 0x18, 0xb6, 0xe2, 0xec, 0xe1, 0xe6, 0x24, 0x6d, 0x44, 0xc7, 0x59, 0xd1, 0x9b, 0x10,
|
||||
0x68, 0x66, 0x39, 0x7e, 0x95, 0xc1, 0x40, 0x53, 0x4f, 0x94, 0x26, 0x34, 0x21, 0x00, 0x6e, 0x40,
|
||||
0x32, 0xcb, 0x0a, 0x1e, 0x95, 0x42, 0xc6, 0xb3, 0xb8, 0xb3, 0x98, 0xab, 0xc3, 0xb0, 0xf1, 0xd5,
|
||||
0x29, 0xa0, 0xb8, 0xae, 0xd5, 0x4a, 0x13, 0x23, 0x24, 0xc6, 0x2e, 0x42, 0x3f, 0x54, 0xb4, 0xc8,
|
||||
0x3c, 0xb0, 0xf3, 0xb5, 0x02, 0x0a, 0x98, 0xb8, 0x2a, 0xf9, 0xfe, 0x15, 0x44, 0x84, 0xa1, 0x68,
|
||||
},
|
||||
{
|
||||
0xea, 0xa6, 0xbd, 0x25, 0x88, 0x0b, 0xf9, 0x3d, 0x3f, 0x5d, 0x1e, 0x4c, 0xa2, 0x61, 0x1d, 0x91,
|
||||
0xcf, 0xa4, 0x5c, 0x9f, 0x7e, 0x71, 0x4b, 0x54, 0xbd, 0xfa, 0x80, 0x02, 0x7c, 0xb1, 0x43, 0x80,
|
||||
0x11, 0x4a, 0xe3, 0x44, 0xde, 0xd7, 0x1b, 0x35, 0xf2, 0xe6, 0x0f, 0xeb, 0xad, 0x72, 0x7f, 0xd8,
|
||||
0x02, 0xe1, 0xe7, 0x05, 0x6b, 0x0f, 0x62, 0x39, 0x00, 0x49, 0x64, 0x22, 0x94, 0x3e, 0x97, 0xb6,
|
||||
0x91, 0xcb, 0x93, 0xc7, 0x87, 0x96, 0x4e, 0x10, 0xd9, 0x52, 0x7d, 0x99, 0x9c, 0x6f, 0x93, 0x6b,
|
||||
0x49, 0xb1, 0x8b, 0x42, 0xf8, 0xe8, 0x36, 0x7c, 0xbe, 0xb5, 0xef, 0x10, 0x4b, 0xa1, 0xc7, 0xcd,
|
||||
0x87, 0x08, 0x4b, 0x3b, 0xa7, 0x00, 0xba, 0xde, 0x95, 0x56, 0x10, 0x67, 0x27, 0x45, 0xb3, 0x74,
|
||||
0xe7, 0xa7, 0xb9, 0xe9, 0xec, 0x54, 0x0d, 0x5f, 0xf4, 0x3b, 0xdb, 0x12, 0x79, 0x2d, 0x1b, 0x35,
|
||||
0xc7, 0x99, 0xb5, 0x96, 0x73, 0x8f, 0x6b, 0x01, 0x8c, 0x76, 0xc7, 0x4b, 0x17, 0x59, 0xbd, 0x90,
|
||||
0x7f, 0xec, 0x5b, 0xfd, 0x9f, 0x9b, 0x89, 0xce, 0x65, 0x48, 0x30, 0x90, 0x92, 0xd7, 0xe9, 0x58,
|
||||
0x40, 0xf2, 0x50, 0xb2, 0x6d, 0x1f, 0x09, 0x6a, 0x4a, 0xfd, 0x4c, 0x34, 0x0a, 0x58, 0x88, 0x15,
|
||||
0x3e, 0x34, 0x13, 0x5c, 0x79, 0xdb, 0x01, 0x02, 0x00, 0x76, 0x76, 0x51, 0xcf, 0x26, 0x30, 0x73,
|
||||
0xf6, 0x56, 0xab, 0xcc, 0xf8, 0x8d, 0xd8, 0x27, 0x02, 0x7b, 0x2c, 0xe9, 0x17, 0xd4, 0x64, 0xec,
|
||||
0x18, 0xb6, 0x25, 0x03, 0xbf, 0xbc, 0x07, 0x7f, 0xba, 0xbb, 0x98, 0xf2, 0x0d, 0x98, 0xab, 0x34,
|
||||
0x8a, 0xed, 0x95, 0xee, 0x5b, 0x0d, 0xcb, 0xfb, 0xef, 0x4e, 0xb2, 0x1d, 0x3a, 0x3f, 0x52, 0xf9,
|
||||
0x62, 0x5a, 0x1a, 0xb0, 0x0e, 0xe3, 0x9a, 0x53, 0x27, 0x34, 0x6b, 0xdd, 0xb0, 0x1a, 0x9c, 0x18,
|
||||
0xa1, 0x3a, 0x7c, 0x79, 0xc7, 0xe1, 0x19, 0xb5, 0xab, 0x02, 0x96, 0xab, 0x28, 0xc3, 0x00, 0xb9,
|
||||
0xf3, 0xe4, 0xc0, 0xa2, 0xe0, 0x2d, 0x1d, 0x01, 0xf7, 0xf0, 0xa7, 0x46, 0x18, 0xaf, 0x2b, 0x48,
|
||||
},
|
||||
};
|
||||
|
||||
unsigned char testdata2[][16 * 18] = {
|
||||
{
|
||||
0x80, 0xad, 0x97, 0xbd, 0xc9, 0x73, 0xdf, 0x8a, 0x2e, 0x87, 0x9e, 0x92, 0xa4, 0x97, 0xef, 0xda,
|
||||
0x20, 0xf0, 0x60, 0xc2, 0xf2, 0xe5, 0x12, 0x65, 0x01, 0xd3, 0xd4, 0xfe, 0xa1, 0x0d, 0x5f, 0xc0,
|
||||
0xfa, 0xa1, 0x48, 0xe9, 0x90, 0x46, 0x18, 0x1f, 0xec, 0x6b, 0x20, 0x85, 0xf3, 0xb2, 0x0e, 0xd9,
|
||||
0xf0, 0xda, 0xf5, 0xba, 0xb3, 0xd5, 0x96, 0x83, 0x98, 0x57, 0x84, 0x6f, 0x73, 0xfb, 0xfe, 0x5a,
|
||||
0x1c, 0x7e, 0x2f, 0xc4, 0x63, 0x92, 0x32, 0xfe, 0x29, 0x75, 0x84, 0xb2, 0x96, 0x99, 0x6b, 0xc8,
|
||||
0x3d, 0xb9, 0xb2, 0x49, 0x40, 0x6c, 0xc8, 0xed, 0xff, 0xac, 0x55, 0xcc, 0xd3, 0x22, 0xba, 0x12,
|
||||
0xe4, 0xf9, 0xf7, 0xe0, 0x06, 0x61, 0x54, 0xbb, 0xd1, 0x25, 0xb7, 0x45, 0x56, 0x9b, 0xc8, 0x97,
|
||||
0x75, 0xd5, 0xef, 0x26, 0x2b, 0x44, 0xc4, 0x1a, 0x9c, 0xf6, 0x3a, 0xe1, 0x45, 0x68, 0xe1, 0xb9,
|
||||
0x6d, 0xa4, 0x53, 0xdb, 0xf8, 0x1e, 0x82, 0x33, 0x4a, 0x3d, 0x88, 0x66, 0xcb, 0x50, 0xa1, 0xe3,
|
||||
0x78, 0x28, 0xd0, 0x74, 0x11, 0x9c, 0xab, 0x5c, 0x22, 0xb2, 0x94, 0xd7, 0xa9, 0xbf, 0xa0, 0xbb,
|
||||
0xad, 0xb8, 0x9c, 0xea, 0x9a, 0x15, 0xfb, 0xe6, 0x17, 0x29, 0x5b, 0xd0, 0x4b, 0x8c, 0xa0, 0x5c,
|
||||
0x62, 0x51, 0xd8, 0x7f, 0xd4, 0xaa, 0xae, 0x9a, 0x7e, 0x4a, 0xd5, 0xc2, 0x17, 0xd3, 0xf3, 0x00,
|
||||
0xe7, 0x11, 0x9b, 0xd6, 0xdd, 0x9b, 0x22, 0xaf, 0xe8, 0xf8, 0x95, 0x85, 0x43, 0x28, 0x81, 0xe2,
|
||||
0x78, 0x5b, 0x60, 0xfd, 0x7e, 0xc4, 0xe9, 0xfc, 0xb6, 0x54, 0x5f, 0x35, 0x0d, 0x66, 0x0f, 0xab,
|
||||
0xaf, 0xec, 0xc0, 0x37, 0xfd, 0xb7, 0xb0, 0x83, 0x8e, 0xb3, 0xd7, 0x0b, 0xcd, 0x26, 0x83, 0x82,
|
||||
0xdb, 0xc1, 0xa7, 0xb4, 0x9d, 0x57, 0x35, 0x8c, 0xc9, 0xfa, 0x6d, 0x61, 0xd7, 0x3b, 0x7c, 0xf0,
|
||||
0x63, 0x49, 0xd1, 0x26, 0xa3, 0x7a, 0xfc, 0xba, 0x89, 0x79, 0x4f, 0x98, 0x04, 0x91, 0x4f, 0xdc,
|
||||
0xbf, 0x42, 0xc3, 0x01, 0x8c, 0x2f, 0x7c, 0x66, 0xbf, 0xde, 0x52, 0x49, 0x75, 0x76, 0x81, 0x15,
|
||||
},
|
||||
{
|
||||
0xbc, 0x92, 0x22, 0xdb, 0xd3, 0x27, 0x4d, 0x8f, 0xc6, 0x6d, 0x14, 0xcc, 0xbd, 0xa6, 0x69, 0x0b,
|
||||
0x7a, 0xe6, 0x27, 0x41, 0x0c, 0x9a, 0x2b, 0xe6, 0x93, 0xdf, 0x5b, 0xb7, 0x48, 0x5a, 0x63, 0xe3,
|
||||
0x3f, 0x09, 0x31, 0xaa, 0x03, 0xde, 0xfb, 0x30, 0x0f, 0x06, 0x01, 0x03, 0x82, 0x6f, 0x2a, 0x64,
|
||||
0xbe, 0xaa, 0x9e, 0xc8, 0xd5, 0x9b, 0xb6, 0x81, 0x29, 0xf3, 0x02, 0x7c, 0x96, 0x36, 0x11, 0x81,
|
||||
0x74, 0xe0, 0x4d, 0xb4, 0x6d, 0x28, 0x64, 0x8d, 0x7d, 0xee, 0x8a, 0x00, 0x64, 0xb0, 0x6c, 0xfe,
|
||||
0x9b, 0x5e, 0x81, 0xc6, 0x2f, 0xe0, 0x23, 0xc5, 0x5b, 0xe4, 0x2f, 0x87, 0xbb, 0xf9, 0x32, 0xb8,
|
||||
0xce, 0x17, 0x8f, 0xc1, 0x82, 0x6e, 0xfe, 0xcb, 0xc1, 0x82, 0xf5, 0x79, 0x99, 0xa4, 0x61, 0x40,
|
||||
0x8b, 0xdf, 0x55, 0xcd, 0x55, 0x06, 0x1c, 0x06, 0xdb, 0xa6, 0xbe, 0x11, 0xde, 0x4a, 0x57, 0x8a,
|
||||
0x62, 0x6f, 0x5f, 0x4d, 0xce, 0x65, 0x25, 0x01, 0xf3, 0x08, 0x7d, 0x39, 0xc9, 0x2c, 0xc3, 0x49,
|
||||
0x42, 0xda, 0xac, 0x6a, 0x8f, 0x9a, 0xb9, 0xa7, 0xfd, 0x13, 0x7c, 0x60, 0x37, 0x82, 0x56, 0x82,
|
||||
0xcc, 0x03, 0xfd, 0xb7, 0x91, 0x92, 0xa2, 0x07, 0x31, 0x2f, 0x53, 0xf5, 0xd4, 0xdc, 0x33, 0xd9,
|
||||
0xf7, 0x0f, 0x14, 0x12, 0x2a, 0x1c, 0x98, 0xa3, 0x15, 0x5d, 0x28, 0xb8, 0xa0, 0xa8, 0xa4, 0x1d,
|
||||
0x2a, 0x3a, 0x30, 0x7a, 0xb2, 0x70, 0x8a, 0x9c, 0x00, 0xfe, 0x0b, 0x42, 0xf9, 0xc2, 0xd6, 0xa1,
|
||||
0x86, 0x26, 0x17, 0x62, 0x7d, 0x22, 0x61, 0xea, 0xb0, 0xb1, 0x24, 0x65, 0x97, 0xca, 0x0a, 0xe9,
|
||||
0x55, 0xf8, 0x77, 0xce, 0x4f, 0x2e, 0x1d, 0xdb, 0xbf, 0x8e, 0x13, 0xe2, 0xcd, 0xe0, 0xfd, 0xc8,
|
||||
0x1b, 0x15, 0x56, 0xcb, 0x93, 0x5f, 0x17, 0x33, 0x37, 0x70, 0x5f, 0xbb, 0x5d, 0x50, 0x1f, 0xc1,
|
||||
0xec, 0xd0, 0xe9, 0x66, 0x02, 0xbe, 0x7f, 0x8d, 0x50, 0x92, 0x81, 0x6c, 0xcc, 0xf2, 0xc2, 0xe9,
|
||||
0x02, 0x78, 0x81, 0xfa, 0xb4, 0x99, 0x3a, 0x1c, 0x26, 0x20, 0x24, 0xa9, 0x4f, 0xff, 0x3f, 0x61,
|
||||
},
|
||||
{
|
||||
0xbb, 0xf6, 0x09, 0xde, 0x94, 0x13, 0x17, 0x2d, 0x07, 0x66, 0x0c, 0xb6, 0x80, 0x71, 0x69, 0x26,
|
||||
0x46, 0x10, 0x1a, 0x6d, 0xab, 0x43, 0x11, 0x5d, 0x6c, 0x52, 0x2b, 0x4f, 0xe9, 0x36, 0x04, 0xa9,
|
||||
0xcb, 0xe1, 0xff, 0xf2, 0x1c, 0x96, 0xf3, 0xee, 0xf6, 0x1e, 0x8f, 0xe0, 0x54, 0x2c, 0xbd, 0xf0,
|
||||
0x34, 0x79, 0x38, 0xbf, 0xfa, 0x40, 0x09, 0xc5, 0x12, 0xcf, 0xb4, 0x03, 0x4b, 0x0d, 0xd1, 0xa7,
|
||||
0x78, 0x67, 0xa7, 0x86, 0xd0, 0x0a, 0x71, 0x47, 0x90, 0x4d, 0x76, 0xdd, 0xf1, 0xe5, 0x20, 0xe3,
|
||||
0x8d, 0x3e, 0x9e, 0x1c, 0xae, 0xfc, 0xcc, 0xb3, 0xfb, 0xf8, 0xd1, 0x8f, 0x64, 0x12, 0x0b, 0x32,
|
||||
0x94, 0x23, 0x37, 0xf8, 0xfd, 0x76, 0xf0, 0xfa, 0xe8, 0xc5, 0x2d, 0x79, 0x54, 0x81, 0x06, 0x72,
|
||||
0xb8, 0x54, 0x8c, 0x10, 0xf5, 0x16, 0x67, 0xf6, 0xe6, 0x0e, 0x18, 0x2f, 0xa1, 0x9b, 0x30, 0xf7,
|
||||
0x02, 0x11, 0xc7, 0xc6, 0x19, 0x0c, 0x9e, 0xfd, 0x12, 0x37, 0xc3, 0x4c, 0x8f, 0x2e, 0x06, 0xc4,
|
||||
0xbd, 0xa6, 0x4f, 0x65, 0x27, 0x6d, 0x2a, 0xac, 0xb8, 0xf9, 0x02, 0x12, 0x20, 0x3a, 0x80, 0x8e,
|
||||
0xbd, 0x38, 0x20, 0xf7, 0x32, 0xff, 0xb5, 0x3e, 0xc1, 0x93, 0xe7, 0x9d, 0x33, 0xe2, 0x7c, 0x73,
|
||||
0xd0, 0x16, 0x86, 0x16, 0x86, 0x19, 0x07, 0xd4, 0x82, 0xe3, 0x6c, 0xda, 0xc8, 0xcf, 0x57, 0x49,
|
||||
0x97, 0xb0, 0xf0, 0xf2, 0x24, 0xb2, 0xd2, 0x31, 0x71, 0x14, 0x80, 0x8f, 0xb0, 0x3a, 0xf7, 0xa0,
|
||||
0xe5, 0x96, 0x16, 0xe4, 0x69, 0x78, 0x79, 0x39, 0xa0, 0x63, 0xce, 0xea, 0x9a, 0xf9, 0x56, 0xd1,
|
||||
0xc4, 0x7e, 0x0d, 0xc1, 0x66, 0x09, 0x19, 0xc1, 0x11, 0x01, 0x20, 0x8f, 0x9e, 0x69, 0xaa, 0x1f,
|
||||
0x5a, 0xe4, 0xf1, 0x28, 0x96, 0xb8, 0x37, 0x9a, 0x2a, 0xad, 0x89, 0xb5, 0xb5, 0x53, 0xd6, 0xb0,
|
||||
0x6b, 0x6b, 0x09, 0x8d, 0x0c, 0x29, 0x3b, 0xc2, 0x99, 0x3d, 0x80, 0xbf, 0x05, 0x18, 0xb6, 0xd9,
|
||||
0x81, 0x70, 0xcc, 0x3c, 0xcd, 0x92, 0xa6, 0x98, 0x62, 0x1b, 0x93, 0x9d, 0xd3, 0x8f, 0xe7, 0xb9,
|
||||
},
|
||||
{
|
||||
0xab, 0x65, 0xc2, 0x6e, 0xdd, 0xb2, 0x87, 0x60, 0x0d, 0xb2, 0xfd, 0xa1, 0x0d, 0x1e, 0x60, 0x5c,
|
||||
0xbb, 0x75, 0x90, 0x10, 0xc2, 0x96, 0x58, 0xf2, 0xc7, 0x2d, 0x93, 0xa2, 0xd1, 0x6d, 0x29, 0x30,
|
||||
0xb9, 0x01, 0xe8, 0x03, 0x6e, 0xd1, 0xc3, 0x83, 0xcd, 0x3c, 0x4c, 0x4d, 0xd0, 0xa6, 0xab, 0x05,
|
||||
0x3d, 0x25, 0xce, 0x49, 0x22, 0x92, 0x4c, 0x55, 0xf0, 0x64, 0x94, 0x33, 0x53, 0xd7, 0x8a, 0x6c,
|
||||
0x12, 0xc1, 0xaa, 0x44, 0xbb, 0xf8, 0x7e, 0x75, 0xe6, 0x11, 0xf6, 0x9b, 0x2c, 0x38, 0xf4, 0x9b,
|
||||
0x28, 0xf2, 0xb3, 0x43, 0x4b, 0x65, 0xc0, 0x98, 0x77, 0x47, 0x00, 0x44, 0xc6, 0xea, 0x17, 0x0d,
|
||||
0xbd, 0x9e, 0xf8, 0x22, 0xde, 0x52, 0x88, 0x19, 0x61, 0x34, 0xcf, 0x8a, 0xf7, 0x83, 0x93, 0x04,
|
||||
0x67, 0x55, 0x9c, 0x23, 0xf0, 0x52, 0x15, 0x84, 0x70, 0xa2, 0x96, 0xf7, 0x25, 0x73, 0x5a, 0x32,
|
||||
0x8b, 0xab, 0x26, 0xfb, 0xc2, 0xc1, 0x2b, 0x0f, 0x13, 0xe2, 0xab, 0x18, 0x5e, 0xab, 0xf2, 0x41,
|
||||
0x31, 0x18, 0x5a, 0x6d, 0x69, 0x6f, 0x0c, 0xfa, 0x9b, 0x42, 0x80, 0x8b, 0x38, 0xe1, 0x32, 0xa2,
|
||||
0x56, 0x4d, 0x3d, 0xae, 0x18, 0x3c, 0x52, 0x34, 0xc8, 0xaf, 0x1e, 0x51, 0x06, 0x1c, 0x44, 0xb5,
|
||||
0x3c, 0x07, 0x78, 0xa7, 0xb5, 0xf7, 0x2d, 0x3c, 0x23, 0xa3, 0x13, 0x5c, 0x7d, 0x67, 0xb9, 0xf4,
|
||||
0xf3, 0x43, 0x69, 0x89, 0x0f, 0xcf, 0x16, 0xfb, 0x51, 0x7d, 0xca, 0xae, 0x44, 0x63, 0xb2, 0xdd,
|
||||
0x02, 0xf3, 0x1c, 0x81, 0xe8, 0x20, 0x07, 0x31, 0xb8, 0x99, 0xb0, 0x28, 0xe7, 0x91, 0xbf, 0xa7,
|
||||
0x72, 0xda, 0x64, 0x62, 0x83, 0x22, 0x8c, 0x14, 0x30, 0x08, 0x53, 0x70, 0x17, 0x95, 0x61, 0x6f,
|
||||
0x4e, 0x0a, 0x8c, 0x6f, 0x79, 0x34, 0xa7, 0x88, 0xe2, 0x26, 0x5e, 0x81, 0xd6, 0xd0, 0xc8, 0xf4,
|
||||
0x43, 0x8d, 0xd5, 0xea, 0xfe, 0xa0, 0x11, 0x1b, 0x6f, 0x36, 0xb4, 0xb9, 0x38, 0xda, 0x2a, 0x68,
|
||||
0x5f, 0x6b, 0xfc, 0x73, 0x81, 0x58, 0x74, 0xd9, 0x71, 0x00, 0xf0, 0x86, 0x97, 0x93, 0x57, 0xd8,
|
||||
},
|
||||
{
|
||||
0x72, 0x0c, 0x94, 0xb6, 0x3e, 0xdf, 0x44, 0xe1, 0x31, 0xd9, 0x50, 0xca, 0x21, 0x1a, 0x5a, 0x30,
|
||||
0xc3, 0x66, 0xfd, 0xea, 0xcf, 0x9c, 0xa8, 0x04, 0x36, 0xbe, 0x7c, 0x35, 0x84, 0x24, 0xd2, 0x0b,
|
||||
0xb3, 0x39, 0x4a, 0x40, 0xaa, 0xbf, 0x75, 0xcb, 0xa4, 0x22, 0x82, 0xef, 0x25, 0xa0, 0x05, 0x9f,
|
||||
0x48, 0x47, 0xd8, 0x1d, 0xa4, 0x94, 0x2d, 0xbc, 0x24, 0x9d, 0xef, 0xc4, 0x8c, 0x92, 0x2b, 0x9f,
|
||||
0x08, 0x12, 0x8c, 0x46, 0x9f, 0x27, 0x53, 0x42, 0xad, 0xda, 0x20, 0x2b, 0x2b, 0x58, 0xda, 0x95,
|
||||
0x97, 0x0d, 0xac, 0xef, 0x40, 0xad, 0x98, 0x72, 0x3b, 0xac, 0x5d, 0x69, 0x55, 0xb8, 0x17, 0x61,
|
||||
0x3c, 0xb8, 0x99, 0x93, 0xb0, 0x7b, 0x0c, 0xed, 0x93, 0xde, 0x13, 0xd2, 0xa1, 0x10, 0x13, 0xac,
|
||||
0xef, 0x2d, 0x67, 0x6f, 0x15, 0x45, 0xc2, 0xc1, 0x3d, 0xc6, 0x80, 0xa0, 0x2f, 0x4a, 0xdb, 0xfe,
|
||||
0xb6, 0x05, 0x95, 0x51, 0x4f, 0x24, 0xbc, 0x9f, 0xe5, 0x22, 0xa6, 0xca, 0xd7, 0x39, 0x36, 0x44,
|
||||
0xb5, 0x15, 0xa8, 0xc5, 0x01, 0x17, 0x54, 0xf5, 0x90, 0x03, 0x05, 0x8b, 0xdb, 0x81, 0x51, 0x4e,
|
||||
0x3c, 0x70, 0x04, 0x7e, 0x8c, 0xbc, 0x03, 0x8e, 0x3b, 0x98, 0x20, 0xdb, 0x60, 0x1d, 0xa4, 0x95,
|
||||
0x11, 0x75, 0xda, 0x6e, 0xe7, 0x56, 0xde, 0x46, 0xa5, 0x3e, 0x2b, 0x07, 0x56, 0x60, 0xb7, 0x70,
|
||||
0x00, 0xa5, 0x42, 0xbb, 0xa0, 0x21, 0x11, 0xcc, 0x2c, 0x65, 0xb3, 0x8e, 0xbd, 0xba, 0x58, 0x7e,
|
||||
0x58, 0x65, 0xfd, 0xbb, 0x5b, 0x48, 0x06, 0x41, 0x04, 0xe8, 0x30, 0xb3, 0x80, 0xf2, 0xae, 0xde,
|
||||
0x34, 0xb2, 0x1a, 0xd2, 0xad, 0x44, 0xe9, 0x99, 0xdb, 0x2d, 0x7f, 0x08, 0x63, 0xf0, 0xd9, 0xb6,
|
||||
0x84, 0xa9, 0x21, 0x8f, 0xc3, 0x6e, 0x8a, 0x5f, 0x2c, 0xcf, 0xbe, 0xae, 0x53, 0xa2, 0x7d, 0x25,
|
||||
0xa2, 0x22, 0x1a, 0x11, 0xb8, 0x33, 0xcc, 0xb4, 0x98, 0xa5, 0x95, 0x40, 0xf0, 0x54, 0x5f, 0x4a,
|
||||
0x5b, 0xbe, 0xb4, 0x78, 0x7d, 0x59, 0xe5, 0x37, 0x3f, 0xdb, 0xea, 0x6c, 0x6f, 0x75, 0xc2, 0x9b,
|
||||
},
|
||||
{
|
||||
0x54, 0xb6, 0x4e, 0x6b, 0x5a, 0x20, 0xb5, 0xe2, 0xec, 0x84, 0x59, 0x3d, 0xc7, 0x98, 0x9d, 0xa7,
|
||||
0xc1, 0x35, 0xee, 0xe2, 0x37, 0xa8, 0x54, 0x65, 0xff, 0x97, 0xdc, 0x03, 0x92, 0x4f, 0x45, 0xce,
|
||||
0xcf, 0xcc, 0x92, 0x2f, 0xb4, 0xa1, 0x4a, 0xb4, 0x5d, 0x61, 0x75, 0xaa, 0xbb, 0xf2, 0xd2, 0x01,
|
||||
0x83, 0x7b, 0x87, 0xe2, 0xa4, 0x46, 0xad, 0x0e, 0xf7, 0x98, 0xac, 0xd0, 0x2b, 0x94, 0x12, 0x4f,
|
||||
0x17, 0xa6, 0xdb, 0xd6, 0x64, 0x92, 0x6a, 0x06, 0x36, 0xb3, 0xf4, 0xc3, 0x7a, 0x4f, 0x46, 0x94,
|
||||
0x4a, 0x5f, 0x9f, 0x26, 0xae, 0xee, 0xd4, 0xd4, 0xa2, 0x5f, 0x63, 0x2d, 0x30, 0x52, 0x33, 0xd9,
|
||||
0x80, 0xa3, 0xd0, 0x1e, 0xf0, 0x0c, 0x8e, 0x9a, 0x42, 0x09, 0xc1, 0x7f, 0x4e, 0xeb, 0x35, 0x8c,
|
||||
0xd1, 0x5e, 0x7d, 0x5f, 0xfa, 0xaa, 0xbc, 0x02, 0x07, 0xbf, 0x20, 0x0a, 0x11, 0x77, 0x93, 0xa2,
|
||||
0x34, 0x96, 0x82, 0xbf, 0x58, 0x8e, 0xaa, 0x52, 0xd0, 0xaa, 0x15, 0x60, 0x34, 0x6a, 0xea, 0xfa,
|
||||
0xf5, 0x85, 0x4c, 0xdb, 0x76, 0xc8, 0x89, 0xe3, 0xad, 0x63, 0x35, 0x4e, 0x5f, 0x72, 0x75, 0xe3,
|
||||
0x53, 0x2c, 0x7c, 0xec, 0xcb, 0x39, 0xdf, 0x32, 0x36, 0x31, 0x84, 0x05, 0xa4, 0xb1, 0x27, 0x9c,
|
||||
0xba, 0xef, 0xe6, 0xd9, 0xce, 0xb6, 0x51, 0x84, 0x22, 0x60, 0xe0, 0xd1, 0xe0, 0x5e, 0x3b, 0x90,
|
||||
0xe8, 0x2d, 0x8c, 0x6d, 0xb5, 0x4e, 0x3c, 0x63, 0x3f, 0x58, 0x1c, 0x95, 0x2b, 0xa0, 0x42, 0x07,
|
||||
0x4b, 0x16, 0xe5, 0x0a, 0xbd, 0x38, 0x1b, 0xd7, 0x09, 0x00, 0xa9, 0xcd, 0x9a, 0x62, 0xcb, 0x23,
|
||||
0x36, 0x82, 0xee, 0x33, 0xbd, 0x14, 0x8b, 0xd9, 0xf5, 0x86, 0x56, 0xcd, 0x8f, 0x30, 0xd9, 0xfb,
|
||||
0x1e, 0x5a, 0x0b, 0x84, 0x75, 0x04, 0x5d, 0x9b, 0x20, 0xb2, 0x62, 0x86, 0x24, 0xed, 0xfd, 0x9e,
|
||||
0x63, 0xed, 0xd6, 0x84, 0xfb, 0x82, 0x62, 0x82, 0xfe, 0x52, 0x8f, 0x9c, 0x0e, 0x92, 0x37, 0xbc,
|
||||
0xe4, 0xdd, 0x2e, 0x98, 0xd6, 0x96, 0x0f, 0xae, 0x0b, 0x43, 0x54, 0x54, 0x56, 0x74, 0x33, 0x91,
|
||||
},
|
||||
{
|
||||
0xdd, 0x5b, 0xcb, 0x00, 0x18, 0xe9, 0x22, 0xd4, 0x94, 0x75, 0x9d, 0x7c, 0x39, 0x5d, 0x02, 0xd3,
|
||||
0xc8, 0x44, 0x6f, 0x8f, 0x77, 0xab, 0xf7, 0x37, 0x68, 0x53, 0x53, 0xeb, 0x89, 0xa1, 0xc9, 0xeb,
|
||||
0xaf, 0x3e, 0x30, 0xf9, 0xc0, 0x95, 0x04, 0x59, 0x38, 0x15, 0x15, 0x75, 0xc3, 0xfb, 0x90, 0x98,
|
||||
0xf8, 0xcb, 0x62, 0x74, 0xdb, 0x99, 0xb8, 0x0b, 0x1d, 0x20, 0x12, 0xa9, 0x8e, 0xd4, 0x8f, 0x0e,
|
||||
0x25, 0xc3, 0x00, 0x5a, 0x1c, 0xb8, 0x5d, 0xe0, 0x76, 0x25, 0x98, 0x39, 0xab, 0x71, 0x98, 0xab,
|
||||
0x9d, 0xcb, 0xc1, 0x83, 0xe8, 0xcb, 0x99, 0x4b, 0x72, 0x7b, 0x75, 0xbe, 0x31, 0x80, 0x76, 0x9c,
|
||||
0xa1, 0xd3, 0x07, 0x8d, 0xfa, 0x91, 0x69, 0x50, 0x3e, 0xd9, 0xd4, 0x49, 0x1d, 0xee, 0x4e, 0xb2,
|
||||
0x85, 0x14, 0xa5, 0x49, 0x58, 0x58, 0x09, 0x6f, 0x59, 0x6e, 0x4b, 0xcd, 0x66, 0xb1, 0x06, 0x65,
|
||||
0x5f, 0x40, 0xd5, 0x9e, 0xc1, 0xb0, 0x3b, 0x33, 0x73, 0x8e, 0xfa, 0x60, 0xb2, 0x25, 0x5d, 0x31,
|
||||
0x34, 0x77, 0xc7, 0xf7, 0x64, 0xa4, 0x1b, 0xac, 0xef, 0xf9, 0x0b, 0xf1, 0x4f, 0x92, 0xb7, 0xcc,
|
||||
0xac, 0x4e, 0x95, 0x36, 0x8d, 0x99, 0xb9, 0xeb, 0x78, 0xb8, 0xda, 0x8f, 0x81, 0xff, 0xa7, 0x95,
|
||||
0x8c, 0x3c, 0x13, 0xf8, 0xc2, 0x38, 0x8b, 0xb7, 0x3f, 0x38, 0x57, 0x6e, 0x65, 0xb7, 0xc4, 0x46,
|
||||
0x13, 0xc4, 0xb9, 0xc1, 0xdf, 0xb6, 0x65, 0x79, 0xed, 0xdd, 0x8a, 0x28, 0x0b, 0x9f, 0x73, 0x16,
|
||||
0xdd, 0xd2, 0x78, 0x20, 0x55, 0x01, 0x26, 0x69, 0x8e, 0xfa, 0xad, 0xc6, 0x4b, 0x64, 0xf6, 0x6e,
|
||||
0xf0, 0x8f, 0x2e, 0x66, 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_set_key(&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_set_key(&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;
|
||||
}
|
||||
116
tests/sha1test.c
Normal file
116
tests/sha1test.c
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.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;
|
||||
unsigned char dgst[20];
|
||||
unsigned char *dgstbuf = NULL;
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) {
|
||||
|
||||
if (!(dgstbuf = OPENSSL_hexstr2buf(dgsthex[i], NULL))) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
sha1_init(&ctx);
|
||||
for (j = 0; j < testcnt[i]; j++) {
|
||||
sha1_update(&ctx, (unsigned char *)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);
|
||||
}
|
||||
|
||||
free(dgstbuf);
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
138
tests/sha224test.c
Normal file
138
tests/sha224test.c
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.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;
|
||||
unsigned char dgst[SHA224_DIGEST_SIZE];
|
||||
unsigned char *dgstbuf = NULL;
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < 7; i++) {
|
||||
|
||||
if (!(dgstbuf = OPENSSL_hexstr2buf(tests[i].dgsthex, NULL))) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
sha224_init(&ctx);
|
||||
for (j = 0; j < tests[i].count; j++) {
|
||||
sha224_update(&ctx, (unsigned char *)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);
|
||||
}
|
||||
|
||||
free(dgstbuf);
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
137
tests/sha256test.c
Normal file
137
tests/sha256test.c
Normal file
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.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;
|
||||
unsigned char dgst[SHA256_DIGEST_SIZE];
|
||||
unsigned char *dgstbuf = NULL;
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < 7; i++) {
|
||||
|
||||
if (!(dgstbuf = OPENSSL_hexstr2buf(tests[i].dgsthex, NULL))) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
sha256_init(&ctx);
|
||||
for (j = 0; j < tests[i].count; j++) {
|
||||
sha256_update(&ctx, (unsigned char *)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);
|
||||
}
|
||||
|
||||
free(dgstbuf);
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
143
tests/sha384test.c
Normal file
143
tests/sha384test.c
Normal file
@@ -0,0 +1,143 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.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;
|
||||
unsigned char dgst[SHA384_DIGEST_SIZE];
|
||||
unsigned char *dgstbuf = NULL;
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < 7; i++) {
|
||||
|
||||
if (!(dgstbuf = OPENSSL_hexstr2buf(tests[i].dgsthex, NULL))) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
sha384_init(&ctx);
|
||||
for (j = 0; j < tests[i].count; j++) {
|
||||
sha384_update(&ctx, (unsigned char *)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);
|
||||
}
|
||||
|
||||
free(dgstbuf);
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
144
tests/sha512test.c
Normal file
144
tests/sha512test.c
Normal file
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.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;
|
||||
unsigned char dgst[SHA512_DIGEST_SIZE];
|
||||
unsigned char *dgstbuf = NULL;
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < 7; i++) {
|
||||
|
||||
if (!(dgstbuf = OPENSSL_hexstr2buf(tests[i].dgsthex, NULL))) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
sha512_init(&ctx);
|
||||
for (j = 0; j < tests[i].count; j++) {
|
||||
sha512_update(&ctx, (unsigned char *)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);
|
||||
}
|
||||
|
||||
free(dgstbuf);
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
180
tests/sm2asn1test.c
Normal file
180
tests/sm2asn1test.c
Normal file
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/oid.h>
|
||||
#include <gmssl/sm2.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
static int test_sm2_point_octets(void)
|
||||
{
|
||||
int err = 0;
|
||||
SM2_KEY sm2_key;
|
||||
SM2_POINT point;
|
||||
uint8_t buf[65];
|
||||
int i;
|
||||
|
||||
// compress
|
||||
for (i = 0; i < 8; i++) {
|
||||
uint8_t buf[33];
|
||||
sm2_keygen(&sm2_key);
|
||||
sm2_point_to_compressed_octets(&sm2_key.public_key, buf);
|
||||
if (sm2_point_from_octets(&point, buf, sizeof(buf)) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
break;
|
||||
}
|
||||
if (memcmp(&sm2_key.public_key, &point, sizeof(SM2_POINT)) != 0) {
|
||||
error_print();
|
||||
err++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// uncompress
|
||||
for (i = 0; i < 8; i++) {
|
||||
uint8_t buf[65];
|
||||
sm2_keygen(&sm2_key);
|
||||
sm2_point_to_uncompressed_octets(&sm2_key.public_key, buf);
|
||||
if (sm2_point_from_octets(&point, buf, sizeof(buf)) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
break;
|
||||
}
|
||||
if (memcmp(&sm2_key.public_key, &point, sizeof(SM2_POINT)) != 0) {
|
||||
error_print();
|
||||
err++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("%s : %s\n", __func__, err ? "failed" : "ok");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_sm2_private_key(void)
|
||||
{
|
||||
int err = 0;
|
||||
SM2_KEY sm2_key;
|
||||
SM2_KEY sm2_tmp;
|
||||
uint8_t buf[256];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
|
||||
sm2_keygen(&sm2_key);
|
||||
|
||||
if (sm2_private_key_to_der(&sm2_key, &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
if (sm2_private_key_from_der(&sm2_tmp, &cp, &len) != 1
|
||||
|| len > 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
if (memcmp(&sm2_tmp, &sm2_key, sizeof(SM2_KEY)) != 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
|
||||
printf("%s : ok\n", __func__);
|
||||
end:
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_sm2_public_key_info(void)
|
||||
{
|
||||
int err = 0;
|
||||
SM2_KEY sm2_key;
|
||||
SM2_KEY sm2_tmp;
|
||||
uint8_t buf[256];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
sm2_keygen(&sm2_key);
|
||||
|
||||
if (sm2_public_key_info_to_der(&sm2_key, &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
if (sm2_public_key_info_from_der(&sm2_tmp, &cp, &len) != 1
|
||||
|| len > 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
if (memcmp(&sm2_key.public_key, &sm2_tmp.public_key, sizeof(SM2_POINT)) != 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
printf("%s : ok\n", __func__);
|
||||
end:
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_sm2_point_octets();
|
||||
test_sm2_private_key();
|
||||
test_sm2_public_key_info();
|
||||
return 0;
|
||||
}
|
||||
|
||||
163
tests/sm2test.c
Normal file
163
tests/sm2test.c
Normal file
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm2.h>
|
||||
|
||||
|
||||
// SM2还需要大量的测试覆盖
|
||||
/*
|
||||
void sm2_point_to_compressed_octets(const SM2_POINT *P, uint8_t out[33]);
|
||||
void sm2_point_to_uncompressed_octets(const SM2_POINT *P, uint8_t out[65]);
|
||||
int sm2_point_from_octets(SM2_POINT *P, const uint8_t *in, size_t inlen);
|
||||
int sm2_point_from_x(SM2_POINT *P, const uint8_t x[32]);
|
||||
int sm2_point_from_xy(SM2_POINT *P, const uint8_t x[32], const uint8_t y[32]);
|
||||
int sm2_point_is_on_curve(const SM2_POINT *P);
|
||||
*/
|
||||
|
||||
static int test_sm2_point(void)
|
||||
{
|
||||
SM2_POINT P;
|
||||
uint8_t k[32] = {0};
|
||||
uint8_t buf[65] = {0};
|
||||
int i;
|
||||
|
||||
k[31] = 2;
|
||||
|
||||
printf("k = "); for (i = 0; i < 32; i++) printf("%02x", k[i]); printf("\n");
|
||||
|
||||
sm2_point_mul_generator(&P, k);
|
||||
|
||||
printf("k * G :\n");
|
||||
sm2_point_print(stdout, &P, 0, 2);
|
||||
|
||||
sm2_point_to_compressed_octets(&P, buf);
|
||||
for (i = 0; i < 33; i++) printf("%02x", buf[i]); printf("\n");
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
sm2_point_to_uncompressed_octets(&P, buf);
|
||||
for (i = 0; i < 65; i++) printf("%02x", buf[i]); printf("\n");
|
||||
|
||||
memset(&P, 0, sizeof(SM2_POINT));
|
||||
/*
|
||||
i = sm2_point_from_x(&P, buf + 1);
|
||||
printf("sm2_point_from_x: %d\n", i);
|
||||
*/
|
||||
|
||||
sm2_point_from_octets(&P, buf, 65);
|
||||
|
||||
i = sm2_point_is_on_curve(&P);
|
||||
printf("point_is_on_curve: %d\n", i);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
static int test_sm2_do_encrypt(void)
|
||||
{
|
||||
SM2_KEY key;
|
||||
uint8_t plaintext[] = "Hello World!";
|
||||
uint8_t cipherbuf[SM2_CIPHERTEXT_SIZE(sizeof(plaintext))] = {0};
|
||||
SM2_CIPHERTEXT *ciphertext = (SM2_CIPHERTEXT *)cipherbuf;
|
||||
uint8_t plainbuf[sizeof(cipherbuf)] = {0};
|
||||
size_t plainlen = 0;
|
||||
int r = 0;
|
||||
|
||||
sm2_keygen(&key);
|
||||
|
||||
sm2_do_encrypt(&key, plaintext, sizeof(plaintext), ciphertext);
|
||||
|
||||
printf("ciphertext:\n");
|
||||
sm2_ciphertext_print(stdout, ciphertext, 0, 2);
|
||||
|
||||
sm2_do_decrypt(&key, ciphertext, plainbuf, &plainlen);
|
||||
|
||||
printf("plaintext = %s\n", (char *)plainbuf);
|
||||
return r;
|
||||
}
|
||||
|
||||
static int test_sm2_sign(void)
|
||||
{
|
||||
SM2_KEY key;
|
||||
SM2_SIGN_CTX ctx;
|
||||
uint8_t msg[] = "Hello World!";
|
||||
uint8_t sig[128] = {0};
|
||||
size_t siglen = sizeof(sig);
|
||||
int i;
|
||||
int r;
|
||||
|
||||
sm2_keygen(&key);
|
||||
sm2_key_print(stdout, &key, 0, 0);
|
||||
|
||||
sm2_sign_init(&ctx, &key, SM2_DEFAULT_ID);
|
||||
sm2_sign_update(&ctx, msg, sizeof(msg));
|
||||
sm2_sign_finish(&ctx, sig, &siglen);
|
||||
|
||||
printf("signature:\n");
|
||||
sm2_print_signature(stdout, sig, siglen, 0, 2);
|
||||
|
||||
sm2_verify_init(&ctx, &key, SM2_DEFAULT_ID);
|
||||
sm2_verify_update(&ctx, msg, sizeof(msg));
|
||||
r = sm2_verify_finish(&ctx, sig, siglen);
|
||||
printf("verify %s\n", r > 0 ? "success" : "failed");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
sm2_algo_selftest();
|
||||
|
||||
//test_sm2_point();
|
||||
//test_sm2_sign();
|
||||
test_sm2_do_encrypt();
|
||||
|
||||
return 0;
|
||||
}
|
||||
219
tests/sm3test.c
Normal file
219
tests/sm3test.c
Normal file
@@ -0,0 +1,219 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2017 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/hex.h>
|
||||
|
||||
static 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",
|
||||
};
|
||||
|
||||
static char *dgsthex[] = {
|
||||
"66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0",
|
||||
"debe9ff92275b8a138604889c18e5a4d6fdb70e5387e5765293dcba39c0c5732",
|
||||
"F4A38489E32B45B6F876E3AC2168CA392362DC8F23459C1D1146FC3DBFB7BC9A",
|
||||
"26352AF82EC19F207BBC6F9474E11E90CE0F7DDACE03B27F801817E897A81FD5",
|
||||
"E4D1D0C3CA4C7F11BC8FF8CB3F4C02A78F108FA098E51A668487240F75E20F31",
|
||||
"6B4B6D0E276691BD4A11BF72F4FB501AE309FDACB72FA6CC336E6656119ABD67",
|
||||
"329c2f6030cc7e0ca3af6c97b76243ca250338ad3d3dc3a8b322d1cfdf98c2b7",
|
||||
/*"ECF0080215977B2E5D6D61B98A99442F03E8803DC39E349F8DCA5621A9ACDF2B",*/
|
||||
"557BAD30E183559AEEC3B2256E1C7C11F870D22B165D015ACF9465B09B87B527",
|
||||
"4D38D2958CA7FD2CFAE3AF04486959CF92C8EF48E8B83A05C112E739D5F181D0",
|
||||
"C3B02E500A8B60B77DEDCF6F4C11BEF8D56E5CDE708C72065654FD7B2167915A",
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int err = 0;
|
||||
char *p;
|
||||
unsigned char *testbuf = NULL;
|
||||
unsigned char *dgstbuf = NULL;
|
||||
size_t testbuflen, dgstbuflen;
|
||||
unsigned char dgst[32];
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < sizeof(testhex)/sizeof(testhex[0]); i++) {
|
||||
if (!(testbuf = OPENSSL_hexstr2buf(testhex[i], &testbuflen))
|
||||
|| !(dgstbuf = OPENSSL_hexstr2buf(dgsthex[i], &dgstbuflen))) {
|
||||
goto end;
|
||||
}
|
||||
|
||||
sm3_digest(testbuf, testbuflen, dgst);
|
||||
|
||||
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
|
||||
int n;
|
||||
printf("error calculating SM3 on %s\n", testhex[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("sm3 test %lu ok\n", i+1);
|
||||
}
|
||||
|
||||
free(testbuf);
|
||||
free(dgstbuf);
|
||||
testbuf = NULL;
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (testbuf) free(testbuf);
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
113
tests/sm4cbctest.c
Normal file
113
tests/sm4cbctest.c
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm4.h>
|
||||
#include <gmssl/rand.h>
|
||||
|
||||
static int test_sm4_cbc(void)
|
||||
{
|
||||
SM4_KEY sm4_key;
|
||||
uint8_t key[16] = {0};
|
||||
uint8_t iv[16];
|
||||
|
||||
uint8_t buf1[2] = {0};
|
||||
uint8_t buf2[32] = {0};
|
||||
uint8_t buf3[47] = {0};
|
||||
uint8_t buf4[96] = {0};
|
||||
uint8_t buf5[96];
|
||||
int i;
|
||||
|
||||
sm4_set_encrypt_key(&sm4_key, key);
|
||||
sm4_cbc_encrypt(&sm4_key, iv, buf2, 2, buf4);
|
||||
|
||||
for (i = 0; i < 32; i++) {
|
||||
printf("%02x", buf4[i]);
|
||||
}
|
||||
printf("\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_sm4_cbc_padding(void)
|
||||
{
|
||||
SM4_KEY enc_key;
|
||||
SM4_KEY dec_key;
|
||||
uint8_t key[16] = {0};
|
||||
uint8_t iv[16] = {0};
|
||||
uint8_t in[64];
|
||||
uint8_t out[128];
|
||||
uint8_t buf[128];
|
||||
size_t len1, len2, i;
|
||||
|
||||
for (i = 0; i < sizeof(in); i++) {
|
||||
in[i] = i;
|
||||
}
|
||||
|
||||
sm4_set_encrypt_key(&enc_key, key);
|
||||
sm4_set_decrypt_key(&dec_key, key);
|
||||
|
||||
sm4_cbc_padding_encrypt(&enc_key, iv, in, 33, out, &len1);
|
||||
printf("c = (%zu) ", len1); for (i = 0; i < len1; i++) printf("%02x", out[i]); printf("\n");
|
||||
|
||||
sm4_cbc_padding_decrypt(&dec_key, iv, out, len1, buf, &len2);
|
||||
printf("m = (%zu) ", len2); for (i = 0; i < len2; i++) printf("%02x", buf[i]); printf("\n");
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_sm4_cbc();
|
||||
test_sm4_cbc_padding();
|
||||
return 1;
|
||||
}
|
||||
311
tests/sm4test.c
Normal file
311
tests/sm4test.c
Normal file
@@ -0,0 +1,311 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/sm4.h>
|
||||
|
||||
# ifdef SM4_AVX2
|
||||
void sm4_avx2_ecb_encrypt_blocks(const unsigned char *in,
|
||||
unsigned char *out, size_t blocks, const SM4_KEY *key);
|
||||
void sm4_avx2_ctr32_encrypt_blocks(const unsigned char *in,
|
||||
unsigned char *out, size_t blocks, const SM4_KEY *key,
|
||||
const unsigned char iv[16]);
|
||||
# endif
|
||||
|
||||
static int test_ecb(int avx)
|
||||
{
|
||||
SM4_KEY key;
|
||||
unsigned char user_key[16] = {0};
|
||||
/* 2 rounds avx-512 and 2 rounds x86 */
|
||||
unsigned char in[(16 * 2 + 2) * 16] = {0};
|
||||
unsigned char out1[sizeof(in)] = {0};
|
||||
unsigned char out2[sizeof(in)] = {0};
|
||||
int i;
|
||||
|
||||
for (i = 0; i < sizeof(user_key); i++) {
|
||||
user_key[i] = (unsigned char)i;
|
||||
}
|
||||
for (i = 0; i < sizeof(in); i++) {
|
||||
in[i] = (unsigned char)i;
|
||||
}
|
||||
/*
|
||||
RAND_bytes(user_key, sizeof(user_key));
|
||||
RAND_bytes(in, sizeof(in));
|
||||
*/
|
||||
|
||||
sm4_set_encrypt_key(&key, user_key);
|
||||
for (i = 0; i < sizeof(in)/SM4_BLOCK_SIZE; i++) {
|
||||
sm4_encrypt(&key, in + 16*i, out1 + 16*i);
|
||||
}
|
||||
|
||||
switch (avx) {
|
||||
# ifdef SM4_AVX2
|
||||
case 2:
|
||||
sm4_avx2_ecb_encrypt_blocks(in, out2, sizeof(in)/SM4_BLOCK_SIZE, &key);
|
||||
break;
|
||||
# endif
|
||||
default:
|
||||
printf("avx shuold be in {2}\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (memcmp(out1, out2, sizeof(out1)) != 0) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void xor_block(unsigned char *out, const unsigned char *in)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 16; i++) {
|
||||
out[i] ^= in[i];
|
||||
}
|
||||
}
|
||||
|
||||
static int test_ctr32(int avx)
|
||||
{
|
||||
SM4_KEY key;
|
||||
unsigned char user_key[16] = {0};
|
||||
unsigned char iv[16] = {0};
|
||||
unsigned char ctr1[16];
|
||||
unsigned char ctr2[16];
|
||||
/* 2 rounds avx-512 and 2 rounds x86 */
|
||||
unsigned char in[(16 * 2 + 2) * 16] = {0};
|
||||
unsigned char out1[sizeof(in)];
|
||||
unsigned char out2[sizeof(in)];
|
||||
int i;
|
||||
|
||||
/*
|
||||
RAND_bytes(user_key, sizeof(user_key));
|
||||
RAND_bytes(iv, sizeof(iv) - 1);
|
||||
RAND_bytes(in, sizeof(in));
|
||||
*/
|
||||
|
||||
sm4_set_encrypt_key(&key, user_key);
|
||||
memcpy(ctr1, iv, sizeof(iv));
|
||||
memcpy(ctr2, iv, sizeof(iv));
|
||||
|
||||
for (i = 0; i < sizeof(in)/16; i++) {
|
||||
sm4_encrypt(&key, ctr1, out1 + 16 * i);
|
||||
xor_block(out1 + 16 * i, in + 16 * i);
|
||||
ctr1[15]++;
|
||||
}
|
||||
|
||||
switch (avx) {
|
||||
# ifdef SM4_AVX2
|
||||
case 2:
|
||||
sm4_avx2_ctr32_encrypt_blocks(in, out2, sizeof(in)/16, &key, ctr2);
|
||||
break;
|
||||
# endif
|
||||
case 0:
|
||||
// do we need this?
|
||||
//sm4_ctr32_encrypt_blocks(in, out2, sizeof(in)/16, &key, ctr2);
|
||||
break;
|
||||
default:
|
||||
printf("avx should be in {0, 2}\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (memcmp(out1, out2, sizeof(out1)) != 0) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
static int test_ede(void)
|
||||
{
|
||||
SM4_KEY key;
|
||||
sm4_ede_key_t ede_key;
|
||||
unsigned char user_key[48];
|
||||
unsigned char in[16];
|
||||
unsigned char out1[16];
|
||||
unsigned char out2[16];
|
||||
|
||||
RAND_bytes(in, sizeof(in));
|
||||
|
||||
RAND_bytes(user_key, 16);
|
||||
memcpy(user_key + 16, user_key, 16);
|
||||
memcpy(user_key + 32, user_key, 16);
|
||||
sm4_set_encrypt_key(&key, user_key);
|
||||
sm4_encrypt(in, out1, &key);
|
||||
sm4_ede_set_encrypt_key(&ede_key, user_key);
|
||||
sm4_ede_encrypt(in, out2, &ede_key);
|
||||
if (memcmp(out1, out2, 16) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
RAND_bytes(user_key, sizeof(user_key));
|
||||
sm4_ede_set_encrypt_key(&ede_key, user_key);
|
||||
sm4_ede_encrypt(in, out1, &ede_key);
|
||||
sm4_ede_set_decrypt_key(&ede_key, user_key);
|
||||
sm4_ede_decrypt(out1, out2, &ede_key);
|
||||
if (memcmp(in, out2, 16) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
*/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int err = 0;
|
||||
int i;
|
||||
SM4_KEY key;
|
||||
unsigned char buf[16];
|
||||
|
||||
unsigned char user_key[16] = {
|
||||
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
|
||||
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
|
||||
};
|
||||
|
||||
uint32_t rk[32] = {
|
||||
0xf12186f9, 0x41662b61, 0x5a6ab19a, 0x7ba92077,
|
||||
0x367360f4, 0x776a0c61, 0xb6bb89b3, 0x24763151,
|
||||
0xa520307c, 0xb7584dbd, 0xc30753ed, 0x7ee55b57,
|
||||
0x6988608c, 0x30d895b7, 0x44ba14af, 0x104495a1,
|
||||
0xd120b428, 0x73b55fa3, 0xcc874966, 0x92244439,
|
||||
0xe89e641f, 0x98ca015a, 0xc7159060, 0x99e1fd2e,
|
||||
0xb79bd80c, 0x1d2115b0, 0x0e228aeb, 0xf1780c81,
|
||||
0x428d3654, 0x62293496, 0x01cf72e5, 0x9124a012,
|
||||
};
|
||||
|
||||
unsigned char plaintext[16] = {
|
||||
0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
|
||||
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
|
||||
};
|
||||
|
||||
unsigned char ciphertext1[16] = {
|
||||
0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
|
||||
0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
|
||||
};
|
||||
|
||||
unsigned char ciphertext2[16] = {
|
||||
0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f,
|
||||
0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66,
|
||||
};
|
||||
|
||||
/* test key scheduling */
|
||||
sm4_set_encrypt_key(&key, user_key);
|
||||
|
||||
if (memcmp(key.rk, rk, sizeof(rk)) != 0) {
|
||||
printf("sm4 key scheduling not passed!\n");
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
printf("sm4 key scheduling passed!\n");
|
||||
|
||||
/* test encrypt once */
|
||||
sm4_encrypt(&key, plaintext, buf);
|
||||
|
||||
if (memcmp(buf, ciphertext1, sizeof(ciphertext1)) != 0) {
|
||||
printf("sm4 encrypt not pass!\n");
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
printf("sm4 encrypt pass!\n");
|
||||
|
||||
/* test encrypt 1000000 times */
|
||||
memcpy(buf, plaintext, sizeof(plaintext));
|
||||
for (i = 0; i < 1000000; i++) {
|
||||
sm4_encrypt(&key, buf, buf);
|
||||
}
|
||||
|
||||
if (memcmp(buf, ciphertext2, sizeof(ciphertext2)) != 0) {
|
||||
printf("sm4 encrypt 1000000 times not pass!\n");
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
printf("sm4 encrypt 1000000 times pass!\n");
|
||||
|
||||
/* test ctr32 */
|
||||
if (!test_ctr32(0)) {
|
||||
printf("sm4 ctr32 not pass!\n");
|
||||
err++;
|
||||
} else
|
||||
printf("sm4 ctr32 pass!\n");
|
||||
|
||||
/* test ede */
|
||||
/*
|
||||
if (!test_ede()) {
|
||||
printf("sm4 ede not pass!\n");
|
||||
err++;
|
||||
} else
|
||||
printf("sm4 ede pass!\n");
|
||||
*/
|
||||
|
||||
|
||||
# ifdef SM4_AVX2
|
||||
/* test ecb in avx2 */
|
||||
if (!test_ecb(2)) {
|
||||
printf("sm4 ecb in avx2 not pass!\n");
|
||||
err++;
|
||||
} else
|
||||
printf("sm4 ecb in avx2 pass!\n");
|
||||
|
||||
/* test ctr32 in avx2 */
|
||||
if (!test_ctr32(2)) {
|
||||
printf("sm4 ctr32 in avx2 not pass!\n");
|
||||
err++;
|
||||
} else
|
||||
printf("sm4 ctr32 in avx2 pass!\n");
|
||||
# endif
|
||||
|
||||
if (err == 0)
|
||||
printf("sm4 all test vectors pass!\n");
|
||||
else
|
||||
end:
|
||||
printf("some test vector failed\n");
|
||||
|
||||
return err;
|
||||
}
|
||||
353
tests/tlstest.c
Normal file
353
tests/tlstest.c
Normal file
@@ -0,0 +1,353 @@
|
||||
/*
|
||||
* Copyright (c) 2021 - 2021 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_tls_cbc(void)
|
||||
{
|
||||
uint8_t key[32];
|
||||
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;
|
||||
|
||||
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);
|
||||
|
||||
printf("%zu\n", len);
|
||||
print_der(out, len);
|
||||
printf("\n");
|
||||
|
||||
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\n", buf);
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_tls_random(void)
|
||||
{
|
||||
uint8_t random[32];
|
||||
tls_random_generate(random);
|
||||
tls_random_print(stdout, random, 0, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_tls_client_hello(void)
|
||||
{
|
||||
uint8_t record[512];
|
||||
size_t recordlen = 0;
|
||||
|
||||
int version = TLS_version_tlcp;
|
||||
uint8_t random[32];
|
||||
uint16_t cipher_suites[] = {
|
||||
TLCP_cipher_ecc_sm4_cbc_sm3,
|
||||
TLCP_cipher_ecc_sm4_gcm_sm3,
|
||||
TLCP_cipher_ecdhe_sm4_cbc_sm3,
|
||||
TLCP_cipher_ecdhe_sm4_gcm_sm3,
|
||||
TLCP_cipher_ibsdh_sm4_cbc_sm3,
|
||||
TLCP_cipher_ibsdh_sm4_gcm_sm3,
|
||||
TLCP_cipher_ibc_sm4_cbc_sm3,
|
||||
TLCP_cipher_ibc_sm4_gcm_sm3,
|
||||
TLCP_cipher_rsa_sm4_cbc_sm3,
|
||||
TLCP_cipher_rsa_sm4_gcm_sm3,
|
||||
TLCP_cipher_rsa_sm4_cbc_sha256,
|
||||
TLCP_cipher_rsa_sm4_gcm_sha256,
|
||||
};
|
||||
uint8_t comp_meths[] = {0};
|
||||
|
||||
tls_record_set_handshake_client_hello(record, &recordlen,
|
||||
version,
|
||||
random,
|
||||
NULL, 0,
|
||||
cipher_suites, sizeof(cipher_suites)/2,
|
||||
NULL, 0);
|
||||
|
||||
tls_client_hello_print(stdout, record + 5 + 4, recordlen - 5 -4, 0, 4);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_tls_server_hello(void)
|
||||
{
|
||||
uint8_t record[512];
|
||||
size_t recordlen = 0;
|
||||
|
||||
|
||||
uint8_t version[2] = {1,1};
|
||||
uint8_t random[32];
|
||||
uint16_t cipher_suite = TLCP_cipher_ecdhe_sm4_cbc_sm3;
|
||||
uint8_t comp_meth = 0;
|
||||
|
||||
tls_record_set_handshake_server_hello(record, &recordlen,
|
||||
version,
|
||||
random,
|
||||
NULL, 0,
|
||||
cipher_suite,
|
||||
comp_meth,
|
||||
NULL, 0);
|
||||
|
||||
tls_server_hello_print(stdout, record + 5 + 4, recordlen - 5 -4, 0, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_tls_certificate(void)
|
||||
{
|
||||
uint8_t record[1024];
|
||||
size_t recordlen = 0;
|
||||
FILE *fp = NULL;
|
||||
|
||||
if (!(fp = fopen("cacerts.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);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_tls_server_key_exchange(void)
|
||||
{
|
||||
uint8_t record[1024];
|
||||
size_t recordlen = 0;
|
||||
const uint8_t version[] = {1,1};
|
||||
uint8_t sig[77];
|
||||
size_t siglen;
|
||||
|
||||
tls_record_set_version(record, version);
|
||||
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, sig, &siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
tls_server_key_exchange_print(stdout, sig, siglen, 0, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_tls_certificate_verify(void)
|
||||
{
|
||||
uint8_t record[1024];
|
||||
size_t recordlen = 0;
|
||||
const uint8_t version[] = {1,1};
|
||||
uint8_t sig[77];
|
||||
size_t siglen;
|
||||
|
||||
tls_record_set_version(record, version);
|
||||
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, sig, &siglen) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
tls_certificate_verify_print(stdout, sig, siglen, 0, 0);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int test_tls_finished(void)
|
||||
{
|
||||
uint8_t record[1024];
|
||||
size_t recordlen = 0;
|
||||
uint8_t verify_data[12];
|
||||
|
||||
if (tls_record_set_handshake_finished(record, &recordlen, verify_data) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
if (tls_record_get_handshake_finished(record, verify_data) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
tls_finished_print(stdout, verify_data, 12, 0, 0);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int err = 0;
|
||||
err += test_tls_encode();
|
||||
err += test_tls_cbc();
|
||||
err += test_tls_random();
|
||||
err += test_tls_client_hello();
|
||||
err += test_tls_server_hello();
|
||||
err += test_tls_certificate();
|
||||
err += test_tls_server_key_exchange();
|
||||
err += test_tls_certificate_verify();
|
||||
err += test_tls_finished();
|
||||
err += test_tls_alert();
|
||||
err += test_tls_change_cipher_spec();
|
||||
err += test_tls_application_data();
|
||||
return 0;
|
||||
}
|
||||
|
||||
62
tests/u128test.c
Normal file
62
tests/u128test.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2020 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
__uint128_t a = 0;
|
||||
uint64_t al = 1;
|
||||
uint64_t ah = 1;
|
||||
|
||||
a = ((__uint128_t)ah << 64) | al;
|
||||
|
||||
return (int)a;
|
||||
}
|
||||
328
tests/x509test.c
Normal file
328
tests/x509test.c
Normal file
@@ -0,0 +1,328 @@
|
||||
/*
|
||||
* Copyright (c) 2014 - 2021 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#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>
|
||||
|
||||
|
||||
static int test_x509_validity(void)
|
||||
{
|
||||
int err = 0;
|
||||
X509_VALIDITY validity;
|
||||
uint8_t buf[64] = {0};
|
||||
const uint8_t *cp = buf;
|
||||
uint8_t *p = buf;
|
||||
size_t len = 0;
|
||||
size_t i;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
memset(&validity, 0, sizeof(X509_VALIDITY));
|
||||
|
||||
x509_validity_set_days(&validity, time(NULL), 365 * 10);
|
||||
x509_validity_to_der(&validity, &p, &len);
|
||||
print_der(buf, len);
|
||||
printf("\n");
|
||||
|
||||
memset(&validity, 0, sizeof(X509_VALIDITY));
|
||||
x509_validity_from_der(&validity, &cp, &len);
|
||||
x509_validity_print(stdout, &validity, 0, 0);
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_x509_name(void)
|
||||
{
|
||||
int err = 0;
|
||||
X509_NAME name;
|
||||
uint8_t buf[1024];
|
||||
const uint8_t *cp = buf;
|
||||
uint8_t *p = buf;
|
||||
size_t len = 0;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
|
||||
memset(&name, 0, sizeof(X509_NAME));
|
||||
x509_name_add_rdn(&name, OID_at_countryName, ASN1_TAG_PrintableString, "CN");
|
||||
x509_name_add_rdn(&name, OID_at_stateOrProvinceName, ASN1_TAG_PrintableString, "Beijing");
|
||||
x509_name_add_rdn(&name, OID_at_organizationName, ASN1_TAG_PrintableString, "PKU");
|
||||
x509_name_add_rdn(&name, OID_at_organizationalUnitName, ASN1_TAG_PrintableString, "CS");
|
||||
x509_name_add_rdn(&name, OID_at_commonName, ASN1_TAG_PrintableString, "infosec");
|
||||
|
||||
if (x509_name_to_der(&name, &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
print_der(buf, len);
|
||||
printf("\n");
|
||||
|
||||
if (x509_name_from_der(&name, &cp, &len) != 1
|
||||
|| len > 0) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
x509_name_print(stdout, &name, 0, 0);
|
||||
|
||||
end:
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_x509_signature_algor(int oid)
|
||||
{
|
||||
int err = 0;
|
||||
int tests[] = {OID_sm2sign_with_sm3, OID_rsasign_with_sm3};
|
||||
int val;
|
||||
uint8_t buf[128];
|
||||
const uint8_t *cp = buf;
|
||||
uint8_t *p = buf;
|
||||
size_t len = 0;
|
||||
size_t i;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
printf("%s\n", asn1_object_identifier_name(tests[i]));
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
if (x509_signature_algor_to_der(tests[i], &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
print_der(buf, len);
|
||||
printf("\n");
|
||||
}
|
||||
for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
|
||||
if (x509_signature_algor_from_der(&val, &cp, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
if (val != tests[i]) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
printf("%s\n", asn1_object_identifier_name(tests[i]));
|
||||
}
|
||||
|
||||
end:
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_x509_public_key_info(void)
|
||||
{
|
||||
int err = 0;
|
||||
SM2_KEY key;
|
||||
X509_PUBLIC_KEY_INFO pkey_info;
|
||||
uint8_t buf[256];
|
||||
const uint8_t *cp = buf;
|
||||
uint8_t *p = buf;
|
||||
size_t len = 0;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
|
||||
sm2_keygen(&key);
|
||||
x509_public_key_info_set_sm2(&pkey_info, &key);
|
||||
|
||||
if (x509_public_key_info_to_der(&pkey_info, &p, &len) != 1) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
print_der(buf, len);
|
||||
printf("\n");
|
||||
|
||||
if (x509_public_key_info_from_der(&pkey_info, &cp, &len) != 1
|
||||
|| len > 0) {
|
||||
error_print();
|
||||
return -1;
|
||||
}
|
||||
|
||||
x509_public_key_info_print(stdout, &pkey_info, 0, 0);
|
||||
|
||||
printf("\n");
|
||||
return err;
|
||||
}
|
||||
|
||||
static int test_x509_certificate(void)
|
||||
{
|
||||
int err = 0;
|
||||
X509_CERTIFICATE _cert, *cert = &_cert;
|
||||
int rv;
|
||||
int version = X509_version_v3;
|
||||
uint8_t sn[12];
|
||||
X509_NAME issuer;
|
||||
X509_NAME subject;
|
||||
time_t not_before;
|
||||
|
||||
SM2_KEY key;
|
||||
|
||||
uint8_t buf[2048] = {0};
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
|
||||
memset(cert, 0, sizeof(X509_CERTIFICATE));
|
||||
|
||||
rand_bytes(sn, sizeof(sn));
|
||||
|
||||
memset(&issuer, 0, sizeof(X509_NAME));
|
||||
// add_rdn 应该用一个ex来支持长度
|
||||
x509_name_add_rdn(&issuer, OID_at_countryName, ASN1_TAG_PrintableString, "CN");
|
||||
x509_name_add_rdn(&issuer, OID_at_stateOrProvinceName, ASN1_TAG_PrintableString, "Beijing");
|
||||
x509_name_add_rdn(&issuer, OID_at_organizationName, ASN1_TAG_PrintableString, "PKU");
|
||||
x509_name_add_rdn(&issuer, OID_at_organizationalUnitName, ASN1_TAG_PrintableString, "CS");
|
||||
x509_name_add_rdn(&issuer, OID_at_commonName, ASN1_TAG_PrintableString, "CA");
|
||||
|
||||
memset(&subject, 0, sizeof(X509_NAME));
|
||||
x509_name_add_rdn(&subject, OID_at_countryName, ASN1_TAG_PrintableString, "CN");
|
||||
x509_name_add_rdn(&subject, OID_at_stateOrProvinceName, ASN1_TAG_PrintableString, "Beijing");
|
||||
x509_name_add_rdn(&subject, OID_at_organizationName, ASN1_TAG_PrintableString, "PKU");
|
||||
x509_name_add_rdn(&subject, OID_at_organizationalUnitName, ASN1_TAG_PrintableString, "CS");
|
||||
x509_name_add_rdn(&subject, OID_at_commonName, ASN1_TAG_PrintableString, "infosec");
|
||||
|
||||
time(¬_before);
|
||||
|
||||
rv = x509_certificate_set_version(cert, version);
|
||||
rv = x509_certificate_set_serial_number(cert, sn, sizeof(sn));
|
||||
rv = x509_certificate_set_signature_algor(cert, OID_sm2sign_with_sm3); // 这个不是应该在设置公钥的时候一起设置吗?
|
||||
rv = x509_certificate_set_issuer(cert, &issuer);
|
||||
rv = x509_certificate_set_subject(cert, &subject);
|
||||
rv = x509_certificate_set_validity(cert, not_before, 365);
|
||||
|
||||
sm2_keygen(&key);
|
||||
rv = x509_certificate_set_subject_public_key_info_sm2(cert, &key);
|
||||
|
||||
|
||||
rv = x509_certificate_generate_subject_key_identifier(cert, 1);
|
||||
|
||||
|
||||
rv = x509_certificate_sign_sm2(cert, &key);
|
||||
|
||||
rv = x509_certificate_to_der(cert, &p, &len);
|
||||
print_der(buf, len);
|
||||
printf("\n");
|
||||
|
||||
memset(cert, 0, sizeof(X509_CERTIFICATE));
|
||||
x509_certificate_from_der(cert, &cp, &len);
|
||||
|
||||
x509_certificate_print(stdout, cert, 0, 0);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int test_x509_cert_request(void)
|
||||
{
|
||||
int err = 0;
|
||||
X509_CERT_REQUEST req;
|
||||
X509_NAME subject;
|
||||
SM2_KEY keypair;
|
||||
uint8_t buf[256];
|
||||
uint8_t *p = buf;
|
||||
const uint8_t *cp = buf;
|
||||
size_t len = 0;
|
||||
|
||||
printf("%s : \n", __func__);
|
||||
|
||||
memset(&subject, 0, sizeof(X509_NAME));
|
||||
x509_name_add_rdn(&subject, OID_at_countryName, ASN1_TAG_PrintableString, "CN");
|
||||
x509_name_add_rdn(&subject, OID_at_stateOrProvinceName, ASN1_TAG_PrintableString, "Beijing");
|
||||
x509_name_add_rdn(&subject, OID_at_organizationName, ASN1_TAG_PrintableString, "PKU");
|
||||
x509_name_add_rdn(&subject, OID_at_organizationalUnitName, ASN1_TAG_PrintableString, "CS");
|
||||
x509_name_add_rdn(&subject, OID_at_commonName, ASN1_TAG_PrintableString, "infosec");
|
||||
|
||||
sm2_keygen(&keypair);
|
||||
|
||||
if (x509_cert_request_set_sm2(&req, &subject, &keypair) != 1
|
||||
|| x509_cert_request_sign_sm2(&req, &keypair) != 1
|
||||
|| x509_cert_request_to_der(&req, &p, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
print_der(buf, len);
|
||||
printf("\n");
|
||||
|
||||
memset(&req, 0, sizeof(req));
|
||||
if (x509_cert_request_from_der(&req, &cp, &len) != 1) {
|
||||
error_print();
|
||||
err++;
|
||||
goto end;
|
||||
}
|
||||
|
||||
x509_cert_request_print(stdout, &req, 0, 0);
|
||||
|
||||
end:
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int err = 0;
|
||||
//err += test_x509_validity();
|
||||
err += test_x509_signature_algor(OID_sm2sign_with_sm3);
|
||||
err += test_x509_signature_algor(OID_rsasign_with_sm3);
|
||||
err += test_x509_name();
|
||||
err += test_x509_public_key_info();
|
||||
err += test_x509_certificate();
|
||||
err += test_x509_cert_request();
|
||||
//test_x509_extensions();
|
||||
return 1;
|
||||
}
|
||||
522
tests/zuctest.c
Normal file
522
tests/zuctest.c
Normal file
@@ -0,0 +1,522 @@
|
||||
/* ====================================================================
|
||||
* Copyright (c) 2014 - 2018 The GmSSL Project. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this
|
||||
* software must display the following acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project.
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* 4. The name "GmSSL Project" must not be used to endorse or promote
|
||||
* products derived from this software without prior written
|
||||
* permission. For written permission, please contact
|
||||
* guanzhi1980@gmail.com.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "GmSSL"
|
||||
* nor may "GmSSL" appear in their names without prior written
|
||||
* permission of the GmSSL Project.
|
||||
*
|
||||
* 6. Redistributions of any form whatsoever must retain the following
|
||||
* acknowledgment:
|
||||
* "This product includes software developed by the GmSSL Project
|
||||
* (http://gmssl.org/)"
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE GmSSL PROJECT ``AS IS'' AND ANY
|
||||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE GmSSL PROJECT OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*/
|
||||
|
||||
#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_KEY zuc = {{0}};
|
||||
uint32_t buf[3] = {0};
|
||||
zuc_set_key(&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_KEY zuc_key;
|
||||
uint32_t buf[20] = {0};
|
||||
|
||||
zuc256_set_key(&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;
|
||||
}
|
||||
Reference in New Issue
Block a user