📄 dsa.cpp
字号:
/************************************************** DSA Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/dsa.h>#include <botan/numthry.h>#include <botan/primes.h>namespace Botan {/************************************************** DSA_PublicKey Constructor **************************************************/DSA_PublicKey::DSA_PublicKey(const DL_Group& domain, const BigInt& key) : group(domain), p(group.get_p()), q(group.get_q()), g(group.get_g()), y(key), powermod_g_p(g, p), powermod_y_p(y, p) { if(group.type() != DL_Group::DSA_Group) throw Invalid_Argument("DSA: Domain is not a DSA-style group"); if(y < 0 || y >= p) throw Invalid_Argument("DSA_PublicKey: Invalid public parameter"); }/************************************************** DSA_PublicKey Copy Constructor **************************************************/DSA_PublicKey::DSA_PublicKey(const DSA_PublicKey& key) : PK_Verifying_wo_MR_Key(), group(key.group), p(group.get_p()), q(group.get_q()), g(group.get_g()), y(key.get_y()), powermod_g_p(g, p), powermod_y_p(y, p) { }/************************************************** Check Public DSA Parameters **************************************************/bool DSA_PublicKey::check_params() const { if(y < 2 || y >= p) return false; if(!group.verify()) return false; return true; }/************************************************** DSA Verification Function **************************************************/bool DSA_PublicKey::verify(const byte msg[], u32bit msg_len, const byte sig[], u32bit sig_len) const { if(sig_len != 2*q.bytes() || msg_len > q.bytes()) return false; BigInt t1(msg, msg_len); BigInt r(sig, q.bytes()); BigInt s(sig + q.bytes(), q.bytes()); if(r.is_zero() || s.is_zero() || r >= q || s >= q) return false; BigInt w = inverse_mod(s, q); BigInt u1 = (w * t1) % q; BigInt u2 = (w * r) % q; BigInt v = (powermod_g_p(u1) * powermod_y_p(u2)) % p; return (v % q == r); }/************************************************** DSA_PrivateKey Constructor **************************************************/DSA_PrivateKey::DSA_PrivateKey(const DL_Group& domain) : DSA_PublicKey(domain, 0) { x = random_integer(1, q - 1); y = powermod_g_p(x); powermod_y_p = FixedBase_Exp(y, p); }/************************************************** DSA_PrivateKey Constructor **************************************************/DSA_PrivateKey::DSA_PrivateKey(const DL_Group& domain, const BigInt& priv, const BigInt& pub) : DSA_PublicKey(domain, pub), x(priv) { if(x <= 1 || x >= p) throw Invalid_Argument("DSA_PrivateKey: Invalid private parameter"); }/************************************************** Check Private DSA Parameters **************************************************/bool DSA_PrivateKey::check_params() const { if(!DSA_PublicKey::check_params()) return false; if(y != powermod_g_p(x)) return false; return true; }/************************************************** DSA Signature Operation **************************************************/SecureVector<byte> DSA_PrivateKey::sign(const byte in[], u32bit len) const { BigInt t1(in, len); while(true) { BigInt k(Random, q.bits()); while(k >= q) k.randomize(q.bits()); BigInt r = powermod_g_p(k) % q; BigInt s = (inverse_mod(k, q) * mul_add(x, r, t1)) % q; if(r.is_zero() || s.is_zero()) continue; SecureVector<byte> output(2*q.bytes()); r.binary_encode(output + (output.size() / 2 - r.bytes())); s.binary_encode(output + (output.size() - s.bytes())); return output; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -