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 %s",
74 QCryptoCipherMode_lookup
[mode
]);
78 if (!qcrypto_cipher_validate_key_length(alg
, mode
, nkey
, errp
)) {
83 case QCRYPTO_CIPHER_ALG_DES_RFB
:
84 gcryalg
= GCRY_CIPHER_DES
;
87 case QCRYPTO_CIPHER_ALG_AES_128
:
88 gcryalg
= GCRY_CIPHER_AES128
;
91 case QCRYPTO_CIPHER_ALG_AES_192
:
92 gcryalg
= GCRY_CIPHER_AES192
;
95 case QCRYPTO_CIPHER_ALG_AES_256
:
96 gcryalg
= GCRY_CIPHER_AES256
;
99 case QCRYPTO_CIPHER_ALG_CAST5_128
:
100 gcryalg
= GCRY_CIPHER_CAST5
;
103 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
104 gcryalg
= GCRY_CIPHER_SERPENT128
;
107 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
108 gcryalg
= GCRY_CIPHER_SERPENT192
;
111 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
112 gcryalg
= GCRY_CIPHER_SERPENT256
;
115 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
116 gcryalg
= GCRY_CIPHER_TWOFISH128
;
119 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
120 gcryalg
= GCRY_CIPHER_TWOFISH
;
124 error_setg(errp
, "Unsupported cipher algorithm %s",
125 QCryptoCipherAlgorithm_lookup
[alg
]);
129 cipher
= g_new0(QCryptoCipher
, 1);
133 ctx
= g_new0(QCryptoCipherGcrypt
, 1);
135 err
= gcry_cipher_open(&ctx
->handle
, gcryalg
, gcrymode
, 0);
137 error_setg(errp
, "Cannot initialize cipher: %s",
141 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
142 err
= gcry_cipher_open(&ctx
->tweakhandle
, gcryalg
, gcrymode
, 0);
144 error_setg(errp
, "Cannot initialize cipher: %s",
150 if (cipher
->alg
== QCRYPTO_CIPHER_ALG_DES_RFB
) {
151 /* We're using standard DES cipher from gcrypt, so we need
152 * to munge the key so that the results are the same as the
153 * bizarre RFB variant of DES :-)
155 uint8_t *rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
156 err
= gcry_cipher_setkey(ctx
->handle
, rfbkey
, nkey
);
160 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
162 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
164 error_setg(errp
, "Cannot set key: %s",
168 err
= gcry_cipher_setkey(ctx
->tweakhandle
, key
+ nkey
, nkey
);
170 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
173 error_setg(errp
, "Cannot set key: %s",
177 switch (cipher
->alg
) {
178 case QCRYPTO_CIPHER_ALG_AES_128
:
179 case QCRYPTO_CIPHER_ALG_AES_192
:
180 case QCRYPTO_CIPHER_ALG_AES_256
:
181 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
182 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
183 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
184 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
185 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
188 case QCRYPTO_CIPHER_ALG_CAST5_128
:
192 g_assert_not_reached();
196 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
197 if (ctx
->blocksize
!= XTS_BLOCK_SIZE
) {
199 "Cipher block size %zu must equal XTS block size %d",
200 ctx
->blocksize
, XTS_BLOCK_SIZE
);
203 ctx
->iv
= g_new0(uint8_t, ctx
->blocksize
);
206 cipher
->opaque
= ctx
;
210 gcry_cipher_close(ctx
->handle
);
211 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
212 gcry_cipher_close(ctx
->tweakhandle
);
220 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
222 QCryptoCipherGcrypt
*ctx
;
226 ctx
= cipher
->opaque
;
227 gcry_cipher_close(ctx
->handle
);
228 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
229 gcry_cipher_close(ctx
->tweakhandle
);
237 static void qcrypto_gcrypt_xts_encrypt(const void *ctx
,
243 err
= gcry_cipher_encrypt((gcry_cipher_hd_t
)ctx
, dst
, length
, src
, length
);
247 static void qcrypto_gcrypt_xts_decrypt(const void *ctx
,
253 err
= gcry_cipher_decrypt((gcry_cipher_hd_t
)ctx
, dst
, length
, src
, length
);
257 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
263 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
266 if (len
% ctx
->blocksize
) {
267 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
268 len
, ctx
->blocksize
);
272 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
273 xts_encrypt(ctx
->handle
, ctx
->tweakhandle
,
274 qcrypto_gcrypt_xts_encrypt
,
275 qcrypto_gcrypt_xts_decrypt
,
276 ctx
->iv
, len
, out
, in
);
278 err
= gcry_cipher_encrypt(ctx
->handle
,
282 error_setg(errp
, "Cannot encrypt data: %s",
292 int qcrypto_cipher_decrypt(QCryptoCipher
*cipher
,
298 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
301 if (len
% ctx
->blocksize
) {
302 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
303 len
, ctx
->blocksize
);
307 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
308 xts_decrypt(ctx
->handle
, ctx
->tweakhandle
,
309 qcrypto_gcrypt_xts_encrypt
,
310 qcrypto_gcrypt_xts_decrypt
,
311 ctx
->iv
, len
, out
, in
);
313 err
= gcry_cipher_decrypt(ctx
->handle
,
317 error_setg(errp
, "Cannot decrypt data: %s",
326 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
327 const uint8_t *iv
, size_t niv
,
330 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
333 if (niv
!= ctx
->blocksize
) {
334 error_setg(errp
, "Expected IV size %zu not %zu",
335 ctx
->blocksize
, niv
);
340 memcpy(ctx
->iv
, iv
, niv
);
342 gcry_cipher_reset(ctx
->handle
);
343 err
= gcry_cipher_setiv(ctx
->handle
, iv
, niv
);
345 error_setg(errp
, "Cannot set IV: %s",