aboutsummaryrefslogtreecommitdiffstats
path: root/src/dl_algo.cpp
blob: e296962005946f3d002f8125757459390caa9bb5 (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
/*************************************************
* DL Scheme Source File                          *
* (C) 1999-2006 The Botan Project                *
*************************************************/

#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 encoding           *
*************************************************/
MemoryVector<byte> DL_Scheme_PublicKey::DER_encode_pub() const
   {
   DER_Encoder der;
   der.encode(y);
   return der.get_contents();
   }

/*************************************************
* Return the X.509 parameters encoding           *
*************************************************/
MemoryVector<byte> DL_Scheme_PublicKey::DER_encode_params() const
   {
   return group.DER_encode(group_format());
   }

/*************************************************
* Decode X.509 public key encoding               *
*************************************************/
void DL_Scheme_PublicKey::BER_decode_pub(DataSource& source)
   {
   BER_Decoder(source).decode(y);

   if(y < 2 || y >= group_p())
      throw Invalid_Argument(algo_name() + ": Invalid public key");
   X509_load_hook();
   }

/*************************************************
* Decode X.509 algorithm parameters              *
*************************************************/
void DL_Scheme_PublicKey::BER_decode_params(DataSource& source)
   {
   group.BER_decode(source, group_format());
   }

/*************************************************
* Return the PKCS #8 private key encoding        *
*************************************************/
SecureVector<byte> DL_Scheme_PrivateKey::DER_encode_priv() const
   {
   DER_Encoder der;
   der.encode(x);
   return der.get_contents();
   }

/*************************************************
* Decode a PKCS #8 private key encoding          *
*************************************************/
void DL_Scheme_PrivateKey::BER_decode_priv(DataSource& source)
   {
   BER_Decoder(source).decode(x);

   PKCS8_load_hook();
   check_loaded_private();
   }

/*************************************************
* Check Public DL Parameters                     *
*************************************************/
bool DL_Scheme_PublicKey::check_key(bool strong) const
   {
   if(y < 2 || y >= group_p())
      return false;
   if(!group.verify_group(strong))
      return false;
   return true;
   }

/*************************************************
* Check DL Scheme Private Parameters             *
*************************************************/
bool DL_Scheme_PrivateKey::check_key(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(strong))
      return false;

   if(!strong)
      return true;

   if(y != power_mod(g, x, p))
      return false;

   return true;
   }

}