]> git.proxmox.com Git - mirror_qemu.git/blame - crypto/cipher-gcrypt.c
Merge remote-tracking branch 'remotes/cleber/tags/python-next-pull-request' into...
[mirror_qemu.git] / crypto / cipher-gcrypt.c
CommitLineData
62893b67
DB
1/*
2 * QEMU Crypto cipher libgcrypt algorithms
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
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
b7cbb874 9 * version 2.1 of the License, or (at your option) any later version.
62893b67
DB
10 *
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.
15 *
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/>.
18 *
19 */
20
42f7a448 21#include "qemu/osdep.h"
e0576942 22#ifdef CONFIG_QEMU_PRIVATE_XTS
eaec903c 23#include "crypto/xts.h"
e0576942 24#endif
75c80078 25#include "cipherpriv.h"
eaec903c 26
62893b67
DB
27#include <gcrypt.h>
28
29
f844836d
GA
30bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
31 QCryptoCipherMode mode)
62893b67
DB
32{
33 switch (alg) {
34 case QCRYPTO_CIPHER_ALG_DES_RFB:
ffb7bf45 35 case QCRYPTO_CIPHER_ALG_3DES:
62893b67
DB
36 case QCRYPTO_CIPHER_ALG_AES_128:
37 case QCRYPTO_CIPHER_ALG_AES_192:
38 case QCRYPTO_CIPHER_ALG_AES_256:
084a85ee 39 case QCRYPTO_CIPHER_ALG_CAST5_128:
94318522
DB
40 case QCRYPTO_CIPHER_ALG_SERPENT_128:
41 case QCRYPTO_CIPHER_ALG_SERPENT_192:
42 case QCRYPTO_CIPHER_ALG_SERPENT_256:
50f6753e
DB
43 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
44 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
f844836d
GA
45 break;
46 default:
47 return false;
48 }
49
50 switch (mode) {
51 case QCRYPTO_CIPHER_MODE_ECB:
52 case QCRYPTO_CIPHER_MODE_CBC:
53 case QCRYPTO_CIPHER_MODE_XTS:
54 case QCRYPTO_CIPHER_MODE_CTR:
62893b67
DB
55 return true;
56 default:
57 return false;
58 }
59}
60
3a661f1e
DB
61typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt;
62struct QCryptoCipherGcrypt {
63 gcry_cipher_hd_t handle;
64 size_t blocksize;
e0576942
DB
65#ifdef CONFIG_QEMU_PRIVATE_XTS
66 gcry_cipher_hd_t tweakhandle;
3c28292f 67 /* Initialization vector or Counter */
eaec903c 68 uint8_t *iv;
e0576942 69#endif
3a661f1e 70};
62893b67 71
75c80078
LM
72static void
73qcrypto_gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx,
74 QCryptoCipherMode mode)
cc5eff01
LM
75{
76 if (!ctx) {
77 return;
78 }
79
80 gcry_cipher_close(ctx->handle);
e0576942 81#ifdef CONFIG_QEMU_PRIVATE_XTS
cc5eff01
LM
82 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
83 gcry_cipher_close(ctx->tweakhandle);
84 }
85 g_free(ctx->iv);
e0576942 86#endif
cc5eff01
LM
87 g_free(ctx);
88}
89
90
468fb271
LM
91static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
92 QCryptoCipherMode mode,
93 const uint8_t *key,
94 size_t nkey,
95 Error **errp)
62893b67 96{
3a661f1e 97 QCryptoCipherGcrypt *ctx;
62893b67
DB
98 gcry_error_t err;
99 int gcryalg, gcrymode;
100
101 switch (mode) {
102 case QCRYPTO_CIPHER_MODE_ECB:
e0576942
DB
103 gcrymode = GCRY_CIPHER_MODE_ECB;
104 break;
eaec903c 105 case QCRYPTO_CIPHER_MODE_XTS:
e0576942 106#ifdef CONFIG_QEMU_PRIVATE_XTS
62893b67 107 gcrymode = GCRY_CIPHER_MODE_ECB;
e0576942
DB
108#else
109 gcrymode = GCRY_CIPHER_MODE_XTS;
110#endif
62893b67
DB
111 break;
112 case QCRYPTO_CIPHER_MODE_CBC:
113 gcrymode = GCRY_CIPHER_MODE_CBC;
114 break;
3c28292f
GA
115 case QCRYPTO_CIPHER_MODE_CTR:
116 gcrymode = GCRY_CIPHER_MODE_CTR;
117 break;
62893b67 118 default:
90d6f60d 119 error_setg(errp, "Unsupported cipher mode %s",
977c736f 120 QCryptoCipherMode_str(mode));
62893b67
DB
121 return NULL;
122 }
123
eaec903c 124 if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
62893b67
DB
125 return NULL;
126 }
127
128 switch (alg) {
129 case QCRYPTO_CIPHER_ALG_DES_RFB:
130 gcryalg = GCRY_CIPHER_DES;
131 break;
132
ffb7bf45
LM
133 case QCRYPTO_CIPHER_ALG_3DES:
134 gcryalg = GCRY_CIPHER_3DES;
135 break;
136
62893b67
DB
137 case QCRYPTO_CIPHER_ALG_AES_128:
138 gcryalg = GCRY_CIPHER_AES128;
139 break;
140
141 case QCRYPTO_CIPHER_ALG_AES_192:
142 gcryalg = GCRY_CIPHER_AES192;
143 break;
144
145 case QCRYPTO_CIPHER_ALG_AES_256:
146 gcryalg = GCRY_CIPHER_AES256;
147 break;
148
084a85ee
DB
149 case QCRYPTO_CIPHER_ALG_CAST5_128:
150 gcryalg = GCRY_CIPHER_CAST5;
151 break;
152
94318522
DB
153 case QCRYPTO_CIPHER_ALG_SERPENT_128:
154 gcryalg = GCRY_CIPHER_SERPENT128;
155 break;
156
157 case QCRYPTO_CIPHER_ALG_SERPENT_192:
158 gcryalg = GCRY_CIPHER_SERPENT192;
159 break;
160
161 case QCRYPTO_CIPHER_ALG_SERPENT_256:
162 gcryalg = GCRY_CIPHER_SERPENT256;
163 break;
164
50f6753e
DB
165 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
166 gcryalg = GCRY_CIPHER_TWOFISH128;
167 break;
168
169 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
170 gcryalg = GCRY_CIPHER_TWOFISH;
171 break;
172
62893b67 173 default:
90d6f60d 174 error_setg(errp, "Unsupported cipher algorithm %s",
977c736f 175 QCryptoCipherAlgorithm_str(alg));
62893b67
DB
176 return NULL;
177 }
178
3a661f1e
DB
179 ctx = g_new0(QCryptoCipherGcrypt, 1);
180
181 err = gcry_cipher_open(&ctx->handle, gcryalg, gcrymode, 0);
62893b67
DB
182 if (err != 0) {
183 error_setg(errp, "Cannot initialize cipher: %s",
184 gcry_strerror(err));
185 goto error;
186 }
e0576942 187#ifdef CONFIG_QEMU_PRIVATE_XTS
468fb271 188 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
eaec903c
DB
189 err = gcry_cipher_open(&ctx->tweakhandle, gcryalg, gcrymode, 0);
190 if (err != 0) {
191 error_setg(errp, "Cannot initialize cipher: %s",
192 gcry_strerror(err));
193 goto error;
194 }
195 }
e0576942 196#endif
62893b67 197
468fb271 198 if (alg == QCRYPTO_CIPHER_ALG_DES_RFB) {
62893b67
DB
199 /* We're using standard DES cipher from gcrypt, so we need
200 * to munge the key so that the results are the same as the
201 * bizarre RFB variant of DES :-)
202 */
203 uint8_t *rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
3a661f1e 204 err = gcry_cipher_setkey(ctx->handle, rfbkey, nkey);
62893b67 205 g_free(rfbkey);
3a661f1e 206 ctx->blocksize = 8;
62893b67 207 } else {
e0576942 208#ifdef CONFIG_QEMU_PRIVATE_XTS
468fb271 209 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
eaec903c
DB
210 nkey /= 2;
211 err = gcry_cipher_setkey(ctx->handle, key, nkey);
212 if (err != 0) {
213 error_setg(errp, "Cannot set key: %s",
214 gcry_strerror(err));
215 goto error;
216 }
217 err = gcry_cipher_setkey(ctx->tweakhandle, key + nkey, nkey);
218 } else {
e0576942 219#endif
eaec903c 220 err = gcry_cipher_setkey(ctx->handle, key, nkey);
e0576942 221#ifdef CONFIG_QEMU_PRIVATE_XTS
eaec903c 222 }
e0576942 223#endif
eaec903c
DB
224 if (err != 0) {
225 error_setg(errp, "Cannot set key: %s",
226 gcry_strerror(err));
227 goto error;
228 }
468fb271 229 switch (alg) {
084a85ee
DB
230 case QCRYPTO_CIPHER_ALG_AES_128:
231 case QCRYPTO_CIPHER_ALG_AES_192:
232 case QCRYPTO_CIPHER_ALG_AES_256:
94318522
DB
233 case QCRYPTO_CIPHER_ALG_SERPENT_128:
234 case QCRYPTO_CIPHER_ALG_SERPENT_192:
235 case QCRYPTO_CIPHER_ALG_SERPENT_256:
50f6753e
DB
236 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
237 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
084a85ee
DB
238 ctx->blocksize = 16;
239 break;
ffb7bf45 240 case QCRYPTO_CIPHER_ALG_3DES:
084a85ee
DB
241 case QCRYPTO_CIPHER_ALG_CAST5_128:
242 ctx->blocksize = 8;
243 break;
244 default:
245 g_assert_not_reached();
246 }
62893b67 247 }
eaec903c 248
e0576942 249#ifdef CONFIG_QEMU_PRIVATE_XTS
468fb271 250 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
a5d2f44d
DB
251 if (ctx->blocksize != XTS_BLOCK_SIZE) {
252 error_setg(errp,
253 "Cipher block size %zu must equal XTS block size %d",
254 ctx->blocksize, XTS_BLOCK_SIZE);
255 goto error;
256 }
eaec903c 257 ctx->iv = g_new0(uint8_t, ctx->blocksize);
62893b67 258 }
e0576942 259#endif
62893b67 260
468fb271 261 return ctx;
62893b67
DB
262
263 error:
75c80078 264 qcrypto_gcrypt_cipher_free_ctx(ctx, mode);
62893b67
DB
265 return NULL;
266}
267
268
75c80078
LM
269static void
270qcrypto_gcrypt_cipher_ctx_free(QCryptoCipher *cipher)
62893b67 271{
75c80078 272 qcrypto_gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode);
62893b67
DB
273}
274
275
e0576942 276#ifdef CONFIG_QEMU_PRIVATE_XTS
eaec903c
DB
277static void qcrypto_gcrypt_xts_encrypt(const void *ctx,
278 size_t length,
279 uint8_t *dst,
280 const uint8_t *src)
281{
282 gcry_error_t err;
283 err = gcry_cipher_encrypt((gcry_cipher_hd_t)ctx, dst, length, src, length);
284 g_assert(err == 0);
285}
286
287static void qcrypto_gcrypt_xts_decrypt(const void *ctx,
288 size_t length,
289 uint8_t *dst,
290 const uint8_t *src)
291{
292 gcry_error_t err;
293 err = gcry_cipher_decrypt((gcry_cipher_hd_t)ctx, dst, length, src, length);
294 g_assert(err == 0);
295}
e0576942 296#endif
eaec903c 297
75c80078
LM
298static int
299qcrypto_gcrypt_cipher_encrypt(QCryptoCipher *cipher,
300 const void *in,
301 void *out,
302 size_t len,
303 Error **errp)
62893b67 304{
3a661f1e 305 QCryptoCipherGcrypt *ctx = cipher->opaque;
62893b67
DB
306 gcry_error_t err;
307
3a661f1e
DB
308 if (len % ctx->blocksize) {
309 error_setg(errp, "Length %zu must be a multiple of block size %zu",
310 len, ctx->blocksize);
311 return -1;
312 }
313
e0576942 314#ifdef CONFIG_QEMU_PRIVATE_XTS
eaec903c
DB
315 if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
316 xts_encrypt(ctx->handle, ctx->tweakhandle,
317 qcrypto_gcrypt_xts_encrypt,
318 qcrypto_gcrypt_xts_decrypt,
319 ctx->iv, len, out, in);
e0576942
DB
320 return 0;
321 }
322#endif
323
324 err = gcry_cipher_encrypt(ctx->handle,
325 out, len,
326 in, len);
327 if (err != 0) {
328 error_setg(errp, "Cannot encrypt data: %s",
329 gcry_strerror(err));
330 return -1;
62893b67
DB
331 }
332
333 return 0;
334}
335
336
75c80078
LM
337static int
338qcrypto_gcrypt_cipher_decrypt(QCryptoCipher *cipher,
339 const void *in,
340 void *out,
341 size_t len,
342 Error **errp)
62893b67 343{
3a661f1e 344 QCryptoCipherGcrypt *ctx = cipher->opaque;
62893b67
DB
345 gcry_error_t err;
346
3a661f1e
DB
347 if (len % ctx->blocksize) {
348 error_setg(errp, "Length %zu must be a multiple of block size %zu",
349 len, ctx->blocksize);
350 return -1;
351 }
352
e0576942 353#ifdef CONFIG_QEMU_PRIVATE_XTS
eaec903c
DB
354 if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
355 xts_decrypt(ctx->handle, ctx->tweakhandle,
356 qcrypto_gcrypt_xts_encrypt,
357 qcrypto_gcrypt_xts_decrypt,
358 ctx->iv, len, out, in);
e0576942
DB
359 return 0;
360 }
361#endif
362
363 err = gcry_cipher_decrypt(ctx->handle,
364 out, len,
365 in, len);
366 if (err != 0) {
367 error_setg(errp, "Cannot decrypt data: %s",
368 gcry_strerror(err));
369 return -1;
62893b67
DB
370 }
371
372 return 0;
373}
374
75c80078
LM
375static int
376qcrypto_gcrypt_cipher_setiv(QCryptoCipher *cipher,
377 const uint8_t *iv, size_t niv,
378 Error **errp)
62893b67 379{
3a661f1e 380 QCryptoCipherGcrypt *ctx = cipher->opaque;
62893b67
DB
381 gcry_error_t err;
382
3a661f1e
DB
383 if (niv != ctx->blocksize) {
384 error_setg(errp, "Expected IV size %zu not %zu",
385 ctx->blocksize, niv);
386 return -1;
387 }
388
e0576942 389#ifdef CONFIG_QEMU_PRIVATE_XTS
eaec903c
DB
390 if (ctx->iv) {
391 memcpy(ctx->iv, iv, niv);
e0576942
DB
392 return 0;
393 }
394#endif
395
396 if (cipher->mode == QCRYPTO_CIPHER_MODE_CTR) {
397 err = gcry_cipher_setctr(ctx->handle, iv, niv);
398 if (err != 0) {
399 error_setg(errp, "Cannot set Counter: %s",
3c28292f 400 gcry_strerror(err));
e0576942
DB
401 return -1;
402 }
403 } else {
404 gcry_cipher_reset(ctx->handle);
405 err = gcry_cipher_setiv(ctx->handle, iv, niv);
406 if (err != 0) {
407 error_setg(errp, "Cannot set IV: %s",
3c28292f 408 gcry_strerror(err));
e0576942 409 return -1;
eaec903c 410 }
62893b67
DB
411 }
412
413 return 0;
414}
468fb271
LM
415
416
75c80078
LM
417static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
418 .cipher_encrypt = qcrypto_gcrypt_cipher_encrypt,
419 .cipher_decrypt = qcrypto_gcrypt_cipher_decrypt,
420 .cipher_setiv = qcrypto_gcrypt_cipher_setiv,
421 .cipher_free = qcrypto_gcrypt_cipher_ctx_free,
422};