fdc: move pick_geometry
[qemu.git] / crypto / cipher-nettle.c
blob7449338d3b657029a9775e5aff89484d5a870719
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 blocksize;
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->blocksize = 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->blocksize = 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->blocksize);
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 if (len % ctx->blocksize) {
188 error_setg(errp, "Length %zu must be a multiple of block size %zu",
189 len, ctx->blocksize);
190 return -1;
193 switch (cipher->mode) {
194 case QCRYPTO_CIPHER_MODE_ECB:
195 ctx->alg_encrypt(ctx->ctx_encrypt, len, out, in);
196 break;
198 case QCRYPTO_CIPHER_MODE_CBC:
199 cbc_encrypt(ctx->ctx_encrypt, ctx->alg_encrypt,
200 ctx->blocksize, ctx->iv,
201 len, out, in);
202 break;
203 default:
204 error_setg(errp, "Unsupported cipher algorithm %d",
205 cipher->alg);
206 return -1;
208 return 0;
212 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
213 const void *in,
214 void *out,
215 size_t len,
216 Error **errp)
218 QCryptoCipherNettle *ctx = cipher->opaque;
220 if (len % ctx->blocksize) {
221 error_setg(errp, "Length %zu must be a multiple of block size %zu",
222 len, ctx->blocksize);
223 return -1;
226 switch (cipher->mode) {
227 case QCRYPTO_CIPHER_MODE_ECB:
228 ctx->alg_decrypt(ctx->ctx_decrypt ? ctx->ctx_decrypt : ctx->ctx_encrypt,
229 len, out, in);
230 break;
232 case QCRYPTO_CIPHER_MODE_CBC:
233 cbc_decrypt(ctx->ctx_decrypt ? ctx->ctx_decrypt : ctx->ctx_encrypt,
234 ctx->alg_decrypt, ctx->blocksize, ctx->iv,
235 len, out, in);
236 break;
237 default:
238 error_setg(errp, "Unsupported cipher algorithm %d",
239 cipher->alg);
240 return -1;
242 return 0;
245 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
246 const uint8_t *iv, size_t niv,
247 Error **errp)
249 QCryptoCipherNettle *ctx = cipher->opaque;
250 if (niv != ctx->blocksize) {
251 error_setg(errp, "Expected IV size %zu not %zu",
252 ctx->blocksize, niv);
253 return -1;
255 memcpy(ctx->iv, iv, niv);
256 return 0;