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
|
/*
* DL Scheme
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#include <botan/dl_algo.h>
#include <botan/numthry.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
namespace Botan {
/*
* Return the X.509 public key encoder
*/
X509_Encoder* DL_Scheme_PublicKey::x509_encoder() const
{
class DL_Scheme_Encoder : public X509_Encoder
{
public:
AlgorithmIdentifier alg_id() const
{
MemoryVector<byte> group =
key->group.DER_encode(key->group_format());
return AlgorithmIdentifier(key->get_oid(), group);
}
MemoryVector<byte> key_bits() const
{
return DER_Encoder().encode(key->y).get_contents();
}
DL_Scheme_Encoder(const DL_Scheme_PublicKey* k) : key(k) {}
private:
const DL_Scheme_PublicKey* key;
};
return new DL_Scheme_Encoder(this);
}
/*
* Return the X.509 public key decoder
*/
X509_Decoder* DL_Scheme_PublicKey::x509_decoder()
{
class DL_Scheme_Decoder : public X509_Decoder
{
public:
void alg_id(const AlgorithmIdentifier& alg_id)
{
DataSource_Memory source(alg_id.parameters);
key->group.BER_decode(source, key->group_format());
}
void key_bits(const MemoryRegion<byte>& bits)
{
BER_Decoder(bits).decode(key->y);
key->X509_load_hook();
}
DL_Scheme_Decoder(DL_Scheme_PublicKey* k) : key(k) {}
private:
DL_Scheme_PublicKey* key;
};
return new DL_Scheme_Decoder(this);
}
/*
* Return the PKCS #8 private key encoder
*/
PKCS8_Encoder* DL_Scheme_PrivateKey::pkcs8_encoder() const
{
class DL_Scheme_Encoder : public PKCS8_Encoder
{
public:
AlgorithmIdentifier alg_id() const
{
MemoryVector<byte> group =
key->group.DER_encode(key->group_format());
return AlgorithmIdentifier(key->get_oid(), group);
}
MemoryVector<byte> key_bits() const
{
return DER_Encoder().encode(key->x).get_contents();
}
DL_Scheme_Encoder(const DL_Scheme_PrivateKey* k) : key(k) {}
private:
const DL_Scheme_PrivateKey* key;
};
return new DL_Scheme_Encoder(this);
}
/*
* Return the PKCS #8 private key decoder
*/
PKCS8_Decoder* DL_Scheme_PrivateKey::pkcs8_decoder(RandomNumberGenerator& rng)
{
class DL_Scheme_Decoder : public PKCS8_Decoder
{
public:
void alg_id(const AlgorithmIdentifier& alg_id)
{
DataSource_Memory source(alg_id.parameters);
key->group.BER_decode(source, key->group_format());
}
void key_bits(const MemoryRegion<byte>& bits)
{
BER_Decoder(bits).decode(key->x);
key->PKCS8_load_hook(rng);
}
DL_Scheme_Decoder(DL_Scheme_PrivateKey* k, RandomNumberGenerator& r) :
key(k), rng(r) {}
private:
DL_Scheme_PrivateKey* key;
RandomNumberGenerator& rng;
};
return new DL_Scheme_Decoder(this, rng);
}
/*
* Check Public DL Parameters
*/
bool DL_Scheme_PublicKey::check_key(RandomNumberGenerator& rng,
bool strong) const
{
if(y < 2 || y >= group_p())
return false;
if(!group.verify_group(rng, strong))
return false;
return true;
}
/*
* Check DL Scheme Private Parameters
*/
bool DL_Scheme_PrivateKey::check_key(RandomNumberGenerator& rng,
bool strong) const
{
const BigInt& p = group_p();
const BigInt& g = group_g();
if(y < 2 || y >= p || x < 2 || x >= p)
return false;
if(!group.verify_group(rng, strong))
return false;
if(!strong)
return true;
if(y != power_mod(g, x, p))
return false;
return true;
}
}
|