| 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.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 | */ |
| 20 | |
| 21 | #include "qemu/osdep.h" |
| 22 | #include "qemu/host-utils.h" |
| 23 | #include "qapi/error.h" |
| 24 | #include "crypto/cipher.h" |
| 25 | #include "cipherpriv.h" |
| 26 | |
| 27 | |
| 28 | static const size_t alg_key_len[QCRYPTO_CIPHER_ALGO__MAX] = { |
| 29 | [QCRYPTO_CIPHER_ALGO_AES_128] = 16, |
| 30 | [QCRYPTO_CIPHER_ALGO_AES_192] = 24, |
| 31 | [QCRYPTO_CIPHER_ALGO_AES_256] = 32, |
| 32 | [QCRYPTO_CIPHER_ALGO_DES] = 8, |
| 33 | [QCRYPTO_CIPHER_ALGO_3DES] = 24, |
| 34 | [QCRYPTO_CIPHER_ALGO_CAST5_128] = 16, |
| 35 | [QCRYPTO_CIPHER_ALGO_SERPENT_128] = 16, |
| 36 | [QCRYPTO_CIPHER_ALGO_SERPENT_192] = 24, |
| 37 | [QCRYPTO_CIPHER_ALGO_SERPENT_256] = 32, |
| 38 | [QCRYPTO_CIPHER_ALGO_TWOFISH_128] = 16, |
| 39 | [QCRYPTO_CIPHER_ALGO_TWOFISH_192] = 24, |
| 40 | [QCRYPTO_CIPHER_ALGO_TWOFISH_256] = 32, |
| 41 | #ifdef CONFIG_CRYPTO_SM4 |
| 42 | [QCRYPTO_CIPHER_ALGO_SM4] = 16, |
| 43 | #endif |
| 44 | }; |
| 45 | |
| 46 | static const size_t alg_block_len[QCRYPTO_CIPHER_ALGO__MAX] = { |
| 47 | [QCRYPTO_CIPHER_ALGO_AES_128] = 16, |
| 48 | [QCRYPTO_CIPHER_ALGO_AES_192] = 16, |
| 49 | [QCRYPTO_CIPHER_ALGO_AES_256] = 16, |
| 50 | [QCRYPTO_CIPHER_ALGO_DES] = 8, |
| 51 | [QCRYPTO_CIPHER_ALGO_3DES] = 8, |
| 52 | [QCRYPTO_CIPHER_ALGO_CAST5_128] = 8, |
| 53 | [QCRYPTO_CIPHER_ALGO_SERPENT_128] = 16, |
| 54 | [QCRYPTO_CIPHER_ALGO_SERPENT_192] = 16, |
| 55 | [QCRYPTO_CIPHER_ALGO_SERPENT_256] = 16, |
| 56 | [QCRYPTO_CIPHER_ALGO_TWOFISH_128] = 16, |
| 57 | [QCRYPTO_CIPHER_ALGO_TWOFISH_192] = 16, |
| 58 | [QCRYPTO_CIPHER_ALGO_TWOFISH_256] = 16, |
| 59 | #ifdef CONFIG_CRYPTO_SM4 |
| 60 | [QCRYPTO_CIPHER_ALGO_SM4] = 16, |
| 61 | #endif |
| 62 | }; |
| 63 | |
| 64 | static const bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = { |
| 65 | [QCRYPTO_CIPHER_MODE_ECB] = false, |
| 66 | [QCRYPTO_CIPHER_MODE_CBC] = true, |
| 67 | [QCRYPTO_CIPHER_MODE_XTS] = true, |
| 68 | [QCRYPTO_CIPHER_MODE_CTR] = true, |
| 69 | [QCRYPTO_CIPHER_MODE_GCM] = true, |
| 70 | }; |
| 71 | |
| 72 | |
| 73 | size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgo alg) |
| 74 | { |
| 75 | assert(alg < G_N_ELEMENTS(alg_key_len)); |
| 76 | return alg_block_len[alg]; |
| 77 | } |
| 78 | |
| 79 | |
| 80 | size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgo alg) |
| 81 | { |
| 82 | assert(alg < G_N_ELEMENTS(alg_key_len)); |
| 83 | return alg_key_len[alg]; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgo alg, |
| 88 | QCryptoCipherMode mode) |
| 89 | { |
| 90 | if (alg >= G_N_ELEMENTS(alg_block_len)) { |
| 91 | return 0; |
| 92 | } |
| 93 | if (mode >= G_N_ELEMENTS(mode_need_iv)) { |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | if (mode_need_iv[mode]) { |
| 98 | return alg_block_len[alg]; |
| 99 | } |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | static bool |
| 105 | qcrypto_cipher_validate_key_length(QCryptoCipherAlgo alg, |
| 106 | QCryptoCipherMode mode, |
| 107 | size_t nkey, |
| 108 | Error **errp) |
| 109 | { |
| 110 | if ((unsigned)alg >= QCRYPTO_CIPHER_ALGO__MAX) { |
| 111 | error_setg(errp, "Cipher algorithm %d out of range", |
| 112 | alg); |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | if (mode == QCRYPTO_CIPHER_MODE_XTS) { |
| 117 | if (alg == QCRYPTO_CIPHER_ALGO_DES || |
| 118 | alg == QCRYPTO_CIPHER_ALGO_3DES) { |
| 119 | error_setg(errp, "XTS mode not compatible with DES/3DES"); |
| 120 | return false; |
| 121 | } |
| 122 | if (nkey % 2) { |
| 123 | error_setg(errp, "XTS cipher key length should be a multiple of 2"); |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | if (alg_key_len[alg] != (nkey / 2)) { |
| 128 | error_setg(errp, "Cipher key length %zu should be %zu", |
| 129 | nkey, alg_key_len[alg] * 2); |
| 130 | return false; |
| 131 | } |
| 132 | } else { |
| 133 | if (alg_key_len[alg] != nkey) { |
| 134 | error_setg(errp, "Cipher key length %zu should be %zu", |
| 135 | nkey, alg_key_len[alg]); |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | #ifdef CONFIG_GCRYPT |
| 143 | #include "cipher-gcrypt.c.inc" |
| 144 | #elif defined CONFIG_NETTLE |
| 145 | #include "cipher-nettle.c.inc" |
| 146 | #elif defined CONFIG_GNUTLS_CRYPTO |
| 147 | #include "cipher-gnutls.c.inc" |
| 148 | #else |
| 149 | #include "cipher-stub.c.inc" |
| 150 | #endif |
| 151 | |
| 152 | QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgo alg, |
| 153 | QCryptoCipherMode mode, |
| 154 | const uint8_t *key, size_t nkey, |
| 155 | Error **errp) |
| 156 | { |
| 157 | QCryptoCipher *cipher = NULL; |
| 158 | |
| 159 | #ifdef CONFIG_AF_ALG |
| 160 | cipher = qcrypto_afalg_cipher_ctx_new(alg, mode, key, nkey, NULL); |
| 161 | #endif |
| 162 | |
| 163 | if (!cipher) { |
| 164 | cipher = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp); |
| 165 | if (!cipher) { |
| 166 | return NULL; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | cipher->alg = alg; |
| 171 | cipher->mode = mode; |
| 172 | |
| 173 | return cipher; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | int qcrypto_cipher_encrypt(QCryptoCipher *cipher, |
| 178 | const void *in, |
| 179 | void *out, |
| 180 | size_t len, |
| 181 | Error **errp) |
| 182 | { |
| 183 | const QCryptoCipherDriver *drv = cipher->driver; |
| 184 | return drv->cipher_encrypt(cipher, in, out, len, errp); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | int qcrypto_cipher_decrypt(QCryptoCipher *cipher, |
| 189 | const void *in, |
| 190 | void *out, |
| 191 | size_t len, |
| 192 | Error **errp) |
| 193 | { |
| 194 | const QCryptoCipherDriver *drv = cipher->driver; |
| 195 | return drv->cipher_decrypt(cipher, in, out, len, errp); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | int qcrypto_cipher_setiv(QCryptoCipher *cipher, |
| 200 | const uint8_t *iv, size_t niv, |
| 201 | Error **errp) |
| 202 | { |
| 203 | const QCryptoCipherDriver *drv = cipher->driver; |
| 204 | return drv->cipher_setiv(cipher, iv, niv, errp); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | int qcrypto_cipher_setaad(QCryptoCipher *cipher, |
| 209 | const uint8_t *aad, size_t len, |
| 210 | Error **errp) |
| 211 | { |
| 212 | const QCryptoCipherDriver *drv = cipher->driver; |
| 213 | |
| 214 | if (!drv->cipher_setaad) { |
| 215 | error_setg(errp, "The cipher mode does not support associated data"); |
| 216 | return -1; |
| 217 | } |
| 218 | |
| 219 | return drv->cipher_setaad(cipher, aad, len, errp); |
| 220 | } |
| 221 | |
| 222 | |
| 223 | int qcrypto_cipher_gettag(QCryptoCipher *cipher, |
| 224 | uint8_t *tag, size_t len, |
| 225 | Error **errp) |
| 226 | { |
| 227 | const QCryptoCipherDriver *drv = cipher->driver; |
| 228 | |
| 229 | if (!drv->cipher_gettag) { |
| 230 | error_setg(errp, |
| 231 | "The cipher mode does not produce an authentication tag"); |
| 232 | return -1; |
| 233 | } |
| 234 | |
| 235 | return drv->cipher_gettag(cipher, tag, len, errp); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | void qcrypto_cipher_free(QCryptoCipher *cipher) |
| 240 | { |
| 241 | if (cipher) { |
| 242 | cipher->driver->cipher_free(cipher); |
| 243 | } |
| 244 | } |