aes_digest.c 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* libanode: the Anode C reference implementation
  2. * Copyright (C) 2009-2010 Adam Ierymenko <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "anode.h"
  17. #include "impl/aes.h"
  18. #include "impl/misc.h"
  19. #include "impl/types.h"
  20. void Anode_aes_digest(const void *const message,unsigned long message_len,void *const hash)
  21. {
  22. unsigned char previous_digest[16];
  23. unsigned char digest[16];
  24. unsigned char block[32];
  25. const unsigned char *in = (const unsigned char *)message;
  26. const unsigned char *end = in + message_len;
  27. unsigned long block_counter;
  28. AnodeAesExpandedKey expkey;
  29. ((uint64_t *)digest)[0] = 0ULL;
  30. ((uint64_t *)digest)[1] = 0ULL;
  31. ((uint64_t *)block)[0] = 0ULL;
  32. ((uint64_t *)block)[1] = 0ULL;
  33. ((uint64_t *)block)[2] = 0ULL;
  34. ((uint64_t *)block)[3] = 0ULL;
  35. /* Davis-Meyer hash function built from block cipher */
  36. block_counter = 0;
  37. while (in != end) {
  38. block[block_counter++] = *(in++);
  39. if (block_counter == 32) {
  40. block_counter = 0;
  41. ((uint64_t *)previous_digest)[0] = ((uint64_t *)digest)[0];
  42. ((uint64_t *)previous_digest)[1] = ((uint64_t *)digest)[1];
  43. Anode_aes256_expand_key(block,&expkey);
  44. Anode_aes256_encrypt(&expkey,digest,digest);
  45. ((uint64_t *)digest)[0] ^= ((uint64_t *)previous_digest)[0];
  46. ((uint64_t *)digest)[1] ^= ((uint64_t *)previous_digest)[1];
  47. }
  48. }
  49. /* Davis-Meyer end marker */
  50. block[block_counter++] = 0x80;
  51. while (block_counter != 32) block[block_counter++] = 0;
  52. ((uint64_t *)previous_digest)[0] = ((uint64_t *)digest)[0];
  53. ((uint64_t *)previous_digest)[1] = ((uint64_t *)digest)[1];
  54. Anode_aes256_expand_key(block,&expkey);
  55. Anode_aes256_encrypt(&expkey,digest,digest);
  56. ((uint64_t *)digest)[0] ^= ((uint64_t *)previous_digest)[0];
  57. ((uint64_t *)digest)[1] ^= ((uint64_t *)previous_digest)[1];
  58. /* Merkle-Damgård length padding */
  59. ((uint64_t *)block)[0] = 0ULL;
  60. if (sizeof(message_len) >= 8) { /* 32/64 bit? this will get optimized out */
  61. block[8] = (uint8_t)((uint64_t)message_len >> 56);
  62. block[9] = (uint8_t)((uint64_t)message_len >> 48);
  63. block[10] = (uint8_t)((uint64_t)message_len >> 40);
  64. block[11] = (uint8_t)((uint64_t)message_len >> 32);
  65. } else ((uint32_t *)block)[2] = 0;
  66. block[12] = (uint8_t)(message_len >> 24);
  67. block[13] = (uint8_t)(message_len >> 16);
  68. block[14] = (uint8_t)(message_len >> 8);
  69. block[15] = (uint8_t)message_len;
  70. ((uint64_t *)previous_digest)[0] = ((uint64_t *)digest)[0];
  71. ((uint64_t *)previous_digest)[1] = ((uint64_t *)digest)[1];
  72. Anode_aes256_expand_key(block,&expkey);
  73. Anode_aes256_encrypt(&expkey,digest,digest);
  74. ((uint64_t *)digest)[0] ^= ((uint64_t *)previous_digest)[0];
  75. ((uint64_t *)digest)[1] ^= ((uint64_t *)previous_digest)[1];
  76. ((uint64_t *)hash)[0] = ((uint64_t *)digest)[0];
  77. ((uint64_t *)hash)[1] = ((uint64_t *)digest)[1];
  78. }