target-i386: Deconstruct the cpu_T array
[qemu/ar7.git] / crypto / cipher-gcrypt.c
blob56d4c9d10b78cad68df5841056af2f1725d3074a
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 "qemu/osdep.h"
22 #include <gcrypt.h>
25 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
27 switch (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:
32 return true;
33 default:
34 return false;
38 typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt;
39 struct QCryptoCipherGcrypt {
40 gcry_cipher_hd_t handle;
41 size_t blocksize;
44 QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
45 QCryptoCipherMode mode,
46 const uint8_t *key, size_t nkey,
47 Error **errp)
49 QCryptoCipher *cipher;
50 QCryptoCipherGcrypt *ctx;
51 gcry_error_t err;
52 int gcryalg, gcrymode;
54 switch (mode) {
55 case QCRYPTO_CIPHER_MODE_ECB:
56 gcrymode = GCRY_CIPHER_MODE_ECB;
57 break;
58 case QCRYPTO_CIPHER_MODE_CBC:
59 gcrymode = GCRY_CIPHER_MODE_CBC;
60 break;
61 default:
62 error_setg(errp, "Unsupported cipher mode %d", mode);
63 return NULL;
66 if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) {
67 return NULL;
70 switch (alg) {
71 case QCRYPTO_CIPHER_ALG_DES_RFB:
72 gcryalg = GCRY_CIPHER_DES;
73 break;
75 case QCRYPTO_CIPHER_ALG_AES_128:
76 gcryalg = GCRY_CIPHER_AES128;
77 break;
79 case QCRYPTO_CIPHER_ALG_AES_192:
80 gcryalg = GCRY_CIPHER_AES192;
81 break;
83 case QCRYPTO_CIPHER_ALG_AES_256:
84 gcryalg = GCRY_CIPHER_AES256;
85 break;
87 default:
88 error_setg(errp, "Unsupported cipher algorithm %d", alg);
89 return NULL;
92 cipher = g_new0(QCryptoCipher, 1);
93 cipher->alg = alg;
94 cipher->mode = mode;
96 ctx = g_new0(QCryptoCipherGcrypt, 1);
98 err = gcry_cipher_open(&ctx->handle, gcryalg, gcrymode, 0);
99 if (err != 0) {
100 error_setg(errp, "Cannot initialize cipher: %s",
101 gcry_strerror(err));
102 goto error;
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);
112 g_free(rfbkey);
113 ctx->blocksize = 8;
114 } else {
115 err = gcry_cipher_setkey(ctx->handle, key, nkey);
116 ctx->blocksize = 16;
118 if (err != 0) {
119 error_setg(errp, "Cannot set key: %s",
120 gcry_strerror(err));
121 goto error;
124 cipher->opaque = ctx;
125 return cipher;
127 error:
128 gcry_cipher_close(ctx->handle);
129 g_free(ctx);
130 g_free(cipher);
131 return NULL;
135 void qcrypto_cipher_free(QCryptoCipher *cipher)
137 QCryptoCipherGcrypt *ctx;
138 if (!cipher) {
139 return;
141 ctx = cipher->opaque;
142 gcry_cipher_close(ctx->handle);
143 g_free(ctx);
144 g_free(cipher);
148 int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
149 const void *in,
150 void *out,
151 size_t len,
152 Error **errp)
154 QCryptoCipherGcrypt *ctx = cipher->opaque;
155 gcry_error_t err;
157 if (len % ctx->blocksize) {
158 error_setg(errp, "Length %zu must be a multiple of block size %zu",
159 len, ctx->blocksize);
160 return -1;
163 err = gcry_cipher_encrypt(ctx->handle,
164 out, len,
165 in, len);
166 if (err != 0) {
167 error_setg(errp, "Cannot encrypt data: %s",
168 gcry_strerror(err));
169 return -1;
172 return 0;
176 int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
177 const void *in,
178 void *out,
179 size_t len,
180 Error **errp)
182 QCryptoCipherGcrypt *ctx = cipher->opaque;
183 gcry_error_t err;
185 if (len % ctx->blocksize) {
186 error_setg(errp, "Length %zu must be a multiple of block size %zu",
187 len, ctx->blocksize);
188 return -1;
191 err = gcry_cipher_decrypt(ctx->handle,
192 out, len,
193 in, len);
194 if (err != 0) {
195 error_setg(errp, "Cannot decrypt data: %s",
196 gcry_strerror(err));
197 return -1;
200 return 0;
203 int qcrypto_cipher_setiv(QCryptoCipher *cipher,
204 const uint8_t *iv, size_t niv,
205 Error **errp)
207 QCryptoCipherGcrypt *ctx = cipher->opaque;
208 gcry_error_t err;
210 if (niv != ctx->blocksize) {
211 error_setg(errp, "Expected IV size %zu not %zu",
212 ctx->blocksize, niv);
213 return -1;
216 gcry_cipher_reset(ctx->handle);
217 err = gcry_cipher_setiv(ctx->handle, iv, niv);
218 if (err != 0) {
219 error_setg(errp, "Cannot set IV: %s",
220 gcry_strerror(err));
221 return -1;
224 return 0;