mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-06 16:36:16 +08:00
remove old api
This commit is contained in:
226
tests/aestest.c
226
tests/aestest.c
@@ -51,8 +51,55 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/aes.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
int main(void)
|
||||
|
||||
int test_aes_ctr(void)
|
||||
{
|
||||
// NIST SP 800-38A F.5.1
|
||||
char *hex_key = "2b7e151628aed2a6abf7158809cf4f3c";
|
||||
char *hex_ctr = "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
|
||||
char *hex_msg = "6bc1bee22e409f96e93d7e117393172a"
|
||||
"ae2d8a571e03ac9c9eb76fac45af8e51"
|
||||
"30c81c46a35ce411e5fbc1191a0a52ef"
|
||||
"f69f2445df4f9b17ad2b417be66c3710";
|
||||
char *hex_out = "874d6191b620e3261bef6864990db6ce"
|
||||
"9806f66b7970fdff8617187bb9fffdff"
|
||||
"5ae4df3edbd5d35e5b4f09020db03eab"
|
||||
"1e031dda2fbe03d1792170a0f3009cee";
|
||||
|
||||
int err = 0;
|
||||
AES_KEY aes_key;
|
||||
uint8_t key[32];
|
||||
uint8_t ctr[16];
|
||||
uint8_t msg[64];
|
||||
uint8_t out[64];
|
||||
uint8_t buf[64];
|
||||
size_t keylen, ctrlen, msglen, outlen, buflen;
|
||||
|
||||
hex_to_bytes(hex_key, strlen(hex_key), key, &keylen);
|
||||
hex_to_bytes(hex_ctr, strlen(hex_ctr), ctr, &ctrlen);
|
||||
hex_to_bytes(hex_msg, strlen(hex_msg), msg, &msglen);
|
||||
hex_to_bytes(hex_out, strlen(hex_out), out, &outlen);
|
||||
|
||||
aes_set_encrypt_key(&aes_key, key, keylen);
|
||||
aes_ctr_encrypt(&aes_key, ctr, msg, msglen, buf);
|
||||
buflen = msglen;
|
||||
|
||||
format_bytes(stdout, 0, 0, "aes_ctr(msg) = ", buf, buflen);
|
||||
format_bytes(stdout, 0, 0, " = ", out, outlen);
|
||||
|
||||
hex_to_bytes(hex_ctr, strlen(hex_ctr), ctr, &ctrlen);
|
||||
aes_ctr_encrypt(&aes_key, ctr, buf, buflen, buf);
|
||||
format_bytes(stdout, 0, 0, "msg = ", msg, msglen);
|
||||
format_bytes(stdout, 0, 0, " = ", buf, buflen);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
int test_aes(void)
|
||||
{
|
||||
int err = 0;
|
||||
AES_KEY aes_key;
|
||||
@@ -185,3 +232,180 @@ int main(void)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
struct {
|
||||
char *K;
|
||||
char *P;
|
||||
char *A;
|
||||
char *IV;
|
||||
char *C;
|
||||
char *T;
|
||||
} aes_gcm_tests[] = {
|
||||
// test 1
|
||||
{
|
||||
"00000000000000000000000000000000",
|
||||
"",
|
||||
"",
|
||||
"000000000000000000000000",
|
||||
"",
|
||||
"58e2fccefa7e3061367f1d57a4e7455a",
|
||||
},
|
||||
// test 2
|
||||
{
|
||||
"00000000000000000000000000000000",
|
||||
"00000000000000000000000000000000",
|
||||
"",
|
||||
"000000000000000000000000",
|
||||
"0388dace60b6a392f328c2b971b2fe78",
|
||||
"ab6e47d42cec13bdf53a67b21257bddf",
|
||||
},
|
||||
// test 3
|
||||
{
|
||||
"feffe9928665731c6d6a8f9467308308",
|
||||
"d9313225f88406e5a55909c5aff5269a"
|
||||
"86a7a9531534f7da2e4c303d8a318a72"
|
||||
"1c3c0c95956809532fcf0e2449a6b525"
|
||||
"b16aedf5aa0de657ba637b391aafd255",
|
||||
"",
|
||||
"cafebabefacedbaddecaf888",
|
||||
"42831ec2217774244b7221b784d0d49c"
|
||||
"e3aa212f2c02a4e035c17e2329aca12e"
|
||||
"21d514b25466931c7d8f6a5aac84aa05"
|
||||
"1ba30b396a0aac973d58e091473f5985",
|
||||
"4d5c2af327cd64a62cf35abd2ba6fab4",
|
||||
},
|
||||
// test 4
|
||||
{
|
||||
"feffe9928665731c6d6a8f9467308308",
|
||||
"d9313225f88406e5a55909c5aff5269a"
|
||||
"86a7a9531534f7da2e4c303d8a318a72"
|
||||
"1c3c0c95956809532fcf0e2449a6b525"
|
||||
"b16aedf5aa0de657ba637b39",
|
||||
"feedfacedeadbeeffeedfacedeadbeef"
|
||||
"abaddad2",
|
||||
"cafebabefacedbaddecaf888",
|
||||
"42831ec2217774244b7221b784d0d49c"
|
||||
"e3aa212f2c02a4e035c17e2329aca12e"
|
||||
"21d514b25466931c7d8f6a5aac84aa05"
|
||||
"1ba30b396a0aac973d58e091",
|
||||
"5bc94fbc3221a5db94fae95ae7121a47",
|
||||
},
|
||||
// test 5
|
||||
{
|
||||
"feffe9928665731c6d6a8f9467308308",
|
||||
"d9313225f88406e5a55909c5aff5269a"
|
||||
"86a7a9531534f7da2e4c303d8a318a72"
|
||||
"1c3c0c95956809532fcf0e2449a6b525"
|
||||
"b16aedf5aa0de657ba637b39",
|
||||
"feedfacedeadbeeffeedfacedeadbeef"
|
||||
"abaddad2",
|
||||
"cafebabefacedbad",
|
||||
"61353b4c2806934a777ff51fa22a4755"
|
||||
"699b2a714fcdc6f83766e5f97b6c7423"
|
||||
"73806900e49f24b22b097544d4896b42"
|
||||
"4989b5e1ebac0f07c23f4598",
|
||||
"3612d2e79e3b0785561be14aaca2fccb",
|
||||
},
|
||||
// test 6
|
||||
{
|
||||
"feffe9928665731c6d6a8f9467308308",
|
||||
"d9313225f88406e5a55909c5aff5269a"
|
||||
"86a7a9531534f7da2e4c303d8a318a72"
|
||||
"1c3c0c95956809532fcf0e2449a6b525"
|
||||
"b16aedf5aa0de657ba637b39",
|
||||
"feedfacedeadbeeffeedfacedeadbeef"
|
||||
"abaddad2",
|
||||
"9313225df88406e555909c5aff5269aa"
|
||||
"6a7a9538534f7da1e4c303d2a318a728"
|
||||
"c3c0c95156809539fcf0e2429a6b5254"
|
||||
"16aedbf5a0de6a57a637b39b",
|
||||
"8ce24998625615b603a033aca13fb894"
|
||||
"be9112a5c3a211a8ba262a3cca7e2ca7"
|
||||
"01e4a9a4fba43c90ccdcb281d48c7c6f"
|
||||
"d62875d2aca417034c34aee5",
|
||||
"619cc5aefffe0bfa462af43c1699d050",
|
||||
},
|
||||
// test 7
|
||||
{
|
||||
"00000000000000000000000000000000"
|
||||
"0000000000000000",
|
||||
"",
|
||||
"",
|
||||
"000000000000000000000000",
|
||||
"",
|
||||
"cd33b28ac773f74ba00ed1f312572435",
|
||||
},
|
||||
};
|
||||
|
||||
int test_aes_gcm(void)
|
||||
{
|
||||
uint8_t K[32];
|
||||
uint8_t P[64];
|
||||
uint8_t A[32];
|
||||
uint8_t IV[64];
|
||||
uint8_t C[64];
|
||||
uint8_t T[16];
|
||||
size_t Klen, Plen, Alen, IVlen, Clen, Tlen;
|
||||
|
||||
AES_KEY aes_key;
|
||||
uint8_t out[64];
|
||||
uint8_t tag[16];
|
||||
uint8_t buf[64];
|
||||
int i;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
|
||||
for (i = 0; i < sizeof(aes_gcm_tests)/sizeof(aes_gcm_tests[0]); i++) {
|
||||
int ok = 1;
|
||||
|
||||
hex_to_bytes(aes_gcm_tests[i].K, strlen(aes_gcm_tests[i].K), K, &Klen);
|
||||
hex_to_bytes(aes_gcm_tests[i].P, strlen(aes_gcm_tests[i].P), P, &Plen);
|
||||
hex_to_bytes(aes_gcm_tests[i].A, strlen(aes_gcm_tests[i].A), A, &Alen);
|
||||
hex_to_bytes(aes_gcm_tests[i].IV, strlen(aes_gcm_tests[i].IV), IV, &IVlen);
|
||||
hex_to_bytes(aes_gcm_tests[i].C, strlen(aes_gcm_tests[i].C), C, &Clen);
|
||||
hex_to_bytes(aes_gcm_tests[i].T, strlen(aes_gcm_tests[i].T), T, &Tlen);
|
||||
|
||||
aes_set_encrypt_key(&aes_key, K, Klen);
|
||||
aes_gcm_encrypt(&aes_key, IV, IVlen, A, Alen, P, Plen, out, Tlen, tag);
|
||||
|
||||
if (aes_gcm_decrypt(&aes_key, IV, IVlen, A, Alen, out, Plen, tag, Tlen, buf) != 1) {
|
||||
error_print();
|
||||
ok = 0;
|
||||
}
|
||||
if (memcmp(buf, P, Plen) != 0) {
|
||||
error_print();
|
||||
ok = 0;
|
||||
}
|
||||
|
||||
printf(" test %d %s\n", i + 1, ok ? "ok" : "error");
|
||||
|
||||
/*
|
||||
format_print(stdout, 0, 2, "K = %s\n", aes_gcm_tests[i].K);
|
||||
format_print(stdout, 0, 2, "P = %s\n", aes_gcm_tests[i].P);
|
||||
format_print(stdout, 0, 2, "A = %s\n", aes_gcm_tests[i].A);
|
||||
format_print(stdout, 0, 2, "IV = %s\n", aes_gcm_tests[i].IV);
|
||||
format_print(stdout, 0, 2, "C = %s\n", aes_gcm_tests[i].C);
|
||||
format_bytes(stdout, 0, 2, " = ", out, Plen);
|
||||
format_print(stdout, 0, 2, "T = %s\n", aes_gcm_tests[i].T);
|
||||
format_bytes(stdout, 0, 2, " = ", tag, Tlen);
|
||||
printf("\n");
|
||||
*/
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_aes();
|
||||
test_aes_ctr();
|
||||
test_aes_gcm();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -85,6 +85,7 @@ int main(void)
|
||||
|
||||
if (memcmp(buf, testdata, sizeof(testdata)) != 0) {
|
||||
printf("chacha20 test 1 failed\n");
|
||||
return -1;
|
||||
} else {
|
||||
printf("chacha20 test 1 ok\n");
|
||||
}
|
||||
|
||||
141
tests/gcmtest.c
141
tests/gcmtest.c
@@ -50,69 +50,110 @@
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <gmssl/gcm.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/error.h>
|
||||
|
||||
|
||||
struct {
|
||||
char *K;
|
||||
char *P;
|
||||
char *IV;
|
||||
char *H;
|
||||
char *A;
|
||||
char *C;
|
||||
char *T;
|
||||
} gcm_tests[] = {
|
||||
/* test 1 */
|
||||
} ghash_tests[] = {
|
||||
// test 1
|
||||
{
|
||||
"66e94bd4ef8a2c3b884cfa59ca342b2e",
|
||||
"",
|
||||
"",
|
||||
"00000000000000000000000000000000",
|
||||
"",
|
||||
"000000000000000000000000",
|
||||
"",
|
||||
"58e2fccefa7e3061367f1d57a4e7455a",
|
||||
},
|
||||
/* test 2 */
|
||||
// test 2
|
||||
{
|
||||
"00000000000000000000000000000000",
|
||||
"00000000000000000000000000000000",
|
||||
"000000000000000000000000",
|
||||
"66e94bd4ef8a2c3b884cfa59ca342b2e",
|
||||
"",
|
||||
"0388dace60b6a392f328c2b971b2fe78",
|
||||
"ab6e47d42cec13bdf53a67b21257bddf",
|
||||
"f38cbb1ad69223dcc3457ae5b6b0f885",
|
||||
|
||||
},
|
||||
// test 3
|
||||
{
|
||||
"b83b533708bf535d0aa6e52980d53b78",
|
||||
"",
|
||||
"42831ec2217774244b7221b784d0d49c"
|
||||
"e3aa212f2c02a4e035c17e2329aca12e"
|
||||
"21d514b25466931c7d8f6a5aac84aa05"
|
||||
"1ba30b396a0aac973d58e091473f5985",
|
||||
"7f1b32b81b820d02614f8895ac1d4eac",
|
||||
},
|
||||
// test 4
|
||||
{
|
||||
"b83b533708bf535d0aa6e52980d53b78",
|
||||
"feedfacedeadbeeffeedfacedeadbeef"
|
||||
"abaddad2",
|
||||
"42831ec2217774244b7221b784d0d49c"
|
||||
"e3aa212f2c02a4e035c17e2329aca12e"
|
||||
"21d514b25466931c7d8f6a5aac84aa05"
|
||||
"1ba30b396a0aac973d58e091",
|
||||
"698e57f70e6ecc7fd9463b7260a9ae5f",
|
||||
},
|
||||
// test 5
|
||||
{
|
||||
"b83b533708bf535d0aa6e52980d53b78",
|
||||
"feedfacedeadbeeffeedfacedeadbeef"
|
||||
"abaddad2",
|
||||
"61353b4c2806934a777ff51fa22a4755"
|
||||
"699b2a714fcdc6f83766e5f97b6c7423"
|
||||
"73806900e49f24b22b097544d4896b42"
|
||||
"4989b5e1ebac0f07c23f4598",
|
||||
"df586bb4c249b92cb6922877e444d37b",
|
||||
},
|
||||
// test 6
|
||||
{
|
||||
"b83b533708bf535d0aa6e52980d53b78",
|
||||
"feedfacedeadbeeffeedfacedeadbeef"
|
||||
"abaddad2",
|
||||
"8ce24998625615b603a033aca13fb894"
|
||||
"be9112a5c3a211a8ba262a3cca7e2ca7"
|
||||
"01e4a9a4fba43c90ccdcb281d48c7c6f"
|
||||
"d62875d2aca417034c34aee5",
|
||||
"1c5afe9760d3932f3c9a878aac3dc3de",
|
||||
},
|
||||
};
|
||||
|
||||
int test_ghash(void)
|
||||
{
|
||||
uint8_t H[16];
|
||||
uint8_t A[32];
|
||||
uint8_t C[64];
|
||||
uint8_t T[16];
|
||||
uint8_t out[16];
|
||||
size_t Hlen, Alen, Clen, Tlen;
|
||||
int i;
|
||||
|
||||
printf("%s\n", __FUNCTION__);
|
||||
|
||||
for (i = 0; i < sizeof(ghash_tests)/sizeof(ghash_tests[0]); i++) {
|
||||
hex_to_bytes(ghash_tests[i].H, strlen(ghash_tests[i].H), H, &Hlen);
|
||||
hex_to_bytes(ghash_tests[i].A, strlen(ghash_tests[i].A), A, &Alen);
|
||||
hex_to_bytes(ghash_tests[i].C, strlen(ghash_tests[i].C), C, &Clen);
|
||||
hex_to_bytes(ghash_tests[i].T, strlen(ghash_tests[i].T), T, &Tlen);
|
||||
ghash(H, A, Alen, C, Clen, out);
|
||||
|
||||
printf(" test %d %s\n", i + 1, memcmp(out ,T, Tlen) == 0 ? "ok" : "error");
|
||||
/*
|
||||
format_print(stdout, 0, 2, "H = %s\n", ghash_tests[i].H);
|
||||
format_print(stdout, 0, 2, "A = %s\n", ghash_tests[i].A);
|
||||
format_print(stdout, 0, 2, "C = %s\n", ghash_tests[i].C);
|
||||
format_bytes(stdout, 0, 2, "GHASH(H,A,C) = ", out, 16);
|
||||
format_print(stdout, 0, 2, " = %s\n\n", ghash_tests[i].T);
|
||||
*/
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
test_ghash();
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -51,26 +51,16 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include "internal/gf128.h"
|
||||
#include <gmssl/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;
|
||||
}
|
||||
/*
|
||||
a = de300f9301a499a965f8bf677e99e80d
|
||||
b = 14b267838ec9ef1bb7b5ce8c19e34bc6
|
||||
a + b = ca8268108f6d76b2d24d71eb677aa3cb
|
||||
a - b = ca8268108f6d76b2d24d71eb677aa3cb
|
||||
a * b = 28e63413cd53b01a3b469375781942c6
|
||||
a * 2 = bc601f2603493352cbf17ecefd33d09d
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
@@ -81,9 +71,25 @@ int main(void)
|
||||
gf128_t b = gf128_from_hex("14b267838ec9ef1bb7b5ce8c19e34bc6");
|
||||
gf128_t r;
|
||||
|
||||
/*
|
||||
r = gf128_add(a, b);
|
||||
gf128_print("a + b = ", r);
|
||||
|
||||
r = gf128_mul(a, b);
|
||||
gf128_print("a * b = ", r);
|
||||
|
||||
r = gf128_mul2(a);
|
||||
gf128_print("a * 2 = ", r);
|
||||
*/
|
||||
|
||||
gf128_t H = gf128_from_hex("66e94bd4ef8a2c3b884cfa59ca342b2e");
|
||||
gf128_t C = gf128_from_hex("0388dace60b6a392f328c2b971b2fe78");
|
||||
gf128_t T = gf128_mul(C, H);
|
||||
|
||||
|
||||
|
||||
gf128_print("C = ", C);
|
||||
gf128_print("H = ", H);
|
||||
gf128_print("C * H = ", T);
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/digest.h>
|
||||
#include <gmssl/hash_drbg.h>
|
||||
@@ -66,31 +67,36 @@
|
||||
#define V1 "f9afadfbbf2c3d1004f9baca38be247342e5fbb83281915d5de18beb963712a344e89bb0e6b925a7bbc32eadb8b441efc1fa0c649df42a"
|
||||
#define C1 "1d41cbbd634909e4761c232fcfd6a6c2edf0a7f4d3d3c164f74a88955f355efce2d86c1e9fa897b7005ef9d4d3a51bf4fc0b805ab896c9"
|
||||
|
||||
#define PR1 "2edb396eeb8960f77943c2a59075a786"
|
||||
#define PR2 "30b565b63a5012676940d3ef17d9e996"
|
||||
|
||||
|
||||
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;
|
||||
uint8_t entropy[sizeof(EntropyInput)/2];
|
||||
uint8_t nonce[sizeof(Nonce)/2];
|
||||
uint8_t personalstr[sizeof(PersonalizationString)/2];
|
||||
uint8_t v[sizeof(V0)/2];
|
||||
uint8_t c[sizeof(C0)/2];
|
||||
uint8_t entropy_pr1[sizeof(EntropyInputPR1)/2];
|
||||
uint8_t pr1[sizeof(PR1)/2];
|
||||
uint8_t pr2[sizeof(PR2)/2];
|
||||
size_t entropy_len, nonce_len, personalstr_len, vlen, clen;
|
||||
|
||||
|
||||
unsigned char *entropy_pr1 = NULL;
|
||||
size_t entropy_pr1len;
|
||||
|
||||
size_t pr1_len, pr2_len;
|
||||
unsigned char out[640/8];
|
||||
int i;
|
||||
|
||||
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);
|
||||
hex_to_bytes(EntropyInput, strlen(EntropyInput), entropy, &entropy_len);
|
||||
hex_to_bytes(Nonce, strlen(Nonce), nonce, &nonce_len);
|
||||
hex_to_bytes(PersonalizationString, strlen(PersonalizationString), personalstr, &personalstr_len);
|
||||
hex_to_bytes(V0, strlen(V0), v, &vlen);
|
||||
hex_to_bytes(C0, strlen(C0), c, &clen);
|
||||
hex_to_bytes(EntropyInputPR1, strlen(EntropyInputPR1), entropy_pr1, &entropy_pr1len);
|
||||
hex_to_bytes(PR1, strlen(PR1), pr1, &pr1_len);
|
||||
hex_to_bytes(PR2, strlen(PR2), pr2, &pr2_len);
|
||||
|
||||
|
||||
hash_drbg_init(&drbg, DIGEST_sha1(),
|
||||
@@ -108,12 +114,6 @@ int main(void)
|
||||
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);
|
||||
@@ -121,7 +121,6 @@ int main(void)
|
||||
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]);
|
||||
}
|
||||
|
||||
@@ -50,9 +50,11 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/md5.h>
|
||||
#include <gmssl/hex.h>
|
||||
|
||||
|
||||
static char *teststr[] = {
|
||||
"",
|
||||
"a",
|
||||
@@ -77,17 +79,14 @@ int main(int argc, char **argv)
|
||||
{
|
||||
int err = 0;
|
||||
char *p;
|
||||
unsigned char *dgstbuf = NULL;
|
||||
uint8_t dgst[16];
|
||||
uint8_t dgstbuf[16];
|
||||
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);
|
||||
hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstbuflen);
|
||||
md5_digest((uint8_t *)teststr[i], strlen(teststr[i]), dgst);
|
||||
|
||||
if (memcmp(dgstbuf, dgst, sizeof(dgst)) != 0) {
|
||||
int n;
|
||||
@@ -102,12 +101,7 @@ int main(int argc, char **argv)
|
||||
} else {
|
||||
printf("md5 test %lu ok\n", i+1);
|
||||
}
|
||||
|
||||
free(dgstbuf);
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -156,5 +156,5 @@ int main(void)
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
0
tests/pemtest.c
Normal file
0
tests/pemtest.c
Normal file
@@ -49,6 +49,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/sha1.h>
|
||||
#include <gmssl/hex.h>
|
||||
|
||||
@@ -78,19 +79,17 @@ int main(void)
|
||||
{
|
||||
int err = 0;
|
||||
SHA1_CTX ctx;
|
||||
unsigned char dgst[20];
|
||||
unsigned char *dgstbuf = NULL;
|
||||
uint8_t dgst[20];
|
||||
uint8_t dgstbuf[20];
|
||||
size_t dgstlen;
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < sizeof(teststr)/sizeof(teststr[0]); i++) {
|
||||
|
||||
if (!(dgstbuf = OPENSSL_hexstr2buf(dgsthex[i], NULL))) {
|
||||
goto end;
|
||||
}
|
||||
hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstlen);
|
||||
|
||||
sha1_init(&ctx);
|
||||
for (j = 0; j < testcnt[i]; j++) {
|
||||
sha1_update(&ctx, (unsigned char *)teststr[i], strlen(teststr[i]));
|
||||
sha1_update(&ctx, (uint8_t *)teststr[i], strlen(teststr[i]));
|
||||
}
|
||||
sha1_finish(&ctx, dgst);
|
||||
|
||||
@@ -105,12 +104,7 @@ int main(void)
|
||||
} else {
|
||||
printf("sha1 test %lu ok\n", i+1);
|
||||
}
|
||||
|
||||
free(dgstbuf);
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/sha2.h>
|
||||
|
||||
@@ -100,19 +101,17 @@ int main(int argc, char **argv)
|
||||
{
|
||||
int err = 0;
|
||||
SHA224_CTX ctx;
|
||||
unsigned char dgst[SHA224_DIGEST_SIZE];
|
||||
unsigned char *dgstbuf = NULL;
|
||||
uint8_t dgst[SHA224_DIGEST_SIZE];
|
||||
uint8_t dgstbuf[SHA224_DIGEST_SIZE];
|
||||
size_t dgstlen;
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < 7; i++) {
|
||||
|
||||
if (!(dgstbuf = OPENSSL_hexstr2buf(tests[i].dgsthex, NULL))) {
|
||||
goto end;
|
||||
}
|
||||
hex_to_bytes(tests[i].dgsthex, strlen(tests[i].dgsthex), dgstbuf, &dgstlen);
|
||||
|
||||
sha224_init(&ctx);
|
||||
for (j = 0; j < tests[i].count; j++) {
|
||||
sha224_update(&ctx, (unsigned char *)tests[i].data, tests[i].length);
|
||||
sha224_update(&ctx, (uint8_t *)tests[i].data, tests[i].length);
|
||||
}
|
||||
sha224_finish(&ctx, dgst);
|
||||
|
||||
@@ -127,12 +126,7 @@ int main(int argc, char **argv)
|
||||
} else {
|
||||
printf("sha224 test %lu ok\n", i+1);
|
||||
}
|
||||
|
||||
free(dgstbuf);
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/sha2.h>
|
||||
|
||||
@@ -99,19 +100,17 @@ int main(int argc, char **argv)
|
||||
{
|
||||
int err = 0;
|
||||
SHA256_CTX ctx;
|
||||
unsigned char dgst[SHA256_DIGEST_SIZE];
|
||||
unsigned char *dgstbuf = NULL;
|
||||
uint8_t dgst[SHA256_DIGEST_SIZE];
|
||||
uint8_t dgstbuf[SHA256_DIGEST_SIZE];
|
||||
size_t dgstlen;
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < 7; i++) {
|
||||
|
||||
if (!(dgstbuf = OPENSSL_hexstr2buf(tests[i].dgsthex, NULL))) {
|
||||
goto end;
|
||||
}
|
||||
hex_to_bytes(tests[i].dgsthex, strlen(tests[i].dgsthex), dgstbuf, &dgstlen);
|
||||
|
||||
sha256_init(&ctx);
|
||||
for (j = 0; j < tests[i].count; j++) {
|
||||
sha256_update(&ctx, (unsigned char *)tests[i].data, tests[i].length);
|
||||
sha256_update(&ctx, (uint8_t *)tests[i].data, tests[i].length);
|
||||
}
|
||||
sha256_finish(&ctx, dgst);
|
||||
|
||||
@@ -126,12 +125,7 @@ int main(int argc, char **argv)
|
||||
} else {
|
||||
printf("sha256 test %lu ok\n", i+1);
|
||||
}
|
||||
|
||||
free(dgstbuf);
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/sha2.h>
|
||||
|
||||
@@ -105,19 +106,17 @@ int main(void)
|
||||
{
|
||||
int err = 0;
|
||||
SHA384_CTX ctx;
|
||||
unsigned char dgst[SHA384_DIGEST_SIZE];
|
||||
unsigned char *dgstbuf = NULL;
|
||||
uint8_t dgst[SHA384_DIGEST_SIZE];
|
||||
uint8_t dgstbuf[SHA384_DIGEST_SIZE];
|
||||
size_t dgstlen;
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < 7; i++) {
|
||||
|
||||
if (!(dgstbuf = OPENSSL_hexstr2buf(tests[i].dgsthex, NULL))) {
|
||||
goto end;
|
||||
}
|
||||
hex_to_bytes(tests[i].dgsthex, strlen(tests[i].dgsthex), dgstbuf, &dgstlen);
|
||||
|
||||
sha384_init(&ctx);
|
||||
for (j = 0; j < tests[i].count; j++) {
|
||||
sha384_update(&ctx, (unsigned char *)tests[i].data, tests[i].length);
|
||||
sha384_update(&ctx, (uint8_t *)tests[i].data, tests[i].length);
|
||||
}
|
||||
sha384_finish(&ctx, dgst);
|
||||
|
||||
@@ -132,12 +131,7 @@ int main(void)
|
||||
} else {
|
||||
printf("sha384 test %lu ok\n", i+1);
|
||||
}
|
||||
|
||||
free(dgstbuf);
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/hex.h>
|
||||
#include <gmssl/sha2.h>
|
||||
|
||||
@@ -106,19 +107,17 @@ int main(void)
|
||||
{
|
||||
int err = 0;
|
||||
SHA512_CTX ctx;
|
||||
unsigned char dgst[SHA512_DIGEST_SIZE];
|
||||
unsigned char *dgstbuf = NULL;
|
||||
uint8_t dgst[SHA512_DIGEST_SIZE];
|
||||
uint8_t dgstbuf[SHA512_DIGEST_SIZE];
|
||||
size_t dgstlen;
|
||||
size_t i, j;
|
||||
|
||||
for (i = 0; i < 7; i++) {
|
||||
|
||||
if (!(dgstbuf = OPENSSL_hexstr2buf(tests[i].dgsthex, NULL))) {
|
||||
goto end;
|
||||
}
|
||||
hex_to_bytes(tests[i].dgsthex, strlen(tests[i].dgsthex), dgstbuf, &dgstlen);
|
||||
|
||||
sha512_init(&ctx);
|
||||
for (j = 0; j < tests[i].count; j++) {
|
||||
sha512_update(&ctx, (unsigned char *)tests[i].data, tests[i].length);
|
||||
sha512_update(&ctx, (uint8_t *)tests[i].data, tests[i].length);
|
||||
}
|
||||
sha512_finish(&ctx, dgst);
|
||||
|
||||
@@ -133,12 +132,7 @@ int main(void)
|
||||
} else {
|
||||
printf("sha512 test %lu ok\n", i+1);
|
||||
}
|
||||
|
||||
free(dgstbuf);
|
||||
dgstbuf = NULL;
|
||||
}
|
||||
|
||||
end:
|
||||
if (dgstbuf) free(dgstbuf);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <gmssl/sm3.h>
|
||||
#include <gmssl/hex.h>
|
||||
|
||||
@@ -178,17 +179,15 @@ int main(int argc, char **argv)
|
||||
{
|
||||
int err = 0;
|
||||
char *p;
|
||||
unsigned char *testbuf = NULL;
|
||||
unsigned char *dgstbuf = NULL;
|
||||
uint8_t testbuf[sizeof(testhex)/2];
|
||||
uint8_t dgstbuf[32];
|
||||
size_t testbuflen, dgstbuflen;
|
||||
unsigned char dgst[32];
|
||||
uint8_t 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;
|
||||
}
|
||||
hex_to_bytes(testhex[i], strlen(testhex[i]), testbuf, &testbuflen);
|
||||
hex_to_bytes(dgsthex[i], strlen(dgsthex[i]), dgstbuf, &dgstbuflen);
|
||||
|
||||
sm3_digest(testbuf, testbuflen, dgst);
|
||||
|
||||
@@ -205,15 +204,7 @@ int main(int argc, char **argv)
|
||||
} 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;
|
||||
}
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
Reference in New Issue
Block a user