]>
Commit | Line | Data |
---|---|---|
ca38a4cc DB |
1 | /* |
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 | ||
42f7a448 | 21 | #include "qemu/osdep.h" |
da34e65c | 22 | #include "qapi/error.h" |
ca38a4cc DB |
23 | #include "crypto/cipher.h" |
24 | ||
62893b67 | 25 | |
d8c02bcc | 26 | static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = { |
ca38a4cc DB |
27 | [QCRYPTO_CIPHER_ALG_AES_128] = 16, |
28 | [QCRYPTO_CIPHER_ALG_AES_192] = 24, | |
29 | [QCRYPTO_CIPHER_ALG_AES_256] = 32, | |
30 | [QCRYPTO_CIPHER_ALG_DES_RFB] = 8, | |
084a85ee | 31 | [QCRYPTO_CIPHER_ALG_CAST5_128] = 16, |
94318522 DB |
32 | [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16, |
33 | [QCRYPTO_CIPHER_ALG_SERPENT_192] = 24, | |
34 | [QCRYPTO_CIPHER_ALG_SERPENT_256] = 32, | |
50f6753e DB |
35 | [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16, |
36 | [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 24, | |
37 | [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 32, | |
ca38a4cc DB |
38 | }; |
39 | ||
d8c02bcc | 40 | static size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = { |
dd2bf9eb DB |
41 | [QCRYPTO_CIPHER_ALG_AES_128] = 16, |
42 | [QCRYPTO_CIPHER_ALG_AES_192] = 16, | |
43 | [QCRYPTO_CIPHER_ALG_AES_256] = 16, | |
44 | [QCRYPTO_CIPHER_ALG_DES_RFB] = 8, | |
084a85ee | 45 | [QCRYPTO_CIPHER_ALG_CAST5_128] = 8, |
94318522 DB |
46 | [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16, |
47 | [QCRYPTO_CIPHER_ALG_SERPENT_192] = 16, | |
48 | [QCRYPTO_CIPHER_ALG_SERPENT_256] = 16, | |
50f6753e DB |
49 | [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16, |
50 | [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 16, | |
51 | [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 16, | |
dd2bf9eb DB |
52 | }; |
53 | ||
d8c02bcc | 54 | static bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = { |
dd2bf9eb DB |
55 | [QCRYPTO_CIPHER_MODE_ECB] = false, |
56 | [QCRYPTO_CIPHER_MODE_CBC] = true, | |
eaec903c | 57 | [QCRYPTO_CIPHER_MODE_XTS] = true, |
3c28292f | 58 | [QCRYPTO_CIPHER_MODE_CTR] = true, |
dd2bf9eb DB |
59 | }; |
60 | ||
61 | ||
62 | size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg) | |
63 | { | |
64 | if (alg >= G_N_ELEMENTS(alg_key_len)) { | |
65 | return 0; | |
66 | } | |
67 | return alg_block_len[alg]; | |
68 | } | |
69 | ||
70 | ||
71 | size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg) | |
72 | { | |
73 | if (alg >= G_N_ELEMENTS(alg_key_len)) { | |
74 | return 0; | |
75 | } | |
76 | return alg_key_len[alg]; | |
77 | } | |
78 | ||
79 | ||
80 | size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgorithm alg, | |
81 | QCryptoCipherMode mode) | |
82 | { | |
83 | if (alg >= G_N_ELEMENTS(alg_block_len)) { | |
84 | return 0; | |
85 | } | |
86 | if (mode >= G_N_ELEMENTS(mode_need_iv)) { | |
87 | return 0; | |
88 | } | |
89 | ||
90 | if (mode_need_iv[mode]) { | |
91 | return alg_block_len[alg]; | |
92 | } | |
93 | return 0; | |
94 | } | |
95 | ||
96 | ||
ca38a4cc DB |
97 | static bool |
98 | qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg, | |
eaec903c | 99 | QCryptoCipherMode mode, |
ca38a4cc DB |
100 | size_t nkey, |
101 | Error **errp) | |
102 | { | |
d8c02bcc | 103 | if ((unsigned)alg >= QCRYPTO_CIPHER_ALG__MAX) { |
ca38a4cc DB |
104 | error_setg(errp, "Cipher algorithm %d out of range", |
105 | alg); | |
106 | return false; | |
107 | } | |
108 | ||
eaec903c DB |
109 | if (mode == QCRYPTO_CIPHER_MODE_XTS) { |
110 | if (alg == QCRYPTO_CIPHER_ALG_DES_RFB) { | |
111 | error_setg(errp, "XTS mode not compatible with DES-RFB"); | |
112 | return false; | |
113 | } | |
114 | if (nkey % 2) { | |
115 | error_setg(errp, "XTS cipher key length should be a multiple of 2"); | |
116 | return false; | |
117 | } | |
118 | ||
119 | if (alg_key_len[alg] != (nkey / 2)) { | |
120 | error_setg(errp, "Cipher key length %zu should be %zu", | |
121 | nkey, alg_key_len[alg] * 2); | |
122 | return false; | |
123 | } | |
124 | } else { | |
125 | if (alg_key_len[alg] != nkey) { | |
126 | error_setg(errp, "Cipher key length %zu should be %zu", | |
127 | nkey, alg_key_len[alg]); | |
128 | return false; | |
129 | } | |
ca38a4cc DB |
130 | } |
131 | return true; | |
132 | } | |
133 | ||
91bfcdb0 | 134 | #if defined(CONFIG_GCRYPT) || defined(CONFIG_NETTLE) |
62893b67 DB |
135 | static uint8_t * |
136 | qcrypto_cipher_munge_des_rfb_key(const uint8_t *key, | |
137 | size_t nkey) | |
138 | { | |
139 | uint8_t *ret = g_new0(uint8_t, nkey); | |
140 | size_t i; | |
141 | for (i = 0; i < nkey; i++) { | |
142 | uint8_t r = key[i]; | |
143 | r = (r & 0xf0) >> 4 | (r & 0x0f) << 4; | |
144 | r = (r & 0xcc) >> 2 | (r & 0x33) << 2; | |
145 | r = (r & 0xaa) >> 1 | (r & 0x55) << 1; | |
146 | ret[i] = r; | |
147 | } | |
148 | return ret; | |
149 | } | |
91bfcdb0 | 150 | #endif /* CONFIG_GCRYPT || CONFIG_NETTLE */ |
62893b67 | 151 | |
91bfcdb0 | 152 | #ifdef CONFIG_GCRYPT |
62893b67 | 153 | #include "crypto/cipher-gcrypt.c" |
91bfcdb0 | 154 | #elif defined CONFIG_NETTLE |
ed754746 | 155 | #include "crypto/cipher-nettle.c" |
62893b67 | 156 | #else |
ca38a4cc | 157 | #include "crypto/cipher-builtin.c" |
62893b67 | 158 | #endif |