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/DSAPrivateKeyImpl.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_DSA("DSA");
34 }
35 
DSAPrivateKeyImpl(const DSAPrivateKey & copy)36 DSAPrivateKeyImpl::DSAPrivateKeyImpl(const DSAPrivateKey& copy)
37 {
38 	_params = new DSAParameterSpec(copy.getParams());
39 	_x = copy.getX();
40 	_enc = 0;
41 }
42 
DSAPrivateKeyImpl(const DSAPrivateKeyImpl & copy)43 DSAPrivateKeyImpl::DSAPrivateKeyImpl(const DSAPrivateKeyImpl& copy)
44 {
45 	_params = new DSAParameterSpec(*copy._params);
46 	_x = copy._x;
47 	_enc = 0;
48 }
49 
DSAPrivateKeyImpl(const DSAParams & params,const BigInteger & x)50 DSAPrivateKeyImpl::DSAPrivateKeyImpl(const DSAParams& params, const BigInteger& x) : _x(x)
51 {
52 	_params = new DSAParameterSpec(params.getP(), params.getQ(), params.getG());
53 	_enc = 0;
54 }
55 
DSAPrivateKeyImpl(const dsaparam & params,const mpnumber & x)56 DSAPrivateKeyImpl::DSAPrivateKeyImpl(const dsaparam& params, const mpnumber& x) : _x(x)
57 {
58 	_params = new DSAParameterSpec(BigInteger(params.p), BigInteger(params.q), BigInteger(params.g));
59 	_enc = 0;
60 }
61 
DSAPrivateKeyImpl(const BigInteger & p,const BigInteger & q,const BigInteger & g,const BigInteger & x)62 DSAPrivateKeyImpl::DSAPrivateKeyImpl(const BigInteger& p, const BigInteger& q, const BigInteger& g, const BigInteger& x) : _x(x)
63 {
64 	_params = new DSAParameterSpec(p, q, g);
65 	_enc = 0;
66 }
67 
~DSAPrivateKeyImpl()68 DSAPrivateKeyImpl::~DSAPrivateKeyImpl()
69 {
70 	delete _params;
71 	delete _enc;
72 }
73 
clone() const74 DSAPrivateKeyImpl* DSAPrivateKeyImpl::clone() const throw ()
75 {
76 	return new DSAPrivateKeyImpl(*this);
77 }
78 
equals(const Object * obj) const79 bool DSAPrivateKeyImpl::equals(const Object* obj) const throw ()
80 {
81 	if (this == obj)
82 		return true;
83 
84 	if (obj)
85 	{
86 		const DSAPrivateKey* pri = dynamic_cast<const DSAPrivateKey*>(obj);
87 		if (pri)
88 		{
89 			if (pri->getParams().getP() != _params->getP())
90 				return false;
91 
92 			if (pri->getParams().getQ() != _params->getQ())
93 				return false;
94 
95 			if (pri->getParams().getG() != _params->getG())
96 				return false;
97 
98 			if (pri->getX() != _x)
99 				return false;
100 
101 			return true;
102 		}
103 	}
104 
105 	return false;
106 }
107 
getParams() const108 const DSAParams& DSAPrivateKeyImpl::getParams() const throw ()
109 {
110 	return *_params;
111 }
112 
getX() const113 const BigInteger& DSAPrivateKeyImpl::getX() const throw ()
114 {
115 	return _x;
116 }
117 
getEncoded() const118 const bytearray* DSAPrivateKeyImpl::getEncoded() const throw ()
119 {
120 	if (!_enc)
121 	{
122 		try
123 		{
124 			ByteArrayOutputStream bos;
125 			BeeOutputStream bee(bos);
126 
127 			bee.writeBigInteger(_params->getP());
128 			bee.writeBigInteger(_params->getQ());
129 			bee.writeBigInteger(_params->getG());
130 			bee.writeBigInteger(_x);
131 			bee.close();
132 
133 			_enc = bos.toByteArray();
134 		}
135 		catch (IOException&)
136 		{
137 		}
138 	}
139 
140 	return _enc;
141 }
142 
getAlgorithm() const143 const String& DSAPrivateKeyImpl::getAlgorithm() const throw ()
144 {
145 	return ALGORITHM_DSA;
146 }
147 
getFormat() const148 const String* DSAPrivateKeyImpl::getFormat() const throw ()
149 {
150 	return &FORMAT_BEE;
151 }
152