Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
16 size_t DL_Scheme_PublicKey::key_length() const
17 {
18 return m_group.get_p().bits();
19 }
20
21 size_t DL_Scheme_PublicKey::estimated_strength() const
22 {
23 return dl_work_factor(key_length());
24 }
25
26 AlgorithmIdentifier DL_Scheme_PublicKey::algorithm_identifier() const
27 {
28 return AlgorithmIdentifier(get_oid(),
29 m_group.DER_encode(group_format()));
30 }
31
32 std::vector<uint8_t> DL_Scheme_PublicKey::public_key_bits() const
33 {
34 return DER_Encoder().encode(m_y).get_contents_unlocked();
35 }
36
37 DL_Scheme_PublicKey::DL_Scheme_PublicKey(const AlgorithmIdentifier& alg_id,
38 const std::vector<uint8_t>& key_bits,
39 DL_Group::Format format)
40 {
41 m_group.BER_decode(alg_id.parameters, format);
42
43 BER_Decoder(key_bits).decode(m_y);
44 }
45
46 secure_vector<uint8_t> DL_Scheme_PrivateKey::private_key_bits() const
47 {
48 return DER_Encoder().encode(m_x).get_contents();
49 }
50
51 DL_Scheme_PrivateKey::DL_Scheme_PrivateKey(const AlgorithmIdentifier& alg_id,
52 const secure_vector<uint8_t>& key_bits,
53 DL_Group::Format format)
54 {
55 m_group.BER_decode(alg_id.parameters, format);
56
57 BER_Decoder(key_bits).decode(m_x);
58 }
59
60 /*
61 * Check Public DL Parameters
62 */
63 bool DL_Scheme_PublicKey::check_key(RandomNumberGenerator& rng,
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& e)
80 {
81 return true;
82 }
83
84 return true;
85 }
86
87 /*
88 * Check DL Scheme Private Parameters
89 */
90 bool DL_Scheme_PrivateKey::check_key(RandomNumberGenerator& rng,
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 }
Botan::DL_Scheme_PublicKey::DL_Scheme_PublicKey
DL_Scheme_PublicKey()=default
Botan::DL_Scheme_PrivateKey::m_x
BigInt m_x
Definition: dl_algo.h:127
Botan::BigInt
Definition: bigint.h:23
Botan::DL_Scheme_PublicKey::key_length
size_t key_length() const override
Definition: dl_algo.cpp:16
Botan::DL_Scheme_PublicKey::check_key
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition: dl_algo.cpp:63
Botan::DER_Encoder::get_contents_unlocked
std::vector< uint8_t > get_contents_unlocked()
Definition: der_enc.h:27
Botan::RandomNumberGenerator
Definition: rng.h:25
Botan::dl_work_factor
size_t dl_work_factor(size_t bits)
Definition: workfactor.cpp:34
Botan::AlgorithmIdentifier::parameters
std::vector< uint8_t > parameters
Definition: alg_id.h:39
Botan::DL_Scheme_PrivateKey::DL_Scheme_PrivateKey
DL_Scheme_PrivateKey()=default
Botan::DL_Scheme_PublicKey::group_q
const BigInt & group_q() const
Definition: dl_algo.h:49
Botan::DER_Encoder::get_contents
secure_vector< uint8_t > get_contents()
Definition: der_enc.cpp:124
Botan::DL_Scheme_PublicKey::group_p
const BigInt & group_p() const
Definition: dl_algo.h:43
Botan::DL_Group::verify_group
bool verify_group(RandomNumberGenerator &rng, bool strong) const
Definition: dl_group.cpp:161
Botan::Invalid_State
Definition: exceptn.h:61
Botan::BER_Decoder::decode
BER_Decoder & decode(bool &v)
Definition: ber_dec.cpp:376
Botan::DL_Group::get_p
const BigInt & get_p() const
Definition: dl_group.cpp:196
Botan::DL_Group::DER_encode
std::vector< uint8_t > DER_encode(Format format) const
Definition: dl_group.cpp:225
Botan::Public_Key::get_oid
virtual OID get_oid() const
Definition: pk_keys.cpp:30
Botan::DER_Encoder::encode
DER_Encoder & encode(bool b)
Definition: der_enc.cpp:216
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:184
Botan::DER_Encoder
Definition: der_enc.h:22
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::DL_Group::Format
Format
Definition: dl_group.h:45
Botan::DL_Scheme_PublicKey::algorithm_identifier
AlgorithmIdentifier algorithm_identifier() const override
Definition: dl_algo.cpp:26
Botan::DL_Scheme_PublicKey::m_group
DL_Group m_group
Definition: dl_algo.h:89
Botan::DL_Scheme_PrivateKey::private_key_bits
secure_vector< uint8_t > private_key_bits() const override
Definition: dl_algo.cpp:46
Botan::DL_Scheme_PublicKey::m_y
BigInt m_y
Definition: dl_algo.h:84
Botan::DL_Scheme_PublicKey::group_format
virtual DL_Group::Format group_format() const =0
Botan::DL_Scheme_PublicKey::estimated_strength
size_t estimated_strength() const override
Definition: dl_algo.cpp:21
Botan
Definition: alg_id.cpp:13
Botan::DL_Scheme_PublicKey::public_key_bits
std::vector< uint8_t > public_key_bits() const override
Definition: dl_algo.cpp:32
Botan::power_mod
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition: numthry.cpp:373
Botan::DL_Scheme_PrivateKey::check_key
bool check_key(RandomNumberGenerator &rng, bool) const override
Definition: dl_algo.cpp:90
Botan::AlgorithmIdentifier
Definition: alg_id.h:20
Botan::DL_Scheme_PublicKey::group_g
const BigInt & group_g() const
Definition: dl_algo.h:55
Botan::DL_Group::BER_decode
void BER_decode(const std::vector< uint8_t > &ber, Format format)
Definition: dl_group.cpp:285
Botan::BER_Decoder
Definition: ber_dec.h:19