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 "crypto/aes.h"
22 #include "crypto/desrfb.h"
24 typedef struct QCryptoCipherBuiltinAES QCryptoCipherBuiltinAES;
25 struct QCryptoCipherBuiltinAES {
28 uint8_t iv[AES_BLOCK_SIZE];
30 typedef struct QCryptoCipherBuiltinDESRFB QCryptoCipherBuiltinDESRFB;
31 struct QCryptoCipherBuiltinDESRFB {
36 typedef struct QCryptoCipherBuiltin QCryptoCipherBuiltin;
37 struct QCryptoCipherBuiltin {
39 QCryptoCipherBuiltinAES aes;
40 QCryptoCipherBuiltinDESRFB desrfb;
43 void (*free)(QCryptoCipher *cipher);
44 int (*setiv)(QCryptoCipher *cipher,
45 const uint8_t *iv, size_t niv,
47 int (*encrypt)(QCryptoCipher *cipher,
52 int (*decrypt)(QCryptoCipher *cipher,
60 static void qcrypto_cipher_free_aes(QCryptoCipher *cipher)
62 QCryptoCipherBuiltin *ctxt = cipher->opaque;
65 cipher->opaque = NULL;
69 static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher,
75 QCryptoCipherBuiltin *ctxt = cipher->opaque;
77 if (cipher->mode == QCRYPTO_CIPHER_MODE_ECB) {
78 const uint8_t *inptr = in;
79 uint8_t *outptr = out;
81 if (len > AES_BLOCK_SIZE) {
82 AES_encrypt(inptr, outptr, &ctxt->state.aes.encrypt_key);
83 inptr += AES_BLOCK_SIZE;
84 outptr += AES_BLOCK_SIZE;
85 len -= AES_BLOCK_SIZE;
87 uint8_t tmp1[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
88 memcpy(tmp1, inptr, len);
89 /* Fill with 0 to avoid valgrind uninitialized reads */
90 memset(tmp1 + len, 0, sizeof(tmp1) - len);
91 AES_encrypt(tmp1, tmp2, &ctxt->state.aes.encrypt_key);
92 memcpy(outptr, tmp2, len);
97 AES_cbc_encrypt(in, out, len,
98 &ctxt->state.aes.encrypt_key,
99 ctxt->state.aes.iv, 1);
106 static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher,
112 QCryptoCipherBuiltin *ctxt = cipher->opaque;
114 if (cipher->mode == QCRYPTO_CIPHER_MODE_ECB) {
115 const uint8_t *inptr = in;
116 uint8_t *outptr = out;
118 if (len > AES_BLOCK_SIZE) {
119 AES_decrypt(inptr, outptr, &ctxt->state.aes.decrypt_key);
120 inptr += AES_BLOCK_SIZE;
121 outptr += AES_BLOCK_SIZE;
122 len -= AES_BLOCK_SIZE;
124 uint8_t tmp1[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
125 memcpy(tmp1, inptr, len);
126 /* Fill with 0 to avoid valgrind uninitialized reads */
127 memset(tmp1 + len, 0, sizeof(tmp1) - len);
128 AES_decrypt(tmp1, tmp2, &ctxt->state.aes.decrypt_key);
129 memcpy(outptr, tmp2, len);
134 AES_cbc_encrypt(in, out, len,
135 &ctxt->state.aes.decrypt_key,
136 ctxt->state.aes.iv, 0);
142 static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
143 const uint8_t *iv, size_t niv,
146 QCryptoCipherBuiltin *ctxt = cipher->opaque;
147 if (niv != AES_BLOCK_SIZE) {
148 error_setg(errp, "IV must be %d bytes not %zu",
149 AES_BLOCK_SIZE, niv);
153 memcpy(ctxt->state.aes.iv, iv, AES_BLOCK_SIZE);
161 static int qcrypto_cipher_init_aes(QCryptoCipher *cipher,
162 const uint8_t *key, size_t nkey,
165 QCryptoCipherBuiltin *ctxt;
167 if (cipher->mode != QCRYPTO_CIPHER_MODE_CBC &&
168 cipher->mode != QCRYPTO_CIPHER_MODE_ECB) {
169 error_setg(errp, "Unsupported cipher mode %d", cipher->mode);
173 ctxt = g_new0(QCryptoCipherBuiltin, 1);
175 if (AES_set_encrypt_key(key, nkey * 8, &ctxt->state.aes.encrypt_key) != 0) {
176 error_setg(errp, "Failed to set encryption key");
180 if (AES_set_decrypt_key(key, nkey * 8, &ctxt->state.aes.decrypt_key) != 0) {
181 error_setg(errp, "Failed to set decryption key");
185 ctxt->blocksize = AES_BLOCK_SIZE;
186 ctxt->free = qcrypto_cipher_free_aes;
187 ctxt->setiv = qcrypto_cipher_setiv_aes;
188 ctxt->encrypt = qcrypto_cipher_encrypt_aes;
189 ctxt->decrypt = qcrypto_cipher_decrypt_aes;
191 cipher->opaque = ctxt;
201 static void qcrypto_cipher_free_des_rfb(QCryptoCipher *cipher)
203 QCryptoCipherBuiltin *ctxt = cipher->opaque;
205 g_free(ctxt->state.desrfb.key);
207 cipher->opaque = NULL;
211 static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher,
217 QCryptoCipherBuiltin *ctxt = cipher->opaque;
221 error_setg(errp, "Buffer size must be multiple of 8 not %zu",
226 deskey(ctxt->state.desrfb.key, EN0);
228 for (i = 0; i < len; i += 8) {
229 des((void *)in + i, out + i);
236 static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher,
242 QCryptoCipherBuiltin *ctxt = cipher->opaque;
246 error_setg(errp, "Buffer size must be multiple of 8 not %zu",
251 deskey(ctxt->state.desrfb.key, DE1);
253 for (i = 0; i < len; i += 8) {
254 des((void *)in + i, out + i);
261 static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher,
262 const uint8_t *iv, size_t niv,
265 error_setg(errp, "Setting IV is not supported");
270 static int qcrypto_cipher_init_des_rfb(QCryptoCipher *cipher,
271 const uint8_t *key, size_t nkey,
274 QCryptoCipherBuiltin *ctxt;
276 if (cipher->mode != QCRYPTO_CIPHER_MODE_ECB) {
277 error_setg(errp, "Unsupported cipher mode %d", cipher->mode);
281 ctxt = g_new0(QCryptoCipherBuiltin, 1);
283 ctxt->state.desrfb.key = g_new0(uint8_t, nkey);
284 memcpy(ctxt->state.desrfb.key, key, nkey);
285 ctxt->state.desrfb.nkey = nkey;
288 ctxt->free = qcrypto_cipher_free_des_rfb;
289 ctxt->setiv = qcrypto_cipher_setiv_des_rfb;
290 ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb;
291 ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb;
293 cipher->opaque = ctxt;
299 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
302 case QCRYPTO_CIPHER_ALG_DES_RFB:
303 case QCRYPTO_CIPHER_ALG_AES_128:
304 case QCRYPTO_CIPHER_ALG_AES_192:
305 case QCRYPTO_CIPHER_ALG_AES_256:
313 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
314 QCryptoCipherMode mode,
315 const uint8_t *key, size_t nkey,
318 QCryptoCipher *cipher;
320 cipher = g_new0(QCryptoCipher, 1);
324 if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) {
328 switch (cipher->alg) {
329 case QCRYPTO_CIPHER_ALG_DES_RFB:
330 if (qcrypto_cipher_init_des_rfb(cipher, key, nkey, errp) < 0) {
334 case QCRYPTO_CIPHER_ALG_AES_128:
335 case QCRYPTO_CIPHER_ALG_AES_192:
336 case QCRYPTO_CIPHER_ALG_AES_256:
337 if (qcrypto_cipher_init_aes(cipher, key, nkey, errp) < 0) {
343 "Unsupported cipher algorithm %d", cipher->alg);
354 void qcrypto_cipher_free(QCryptoCipher *cipher)
356 QCryptoCipherBuiltin *ctxt;
362 ctxt = cipher->opaque;
368 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
374 QCryptoCipherBuiltin *ctxt = cipher->opaque;
376 if (len % ctxt->blocksize) {
377 error_setg(errp, "Length %zu must be a multiple of block size %zu",
378 len, ctxt->blocksize);
382 return ctxt->encrypt(cipher, in, out, len, errp);
386 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
392 QCryptoCipherBuiltin *ctxt = cipher->opaque;
394 if (len % ctxt->blocksize) {
395 error_setg(errp, "Length %zu must be a multiple of block size %zu",
396 len, ctxt->blocksize);
400 return ctxt->decrypt(cipher, in, out, len, errp);
404 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
405 const uint8_t *iv, size_t niv,
408 QCryptoCipherBuiltin *ctxt = cipher->opaque;
410 return ctxt->setiv(cipher, iv, niv, errp);