target/i386: [tcg] Port to translate_insn
[qemu/ar7.git] / crypto / cipher.c
blob0aad9d6d79c78af5631a2db0ca6d17737dae633f
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"
24 #include "cipherpriv.h"
27 static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
28 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
29 [QCRYPTO_CIPHER_ALG_AES_192] = 24,
30 [QCRYPTO_CIPHER_ALG_AES_256] = 32,
31 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
32 [QCRYPTO_CIPHER_ALG_3DES] = 24,
33 [QCRYPTO_CIPHER_ALG_CAST5_128] = 16,
34 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
35 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 24,
36 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 32,
37 [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
38 [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 24,
39 [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 32,
42 static size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
43 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
44 [QCRYPTO_CIPHER_ALG_AES_192] = 16,
45 [QCRYPTO_CIPHER_ALG_AES_256] = 16,
46 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
47 [QCRYPTO_CIPHER_ALG_3DES] = 8,
48 [QCRYPTO_CIPHER_ALG_CAST5_128] = 8,
49 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
50 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 16,
51 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 16,
52 [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
53 [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 16,
54 [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 16,
57 static bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
58 [QCRYPTO_CIPHER_MODE_ECB] = false,
59 [QCRYPTO_CIPHER_MODE_CBC] = true,
60 [QCRYPTO_CIPHER_MODE_XTS] = true,
61 [QCRYPTO_CIPHER_MODE_CTR] = true,
65 size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg)
67 assert(alg < G_N_ELEMENTS(alg_key_len));
68 return alg_block_len[alg];
72 size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg)
74 assert(alg < G_N_ELEMENTS(alg_key_len));
75 return alg_key_len[alg];
79 size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgorithm alg,
80 QCryptoCipherMode mode)
82 if (alg >= G_N_ELEMENTS(alg_block_len)) {
83 return 0;
85 if (mode >= G_N_ELEMENTS(mode_need_iv)) {
86 return 0;
89 if (mode_need_iv[mode]) {
90 return alg_block_len[alg];
92 return 0;
96 static bool
97 qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
98 QCryptoCipherMode mode,
99 size_t nkey,
100 Error **errp)
102 if ((unsigned)alg >= QCRYPTO_CIPHER_ALG__MAX) {
103 error_setg(errp, "Cipher algorithm %d out of range",
104 alg);
105 return false;
108 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
109 if (alg == QCRYPTO_CIPHER_ALG_DES_RFB
110 || alg == QCRYPTO_CIPHER_ALG_3DES) {
111 error_setg(errp, "XTS mode not compatible with DES-RFB/3DES");
112 return false;
114 if (nkey % 2) {
115 error_setg(errp, "XTS cipher key length should be a multiple of 2");
116 return false;
119 if (alg_key_len[alg] != (nkey / 2)) {
120 error_setg(errp, "Cipher key length %zu should be %zu",
121 nkey, alg_key_len[alg] * 2);
122 return false;
124 } else {
125 if (alg_key_len[alg] != nkey) {
126 error_setg(errp, "Cipher key length %zu should be %zu",
127 nkey, alg_key_len[alg]);
128 return false;
131 return true;
134 #if defined(CONFIG_GCRYPT) || defined(CONFIG_NETTLE)
135 static uint8_t *
136 qcrypto_cipher_munge_des_rfb_key(const uint8_t *key,
137 size_t nkey)
139 uint8_t *ret = g_new0(uint8_t, nkey);
140 size_t i;
141 for (i = 0; i < nkey; i++) {
142 uint8_t r = key[i];
143 r = (r & 0xf0) >> 4 | (r & 0x0f) << 4;
144 r = (r & 0xcc) >> 2 | (r & 0x33) << 2;
145 r = (r & 0xaa) >> 1 | (r & 0x55) << 1;
146 ret[i] = r;
148 return ret;
150 #endif /* CONFIG_GCRYPT || CONFIG_NETTLE */
152 #ifdef CONFIG_GCRYPT
153 #include "crypto/cipher-gcrypt.c"
154 #elif defined CONFIG_NETTLE
155 #include "crypto/cipher-nettle.c"
156 #else
157 #include "crypto/cipher-builtin.c"
158 #endif
160 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
161 QCryptoCipherMode mode,
162 const uint8_t *key, size_t nkey,
163 Error **errp)
165 QCryptoCipher *cipher;
166 void *ctx = NULL;
167 Error *err2 = NULL;
168 QCryptoCipherDriver *drv = NULL;
170 #ifdef CONFIG_AF_ALG
171 ctx = qcrypto_afalg_cipher_ctx_new(alg, mode, key, nkey, &err2);
172 if (ctx) {
173 drv = &qcrypto_cipher_afalg_driver;
175 #endif
177 if (!ctx) {
178 ctx = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp);
179 if (!ctx) {
180 error_free(err2);
181 return NULL;
184 drv = &qcrypto_cipher_lib_driver;
185 error_free(err2);
188 cipher = g_new0(QCryptoCipher, 1);
189 cipher->alg = alg;
190 cipher->mode = mode;
191 cipher->opaque = ctx;
192 cipher->driver = (void *)drv;
194 return cipher;
198 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
199 const void *in,
200 void *out,
201 size_t len,
202 Error **errp)
204 QCryptoCipherDriver *drv = cipher->driver;
205 return drv->cipher_encrypt(cipher, in, out, len, errp);
209 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
210 const void *in,
211 void *out,
212 size_t len,
213 Error **errp)
215 QCryptoCipherDriver *drv = cipher->driver;
216 return drv->cipher_decrypt(cipher, in, out, len, errp);
220 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
221 const uint8_t *iv, size_t niv,
222 Error **errp)
224 QCryptoCipherDriver *drv = cipher->driver;
225 return drv->cipher_setiv(cipher, iv, niv, errp);
229 void qcrypto_cipher_free(QCryptoCipher *cipher)
231 QCryptoCipherDriver *drv;
232 if (cipher) {
233 drv = cipher->driver;
234 drv->cipher_free(cipher);
235 g_free(cipher);