2 * QEMU Crypto cipher built-in 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.1 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 "crypto/aes.h"
22 #include "crypto/desrfb.h"
23 #include "crypto/xts.h"
25 static const struct QCryptoCipherDriver qcrypto_cipher_lib_driver;
27 typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext;
28 struct QCryptoCipherBuiltinAESContext {
32 typedef struct QCryptoCipherBuiltinAES QCryptoCipherBuiltinAES;
33 struct QCryptoCipherBuiltinAES {
34 QCryptoCipherBuiltinAESContext key;
35 QCryptoCipherBuiltinAESContext key_tweak;
36 uint8_t iv[AES_BLOCK_SIZE];
38 typedef struct QCryptoCipherBuiltinDESRFB QCryptoCipherBuiltinDESRFB;
39 struct QCryptoCipherBuiltinDESRFB {
44 typedef struct QCryptoCipherBuiltin QCryptoCipherBuiltin;
45 struct QCryptoCipherBuiltin {
49 QCryptoCipherBuiltinAES aes;
50 QCryptoCipherBuiltinDESRFB desrfb;
53 void (*free)(QCryptoCipher *cipher);
54 int (*setiv)(QCryptoCipher *cipher,
55 const uint8_t *iv, size_t niv,
57 int (*encrypt)(QCryptoCipher *cipher,
62 int (*decrypt)(QCryptoCipher *cipher,
70 static void qcrypto_cipher_free_aes(QCryptoCipher *cipher)
75 static void do_aes_encrypt_ecb(const void *vctx,
80 const QCryptoCipherBuiltinAESContext *ctx = vctx;
82 /* We have already verified that len % AES_BLOCK_SIZE == 0. */
84 AES_encrypt(in, out, &ctx->enc);
86 out += AES_BLOCK_SIZE;
87 len -= AES_BLOCK_SIZE;
91 static void do_aes_decrypt_ecb(const void *vctx,
96 const QCryptoCipherBuiltinAESContext *ctx = vctx;
98 /* We have already verified that len % AES_BLOCK_SIZE == 0. */
100 AES_decrypt(in, out, &ctx->dec);
101 in += AES_BLOCK_SIZE;
102 out += AES_BLOCK_SIZE;
103 len -= AES_BLOCK_SIZE;
107 static void do_aes_encrypt_cbc(const AES_KEY *key,
113 uint8_t tmp[AES_BLOCK_SIZE];
116 /* We have already verified that len % AES_BLOCK_SIZE == 0. */
118 for (n = 0; n < AES_BLOCK_SIZE; ++n) {
119 tmp[n] = in[n] ^ ivec[n];
121 AES_encrypt(tmp, out, key);
122 memcpy(ivec, out, AES_BLOCK_SIZE);
123 len -= AES_BLOCK_SIZE;
124 in += AES_BLOCK_SIZE;
125 out += AES_BLOCK_SIZE;
129 static void do_aes_decrypt_cbc(const AES_KEY *key,
135 uint8_t tmp[AES_BLOCK_SIZE];
138 /* We have already verified that len % AES_BLOCK_SIZE == 0. */
140 memcpy(tmp, in, AES_BLOCK_SIZE);
141 AES_decrypt(in, out, key);
142 for (n = 0; n < AES_BLOCK_SIZE; ++n) {
145 memcpy(ivec, tmp, AES_BLOCK_SIZE);
146 len -= AES_BLOCK_SIZE;
147 in += AES_BLOCK_SIZE;
148 out += AES_BLOCK_SIZE;
152 static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher,
158 QCryptoCipherBuiltin *ctxt
159 = container_of(cipher, QCryptoCipherBuiltin, base);
161 switch (cipher->mode) {
162 case QCRYPTO_CIPHER_MODE_ECB:
163 do_aes_encrypt_ecb(&ctxt->state.aes.key, len, out, in);
165 case QCRYPTO_CIPHER_MODE_CBC:
166 do_aes_encrypt_cbc(&ctxt->state.aes.key.enc, len, out, in,
169 case QCRYPTO_CIPHER_MODE_XTS:
170 xts_encrypt(&ctxt->state.aes.key,
171 &ctxt->state.aes.key_tweak,
178 g_assert_not_reached();
185 static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher,
191 QCryptoCipherBuiltin *ctxt
192 = container_of(cipher, QCryptoCipherBuiltin, base);
194 switch (cipher->mode) {
195 case QCRYPTO_CIPHER_MODE_ECB:
196 do_aes_decrypt_ecb(&ctxt->state.aes.key, len, out, in);
198 case QCRYPTO_CIPHER_MODE_CBC:
199 do_aes_decrypt_cbc(&ctxt->state.aes.key.dec, len, out, in,
202 case QCRYPTO_CIPHER_MODE_XTS:
203 xts_decrypt(&ctxt->state.aes.key,
204 &ctxt->state.aes.key_tweak,
211 g_assert_not_reached();
217 static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
218 const uint8_t *iv, size_t niv,
221 QCryptoCipherBuiltin *ctxt
222 = container_of(cipher, QCryptoCipherBuiltin, base);
224 if (niv != AES_BLOCK_SIZE) {
225 error_setg(errp, "IV must be %d bytes not %zu",
226 AES_BLOCK_SIZE, niv);
230 memcpy(ctxt->state.aes.iv, iv, AES_BLOCK_SIZE);
238 static QCryptoCipher *
239 qcrypto_cipher_init_aes(QCryptoCipherMode mode,
240 const uint8_t *key, size_t nkey,
243 QCryptoCipherBuiltin *ctxt;
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_str(mode));
253 ctxt = g_new0(QCryptoCipherBuiltin, 1);
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");
261 if (AES_set_decrypt_key(key, nkey * 4, &ctxt->state.aes.key.dec) != 0) {
262 error_setg(errp, "Failed to set decryption key");
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");
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");
278 if (AES_set_encrypt_key(key, nkey * 8, &ctxt->state.aes.key.enc) != 0) {
279 error_setg(errp, "Failed to set encryption key");
283 if (AES_set_decrypt_key(key, nkey * 8, &ctxt->state.aes.key.dec) != 0) {
284 error_setg(errp, "Failed to set decryption key");
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;
295 ctxt->base.driver = &qcrypto_cipher_lib_driver;
304 static void qcrypto_cipher_free_des_rfb(QCryptoCipher *cipher)
306 QCryptoCipherBuiltin *ctxt
307 = container_of(cipher, QCryptoCipherBuiltin, base);
309 g_free(ctxt->state.desrfb.key);
314 static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher,
320 QCryptoCipherBuiltin *ctxt
321 = container_of(cipher, QCryptoCipherBuiltin, base);
325 error_setg(errp, "Buffer size must be multiple of 8 not %zu",
330 deskey(ctxt->state.desrfb.key, EN0);
332 for (i = 0; i < len; i += 8) {
333 des((void *)in + i, out + i);
340 static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher,
346 QCryptoCipherBuiltin *ctxt
347 = container_of(cipher, QCryptoCipherBuiltin, base);
351 error_setg(errp, "Buffer size must be multiple of 8 not %zu",
356 deskey(ctxt->state.desrfb.key, DE1);
358 for (i = 0; i < len; i += 8) {
359 des((void *)in + i, out + i);
366 static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher,
367 const uint8_t *iv, size_t niv,
370 error_setg(errp, "Setting IV is not supported");
375 static QCryptoCipher *
376 qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode,
377 const uint8_t *key, size_t nkey,
380 QCryptoCipherBuiltin *ctxt;
382 if (mode != QCRYPTO_CIPHER_MODE_ECB) {
383 error_setg(errp, "Unsupported cipher mode %s",
384 QCryptoCipherMode_str(mode));
388 ctxt = g_new0(QCryptoCipherBuiltin, 1);
390 ctxt->state.desrfb.key = g_new0(uint8_t, nkey);
391 memcpy(ctxt->state.desrfb.key, key, nkey);
392 ctxt->state.desrfb.nkey = nkey;
395 ctxt->free = qcrypto_cipher_free_des_rfb;
396 ctxt->setiv = qcrypto_cipher_setiv_des_rfb;
397 ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb;
398 ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb;
400 ctxt->base.driver = &qcrypto_cipher_lib_driver;
405 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
406 QCryptoCipherMode mode)
409 case QCRYPTO_CIPHER_ALG_DES_RFB:
410 case QCRYPTO_CIPHER_ALG_AES_128:
411 case QCRYPTO_CIPHER_ALG_AES_192:
412 case QCRYPTO_CIPHER_ALG_AES_256:
419 case QCRYPTO_CIPHER_MODE_ECB:
420 case QCRYPTO_CIPHER_MODE_CBC:
421 case QCRYPTO_CIPHER_MODE_XTS:
423 case QCRYPTO_CIPHER_MODE_CTR:
431 static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
432 QCryptoCipherMode mode,
438 case QCRYPTO_CIPHER_MODE_ECB:
439 case QCRYPTO_CIPHER_MODE_CBC:
440 case QCRYPTO_CIPHER_MODE_XTS:
443 error_setg(errp, "Unsupported cipher mode %s",
444 QCryptoCipherMode_str(mode));
448 if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
453 case QCRYPTO_CIPHER_ALG_DES_RFB:
454 return qcrypto_cipher_init_des_rfb(mode, key, nkey, errp);
455 case QCRYPTO_CIPHER_ALG_AES_128:
456 case QCRYPTO_CIPHER_ALG_AES_192:
457 case QCRYPTO_CIPHER_ALG_AES_256:
458 return qcrypto_cipher_init_aes(mode, key, nkey, errp);
461 "Unsupported cipher algorithm %s",
462 QCryptoCipherAlgorithm_str(alg));
468 qcrypto_builtin_cipher_ctx_free(QCryptoCipher *cipher)
470 QCryptoCipherBuiltin *ctxt
471 = container_of(cipher, QCryptoCipherBuiltin, base);
478 qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher,
484 QCryptoCipherBuiltin *ctxt
485 = container_of(cipher, QCryptoCipherBuiltin, base);
487 if (len & (ctxt->blocksize - 1)) {
488 error_setg(errp, "Length %zu must be a multiple of block size %zu",
489 len, ctxt->blocksize);
493 return ctxt->encrypt(cipher, in, out, len, errp);
498 qcrypto_builtin_cipher_decrypt(QCryptoCipher *cipher,
504 QCryptoCipherBuiltin *ctxt
505 = container_of(cipher, QCryptoCipherBuiltin, base);
507 if (len & (ctxt->blocksize - 1)) {
508 error_setg(errp, "Length %zu must be a multiple of block size %zu",
509 len, ctxt->blocksize);
513 return ctxt->decrypt(cipher, in, out, len, errp);
518 qcrypto_builtin_cipher_setiv(QCryptoCipher *cipher,
519 const uint8_t *iv, size_t niv,
522 QCryptoCipherBuiltin *ctxt
523 = container_of(cipher, QCryptoCipherBuiltin, base);
525 return ctxt->setiv(cipher, iv, niv, errp);
529 static const struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
530 .cipher_encrypt = qcrypto_builtin_cipher_encrypt,
531 .cipher_decrypt = qcrypto_builtin_cipher_decrypt,
532 .cipher_setiv = qcrypto_builtin_cipher_setiv,
533 .cipher_free = qcrypto_builtin_cipher_ctx_free,