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 typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext;
26 struct QCryptoCipherBuiltinAESContext {
30 typedef struct QCryptoCipherBuiltinAES QCryptoCipherBuiltinAES;
31 struct QCryptoCipherBuiltinAES {
32 QCryptoCipherBuiltinAESContext key;
33 QCryptoCipherBuiltinAESContext key_tweak;
34 uint8_t iv[AES_BLOCK_SIZE];
36 typedef struct QCryptoCipherBuiltinDESRFB QCryptoCipherBuiltinDESRFB;
37 struct QCryptoCipherBuiltinDESRFB {
42 typedef struct QCryptoCipherBuiltin QCryptoCipherBuiltin;
43 struct QCryptoCipherBuiltin {
45 QCryptoCipherBuiltinAES aes;
46 QCryptoCipherBuiltinDESRFB desrfb;
49 void (*free)(QCryptoCipher *cipher);
50 int (*setiv)(QCryptoCipher *cipher,
51 const uint8_t *iv, size_t niv,
53 int (*encrypt)(QCryptoCipher *cipher,
58 int (*decrypt)(QCryptoCipher *cipher,
66 static void qcrypto_cipher_free_aes(QCryptoCipher *cipher)
68 QCryptoCipherBuiltin *ctxt = cipher->opaque;
71 cipher->opaque = NULL;
75 static void qcrypto_cipher_aes_ecb_encrypt(const AES_KEY *key,
80 const uint8_t *inptr = in;
81 uint8_t *outptr = out;
83 if (len > AES_BLOCK_SIZE) {
84 AES_encrypt(inptr, outptr, key);
85 inptr += AES_BLOCK_SIZE;
86 outptr += AES_BLOCK_SIZE;
87 len -= AES_BLOCK_SIZE;
89 uint8_t tmp1[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
90 memcpy(tmp1, inptr, len);
91 /* Fill with 0 to avoid valgrind uninitialized reads */
92 memset(tmp1 + len, 0, sizeof(tmp1) - len);
93 AES_encrypt(tmp1, tmp2, key);
94 memcpy(outptr, tmp2, len);
101 static void qcrypto_cipher_aes_ecb_decrypt(const AES_KEY *key,
106 const uint8_t *inptr = in;
107 uint8_t *outptr = out;
109 if (len > AES_BLOCK_SIZE) {
110 AES_decrypt(inptr, outptr, key);
111 inptr += AES_BLOCK_SIZE;
112 outptr += AES_BLOCK_SIZE;
113 len -= AES_BLOCK_SIZE;
115 uint8_t tmp1[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
116 memcpy(tmp1, inptr, len);
117 /* Fill with 0 to avoid valgrind uninitialized reads */
118 memset(tmp1 + len, 0, sizeof(tmp1) - len);
119 AES_decrypt(tmp1, tmp2, key);
120 memcpy(outptr, tmp2, len);
127 static void qcrypto_cipher_aes_xts_encrypt(const void *ctx,
132 const QCryptoCipherBuiltinAESContext *aesctx = ctx;
134 qcrypto_cipher_aes_ecb_encrypt(&aesctx->enc, src, dst, length);
138 static void qcrypto_cipher_aes_xts_decrypt(const void *ctx,
143 const QCryptoCipherBuiltinAESContext *aesctx = ctx;
145 qcrypto_cipher_aes_ecb_decrypt(&aesctx->dec, src, dst, length);
149 static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher,
155 QCryptoCipherBuiltin *ctxt = cipher->opaque;
157 switch (cipher->mode) {
158 case QCRYPTO_CIPHER_MODE_ECB:
159 qcrypto_cipher_aes_ecb_encrypt(&ctxt->state.aes.key.enc,
162 case QCRYPTO_CIPHER_MODE_CBC:
163 AES_cbc_encrypt(in, out, len,
164 &ctxt->state.aes.key.enc,
165 ctxt->state.aes.iv, 1);
167 case QCRYPTO_CIPHER_MODE_XTS:
168 xts_encrypt(&ctxt->state.aes.key,
169 &ctxt->state.aes.key_tweak,
170 qcrypto_cipher_aes_xts_encrypt,
171 qcrypto_cipher_aes_xts_decrypt,
176 g_assert_not_reached();
183 static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher,
189 QCryptoCipherBuiltin *ctxt = cipher->opaque;
191 switch (cipher->mode) {
192 case QCRYPTO_CIPHER_MODE_ECB:
193 qcrypto_cipher_aes_ecb_decrypt(&ctxt->state.aes.key.dec,
196 case QCRYPTO_CIPHER_MODE_CBC:
197 AES_cbc_encrypt(in, out, len,
198 &ctxt->state.aes.key.dec,
199 ctxt->state.aes.iv, 0);
201 case QCRYPTO_CIPHER_MODE_XTS:
202 xts_decrypt(&ctxt->state.aes.key,
203 &ctxt->state.aes.key_tweak,
204 qcrypto_cipher_aes_xts_encrypt,
205 qcrypto_cipher_aes_xts_decrypt,
210 g_assert_not_reached();
216 static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
217 const uint8_t *iv, size_t niv,
220 QCryptoCipherBuiltin *ctxt = cipher->opaque;
221 if (niv != AES_BLOCK_SIZE) {
222 error_setg(errp, "IV must be %d bytes not %zu",
223 AES_BLOCK_SIZE, niv);
227 memcpy(ctxt->state.aes.iv, iv, AES_BLOCK_SIZE);
235 static QCryptoCipherBuiltin *
236 qcrypto_cipher_init_aes(QCryptoCipherMode mode,
237 const uint8_t *key, size_t nkey,
240 QCryptoCipherBuiltin *ctxt;
242 if (mode != QCRYPTO_CIPHER_MODE_CBC &&
243 mode != QCRYPTO_CIPHER_MODE_ECB &&
244 mode != QCRYPTO_CIPHER_MODE_XTS) {
245 error_setg(errp, "Unsupported cipher mode %s",
246 QCryptoCipherMode_str(mode));
250 ctxt = g_new0(QCryptoCipherBuiltin, 1);
252 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
253 if (AES_set_encrypt_key(key, nkey * 4, &ctxt->state.aes.key.enc) != 0) {
254 error_setg(errp, "Failed to set encryption key");
258 if (AES_set_decrypt_key(key, nkey * 4, &ctxt->state.aes.key.dec) != 0) {
259 error_setg(errp, "Failed to set decryption key");
263 if (AES_set_encrypt_key(key + (nkey / 2), nkey * 4,
264 &ctxt->state.aes.key_tweak.enc) != 0) {
265 error_setg(errp, "Failed to set encryption key");
269 if (AES_set_decrypt_key(key + (nkey / 2), nkey * 4,
270 &ctxt->state.aes.key_tweak.dec) != 0) {
271 error_setg(errp, "Failed to set decryption key");
275 if (AES_set_encrypt_key(key, nkey * 8, &ctxt->state.aes.key.enc) != 0) {
276 error_setg(errp, "Failed to set encryption key");
280 if (AES_set_decrypt_key(key, nkey * 8, &ctxt->state.aes.key.dec) != 0) {
281 error_setg(errp, "Failed to set decryption key");
286 ctxt->blocksize = AES_BLOCK_SIZE;
287 ctxt->free = qcrypto_cipher_free_aes;
288 ctxt->setiv = qcrypto_cipher_setiv_aes;
289 ctxt->encrypt = qcrypto_cipher_encrypt_aes;
290 ctxt->decrypt = qcrypto_cipher_decrypt_aes;
300 static void qcrypto_cipher_free_des_rfb(QCryptoCipher *cipher)
302 QCryptoCipherBuiltin *ctxt = cipher->opaque;
304 g_free(ctxt->state.desrfb.key);
306 cipher->opaque = NULL;
310 static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher,
316 QCryptoCipherBuiltin *ctxt = cipher->opaque;
320 error_setg(errp, "Buffer size must be multiple of 8 not %zu",
325 deskey(ctxt->state.desrfb.key, EN0);
327 for (i = 0; i < len; i += 8) {
328 des((void *)in + i, out + i);
335 static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher,
341 QCryptoCipherBuiltin *ctxt = cipher->opaque;
345 error_setg(errp, "Buffer size must be multiple of 8 not %zu",
350 deskey(ctxt->state.desrfb.key, DE1);
352 for (i = 0; i < len; i += 8) {
353 des((void *)in + i, out + i);
360 static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher,
361 const uint8_t *iv, size_t niv,
364 error_setg(errp, "Setting IV is not supported");
369 static QCryptoCipherBuiltin *
370 qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode,
371 const uint8_t *key, size_t nkey,
374 QCryptoCipherBuiltin *ctxt;
376 if (mode != QCRYPTO_CIPHER_MODE_ECB) {
377 error_setg(errp, "Unsupported cipher mode %s",
378 QCryptoCipherMode_str(mode));
382 ctxt = g_new0(QCryptoCipherBuiltin, 1);
384 ctxt->state.desrfb.key = g_new0(uint8_t, nkey);
385 memcpy(ctxt->state.desrfb.key, key, nkey);
386 ctxt->state.desrfb.nkey = nkey;
389 ctxt->free = qcrypto_cipher_free_des_rfb;
390 ctxt->setiv = qcrypto_cipher_setiv_des_rfb;
391 ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb;
392 ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb;
398 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
399 QCryptoCipherMode mode)
402 case QCRYPTO_CIPHER_ALG_DES_RFB:
403 case QCRYPTO_CIPHER_ALG_AES_128:
404 case QCRYPTO_CIPHER_ALG_AES_192:
405 case QCRYPTO_CIPHER_ALG_AES_256:
412 case QCRYPTO_CIPHER_MODE_ECB:
413 case QCRYPTO_CIPHER_MODE_CBC:
414 case QCRYPTO_CIPHER_MODE_XTS:
416 case QCRYPTO_CIPHER_MODE_CTR:
424 static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
425 QCryptoCipherMode mode,
430 QCryptoCipherBuiltin *ctxt;
433 case QCRYPTO_CIPHER_MODE_ECB:
434 case QCRYPTO_CIPHER_MODE_CBC:
435 case QCRYPTO_CIPHER_MODE_XTS:
438 error_setg(errp, "Unsupported cipher mode %s",
439 QCryptoCipherMode_str(mode));
443 if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
448 case QCRYPTO_CIPHER_ALG_DES_RFB:
449 ctxt = qcrypto_cipher_init_des_rfb(mode, key, nkey, errp);
451 case QCRYPTO_CIPHER_ALG_AES_128:
452 case QCRYPTO_CIPHER_ALG_AES_192:
453 case QCRYPTO_CIPHER_ALG_AES_256:
454 ctxt = qcrypto_cipher_init_aes(mode, key, nkey, errp);
458 "Unsupported cipher algorithm %s",
459 QCryptoCipherAlgorithm_str(alg));
467 qcrypto_builtin_cipher_ctx_free(QCryptoCipher *cipher)
469 QCryptoCipherBuiltin *ctxt;
471 ctxt = cipher->opaque;
477 qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher,
483 QCryptoCipherBuiltin *ctxt = cipher->opaque;
485 if (len & (ctxt->blocksize - 1)) {
486 error_setg(errp, "Length %zu must be a multiple of block size %zu",
487 len, ctxt->blocksize);
491 return ctxt->encrypt(cipher, in, out, len, errp);
496 qcrypto_builtin_cipher_decrypt(QCryptoCipher *cipher,
502 QCryptoCipherBuiltin *ctxt = cipher->opaque;
504 if (len & (ctxt->blocksize - 1)) {
505 error_setg(errp, "Length %zu must be a multiple of block size %zu",
506 len, ctxt->blocksize);
510 return ctxt->decrypt(cipher, in, out, len, errp);
515 qcrypto_builtin_cipher_setiv(QCryptoCipher *cipher,
516 const uint8_t *iv, size_t niv,
519 QCryptoCipherBuiltin *ctxt = cipher->opaque;
521 return ctxt->setiv(cipher, iv, niv, errp);
525 static const struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
526 .cipher_encrypt = qcrypto_builtin_cipher_encrypt,
527 .cipher_decrypt = qcrypto_builtin_cipher_decrypt,
528 .cipher_setiv = qcrypto_builtin_cipher_setiv,
529 .cipher_free = qcrypto_builtin_cipher_ctx_free,