2 * QEMU Crypto cipher nettle algorithms
4 * Copyright (c) 2015 Red Hat, Inc.
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.
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.
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/>.
21 #include "qemu/osdep.h"
22 #include "crypto/xts.h"
24 #include <nettle/nettle-types.h>
25 #include <nettle/aes.h>
26 #include <nettle/des.h>
27 #include <nettle/cbc.h>
28 #include <nettle/cast128.h>
29 #include <nettle/serpent.h>
30 #include <nettle/twofish.h>
31 #include <nettle/ctr.h>
33 typedef void (*QCryptoCipherNettleFuncWrapper
)(const void *ctx
,
38 #if CONFIG_NETTLE_VERSION_MAJOR < 3
39 typedef nettle_crypt_func
* QCryptoCipherNettleFuncNative
;
40 typedef void * cipher_ctx_t
;
41 typedef unsigned cipher_length_t
;
43 #define cast5_set_key cast128_set_key
45 typedef nettle_cipher_func
* QCryptoCipherNettleFuncNative
;
46 typedef const void * cipher_ctx_t
;
47 typedef size_t cipher_length_t
;
50 typedef struct QCryptoNettleAES
{
55 static void aes_encrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
56 uint8_t *dst
, const uint8_t *src
)
58 const QCryptoNettleAES
*aesctx
= ctx
;
59 aes_encrypt(&aesctx
->enc
, length
, dst
, src
);
62 static void aes_decrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
63 uint8_t *dst
, const uint8_t *src
)
65 const QCryptoNettleAES
*aesctx
= ctx
;
66 aes_decrypt(&aesctx
->dec
, length
, dst
, src
);
69 static void des_encrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
70 uint8_t *dst
, const uint8_t *src
)
72 des_encrypt(ctx
, length
, dst
, src
);
75 static void des_decrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
76 uint8_t *dst
, const uint8_t *src
)
78 des_decrypt(ctx
, length
, dst
, src
);
81 static void des3_encrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
82 uint8_t *dst
, const uint8_t *src
)
84 des3_encrypt(ctx
, length
, dst
, src
);
87 static void des3_decrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
88 uint8_t *dst
, const uint8_t *src
)
90 des3_decrypt(ctx
, length
, dst
, src
);
93 static void cast128_encrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
94 uint8_t *dst
, const uint8_t *src
)
96 cast128_encrypt(ctx
, length
, dst
, src
);
99 static void cast128_decrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
100 uint8_t *dst
, const uint8_t *src
)
102 cast128_decrypt(ctx
, length
, dst
, src
);
105 static void serpent_encrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
106 uint8_t *dst
, const uint8_t *src
)
108 serpent_encrypt(ctx
, length
, dst
, src
);
111 static void serpent_decrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
112 uint8_t *dst
, const uint8_t *src
)
114 serpent_decrypt(ctx
, length
, dst
, src
);
117 static void twofish_encrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
118 uint8_t *dst
, const uint8_t *src
)
120 twofish_encrypt(ctx
, length
, dst
, src
);
123 static void twofish_decrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
124 uint8_t *dst
, const uint8_t *src
)
126 twofish_decrypt(ctx
, length
, dst
, src
);
129 static void aes_encrypt_wrapper(const void *ctx
, size_t length
,
130 uint8_t *dst
, const uint8_t *src
)
132 const QCryptoNettleAES
*aesctx
= ctx
;
133 aes_encrypt(&aesctx
->enc
, length
, dst
, src
);
136 static void aes_decrypt_wrapper(const void *ctx
, size_t length
,
137 uint8_t *dst
, const uint8_t *src
)
139 const QCryptoNettleAES
*aesctx
= ctx
;
140 aes_decrypt(&aesctx
->dec
, length
, dst
, src
);
143 static void des_encrypt_wrapper(const void *ctx
, size_t length
,
144 uint8_t *dst
, const uint8_t *src
)
146 des_encrypt(ctx
, length
, dst
, src
);
149 static void des_decrypt_wrapper(const void *ctx
, size_t length
,
150 uint8_t *dst
, const uint8_t *src
)
152 des_decrypt(ctx
, length
, dst
, src
);
155 static void des3_encrypt_wrapper(const void *ctx
, size_t length
,
156 uint8_t *dst
, const uint8_t *src
)
158 des3_encrypt(ctx
, length
, dst
, src
);
161 static void des3_decrypt_wrapper(const void *ctx
, size_t length
,
162 uint8_t *dst
, const uint8_t *src
)
164 des3_decrypt(ctx
, length
, dst
, src
);
167 static void cast128_encrypt_wrapper(const void *ctx
, size_t length
,
168 uint8_t *dst
, const uint8_t *src
)
170 cast128_encrypt(ctx
, length
, dst
, src
);
173 static void cast128_decrypt_wrapper(const void *ctx
, size_t length
,
174 uint8_t *dst
, const uint8_t *src
)
176 cast128_decrypt(ctx
, length
, dst
, src
);
179 static void serpent_encrypt_wrapper(const void *ctx
, size_t length
,
180 uint8_t *dst
, const uint8_t *src
)
182 serpent_encrypt(ctx
, length
, dst
, src
);
185 static void serpent_decrypt_wrapper(const void *ctx
, size_t length
,
186 uint8_t *dst
, const uint8_t *src
)
188 serpent_decrypt(ctx
, length
, dst
, src
);
191 static void twofish_encrypt_wrapper(const void *ctx
, size_t length
,
192 uint8_t *dst
, const uint8_t *src
)
194 twofish_encrypt(ctx
, length
, dst
, src
);
197 static void twofish_decrypt_wrapper(const void *ctx
, size_t length
,
198 uint8_t *dst
, const uint8_t *src
)
200 twofish_decrypt(ctx
, length
, dst
, src
);
203 typedef struct QCryptoCipherNettle QCryptoCipherNettle
;
204 struct QCryptoCipherNettle
{
205 /* Primary cipher context for all modes */
207 /* Second cipher context for XTS mode only */
209 /* Cipher callbacks for both contexts */
210 QCryptoCipherNettleFuncNative alg_encrypt_native
;
211 QCryptoCipherNettleFuncNative alg_decrypt_native
;
212 QCryptoCipherNettleFuncWrapper alg_encrypt_wrapper
;
213 QCryptoCipherNettleFuncWrapper alg_decrypt_wrapper
;
214 /* Initialization vector or Counter */
219 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg
,
220 QCryptoCipherMode mode
)
223 case QCRYPTO_CIPHER_ALG_DES_RFB
:
224 case QCRYPTO_CIPHER_ALG_3DES
:
225 case QCRYPTO_CIPHER_ALG_AES_128
:
226 case QCRYPTO_CIPHER_ALG_AES_192
:
227 case QCRYPTO_CIPHER_ALG_AES_256
:
228 case QCRYPTO_CIPHER_ALG_CAST5_128
:
229 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
230 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
231 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
232 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
233 case QCRYPTO_CIPHER_ALG_TWOFISH_192
:
234 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
241 case QCRYPTO_CIPHER_MODE_ECB
:
242 case QCRYPTO_CIPHER_MODE_CBC
:
243 case QCRYPTO_CIPHER_MODE_XTS
:
244 case QCRYPTO_CIPHER_MODE_CTR
:
252 static void nettle_cipher_free_ctx(QCryptoCipherNettle
*ctx
)
260 g_free(ctx
->ctx_tweak
);
265 QCryptoCipher
*qcrypto_cipher_new(QCryptoCipherAlgorithm alg
,
266 QCryptoCipherMode mode
,
267 const uint8_t *key
, size_t nkey
,
270 QCryptoCipher
*cipher
;
271 QCryptoCipherNettle
*ctx
;
275 case QCRYPTO_CIPHER_MODE_ECB
:
276 case QCRYPTO_CIPHER_MODE_CBC
:
277 case QCRYPTO_CIPHER_MODE_XTS
:
278 case QCRYPTO_CIPHER_MODE_CTR
:
281 error_setg(errp
, "Unsupported cipher mode %s",
282 QCryptoCipherMode_lookup
[mode
]);
286 if (!qcrypto_cipher_validate_key_length(alg
, mode
, nkey
, errp
)) {
290 cipher
= g_new0(QCryptoCipher
, 1);
294 ctx
= g_new0(QCryptoCipherNettle
, 1);
295 cipher
->opaque
= ctx
;
298 case QCRYPTO_CIPHER_ALG_DES_RFB
:
299 ctx
->ctx
= g_new0(struct des_ctx
, 1);
300 rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
301 des_set_key(ctx
->ctx
, rfbkey
);
304 ctx
->alg_encrypt_native
= des_encrypt_native
;
305 ctx
->alg_decrypt_native
= des_decrypt_native
;
306 ctx
->alg_encrypt_wrapper
= des_encrypt_wrapper
;
307 ctx
->alg_decrypt_wrapper
= des_decrypt_wrapper
;
309 ctx
->blocksize
= DES_BLOCK_SIZE
;
312 case QCRYPTO_CIPHER_ALG_3DES
:
313 ctx
->ctx
= g_new0(struct des3_ctx
, 1);
314 des3_set_key(ctx
->ctx
, key
);
316 ctx
->alg_encrypt_native
= des3_encrypt_native
;
317 ctx
->alg_decrypt_native
= des3_decrypt_native
;
318 ctx
->alg_encrypt_wrapper
= des3_encrypt_wrapper
;
319 ctx
->alg_decrypt_wrapper
= des3_decrypt_wrapper
;
321 ctx
->blocksize
= DES3_BLOCK_SIZE
;
324 case QCRYPTO_CIPHER_ALG_AES_128
:
325 case QCRYPTO_CIPHER_ALG_AES_192
:
326 case QCRYPTO_CIPHER_ALG_AES_256
:
327 ctx
->ctx
= g_new0(QCryptoNettleAES
, 1);
329 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
330 ctx
->ctx_tweak
= g_new0(QCryptoNettleAES
, 1);
333 aes_set_encrypt_key(&((QCryptoNettleAES
*)ctx
->ctx
)->enc
,
335 aes_set_decrypt_key(&((QCryptoNettleAES
*)ctx
->ctx
)->dec
,
338 aes_set_encrypt_key(&((QCryptoNettleAES
*)ctx
->ctx_tweak
)->enc
,
340 aes_set_decrypt_key(&((QCryptoNettleAES
*)ctx
->ctx_tweak
)->dec
,
343 aes_set_encrypt_key(&((QCryptoNettleAES
*)ctx
->ctx
)->enc
,
345 aes_set_decrypt_key(&((QCryptoNettleAES
*)ctx
->ctx
)->dec
,
349 ctx
->alg_encrypt_native
= aes_encrypt_native
;
350 ctx
->alg_decrypt_native
= aes_decrypt_native
;
351 ctx
->alg_encrypt_wrapper
= aes_encrypt_wrapper
;
352 ctx
->alg_decrypt_wrapper
= aes_decrypt_wrapper
;
354 ctx
->blocksize
= AES_BLOCK_SIZE
;
357 case QCRYPTO_CIPHER_ALG_CAST5_128
:
358 ctx
->ctx
= g_new0(struct cast128_ctx
, 1);
360 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
361 ctx
->ctx_tweak
= g_new0(struct cast128_ctx
, 1);
364 cast5_set_key(ctx
->ctx
, nkey
, key
);
365 cast5_set_key(ctx
->ctx_tweak
, nkey
, key
+ nkey
);
367 cast5_set_key(ctx
->ctx
, nkey
, key
);
370 ctx
->alg_encrypt_native
= cast128_encrypt_native
;
371 ctx
->alg_decrypt_native
= cast128_decrypt_native
;
372 ctx
->alg_encrypt_wrapper
= cast128_encrypt_wrapper
;
373 ctx
->alg_decrypt_wrapper
= cast128_decrypt_wrapper
;
375 ctx
->blocksize
= CAST128_BLOCK_SIZE
;
378 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
379 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
380 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
381 ctx
->ctx
= g_new0(struct serpent_ctx
, 1);
383 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
384 ctx
->ctx_tweak
= g_new0(struct serpent_ctx
, 1);
387 serpent_set_key(ctx
->ctx
, nkey
, key
);
388 serpent_set_key(ctx
->ctx_tweak
, nkey
, key
+ nkey
);
390 serpent_set_key(ctx
->ctx
, nkey
, key
);
393 ctx
->alg_encrypt_native
= serpent_encrypt_native
;
394 ctx
->alg_decrypt_native
= serpent_decrypt_native
;
395 ctx
->alg_encrypt_wrapper
= serpent_encrypt_wrapper
;
396 ctx
->alg_decrypt_wrapper
= serpent_decrypt_wrapper
;
398 ctx
->blocksize
= SERPENT_BLOCK_SIZE
;
401 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
402 case QCRYPTO_CIPHER_ALG_TWOFISH_192
:
403 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
404 ctx
->ctx
= g_new0(struct twofish_ctx
, 1);
406 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
407 ctx
->ctx_tweak
= g_new0(struct twofish_ctx
, 1);
410 twofish_set_key(ctx
->ctx
, nkey
, key
);
411 twofish_set_key(ctx
->ctx_tweak
, nkey
, key
+ nkey
);
413 twofish_set_key(ctx
->ctx
, nkey
, key
);
416 ctx
->alg_encrypt_native
= twofish_encrypt_native
;
417 ctx
->alg_decrypt_native
= twofish_decrypt_native
;
418 ctx
->alg_encrypt_wrapper
= twofish_encrypt_wrapper
;
419 ctx
->alg_decrypt_wrapper
= twofish_decrypt_wrapper
;
421 ctx
->blocksize
= TWOFISH_BLOCK_SIZE
;
425 error_setg(errp
, "Unsupported cipher algorithm %s",
426 QCryptoCipherAlgorithm_lookup
[alg
]);
430 if (mode
== QCRYPTO_CIPHER_MODE_XTS
&&
431 ctx
->blocksize
!= XTS_BLOCK_SIZE
) {
432 error_setg(errp
, "Cipher block size %zu must equal XTS block size %d",
433 ctx
->blocksize
, XTS_BLOCK_SIZE
);
437 ctx
->iv
= g_new0(uint8_t, ctx
->blocksize
);
442 qcrypto_cipher_free(cipher
);
447 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
449 QCryptoCipherNettle
*ctx
;
455 ctx
= cipher
->opaque
;
456 nettle_cipher_free_ctx(ctx
);
461 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
467 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
469 if (len
% ctx
->blocksize
) {
470 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
471 len
, ctx
->blocksize
);
475 switch (cipher
->mode
) {
476 case QCRYPTO_CIPHER_MODE_ECB
:
477 ctx
->alg_encrypt_wrapper(ctx
->ctx
, len
, out
, in
);
480 case QCRYPTO_CIPHER_MODE_CBC
:
481 cbc_encrypt(ctx
->ctx
, ctx
->alg_encrypt_native
,
482 ctx
->blocksize
, ctx
->iv
,
486 case QCRYPTO_CIPHER_MODE_XTS
:
487 xts_encrypt(ctx
->ctx
, ctx
->ctx_tweak
,
488 ctx
->alg_encrypt_wrapper
, ctx
->alg_encrypt_wrapper
,
489 ctx
->iv
, len
, out
, in
);
492 case QCRYPTO_CIPHER_MODE_CTR
:
493 ctr_crypt(ctx
->ctx
, ctx
->alg_encrypt_native
,
494 ctx
->blocksize
, ctx
->iv
,
499 error_setg(errp
, "Unsupported cipher mode %s",
500 QCryptoCipherMode_lookup
[cipher
->mode
]);
507 int qcrypto_cipher_decrypt(QCryptoCipher
*cipher
,
513 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
515 if (len
% ctx
->blocksize
) {
516 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
517 len
, ctx
->blocksize
);
521 switch (cipher
->mode
) {
522 case QCRYPTO_CIPHER_MODE_ECB
:
523 ctx
->alg_decrypt_wrapper(ctx
->ctx
, len
, out
, in
);
526 case QCRYPTO_CIPHER_MODE_CBC
:
527 cbc_decrypt(ctx
->ctx
, ctx
->alg_decrypt_native
,
528 ctx
->blocksize
, ctx
->iv
,
532 case QCRYPTO_CIPHER_MODE_XTS
:
533 xts_decrypt(ctx
->ctx
, ctx
->ctx_tweak
,
534 ctx
->alg_encrypt_wrapper
, ctx
->alg_decrypt_wrapper
,
535 ctx
->iv
, len
, out
, in
);
537 case QCRYPTO_CIPHER_MODE_CTR
:
538 ctr_crypt(ctx
->ctx
, ctx
->alg_encrypt_native
,
539 ctx
->blocksize
, ctx
->iv
,
544 error_setg(errp
, "Unsupported cipher mode %s",
545 QCryptoCipherMode_lookup
[cipher
->mode
]);
551 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
552 const uint8_t *iv
, size_t niv
,
555 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
556 if (niv
!= ctx
->blocksize
) {
557 error_setg(errp
, "Expected IV size %zu not %zu",
558 ctx
->blocksize
, niv
);
561 memcpy(ctx
->iv
, iv
, niv
);