monotone

monotone Mtn Source Tree

Root/botan/dh.cpp

1/*************************************************
2* Diffie-Hellman Source File *
3* (C) 1999-2005 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 check_loaded_public();
28 }
29
30/*************************************************
31* Return the public value for key agreement *
32*************************************************/
33MemoryVector<byte> DH_PublicKey::public_value() const
34 {
35 return BigInt::encode_1363(y, group_p().bytes());
36 }
37
38/*************************************************
39* Create a DH private key *
40*************************************************/
41DH_PrivateKey::DH_PrivateKey(const DL_Group& grp)
42 {
43 group = grp;
44
45 const BigInt& p = group_p();
46 x = random_integer(2 * dl_work_factor(p.bits()), LongTermKey);
47
48 PKCS8_load_hook();
49 check_generated_private();
50 }
51
52/*************************************************
53* DH_PrivateKey Constructor *
54*************************************************/
55DH_PrivateKey::DH_PrivateKey(const DL_Group& grp, const BigInt& x1,
56 const BigInt& y1)
57 {
58 group = grp;
59 y = y1;
60 x = x1;
61
62 PKCS8_load_hook();
63 check_loaded_private();
64 }
65
66/*************************************************
67* Algorithm Specific PKCS #8 Initialization Code *
68*************************************************/
69void DH_PrivateKey::PKCS8_load_hook()
70 {
71 if(y == 0)
72 y = power_mod(group_g(), x, group_p());
73 core = DH_Core(group, x);
74 }
75
76/*************************************************
77* Return the public value for key agreement *
78*************************************************/
79MemoryVector<byte> DH_PrivateKey::public_value() const
80 {
81 return DH_PublicKey::public_value();
82 }
83
84/*************************************************
85* Derive a key *
86*************************************************/
87SecureVector<byte> DH_PrivateKey::derive_key(const byte w[],
88 u32bit w_len) const
89 {
90 return derive_key(BigInt::decode(w, w_len));
91 }
92
93/*************************************************
94* Derive a key *
95*************************************************/
96SecureVector<byte> DH_PrivateKey::derive_key(const DH_PublicKey& key) const
97 {
98 return derive_key(key.get_y());
99 }
100
101/*************************************************
102* Derive a key *
103*************************************************/
104SecureVector<byte> DH_PrivateKey::derive_key(const BigInt& w) const
105 {
106 const BigInt& p = group_p();
107 if(w <= 1 || w >= p-1)
108 throw Invalid_Argument(algo_name() + "::derive_key: Invalid key input");
109 return BigInt::encode_1363(core.agree(w), p.bytes());
110 }
111
112}

Archive Download this file

Branches

Tags

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