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:
38 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
39 QCryptoCipherMode mode,
40 const uint8_t *key, size_t nkey,
43 QCryptoCipher *cipher;
44 gcry_cipher_hd_t handle;
46 int gcryalg, gcrymode;
49 case QCRYPTO_CIPHER_MODE_ECB:
50 gcrymode = GCRY_CIPHER_MODE_ECB;
52 case QCRYPTO_CIPHER_MODE_CBC:
53 gcrymode = GCRY_CIPHER_MODE_CBC;
56 error_setg(errp, "Unsupported cipher mode %d", mode);
60 if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) {
65 case QCRYPTO_CIPHER_ALG_DES_RFB:
66 gcryalg = GCRY_CIPHER_DES;
69 case QCRYPTO_CIPHER_ALG_AES_128:
70 gcryalg = GCRY_CIPHER_AES128;
73 case QCRYPTO_CIPHER_ALG_AES_192:
74 gcryalg = GCRY_CIPHER_AES192;
77 case QCRYPTO_CIPHER_ALG_AES_256:
78 gcryalg = GCRY_CIPHER_AES256;
82 error_setg(errp, "Unsupported cipher algorithm %d", alg);
86 cipher = g_new0(QCryptoCipher, 1);
90 err = gcry_cipher_open(&handle, gcryalg, gcrymode, 0);
92 error_setg(errp, "Cannot initialize cipher: %s",
97 if (cipher->alg == QCRYPTO_CIPHER_ALG_DES_RFB) {
98 /* We're using standard DES cipher from gcrypt, so we need
99 * to munge the key so that the results are the same as the
100 * bizarre RFB variant of DES :-)
102 uint8_t *rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey);
103 err = gcry_cipher_setkey(handle, rfbkey, nkey);
106 err = gcry_cipher_setkey(handle, key, nkey);
109 error_setg(errp, "Cannot set key: %s",
114 cipher->opaque = handle;
118 gcry_cipher_close(handle);
124 void qcrypto_cipher_free(QCryptoCipher *cipher)
126 gcry_cipher_hd_t handle;
130 handle = cipher->opaque;
131 gcry_cipher_close(handle);
136 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
142 gcry_cipher_hd_t handle = cipher->opaque;
145 err = gcry_cipher_encrypt(handle,
149 error_setg(errp, "Cannot encrypt data: %s",
158 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
164 gcry_cipher_hd_t handle = cipher->opaque;
167 err = gcry_cipher_decrypt(handle,
171 error_setg(errp, "Cannot decrypt data: %s",
179 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
180 const uint8_t *iv, size_t niv,
183 gcry_cipher_hd_t handle = cipher->opaque;
186 gcry_cipher_reset(handle);
187 err = gcry_cipher_setiv(handle, iv, niv);
189 error_setg(errp, "Cannot set IV: %s",