]> git.proxmox.com Git - mirror_qemu.git/blame - crypto/cipher-nettle.c
configure: Don't claim 'unsupported host OS' when better message available
[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>
3c28292f 31#include <nettle/ctr.h>
ed754746 32
f7ac78cf
DB
33typedef void (*QCryptoCipherNettleFuncWrapper)(const void *ctx,
34 size_t length,
35 uint8_t *dst,
36 const uint8_t *src);
d3462e37 37
f7ac78cf
DB
38#if CONFIG_NETTLE_VERSION_MAJOR < 3
39typedef nettle_crypt_func * QCryptoCipherNettleFuncNative;
d3462e37
RK
40typedef void * cipher_ctx_t;
41typedef unsigned cipher_length_t;
621e6ae6
DB
42
43#define cast5_set_key cast128_set_key
d3462e37 44#else
f7ac78cf 45typedef nettle_cipher_func * QCryptoCipherNettleFuncNative;
d3462e37
RK
46typedef const void * cipher_ctx_t;
47typedef size_t cipher_length_t;
becaeb72
RK
48#endif
49
e3ba0b67
DB
50typedef struct QCryptoNettleAES {
51 struct aes_ctx enc;
52 struct aes_ctx dec;
53} QCryptoNettleAES;
54
f7ac78cf
DB
55static void aes_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
56 uint8_t *dst, const uint8_t *src)
57{
58 const QCryptoNettleAES *aesctx = ctx;
59 aes_encrypt(&aesctx->enc, length, dst, src);
60}
61
62static void aes_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
63 uint8_t *dst, const uint8_t *src)
64{
65 const QCryptoNettleAES *aesctx = ctx;
66 aes_decrypt(&aesctx->dec, length, dst, src);
67}
68
69static void des_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
70 uint8_t *dst, const uint8_t *src)
71{
72 des_encrypt(ctx, length, dst, src);
73}
74
75static void des_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
76 uint8_t *dst, const uint8_t *src)
77{
78 des_decrypt(ctx, length, dst, src);
79}
80
ffb7bf45
LM
81static void des3_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
82 uint8_t *dst, const uint8_t *src)
83{
84 des3_encrypt(ctx, length, dst, src);
85}
86
87static void des3_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
88 uint8_t *dst, const uint8_t *src)
89{
90 des3_decrypt(ctx, length, dst, src);
91}
92
f7ac78cf
DB
93static void cast128_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
94 uint8_t *dst, const uint8_t *src)
95{
96 cast128_encrypt(ctx, length, dst, src);
97}
98
99static void cast128_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
100 uint8_t *dst, const uint8_t *src)
101{
102 cast128_decrypt(ctx, length, dst, src);
103}
104
105static void serpent_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
106 uint8_t *dst, const uint8_t *src)
107{
108 serpent_encrypt(ctx, length, dst, src);
109}
110
111static void serpent_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
112 uint8_t *dst, const uint8_t *src)
113{
114 serpent_decrypt(ctx, length, dst, src);
115}
116
117static void twofish_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
118 uint8_t *dst, const uint8_t *src)
119{
120 twofish_encrypt(ctx, length, dst, src);
121}
122
123static void twofish_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
124 uint8_t *dst, const uint8_t *src)
125{
126 twofish_decrypt(ctx, length, dst, src);
127}
128
129static void aes_encrypt_wrapper(const void *ctx, size_t length,
d3462e37
RK
130 uint8_t *dst, const uint8_t *src)
131{
e3ba0b67
DB
132 const QCryptoNettleAES *aesctx = ctx;
133 aes_encrypt(&aesctx->enc, length, dst, src);
d3462e37
RK
134}
135
f7ac78cf 136static void aes_decrypt_wrapper(const void *ctx, size_t length,
d3462e37
RK
137 uint8_t *dst, const uint8_t *src)
138{
e3ba0b67
DB
139 const QCryptoNettleAES *aesctx = ctx;
140 aes_decrypt(&aesctx->dec, length, dst, src);
d3462e37
RK
141}
142
f7ac78cf 143static void des_encrypt_wrapper(const void *ctx, size_t length,
d3462e37
RK
144 uint8_t *dst, const uint8_t *src)
145{
146 des_encrypt(ctx, length, dst, src);
147}
148
f7ac78cf 149static void des_decrypt_wrapper(const void *ctx, size_t length,
d3462e37
RK
150 uint8_t *dst, const uint8_t *src)
151{
152 des_decrypt(ctx, length, dst, src);
153}
154
ffb7bf45
LM
155static void des3_encrypt_wrapper(const void *ctx, size_t length,
156 uint8_t *dst, const uint8_t *src)
157{
158 des3_encrypt(ctx, length, dst, src);
159}
160
161static void des3_decrypt_wrapper(const void *ctx, size_t length,
162 uint8_t *dst, const uint8_t *src)
163{
164 des3_decrypt(ctx, length, dst, src);
165}
166
f7ac78cf 167static void cast128_encrypt_wrapper(const void *ctx, size_t length,
084a85ee
DB
168 uint8_t *dst, const uint8_t *src)
169{
170 cast128_encrypt(ctx, length, dst, src);
171}
172
f7ac78cf 173static void cast128_decrypt_wrapper(const void *ctx, size_t length,
084a85ee
DB
174 uint8_t *dst, const uint8_t *src)
175{
176 cast128_decrypt(ctx, length, dst, src);
177}
178
f7ac78cf 179static void serpent_encrypt_wrapper(const void *ctx, size_t length,
94318522
DB
180 uint8_t *dst, const uint8_t *src)
181{
182 serpent_encrypt(ctx, length, dst, src);
183}
184
f7ac78cf 185static void serpent_decrypt_wrapper(const void *ctx, size_t length,
94318522
DB
186 uint8_t *dst, const uint8_t *src)
187{
188 serpent_decrypt(ctx, length, dst, src);
189}
190
f7ac78cf 191static void twofish_encrypt_wrapper(const void *ctx, size_t length,
50f6753e
DB
192 uint8_t *dst, const uint8_t *src)
193{
194 twofish_encrypt(ctx, length, dst, src);
195}
196
f7ac78cf 197static void twofish_decrypt_wrapper(const void *ctx, size_t length,
50f6753e
DB
198 uint8_t *dst, const uint8_t *src)
199{
200 twofish_decrypt(ctx, length, dst, src);
201}
202
ed754746
DB
203typedef struct QCryptoCipherNettle QCryptoCipherNettle;
204struct QCryptoCipherNettle {
eaec903c 205 /* Primary cipher context for all modes */
e3ba0b67 206 void *ctx;
eaec903c
DB
207 /* Second cipher context for XTS mode only */
208 void *ctx_tweak;
209 /* Cipher callbacks for both contexts */
f7ac78cf
DB
210 QCryptoCipherNettleFuncNative alg_encrypt_native;
211 QCryptoCipherNettleFuncNative alg_decrypt_native;
212 QCryptoCipherNettleFuncWrapper alg_encrypt_wrapper;
213 QCryptoCipherNettleFuncWrapper alg_decrypt_wrapper;
3c28292f 214 /* Initialization vector or Counter */
ed754746 215 uint8_t *iv;
3a661f1e 216 size_t blocksize;
ed754746
DB
217};
218
f844836d
GA
219bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
220 QCryptoCipherMode mode)
ed754746
DB
221{
222 switch (alg) {
223 case QCRYPTO_CIPHER_ALG_DES_RFB:
ffb7bf45 224 case QCRYPTO_CIPHER_ALG_3DES:
ed754746
DB
225 case QCRYPTO_CIPHER_ALG_AES_128:
226 case QCRYPTO_CIPHER_ALG_AES_192:
227 case QCRYPTO_CIPHER_ALG_AES_256:
084a85ee 228 case QCRYPTO_CIPHER_ALG_CAST5_128:
94318522
DB
229 case QCRYPTO_CIPHER_ALG_SERPENT_128:
230 case QCRYPTO_CIPHER_ALG_SERPENT_192:
231 case QCRYPTO_CIPHER_ALG_SERPENT_256:
50f6753e
DB
232 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
233 case QCRYPTO_CIPHER_ALG_TWOFISH_192:
234 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
f844836d
GA
235 break;
236 default:
237 return false;
238 }
239
240 switch (mode) {
241 case QCRYPTO_CIPHER_MODE_ECB:
242 case QCRYPTO_CIPHER_MODE_CBC:
243 case QCRYPTO_CIPHER_MODE_XTS:
244 case QCRYPTO_CIPHER_MODE_CTR:
ed754746
DB
245 return true;
246 default:
247 return false;
248 }
249}
250
251
252QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
253 QCryptoCipherMode mode,
254 const uint8_t *key, size_t nkey,
255 Error **errp)
256{
257 QCryptoCipher *cipher;
258 QCryptoCipherNettle *ctx;
259 uint8_t *rfbkey;
260
261 switch (mode) {
262 case QCRYPTO_CIPHER_MODE_ECB:
263 case QCRYPTO_CIPHER_MODE_CBC:
eaec903c 264 case QCRYPTO_CIPHER_MODE_XTS:
3c28292f 265 case QCRYPTO_CIPHER_MODE_CTR:
ed754746
DB
266 break;
267 default:
90d6f60d
DB
268 error_setg(errp, "Unsupported cipher mode %s",
269 QCryptoCipherMode_lookup[mode]);
ed754746
DB
270 return NULL;
271 }
272
eaec903c 273 if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
ed754746
DB
274 return NULL;
275 }
276
277 cipher = g_new0(QCryptoCipher, 1);
278 cipher->alg = alg;
279 cipher->mode = mode;
280
281 ctx = g_new0(QCryptoCipherNettle, 1);
d4c64800 282 cipher->opaque = ctx;
ed754746
DB
283
284 switch (alg) {
285 case QCRYPTO_CIPHER_ALG_DES_RFB:
e3ba0b67 286 ctx->ctx = g_new0(struct des_ctx, 1);
ed754746 287 rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
e3ba0b67 288 des_set_key(ctx->ctx, rfbkey);
ed754746
DB
289 g_free(rfbkey);
290
f7ac78cf
DB
291 ctx->alg_encrypt_native = des_encrypt_native;
292 ctx->alg_decrypt_native = des_decrypt_native;
293 ctx->alg_encrypt_wrapper = des_encrypt_wrapper;
294 ctx->alg_decrypt_wrapper = des_decrypt_wrapper;
ed754746 295
3a661f1e 296 ctx->blocksize = DES_BLOCK_SIZE;
ed754746
DB
297 break;
298
ffb7bf45
LM
299 case QCRYPTO_CIPHER_ALG_3DES:
300 ctx->ctx = g_new0(struct des3_ctx, 1);
301 des3_set_key(ctx->ctx, key);
302
303 ctx->alg_encrypt_native = des3_encrypt_native;
304 ctx->alg_decrypt_native = des3_decrypt_native;
305 ctx->alg_encrypt_wrapper = des3_encrypt_wrapper;
306 ctx->alg_decrypt_wrapper = des3_decrypt_wrapper;
307
308 ctx->blocksize = DES3_BLOCK_SIZE;
309 break;
310
ed754746
DB
311 case QCRYPTO_CIPHER_ALG_AES_128:
312 case QCRYPTO_CIPHER_ALG_AES_192:
313 case QCRYPTO_CIPHER_ALG_AES_256:
e3ba0b67 314 ctx->ctx = g_new0(QCryptoNettleAES, 1);
ed754746 315
eaec903c
DB
316 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
317 ctx->ctx_tweak = g_new0(QCryptoNettleAES, 1);
318
319 nkey /= 2;
320 aes_set_encrypt_key(&((QCryptoNettleAES *)ctx->ctx)->enc,
321 nkey, key);
322 aes_set_decrypt_key(&((QCryptoNettleAES *)ctx->ctx)->dec,
323 nkey, key);
324
325 aes_set_encrypt_key(&((QCryptoNettleAES *)ctx->ctx_tweak)->enc,
326 nkey, key + nkey);
327 aes_set_decrypt_key(&((QCryptoNettleAES *)ctx->ctx_tweak)->dec,
328 nkey, key + nkey);
329 } else {
330 aes_set_encrypt_key(&((QCryptoNettleAES *)ctx->ctx)->enc,
331 nkey, key);
332 aes_set_decrypt_key(&((QCryptoNettleAES *)ctx->ctx)->dec,
333 nkey, key);
334 }
ed754746 335
f7ac78cf
DB
336 ctx->alg_encrypt_native = aes_encrypt_native;
337 ctx->alg_decrypt_native = aes_decrypt_native;
338 ctx->alg_encrypt_wrapper = aes_encrypt_wrapper;
339 ctx->alg_decrypt_wrapper = aes_decrypt_wrapper;
ed754746 340
3a661f1e 341 ctx->blocksize = AES_BLOCK_SIZE;
ed754746 342 break;
084a85ee
DB
343
344 case QCRYPTO_CIPHER_ALG_CAST5_128:
e3ba0b67 345 ctx->ctx = g_new0(struct cast128_ctx, 1);
084a85ee 346
eaec903c
DB
347 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
348 ctx->ctx_tweak = g_new0(struct cast128_ctx, 1);
349
350 nkey /= 2;
351 cast5_set_key(ctx->ctx, nkey, key);
352 cast5_set_key(ctx->ctx_tweak, nkey, key + nkey);
353 } else {
354 cast5_set_key(ctx->ctx, nkey, key);
355 }
084a85ee 356
f7ac78cf
DB
357 ctx->alg_encrypt_native = cast128_encrypt_native;
358 ctx->alg_decrypt_native = cast128_decrypt_native;
359 ctx->alg_encrypt_wrapper = cast128_encrypt_wrapper;
360 ctx->alg_decrypt_wrapper = cast128_decrypt_wrapper;
084a85ee
DB
361
362 ctx->blocksize = CAST128_BLOCK_SIZE;
363 break;
94318522
DB
364
365 case QCRYPTO_CIPHER_ALG_SERPENT_128:
366 case QCRYPTO_CIPHER_ALG_SERPENT_192:
367 case QCRYPTO_CIPHER_ALG_SERPENT_256:
e3ba0b67 368 ctx->ctx = g_new0(struct serpent_ctx, 1);
94318522 369
eaec903c
DB
370 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
371 ctx->ctx_tweak = g_new0(struct serpent_ctx, 1);
372
373 nkey /= 2;
374 serpent_set_key(ctx->ctx, nkey, key);
375 serpent_set_key(ctx->ctx_tweak, nkey, key + nkey);
376 } else {
377 serpent_set_key(ctx->ctx, nkey, key);
378 }
94318522 379
f7ac78cf
DB
380 ctx->alg_encrypt_native = serpent_encrypt_native;
381 ctx->alg_decrypt_native = serpent_decrypt_native;
382 ctx->alg_encrypt_wrapper = serpent_encrypt_wrapper;
383 ctx->alg_decrypt_wrapper = serpent_decrypt_wrapper;
94318522
DB
384
385 ctx->blocksize = SERPENT_BLOCK_SIZE;
386 break;
387
50f6753e
DB
388 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
389 case QCRYPTO_CIPHER_ALG_TWOFISH_192:
390 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
e3ba0b67 391 ctx->ctx = g_new0(struct twofish_ctx, 1);
50f6753e 392
eaec903c
DB
393 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
394 ctx->ctx_tweak = g_new0(struct twofish_ctx, 1);
395
396 nkey /= 2;
397 twofish_set_key(ctx->ctx, nkey, key);
398 twofish_set_key(ctx->ctx_tweak, nkey, key + nkey);
399 } else {
400 twofish_set_key(ctx->ctx, nkey, key);
401 }
50f6753e 402
f7ac78cf
DB
403 ctx->alg_encrypt_native = twofish_encrypt_native;
404 ctx->alg_decrypt_native = twofish_decrypt_native;
405 ctx->alg_encrypt_wrapper = twofish_encrypt_wrapper;
406 ctx->alg_decrypt_wrapper = twofish_decrypt_wrapper;
50f6753e
DB
407
408 ctx->blocksize = TWOFISH_BLOCK_SIZE;
409 break;
410
ed754746 411 default:
90d6f60d
DB
412 error_setg(errp, "Unsupported cipher algorithm %s",
413 QCryptoCipherAlgorithm_lookup[alg]);
ed754746
DB
414 goto error;
415 }
416
a5d2f44d
DB
417 if (mode == QCRYPTO_CIPHER_MODE_XTS &&
418 ctx->blocksize != XTS_BLOCK_SIZE) {
419 error_setg(errp, "Cipher block size %zu must equal XTS block size %d",
420 ctx->blocksize, XTS_BLOCK_SIZE);
421 goto error;
422 }
423
3a661f1e 424 ctx->iv = g_new0(uint8_t, ctx->blocksize);
ed754746
DB
425
426 return cipher;
427
428 error:
d4c64800 429 qcrypto_cipher_free(cipher);
ed754746
DB
430 return NULL;
431}
432
433
434void qcrypto_cipher_free(QCryptoCipher *cipher)
435{
436 QCryptoCipherNettle *ctx;
437
438 if (!cipher) {
439 return;
440 }
441
442 ctx = cipher->opaque;
443 g_free(ctx->iv);
e3ba0b67 444 g_free(ctx->ctx);
eaec903c 445 g_free(ctx->ctx_tweak);
ed754746
DB
446 g_free(ctx);
447 g_free(cipher);
448}
449
450
451int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
452 const void *in,
453 void *out,
454 size_t len,
455 Error **errp)
456{
457 QCryptoCipherNettle *ctx = cipher->opaque;
458
3a661f1e
DB
459 if (len % ctx->blocksize) {
460 error_setg(errp, "Length %zu must be a multiple of block size %zu",
461 len, ctx->blocksize);
462 return -1;
463 }
464
ed754746
DB
465 switch (cipher->mode) {
466 case QCRYPTO_CIPHER_MODE_ECB:
f7ac78cf 467 ctx->alg_encrypt_wrapper(ctx->ctx, len, out, in);
ed754746
DB
468 break;
469
470 case QCRYPTO_CIPHER_MODE_CBC:
f7ac78cf 471 cbc_encrypt(ctx->ctx, ctx->alg_encrypt_native,
3a661f1e 472 ctx->blocksize, ctx->iv,
ed754746
DB
473 len, out, in);
474 break;
e3ba0b67 475
eaec903c
DB
476 case QCRYPTO_CIPHER_MODE_XTS:
477 xts_encrypt(ctx->ctx, ctx->ctx_tweak,
f7ac78cf 478 ctx->alg_encrypt_wrapper, ctx->alg_encrypt_wrapper,
eaec903c
DB
479 ctx->iv, len, out, in);
480 break;
481
3c28292f
GA
482 case QCRYPTO_CIPHER_MODE_CTR:
483 ctr_crypt(ctx->ctx, ctx->alg_encrypt_native,
484 ctx->blocksize, ctx->iv,
485 len, out, in);
486 break;
487
ed754746 488 default:
90d6f60d
DB
489 error_setg(errp, "Unsupported cipher mode %s",
490 QCryptoCipherMode_lookup[cipher->mode]);
ed754746
DB
491 return -1;
492 }
493 return 0;
494}
495
496
497int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
498 const void *in,
499 void *out,
500 size_t len,
501 Error **errp)
502{
503 QCryptoCipherNettle *ctx = cipher->opaque;
504
3a661f1e
DB
505 if (len % ctx->blocksize) {
506 error_setg(errp, "Length %zu must be a multiple of block size %zu",
507 len, ctx->blocksize);
508 return -1;
509 }
510
ed754746
DB
511 switch (cipher->mode) {
512 case QCRYPTO_CIPHER_MODE_ECB:
f7ac78cf 513 ctx->alg_decrypt_wrapper(ctx->ctx, len, out, in);
ed754746
DB
514 break;
515
516 case QCRYPTO_CIPHER_MODE_CBC:
f7ac78cf 517 cbc_decrypt(ctx->ctx, ctx->alg_decrypt_native,
e3ba0b67 518 ctx->blocksize, ctx->iv,
ed754746
DB
519 len, out, in);
520 break;
e3ba0b67 521
eaec903c 522 case QCRYPTO_CIPHER_MODE_XTS:
eaec903c 523 xts_decrypt(ctx->ctx, ctx->ctx_tweak,
f7ac78cf 524 ctx->alg_encrypt_wrapper, ctx->alg_decrypt_wrapper,
eaec903c
DB
525 ctx->iv, len, out, in);
526 break;
3c28292f
GA
527 case QCRYPTO_CIPHER_MODE_CTR:
528 ctr_crypt(ctx->ctx, ctx->alg_encrypt_native,
529 ctx->blocksize, ctx->iv,
530 len, out, in);
531 break;
eaec903c 532
ed754746 533 default:
90d6f60d
DB
534 error_setg(errp, "Unsupported cipher mode %s",
535 QCryptoCipherMode_lookup[cipher->mode]);
ed754746
DB
536 return -1;
537 }
538 return 0;
539}
540
541int qcrypto_cipher_setiv(QCryptoCipher *cipher,
542 const uint8_t *iv, size_t niv,
543 Error **errp)
544{
545 QCryptoCipherNettle *ctx = cipher->opaque;
3a661f1e 546 if (niv != ctx->blocksize) {
ed754746 547 error_setg(errp, "Expected IV size %zu not %zu",
3a661f1e 548 ctx->blocksize, niv);
ed754746
DB
549 return -1;
550 }
551 memcpy(ctx->iv, iv, niv);
552 return 0;
553}