qemu/crypto/ivgen.c
<<
>>
Prefs
   1/*
   2 * QEMU Crypto block IV generator
   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.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 "qapi/error.h"
  23
  24#include "ivgenpriv.h"
  25#include "ivgen-plain.h"
  26#include "ivgen-plain64.h"
  27#include "ivgen-essiv.h"
  28
  29
  30QCryptoIVGen *qcrypto_ivgen_new(QCryptoIVGenAlgorithm alg,
  31                                QCryptoCipherAlgorithm cipheralg,
  32                                QCryptoHashAlgorithm hash,
  33                                const uint8_t *key, size_t nkey,
  34                                Error **errp)
  35{
  36    QCryptoIVGen *ivgen = g_new0(QCryptoIVGen, 1);
  37
  38    ivgen->algorithm = alg;
  39    ivgen->cipher = cipheralg;
  40    ivgen->hash = hash;
  41
  42    switch (alg) {
  43    case QCRYPTO_IVGEN_ALG_PLAIN:
  44        ivgen->driver = &qcrypto_ivgen_plain;
  45        break;
  46    case QCRYPTO_IVGEN_ALG_PLAIN64:
  47        ivgen->driver = &qcrypto_ivgen_plain64;
  48        break;
  49    case QCRYPTO_IVGEN_ALG_ESSIV:
  50        ivgen->driver = &qcrypto_ivgen_essiv;
  51        break;
  52    default:
  53        error_setg(errp, "Unknown block IV generator algorithm %d", alg);
  54        g_free(ivgen);
  55        return NULL;
  56    }
  57
  58    if (ivgen->driver->init(ivgen, key, nkey, errp) < 0) {
  59        g_free(ivgen);
  60        return NULL;
  61    }
  62
  63    return ivgen;
  64}
  65
  66
  67int qcrypto_ivgen_calculate(QCryptoIVGen *ivgen,
  68                            uint64_t sector,
  69                            uint8_t *iv, size_t niv,
  70                            Error **errp)
  71{
  72    return ivgen->driver->calculate(ivgen, sector, iv, niv, errp);
  73}
  74
  75
  76QCryptoIVGenAlgorithm qcrypto_ivgen_get_algorithm(QCryptoIVGen *ivgen)
  77{
  78    return ivgen->algorithm;
  79}
  80
  81
  82QCryptoCipherAlgorithm qcrypto_ivgen_get_cipher(QCryptoIVGen *ivgen)
  83{
  84    return ivgen->cipher;
  85}
  86
  87
  88QCryptoHashAlgorithm qcrypto_ivgen_get_hash(QCryptoIVGen *ivgen)
  89{
  90    return ivgen->hash;
  91}
  92
  93
  94void qcrypto_ivgen_free(QCryptoIVGen *ivgen)
  95{
  96    if (!ivgen) {
  97        return;
  98    }
  99    ivgen->driver->cleanup(ivgen);
 100    g_free(ivgen);
 101}
 102