/************************************************* * Derive a key * *************************************************/ SecureVector<byte> DH_PrivateKey::derive_key(const BigInt& w) const { const BigInt& p = group_p(); if(w <= 1 || w >= p-1) throw Invalid_Argument(algo_name() + "::derive_key: Invalid key input"); return BigInt::encode_1363(core.agree(w), p.bytes()); }
/* * Load a DH private key */ DH_PrivateKey::DH_PrivateKey(const AlgorithmIdentifier& alg_id, const secure_vector<uint8_t>& key_bits) : DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_42) { if(m_y == 0) m_y = power_mod(group_g(), m_x, group_p()); }
NR_PrivateKey::NR_PrivateKey(const AlgorithmIdentifier& alg_id, const secure_vector<byte>& key_bits, RandomNumberGenerator& rng) : DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_57) { m_y = power_mod(group_g(), m_x, group_p()); load_check(rng); }
DSA_PrivateKey::DSA_PrivateKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits, RandomNumberGenerator& rng) : DL_Scheme_PrivateKey(alg_id, key_bits, DL_Group::ANSI_X9_57) { y = power_mod(group_g(), x, group_p()); load_check(rng); }
/************************************************* * Algorithm Specific PKCS #8 Initialization Code * *************************************************/ void DH_PrivateKey::PKCS8_load_hook(RandomNumberGenerator& rng, bool generated) { if(y == 0) y = power_mod(group_g(), x, group_p()); core = DH_Core(rng, group, x); if(generated) gen_check(rng); else load_check(rng); }
/* * Create a DH private key */ DH_PrivateKey::DH_PrivateKey(RandomNumberGenerator& rng, const DL_Group& grp, const BigInt& x_arg) { m_group = grp; if(x_arg == 0) { const BigInt& p = group_p(); m_x.randomize(rng, dl_exponent_size(p.bits())); } else { m_x = x_arg; } if(m_y == 0) { m_y = power_mod(group_g(), m_x, group_p()); } }
/* * Create a DH private key */ DH_PrivateKey::DH_PrivateKey(RandomNumberGenerator& rng, const DL_Group& grp, const BigInt& x_arg) { group = grp; x = x_arg; if(x == 0) { const BigInt& p = group_p(); x.randomize(rng, 2 * dl_work_factor(p.bits())); } if(y == 0) y = power_mod(group_g(), x, group_p()); if(x == 0) gen_check(rng); else load_check(rng); }
/************************************************* * Create a DH private key * *************************************************/ DH_PrivateKey::DH_PrivateKey(RandomNumberGenerator& rng, const DL_Group& grp, const BigInt& x_arg) { group = grp; x = x_arg; if(x == 0) { const BigInt& p = group_p(); x.randomize(rng, 2 * dl_work_factor(p.bits())); PKCS8_load_hook(rng, true); } else PKCS8_load_hook(rng, false); }
/* * Create a NR private key */ NR_PrivateKey::NR_PrivateKey(RandomNumberGenerator& rng, const DL_Group& grp, const BigInt& x_arg) { m_group = grp; m_x = x_arg; if(m_x == 0) m_x = BigInt::random_integer(rng, 2, group_q() - 1); m_y = power_mod(group_g(), m_x, group_p()); if(x_arg == 0) gen_check(rng); else load_check(rng); }
/************************************************* * Return the public value for key agreement * *************************************************/ MemoryVector<byte> DH_PublicKey::public_value() const { return BigInt::encode_1363(y, group_p().bytes()); }
/************************************************* * Return the maximum input size in bits * *************************************************/ u32bit DH_PublicKey::max_input_bits() const { return group_p().bits(); }
/* * Return the public value for key agreement */ std::vector<uint8_t> DH_PublicKey::public_value() const { return unlock(BigInt::encode_1363(m_y, group_p().bytes())); }