crypto: Use the correct const type for driver
[qemu/ar7.git] / crypto / cipher-builtin.c.inc
blob156f32f1c701befca248c79735f983f786ffdc1e
1 /*
2  * QEMU Crypto cipher built-in 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.1 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  */
21 #include "crypto/aes.h"
22 #include "crypto/desrfb.h"
23 #include "crypto/xts.h"
25 typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext;
26 struct QCryptoCipherBuiltinAESContext {
27     AES_KEY enc;
28     AES_KEY dec;
30 typedef struct QCryptoCipherBuiltinAES QCryptoCipherBuiltinAES;
31 struct QCryptoCipherBuiltinAES {
32     QCryptoCipherBuiltinAESContext key;
33     QCryptoCipherBuiltinAESContext key_tweak;
34     uint8_t iv[AES_BLOCK_SIZE];
36 typedef struct QCryptoCipherBuiltinDESRFB QCryptoCipherBuiltinDESRFB;
37 struct QCryptoCipherBuiltinDESRFB {
38     uint8_t *key;
39     size_t nkey;
42 typedef struct QCryptoCipherBuiltin QCryptoCipherBuiltin;
43 struct QCryptoCipherBuiltin {
44     union {
45         QCryptoCipherBuiltinAES aes;
46         QCryptoCipherBuiltinDESRFB desrfb;
47     } state;
48     size_t blocksize;
49     void (*free)(QCryptoCipher *cipher);
50     int (*setiv)(QCryptoCipher *cipher,
51                  const uint8_t *iv, size_t niv,
52                  Error **errp);
53     int (*encrypt)(QCryptoCipher *cipher,
54                    const void *in,
55                    void *out,
56                    size_t len,
57                    Error **errp);
58     int (*decrypt)(QCryptoCipher *cipher,
59                    const void *in,
60                    void *out,
61                    size_t len,
62                    Error **errp);
66 static void qcrypto_cipher_free_aes(QCryptoCipher *cipher)
68     QCryptoCipherBuiltin *ctxt = cipher->opaque;
70     g_free(ctxt);
71     cipher->opaque = NULL;
75 static void qcrypto_cipher_aes_ecb_encrypt(const AES_KEY *key,
76                                            const void *in,
77                                            void *out,
78                                            size_t len)
80     const uint8_t *inptr = in;
81     uint8_t *outptr = out;
82     while (len) {
83         if (len > AES_BLOCK_SIZE) {
84             AES_encrypt(inptr, outptr, key);
85             inptr += AES_BLOCK_SIZE;
86             outptr += AES_BLOCK_SIZE;
87             len -= AES_BLOCK_SIZE;
88         } else {
89             uint8_t tmp1[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
90             memcpy(tmp1, inptr, len);
91             /* Fill with 0 to avoid valgrind uninitialized reads */
92             memset(tmp1 + len, 0, sizeof(tmp1) - len);
93             AES_encrypt(tmp1, tmp2, key);
94             memcpy(outptr, tmp2, len);
95             len = 0;
96         }
97     }
101 static void qcrypto_cipher_aes_ecb_decrypt(const AES_KEY *key,
102                                            const void *in,
103                                            void *out,
104                                            size_t len)
106     const uint8_t *inptr = in;
107     uint8_t *outptr = out;
108     while (len) {
109         if (len > AES_BLOCK_SIZE) {
110             AES_decrypt(inptr, outptr, key);
111             inptr += AES_BLOCK_SIZE;
112             outptr += AES_BLOCK_SIZE;
113             len -= AES_BLOCK_SIZE;
114         } else {
115             uint8_t tmp1[AES_BLOCK_SIZE], tmp2[AES_BLOCK_SIZE];
116             memcpy(tmp1, inptr, len);
117             /* Fill with 0 to avoid valgrind uninitialized reads */
118             memset(tmp1 + len, 0, sizeof(tmp1) - len);
119             AES_decrypt(tmp1, tmp2, key);
120             memcpy(outptr, tmp2, len);
121             len = 0;
122         }
123     }
127 static void qcrypto_cipher_aes_xts_encrypt(const void *ctx,
128                                            size_t length,
129                                            uint8_t *dst,
130                                            const uint8_t *src)
132     const QCryptoCipherBuiltinAESContext *aesctx = ctx;
134     qcrypto_cipher_aes_ecb_encrypt(&aesctx->enc, src, dst, length);
138 static void qcrypto_cipher_aes_xts_decrypt(const void *ctx,
139                                            size_t length,
140                                            uint8_t *dst,
141                                            const uint8_t *src)
143     const QCryptoCipherBuiltinAESContext *aesctx = ctx;
145     qcrypto_cipher_aes_ecb_decrypt(&aesctx->dec, src, dst, length);
149 static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher,
150                                       const void *in,
151                                       void *out,
152                                       size_t len,
153                                       Error **errp)
155     QCryptoCipherBuiltin *ctxt = cipher->opaque;
157     switch (cipher->mode) {
158     case QCRYPTO_CIPHER_MODE_ECB:
159         qcrypto_cipher_aes_ecb_encrypt(&ctxt->state.aes.key.enc,
160                                        in, out, len);
161         break;
162     case QCRYPTO_CIPHER_MODE_CBC:
163         AES_cbc_encrypt(in, out, len,
164                         &ctxt->state.aes.key.enc,
165                         ctxt->state.aes.iv, 1);
166         break;
167     case QCRYPTO_CIPHER_MODE_XTS:
168         xts_encrypt(&ctxt->state.aes.key,
169                     &ctxt->state.aes.key_tweak,
170                     qcrypto_cipher_aes_xts_encrypt,
171                     qcrypto_cipher_aes_xts_decrypt,
172                     ctxt->state.aes.iv,
173                     len, out, in);
174         break;
175     default:
176         g_assert_not_reached();
177     }
179     return 0;
183 static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher,
184                                       const void *in,
185                                       void *out,
186                                       size_t len,
187                                       Error **errp)
189     QCryptoCipherBuiltin *ctxt = cipher->opaque;
191     switch (cipher->mode) {
192     case QCRYPTO_CIPHER_MODE_ECB:
193         qcrypto_cipher_aes_ecb_decrypt(&ctxt->state.aes.key.dec,
194                                        in, out, len);
195         break;
196     case QCRYPTO_CIPHER_MODE_CBC:
197         AES_cbc_encrypt(in, out, len,
198                         &ctxt->state.aes.key.dec,
199                         ctxt->state.aes.iv, 0);
200         break;
201     case QCRYPTO_CIPHER_MODE_XTS:
202         xts_decrypt(&ctxt->state.aes.key,
203                     &ctxt->state.aes.key_tweak,
204                     qcrypto_cipher_aes_xts_encrypt,
205                     qcrypto_cipher_aes_xts_decrypt,
206                     ctxt->state.aes.iv,
207                     len, out, in);
208         break;
209     default:
210         g_assert_not_reached();
211     }
213     return 0;
216 static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
217                                      const uint8_t *iv, size_t niv,
218                                      Error **errp)
220     QCryptoCipherBuiltin *ctxt = cipher->opaque;
221     if (niv != AES_BLOCK_SIZE) {
222         error_setg(errp, "IV must be %d bytes not %zu",
223                    AES_BLOCK_SIZE, niv);
224         return -1;
225     }
227     memcpy(ctxt->state.aes.iv, iv, AES_BLOCK_SIZE);
229     return 0;
235 static QCryptoCipherBuiltin *
236 qcrypto_cipher_init_aes(QCryptoCipherMode mode,
237                         const uint8_t *key, size_t nkey,
238                         Error **errp)
240     QCryptoCipherBuiltin *ctxt;
242     if (mode != QCRYPTO_CIPHER_MODE_CBC &&
243         mode != QCRYPTO_CIPHER_MODE_ECB &&
244         mode != QCRYPTO_CIPHER_MODE_XTS) {
245         error_setg(errp, "Unsupported cipher mode %s",
246                    QCryptoCipherMode_str(mode));
247         return NULL;
248     }
250     ctxt = g_new0(QCryptoCipherBuiltin, 1);
252     if (mode == QCRYPTO_CIPHER_MODE_XTS) {
253         if (AES_set_encrypt_key(key, nkey * 4, &ctxt->state.aes.key.enc) != 0) {
254             error_setg(errp, "Failed to set encryption key");
255             goto error;
256         }
258         if (AES_set_decrypt_key(key, nkey * 4, &ctxt->state.aes.key.dec) != 0) {
259             error_setg(errp, "Failed to set decryption key");
260             goto error;
261         }
263         if (AES_set_encrypt_key(key + (nkey / 2), nkey * 4,
264                                 &ctxt->state.aes.key_tweak.enc) != 0) {
265             error_setg(errp, "Failed to set encryption key");
266             goto error;
267         }
269         if (AES_set_decrypt_key(key + (nkey / 2), nkey * 4,
270                                 &ctxt->state.aes.key_tweak.dec) != 0) {
271             error_setg(errp, "Failed to set decryption key");
272             goto error;
273         }
274     } else {
275         if (AES_set_encrypt_key(key, nkey * 8, &ctxt->state.aes.key.enc) != 0) {
276             error_setg(errp, "Failed to set encryption key");
277             goto error;
278         }
280         if (AES_set_decrypt_key(key, nkey * 8, &ctxt->state.aes.key.dec) != 0) {
281             error_setg(errp, "Failed to set decryption key");
282             goto error;
283         }
284     }
286     ctxt->blocksize = AES_BLOCK_SIZE;
287     ctxt->free = qcrypto_cipher_free_aes;
288     ctxt->setiv = qcrypto_cipher_setiv_aes;
289     ctxt->encrypt = qcrypto_cipher_encrypt_aes;
290     ctxt->decrypt = qcrypto_cipher_decrypt_aes;
292     return ctxt;
294  error:
295     g_free(ctxt);
296     return NULL;
300 static void qcrypto_cipher_free_des_rfb(QCryptoCipher *cipher)
302     QCryptoCipherBuiltin *ctxt = cipher->opaque;
304     g_free(ctxt->state.desrfb.key);
305     g_free(ctxt);
306     cipher->opaque = NULL;
310 static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher,
311                                           const void *in,
312                                           void *out,
313                                           size_t len,
314                                           Error **errp)
316     QCryptoCipherBuiltin *ctxt = cipher->opaque;
317     size_t i;
319     if (len % 8) {
320         error_setg(errp, "Buffer size must be multiple of 8 not %zu",
321                    len);
322         return -1;
323     }
325     deskey(ctxt->state.desrfb.key, EN0);
327     for (i = 0; i < len; i += 8) {
328         des((void *)in + i, out + i);
329     }
331     return 0;
335 static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher,
336                                           const void *in,
337                                           void *out,
338                                           size_t len,
339                                           Error **errp)
341     QCryptoCipherBuiltin *ctxt = cipher->opaque;
342     size_t i;
344     if (len % 8) {
345         error_setg(errp, "Buffer size must be multiple of 8 not %zu",
346                    len);
347         return -1;
348     }
350     deskey(ctxt->state.desrfb.key, DE1);
352     for (i = 0; i < len; i += 8) {
353         des((void *)in + i, out + i);
354     }
356     return 0;
360 static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher,
361                                         const uint8_t *iv, size_t niv,
362                                         Error **errp)
364     error_setg(errp, "Setting IV is not supported");
365     return -1;
369 static QCryptoCipherBuiltin *
370 qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode,
371                             const uint8_t *key, size_t nkey,
372                             Error **errp)
374     QCryptoCipherBuiltin *ctxt;
376     if (mode != QCRYPTO_CIPHER_MODE_ECB) {
377         error_setg(errp, "Unsupported cipher mode %s",
378                    QCryptoCipherMode_str(mode));
379         return NULL;
380     }
382     ctxt = g_new0(QCryptoCipherBuiltin, 1);
384     ctxt->state.desrfb.key = g_new0(uint8_t, nkey);
385     memcpy(ctxt->state.desrfb.key, key, nkey);
386     ctxt->state.desrfb.nkey = nkey;
388     ctxt->blocksize = 8;
389     ctxt->free = qcrypto_cipher_free_des_rfb;
390     ctxt->setiv = qcrypto_cipher_setiv_des_rfb;
391     ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb;
392     ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb;
394     return ctxt;
398 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
399                              QCryptoCipherMode mode)
401     switch (alg) {
402     case QCRYPTO_CIPHER_ALG_DES_RFB:
403     case QCRYPTO_CIPHER_ALG_AES_128:
404     case QCRYPTO_CIPHER_ALG_AES_192:
405     case QCRYPTO_CIPHER_ALG_AES_256:
406         break;
407     default:
408         return false;
409     }
411     switch (mode) {
412     case QCRYPTO_CIPHER_MODE_ECB:
413     case QCRYPTO_CIPHER_MODE_CBC:
414     case QCRYPTO_CIPHER_MODE_XTS:
415         return true;
416     case QCRYPTO_CIPHER_MODE_CTR:
417         return false;
418     default:
419         return false;
420     }
424 static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
425                                                     QCryptoCipherMode mode,
426                                                     const uint8_t *key,
427                                                     size_t nkey,
428                                                     Error **errp)
430     QCryptoCipherBuiltin *ctxt;
432     switch (mode) {
433     case QCRYPTO_CIPHER_MODE_ECB:
434     case QCRYPTO_CIPHER_MODE_CBC:
435     case QCRYPTO_CIPHER_MODE_XTS:
436         break;
437     default:
438         error_setg(errp, "Unsupported cipher mode %s",
439                    QCryptoCipherMode_str(mode));
440         return NULL;
441     }
443     if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
444         return NULL;
445     }
447     switch (alg) {
448     case QCRYPTO_CIPHER_ALG_DES_RFB:
449         ctxt = qcrypto_cipher_init_des_rfb(mode, key, nkey, errp);
450         break;
451     case QCRYPTO_CIPHER_ALG_AES_128:
452     case QCRYPTO_CIPHER_ALG_AES_192:
453     case QCRYPTO_CIPHER_ALG_AES_256:
454         ctxt = qcrypto_cipher_init_aes(mode, key, nkey, errp);
455         break;
456     default:
457         error_setg(errp,
458                    "Unsupported cipher algorithm %s",
459                    QCryptoCipherAlgorithm_str(alg));
460         return NULL;
461     }
463     return ctxt;
466 static void
467 qcrypto_builtin_cipher_ctx_free(QCryptoCipher *cipher)
469     QCryptoCipherBuiltin *ctxt;
471     ctxt = cipher->opaque;
472     ctxt->free(cipher);
476 static int
477 qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher,
478                                const void *in,
479                                void *out,
480                                size_t len,
481                                Error **errp)
483     QCryptoCipherBuiltin *ctxt = cipher->opaque;
485     if (len & (ctxt->blocksize - 1)) {
486         error_setg(errp, "Length %zu must be a multiple of block size %zu",
487                    len, ctxt->blocksize);
488         return -1;
489     }
491     return ctxt->encrypt(cipher, in, out, len, errp);
495 static int
496 qcrypto_builtin_cipher_decrypt(QCryptoCipher *cipher,
497                                const void *in,
498                                void *out,
499                                size_t len,
500                                Error **errp)
502     QCryptoCipherBuiltin *ctxt = cipher->opaque;
504     if (len & (ctxt->blocksize - 1)) {
505         error_setg(errp, "Length %zu must be a multiple of block size %zu",
506                    len, ctxt->blocksize);
507         return -1;
508     }
510     return ctxt->decrypt(cipher, in, out, len, errp);
514 static int
515 qcrypto_builtin_cipher_setiv(QCryptoCipher *cipher,
516                              const uint8_t *iv, size_t niv,
517                              Error **errp)
519     QCryptoCipherBuiltin *ctxt = cipher->opaque;
521     return ctxt->setiv(cipher, iv, niv, errp);
525 static const struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
526     .cipher_encrypt = qcrypto_builtin_cipher_encrypt,
527     .cipher_decrypt = qcrypto_builtin_cipher_decrypt,
528     .cipher_setiv = qcrypto_builtin_cipher_setiv,
529     .cipher_free = qcrypto_builtin_cipher_ctx_free,