hmac.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 HMAC_H
  15. #define HMAC_H
  16. #include <stddef.h>
  17. #include <stdint.h>
  18. #include "chash.h"
  19. /**
  20. * HMAC
  21. * ====
  22. * This is a one-shot and incremental interface to computing
  23. * HMAC with any hash function.
  24. *
  25. * (Note: HMAC with SHA3 is possible, but is probably not a
  26. * sensible thing to want.)
  27. */
  28. /* .. c:type:: cf_hmac_ctx
  29. * HMAC incremental interface context.
  30. *
  31. * .. c:member:: cf_hmac_ctx.hash
  32. * Hash function description.
  33. *
  34. * .. c:member:: cf_hmac_ctx.inner
  35. * Inner hash computation.
  36. *
  37. * .. c:member:: cf_hmac_ctx.outer
  38. * Outer hash computation.
  39. */
  40. typedef struct
  41. {
  42. const cf_chash *hash;
  43. cf_chash_ctx inner;
  44. cf_chash_ctx outer;
  45. } cf_hmac_ctx;
  46. /* .. c:function:: $DECL
  47. * Set up ctx for computing a HMAC using the given hash and key. */
  48. void cf_hmac_init(cf_hmac_ctx *ctx,
  49. const cf_chash *hash,
  50. const uint8_t *key, size_t nkey);
  51. /* .. c:function:: $DECL
  52. * Input data. */
  53. void cf_hmac_update(cf_hmac_ctx *ctx,
  54. const void *data, size_t ndata);
  55. /* .. c:function:: $DECL
  56. * Finish and compute HMAC.
  57. * `ctx->hash->hashsz` bytes are written to `out`. */
  58. void cf_hmac_finish(cf_hmac_ctx *ctx, uint8_t *out);
  59. /* .. c:function:: $DECL
  60. * One shot interface: compute `HMAC_hash(key, msg)`, writing the
  61. * answer (which is `hash->hashsz` long) to `out`.
  62. *
  63. * This function does not fail. */
  64. void cf_hmac(const uint8_t *key, size_t nkey,
  65. const uint8_t *msg, size_t nmsg,
  66. uint8_t *out,
  67. const cf_chash *hash);
  68. #endif