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.1 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 #ifdef CONFIG_QEMU_PRIVATE_XTS
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 {
60 gcry_cipher_hd_t handle;
62 #ifdef CONFIG_QEMU_PRIVATE_XTS
63 gcry_cipher_hd_t tweakhandle;
64 uint8_t iv[XTS_BLOCK_SIZE];
66 } QCryptoCipherGcrypt;
69 static void qcrypto_gcrypt_ctx_free(QCryptoCipher *cipher)
71 QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
73 gcry_cipher_close(ctx->handle);
77 static int qcrypto_gcrypt_encrypt(QCryptoCipher *cipher, const void *in,
78 void *out, size_t len, Error **errp)
80 QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
83 if (len & (ctx->blocksize - 1)) {
84 error_setg(errp, "Length %zu must be a multiple of block size %zu",
89 err = gcry_cipher_encrypt(ctx->handle, out, len, in, len);
91 error_setg(errp, "Cannot encrypt data: %s", gcry_strerror(err));
99 static int qcrypto_gcrypt_decrypt(QCryptoCipher *cipher, const void *in,
100 void *out, size_t len, Error **errp)
102 QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
105 if (len & (ctx->blocksize - 1)) {
106 error_setg(errp, "Length %zu must be a multiple of block size %zu",
107 len, ctx->blocksize);
111 err = gcry_cipher_decrypt(ctx->handle, out, len, in, len);
113 error_setg(errp, "Cannot decrypt data: %s",
121 static int qcrypto_gcrypt_setiv(QCryptoCipher *cipher,
122 const uint8_t *iv, size_t niv,
125 QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
128 if (niv != ctx->blocksize) {
129 error_setg(errp, "Expected IV size %zu not %zu",
130 ctx->blocksize, niv);
134 gcry_cipher_reset(ctx->handle);
135 err = gcry_cipher_setiv(ctx->handle, iv, niv);
137 error_setg(errp, "Cannot set IV: %s", gcry_strerror(err));
144 static int qcrypto_gcrypt_ctr_setiv(QCryptoCipher *cipher,
145 const uint8_t *iv, size_t niv,
148 QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
151 if (niv != ctx->blocksize) {
152 error_setg(errp, "Expected IV size %zu not %zu",
153 ctx->blocksize, niv);
157 err = gcry_cipher_setctr(ctx->handle, iv, niv);
159 error_setg(errp, "Cannot set Counter: %s", gcry_strerror(err));
167 static const struct QCryptoCipherDriver qcrypto_gcrypt_driver = {
168 .cipher_encrypt = qcrypto_gcrypt_encrypt,
169 .cipher_decrypt = qcrypto_gcrypt_decrypt,
170 .cipher_setiv = qcrypto_gcrypt_setiv,
171 .cipher_free = qcrypto_gcrypt_ctx_free,
174 static const struct QCryptoCipherDriver qcrypto_gcrypt_ctr_driver = {
175 .cipher_encrypt = qcrypto_gcrypt_encrypt,
176 .cipher_decrypt = qcrypto_gcrypt_decrypt,
177 .cipher_setiv = qcrypto_gcrypt_ctr_setiv,
178 .cipher_free = qcrypto_gcrypt_ctx_free,
181 #ifdef CONFIG_QEMU_PRIVATE_XTS
182 static void qcrypto_gcrypt_xts_ctx_free(QCryptoCipher *cipher)
184 QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
186 gcry_cipher_close(ctx->tweakhandle);
187 qcrypto_gcrypt_ctx_free(cipher);
190 static void qcrypto_gcrypt_xts_wrape(const void *ctx, size_t length,
191 uint8_t *dst, const uint8_t *src)
194 err = gcry_cipher_encrypt((gcry_cipher_hd_t)ctx, dst, length, src, length);
198 static void qcrypto_gcrypt_xts_wrapd(const void *ctx, size_t length,
199 uint8_t *dst, const uint8_t *src)
202 err = gcry_cipher_decrypt((gcry_cipher_hd_t)ctx, dst, length, src, length);
206 static int qcrypto_gcrypt_xts_encrypt(QCryptoCipher *cipher, const void *in,
207 void *out, size_t len, Error **errp)
209 QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
211 if (len & (ctx->blocksize - 1)) {
212 error_setg(errp, "Length %zu must be a multiple of block size %zu",
213 len, ctx->blocksize);
217 xts_encrypt(ctx->handle, ctx->tweakhandle,
218 qcrypto_gcrypt_xts_wrape, qcrypto_gcrypt_xts_wrapd,
219 ctx->iv, len, out, in);
223 static int qcrypto_gcrypt_xts_decrypt(QCryptoCipher *cipher, const void *in,
224 void *out, size_t len, Error **errp)
226 QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
228 if (len & (ctx->blocksize - 1)) {
229 error_setg(errp, "Length %zu must be a multiple of block size %zu",
230 len, ctx->blocksize);
234 xts_decrypt(ctx->handle, ctx->tweakhandle,
235 qcrypto_gcrypt_xts_wrape, qcrypto_gcrypt_xts_wrapd,
236 ctx->iv, len, out, in);
240 static int qcrypto_gcrypt_xts_setiv(QCryptoCipher *cipher,
241 const uint8_t *iv, size_t niv,
244 QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
246 if (niv != ctx->blocksize) {
247 error_setg(errp, "Expected IV size %zu not %zu",
248 ctx->blocksize, niv);
252 memcpy(ctx->iv, iv, niv);
256 static const struct QCryptoCipherDriver qcrypto_gcrypt_xts_driver = {
257 .cipher_encrypt = qcrypto_gcrypt_xts_encrypt,
258 .cipher_decrypt = qcrypto_gcrypt_xts_decrypt,
259 .cipher_setiv = qcrypto_gcrypt_xts_setiv,
260 .cipher_free = qcrypto_gcrypt_xts_ctx_free,
262 #endif /* CONFIG_QEMU_PRIVATE_XTS */
265 static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
266 QCryptoCipherMode mode,
271 QCryptoCipherGcrypt *ctx;
272 const QCryptoCipherDriver *drv;
274 int gcryalg, gcrymode;
276 if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
281 case QCRYPTO_CIPHER_ALG_DES_RFB:
282 gcryalg = GCRY_CIPHER_DES;
284 case QCRYPTO_CIPHER_ALG_3DES:
285 gcryalg = GCRY_CIPHER_3DES;
287 case QCRYPTO_CIPHER_ALG_AES_128:
288 gcryalg = GCRY_CIPHER_AES128;
290 case QCRYPTO_CIPHER_ALG_AES_192:
291 gcryalg = GCRY_CIPHER_AES192;
293 case QCRYPTO_CIPHER_ALG_AES_256:
294 gcryalg = GCRY_CIPHER_AES256;
296 case QCRYPTO_CIPHER_ALG_CAST5_128:
297 gcryalg = GCRY_CIPHER_CAST5;
299 case QCRYPTO_CIPHER_ALG_SERPENT_128:
300 gcryalg = GCRY_CIPHER_SERPENT128;
302 case QCRYPTO_CIPHER_ALG_SERPENT_192:
303 gcryalg = GCRY_CIPHER_SERPENT192;
305 case QCRYPTO_CIPHER_ALG_SERPENT_256:
306 gcryalg = GCRY_CIPHER_SERPENT256;
308 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
309 gcryalg = GCRY_CIPHER_TWOFISH128;
311 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
312 gcryalg = GCRY_CIPHER_TWOFISH;
315 error_setg(errp, "Unsupported cipher algorithm %s",
316 QCryptoCipherAlgorithm_str(alg));
320 drv = &qcrypto_gcrypt_driver;
322 case QCRYPTO_CIPHER_MODE_ECB:
323 gcrymode = GCRY_CIPHER_MODE_ECB;
325 case QCRYPTO_CIPHER_MODE_XTS:
326 #ifdef CONFIG_QEMU_PRIVATE_XTS
327 drv = &qcrypto_gcrypt_xts_driver;
328 gcrymode = GCRY_CIPHER_MODE_ECB;
330 gcrymode = GCRY_CIPHER_MODE_XTS;
333 case QCRYPTO_CIPHER_MODE_CBC:
334 gcrymode = GCRY_CIPHER_MODE_CBC;
336 case QCRYPTO_CIPHER_MODE_CTR:
337 drv = &qcrypto_gcrypt_ctr_driver;
338 gcrymode = GCRY_CIPHER_MODE_CTR;
341 error_setg(errp, "Unsupported cipher mode %s",
342 QCryptoCipherMode_str(mode));
346 ctx = g_new0(QCryptoCipherGcrypt, 1);
347 ctx->base.driver = drv;
349 err = gcry_cipher_open(&ctx->handle, gcryalg, gcrymode, 0);
351 error_setg(errp, "Cannot initialize cipher: %s",
355 ctx->blocksize = gcry_cipher_get_algo_blklen(gcryalg);
357 #ifdef CONFIG_QEMU_PRIVATE_XTS
358 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
359 if (ctx->blocksize != XTS_BLOCK_SIZE) {
361 "Cipher block size %zu must equal XTS block size %d",
362 ctx->blocksize, XTS_BLOCK_SIZE);
365 err = gcry_cipher_open(&ctx->tweakhandle, gcryalg, gcrymode, 0);
367 error_setg(errp, "Cannot initialize cipher: %s",
374 if (alg == QCRYPTO_CIPHER_ALG_DES_RFB) {
375 /* We're using standard DES cipher from gcrypt, so we need
376 * to munge the key so that the results are the same as the
377 * bizarre RFB variant of DES :-)
379 uint8_t *rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
380 err = gcry_cipher_setkey(ctx->handle, rfbkey, nkey);
383 #ifdef CONFIG_QEMU_PRIVATE_XTS
384 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
386 err = gcry_cipher_setkey(ctx->tweakhandle, key + nkey, nkey);
388 error_setg(errp, "Cannot set key: %s", gcry_strerror(err));
393 err = gcry_cipher_setkey(ctx->handle, key, nkey);
396 error_setg(errp, "Cannot set key: %s", gcry_strerror(err));
403 #ifdef CONFIG_QEMU_PRIVATE_XTS
404 gcry_cipher_close(ctx->tweakhandle);
406 gcry_cipher_close(ctx->handle);