AFSplitter.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/dl.h>
  24. #include <grub/mm.h>
  25. #include <grub/misc.h>
  26. GRUB_MOD_LICENSE ("GPLv2+");
  27. gcry_err_code_t AF_merge (const gcry_md_spec_t * hash, grub_uint8_t * src,
  28. grub_uint8_t * dst, grub_size_t blocksize,
  29. grub_size_t blocknumbers);
  30. static void
  31. diffuse (const gcry_md_spec_t * hash, grub_uint8_t * src,
  32. grub_uint8_t * dst, grub_size_t size)
  33. {
  34. grub_size_t i;
  35. grub_uint32_t IV; /* host byte order independend hash IV */
  36. grub_size_t fullblocks = size / hash->mdlen;
  37. int padding = size % hash->mdlen;
  38. grub_uint8_t final[GRUB_CRYPTO_MAX_MDLEN];
  39. grub_uint8_t temp[sizeof (IV) + GRUB_CRYPTO_MAX_MDLEN];
  40. /* hash block the whole data set with different IVs to produce
  41. * more than just a single data block
  42. */
  43. for (i = 0; i < fullblocks; i++)
  44. {
  45. IV = grub_cpu_to_be32 (i);
  46. grub_memcpy (temp, &IV, sizeof (IV));
  47. grub_memcpy (temp + sizeof (IV), src + hash->mdlen * i, hash->mdlen);
  48. grub_crypto_hash (hash, dst + hash->mdlen * i, temp,
  49. sizeof (IV) + hash->mdlen);
  50. }
  51. if (padding)
  52. {
  53. IV = grub_cpu_to_be32 (i);
  54. grub_memcpy (temp, &IV, sizeof (IV));
  55. grub_memcpy (temp + sizeof (IV), src + hash->mdlen * i, padding);
  56. grub_crypto_hash (hash, final, temp, sizeof (IV) + padding);
  57. grub_memcpy (dst + hash->mdlen * i, final, padding);
  58. }
  59. }
  60. /**
  61. * Merges the splitted master key stored on disk into the original key
  62. */
  63. gcry_err_code_t
  64. AF_merge (const gcry_md_spec_t * hash, grub_uint8_t * src, grub_uint8_t * dst,
  65. grub_size_t blocksize, grub_size_t blocknumbers)
  66. {
  67. grub_size_t i;
  68. grub_uint8_t *bufblock;
  69. if (hash->mdlen > GRUB_CRYPTO_MAX_MDLEN || hash->mdlen == 0)
  70. return GPG_ERR_INV_ARG;
  71. bufblock = grub_zalloc (blocksize);
  72. if (bufblock == NULL)
  73. return GPG_ERR_OUT_OF_MEMORY;
  74. grub_memset (bufblock, 0, blocksize);
  75. for (i = 0; i < blocknumbers - 1; i++)
  76. {
  77. grub_crypto_xor (bufblock, src + (blocksize * i), bufblock, blocksize);
  78. diffuse (hash, bufblock, bufblock, blocksize);
  79. }
  80. grub_crypto_xor (dst, src + (i * blocksize), bufblock, blocksize);
  81. grub_free (bufblock);
  82. return GPG_ERR_NO_ERROR;
  83. }