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
;
33 typedef const void * cipher_ctx_t
;
34 typedef size_t cipher_length_t
;
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
{
70 nettle_cipher_func
*alg_encrypt
;
71 nettle_cipher_func
*alg_decrypt
;
76 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm 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
:
90 QCryptoCipher
*qcrypto_cipher_new(QCryptoCipherAlgorithm alg
,
91 QCryptoCipherMode mode
,
92 const uint8_t *key
, size_t nkey
,
95 QCryptoCipher
*cipher
;
96 QCryptoCipherNettle
*ctx
;
100 case QCRYPTO_CIPHER_MODE_ECB
:
101 case QCRYPTO_CIPHER_MODE_CBC
:
104 error_setg(errp
, "Unsupported cipher mode %d", mode
);
108 if (!qcrypto_cipher_validate_key_length(alg
, nkey
, errp
)) {
112 cipher
= g_new0(QCryptoCipher
, 1);
116 ctx
= g_new0(QCryptoCipherNettle
, 1);
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
);
126 ctx
->alg_encrypt
= des_encrypt_wrapper
;
127 ctx
->alg_decrypt
= des_decrypt_wrapper
;
129 ctx
->blocksize
= DES_BLOCK_SIZE
;
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
;
147 error_setg(errp
, "Unsupported cipher algorithm %d", alg
);
151 ctx
->iv
= g_new0(uint8_t, ctx
->blocksize
);
152 cipher
->opaque
= ctx
;
163 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
165 QCryptoCipherNettle
*ctx
;
171 ctx
= cipher
->opaque
;
173 g_free(ctx
->ctx_encrypt
);
174 g_free(ctx
->ctx_decrypt
);
180 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
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
);
194 switch (cipher
->mode
) {
195 case QCRYPTO_CIPHER_MODE_ECB
:
196 ctx
->alg_encrypt(ctx
->ctx_encrypt
, len
, out
, in
);
199 case QCRYPTO_CIPHER_MODE_CBC
:
200 cbc_encrypt(ctx
->ctx_encrypt
, ctx
->alg_encrypt
,
201 ctx
->blocksize
, ctx
->iv
,
205 error_setg(errp
, "Unsupported cipher algorithm %d",
213 int qcrypto_cipher_decrypt(QCryptoCipher
*cipher
,
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
);
227 switch (cipher
->mode
) {
228 case QCRYPTO_CIPHER_MODE_ECB
:
229 ctx
->alg_decrypt(ctx
->ctx_decrypt
? ctx
->ctx_decrypt
: ctx
->ctx_encrypt
,
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
,
239 error_setg(errp
, "Unsupported cipher algorithm %d",
246 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
247 const uint8_t *iv
, size_t niv
,
250 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
251 if (niv
!= ctx
->blocksize
) {
252 error_setg(errp
, "Expected IV size %zu not %zu",
253 ctx
->blocksize
, niv
);
256 memcpy(ctx
->iv
, iv
, niv
);