]> git.proxmox.com Git - mirror_qemu.git/blame - crypto/cipher.c
main-loop: Acquire main_context lock around os_host_main_loop_wait.
[mirror_qemu.git] / crypto / cipher.c
CommitLineData
ca38a4cc
DB
1/*
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
42f7a448 21#include "qemu/osdep.h"
da34e65c 22#include "qapi/error.h"
ca38a4cc
DB
23#include "crypto/cipher.h"
24
62893b67 25
d8c02bcc 26static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
ca38a4cc
DB
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,
ffb7bf45 31 [QCRYPTO_CIPHER_ALG_3DES] = 24,
084a85ee 32 [QCRYPTO_CIPHER_ALG_CAST5_128] = 16,
94318522
DB
33 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
34 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 24,
35 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 32,
50f6753e
DB
36 [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
37 [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 24,
38 [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 32,
ca38a4cc
DB
39};
40
d8c02bcc 41static size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
dd2bf9eb
DB
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,
ffb7bf45 46 [QCRYPTO_CIPHER_ALG_3DES] = 8,
084a85ee 47 [QCRYPTO_CIPHER_ALG_CAST5_128] = 8,
94318522
DB
48 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
49 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 16,
50 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 16,
50f6753e
DB
51 [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
52 [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 16,
53 [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 16,
dd2bf9eb
DB
54};
55
d8c02bcc 56static bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
dd2bf9eb
DB
57 [QCRYPTO_CIPHER_MODE_ECB] = false,
58 [QCRYPTO_CIPHER_MODE_CBC] = true,
eaec903c 59 [QCRYPTO_CIPHER_MODE_XTS] = true,
3c28292f 60 [QCRYPTO_CIPHER_MODE_CTR] = true,
dd2bf9eb
DB
61};
62
63
64size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg)
65{
32c813e6 66 assert(alg < G_N_ELEMENTS(alg_key_len));
dd2bf9eb
DB
67 return alg_block_len[alg];
68}
69
70
71size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg)
72{
32c813e6 73 assert(alg < G_N_ELEMENTS(alg_key_len));
dd2bf9eb
DB
74 return alg_key_len[alg];
75}
76
77
78size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgorithm alg,
79 QCryptoCipherMode mode)
80{
81 if (alg >= G_N_ELEMENTS(alg_block_len)) {
82 return 0;
83 }
84 if (mode >= G_N_ELEMENTS(mode_need_iv)) {
85 return 0;
86 }
87
88 if (mode_need_iv[mode]) {
89 return alg_block_len[alg];
90 }
91 return 0;
92}
93
94
ca38a4cc
DB
95static bool
96qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
eaec903c 97 QCryptoCipherMode mode,
ca38a4cc
DB
98 size_t nkey,
99 Error **errp)
100{
d8c02bcc 101 if ((unsigned)alg >= QCRYPTO_CIPHER_ALG__MAX) {
ca38a4cc
DB
102 error_setg(errp, "Cipher algorithm %d out of range",
103 alg);
104 return false;
105 }
106
eaec903c 107 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
ffb7bf45
LM
108 if (alg == QCRYPTO_CIPHER_ALG_DES_RFB
109 || alg == QCRYPTO_CIPHER_ALG_3DES) {
110 error_setg(errp, "XTS mode not compatible with DES-RFB/3DES");
eaec903c
DB
111 return false;
112 }
113 if (nkey % 2) {
114 error_setg(errp, "XTS cipher key length should be a multiple of 2");
115 return false;
116 }
117
118 if (alg_key_len[alg] != (nkey / 2)) {
119 error_setg(errp, "Cipher key length %zu should be %zu",
120 nkey, alg_key_len[alg] * 2);
121 return false;
122 }
123 } else {
124 if (alg_key_len[alg] != nkey) {
125 error_setg(errp, "Cipher key length %zu should be %zu",
126 nkey, alg_key_len[alg]);
127 return false;
128 }
ca38a4cc
DB
129 }
130 return true;
131}
132
91bfcdb0 133#if defined(CONFIG_GCRYPT) || defined(CONFIG_NETTLE)
62893b67
DB
134static uint8_t *
135qcrypto_cipher_munge_des_rfb_key(const uint8_t *key,
136 size_t nkey)
137{
138 uint8_t *ret = g_new0(uint8_t, nkey);
139 size_t i;
140 for (i = 0; i < nkey; i++) {
141 uint8_t r = key[i];
142 r = (r & 0xf0) >> 4 | (r & 0x0f) << 4;
143 r = (r & 0xcc) >> 2 | (r & 0x33) << 2;
144 r = (r & 0xaa) >> 1 | (r & 0x55) << 1;
145 ret[i] = r;
146 }
147 return ret;
148}
91bfcdb0 149#endif /* CONFIG_GCRYPT || CONFIG_NETTLE */
62893b67 150
91bfcdb0 151#ifdef CONFIG_GCRYPT
62893b67 152#include "crypto/cipher-gcrypt.c"
91bfcdb0 153#elif defined CONFIG_NETTLE
ed754746 154#include "crypto/cipher-nettle.c"
62893b67 155#else
ca38a4cc 156#include "crypto/cipher-builtin.c"
62893b67 157#endif