1cb730894SDaniel P. Berrange /* 2cb730894SDaniel P. Berrange * QEMU Crypto block IV generator 3cb730894SDaniel P. Berrange * 4cb730894SDaniel P. Berrange * Copyright (c) 2015-2016 Red Hat, Inc. 5cb730894SDaniel P. Berrange * 6cb730894SDaniel P. Berrange * This library is free software; you can redistribute it and/or 7cb730894SDaniel P. Berrange * modify it under the terms of the GNU Lesser General Public 8cb730894SDaniel P. Berrange * License as published by the Free Software Foundation; either 9*b7cbb874SThomas Huth * version 2.1 of the License, or (at your option) any later version. 10cb730894SDaniel P. Berrange * 11cb730894SDaniel P. Berrange * This library is distributed in the hope that it will be useful, 12cb730894SDaniel P. Berrange * but WITHOUT ANY WARRANTY; without even the implied warranty of 13cb730894SDaniel P. Berrange * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14cb730894SDaniel P. Berrange * Lesser General Public License for more details. 15cb730894SDaniel P. Berrange * 16cb730894SDaniel P. Berrange * You should have received a copy of the GNU Lesser General Public 17cb730894SDaniel P. Berrange * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18cb730894SDaniel P. Berrange * 19cb730894SDaniel P. Berrange */ 20cb730894SDaniel P. Berrange 21cb730894SDaniel P. Berrange #include "qemu/osdep.h" 22da34e65cSMarkus Armbruster #include "qapi/error.h" 23da34e65cSMarkus Armbruster 24986bc8deSMichael S. Tsirkin #include "ivgenpriv.h" 25986bc8deSMichael S. Tsirkin #include "ivgen-plain.h" 26986bc8deSMichael S. Tsirkin #include "ivgen-plain64.h" 27986bc8deSMichael S. Tsirkin #include "ivgen-essiv.h" 28cb730894SDaniel P. Berrange 29cb730894SDaniel P. Berrange 30cb730894SDaniel P. Berrange QCryptoIVGen *qcrypto_ivgen_new(QCryptoIVGenAlgorithm alg, 31cb730894SDaniel P. Berrange QCryptoCipherAlgorithm cipheralg, 32cb730894SDaniel P. Berrange QCryptoHashAlgorithm hash, 33cb730894SDaniel P. Berrange const uint8_t *key, size_t nkey, 34cb730894SDaniel P. Berrange Error **errp) 35cb730894SDaniel P. Berrange { 36cb730894SDaniel P. Berrange QCryptoIVGen *ivgen = g_new0(QCryptoIVGen, 1); 37cb730894SDaniel P. Berrange 38cb730894SDaniel P. Berrange ivgen->algorithm = alg; 39cb730894SDaniel P. Berrange ivgen->cipher = cipheralg; 40cb730894SDaniel P. Berrange ivgen->hash = hash; 41cb730894SDaniel P. Berrange 42cb730894SDaniel P. Berrange switch (alg) { 43cb730894SDaniel P. Berrange case QCRYPTO_IVGEN_ALG_PLAIN: 44cb730894SDaniel P. Berrange ivgen->driver = &qcrypto_ivgen_plain; 45cb730894SDaniel P. Berrange break; 46cb730894SDaniel P. Berrange case QCRYPTO_IVGEN_ALG_PLAIN64: 47cb730894SDaniel P. Berrange ivgen->driver = &qcrypto_ivgen_plain64; 48cb730894SDaniel P. Berrange break; 49cb730894SDaniel P. Berrange case QCRYPTO_IVGEN_ALG_ESSIV: 50cb730894SDaniel P. Berrange ivgen->driver = &qcrypto_ivgen_essiv; 51cb730894SDaniel P. Berrange break; 52cb730894SDaniel P. Berrange default: 53cb730894SDaniel P. Berrange error_setg(errp, "Unknown block IV generator algorithm %d", alg); 54cb730894SDaniel P. Berrange g_free(ivgen); 55cb730894SDaniel P. Berrange return NULL; 56cb730894SDaniel P. Berrange } 57cb730894SDaniel P. Berrange 58cb730894SDaniel P. Berrange if (ivgen->driver->init(ivgen, key, nkey, errp) < 0) { 59cb730894SDaniel P. Berrange g_free(ivgen); 60cb730894SDaniel P. Berrange return NULL; 61cb730894SDaniel P. Berrange } 62cb730894SDaniel P. Berrange 63cb730894SDaniel P. Berrange return ivgen; 64cb730894SDaniel P. Berrange } 65cb730894SDaniel P. Berrange 66cb730894SDaniel P. Berrange 67cb730894SDaniel P. Berrange int qcrypto_ivgen_calculate(QCryptoIVGen *ivgen, 68cb730894SDaniel P. Berrange uint64_t sector, 69cb730894SDaniel P. Berrange uint8_t *iv, size_t niv, 70cb730894SDaniel P. Berrange Error **errp) 71cb730894SDaniel P. Berrange { 72cb730894SDaniel P. Berrange return ivgen->driver->calculate(ivgen, sector, iv, niv, errp); 73cb730894SDaniel P. Berrange } 74cb730894SDaniel P. Berrange 75cb730894SDaniel P. Berrange 76cb730894SDaniel P. Berrange QCryptoIVGenAlgorithm qcrypto_ivgen_get_algorithm(QCryptoIVGen *ivgen) 77cb730894SDaniel P. Berrange { 78cb730894SDaniel P. Berrange return ivgen->algorithm; 79cb730894SDaniel P. Berrange } 80cb730894SDaniel P. Berrange 81cb730894SDaniel P. Berrange 82cb730894SDaniel P. Berrange QCryptoCipherAlgorithm qcrypto_ivgen_get_cipher(QCryptoIVGen *ivgen) 83cb730894SDaniel P. Berrange { 84cb730894SDaniel P. Berrange return ivgen->cipher; 85cb730894SDaniel P. Berrange } 86cb730894SDaniel P. Berrange 87cb730894SDaniel P. Berrange 88cb730894SDaniel P. Berrange QCryptoHashAlgorithm qcrypto_ivgen_get_hash(QCryptoIVGen *ivgen) 89cb730894SDaniel P. Berrange { 90cb730894SDaniel P. Berrange return ivgen->hash; 91cb730894SDaniel P. Berrange } 92cb730894SDaniel P. Berrange 93cb730894SDaniel P. Berrange 94cb730894SDaniel P. Berrange void qcrypto_ivgen_free(QCryptoIVGen *ivgen) 95cb730894SDaniel P. Berrange { 96cb730894SDaniel P. Berrange if (!ivgen) { 97cb730894SDaniel P. Berrange return; 98cb730894SDaniel P. Berrange } 99cb730894SDaniel P. Berrange ivgen->driver->cleanup(ivgen); 100cb730894SDaniel P. Berrange g_free(ivgen); 101cb730894SDaniel P. Berrange } 102