]> git.proxmox.com Git - mirror_qemu.git/blame - crypto/cipher.c
crypto: Move USER_CREATABLE to secret_common base class
[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
b7cbb874 9 * version 2.1 of the License, or (at your option) any later version.
ca38a4cc
DB
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"
eba29771 22#include "qemu/host-utils.h"
da34e65c 23#include "qapi/error.h"
ca38a4cc 24#include "crypto/cipher.h"
75c80078 25#include "cipherpriv.h"
ca38a4cc 26
62893b67 27
e46064a4 28static const size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
ca38a4cc
DB
29 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
30 [QCRYPTO_CIPHER_ALG_AES_192] = 24,
31 [QCRYPTO_CIPHER_ALG_AES_256] = 32,
32 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
ffb7bf45 33 [QCRYPTO_CIPHER_ALG_3DES] = 24,
084a85ee 34 [QCRYPTO_CIPHER_ALG_CAST5_128] = 16,
94318522
DB
35 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
36 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 24,
37 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 32,
50f6753e
DB
38 [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
39 [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 24,
40 [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 32,
ca38a4cc
DB
41};
42
e46064a4 43static const size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
dd2bf9eb
DB
44 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
45 [QCRYPTO_CIPHER_ALG_AES_192] = 16,
46 [QCRYPTO_CIPHER_ALG_AES_256] = 16,
47 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
ffb7bf45 48 [QCRYPTO_CIPHER_ALG_3DES] = 8,
084a85ee 49 [QCRYPTO_CIPHER_ALG_CAST5_128] = 8,
94318522
DB
50 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
51 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 16,
52 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 16,
50f6753e
DB
53 [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
54 [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 16,
55 [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 16,
dd2bf9eb
DB
56};
57
e46064a4 58static const bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
dd2bf9eb
DB
59 [QCRYPTO_CIPHER_MODE_ECB] = false,
60 [QCRYPTO_CIPHER_MODE_CBC] = true,
eaec903c 61 [QCRYPTO_CIPHER_MODE_XTS] = true,
3c28292f 62 [QCRYPTO_CIPHER_MODE_CTR] = true,
dd2bf9eb
DB
63};
64
65
66size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg)
67{
32c813e6 68 assert(alg < G_N_ELEMENTS(alg_key_len));
dd2bf9eb
DB
69 return alg_block_len[alg];
70}
71
72
73size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg)
74{
32c813e6 75 assert(alg < G_N_ELEMENTS(alg_key_len));
dd2bf9eb
DB
76 return alg_key_len[alg];
77}
78
79
80size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgorithm alg,
81 QCryptoCipherMode mode)
82{
83 if (alg >= G_N_ELEMENTS(alg_block_len)) {
84 return 0;
85 }
86 if (mode >= G_N_ELEMENTS(mode_need_iv)) {
87 return 0;
88 }
89
90 if (mode_need_iv[mode]) {
91 return alg_block_len[alg];
92 }
93 return 0;
94}
95
96
ca38a4cc
DB
97static bool
98qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
eaec903c 99 QCryptoCipherMode mode,
ca38a4cc
DB
100 size_t nkey,
101 Error **errp)
102{
d8c02bcc 103 if ((unsigned)alg >= QCRYPTO_CIPHER_ALG__MAX) {
ca38a4cc
DB
104 error_setg(errp, "Cipher algorithm %d out of range",
105 alg);
106 return false;
107 }
108
eaec903c 109 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
ffb7bf45
LM
110 if (alg == QCRYPTO_CIPHER_ALG_DES_RFB
111 || alg == QCRYPTO_CIPHER_ALG_3DES) {
112 error_setg(errp, "XTS mode not compatible with DES-RFB/3DES");
eaec903c
DB
113 return false;
114 }
115 if (nkey % 2) {
116 error_setg(errp, "XTS cipher key length should be a multiple of 2");
117 return false;
118 }
119
120 if (alg_key_len[alg] != (nkey / 2)) {
121 error_setg(errp, "Cipher key length %zu should be %zu",
122 nkey, alg_key_len[alg] * 2);
123 return false;
124 }
125 } else {
126 if (alg_key_len[alg] != nkey) {
127 error_setg(errp, "Cipher key length %zu should be %zu",
128 nkey, alg_key_len[alg]);
129 return false;
130 }
ca38a4cc
DB
131 }
132 return true;
133}
134
91bfcdb0 135#if defined(CONFIG_GCRYPT) || defined(CONFIG_NETTLE)
62893b67
DB
136static uint8_t *
137qcrypto_cipher_munge_des_rfb_key(const uint8_t *key,
138 size_t nkey)
139{
140 uint8_t *ret = g_new0(uint8_t, nkey);
141 size_t i;
142 for (i = 0; i < nkey; i++) {
143 uint8_t r = key[i];
144 r = (r & 0xf0) >> 4 | (r & 0x0f) << 4;
145 r = (r & 0xcc) >> 2 | (r & 0x33) << 2;
146 r = (r & 0xaa) >> 1 | (r & 0x55) << 1;
147 ret[i] = r;
148 }
149 return ret;
150}
91bfcdb0 151#endif /* CONFIG_GCRYPT || CONFIG_NETTLE */
62893b67 152
91bfcdb0 153#ifdef CONFIG_GCRYPT
6d92bdf4 154#include "cipher-gcrypt.c.inc"
91bfcdb0 155#elif defined CONFIG_NETTLE
6d92bdf4 156#include "cipher-nettle.c.inc"
62893b67 157#else
6d92bdf4 158#include "cipher-builtin.c.inc"
62893b67 159#endif
75c80078
LM
160
161QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
162 QCryptoCipherMode mode,
163 const uint8_t *key, size_t nkey,
164 Error **errp)
165{
3eedf5cc 166 QCryptoCipher *cipher = NULL;
25c60df3
LM
167
168#ifdef CONFIG_AF_ALG
3eedf5cc 169 cipher = qcrypto_afalg_cipher_ctx_new(alg, mode, key, nkey, NULL);
25c60df3 170#endif
75c80078 171
3eedf5cc
RH
172 if (!cipher) {
173 cipher = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp);
174 if (!cipher) {
25c60df3
LM
175 return NULL;
176 }
75c80078
LM
177 }
178
75c80078
LM
179 cipher->alg = alg;
180 cipher->mode = mode;
75c80078
LM
181
182 return cipher;
183}
184
185
186int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
187 const void *in,
188 void *out,
189 size_t len,
190 Error **errp)
191{
7b5dbfb7 192 const QCryptoCipherDriver *drv = cipher->driver;
75c80078
LM
193 return drv->cipher_encrypt(cipher, in, out, len, errp);
194}
195
196
197int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
198 const void *in,
199 void *out,
200 size_t len,
201 Error **errp)
202{
7b5dbfb7 203 const QCryptoCipherDriver *drv = cipher->driver;
75c80078
LM
204 return drv->cipher_decrypt(cipher, in, out, len, errp);
205}
206
207
208int qcrypto_cipher_setiv(QCryptoCipher *cipher,
209 const uint8_t *iv, size_t niv,
210 Error **errp)
211{
7b5dbfb7 212 const QCryptoCipherDriver *drv = cipher->driver;
75c80078
LM
213 return drv->cipher_setiv(cipher, iv, niv, errp);
214}
215
216
217void qcrypto_cipher_free(QCryptoCipher *cipher)
218{
75c80078 219 if (cipher) {
3eedf5cc 220 cipher->driver->cipher_free(cipher);
75c80078
LM
221 }
222}