Debian Codegraph @

src/lib/crypto/BotanGOSTPublicKey.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 BotanGOSTPublicKey.cpp
29
30 Botan GOST R 34.10-2001 public key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #ifdef WITH_GOST
35 #include "log.h"
36 #include "BotanGOSTPublicKey.h"
37 #include "BotanUtil.h"
38 #include <string.h>
39
40 // Constructors
41 BotanGOSTPublicKey::BotanGOSTPublicKey()
42 {
43 eckey = NULL;
44 }
45
46 BotanGOSTPublicKey::BotanGOSTPublicKey(const Botan::GOST_3410_PublicKey* inECKEY)
47 {
48 BotanGOSTPublicKey();
49
50 setFromBotan(inECKEY);
51 }
52
53 // Destructor
54 BotanGOSTPublicKey::~BotanGOSTPublicKey()
55 {
56 delete eckey;
57 }
58
59 // The type
60 /*static*/ const char* BotanGOSTPublicKey::type = "Botan GOST Public Key";
61
62 // Get the base point order length
63 unsigned long BotanGOSTPublicKey::getOrderLength() const
64 {
65 try
66 {
67 Botan::EC_Group group = BotanUtil::byteString2ECGroup(ec);
68 return group.get_order().bytes();
69 }
70 catch (...)
71 {
72 ERROR_MSG("Can't get EC group for order length");
73
74 return 0;
75 }
76 }
77
78 // Get the output length
79 unsigned long BotanGOSTPublicKey::getOutputLength() const
80 {
81 return getOrderLength() * 2;
82 }
83
84 // Set from Botan representation
85 void BotanGOSTPublicKey::setFromBotan(const Botan::GOST_3410_PublicKey* inECKEY)
86 {
87 ByteString inEC = BotanUtil::ecGroup2ByteString(inECKEY->domain());
88 setEC(inEC);
89
90 ByteString inQ = BotanUtil::ecPoint2ByteString(inECKEY->public_point()).substr(3);
91
92 /* The points must be stored in little endian */
93 const size_t length = inQ.size() / 2;
94 for (size_t i = 0; i < (length / 2); i++)
95 {
96 std::swap(inQ[i], inQ[length-1-i]);
97 std::swap(inQ[length+i], inQ[2*length-1-i]);
98 }
99
100 setQ(inQ);
101 }
102
103 // Check if the key is of the given type
104 bool BotanGOSTPublicKey::isOfType(const char* inType)
105 {
106 return !strcmp(type, inType);
107 }
108
109 // Setters for the GOST public key components
110 void BotanGOSTPublicKey::setEC(const ByteString& inEC)
111 {
112 GOSTPublicKey::setEC(inEC);
113
114 if (eckey)
115 {
116 delete eckey;
117 eckey = NULL;
118 }
119 }
120
121 void BotanGOSTPublicKey::setQ(const ByteString& inQ)
122 {
123 GOSTPublicKey::setQ(inQ);
124
125 if (eckey)
126 {
127 delete eckey;
128 eckey = NULL;
129 }
130 }
131
132 // Serialisation
133 ByteString BotanGOSTPublicKey::serialise() const
134 {
135 return ec.serialise() +
136 q.serialise();
137 }
138
139 bool BotanGOSTPublicKey::deserialise(ByteString& serialised)
140 {
141 ByteString dEC = ByteString::chainDeserialise(serialised);
142 ByteString dQ = ByteString::chainDeserialise(serialised);
143
144 if ((dEC.size() == 0) ||
145 (dQ.size() == 0))
146 {
147 return false;
148 }
149
150 setEC(dEC);
151 setQ(dQ);
152
153 return true;
154 }
155
156 // Retrieve the Botan representation of the key
157 Botan::GOST_3410_PublicKey* BotanGOSTPublicKey::getBotanKey()
158 {
159 if (!eckey)
160 {
161 createBotanKey();
162 }
163
164 return eckey;
165 }
166
167 // Create the Botan representation of the key
168 void BotanGOSTPublicKey::createBotanKey()
169 {
170 if (ec.size() != 0 &&
171 q.size() != 0)
172 {
173 if (eckey)
174 {
175 delete eckey;
176 eckey = NULL;
177 }
178
179 try
180 {
181 /* The points are stored in little endian */
182 ByteString bPoint = q;
183 const size_t length = bPoint.size() / 2;
184 for (size_t i = 0; i < (length / 2); i++)
185 {
186 std::swap(bPoint[i], bPoint[length-1-i]);
187 std::swap(bPoint[length+i], bPoint[2*length-1-i]);
188 }
189 ByteString p = "044104" + bPoint;
190
191 Botan::EC_Group group = BotanUtil::byteString2ECGroup(ec);
192 Botan::PointGFp point = BotanUtil::byteString2ECPoint(p, group);
193 eckey = new Botan::GOST_3410_PublicKey(group, point);
194 }
195 catch (...)
196 {
197 ERROR_MSG("Could not create the Botan public key");
198 }
199 }
200 }
201 #endif