]> git.proxmox.com Git - mirror_qemu.git/blame - crypto/cipher-gcrypt.c
crypto: add support for the serpent cipher algorithm
[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"
62893b67
DB
22#include <gcrypt.h>
23
24
25bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
26{
27 switch (alg) {
28 case QCRYPTO_CIPHER_ALG_DES_RFB:
29 case QCRYPTO_CIPHER_ALG_AES_128:
30 case QCRYPTO_CIPHER_ALG_AES_192:
31 case QCRYPTO_CIPHER_ALG_AES_256:
084a85ee 32 case QCRYPTO_CIPHER_ALG_CAST5_128:
94318522
DB
33 case QCRYPTO_CIPHER_ALG_SERPENT_128:
34 case QCRYPTO_CIPHER_ALG_SERPENT_192:
35 case QCRYPTO_CIPHER_ALG_SERPENT_256:
62893b67
DB
36 return true;
37 default:
38 return false;
39 }
40}
41
3a661f1e
DB
42typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt;
43struct QCryptoCipherGcrypt {
44 gcry_cipher_hd_t handle;
45 size_t blocksize;
46};
62893b67
DB
47
48QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
49 QCryptoCipherMode mode,
50 const uint8_t *key, size_t nkey,
51 Error **errp)
52{
53 QCryptoCipher *cipher;
3a661f1e 54 QCryptoCipherGcrypt *ctx;
62893b67
DB
55 gcry_error_t err;
56 int gcryalg, gcrymode;
57
58 switch (mode) {
59 case QCRYPTO_CIPHER_MODE_ECB:
60 gcrymode = GCRY_CIPHER_MODE_ECB;
61 break;
62 case QCRYPTO_CIPHER_MODE_CBC:
63 gcrymode = GCRY_CIPHER_MODE_CBC;
64 break;
65 default:
66 error_setg(errp, "Unsupported cipher mode %d", mode);
67 return NULL;
68 }
69
70 if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) {
71 return NULL;
72 }
73
74 switch (alg) {
75 case QCRYPTO_CIPHER_ALG_DES_RFB:
76 gcryalg = GCRY_CIPHER_DES;
77 break;
78
79 case QCRYPTO_CIPHER_ALG_AES_128:
80 gcryalg = GCRY_CIPHER_AES128;
81 break;
82
83 case QCRYPTO_CIPHER_ALG_AES_192:
84 gcryalg = GCRY_CIPHER_AES192;
85 break;
86
87 case QCRYPTO_CIPHER_ALG_AES_256:
88 gcryalg = GCRY_CIPHER_AES256;
89 break;
90
084a85ee
DB
91 case QCRYPTO_CIPHER_ALG_CAST5_128:
92 gcryalg = GCRY_CIPHER_CAST5;
93 break;
94
94318522
DB
95 case QCRYPTO_CIPHER_ALG_SERPENT_128:
96 gcryalg = GCRY_CIPHER_SERPENT128;
97 break;
98
99 case QCRYPTO_CIPHER_ALG_SERPENT_192:
100 gcryalg = GCRY_CIPHER_SERPENT192;
101 break;
102
103 case QCRYPTO_CIPHER_ALG_SERPENT_256:
104 gcryalg = GCRY_CIPHER_SERPENT256;
105 break;
106
62893b67
DB
107 default:
108 error_setg(errp, "Unsupported cipher algorithm %d", alg);
109 return NULL;
110 }
111
112 cipher = g_new0(QCryptoCipher, 1);
113 cipher->alg = alg;
114 cipher->mode = mode;
115
3a661f1e
DB
116 ctx = g_new0(QCryptoCipherGcrypt, 1);
117
118 err = gcry_cipher_open(&ctx->handle, gcryalg, gcrymode, 0);
62893b67
DB
119 if (err != 0) {
120 error_setg(errp, "Cannot initialize cipher: %s",
121 gcry_strerror(err));
122 goto error;
123 }
124
125 if (cipher->alg == QCRYPTO_CIPHER_ALG_DES_RFB) {
126 /* We're using standard DES cipher from gcrypt, so we need
127 * to munge the key so that the results are the same as the
128 * bizarre RFB variant of DES :-)
129 */
130 uint8_t *rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
3a661f1e 131 err = gcry_cipher_setkey(ctx->handle, rfbkey, nkey);
62893b67 132 g_free(rfbkey);
3a661f1e 133 ctx->blocksize = 8;
62893b67 134 } else {
3a661f1e 135 err = gcry_cipher_setkey(ctx->handle, key, nkey);
084a85ee
DB
136 switch (cipher->alg) {
137 case QCRYPTO_CIPHER_ALG_AES_128:
138 case QCRYPTO_CIPHER_ALG_AES_192:
139 case QCRYPTO_CIPHER_ALG_AES_256:
94318522
DB
140 case QCRYPTO_CIPHER_ALG_SERPENT_128:
141 case QCRYPTO_CIPHER_ALG_SERPENT_192:
142 case QCRYPTO_CIPHER_ALG_SERPENT_256:
084a85ee
DB
143 ctx->blocksize = 16;
144 break;
145 case QCRYPTO_CIPHER_ALG_CAST5_128:
146 ctx->blocksize = 8;
147 break;
148 default:
149 g_assert_not_reached();
150 }
62893b67
DB
151 }
152 if (err != 0) {
153 error_setg(errp, "Cannot set key: %s",
154 gcry_strerror(err));
155 goto error;
156 }
157
3a661f1e 158 cipher->opaque = ctx;
62893b67
DB
159 return cipher;
160
161 error:
3a661f1e
DB
162 gcry_cipher_close(ctx->handle);
163 g_free(ctx);
62893b67
DB
164 g_free(cipher);
165 return NULL;
166}
167
168
169void qcrypto_cipher_free(QCryptoCipher *cipher)
170{
3a661f1e 171 QCryptoCipherGcrypt *ctx;
62893b67
DB
172 if (!cipher) {
173 return;
174 }
3a661f1e
DB
175 ctx = cipher->opaque;
176 gcry_cipher_close(ctx->handle);
177 g_free(ctx);
62893b67
DB
178 g_free(cipher);
179}
180
181
182int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
183 const void *in,
184 void *out,
185 size_t len,
186 Error **errp)
187{
3a661f1e 188 QCryptoCipherGcrypt *ctx = cipher->opaque;
62893b67
DB
189 gcry_error_t err;
190
3a661f1e
DB
191 if (len % ctx->blocksize) {
192 error_setg(errp, "Length %zu must be a multiple of block size %zu",
193 len, ctx->blocksize);
194 return -1;
195 }
196
197 err = gcry_cipher_encrypt(ctx->handle,
62893b67
DB
198 out, len,
199 in, len);
200 if (err != 0) {
201 error_setg(errp, "Cannot encrypt data: %s",
202 gcry_strerror(err));
203 return -1;
204 }
205
206 return 0;
207}
208
209
210int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
211 const void *in,
212 void *out,
213 size_t len,
214 Error **errp)
215{
3a661f1e 216 QCryptoCipherGcrypt *ctx = cipher->opaque;
62893b67
DB
217 gcry_error_t err;
218
3a661f1e
DB
219 if (len % ctx->blocksize) {
220 error_setg(errp, "Length %zu must be a multiple of block size %zu",
221 len, ctx->blocksize);
222 return -1;
223 }
224
225 err = gcry_cipher_decrypt(ctx->handle,
62893b67
DB
226 out, len,
227 in, len);
228 if (err != 0) {
229 error_setg(errp, "Cannot decrypt data: %s",
230 gcry_strerror(err));
231 return -1;
232 }
233
234 return 0;
235}
236
237int qcrypto_cipher_setiv(QCryptoCipher *cipher,
238 const uint8_t *iv, size_t niv,
239 Error **errp)
240{
3a661f1e 241 QCryptoCipherGcrypt *ctx = cipher->opaque;
62893b67
DB
242 gcry_error_t err;
243
3a661f1e
DB
244 if (niv != ctx->blocksize) {
245 error_setg(errp, "Expected IV size %zu not %zu",
246 ctx->blocksize, niv);
247 return -1;
248 }
249
250 gcry_cipher_reset(ctx->handle);
251 err = gcry_cipher_setiv(ctx->handle, iv, niv);
62893b67
DB
252 if (err != 0) {
253 error_setg(errp, "Cannot set IV: %s",
254 gcry_strerror(err));
255 return -1;
256 }
257
258 return 0;
259}