tests: acpi tables expected blobs update
[qemu/kevin.git] / crypto / cipher-gcrypt.c
blobda3f4c74dbf2106303b453a523b57f55762f61bc
1 /*
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"
24 #include <gcrypt.h>
27 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
29 switch (alg) {
30 case QCRYPTO_CIPHER_ALG_DES_RFB:
31 case QCRYPTO_CIPHER_ALG_AES_128:
32 case QCRYPTO_CIPHER_ALG_AES_192:
33 case QCRYPTO_CIPHER_ALG_AES_256:
34 case QCRYPTO_CIPHER_ALG_CAST5_128:
35 case QCRYPTO_CIPHER_ALG_SERPENT_128:
36 case QCRYPTO_CIPHER_ALG_SERPENT_192:
37 case QCRYPTO_CIPHER_ALG_SERPENT_256:
38 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
39 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
40 return true;
41 default:
42 return false;
46 typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt;
47 struct QCryptoCipherGcrypt {
48 gcry_cipher_hd_t handle;
49 gcry_cipher_hd_t tweakhandle;
50 size_t blocksize;
51 uint8_t *iv;
54 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
55 QCryptoCipherMode mode,
56 const uint8_t *key, size_t nkey,
57 Error **errp)
59 QCryptoCipher *cipher;
60 QCryptoCipherGcrypt *ctx;
61 gcry_error_t err;
62 int gcryalg, gcrymode;
64 switch (mode) {
65 case QCRYPTO_CIPHER_MODE_ECB:
66 case QCRYPTO_CIPHER_MODE_XTS:
67 gcrymode = GCRY_CIPHER_MODE_ECB;
68 break;
69 case QCRYPTO_CIPHER_MODE_CBC:
70 gcrymode = GCRY_CIPHER_MODE_CBC;
71 break;
72 default:
73 error_setg(errp, "Unsupported cipher mode %s",
74 QCryptoCipherMode_lookup[mode]);
75 return NULL;
78 if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
79 return NULL;
82 switch (alg) {
83 case QCRYPTO_CIPHER_ALG_DES_RFB:
84 gcryalg = GCRY_CIPHER_DES;
85 break;
87 case QCRYPTO_CIPHER_ALG_AES_128:
88 gcryalg = GCRY_CIPHER_AES128;
89 break;
91 case QCRYPTO_CIPHER_ALG_AES_192:
92 gcryalg = GCRY_CIPHER_AES192;
93 break;
95 case QCRYPTO_CIPHER_ALG_AES_256:
96 gcryalg = GCRY_CIPHER_AES256;
97 break;
99 case QCRYPTO_CIPHER_ALG_CAST5_128:
100 gcryalg = GCRY_CIPHER_CAST5;
101 break;
103 case QCRYPTO_CIPHER_ALG_SERPENT_128:
104 gcryalg = GCRY_CIPHER_SERPENT128;
105 break;
107 case QCRYPTO_CIPHER_ALG_SERPENT_192:
108 gcryalg = GCRY_CIPHER_SERPENT192;
109 break;
111 case QCRYPTO_CIPHER_ALG_SERPENT_256:
112 gcryalg = GCRY_CIPHER_SERPENT256;
113 break;
115 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
116 gcryalg = GCRY_CIPHER_TWOFISH128;
117 break;
119 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
120 gcryalg = GCRY_CIPHER_TWOFISH;
121 break;
123 default:
124 error_setg(errp, "Unsupported cipher algorithm %s",
125 QCryptoCipherAlgorithm_lookup[alg]);
126 return NULL;
129 cipher = g_new0(QCryptoCipher, 1);
130 cipher->alg = alg;
131 cipher->mode = mode;
133 ctx = g_new0(QCryptoCipherGcrypt, 1);
135 err = gcry_cipher_open(&ctx->handle, gcryalg, gcrymode, 0);
136 if (err != 0) {
137 error_setg(errp, "Cannot initialize cipher: %s",
138 gcry_strerror(err));
139 goto error;
141 if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
142 err = gcry_cipher_open(&ctx->tweakhandle, gcryalg, gcrymode, 0);
143 if (err != 0) {
144 error_setg(errp, "Cannot initialize cipher: %s",
145 gcry_strerror(err));
146 goto error;
150 if (cipher->alg == QCRYPTO_CIPHER_ALG_DES_RFB) {
151 /* We're using standard DES cipher from gcrypt, so we need
152 * to munge the key so that the results are the same as the
153 * bizarre RFB variant of DES :-)
155 uint8_t *rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
156 err = gcry_cipher_setkey(ctx->handle, rfbkey, nkey);
157 g_free(rfbkey);
158 ctx->blocksize = 8;
159 } else {
160 if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
161 nkey /= 2;
162 err = gcry_cipher_setkey(ctx->handle, key, nkey);
163 if (err != 0) {
164 error_setg(errp, "Cannot set key: %s",
165 gcry_strerror(err));
166 goto error;
168 err = gcry_cipher_setkey(ctx->tweakhandle, key + nkey, nkey);
169 } else {
170 err = gcry_cipher_setkey(ctx->handle, key, nkey);
172 if (err != 0) {
173 error_setg(errp, "Cannot set key: %s",
174 gcry_strerror(err));
175 goto error;
177 switch (cipher->alg) {
178 case QCRYPTO_CIPHER_ALG_AES_128:
179 case QCRYPTO_CIPHER_ALG_AES_192:
180 case QCRYPTO_CIPHER_ALG_AES_256:
181 case QCRYPTO_CIPHER_ALG_SERPENT_128:
182 case QCRYPTO_CIPHER_ALG_SERPENT_192:
183 case QCRYPTO_CIPHER_ALG_SERPENT_256:
184 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
185 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
186 ctx->blocksize = 16;
187 break;
188 case QCRYPTO_CIPHER_ALG_CAST5_128:
189 ctx->blocksize = 8;
190 break;
191 default:
192 g_assert_not_reached();
196 if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
197 if (ctx->blocksize != XTS_BLOCK_SIZE) {
198 error_setg(errp,
199 "Cipher block size %zu must equal XTS block size %d",
200 ctx->blocksize, XTS_BLOCK_SIZE);
201 goto error;
203 ctx->iv = g_new0(uint8_t, ctx->blocksize);
206 cipher->opaque = ctx;
207 return cipher;
209 error:
210 gcry_cipher_close(ctx->handle);
211 if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
212 gcry_cipher_close(ctx->tweakhandle);
214 g_free(ctx);
215 g_free(cipher);
216 return NULL;
220 void qcrypto_cipher_free(QCryptoCipher *cipher)
222 QCryptoCipherGcrypt *ctx;
223 if (!cipher) {
224 return;
226 ctx = cipher->opaque;
227 gcry_cipher_close(ctx->handle);
228 if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
229 gcry_cipher_close(ctx->tweakhandle);
231 g_free(ctx->iv);
232 g_free(ctx);
233 g_free(cipher);
237 static void qcrypto_gcrypt_xts_encrypt(const void *ctx,
238 size_t length,
239 uint8_t *dst,
240 const uint8_t *src)
242 gcry_error_t err;
243 err = gcry_cipher_encrypt((gcry_cipher_hd_t)ctx, dst, length, src, length);
244 g_assert(err == 0);
247 static void qcrypto_gcrypt_xts_decrypt(const void *ctx,
248 size_t length,
249 uint8_t *dst,
250 const uint8_t *src)
252 gcry_error_t err;
253 err = gcry_cipher_decrypt((gcry_cipher_hd_t)ctx, dst, length, src, length);
254 g_assert(err == 0);
257 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
258 const void *in,
259 void *out,
260 size_t len,
261 Error **errp)
263 QCryptoCipherGcrypt *ctx = cipher->opaque;
264 gcry_error_t err;
266 if (len % ctx->blocksize) {
267 error_setg(errp, "Length %zu must be a multiple of block size %zu",
268 len, ctx->blocksize);
269 return -1;
272 if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
273 xts_encrypt(ctx->handle, ctx->tweakhandle,
274 qcrypto_gcrypt_xts_encrypt,
275 qcrypto_gcrypt_xts_decrypt,
276 ctx->iv, len, out, in);
277 } else {
278 err = gcry_cipher_encrypt(ctx->handle,
279 out, len,
280 in, len);
281 if (err != 0) {
282 error_setg(errp, "Cannot encrypt data: %s",
283 gcry_strerror(err));
284 return -1;
288 return 0;
292 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
293 const void *in,
294 void *out,
295 size_t len,
296 Error **errp)
298 QCryptoCipherGcrypt *ctx = cipher->opaque;
299 gcry_error_t err;
301 if (len % ctx->blocksize) {
302 error_setg(errp, "Length %zu must be a multiple of block size %zu",
303 len, ctx->blocksize);
304 return -1;
307 if (cipher->mode == QCRYPTO_CIPHER_MODE_XTS) {
308 xts_decrypt(ctx->handle, ctx->tweakhandle,
309 qcrypto_gcrypt_xts_encrypt,
310 qcrypto_gcrypt_xts_decrypt,
311 ctx->iv, len, out, in);
312 } else {
313 err = gcry_cipher_decrypt(ctx->handle,
314 out, len,
315 in, len);
316 if (err != 0) {
317 error_setg(errp, "Cannot decrypt data: %s",
318 gcry_strerror(err));
319 return -1;
323 return 0;
326 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
327 const uint8_t *iv, size_t niv,
328 Error **errp)
330 QCryptoCipherGcrypt *ctx = cipher->opaque;
331 gcry_error_t err;
333 if (niv != ctx->blocksize) {
334 error_setg(errp, "Expected IV size %zu not %zu",
335 ctx->blocksize, niv);
336 return -1;
339 if (ctx->iv) {
340 memcpy(ctx->iv, iv, niv);
341 } else {
342 gcry_cipher_reset(ctx->handle);
343 err = gcry_cipher_setiv(ctx->handle, iv, niv);
344 if (err != 0) {
345 error_setg(errp, "Cannot set IV: %s",
346 gcry_strerror(err));
347 return -1;
351 return 0;