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/>.
24 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg
)
27 case QCRYPTO_CIPHER_ALG_DES_RFB
:
28 case QCRYPTO_CIPHER_ALG_AES_128
:
29 case QCRYPTO_CIPHER_ALG_AES_192
:
30 case QCRYPTO_CIPHER_ALG_AES_256
:
37 typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt
;
38 struct QCryptoCipherGcrypt
{
39 gcry_cipher_hd_t handle
;
43 QCryptoCipher
*qcrypto_cipher_new(QCryptoCipherAlgorithm alg
,
44 QCryptoCipherMode mode
,
45 const uint8_t *key
, size_t nkey
,
48 QCryptoCipher
*cipher
;
49 QCryptoCipherGcrypt
*ctx
;
51 int gcryalg
, gcrymode
;
54 case QCRYPTO_CIPHER_MODE_ECB
:
55 gcrymode
= GCRY_CIPHER_MODE_ECB
;
57 case QCRYPTO_CIPHER_MODE_CBC
:
58 gcrymode
= GCRY_CIPHER_MODE_CBC
;
61 error_setg(errp
, "Unsupported cipher mode %d", mode
);
65 if (!qcrypto_cipher_validate_key_length(alg
, nkey
, errp
)) {
70 case QCRYPTO_CIPHER_ALG_DES_RFB
:
71 gcryalg
= GCRY_CIPHER_DES
;
74 case QCRYPTO_CIPHER_ALG_AES_128
:
75 gcryalg
= GCRY_CIPHER_AES128
;
78 case QCRYPTO_CIPHER_ALG_AES_192
:
79 gcryalg
= GCRY_CIPHER_AES192
;
82 case QCRYPTO_CIPHER_ALG_AES_256
:
83 gcryalg
= GCRY_CIPHER_AES256
;
87 error_setg(errp
, "Unsupported cipher algorithm %d", alg
);
91 cipher
= g_new0(QCryptoCipher
, 1);
95 ctx
= g_new0(QCryptoCipherGcrypt
, 1);
97 err
= gcry_cipher_open(&ctx
->handle
, gcryalg
, gcrymode
, 0);
99 error_setg(errp
, "Cannot initialize cipher: %s",
104 if (cipher
->alg
== QCRYPTO_CIPHER_ALG_DES_RFB
) {
105 /* We're using standard DES cipher from gcrypt, so we need
106 * to munge the key so that the results are the same as the
107 * bizarre RFB variant of DES :-)
109 uint8_t *rfbkey
= qcrypto_cipher_munge_des_rfb_key(key
, nkey
);
110 err
= gcry_cipher_setkey(ctx
->handle
, rfbkey
, nkey
);
114 err
= gcry_cipher_setkey(ctx
->handle
, key
, nkey
);
118 error_setg(errp
, "Cannot set key: %s",
123 cipher
->opaque
= ctx
;
127 gcry_cipher_close(ctx
->handle
);
134 void qcrypto_cipher_free(QCryptoCipher
*cipher
)
136 QCryptoCipherGcrypt
*ctx
;
140 ctx
= cipher
->opaque
;
141 gcry_cipher_close(ctx
->handle
);
147 int qcrypto_cipher_encrypt(QCryptoCipher
*cipher
,
153 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
156 if (len
% ctx
->blocksize
) {
157 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
158 len
, ctx
->blocksize
);
162 err
= gcry_cipher_encrypt(ctx
->handle
,
166 error_setg(errp
, "Cannot encrypt data: %s",
175 int qcrypto_cipher_decrypt(QCryptoCipher
*cipher
,
181 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
184 if (len
% ctx
->blocksize
) {
185 error_setg(errp
, "Length %zu must be a multiple of block size %zu",
186 len
, ctx
->blocksize
);
190 err
= gcry_cipher_decrypt(ctx
->handle
,
194 error_setg(errp
, "Cannot decrypt data: %s",
202 int qcrypto_cipher_setiv(QCryptoCipher
*cipher
,
203 const uint8_t *iv
, size_t niv
,
206 QCryptoCipherGcrypt
*ctx
= cipher
->opaque
;
209 if (niv
!= ctx
->blocksize
) {
210 error_setg(errp
, "Expected IV size %zu not %zu",
211 ctx
->blocksize
, niv
);
215 gcry_cipher_reset(ctx
->handle
);
216 err
= gcry_cipher_setiv(ctx
->handle
, iv
, niv
);
218 error_setg(errp
, "Cannot set IV: %s",