monotone

monotone Mtn Source Tree

Root/botan/dh.cpp

1/*************************************************
2* Diffie-Hellman Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/dh.h>
7#include <botan/numthry.h>
8#include <botan/util.h>
9
10namespace Botan {
11
12/*************************************************
13* DH_PublicKey Constructor *
14*************************************************/
15DH_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*************************************************/
25void DH_PublicKey::X509_load_hook()
26 {
27 load_check();
28 }
29
30/*************************************************
31* Return the maximum input size in bits *
32*************************************************/
33u32bit DH_PublicKey::max_input_bits() const
34 {
35 return group_p().bits();
36 }
37
38/*************************************************
39* Return the public value for key agreement *
40*************************************************/
41MemoryVector<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*************************************************/
49DH_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*************************************************/
62DH_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*************************************************/
75void 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*************************************************/
90MemoryVector<byte> DH_PrivateKey::public_value() const
91 {
92 return DH_PublicKey::public_value();
93 }
94
95/*************************************************
96* Derive a key *
97*************************************************/
98SecureVector<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*************************************************/
107SecureVector<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*************************************************/
115SecureVector<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}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status