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>
28 #if CONFIG_NETTLE_VERSION_MAJOR < 3
29 typedef nettle_crypt_func nettle_cipher_func
;
31 typedef void * cipher_ctx_t
;
32 typedef unsigned cipher_length_t
;
34 typedef const void * cipher_ctx_t
;
35 typedef size_t cipher_length_t
;
38 static nettle_cipher_func aes_encrypt_wrapper
;
39 static nettle_cipher_func aes_decrypt_wrapper
;
40 static nettle_cipher_func des_encrypt_wrapper
;
41 static nettle_cipher_func des_decrypt_wrapper
;
43 static void aes_encrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
44 uint8_t *dst
, const uint8_t *src
)
46 aes_encrypt(ctx
, length
, dst
, src
);
49 static void aes_decrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
50 uint8_t *dst
, const uint8_t *src
)
52 aes_decrypt(ctx
, length
, dst
, src
);
55 static void des_encrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
56 uint8_t *dst
, const uint8_t *src
)
58 des_encrypt(ctx
, length
, dst
, src
);
61 static void des_decrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
62 uint8_t *dst
, const uint8_t *src
)
64 des_decrypt(ctx
, length
, dst
, src
);
67 static void cast128_encrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
68 uint8_t *dst
, const uint8_t *src
)
70 cast128_encrypt(ctx
, length
, dst
, src
);
73 static void cast128_decrypt_wrapper(cipher_ctx_t ctx
, cipher_length_t length
,
74 uint8_t *dst
, const uint8_t *src
)
76 cast128_decrypt(ctx
, length
, dst
, src
);
79 typedef struct QCryptoCipherNettle QCryptoCipherNettle
;
80 struct QCryptoCipherNettle
{
83 nettle_cipher_func
*alg_encrypt
;
84 nettle_cipher_func
*alg_decrypt
;
89 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg
)
92 case QCRYPTO_CIPHER_ALG_DES_RFB
:
93 case QCRYPTO_CIPHER_ALG_AES_128
:
94 case QCRYPTO_CIPHER_ALG_AES_192
:
95 case QCRYPTO_CIPHER_ALG_AES_256
:
96 case QCRYPTO_CIPHER_ALG_CAST5_128
:
104 QCryptoCipher
*qcrypto_cipher_new(QCryptoCipherAlgorithm alg
,
105 QCryptoCipherMode mode
,
106 const uint8_t *key
, size_t nkey
,
109 QCryptoCipher
*cipher
;
110 QCryptoCipherNettle
*ctx
;
114 case QCRYPTO_CIPHER_MODE_ECB
:
115 case QCRYPTO_CIPHER_MODE_CBC
:
118 error_setg(errp
, "Unsupported cipher mode %d", mode
);
122 if (!qcrypto_cipher_validate_key_length(alg
, nkey
, errp
)) {
126 cipher
= g_new0(QCryptoCipher
, 1);
130 ctx
= g_new0(QCryptoCipherNettle
, 1);
133 case QCRYPTO_CIPHER_ALG_DES_RFB
:
134 ctx
->ctx_encrypt
= g_new0(struct des_ctx
, 1);
135 ctx
->ctx_decrypt
= NULL
; /* 1 ctx can do both */
136 rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
137 des_set_key(ctx
->ctx_encrypt
, rfbkey
);
140 ctx
->alg_encrypt
= des_encrypt_wrapper
;
141 ctx
->alg_decrypt
= des_decrypt_wrapper
;
143 ctx
->blocksize
= DES_BLOCK_SIZE
;
146 case QCRYPTO_CIPHER_ALG_AES_128
:
147 case QCRYPTO_CIPHER_ALG_AES_192
:
148 case QCRYPTO_CIPHER_ALG_AES_256
:
149 ctx
->ctx_encrypt
= g_new0(struct aes_ctx
, 1);
150 ctx
->ctx_decrypt
= g_new0(struct aes_ctx
, 1);
152 aes_set_encrypt_key(ctx
->ctx_encrypt
, nkey
, key
);
153 aes_set_decrypt_key(ctx
->ctx_decrypt
, nkey
, key
);
155 ctx
->alg_encrypt
= aes_encrypt_wrapper
;
156 ctx
->alg_decrypt
= aes_decrypt_wrapper
;
158 ctx
->blocksize
= AES_BLOCK_SIZE
;
161 case QCRYPTO_CIPHER_ALG_CAST5_128
:
162 ctx
->ctx_encrypt
= g_new0(struct cast128_ctx
, 1);
163 ctx
->ctx_decrypt
= NULL
; /* 1 ctx can do both */
165 cast5_set_key(ctx
->ctx_encrypt
, nkey
, key
);
167 ctx
->alg_encrypt
= cast128_encrypt_wrapper
;
168 ctx
->alg_decrypt
= cast128_decrypt_wrapper
;
170 ctx
->blocksize
= CAST128_BLOCK_SIZE
;
173 error_setg(errp
, "Unsupported cipher algorithm %d", alg
);
177 ctx
->iv
= g_new0(uint8_t, ctx
->blocksize
);
178 cipher
->opaque
= ctx
;
189 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
191 QCryptoCipherNettle
*ctx
;
197 ctx
= cipher
->opaque
;
199 g_free(ctx
->ctx_encrypt
);
200 g_free(ctx
->ctx_decrypt
);
206 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
212 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
214 if (len
% ctx
->blocksize
) {
215 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
216 len
, ctx
->blocksize
);
220 switch (cipher
->mode
) {
221 case QCRYPTO_CIPHER_MODE_ECB
:
222 ctx
->alg_encrypt(ctx
->ctx_encrypt
, len
, out
, in
);
225 case QCRYPTO_CIPHER_MODE_CBC
:
226 cbc_encrypt(ctx
->ctx_encrypt
, ctx
->alg_encrypt
,
227 ctx
->blocksize
, ctx
->iv
,
231 error_setg(errp
, "Unsupported cipher algorithm %d",
239 int qcrypto_cipher_decrypt(QCryptoCipher
*cipher
,
245 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
247 if (len
% ctx
->blocksize
) {
248 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
249 len
, ctx
->blocksize
);
253 switch (cipher
->mode
) {
254 case QCRYPTO_CIPHER_MODE_ECB
:
255 ctx
->alg_decrypt(ctx
->ctx_decrypt
? ctx
->ctx_decrypt
: ctx
->ctx_encrypt
,
259 case QCRYPTO_CIPHER_MODE_CBC
:
260 cbc_decrypt(ctx
->ctx_decrypt
? ctx
->ctx_decrypt
: ctx
->ctx_encrypt
,
261 ctx
->alg_decrypt
, ctx
->blocksize
, ctx
->iv
,
265 error_setg(errp
, "Unsupported cipher algorithm %d",
272 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
273 const uint8_t *iv
, size_t niv
,
276 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
277 if (niv
!= ctx
->blocksize
) {
278 error_setg(errp
, "Expected IV size %zu not %zu",
279 ctx
->blocksize
, niv
);
282 memcpy(ctx
->iv
, iv
, niv
);