crypto/builtin: Split and simplify AES_encrypt_cbc
[qemu/ar7.git] / crypto / cipher-builtin.c.inc
blobb1fe3b08c30447425c23502ed00649b403792dc1
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 static const struct QCryptoCipherDriver qcrypto_cipher_lib_driver;
27 typedef struct QCryptoCipherBuiltinAESContext QCryptoCipherBuiltinAESContext;
28 struct QCryptoCipherBuiltinAESContext {
29     AES_KEY enc;
30     AES_KEY dec;
32 typedef struct QCryptoCipherBuiltinAES QCryptoCipherBuiltinAES;
33 struct QCryptoCipherBuiltinAES {
34     QCryptoCipherBuiltinAESContext key;
35     QCryptoCipherBuiltinAESContext key_tweak;
36     uint8_t iv[AES_BLOCK_SIZE];
38 typedef struct QCryptoCipherBuiltinDESRFB QCryptoCipherBuiltinDESRFB;
39 struct QCryptoCipherBuiltinDESRFB {
40     uint8_t *key;
41     size_t nkey;
44 typedef struct QCryptoCipherBuiltin QCryptoCipherBuiltin;
45 struct QCryptoCipherBuiltin {
46     QCryptoCipher base;
48     union {
49         QCryptoCipherBuiltinAES aes;
50         QCryptoCipherBuiltinDESRFB desrfb;
51     } state;
52     size_t blocksize;
53     void (*free)(QCryptoCipher *cipher);
54     int (*setiv)(QCryptoCipher *cipher,
55                  const uint8_t *iv, size_t niv,
56                  Error **errp);
57     int (*encrypt)(QCryptoCipher *cipher,
58                    const void *in,
59                    void *out,
60                    size_t len,
61                    Error **errp);
62     int (*decrypt)(QCryptoCipher *cipher,
63                    const void *in,
64                    void *out,
65                    size_t len,
66                    Error **errp);
70 static void qcrypto_cipher_free_aes(QCryptoCipher *cipher)
72     g_free(cipher);
75 static void do_aes_encrypt_ecb(const void *vctx,
76                                size_t len,
77                                uint8_t *out,
78                                const uint8_t *in)
80     const QCryptoCipherBuiltinAESContext *ctx = vctx;
82     /* We have already verified that len % AES_BLOCK_SIZE == 0. */
83     while (len) {
84         AES_encrypt(in, out, &ctx->enc);
85         in += AES_BLOCK_SIZE;
86         out += AES_BLOCK_SIZE;
87         len -= AES_BLOCK_SIZE;
88     }
91 static void do_aes_decrypt_ecb(const void *vctx,
92                                size_t len,
93                                uint8_t *out,
94                                const uint8_t *in)
96     const QCryptoCipherBuiltinAESContext *ctx = vctx;
98     /* We have already verified that len % AES_BLOCK_SIZE == 0. */
99     while (len) {
100         AES_decrypt(in, out, &ctx->dec);
101         in += AES_BLOCK_SIZE;
102         out += AES_BLOCK_SIZE;
103         len -= AES_BLOCK_SIZE;
104     }
107 static void do_aes_encrypt_cbc(const AES_KEY *key,
108                                size_t len,
109                                uint8_t *out,
110                                const uint8_t *in,
111                                uint8_t *ivec)
113     uint8_t tmp[AES_BLOCK_SIZE];
114     size_t n;
116     /* We have already verified that len % AES_BLOCK_SIZE == 0. */
117     while (len) {
118         for (n = 0; n < AES_BLOCK_SIZE; ++n) {
119             tmp[n] = in[n] ^ ivec[n];
120         }
121         AES_encrypt(tmp, out, key);
122         memcpy(ivec, out, AES_BLOCK_SIZE);
123         len -= AES_BLOCK_SIZE;
124         in += AES_BLOCK_SIZE;
125         out += AES_BLOCK_SIZE;
126     }
129 static void do_aes_decrypt_cbc(const AES_KEY *key,
130                                size_t len,
131                                uint8_t *out,
132                                const uint8_t *in,
133                                uint8_t *ivec)
135     uint8_t tmp[AES_BLOCK_SIZE];
136     size_t n;
138     /* We have already verified that len % AES_BLOCK_SIZE == 0. */
139     while (len) {
140         memcpy(tmp, in, AES_BLOCK_SIZE);
141         AES_decrypt(in, out, key);
142         for (n = 0; n < AES_BLOCK_SIZE; ++n) {
143             out[n] ^= ivec[n];
144         }
145         memcpy(ivec, tmp, AES_BLOCK_SIZE);
146         len -= AES_BLOCK_SIZE;
147         in += AES_BLOCK_SIZE;
148         out += AES_BLOCK_SIZE;
149     }
152 static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher,
153                                       const void *in,
154                                       void *out,
155                                       size_t len,
156                                       Error **errp)
158     QCryptoCipherBuiltin *ctxt
159         = container_of(cipher, QCryptoCipherBuiltin, base);
161     switch (cipher->mode) {
162     case QCRYPTO_CIPHER_MODE_ECB:
163         do_aes_encrypt_ecb(&ctxt->state.aes.key, len, out, in);
164         break;
165     case QCRYPTO_CIPHER_MODE_CBC:
166         do_aes_encrypt_cbc(&ctxt->state.aes.key.enc, len, out, in,
167                            ctxt->state.aes.iv);
168         break;
169     case QCRYPTO_CIPHER_MODE_XTS:
170         xts_encrypt(&ctxt->state.aes.key,
171                     &ctxt->state.aes.key_tweak,
172                     do_aes_encrypt_ecb,
173                     do_aes_decrypt_ecb,
174                     ctxt->state.aes.iv,
175                     len, out, in);
176         break;
177     default:
178         g_assert_not_reached();
179     }
181     return 0;
185 static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher,
186                                       const void *in,
187                                       void *out,
188                                       size_t len,
189                                       Error **errp)
191     QCryptoCipherBuiltin *ctxt
192         = container_of(cipher, QCryptoCipherBuiltin, base);
194     switch (cipher->mode) {
195     case QCRYPTO_CIPHER_MODE_ECB:
196         do_aes_decrypt_ecb(&ctxt->state.aes.key, len, out, in);
197         break;
198     case QCRYPTO_CIPHER_MODE_CBC:
199         do_aes_decrypt_cbc(&ctxt->state.aes.key.dec, len, out, in,
200                            ctxt->state.aes.iv);
201         break;
202     case QCRYPTO_CIPHER_MODE_XTS:
203         xts_decrypt(&ctxt->state.aes.key,
204                     &ctxt->state.aes.key_tweak,
205                     do_aes_encrypt_ecb,
206                     do_aes_decrypt_ecb,
207                     ctxt->state.aes.iv,
208                     len, out, in);
209         break;
210     default:
211         g_assert_not_reached();
212     }
214     return 0;
217 static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
218                                      const uint8_t *iv, size_t niv,
219                                      Error **errp)
221     QCryptoCipherBuiltin *ctxt
222         = container_of(cipher, QCryptoCipherBuiltin, base);
224     if (niv != AES_BLOCK_SIZE) {
225         error_setg(errp, "IV must be %d bytes not %zu",
226                    AES_BLOCK_SIZE, niv);
227         return -1;
228     }
230     memcpy(ctxt->state.aes.iv, iv, AES_BLOCK_SIZE);
232     return 0;
238 static QCryptoCipher *
239 qcrypto_cipher_init_aes(QCryptoCipherMode mode,
240                         const uint8_t *key, size_t nkey,
241                         Error **errp)
243     QCryptoCipherBuiltin *ctxt;
245     if (mode != QCRYPTO_CIPHER_MODE_CBC &&
246         mode != QCRYPTO_CIPHER_MODE_ECB &&
247         mode != QCRYPTO_CIPHER_MODE_XTS) {
248         error_setg(errp, "Unsupported cipher mode %s",
249                    QCryptoCipherMode_str(mode));
250         return NULL;
251     }
253     ctxt = g_new0(QCryptoCipherBuiltin, 1);
255     if (mode == QCRYPTO_CIPHER_MODE_XTS) {
256         if (AES_set_encrypt_key(key, nkey * 4, &ctxt->state.aes.key.enc) != 0) {
257             error_setg(errp, "Failed to set encryption key");
258             goto error;
259         }
261         if (AES_set_decrypt_key(key, nkey * 4, &ctxt->state.aes.key.dec) != 0) {
262             error_setg(errp, "Failed to set decryption key");
263             goto error;
264         }
266         if (AES_set_encrypt_key(key + (nkey / 2), nkey * 4,
267                                 &ctxt->state.aes.key_tweak.enc) != 0) {
268             error_setg(errp, "Failed to set encryption key");
269             goto error;
270         }
272         if (AES_set_decrypt_key(key + (nkey / 2), nkey * 4,
273                                 &ctxt->state.aes.key_tweak.dec) != 0) {
274             error_setg(errp, "Failed to set decryption key");
275             goto error;
276         }
277     } else {
278         if (AES_set_encrypt_key(key, nkey * 8, &ctxt->state.aes.key.enc) != 0) {
279             error_setg(errp, "Failed to set encryption key");
280             goto error;
281         }
283         if (AES_set_decrypt_key(key, nkey * 8, &ctxt->state.aes.key.dec) != 0) {
284             error_setg(errp, "Failed to set decryption key");
285             goto error;
286         }
287     }
289     ctxt->blocksize = AES_BLOCK_SIZE;
290     ctxt->free = qcrypto_cipher_free_aes;
291     ctxt->setiv = qcrypto_cipher_setiv_aes;
292     ctxt->encrypt = qcrypto_cipher_encrypt_aes;
293     ctxt->decrypt = qcrypto_cipher_decrypt_aes;
295     ctxt->base.driver = &qcrypto_cipher_lib_driver;
296     return &ctxt->base;
298  error:
299     g_free(ctxt);
300     return NULL;
304 static void qcrypto_cipher_free_des_rfb(QCryptoCipher *cipher)
306     QCryptoCipherBuiltin *ctxt
307         = container_of(cipher, QCryptoCipherBuiltin, base);
309     g_free(ctxt->state.desrfb.key);
310     g_free(ctxt);
314 static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher,
315                                           const void *in,
316                                           void *out,
317                                           size_t len,
318                                           Error **errp)
320     QCryptoCipherBuiltin *ctxt
321         = container_of(cipher, QCryptoCipherBuiltin, base);
322     size_t i;
324     if (len % 8) {
325         error_setg(errp, "Buffer size must be multiple of 8 not %zu",
326                    len);
327         return -1;
328     }
330     deskey(ctxt->state.desrfb.key, EN0);
332     for (i = 0; i < len; i += 8) {
333         des((void *)in + i, out + i);
334     }
336     return 0;
340 static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher,
341                                           const void *in,
342                                           void *out,
343                                           size_t len,
344                                           Error **errp)
346     QCryptoCipherBuiltin *ctxt
347         = container_of(cipher, QCryptoCipherBuiltin, base);
348     size_t i;
350     if (len % 8) {
351         error_setg(errp, "Buffer size must be multiple of 8 not %zu",
352                    len);
353         return -1;
354     }
356     deskey(ctxt->state.desrfb.key, DE1);
358     for (i = 0; i < len; i += 8) {
359         des((void *)in + i, out + i);
360     }
362     return 0;
366 static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher,
367                                         const uint8_t *iv, size_t niv,
368                                         Error **errp)
370     error_setg(errp, "Setting IV is not supported");
371     return -1;
375 static QCryptoCipher *
376 qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode,
377                             const uint8_t *key, size_t nkey,
378                             Error **errp)
380     QCryptoCipherBuiltin *ctxt;
382     if (mode != QCRYPTO_CIPHER_MODE_ECB) {
383         error_setg(errp, "Unsupported cipher mode %s",
384                    QCryptoCipherMode_str(mode));
385         return NULL;
386     }
388     ctxt = g_new0(QCryptoCipherBuiltin, 1);
390     ctxt->state.desrfb.key = g_new0(uint8_t, nkey);
391     memcpy(ctxt->state.desrfb.key, key, nkey);
392     ctxt->state.desrfb.nkey = nkey;
394     ctxt->blocksize = 8;
395     ctxt->free = qcrypto_cipher_free_des_rfb;
396     ctxt->setiv = qcrypto_cipher_setiv_des_rfb;
397     ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb;
398     ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb;
400     ctxt->base.driver = &qcrypto_cipher_lib_driver;
401     return &ctxt->base;
405 bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
406                              QCryptoCipherMode mode)
408     switch (alg) {
409     case QCRYPTO_CIPHER_ALG_DES_RFB:
410     case QCRYPTO_CIPHER_ALG_AES_128:
411     case QCRYPTO_CIPHER_ALG_AES_192:
412     case QCRYPTO_CIPHER_ALG_AES_256:
413         break;
414     default:
415         return false;
416     }
418     switch (mode) {
419     case QCRYPTO_CIPHER_MODE_ECB:
420     case QCRYPTO_CIPHER_MODE_CBC:
421     case QCRYPTO_CIPHER_MODE_XTS:
422         return true;
423     case QCRYPTO_CIPHER_MODE_CTR:
424         return false;
425     default:
426         return false;
427     }
431 static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
432                                              QCryptoCipherMode mode,
433                                              const uint8_t *key,
434                                              size_t nkey,
435                                              Error **errp)
437     switch (mode) {
438     case QCRYPTO_CIPHER_MODE_ECB:
439     case QCRYPTO_CIPHER_MODE_CBC:
440     case QCRYPTO_CIPHER_MODE_XTS:
441         break;
442     default:
443         error_setg(errp, "Unsupported cipher mode %s",
444                    QCryptoCipherMode_str(mode));
445         return NULL;
446     }
448     if (!qcrypto_cipher_validate_key_length(alg, mode, nkey, errp)) {
449         return NULL;
450     }
452     switch (alg) {
453     case QCRYPTO_CIPHER_ALG_DES_RFB:
454         return qcrypto_cipher_init_des_rfb(mode, key, nkey, errp);
455     case QCRYPTO_CIPHER_ALG_AES_128:
456     case QCRYPTO_CIPHER_ALG_AES_192:
457     case QCRYPTO_CIPHER_ALG_AES_256:
458         return qcrypto_cipher_init_aes(mode, key, nkey, errp);
459     default:
460         error_setg(errp,
461                    "Unsupported cipher algorithm %s",
462                    QCryptoCipherAlgorithm_str(alg));
463         return NULL;
464     }
467 static void
468 qcrypto_builtin_cipher_ctx_free(QCryptoCipher *cipher)
470     QCryptoCipherBuiltin *ctxt
471         = container_of(cipher, QCryptoCipherBuiltin, base);
473     ctxt->free(cipher);
477 static int
478 qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher,
479                                const void *in,
480                                void *out,
481                                size_t len,
482                                Error **errp)
484     QCryptoCipherBuiltin *ctxt
485         = container_of(cipher, QCryptoCipherBuiltin, base);
487     if (len & (ctxt->blocksize - 1)) {
488         error_setg(errp, "Length %zu must be a multiple of block size %zu",
489                    len, ctxt->blocksize);
490         return -1;
491     }
493     return ctxt->encrypt(cipher, in, out, len, errp);
497 static int
498 qcrypto_builtin_cipher_decrypt(QCryptoCipher *cipher,
499                                const void *in,
500                                void *out,
501                                size_t len,
502                                Error **errp)
504     QCryptoCipherBuiltin *ctxt
505         = container_of(cipher, QCryptoCipherBuiltin, base);
507     if (len & (ctxt->blocksize - 1)) {
508         error_setg(errp, "Length %zu must be a multiple of block size %zu",
509                    len, ctxt->blocksize);
510         return -1;
511     }
513     return ctxt->decrypt(cipher, in, out, len, errp);
517 static int
518 qcrypto_builtin_cipher_setiv(QCryptoCipher *cipher,
519                              const uint8_t *iv, size_t niv,
520                              Error **errp)
522     QCryptoCipherBuiltin *ctxt
523         = container_of(cipher, QCryptoCipherBuiltin, base);
525     return ctxt->setiv(cipher, iv, niv, errp);
529 static const struct QCryptoCipherDriver qcrypto_cipher_lib_driver = {
530     .cipher_encrypt = qcrypto_builtin_cipher_encrypt,
531     .cipher_decrypt = qcrypto_builtin_cipher_decrypt,
532     .cipher_setiv = qcrypto_builtin_cipher_setiv,
533     .cipher_free = qcrypto_builtin_cipher_ctx_free,