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 "crypto/xts.h"
24 #include <nettle/nettle-types.h>
25 #include <nettle/aes.h>
26 #include <nettle/des.h>
27 #include <nettle/cbc.h>
28 #include <nettle/cast128.h>
29 #include <nettle/serpent.h>
30 #include <nettle/twofish.h>
31 #include <nettle/ctr.h>
33 typedef void (*QCryptoCipherNettleFuncWrapper)(const void *ctx,
38 #if CONFIG_NETTLE_VERSION_MAJOR < 3
39 typedef nettle_crypt_func * QCryptoCipherNettleFuncNative;
40 typedef void * cipher_ctx_t;
41 typedef unsigned cipher_length_t;
43 #define cast5_set_key cast128_set_key
45 typedef nettle_cipher_func * QCryptoCipherNettleFuncNative;
46 typedef const void * cipher_ctx_t;
47 typedef size_t cipher_length_t;
50 typedef struct QCryptoNettleAES {
55 static void aes_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
56 uint8_t *dst, const uint8_t *src)
58 const QCryptoNettleAES *aesctx = ctx;
59 aes_encrypt(&aesctx->enc, length, dst, src);
62 static void aes_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
63 uint8_t *dst, const uint8_t *src)
65 const QCryptoNettleAES *aesctx = ctx;
66 aes_decrypt(&aesctx->dec, length, dst, src);
69 static void des_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
70 uint8_t *dst, const uint8_t *src)
72 des_encrypt(ctx, length, dst, src);
75 static void des_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
76 uint8_t *dst, const uint8_t *src)
78 des_decrypt(ctx, length, dst, src);
81 static void cast128_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
82 uint8_t *dst, const uint8_t *src)
84 cast128_encrypt(ctx, length, dst, src);
87 static void cast128_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
88 uint8_t *dst, const uint8_t *src)
90 cast128_decrypt(ctx, length, dst, src);
93 static void serpent_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
94 uint8_t *dst, const uint8_t *src)
96 serpent_encrypt(ctx, length, dst, src);
99 static void serpent_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
100 uint8_t *dst, const uint8_t *src)
102 serpent_decrypt(ctx, length, dst, src);
105 static void twofish_encrypt_native(cipher_ctx_t ctx, cipher_length_t length,
106 uint8_t *dst, const uint8_t *src)
108 twofish_encrypt(ctx, length, dst, src);
111 static void twofish_decrypt_native(cipher_ctx_t ctx, cipher_length_t length,
112 uint8_t *dst, const uint8_t *src)
114 twofish_decrypt(ctx, length, dst, src);
117 static void aes_encrypt_wrapper(const void *ctx, size_t length,
118 uint8_t *dst, const uint8_t *src)
120 const QCryptoNettleAES *aesctx = ctx;
121 aes_encrypt(&aesctx->enc, length, dst, src);
124 static void aes_decrypt_wrapper(const void *ctx, size_t length,
125 uint8_t *dst, const uint8_t *src)
127 const QCryptoNettleAES *aesctx = ctx;
128 aes_decrypt(&aesctx->dec, length, dst, src);
131 static void des_encrypt_wrapper(const void *ctx, size_t length,
132 uint8_t *dst, const uint8_t *src)
134 des_encrypt(ctx, length, dst, src);
137 static void des_decrypt_wrapper(const void *ctx, size_t length,
138 uint8_t *dst, const uint8_t *src)
140 des_decrypt(ctx, length, dst, src);
143 static void cast128_encrypt_wrapper(const void *ctx, size_t length,
144 uint8_t *dst, const uint8_t *src)
146 cast128_encrypt(ctx, length, dst, src);
149 static void cast128_decrypt_wrapper(const void *ctx, size_t length,
150 uint8_t *dst, const uint8_t *src)
152 cast128_decrypt(ctx, length, dst, src);
155 static void serpent_encrypt_wrapper(const void *ctx, size_t length,
156 uint8_t *dst, const uint8_t *src)
158 serpent_encrypt(ctx, length, dst, src);
161 static void serpent_decrypt_wrapper(const void *ctx, size_t length,
162 uint8_t *dst, const uint8_t *src)
164 serpent_decrypt(ctx, length, dst, src);
167 static void twofish_encrypt_wrapper(const void *ctx, size_t length,
168 uint8_t *dst, const uint8_t *src)
170 twofish_encrypt(ctx, length, dst, src);
173 static void twofish_decrypt_wrapper(const void *ctx, size_t length,
174 uint8_t *dst, const uint8_t *src)
176 twofish_decrypt(ctx, length, dst, src);
179 typedef struct QCryptoCipherNettle QCryptoCipherNettle;
180 struct QCryptoCipherNettle {
181 /* Primary cipher context for all modes */
183 /* Second cipher context for XTS mode only */
185 /* Cipher callbacks for both contexts */
186 QCryptoCipherNettleFuncNative alg_encrypt_native;
187 QCryptoCipherNettleFuncNative alg_decrypt_native;
188 QCryptoCipherNettleFuncWrapper alg_encrypt_wrapper;
189 QCryptoCipherNettleFuncWrapper alg_decrypt_wrapper;
190 /* Initialization vector or Counter */
195 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
196 QCryptoCipherMode mode)
199 case QCRYPTO_CIPHER_ALG_DES_RFB:
200 case QCRYPTO_CIPHER_ALG_AES_128:
201 case QCRYPTO_CIPHER_ALG_AES_192:
202 case QCRYPTO_CIPHER_ALG_AES_256:
203 case QCRYPTO_CIPHER_ALG_CAST5_128:
204 case QCRYPTO_CIPHER_ALG_SERPENT_128:
205 case QCRYPTO_CIPHER_ALG_SERPENT_192:
206 case QCRYPTO_CIPHER_ALG_SERPENT_256:
207 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
208 case QCRYPTO_CIPHER_ALG_TWOFISH_192:
209 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
216 case QCRYPTO_CIPHER_MODE_ECB:
217 case QCRYPTO_CIPHER_MODE_CBC:
218 case QCRYPTO_CIPHER_MODE_XTS:
219 case QCRYPTO_CIPHER_MODE_CTR:
227 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
228 QCryptoCipherMode mode,
229 const uint8_t *key, size_t nkey,
232 QCryptoCipher *cipher;
233 QCryptoCipherNettle *ctx;
237 case QCRYPTO_CIPHER_MODE_ECB:
238 case QCRYPTO_CIPHER_MODE_CBC:
239 case QCRYPTO_CIPHER_MODE_XTS:
240 case QCRYPTO_CIPHER_MODE_CTR:
243 error_setg(errp, "Unsupported cipher mode %s",
244 QCryptoCipherMode_lookup[mode]);
248 if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
252 cipher = g_new0(QCryptoCipher, 1);
256 ctx = g_new0(QCryptoCipherNettle, 1);
257 cipher->opaque = ctx;
260 case QCRYPTO_CIPHER_ALG_DES_RFB:
261 ctx->ctx = g_new0(struct des_ctx, 1);
262 rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
263 des_set_key(ctx->ctx, rfbkey);
266 ctx->alg_encrypt_native = des_encrypt_native;
267 ctx->alg_decrypt_native = des_decrypt_native;
268 ctx->alg_encrypt_wrapper = des_encrypt_wrapper;
269 ctx->alg_decrypt_wrapper = des_decrypt_wrapper;
271 ctx->blocksize = DES_BLOCK_SIZE;
274 case QCRYPTO_CIPHER_ALG_AES_128:
275 case QCRYPTO_CIPHER_ALG_AES_192:
276 case QCRYPTO_CIPHER_ALG_AES_256:
277 ctx->ctx = g_new0(QCryptoNettleAES, 1);
279 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
280 ctx->ctx_tweak = g_new0(QCryptoNettleAES, 1);
283 aes_set_encrypt_key(&((QCryptoNettleAES *)ctx->ctx)->enc,
285 aes_set_decrypt_key(&((QCryptoNettleAES *)ctx->ctx)->dec,
288 aes_set_encrypt_key(&((QCryptoNettleAES *)ctx->ctx_tweak)->enc,
290 aes_set_decrypt_key(&((QCryptoNettleAES *)ctx->ctx_tweak)->dec,
293 aes_set_encrypt_key(&((QCryptoNettleAES *)ctx->ctx)->enc,
295 aes_set_decrypt_key(&((QCryptoNettleAES *)ctx->ctx)->dec,
299 ctx->alg_encrypt_native = aes_encrypt_native;
300 ctx->alg_decrypt_native = aes_decrypt_native;
301 ctx->alg_encrypt_wrapper = aes_encrypt_wrapper;
302 ctx->alg_decrypt_wrapper = aes_decrypt_wrapper;
304 ctx->blocksize = AES_BLOCK_SIZE;
307 case QCRYPTO_CIPHER_ALG_CAST5_128:
308 ctx->ctx = g_new0(struct cast128_ctx, 1);
310 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
311 ctx->ctx_tweak = g_new0(struct cast128_ctx, 1);
314 cast5_set_key(ctx->ctx, nkey, key);
315 cast5_set_key(ctx->ctx_tweak, nkey, key + nkey);
317 cast5_set_key(ctx->ctx, nkey, key);
320 ctx->alg_encrypt_native = cast128_encrypt_native;
321 ctx->alg_decrypt_native = cast128_decrypt_native;
322 ctx->alg_encrypt_wrapper = cast128_encrypt_wrapper;
323 ctx->alg_decrypt_wrapper = cast128_decrypt_wrapper;
325 ctx->blocksize = CAST128_BLOCK_SIZE;
328 case QCRYPTO_CIPHER_ALG_SERPENT_128:
329 case QCRYPTO_CIPHER_ALG_SERPENT_192:
330 case QCRYPTO_CIPHER_ALG_SERPENT_256:
331 ctx->ctx = g_new0(struct serpent_ctx, 1);
333 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
334 ctx->ctx_tweak = g_new0(struct serpent_ctx, 1);
337 serpent_set_key(ctx->ctx, nkey, key);
338 serpent_set_key(ctx->ctx_tweak, nkey, key + nkey);
340 serpent_set_key(ctx->ctx, nkey, key);
343 ctx->alg_encrypt_native = serpent_encrypt_native;
344 ctx->alg_decrypt_native = serpent_decrypt_native;
345 ctx->alg_encrypt_wrapper = serpent_encrypt_wrapper;
346 ctx->alg_decrypt_wrapper = serpent_decrypt_wrapper;
348 ctx->blocksize = SERPENT_BLOCK_SIZE;
351 case QCRYPTO_CIPHER_ALG_TWOFISH_128:
352 case QCRYPTO_CIPHER_ALG_TWOFISH_192:
353 case QCRYPTO_CIPHER_ALG_TWOFISH_256:
354 ctx->ctx = g_new0(struct twofish_ctx, 1);
356 if (mode == QCRYPTO_CIPHER_MODE_XTS) {
357 ctx->ctx_tweak = g_new0(struct twofish_ctx, 1);
360 twofish_set_key(ctx->ctx, nkey, key);
361 twofish_set_key(ctx->ctx_tweak, nkey, key + nkey);
363 twofish_set_key(ctx->ctx, nkey, key);
366 ctx->alg_encrypt_native = twofish_encrypt_native;
367 ctx->alg_decrypt_native = twofish_decrypt_native;
368 ctx->alg_encrypt_wrapper = twofish_encrypt_wrapper;
369 ctx->alg_decrypt_wrapper = twofish_decrypt_wrapper;
371 ctx->blocksize = TWOFISH_BLOCK_SIZE;
375 error_setg(errp, "Unsupported cipher algorithm %s",
376 QCryptoCipherAlgorithm_lookup[alg]);
380 if (mode == QCRYPTO_CIPHER_MODE_XTS &&
381 ctx->blocksize != XTS_BLOCK_SIZE) {
382 error_setg(errp, "Cipher block size %zu must equal XTS block size %d",
383 ctx->blocksize, XTS_BLOCK_SIZE);
387 ctx->iv = g_new0(uint8_t, ctx->blocksize);
392 qcrypto_cipher_free(cipher);
397 void qcrypto_cipher_free(QCryptoCipher *cipher)
399 QCryptoCipherNettle *ctx;
405 ctx = cipher->opaque;
408 g_free(ctx->ctx_tweak);
414 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
420 QCryptoCipherNettle *ctx = cipher->opaque;
422 if (len % ctx->blocksize) {
423 error_setg(errp, "Length %zu must be a multiple of block size %zu",
424 len, ctx->blocksize);
428 switch (cipher->mode) {
429 case QCRYPTO_CIPHER_MODE_ECB:
430 ctx->alg_encrypt_wrapper(ctx->ctx, len, out, in);
433 case QCRYPTO_CIPHER_MODE_CBC:
434 cbc_encrypt(ctx->ctx, ctx->alg_encrypt_native,
435 ctx->blocksize, ctx->iv,
439 case QCRYPTO_CIPHER_MODE_XTS:
440 xts_encrypt(ctx->ctx, ctx->ctx_tweak,
441 ctx->alg_encrypt_wrapper, ctx->alg_encrypt_wrapper,
442 ctx->iv, len, out, in);
445 case QCRYPTO_CIPHER_MODE_CTR:
446 ctr_crypt(ctx->ctx, ctx->alg_encrypt_native,
447 ctx->blocksize, ctx->iv,
452 error_setg(errp, "Unsupported cipher mode %s",
453 QCryptoCipherMode_lookup[cipher->mode]);
460 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
466 QCryptoCipherNettle *ctx = cipher->opaque;
468 if (len % ctx->blocksize) {
469 error_setg(errp, "Length %zu must be a multiple of block size %zu",
470 len, ctx->blocksize);
474 switch (cipher->mode) {
475 case QCRYPTO_CIPHER_MODE_ECB:
476 ctx->alg_decrypt_wrapper(ctx->ctx, len, out, in);
479 case QCRYPTO_CIPHER_MODE_CBC:
480 cbc_decrypt(ctx->ctx, ctx->alg_decrypt_native,
481 ctx->blocksize, ctx->iv,
485 case QCRYPTO_CIPHER_MODE_XTS:
486 xts_decrypt(ctx->ctx, ctx->ctx_tweak,
487 ctx->alg_encrypt_wrapper, ctx->alg_decrypt_wrapper,
488 ctx->iv, len, out, in);
490 case QCRYPTO_CIPHER_MODE_CTR:
491 ctr_crypt(ctx->ctx, ctx->alg_encrypt_native,
492 ctx->blocksize, ctx->iv,
497 error_setg(errp, "Unsupported cipher mode %s",
498 QCryptoCipherMode_lookup[cipher->mode]);
504 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
505 const uint8_t *iv, size_t niv,
508 QCryptoCipherNettle *ctx = cipher->opaque;
509 if (niv != ctx->blocksize) {
510 error_setg(errp, "Expected IV size %zu not %zu",
511 ctx->blocksize, niv);
514 memcpy(ctx->iv, iv, niv);