]> Git Repo - qemu.git/blame - crypto/cipher-gcrypt.c
crypto: Assume blocksize is a power of 2
[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 }
eba29771 248 g_assert(is_power_of_2(ctx->blocksize));
eaec903c 249
e0576942 250#ifdef CONFIG_QEMU_PRIVATE_XTS
468fb271 251 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
a5d2f44d
DB
252 if (ctx->blocksize != XTS_BLOCK_SIZE) {
253 error_setg(errp,
254 "Cipher block size %zu must equal XTS block size %d",
255 ctx->blocksize, XTS_BLOCK_SIZE);
256 goto error;
257 }
eaec903c 258 ctx->iv = g_new0(uint8_t, ctx->blocksize);
62893b67 259 }
e0576942 260#endif
62893b67 261
468fb271 262 return ctx;
62893b67
DB
263
264 error:
75c80078 265 qcrypto_gcrypt_cipher_free_ctx(ctx, mode);
62893b67
DB
266 return NULL;
267}
268
269
75c80078
LM
270static void
271qcrypto_gcrypt_cipher_ctx_free(QCryptoCipher *cipher)
62893b67 272{
75c80078 273 qcrypto_gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode);
62893b67
DB
274}
275
276
e0576942 277#ifdef CONFIG_QEMU_PRIVATE_XTS
eaec903c
DB
278static void qcrypto_gcrypt_xts_encrypt(const void *ctx,
279 size_t length,
280 uint8_t *dst,
281 const uint8_t *src)
282{
283 gcry_error_t err;
284 err = gcry_cipher_encrypt((gcry_cipher_hd_t)ctx, dst, length, src, length);
285 g_assert(err == 0);
286}
287
288static void qcrypto_gcrypt_xts_decrypt(const void *ctx,
289 size_t length,
290 uint8_t *dst,
291 const uint8_t *src)
292{
293 gcry_error_t err;
294 err = gcry_cipher_decrypt((gcry_cipher_hd_t)ctx, dst, length, src, length);
295 g_assert(err == 0);
296}
e0576942 297#endif
eaec903c 298
75c80078
LM
299static int
300qcrypto_gcrypt_cipher_encrypt(QCryptoCipher *cipher,
301 const void *in,
302 void *out,
303 size_t len,
304 Error **errp)
62893b67 305{
3a661f1e 306 QCryptoCipherGcrypt *ctx = cipher->opaque;
62893b67
DB
307 gcry_error_t err;
308
eba29771 309 if (len & (ctx->blocksize - 1)) {
3a661f1e
DB
310 error_setg(errp, "Length %zu must be a multiple of block size %zu",
311 len, ctx->blocksize);
312 return -1;
313 }
314
e0576942 315#ifdef CONFIG_QEMU_PRIVATE_XTS
eaec903c
DB
316 if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
317 xts_encrypt(ctx->handle, ctx->tweakhandle,
318 qcrypto_gcrypt_xts_encrypt,
319 qcrypto_gcrypt_xts_decrypt,
320 ctx->iv, len, out, in);
e0576942
DB
321 return 0;
322 }
323#endif
324
325 err = gcry_cipher_encrypt(ctx->handle,
326 out, len,
327 in, len);
328 if (err != 0) {
329 error_setg(errp, "Cannot encrypt data: %s",
330 gcry_strerror(err));
331 return -1;
62893b67
DB
332 }
333
334 return 0;
335}
336
337
75c80078
LM
338static int
339qcrypto_gcrypt_cipher_decrypt(QCryptoCipher *cipher,
340 const void *in,
341 void *out,
342 size_t len,
343 Error **errp)
62893b67 344{
3a661f1e 345 QCryptoCipherGcrypt *ctx = cipher->opaque;
62893b67
DB
346 gcry_error_t err;
347
eba29771 348 if (len & (ctx->blocksize - 1)) {
3a661f1e
DB
349 error_setg(errp, "Length %zu must be a multiple of block size %zu",
350 len, ctx->blocksize);
351 return -1;
352 }
353
e0576942 354#ifdef CONFIG_QEMU_PRIVATE_XTS
eaec903c
DB
355 if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
356 xts_decrypt(ctx->handle, ctx->tweakhandle,
357 qcrypto_gcrypt_xts_encrypt,
358 qcrypto_gcrypt_xts_decrypt,
359 ctx->iv, len, out, in);
e0576942
DB
360 return 0;
361 }
362#endif
363
364 err = gcry_cipher_decrypt(ctx->handle,
365 out, len,
366 in, len);
367 if (err != 0) {
368 error_setg(errp, "Cannot decrypt data: %s",
369 gcry_strerror(err));
370 return -1;
62893b67
DB
371 }
372
373 return 0;
374}
375
75c80078
LM
376static int
377qcrypto_gcrypt_cipher_setiv(QCryptoCipher *cipher,
378 const uint8_t *iv, size_t niv,
379 Error **errp)
62893b67 380{
3a661f1e 381 QCryptoCipherGcrypt *ctx = cipher->opaque;
62893b67
DB
382 gcry_error_t err;
383
3a661f1e
DB
384 if (niv != ctx->blocksize) {
385 error_setg(errp, "Expected IV size %zu not %zu",
386 ctx->blocksize, niv);
387 return -1;
388 }
389
e0576942 390#ifdef CONFIG_QEMU_PRIVATE_XTS
eaec903c
DB
391 if (ctx->iv) {
392 memcpy(ctx->iv, iv, niv);
e0576942
DB
393 return 0;
394 }
395#endif
396
397 if (cipher->mode == QCRYPTO_CIPHER_MODE_CTR) {
398 err = gcry_cipher_setctr(ctx->handle, iv, niv);
399 if (err != 0) {
400 error_setg(errp, "Cannot set Counter: %s",
3c28292f 401 gcry_strerror(err));
e0576942
DB
402 return -1;
403 }
404 } else {
405 gcry_cipher_reset(ctx->handle);
406 err = gcry_cipher_setiv(ctx->handle, iv, niv);
407 if (err != 0) {
408 error_setg(errp, "Cannot set IV: %s",
3c28292f 409 gcry_strerror(err));
e0576942 410 return -1;
eaec903c 411 }
62893b67
DB
412 }
413
414 return 0;
415}
468fb271
LM
416
417
75c80078
LM
418static struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
419 .cipher_encrypt = qcrypto_gcrypt_cipher_encrypt,
420 .cipher_decrypt = qcrypto_gcrypt_cipher_decrypt,
421 .cipher_setiv = qcrypto_gcrypt_cipher_setiv,
422 .cipher_free = qcrypto_gcrypt_cipher_ctx_free,
423};
This page took 0.283404 seconds and 4 git commands to generate.