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 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 "qemu/osdep.h"
22 #include "crypto/aes.h"
23 #include "crypto/desrfb.h"
24 #include "crypto/xts.h"
26 typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext
;
27 struct QCryptoCipherBuiltinAESContext
{
31 typedef struct QCryptoCipherBuiltinAES QCryptoCipherBuiltinAES
;
32 struct QCryptoCipherBuiltinAES
{
33 QCryptoCipherBuiltinAESContext key
;
34 QCryptoCipherBuiltinAESContext key_tweak
;
35 uint8_t iv
[AES_BLOCK_SIZE
];
37 typedef struct QCryptoCipherBuiltinDESRFB QCryptoCipherBuiltinDESRFB
;
38 struct QCryptoCipherBuiltinDESRFB
{
43 typedef struct QCryptoCipherBuiltin QCryptoCipherBuiltin
;
44 struct QCryptoCipherBuiltin
{
46 QCryptoCipherBuiltinAES aes
;
47 QCryptoCipherBuiltinDESRFB desrfb
;
50 void (*free
)(QCryptoCipher
*cipher
);
51 int (*setiv
)(QCryptoCipher
*cipher
,
52 const uint8_t *iv
, size_t niv
,
54 int (*encrypt
)(QCryptoCipher
*cipher
,
59 int (*decrypt
)(QCryptoCipher
*cipher
,
67 static void qcrypto_cipher_free_aes(QCryptoCipher
*cipher
)
69 QCryptoCipherBuiltin
*ctxt
= cipher
->opaque
;
72 cipher
->opaque
= NULL
;
76 static void qcrypto_cipher_aes_ecb_encrypt(AES_KEY
*key
,
81 const uint8_t *inptr
= in
;
82 uint8_t *outptr
= out
;
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
;
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
);
102 static void qcrypto_cipher_aes_ecb_decrypt(AES_KEY
*key
,
107 const uint8_t *inptr
= in
;
108 uint8_t *outptr
= out
;
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
;
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
);
128 static void qcrypto_cipher_aes_xts_encrypt(const void *ctx
,
133 const QCryptoCipherBuiltinAESContext
*aesctx
= ctx
;
135 qcrypto_cipher_aes_ecb_encrypt((AES_KEY
*)&aesctx
->enc
,
140 static void qcrypto_cipher_aes_xts_decrypt(const void *ctx
,
145 const QCryptoCipherBuiltinAESContext
*aesctx
= ctx
;
147 qcrypto_cipher_aes_ecb_decrypt((AES_KEY
*)&aesctx
->dec
,
152 static int qcrypto_cipher_encrypt_aes(QCryptoCipher
*cipher
,
158 QCryptoCipherBuiltin
*ctxt
= cipher
->opaque
;
160 switch (cipher
->mode
) {
161 case QCRYPTO_CIPHER_MODE_ECB
:
162 qcrypto_cipher_aes_ecb_encrypt(&ctxt
->state
.aes
.key
.enc
,
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);
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
,
179 g_assert_not_reached();
186 static int qcrypto_cipher_decrypt_aes(QCryptoCipher
*cipher
,
192 QCryptoCipherBuiltin
*ctxt
= cipher
->opaque
;
194 switch (cipher
->mode
) {
195 case QCRYPTO_CIPHER_MODE_ECB
:
196 qcrypto_cipher_aes_ecb_decrypt(&ctxt
->state
.aes
.key
.dec
,
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);
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
,
213 g_assert_not_reached();
219 static int qcrypto_cipher_setiv_aes(QCryptoCipher
*cipher
,
220 const uint8_t *iv
, size_t niv
,
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
);
230 memcpy(ctxt
->state
.aes
.iv
, iv
, AES_BLOCK_SIZE
);
238 static int qcrypto_cipher_init_aes(QCryptoCipher
*cipher
,
239 const uint8_t *key
, size_t nkey
,
242 QCryptoCipherBuiltin
*ctxt
;
244 if (cipher
->mode
!= QCRYPTO_CIPHER_MODE_CBC
&&
245 cipher
->mode
!= QCRYPTO_CIPHER_MODE_ECB
&&
246 cipher
->mode
!= QCRYPTO_CIPHER_MODE_XTS
) {
247 error_setg(errp
, "Unsupported cipher mode %s",
248 QCryptoCipherMode_lookup
[cipher
->mode
]);
252 ctxt
= g_new0(QCryptoCipherBuiltin
, 1);
254 if (cipher
->mode
== QCRYPTO_CIPHER_MODE_XTS
) {
255 if (AES_set_encrypt_key(key
, nkey
* 4, &ctxt
->state
.aes
.key
.enc
) != 0) {
256 error_setg(errp
, "Failed to set encryption key");
260 if (AES_set_decrypt_key(key
, nkey
* 4, &ctxt
->state
.aes
.key
.dec
) != 0) {
261 error_setg(errp
, "Failed to set decryption key");
265 if (AES_set_encrypt_key(key
+ (nkey
/ 2), nkey
* 4,
266 &ctxt
->state
.aes
.key_tweak
.enc
) != 0) {
267 error_setg(errp
, "Failed to set encryption key");
271 if (AES_set_decrypt_key(key
+ (nkey
/ 2), nkey
* 4,
272 &ctxt
->state
.aes
.key_tweak
.dec
) != 0) {
273 error_setg(errp
, "Failed to set decryption key");
277 if (AES_set_encrypt_key(key
, nkey
* 8, &ctxt
->state
.aes
.key
.enc
) != 0) {
278 error_setg(errp
, "Failed to set encryption key");
282 if (AES_set_decrypt_key(key
, nkey
* 8, &ctxt
->state
.aes
.key
.dec
) != 0) {
283 error_setg(errp
, "Failed to set decryption key");
288 ctxt
->blocksize
= AES_BLOCK_SIZE
;
289 ctxt
->free
= qcrypto_cipher_free_aes
;
290 ctxt
->setiv
= qcrypto_cipher_setiv_aes
;
291 ctxt
->encrypt
= qcrypto_cipher_encrypt_aes
;
292 ctxt
->decrypt
= qcrypto_cipher_decrypt_aes
;
294 cipher
->opaque
= ctxt
;
304 static void qcrypto_cipher_free_des_rfb(QCryptoCipher
*cipher
)
306 QCryptoCipherBuiltin
*ctxt
= cipher
->opaque
;
308 g_free(ctxt
->state
.desrfb
.key
);
310 cipher
->opaque
= NULL
;
314 static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher
*cipher
,
320 QCryptoCipherBuiltin
*ctxt
= cipher
->opaque
;
324 error_setg(errp
, "Buffer size must be multiple of 8 not %zu",
329 deskey(ctxt
->state
.desrfb
.key
, EN0
);
331 for (i
= 0; i
< len
; i
+= 8) {
332 des((void *)in
+ i
, out
+ i
);
339 static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher
*cipher
,
345 QCryptoCipherBuiltin
*ctxt
= cipher
->opaque
;
349 error_setg(errp
, "Buffer size must be multiple of 8 not %zu",
354 deskey(ctxt
->state
.desrfb
.key
, DE1
);
356 for (i
= 0; i
< len
; i
+= 8) {
357 des((void *)in
+ i
, out
+ i
);
364 static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher
*cipher
,
365 const uint8_t *iv
, size_t niv
,
368 error_setg(errp
, "Setting IV is not supported");
373 static int qcrypto_cipher_init_des_rfb(QCryptoCipher
*cipher
,
374 const uint8_t *key
, size_t nkey
,
377 QCryptoCipherBuiltin
*ctxt
;
379 if (cipher
->mode
!= QCRYPTO_CIPHER_MODE_ECB
) {
380 error_setg(errp
, "Unsupported cipher mode %s",
381 QCryptoCipherMode_lookup
[cipher
->mode
]);
385 ctxt
= g_new0(QCryptoCipherBuiltin
, 1);
387 ctxt
->state
.desrfb
.key
= g_new0(uint8_t, nkey
);
388 memcpy(ctxt
->state
.desrfb
.key
, key
, nkey
);
389 ctxt
->state
.desrfb
.nkey
= nkey
;
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
;
397 cipher
->opaque
= ctxt
;
403 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg
,
404 QCryptoCipherMode mode
)
407 case QCRYPTO_CIPHER_ALG_DES_RFB
:
408 case QCRYPTO_CIPHER_ALG_AES_128
:
409 case QCRYPTO_CIPHER_ALG_AES_192
:
410 case QCRYPTO_CIPHER_ALG_AES_256
:
417 case QCRYPTO_CIPHER_MODE_ECB
:
418 case QCRYPTO_CIPHER_MODE_CBC
:
419 case QCRYPTO_CIPHER_MODE_XTS
:
421 case QCRYPTO_CIPHER_MODE_CTR
:
429 QCryptoCipher
*qcrypto_cipher_new(QCryptoCipherAlgorithm alg
,
430 QCryptoCipherMode mode
,
431 const uint8_t *key
, size_t nkey
,
434 QCryptoCipher
*cipher
;
437 case QCRYPTO_CIPHER_MODE_ECB
:
438 case QCRYPTO_CIPHER_MODE_CBC
:
439 case QCRYPTO_CIPHER_MODE_XTS
:
442 error_setg(errp
, "Unsupported cipher mode %s",
443 QCryptoCipherMode_lookup
[mode
]);
447 cipher
= g_new0(QCryptoCipher
, 1);
451 if (!qcrypto_cipher_validate_key_length(alg
, mode
, nkey
, errp
)) {
455 switch (cipher
->alg
) {
456 case QCRYPTO_CIPHER_ALG_DES_RFB
:
457 if (qcrypto_cipher_init_des_rfb(cipher
, key
, nkey
, errp
) < 0) {
461 case QCRYPTO_CIPHER_ALG_AES_128
:
462 case QCRYPTO_CIPHER_ALG_AES_192
:
463 case QCRYPTO_CIPHER_ALG_AES_256
:
464 if (qcrypto_cipher_init_aes(cipher
, key
, nkey
, errp
) < 0) {
470 "Unsupported cipher algorithm %s",
471 QCryptoCipherAlgorithm_lookup
[cipher
->alg
]);
482 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
484 QCryptoCipherBuiltin
*ctxt
;
490 ctxt
= cipher
->opaque
;
496 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
502 QCryptoCipherBuiltin
*ctxt
= cipher
->opaque
;
504 if (len
% ctxt
->blocksize
) {
505 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
506 len
, ctxt
->blocksize
);
510 return ctxt
->encrypt(cipher
, in
, out
, len
, errp
);
514 int qcrypto_cipher_decrypt(QCryptoCipher
*cipher
,
520 QCryptoCipherBuiltin
*ctxt
= cipher
->opaque
;
522 if (len
% ctxt
->blocksize
) {
523 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
524 len
, ctxt
->blocksize
);
528 return ctxt
->decrypt(cipher
, in
, out
, len
, errp
);
532 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
533 const uint8_t *iv
, size_t niv
,
536 QCryptoCipherBuiltin
*ctxt
= cipher
->opaque
;
538 return ctxt
->setiv(cipher
, iv
, niv
, errp
);