Botan  1.10.9
if_algo.cpp
Go to the documentation of this file.
1 /*
2 * IF Scheme
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/if_algo.h>
9 #include <botan/numthry.h>
10 #include <botan/der_enc.h>
11 #include <botan/ber_dec.h>
12 
13 namespace Botan {
14 
16  {
19  }
20 
22  {
23  return DER_Encoder()
25  .encode(n)
26  .encode(e)
27  .end_cons()
28  .get_contents();
29  }
30 
32  const MemoryRegion<byte>& key_bits)
33  {
34  BER_Decoder(key_bits)
36  .decode(n)
37  .decode(e)
38  .verify_end()
39  .end_cons();
40  }
41 
42 /*
43 * Check IF Scheme Public Parameters
44 */
46  {
47  if(n < 35 || n.is_even() || e < 2)
48  return false;
49  return true;
50  }
51 
53  {
54  return DER_Encoder()
56  .encode(static_cast<size_t>(0))
57  .encode(n)
58  .encode(e)
59  .encode(d)
60  .encode(p)
61  .encode(q)
62  .encode(d1)
63  .encode(d2)
64  .encode(c)
65  .end_cons()
66  .get_contents();
67  }
68 
70  const AlgorithmIdentifier&,
71  const MemoryRegion<byte>& key_bits)
72  {
73  BER_Decoder(key_bits)
75  .decode_and_check<size_t>(0, "Unknown PKCS #1 key format version")
76  .decode(n)
77  .decode(e)
78  .decode(d)
79  .decode(p)
80  .decode(q)
81  .decode(d1)
82  .decode(d2)
83  .decode(c)
84  .end_cons();
85 
86  load_check(rng);
87  }
88 
90  const BigInt& prime1,
91  const BigInt& prime2,
92  const BigInt& exp,
93  const BigInt& d_exp,
94  const BigInt& mod)
95  {
96  p = prime1;
97  q = prime2;
98  e = exp;
99  d = d_exp;
100  n = mod.is_nonzero() ? mod : p * q;
101 
102  if(d == 0)
103  {
104  BigInt inv_for_d = lcm(p - 1, q - 1);
105  if(e.is_even())
106  inv_for_d >>= 1;
107 
108  d = inverse_mod(e, inv_for_d);
109  }
110 
111  d1 = d % (p - 1);
112  d2 = d % (q - 1);
113  c = inverse_mod(q, p);
114 
115  load_check(rng);
116  }
117 
118 /*
119 * Check IF Scheme Private Parameters
120 */
122  bool strong) const
123  {
124  if(n < 35 || n.is_even() || e < 2 || d < 2 || p < 3 || q < 3 || p*q != n)
125  return false;
126 
127  if(!strong)
128  return true;
129 
130  if(d1 != d % (p - 1) || d2 != d % (q - 1) || c != inverse_mod(q, p))
131  return false;
132  if(!check_prime(p, rng) || !check_prime(q, rng))
133  return false;
134  return true;
135  }
136 
137 }
void load_check(RandomNumberGenerator &rng) const
Definition: pk_keys.cpp:40
bool is_even() const
Definition: bigint.h:158
SecureVector< byte > get_contents()
Definition: der_enc.cpp:122
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:338
BER_Decoder & decode_and_check(const T &expected, const std::string &error_msg)
Definition: ber_dec.h:62
MemoryVector< byte > pkcs8_private_key() const
Definition: if_algo.cpp:52
DER_Encoder & end_cons()
Definition: der_enc.cpp:145
BER_Decoder start_cons(ASN1_Tag, ASN1_Tag=UNIVERSAL)
Definition: ber_dec.cpp:232
bool is_nonzero() const
Definition: bigint.h:170
virtual OID get_oid() const
Definition: pk_keys.cpp:17
SecureVector< byte > decode(DataSource &source, std::string &label)
Definition: pem.cpp:56
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:209
RandomNumberGenerator * rng
Definition: global_rng.cpp:165
BER_Decoder & end_cons()
Definition: ber_dec.cpp:246
GMP_MPZ exp
Definition: gmp_powm.cpp:29
GMP_MPZ mod
Definition: gmp_powm.cpp:29
MemoryVector< byte > x509_subject_public_key() const
Definition: if_algo.cpp:21
bool check_key(RandomNumberGenerator &rng, bool) const
Definition: if_algo.cpp:121
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:202
AlgorithmIdentifier algorithm_identifier() const
Definition: if_algo.cpp:15
BER_Decoder & verify_end()
Definition: ber_dec.cpp:160
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:135
bool check_key(RandomNumberGenerator &rng, bool) const
Definition: if_algo.cpp:45
BigInt lcm(const BigInt &a, const BigInt &b)
Definition: numthry.cpp:194
bool check_prime(const BigInt &n, RandomNumberGenerator &rng)
Definition: numthry.h:143