Exemplo n.º 1
0
// Input:  priv_key = buffer of 200 bytes
//         pub_key  = buffer of 200 bytes
// Output: priv_key = Your private key
//         pub_key  = Your public key
int DH1080_gen(char *priv_key, char *pub_key)
{
    unsigned char raw_buf[256]; //, iniHash[33];
    //unsigned long seed;
    int iRet, i;
    size_t len;

    mpz_t b_privkey, b_pubkey, b_base;
    unsigned char temp[DH1080_PRIME_BYTES];
    //FILE *hRnd;

    priv_key[0]='0';
    priv_key[1]='\0';
    pub_key[0]='0';
    pub_key[1]='\0';

    mpz_init(b_privkey);
    mpz_init(b_pubkey);
    mpz_init_set_ui(b_base, 2);

    do
    {
        for(i=0; i < DH1080_PRIME_BYTES; i++)
            temp[i] = (unsigned char)rand(&csprng);
        mpz_import(b_privkey, DH1080_PRIME_BYTES, 1, 1, 0, 0, temp);
        mpz_mod(b_privkey, b_privkey, b_prime1080); /* [2, prime1080-1] */
    } while( mpz_cmp_ui(b_privkey, 1) != 1); /* while smaller than 2 */

    mpz_powm(b_pubkey, b_base, b_privkey, b_prime1080);

    if(DH_verifyPubKey(b_pubkey))
    {
        mpz_export(raw_buf, &len, 1, 1, 0, 0, b_privkey);
        mpz_clear(b_privkey);
        htob64((char *)raw_buf, priv_key, len);

        mpz_export(raw_buf, &len, 1, 1, 0, 0, b_pubkey);
        htob64((char *)raw_buf, pub_key, len);

        iRet=1;
    }
    else iRet=0;

    ZeroMemory(raw_buf, sizeof(raw_buf));

    mpz_clear(b_pubkey);
    mpz_clear(b_base);

    return iRet;
}
Exemplo n.º 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;
    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;
}
Exemplo n.º 3
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;
}
Exemplo n.º 4
0
// Input:  priv_key = buffer of 200 bytes
//         pub_key  = buffer of 200 bytes
// Output: priv_key = Your private key
//         pub_key  = Your public key
int DH1080_gen(char *priv_key, char *pub_key)
{
	unsigned char raw_buf[160], iniHash[33];
	unsigned long seed;
	int len, iRet;

	big b_privkey, b_pubkey;
	csprng myRNG;

	FILE *hRnd;

	priv_key[0]='0';
	priv_key[1]='\0';
	pub_key[0]='0';
	pub_key[1]='\0';
	hRnd = fopen("/dev/urandom", "r");	// don't use /dev/random, it's a blocking device
	if(!hRnd) return 0;

	b_privkey=mirvar(0);
	b_pubkey=mirvar(0);

	// #*#*#*#*#* RNG START #*#*#*#*#*
	time((time_t *)&seed);

	seed ^= (long)hRnd << 16;
	if(fread(raw_buf, 1, sizeof(raw_buf), hRnd) < 32)
	{
		ZeroMemory(raw_buf, sizeof(raw_buf));
		fclose(hRnd);
		mirkill(b_privkey);
		mirkill(b_pubkey);

		return 0;
	}
	fclose(hRnd);

	sha_file(iniPath, iniHash);
	memXOR(raw_buf+128, iniHash, 32);
	sha_file((unsigned char *)get_irssi_config(), iniHash);
	memXOR(raw_buf+128, iniHash, 32);
	ZeroMemory(iniHash, sizeof(iniHash));
	// first 128 byte in raw_buf: output from /dev/urandom
	// last 32 byte in raw_buf: SHA-256 digest from blow.ini and irssi.conf

	seed *= (unsigned long)mip;
	strong_init(&myRNG, sizeof(raw_buf), raw_buf, (unsigned int)seed);
	strong_rng(&myRNG);
	strong_bigdig(&myRNG, 1080, 2, b_privkey);
	strong_kill(&myRNG);
	seed=0;
	// #*#*#*#*#* RNG END #*#*#*#*#*

	powltr(2, b_privkey, b_prime1080, b_pubkey);

	if(DH_verifyPubKey(b_pubkey))
	{
		len=big_to_bytes(sizeof(raw_buf), b_privkey, raw_buf, FALSE);
		htob64(raw_buf, priv_key, len);

		len=big_to_bytes(sizeof(raw_buf), b_pubkey, raw_buf, FALSE);
		htob64(raw_buf, pub_key, len);

		iRet=1;
	}
	else iRet=0;

	ZeroMemory(raw_buf, sizeof(raw_buf));

	mirkill(b_privkey);
	mirkill(b_pubkey);

	return iRet;
}