aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/pubkey/rw/rw.cpp
blob: bf6b647a11daff2589ffce149e9cad4eb08677be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Rabin-Williams
* (C) 1999-2008 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/internal/pk_utils.h>
#include <botan/rw.h>
#include <botan/keypair.h>
#include <botan/parsing.h>
#include <botan/reducer.h>
#include <botan/blinding.h>
#include <algorithm>
#include <future>

namespace Botan {

/*
* Create a Rabin-Williams private key
*/
RW_PrivateKey::RW_PrivateKey(RandomNumberGenerator& rng,
                             size_t bits, size_t exp)
   {
   if(bits < 1024)
      throw Invalid_Argument(algo_name() + ": Can't make a key that is only " +
                             std::to_string(bits) + " bits long");
   if(exp < 2 || exp % 2 == 1)
      throw Invalid_Argument(algo_name() + ": Invalid encryption exponent");

   m_e = exp;

   do
      {
      m_p = random_prime(rng, (bits + 1) / 2, m_e / 2, 3, 4);
      m_q = random_prime(rng, bits - m_p.bits(), m_e / 2, ((m_p % 8 == 3) ? 7 : 3), 8);
      m_n = m_p * m_q;
      } while(m_n.bits() != bits);

   m_d = inverse_mod(m_e, lcm(m_p - 1, m_q - 1) >> 1);
   m_d1 = m_d % (m_p - 1);
   m_d2 = m_d % (m_q - 1);
   m_c = inverse_mod(m_q, m_p);

   gen_check(rng);
   }

/*
* Check Private Rabin-Williams Parameters
*/
bool RW_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const
   {
   if(!IF_Scheme_PrivateKey::check_key(rng, strong))
      return false;

   if(!strong)
      return true;

   if((m_e * m_d) % (lcm(m_p - 1, m_q - 1) / 2) != 1)
      return false;

   return KeyPair::signature_consistency_check(rng, *this, "EMSA2(SHA-1)");
   }

namespace {

/**
* Rabin-Williams Signature Operation
*/
class RW_Signature_Operation : public PK_Ops::Signature_with_EMSA
   {
   public:
      typedef RW_PrivateKey Key_Type;

      RW_Signature_Operation(const RW_PrivateKey& rw,
                             const std::string& emsa) :
         PK_Ops::Signature_with_EMSA(emsa),
         m_n(rw.get_n()),
         m_e(rw.get_e()),
         m_q(rw.get_q()),
         m_c(rw.get_c()),
         m_powermod_d1_p(rw.get_d1(), rw.get_p()),
         m_powermod_d2_q(rw.get_d2(), rw.get_q()),
         m_mod_p(rw.get_p()),
         m_blinder(m_n,
                 [this](const BigInt& k) { return power_mod(k, m_e, m_n); },
                 [this](const BigInt& k) { return inverse_mod(k, m_n); })
         {
         }

      size_t max_input_bits() const override { return (m_n.bits() - 1); }

      secure_vector<byte> raw_sign(const byte msg[], size_t msg_len,
                                   RandomNumberGenerator& rng) override;
   private:
      const BigInt& m_n;
      const BigInt& m_e;
      const BigInt& m_q;
      const BigInt& m_c;

      Fixed_Exponent_Power_Mod m_powermod_d1_p, m_powermod_d2_q;
      Modular_Reducer m_mod_p;
      Blinder m_blinder;
   };

secure_vector<byte>
RW_Signature_Operation::raw_sign(const byte msg[], size_t msg_len,
                                 RandomNumberGenerator&)
   {
   BigInt i(msg, msg_len);

   if(i >= m_n || i % 16 != 12)
      throw Invalid_Argument("Rabin-Williams: invalid input");

   if(jacobi(i, m_n) != 1)
      i >>= 1;

   i = m_blinder.blind(i);

   auto future_j1 = std::async(std::launch::async, m_powermod_d1_p, i);
   const BigInt j2 = m_powermod_d2_q(i);
   BigInt j1 = future_j1.get();

   j1 = m_mod_p.reduce(sub_mul(j1, j2, m_c));

   const BigInt r = m_blinder.unblind(mul_add(j1, m_q, j2));

   return BigInt::encode_1363(std::min(r, m_n - r), m_n.bytes());
   }

/**
* Rabin-Williams Verification Operation
*/
class RW_Verification_Operation : public PK_Ops::Verification_with_EMSA
   {
   public:
      typedef RW_PublicKey Key_Type;

      RW_Verification_Operation(const RW_PublicKey& rw, const std::string& emsa) :
         PK_Ops::Verification_with_EMSA(emsa),
         m_n(rw.get_n()), m_powermod_e_n(rw.get_e(), rw.get_n())
         {}

      size_t max_input_bits() const override { return (m_n.bits() - 1); }
      bool with_recovery() const override { return true; }

      secure_vector<byte> verify_mr(const byte msg[], size_t msg_len) override;

   private:
      const BigInt& m_n;
      Fixed_Exponent_Power_Mod m_powermod_e_n;
   };

secure_vector<byte>
RW_Verification_Operation::verify_mr(const byte msg[], size_t msg_len)
   {
   BigInt m(msg, msg_len);

   if((m > (m_n >> 1)) || m.is_negative())
      throw Invalid_Argument("RW signature verification: m > n / 2 || m < 0");

   BigInt r = m_powermod_e_n(m);
   if(r % 16 == 12)
      return BigInt::encode_locked(r);
   if(r % 8 == 6)
      return BigInt::encode_locked(2*r);

   r = m_n - r;
   if(r % 16 == 12)
      return BigInt::encode_locked(r);
   if(r % 8 == 6)
      return BigInt::encode_locked(2*r);

   throw Invalid_Argument("RW signature verification: Invalid signature");
   }

BOTAN_REGISTER_PK_SIGNATURE_OP("RW", RW_Signature_Operation);
BOTAN_REGISTER_PK_VERIFY_OP("RW", RW_Verification_Operation);

}

}