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