blob: ed4d854a133221902e03626ae89a01872daed23d [file] [log] [blame]
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +01001/*
2 * QEMU Crypto cipher 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 "crypto/cipher.h"
22
Daniel P. Berrange62893b62015-07-01 18:10:33 +010023
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010024static size_t alg_key_len[QCRYPTO_CIPHER_ALG_LAST] = {
25 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
26 [QCRYPTO_CIPHER_ALG_AES_192] = 24,
27 [QCRYPTO_CIPHER_ALG_AES_256] = 32,
28 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
29};
30
31static bool
32qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
33 size_t nkey,
34 Error **errp)
35{
36 if ((unsigned)alg >= QCRYPTO_CIPHER_ALG_LAST) {
37 error_setg(errp, "Cipher algorithm %d out of range",
38 alg);
39 return false;
40 }
41
42 if (alg_key_len[alg] != nkey) {
43 error_setg(errp, "Cipher key length %zu should be %zu",
44 alg_key_len[alg], nkey);
45 return false;
46 }
47 return true;
48}
49
Daniel P. Berrange62893b62015-07-01 18:10:33 +010050#if defined(CONFIG_GNUTLS_GCRYPT)
51static uint8_t *
52qcrypto_cipher_munge_des_rfb_key(const uint8_t *key,
53 size_t nkey)
54{
55 uint8_t *ret = g_new0(uint8_t, nkey);
56 size_t i;
57 for (i = 0; i < nkey; i++) {
58 uint8_t r = key[i];
59 r = (r & 0xf0) >> 4 | (r & 0x0f) << 4;
60 r = (r & 0xcc) >> 2 | (r & 0x33) << 2;
61 r = (r & 0xaa) >> 1 | (r & 0x55) << 1;
62 ret[i] = r;
63 }
64 return ret;
65}
66#endif /* CONFIG_GNUTLS_GCRYPT */
67
68#ifdef CONFIG_GNUTLS_GCRYPT
69#include "crypto/cipher-gcrypt.c"
70#else
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010071#include "crypto/cipher-builtin.c"
Daniel P. Berrange62893b62015-07-01 18:10:33 +010072#endif