qapi-commands: Don't feed output of mcgen() to mcgen() again
[qemu.git] / crypto / cipher-nettle.c
blobb01cb1c85785806d333ccd9844a215f7d2507388
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 <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;
31 #else
32 typedef const void * cipher_ctx_t;
33 typedef size_t cipher_length_t;
34 #endif
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 {
67 void *ctx_encrypt;
68 void *ctx_decrypt;
69 nettle_cipher_func *alg_encrypt;
70 nettle_cipher_func *alg_decrypt;
71 uint8_t *iv;
72 size_t niv;
75 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
77 switch (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:
82 return true;
83 default:
84 return false;
89 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
90 QCryptoCipherMode mode,
91 const uint8_t *key, size_t nkey,
92 Error **errp)
94 QCryptoCipher *cipher;
95 QCryptoCipherNettle *ctx;
96 uint8_t *rfbkey;
98 switch (mode) {
99 case QCRYPTO_CIPHER_MODE_ECB:
100 case QCRYPTO_CIPHER_MODE_CBC:
101 break;
102 default:
103 error_setg(errp, "Unsupported cipher mode %d", mode);
104 return NULL;
107 if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) {
108 return NULL;
111 cipher = g_new0(QCryptoCipher, 1);
112 cipher->alg = alg;
113 cipher->mode = mode;
115 ctx = g_new0(QCryptoCipherNettle, 1);
117 switch (alg) {
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);
123 g_free(rfbkey);
125 ctx->alg_encrypt = des_encrypt_wrapper;
126 ctx->alg_decrypt = des_decrypt_wrapper;
128 ctx->niv = DES_BLOCK_SIZE;
129 break;
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;
144 break;
145 default:
146 error_setg(errp, "Unsupported cipher algorithm %d", alg);
147 goto error;
150 ctx->iv = g_new0(uint8_t, ctx->niv);
151 cipher->opaque = ctx;
153 return cipher;
155 error:
156 g_free(cipher);
157 g_free(ctx);
158 return NULL;
162 void qcrypto_cipher_free(QCryptoCipher *cipher)
164 QCryptoCipherNettle *ctx;
166 if (!cipher) {
167 return;
170 ctx = cipher->opaque;
171 g_free(ctx->iv);
172 g_free(ctx->ctx_encrypt);
173 g_free(ctx->ctx_decrypt);
174 g_free(ctx);
175 g_free(cipher);
179 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
180 const void *in,
181 void *out,
182 size_t len,
183 Error **errp)
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);
190 break;
192 case QCRYPTO_CIPHER_MODE_CBC:
193 cbc_encrypt(ctx->ctx_encrypt, ctx->alg_encrypt,
194 ctx->niv, ctx->iv,
195 len, out, in);
196 break;
197 default:
198 error_setg(errp, "Unsupported cipher algorithm %d",
199 cipher->alg);
200 return -1;
202 return 0;
206 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
207 const void *in,
208 void *out,
209 size_t len,
210 Error **errp)
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,
217 len, out, in);
218 break;
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,
223 len, out, in);
224 break;
225 default:
226 error_setg(errp, "Unsupported cipher algorithm %d",
227 cipher->alg);
228 return -1;
230 return 0;
233 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
234 const uint8_t *iv, size_t niv,
235 Error **errp)
237 QCryptoCipherNettle *ctx = cipher->opaque;
238 if (niv != ctx->niv) {
239 error_setg(errp, "Expected IV size %zu not %zu",
240 ctx->niv, niv);
241 return -1;
243 memcpy(ctx->iv, iv, niv);
244 return 0;