SHA256Fetch.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*====================================================================*
  2. *
  3. * void SHA256Fetch (struct sha256 * sha256, uint8_t digest []);
  4. *
  5. * SHA256.h
  6. *
  7. * read the SHA256 digest; this function works like function read()
  8. * but the function returns no value and the digest buffer is fixed
  9. * length;
  10. *
  11. * to start a digest, use function SHA256Reset(); to write data to
  12. * the digest use function SHA256Write();
  13. *
  14. * Read standard FIPS180-2 sec 5.3.2 for an explanation;
  15. *
  16. * Motley Tools by Charles Maier;
  17. * Copyright (c) 2001-2006 by Charles Maier Associates;
  18. * Licensed under the Internet Software Consortium License;
  19. *
  20. *--------------------------------------------------------------------*/
  21. #ifndef SHA256FETCH_SOURCE
  22. #define SHA256FETCH_SOURCE
  23. #include "../key/SHA256.h"
  24. static void encode (uint8_t memory [], uint32_t number)
  25. {
  26. *memory++ = (uint8_t)(number >> 24);
  27. *memory++ = (uint8_t)(number >> 16);
  28. *memory++ = (uint8_t)(number >> 8);
  29. *memory++ = (uint8_t)(number >> 0);
  30. return;
  31. }
  32. void SHA256Fetch (struct sha256 * sha256, uint8_t digest [])
  33. {
  34. unsigned word;
  35. uint8_t bits [8];
  36. uint32_t upper = (sha256->count [0] >> 29) | (sha256->count [1] << 3);
  37. uint32_t lower = (sha256->count [0] << 3);
  38. uint32_t final = (sha256->count [0] & 0x3F);
  39. uint32_t extra = (final < 56)? (56 - final): (120 - final);
  40. encode (&bits [0], upper);
  41. encode (&bits [4], lower);
  42. SHA256Write (sha256, sha256->extra, extra);
  43. SHA256Write (sha256, bits, sizeof (bits));
  44. for (word = 0; word < sizeof (sha256->state) / sizeof (uint32_t); word++)
  45. {
  46. encode (digest, sha256->state [word]);
  47. digest += sizeof (uint32_t);
  48. }
  49. memset (sha256, 0, sizeof (struct sha256));
  50. return;
  51. }
  52. #endif