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 <nettle/nettle-types.h>
22 #include <nettle/aes.h>
23 #include <nettle/des.h>
24 #include <nettle/cbc.h>
26 #if CONFIG_NETTLE_VERSION_MAJOR < 3
27 typedef nettle_crypt_func nettle_cipher_func;
29 typedef void * cipher_ctx_t;
30 typedef unsigned cipher_length_t;
32 typedef const void * cipher_ctx_t;
33 typedef size_t cipher_length_t;
36 static nettle_cipher_func aes_encrypt_wrapper;
37 static nettle_cipher_func aes_decrypt_wrapper;
38 static nettle_cipher_func des_encrypt_wrapper;
39 static nettle_cipher_func des_decrypt_wrapper;
41 static void aes_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
42 uint8_t *dst, const uint8_t *src)
44 aes_encrypt(ctx, length, dst, src);
47 static void aes_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
48 uint8_t *dst, const uint8_t *src)
50 aes_decrypt(ctx, length, dst, src);
53 static void des_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
54 uint8_t *dst, const uint8_t *src)
56 des_encrypt(ctx, length, dst, src);
59 static void des_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
60 uint8_t *dst, const uint8_t *src)
62 des_decrypt(ctx, length, dst, src);
65 typedef struct QCryptoCipherNettle QCryptoCipherNettle;
66 struct QCryptoCipherNettle {
69 nettle_cipher_func *alg_encrypt;
70 nettle_cipher_func *alg_decrypt;
75 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
78 case QCRYPTO_CIPHER_ALG_DES_RFB:
79 case QCRYPTO_CIPHER_ALG_AES_128:
80 case QCRYPTO_CIPHER_ALG_AES_192:
81 case QCRYPTO_CIPHER_ALG_AES_256:
89 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
90 QCryptoCipherMode mode,
91 const uint8_t *key, size_t nkey,
94 QCryptoCipher *cipher;
95 QCryptoCipherNettle *ctx;
99 case QCRYPTO_CIPHER_MODE_ECB:
100 case QCRYPTO_CIPHER_MODE_CBC:
103 error_setg(errp, "Unsupported cipher mode %d", mode);
107 if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) {
111 cipher = g_new0(QCryptoCipher, 1);
115 ctx = g_new0(QCryptoCipherNettle, 1);
118 case QCRYPTO_CIPHER_ALG_DES_RFB:
119 ctx->ctx_encrypt = g_new0(struct des_ctx, 1);
120 ctx->ctx_decrypt = NULL; /* 1 ctx can do both */
121 rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
122 des_set_key(ctx->ctx_encrypt, rfbkey);
125 ctx->alg_encrypt = des_encrypt_wrapper;
126 ctx->alg_decrypt = des_decrypt_wrapper;
128 ctx->niv = DES_BLOCK_SIZE;
131 case QCRYPTO_CIPHER_ALG_AES_128:
132 case QCRYPTO_CIPHER_ALG_AES_192:
133 case QCRYPTO_CIPHER_ALG_AES_256:
134 ctx->ctx_encrypt = g_new0(struct aes_ctx, 1);
135 ctx->ctx_decrypt = g_new0(struct aes_ctx, 1);
137 aes_set_encrypt_key(ctx->ctx_encrypt, nkey, key);
138 aes_set_decrypt_key(ctx->ctx_decrypt, nkey, key);
140 ctx->alg_encrypt = aes_encrypt_wrapper;
141 ctx->alg_decrypt = aes_decrypt_wrapper;
143 ctx->niv = AES_BLOCK_SIZE;
146 error_setg(errp, "Unsupported cipher algorithm %d", alg);
150 ctx->iv = g_new0(uint8_t, ctx->niv);
151 cipher->opaque = ctx;
162 void qcrypto_cipher_free(QCryptoCipher *cipher)
164 QCryptoCipherNettle *ctx;
170 ctx = cipher->opaque;
172 g_free(ctx->ctx_encrypt);
173 g_free(ctx->ctx_decrypt);
179 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
185 QCryptoCipherNettle *ctx = cipher->opaque;
187 switch (cipher->mode) {
188 case QCRYPTO_CIPHER_MODE_ECB:
189 ctx->alg_encrypt(ctx->ctx_encrypt, len, out, in);
192 case QCRYPTO_CIPHER_MODE_CBC:
193 cbc_encrypt(ctx->ctx_encrypt, ctx->alg_encrypt,
198 error_setg(errp, "Unsupported cipher algorithm %d",
206 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
212 QCryptoCipherNettle *ctx = cipher->opaque;
214 switch (cipher->mode) {
215 case QCRYPTO_CIPHER_MODE_ECB:
216 ctx->alg_decrypt(ctx->ctx_decrypt ? ctx->ctx_decrypt : ctx->ctx_encrypt,
220 case QCRYPTO_CIPHER_MODE_CBC:
221 cbc_decrypt(ctx->ctx_decrypt ? ctx->ctx_decrypt : ctx->ctx_encrypt,
222 ctx->alg_decrypt, ctx->niv, ctx->iv,
226 error_setg(errp, "Unsupported cipher algorithm %d",
233 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
234 const uint8_t *iv, size_t niv,
237 QCryptoCipherNettle *ctx = cipher->opaque;
238 if (niv != ctx->niv) {
239 error_setg(errp, "Expected IV size %zu not %zu",
243 memcpy(ctx->iv, iv, niv);