Debian Codegraph @

src/lib/crypto/BotanAES.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 /*****************************************************************************
28 BotanAES.cpp
29
30 Botan AES implementation
31 *****************************************************************************/
32
33 #include "config.h"
34 #include "BotanAES.h"
35 #include <algorithm>
36 #include <botan/rfc3394.h>
37 #include <botan/version.h>
38
39 #ifdef HAVE_AES_KEY_WRAP_PAD
40 #include <botan/nist_keywrap.h>
41 #include <botan/block_cipher.h>
42 #endif
43
44 // Wrap/Unwrap keys
45 bool BotanAES::wrapKey(const SymmetricKey* key, const SymWrap::Type mode, const ByteString& in, ByteString& out)
46 {
47 // Check key bit length; AES only supports 128, 192 or 256 bit keys
48 if ((key->getBitLen() != 128) &&
49 (key->getBitLen() != 192) &&
50 (key->getBitLen() != 256))
51 {
52 ERROR_MSG("Invalid AES key length (%d bits)", key->getBitLen());
53
54 return false;
55 }
56
57 // Determine the wrapping mode
58 if (mode == SymWrap::AES_KEYWRAP)
59 {
60 // RFC 3394 AES key wrap
61 if (in.size() < 16)
62 {
63 ERROR_MSG("key data to wrap too small");
64
65 return false;
66 }
67 if ((in.size() % 8) != 0)
68 {
69 ERROR_MSG("key data to wrap not aligned");
70
71 return false;
72 }
73
74 Botan::secure_vector<uint8_t> data(in.size());
75 memcpy(data.data(), in.const_byte_str(), in.size());
76 Botan::secure_vector<uint8_t> wrapped;
77 Botan::SymmetricKey botanKey = Botan::SymmetricKey(key->getKeyBits().const_byte_str(), key->getKeyBits().size());
78 try
79 {
80 wrapped = Botan::rfc3394_keywrap(data, botanKey);
81 }
82 catch (...)
83 {
84 ERROR_MSG("AES key wrap failed");
85
86 return false;
87 }
88 out.resize(wrapped.size());
89 memcpy(&out[0], wrapped.data(), out.size());
90
91 return true;
92 }
93 #ifdef HAVE_AES_KEY_WRAP_PAD
94 else if (mode == SymWrap::AES_KEYWRAP_PAD)
95 {
96 try
97 {
98 const std::string aes_name = "AES-" + std::to_string(key->getKeyBits().size()*8);
99 auto aes = Botan::BlockCipher::create_or_throw(aes_name);
100 aes->set_key(key->getKeyBits().const_byte_str(), key->getKeyBits().size());
101 std::vector<uint8_t> wrapped = Botan::nist_key_wrap_padded(in.const_byte_str(), in.size(), *aes);
102 out.resize(wrapped.size());
103 memcpy(&out[0], wrapped.data(), out.size());
104 return true;
105 }
106 catch (...)
107 {
108 ERROR_MSG("AES key wrap failed");
109
110 return false;
111 }
112 return true;
113 }
114 #endif
115 else
116 {
117 ERROR_MSG("unknown AES key wrap mode %i", mode);
118
119 return false;
120 }
121 }
122
123 bool BotanAES::unwrapKey(const SymmetricKey* key, const SymWrap::Type mode, const ByteString& in, ByteString& out)
124 {
125 // Check key bit length; AES only supports 128, 192 or 256 bit keys
126 if ((key->getBitLen() != 128) &&
127 (key->getBitLen() != 192) &&
128 (key->getBitLen() != 256))
129 {
130 ERROR_MSG("Invalid AES key length (%d bits)", key->getBitLen());
131
132 return false;
133 }
134
135 // Determine the unwrapping mode
136 if (mode == SymWrap::AES_KEYWRAP)
137 {
138 // RFC 3394 AES key wrap
139 if (in.size() < 24)
140 {
141 ERROR_MSG("key data to unwrap too small");
142
143 return false;
144 }
145 if ((in.size() % 8) != 0)
146 {
147 ERROR_MSG("key data to unwrap not aligned");
148
149 return false;
150 }
151
152 Botan::secure_vector<uint8_t> wrapped(in.size());
153 memcpy(wrapped.data(), in.const_byte_str(), in.size());
154 Botan::secure_vector<uint8_t> unwrapped;
155 Botan::SymmetricKey botanKey = Botan::SymmetricKey(key->getKeyBits().const_byte_str(), key->getKeyBits().size());
156 try
157 {
158 unwrapped = Botan::rfc3394_keyunwrap(wrapped, botanKey);
159 }
160 catch (...)
161 {
162 ERROR_MSG("AES key unwrap failed");
163
164 return false;
165 }
166 out.resize(unwrapped.size());
167 memcpy(&out[0], unwrapped.data(), out.size());
168
169 return true;
170 }
171 #ifdef HAVE_AES_KEY_WRAP_PAD
172 else if (mode == SymWrap::AES_KEYWRAP_PAD)
173 {
174 // RFC 5649 AES key wrap with wrap
175 if (in.size() < 16)
176 {
177 ERROR_MSG("key data to unwrap too small");
178
179 return false;
180 }
181 if ((in.size() % 8) != 0)
182 {
183 ERROR_MSG("key data to unwrap not aligned");
184
185 return false;
186 }
187 try
188 {
189 const std::string aes_name = "AES-" + std::to_string(key->getKeyBits().size()*8);
190 auto aes = Botan::BlockCipher::create_or_throw(aes_name);
191 aes->set_key(key->getKeyBits().const_byte_str(), key->getKeyBits().size());
192 auto wrapped = Botan::nist_key_unwrap_padded(in.const_byte_str(), in.size(), *aes);
193 out.resize(wrapped.size());
194 memcpy(&out[0], wrapped.data(), out.size());
195 return true;
196 }
197 catch (...)
198 {
199 ERROR_MSG("AES key unwrap failed");
200
201 return false;
202 }
203 return true;
204 }
205 #endif
206 else
207 {
208 ERROR_MSG("unknown AES key wrap mode %i", mode);
209
210 return false;
211 }
212 }
213
214 std::string BotanAES::getCipher() const
215 {
216 std::string algo;
217 std::string mode;
218 std::string padding;
219
220 if (currentKey == NULL) return "";
221
222 // Check currentKey bit length; AES only supports 128, 192 or 256 bit keys
223 switch (currentKey->getBitLen())
224 {
225 case 128:
226 algo = "AES-128";
227 break;
228 case 192:
229 algo = "AES-192";
230 break;
231 case 256:
232 algo = "AES-256";
233 break;
234 default:
235 ERROR_MSG("Invalid AES currentKey length (%d bits)", currentKey->getBitLen());
236
237 return "";
238 }
239
240 // Determine the cipher mode
241 switch (currentCipherMode)
242 {
243 case SymMode::CBC:
244 mode = "CBC";
245 break;
246 case SymMode::CTR:
247 return algo + "/CTR-BE";
248 case SymMode::ECB:
249 mode = "ECB";
250 break;
251 case SymMode::GCM:
252 return algo + "/GCM(" + std::to_string(currentTagBytes) + ")";
253 default:
254 ERROR_MSG("Invalid AES cipher mode %i", currentCipherMode);
255
256 return "";
257 }
258
259 // Check padding mode
260 if (currentPaddingMode)
261 {
262 padding = "PKCS7";
263 }
264 else
265 {
266 padding = "NoPadding";
267 }
268
269 return algo + "/" + mode + "/" + padding;
270 }
271
272 size_t BotanAES::getBlockSize() const
273 {
274 // The block size is 128 bits
275 return 128 >> 3;
276 }
277