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 typedef struct QCryptoCipherNettle QCryptoCipherNettle
;
27 struct QCryptoCipherNettle
{
30 nettle_crypt_func
*alg_encrypt
;
31 nettle_crypt_func
*alg_decrypt
;
36 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg
)
39 case QCRYPTO_CIPHER_ALG_DES_RFB
:
40 case QCRYPTO_CIPHER_ALG_AES_128
:
41 case QCRYPTO_CIPHER_ALG_AES_192
:
42 case QCRYPTO_CIPHER_ALG_AES_256
:
50 QCryptoCipher
*qcrypto_cipher_new(QCryptoCipherAlgorithm alg
,
51 QCryptoCipherMode mode
,
52 const uint8_t *key
, size_t nkey
,
55 QCryptoCipher
*cipher
;
56 QCryptoCipherNettle
*ctx
;
60 case QCRYPTO_CIPHER_MODE_ECB
:
61 case QCRYPTO_CIPHER_MODE_CBC
:
64 error_setg(errp
, "Unsupported cipher mode %d", mode
);
68 if (!qcrypto_cipher_validate_key_length(alg
, nkey
, errp
)) {
72 cipher
= g_new0(QCryptoCipher
, 1);
76 ctx
= g_new0(QCryptoCipherNettle
, 1);
79 case QCRYPTO_CIPHER_ALG_DES_RFB
:
80 ctx
->ctx_encrypt
= g_new0(struct des_ctx
, 1);
81 ctx
->ctx_decrypt
= NULL
; /* 1 ctx can do both */
82 rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
83 des_set_key(ctx
->ctx_encrypt
, rfbkey
);
86 ctx
->alg_encrypt
= (nettle_crypt_func
*)des_encrypt
;
87 ctx
->alg_decrypt
= (nettle_crypt_func
*)des_decrypt
;
89 ctx
->niv
= DES_BLOCK_SIZE
;
92 case QCRYPTO_CIPHER_ALG_AES_128
:
93 case QCRYPTO_CIPHER_ALG_AES_192
:
94 case QCRYPTO_CIPHER_ALG_AES_256
:
95 ctx
->ctx_encrypt
= g_new0(struct aes_ctx
, 1);
96 ctx
->ctx_decrypt
= g_new0(struct aes_ctx
, 1);
98 aes_set_encrypt_key(ctx
->ctx_encrypt
, nkey
, key
);
99 aes_set_decrypt_key(ctx
->ctx_decrypt
, nkey
, key
);
101 ctx
->alg_encrypt
= (nettle_crypt_func
*)aes_encrypt
;
102 ctx
->alg_decrypt
= (nettle_crypt_func
*)aes_decrypt
;
104 ctx
->niv
= AES_BLOCK_SIZE
;
107 error_setg(errp
, "Unsupported cipher algorithm %d", alg
);
111 ctx
->iv
= g_new0(uint8_t, ctx
->niv
);
112 cipher
->opaque
= ctx
;
123 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
125 QCryptoCipherNettle
*ctx
;
131 ctx
= cipher
->opaque
;
133 g_free(ctx
->ctx_encrypt
);
134 g_free(ctx
->ctx_decrypt
);
140 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
146 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
148 switch (cipher
->mode
) {
149 case QCRYPTO_CIPHER_MODE_ECB
:
150 ctx
->alg_encrypt(ctx
->ctx_encrypt
, len
, out
, in
);
153 case QCRYPTO_CIPHER_MODE_CBC
:
154 cbc_encrypt(ctx
->ctx_encrypt
, ctx
->alg_encrypt
,
159 error_setg(errp
, "Unsupported cipher algorithm %d",
167 int qcrypto_cipher_decrypt(QCryptoCipher
*cipher
,
173 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
175 switch (cipher
->mode
) {
176 case QCRYPTO_CIPHER_MODE_ECB
:
177 ctx
->alg_decrypt(ctx
->ctx_decrypt ? ctx
->ctx_decrypt
: ctx
->ctx_encrypt
,
181 case QCRYPTO_CIPHER_MODE_CBC
:
182 cbc_decrypt(ctx
->ctx_decrypt ? ctx
->ctx_decrypt
: ctx
->ctx_encrypt
,
183 ctx
->alg_decrypt
, ctx
->niv
, ctx
->iv
,
187 error_setg(errp
, "Unsupported cipher algorithm %d",
194 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
195 const uint8_t *iv
, size_t niv
,
198 QCryptoCipherNettle
*ctx
= cipher
->opaque
;
199 if (niv
!= ctx
->niv
) {
200 error_setg(errp
, "Expected IV size %zu not %zu",
204 memcpy(ctx
->iv
, iv
, niv
);