Debian Codegraph @

src/lib/crypto/OSSLEVPCMacAlgorithm.cpp

raw
1 /*
2 * Copyright (c) 2017 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 // TODO: Store context in securely allocated memory
28
29 /*****************************************************************************
30 OSSLEVPCMacAlgorithm.cpp
31
32 OpenSSL CMAC algorithm implementation
33 *****************************************************************************/
34
35 #include "config.h"
36 #include "OSSLEVPCMacAlgorithm.h"
37 #include "OSSLComp.h"
38 #include <openssl/err.h>
39
40 // Destructor
41 OSSLEVPCMacAlgorithm::~OSSLEVPCMacAlgorithm()
42 {
43 if (curCTX != NULL)
44 CMAC_CTX_free(curCTX);
45 }
46
47 // Signing functions
48 bool OSSLEVPCMacAlgorithm::signInit(const SymmetricKey* key)
49 {
50 // Call the superclass initialiser
51 if (!MacAlgorithm::signInit(key))
52 {
53 return false;
54 }
55
56 // Determine the cipher class
57 const EVP_CIPHER* cipher = getEVPCipher();
58 if (cipher == NULL)
59 {
60 ERROR_MSG("Invalid sign mac algorithm");
61
62 ByteString dummy;
63 MacAlgorithm::signFinal(dummy);
64
65 return false;
66 }
67
68 // Initialize the context
69 curCTX = CMAC_CTX_new();
70 if (curCTX == NULL)
71 {
72 ERROR_MSG("Failed to allocate space for CMAC_CTX");
73
74 return false;
75 }
76
77 // Initialize EVP signing
78 if (!CMAC_Init(curCTX, key->getKeyBits().const_byte_str(), key->getKeyBits().size(), cipher, NULL))
79 {
80 ERROR_MSG("CMAC_Init failed: %s", ERR_error_string(ERR_get_error(), NULL));
81
82 CMAC_CTX_free(curCTX);
83 curCTX = NULL;
84
85 ByteString dummy;
86 MacAlgorithm::signFinal(dummy);
87
88 return false;
89 }
90
91 return true;
92 }
93
94 bool OSSLEVPCMacAlgorithm::signUpdate(const ByteString& dataToSign)
95 {
96 if (!MacAlgorithm::signUpdate(dataToSign))
97 {
98 return false;
99 }
100
101 if (dataToSign.size() == 0) return true;
102
103 if (!CMAC_Update(curCTX, dataToSign.const_byte_str(), dataToSign.size()))
104 {
105 ERROR_MSG("CMAC_Update failed");
106
107 CMAC_CTX_free(curCTX);
108 curCTX = NULL;
109
110 ByteString dummy;
111 MacAlgorithm::signFinal(dummy);
112
113 return false;
114 }
115
116 return true;
117 }
118
119 bool OSSLEVPCMacAlgorithm::signFinal(ByteString& signature)
120 {
121 if (!MacAlgorithm::signFinal(signature))
122 {
123 return false;
124 }
125
126 size_t outLen = getMacSize();
127 signature.resize(outLen);
128
129 if (!CMAC_Final(curCTX, &signature[0], &outLen))
130 {
131 ERROR_MSG("CMAC_Final failed");
132
133 CMAC_CTX_free(curCTX);
134 curCTX = NULL;
135
136 return false;
137 }
138
139 signature.resize(outLen);
140
141 CMAC_CTX_free(curCTX);
142 curCTX = NULL;
143
144 return true;
145 }
146
147 // Verification functions
148 bool OSSLEVPCMacAlgorithm::verifyInit(const SymmetricKey* key)
149 {
150 // Call the superclass initialiser
151 if (!MacAlgorithm::verifyInit(key))
152 {
153 return false;
154 }
155
156 // Determine the cipher class
157 const EVP_CIPHER* cipher = getEVPCipher();
158 if (cipher == NULL)
159 {
160 ERROR_MSG("Invalid verify mac algorithm");
161
162 ByteString dummy;
163 MacAlgorithm::signFinal(dummy);
164
165 return false;
166 }
167
168 // Initialize the context
169 curCTX = CMAC_CTX_new();
170 if (curCTX == NULL)
171 {
172 ERROR_MSG("Failed to allocate space for CMAC_CTX");
173
174 return false;
175 }
176
177 // Initialize EVP signing
178 if (!CMAC_Init(curCTX, key->getKeyBits().const_byte_str(), key->getKeyBits().size(), cipher, NULL))
179 {
180 ERROR_MSG("CMAC_Init failed: %s", ERR_error_string(ERR_get_error(), NULL));
181
182 CMAC_CTX_free(curCTX);
183 curCTX = NULL;
184
185 ByteString dummy;
186 MacAlgorithm::verifyFinal(dummy);
187
188 return false;
189 }
190
191 return true;
192 }
193
194 bool OSSLEVPCMacAlgorithm::verifyUpdate(const ByteString& originalData)
195 {
196 if (!MacAlgorithm::verifyUpdate(originalData))
197 {
198 return false;
199 }
200
201 if (originalData.size() == 0) return true;
202
203 if (!CMAC_Update(curCTX, originalData.const_byte_str(), originalData.size()))
204 {
205 ERROR_MSG("CMAC_Update failed");
206
207 CMAC_CTX_free(curCTX);
208 curCTX = NULL;
209
210 ByteString dummy;
211 MacAlgorithm::verifyFinal(dummy);
212
213 return false;
214 }
215
216 return true;
217 }
218
219 bool OSSLEVPCMacAlgorithm::verifyFinal(ByteString& signature)
220 {
221 if (!MacAlgorithm::verifyFinal(signature))
222 {
223 return false;
224 }
225
226 ByteString macResult;
227 size_t outLen = getMacSize();
228 macResult.resize(outLen);
229
230 if (!CMAC_Final(curCTX, &macResult[0], &outLen))
231 {
232 ERROR_MSG("CMAC_Final failed");
233
234 CMAC_CTX_free(curCTX);
235 curCTX = NULL;
236
237 return false;
238 }
239
240 CMAC_CTX_free(curCTX);
241 curCTX = NULL;
242
243 return macResult == signature;
244 }