📄 dh.cpp
字号:
/************************************************** Diffie-Hellman Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/dh.h>#include <botan/numthry.h>namespace Botan {/************************************************** DH_PublicKey Constructor **************************************************/DH_PublicKey::DH_PublicKey(const DL_Group& domain, const BigInt& key) : group(domain), p(group.get_p()), g(group.get_g()), y(key) { if(group.type() != DL_Group::DH_Group) throw Invalid_Argument("DH: Domain is not a DH-style group"); if(y < 0 || y >= p) throw Invalid_Argument("DH_PublicKey: Invalid public value"); }/************************************************** DH_PublicKey Copy Constructor **************************************************/DH_PublicKey::DH_PublicKey(const DH_PublicKey& key) : group(key.group), p(group.get_p()), g(group.get_g()), y(key.get_y()) { }/************************************************** Return the public value for key agreement **************************************************/SecureVector<byte> DH_PublicKey::public_value() const { return encode(y); }/************************************************** Check Public DH Parameters **************************************************/bool DH_PublicKey::check_params() const { if(y < 2 || y >= p) return false; if(!group.verify()) return false; return true; }/************************************************** DH_PrivateKey Constructor **************************************************/DH_PrivateKey::DH_PrivateKey(const DL_Group& domain) : DH_PublicKey(domain, 0) { x = random_integer(2 * dl_work_factor(p.bits())); powermod_x_p = FixedExponent_Exp(x, p); y = powermod_x_p(g); }/************************************************** DH_PrivateKey Constructor **************************************************/DH_PrivateKey::DH_PrivateKey(const DL_Group& domain, const BigInt& priv_key, const BigInt& pub_key) : DH_PublicKey(domain, pub_key), x(priv_key), powermod_x_p(x, p) { if(y == 0) y = powermod_x_p(g); if(x <= 1 || y <= 1 || x >= p || y >= p) throw Invalid_Argument("DH_PrivateKey: invalid private key"); }/************************************************** Return the public value for key agreement **************************************************/SecureVector<byte> DH_PrivateKey::public_value() const { return DH_PublicKey::public_value(); }/************************************************** Check Private DH Parameters **************************************************/bool DH_PrivateKey::check_params() const { if(!DH_PublicKey::check_params()) return false; if(x < 1 || x >= p) return false; if(y != powermod_x_p(g)) return false; return true; }/************************************************** Derive a key **************************************************/SecureVector<byte> DH_PrivateKey::derive_key(const byte w[], u32bit w_len) const { return derive_key(decode(w, w_len)); }/************************************************** Derive a key **************************************************/SecureVector<byte> DH_PrivateKey::derive_key(const DH_PublicKey& key) const { return derive_key(key.get_y()); }/************************************************** Derive a key **************************************************/SecureVector<byte> DH_PrivateKey::derive_key(const BigInt& w) const { if(w <= 1 || w >= p) throw Invalid_Argument("DH_PrivateKey::derive_key: Invalid key input"); return encode(powermod_x_p(w)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -