Debian Codegraph @

src/lib/crypto/OSSLDES.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 OSSLDES.cpp
29
30 OpenSSL (3)DES implementation
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "OSSLDES.h"
35 #include <algorithm>
36 #include "odd.h"
37
38 bool OSSLDES::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 OSSLDES::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 const EVP_CIPHER* OSSLDES::getCipher() const
53 {
54 if (currentKey == NULL) return NULL;
55
56 // Check currentKey bit length; 3DES only supports 56-bit, 112-bit or 168-bit keys
57 if (
58 #ifndef WITH_FIPS
59 (currentKey->getBitLen() != 56) &&
60 #endif
61 (currentKey->getBitLen() != 112) &&
62 (currentKey->getBitLen() != 168))
63 {
64 ERROR_MSG("Invalid DES currentKey length (%d bits)", currentKey->getBitLen());
65
66 return NULL;
67 }
68
69 // People shouldn't really be using 56-bit DES keys, generate a warning
70 if (currentKey->getBitLen() == 56)
71 {
72 DEBUG_MSG("CAUTION: use of 56-bit DES keys is not recommended!");
73 }
74
75 // Determine the cipher mode
76 if (currentCipherMode == SymMode::CBC)
77 {
78 switch(currentKey->getBitLen())
79 {
80 case 56:
81 return EVP_des_cbc();
82 case 112:
83 return EVP_des_ede_cbc();
84 case 168:
85 return EVP_des_ede3_cbc();
86 };
87 }
88 else if (currentCipherMode == SymMode::ECB)
89 {
90 switch(currentKey->getBitLen())
91 {
92 case 56:
93 return EVP_des_ecb();
94 case 112:
95 return EVP_des_ede_ecb();
96 case 168:
97 return EVP_des_ede3_ecb();
98 };
99 }
100 else if (currentCipherMode == SymMode::OFB)
101 {
102 switch(currentKey->getBitLen())
103 {
104 case 56:
105 return EVP_des_ofb();
106 case 112:
107 return EVP_des_ede_ofb();
108 case 168:
109 return EVP_des_ede3_ofb();
110 };
111 }
112 else if (currentCipherMode == SymMode::CFB)
113 {
114 switch(currentKey->getBitLen())
115 {
116 case 56:
117 return EVP_des_cfb();
118 case 112:
119 return EVP_des_ede_cfb();
120 case 168:
121 return EVP_des_ede3_cfb();
122 };
123 }
124
125 ERROR_MSG("Invalid DES cipher mode %i", currentCipherMode);
126
127 return NULL;
128 }
129
130 bool OSSLDES::generateKey(SymmetricKey& key, RNG* rng /* = NULL */)
131 {
132 if (rng == NULL)
133 {
134 return false;
135 }
136
137 if (key.getBitLen() == 0)
138 {
139 return false;
140 }
141
142 ByteString keyBits;
143
144 // don't count parity bit
145 if (!rng->generateRandom(keyBits, key.getBitLen()/7))
146 {
147 return false;
148 }
149
150 // fix the odd parity
151 size_t i;
152 for (i = 0; i < keyBits.size(); i++)
153 {
154 keyBits[i] = odd_parity[keyBits[i]];
155 }
156
157 return key.setKeyBits(keyBits);
158 }
159
160 size_t OSSLDES::getBlockSize() const
161 {
162 // The block size is 64 bits
163 return 64 >> 3;
164 }
165