Use qemu_get_buffer_in_place for xbzrle data
[qemu/ar7.git] / crypto / cipher-gcrypt.c
blobc4f811487aaf43bdbbcc68956533915547a477d9
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 <gcrypt.h>
24 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
26 switch (alg) {
27 case QCRYPTO_CIPHER_ALG_DES_RFB:
28 case QCRYPTO_CIPHER_ALG_AES_128:
29 case QCRYPTO_CIPHER_ALG_AES_192:
30 case QCRYPTO_CIPHER_ALG_AES_256:
31 return true;
32 default:
33 return false;
37 typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt;
38 struct QCryptoCipherGcrypt {
39 gcry_cipher_hd_t handle;
40 size_t blocksize;
43 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
44 QCryptoCipherMode mode,
45 const uint8_t *key, size_t nkey,
46 Error **errp)
48 QCryptoCipher *cipher;
49 QCryptoCipherGcrypt *ctx;
50 gcry_error_t err;
51 int gcryalg, gcrymode;
53 switch (mode) {
54 case QCRYPTO_CIPHER_MODE_ECB:
55 gcrymode = GCRY_CIPHER_MODE_ECB;
56 break;
57 case QCRYPTO_CIPHER_MODE_CBC:
58 gcrymode = GCRY_CIPHER_MODE_CBC;
59 break;
60 default:
61 error_setg(errp, "Unsupported cipher mode %d", mode);
62 return NULL;
65 if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) {
66 return NULL;
69 switch (alg) {
70 case QCRYPTO_CIPHER_ALG_DES_RFB:
71 gcryalg = GCRY_CIPHER_DES;
72 break;
74 case QCRYPTO_CIPHER_ALG_AES_128:
75 gcryalg = GCRY_CIPHER_AES128;
76 break;
78 case QCRYPTO_CIPHER_ALG_AES_192:
79 gcryalg = GCRY_CIPHER_AES192;
80 break;
82 case QCRYPTO_CIPHER_ALG_AES_256:
83 gcryalg = GCRY_CIPHER_AES256;
84 break;
86 default:
87 error_setg(errp, "Unsupported cipher algorithm %d", alg);
88 return NULL;
91 cipher = g_new0(QCryptoCipher, 1);
92 cipher->alg = alg;
93 cipher->mode = mode;
95 ctx = g_new0(QCryptoCipherGcrypt, 1);
97 err = gcry_cipher_open(&ctx->handle, gcryalg, gcrymode, 0);
98 if (err != 0) {
99 error_setg(errp, "Cannot initialize cipher: %s",
100 gcry_strerror(err));
101 goto error;
104 if (cipher->alg == QCRYPTO_CIPHER_ALG_DES_RFB) {
105 /* We're using standard DES cipher from gcrypt, so we need
106 * to munge the key so that the results are the same as the
107 * bizarre RFB variant of DES :-)
109 uint8_t *rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
110 err = gcry_cipher_setkey(ctx->handle, rfbkey, nkey);
111 g_free(rfbkey);
112 ctx->blocksize = 8;
113 } else {
114 err = gcry_cipher_setkey(ctx->handle, key, nkey);
115 ctx->blocksize = 16;
117 if (err != 0) {
118 error_setg(errp, "Cannot set key: %s",
119 gcry_strerror(err));
120 goto error;
123 cipher->opaque = ctx;
124 return cipher;
126 error:
127 gcry_cipher_close(ctx->handle);
128 g_free(ctx);
129 g_free(cipher);
130 return NULL;
134 void qcrypto_cipher_free(QCryptoCipher *cipher)
136 QCryptoCipherGcrypt *ctx;
137 if (!cipher) {
138 return;
140 ctx = cipher->opaque;
141 gcry_cipher_close(ctx->handle);
142 g_free(ctx);
143 g_free(cipher);
147 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
148 const void *in,
149 void *out,
150 size_t len,
151 Error **errp)
153 QCryptoCipherGcrypt *ctx = cipher->opaque;
154 gcry_error_t err;
156 if (len % ctx->blocksize) {
157 error_setg(errp, "Length %zu must be a multiple of block size %zu",
158 len, ctx->blocksize);
159 return -1;
162 err = gcry_cipher_encrypt(ctx->handle,
163 out, len,
164 in, len);
165 if (err != 0) {
166 error_setg(errp, "Cannot encrypt data: %s",
167 gcry_strerror(err));
168 return -1;
171 return 0;
175 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
176 const void *in,
177 void *out,
178 size_t len,
179 Error **errp)
181 QCryptoCipherGcrypt *ctx = cipher->opaque;
182 gcry_error_t err;
184 if (len % ctx->blocksize) {
185 error_setg(errp, "Length %zu must be a multiple of block size %zu",
186 len, ctx->blocksize);
187 return -1;
190 err = gcry_cipher_decrypt(ctx->handle,
191 out, len,
192 in, len);
193 if (err != 0) {
194 error_setg(errp, "Cannot decrypt data: %s",
195 gcry_strerror(err));
196 return -1;
199 return 0;
202 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
203 const uint8_t *iv, size_t niv,
204 Error **errp)
206 QCryptoCipherGcrypt *ctx = cipher->opaque;
207 gcry_error_t err;
209 if (niv != ctx->blocksize) {
210 error_setg(errp, "Expected IV size %zu not %zu",
211 ctx->blocksize, niv);
212 return -1;
215 gcry_cipher_reset(ctx->handle);
216 err = gcry_cipher_setiv(ctx->handle, iv, niv);
217 if (err != 0) {
218 error_setg(errp, "Cannot set IV: %s",
219 gcry_strerror(err));
220 return -1;
223 return 0;