]> git.proxmox.com Git - mirror_qemu.git/blob - crypto/cipher-nettle.c
crypto: add support for the cast5-128 cipher algorithm
[mirror_qemu.git] / crypto / cipher-nettle.c
1 /*
2 * QEMU Crypto cipher nettle 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 "qemu/osdep.h"
22 #include <nettle/nettle-types.h>
23 #include <nettle/aes.h>
24 #include <nettle/des.h>
25 #include <nettle/cbc.h>
26 #include <nettle/cast128.h>
27
28 #if CONFIG_NETTLE_VERSION_MAJOR < 3
29 typedef nettle_crypt_func nettle_cipher_func;
30
31 typedef void * cipher_ctx_t;
32 typedef unsigned cipher_length_t;
33 #else
34 typedef const void * cipher_ctx_t;
35 typedef size_t cipher_length_t;
36 #endif
37
38 static nettle_cipher_func aes_encrypt_wrapper;
39 static nettle_cipher_func aes_decrypt_wrapper;
40 static nettle_cipher_func des_encrypt_wrapper;
41 static nettle_cipher_func des_decrypt_wrapper;
42
43 static void aes_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
44 uint8_t *dst, const uint8_t *src)
45 {
46 aes_encrypt(ctx, length, dst, src);
47 }
48
49 static void aes_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
50 uint8_t *dst, const uint8_t *src)
51 {
52 aes_decrypt(ctx, length, dst, src);
53 }
54
55 static void des_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
56 uint8_t *dst, const uint8_t *src)
57 {
58 des_encrypt(ctx, length, dst, src);
59 }
60
61 static void des_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
62 uint8_t *dst, const uint8_t *src)
63 {
64 des_decrypt(ctx, length, dst, src);
65 }
66
67 static void cast128_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
68 uint8_t *dst, const uint8_t *src)
69 {
70 cast128_encrypt(ctx, length, dst, src);
71 }
72
73 static void cast128_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
74 uint8_t *dst, const uint8_t *src)
75 {
76 cast128_decrypt(ctx, length, dst, src);
77 }
78
79 typedef struct QCryptoCipherNettle QCryptoCipherNettle;
80 struct QCryptoCipherNettle {
81 void *ctx_encrypt;
82 void *ctx_decrypt;
83 nettle_cipher_func *alg_encrypt;
84 nettle_cipher_func *alg_decrypt;
85 uint8_t *iv;
86 size_t blocksize;
87 };
88
89 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
90 {
91 switch (alg) {
92 case QCRYPTO_CIPHER_ALG_DES_RFB:
93 case QCRYPTO_CIPHER_ALG_AES_128:
94 case QCRYPTO_CIPHER_ALG_AES_192:
95 case QCRYPTO_CIPHER_ALG_AES_256:
96 case QCRYPTO_CIPHER_ALG_CAST5_128:
97 return true;
98 default:
99 return false;
100 }
101 }
102
103
104 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
105 QCryptoCipherMode mode,
106 const uint8_t *key, size_t nkey,
107 Error **errp)
108 {
109 QCryptoCipher *cipher;
110 QCryptoCipherNettle *ctx;
111 uint8_t *rfbkey;
112
113 switch (mode) {
114 case QCRYPTO_CIPHER_MODE_ECB:
115 case QCRYPTO_CIPHER_MODE_CBC:
116 break;
117 default:
118 error_setg(errp, "Unsupported cipher mode %d", mode);
119 return NULL;
120 }
121
122 if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) {
123 return NULL;
124 }
125
126 cipher = g_new0(QCryptoCipher, 1);
127 cipher->alg = alg;
128 cipher->mode = mode;
129
130 ctx = g_new0(QCryptoCipherNettle, 1);
131
132 switch (alg) {
133 case QCRYPTO_CIPHER_ALG_DES_RFB:
134 ctx->ctx_encrypt = g_new0(struct des_ctx, 1);
135 ctx->ctx_decrypt = NULL; /* 1 ctx can do both */
136 rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
137 des_set_key(ctx->ctx_encrypt, rfbkey);
138 g_free(rfbkey);
139
140 ctx->alg_encrypt = des_encrypt_wrapper;
141 ctx->alg_decrypt = des_decrypt_wrapper;
142
143 ctx->blocksize = DES_BLOCK_SIZE;
144 break;
145
146 case QCRYPTO_CIPHER_ALG_AES_128:
147 case QCRYPTO_CIPHER_ALG_AES_192:
148 case QCRYPTO_CIPHER_ALG_AES_256:
149 ctx->ctx_encrypt = g_new0(struct aes_ctx, 1);
150 ctx->ctx_decrypt = g_new0(struct aes_ctx, 1);
151
152 aes_set_encrypt_key(ctx->ctx_encrypt, nkey, key);
153 aes_set_decrypt_key(ctx->ctx_decrypt, nkey, key);
154
155 ctx->alg_encrypt = aes_encrypt_wrapper;
156 ctx->alg_decrypt = aes_decrypt_wrapper;
157
158 ctx->blocksize = AES_BLOCK_SIZE;
159 break;
160
161 case QCRYPTO_CIPHER_ALG_CAST5_128:
162 ctx->ctx_encrypt = g_new0(struct cast128_ctx, 1);
163 ctx->ctx_decrypt = NULL; /* 1 ctx can do both */
164
165 cast5_set_key(ctx->ctx_encrypt, nkey, key);
166
167 ctx->alg_encrypt = cast128_encrypt_wrapper;
168 ctx->alg_decrypt = cast128_decrypt_wrapper;
169
170 ctx->blocksize = CAST128_BLOCK_SIZE;
171 break;
172 default:
173 error_setg(errp, "Unsupported cipher algorithm %d", alg);
174 goto error;
175 }
176
177 ctx->iv = g_new0(uint8_t, ctx->blocksize);
178 cipher->opaque = ctx;
179
180 return cipher;
181
182 error:
183 g_free(cipher);
184 g_free(ctx);
185 return NULL;
186 }
187
188
189 void qcrypto_cipher_free(QCryptoCipher *cipher)
190 {
191 QCryptoCipherNettle *ctx;
192
193 if (!cipher) {
194 return;
195 }
196
197 ctx = cipher->opaque;
198 g_free(ctx->iv);
199 g_free(ctx->ctx_encrypt);
200 g_free(ctx->ctx_decrypt);
201 g_free(ctx);
202 g_free(cipher);
203 }
204
205
206 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
207 const void *in,
208 void *out,
209 size_t len,
210 Error **errp)
211 {
212 QCryptoCipherNettle *ctx = cipher->opaque;
213
214 if (len % ctx->blocksize) {
215 error_setg(errp, "Length %zu must be a multiple of block size %zu",
216 len, ctx->blocksize);
217 return -1;
218 }
219
220 switch (cipher->mode) {
221 case QCRYPTO_CIPHER_MODE_ECB:
222 ctx->alg_encrypt(ctx->ctx_encrypt, len, out, in);
223 break;
224
225 case QCRYPTO_CIPHER_MODE_CBC:
226 cbc_encrypt(ctx->ctx_encrypt, ctx->alg_encrypt,
227 ctx->blocksize, ctx->iv,
228 len, out, in);
229 break;
230 default:
231 error_setg(errp, "Unsupported cipher algorithm %d",
232 cipher->alg);
233 return -1;
234 }
235 return 0;
236 }
237
238
239 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
240 const void *in,
241 void *out,
242 size_t len,
243 Error **errp)
244 {
245 QCryptoCipherNettle *ctx = cipher->opaque;
246
247 if (len % ctx->blocksize) {
248 error_setg(errp, "Length %zu must be a multiple of block size %zu",
249 len, ctx->blocksize);
250 return -1;
251 }
252
253 switch (cipher->mode) {
254 case QCRYPTO_CIPHER_MODE_ECB:
255 ctx->alg_decrypt(ctx->ctx_decrypt ? ctx->ctx_decrypt : ctx->ctx_encrypt,
256 len, out, in);
257 break;
258
259 case QCRYPTO_CIPHER_MODE_CBC:
260 cbc_decrypt(ctx->ctx_decrypt ? ctx->ctx_decrypt : ctx->ctx_encrypt,
261 ctx->alg_decrypt, ctx->blocksize, ctx->iv,
262 len, out, in);
263 break;
264 default:
265 error_setg(errp, "Unsupported cipher algorithm %d",
266 cipher->alg);
267 return -1;
268 }
269 return 0;
270 }
271
272 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
273 const uint8_t *iv, size_t niv,
274 Error **errp)
275 {
276 QCryptoCipherNettle *ctx = cipher->opaque;
277 if (niv != ctx->blocksize) {
278 error_setg(errp, "Expected IV size %zu not %zu",
279 ctx->blocksize, niv);
280 return -1;
281 }
282 memcpy(ctx->iv, iv, niv);
283 return 0;
284 }