| 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 |
BotanMacAlgorithm.cpp |
| 31 |
|
| 32 |
Botan MAC algorithm implementation |
| 33 |
*****************************************************************************/ |
| 34 |
|
| 35 |
#include "config.h" |
| 36 |
#include "BotanMacAlgorithm.h" |
| 37 |
#include "salloc.h" |
| 38 |
|
| 39 |
#include <botan/symkey.h> |
| 40 |
#include <botan/mac.h> |
| 41 |
#include <botan/version.h> |
| 42 |
|
| 43 |
// Constructor |
| 44 |
BotanMacAlgorithm::BotanMacAlgorithm() |
| 45 |
{ |
| 46 |
mac = NULL; |
| 47 |
} |
| 48 |
|
| 49 |
// Destructor |
| 50 |
BotanMacAlgorithm::~BotanMacAlgorithm() |
| 51 |
{ |
| 52 |
delete mac; |
| 53 |
mac = NULL; |
| 54 |
} |
| 55 |
|
| 56 |
// Signing functions |
| 57 |
bool BotanMacAlgorithm::signInit(const SymmetricKey* key) |
| 58 |
{ |
| 59 |
// Call the superclass initialiser |
| 60 |
if (!MacAlgorithm::signInit(key)) |
| 61 |
{ |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
// Determine the hash name |
| 66 |
std::string macName = getAlgorithm(); |
| 67 |
|
| 68 |
if (macName == "") |
| 69 |
{ |
| 70 |
ERROR_MSG("Invalid sign mac algorithm"); |
| 71 |
|
| 72 |
ByteString dummy; |
| 73 |
MacAlgorithm::signFinal(dummy); |
| 74 |
|
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
// Allocate the context |
| 79 |
try |
| 80 |
{ |
| 81 |
mac = Botan::MessageAuthenticationCode::create_or_throw(macName).release(); |
| 82 |
mac->set_key(key->getKeyBits().const_byte_str(), key->getKeyBits().size()); |
| 83 |
} |
| 84 |
catch (std::exception &e) |
| 85 |
{ |
| 86 |
ERROR_MSG("Failed to create the sign mac token: %s", e.what()); |
| 87 |
|
| 88 |
ByteString dummy; |
| 89 |
MacAlgorithm::signFinal(dummy); |
| 90 |
|
| 91 |
delete mac; |
| 92 |
mac = NULL; |
| 93 |
|
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
bool BotanMacAlgorithm::signUpdate(const ByteString& dataToSign) |
| 101 |
{ |
| 102 |
if (!MacAlgorithm::signUpdate(dataToSign)) |
| 103 |
{ |
| 104 |
delete mac; |
| 105 |
mac = NULL; |
| 106 |
|
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
try |
| 111 |
{ |
| 112 |
if (dataToSign.size() != 0) |
| 113 |
{ |
| 114 |
mac->update(dataToSign.const_byte_str(), |
| 115 |
dataToSign.size()); |
| 116 |
} |
| 117 |
} |
| 118 |
catch (...) |
| 119 |
{ |
| 120 |
ERROR_MSG("Failed to update the sign mac token"); |
| 121 |
|
| 122 |
ByteString dummy; |
| 123 |
MacAlgorithm::signFinal(dummy); |
| 124 |
|
| 125 |
delete mac; |
| 126 |
mac = NULL; |
| 127 |
|
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
bool BotanMacAlgorithm::signFinal(ByteString& signature) |
| 135 |
{ |
| 136 |
if (!MacAlgorithm::signFinal(signature)) |
| 137 |
{ |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
// Perform the signature operation |
| 142 |
Botan::secure_vector<uint8_t> signResult; |
| 143 |
try |
| 144 |
{ |
| 145 |
signResult = mac->final(); |
| 146 |
} |
| 147 |
catch (...) |
| 148 |
{ |
| 149 |
ERROR_MSG("Could not sign the data"); |
| 150 |
|
| 151 |
delete mac; |
| 152 |
mac = NULL; |
| 153 |
|
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
// Return the result |
| 158 |
signature.resize(signResult.size()); |
| 159 |
memcpy(&signature[0], signResult.data(), signResult.size()); |
| 160 |
|
| 161 |
delete mac; |
| 162 |
mac = NULL; |
| 163 |
|
| 164 |
return true; |
| 165 |
} |
| 166 |
|
| 167 |
// Verification functions |
| 168 |
bool BotanMacAlgorithm::verifyInit(const SymmetricKey* key) |
| 169 |
{ |
| 170 |
// Call the superclass initialiser |
| 171 |
if (!MacAlgorithm::verifyInit(key)) |
| 172 |
{ |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
// Determine the hash name |
| 177 |
std::string macName = getAlgorithm(); |
| 178 |
|
| 179 |
if (macName == "") |
| 180 |
{ |
| 181 |
ERROR_MSG("Invalid verify mac algorithm"); |
| 182 |
|
| 183 |
ByteString dummy; |
| 184 |
MacAlgorithm::verifyFinal(dummy); |
| 185 |
|
| 186 |
return false; |
| 187 |
} |
| 188 |
|
| 189 |
// Allocate the context |
| 190 |
try |
| 191 |
{ |
| 192 |
mac = Botan::MessageAuthenticationCode::create_or_throw(macName).release(); |
| 193 |
mac->set_key(key->getKeyBits().const_byte_str(), key->getKeyBits().size()); |
| 194 |
} |
| 195 |
catch (std::exception &e) |
| 196 |
{ |
| 197 |
ERROR_MSG("Failed to create the verify mac token: %s", e.what()); |
| 198 |
|
| 199 |
ByteString dummy; |
| 200 |
MacAlgorithm::verifyFinal(dummy); |
| 201 |
|
| 202 |
delete mac; |
| 203 |
mac = NULL; |
| 204 |
|
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
bool BotanMacAlgorithm::verifyUpdate(const ByteString& originalData) |
| 212 |
{ |
| 213 |
if (!MacAlgorithm::verifyUpdate(originalData)) |
| 214 |
{ |
| 215 |
delete mac; |
| 216 |
mac = NULL; |
| 217 |
|
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
try |
| 222 |
{ |
| 223 |
if (originalData.size() != 0) |
| 224 |
{ |
| 225 |
mac->update(originalData.const_byte_str(), |
| 226 |
originalData.size()); |
| 227 |
} |
| 228 |
} |
| 229 |
catch (...) |
| 230 |
{ |
| 231 |
ERROR_MSG("Failed to update the verify mac token"); |
| 232 |
|
| 233 |
ByteString dummy; |
| 234 |
MacAlgorithm::verifyFinal(dummy); |
| 235 |
|
| 236 |
delete mac; |
| 237 |
mac = NULL; |
| 238 |
|
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
return true; |
| 243 |
} |
| 244 |
|
| 245 |
bool BotanMacAlgorithm::verifyFinal(ByteString& signature) |
| 246 |
{ |
| 247 |
if (!MacAlgorithm::verifyFinal(signature)) |
| 248 |
{ |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
// Perform the verify operation |
| 253 |
Botan::secure_vector<uint8_t> macResult; |
| 254 |
try |
| 255 |
{ |
| 256 |
macResult = mac->final(); |
| 257 |
} |
| 258 |
catch (...) |
| 259 |
{ |
| 260 |
ERROR_MSG("Failed to verify the data"); |
| 261 |
|
| 262 |
delete mac; |
| 263 |
mac = NULL; |
| 264 |
|
| 265 |
return false; |
| 266 |
} |
| 267 |
|
| 268 |
if (macResult.size() != signature.size()) |
| 269 |
{ |
| 270 |
ERROR_MSG("Bad verify result size"); |
| 271 |
|
| 272 |
delete mac; |
| 273 |
mac = NULL; |
| 274 |
|
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
delete mac; |
| 279 |
mac = NULL; |
| 280 |
|
| 281 |
return Botan::same_mem(&signature[0], macResult.data(), macResult.size()); |
| 282 |
} |