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
)
30 case QCRYPTO_CIPHER_ALG_DES_RFB
:
31 case QCRYPTO_CIPHER_ALG_AES_128
:
32 case QCRYPTO_CIPHER_ALG_AES_192
:
33 case QCRYPTO_CIPHER_ALG_AES_256
:
34 case QCRYPTO_CIPHER_ALG_CAST5_128
:
35 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
36 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
37 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
38 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
39 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
46 typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt
;
47 struct QCryptoCipherGcrypt
{
48 gcry_cipher_hd_t handle
;
49 gcry_cipher_hd_t tweakhandle
;
54 QCryptoCipher
*qcrypto_cipher_new(QCryptoCipherAlgorithm alg
,
55 QCryptoCipherMode mode
,
56 const uint8_t *key
, size_t nkey
,
59 QCryptoCipher
*cipher
;
60 QCryptoCipherGcrypt
*ctx
;
62 int gcryalg
, gcrymode
;
65 case QCRYPTO_CIPHER_MODE_ECB
:
66 case QCRYPTO_CIPHER_MODE_XTS
:
67 gcrymode
= GCRY_CIPHER_MODE_ECB
;
69 case QCRYPTO_CIPHER_MODE_CBC
:
70 gcrymode
= GCRY_CIPHER_MODE_CBC
;
73 error_setg(errp
, "Unsupported cipher mode %d", mode
);
77 if (!qcrypto_cipher_validate_key_length(alg
, mode
, nkey
, errp
)) {
82 case QCRYPTO_CIPHER_ALG_DES_RFB
:
83 gcryalg
= GCRY_CIPHER_DES
;
86 case QCRYPTO_CIPHER_ALG_AES_128
:
87 gcryalg
= GCRY_CIPHER_AES128
;
90 case QCRYPTO_CIPHER_ALG_AES_192
:
91 gcryalg
= GCRY_CIPHER_AES192
;
94 case QCRYPTO_CIPHER_ALG_AES_256
:
95 gcryalg
= GCRY_CIPHER_AES256
;
98 case QCRYPTO_CIPHER_ALG_CAST5_128
:
99 gcryalg
= GCRY_CIPHER_CAST5
;
102 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
103 gcryalg
= GCRY_CIPHER_SERPENT128
;
106 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
107 gcryalg
= GCRY_CIPHER_SERPENT192
;
110 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
111 gcryalg
= GCRY_CIPHER_SERPENT256
;
114 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
115 gcryalg
= GCRY_CIPHER_TWOFISH128
;
118 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
119 gcryalg
= GCRY_CIPHER_TWOFISH
;
123 error_setg(errp
, "Unsupported cipher algorithm %d", alg
);
127 cipher
= g_new0(QCryptoCipher
, 1);
131 ctx
= g_new0(QCryptoCipherGcrypt
, 1);
133 err
= gcry_cipher_open(&ctx
->handle
, gcryalg
, gcrymode
, 0);
135 error_setg(errp
, "Cannot initialize cipher: %s",
139 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
140 err
= gcry_cipher_open(&ctx
->tweakhandle
, gcryalg
, gcrymode
, 0);
142 error_setg(errp
, "Cannot initialize cipher: %s",
148 if (cipher
->alg
== QCRYPTO_CIPHER_ALG_DES_RFB
) {
149 /* We're using standard DES cipher from gcrypt, so we need
150 * to munge the key so that the results are the same as the
151 * bizarre RFB variant of DES :-)
153 uint8_t *rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
154 err
= gcry_cipher_setkey(ctx
->handle
, rfbkey
, nkey
);
158 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
160 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
162 error_setg(errp
, "Cannot set key: %s",
166 err
= gcry_cipher_setkey(ctx
->tweakhandle
, key
+ nkey
, nkey
);
168 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
171 error_setg(errp
, "Cannot set key: %s",
175 switch (cipher
->alg
) {
176 case QCRYPTO_CIPHER_ALG_AES_128
:
177 case QCRYPTO_CIPHER_ALG_AES_192
:
178 case QCRYPTO_CIPHER_ALG_AES_256
:
179 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
180 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
181 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
182 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
183 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
186 case QCRYPTO_CIPHER_ALG_CAST5_128
:
190 g_assert_not_reached();
194 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
195 ctx
->iv
= g_new0(uint8_t, ctx
->blocksize
);
198 cipher
->opaque
= ctx
;
202 gcry_cipher_close(ctx
->handle
);
203 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
204 gcry_cipher_close(ctx
->tweakhandle
);
212 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
214 QCryptoCipherGcrypt
*ctx
;
218 ctx
= cipher
->opaque
;
219 gcry_cipher_close(ctx
->handle
);
220 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
221 gcry_cipher_close(ctx
->tweakhandle
);
229 static void qcrypto_gcrypt_xts_encrypt(const void *ctx
,
235 err
= gcry_cipher_encrypt((gcry_cipher_hd_t
)ctx
, dst
, length
, src
, length
);
239 static void qcrypto_gcrypt_xts_decrypt(const void *ctx
,
245 err
= gcry_cipher_decrypt((gcry_cipher_hd_t
)ctx
, dst
, length
, src
, length
);
249 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
255 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
258 if (len
% ctx
->blocksize
) {
259 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
260 len
, ctx
->blocksize
);
264 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
265 xts_encrypt(ctx
->handle
, ctx
->tweakhandle
,
266 qcrypto_gcrypt_xts_encrypt
,
267 qcrypto_gcrypt_xts_decrypt
,
268 ctx
->iv
, len
, out
, in
);
270 err
= gcry_cipher_encrypt(ctx
->handle
,
274 error_setg(errp
, "Cannot encrypt data: %s",
284 int qcrypto_cipher_decrypt(QCryptoCipher
*cipher
,
290 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
293 if (len
% ctx
->blocksize
) {
294 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
295 len
, ctx
->blocksize
);
299 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
300 xts_decrypt(ctx
->handle
, ctx
->tweakhandle
,
301 qcrypto_gcrypt_xts_encrypt
,
302 qcrypto_gcrypt_xts_decrypt
,
303 ctx
->iv
, len
, out
, in
);
305 err
= gcry_cipher_decrypt(ctx
->handle
,
309 error_setg(errp
, "Cannot decrypt data: %s",
318 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
319 const uint8_t *iv
, size_t niv
,
322 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
325 if (niv
!= ctx
->blocksize
) {
326 error_setg(errp
, "Expected IV size %zu not %zu",
327 ctx
->blocksize
, niv
);
332 memcpy(ctx
->iv
, iv
, niv
);
334 gcry_cipher_reset(ctx
->handle
);
335 err
= gcry_cipher_setiv(ctx
->handle
, iv
, niv
);
337 error_setg(errp
, "Cannot set IV: %s",