Debian Codegraph @

src/lib/crypto/BotanEDPrivateKey.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 BotanEDPrivateKey.cpp
29
30 Botan EDDSA private key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #ifdef WITH_EDDSA
35 #include "log.h"
36 #include "BotanEDPrivateKey.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 #include <botan/curve25519.h>
48 #include <botan/ed25519.h>
49 // #include <botan/curve448.h>
50 // #include <botan/ed448.h>
51
52 const Botan::OID x25519_oid("1.3.101.110");
53 // const Botan::OID x448_oid("1.3.101.111");
54 const Botan::OID ed25519_oid("1.3.101.112");
55 // const Botan::OID ed448_oid("1.3.101.113");
56
57 // Constructors
58 BotanEDPrivateKey::BotanEDPrivateKey()
59 {
60 edkey = NULL;
61 }
62
63 BotanEDPrivateKey::BotanEDPrivateKey(const Botan::Private_Key* inEDKEY)
64 {
65 edkey = NULL;
66
67 setFromBotan(inEDKEY);
68 }
69
70 // Destructor
71 BotanEDPrivateKey::~BotanEDPrivateKey()
72 {
73 delete edkey;
74 }
75
76 // The type
77 /*static*/ const char* BotanEDPrivateKey::type = "Botan EDDSA Private Key";
78
79 // Get the base point order length
80 unsigned long BotanEDPrivateKey::getOrderLength() const
81 {
82 // Only Ed25519 is supported
83 return 32;
84 }
85
86 // Set from Botan representation
87 void BotanEDPrivateKey::setFromBotan(const Botan::Private_Key* inEDKEY)
88 {
89 Botan::OID oid;
90 Botan::secure_vector<uint8_t> priv;
91
92 for (;;)
93 {
94 const Botan::Curve25519_PrivateKey* x25519 = dynamic_cast<const Botan::Curve25519_PrivateKey*>(inEDKEY);
95 if (x25519) {
96 oid = x25519_oid;
97 priv = x25519->get_x();
98 break;
99 }
100 const Botan::Ed25519_PrivateKey* ed25519 = dynamic_cast<const Botan::Ed25519_PrivateKey*>(inEDKEY);
101 if (ed25519) {
102 oid = ed25519_oid;
103 priv = ed25519->get_private_key();
104 // Botan returns public part too
105 priv.resize(32);
106 break;
107 }
108 return;
109 }
110 ByteString inEC = BotanUtil::oid2ByteString(oid);
111 setEC(inEC);
112 ByteString inK;
113 inK.resize(priv.size());
114 memcpy(&inK[0], &priv[0], priv.size());
115 setK(inK);
116 }
117
118 // Check if the key is of the given type
119 bool BotanEDPrivateKey::isOfType(const char* inType)
120 {
121 return !strcmp(type, inType);
122 }
123
124 // Setters for the EDDSA private key components
125 void BotanEDPrivateKey::setK(const ByteString& inK)
126 {
127 EDPrivateKey::setK(inK);
128
129 if (edkey)
130 {
131 delete edkey;
132 edkey = NULL;
133 }
134 }
135
136 // Setters for the EDDSA public key components
137 void BotanEDPrivateKey::setEC(const ByteString& inEC)
138 {
139 EDPrivateKey::setEC(inEC);
140
141 if (edkey)
142 {
143 delete edkey;
144 edkey = NULL;
145 }
146 }
147
148 // Encode into PKCS#8 DER
149 ByteString BotanEDPrivateKey::PKCS8Encode()
150 {
151 ByteString der;
152 createBotanKey();
153 if (edkey == NULL) return der;
154 const Botan::secure_vector<uint8_t> ber = Botan::PKCS8::BER_encode(*edkey);
155 der.resize(ber.size());
156 memcpy(&der[0], &ber[0], ber.size());
157 return der;
158 }
159
160 // Decode from PKCS#8 BER
161 bool BotanEDPrivateKey::PKCS8Decode(const ByteString& ber)
162 {
163 Botan::DataSource_Memory source(ber.const_byte_str(), ber.size());
164 if (source.end_of_data()) return false;
165 Botan::secure_vector<uint8_t> keydata;
166 Botan::AlgorithmIdentifier alg_id;
167 Botan::Private_Key* key = NULL;
168 try
169 {
170 Botan::BER_Decoder(source)
171 .start_cons(Botan::SEQUENCE)
172 .decode_and_check<size_t>(0, "Unknown PKCS #8 version number")
173 .decode(alg_id)
174 .decode(keydata, Botan::OCTET_STRING)
175 .discard_remaining()
176 .end_cons();
177 if (keydata.empty())
178 throw Botan::Decoding_Error("PKCS #8 private key decoding failed");
179 if (alg_id.oid == x25519_oid)
180 {
181 key = new Botan::Curve25519_PrivateKey(alg_id, keydata);
182 }
183 else if (alg_id.oid == ed25519_oid)
184 {
185 key = new Botan::Ed25519_PrivateKey(alg_id, keydata);
186 }
187 else
188 {
189 ERROR_MSG("Decoded private key not Ed25519");
190
191 return false;
192 }
193 if (key == NULL) return false;
194
195 setFromBotan(key);
196
197 delete key;
198 }
199 catch (std::exception& e)
200 {
201 ERROR_MSG("Decode failed on %s", e.what());
202
203 return false;
204 }
205
206 return true;
207 }
208
209 // Retrieve the Botan representation of the key
210 Botan::Private_Key* BotanEDPrivateKey::getBotanKey()
211 {
212 if (!edkey)
213 {
214 createBotanKey();
215 }
216
217 return edkey;
218 }
219
220 // Create the Botan representation of the key
221 void BotanEDPrivateKey::createBotanKey()
222 {
223 if (ec.size() != 0 &&
224 k.size() != 0)
225 {
226 if (edkey)
227 {
228 delete edkey;
229 edkey = NULL;
230 }
231
232 try
233 {
234 Botan::secure_vector<uint8_t> priv(k.size());
235 memcpy(&priv[0], k.const_byte_str(), k.size());
236 Botan::OID oid = BotanUtil::byteString2Oid(ec);
237 if (oid == x25519_oid)
238 {
239 edkey = new Botan::Curve25519_PrivateKey(priv);
240 }
241 else if (oid == ed25519_oid)
242 {
243 edkey = new Botan::Ed25519_PrivateKey(priv);
244 }
245 }
246 catch (...)
247 {
248 ERROR_MSG("Could not create the Botan private key");
249 }
250 }
251 }
252 #endif