]>
Commit | Line | Data |
---|---|---|
62893b67 DB |
1 | /* |
2 | * QEMU Crypto cipher libgcrypt algorithms | |
3 | * | |
4 | * Copyright (c) 2015 Red Hat, Inc. | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | * | |
19 | */ | |
20 | ||
42f7a448 | 21 | #include "qemu/osdep.h" |
62893b67 DB |
22 | #include <gcrypt.h> |
23 | ||
24 | ||
25 | bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg) | |
26 | { | |
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; | |
35 | } | |
36 | } | |
37 | ||
3a661f1e DB |
38 | typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt; |
39 | struct QCryptoCipherGcrypt { | |
40 | gcry_cipher_hd_t handle; | |
41 | size_t blocksize; | |
42 | }; | |
62893b67 DB |
43 | |
44 | QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, | |
45 | QCryptoCipherMode mode, | |
46 | const uint8_t *key, size_t nkey, | |
47 | Error **errp) | |
48 | { | |
49 | QCryptoCipher *cipher; | |
3a661f1e | 50 | QCryptoCipherGcrypt *ctx; |
62893b67 DB |
51 | gcry_error_t err; |
52 | int gcryalg, gcrymode; | |
53 | ||
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; | |
64 | } | |
65 | ||
66 | if (!qcrypto_cipher_validate_key_length(alg, nkey, errp)) { | |
67 | return NULL; | |
68 | } | |
69 | ||
70 | switch (alg) { | |
71 | case QCRYPTO_CIPHER_ALG_DES_RFB: | |
72 | gcryalg = GCRY_CIPHER_DES; | |
73 | break; | |
74 | ||
75 | case QCRYPTO_CIPHER_ALG_AES_128: | |
76 | gcryalg = GCRY_CIPHER_AES128; | |
77 | break; | |
78 | ||
79 | case QCRYPTO_CIPHER_ALG_AES_192: | |
80 | gcryalg = GCRY_CIPHER_AES192; | |
81 | break; | |
82 | ||
83 | case QCRYPTO_CIPHER_ALG_AES_256: | |
84 | gcryalg = GCRY_CIPHER_AES256; | |
85 | break; | |
86 | ||
87 | default: | |
88 | error_setg(errp, "Unsupported cipher algorithm %d", alg); | |
89 | return NULL; | |
90 | } | |
91 | ||
92 | cipher = g_new0(QCryptoCipher, 1); | |
93 | cipher->alg = alg; | |
94 | cipher->mode = mode; | |
95 | ||
3a661f1e DB |
96 | ctx = g_new0(QCryptoCipherGcrypt, 1); |
97 | ||
98 | err = gcry_cipher_open(&ctx->handle, gcryalg, gcrymode, 0); | |
62893b67 DB |
99 | if (err != 0) { |
100 | error_setg(errp, "Cannot initialize cipher: %s", | |
101 | gcry_strerror(err)); | |
102 | goto error; | |
103 | } | |
104 | ||
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 :-) | |
109 | */ | |
110 | uint8_t *rfbkey = qcrypto_cipher_munge_des_rfb_key(key, nkey); | |
3a661f1e | 111 | err = gcry_cipher_setkey(ctx->handle, rfbkey, nkey); |
62893b67 | 112 | g_free(rfbkey); |
3a661f1e | 113 | ctx->blocksize = 8; |
62893b67 | 114 | } else { |
3a661f1e DB |
115 | err = gcry_cipher_setkey(ctx->handle, key, nkey); |
116 | ctx->blocksize = 16; | |
62893b67 DB |
117 | } |
118 | if (err != 0) { | |
119 | error_setg(errp, "Cannot set key: %s", | |
120 | gcry_strerror(err)); | |
121 | goto error; | |
122 | } | |
123 | ||
3a661f1e | 124 | cipher->opaque = ctx; |
62893b67 DB |
125 | return cipher; |
126 | ||
127 | error: | |
3a661f1e DB |
128 | gcry_cipher_close(ctx->handle); |
129 | g_free(ctx); | |
62893b67 DB |
130 | g_free(cipher); |
131 | return NULL; | |
132 | } | |
133 | ||
134 | ||
135 | void qcrypto_cipher_free(QCryptoCipher *cipher) | |
136 | { | |
3a661f1e | 137 | QCryptoCipherGcrypt *ctx; |
62893b67 DB |
138 | if (!cipher) { |
139 | return; | |
140 | } | |
3a661f1e DB |
141 | ctx = cipher->opaque; |
142 | gcry_cipher_close(ctx->handle); | |
143 | g_free(ctx); | |
62893b67 DB |
144 | g_free(cipher); |
145 | } | |
146 | ||
147 | ||
148 | int qcrypto_cipher_encrypt(QCryptoCipher *cipher, | |
149 | const void *in, | |
150 | void *out, | |
151 | size_t len, | |
152 | Error **errp) | |
153 | { | |
3a661f1e | 154 | QCryptoCipherGcrypt *ctx = cipher->opaque; |
62893b67 DB |
155 | gcry_error_t err; |
156 | ||
3a661f1e DB |
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; | |
161 | } | |
162 | ||
163 | err = gcry_cipher_encrypt(ctx->handle, | |
62893b67 DB |
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; | |
170 | } | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | ||
176 | int qcrypto_cipher_decrypt(QCryptoCipher *cipher, | |
177 | const void *in, | |
178 | void *out, | |
179 | size_t len, | |
180 | Error **errp) | |
181 | { | |
3a661f1e | 182 | QCryptoCipherGcrypt *ctx = cipher->opaque; |
62893b67 DB |
183 | gcry_error_t err; |
184 | ||
3a661f1e DB |
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; | |
189 | } | |
190 | ||
191 | err = gcry_cipher_decrypt(ctx->handle, | |
62893b67 DB |
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; | |
198 | } | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
203 | int qcrypto_cipher_setiv(QCryptoCipher *cipher, | |
204 | const uint8_t *iv, size_t niv, | |
205 | Error **errp) | |
206 | { | |
3a661f1e | 207 | QCryptoCipherGcrypt *ctx = cipher->opaque; |
62893b67 DB |
208 | gcry_error_t err; |
209 | ||
3a661f1e DB |
210 | if (niv != ctx->blocksize) { |
211 | error_setg(errp, "Expected IV size %zu not %zu", | |
212 | ctx->blocksize, niv); | |
213 | return -1; | |
214 | } | |
215 | ||
216 | gcry_cipher_reset(ctx->handle); | |
217 | err = gcry_cipher_setiv(ctx->handle, iv, niv); | |
62893b67 DB |
218 | if (err != 0) { |
219 | error_setg(errp, "Cannot set IV: %s", | |
220 | gcry_strerror(err)); | |
221 | return -1; | |
222 | } | |
223 | ||
224 | return 0; | |
225 | } |