Debian Codegraph @

src/lib/crypto/BotanDHPublicKey.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 BotanDHPublicKey.cpp
29
30 Botan Diffie-Hellman public key class
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "log.h"
35 #include "BotanDHPublicKey.h"
36 #include "BotanUtil.h"
37 #include <string.h>
38
39 // Constructors
40 BotanDHPublicKey::BotanDHPublicKey()
41 {
42 dh = NULL;
43 }
44
45 BotanDHPublicKey::BotanDHPublicKey(const Botan::DH_PublicKey* inDH)
46 {
47 dh = NULL;
48
49 setFromBotan(inDH);
50 }
51
52 // Destructor
53 BotanDHPublicKey::~BotanDHPublicKey()
54 {
55 delete dh;
56 }
57
58 // The type
59 /*static*/ const char* BotanDHPublicKey::type = "Botan DH Public Key";
60
61 // Set from Botan representation
62 void BotanDHPublicKey::setFromBotan(const Botan::DH_PublicKey* inDH)
63 {
64 ByteString inP = BotanUtil::bigInt2ByteString(inDH->group_p());
65 setP(inP);
66 ByteString inG = BotanUtil::bigInt2ByteString(inDH->group_g());
67 setG(inG);
68 ByteString inY = BotanUtil::bigInt2ByteString(inDH->get_y());
69 setY(inY);
70 }
71
72 // Check if the key is of the given type
73 bool BotanDHPublicKey::isOfType(const char* inType)
74 {
75 return !strcmp(type, inType);
76 }
77
78 // Setters for the DH public key components
79 void BotanDHPublicKey::setP(const ByteString& inP)
80 {
81 DHPublicKey::setP(inP);
82
83 if (dh)
84 {
85 delete dh;
86 dh = NULL;
87 }
88 }
89
90 void BotanDHPublicKey::setG(const ByteString& inG)
91 {
92 DHPublicKey::setG(inG);
93
94 if (dh)
95 {
96 delete dh;
97 dh = NULL;
98 }
99 }
100
101 void BotanDHPublicKey::setY(const ByteString& inY)
102 {
103 DHPublicKey::setY(inY);
104
105 if (dh)
106 {
107 delete dh;
108 dh = NULL;
109 }
110 }
111
112 // Retrieve the Botan representation of the key
113 Botan::DH_PublicKey* BotanDHPublicKey::getBotanKey()
114 {
115 if (!dh)
116 {
117 createBotanKey();
118 }
119
120 return dh;
121 }
122
123 // Create the Botan representation of the key
124 void BotanDHPublicKey::createBotanKey()
125 {
126 // We actually do not need to check q, since it can be set zero
127 if (p.size() != 0 && y.size() != 0)
128 {
129 if (dh)
130 {
131 delete dh;
132 dh = NULL;
133 }
134
135 try
136 {
137 dh = new Botan::DH_PublicKey(Botan::DL_Group(BotanUtil::byteString2bigInt(p),
138 BotanUtil::byteString2bigInt(g)),
139 BotanUtil::byteString2bigInt(y));
140 }
141 catch (...)
142 {
143 ERROR_MSG("Could not create the Botan public key");
144 }
145 }
146 }