Daniel P. Berrange | ca38a4c | 2015-07-01 18:10:32 +0100 | [diff] [blame] | 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 |
Thomas Huth | b7cbb87 | 2019-02-13 16:54:59 +0100 | [diff] [blame] | 9 | * version 2.1 of the License, or (at your option) any later version. |
Daniel P. Berrange | ca38a4c | 2015-07-01 18:10:32 +0100 | [diff] [blame] | 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 Maydell | 42f7a44 | 2016-01-26 18:16:55 +0000 | [diff] [blame] | 21 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 22 | #include "qapi/error.h" |
Daniel P. Berrange | ca38a4c | 2015-07-01 18:10:32 +0100 | [diff] [blame] | 23 | #include "crypto/cipher.h" |
Longpeng(Mike) | 75c8007 | 2017-07-14 14:03:58 -0400 | [diff] [blame] | 24 | #include "cipherpriv.h" |
Daniel P. Berrange | ca38a4c | 2015-07-01 18:10:32 +0100 | [diff] [blame] | 25 | |
Daniel P. Berrange | 62893b6 | 2015-07-01 18:10:33 +0100 | [diff] [blame] | 26 | |
Daniel P. Berrange | d8c02bc | 2015-11-19 17:09:01 +0000 | [diff] [blame] | 27 | static size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = { |
Daniel P. Berrange | ca38a4c | 2015-07-01 18:10:32 +0100 | [diff] [blame] | 28 | [QCRYPTO_CIPHER_ALG_AES_128] = 16, |
| 29 | [QCRYPTO_CIPHER_ALG_AES_192] = 24, |
| 30 | [QCRYPTO_CIPHER_ALG_AES_256] = 32, |
| 31 | [QCRYPTO_CIPHER_ALG_DES_RFB] = 8, |
Longpeng(Mike) | ffb7bf4 | 2016-12-08 10:33:28 +0800 | [diff] [blame] | 32 | [QCRYPTO_CIPHER_ALG_3DES] = 24, |
Daniel P. Berrange | 084a85e | 2016-02-10 17:07:42 +0000 | [diff] [blame] | 33 | [QCRYPTO_CIPHER_ALG_CAST5_128] = 16, |
Daniel P. Berrange | 9431852 | 2016-02-10 17:07:42 +0000 | [diff] [blame] | 34 | [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16, |
| 35 | [QCRYPTO_CIPHER_ALG_SERPENT_192] = 24, |
| 36 | [QCRYPTO_CIPHER_ALG_SERPENT_256] = 32, |
Daniel P. Berrange | 50f6753 | 2016-02-10 17:07:42 +0000 | [diff] [blame] | 37 | [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16, |
| 38 | [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 24, |
| 39 | [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 32, |
Daniel P. Berrange | ca38a4c | 2015-07-01 18:10:32 +0100 | [diff] [blame] | 40 | }; |
| 41 | |
Daniel P. Berrange | d8c02bc | 2015-11-19 17:09:01 +0000 | [diff] [blame] | 42 | static size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = { |
Daniel P. Berrange | dd2bf9e | 2015-10-23 16:13:50 +0100 | [diff] [blame] | 43 | [QCRYPTO_CIPHER_ALG_AES_128] = 16, |
| 44 | [QCRYPTO_CIPHER_ALG_AES_192] = 16, |
| 45 | [QCRYPTO_CIPHER_ALG_AES_256] = 16, |
| 46 | [QCRYPTO_CIPHER_ALG_DES_RFB] = 8, |
Longpeng(Mike) | ffb7bf4 | 2016-12-08 10:33:28 +0800 | [diff] [blame] | 47 | [QCRYPTO_CIPHER_ALG_3DES] = 8, |
Daniel P. Berrange | 084a85e | 2016-02-10 17:07:42 +0000 | [diff] [blame] | 48 | [QCRYPTO_CIPHER_ALG_CAST5_128] = 8, |
Daniel P. Berrange | 9431852 | 2016-02-10 17:07:42 +0000 | [diff] [blame] | 49 | [QCRYPTO_CIPHER_ALG_SERPENT_128] = 16, |
| 50 | [QCRYPTO_CIPHER_ALG_SERPENT_192] = 16, |
| 51 | [QCRYPTO_CIPHER_ALG_SERPENT_256] = 16, |
Daniel P. Berrange | 50f6753 | 2016-02-10 17:07:42 +0000 | [diff] [blame] | 52 | [QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16, |
| 53 | [QCRYPTO_CIPHER_ALG_TWOFISH_192] = 16, |
| 54 | [QCRYPTO_CIPHER_ALG_TWOFISH_256] = 16, |
Daniel P. Berrange | dd2bf9e | 2015-10-23 16:13:50 +0100 | [diff] [blame] | 55 | }; |
| 56 | |
Daniel P. Berrange | d8c02bc | 2015-11-19 17:09:01 +0000 | [diff] [blame] | 57 | static bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = { |
Daniel P. Berrange | dd2bf9e | 2015-10-23 16:13:50 +0100 | [diff] [blame] | 58 | [QCRYPTO_CIPHER_MODE_ECB] = false, |
| 59 | [QCRYPTO_CIPHER_MODE_CBC] = true, |
Daniel P. Berrange | eaec903 | 2016-02-11 14:05:21 +0000 | [diff] [blame] | 60 | [QCRYPTO_CIPHER_MODE_XTS] = true, |
Gonglei | 3c28292 | 2016-09-26 17:23:22 +0800 | [diff] [blame] | 61 | [QCRYPTO_CIPHER_MODE_CTR] = true, |
Daniel P. Berrange | dd2bf9e | 2015-10-23 16:13:50 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | |
| 65 | size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg) |
| 66 | { |
Prasad J Pandit | 32c813e | 2017-02-20 16:53:07 +0530 | [diff] [blame] | 67 | assert(alg < G_N_ELEMENTS(alg_key_len)); |
Daniel P. Berrange | dd2bf9e | 2015-10-23 16:13:50 +0100 | [diff] [blame] | 68 | return alg_block_len[alg]; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg) |
| 73 | { |
Prasad J Pandit | 32c813e | 2017-02-20 16:53:07 +0530 | [diff] [blame] | 74 | assert(alg < G_N_ELEMENTS(alg_key_len)); |
Daniel P. Berrange | dd2bf9e | 2015-10-23 16:13:50 +0100 | [diff] [blame] | 75 | return alg_key_len[alg]; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgorithm alg, |
| 80 | QCryptoCipherMode mode) |
| 81 | { |
| 82 | if (alg >= G_N_ELEMENTS(alg_block_len)) { |
| 83 | return 0; |
| 84 | } |
| 85 | if (mode >= G_N_ELEMENTS(mode_need_iv)) { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | if (mode_need_iv[mode]) { |
| 90 | return alg_block_len[alg]; |
| 91 | } |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | |
Daniel P. Berrange | ca38a4c | 2015-07-01 18:10:32 +0100 | [diff] [blame] | 96 | static bool |
| 97 | qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg, |
Daniel P. Berrange | eaec903 | 2016-02-11 14:05:21 +0000 | [diff] [blame] | 98 | QCryptoCipherMode mode, |
Daniel P. Berrange | ca38a4c | 2015-07-01 18:10:32 +0100 | [diff] [blame] | 99 | size_t nkey, |
| 100 | Error **errp) |
| 101 | { |
Daniel P. Berrange | d8c02bc | 2015-11-19 17:09:01 +0000 | [diff] [blame] | 102 | if ((unsigned)alg >= QCRYPTO_CIPHER_ALG__MAX) { |
Daniel P. Berrange | ca38a4c | 2015-07-01 18:10:32 +0100 | [diff] [blame] | 103 | error_setg(errp, "Cipher algorithm %d out of range", |
| 104 | alg); |
| 105 | return false; |
| 106 | } |
| 107 | |
Daniel P. Berrange | eaec903 | 2016-02-11 14:05:21 +0000 | [diff] [blame] | 108 | if (mode == QCRYPTO_CIPHER_MODE_XTS) { |
Longpeng(Mike) | ffb7bf4 | 2016-12-08 10:33:28 +0800 | [diff] [blame] | 109 | if (alg == QCRYPTO_CIPHER_ALG_DES_RFB |
| 110 | || alg == QCRYPTO_CIPHER_ALG_3DES) { |
| 111 | error_setg(errp, "XTS mode not compatible with DES-RFB/3DES"); |
Daniel P. Berrange | eaec903 | 2016-02-11 14:05:21 +0000 | [diff] [blame] | 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 | } |
Daniel P. Berrange | ca38a4c | 2015-07-01 18:10:32 +0100 | [diff] [blame] | 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
Daniel P. Berrange | 91bfcdb | 2015-10-16 16:36:53 +0100 | [diff] [blame] | 134 | #if defined(CONFIG_GCRYPT) || defined(CONFIG_NETTLE) |
Daniel P. Berrange | 62893b6 | 2015-07-01 18:10:33 +0100 | [diff] [blame] | 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 | } |
Daniel P. Berrange | 91bfcdb | 2015-10-16 16:36:53 +0100 | [diff] [blame] | 150 | #endif /* CONFIG_GCRYPT || CONFIG_NETTLE */ |
Daniel P. Berrange | 62893b6 | 2015-07-01 18:10:33 +0100 | [diff] [blame] | 151 | |
Daniel P. Berrange | 91bfcdb | 2015-10-16 16:36:53 +0100 | [diff] [blame] | 152 | #ifdef CONFIG_GCRYPT |
Michael S. Tsirkin | 986bc8d | 2018-05-03 22:50:23 +0300 | [diff] [blame] | 153 | #include "cipher-gcrypt.c" |
Daniel P. Berrange | 91bfcdb | 2015-10-16 16:36:53 +0100 | [diff] [blame] | 154 | #elif defined CONFIG_NETTLE |
Michael S. Tsirkin | 986bc8d | 2018-05-03 22:50:23 +0300 | [diff] [blame] | 155 | #include "cipher-nettle.c" |
Daniel P. Berrange | 62893b6 | 2015-07-01 18:10:33 +0100 | [diff] [blame] | 156 | #else |
Michael S. Tsirkin | 986bc8d | 2018-05-03 22:50:23 +0300 | [diff] [blame] | 157 | #include "cipher-builtin.c" |
Daniel P. Berrange | 62893b6 | 2015-07-01 18:10:33 +0100 | [diff] [blame] | 158 | #endif |
Longpeng(Mike) | 75c8007 | 2017-07-14 14:03:58 -0400 | [diff] [blame] | 159 | |
| 160 | QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg, |
| 161 | QCryptoCipherMode mode, |
| 162 | const uint8_t *key, size_t nkey, |
| 163 | Error **errp) |
| 164 | { |
| 165 | QCryptoCipher *cipher; |
Longpeng(Mike) | 25c60df | 2017-07-14 14:04:06 -0400 | [diff] [blame] | 166 | void *ctx = NULL; |
Longpeng(Mike) | 25c60df | 2017-07-14 14:04:06 -0400 | [diff] [blame] | 167 | QCryptoCipherDriver *drv = NULL; |
Longpeng(Mike) | 75c8007 | 2017-07-14 14:03:58 -0400 | [diff] [blame] | 168 | |
Longpeng(Mike) | 25c60df | 2017-07-14 14:04:06 -0400 | [diff] [blame] | 169 | #ifdef CONFIG_AF_ALG |
Longpeng | f171063 | 2017-11-07 19:32:06 +0800 | [diff] [blame] | 170 | ctx = qcrypto_afalg_cipher_ctx_new(alg, mode, key, nkey, NULL); |
Longpeng(Mike) | 25c60df | 2017-07-14 14:04:06 -0400 | [diff] [blame] | 171 | if (ctx) { |
| 172 | drv = &qcrypto_cipher_afalg_driver; |
| 173 | } |
| 174 | #endif |
| 175 | |
Longpeng(Mike) | 75c8007 | 2017-07-14 14:03:58 -0400 | [diff] [blame] | 176 | if (!ctx) { |
Longpeng(Mike) | 25c60df | 2017-07-14 14:04:06 -0400 | [diff] [blame] | 177 | ctx = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); |
| 178 | if (!ctx) { |
Longpeng(Mike) | 25c60df | 2017-07-14 14:04:06 -0400 | [diff] [blame] | 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | drv = &qcrypto_cipher_lib_driver; |
Longpeng(Mike) | 75c8007 | 2017-07-14 14:03:58 -0400 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | cipher = g_new0(QCryptoCipher, 1); |
| 186 | cipher->alg = alg; |
| 187 | cipher->mode = mode; |
| 188 | cipher->opaque = ctx; |
Longpeng(Mike) | 25c60df | 2017-07-14 14:04:06 -0400 | [diff] [blame] | 189 | cipher->driver = (void *)drv; |
Longpeng(Mike) | 75c8007 | 2017-07-14 14:03:58 -0400 | [diff] [blame] | 190 | |
| 191 | return cipher; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | int qcrypto_cipher_encrypt(QCryptoCipher *cipher, |
| 196 | const void *in, |
| 197 | void *out, |
| 198 | size_t len, |
| 199 | Error **errp) |
| 200 | { |
| 201 | QCryptoCipherDriver *drv = cipher->driver; |
| 202 | return drv->cipher_encrypt(cipher, in, out, len, errp); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | int qcrypto_cipher_decrypt(QCryptoCipher *cipher, |
| 207 | const void *in, |
| 208 | void *out, |
| 209 | size_t len, |
| 210 | Error **errp) |
| 211 | { |
| 212 | QCryptoCipherDriver *drv = cipher->driver; |
| 213 | return drv->cipher_decrypt(cipher, in, out, len, errp); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | int qcrypto_cipher_setiv(QCryptoCipher *cipher, |
| 218 | const uint8_t *iv, size_t niv, |
| 219 | Error **errp) |
| 220 | { |
| 221 | QCryptoCipherDriver *drv = cipher->driver; |
| 222 | return drv->cipher_setiv(cipher, iv, niv, errp); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | void qcrypto_cipher_free(QCryptoCipher *cipher) |
| 227 | { |
| 228 | QCryptoCipherDriver *drv; |
| 229 | if (cipher) { |
| 230 | drv = cipher->driver; |
| 231 | drv->cipher_free(cipher); |
| 232 | g_free(cipher); |
| 233 | } |
| 234 | } |