📄 dl_parm.cpp
字号:
/************************************************** Discrete Logarithm Parameters Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/dl_parm.h>#include <botan/primes.h>#include <botan/numthry.h>namespace Botan {/************************************************** DL_Group Constructor **************************************************/DL_Group::DL_Group(u32bit pbits, PrimeType type) { if(pbits < 64) throw Invalid_Argument("DL_Group: prime size too small"); if(type == Strong) group_type = DH_Group; else group_type = DSA_Group; if(type == Strong) { p = random_safe_prime(pbits); q = (p - 1) / 2; g = 2; } else if(type == Prime_Subgroup || type == DSA_Kosherizer) { if(type == Prime_Subgroup) { u32bit qbits = 2 * dl_work_factor(pbits); q = random_prime(qbits); BigInt q_2 = 2 * q; do p = random_integer(pbits - qbits - 1) * q_2 + 1; while(p.bits() != pbits || !is_prime(p)); } else generate_dsa_primes(p, q, pbits); BigInt e = (p - 1) / q; u32bit prime_no = 0; do g = power_mod(PRIMES[prime_no++], e, p); while(g == 1); } }/************************************************** DL_Group Constructor **************************************************/DL_Group::DL_Group(const SecureVector<byte>& seed, u32bit pbits, u32bit start) { if(!generate_dsa_primes(p, q, seed, seed.size(), pbits, start)) throw Invalid_Argument("DL_Group: The seed/counter given does not " "generate a DSA group"); BigInt e = (p - 1) / q; u32bit prime_no = 0; do g = power_mod(PRIMES[prime_no++], e, p); while(g == 1); group_type = DSA_Group; }/************************************************** DL_Group Constructor **************************************************/DL_Group::DL_Group(const BigInt& p1, const BigInt& g1) { if(p1 < 3 || g1 <= 1 || g1 >= p1) throw Invalid_Argument("DL parameters: Invalid prime or generator"); p = p1; g = g1; q = (p1 - 1) / 2; group_type = DH_Group; }/************************************************** DL_Group Constructor **************************************************/DL_Group::DL_Group(const BigInt& p1, const BigInt& q1, const BigInt& g1) { if(p1 < 3 || q1 < 3 || q1 >= p1 || g1 <= 1 || g1 >= p1) throw Invalid_Argument("DL parameters: Invalid prime(s) or generator"); p = p1; g = g1; q = q1; if((p - 1) / 2 == q1) group_type = DH_Group; else group_type = DSA_Group; }/************************************************** Verify the parameters **************************************************/bool DL_Group::verify() const { if(!verify_prime(p) || !verify_prime(q)) return false; if((p - 1) % q != 0) return false; return true; }/************************************************** Return the prime **************************************************/const BigInt& DL_Group::get_p() const { return p; }/************************************************** Return the subgroup **************************************************/const BigInt& DL_Group::get_q() const { return q; }/************************************************** Return the generator **************************************************/const BigInt& DL_Group::get_g() const { return g; }/************************************************** See if the prime is st p = 2*q + 1, q prime **************************************************/DL_Group::GroupType DL_Group::type() const { return group_type; }/************************************************** See if the prime is st p = 2*q + 1, q prime **************************************************/bool DL_Group::prime_is_safe() const { if(type() == DH_Group) return true; else return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -