2 * QEMU Crypto cipher libgcrypt 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.1 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 #ifdef CONFIG_QEMU_PRIVATE_XTS
23 #include "crypto/xts.h"
25 #include "cipherpriv.h"
30 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg
,
31 QCryptoCipherMode mode
)
34 case QCRYPTO_CIPHER_ALG_DES_RFB
:
35 case QCRYPTO_CIPHER_ALG_3DES
:
36 case QCRYPTO_CIPHER_ALG_AES_128
:
37 case QCRYPTO_CIPHER_ALG_AES_192
:
38 case QCRYPTO_CIPHER_ALG_AES_256
:
39 case QCRYPTO_CIPHER_ALG_CAST5_128
:
40 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
41 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
42 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
43 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
44 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
51 case QCRYPTO_CIPHER_MODE_ECB
:
52 case QCRYPTO_CIPHER_MODE_CBC
:
53 case QCRYPTO_CIPHER_MODE_XTS
:
54 case QCRYPTO_CIPHER_MODE_CTR
:
61 typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt
;
62 struct QCryptoCipherGcrypt
{
63 gcry_cipher_hd_t handle
;
65 #ifdef CONFIG_QEMU_PRIVATE_XTS
66 gcry_cipher_hd_t tweakhandle
;
67 /* Initialization vector or Counter */
73 qcrypto_gcrypt_cipher_free_ctx(QCryptoCipherGcrypt
*ctx
,
74 QCryptoCipherMode mode
)
80 gcry_cipher_close(ctx
->handle
);
81 #ifdef CONFIG_QEMU_PRIVATE_XTS
82 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
83 gcry_cipher_close(ctx
->tweakhandle
);
91 static QCryptoCipherGcrypt
*qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg
,
92 QCryptoCipherMode mode
,
97 QCryptoCipherGcrypt
*ctx
;
99 int gcryalg
, gcrymode
;
102 case QCRYPTO_CIPHER_MODE_ECB
:
103 gcrymode
= GCRY_CIPHER_MODE_ECB
;
105 case QCRYPTO_CIPHER_MODE_XTS
:
106 #ifdef CONFIG_QEMU_PRIVATE_XTS
107 gcrymode
= GCRY_CIPHER_MODE_ECB
;
109 gcrymode
= GCRY_CIPHER_MODE_XTS
;
112 case QCRYPTO_CIPHER_MODE_CBC
:
113 gcrymode
= GCRY_CIPHER_MODE_CBC
;
115 case QCRYPTO_CIPHER_MODE_CTR
:
116 gcrymode
= GCRY_CIPHER_MODE_CTR
;
119 error_setg(errp
, "Unsupported cipher mode %s",
120 QCryptoCipherMode_str(mode
));
124 if (!qcrypto_cipher_validate_key_length(alg
, mode
, nkey
, errp
)) {
129 case QCRYPTO_CIPHER_ALG_DES_RFB
:
130 gcryalg
= GCRY_CIPHER_DES
;
133 case QCRYPTO_CIPHER_ALG_3DES
:
134 gcryalg
= GCRY_CIPHER_3DES
;
137 case QCRYPTO_CIPHER_ALG_AES_128
:
138 gcryalg
= GCRY_CIPHER_AES128
;
141 case QCRYPTO_CIPHER_ALG_AES_192
:
142 gcryalg
= GCRY_CIPHER_AES192
;
145 case QCRYPTO_CIPHER_ALG_AES_256
:
146 gcryalg
= GCRY_CIPHER_AES256
;
149 case QCRYPTO_CIPHER_ALG_CAST5_128
:
150 gcryalg
= GCRY_CIPHER_CAST5
;
153 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
154 gcryalg
= GCRY_CIPHER_SERPENT128
;
157 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
158 gcryalg
= GCRY_CIPHER_SERPENT192
;
161 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
162 gcryalg
= GCRY_CIPHER_SERPENT256
;
165 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
166 gcryalg
= GCRY_CIPHER_TWOFISH128
;
169 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
170 gcryalg
= GCRY_CIPHER_TWOFISH
;
174 error_setg(errp
, "Unsupported cipher algorithm %s",
175 QCryptoCipherAlgorithm_str(alg
));
179 ctx
= g_new0(QCryptoCipherGcrypt
, 1);
181 err
= gcry_cipher_open(&ctx
->handle
, gcryalg
, gcrymode
, 0);
183 error_setg(errp
, "Cannot initialize cipher: %s",
187 #ifdef CONFIG_QEMU_PRIVATE_XTS
188 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
189 err
= gcry_cipher_open(&ctx
->tweakhandle
, gcryalg
, gcrymode
, 0);
191 error_setg(errp
, "Cannot initialize cipher: %s",
198 if (alg
== QCRYPTO_CIPHER_ALG_DES_RFB
) {
199 /* We're using standard DES cipher from gcrypt, so we need
200 * to munge the key so that the results are the same as the
201 * bizarre RFB variant of DES :-)
203 uint8_t *rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
204 err
= gcry_cipher_setkey(ctx
->handle
, rfbkey
, nkey
);
208 #ifdef CONFIG_QEMU_PRIVATE_XTS
209 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
211 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
213 error_setg(errp
, "Cannot set key: %s",
217 err
= gcry_cipher_setkey(ctx
->tweakhandle
, key
+ nkey
, nkey
);
220 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
221 #ifdef CONFIG_QEMU_PRIVATE_XTS
225 error_setg(errp
, "Cannot set key: %s",
230 case QCRYPTO_CIPHER_ALG_AES_128
:
231 case QCRYPTO_CIPHER_ALG_AES_192
:
232 case QCRYPTO_CIPHER_ALG_AES_256
:
233 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
234 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
235 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
236 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
237 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
240 case QCRYPTO_CIPHER_ALG_3DES
:
241 case QCRYPTO_CIPHER_ALG_CAST5_128
:
245 g_assert_not_reached();
249 #ifdef CONFIG_QEMU_PRIVATE_XTS
250 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
251 if (ctx
->blocksize
!= XTS_BLOCK_SIZE
) {
253 "Cipher block size %zu must equal XTS block size %d",
254 ctx
->blocksize
, XTS_BLOCK_SIZE
);
257 ctx
->iv
= g_new0(uint8_t, ctx
->blocksize
);
264 qcrypto_gcrypt_cipher_free_ctx(ctx
, mode
);
270 qcrypto_gcrypt_cipher_ctx_free(QCryptoCipher
*cipher
)
272 qcrypto_gcrypt_cipher_free_ctx(cipher
->opaque
, cipher
->mode
);
276 #ifdef CONFIG_QEMU_PRIVATE_XTS
277 static void qcrypto_gcrypt_xts_encrypt(const void *ctx
,
283 err
= gcry_cipher_encrypt((gcry_cipher_hd_t
)ctx
, dst
, length
, src
, length
);
287 static void qcrypto_gcrypt_xts_decrypt(const void *ctx
,
293 err
= gcry_cipher_decrypt((gcry_cipher_hd_t
)ctx
, dst
, length
, src
, length
);
299 qcrypto_gcrypt_cipher_encrypt(QCryptoCipher
*cipher
,
305 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
308 if (len
% ctx
->blocksize
) {
309 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
310 len
, ctx
->blocksize
);
314 #ifdef CONFIG_QEMU_PRIVATE_XTS
315 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
316 xts_encrypt(ctx
->handle
, ctx
->tweakhandle
,
317 qcrypto_gcrypt_xts_encrypt
,
318 qcrypto_gcrypt_xts_decrypt
,
319 ctx
->iv
, len
, out
, in
);
324 err
= gcry_cipher_encrypt(ctx
->handle
,
328 error_setg(errp
, "Cannot encrypt data: %s",
338 qcrypto_gcrypt_cipher_decrypt(QCryptoCipher
*cipher
,
344 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
347 if (len
% ctx
->blocksize
) {
348 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
349 len
, ctx
->blocksize
);
353 #ifdef CONFIG_QEMU_PRIVATE_XTS
354 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
355 xts_decrypt(ctx
->handle
, ctx
->tweakhandle
,
356 qcrypto_gcrypt_xts_encrypt
,
357 qcrypto_gcrypt_xts_decrypt
,
358 ctx
->iv
, len
, out
, in
);
363 err
= gcry_cipher_decrypt(ctx
->handle
,
367 error_setg(errp
, "Cannot decrypt data: %s",
376 qcrypto_gcrypt_cipher_setiv(QCryptoCipher
*cipher
,
377 const uint8_t *iv
, size_t niv
,
380 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
383 if (niv
!= ctx
->blocksize
) {
384 error_setg(errp
, "Expected IV size %zu not %zu",
385 ctx
->blocksize
, niv
);
389 #ifdef CONFIG_QEMU_PRIVATE_XTS
391 memcpy(ctx
->iv
, iv
, niv
);
396 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_CTR
) {
397 err
= gcry_cipher_setctr(ctx
->handle
, iv
, niv
);
399 error_setg(errp
, "Cannot set Counter: %s",
404 gcry_cipher_reset(ctx
->handle
);
405 err
= gcry_cipher_setiv(ctx
->handle
, iv
, niv
);
407 error_setg(errp
, "Cannot set IV: %s",
417 static struct QCryptoCipherDriver qcrypto_cipher_lib_driver
= {
418 .cipher_encrypt
= qcrypto_gcrypt_cipher_encrypt
,
419 .cipher_decrypt
= qcrypto_gcrypt_cipher_decrypt
,
420 .cipher_setiv
= qcrypto_gcrypt_cipher_setiv
,
421 .cipher_free
= qcrypto_gcrypt_cipher_ctx_free
,