crypto: add support for the twofish cipher algorithm
[qemu/ar7.git] / crypto / cipher-nettle.c
blob9b057fd6a3ce6b320b8a08c6267f940767feb2ae
1 /*
2 * QEMU Crypto cipher nettle 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 <nettle/nettle-types.h>
23 #include <nettle/aes.h>
24 #include <nettle/des.h>
25 #include <nettle/cbc.h>
26 #include <nettle/cast128.h>
27 #include <nettle/serpent.h>
28 #include <nettle/twofish.h>
30 #if CONFIG_NETTLE_VERSION_MAJOR < 3
31 typedef nettle_crypt_func nettle_cipher_func;
33 typedef void * cipher_ctx_t;
34 typedef unsigned cipher_length_t;
35 #else
36 typedef const void * cipher_ctx_t;
37 typedef size_t cipher_length_t;
38 #endif
40 static nettle_cipher_func aes_encrypt_wrapper;
41 static nettle_cipher_func aes_decrypt_wrapper;
42 static nettle_cipher_func des_encrypt_wrapper;
43 static nettle_cipher_func des_decrypt_wrapper;
45 static void aes_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
46 uint8_t *dst, const uint8_t *src)
48 aes_encrypt(ctx, length, dst, src);
51 static void aes_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
52 uint8_t *dst, const uint8_t *src)
54 aes_decrypt(ctx, length, dst, src);
57 static void des_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
58 uint8_t *dst, const uint8_t *src)
60 des_encrypt(ctx, length, dst, src);
63 static void des_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
64 uint8_t *dst, const uint8_t *src)
66 des_decrypt(ctx, length, dst, src);
69 static void cast128_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
70 uint8_t *dst, const uint8_t *src)
72 cast128_encrypt(ctx, length, dst, src);
75 static void cast128_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
76 uint8_t *dst, const uint8_t *src)
78 cast128_decrypt(ctx, length, dst, src);
81 static void serpent_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
82 uint8_t *dst, const uint8_t *src)
84 serpent_encrypt(ctx, length, dst, src);
87 static void serpent_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
88 uint8_t *dst, const uint8_t *src)
90 serpent_decrypt(ctx, length, dst, src);
93 static void twofish_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
94 uint8_t *dst, const uint8_t *src)
96 twofish_encrypt(ctx, length, dst, src);
99 static void twofish_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
100 uint8_t *dst, const uint8_t *src)
102 twofish_decrypt(ctx, length, dst, src);
105 typedef struct QCryptoCipherNettle QCryptoCipherNettle;
106 struct QCryptoCipherNettle {
107 void *ctx_encrypt;
108 void *ctx_decrypt;
109 nettle_cipher_func *alg_encrypt;
110 nettle_cipher_func *alg_decrypt;
111 uint8_t *iv;
112 size_t blocksize;
115 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
117 switch (alg) {
118 case QCRYPTO_CIPHER_ALG_DES_RFB:
119 case QCRYPTO_CIPHER_ALG_AES_128:
120 case QCRYPTO_CIPHER_ALG_AES_192:
121 case QCRYPTO_CIPHER_ALG_AES_256:
122 case QCRYPTO_CIPHER_ALG_CAST5_128:
123 case QCRYPTO_CIPHER_ALG_SERPENT_128:
124 case QCRYPTO_CIPHER_ALG_SERPENT_192:
125 case QCRYPTO_CIPHER_ALG_SERPENT_256:
126 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
127 case QCRYPTO_CIPHER_ALG_TWOFISH_192:
128 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
129 return true;
130 default:
131 return false;
136 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
137 QCryptoCipherMode mode,
138 const uint8_t *key, size_t nkey,
139 Error **errp)
141 QCryptoCipher *cipher;
142 QCryptoCipherNettle *ctx;
143 uint8_t *rfbkey;
145 switch (mode) {
146 case QCRYPTO_CIPHER_MODE_ECB:
147 case QCRYPTO_CIPHER_MODE_CBC:
148 break;
149 default:
150 error_setg(errp, "Unsupported cipher mode %d", mode);
151 return NULL;
154 if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) {
155 return NULL;
158 cipher = g_new0(QCryptoCipher, 1);
159 cipher->alg = alg;
160 cipher->mode = mode;
162 ctx = g_new0(QCryptoCipherNettle, 1);
164 switch (alg) {
165 case QCRYPTO_CIPHER_ALG_DES_RFB:
166 ctx->ctx_encrypt = g_new0(struct des_ctx, 1);
167 ctx->ctx_decrypt = NULL; /* 1 ctx can do both */
168 rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
169 des_set_key(ctx->ctx_encrypt, rfbkey);
170 g_free(rfbkey);
172 ctx->alg_encrypt = des_encrypt_wrapper;
173 ctx->alg_decrypt = des_decrypt_wrapper;
175 ctx->blocksize = DES_BLOCK_SIZE;
176 break;
178 case QCRYPTO_CIPHER_ALG_AES_128:
179 case QCRYPTO_CIPHER_ALG_AES_192:
180 case QCRYPTO_CIPHER_ALG_AES_256:
181 ctx->ctx_encrypt = g_new0(struct aes_ctx, 1);
182 ctx->ctx_decrypt = g_new0(struct aes_ctx, 1);
184 aes_set_encrypt_key(ctx->ctx_encrypt, nkey, key);
185 aes_set_decrypt_key(ctx->ctx_decrypt, nkey, key);
187 ctx->alg_encrypt = aes_encrypt_wrapper;
188 ctx->alg_decrypt = aes_decrypt_wrapper;
190 ctx->blocksize = AES_BLOCK_SIZE;
191 break;
193 case QCRYPTO_CIPHER_ALG_CAST5_128:
194 ctx->ctx_encrypt = g_new0(struct cast128_ctx, 1);
195 ctx->ctx_decrypt = NULL; /* 1 ctx can do both */
197 cast5_set_key(ctx->ctx_encrypt, nkey, key);
199 ctx->alg_encrypt = cast128_encrypt_wrapper;
200 ctx->alg_decrypt = cast128_decrypt_wrapper;
202 ctx->blocksize = CAST128_BLOCK_SIZE;
203 break;
205 case QCRYPTO_CIPHER_ALG_SERPENT_128:
206 case QCRYPTO_CIPHER_ALG_SERPENT_192:
207 case QCRYPTO_CIPHER_ALG_SERPENT_256:
208 ctx->ctx_encrypt = g_new0(struct serpent_ctx, 1);
209 ctx->ctx_decrypt = NULL; /* 1 ctx can do both */
211 serpent_set_key(ctx->ctx_encrypt, nkey, key);
213 ctx->alg_encrypt = serpent_encrypt_wrapper;
214 ctx->alg_decrypt = serpent_decrypt_wrapper;
216 ctx->blocksize = SERPENT_BLOCK_SIZE;
217 break;
219 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
220 case QCRYPTO_CIPHER_ALG_TWOFISH_192:
221 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
222 ctx->ctx_encrypt = g_new0(struct twofish_ctx, 1);
223 ctx->ctx_decrypt = NULL; /* 1 ctx can do both */
225 twofish_set_key(ctx->ctx_encrypt, nkey, key);
227 ctx->alg_encrypt = twofish_encrypt_wrapper;
228 ctx->alg_decrypt = twofish_decrypt_wrapper;
230 ctx->blocksize = TWOFISH_BLOCK_SIZE;
231 break;
233 default:
234 error_setg(errp, "Unsupported cipher algorithm %d", alg);
235 goto error;
238 ctx->iv = g_new0(uint8_t, ctx->blocksize);
239 cipher->opaque = ctx;
241 return cipher;
243 error:
244 g_free(cipher);
245 g_free(ctx);
246 return NULL;
250 void qcrypto_cipher_free(QCryptoCipher *cipher)
252 QCryptoCipherNettle *ctx;
254 if (!cipher) {
255 return;
258 ctx = cipher->opaque;
259 g_free(ctx->iv);
260 g_free(ctx->ctx_encrypt);
261 g_free(ctx->ctx_decrypt);
262 g_free(ctx);
263 g_free(cipher);
267 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
268 const void *in,
269 void *out,
270 size_t len,
271 Error **errp)
273 QCryptoCipherNettle *ctx = cipher->opaque;
275 if (len % ctx->blocksize) {
276 error_setg(errp, "Length %zu must be a multiple of block size %zu",
277 len, ctx->blocksize);
278 return -1;
281 switch (cipher->mode) {
282 case QCRYPTO_CIPHER_MODE_ECB:
283 ctx->alg_encrypt(ctx->ctx_encrypt, len, out, in);
284 break;
286 case QCRYPTO_CIPHER_MODE_CBC:
287 cbc_encrypt(ctx->ctx_encrypt, ctx->alg_encrypt,
288 ctx->blocksize, ctx->iv,
289 len, out, in);
290 break;
291 default:
292 error_setg(errp, "Unsupported cipher algorithm %d",
293 cipher->alg);
294 return -1;
296 return 0;
300 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
301 const void *in,
302 void *out,
303 size_t len,
304 Error **errp)
306 QCryptoCipherNettle *ctx = cipher->opaque;
308 if (len % ctx->blocksize) {
309 error_setg(errp, "Length %zu must be a multiple of block size %zu",
310 len, ctx->blocksize);
311 return -1;
314 switch (cipher->mode) {
315 case QCRYPTO_CIPHER_MODE_ECB:
316 ctx->alg_decrypt(ctx->ctx_decrypt ? ctx->ctx_decrypt : ctx->ctx_encrypt,
317 len, out, in);
318 break;
320 case QCRYPTO_CIPHER_MODE_CBC:
321 cbc_decrypt(ctx->ctx_decrypt ? ctx->ctx_decrypt : ctx->ctx_encrypt,
322 ctx->alg_decrypt, ctx->blocksize, ctx->iv,
323 len, out, in);
324 break;
325 default:
326 error_setg(errp, "Unsupported cipher algorithm %d",
327 cipher->alg);
328 return -1;
330 return 0;
333 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
334 const uint8_t *iv, size_t niv,
335 Error **errp)
337 QCryptoCipherNettle *ctx = cipher->opaque;
338 if (niv != ctx->blocksize) {
339 error_setg(errp, "Expected IV size %zu not %zu",
340 ctx->blocksize, niv);
341 return -1;
343 memcpy(ctx->iv, iv, niv);
344 return 0;