block: Introduce parents list
[qemu.git] / crypto / cipher-gcrypt.c
blob8cfc5625004370121cfb8eec04ef0098a42d0233
1 /*
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 <gcrypt.h>
24 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
26 switch (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:
31 return true;
32 default:
33 return false;
38 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
39 QCryptoCipherMode mode,
40 const uint8_t *key, size_t nkey,
41 Error **errp)
43 QCryptoCipher *cipher;
44 gcry_cipher_hd_t handle;
45 gcry_error_t err;
46 int gcryalg, gcrymode;
48 switch (mode) {
49 case QCRYPTO_CIPHER_MODE_ECB:
50 gcrymode = GCRY_CIPHER_MODE_ECB;
51 break;
52 case QCRYPTO_CIPHER_MODE_CBC:
53 gcrymode = GCRY_CIPHER_MODE_CBC;
54 break;
55 default:
56 error_setg(errp, "Unsupported cipher mode %d", mode);
57 return NULL;
60 if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) {
61 return NULL;
64 switch (alg) {
65 case QCRYPTO_CIPHER_ALG_DES_RFB:
66 gcryalg = GCRY_CIPHER_DES;
67 break;
69 case QCRYPTO_CIPHER_ALG_AES_128:
70 gcryalg = GCRY_CIPHER_AES128;
71 break;
73 case QCRYPTO_CIPHER_ALG_AES_192:
74 gcryalg = GCRY_CIPHER_AES192;
75 break;
77 case QCRYPTO_CIPHER_ALG_AES_256:
78 gcryalg = GCRY_CIPHER_AES256;
79 break;
81 default:
82 error_setg(errp, "Unsupported cipher algorithm %d", alg);
83 return NULL;
86 cipher = g_new0(QCryptoCipher, 1);
87 cipher->alg = alg;
88 cipher->mode = mode;
90 err = gcry_cipher_open(&handle, gcryalg, gcrymode, 0);
91 if (err != 0) {
92 error_setg(errp, "Cannot initialize cipher: %s",
93 gcry_strerror(err));
94 goto error;
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);
104 g_free(rfbkey);
105 } else {
106 err = gcry_cipher_setkey(handle, key, nkey);
108 if (err != 0) {
109 error_setg(errp, "Cannot set key: %s",
110 gcry_strerror(err));
111 goto error;
114 cipher->opaque = handle;
115 return cipher;
117 error:
118 gcry_cipher_close(handle);
119 g_free(cipher);
120 return NULL;
124 void qcrypto_cipher_free(QCryptoCipher *cipher)
126 gcry_cipher_hd_t handle;
127 if (!cipher) {
128 return;
130 handle = cipher->opaque;
131 gcry_cipher_close(handle);
132 g_free(cipher);
136 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
137 const void *in,
138 void *out,
139 size_t len,
140 Error **errp)
142 gcry_cipher_hd_t handle = cipher->opaque;
143 gcry_error_t err;
145 err = gcry_cipher_encrypt(handle,
146 out, len,
147 in, len);
148 if (err != 0) {
149 error_setg(errp, "Cannot encrypt data: %s",
150 gcry_strerror(err));
151 return -1;
154 return 0;
158 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
159 const void *in,
160 void *out,
161 size_t len,
162 Error **errp)
164 gcry_cipher_hd_t handle = cipher->opaque;
165 gcry_error_t err;
167 err = gcry_cipher_decrypt(handle,
168 out, len,
169 in, len);
170 if (err != 0) {
171 error_setg(errp, "Cannot decrypt data: %s",
172 gcry_strerror(err));
173 return -1;
176 return 0;
179 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
180 const uint8_t *iv, size_t niv,
181 Error **errp)
183 gcry_cipher_hd_t handle = cipher->opaque;
184 gcry_error_t err;
186 gcry_cipher_reset(handle);
187 err = gcry_cipher_setiv(handle, iv, niv);
188 if (err != 0) {
189 error_setg(errp, "Cannot set IV: %s",
190 gcry_strerror(err));
191 return -1;
194 return 0;