chash.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 CHASH_H
  15. #define CHASH_H
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. /**
  19. * General hash function description
  20. * =================================
  21. * This allows us to make use of hash functions without depending
  22. * on a specific one. This is useful in implementing, for example,
  23. * :doc:`HMAC <hmac>`.
  24. */
  25. /* .. c:type:: cf_chash_init
  26. * Hashing initialisation function type.
  27. *
  28. * Functions of this type should initialise the context in preparation
  29. * for hashing a message with `cf_chash_update` functions.
  30. *
  31. * :rtype: void
  32. * :param ctx: hash function-specific context structure.
  33. */
  34. typedef void (*cf_chash_init)(void *ctx);
  35. /* .. c:type:: cf_chash_update
  36. * Hashing data processing function type.
  37. *
  38. * Functions of this type hash `count` bytes of data at `data`,
  39. * updating the contents of `ctx`.
  40. *
  41. * :rtype: void
  42. * :param ctx: hash function-specific context structure.
  43. * :param data: input data to hash.
  44. * :param count: number of bytes to hash.
  45. */
  46. typedef void (*cf_chash_update)(void *ctx, const void *data, size_t count);
  47. /* .. c:type:: cf_chash_digest
  48. * Hashing completion function type.
  49. *
  50. * Functions of this type complete a hashing operation,
  51. * writing :c:member:`cf_chash.hashsz` bytes to `hash`.
  52. *
  53. * This function does not change `ctx` -- any padding which needs doing
  54. * must be done seperately (in a copy of `ctx`, say).
  55. *
  56. * This means you can interlave `_update` and `_digest` calls to
  57. * learn `H(A)` and `H(A || B)` without hashing `A` twice.
  58. *
  59. * :rtype: void
  60. * :param ctx: hash function-specific context structure.
  61. * :param hash: location to write hash result.
  62. */
  63. typedef void (*cf_chash_digest)(const void *ctx, uint8_t *hash);
  64. /* .. c:type:: cf_chash
  65. * This type describes an incremental hash function in an abstract way.
  66. *
  67. * .. c:member:: cf_chash.hashsz
  68. * The hash function's output, in bytes.
  69. *
  70. * .. c:member:: cf_chash.blocksz
  71. * The hash function's internal block size, in bytes.
  72. *
  73. * .. c:member:: cf_chash.init
  74. * Context initialisation function.
  75. *
  76. * .. c:member:: cf_chash:update
  77. * Data processing function.
  78. *
  79. * .. c:member:: cf_chash:digest
  80. * Completion function.
  81. *
  82. */
  83. typedef struct
  84. {
  85. size_t hashsz;
  86. size_t blocksz;
  87. cf_chash_init init;
  88. cf_chash_update update;
  89. cf_chash_digest digest;
  90. } cf_chash;
  91. /* .. c:macro:: CF_CHASH_MAXCTX
  92. * The maximum size of a :c:type:`cf_chash_ctx`. This allows
  93. * use to put a structure in automatic storage that can
  94. * store working data for any supported hash function. */
  95. #define CF_CHASH_MAXCTX 360
  96. /* .. c:macro:: CF_CHASH_MAXBLK
  97. * Maximum hash function block size (in bytes). */
  98. #define CF_CHASH_MAXBLK 128
  99. /* .. c:macro:: CF_MAXHASH
  100. * Maximum hash function output (in bytes). */
  101. #define CF_MAXHASH 64
  102. /* .. c:type:: cf_chash_ctx
  103. * A type usable with any `cf_chash` as a context. */
  104. typedef union
  105. {
  106. uint8_t ctx[CF_CHASH_MAXCTX];
  107. uint16_t u16;
  108. uint32_t u32;
  109. uint64_t u64;
  110. } cf_chash_ctx;
  111. /* .. c:function:: $DECL
  112. * One shot hashing: `out = h(m)`.
  113. *
  114. * Using the hash function `h`, `nm` bytes at `m` are hashed and `h->hashsz` bytes
  115. * of result is written to the buffer `out`.
  116. *
  117. * :param h: hash function description.
  118. * :param m: message buffer.
  119. * :param nm: message length.
  120. * :param out: hash result buffer (written).
  121. */
  122. void cf_hash(const cf_chash *h, const void *m, size_t nm, uint8_t *out);
  123. #endif