Debian Codegraph @

src/lib/crypto/OSSLAES.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 OSSLAES.cpp
29
30 OpenSSL AES implementation
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "OSSLAES.h"
35 #include <algorithm>
36 #include <openssl/aes.h>
37 #include "salloc.h"
38
39 // Wrap/Unwrap keys
40 #ifdef HAVE_AES_KEY_WRAP
41 bool OSSLAES::wrapKey(const SymmetricKey* key, const SymWrap::Type mode, const ByteString& in, ByteString& out)
42 {
43 // RFC 3394 input length checks do not apply to RFC 5649 mode with padding
44 if (mode == SymWrap::AES_KEYWRAP && !checkLength(in.size(), 16, "wrap"))
45 return false;
46
47 return wrapUnwrapKey(key, mode, in, out, 1);
48 }
49 #else
50 bool OSSLAES::wrapKey(const SymmetricKey* /*key*/, const SymWrap::Type /*mode*/, const ByteString& /*in*/, ByteString& /*out*/)
51 {
52 return false;
53 }
54 #endif
55
56 #ifdef HAVE_AES_KEY_WRAP
57 bool OSSLAES::unwrapKey(const SymmetricKey* key, const SymWrap::Type mode, const ByteString& in, ByteString& out)
58 {
59 // RFC 3394 algorithm produce at least 3 blocks of data
60 if ((mode == SymWrap::AES_KEYWRAP && !checkLength(in.size(), 24, "unwrap")) ||
61 // RFC 5649 algorithm produce at least 2 blocks of data
62 (mode == SymWrap::AES_KEYWRAP_PAD && !checkLength(in.size(), 16, "unwrap")))
63 return false;
64 return wrapUnwrapKey(key, mode, in, out, 0);
65 }
66 #else
67 bool OSSLAES::unwrapKey(const SymmetricKey* /*key*/, const SymWrap::Type /*mode*/, const ByteString& /*in*/, ByteString& /*out*/)
68 {
69 return false;
70 }
71 #endif
72
73 #ifdef HAVE_AES_KEY_WRAP
74 // RFC 3394 wrapping and all unwrapping algorithms require aligned blocks
75 bool OSSLAES::checkLength(const int insize, const int minsize, const char * const operation) const
76 {
77 if (insize < minsize)
78 {
79 ERROR_MSG("key data to %s too small", operation);
80 return false;
81 }
82 if ((insize % 8) != 0)
83 {
84 ERROR_MSG("key data to %s not aligned", operation);
85 return false;
86 }
87 return true;
88 }
89
90 const EVP_CIPHER* OSSLAES::getWrapCipher(const SymWrap::Type mode, const SymmetricKey* key) const
91 {
92 if (key == NULL)
93 return NULL;
94
95 // Check currentKey bit length; AES only supports 128, 192 or 256 bit keys
96 if ((key->getBitLen() != 128) &&
97 (key->getBitLen() != 192) &&
98 (key->getBitLen() != 256))
99 {
100 ERROR_MSG("Invalid AES key length (%d bits)", key->getBitLen());
101
102 return NULL;
103 }
104
105 #ifdef HAVE_AES_KEY_WRAP
106 // Determine the un/wrapping mode
107 if (mode == SymWrap::AES_KEYWRAP)
108 {
109 // RFC 3394 AES key wrap
110 switch(key->getBitLen())
111 {
112 case 128:
113 return EVP_aes_128_wrap();
114 case 192:
115 return EVP_aes_192_wrap();
116 case 256:
117 return EVP_aes_256_wrap();
118 };
119 }
120 #endif
121 #ifdef HAVE_AES_KEY_WRAP_PAD
122 if (mode == SymWrap::AES_KEYWRAP_PAD)
123 {
124 // RFC 5649 AES key wrap with pad
125 switch(key->getBitLen())
126 {
127 case 128:
128 return EVP_aes_128_wrap_pad();
129 case 192:
130 return EVP_aes_192_wrap_pad();
131 case 256:
132 return EVP_aes_256_wrap_pad();
133 };
134 }
135 #endif
136
137 ERROR_MSG("unknown AES key wrap mode %i", mode);
138 return NULL;
139 }
140
141 // EVP wrapping/unwrapping
142 // wrap = 1 -> wrapping
143 // wrap = 0 -> unwrapping
144 bool OSSLAES::wrapUnwrapKey(const SymmetricKey* key, const SymWrap::Type mode, const ByteString& in, ByteString& out, const int wrap) const
145 {
146 const char *prefix = "";
147 if (wrap == 0)
148 prefix = "un";
149
150 // Determine the cipher method
151 const EVP_CIPHER* cipher = getWrapCipher(mode, key);
152 if (cipher == NULL)
153 {
154 ERROR_MSG("Failed to get EVP %swrap cipher", prefix);
155 return false;
156 }
157
158 // Allocate the EVP context
159 EVP_CIPHER_CTX* pWrapCTX = EVP_CIPHER_CTX_new();
160 if (pWrapCTX == NULL)
161 {
162 ERROR_MSG("Failed to allocate space for EVP_CIPHER_CTX");
163 return false;
164 }
165 EVP_CIPHER_CTX_set_flags(pWrapCTX, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
166
167 int rv = EVP_CipherInit_ex(pWrapCTX, cipher, NULL, (unsigned char*) key->getKeyBits().const_byte_str(), NULL, wrap);
168 if (rv)
169 // Padding is handled by cipher mode separately
170 rv = EVP_CIPHER_CTX_set_padding(pWrapCTX, 0);
171 if (!rv)
172 {
173 ERROR_MSG("Failed to initialise EVP cipher %swrap operation", prefix);
174
175 EVP_CIPHER_CTX_free(pWrapCTX);
176 return false;
177 }
178
179 // 1 input byte could be expanded to two AES blocks
180 out.resize(in.size() + 2 * EVP_CIPHER_CTX_block_size(pWrapCTX) - 1);
181 int outLen = 0;
182 int curBlockLen = 0;
183 rv = EVP_CipherUpdate(pWrapCTX, &out[0], &curBlockLen, in.const_byte_str(), in.size());
184 if (rv == 1) {
185 outLen = curBlockLen;
186 rv = EVP_CipherFinal_ex(pWrapCTX, &out[0] + outLen, &curBlockLen);
187 }
188 if (rv != 1)
189 {
190 ERROR_MSG("Failed EVP %swrap operation", prefix);
191
192 EVP_CIPHER_CTX_free(pWrapCTX);
193 return false;
194 }
195 EVP_CIPHER_CTX_free(pWrapCTX);
196 outLen += curBlockLen;
197 out.resize(outLen);
198 return true;
199 }
200 #endif
201
202 const EVP_CIPHER* OSSLAES::getCipher() const
203 {
204 if (currentKey == NULL) return NULL;
205
206 // Check currentKey bit length; AES only supports 128, 192 or 256 bit keys
207 if ((currentKey->getBitLen() != 128) &&
208 (currentKey->getBitLen() != 192) &&
209 (currentKey->getBitLen() != 256))
210 {
211 ERROR_MSG("Invalid AES currentKey length (%d bits)", currentKey->getBitLen());
212
213 return NULL;
214 }
215
216 // Determine the cipher mode
217 if (currentCipherMode == SymMode::CBC)
218 {
219 switch(currentKey->getBitLen())
220 {
221 case 128:
222 return EVP_aes_128_cbc();
223 case 192:
224 return EVP_aes_192_cbc();
225 case 256:
226 return EVP_aes_256_cbc();
227 };
228 }
229 else if (currentCipherMode == SymMode::ECB)
230 {
231 switch(currentKey->getBitLen())
232 {
233 case 128:
234 return EVP_aes_128_ecb();
235 case 192:
236 return EVP_aes_192_ecb();
237 case 256:
238 return EVP_aes_256_ecb();
239 };
240 }
241 else if (currentCipherMode == SymMode::CTR)
242 {
243 switch(currentKey->getBitLen())
244 {
245 case 128:
246 return EVP_aes_128_ctr();
247 case 192:
248 return EVP_aes_192_ctr();
249 case 256:
250 return EVP_aes_256_ctr();
251 };
252 }
253 else if (currentCipherMode == SymMode::GCM)
254 {
255 switch(currentKey->getBitLen())
256 {
257 case 128:
258 return EVP_aes_128_gcm();
259 case 192:
260 return EVP_aes_192_gcm();
261 case 256:
262 return EVP_aes_256_gcm();
263 };
264 }
265
266 ERROR_MSG("Invalid AES cipher mode %i", currentCipherMode);
267
268 return NULL;
269 }
270
271 size_t OSSLAES::getBlockSize() const
272 {
273 // The block size is 128 bits
274 return 128 >> 3;
275 }