afsplit.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * QEMU Crypto anti forensic information splitter
  3. *
  4. * Copyright (c) 2015-2016 Red Hat, Inc.
  5. *
  6. * Derived from cryptsetup package lib/luks1/af.c
  7. *
  8. * Copyright (C) 2004, Clemens Fruhwirth <clemens@endorphin.org>
  9. * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/bswap.h"
  26. #include "crypto/afsplit.h"
  27. #include "crypto/random.h"
  28. static void qcrypto_afsplit_xor(size_t blocklen,
  29. const uint8_t *in1,
  30. const uint8_t *in2,
  31. uint8_t *out)
  32. {
  33. size_t i;
  34. for (i = 0; i < blocklen; i++) {
  35. out[i] = in1[i] ^ in2[i];
  36. }
  37. }
  38. static int qcrypto_afsplit_hash(QCryptoHashAlgorithm hash,
  39. size_t blocklen,
  40. uint8_t *block,
  41. Error **errp)
  42. {
  43. size_t digestlen = qcrypto_hash_digest_len(hash);
  44. size_t hashcount = blocklen / digestlen;
  45. size_t finallen = blocklen % digestlen;
  46. uint32_t i;
  47. if (finallen) {
  48. hashcount++;
  49. } else {
  50. finallen = digestlen;
  51. }
  52. for (i = 0; i < hashcount; i++) {
  53. g_autofree uint8_t *out = NULL;
  54. size_t outlen = 0;
  55. uint32_t iv = cpu_to_be32(i);
  56. struct iovec in[] = {
  57. { .iov_base = &iv,
  58. .iov_len = sizeof(iv) },
  59. { .iov_base = block + (i * digestlen),
  60. .iov_len = (i == (hashcount - 1)) ? finallen : digestlen },
  61. };
  62. if (qcrypto_hash_bytesv(hash,
  63. in,
  64. G_N_ELEMENTS(in),
  65. &out, &outlen,
  66. errp) < 0) {
  67. return -1;
  68. }
  69. assert(outlen == digestlen);
  70. memcpy(block + (i * digestlen), out,
  71. (i == (hashcount - 1)) ? finallen : digestlen);
  72. }
  73. return 0;
  74. }
  75. int qcrypto_afsplit_encode(QCryptoHashAlgorithm hash,
  76. size_t blocklen,
  77. uint32_t stripes,
  78. const uint8_t *in,
  79. uint8_t *out,
  80. Error **errp)
  81. {
  82. g_autofree uint8_t *block = g_new0(uint8_t, blocklen);
  83. size_t i;
  84. for (i = 0; i < (stripes - 1); i++) {
  85. if (qcrypto_random_bytes(out + (i * blocklen), blocklen, errp) < 0) {
  86. return -1;
  87. }
  88. qcrypto_afsplit_xor(blocklen,
  89. out + (i * blocklen),
  90. block,
  91. block);
  92. if (qcrypto_afsplit_hash(hash, blocklen, block,
  93. errp) < 0) {
  94. return -1;
  95. }
  96. }
  97. qcrypto_afsplit_xor(blocklen,
  98. in,
  99. block,
  100. out + (i * blocklen));
  101. return 0;
  102. }
  103. int qcrypto_afsplit_decode(QCryptoHashAlgorithm hash,
  104. size_t blocklen,
  105. uint32_t stripes,
  106. const uint8_t *in,
  107. uint8_t *out,
  108. Error **errp)
  109. {
  110. g_autofree uint8_t *block = g_new0(uint8_t, blocklen);
  111. size_t i;
  112. for (i = 0; i < (stripes - 1); i++) {
  113. qcrypto_afsplit_xor(blocklen,
  114. in + (i * blocklen),
  115. block,
  116. block);
  117. if (qcrypto_afsplit_hash(hash, blocklen, block,
  118. errp) < 0) {
  119. return -1;
  120. }
  121. }
  122. qcrypto_afsplit_xor(blocklen,
  123. in + (i * blocklen),
  124. block,
  125. out);
  126. return 0;
  127. }