1 /*
2 * Copyright (c) 2004 Beeyond Software Holding BV
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
22
23 #include "beecrypt/c++/provider/DHPublicKeyImpl.h"
24 #include "beecrypt/c++/io/ByteArrayOutputStream.h"
25 using beecrypt::io::ByteArrayOutputStream;
26 #include "beecrypt/c++/beeyond/BeeOutputStream.h"
27 using beecrypt::beeyond::BeeOutputStream;
28
29 using namespace beecrypt::provider;
30
31 namespace {
32 const String FORMAT_BEE("BEE");
33 const String ALGORITHM_DH("DH");
34 }
35
DHPublicKeyImpl(const DHPublicKey & copy)36 DHPublicKeyImpl::DHPublicKeyImpl(const DHPublicKey& copy) : _y(copy.getY())
37 {
38 _params = new DHParameterSpec(copy.getParams());
39 _enc = 0;
40 }
41
DHPublicKeyImpl(const DHPublicKeyImpl & copy)42 DHPublicKeyImpl::DHPublicKeyImpl(const DHPublicKeyImpl& copy) : _y(copy._y)
43 {
44 _params = new DHParameterSpec(*copy._params);
45 _enc = 0;
46 }
47
DHPublicKeyImpl(const DHParams & params,const BigInteger & y)48 DHPublicKeyImpl::DHPublicKeyImpl(const DHParams& params, const BigInteger& y) : _y(y)
49 {
50 _params = new DHParameterSpec(params.getP(), params.getG(), params.getL());
51 _enc = 0;
52 }
53
DHPublicKeyImpl(const dhparam & params,const mpnumber & y)54 DHPublicKeyImpl::DHPublicKeyImpl(const dhparam& params, const mpnumber& y) : _y(y)
55 {
56 _params = new DHParameterSpec(BigInteger(params.p), BigInteger(params.g));
57 _enc = 0;
58 }
59
DHPublicKeyImpl(const BigInteger & p,const BigInteger & g,const BigInteger & y)60 DHPublicKeyImpl::DHPublicKeyImpl(const BigInteger& p, const BigInteger& g, const BigInteger& y) : _y(y)
61 {
62 _params = new DHParameterSpec(p, g);
63 _enc = 0;
64 }
65
~DHPublicKeyImpl()66 DHPublicKeyImpl::~DHPublicKeyImpl()
67 {
68 delete _params;
69 delete _enc;
70 }
71
clone() const72 DHPublicKeyImpl* DHPublicKeyImpl::clone() const throw ()
73 {
74 return new DHPublicKeyImpl(*this);
75 }
76
equals(const Object * obj) const77 bool DHPublicKeyImpl::equals(const Object* obj) const throw ()
78 {
79 if (this == obj)
80 return true;
81
82 const DHPublicKey* pub = dynamic_cast<const DHPublicKey*>(obj);
83 if (pub)
84 {
85 if (pub->getParams().getP() != _params->getP())
86 return false;
87
88 if (pub->getParams().getG() != _params->getG())
89 return false;
90
91 if (pub->getY() != _y)
92 return false;
93
94 return true;
95 }
96
97 return false;
98 }
99
getParams() const100 const DHParams& DHPublicKeyImpl::getParams() const throw ()
101 {
102 return *_params;
103 }
104
getY() const105 const BigInteger& DHPublicKeyImpl::getY() const throw ()
106 {
107 return _y;
108 }
109
getEncoded() const110 const bytearray* DHPublicKeyImpl::getEncoded() const throw ()
111 {
112 if (!_enc)
113 {
114 try
115 {
116 ByteArrayOutputStream bos;
117 BeeOutputStream bee(bos);
118
119 bee.writeBigInteger(_params->getP());
120 bee.writeBigInteger(_params->getG());
121 bee.writeBigInteger(_y);
122 bee.close();
123
124 _enc = bos.toByteArray();
125 }
126 catch (IOException&)
127 {
128 }
129 }
130
131 return _enc;
132 }
133
getAlgorithm() const134 const String& DHPublicKeyImpl::getAlgorithm() const throw ()
135 {
136 return ALGORITHM_DH;
137 }
138
getFormat() const139 const String* DHPublicKeyImpl::getFormat() const throw ()
140 {
141 return &FORMAT_BEE;
142 }
143