示例#1
0
static BOOL bign_keyunwrap(byte *X, byte *d, byte *untoken){
	Point q;
	REV_PI(X, q);
	BigInteger Q = bign_curve256v1::getQ();
	byte s0[32];
	memcpy(s0, d, sizeof s0);
	for (size_t jj = 0; jj < 32; jj += 4) change_endian(s0 + jj);
	BigInteger S0(s0, 32);
	S0 <<= 128;
	S0 %= Q;
	byte h_belt[32];
	memcpy(h_belt, H, 32);
	for (size_t jj = 0; jj <32; jj += 4) change_endian(h_belt + jj);
	BigInteger temp2(h_belt, 32);
	temp2 %= Q;
	byte _qq[32];
	memcpy(_qq, d + 32, sizeof _qq);
	for (size_t jj = 0; jj < 32; jj += 4) change_endian(_qq + jj);
	BigInteger S1(_qq, 32);
	if (S1 >= Q) return false;

	BigInteger rr = (temp2 + S1) % Q;
	BigInteger zero = BigInteger(0);
	Point G(zero, bign_curve256v1::getY());
	Point R = shamir(G, rr, q, S0);
	if (R.x == zero && R.y == zero) return false;
	byte toHash[108];
	byte bR[64];
	PI(bR, R);
	belt_hash(toHash, sizeof toHash, h_belt);
	for (size_t jj = 0; jj < 32; ++jj) if (h_belt[jj] != bR[jj]) return false;
	return true;
}
示例#2
0
static  BOOL bign_verify(byte *H, byte *_q, byte *S, uint32 size){
	Point q;
	REV_PI(_q, q);
	BigInteger Q = bign_curve256v1::getQ();
	byte s0[32];
	memcpy(s0, S, sizeof s0);
	for (size_t jj = 0; jj < 32; jj += 4) change_endian(s0 + jj);
	BigInteger S0(s0, 32);
	S0 <<= 128;
	S0 %= Q;
	byte _qq[32];
	memcpy(_qq, S + 32, sizeof _qq);
	for (size_t jj = 0; jj < 32; jj += 4) change_endian(_qq + jj);
	BigInteger S1(_qq, 32);
	if (S1 >= Q) return false;
	byte h_belt[32];
	memcpy(h_belt, H, 32);
	for (size_t jj = 0; jj <32; jj += 4) change_endian(h_belt + jj);
	BigInteger temp2(h_belt, 32);
	temp2 %= Q;
	BigInteger rr = (temp2 + S1) % Q;
	BigInteger zero = BigInteger(0);
	Point G(zero, bign_curve256v1::getY());
	Point R = shamir(G, rr, q, S0);
	if (R.x == zero && R.y == zero) return false;
	byte toHash[108];
	byte bR[64];
	PI(bR, R);
	memcpy(toHash, OID, sizeof OID);
	memcpy(toHash + sizeof OID, bR, sizeof bR);
	memcpy(toHash + sizeof OID + sizeof bR, H, 32);
	belt_hash(toHash, sizeof toHash, h_belt);
	for (size_t jj = 0; jj < 32; ++jj) if (h_belt[jj] != S[jj]) return false;
	return true;
}
int main(int argc, const char *argv[])
{
    long int x, y, g;

    printf("Shamir\n");
    for (int i = 0; i < 5; i++) {
        x = simple_rand();
        y = shamir(x);
        printf("sent:%ld, received:%ld %s\n", x, y, x == y ? "PASSED" : "FAILED");
    }

    printf("\nVernam\n");
    char f_name[] = "files/test_file";
    if (vernam_e(f_name)) {
        printf("Vernam encode ERROR\n");
    }
    f_name[15] = '\0';
    if (vernam_d(f_name)) {
        printf("Vernam decode ERROR\n");
    }
    if (!system("diff files/test_file files/test_file.vrm.key.s > /dev/null")) {
        printf("PASSED\n");
    } else {
        printf("FAILED\n");
    }

    printf("\nEl Gamal\n");
    char f_name_elg[] = "files/test_file";
    if (elgamal_e(f_name_elg)) {
        printf("RSA encode ERROR\n");
    }
    f_name_elg[15] = '\0';
    if (elgamal_d(f_name_elg)) {
        printf("RSA decode ERROR\n");
    }
    if (!system("diff files/test_file files/test_file.elg.s > /dev/null")) {
        printf("PASSED\n");
    } else {
        printf("FAILED\n");
    }

    printf("\nRSA\n");
    char f_name_rsa[] = "files/test_file";
    if (RSA_e(f_name_rsa)) {
        printf("RSA encode ERROR\n");
    }
    f_name_rsa[15] = '\0';
    if (RSA_d(f_name_rsa)) {
        printf("RSA decode ERROR\n");
    }
    if (!system("diff files/test_file files/test_file.rsa.s > /dev/null")) {
        printf("PASSED\n");
    } else {
        printf("FAILED\n");
    }

    return 0;
}