qemu-doc: Clarify that -vga std is now the default
[qemu/ar7.git] / crypto / cipher.c
blob9ecaff702b7ae0fa6af9a090ca331048790b2b1a
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 "qemu/osdep.h"
22 #include "qapi/error.h"
23 #include "crypto/cipher.h"
26 static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
27 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
28 [QCRYPTO_CIPHER_ALG_AES_192] = 24,
29 [QCRYPTO_CIPHER_ALG_AES_256] = 32,
30 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
31 [QCRYPTO_CIPHER_ALG_3DES] = 24,
32 [QCRYPTO_CIPHER_ALG_CAST5_128] = 16,
33 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
34 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 24,
35 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 32,
36 [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
37 [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 24,
38 [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 32,
41 static size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
42 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
43 [QCRYPTO_CIPHER_ALG_AES_192] = 16,
44 [QCRYPTO_CIPHER_ALG_AES_256] = 16,
45 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
46 [QCRYPTO_CIPHER_ALG_3DES] = 8,
47 [QCRYPTO_CIPHER_ALG_CAST5_128] = 8,
48 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
49 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 16,
50 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 16,
51 [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
52 [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 16,
53 [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 16,
56 static bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
57 [QCRYPTO_CIPHER_MODE_ECB] = false,
58 [QCRYPTO_CIPHER_MODE_CBC] = true,
59 [QCRYPTO_CIPHER_MODE_XTS] = true,
60 [QCRYPTO_CIPHER_MODE_CTR] = true,
64 size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg)
66 if (alg >= G_N_ELEMENTS(alg_key_len)) {
67 return 0;
69 return alg_block_len[alg];
73 size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg)
75 if (alg >= G_N_ELEMENTS(alg_key_len)) {
76 return 0;
78 return alg_key_len[alg];
82 size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgorithm alg,
83 QCryptoCipherMode mode)
85 if (alg >= G_N_ELEMENTS(alg_block_len)) {
86 return 0;
88 if (mode >= G_N_ELEMENTS(mode_need_iv)) {
89 return 0;
92 if (mode_need_iv[mode]) {
93 return alg_block_len[alg];
95 return 0;
99 static bool
100 qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
101 QCryptoCipherMode mode,
102 size_t nkey,
103 Error **errp)
105 if ((unsigned)alg >= QCRYPTO_CIPHER_ALG__MAX) {
106 error_setg(errp, "Cipher algorithm %d out of range",
107 alg);
108 return false;
111 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
112 if (alg == QCRYPTO_CIPHER_ALG_DES_RFB
113 || alg == QCRYPTO_CIPHER_ALG_3DES) {
114 error_setg(errp, "XTS mode not compatible with DES-RFB/3DES");
115 return false;
117 if (nkey % 2) {
118 error_setg(errp, "XTS cipher key length should be a multiple of 2");
119 return false;
122 if (alg_key_len[alg] != (nkey / 2)) {
123 error_setg(errp, "Cipher key length %zu should be %zu",
124 nkey, alg_key_len[alg] * 2);
125 return false;
127 } else {
128 if (alg_key_len[alg] != nkey) {
129 error_setg(errp, "Cipher key length %zu should be %zu",
130 nkey, alg_key_len[alg]);
131 return false;
134 return true;
137 #if defined(CONFIG_GCRYPT) || defined(CONFIG_NETTLE)
138 static uint8_t *
139 qcrypto_cipher_munge_des_rfb_key(const uint8_t *key,
140 size_t nkey)
142 uint8_t *ret = g_new0(uint8_t, nkey);
143 size_t i;
144 for (i = 0; i < nkey; i++) {
145 uint8_t r = key[i];
146 r = (r & 0xf0) >> 4 | (r & 0x0f) << 4;
147 r = (r & 0xcc) >> 2 | (r & 0x33) << 2;
148 r = (r & 0xaa) >> 1 | (r & 0x55) << 1;
149 ret[i] = r;
151 return ret;
153 #endif /* CONFIG_GCRYPT || CONFIG_NETTLE */
155 #ifdef CONFIG_GCRYPT
156 #include "crypto/cipher-gcrypt.c"
157 #elif defined CONFIG_NETTLE
158 #include "crypto/cipher-nettle.c"
159 #else
160 #include "crypto/cipher-builtin.c"
161 #endif