Botan  2.4.0
Crypto and TLS for C++11
dl_algo.cpp
Go to the documentation of this file.
1 /*
2 * DL Scheme
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/dl_algo.h>
9 #include <botan/numthry.h>
10 #include <botan/workfactor.h>
11 #include <botan/der_enc.h>
12 #include <botan/ber_dec.h>
13 
14 namespace Botan {
15 
17  {
18  return m_group.get_p().bits();
19  }
20 
22  {
23  return dl_work_factor(key_length());
24  }
25 
27  {
30  }
31 
32 std::vector<uint8_t> DL_Scheme_PublicKey::public_key_bits() const
33  {
35  }
36 
38  const std::vector<uint8_t>& key_bits,
39  DL_Group::Format format)
40  {
41  m_group.BER_decode(alg_id.get_parameters(), format);
42 
43  BER_Decoder(key_bits).decode(m_y);
44  }
45 
47  {
48  return DER_Encoder().encode(m_x).get_contents();
49  }
50 
52  const secure_vector<uint8_t>& key_bits,
53  DL_Group::Format format)
54  {
55  m_group.BER_decode(alg_id.get_parameters(), format);
56 
57  BER_Decoder(key_bits).decode(m_x);
58  }
59 
60 /*
61 * Check Public DL Parameters
62 */
64  bool strong) const
65  {
66  const BigInt& p = group_p();
67 
68  if(m_y < 2 || m_y >= p)
69  return false;
70  if(!m_group.verify_group(rng, strong))
71  return false;
72 
73  try
74  {
75  const BigInt& q = group_q();
76  if(power_mod(m_y, q, p) != 1)
77  return false;
78  }
79  catch(const Invalid_State&)
80  {
81  return true;
82  }
83 
84  return true;
85  }
86 
87 /*
88 * Check DL Scheme Private Parameters
89 */
91  bool strong) const
92  {
93  const BigInt& p = group_p();
94  const BigInt& g = group_g();
95 
96  if(m_y < 2 || m_y >= p || m_x < 2 || m_x >= p)
97  return false;
98  if(!m_group.verify_group(rng, strong))
99  return false;
100 
101  if(!strong)
102  return true;
103 
104  if(m_y != power_mod(g, m_x, p))
105  return false;
106 
107  return true;
108  }
109 
110 }
size_t key_length() const override
Definition: dl_algo.cpp:16
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition: dl_algo.cpp:63
std::vector< uint8_t > get_contents_unlocked()
Definition: der_enc.h:27
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition: numthry.cpp:374
size_t bits() const
Definition: bigint.cpp:183
secure_vector< uint8_t > get_contents()
Definition: der_enc.cpp:123
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:355
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:202
bool verify_group(RandomNumberGenerator &rng, bool strong) const
Definition: dl_group.cpp:159
virtual OID get_oid() const
Definition: pk_keys.cpp:51
const BigInt & get_p() const
Definition: dl_group.cpp:194
AlgorithmIdentifier algorithm_identifier() const override
Definition: dl_algo.cpp:26
secure_vector< uint8_t > private_key_bits() const override
Definition: dl_algo.cpp:46
std::vector< uint8_t > DER_encode(Format format) const
Definition: dl_group.cpp:223
virtual DL_Group::Format group_format() const =0
size_t estimated_strength() const override
Definition: dl_algo.cpp:21
Definition: alg_id.cpp:13
std::vector< uint8_t > public_key_bits() const override
Definition: dl_algo.cpp:32
const std::vector< uint8_t > & get_parameters() const
Definition: alg_id.h:38
const BigInt & group_p() const
Definition: dl_algo.h:43
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition: dl_algo.cpp:90
size_t dl_work_factor(size_t bits)
Definition: workfactor.cpp:34
void BER_decode(const std::vector< uint8_t > &ber, Format format)
Definition: dl_group.cpp:283
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
const BigInt & group_g() const
Definition: dl_algo.h:55
const BigInt & group_q() const
Definition: dl_algo.h:49