]> git.proxmox.com Git - mirror_qemu.git/blob - crypto/cipher-builtin.c
crypto: cipher: introduce qcrypto_cipher_ctx_new for builtin-backend
[mirror_qemu.git] / crypto / cipher-builtin.c
1 /*
2 * QEMU Crypto cipher built-in 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 "crypto/aes.h"
23 #include "crypto/desrfb.h"
24 #include "crypto/xts.h"
25
26 typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext;
27 struct QCryptoCipherBuiltinAESContext {
28 AES_KEY enc;
29 AES_KEY dec;
30 };
31 typedef struct QCryptoCipherBuiltinAES QCryptoCipherBuiltinAES;
32 struct QCryptoCipherBuiltinAES {
33 QCryptoCipherBuiltinAESContext key;
34 QCryptoCipherBuiltinAESContext key_tweak;
35 uint8_t iv[AES_BLOCK_SIZE];
36 };
37 typedef struct QCryptoCipherBuiltinDESRFB QCryptoCipherBuiltinDESRFB;
38 struct QCryptoCipherBuiltinDESRFB {
39 uint8_t *key;
40 size_t nkey;
41 };
42
43 typedef struct QCryptoCipherBuiltin QCryptoCipherBuiltin;
44 struct QCryptoCipherBuiltin {
45 union {
46 QCryptoCipherBuiltinAES aes;
47 QCryptoCipherBuiltinDESRFB desrfb;
48 } state;
49 size_t blocksize;
50 void (*free)(QCryptoCipher *cipher);
51 int (*setiv)(QCryptoCipher *cipher,
52 const uint8_t *iv, size_t niv,
53 Error **errp);
54 int (*encrypt)(QCryptoCipher *cipher,
55 const void *in,
56 void *out,
57 size_t len,
58 Error **errp);
59 int (*decrypt)(QCryptoCipher *cipher,
60 const void *in,
61 void *out,
62 size_t len,
63 Error **errp);
64 };
65
66
67 static void qcrypto_cipher_free_aes(QCryptoCipher *cipher)
68 {
69 QCryptoCipherBuiltin *ctxt = cipher->opaque;
70
71 g_free(ctxt);
72 cipher->opaque = NULL;
73 }
74
75
76 static void qcrypto_cipher_aes_ecb_encrypt(AES_KEY *key,
77 const void *in,
78 void *out,
79 size_t len)
80 {
81 const uint8_t *inptr = in;
82 uint8_t *outptr = out;
83 while (len) {
84 if (len > AES_BLOCK_SIZE) {
85 AES_encrypt(inptr, outptr, key);
86 inptr += AES_BLOCK_SIZE;
87 outptr += AES_BLOCK_SIZE;
88 len -= AES_BLOCK_SIZE;
89 } else {
90 uint8_t tmp1[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
91 memcpy(tmp1, inptr, len);
92 /* Fill with 0 to avoid valgrind uninitialized reads */
93 memset(tmp1 + len, 0, sizeof(tmp1) - len);
94 AES_encrypt(tmp1, tmp2, key);
95 memcpy(outptr, tmp2, len);
96 len = 0;
97 }
98 }
99 }
100
101
102 static void qcrypto_cipher_aes_ecb_decrypt(AES_KEY *key,
103 const void *in,
104 void *out,
105 size_t len)
106 {
107 const uint8_t *inptr = in;
108 uint8_t *outptr = out;
109 while (len) {
110 if (len > AES_BLOCK_SIZE) {
111 AES_decrypt(inptr, outptr, key);
112 inptr += AES_BLOCK_SIZE;
113 outptr += AES_BLOCK_SIZE;
114 len -= AES_BLOCK_SIZE;
115 } else {
116 uint8_t tmp1[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
117 memcpy(tmp1, inptr, len);
118 /* Fill with 0 to avoid valgrind uninitialized reads */
119 memset(tmp1 + len, 0, sizeof(tmp1) - len);
120 AES_decrypt(tmp1, tmp2, key);
121 memcpy(outptr, tmp2, len);
122 len = 0;
123 }
124 }
125 }
126
127
128 static void qcrypto_cipher_aes_xts_encrypt(const void *ctx,
129 size_t length,
130 uint8_t *dst,
131 const uint8_t *src)
132 {
133 const QCryptoCipherBuiltinAESContext *aesctx = ctx;
134
135 qcrypto_cipher_aes_ecb_encrypt((AES_KEY *)&aesctx->enc,
136 src, dst, length);
137 }
138
139
140 static void qcrypto_cipher_aes_xts_decrypt(const void *ctx,
141 size_t length,
142 uint8_t *dst,
143 const uint8_t *src)
144 {
145 const QCryptoCipherBuiltinAESContext *aesctx = ctx;
146
147 qcrypto_cipher_aes_ecb_decrypt((AES_KEY *)&aesctx->dec,
148 src, dst, length);
149 }
150
151
152 static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher,
153 const void *in,
154 void *out,
155 size_t len,
156 Error **errp)
157 {
158 QCryptoCipherBuiltin *ctxt = cipher->opaque;
159
160 switch (cipher->mode) {
161 case QCRYPTO_CIPHER_MODE_ECB:
162 qcrypto_cipher_aes_ecb_encrypt(&ctxt->state.aes.key.enc,
163 in, out, len);
164 break;
165 case QCRYPTO_CIPHER_MODE_CBC:
166 AES_cbc_encrypt(in, out, len,
167 &ctxt->state.aes.key.enc,
168 ctxt->state.aes.iv, 1);
169 break;
170 case QCRYPTO_CIPHER_MODE_XTS:
171 xts_encrypt(&ctxt->state.aes.key,
172 &ctxt->state.aes.key_tweak,
173 qcrypto_cipher_aes_xts_encrypt,
174 qcrypto_cipher_aes_xts_decrypt,
175 ctxt->state.aes.iv,
176 len, out, in);
177 break;
178 default:
179 g_assert_not_reached();
180 }
181
182 return 0;
183 }
184
185
186 static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher,
187 const void *in,
188 void *out,
189 size_t len,
190 Error **errp)
191 {
192 QCryptoCipherBuiltin *ctxt = cipher->opaque;
193
194 switch (cipher->mode) {
195 case QCRYPTO_CIPHER_MODE_ECB:
196 qcrypto_cipher_aes_ecb_decrypt(&ctxt->state.aes.key.dec,
197 in, out, len);
198 break;
199 case QCRYPTO_CIPHER_MODE_CBC:
200 AES_cbc_encrypt(in, out, len,
201 &ctxt->state.aes.key.dec,
202 ctxt->state.aes.iv, 0);
203 break;
204 case QCRYPTO_CIPHER_MODE_XTS:
205 xts_decrypt(&ctxt->state.aes.key,
206 &ctxt->state.aes.key_tweak,
207 qcrypto_cipher_aes_xts_encrypt,
208 qcrypto_cipher_aes_xts_decrypt,
209 ctxt->state.aes.iv,
210 len, out, in);
211 break;
212 default:
213 g_assert_not_reached();
214 }
215
216 return 0;
217 }
218
219 static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
220 const uint8_t *iv, size_t niv,
221 Error **errp)
222 {
223 QCryptoCipherBuiltin *ctxt = cipher->opaque;
224 if (niv != AES_BLOCK_SIZE) {
225 error_setg(errp, "IV must be %d bytes not %zu",
226 AES_BLOCK_SIZE, niv);
227 return -1;
228 }
229
230 memcpy(ctxt->state.aes.iv, iv, AES_BLOCK_SIZE);
231
232 return 0;
233 }
234
235
236
237
238 static QCryptoCipherBuiltin *
239 qcrypto_cipher_init_aes(QCryptoCipherMode mode,
240 const uint8_t *key, size_t nkey,
241 Error **errp)
242 {
243 QCryptoCipherBuiltin *ctxt;
244
245 if (mode != QCRYPTO_CIPHER_MODE_CBC &&
246 mode != QCRYPTO_CIPHER_MODE_ECB &&
247 mode != QCRYPTO_CIPHER_MODE_XTS) {
248 error_setg(errp, "Unsupported cipher mode %s",
249 QCryptoCipherMode_lookup[mode]);
250 return NULL;
251 }
252
253 ctxt = g_new0(QCryptoCipherBuiltin, 1);
254
255 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
256 if (AES_set_encrypt_key(key, nkey * 4, &ctxt->state.aes.key.enc) != 0) {
257 error_setg(errp, "Failed to set encryption key");
258 goto error;
259 }
260
261 if (AES_set_decrypt_key(key, nkey * 4, &ctxt->state.aes.key.dec) != 0) {
262 error_setg(errp, "Failed to set decryption key");
263 goto error;
264 }
265
266 if (AES_set_encrypt_key(key + (nkey / 2), nkey * 4,
267 &ctxt->state.aes.key_tweak.enc) != 0) {
268 error_setg(errp, "Failed to set encryption key");
269 goto error;
270 }
271
272 if (AES_set_decrypt_key(key + (nkey / 2), nkey * 4,
273 &ctxt->state.aes.key_tweak.dec) != 0) {
274 error_setg(errp, "Failed to set decryption key");
275 goto error;
276 }
277 } else {
278 if (AES_set_encrypt_key(key, nkey * 8, &ctxt->state.aes.key.enc) != 0) {
279 error_setg(errp, "Failed to set encryption key");
280 goto error;
281 }
282
283 if (AES_set_decrypt_key(key, nkey * 8, &ctxt->state.aes.key.dec) != 0) {
284 error_setg(errp, "Failed to set decryption key");
285 goto error;
286 }
287 }
288
289 ctxt->blocksize = AES_BLOCK_SIZE;
290 ctxt->free = qcrypto_cipher_free_aes;
291 ctxt->setiv = qcrypto_cipher_setiv_aes;
292 ctxt->encrypt = qcrypto_cipher_encrypt_aes;
293 ctxt->decrypt = qcrypto_cipher_decrypt_aes;
294
295 return ctxt;
296
297 error:
298 g_free(ctxt);
299 return NULL;
300 }
301
302
303 static void qcrypto_cipher_free_des_rfb(QCryptoCipher *cipher)
304 {
305 QCryptoCipherBuiltin *ctxt = cipher->opaque;
306
307 g_free(ctxt->state.desrfb.key);
308 g_free(ctxt);
309 cipher->opaque = NULL;
310 }
311
312
313 static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher,
314 const void *in,
315 void *out,
316 size_t len,
317 Error **errp)
318 {
319 QCryptoCipherBuiltin *ctxt = cipher->opaque;
320 size_t i;
321
322 if (len % 8) {
323 error_setg(errp, "Buffer size must be multiple of 8 not %zu",
324 len);
325 return -1;
326 }
327
328 deskey(ctxt->state.desrfb.key, EN0);
329
330 for (i = 0; i < len; i += 8) {
331 des((void *)in + i, out + i);
332 }
333
334 return 0;
335 }
336
337
338 static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher,
339 const void *in,
340 void *out,
341 size_t len,
342 Error **errp)
343 {
344 QCryptoCipherBuiltin *ctxt = cipher->opaque;
345 size_t i;
346
347 if (len % 8) {
348 error_setg(errp, "Buffer size must be multiple of 8 not %zu",
349 len);
350 return -1;
351 }
352
353 deskey(ctxt->state.desrfb.key, DE1);
354
355 for (i = 0; i < len; i += 8) {
356 des((void *)in + i, out + i);
357 }
358
359 return 0;
360 }
361
362
363 static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher,
364 const uint8_t *iv, size_t niv,
365 Error **errp)
366 {
367 error_setg(errp, "Setting IV is not supported");
368 return -1;
369 }
370
371
372 static QCryptoCipherBuiltin *
373 qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode,
374 const uint8_t *key, size_t nkey,
375 Error **errp)
376 {
377 QCryptoCipherBuiltin *ctxt;
378
379 if (mode != QCRYPTO_CIPHER_MODE_ECB) {
380 error_setg(errp, "Unsupported cipher mode %s",
381 QCryptoCipherMode_lookup[mode]);
382 return NULL;
383 }
384
385 ctxt = g_new0(QCryptoCipherBuiltin, 1);
386
387 ctxt->state.desrfb.key = g_new0(uint8_t, nkey);
388 memcpy(ctxt->state.desrfb.key, key, nkey);
389 ctxt->state.desrfb.nkey = nkey;
390
391 ctxt->blocksize = 8;
392 ctxt->free = qcrypto_cipher_free_des_rfb;
393 ctxt->setiv = qcrypto_cipher_setiv_des_rfb;
394 ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb;
395 ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb;
396
397 return ctxt;
398 }
399
400
401 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
402 QCryptoCipherMode mode)
403 {
404 switch (alg) {
405 case QCRYPTO_CIPHER_ALG_DES_RFB:
406 case QCRYPTO_CIPHER_ALG_AES_128:
407 case QCRYPTO_CIPHER_ALG_AES_192:
408 case QCRYPTO_CIPHER_ALG_AES_256:
409 break;
410 default:
411 return false;
412 }
413
414 switch (mode) {
415 case QCRYPTO_CIPHER_MODE_ECB:
416 case QCRYPTO_CIPHER_MODE_CBC:
417 case QCRYPTO_CIPHER_MODE_XTS:
418 return true;
419 case QCRYPTO_CIPHER_MODE_CTR:
420 return false;
421 default:
422 return false;
423 }
424 }
425
426
427 static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
428 QCryptoCipherMode mode,
429 const uint8_t *key,
430 size_t nkey,
431 Error **errp)
432 {
433 QCryptoCipherBuiltin *ctxt;
434
435 switch (mode) {
436 case QCRYPTO_CIPHER_MODE_ECB:
437 case QCRYPTO_CIPHER_MODE_CBC:
438 case QCRYPTO_CIPHER_MODE_XTS:
439 break;
440 default:
441 error_setg(errp, "Unsupported cipher mode %s",
442 QCryptoCipherMode_lookup[mode]);
443 return NULL;
444 }
445
446 if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
447 return NULL;
448 }
449
450 switch (alg) {
451 case QCRYPTO_CIPHER_ALG_DES_RFB:
452 ctxt = qcrypto_cipher_init_des_rfb(mode, key, nkey, errp);
453 break;
454 case QCRYPTO_CIPHER_ALG_AES_128:
455 case QCRYPTO_CIPHER_ALG_AES_192:
456 case QCRYPTO_CIPHER_ALG_AES_256:
457 ctxt = qcrypto_cipher_init_aes(mode, key, nkey, errp);
458 break;
459 default:
460 error_setg(errp,
461 "Unsupported cipher algorithm %s",
462 QCryptoCipherAlgorithm_lookup[alg]);
463 return NULL;
464 }
465
466 return ctxt;
467 }
468
469 void qcrypto_cipher_free(QCryptoCipher *cipher)
470 {
471 QCryptoCipherBuiltin *ctxt;
472
473 if (!cipher) {
474 return;
475 }
476
477 ctxt = cipher->opaque;
478 ctxt->free(cipher);
479 g_free(cipher);
480 }
481
482
483 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
484 const void *in,
485 void *out,
486 size_t len,
487 Error **errp)
488 {
489 QCryptoCipherBuiltin *ctxt = cipher->opaque;
490
491 if (len % ctxt->blocksize) {
492 error_setg(errp, "Length %zu must be a multiple of block size %zu",
493 len, ctxt->blocksize);
494 return -1;
495 }
496
497 return ctxt->encrypt(cipher, in, out, len, errp);
498 }
499
500
501 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
502 const void *in,
503 void *out,
504 size_t len,
505 Error **errp)
506 {
507 QCryptoCipherBuiltin *ctxt = cipher->opaque;
508
509 if (len % ctxt->blocksize) {
510 error_setg(errp, "Length %zu must be a multiple of block size %zu",
511 len, ctxt->blocksize);
512 return -1;
513 }
514
515 return ctxt->decrypt(cipher, in, out, len, errp);
516 }
517
518
519 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
520 const uint8_t *iv, size_t niv,
521 Error **errp)
522 {
523 QCryptoCipherBuiltin *ctxt = cipher->opaque;
524
525 return ctxt->setiv(cipher, iv, niv, errp);
526 }
527
528
529 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
530 QCryptoCipherMode mode,
531 const uint8_t *key, size_t nkey,
532 Error **errp)
533 {
534 QCryptoCipher *cipher;
535 QCryptoCipherBuiltin *ctxt;
536
537 ctxt = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp);
538 if (!ctxt) {
539 return NULL;
540 }
541
542 cipher = g_new0(QCryptoCipher, 1);
543 cipher->alg = alg;
544 cipher->mode = mode;
545 cipher->opaque = ctxt;
546
547 return cipher;
548 }