Debian Codegraph @

src/lib/crypto/BotanEDPublicKey.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 BotanEDPublicKey.cpp
29
30 Botan EDDSA public key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #ifdef WITH_EDDSA
35 #include "log.h"
36 #include "BotanEDPublicKey.h"
37 #include "BotanUtil.h"
38 #include "DerUtil.h"
39 #include <string.h>
40 #include <botan/curve25519.h>
41 #include <botan/ed25519.h>
42 // #include <botan/curve448.h>
43 // #include <botan/ed448.h>
44
45 const Botan::OID x25519_oid("1.3.101.110");
46 // const Botan::OID x448_oid("1.3.101.111");
47 const Botan::OID ed25519_oid("1.3.101.112");
48 // const Botan::OID ed448_oid("1.3.101.113");
49
50 // Constructors
51 BotanEDPublicKey::BotanEDPublicKey()
52 {
53 edkey = NULL;
54 }
55
56 BotanEDPublicKey::BotanEDPublicKey(const Botan::Public_Key* inEDKEY)
57 {
58 edkey = NULL;
59
60 setFromBotan(inEDKEY);
61 }
62
63 // Destructor
64 BotanEDPublicKey::~BotanEDPublicKey()
65 {
66 delete edkey;
67 }
68
69 // The type
70 /*static*/ const char* BotanEDPublicKey::type = "Botan EDDSA Public Key";
71
72 // Get the base point order length
73 unsigned long BotanEDPublicKey::getOrderLength() const
74 {
75 // Only Ed25519 is supported
76 return 32;
77 }
78
79 // Set from Botan representation
80 void BotanEDPublicKey::setFromBotan(const Botan::Public_Key* inEDKEY)
81 {
82 Botan::OID oid;
83 std::vector<uint8_t> pub;
84
85 for (;;)
86 {
87 const Botan::Curve25519_PublicKey* x25519 = dynamic_cast<const Botan::Curve25519_PublicKey*>(inEDKEY);
88 if (x25519) {
89 oid = x25519_oid;
90 pub = x25519->public_value();
91 break;
92 }
93 const Botan::Ed25519_PublicKey* ed25519 = dynamic_cast<const Botan::Ed25519_PublicKey*>(inEDKEY);
94 if (ed25519) {
95 oid = ed25519_oid;
96 pub = ed25519->get_public_key();
97 break;
98 }
99 return;
100 }
101 ByteString inEC = BotanUtil::oid2ByteString(oid);
102 setEC(inEC);
103 ByteString inA;
104 inA.resize(pub.size());
105 memcpy(&inA[0], &pub[0], pub.size());
106 setA(DERUTIL::raw2Octet(inA));
107 }
108
109 // Check if the key is of the given type
110 bool BotanEDPublicKey::isOfType(const char* inType)
111 {
112 return !strcmp(type, inType);
113 }
114
115 // Setters for the EDDSA public key components
116 void BotanEDPublicKey::setEC(const ByteString& inEC)
117 {
118 EDPublicKey::setEC(inEC);
119
120 if (edkey)
121 {
122 delete edkey;
123 edkey = NULL;
124 }
125 }
126
127 void BotanEDPublicKey::setA(const ByteString& inA)
128 {
129 EDPublicKey::setA(inA);
130
131 if (edkey)
132 {
133 delete edkey;
134 edkey = NULL;
135 }
136 }
137
138 // Retrieve the Botan representation of the key
139 Botan::Public_Key* BotanEDPublicKey::getBotanKey()
140 {
141 if (!edkey)
142 {
143 createBotanKey();
144 }
145
146 return edkey;
147 }
148
149 // Create the Botan representation of the key
150 void BotanEDPublicKey::createBotanKey()
151 {
152 if (ec.size() != 0 &&
153 a.size() != 0)
154 {
155 if (edkey)
156 {
157 delete edkey;
158 edkey = NULL;
159 }
160
161 try
162 {
163 ByteString raw = DERUTIL::octet2Raw(a);
164 size_t len = raw.size();
165 if (len == 0) return;
166
167 std::vector<uint8_t> pub(len);
168 memcpy(&pub[0], raw.const_byte_str(), len);
169 Botan::OID oid = BotanUtil::byteString2Oid(ec);
170 if (oid == x25519_oid)
171 {
172 edkey = new Botan::Curve25519_PublicKey(pub);
173 }
174 else if (oid == ed25519_oid)
175 {
176 edkey = new Botan::Ed25519_PublicKey(pub);
177 }
178 }
179 catch (...)
180 {
181 ERROR_MSG("Could not create the Botan public key");
182 }
183 }
184 }
185 #endif