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 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"
23 #include "cipherpriv.h"
28 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg
,
29 QCryptoCipherMode mode
)
32 case QCRYPTO_CIPHER_ALG_DES_RFB
:
33 case QCRYPTO_CIPHER_ALG_3DES
:
34 case QCRYPTO_CIPHER_ALG_AES_128
:
35 case QCRYPTO_CIPHER_ALG_AES_192
:
36 case QCRYPTO_CIPHER_ALG_AES_256
:
37 case QCRYPTO_CIPHER_ALG_CAST5_128
:
38 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
39 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
40 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
41 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
42 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
49 case QCRYPTO_CIPHER_MODE_ECB
:
50 case QCRYPTO_CIPHER_MODE_CBC
:
51 case QCRYPTO_CIPHER_MODE_XTS
:
52 case QCRYPTO_CIPHER_MODE_CTR
:
59 typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt
;
60 struct QCryptoCipherGcrypt
{
61 gcry_cipher_hd_t handle
;
62 gcry_cipher_hd_t tweakhandle
;
64 /* Initialization vector or Counter */
69 qcrypto_gcrypt_cipher_free_ctx(QCryptoCipherGcrypt
*ctx
,
70 QCryptoCipherMode mode
)
76 gcry_cipher_close(ctx
->handle
);
77 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
78 gcry_cipher_close(ctx
->tweakhandle
);
85 static QCryptoCipherGcrypt
*qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg
,
86 QCryptoCipherMode mode
,
91 QCryptoCipherGcrypt
*ctx
;
93 int gcryalg
, gcrymode
;
96 case QCRYPTO_CIPHER_MODE_ECB
:
97 case QCRYPTO_CIPHER_MODE_XTS
:
98 gcrymode
= GCRY_CIPHER_MODE_ECB
;
100 case QCRYPTO_CIPHER_MODE_CBC
:
101 gcrymode
= GCRY_CIPHER_MODE_CBC
;
103 case QCRYPTO_CIPHER_MODE_CTR
:
104 gcrymode
= GCRY_CIPHER_MODE_CTR
;
107 error_setg(errp
, "Unsupported cipher mode %s",
108 QCryptoCipherMode_str(mode
));
112 if (!qcrypto_cipher_validate_key_length(alg
, mode
, nkey
, errp
)) {
117 case QCRYPTO_CIPHER_ALG_DES_RFB
:
118 gcryalg
= GCRY_CIPHER_DES
;
121 case QCRYPTO_CIPHER_ALG_3DES
:
122 gcryalg
= GCRY_CIPHER_3DES
;
125 case QCRYPTO_CIPHER_ALG_AES_128
:
126 gcryalg
= GCRY_CIPHER_AES128
;
129 case QCRYPTO_CIPHER_ALG_AES_192
:
130 gcryalg
= GCRY_CIPHER_AES192
;
133 case QCRYPTO_CIPHER_ALG_AES_256
:
134 gcryalg
= GCRY_CIPHER_AES256
;
137 case QCRYPTO_CIPHER_ALG_CAST5_128
:
138 gcryalg
= GCRY_CIPHER_CAST5
;
141 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
142 gcryalg
= GCRY_CIPHER_SERPENT128
;
145 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
146 gcryalg
= GCRY_CIPHER_SERPENT192
;
149 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
150 gcryalg
= GCRY_CIPHER_SERPENT256
;
153 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
154 gcryalg
= GCRY_CIPHER_TWOFISH128
;
157 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
158 gcryalg
= GCRY_CIPHER_TWOFISH
;
162 error_setg(errp
, "Unsupported cipher algorithm %s",
163 QCryptoCipherAlgorithm_str(alg
));
167 ctx
= g_new0(QCryptoCipherGcrypt
, 1);
169 err
= gcry_cipher_open(&ctx
->handle
, gcryalg
, gcrymode
, 0);
171 error_setg(errp
, "Cannot initialize cipher: %s",
175 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
176 err
= gcry_cipher_open(&ctx
->tweakhandle
, gcryalg
, gcrymode
, 0);
178 error_setg(errp
, "Cannot initialize cipher: %s",
184 if (alg
== QCRYPTO_CIPHER_ALG_DES_RFB
) {
185 /* We're using standard DES cipher from gcrypt, so we need
186 * to munge the key so that the results are the same as the
187 * bizarre RFB variant of DES :-)
189 uint8_t *rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
190 err
= gcry_cipher_setkey(ctx
->handle
, rfbkey
, nkey
);
194 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
196 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
198 error_setg(errp
, "Cannot set key: %s",
202 err
= gcry_cipher_setkey(ctx
->tweakhandle
, key
+ nkey
, nkey
);
204 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
207 error_setg(errp
, "Cannot set key: %s",
212 case QCRYPTO_CIPHER_ALG_AES_128
:
213 case QCRYPTO_CIPHER_ALG_AES_192
:
214 case QCRYPTO_CIPHER_ALG_AES_256
:
215 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
216 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
217 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
218 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
219 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
222 case QCRYPTO_CIPHER_ALG_3DES
:
223 case QCRYPTO_CIPHER_ALG_CAST5_128
:
227 g_assert_not_reached();
231 if (mode
== QCRYPTO_CIPHER_MODE_XTS
) {
232 if (ctx
->blocksize
!= XTS_BLOCK_SIZE
) {
234 "Cipher block size %zu must equal XTS block size %d",
235 ctx
->blocksize
, XTS_BLOCK_SIZE
);
238 ctx
->iv
= g_new0(uint8_t, ctx
->blocksize
);
244 qcrypto_gcrypt_cipher_free_ctx(ctx
, mode
);
250 qcrypto_gcrypt_cipher_ctx_free(QCryptoCipher
*cipher
)
252 qcrypto_gcrypt_cipher_free_ctx(cipher
->opaque
, cipher
->mode
);
256 static void qcrypto_gcrypt_xts_encrypt(const void *ctx
,
262 err
= gcry_cipher_encrypt((gcry_cipher_hd_t
)ctx
, dst
, length
, src
, length
);
266 static void qcrypto_gcrypt_xts_decrypt(const void *ctx
,
272 err
= gcry_cipher_decrypt((gcry_cipher_hd_t
)ctx
, dst
, length
, src
, length
);
277 qcrypto_gcrypt_cipher_encrypt(QCryptoCipher
*cipher
,
283 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
286 if (len
% ctx
->blocksize
) {
287 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
288 len
, ctx
->blocksize
);
292 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
293 xts_encrypt(ctx
->handle
, ctx
->tweakhandle
,
294 qcrypto_gcrypt_xts_encrypt
,
295 qcrypto_gcrypt_xts_decrypt
,
296 ctx
->iv
, len
, out
, in
);
298 err
= gcry_cipher_encrypt(ctx
->handle
,
302 error_setg(errp
, "Cannot encrypt data: %s",
313 qcrypto_gcrypt_cipher_decrypt(QCryptoCipher
*cipher
,
319 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
322 if (len
% ctx
->blocksize
) {
323 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
324 len
, ctx
->blocksize
);
328 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
329 xts_decrypt(ctx
->handle
, ctx
->tweakhandle
,
330 qcrypto_gcrypt_xts_encrypt
,
331 qcrypto_gcrypt_xts_decrypt
,
332 ctx
->iv
, len
, out
, in
);
334 err
= gcry_cipher_decrypt(ctx
->handle
,
338 error_setg(errp
, "Cannot decrypt data: %s",
348 qcrypto_gcrypt_cipher_setiv(QCryptoCipher
*cipher
,
349 const uint8_t *iv
, size_t niv
,
352 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
355 if (niv
!= ctx
->blocksize
) {
356 error_setg(errp
, "Expected IV size %zu not %zu",
357 ctx
->blocksize
, niv
);
362 memcpy(ctx
->iv
, iv
, niv
);
364 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_CTR
) {
365 err
= gcry_cipher_setctr(ctx
->handle
, iv
, niv
);
367 error_setg(errp
, "Cannot set Counter: %s",
372 gcry_cipher_reset(ctx
->handle
);
373 err
= gcry_cipher_setiv(ctx
->handle
, iv
, niv
);
375 error_setg(errp
, "Cannot set IV: %s",
386 static struct QCryptoCipherDriver qcrypto_cipher_lib_driver
= {
387 .cipher_encrypt
= qcrypto_gcrypt_cipher_encrypt
,
388 .cipher_decrypt
= qcrypto_gcrypt_cipher_decrypt
,
389 .cipher_setiv
= qcrypto_gcrypt_cipher_setiv
,
390 .cipher_free
= qcrypto_gcrypt_cipher_ctx_free
,