sha1.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #ifndef SHA1_H
  15. #define SHA1_H
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include "chash.h"
  19. /**
  20. * SHA1
  21. * ====
  22. *
  23. * You shouldn't use this for anything new.
  24. */
  25. /* .. c:macro:: CF_SHA1_HASHSZ
  26. * The output size of SHA1: 20 bytes. */
  27. #define CF_SHA1_HASHSZ 20
  28. /* .. c:macro:: CF_SHA1_BLOCKSZ
  29. * The block size of SHA1: 64 bytes. */
  30. #define CF_SHA1_BLOCKSZ 64
  31. /* .. c:type:: cf_sha1_context
  32. * Incremental SHA1 hashing context.
  33. *
  34. * .. c:member:: cf_sha1_context.H
  35. * Intermediate values.
  36. *
  37. * .. c:member:: cf_sha1_context.partial
  38. * Unprocessed input.
  39. *
  40. * .. c:member:: cf_sha1_context.npartial
  41. * Number of bytes of unprocessed input.
  42. *
  43. * .. c:member:: cf_sha1_context.blocks
  44. * Number of full blocks processed.
  45. */
  46. typedef struct
  47. {
  48. uint32_t H[5]; /* State. */
  49. uint8_t partial[CF_SHA1_BLOCKSZ]; /* Partial block of input. */
  50. uint32_t blocks; /* Number of full blocks processed into H. */
  51. size_t npartial; /* Number of bytes in prefix of partial. */
  52. } cf_sha1_context;
  53. /* .. c:function:: $DECL
  54. * Sets up `ctx` ready to hash a new message.
  55. */
  56. extern void cf_sha1_init(cf_sha1_context *ctx);
  57. /* .. c:function:: $DECL
  58. * Hashes `nbytes` at `data`. Copies the data if there isn't enough to make
  59. * a full block.
  60. */
  61. extern void cf_sha1_update(cf_sha1_context *ctx, const void *data, size_t nbytes);
  62. /* .. c:function:: $DECL
  63. * Finishes the hash operation, writing `CF_SHA1_HASHSZ` bytes to `hash`.
  64. *
  65. * This leaves `ctx` unchanged.
  66. */
  67. extern void cf_sha1_digest(const cf_sha1_context *ctx, uint8_t hash[CF_SHA1_HASHSZ]);
  68. /* .. c:function:: $DECL
  69. * Finishes the hash operation, writing `CF_SHA1_HASHSZ` bytes to `hash`.
  70. *
  71. * This destroys `ctx`, but uses less stack than :c:func:`cf_sha1_digest`.
  72. */
  73. extern void cf_sha1_digest_final(cf_sha1_context *ctx, uint8_t hash[CF_SHA1_HASHSZ]);
  74. /* .. c:var:: cf_sha1
  75. * Abstract interface to SHA1. See :c:type:`cf_chash` for more information.
  76. */
  77. extern const cf_chash cf_sha1;
  78. #endif