Exemplo n.º 1
0
// Generate a Shared secret key from 
BIGNUM * CAlphaCrypt::GenerateSharedSecretKey(BIGNUM * pMasterKey, EC_POINT * lpPeerPubKey) {
	EC_KEY * lpFullCurve = NULL;				// Full elliptic curve
	EC_POINT * pubKey = NULL;					// The peer public key (bad guys one)
	ECDH_DATA * ecdh_data = NULL;				// Elliptic Curve data structure
	BYTE secretKey[0x20] = {0};					// Shared secret key
	BIGNUM * pSecretBn = NULL;					// Secret shared key BIGNUM
	int iRet = 0;

	if (!lpPeerPubKey)
		// Get the default AlphaCrypt peer public key
		pubKey = GetAlphaCryptPublicKey();		// DON'T forget to delete it, damn heck! :-)
	else
		// Don't delete the following one:
		pubKey = lpPeerPubKey;

	if (!pubKey) return NULL;

	// Create the FULL curve that contains public/private key pair
	lpFullCurve = EC_KEY_new_by_curve_name(NID_secp256k1);
	//EC_KEY_set_public_key(lpFullCurve, pStartKey);			// No my own public key (I need to calculate it)
	iRet = SetPrivateKey(lpFullCurve, pMasterKey);
	iRet = EC_KEY_check_key(lpFullCurve);

	// Compute the shared secret key
	ecdh_data = ecdh_check(lpFullCurve);
	if (ecdh_data)
		ecdh_data->meth = ECDH_OpenSSL();
	// Calculate shared secret key: My private Key * Peer public key
	iRet = ECDH_compute_key(secretKey, 0x20, pubKey, lpFullCurve, NULL);

	// Convert the secret key in a BIGNUMBER 
	pSecretBn = BN_bin2bn(secretKey, 0x20, NULL);

	/*////////////////////////////////////////////////////////////////////////
	//							Brief explaination:							//
		Here is what "ECDH_compute_key" does:
		Calculate "da * Qb" (that is equal to "da * db * G"). Where:
			da = my ownPrivate key (the master key)
			Qb = the peer Public key (standard one inserted in AlphaCrypt)
	*////////////////////////////////////////////////////////////////////////

	// Cleanup
	EC_KEY_free(lpFullCurve);
	if (pubKey != lpPeerPubKey)
		EC_POINT_free(pubKey);
	return pSecretBn;
}
Exemplo n.º 2
0
	void CryptoManager::RestoreKeyForAccount (IAccount *acc)
	{
		const auto pgp = qobject_cast<ISupportPGP*> (acc->GetQObject ());
		if (!pgp)
			return;

		QSettings settings { QCoreApplication::organizationName (),
			QCoreApplication::applicationName () + "_Azoth" };
		settings.beginGroup ("PrivateKeys");
		const auto& keyId = settings.value (acc->GetAccountID ()).toString ();
		settings.endGroup ();

		if (keyId.isEmpty ())
			return;

		for (const auto& key : GetPrivateKeys ())
			if (key.keyId () == keyId)
			{
				pgp->SetPrivateKey (key);
				break;
			}
	}