Debian Codegraph @

src/lib/crypto/BotanGOSTPrivateKey.cpp

raw
1 /*
2 * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation)
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 BotanGOSTPrivateKey.cpp
29
30 Botan GOST R 34.10-2001 private key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #ifdef WITH_GOST
35 #include "log.h"
36 #include "BotanGOSTPrivateKey.h"
37 #include "BotanCryptoFactory.h"
38 #include "BotanRNG.h"
39 #include "BotanUtil.h"
40 #include <string.h>
41 #include <botan/pkcs8.h>
42 #include <botan/ber_dec.h>
43 #include <botan/der_enc.h>
44 #include <botan/asn1_oid.h>
45 #include <botan/oids.h>
46 #include <botan/version.h>
47
48 // Constructors
49 BotanGOSTPrivateKey::BotanGOSTPrivateKey()
50 {
51 eckey = NULL;
52 }
53
54 BotanGOSTPrivateKey::BotanGOSTPrivateKey(const Botan::GOST_3410_PrivateKey* inECKEY)
55 {
56 BotanGOSTPrivateKey();
57
58 setFromBotan(inECKEY);
59 }
60
61 // Destructor
62 BotanGOSTPrivateKey::~BotanGOSTPrivateKey()
63 {
64 delete eckey;
65 }
66
67 // The type
68 /*static*/ const char* BotanGOSTPrivateKey::type = "Botan GOST Private Key";
69
70 // Get the base point order length
71 unsigned long BotanGOSTPrivateKey::getOrderLength() const
72 {
73 try
74 {
75 Botan::EC_Group group = BotanUtil::byteString2ECGroup(ec);
76 return group.get_order().bytes();
77 }
78 catch (...)
79 {
80 ERROR_MSG("Can't get EC group for order length");
81
82 return 0;
83 }
84 }
85
86 // Get the output length
87 unsigned long BotanGOSTPrivateKey::getOutputLength() const
88 {
89 return getOrderLength() * 2;
90 }
91
92 // Set from Botan representation
93 void BotanGOSTPrivateKey::setFromBotan(const Botan::GOST_3410_PrivateKey* inECKEY)
94 {
95 ByteString inEC = BotanUtil::ecGroup2ByteString(inECKEY->domain());
96 setEC(inEC);
97 ByteString inD = BotanUtil::bigInt2ByteStringPrefix(inECKEY->private_value(), 32);
98 setD(inD);
99 }
100
101 // Check if the key is of the given type
102 bool BotanGOSTPrivateKey::isOfType(const char* inType)
103 {
104 return !strcmp(type, inType);
105 }
106
107 // Setters for the GOST private key components
108 void BotanGOSTPrivateKey::setD(const ByteString& inD)
109 {
110 GOSTPrivateKey::setD(inD);
111
112 if (eckey)
113 {
114 delete eckey;
115 eckey = NULL;
116 }
117 }
118
119
120 // Setters for the GOST public key components
121 void BotanGOSTPrivateKey::setEC(const ByteString& inEC)
122 {
123 GOSTPrivateKey::setEC(inEC);
124
125 if (eckey)
126 {
127 delete eckey;
128 eckey = NULL;
129 }
130 }
131
132 // Serialisation
133 ByteString BotanGOSTPrivateKey::serialise() const
134 {
135 return ec.serialise() +
136 d.serialise();
137 }
138
139 bool BotanGOSTPrivateKey::deserialise(ByteString& serialised)
140 {
141 ByteString dEC = ByteString::chainDeserialise(serialised);
142 ByteString dD = ByteString::chainDeserialise(serialised);
143
144 if ((dEC.size() == 0) ||
145 (dD.size() == 0))
146 {
147 return false;
148 }
149
150 setEC(dEC);
151 setD(dD);
152
153 return true;
154 }
155
156 // Encode into PKCS#8 DER
157 ByteString BotanGOSTPrivateKey::PKCS8Encode()
158 {
159 ByteString der;
160 createBotanKey();
161 if (eckey == NULL) return der;
162 // Force EC_DOMPAR_ENC_OID
163 const size_t PKCS8_VERSION = 0;
164 const std::vector<uint8_t> parameters = eckey->domain().DER_encode(Botan::EC_DOMPAR_ENC_OID);
165 const Botan::AlgorithmIdentifier alg_id(Botan::OIDS::lookup("GOST-34.10"), parameters);
166 const Botan::secure_vector<uint8_t> ber =
167 Botan::DER_Encoder()
168 .start_cons(Botan::SEQUENCE)
169 .encode(PKCS8_VERSION)
170 .encode(alg_id)
171 .encode(eckey->private_key_bits(), Botan::OCTET_STRING)
172 .end_cons()
173 .get_contents();
174 der.resize(ber.size());
175 memcpy(&der[0], &ber[0], ber.size());
176 return der;
177 }
178
179 // Decode from PKCS#8 BER
180 bool BotanGOSTPrivateKey::PKCS8Decode(const ByteString& ber)
181 {
182 Botan::DataSource_Memory source(ber.const_byte_str(), ber.size());
183 if (source.end_of_data()) return false;
184 Botan::secure_vector<uint8_t> keydata;
185 Botan::AlgorithmIdentifier alg_id;
186 Botan::GOST_3410_PrivateKey* key = NULL;
187 try
188 {
189 Botan::BER_Decoder(source)
190 .start_cons(Botan::SEQUENCE)
191 .decode_and_check<size_t>(0, "Unknown PKCS #8 version number")
192 .decode(alg_id)
193 .decode(keydata, Botan::OCTET_STRING)
194 .discard_remaining()
195 .end_cons();
196 if (keydata.empty())
197 throw Botan::Decoding_Error("PKCS #8 private key decoding failed");
198 if (Botan::OIDS::lookup(alg_id.oid).compare("GOST-34.10"))
199 {
200 ERROR_MSG("Decoded private key not GOST-34.10");
201
202 return false;
203 }
204 key = new Botan::GOST_3410_PrivateKey(alg_id, keydata);
205 if (key == NULL) return false;
206
207 setFromBotan(key);
208
209 delete key;
210 }
211 catch (std::exception& e)
212 {
213 ERROR_MSG("Decode failed on %s", e.what());
214
215 return false;
216 }
217
218 return true;
219 }
220
221 // Retrieve the Botan representation of the key
222 Botan::GOST_3410_PrivateKey* BotanGOSTPrivateKey::getBotanKey()
223 {
224 if (!eckey)
225 {
226 createBotanKey();
227 }
228
229 return eckey;
230 }
231
232 // Create the Botan representation of the key
233 void BotanGOSTPrivateKey::createBotanKey()
234 {
235 if (ec.size() != 0 &&
236 d.size() != 0)
237 {
238 if (eckey)
239 {
240 delete eckey;
241 eckey = NULL;
242 }
243
244 try
245 {
246 BotanRNG* rng = (BotanRNG*)BotanCryptoFactory::i()->getRNG();
247 Botan::EC_Group group = BotanUtil::byteString2ECGroup(ec);
248 eckey = new Botan::GOST_3410_PrivateKey(*rng->getRNG(),
249 group,
250 BotanUtil::byteString2bigInt(d));
251 }
252 catch (...)
253 {
254 ERROR_MSG("Could not create the Botan public key");
255 }
256 }
257 }
258 #endif