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_AES_128
:
33 case QCRYPTO_CIPHER_ALG_AES_192
:
34 case QCRYPTO_CIPHER_ALG_AES_256
:
35 case QCRYPTO_CIPHER_ALG_CAST5_128
:
36 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
37 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
38 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
39 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
40 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
47 case QCRYPTO_CIPHER_MODE_ECB
:
48 case QCRYPTO_CIPHER_MODE_CBC
:
49 case QCRYPTO_CIPHER_MODE_XTS
:
50 case QCRYPTO_CIPHER_MODE_CTR
:
57 typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt
;
58 struct QCryptoCipherGcrypt
{
59 gcry_cipher_hd_t handle
;
60 gcry_cipher_hd_t tweakhandle
;
62 /* Initialization vector or Counter */
66 QCryptoCipher
*qcrypto_cipher_new(QCryptoCipherAlgorithm alg
,
67 QCryptoCipherMode mode
,
68 const uint8_t *key
, size_t nkey
,
71 QCryptoCipher
*cipher
;
72 QCryptoCipherGcrypt
*ctx
;
74 int gcryalg
, gcrymode
;
77 case QCRYPTO_CIPHER_MODE_ECB
:
78 case QCRYPTO_CIPHER_MODE_XTS
:
79 gcrymode
= GCRY_CIPHER_MODE_ECB
;
81 case QCRYPTO_CIPHER_MODE_CBC
:
82 gcrymode
= GCRY_CIPHER_MODE_CBC
;
84 case QCRYPTO_CIPHER_MODE_CTR
:
85 gcrymode
= GCRY_CIPHER_MODE_CTR
;
88 error_setg(errp
, "Unsupported cipher mode %s",
89 QCryptoCipherMode_lookup
[mode
]);
93 if (!qcrypto_cipher_validate_key_length(alg
, mode
, nkey
, errp
)) {
98 case QCRYPTO_CIPHER_ALG_DES_RFB
:
99 gcryalg
= GCRY_CIPHER_DES
;
102 case QCRYPTO_CIPHER_ALG_AES_128
:
103 gcryalg
= GCRY_CIPHER_AES128
;
106 case QCRYPTO_CIPHER_ALG_AES_192
:
107 gcryalg
= GCRY_CIPHER_AES192
;
110 case QCRYPTO_CIPHER_ALG_AES_256
:
111 gcryalg
= GCRY_CIPHER_AES256
;
114 case QCRYPTO_CIPHER_ALG_CAST5_128
:
115 gcryalg
= GCRY_CIPHER_CAST5
;
118 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
119 gcryalg
= GCRY_CIPHER_SERPENT128
;
122 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
123 gcryalg
= GCRY_CIPHER_SERPENT192
;
126 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
127 gcryalg
= GCRY_CIPHER_SERPENT256
;
130 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
131 gcryalg
= GCRY_CIPHER_TWOFISH128
;
134 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
135 gcryalg
= GCRY_CIPHER_TWOFISH
;
139 error_setg(errp
, "Unsupported cipher algorithm %s",
140 QCryptoCipherAlgorithm_lookup
[alg
]);
144 cipher
= g_new0(QCryptoCipher
, 1);
148 ctx
= g_new0(QCryptoCipherGcrypt
, 1);
150 err
= gcry_cipher_open(&ctx
->handle
, gcryalg
, gcrymode
, 0);
152 error_setg(errp
, "Cannot initialize cipher: %s",
156 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
157 err
= gcry_cipher_open(&ctx
->tweakhandle
, gcryalg
, gcrymode
, 0);
159 error_setg(errp
, "Cannot initialize cipher: %s",
165 if (cipher
->alg
== QCRYPTO_CIPHER_ALG_DES_RFB
) {
166 /* We're using standard DES cipher from gcrypt, so we need
167 * to munge the key so that the results are the same as the
168 * bizarre RFB variant of DES :-)
170 uint8_t *rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
171 err
= gcry_cipher_setkey(ctx
->handle
, rfbkey
, nkey
);
175 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
177 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
179 error_setg(errp
, "Cannot set key: %s",
183 err
= gcry_cipher_setkey(ctx
->tweakhandle
, key
+ nkey
, nkey
);
185 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
188 error_setg(errp
, "Cannot set key: %s",
192 switch (cipher
->alg
) {
193 case QCRYPTO_CIPHER_ALG_AES_128
:
194 case QCRYPTO_CIPHER_ALG_AES_192
:
195 case QCRYPTO_CIPHER_ALG_AES_256
:
196 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
197 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
198 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
199 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
200 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
203 case QCRYPTO_CIPHER_ALG_CAST5_128
:
207 g_assert_not_reached();
211 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
212 if (ctx
->blocksize
!= XTS_BLOCK_SIZE
) {
214 "Cipher block size %zu must equal XTS block size %d",
215 ctx
->blocksize
, XTS_BLOCK_SIZE
);
218 ctx
->iv
= g_new0(uint8_t, ctx
->blocksize
);
221 cipher
->opaque
= ctx
;
225 gcry_cipher_close(ctx
->handle
);
226 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
227 gcry_cipher_close(ctx
->tweakhandle
);
235 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
237 QCryptoCipherGcrypt
*ctx
;
241 ctx
= cipher
->opaque
;
242 gcry_cipher_close(ctx
->handle
);
243 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
244 gcry_cipher_close(ctx
->tweakhandle
);
252 static void qcrypto_gcrypt_xts_encrypt(const void *ctx
,
258 err
= gcry_cipher_encrypt((gcry_cipher_hd_t
)ctx
, dst
, length
, src
, length
);
262 static void qcrypto_gcrypt_xts_decrypt(const void *ctx
,
268 err
= gcry_cipher_decrypt((gcry_cipher_hd_t
)ctx
, dst
, length
, src
, length
);
272 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
278 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
281 if (len
% ctx
->blocksize
) {
282 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
283 len
, ctx
->blocksize
);
287 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
288 xts_encrypt(ctx
->handle
, ctx
->tweakhandle
,
289 qcrypto_gcrypt_xts_encrypt
,
290 qcrypto_gcrypt_xts_decrypt
,
291 ctx
->iv
, len
, out
, in
);
293 err
= gcry_cipher_encrypt(ctx
->handle
,
297 error_setg(errp
, "Cannot encrypt data: %s",
307 int qcrypto_cipher_decrypt(QCryptoCipher
*cipher
,
313 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
316 if (len
% ctx
->blocksize
) {
317 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
318 len
, ctx
->blocksize
);
322 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
323 xts_decrypt(ctx
->handle
, ctx
->tweakhandle
,
324 qcrypto_gcrypt_xts_encrypt
,
325 qcrypto_gcrypt_xts_decrypt
,
326 ctx
->iv
, len
, out
, in
);
328 err
= gcry_cipher_decrypt(ctx
->handle
,
332 error_setg(errp
, "Cannot decrypt data: %s",
341 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
342 const uint8_t *iv
, size_t niv
,
345 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
348 if (niv
!= ctx
->blocksize
) {
349 error_setg(errp
, "Expected IV size %zu not %zu",
350 ctx
->blocksize
, niv
);
355 memcpy(ctx
->iv
, iv
, niv
);
357 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_CTR
) {
358 err
= gcry_cipher_setctr(ctx
->handle
, iv
, niv
);
360 error_setg(errp
, "Cannot set Counter: %s",
365 gcry_cipher_reset(ctx
->handle
);
366 err
= gcry_cipher_setiv(ctx
->handle
, iv
, niv
);
368 error_setg(errp
, "Cannot set IV: %s",