Botan: src/pubkey/rw/rw.cpp Source File

Botan 1.10.17

rw.cpp
Go to the documentation of this file.
1 /*
2 * Rabin-Williams
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/rw.h>
9 #include <botan/numthry.h>
10 #include <botan/keypair.h>
11 #include <botan/parsing.h>
12 #include <algorithm>
13
14 namespace Botan {
15
16 /*
17 * Create a Rabin-Williams private key
18 */
19 RW_PrivateKey::RW_PrivateKey(RandomNumberGenerator& rng,
20 size_t bits, size_t exp)
21 {
22 if(bits < 512)
23 throw Invalid_Argument(algo_name() + ": Can't make a key that is only " +
24 to_string(bits) + " bits long");
25 if(exp < 2 || exp % 2 == 1)
26 throw Invalid_Argument(algo_name() + ": Invalid encryption exponent");
27
28 e = exp;
29
30 do
31 {
32 p = random_prime(rng, (bits + 1) / 2, e / 2, 3, 4);
33 q = random_prime(rng, bits - p.bits(), e / 2, ((p % 8 == 3) ? 7 : 3), 8);
34 n = p * q;
35 } while(n.bits() != bits);
36
37 d = inverse_mod(e, lcm(p - 1, q - 1) >> 1);
38 d1 = d % (p - 1);
39 d2 = d % (q - 1);
40 c = inverse_mod(q, p);
41
42 gen_check(rng);
43 }
44
45 /*
46 * Check Private Rabin-Williams Parameters
47 */
48 bool RW_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const
49 {
50 if(!IF_Scheme_PrivateKey::check_key(rng, strong))
51 return false;
52
53 if(!strong)
54 return true;
55
56 if((e * d) % (lcm(p - 1, q - 1) / 2) != 1)
57 return false;
58
59 return KeyPair::signature_consistency_check(rng, *this, "EMSA2(SHA-1)");
60 }
61
62 RW_Signature_Operation::RW_Signature_Operation(const RW_PrivateKey& rw) :
63 n(rw.get_n()),
64 e(rw.get_e()),
65 q(rw.get_q()),
66 c(rw.get_c()),
67 powermod_d1_p(rw.get_d1(), rw.get_p()),
68 powermod_d2_q(rw.get_d2(), rw.get_q()),
69 mod_p(rw.get_p())
70 {
71 }
72
73 SecureVector<byte>
74 RW_Signature_Operation::sign(const byte msg[], size_t msg_len,
75 RandomNumberGenerator& rng)
76 {
77 if(!blinder.initialized())
78 {
79 BigInt k(rng, n.bits() / 2);
80 blinder = Blinder(power_mod(k, e, n), inverse_mod(k, n), n);
81 }
82
83 BigInt i(msg, msg_len);
84
85 if(i >= n || i % 16 != 12)
86 throw Invalid_Argument("Rabin-Williams: invalid input");
87
88 if(jacobi(i, n) != 1)
89 i >>= 1;
90
91 i = blinder.blind(i);
92
93 BigInt j1 = powermod_d1_p(i);
94 BigInt j2 = powermod_d2_q(i);
95 j1 = mod_p.reduce(sub_mul(j1, j2, c));
96
97 BigInt r = blinder.unblind(mul_add(j1, q, j2));
98
99 r = std::min(r, n - r);
100
101 return BigInt::encode_1363(r, n.bytes());
102 }
103
104 SecureVector<byte>
105 RW_Verification_Operation::verify_mr(const byte msg[], size_t msg_len)
106 {
107 BigInt m(msg, msg_len);
108
109 if((m > (n >> 1)) || m.is_negative())
110 throw Invalid_Argument("RW signature verification: m > n / 2 || m < 0");
111
112 BigInt r = powermod_e_n(m);
113 if(r % 16 == 12)
114 return BigInt::encode(r);
115 if(r % 8 == 6)
116 return BigInt::encode(2*r);
117
118 r = n - r;
119 if(r % 16 == 12)
120 return BigInt::encode(r);
121 if(r % 8 == 6)
122 return BigInt::encode(2*r);
123
124 throw Invalid_Argument("RW signature verification: Invalid signature");
125 }
126
127 }
Botan::BigInt::is_negative
bool is_negative() const
Definition: bigint.h:245
Botan::IF_Scheme_PublicKey::n
BigInt n
Definition: if_algo.h:51
Botan::RW_PrivateKey::check_key
bool check_key(RandomNumberGenerator &rng, bool) const
Definition: rw.cpp:48
Botan::KeyPair::signature_consistency_check
bool signature_consistency_check(RandomNumberGenerator &rng, const Private_Key &key, const std::string &padding)
Definition: keypair.cpp:47
Botan::BigInt
Definition: bigint.h:22
Botan::RW_Signature_Operation::RW_Signature_Operation
RW_Signature_Operation(const RW_PrivateKey &rw)
Definition: rw.cpp:62
Botan::RandomNumberGenerator
Definition: rng.h:20
Botan::IF_Scheme_PrivateKey::d2
BigInt d2
Definition: if_algo.h:101
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:254
Botan::RW_Signature_Operation::sign
SecureVector< byte > sign(const byte msg[], size_t msg_len, RandomNumberGenerator &rng)
Definition: rw.cpp:74
Botan::IF_Scheme_PublicKey::e
BigInt e
Definition: if_algo.h:51
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::BigInt::encode
static SecureVector< byte > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:64
Botan::Blinder::initialized
bool initialized() const
Definition: blinding.h:25
Botan::RW_Verification_Operation::verify_mr
SecureVector< byte > verify_mr(const byte msg[], size_t msg_len)
Definition: rw.cpp:105
Botan::RW_PrivateKey::RW_PrivateKey
RW_PrivateKey(const AlgorithmIdentifier &alg_id, const MemoryRegion< byte > &key_bits, RandomNumberGenerator &rng)
Definition: rw.h:46
Botan::RW_PublicKey::algo_name
std::string algo_name() const
Definition: rw.h:24
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::Blinder::unblind
BigInt unblind(const BigInt &x) const
Definition: blinding.cpp:42
Botan::Private_Key::gen_check
void gen_check(RandomNumberGenerator &rng) const
Definition: pk_keys.cpp:49
Botan::IF_Scheme_PrivateKey::d1
BigInt d1
Definition: if_algo.h:101
Botan::jacobi
s32bit jacobi(const BigInt &a, const BigInt &n)
Definition: jacobi.cpp:15
Botan
Definition: algo_base.h:14
Botan::BigInt::bytes
size_t bytes() const
Definition: bigint.cpp:246
Botan::IF_Scheme_PrivateKey::q
BigInt q
Definition: if_algo.h:101
Botan::Blinder
Definition: blinding.h:19
Botan::Blinder::blind
BigInt blind(const BigInt &x) const
Definition: blinding.cpp:29
Botan::inverse_mod
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:314
Botan::IF_Scheme_PrivateKey::d
BigInt d
Definition: if_algo.h:101
Botan::IF_Scheme_PrivateKey::check_key
bool check_key(RandomNumberGenerator &rng, bool) const
Definition: if_algo.cpp:121
Botan::sub_mul
BigInt sub_mul(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:60
Botan::Modular_Reducer::reduce
BigInt reduce(const BigInt &x) const
Definition: reducer.cpp:32
Botan::power_mod
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition: numthry.cpp:366
Botan::IF_Scheme_PrivateKey::c
BigInt c
Definition: if_algo.h:101
Botan::RW_PrivateKey
Definition: rw.h:42
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:127
Botan::to_string
std::string to_string(u64bit n, size_t min_len)
Definition: parsing.cpp:42
Botan::SecureVector< byte >
Botan::BigInt::encode_1363
static SecureVector< byte > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:78
Botan::lcm
BigInt lcm(const BigInt &a, const BigInt &b)
Definition: numthry.cpp:195
Botan::mul_add
BigInt mul_add(const BigInt &a, const BigInt &b, const BigInt &c)
Definition: mp_numth.cpp:33
Botan::random_prime
BigInt random_prime(RandomNumberGenerator &rng, size_t bits, const BigInt &coprime, size_t equiv, size_t modulo)
Definition: make_prm.cpp:17
Botan::IF_Scheme_PrivateKey::p
BigInt p
Definition: if_algo.h:101