aboutsummaryrefslogtreecommitdiffstats
path: root/src/pubkey/rw
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-03-05 01:09:28 +0000
committerlloyd <[email protected]>2010-03-05 01:09:28 +0000
commit1c7dbb21d19702872379421e6ae44a15caf67da2 (patch)
treebd4ee9e01f8cfd9631655d0e0b0991d49c0a7e8e /src/pubkey/rw
parent78b5b103291ee668185dc71d138a50e8e7e54808 (diff)
Add signature generation operation classes. Remove sign() from
PK_Signing_Key, though for the moment the class remains because there are a few pieces of code that use it to detect if signatures are supported, or for passing to functions in look_pk
Diffstat (limited to 'src/pubkey/rw')
-rw-r--r--src/pubkey/rw/rw.cpp33
-rw-r--r--src/pubkey/rw/rw.h29
2 files changed, 57 insertions, 5 deletions
diff --git a/src/pubkey/rw/rw.cpp b/src/pubkey/rw/rw.cpp
index 72fa29afa..de50313d2 100644
--- a/src/pubkey/rw/rw.cpp
+++ b/src/pubkey/rw/rw.cpp
@@ -123,4 +123,37 @@ bool RW_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const
return true;
}
+RW_Signature_Operation::RW_Signature_Operation(const RW_PrivateKey& rw) :
+ q(rw.get_q()),
+ c(rw.get_c()),
+ n(rw.get_n()),
+ powermod_d1_p(rw.get_d1(), rw.get_p()),
+ powermod_d2_q(rw.get_d2(), rw.get_q()),
+ mod_p(rw.get_p())
+ {
+ }
+
+SecureVector<byte> RW_Signature_Operation::sign(const byte msg[],
+ u32bit msg_len,
+ RandomNumberGenerator&)
+ {
+ BigInt i(msg, msg_len);
+
+ if(i >= n || i % 16 != 12)
+ throw Invalid_Argument("Rabin-Williams: invalid input");
+
+ if(jacobi(i, n) != 1)
+ i >>= 1;
+
+ BigInt j1 = powermod_d1_p(i);
+ BigInt j2 = powermod_d2_q(i);
+ j1 = mod_p.reduce(sub_mul(j1, j2, c));
+
+ BigInt r = mul_add(j1, q, j2);
+
+ r = std::min(r, n - r);
+
+ return BigInt::encode_1363(r, n.bytes());
+ }
+
}
diff --git a/src/pubkey/rw/rw.h b/src/pubkey/rw/rw.h
index efdca04e7..a46cb5bb3 100644
--- a/src/pubkey/rw/rw.h
+++ b/src/pubkey/rw/rw.h
@@ -49,11 +49,6 @@ class BOTAN_DLL RW_PrivateKey : public RW_PublicKey,
public IF_Scheme_PrivateKey
{
public:
- SecureVector<byte> sign(const byte[], u32bit,
- RandomNumberGenerator& rng) const;
-
- bool check_key(RandomNumberGenerator& rng, bool) const;
-
RW_PrivateKey(const AlgorithmIdentifier& alg_id,
const MemoryRegion<byte>& key_bits,
RandomNumberGenerator& rng) :
@@ -66,6 +61,30 @@ class BOTAN_DLL RW_PrivateKey : public RW_PublicKey,
IF_Scheme_PrivateKey(rng, p, q, e, d, n) {}
RW_PrivateKey(RandomNumberGenerator& rng, u32bit bits, u32bit = 2);
+
+ bool check_key(RandomNumberGenerator& rng, bool) const;
+
+ SecureVector<byte> sign(const byte[], u32bit,
+ RandomNumberGenerator& rng) const;
+ };
+
+class BOTAN_DLL RW_Signature_Operation : public PK_Ops::Signature_Operation
+ {
+ public:
+ RW_Signature_Operation(const RW_PrivateKey& rw);
+
+ u32bit max_input_bits() const { return (n.bits() - 1); }
+
+ SecureVector<byte> sign(const byte msg[], u32bit msg_len,
+ RandomNumberGenerator& rng);
+ private:
+ const BigInt& q;
+ const BigInt& c;
+ const BigInt& n;
+
+ Fixed_Exponent_Power_Mod powermod_d1_p, powermod_d2_q;
+ Modular_Reducer mod_p;
+ u32bit n_bits;
};
}