mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-05-13 03:46:26 +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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user