2
0

ivgen.c 2.6 KB

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