You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
335 lines
11 KiB
335 lines
11 KiB
#include "daa-test.h"
|
|
|
|
int test1();
|
|
int test2();
|
|
int test3();
|
|
|
|
int main(int argc, char **argv) {
|
|
if(0 != test1()) {
|
|
printf("test1 failed\n");
|
|
} else {
|
|
printf("test1 succeeded\n");
|
|
}
|
|
if(0 != test2()) {
|
|
printf("test2 failed\n");
|
|
} else {
|
|
printf("test2 succeeded\n");
|
|
}
|
|
if(0 != test3()) {
|
|
printf("test3 failed\n");
|
|
} else {
|
|
printf("test3 succeeded\n");
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int test1() {
|
|
struct ecdaa_issuer_public_key_FP256BN ipk;
|
|
struct ecdaa_issuer_secret_key_FP256BN isk;
|
|
struct ecdaa_member_public_key_FP256BN mpk;
|
|
struct ecdaa_member_secret_key_FP256BN msk;
|
|
uint8_t nonce[NONCE_SIZE];
|
|
struct ecdaa_credential_FP256BN cred;
|
|
struct ecdaa_credential_FP256BN_signature cred_sig;
|
|
struct ecdaa_signature_FP256BN sig;
|
|
uint8_t bsn[MAX_BSNSIZE];
|
|
size_t bsn_len = 0;
|
|
uint8_t msg[MAX_MSGSIZE];
|
|
size_t msg_len = 0;
|
|
struct ecdaa_revocations_FP256BN revocations;
|
|
bzero(bsn, MAX_BSNSIZE);
|
|
bzero(msg, MAX_MSGSIZE);
|
|
|
|
if(0 != ecdaa_issuer_key_pair_FP256BN_generate(&ipk, &isk, ecdaa_rand)) {
|
|
printf("generate issuer key pair failed\n");
|
|
return 1;
|
|
}
|
|
ecdaa_rand(nonce, NONCE_SIZE);
|
|
if(0 != ecdaa_member_key_pair_FP256BN_generate(&mpk, &msk, nonce, NONCE_SIZE, ecdaa_rand)) {
|
|
printf("generate member key pair failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_credential_FP256BN_generate(&cred, &cred_sig, &isk, &mpk, ecdaa_rand)) {
|
|
printf("generate credential failed\n");
|
|
return 1;
|
|
}
|
|
strncpy(msg, "test message\n", 13);
|
|
msg_len = strlen(msg);
|
|
|
|
if(0 != ecdaa_credential_FP256BN_validate(&cred, &cred_sig, &mpk, &ipk.gpk)) {
|
|
printf("credential verification failed\n");
|
|
return 1;
|
|
}
|
|
strncpy(bsn, "test basename", 13);
|
|
bsn_len = strlen(bsn);
|
|
|
|
if(0 != ecdaa_signature_FP256BN_sign(&sig, msg, msg_len, bsn, bsn_len, &msk, &cred, ecdaa_rand)) {
|
|
printf("signing message with bsn failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_signature_FP256BN_verify(&sig, &ipk.gpk, &revocations, msg, msg_len, bsn, bsn_len)) {
|
|
printf("verify signature with bsn failed\n");
|
|
return 1;
|
|
}
|
|
|
|
bzero(bsn, MAX_BSNSIZE);
|
|
bsn_len = 0;
|
|
if(0 != ecdaa_signature_FP256BN_sign(&sig, msg, msg_len, NULL, bsn_len, &msk, &cred, ecdaa_rand)) {
|
|
printf("signing message failed\n");
|
|
return 1;
|
|
}
|
|
|
|
if(0 != ecdaa_signature_FP256BN_verify(&sig, &ipk.gpk, &revocations, msg, msg_len, bsn, bsn_len)) {
|
|
printf("verify message failed\n");
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int test2() {
|
|
struct ecdaa_issuer_public_key_FP256BN ipk;
|
|
struct ecdaa_issuer_secret_key_FP256BN isk;
|
|
struct ecdaa_member_public_key_FP256BN mpk;
|
|
struct ecdaa_member_secret_key_FP256BN msk;
|
|
struct ecdaa_credential_FP256BN cred;
|
|
struct ecdaa_credential_FP256BN_signature cred_sig;
|
|
struct ecdaa_signature_FP256BN sig;
|
|
uint8_t nonce[NONCE_SIZE];
|
|
struct ecdaa_issuer_public_key_FP256BN ipk2;
|
|
struct ecdaa_issuer_secret_key_FP256BN isk2;
|
|
struct ecdaa_member_public_key_FP256BN mpk2;
|
|
struct ecdaa_member_secret_key_FP256BN msk2;
|
|
struct ecdaa_credential_FP256BN cred2;
|
|
struct ecdaa_credential_FP256BN_signature cred_sig2;
|
|
struct ecdaa_signature_FP256BN sig2;
|
|
uint8_t bsn[MAX_BSNSIZE];
|
|
size_t bsn_len = 0;
|
|
uint8_t msg[MAX_MSGSIZE];
|
|
size_t msg_len = 0;
|
|
struct ecdaa_revocations_FP256BN revocations;
|
|
uint8_t binbuf[MAX_BUFSIZE];
|
|
char buffer[MAX_BUFSIZE];
|
|
bzero(binbuf,MAX_BUFSIZE);
|
|
bzero(buffer,MAX_BUFSIZE);
|
|
bzero(bsn, MAX_BSNSIZE);
|
|
bzero(msg, MAX_MSGSIZE);
|
|
|
|
if(0 != ecdaa_issuer_key_pair_FP256BN_generate(&ipk, &isk, ecdaa_rand)) {
|
|
printf("generate issuer key pair failed\n");
|
|
return 1;
|
|
}
|
|
|
|
if(0 != ecdaa_issuer_public_key_FP256BN_serialize_file("ipktest.bin", &ipk) ||
|
|
0 != ecdaa_issuer_secret_key_FP256BN_serialize_file("isktest.bin", &isk)) {
|
|
printf("saving issuer key-pair to disk failed\n");
|
|
return 1;
|
|
}
|
|
|
|
if(0 != ecdaa_issuer_public_key_FP256BN_deserialize_file(&ipk2, "ipktest.bin") ||
|
|
0 != ecdaa_issuer_secret_key_FP256BN_deserialize_file(&isk2, "isktest.bin")) {
|
|
printf("loading issuer key pair failed\n");
|
|
return 1;
|
|
}
|
|
|
|
ecdaa_rand(nonce, NONCE_SIZE);
|
|
if(0 != ecdaa_member_key_pair_FP256BN_generate(&mpk, &msk, nonce, NONCE_SIZE, ecdaa_rand)) {
|
|
printf("generate member key pair failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_member_public_key_FP256BN_serialize_file("mpktest.bin", &mpk) ||
|
|
0 != ecdaa_member_secret_key_FP256BN_serialize_file("msktest.bin", &msk)) {
|
|
printf("saving member key-pair to disk failed\n");
|
|
return 1;
|
|
}
|
|
|
|
if(0 != ecdaa_member_public_key_FP256BN_deserialize_file(&mpk2, "mpktest.bin", nonce, NONCE_SIZE) ||
|
|
0 != ecdaa_member_secret_key_FP256BN_deserialize_file(&msk2, "msktest.bin")) {
|
|
printf("loading member key pair failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_credential_FP256BN_generate(&cred, &cred_sig, &isk2, &mpk2, ecdaa_rand)) {
|
|
printf("generate credential failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_credential_FP256BN_serialize_file("credtest.bin", &cred)) {
|
|
printf("saving credential to disk failed\n");
|
|
}
|
|
if(0 != ecdaa_credential_FP256BN_deserialize_file(&cred2, "credtest.bin")) {
|
|
printf("loading credential failed\n");
|
|
}
|
|
|
|
strncpy(msg, "test message\n", 13);
|
|
msg_len = strlen(msg);
|
|
bsn_len = 0;
|
|
if(0 != ecdaa_signature_FP256BN_sign(&sig, msg, msg_len, NULL, bsn_len, &msk2, &cred2, ecdaa_rand)) {
|
|
printf("signing message failed\n");
|
|
return 1;
|
|
}
|
|
|
|
if(0 != ecdaa_signature_FP256BN_serialize_file("sigtest.bin", &sig, 0)) {
|
|
printf("saving signature to disk failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_signature_FP256BN_deserialize_file(&sig2, "sigtest.bin", 0)) {
|
|
printf("loading signature failed\n");
|
|
return 1;
|
|
}
|
|
|
|
if(0 != ecdaa_signature_FP256BN_verify(&sig2, &ipk2.gpk, &revocations, msg, msg_len, bsn, bsn_len)) {
|
|
printf("verify message failed\n");
|
|
return 1;
|
|
}
|
|
|
|
strncpy(bsn, "test basename", 13);
|
|
bsn_len = strlen(bsn);
|
|
|
|
if(0 != ecdaa_signature_FP256BN_sign(&sig, msg, msg_len, bsn, bsn_len, &msk2, &cred2, ecdaa_rand)) {
|
|
printf("signing message with bsn failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_signature_FP256BN_serialize_file("sigtest.bin", &sig, 1)) {
|
|
printf("saving signature to disk failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_signature_FP256BN_deserialize_file(&sig2, "sigtest.bin", 1)) {
|
|
printf("loading signature failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_signature_FP256BN_verify(&sig2, &ipk2.gpk, &revocations, msg, msg_len, bsn, bsn_len)) {
|
|
printf("verify signature with bsn failed\n");
|
|
return 1;
|
|
}
|
|
return 0;
|
|
|
|
}
|
|
|
|
int test3() {
|
|
struct ecdaa_issuer_public_key_FP256BN ipk;
|
|
struct ecdaa_issuer_secret_key_FP256BN isk;
|
|
struct ecdaa_member_public_key_FP256BN mpk;
|
|
struct ecdaa_member_secret_key_FP256BN msk;
|
|
struct ecdaa_credential_FP256BN cred;
|
|
struct ecdaa_credential_FP256BN_signature cred_sig;
|
|
struct ecdaa_signature_FP256BN sig;
|
|
uint8_t nonce[NONCE_SIZE];
|
|
struct ecdaa_issuer_public_key_FP256BN ipk2;
|
|
struct ecdaa_issuer_secret_key_FP256BN isk2;
|
|
struct ecdaa_member_public_key_FP256BN mpk2;
|
|
struct ecdaa_member_secret_key_FP256BN msk2;
|
|
struct ecdaa_credential_FP256BN cred2;
|
|
struct ecdaa_credential_FP256BN_signature cred_sig2;
|
|
struct ecdaa_signature_FP256BN sig2;
|
|
uint8_t bsn[MAX_BSNSIZE];
|
|
size_t bsn_len = 0;
|
|
uint8_t msg[MAX_MSGSIZE];
|
|
size_t msg_len = 0;
|
|
struct ecdaa_revocations_FP256BN revocations;
|
|
uint8_t binbuf[MAX_BUFSIZE];
|
|
char buffer[MAX_BUFSIZE];
|
|
bzero(binbuf,MAX_BUFSIZE);
|
|
bzero(buffer,MAX_BUFSIZE);
|
|
bzero(bsn, MAX_BSNSIZE);
|
|
bzero(msg, MAX_MSGSIZE);
|
|
char *current = buffer;
|
|
if(0 != ecdaa_issuer_key_pair_FP256BN_generate(&ipk, &isk, ecdaa_rand)) {
|
|
printf("generate issuer key pair failed\n");
|
|
return 1;
|
|
}
|
|
|
|
ecdaa_issuer_public_key_FP256BN_serialize(binbuf, &ipk);
|
|
int numbytes = ecdaa_encode(binbuf, current, ECDAA_ISSUER_PUBLIC_KEY_FP256BN_LENGTH);
|
|
current = ¤t[numbytes];
|
|
|
|
bzero(binbuf,MAX_BUFSIZE);
|
|
ecdaa_issuer_secret_key_FP256BN_serialize(binbuf, &isk);
|
|
numbytes += ecdaa_encode(binbuf, current, ECDAA_ISSUER_SECRET_KEY_FP256BN_LENGTH);
|
|
//current = &buffer[numbytes];
|
|
|
|
printf("Encoded issuer key pair: %s\n%s\n", buffer, current);
|
|
|
|
current = buffer;
|
|
int numbytes2 = ecdaa_decode(current, binbuf, ECDAA_ISSUER_PUBLIC_KEY_FP256BN_LENGTH);
|
|
int ret = ecdaa_issuer_public_key_FP256BN_deserialize(&ipk2, binbuf);
|
|
|
|
current = &buffer[numbytes2];
|
|
bzero(binbuf,MAX_BUFSIZE);
|
|
numbytes2 += ecdaa_decode(current, binbuf, ECDAA_ISSUER_SECRET_KEY_FP256BN_LENGTH);
|
|
ret += ecdaa_issuer_secret_key_FP256BN_deserialize(&isk2, binbuf);
|
|
if(0 != ret) {
|
|
printf("numbytes = %i, numbytes2 = %i\n", numbytes, numbytes2);
|
|
printf("loading issuer key pair failed\n");
|
|
return 1;
|
|
}
|
|
|
|
ecdaa_rand(nonce, NONCE_SIZE);
|
|
if(0 != ecdaa_member_key_pair_FP256BN_generate(&mpk, &msk, nonce, NONCE_SIZE, ecdaa_rand)) {
|
|
printf("generate member key pair failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_member_public_key_FP256BN_serialize_file("mpktest.bin", &mpk) ||
|
|
0 != ecdaa_member_secret_key_FP256BN_serialize_file("msktest.bin", &msk)) {
|
|
printf("saving member key-pair to disk failed\n");
|
|
return 1;
|
|
}
|
|
|
|
if(0 != ecdaa_member_public_key_FP256BN_deserialize_file(&mpk2, "mpktest.bin", nonce, NONCE_SIZE) ||
|
|
0 != ecdaa_member_secret_key_FP256BN_deserialize_file(&msk2, "msktest.bin")) {
|
|
printf("loading member key pair failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_credential_FP256BN_generate(&cred, &cred_sig, &isk2, &mpk2, ecdaa_rand)) {
|
|
printf("generate credential failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_credential_FP256BN_serialize_file("credtest.bin", &cred)) {
|
|
printf("saving credential to disk failed\n");
|
|
}
|
|
if(0 != ecdaa_credential_FP256BN_deserialize_file(&cred2, "credtest.bin")) {
|
|
printf("loading credential failed\n");
|
|
}
|
|
|
|
strncpy(msg, "test message\n", 13);
|
|
msg_len = strlen(msg);
|
|
bsn_len = 0;
|
|
|
|
if(0 != ecdaa_signature_FP256BN_sign(&sig, msg, msg_len, NULL, bsn_len, &msk2, &cred2, ecdaa_rand)) {
|
|
printf("signing message failed\n");
|
|
return 1;
|
|
}
|
|
|
|
if(0 != ecdaa_signature_FP256BN_serialize_file("sigtest.bin", &sig, 0)) {
|
|
printf("saving signature to disk failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_signature_FP256BN_deserialize_file(&sig2, "sigtest.bin", 0)) {
|
|
printf("loading signature failed\n");
|
|
return 1;
|
|
}
|
|
|
|
if(0 != ecdaa_signature_FP256BN_verify(&sig2, &ipk2.gpk, &revocations, msg, msg_len, bsn, bsn_len)) {
|
|
printf("verify message failed\n");
|
|
return 1;
|
|
}
|
|
|
|
strncpy(bsn, "test basename", 13);
|
|
bsn_len = strlen(bsn);
|
|
|
|
if(0 != ecdaa_signature_FP256BN_sign(&sig, msg, msg_len, bsn, bsn_len, &msk2, &cred2, ecdaa_rand)) {
|
|
printf("signing message with bsn failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_signature_FP256BN_serialize_file("sigtest.bin", &sig, 1)) {
|
|
printf("saving signature to disk failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_signature_FP256BN_deserialize_file(&sig2, "sigtest.bin", 1)) {
|
|
printf("loading signature failed\n");
|
|
return 1;
|
|
}
|
|
if(0 != ecdaa_signature_FP256BN_verify(&sig2, &ipk2.gpk, &revocations, msg, msg_len, bsn, bsn_len)) {
|
|
printf("verify signature with bsn failed\n");
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|