2 * Copyright (c) 2010 .SE (The Internet Infrastructure Foundation)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 /*****************************************************************************
28 BotanGOSTPrivateKey.cpp
30 Botan GOST R 34.10-2001 private key class
31 *****************************************************************************/
36 #include "BotanGOSTPrivateKey.h"
37 #include "BotanCryptoFactory.h"
39 #include "BotanUtil.h"
43 BotanGOSTPrivateKey::BotanGOSTPrivateKey()
48 BotanGOSTPrivateKey::BotanGOSTPrivateKey(const Botan::GOST_3410_PrivateKey* inECKEY)
50 BotanGOSTPrivateKey();
52 setFromBotan(inECKEY);
56 BotanGOSTPrivateKey::~BotanGOSTPrivateKey()
62 /*static*/ const char* BotanGOSTPrivateKey::type = "Botan GOST Private Key";
64 // Get the base point order length
65 unsigned long BotanGOSTPrivateKey::getOrderLength() const
69 Botan::EC_Group group = BotanUtil::byteString2ECGroup(ec);
70 return group.get_order().bytes();
74 ERROR_MSG("Can't get EC group for order length");
80 // Get the output length
81 unsigned long BotanGOSTPrivateKey::getOutputLength() const
83 return getOrderLength() * 2;
86 // Set from Botan representation
87 void BotanGOSTPrivateKey::setFromBotan(const Botan::GOST_3410_PrivateKey* inECKEY)
89 ByteString inEC = BotanUtil::ecGroup2ByteString(inECKEY->domain());
91 ByteString inD = BotanUtil::bigInt2ByteStringPrefix(inECKEY->private_value(), 32);
95 // Check if the key is of the given type
96 bool BotanGOSTPrivateKey::isOfType(const char* inType)
98 return !strcmp(type, inType);
101 // Setters for the GOST private key components
102 void BotanGOSTPrivateKey::setD(const ByteString& inD)
104 GOSTPrivateKey::setD(inD);
114 // Setters for the GOST public key components
115 void BotanGOSTPrivateKey::setEC(const ByteString& inEC)
117 GOSTPrivateKey::setEC(inEC);
127 ByteString BotanGOSTPrivateKey::serialise() const
129 return ec.serialise() +
133 bool BotanGOSTPrivateKey::deserialise(ByteString& serialised)
135 ByteString dEC = ByteString::chainDeserialise(serialised);
136 ByteString dD = ByteString::chainDeserialise(serialised);
138 if ((dEC.size() == 0) ||
150 // Encode into PKCS#8 DER
151 ByteString BotanGOSTPrivateKey::PKCS8Encode()
158 // Decode from PKCS#8 BER
159 bool BotanGOSTPrivateKey::PKCS8Decode(const ByteString& /*ber*/)
164 // Retrieve the Botan representation of the key
165 Botan::GOST_3410_PrivateKey* BotanGOSTPrivateKey::getBotanKey()
175 // Create the Botan representation of the key
176 void BotanGOSTPrivateKey::createBotanKey()
178 if (ec.size() != 0 &&
189 BotanRNG* rng = (BotanRNG*)BotanCryptoFactory::i()->getRNG();
190 Botan::EC_Group group = BotanUtil::byteString2ECGroup(ec);
191 eckey = new Botan::GOST_3410_PrivateKey(*rng->getRNG(),
193 BotanUtil::byteString2bigInt(d));
197 ERROR_MSG("Could not create the Botan public key");