qmp-commands.hx: Document the missing options for migration capability commands
[qemu/ar7.git] / crypto / cipher-nettle.c
blobcd2675c0bca3aff7c0d4768d1523aa4620c759f2
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>
27 #if CONFIG_NETTLE_VERSION_MAJOR < 3
28 typedef nettle_crypt_func nettle_cipher_func;
30 typedef void * cipher_ctx_t;
31 typedef unsigned cipher_length_t;
32 #else
33 typedef const void * cipher_ctx_t;
34 typedef size_t cipher_length_t;
35 #endif
37 static nettle_cipher_func aes_encrypt_wrapper;
38 static nettle_cipher_func aes_decrypt_wrapper;
39 static nettle_cipher_func des_encrypt_wrapper;
40 static nettle_cipher_func des_decrypt_wrapper;
42 static void aes_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
43 uint8_t *dst, const uint8_t *src)
45 aes_encrypt(ctx, length, dst, src);
48 static void aes_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
49 uint8_t *dst, const uint8_t *src)
51 aes_decrypt(ctx, length, dst, src);
54 static void des_encrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
55 uint8_t *dst, const uint8_t *src)
57 des_encrypt(ctx, length, dst, src);
60 static void des_decrypt_wrapper(cipher_ctx_t ctx, cipher_length_t length,
61 uint8_t *dst, const uint8_t *src)
63 des_decrypt(ctx, length, dst, src);
66 typedef struct QCryptoCipherNettle QCryptoCipherNettle;
67 struct QCryptoCipherNettle {
68 void *ctx_encrypt;
69 void *ctx_decrypt;
70 nettle_cipher_func *alg_encrypt;
71 nettle_cipher_func *alg_decrypt;
72 uint8_t *iv;
73 size_t blocksize;
76 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
78 switch (alg) {
79 case QCRYPTO_CIPHER_ALG_DES_RFB:
80 case QCRYPTO_CIPHER_ALG_AES_128:
81 case QCRYPTO_CIPHER_ALG_AES_192:
82 case QCRYPTO_CIPHER_ALG_AES_256:
83 return true;
84 default:
85 return false;
90 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
91 QCryptoCipherMode mode,
92 const uint8_t *key, size_t nkey,
93 Error **errp)
95 QCryptoCipher *cipher;
96 QCryptoCipherNettle *ctx;
97 uint8_t *rfbkey;
99 switch (mode) {
100 case QCRYPTO_CIPHER_MODE_ECB:
101 case QCRYPTO_CIPHER_MODE_CBC:
102 break;
103 default:
104 error_setg(errp, "Unsupported cipher mode %d", mode);
105 return NULL;
108 if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) {
109 return NULL;
112 cipher = g_new0(QCryptoCipher, 1);
113 cipher->alg = alg;
114 cipher->mode = mode;
116 ctx = g_new0(QCryptoCipherNettle, 1);
118 switch (alg) {
119 case QCRYPTO_CIPHER_ALG_DES_RFB:
120 ctx->ctx_encrypt = g_new0(struct des_ctx, 1);
121 ctx->ctx_decrypt = NULL; /* 1 ctx can do both */
122 rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
123 des_set_key(ctx->ctx_encrypt, rfbkey);
124 g_free(rfbkey);
126 ctx->alg_encrypt = des_encrypt_wrapper;
127 ctx->alg_decrypt = des_decrypt_wrapper;
129 ctx->blocksize = DES_BLOCK_SIZE;
130 break;
132 case QCRYPTO_CIPHER_ALG_AES_128:
133 case QCRYPTO_CIPHER_ALG_AES_192:
134 case QCRYPTO_CIPHER_ALG_AES_256:
135 ctx->ctx_encrypt = g_new0(struct aes_ctx, 1);
136 ctx->ctx_decrypt = g_new0(struct aes_ctx, 1);
138 aes_set_encrypt_key(ctx->ctx_encrypt, nkey, key);
139 aes_set_decrypt_key(ctx->ctx_decrypt, nkey, key);
141 ctx->alg_encrypt = aes_encrypt_wrapper;
142 ctx->alg_decrypt = aes_decrypt_wrapper;
144 ctx->blocksize = AES_BLOCK_SIZE;
145 break;
146 default:
147 error_setg(errp, "Unsupported cipher algorithm %d", alg);
148 goto error;
151 ctx->iv = g_new0(uint8_t, ctx->blocksize);
152 cipher->opaque = ctx;
154 return cipher;
156 error:
157 g_free(cipher);
158 g_free(ctx);
159 return NULL;
163 void qcrypto_cipher_free(QCryptoCipher *cipher)
165 QCryptoCipherNettle *ctx;
167 if (!cipher) {
168 return;
171 ctx = cipher->opaque;
172 g_free(ctx->iv);
173 g_free(ctx->ctx_encrypt);
174 g_free(ctx->ctx_decrypt);
175 g_free(ctx);
176 g_free(cipher);
180 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
181 const void *in,
182 void *out,
183 size_t len,
184 Error **errp)
186 QCryptoCipherNettle *ctx = cipher->opaque;
188 if (len % ctx->blocksize) {
189 error_setg(errp, "Length %zu must be a multiple of block size %zu",
190 len, ctx->blocksize);
191 return -1;
194 switch (cipher->mode) {
195 case QCRYPTO_CIPHER_MODE_ECB:
196 ctx->alg_encrypt(ctx->ctx_encrypt, len, out, in);
197 break;
199 case QCRYPTO_CIPHER_MODE_CBC:
200 cbc_encrypt(ctx->ctx_encrypt, ctx->alg_encrypt,
201 ctx->blocksize, ctx->iv,
202 len, out, in);
203 break;
204 default:
205 error_setg(errp, "Unsupported cipher algorithm %d",
206 cipher->alg);
207 return -1;
209 return 0;
213 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
214 const void *in,
215 void *out,
216 size_t len,
217 Error **errp)
219 QCryptoCipherNettle *ctx = cipher->opaque;
221 if (len % ctx->blocksize) {
222 error_setg(errp, "Length %zu must be a multiple of block size %zu",
223 len, ctx->blocksize);
224 return -1;
227 switch (cipher->mode) {
228 case QCRYPTO_CIPHER_MODE_ECB:
229 ctx->alg_decrypt(ctx->ctx_decrypt ? ctx->ctx_decrypt : ctx->ctx_encrypt,
230 len, out, in);
231 break;
233 case QCRYPTO_CIPHER_MODE_CBC:
234 cbc_decrypt(ctx->ctx_decrypt ? ctx->ctx_decrypt : ctx->ctx_encrypt,
235 ctx->alg_decrypt, ctx->blocksize, ctx->iv,
236 len, out, in);
237 break;
238 default:
239 error_setg(errp, "Unsupported cipher algorithm %d",
240 cipher->alg);
241 return -1;
243 return 0;
246 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
247 const uint8_t *iv, size_t niv,
248 Error **errp)
250 QCryptoCipherNettle *ctx = cipher->opaque;
251 if (niv != ctx->blocksize) {
252 error_setg(errp, "Expected IV size %zu not %zu",
253 ctx->blocksize, niv);
254 return -1;
256 memcpy(ctx->iv, iv, niv);
257 return 0;