| 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 |
BotanDES.cpp |
| 29 |
|
| 30 |
Botan (3)DES implementation |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#include "BotanDES.h" |
| 35 |
#include <algorithm> |
| 36 |
#include "odd.h" |
| 37 |
|
| 38 |
bool BotanDES::wrapKey(const SymmetricKey* /*key*/, const SymWrap::Type /*mode*/, const ByteString& /*in*/, ByteString& /*out*/) |
| 39 |
{ |
| 40 |
ERROR_MSG("DES does not support key wrapping"); |
| 41 |
|
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
bool BotanDES::unwrapKey(const SymmetricKey* /*key*/, const SymWrap::Type /*mode*/, const ByteString& /*in*/, ByteString& /*out*/) |
| 46 |
{ |
| 47 |
ERROR_MSG("DES does not support key unwrapping"); |
| 48 |
|
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
std::string BotanDES::getCipher() const |
| 53 |
{ |
| 54 |
std::string algo; |
| 55 |
std::string mode; |
| 56 |
std::string padding; |
| 57 |
|
| 58 |
if (currentKey == NULL) return ""; |
| 59 |
|
| 60 |
// Check currentKey bit length; 3DES only supports 56-bit, 112-bit or 168-bit keys |
| 61 |
switch (currentKey->getBitLen()) |
| 62 |
{ |
| 63 |
case 56: |
| 64 |
// People shouldn't really be using 56-bit DES keys, generate a warning |
| 65 |
DEBUG_MSG("CAUTION: use of 56-bit DES keys is not recommended!"); |
| 66 |
algo = "DES"; |
| 67 |
break; |
| 68 |
case 112: |
| 69 |
case 168: |
| 70 |
algo = "TripleDES"; |
| 71 |
break; |
| 72 |
default: |
| 73 |
ERROR_MSG("Invalid DES currentKey length (%d bits)", currentKey->getBitLen()); |
| 74 |
|
| 75 |
return ""; |
| 76 |
} |
| 77 |
|
| 78 |
// Determine the cipher mode |
| 79 |
switch (currentCipherMode) |
| 80 |
{ |
| 81 |
case SymMode::CBC: |
| 82 |
mode = "CBC"; |
| 83 |
break; |
| 84 |
case SymMode::CFB: |
| 85 |
mode = "CFB"; |
| 86 |
break; |
| 87 |
case SymMode::ECB: |
| 88 |
mode = "ECB"; |
| 89 |
break; |
| 90 |
case SymMode::OFB: |
| 91 |
mode = "OFB"; |
| 92 |
break; |
| 93 |
default: |
| 94 |
ERROR_MSG("Invalid DES cipher mode %i", currentCipherMode); |
| 95 |
|
| 96 |
return ""; |
| 97 |
} |
| 98 |
|
| 99 |
// Check padding mode |
| 100 |
if (currentCipherMode == SymMode::OFB || |
| 101 |
currentCipherMode == SymMode::CFB) |
| 102 |
{ |
| 103 |
padding = ""; |
| 104 |
} |
| 105 |
else if (currentPaddingMode) |
| 106 |
{ |
| 107 |
padding = "/PKCS7"; |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
padding = "/NoPadding"; |
| 112 |
} |
| 113 |
|
| 114 |
return algo + "/" + mode + padding; |
| 115 |
} |
| 116 |
|
| 117 |
bool BotanDES::generateKey(SymmetricKey& key, RNG* rng /* = NULL */) |
| 118 |
{ |
| 119 |
if (rng == NULL) |
| 120 |
{ |
| 121 |
return false; |
| 122 |
} |
| 123 |
|
| 124 |
if (key.getBitLen() == 0) |
| 125 |
{ |
| 126 |
return false; |
| 127 |
} |
| 128 |
|
| 129 |
ByteString keyBits; |
| 130 |
|
| 131 |
// don't count parity bit |
| 132 |
if (!rng->generateRandom(keyBits, key.getBitLen()/7)) |
| 133 |
{ |
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
// fix the odd parity |
| 138 |
size_t i; |
| 139 |
for (i = 0; i < keyBits.size(); i++) |
| 140 |
{ |
| 141 |
keyBits[i] = odd_parity[keyBits[i]]; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
return key.setKeyBits(keyBits); |
| 146 |
} |
| 147 |
|
| 148 |
size_t BotanDES::getBlockSize() const |
| 149 |
{ |
| 150 |
// The block size is 64 bits |
| 151 |
return 64 >> 3; |
| 152 |
} |
| 153 |
|