AFSplitter.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * AFsplitter - Anti forensic information splitter
  3. * Copyright 2004, Clemens Fruhwirth <clemens@endorphin.org>
  4. *
  5. * AFsplitter diffuses information over a large stripe of data,
  6. * therefor supporting secure data destruction.
  7. *
  8. * This program is grub_free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the grub_free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Library General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the grub_free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <grub/crypto.h>
  23. #include <grub/mm.h>
  24. #include <grub/misc.h>
  25. gcry_err_code_t AF_merge (const gcry_md_spec_t * hash, grub_uint8_t * src,
  26. grub_uint8_t * dst, grub_size_t blocksize,
  27. grub_size_t blocknumbers);
  28. static void
  29. diffuse (const gcry_md_spec_t * hash, grub_uint8_t * src,
  30. grub_uint8_t * dst, grub_size_t size)
  31. {
  32. grub_size_t i;
  33. grub_uint32_t IV; /* host byte order independend hash IV */
  34. grub_size_t fullblocks = size / hash->mdlen;
  35. int padding = size % hash->mdlen;
  36. grub_uint8_t final[GRUB_CRYPTO_MAX_MDLEN];
  37. grub_uint8_t temp[sizeof (IV) + GRUB_CRYPTO_MAX_MDLEN];
  38. /* hash block the whole data set with different IVs to produce
  39. * more than just a single data block
  40. */
  41. for (i = 0; i < fullblocks; i++)
  42. {
  43. IV = grub_cpu_to_be32 (i);
  44. grub_memcpy (temp, &IV, sizeof (IV));
  45. grub_memcpy (temp + sizeof (IV), src + hash->mdlen * i, hash->mdlen);
  46. grub_crypto_hash (hash, dst + hash->mdlen * i, temp,
  47. sizeof (IV) + hash->mdlen);
  48. }
  49. if (padding)
  50. {
  51. IV = grub_cpu_to_be32 (i);
  52. grub_memcpy (temp, &IV, sizeof (IV));
  53. grub_memcpy (temp + sizeof (IV), src + hash->mdlen * i, padding);
  54. grub_crypto_hash (hash, final, temp, sizeof (IV) + padding);
  55. grub_memcpy (dst + hash->mdlen * i, final, padding);
  56. }
  57. }
  58. /**
  59. * Merges the splitted master key stored on disk into the original key
  60. */
  61. gcry_err_code_t
  62. AF_merge (const gcry_md_spec_t * hash, grub_uint8_t * src, grub_uint8_t * dst,
  63. grub_size_t blocksize, grub_size_t blocknumbers)
  64. {
  65. grub_size_t i;
  66. grub_uint8_t *bufblock;
  67. if (hash->mdlen > GRUB_CRYPTO_MAX_MDLEN || hash->mdlen == 0)
  68. return GPG_ERR_INV_ARG;
  69. bufblock = grub_zalloc (blocksize);
  70. if (bufblock == NULL)
  71. return GPG_ERR_OUT_OF_MEMORY;
  72. grub_memset (bufblock, 0, blocksize);
  73. for (i = 0; i < blocknumbers - 1; i++)
  74. {
  75. grub_crypto_xor (bufblock, src + (blocksize * i), bufblock, blocksize);
  76. diffuse (hash, bufblock, bufblock, blocksize);
  77. }
  78. grub_crypto_xor (dst, src + (i * blocksize), bufblock, blocksize);
  79. grub_free (bufblock);
  80. return GPG_ERR_NO_ERROR;
  81. }