blob: 0f6fe98a1fc2f6c831528db36bc4808d731aecf4 [file] [log] [blame]
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +01001/*
2 * QEMU Crypto cipher 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
Peter Maydell42f7a442016-01-26 18:16:55 +000021#include "qemu/osdep.h"
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010022#include "crypto/cipher.h"
23
Daniel P. Berrange62893b62015-07-01 18:10:33 +010024
Daniel P. Berranged8c02bc2015-11-19 17:09:01 +000025static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010026 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
27 [QCRYPTO_CIPHER_ALG_AES_192] = 24,
28 [QCRYPTO_CIPHER_ALG_AES_256] = 32,
29 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
Daniel P. Berrange084a85e2016-02-10 17:07:42 +000030 [QCRYPTO_CIPHER_ALG_CAST5_128] = 16,
Daniel P. Berrange94318522016-02-10 17:07:42 +000031 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
32 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 24,
33 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 32,
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010034};
35
Daniel P. Berranged8c02bc2015-11-19 17:09:01 +000036static size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
Daniel P. Berrangedd2bf9e2015-10-23 16:13:50 +010037 [QCRYPTO_CIPHER_ALG_AES_128] = 16,
38 [QCRYPTO_CIPHER_ALG_AES_192] = 16,
39 [QCRYPTO_CIPHER_ALG_AES_256] = 16,
40 [QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
Daniel P. Berrange084a85e2016-02-10 17:07:42 +000041 [QCRYPTO_CIPHER_ALG_CAST5_128] = 8,
Daniel P. Berrange94318522016-02-10 17:07:42 +000042 [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
43 [QCRYPTO_CIPHER_ALG_SERPENT_192] = 16,
44 [QCRYPTO_CIPHER_ALG_SERPENT_256] = 16,
Daniel P. Berrangedd2bf9e2015-10-23 16:13:50 +010045};
46
Daniel P. Berranged8c02bc2015-11-19 17:09:01 +000047static bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
Daniel P. Berrangedd2bf9e2015-10-23 16:13:50 +010048 [QCRYPTO_CIPHER_MODE_ECB] = false,
49 [QCRYPTO_CIPHER_MODE_CBC] = true,
50};
51
52
53size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg)
54{
55 if (alg >= G_N_ELEMENTS(alg_key_len)) {
56 return 0;
57 }
58 return alg_block_len[alg];
59}
60
61
62size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg)
63{
64 if (alg >= G_N_ELEMENTS(alg_key_len)) {
65 return 0;
66 }
67 return alg_key_len[alg];
68}
69
70
71size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgorithm alg,
72 QCryptoCipherMode mode)
73{
74 if (alg >= G_N_ELEMENTS(alg_block_len)) {
75 return 0;
76 }
77 if (mode >= G_N_ELEMENTS(mode_need_iv)) {
78 return 0;
79 }
80
81 if (mode_need_iv[mode]) {
82 return alg_block_len[alg];
83 }
84 return 0;
85}
86
87
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010088static bool
89qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
90 size_t nkey,
91 Error **errp)
92{
Daniel P. Berranged8c02bc2015-11-19 17:09:01 +000093 if ((unsigned)alg >= QCRYPTO_CIPHER_ALG__MAX) {
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +010094 error_setg(errp, "Cipher algorithm %d out of range",
95 alg);
96 return false;
97 }
98
99 if (alg_key_len[alg] != nkey) {
100 error_setg(errp, "Cipher key length %zu should be %zu",
Daniel P. Berrange50de6262015-11-20 16:15:42 +0000101 nkey, alg_key_len[alg]);
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +0100102 return false;
103 }
104 return true;
105}
106
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +0100107#if defined(CONFIG_GCRYPT) || defined(CONFIG_NETTLE)
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100108static uint8_t *
109qcrypto_cipher_munge_des_rfb_key(const uint8_t *key,
110 size_t nkey)
111{
112 uint8_t *ret = g_new0(uint8_t, nkey);
113 size_t i;
114 for (i = 0; i < nkey; i++) {
115 uint8_t r = key[i];
116 r = (r & 0xf0) >> 4 | (r & 0x0f) << 4;
117 r = (r & 0xcc) >> 2 | (r & 0x33) << 2;
118 r = (r & 0xaa) >> 1 | (r & 0x55) << 1;
119 ret[i] = r;
120 }
121 return ret;
122}
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +0100123#endif /* CONFIG_GCRYPT || CONFIG_NETTLE */
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100124
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +0100125#ifdef CONFIG_GCRYPT
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100126#include "crypto/cipher-gcrypt.c"
Daniel P. Berrange91bfcdb2015-10-16 16:36:53 +0100127#elif defined CONFIG_NETTLE
Daniel P. Berrangeed754742015-07-01 18:10:34 +0100128#include "crypto/cipher-nettle.c"
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100129#else
Daniel P. Berrangeca38a4c2015-07-01 18:10:32 +0100130#include "crypto/cipher-builtin.c"
Daniel P. Berrange62893b62015-07-01 18:10:33 +0100131#endif