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>
26 #include <nettle/cast128.h>
27 #include <nettle/serpent.h>
28 #include <nettle/twofish.h>
30 #if CONFIG_NETTLE_VERSION_MAJOR < 3
31 typedef nettle_crypt_func nettle_cipher_func
;
33 typedef void * cipher_ctx_t
;
34 typedef unsigned cipher_length_t
;
36 typedef const void * cipher_ctx_t
;
37 typedef size_t cipher_length_t
;
40 static nettle_cipher_func aes_encrypt_wrapper
;
41 static nettle_cipher_func aes_decrypt_wrapper
;
42 static nettle_cipher_func des_encrypt_wrapper
;
43 static nettle_cipher_func des_decrypt_wrapper
;
45 static void aes_encrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
46 uint8_t *dst
, const uint8_t *src
)
48 aes_encrypt(ctx
, length
, dst
, src
);
51 static void aes_decrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
52 uint8_t *dst
, const uint8_t *src
)
54 aes_decrypt(ctx
, length
, dst
, src
);
57 static void des_encrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
58 uint8_t *dst
, const uint8_t *src
)
60 des_encrypt(ctx
, length
, dst
, src
);
63 static void des_decrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
64 uint8_t *dst
, const uint8_t *src
)
66 des_decrypt(ctx
, length
, dst
, src
);
69 static void cast128_encrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
70 uint8_t *dst
, const uint8_t *src
)
72 cast128_encrypt(ctx
, length
, dst
, src
);
75 static void cast128_decrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
76 uint8_t *dst
, const uint8_t *src
)
78 cast128_decrypt(ctx
, length
, dst
, src
);
81 static void serpent_encrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
82 uint8_t *dst
, const uint8_t *src
)
84 serpent_encrypt(ctx
, length
, dst
, src
);
87 static void serpent_decrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
88 uint8_t *dst
, const uint8_t *src
)
90 serpent_decrypt(ctx
, length
, dst
, src
);
93 static void twofish_encrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
94 uint8_t *dst
, const uint8_t *src
)
96 twofish_encrypt(ctx
, length
, dst
, src
);
99 static void twofish_decrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
100 uint8_t *dst
, const uint8_t *src
)
102 twofish_decrypt(ctx
, length
, dst
, src
);
105 typedef struct QCryptoCipherNettle QCryptoCipherNettle
;
106 struct QCryptoCipherNettle
{
109 nettle_cipher_func
*alg_encrypt
;
110 nettle_cipher_func
*alg_decrypt
;
115 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg
)
118 case QCRYPTO_CIPHER_ALG_DES_RFB
:
119 case QCRYPTO_CIPHER_ALG_AES_128
:
120 case QCRYPTO_CIPHER_ALG_AES_192
:
121 case QCRYPTO_CIPHER_ALG_AES_256
:
122 case QCRYPTO_CIPHER_ALG_CAST5_128
:
123 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
124 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
125 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
126 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
127 case QCRYPTO_CIPHER_ALG_TWOFISH_192
:
128 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
136 QCryptoCipher
*qcrypto_cipher_new(QCryptoCipherAlgorithm alg
,
137 QCryptoCipherMode mode
,
138 const uint8_t *key
, size_t nkey
,
141 QCryptoCipher
*cipher
;
142 QCryptoCipherNettle
*ctx
;
146 case QCRYPTO_CIPHER_MODE_ECB
:
147 case QCRYPTO_CIPHER_MODE_CBC
:
150 error_setg(errp
, "Unsupported cipher mode %d", mode
);
154 if (!qcrypto_cipher_validate_key_length(alg
, nkey
, errp
)) {
158 cipher
= g_new0(QCryptoCipher
, 1);
162 ctx
= g_new0(QCryptoCipherNettle
, 1);
165 case QCRYPTO_CIPHER_ALG_DES_RFB
:
166 ctx
->ctx_encrypt
= g_new0(struct des_ctx
, 1);
167 ctx
->ctx_decrypt
= NULL
; /* 1 ctx can do both */
168 rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
169 des_set_key(ctx
->ctx_encrypt
, rfbkey
);
172 ctx
->alg_encrypt
= des_encrypt_wrapper
;
173 ctx
->alg_decrypt
= des_decrypt_wrapper
;
175 ctx
->blocksize
= DES_BLOCK_SIZE
;
178 case QCRYPTO_CIPHER_ALG_AES_128
:
179 case QCRYPTO_CIPHER_ALG_AES_192
:
180 case QCRYPTO_CIPHER_ALG_AES_256
:
181 ctx
->ctx_encrypt
= g_new0(struct aes_ctx
, 1);
182 ctx
->ctx_decrypt
= g_new0(struct aes_ctx
, 1);
184 aes_set_encrypt_key(ctx
->ctx_encrypt
, nkey
, key
);
185 aes_set_decrypt_key(ctx
->ctx_decrypt
, nkey
, key
);
187 ctx
->alg_encrypt
= aes_encrypt_wrapper
;
188 ctx
->alg_decrypt
= aes_decrypt_wrapper
;
190 ctx
->blocksize
= AES_BLOCK_SIZE
;
193 case QCRYPTO_CIPHER_ALG_CAST5_128
:
194 ctx
->ctx_encrypt
= g_new0(struct cast128_ctx
, 1);
195 ctx
->ctx_decrypt
= NULL
; /* 1 ctx can do both */
197 cast5_set_key(ctx
->ctx_encrypt
, nkey
, key
);
199 ctx
->alg_encrypt
= cast128_encrypt_wrapper
;
200 ctx
->alg_decrypt
= cast128_decrypt_wrapper
;
202 ctx
->blocksize
= CAST128_BLOCK_SIZE
;
205 case QCRYPTO_CIPHER_ALG_SERPENT_128
:
206 case QCRYPTO_CIPHER_ALG_SERPENT_192
:
207 case QCRYPTO_CIPHER_ALG_SERPENT_256
:
208 ctx
->ctx_encrypt
= g_new0(struct serpent_ctx
, 1);
209 ctx
->ctx_decrypt
= NULL
; /* 1 ctx can do both */
211 serpent_set_key(ctx
->ctx_encrypt
, nkey
, key
);
213 ctx
->alg_encrypt
= serpent_encrypt_wrapper
;
214 ctx
->alg_decrypt
= serpent_decrypt_wrapper
;
216 ctx
->blocksize
= SERPENT_BLOCK_SIZE
;
219 case QCRYPTO_CIPHER_ALG_TWOFISH_128
:
220 case QCRYPTO_CIPHER_ALG_TWOFISH_192
:
221 case QCRYPTO_CIPHER_ALG_TWOFISH_256
:
222 ctx
->ctx_encrypt
= g_new0(struct twofish_ctx
, 1);
223 ctx
->ctx_decrypt
= NULL
; /* 1 ctx can do both */
225 twofish_set_key(ctx
->ctx_encrypt
, nkey
, key
);
227 ctx
->alg_encrypt
= twofish_encrypt_wrapper
;
228 ctx
->alg_decrypt
= twofish_decrypt_wrapper
;
230 ctx
->blocksize
= TWOFISH_BLOCK_SIZE
;
234 error_setg(errp
, "Unsupported cipher algorithm %d", alg
);
238 ctx
->iv
= g_new0(uint8_t, ctx
->blocksize
);
239 cipher
->opaque
= ctx
;
250 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
252 QCryptoCipherNettle
*ctx
;
258 ctx
= cipher
->opaque
;
260 g_free(ctx
->ctx_encrypt
);
261 g_free(ctx
->ctx_decrypt
);
267 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
273 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
275 if (len
% ctx
->blocksize
) {
276 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
277 len
, ctx
->blocksize
);
281 switch (cipher
->mode
) {
282 case QCRYPTO_CIPHER_MODE_ECB
:
283 ctx
->alg_encrypt(ctx
->ctx_encrypt
, len
, out
, in
);
286 case QCRYPTO_CIPHER_MODE_CBC
:
287 cbc_encrypt(ctx
->ctx_encrypt
, ctx
->alg_encrypt
,
288 ctx
->blocksize
, ctx
->iv
,
292 error_setg(errp
, "Unsupported cipher algorithm %d",
300 int qcrypto_cipher_decrypt(QCryptoCipher
*cipher
,
306 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
308 if (len
% ctx
->blocksize
) {
309 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
310 len
, ctx
->blocksize
);
314 switch (cipher
->mode
) {
315 case QCRYPTO_CIPHER_MODE_ECB
:
316 ctx
->alg_decrypt(ctx
->ctx_decrypt
? ctx
->ctx_decrypt
: ctx
->ctx_encrypt
,
320 case QCRYPTO_CIPHER_MODE_CBC
:
321 cbc_decrypt(ctx
->ctx_decrypt
? ctx
->ctx_decrypt
: ctx
->ctx_encrypt
,
322 ctx
->alg_decrypt
, ctx
->blocksize
, ctx
->iv
,
326 error_setg(errp
, "Unsupported cipher algorithm %d",
333 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
334 const uint8_t *iv
, size_t niv
,
337 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
338 if (niv
!= ctx
->blocksize
) {
339 error_setg(errp
, "Expected IV size %zu not %zu",
340 ctx
->blocksize
, niv
);
343 memcpy(ctx
->iv
, iv
, niv
);