1 /* 2 * QEMU Crypto block IV generator - essiv 3 * 4 * Copyright (c) 2015-2016 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 21 #include "qemu/osdep.h" 22 #include "crypto/ivgen-essiv.h" 23 24 typedef struct QCryptoIVGenESSIV QCryptoIVGenESSIV; 25 struct QCryptoIVGenESSIV { 26 QCryptoCipher *cipher; 27 }; 28 29 static int qcrypto_ivgen_essiv_init(QCryptoIVGen *ivgen, 30 const uint8_t *key, size_t nkey, 31 Error **errp) 32 { 33 uint8_t *salt; 34 size_t nhash; 35 size_t nsalt; 36 QCryptoIVGenESSIV *essiv = g_new0(QCryptoIVGenESSIV, 1); 37 38 /* Not necessarily the same as nkey */ 39 nsalt = qcrypto_cipher_get_key_len(ivgen->cipher); 40 41 nhash = qcrypto_hash_digest_len(ivgen->hash); 42 /* Salt must be larger of hash size or key size */ 43 salt = g_new0(uint8_t, MAX(nhash, nsalt)); 44 45 if (qcrypto_hash_bytes(ivgen->hash, (const gchar *)key, nkey, 46 &salt, &nhash, 47 errp) < 0) { 48 g_free(essiv); 49 return -1; 50 } 51 52 /* Now potentially truncate salt to match cipher key len */ 53 essiv->cipher = qcrypto_cipher_new(ivgen->cipher, 54 QCRYPTO_CIPHER_MODE_ECB, 55 salt, MIN(nhash, nsalt), 56 errp); 57 if (!essiv->cipher) { 58 g_free(essiv); 59 g_free(salt); 60 return -1; 61 } 62 63 g_free(salt); 64 ivgen->private = essiv; 65 66 return 0; 67 } 68 69 static int qcrypto_ivgen_essiv_calculate(QCryptoIVGen *ivgen, 70 uint64_t sector, 71 uint8_t *iv, size_t niv, 72 Error **errp) 73 { 74 QCryptoIVGenESSIV *essiv = ivgen->private; 75 size_t ndata = qcrypto_cipher_get_block_len(ivgen->cipher); 76 uint8_t *data = g_new(uint8_t, ndata); 77 78 sector = cpu_to_le64(sector); 79 memcpy(data, (uint8_t *)§or, ndata); 80 if (sizeof(sector) < ndata) { 81 memset(data + sizeof(sector), 0, ndata - sizeof(sector)); 82 } 83 84 if (qcrypto_cipher_encrypt(essiv->cipher, 85 data, 86 data, 87 ndata, 88 errp) < 0) { 89 g_free(data); 90 return -1; 91 } 92 93 if (ndata > niv) { 94 ndata = niv; 95 } 96 memcpy(iv, data, ndata); 97 if (ndata < niv) { 98 memset(iv + ndata, 0, niv - ndata); 99 } 100 g_free(data); 101 return 0; 102 } 103 104 static void qcrypto_ivgen_essiv_cleanup(QCryptoIVGen *ivgen) 105 { 106 QCryptoIVGenESSIV *essiv = ivgen->private; 107 108 qcrypto_cipher_free(essiv->cipher); 109 g_free(essiv); 110 } 111 112 113 struct QCryptoIVGenDriver qcrypto_ivgen_essiv = { 114 .init = qcrypto_ivgen_essiv_init, 115 .calculate = qcrypto_ivgen_essiv_calculate, 116 .cleanup = qcrypto_ivgen_essiv_cleanup, 117 }; 118 119