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"
27 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg
,
28 QCryptoCipherMode mode
)
31 case QCRYPTO_CIPHER_ALG_DES_RFB
:
32 case QCRYPTO_CIPHER_ALG_3DES
:
33 case QCRYPTO_CIPHER_ALG_AES_128
:
34 case QCRYPTO_CIPHER_ALG_AES_192
:
35 case QCRYPTO_CIPHER_ALG_AES_256
:
36 case QCRYPTO_CIPHER_ALG_CAST5_128
:
37 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
38 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
39 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
40 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
41 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
48 case QCRYPTO_CIPHER_MODE_ECB
:
49 case QCRYPTO_CIPHER_MODE_CBC
:
50 case QCRYPTO_CIPHER_MODE_XTS
:
51 case QCRYPTO_CIPHER_MODE_CTR
:
58 typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt
;
59 struct QCryptoCipherGcrypt
{
60 gcry_cipher_hd_t handle
;
61 gcry_cipher_hd_t tweakhandle
;
63 /* Initialization vector or Counter */
67 QCryptoCipher
*qcrypto_cipher_new(QCryptoCipherAlgorithm alg
,
68 QCryptoCipherMode mode
,
69 const uint8_t *key
, size_t nkey
,
72 QCryptoCipher
*cipher
;
73 QCryptoCipherGcrypt
*ctx
;
75 int gcryalg
, gcrymode
;
78 case QCRYPTO_CIPHER_MODE_ECB
:
79 case QCRYPTO_CIPHER_MODE_XTS
:
80 gcrymode
= GCRY_CIPHER_MODE_ECB
;
82 case QCRYPTO_CIPHER_MODE_CBC
:
83 gcrymode
= GCRY_CIPHER_MODE_CBC
;
85 case QCRYPTO_CIPHER_MODE_CTR
:
86 gcrymode
= GCRY_CIPHER_MODE_CTR
;
89 error_setg(errp
, "Unsupported cipher mode %s",
90 QCryptoCipherMode_lookup
[mode
]);
94 if (!qcrypto_cipher_validate_key_length(alg
, mode
, nkey
, errp
)) {
99 case QCRYPTO_CIPHER_ALG_DES_RFB
:
100 gcryalg
= GCRY_CIPHER_DES
;
103 case QCRYPTO_CIPHER_ALG_3DES
:
104 gcryalg
= GCRY_CIPHER_3DES
;
107 case QCRYPTO_CIPHER_ALG_AES_128
:
108 gcryalg
= GCRY_CIPHER_AES128
;
111 case QCRYPTO_CIPHER_ALG_AES_192
:
112 gcryalg
= GCRY_CIPHER_AES192
;
115 case QCRYPTO_CIPHER_ALG_AES_256
:
116 gcryalg
= GCRY_CIPHER_AES256
;
119 case QCRYPTO_CIPHER_ALG_CAST5_128
:
120 gcryalg
= GCRY_CIPHER_CAST5
;
123 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
124 gcryalg
= GCRY_CIPHER_SERPENT128
;
127 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
128 gcryalg
= GCRY_CIPHER_SERPENT192
;
131 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
132 gcryalg
= GCRY_CIPHER_SERPENT256
;
135 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
136 gcryalg
= GCRY_CIPHER_TWOFISH128
;
139 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
140 gcryalg
= GCRY_CIPHER_TWOFISH
;
144 error_setg(errp
, "Unsupported cipher algorithm %s",
145 QCryptoCipherAlgorithm_lookup
[alg
]);
149 cipher
= g_new0(QCryptoCipher
, 1);
153 ctx
= g_new0(QCryptoCipherGcrypt
, 1);
155 err
= gcry_cipher_open(&ctx
->handle
, gcryalg
, gcrymode
, 0);
157 error_setg(errp
, "Cannot initialize cipher: %s",
161 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
162 err
= gcry_cipher_open(&ctx
->tweakhandle
, gcryalg
, gcrymode
, 0);
164 error_setg(errp
, "Cannot initialize cipher: %s",
170 if (cipher
->alg
== QCRYPTO_CIPHER_ALG_DES_RFB
) {
171 /* We're using standard DES cipher from gcrypt, so we need
172 * to munge the key so that the results are the same as the
173 * bizarre RFB variant of DES :-)
175 uint8_t *rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
176 err
= gcry_cipher_setkey(ctx
->handle
, rfbkey
, nkey
);
180 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
182 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
184 error_setg(errp
, "Cannot set key: %s",
188 err
= gcry_cipher_setkey(ctx
->tweakhandle
, key
+ nkey
, nkey
);
190 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
193 error_setg(errp
, "Cannot set key: %s",
197 switch (cipher
->alg
) {
198 case QCRYPTO_CIPHER_ALG_AES_128
:
199 case QCRYPTO_CIPHER_ALG_AES_192
:
200 case QCRYPTO_CIPHER_ALG_AES_256
:
201 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
202 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
203 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
204 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
205 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
208 case QCRYPTO_CIPHER_ALG_3DES
:
209 case QCRYPTO_CIPHER_ALG_CAST5_128
:
213 g_assert_not_reached();
217 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
218 if (ctx
->blocksize
!= XTS_BLOCK_SIZE
) {
220 "Cipher block size %zu must equal XTS block size %d",
221 ctx
->blocksize
, XTS_BLOCK_SIZE
);
224 ctx
->iv
= g_new0(uint8_t, ctx
->blocksize
);
227 cipher
->opaque
= ctx
;
231 gcry_cipher_close(ctx
->handle
);
232 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
233 gcry_cipher_close(ctx
->tweakhandle
);
241 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
243 QCryptoCipherGcrypt
*ctx
;
247 ctx
= cipher
->opaque
;
248 gcry_cipher_close(ctx
->handle
);
249 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
250 gcry_cipher_close(ctx
->tweakhandle
);
258 static void qcrypto_gcrypt_xts_encrypt(const void *ctx
,
264 err
= gcry_cipher_encrypt((gcry_cipher_hd_t
)ctx
, dst
, length
, src
, length
);
268 static void qcrypto_gcrypt_xts_decrypt(const void *ctx
,
274 err
= gcry_cipher_decrypt((gcry_cipher_hd_t
)ctx
, dst
, length
, src
, length
);
278 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
284 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
287 if (len
% ctx
->blocksize
) {
288 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
289 len
, ctx
->blocksize
);
293 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
294 xts_encrypt(ctx
->handle
, ctx
->tweakhandle
,
295 qcrypto_gcrypt_xts_encrypt
,
296 qcrypto_gcrypt_xts_decrypt
,
297 ctx
->iv
, len
, out
, in
);
299 err
= gcry_cipher_encrypt(ctx
->handle
,
303 error_setg(errp
, "Cannot encrypt data: %s",
313 int qcrypto_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",
347 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
348 const uint8_t *iv
, size_t niv
,
351 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
354 if (niv
!= ctx
->blocksize
) {
355 error_setg(errp
, "Expected IV size %zu not %zu",
356 ctx
->blocksize
, niv
);
361 memcpy(ctx
->iv
, iv
, niv
);
363 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_CTR
) {
364 err
= gcry_cipher_setctr(ctx
->handle
, iv
, niv
);
366 error_setg(errp
, "Cannot set Counter: %s",
371 gcry_cipher_reset(ctx
->handle
);
372 err
= gcry_cipher_setiv(ctx
->handle
, iv
, niv
);
374 error_setg(errp
, "Cannot set IV: %s",