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>
32 typedef void (*QCryptoCipherNettleFuncWrapper
)(const void *ctx
,
37 #if CONFIG_NETTLE_VERSION_MAJOR < 3
38 typedef nettle_crypt_func
* QCryptoCipherNettleFuncNative
;
39 typedef void * cipher_ctx_t
;
40 typedef unsigned cipher_length_t
;
42 #define cast5_set_key cast128_set_key
44 typedef nettle_cipher_func
* QCryptoCipherNettleFuncNative
;
45 typedef const void * cipher_ctx_t
;
46 typedef size_t cipher_length_t
;
49 typedef struct QCryptoNettleAES
{
54 static void aes_encrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
55 uint8_t *dst
, const uint8_t *src
)
57 const QCryptoNettleAES
*aesctx
= ctx
;
58 aes_encrypt(&aesctx
->enc
, length
, dst
, src
);
61 static void aes_decrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
62 uint8_t *dst
, const uint8_t *src
)
64 const QCryptoNettleAES
*aesctx
= ctx
;
65 aes_decrypt(&aesctx
->dec
, length
, dst
, src
);
68 static void des_encrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
69 uint8_t *dst
, const uint8_t *src
)
71 des_encrypt(ctx
, length
, dst
, src
);
74 static void des_decrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
75 uint8_t *dst
, const uint8_t *src
)
77 des_decrypt(ctx
, length
, dst
, src
);
80 static void cast128_encrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
81 uint8_t *dst
, const uint8_t *src
)
83 cast128_encrypt(ctx
, length
, dst
, src
);
86 static void cast128_decrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
87 uint8_t *dst
, const uint8_t *src
)
89 cast128_decrypt(ctx
, length
, dst
, src
);
92 static void serpent_encrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
93 uint8_t *dst
, const uint8_t *src
)
95 serpent_encrypt(ctx
, length
, dst
, src
);
98 static void serpent_decrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
99 uint8_t *dst
, const uint8_t *src
)
101 serpent_decrypt(ctx
, length
, dst
, src
);
104 static void twofish_encrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
105 uint8_t *dst
, const uint8_t *src
)
107 twofish_encrypt(ctx
, length
, dst
, src
);
110 static void twofish_decrypt_native(cipher_ctx_t ctx
, cipher_length_t length
,
111 uint8_t *dst
, const uint8_t *src
)
113 twofish_decrypt(ctx
, length
, dst
, src
);
116 static void aes_encrypt_wrapper(const void *ctx
, size_t length
,
117 uint8_t *dst
, const uint8_t *src
)
119 const QCryptoNettleAES
*aesctx
= ctx
;
120 aes_encrypt(&aesctx
->enc
, length
, dst
, src
);
123 static void aes_decrypt_wrapper(const void *ctx
, size_t length
,
124 uint8_t *dst
, const uint8_t *src
)
126 const QCryptoNettleAES
*aesctx
= ctx
;
127 aes_decrypt(&aesctx
->dec
, length
, dst
, src
);
130 static void des_encrypt_wrapper(const void *ctx
, size_t length
,
131 uint8_t *dst
, const uint8_t *src
)
133 des_encrypt(ctx
, length
, dst
, src
);
136 static void des_decrypt_wrapper(const void *ctx
, size_t length
,
137 uint8_t *dst
, const uint8_t *src
)
139 des_decrypt(ctx
, length
, dst
, src
);
142 static void cast128_encrypt_wrapper(const void *ctx
, size_t length
,
143 uint8_t *dst
, const uint8_t *src
)
145 cast128_encrypt(ctx
, length
, dst
, src
);
148 static void cast128_decrypt_wrapper(const void *ctx
, size_t length
,
149 uint8_t *dst
, const uint8_t *src
)
151 cast128_decrypt(ctx
, length
, dst
, src
);
154 static void serpent_encrypt_wrapper(const void *ctx
, size_t length
,
155 uint8_t *dst
, const uint8_t *src
)
157 serpent_encrypt(ctx
, length
, dst
, src
);
160 static void serpent_decrypt_wrapper(const void *ctx
, size_t length
,
161 uint8_t *dst
, const uint8_t *src
)
163 serpent_decrypt(ctx
, length
, dst
, src
);
166 static void twofish_encrypt_wrapper(const void *ctx
, size_t length
,
167 uint8_t *dst
, const uint8_t *src
)
169 twofish_encrypt(ctx
, length
, dst
, src
);
172 static void twofish_decrypt_wrapper(const void *ctx
, size_t length
,
173 uint8_t *dst
, const uint8_t *src
)
175 twofish_decrypt(ctx
, length
, dst
, src
);
178 typedef struct QCryptoCipherNettle QCryptoCipherNettle
;
179 struct QCryptoCipherNettle
{
180 /* Primary cipher context for all modes */
182 /* Second cipher context for XTS mode only */
184 /* Cipher callbacks for both contexts */
185 QCryptoCipherNettleFuncNative alg_encrypt_native
;
186 QCryptoCipherNettleFuncNative alg_decrypt_native
;
187 QCryptoCipherNettleFuncWrapper alg_encrypt_wrapper
;
188 QCryptoCipherNettleFuncWrapper alg_decrypt_wrapper
;
194 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg
,
195 QCryptoCipherMode mode
)
198 case QCRYPTO_CIPHER_ALG_DES_RFB
:
199 case QCRYPTO_CIPHER_ALG_AES_128
:
200 case QCRYPTO_CIPHER_ALG_AES_192
:
201 case QCRYPTO_CIPHER_ALG_AES_256
:
202 case QCRYPTO_CIPHER_ALG_CAST5_128
:
203 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
204 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
205 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
206 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
207 case QCRYPTO_CIPHER_ALG_TWOFISH_192
:
208 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
215 case QCRYPTO_CIPHER_MODE_ECB
:
216 case QCRYPTO_CIPHER_MODE_CBC
:
217 case QCRYPTO_CIPHER_MODE_XTS
:
218 case QCRYPTO_CIPHER_MODE_CTR
:
226 QCryptoCipher
*qcrypto_cipher_new(QCryptoCipherAlgorithm alg
,
227 QCryptoCipherMode mode
,
228 const uint8_t *key
, size_t nkey
,
231 QCryptoCipher
*cipher
;
232 QCryptoCipherNettle
*ctx
;
236 case QCRYPTO_CIPHER_MODE_ECB
:
237 case QCRYPTO_CIPHER_MODE_CBC
:
238 case QCRYPTO_CIPHER_MODE_XTS
:
241 error_setg(errp
, "Unsupported cipher mode %s",
242 QCryptoCipherMode_lookup
[mode
]);
246 if (!qcrypto_cipher_validate_key_length(alg
, mode
, nkey
, errp
)) {
250 cipher
= g_new0(QCryptoCipher
, 1);
254 ctx
= g_new0(QCryptoCipherNettle
, 1);
257 case QCRYPTO_CIPHER_ALG_DES_RFB
:
258 ctx
->ctx
= g_new0(struct des_ctx
, 1);
259 rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
260 des_set_key(ctx
->ctx
, rfbkey
);
263 ctx
->alg_encrypt_native
= des_encrypt_native
;
264 ctx
->alg_decrypt_native
= des_decrypt_native
;
265 ctx
->alg_encrypt_wrapper
= des_encrypt_wrapper
;
266 ctx
->alg_decrypt_wrapper
= des_decrypt_wrapper
;
268 ctx
->blocksize
= DES_BLOCK_SIZE
;
271 case QCRYPTO_CIPHER_ALG_AES_128
:
272 case QCRYPTO_CIPHER_ALG_AES_192
:
273 case QCRYPTO_CIPHER_ALG_AES_256
:
274 ctx
->ctx
= g_new0(QCryptoNettleAES
, 1);
276 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
277 ctx
->ctx_tweak
= g_new0(QCryptoNettleAES
, 1);
280 aes_set_encrypt_key(&((QCryptoNettleAES
*)ctx
->ctx
)->enc
,
282 aes_set_decrypt_key(&((QCryptoNettleAES
*)ctx
->ctx
)->dec
,
285 aes_set_encrypt_key(&((QCryptoNettleAES
*)ctx
->ctx_tweak
)->enc
,
287 aes_set_decrypt_key(&((QCryptoNettleAES
*)ctx
->ctx_tweak
)->dec
,
290 aes_set_encrypt_key(&((QCryptoNettleAES
*)ctx
->ctx
)->enc
,
292 aes_set_decrypt_key(&((QCryptoNettleAES
*)ctx
->ctx
)->dec
,
296 ctx
->alg_encrypt_native
= aes_encrypt_native
;
297 ctx
->alg_decrypt_native
= aes_decrypt_native
;
298 ctx
->alg_encrypt_wrapper
= aes_encrypt_wrapper
;
299 ctx
->alg_decrypt_wrapper
= aes_decrypt_wrapper
;
301 ctx
->blocksize
= AES_BLOCK_SIZE
;
304 case QCRYPTO_CIPHER_ALG_CAST5_128
:
305 ctx
->ctx
= g_new0(struct cast128_ctx
, 1);
307 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
308 ctx
->ctx_tweak
= g_new0(struct cast128_ctx
, 1);
311 cast5_set_key(ctx
->ctx
, nkey
, key
);
312 cast5_set_key(ctx
->ctx_tweak
, nkey
, key
+ nkey
);
314 cast5_set_key(ctx
->ctx
, nkey
, key
);
317 ctx
->alg_encrypt_native
= cast128_encrypt_native
;
318 ctx
->alg_decrypt_native
= cast128_decrypt_native
;
319 ctx
->alg_encrypt_wrapper
= cast128_encrypt_wrapper
;
320 ctx
->alg_decrypt_wrapper
= cast128_decrypt_wrapper
;
322 ctx
->blocksize
= CAST128_BLOCK_SIZE
;
325 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
326 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
327 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
328 ctx
->ctx
= g_new0(struct serpent_ctx
, 1);
330 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
331 ctx
->ctx_tweak
= g_new0(struct serpent_ctx
, 1);
334 serpent_set_key(ctx
->ctx
, nkey
, key
);
335 serpent_set_key(ctx
->ctx_tweak
, nkey
, key
+ nkey
);
337 serpent_set_key(ctx
->ctx
, nkey
, key
);
340 ctx
->alg_encrypt_native
= serpent_encrypt_native
;
341 ctx
->alg_decrypt_native
= serpent_decrypt_native
;
342 ctx
->alg_encrypt_wrapper
= serpent_encrypt_wrapper
;
343 ctx
->alg_decrypt_wrapper
= serpent_decrypt_wrapper
;
345 ctx
->blocksize
= SERPENT_BLOCK_SIZE
;
348 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
349 case QCRYPTO_CIPHER_ALG_TWOFISH_192
:
350 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
351 ctx
->ctx
= g_new0(struct twofish_ctx
, 1);
353 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
354 ctx
->ctx_tweak
= g_new0(struct twofish_ctx
, 1);
357 twofish_set_key(ctx
->ctx
, nkey
, key
);
358 twofish_set_key(ctx
->ctx_tweak
, nkey
, key
+ nkey
);
360 twofish_set_key(ctx
->ctx
, nkey
, key
);
363 ctx
->alg_encrypt_native
= twofish_encrypt_native
;
364 ctx
->alg_decrypt_native
= twofish_decrypt_native
;
365 ctx
->alg_encrypt_wrapper
= twofish_encrypt_wrapper
;
366 ctx
->alg_decrypt_wrapper
= twofish_decrypt_wrapper
;
368 ctx
->blocksize
= TWOFISH_BLOCK_SIZE
;
372 error_setg(errp
, "Unsupported cipher algorithm %s",
373 QCryptoCipherAlgorithm_lookup
[alg
]);
377 if (mode
== QCRYPTO_CIPHER_MODE_XTS
&&
378 ctx
->blocksize
!= XTS_BLOCK_SIZE
) {
379 error_setg(errp
, "Cipher block size %zu must equal XTS block size %d",
380 ctx
->blocksize
, XTS_BLOCK_SIZE
);
384 ctx
->iv
= g_new0(uint8_t, ctx
->blocksize
);
385 cipher
->opaque
= ctx
;
396 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
398 QCryptoCipherNettle
*ctx
;
404 ctx
= cipher
->opaque
;
407 g_free(ctx
->ctx_tweak
);
413 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
419 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
421 if (len
% ctx
->blocksize
) {
422 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
423 len
, ctx
->blocksize
);
427 switch (cipher
->mode
) {
428 case QCRYPTO_CIPHER_MODE_ECB
:
429 ctx
->alg_encrypt_wrapper(ctx
->ctx
, len
, out
, in
);
432 case QCRYPTO_CIPHER_MODE_CBC
:
433 cbc_encrypt(ctx
->ctx
, ctx
->alg_encrypt_native
,
434 ctx
->blocksize
, ctx
->iv
,
438 case QCRYPTO_CIPHER_MODE_XTS
:
439 xts_encrypt(ctx
->ctx
, ctx
->ctx_tweak
,
440 ctx
->alg_encrypt_wrapper
, ctx
->alg_encrypt_wrapper
,
441 ctx
->iv
, len
, out
, in
);
445 error_setg(errp
, "Unsupported cipher mode %s",
446 QCryptoCipherMode_lookup
[cipher
->mode
]);
453 int qcrypto_cipher_decrypt(QCryptoCipher
*cipher
,
459 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
461 if (len
% ctx
->blocksize
) {
462 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
463 len
, ctx
->blocksize
);
467 switch (cipher
->mode
) {
468 case QCRYPTO_CIPHER_MODE_ECB
:
469 ctx
->alg_decrypt_wrapper(ctx
->ctx
, len
, out
, in
);
472 case QCRYPTO_CIPHER_MODE_CBC
:
473 cbc_decrypt(ctx
->ctx
, ctx
->alg_decrypt_native
,
474 ctx
->blocksize
, ctx
->iv
,
478 case QCRYPTO_CIPHER_MODE_XTS
:
479 xts_decrypt(ctx
->ctx
, ctx
->ctx_tweak
,
480 ctx
->alg_encrypt_wrapper
, ctx
->alg_decrypt_wrapper
,
481 ctx
->iv
, len
, out
, in
);
485 error_setg(errp
, "Unsupported cipher mode %s",
486 QCryptoCipherMode_lookup
[cipher
->mode
]);
492 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
493 const uint8_t *iv
, size_t niv
,
496 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
497 if (niv
!= ctx
->blocksize
) {
498 error_setg(errp
, "Expected IV size %zu not %zu",
499 ctx
->blocksize
, niv
);
502 memcpy(ctx
->iv
, iv
, niv
);