]> git.proxmox.com Git - mirror_qemu.git/blob - crypto/cipher-gcrypt.c
crypto: add sanity checking of plaintext/ciphertext length
[mirror_qemu.git] / crypto / cipher-gcrypt.c
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
21 #include <gcrypt.h>
22
23
24 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
25 {
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;
34 }
35 }
36
37 typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt;
38 struct QCryptoCipherGcrypt {
39 gcry_cipher_hd_t handle;
40 size_t blocksize;
41 };
42
43 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
44 QCryptoCipherMode mode,
45 const uint8_t *key, size_t nkey,
46 Error **errp)
47 {
48 QCryptoCipher *cipher;
49 QCryptoCipherGcrypt *ctx;
50 gcry_error_t err;
51 int gcryalg, gcrymode;
52
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;
63 }
64
65 if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) {
66 return NULL;
67 }
68
69 switch (alg) {
70 case QCRYPTO_CIPHER_ALG_DES_RFB:
71 gcryalg = GCRY_CIPHER_DES;
72 break;
73
74 case QCRYPTO_CIPHER_ALG_AES_128:
75 gcryalg = GCRY_CIPHER_AES128;
76 break;
77
78 case QCRYPTO_CIPHER_ALG_AES_192:
79 gcryalg = GCRY_CIPHER_AES192;
80 break;
81
82 case QCRYPTO_CIPHER_ALG_AES_256:
83 gcryalg = GCRY_CIPHER_AES256;
84 break;
85
86 default:
87 error_setg(errp, "Unsupported cipher algorithm %d", alg);
88 return NULL;
89 }
90
91 cipher = g_new0(QCryptoCipher, 1);
92 cipher->alg = alg;
93 cipher->mode = mode;
94
95 ctx = g_new0(QCryptoCipherGcrypt, 1);
96
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;
102 }
103
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 :-)
108 */
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;
116 }
117 if (err != 0) {
118 error_setg(errp, "Cannot set key: %s",
119 gcry_strerror(err));
120 goto error;
121 }
122
123 cipher->opaque = ctx;
124 return cipher;
125
126 error:
127 gcry_cipher_close(ctx->handle);
128 g_free(ctx);
129 g_free(cipher);
130 return NULL;
131 }
132
133
134 void qcrypto_cipher_free(QCryptoCipher *cipher)
135 {
136 QCryptoCipherGcrypt *ctx;
137 if (!cipher) {
138 return;
139 }
140 ctx = cipher->opaque;
141 gcry_cipher_close(ctx->handle);
142 g_free(ctx);
143 g_free(cipher);
144 }
145
146
147 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
148 const void *in,
149 void *out,
150 size_t len,
151 Error **errp)
152 {
153 QCryptoCipherGcrypt *ctx = cipher->opaque;
154 gcry_error_t err;
155
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;
160 }
161
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;
169 }
170
171 return 0;
172 }
173
174
175 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
176 const void *in,
177 void *out,
178 size_t len,
179 Error **errp)
180 {
181 QCryptoCipherGcrypt *ctx = cipher->opaque;
182 gcry_error_t err;
183
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;
188 }
189
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;
197 }
198
199 return 0;
200 }
201
202 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
203 const uint8_t *iv, size_t niv,
204 Error **errp)
205 {
206 QCryptoCipherGcrypt *ctx = cipher->opaque;
207 gcry_error_t err;
208
209 if (niv != ctx->blocksize) {
210 error_setg(errp, "Expected IV size %zu not %zu",
211 ctx->blocksize, niv);
212 return -1;
213 }
214
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;
221 }
222
223 return 0;
224 }