890f135449805a5c4e8acf5d451d60e767f9adf6
[aaf/sshsm.git] / SoftHSMv2 / src / lib / crypto / BotanGOSTPrivateKey.cpp
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
42 // Constructors
43 BotanGOSTPrivateKey::BotanGOSTPrivateKey()
44 {
45         eckey = NULL;
46 }
47
48 BotanGOSTPrivateKey::BotanGOSTPrivateKey(const Botan::GOST_3410_PrivateKey* inECKEY)
49 {
50         BotanGOSTPrivateKey();
51
52         setFromBotan(inECKEY);
53 }
54
55 // Destructor
56 BotanGOSTPrivateKey::~BotanGOSTPrivateKey()
57 {
58         delete eckey;
59 }
60
61 // The type
62 /*static*/ const char* BotanGOSTPrivateKey::type = "Botan GOST Private Key";
63
64 // Get the base point order length
65 unsigned long BotanGOSTPrivateKey::getOrderLength() const
66 {
67         try
68         {
69                 Botan::EC_Group group = BotanUtil::byteString2ECGroup(ec);
70                 return group.get_order().bytes();
71         }
72         catch (...)
73         {
74                 ERROR_MSG("Can't get EC group for order length");
75
76                 return 0;
77         }
78 }
79
80 // Get the output length
81 unsigned long BotanGOSTPrivateKey::getOutputLength() const
82 {
83         return getOrderLength() * 2;
84 }
85
86 // Set from Botan representation
87 void BotanGOSTPrivateKey::setFromBotan(const Botan::GOST_3410_PrivateKey* inECKEY)
88 {
89         ByteString inEC = BotanUtil::ecGroup2ByteString(inECKEY->domain());
90         setEC(inEC);
91         ByteString inD = BotanUtil::bigInt2ByteStringPrefix(inECKEY->private_value(), 32);
92         setD(inD);
93 }
94
95 // Check if the key is of the given type
96 bool BotanGOSTPrivateKey::isOfType(const char* inType)
97 {
98         return !strcmp(type, inType);
99 }
100
101 // Setters for the GOST private key components
102 void BotanGOSTPrivateKey::setD(const ByteString& inD)
103 {
104         GOSTPrivateKey::setD(inD);
105
106         if (eckey)
107         {
108                 delete eckey;
109                 eckey = NULL;
110         }
111 }
112
113
114 // Setters for the GOST public key components
115 void BotanGOSTPrivateKey::setEC(const ByteString& inEC)
116 {
117         GOSTPrivateKey::setEC(inEC);
118
119         if (eckey)
120         {
121                 delete eckey;
122                 eckey = NULL;
123         }
124 }
125
126 // Serialisation
127 ByteString BotanGOSTPrivateKey::serialise() const
128 {
129         return ec.serialise() +
130                d.serialise();
131 }
132
133 bool BotanGOSTPrivateKey::deserialise(ByteString& serialised)
134 {
135         ByteString dEC = ByteString::chainDeserialise(serialised);
136         ByteString dD = ByteString::chainDeserialise(serialised);
137
138         if ((dEC.size() == 0) ||
139             (dD.size() == 0))
140         {
141                 return false;
142         }
143
144         setEC(dEC);
145         setD(dD);
146
147         return true;
148 }
149
150 // Encode into PKCS#8 DER
151 ByteString BotanGOSTPrivateKey::PKCS8Encode()
152 {
153         ByteString der;
154         // TODO
155         return der;
156 }
157
158 // Decode from PKCS#8 BER
159 bool BotanGOSTPrivateKey::PKCS8Decode(const ByteString& /*ber*/)
160 {
161         return false;
162 }
163
164 // Retrieve the Botan representation of the key
165 Botan::GOST_3410_PrivateKey* BotanGOSTPrivateKey::getBotanKey()
166 {
167         if (!eckey)
168         {
169                 createBotanKey();
170         }
171
172         return eckey;
173 }
174
175 // Create the Botan representation of the key
176 void BotanGOSTPrivateKey::createBotanKey()
177 {
178         if (ec.size() != 0 &&
179             d.size() != 0)
180         {
181                 if (eckey)
182                 {
183                         delete eckey;
184                         eckey = NULL;
185                 }
186
187                 try
188                 {
189                         BotanRNG* rng = (BotanRNG*)BotanCryptoFactory::i()->getRNG();
190                         Botan::EC_Group group = BotanUtil::byteString2ECGroup(ec);
191                         eckey = new Botan::GOST_3410_PrivateKey(*rng->getRNG(),
192                                                         group,
193                                                         BotanUtil::byteString2bigInt(d));
194                 }
195                 catch (...)
196                 {
197                         ERROR_MSG("Could not create the Botan public key");
198                 }
199         }
200 }
201 #endif