Exemple #1
0
// Input:  MyPrivKey = Your private key
//         HisPubKey = Someones public key
// Output: MyPrivKey has been destroyed for security reasons
//         HisPubKey = the secret key
int DH1080_comp(char *MyPrivKey, char *HisPubKey)
{
    //int i=0;
    int iRet;
    unsigned char SHA256digest[35], base64_tmp[160];
    mpz_t b_myPrivkey, b_HisPubkey, b_theKey;
    size_t len;

    // Verify base64 strings
    if((strspn(MyPrivKey, B64ABC) != strlen(MyPrivKey)) || (strspn(HisPubKey, B64ABC) != strlen(HisPubKey)))
    {
        memset(MyPrivKey, 0x20, strlen(MyPrivKey));
        memset(HisPubKey, 0x20, strlen(HisPubKey));
        return 0;
    }

    mpz_init(b_HisPubkey);
    mpz_init(b_theKey);

    len=b64toh(HisPubKey, (char *)base64_tmp);
    mpz_import(b_HisPubkey, len, 1, 1, 0, 0, base64_tmp);

    if(DH_verifyPubKey(b_HisPubkey))
    {
        mpz_init(b_myPrivkey);

        len=b64toh(MyPrivKey, (char *)base64_tmp);
        mpz_import(b_myPrivkey, len, 1, 1, 0, 0, base64_tmp);
        memset(MyPrivKey, 0x20, strlen(MyPrivKey));

        mpz_powm(b_theKey, b_HisPubkey, b_myPrivkey, b_prime1080);
        mpz_clear(b_myPrivkey);

        mpz_export(base64_tmp, &len, 1, 1, 0, 0, b_theKey);
        SHA256_memory((char *)base64_tmp, len, (char *)SHA256digest);
        htob64((char *)SHA256digest, HisPubKey, 32);

        iRet=1;
    }
    else iRet=0;


    ZeroMemory(base64_tmp, sizeof(base64_tmp));
    ZeroMemory(SHA256digest, sizeof(SHA256digest));

    mpz_clear(b_theKey);
    mpz_clear(b_HisPubkey);

    return iRet;
}
Exemple #2
0
// Input:  MyPrivKey = Your private key
//         HisPubKey = Someones public key
// Output: MyPrivKey has been destroyed for security reasons
//         HisPubKey = the secret key
int DH1080_comp(char *MyPrivKey, char *HisPubKey)
{
	int i=0, len, iRet;
	unsigned char SHA256digest[35], base64_tmp[160];
	big b_myPrivkey, b_HisPubkey, b_theKey;

	// Verify base64 strings
	if((strspn(MyPrivKey, B64ABC) != strlen(MyPrivKey)) || (strspn(HisPubKey, B64ABC) != strlen(HisPubKey)))
	{
		memset(MyPrivKey, 0x20, strlen(MyPrivKey));
		memset(HisPubKey, 0x20, strlen(HisPubKey));
		return 0;
	}

	b_HisPubkey=mirvar(0);
	b_theKey=mirvar(0);


	len=b64toh(HisPubKey, base64_tmp);
	bytes_to_big(len, base64_tmp, b_HisPubkey);

	if(DH_verifyPubKey(b_HisPubkey))
	{
		b_myPrivkey=mirvar(0);

		len=b64toh(MyPrivKey, base64_tmp);
		bytes_to_big(len, base64_tmp, b_myPrivkey);
		memset(MyPrivKey, 0x20, strlen(MyPrivKey));

		powmod(b_HisPubkey, b_myPrivkey, b_prime1080, b_theKey);
		mirkill(b_myPrivkey);

		len=big_to_bytes(sizeof(base64_tmp), b_theKey, base64_tmp, FALSE);
		SHA256_memory(base64_tmp, len, SHA256digest);
		htob64(SHA256digest, HisPubKey, 32);

		iRet=1;
	}
	else iRet=0;


	ZeroMemory(base64_tmp, sizeof(base64_tmp));
	ZeroMemory(SHA256digest, sizeof(SHA256digest));

	mirkill(b_theKey);
	mirkill(b_HisPubkey);

	return iRet;
}