Debian Codegraph @

src/lib/crypto/AsymmetricAlgorithm.cpp

raw
1 /*
2 * Copyright (c) 2010 SURFnet bv
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 AsymmetricAlgorithm.cpp
29
30 Base class for asymmetric algorithm classes
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "log.h"
35 #include "AsymmetricAlgorithm.h"
36
37 // Base constructor
38 AsymmetricAlgorithm::AsymmetricAlgorithm()
39 {
40 currentOperation = NONE;
41 currentMechanism = AsymMech::Unknown;
42 currentPadding = AsymMech::Unknown;
43 currentPublicKey = NULL;
44 currentPrivateKey = NULL;
45 }
46
47 // Signing functions
48 bool AsymmetricAlgorithm::sign(PrivateKey* privateKey, const ByteString& dataToSign,
49 ByteString& signature, const AsymMech::Type mechanism,
50 const void* param /* = NULL */, const size_t paramLen /* = 0 */)
51 {
52 // Compose from multi-part operations
53 return (signInit(privateKey, mechanism, param, paramLen) && signUpdate(dataToSign) && signFinal(signature));
54 }
55
56 bool AsymmetricAlgorithm::signInit(PrivateKey* privateKey, const AsymMech::Type mechanism,
57 const void* /* param = NULL */, const size_t /* paramLen = 0 */)
58 {
59 if ((currentOperation != NONE) || (privateKey == NULL))
60 {
61 return false;
62 }
63
64 currentPrivateKey = privateKey;
65 currentMechanism = mechanism;
66 currentOperation = SIGN;
67
68 return true;
69 }
70
71 bool AsymmetricAlgorithm::signUpdate(const ByteString& /*dataToSign*/)
72 {
73 if (currentOperation != SIGN)
74 {
75 return false;
76 }
77
78 return true;
79 }
80
81 bool AsymmetricAlgorithm::signFinal(ByteString& /*signature*/)
82 {
83 if (currentOperation != SIGN)
84 {
85 return false;
86 }
87
88 currentOperation = NONE;
89 currentPrivateKey = NULL;
90 currentMechanism = AsymMech::Unknown;
91
92 return true;
93 }
94
95 // Verification functions
96 bool AsymmetricAlgorithm::verify(PublicKey* publicKey, const ByteString& originalData,
97 const ByteString& signature, const AsymMech::Type mechanism,
98 const void* param /* = NULL */, const size_t paramLen /* = 0 */)
99 {
100 // Compose from multi-part operations
101 return (verifyInit(publicKey, mechanism, param, paramLen) && verifyUpdate(originalData) && verifyFinal(signature));
102 }
103
104 bool AsymmetricAlgorithm::verifyInit(PublicKey* publicKey, const AsymMech::Type mechanism,
105 const void* /* param = NULL */, const size_t /* paramLen = 0 */)
106 {
107 if ((currentOperation != NONE) || (publicKey == NULL))
108 {
109 return false;
110 }
111
112 currentOperation = VERIFY;
113 currentPublicKey = publicKey;
114 currentMechanism = mechanism;
115
116 return true;
117 }
118
119 bool AsymmetricAlgorithm::verifyUpdate(const ByteString& /*originalData*/)
120 {
121 if (currentOperation != VERIFY)
122 {
123 return false;
124 }
125
126 return true;
127 }
128
129 bool AsymmetricAlgorithm::verifyFinal(const ByteString& /*signature*/)
130 {
131 if (currentOperation != VERIFY)
132 {
133 return false;
134 }
135
136 currentOperation = NONE;
137 currentPublicKey = NULL;
138 currentMechanism = AsymMech::Unknown;
139
140 return true;
141 }
142
143 // Returns true for mechanisms which have 'tick mark' in Wrap&Unwrap column in PKCS #11 Mechanisms v2.40
144 bool AsymmetricAlgorithm::isWrappingMech(AsymMech::Type padding)
145 {
146 switch (padding)
147 {
148 case AsymMech::RSA:
149 case AsymMech::RSA_PKCS:
150 case AsymMech::RSA_PKCS_OAEP:
151 return true;
152
153 default:
154 return false;
155 }
156 }
157
158 // Wrap/Unwrap keys
159 bool AsymmetricAlgorithm::wrapKey(PublicKey* publicKey, const ByteString& data, ByteString& encryptedData, const AsymMech::Type padding)
160 {
161 if (!isWrappingMech(padding))
162 return false;
163
164 return encrypt(publicKey, data, encryptedData, padding);
165 }
166
167 bool AsymmetricAlgorithm::unwrapKey(PrivateKey* privateKey, const ByteString& encryptedData, ByteString& data, const AsymMech::Type padding)
168 {
169 if (!isWrappingMech(padding))
170 return false;
171
172 return decrypt(privateKey, encryptedData, data, padding);
173 }
174
175
176 bool AsymmetricAlgorithm::generateParameters(AsymmetricParameters** /*ppParams*/, void* /*parameters = NULL*/, RNG* /*rng = NULL*/)
177 {
178 return false;
179 }
180
181 bool AsymmetricAlgorithm::deriveKey(SymmetricKey** /*ppSymmetricKey*/, PublicKey* /*publicKey*/, PrivateKey* /*privateKey*/)
182 {
183 return false;
184 }
185
186 bool AsymmetricAlgorithm::reconstructParameters(AsymmetricParameters** /*ppParams*/, ByteString& /*serialisedData*/)
187 {
188 return false;
189 }
190
191 AsymmetricParameters* AsymmetricAlgorithm::newParameters()
192 {
193 return NULL;
194 }
195
196 // Key recycling -- override these functions in a derived class if you need to perform specific cleanup
197 void AsymmetricAlgorithm::recycleKeyPair(AsymmetricKeyPair* toRecycle)
198 {
199 delete toRecycle;
200 }
201
202 void AsymmetricAlgorithm::recycleParameters(AsymmetricParameters* toRecycle)
203 {
204 delete toRecycle;
205 }
206
207 void AsymmetricAlgorithm::recyclePublicKey(PublicKey* toRecycle)
208 {
209 delete toRecycle;
210 }
211
212 void AsymmetricAlgorithm::recyclePrivateKey(PrivateKey* toRecycle)
213 {
214 delete toRecycle;
215 }
216
217 void AsymmetricAlgorithm::recycleSymmetricKey(SymmetricKey* toRecycle)
218 {
219 delete toRecycle;
220 }
221