afsplit.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. 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. g_free(out);
  73. }
  74. return 0;
  75. }
  76. int qcrypto_afsplit_encode(QCryptoHashAlgorithm hash,
  77. size_t blocklen,
  78. uint32_t stripes,
  79. const uint8_t *in,
  80. uint8_t *out,
  81. Error **errp)
  82. {
  83. uint8_t *block = g_new0(uint8_t, blocklen);
  84. size_t i;
  85. int ret = -1;
  86. for (i = 0; i < (stripes - 1); i++) {
  87. if (qcrypto_random_bytes(out + (i * blocklen), blocklen, errp) < 0) {
  88. goto cleanup;
  89. }
  90. qcrypto_afsplit_xor(blocklen,
  91. out + (i * blocklen),
  92. block,
  93. block);
  94. if (qcrypto_afsplit_hash(hash, blocklen, block,
  95. errp) < 0) {
  96. goto cleanup;
  97. }
  98. }
  99. qcrypto_afsplit_xor(blocklen,
  100. in,
  101. block,
  102. out + (i * blocklen));
  103. ret = 0;
  104. cleanup:
  105. g_free(block);
  106. return ret;
  107. }
  108. int qcrypto_afsplit_decode(QCryptoHashAlgorithm hash,
  109. size_t blocklen,
  110. uint32_t stripes,
  111. const uint8_t *in,
  112. uint8_t *out,
  113. Error **errp)
  114. {
  115. uint8_t *block = g_new0(uint8_t, blocklen);
  116. size_t i;
  117. int ret = -1;
  118. for (i = 0; i < (stripes - 1); i++) {
  119. qcrypto_afsplit_xor(blocklen,
  120. in + (i * blocklen),
  121. block,
  122. block);
  123. if (qcrypto_afsplit_hash(hash, blocklen, block,
  124. errp) < 0) {
  125. goto cleanup;
  126. }
  127. }
  128. qcrypto_afsplit_xor(blocklen,
  129. in + (i * blocklen),
  130. block,
  131. out);
  132. ret = 0;
  133. cleanup:
  134. g_free(block);
  135. return ret;
  136. }