Debian Codegraph @

src/lib/crypto/BotanECDHPrivateKey.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 BotanECDHPrivateKey.cpp
29
30 Botan ECDH private key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #ifdef WITH_ECC
35 #include "log.h"
36 #include "BotanECDHPrivateKey.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 BotanECDHPrivateKey::BotanECDHPrivateKey()
50 {
51 eckey = NULL;
52 }
53
54 BotanECDHPrivateKey::BotanECDHPrivateKey(const Botan::ECDH_PrivateKey* inECKEY)
55 {
56 eckey = NULL;
57
58 setFromBotan(inECKEY);
59 }
60
61 // Destructor
62 BotanECDHPrivateKey::~BotanECDHPrivateKey()
63 {
64 delete eckey;
65 }
66
67 // The type
68 /*static*/ const char* BotanECDHPrivateKey::type = "Botan ECDH Private Key";
69
70 // Get the base point order length
71 unsigned long BotanECDHPrivateKey::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 // Set from Botan representation
87 void BotanECDHPrivateKey::setFromBotan(const Botan::ECDH_PrivateKey* inECKEY)
88 {
89 ByteString inEC = BotanUtil::ecGroup2ByteString(inECKEY->domain());
90 setEC(inEC);
91 ByteString inD = BotanUtil::bigInt2ByteString(inECKEY->private_value());
92 setD(inD);
93 }
94
95 // Check if the key is of the given type
96 bool BotanECDHPrivateKey::isOfType(const char* inType)
97 {
98 return !strcmp(type, inType);
99 }
100
101 // Setters for the ECDH private key components
102 void BotanECDHPrivateKey::setD(const ByteString& inD)
103 {
104 ECPrivateKey::setD(inD);
105
106 if (eckey)
107 {
108 delete eckey;
109 eckey = NULL;
110 }
111 }
112
113 // Setters for the ECDH public key components
114 void BotanECDHPrivateKey::setEC(const ByteString& inEC)
115 {
116 ECPrivateKey::setEC(inEC);
117
118 if (eckey)
119 {
120 delete eckey;
121 eckey = NULL;
122 }
123 }
124
125 // Encode into PKCS#8 DER
126 ByteString BotanECDHPrivateKey::PKCS8Encode()
127 {
128 ByteString der;
129 createBotanKey();
130 if (eckey == NULL) return der;
131 const size_t PKCS8_VERSION = 0;
132 // No OID for ECDH
133 const Botan::OID oid("1.2.840.10045.2.1");
134 // Force EC_DOMPAR_ENC_OID
135 const std::vector<uint8_t> parameters = eckey->domain().DER_encode(Botan::EC_DOMPAR_ENC_OID);
136 const Botan::AlgorithmIdentifier alg_id(oid, parameters);
137 const Botan::secure_vector<uint8_t> ber =
138 Botan::DER_Encoder()
139 .start_cons(Botan::SEQUENCE)
140 .encode(PKCS8_VERSION)
141 .encode(alg_id)
142 .encode(eckey->private_key_bits(), Botan::OCTET_STRING)
143 .end_cons()
144 .get_contents();
145 der.resize(ber.size());
146 memcpy(&der[0], &ber[0], ber.size());
147 return der;
148 }
149
150 // Decode from PKCS#8 BER
151 bool BotanECDHPrivateKey::PKCS8Decode(const ByteString& ber)
152 {
153 Botan::DataSource_Memory source(ber.const_byte_str(), ber.size());
154 if (source.end_of_data()) return false;
155 Botan::secure_vector<uint8_t> keydata;
156 Botan::AlgorithmIdentifier alg_id;
157 const Botan::OID oid("1.2.840.10045.2.1");
158 Botan::ECDH_PrivateKey* key = NULL;
159 try
160 {
161 Botan::BER_Decoder(source)
162 .start_cons(Botan::SEQUENCE)
163 .decode_and_check<size_t>(0, "Unknown PKCS #8 version number")
164 .decode(alg_id)
165 .decode(keydata, Botan::OCTET_STRING)
166 .discard_remaining()
167 .end_cons();
168 if (keydata.empty())
169 throw Botan::Decoding_Error("PKCS #8 private key decoding failed");
170 if (alg_id.oid != oid)
171 {
172 ERROR_MSG("Decoded private key not ECDH");
173
174 return false;
175 }
176 key = new Botan::ECDH_PrivateKey(alg_id, keydata);
177 if (key == NULL) return false;
178
179 setFromBotan(key);
180
181 delete key;
182 }
183 catch (std::exception& e)
184 {
185 ERROR_MSG("Decode failed on %s", e.what());
186
187 return false;
188 }
189
190 return true;
191 }
192
193 // Retrieve the Botan representation of the key
194 Botan::ECDH_PrivateKey* BotanECDHPrivateKey::getBotanKey()
195 {
196 if (!eckey)
197 {
198 createBotanKey();
199 }
200
201 return eckey;
202 }
203
204 // Create the Botan representation of the key
205 void BotanECDHPrivateKey::createBotanKey()
206 {
207 if (ec.size() != 0 &&
208 d.size() != 0)
209 {
210 if (eckey)
211 {
212 delete eckey;
213 eckey = NULL;
214 }
215
216 try
217 {
218 BotanRNG* rng = (BotanRNG*)BotanCryptoFactory::i()->getRNG();
219 Botan::EC_Group group = BotanUtil::byteString2ECGroup(ec);
220 eckey = new Botan::ECDH_PrivateKey(*rng->getRNG(),
221 group,
222 BotanUtil::byteString2bigInt(d));
223 }
224 catch (...)
225 {
226 ERROR_MSG("Could not create the Botan public key");
227 }
228 }
229 }
230 #endif