Debian Codegraph @

src/lib/crypto/BotanECDHPublicKey.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 BotanECDHPublicKey.cpp
29
30 Botan ECDH public key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #ifdef WITH_ECC
35 #include "log.h"
36 #include "BotanECDHPublicKey.h"
37 #include "BotanUtil.h"
38 #include <string.h>
39
40 // Constructors
41 BotanECDHPublicKey::BotanECDHPublicKey()
42 {
43 eckey = NULL;
44 }
45
46 BotanECDHPublicKey::BotanECDHPublicKey(const Botan::ECDH_PublicKey* inECKEY)
47 {
48 eckey = NULL;
49
50 setFromBotan(inECKEY);
51 }
52
53 // Destructor
54 BotanECDHPublicKey::~BotanECDHPublicKey()
55 {
56 delete eckey;
57 }
58
59 // The type
60 /*static*/ const char* BotanECDHPublicKey::type = "Botan ECDH Public Key";
61
62 // Get the base point order length
63 unsigned long BotanECDHPublicKey::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 // Set from Botan representation
79 void BotanECDHPublicKey::setFromBotan(const Botan::ECDH_PublicKey* inECKEY)
80 {
81 ByteString inEC = BotanUtil::ecGroup2ByteString(inECKEY->domain());
82 setEC(inEC);
83 ByteString inQ = BotanUtil::ecPoint2ByteString(inECKEY->public_point());
84 setQ(inQ);
85 }
86
87 // Check if the key is of the given type
88 bool BotanECDHPublicKey::isOfType(const char* inType)
89 {
90 return !strcmp(type, inType);
91 }
92
93 // Setters for the ECDH public key components
94 void BotanECDHPublicKey::setEC(const ByteString& inEC)
95 {
96 ECPublicKey::setEC(inEC);
97
98 if (eckey)
99 {
100 delete eckey;
101 eckey = NULL;
102 }
103 }
104
105 void BotanECDHPublicKey::setQ(const ByteString& inQ)
106 {
107 ECPublicKey::setQ(inQ);
108
109 if (eckey)
110 {
111 delete eckey;
112 eckey = NULL;
113 }
114 }
115
116 // Retrieve the Botan representation of the key
117 Botan::ECDH_PublicKey* BotanECDHPublicKey::getBotanKey()
118 {
119 if (!eckey)
120 {
121 createBotanKey();
122 }
123
124 return eckey;
125 }
126
127 // Create the Botan representation of the key
128 void BotanECDHPublicKey::createBotanKey()
129 {
130 if (ec.size() != 0 &&
131 q.size() != 0)
132 {
133 if (eckey)
134 {
135 delete eckey;
136 eckey = NULL;
137 }
138
139 try
140 {
141 Botan::EC_Group group = BotanUtil::byteString2ECGroup(ec);
142 Botan::PointGFp point = BotanUtil::byteString2ECPoint(q, group);
143 eckey = new Botan::ECDH_PublicKey(group, point);
144 }
145 catch (...)
146 {
147 ERROR_MSG("Could not create the Botan public key");
148 }
149 }
150 }
151 #endif