enable multi-function hot-add
[qemu/ar7.git] / crypto / cipher.c
blobc8bd18053230c9041c901c7ab7c79b89f2a37709
1 /*
2 * QEMU Crypto cipher 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/cipher.h"
24 static 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,
31 static bool
32 qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
33 size_t nkey,
34 Error **errp)
36 if ((unsigned)alg >= QCRYPTO_CIPHER_ALG_LAST) {
37 error_setg(errp, "Cipher algorithm %d out of range",
38 alg);
39 return false;
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;
47 return true;
50 #if defined(CONFIG_GCRYPT) || defined(CONFIG_NETTLE)
51 static uint8_t *
52 qcrypto_cipher_munge_des_rfb_key(const uint8_t *key,
53 size_t nkey)
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;
64 return ret;
66 #endif /* CONFIG_GCRYPT || CONFIG_NETTLE */
68 #ifdef CONFIG_GCRYPT
69 #include "crypto/cipher-gcrypt.c"
70 #elif defined CONFIG_NETTLE
71 #include "crypto/cipher-nettle.c"
72 #else
73 #include "crypto/cipher-builtin.c"
74 #endif