2 * QEMU Crypto cipher libgcrypt 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"
25 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg
)
28 case QCRYPTO_CIPHER_ALG_DES_RFB
:
29 case QCRYPTO_CIPHER_ALG_AES_128
:
30 case QCRYPTO_CIPHER_ALG_AES_192
:
31 case QCRYPTO_CIPHER_ALG_AES_256
:
38 typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt
;
39 struct QCryptoCipherGcrypt
{
40 gcry_cipher_hd_t handle
;
44 QCryptoCipher
*qcrypto_cipher_new(QCryptoCipherAlgorithm alg
,
45 QCryptoCipherMode mode
,
46 const uint8_t *key
, size_t nkey
,
49 QCryptoCipher
*cipher
;
50 QCryptoCipherGcrypt
*ctx
;
52 int gcryalg
, gcrymode
;
55 case QCRYPTO_CIPHER_MODE_ECB
:
56 gcrymode
= GCRY_CIPHER_MODE_ECB
;
58 case QCRYPTO_CIPHER_MODE_CBC
:
59 gcrymode
= GCRY_CIPHER_MODE_CBC
;
62 error_setg(errp
, "Unsupported cipher mode %d", mode
);
66 if (!qcrypto_cipher_validate_key_length(alg
, nkey
, errp
)) {
71 case QCRYPTO_CIPHER_ALG_DES_RFB
:
72 gcryalg
= GCRY_CIPHER_DES
;
75 case QCRYPTO_CIPHER_ALG_AES_128
:
76 gcryalg
= GCRY_CIPHER_AES128
;
79 case QCRYPTO_CIPHER_ALG_AES_192
:
80 gcryalg
= GCRY_CIPHER_AES192
;
83 case QCRYPTO_CIPHER_ALG_AES_256
:
84 gcryalg
= GCRY_CIPHER_AES256
;
88 error_setg(errp
, "Unsupported cipher algorithm %d", alg
);
92 cipher
= g_new0(QCryptoCipher
, 1);
96 ctx
= g_new0(QCryptoCipherGcrypt
, 1);
98 err
= gcry_cipher_open(&ctx
->handle
, gcryalg
, gcrymode
, 0);
100 error_setg(errp
, "Cannot initialize cipher: %s",
105 if (cipher
->alg
== QCRYPTO_CIPHER_ALG_DES_RFB
) {
106 /* We're using standard DES cipher from gcrypt, so we need
107 * to munge the key so that the results are the same as the
108 * bizarre RFB variant of DES :-)
110 uint8_t *rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
111 err
= gcry_cipher_setkey(ctx
->handle
, rfbkey
, nkey
);
115 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
119 error_setg(errp
, "Cannot set key: %s",
124 cipher
->opaque
= ctx
;
128 gcry_cipher_close(ctx
->handle
);
135 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
137 QCryptoCipherGcrypt
*ctx
;
141 ctx
= cipher
->opaque
;
142 gcry_cipher_close(ctx
->handle
);
148 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
154 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
157 if (len
% ctx
->blocksize
) {
158 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
159 len
, ctx
->blocksize
);
163 err
= gcry_cipher_encrypt(ctx
->handle
,
167 error_setg(errp
, "Cannot encrypt data: %s",
176 int qcrypto_cipher_decrypt(QCryptoCipher
*cipher
,
182 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
185 if (len
% ctx
->blocksize
) {
186 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
187 len
, ctx
->blocksize
);
191 err
= gcry_cipher_decrypt(ctx
->handle
,
195 error_setg(errp
, "Cannot decrypt data: %s",
203 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
204 const uint8_t *iv
, size_t niv
,
207 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
210 if (niv
!= ctx
->blocksize
) {
211 error_setg(errp
, "Expected IV size %zu not %zu",
212 ctx
->blocksize
, niv
);
216 gcry_cipher_reset(ctx
->handle
);
217 err
= gcry_cipher_setiv(ctx
->handle
, iv
, niv
);
219 error_setg(errp
, "Cannot set IV: %s",