~ubuntu-branches/debian/sid/botan/sid

« back to all changes in this revision

Viewing changes to src/dh.cpp

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2018-03-01 22:23:25 UTC
  • mfrom: (1.2.2)
  • Revision ID: package-import@ubuntu.com-20180301222325-7p7vc45gu3hta34d
Tags: 2.4.0-2
* Don't remove .doctrees from the manual if it doesn't exist.
* Don't specify parallel to debhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*************************************************
2
 
* Diffie-Hellman Source File                     *
3
 
* (C) 1999-2007 The Botan Project                *
4
 
*************************************************/
5
 
 
6
 
#include <botan/dh.h>
7
 
#include <botan/numthry.h>
8
 
#include <botan/util.h>
9
 
 
10
 
namespace Botan {
11
 
 
12
 
/*************************************************
13
 
* DH_PublicKey Constructor                       *
14
 
*************************************************/
15
 
DH_PublicKey::DH_PublicKey(const DL_Group& grp, const BigInt& y1)
16
 
   {
17
 
   group = grp;
18
 
   y = y1;
19
 
   X509_load_hook();
20
 
   }
21
 
 
22
 
/*************************************************
23
 
* Algorithm Specific X.509 Initialization Code   *
24
 
*************************************************/
25
 
void DH_PublicKey::X509_load_hook()
26
 
   {
27
 
   load_check();
28
 
   }
29
 
 
30
 
/*************************************************
31
 
* Return the maximum input size in bits          *
32
 
*************************************************/
33
 
u32bit DH_PublicKey::max_input_bits() const
34
 
   {
35
 
   return group_p().bits();
36
 
   }
37
 
 
38
 
/*************************************************
39
 
* Return the public value for key agreement      *
40
 
*************************************************/
41
 
MemoryVector<byte> DH_PublicKey::public_value() const
42
 
   {
43
 
   return BigInt::encode_1363(y, group_p().bytes());
44
 
   }
45
 
 
46
 
/*************************************************
47
 
* Create a DH private key                        *
48
 
*************************************************/
49
 
DH_PrivateKey::DH_PrivateKey(const DL_Group& grp)
50
 
   {
51
 
   group = grp;
52
 
 
53
 
   const BigInt& p = group_p();
54
 
   x = random_integer(2 * dl_work_factor(p.bits()));
55
 
 
56
 
   PKCS8_load_hook(true);
57
 
   }
58
 
 
59
 
/*************************************************
60
 
* DH_PrivateKey Constructor                      *
61
 
*************************************************/
62
 
DH_PrivateKey::DH_PrivateKey(const DL_Group& grp, const BigInt& x1,
63
 
                             const BigInt& y1)
64
 
   {
65
 
   group = grp;
66
 
   y = y1;
67
 
   x = x1;
68
 
 
69
 
   PKCS8_load_hook();
70
 
   }
71
 
 
72
 
/*************************************************
73
 
* Algorithm Specific PKCS #8 Initialization Code *
74
 
*************************************************/
75
 
void DH_PrivateKey::PKCS8_load_hook(bool generated)
76
 
   {
77
 
   if(y == 0)
78
 
      y = power_mod(group_g(), x, group_p());
79
 
   core = DH_Core(group, x);
80
 
 
81
 
   if(generated)
82
 
      gen_check();
83
 
   else
84
 
      load_check();
85
 
   }
86
 
 
87
 
/*************************************************
88
 
* Return the public value for key agreement      *
89
 
*************************************************/
90
 
MemoryVector<byte> DH_PrivateKey::public_value() const
91
 
   {
92
 
   return DH_PublicKey::public_value();
93
 
   }
94
 
 
95
 
/*************************************************
96
 
* Derive a key                                   *
97
 
*************************************************/
98
 
SecureVector<byte> DH_PrivateKey::derive_key(const byte w[],
99
 
                                             u32bit w_len) const
100
 
   {
101
 
   return derive_key(BigInt::decode(w, w_len));
102
 
   }
103
 
 
104
 
/*************************************************
105
 
* Derive a key                                   *
106
 
*************************************************/
107
 
SecureVector<byte> DH_PrivateKey::derive_key(const DH_PublicKey& key) const
108
 
   {
109
 
   return derive_key(key.get_y());
110
 
   }
111
 
 
112
 
/*************************************************
113
 
* Derive a key                                   *
114
 
*************************************************/
115
 
SecureVector<byte> DH_PrivateKey::derive_key(const BigInt& w) const
116
 
   {
117
 
   const BigInt& p = group_p();
118
 
   if(w <= 1 || w >= p-1)
119
 
      throw Invalid_Argument(algo_name() + "::derive_key: Invalid key input");
120
 
   return BigInt::encode_1363(core.agree(w), p.bytes());
121
 
   }
122
 
 
123
 
}