aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/gnump/gnump_pk.cpp
blob: 25735fe55a2b653acde627a1994598549d056158 (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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* GnuMP PK operations
* (C) 1999-2010 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/internal/gnump_engine.h>
#include <botan/internal/gmp_wrap.h>
#include <gmp.h>

/* GnuMP 5.0 and later have a side-channel resistent powm */
#if defined(HAVE_MPZ_POWM_SEC)
   #undef mpz_powm
   #define mpz_powm mpz_powm_sec
#endif

#if defined(BOTAN_HAS_RSA)
  #include <botan/rsa.h>
#endif

#if defined(BOTAN_HAS_DSA)
  #include <botan/dsa.h>
#endif

#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
  #include <botan/dh.h>
#endif

namespace Botan {

namespace {

#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
class GMP_DH_KA_Operation : public PK_Ops::Key_Agreement
   {
   public:
      GMP_DH_KA_Operation(const DH_PrivateKey& dh) :
         x(dh.get_x()), p(dh.group_p()) {}

      SecureVector<byte> agree(const byte w[], size_t w_len)
         {
         GMP_MPZ z(w, w_len);
         mpz_powm(z.value, z.value, x.value, p.value);
         return z.to_bytes();
         }

   private:
      GMP_MPZ x, p;
   };
#endif

#if defined(BOTAN_HAS_DSA)

class GMP_DSA_Signature_Operation : public PK_Ops::Signature
   {
   public:
      GMP_DSA_Signature_Operation(const DSA_PrivateKey& dsa) :
         x(dsa.get_x()),
         p(dsa.group_p()),
         q(dsa.group_q()),
         g(dsa.group_g()),
         q_bits(dsa.group_q().bits()) {}

      size_t message_parts() const { return 2; }
      size_t message_part_size() const { return (q_bits + 7) / 8; }
      size_t max_input_bits() const { return q_bits; }

      SecureVector<byte> sign(const byte msg[], size_t msg_len,
                              RandomNumberGenerator& rng);
   private:
      const GMP_MPZ x, p, q, g;
      size_t q_bits;
   };

SecureVector<byte>
GMP_DSA_Signature_Operation::sign(const byte msg[], size_t msg_len,
                                  RandomNumberGenerator& rng)
   {
   const size_t q_bytes = (q_bits + 7) / 8;

   rng.add_entropy(msg, msg_len);

   BigInt k_bn;
   do
      k_bn.randomize(rng, q_bits);
   while(k_bn >= q.to_bigint());

   GMP_MPZ i(msg, msg_len);
   GMP_MPZ k(k_bn);

   GMP_MPZ r;
   mpz_powm(r.value, g.value, k.value, p.value);
   mpz_mod(r.value, r.value, q.value);

   mpz_invert(k.value, k.value, q.value);

   GMP_MPZ s;
   mpz_mul(s.value, x.value, r.value);
   mpz_add(s.value, s.value, i.value);
   mpz_mul(s.value, s.value, k.value);
   mpz_mod(s.value, s.value, q.value);

   if(mpz_cmp_ui(r.value, 0) == 0 || mpz_cmp_ui(s.value, 0) == 0)
      throw Internal_Error("GMP_DSA_Op::sign: r or s was zero");

   SecureVector<byte> output(2*q_bytes);
   r.encode(output, q_bytes);
   s.encode(output + q_bytes, q_bytes);
   return output;
   }

class GMP_DSA_Verification_Operation : public PK_Ops::Verification
   {
   public:
      GMP_DSA_Verification_Operation(const DSA_PublicKey& dsa) :
         y(dsa.get_y()),
         p(dsa.group_p()),
         q(dsa.group_q()),
         g(dsa.group_g()),
         q_bits(dsa.group_q().bits()) {}

      size_t message_parts() const { return 2; }
      size_t message_part_size() const { return (q_bits + 7) / 8; }
      size_t max_input_bits() const { return q_bits; }

      bool with_recovery() const { return false; }

      bool verify(const byte msg[], size_t msg_len,
                  const byte sig[], size_t sig_len);
   private:
      const GMP_MPZ y, p, q, g;
      size_t q_bits;
   };

bool GMP_DSA_Verification_Operation::verify(const byte msg[], size_t msg_len,
                                            const byte sig[], size_t sig_len)
   {
   const size_t q_bytes = q.bytes();

   if(sig_len != 2*q_bytes || msg_len > q_bytes)
      return false;

   GMP_MPZ r(sig, q_bytes);
   GMP_MPZ s(sig + q_bytes, q_bytes);
   GMP_MPZ i(msg, msg_len);

   if(mpz_cmp_ui(r.value, 0) <= 0 || mpz_cmp(r.value, q.value) >= 0)
      return false;
   if(mpz_cmp_ui(s.value, 0) <= 0 || mpz_cmp(s.value, q.value) >= 0)
      return false;

   if(mpz_invert(s.value, s.value, q.value) == 0)
      return false;

   GMP_MPZ si;
   mpz_mul(si.value, s.value, i.value);
   mpz_mod(si.value, si.value, q.value);
   mpz_powm(si.value, g.value, si.value, p.value);

   GMP_MPZ sr;
   mpz_mul(sr.value, s.value, r.value);
   mpz_mod(sr.value, sr.value, q.value);
   mpz_powm(sr.value, y.value, sr.value, p.value);

   mpz_mul(si.value, si.value, sr.value);
   mpz_mod(si.value, si.value, p.value);
   mpz_mod(si.value, si.value, q.value);

   if(mpz_cmp(si.value, r.value) == 0)
      return true;
   return false;
   }

#endif

#if defined(BOTAN_HAS_RSA)

class GMP_RSA_Private_Operation : public PK_Ops::Signature,
                                  public PK_Ops::Decryption
   {
   public:
      GMP_RSA_Private_Operation(const RSA_PrivateKey& rsa) :
         mod(rsa.get_n()),
         p(rsa.get_p()),
         q(rsa.get_q()),
         d1(rsa.get_d1()),
         d2(rsa.get_d2()),
         c(rsa.get_c()),
         n_bits(rsa.get_n().bits())
         {}

      size_t max_input_bits() const { return (n_bits - 1); }

      SecureVector<byte> sign(const byte msg[], size_t msg_len,
                              RandomNumberGenerator&)
         {
         BigInt m(msg, msg_len);
         BigInt x = private_op(m);
         return BigInt::encode_1363(x, (n_bits + 7) / 8);
         }

      SecureVector<byte> decrypt(const byte msg[], size_t msg_len)
         {
         BigInt m(msg, msg_len);
         return BigInt::encode(private_op(m));
         }

   private:
      BigInt private_op(const BigInt& m) const;

      GMP_MPZ mod, p, q, d1, d2, c;
      size_t n_bits;
   };

BigInt GMP_RSA_Private_Operation::private_op(const BigInt& m) const
   {
   GMP_MPZ j1, j2, h(m);

   mpz_powm(j1.value, h.value, d1.value, p.value);
   mpz_powm(j2.value, h.value, d2.value, q.value);
   mpz_sub(h.value, j1.value, j2.value);
   mpz_mul(h.value, h.value, c.value);
   mpz_mod(h.value, h.value, p.value);
   mpz_mul(h.value, h.value, q.value);
   mpz_add(h.value, h.value, j2.value);
   return h.to_bigint();
   }

class GMP_RSA_Public_Operation : public PK_Ops::Verification,
                                 public PK_Ops::Encryption
   {
   public:
      GMP_RSA_Public_Operation(const RSA_PublicKey& rsa) :
         n(rsa.get_n()), e(rsa.get_e()), mod(rsa.get_n())
         {}

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

      SecureVector<byte> encrypt(const byte msg[], size_t msg_len,
                                 RandomNumberGenerator&)
         {
         BigInt m(msg, msg_len);
         return BigInt::encode_1363(public_op(m), n.bytes());
         }

      SecureVector<byte> verify_mr(const byte msg[], size_t msg_len)
         {
         BigInt m(msg, msg_len);
         return BigInt::encode(public_op(m));
         }

   private:
      BigInt public_op(const BigInt& m) const
         {
         if(m >= n)
            throw Invalid_Argument("RSA public op - input is too large");

         GMP_MPZ m_gmp(m);
         mpz_powm(m_gmp.value, m_gmp.value, e.value, mod.value);
         return m_gmp.to_bigint();
         }

      const BigInt& n;
      const GMP_MPZ e, mod;
   };

#endif

}

PK_Ops::Key_Agreement*
GMP_Engine::get_key_agreement_op(const Private_Key& key) const
   {
#if defined(BOTAN_HAS_DIFFIE_HELLMAN)
   if(const DH_PrivateKey* dh = dynamic_cast<const DH_PrivateKey*>(&key))
      return new GMP_DH_KA_Operation(*dh);
#endif

   return 0;
   }

PK_Ops::Signature*
GMP_Engine::get_signature_op(const Private_Key& key) const
   {
#if defined(BOTAN_HAS_RSA)
   if(const RSA_PrivateKey* s = dynamic_cast<const RSA_PrivateKey*>(&key))
      return new GMP_RSA_Private_Operation(*s);
#endif

#if defined(BOTAN_HAS_DSA)
   if(const DSA_PrivateKey* s = dynamic_cast<const DSA_PrivateKey*>(&key))
      return new GMP_DSA_Signature_Operation(*s);
#endif

   return 0;
   }

PK_Ops::Verification*
GMP_Engine::get_verify_op(const Public_Key& key) const
   {
#if defined(BOTAN_HAS_RSA)
   if(const RSA_PublicKey* s = dynamic_cast<const RSA_PublicKey*>(&key))
      return new GMP_RSA_Public_Operation(*s);
#endif

#if defined(BOTAN_HAS_DSA)
   if(const DSA_PublicKey* s = dynamic_cast<const DSA_PublicKey*>(&key))
      return new GMP_DSA_Verification_Operation(*s);
#endif

   return 0;
   }

PK_Ops::Encryption*
GMP_Engine::get_encryption_op(const Public_Key& key) const
   {
#if defined(BOTAN_HAS_RSA)
   if(const RSA_PublicKey* s = dynamic_cast<const RSA_PublicKey*>(&key))
      return new GMP_RSA_Public_Operation(*s);
#endif

   return 0;
   }

PK_Ops::Decryption*
GMP_Engine::get_decryption_op(const Private_Key& key) const
   {
#if defined(BOTAN_HAS_RSA)
   if(const RSA_PrivateKey* s = dynamic_cast<const RSA_PrivateKey*>(&key))
      return new GMP_RSA_Private_Operation(*s);
#endif

   return 0;
   }

}