int ECDH_set_method(EC_KEY *eckey, const ECDH_METHOD *meth) { ECDH_DATA *ecdh; ecdh = ecdh_check(eckey); if (ecdh == NULL) return 0; #if 0 mtmp = ecdh->meth; if (mtmp->finish) mtmp->finish(eckey); #endif #ifndef OPENSSL_NO_ENGINE if (ecdh->engine) { ENGINE_finish(ecdh->engine); ecdh->engine = NULL; } #endif ecdh->meth = meth; #if 0 if (meth->init) meth->init(eckey); #endif return 1; }
void *ECDH_get_ex_data(EC_KEY *d, int idx) { ECDH_DATA *ecdh; ecdh = ecdh_check(d); if (ecdh == NULL) return NULL; return (CRYPTO_get_ex_data(&ecdh->ex_data, idx)); }
int ECDH_set_ex_data(EC_KEY *d, int idx, void *arg) { ECDH_DATA *ecdh; ecdh = ecdh_check(d); if (ecdh == NULL) return 0; return (CRYPTO_set_ex_data(&ecdh->ex_data, idx, arg)); }
int ECDH_compute_key(void *out, size_t outlen, const EC_POINT *pub_key, EC_KEY *eckey, void *(*KDF)(const void *in, size_t inlen, void *out, size_t *outlen)) { ECDH_DATA *ecdh = ecdh_check(eckey); if (ecdh == NULL) return 0; return ecdh->meth->compute_key(out, outlen, pub_key, eckey, KDF); }
// 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; }