]> git.proxmox.com Git - mirror_qemu.git/blame - crypto/cipher-nettle.c
crypto: make PBKDF iterations configurable for LUKS format
[mirror_qemu.git] / crypto / cipher-nettle.c
CommitLineData
ed754746
DB
1/*
2 * QEMU Crypto cipher nettle algorithms
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
42f7a448 21#include "qemu/osdep.h"
eaec903c
DB
22#include "crypto/xts.h"
23
ed754746
DB
24#include <nettle/nettle-types.h>
25#include <nettle/aes.h>
26#include <nettle/des.h>
27#include <nettle/cbc.h>
084a85ee 28#include <nettle/cast128.h>
94318522 29#include <nettle/serpent.h>
50f6753e 30#include <nettle/twofish.h>
ed754746 31
f7ac78cf
DB
32typedef void (*QCryptoCipherNettleFuncWrapper)(const void *ctx,
33 size_t length,
34 uint8_t *dst,
35 const uint8_t *src);
d3462e37 36
f7ac78cf
DB
37#if CONFIG_NETTLE_VERSION_MAJOR < 3
38typedef nettle_crypt_func * QCryptoCipherNettleFuncNative;
d3462e37
RK
39typedef void * cipher_ctx_t;
40typedef unsigned cipher_length_t;
621e6ae6
DB
41
42#define cast5_set_key cast128_set_key
d3462e37 43#else
f7ac78cf 44typedef nettle_cipher_func * QCryptoCipherNettleFuncNative;
d3462e37
RK
45typedef const void * cipher_ctx_t;
46typedef size_t cipher_length_t;
becaeb72
RK
47#endif
48
e3ba0b67
DB
49typedef struct QCryptoNettleAES {
50 struct aes_ctx enc;
51 struct aes_ctx dec;
52} QCryptoNettleAES;
53
f7ac78cf
DB
54static void aes_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
55 uint8_t *dst, const uint8_t *src)
56{
57 const QCryptoNettleAES *aesctx = ctx;
58 aes_encrypt(&aesctx->enc, length, dst, src);
59}
60
61static void aes_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
62 uint8_t *dst, const uint8_t *src)
63{
64 const QCryptoNettleAES *aesctx = ctx;
65 aes_decrypt(&aesctx->dec, length, dst, src);
66}
67
68static void des_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
69 uint8_t *dst, const uint8_t *src)
70{
71 des_encrypt(ctx, length, dst, src);
72}
73
74static void des_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
75 uint8_t *dst, const uint8_t *src)
76{
77 des_decrypt(ctx, length, dst, src);
78}
79
80static void cast128_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
81 uint8_t *dst, const uint8_t *src)
82{
83 cast128_encrypt(ctx, length, dst, src);
84}
85
86static void cast128_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
87 uint8_t *dst, const uint8_t *src)
88{
89 cast128_decrypt(ctx, length, dst, src);
90}
91
92static void serpent_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
93 uint8_t *dst, const uint8_t *src)
94{
95 serpent_encrypt(ctx, length, dst, src);
96}
97
98static void serpent_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
99 uint8_t *dst, const uint8_t *src)
100{
101 serpent_decrypt(ctx, length, dst, src);
102}
103
104static void twofish_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
105 uint8_t *dst, const uint8_t *src)
106{
107 twofish_encrypt(ctx, length, dst, src);
108}
109
110static void twofish_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
111 uint8_t *dst, const uint8_t *src)
112{
113 twofish_decrypt(ctx, length, dst, src);
114}
115
116static void aes_encrypt_wrapper(const void *ctx, size_t length,
d3462e37
RK
117 uint8_t *dst, const uint8_t *src)
118{
e3ba0b67
DB
119 const QCryptoNettleAES *aesctx = ctx;
120 aes_encrypt(&aesctx->enc, length, dst, src);
d3462e37
RK
121}
122
f7ac78cf 123static void aes_decrypt_wrapper(const void *ctx, size_t length,
d3462e37
RK
124 uint8_t *dst, const uint8_t *src)
125{
e3ba0b67
DB
126 const QCryptoNettleAES *aesctx = ctx;
127 aes_decrypt(&aesctx->dec, length, dst, src);
d3462e37
RK
128}
129
f7ac78cf 130static void des_encrypt_wrapper(const void *ctx, size_t length,
d3462e37
RK
131 uint8_t *dst, const uint8_t *src)
132{
133 des_encrypt(ctx, length, dst, src);
134}
135
f7ac78cf 136static void des_decrypt_wrapper(const void *ctx, size_t length,
d3462e37
RK
137 uint8_t *dst, const uint8_t *src)
138{
139 des_decrypt(ctx, length, dst, src);
140}
141
f7ac78cf 142static void cast128_encrypt_wrapper(const void *ctx, size_t length,
084a85ee
DB
143 uint8_t *dst, const uint8_t *src)
144{
145 cast128_encrypt(ctx, length, dst, src);
146}
147
f7ac78cf 148static void cast128_decrypt_wrapper(const void *ctx, size_t length,
084a85ee
DB
149 uint8_t *dst, const uint8_t *src)
150{
151 cast128_decrypt(ctx, length, dst, src);
152}
153
f7ac78cf 154static void serpent_encrypt_wrapper(const void *ctx, size_t length,
94318522
DB
155 uint8_t *dst, const uint8_t *src)
156{
157 serpent_encrypt(ctx, length, dst, src);
158}
159
f7ac78cf 160static void serpent_decrypt_wrapper(const void *ctx, size_t length,
94318522
DB
161 uint8_t *dst, const uint8_t *src)
162{
163 serpent_decrypt(ctx, length, dst, src);
164}
165
f7ac78cf 166static void twofish_encrypt_wrapper(const void *ctx, size_t length,
50f6753e
DB
167 uint8_t *dst, const uint8_t *src)
168{
169 twofish_encrypt(ctx, length, dst, src);
170}
171
f7ac78cf 172static void twofish_decrypt_wrapper(const void *ctx, size_t length,
50f6753e
DB
173 uint8_t *dst, const uint8_t *src)
174{
175 twofish_decrypt(ctx, length, dst, src);
176}
177
ed754746
DB
178typedef struct QCryptoCipherNettle QCryptoCipherNettle;
179struct QCryptoCipherNettle {
eaec903c 180 /* Primary cipher context for all modes */
e3ba0b67 181 void *ctx;
eaec903c
DB
182 /* Second cipher context for XTS mode only */
183 void *ctx_tweak;
184 /* Cipher callbacks for both contexts */
f7ac78cf
DB
185 QCryptoCipherNettleFuncNative alg_encrypt_native;
186 QCryptoCipherNettleFuncNative alg_decrypt_native;
187 QCryptoCipherNettleFuncWrapper alg_encrypt_wrapper;
188 QCryptoCipherNettleFuncWrapper alg_decrypt_wrapper;
eaec903c 189
ed754746 190 uint8_t *iv;
3a661f1e 191 size_t blocksize;
ed754746
DB
192};
193
194bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
195{
196 switch (alg) {
197 case QCRYPTO_CIPHER_ALG_DES_RFB:
198 case QCRYPTO_CIPHER_ALG_AES_128:
199 case QCRYPTO_CIPHER_ALG_AES_192:
200 case QCRYPTO_CIPHER_ALG_AES_256:
084a85ee 201 case QCRYPTO_CIPHER_ALG_CAST5_128:
94318522
DB
202 case QCRYPTO_CIPHER_ALG_SERPENT_128:
203 case QCRYPTO_CIPHER_ALG_SERPENT_192:
204 case QCRYPTO_CIPHER_ALG_SERPENT_256:
50f6753e
DB
205 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
206 case QCRYPTO_CIPHER_ALG_TWOFISH_192:
207 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
ed754746
DB
208 return true;
209 default:
210 return false;
211 }
212}
213
214
215QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
216 QCryptoCipherMode mode,
217 const uint8_t *key, size_t nkey,
218 Error **errp)
219{
220 QCryptoCipher *cipher;
221 QCryptoCipherNettle *ctx;
222 uint8_t *rfbkey;
223
224 switch (mode) {
225 case QCRYPTO_CIPHER_MODE_ECB:
226 case QCRYPTO_CIPHER_MODE_CBC:
eaec903c 227 case QCRYPTO_CIPHER_MODE_XTS:
ed754746
DB
228 break;
229 default:
90d6f60d
DB
230 error_setg(errp, "Unsupported cipher mode %s",
231 QCryptoCipherMode_lookup[mode]);
ed754746
DB
232 return NULL;
233 }
234
eaec903c 235 if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
ed754746
DB
236 return NULL;
237 }
238
239 cipher = g_new0(QCryptoCipher, 1);
240 cipher->alg = alg;
241 cipher->mode = mode;
242
243 ctx = g_new0(QCryptoCipherNettle, 1);
244
245 switch (alg) {
246 case QCRYPTO_CIPHER_ALG_DES_RFB:
e3ba0b67 247 ctx->ctx = g_new0(struct des_ctx, 1);
ed754746 248 rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
e3ba0b67 249 des_set_key(ctx->ctx, rfbkey);
ed754746
DB
250 g_free(rfbkey);
251
f7ac78cf
DB
252 ctx->alg_encrypt_native = des_encrypt_native;
253 ctx->alg_decrypt_native = des_decrypt_native;
254 ctx->alg_encrypt_wrapper = des_encrypt_wrapper;
255 ctx->alg_decrypt_wrapper = des_decrypt_wrapper;
ed754746 256
3a661f1e 257 ctx->blocksize = DES_BLOCK_SIZE;
ed754746
DB
258 break;
259
260 case QCRYPTO_CIPHER_ALG_AES_128:
261 case QCRYPTO_CIPHER_ALG_AES_192:
262 case QCRYPTO_CIPHER_ALG_AES_256:
e3ba0b67 263 ctx->ctx = g_new0(QCryptoNettleAES, 1);
ed754746 264
eaec903c
DB
265 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
266 ctx->ctx_tweak = g_new0(QCryptoNettleAES, 1);
267
268 nkey /= 2;
269 aes_set_encrypt_key(&((QCryptoNettleAES *)ctx->ctx)->enc,
270 nkey, key);
271 aes_set_decrypt_key(&((QCryptoNettleAES *)ctx->ctx)->dec,
272 nkey, key);
273
274 aes_set_encrypt_key(&((QCryptoNettleAES *)ctx->ctx_tweak)->enc,
275 nkey, key + nkey);
276 aes_set_decrypt_key(&((QCryptoNettleAES *)ctx->ctx_tweak)->dec,
277 nkey, key + nkey);
278 } else {
279 aes_set_encrypt_key(&((QCryptoNettleAES *)ctx->ctx)->enc,
280 nkey, key);
281 aes_set_decrypt_key(&((QCryptoNettleAES *)ctx->ctx)->dec,
282 nkey, key);
283 }
ed754746 284
f7ac78cf
DB
285 ctx->alg_encrypt_native = aes_encrypt_native;
286 ctx->alg_decrypt_native = aes_decrypt_native;
287 ctx->alg_encrypt_wrapper = aes_encrypt_wrapper;
288 ctx->alg_decrypt_wrapper = aes_decrypt_wrapper;
ed754746 289
3a661f1e 290 ctx->blocksize = AES_BLOCK_SIZE;
ed754746 291 break;
084a85ee
DB
292
293 case QCRYPTO_CIPHER_ALG_CAST5_128:
e3ba0b67 294 ctx->ctx = g_new0(struct cast128_ctx, 1);
084a85ee 295
eaec903c
DB
296 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
297 ctx->ctx_tweak = g_new0(struct cast128_ctx, 1);
298
299 nkey /= 2;
300 cast5_set_key(ctx->ctx, nkey, key);
301 cast5_set_key(ctx->ctx_tweak, nkey, key + nkey);
302 } else {
303 cast5_set_key(ctx->ctx, nkey, key);
304 }
084a85ee 305
f7ac78cf
DB
306 ctx->alg_encrypt_native = cast128_encrypt_native;
307 ctx->alg_decrypt_native = cast128_decrypt_native;
308 ctx->alg_encrypt_wrapper = cast128_encrypt_wrapper;
309 ctx->alg_decrypt_wrapper = cast128_decrypt_wrapper;
084a85ee
DB
310
311 ctx->blocksize = CAST128_BLOCK_SIZE;
312 break;
94318522
DB
313
314 case QCRYPTO_CIPHER_ALG_SERPENT_128:
315 case QCRYPTO_CIPHER_ALG_SERPENT_192:
316 case QCRYPTO_CIPHER_ALG_SERPENT_256:
e3ba0b67 317 ctx->ctx = g_new0(struct serpent_ctx, 1);
94318522 318
eaec903c
DB
319 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
320 ctx->ctx_tweak = g_new0(struct serpent_ctx, 1);
321
322 nkey /= 2;
323 serpent_set_key(ctx->ctx, nkey, key);
324 serpent_set_key(ctx->ctx_tweak, nkey, key + nkey);
325 } else {
326 serpent_set_key(ctx->ctx, nkey, key);
327 }
94318522 328
f7ac78cf
DB
329 ctx->alg_encrypt_native = serpent_encrypt_native;
330 ctx->alg_decrypt_native = serpent_decrypt_native;
331 ctx->alg_encrypt_wrapper = serpent_encrypt_wrapper;
332 ctx->alg_decrypt_wrapper = serpent_decrypt_wrapper;
94318522
DB
333
334 ctx->blocksize = SERPENT_BLOCK_SIZE;
335 break;
336
50f6753e
DB
337 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
338 case QCRYPTO_CIPHER_ALG_TWOFISH_192:
339 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
e3ba0b67 340 ctx->ctx = g_new0(struct twofish_ctx, 1);
50f6753e 341
eaec903c
DB
342 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
343 ctx->ctx_tweak = g_new0(struct twofish_ctx, 1);
344
345 nkey /= 2;
346 twofish_set_key(ctx->ctx, nkey, key);
347 twofish_set_key(ctx->ctx_tweak, nkey, key + nkey);
348 } else {
349 twofish_set_key(ctx->ctx, nkey, key);
350 }
50f6753e 351
f7ac78cf
DB
352 ctx->alg_encrypt_native = twofish_encrypt_native;
353 ctx->alg_decrypt_native = twofish_decrypt_native;
354 ctx->alg_encrypt_wrapper = twofish_encrypt_wrapper;
355 ctx->alg_decrypt_wrapper = twofish_decrypt_wrapper;
50f6753e
DB
356
357 ctx->blocksize = TWOFISH_BLOCK_SIZE;
358 break;
359
ed754746 360 default:
90d6f60d
DB
361 error_setg(errp, "Unsupported cipher algorithm %s",
362 QCryptoCipherAlgorithm_lookup[alg]);
ed754746
DB
363 goto error;
364 }
365
a5d2f44d
DB
366 if (mode == QCRYPTO_CIPHER_MODE_XTS &&
367 ctx->blocksize != XTS_BLOCK_SIZE) {
368 error_setg(errp, "Cipher block size %zu must equal XTS block size %d",
369 ctx->blocksize, XTS_BLOCK_SIZE);
370 goto error;
371 }
372
3a661f1e 373 ctx->iv = g_new0(uint8_t, ctx->blocksize);
ed754746
DB
374 cipher->opaque = ctx;
375
376 return cipher;
377
378 error:
379 g_free(cipher);
380 g_free(ctx);
381 return NULL;
382}
383
384
385void qcrypto_cipher_free(QCryptoCipher *cipher)
386{
387 QCryptoCipherNettle *ctx;
388
389 if (!cipher) {
390 return;
391 }
392
393 ctx = cipher->opaque;
394 g_free(ctx->iv);
e3ba0b67 395 g_free(ctx->ctx);
eaec903c 396 g_free(ctx->ctx_tweak);
ed754746
DB
397 g_free(ctx);
398 g_free(cipher);
399}
400
401
402int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
403 const void *in,
404 void *out,
405 size_t len,
406 Error **errp)
407{
408 QCryptoCipherNettle *ctx = cipher->opaque;
409
3a661f1e
DB
410 if (len % ctx->blocksize) {
411 error_setg(errp, "Length %zu must be a multiple of block size %zu",
412 len, ctx->blocksize);
413 return -1;
414 }
415
ed754746
DB
416 switch (cipher->mode) {
417 case QCRYPTO_CIPHER_MODE_ECB:
f7ac78cf 418 ctx->alg_encrypt_wrapper(ctx->ctx, len, out, in);
ed754746
DB
419 break;
420
421 case QCRYPTO_CIPHER_MODE_CBC:
f7ac78cf 422 cbc_encrypt(ctx->ctx, ctx->alg_encrypt_native,
3a661f1e 423 ctx->blocksize, ctx->iv,
ed754746
DB
424 len, out, in);
425 break;
e3ba0b67 426
eaec903c
DB
427 case QCRYPTO_CIPHER_MODE_XTS:
428 xts_encrypt(ctx->ctx, ctx->ctx_tweak,
f7ac78cf 429 ctx->alg_encrypt_wrapper, ctx->alg_encrypt_wrapper,
eaec903c
DB
430 ctx->iv, len, out, in);
431 break;
432
ed754746 433 default:
90d6f60d
DB
434 error_setg(errp, "Unsupported cipher mode %s",
435 QCryptoCipherMode_lookup[cipher->mode]);
ed754746
DB
436 return -1;
437 }
438 return 0;
439}
440
441
442int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
443 const void *in,
444 void *out,
445 size_t len,
446 Error **errp)
447{
448 QCryptoCipherNettle *ctx = cipher->opaque;
449
3a661f1e
DB
450 if (len % ctx->blocksize) {
451 error_setg(errp, "Length %zu must be a multiple of block size %zu",
452 len, ctx->blocksize);
453 return -1;
454 }
455
ed754746
DB
456 switch (cipher->mode) {
457 case QCRYPTO_CIPHER_MODE_ECB:
f7ac78cf 458 ctx->alg_decrypt_wrapper(ctx->ctx, len, out, in);
ed754746
DB
459 break;
460
461 case QCRYPTO_CIPHER_MODE_CBC:
f7ac78cf 462 cbc_decrypt(ctx->ctx, ctx->alg_decrypt_native,
e3ba0b67 463 ctx->blocksize, ctx->iv,
ed754746
DB
464 len, out, in);
465 break;
e3ba0b67 466
eaec903c 467 case QCRYPTO_CIPHER_MODE_XTS:
eaec903c 468 xts_decrypt(ctx->ctx, ctx->ctx_tweak,
f7ac78cf 469 ctx->alg_encrypt_wrapper, ctx->alg_decrypt_wrapper,
eaec903c
DB
470 ctx->iv, len, out, in);
471 break;
472
ed754746 473 default:
90d6f60d
DB
474 error_setg(errp, "Unsupported cipher mode %s",
475 QCryptoCipherMode_lookup[cipher->mode]);
ed754746
DB
476 return -1;
477 }
478 return 0;
479}
480
481int qcrypto_cipher_setiv(QCryptoCipher *cipher,
482 const uint8_t *iv, size_t niv,
483 Error **errp)
484{
485 QCryptoCipherNettle *ctx = cipher->opaque;
3a661f1e 486 if (niv != ctx->blocksize) {
ed754746 487 error_setg(errp, "Expected IV size %zu not %zu",
3a661f1e 488 ctx->blocksize, niv);
ed754746
DB
489 return -1;
490 }
491 memcpy(ctx->iv, iv, niv);
492 return 0;
493}