crc64.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* crc64.c - crc64 function */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008,2011 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/types.h>
  20. #include <grub/dl.h>
  21. #include <grub/crypto.h>
  22. GRUB_MOD_LICENSE ("GPLv3+");
  23. static grub_uint64_t crc64_table [256];
  24. /* Helper for init_crc64_table. */
  25. static grub_uint64_t
  26. reflect (grub_uint64_t ref, int len)
  27. {
  28. grub_uint64_t result = 0;
  29. int i;
  30. for (i = 1; i <= len; i++)
  31. {
  32. if (ref & 1)
  33. result |= 1ULL << (len - i);
  34. ref >>= 1;
  35. }
  36. return result;
  37. }
  38. static void
  39. init_crc64_table (void)
  40. {
  41. grub_uint64_t polynomial = 0x42f0e1eba9ea3693ULL;
  42. int i, j;
  43. for(i = 0; i < 256; i++)
  44. {
  45. crc64_table[i] = reflect(i, 8) << 56;
  46. for (j = 0; j < 8; j++)
  47. {
  48. crc64_table[i] = (crc64_table[i] << 1) ^
  49. (crc64_table[i] & (1ULL << 63) ? polynomial : 0);
  50. }
  51. crc64_table[i] = reflect(crc64_table[i], 64);
  52. }
  53. }
  54. static void
  55. crc64_init (void *context)
  56. {
  57. if (! crc64_table[1])
  58. init_crc64_table ();
  59. *(grub_uint64_t *) context = 0;
  60. }
  61. static void
  62. crc64_write (void *context, const void *buf, grub_size_t size)
  63. {
  64. unsigned i;
  65. const grub_uint8_t *data = buf;
  66. grub_uint64_t crc = ~grub_le_to_cpu64 (*(grub_uint64_t *) context);
  67. for (i = 0; i < size; i++)
  68. {
  69. crc = (crc >> 8) ^ crc64_table[(crc & 0xFF) ^ *data];
  70. data++;
  71. }
  72. *(grub_uint64_t *) context = grub_cpu_to_le64 (~crc);
  73. }
  74. static grub_uint8_t *
  75. crc64_read (void *context)
  76. {
  77. return context;
  78. }
  79. static void
  80. crc64_final (void *context __attribute__ ((unused)))
  81. {
  82. }
  83. gcry_md_spec_t _gcry_digest_spec_crc64 =
  84. {
  85. "CRC64", 0, 0, 0, 8,
  86. crc64_init, crc64_write, crc64_final, crc64_read,
  87. sizeof (grub_uint64_t),
  88. .blocksize = 64
  89. };
  90. GRUB_MOD_INIT(crc64)
  91. {
  92. grub_md_register (&_gcry_digest_spec_crc64);
  93. }
  94. GRUB_MOD_FINI(crc64)
  95. {
  96. grub_md_unregister (&_gcry_digest_spec_crc64);
  97. }