📄 nr.cpp
字号:
/************************************************** Nyberg-Rueppel Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/nr.h>#include <botan/numthry.h>#include <botan/primes.h>namespace Botan {/************************************************** NR_PublicKey Constructor **************************************************/NR_PublicKey::NR_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("NR: Domain is not a DSA-style group"); if(y < 0 || y >= p) throw Invalid_Argument("NR_PublicKey: Invalid public parameter"); }/************************************************** NR_PublicKey Copy Constructor **************************************************/NR_PublicKey::NR_PublicKey(const NR_PublicKey& key) : PK_Verifying_with_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 Nyberg-Rueppel Parameters **************************************************/bool NR_PublicKey::check_params() const { if(y < 2 || y >= p) return false; if(!group.verify()) return false; return true; }/************************************************** Nyberg-Rueppel Verification Function **************************************************/SecureVector<byte> NR_PublicKey::verify(const byte sig[], u32bit sig_len) const { if(sig_len != 2*q.bytes()) return false; BigInt c(sig, q.bytes()); BigInt d(sig + q.bytes(), q.bytes()); if(c.is_zero() || c >= q || d >= q) throw Invalid_Argument("Nyberg-Rueppel: Invalid signature"); BigInt i = (powermod_g_p(d) * powermod_y_p(c)) % p; BigInt f = (c - i) % q; return encode(f); }/************************************************** NR_PrivateKey Constructor **************************************************/NR_PrivateKey::NR_PrivateKey(const DL_Group& domain) : NR_PublicKey(domain, 0) { x = random_integer(1, q - 1); y = powermod_g_p(x); powermod_y_p = FixedBase_Exp(y, p); }/************************************************** NR_PrivateKey Constructor **************************************************/NR_PrivateKey::NR_PrivateKey(const DL_Group& domain, const BigInt& priv, const BigInt& pub) : NR_PublicKey(domain, pub), x(priv) { if(x <= 1 || y <= 1 || x >= p || y >= p) throw Invalid_Argument("NR_PrivateKey: invalid private key"); }/************************************************** Check Private Nyberg-Rueppel Parameters **************************************************/bool NR_PrivateKey::check_params() const { if(!NR_PublicKey::check_params()) return false; if(y != powermod_g_p(x)) return false; return true; }/************************************************** Nyberg-Rueppel Signature Operation **************************************************/SecureVector<byte> NR_PrivateKey::sign(const byte in[], u32bit len) const { BigInt f; f.binary_decode(in, len); if(f > q) throw Invalid_Argument("NR_PrivateKey::sign: Input is too large"); while(true) { BigInt k(Random, q.bits()); while(k >= q) k.randomize(q.bits()); BigInt c = (powermod_g_p(k) + f) % q; if(c.is_zero()) continue; BigInt d = (k - x * c) % q; SecureVector<byte> output(2*q.bytes()); c.binary_encode(output + (output.size() / 2 - c.bytes())); d.binary_encode(output + (output.size() - d.bytes())); return output; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -