Debian Codegraph @

src/lib/crypto/OSSLEVPMacAlgorithm.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 // TODO: Store context in securely allocated memory
28
29 /*****************************************************************************
30 OSSLEVPMacAlgorithm.cpp
31
32 OpenSSL MAC algorithm implementation
33 *****************************************************************************/
34
35 #include "config.h"
36 #include "OSSLEVPMacAlgorithm.h"
37 #include "OSSLComp.h"
38
39 // Destructor
40 OSSLEVPMacAlgorithm::~OSSLEVPMacAlgorithm()
41 {
42 HMAC_CTX_free(curCTX);
43 }
44
45 // Signing functions
46 bool OSSLEVPMacAlgorithm::signInit(const SymmetricKey* key)
47 {
48 // Call the superclass initialiser
49 if (!MacAlgorithm::signInit(key))
50 {
51 return false;
52 }
53
54 // Initialize the context
55 curCTX = HMAC_CTX_new();
56 if (curCTX == NULL)
57 {
58 ERROR_MSG("Failed to allocate space for HMAC_CTX");
59
60 return false;
61 }
62
63 // Initialize EVP signing
64 if (!HMAC_Init_ex(curCTX, key->getKeyBits().const_byte_str(), key->getKeyBits().size(), getEVPHash(), NULL))
65 {
66 ERROR_MSG("HMAC_Init failed");
67
68 HMAC_CTX_free(curCTX);
69 curCTX = NULL;
70
71 ByteString dummy;
72 MacAlgorithm::signFinal(dummy);
73
74 return false;
75 }
76
77 return true;
78 }
79
80 bool OSSLEVPMacAlgorithm::signUpdate(const ByteString& dataToSign)
81 {
82 if (!MacAlgorithm::signUpdate(dataToSign))
83 {
84 return false;
85 }
86
87 // The GOST implementation in OpenSSL will segfault if we update with zero length.
88 if (dataToSign.size() == 0) return true;
89
90 if (!HMAC_Update(curCTX, dataToSign.const_byte_str(), dataToSign.size()))
91 {
92 ERROR_MSG("HMAC_Update failed");
93
94 HMAC_CTX_free(curCTX);
95 curCTX = NULL;
96
97 ByteString dummy;
98 MacAlgorithm::signFinal(dummy);
99
100 return false;
101 }
102
103 return true;
104 }
105
106 bool OSSLEVPMacAlgorithm::signFinal(ByteString& signature)
107 {
108 if (!MacAlgorithm::signFinal(signature))
109 {
110 return false;
111 }
112
113 signature.resize(EVP_MD_size(getEVPHash()));
114 unsigned int outLen = signature.size();
115
116 if (!HMAC_Final(curCTX, &signature[0], &outLen))
117 {
118 ERROR_MSG("HMAC_Final failed");
119
120 HMAC_CTX_free(curCTX);
121 curCTX = NULL;
122
123 return false;
124 }
125
126 signature.resize(outLen);
127
128 HMAC_CTX_free(curCTX);
129 curCTX = NULL;
130
131 return true;
132 }
133
134 // Verification functions
135 bool OSSLEVPMacAlgorithm::verifyInit(const SymmetricKey* key)
136 {
137 // Call the superclass initialiser
138 if (!MacAlgorithm::verifyInit(key))
139 {
140 return false;
141 }
142
143 // Initialize the context
144 curCTX = HMAC_CTX_new();
145 if (curCTX == NULL)
146 {
147 ERROR_MSG("Failed to allocate space for HMAC_CTX");
148
149 return false;
150 }
151
152 // Initialize EVP signing
153 if (!HMAC_Init_ex(curCTX, key->getKeyBits().const_byte_str(), key->getKeyBits().size(), getEVPHash(), NULL))
154 {
155 ERROR_MSG("HMAC_Init failed");
156
157 HMAC_CTX_free(curCTX);
158 curCTX = NULL;
159
160 ByteString dummy;
161 MacAlgorithm::verifyFinal(dummy);
162
163 return false;
164 }
165
166 return true;
167 }
168
169 bool OSSLEVPMacAlgorithm::verifyUpdate(const ByteString& originalData)
170 {
171 if (!MacAlgorithm::verifyUpdate(originalData))
172 {
173 return false;
174 }
175
176 // The GOST implementation in OpenSSL will segfault if we update with zero length.
177 if (originalData.size() == 0) return true;
178
179 if (!HMAC_Update(curCTX, originalData.const_byte_str(), originalData.size()))
180 {
181 ERROR_MSG("HMAC_Update failed");
182
183 HMAC_CTX_free(curCTX);
184 curCTX = NULL;
185
186 ByteString dummy;
187 MacAlgorithm::verifyFinal(dummy);
188
189 return false;
190 }
191
192 return true;
193 }
194
195 bool OSSLEVPMacAlgorithm::verifyFinal(ByteString& signature)
196 {
197 if (!MacAlgorithm::verifyFinal(signature))
198 {
199 return false;
200 }
201
202 ByteString macResult;
203 unsigned int outLen = EVP_MD_size(getEVPHash());
204 macResult.resize(outLen);
205
206 if (!HMAC_Final(curCTX, &macResult[0], &outLen))
207 {
208 ERROR_MSG("HMAC_Final failed");
209
210 HMAC_CTX_free(curCTX);
211 curCTX = NULL;
212
213 return false;
214 }
215
216 HMAC_CTX_free(curCTX);
217 curCTX = NULL;
218
219 return macResult == signature;
220 }