digests.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef _CRYPTO_DIGESTS_H_
  2. #define _CRYPTO_DIGESTS_H_
  3. #include <c_types.h>
  4. typedef void (*create_ctx_fn)(void *ctx);
  5. typedef void (*update_ctx_fn)(void *ctx, const uint8_t *msg, int len);
  6. typedef void (*finalize_ctx_fn)(uint8_t *digest, void *ctx);
  7. typedef size_t ( *read_fn )(int fd, void *ptr, size_t len);
  8. /**
  9. * Description of a message digest mechanism.
  10. *
  11. * Typical usage (if not using the crypto_xxxx() functions below):
  12. * digest_mech_info_t *mi = crypto_digest_mech (chosen_algorithm);
  13. * void *ctx = os_malloc (mi->ctx_size);
  14. * mi->create (ctx);
  15. * mi->update (ctx, data, len);
  16. * ...
  17. * uint8_t *digest = os_malloc (mi->digest_size);
  18. * mi->finalize (digest, ctx);
  19. * ...
  20. * os_free (ctx);
  21. * os_free (digest);
  22. */
  23. typedef struct
  24. {
  25. /* Note: All entries are 32bit to enable placement using ICACHE_RODATA_ATTR.*/
  26. const char * name;
  27. create_ctx_fn create;
  28. update_ctx_fn update;
  29. finalize_ctx_fn finalize;
  30. uint32_t ctx_size;
  31. uint32_t digest_size;
  32. uint32_t block_size;
  33. } digest_mech_info_t;
  34. /**
  35. * Looks up the mech data for a specified digest algorithm.
  36. * @param mech The name of the algorithm, e.g. "MD5", "SHA256"
  37. * @returns The mech data, or null if the mech is unknown.
  38. */
  39. const digest_mech_info_t *crypto_digest_mech (const char *mech);
  40. /**
  41. * Wrapper function for performing a one-in-all hashing operation.
  42. * @param mi A mech from @c crypto_digest_mech(). A null pointer @c mi
  43. * is harmless, but will of course result in an error return.
  44. * @param data The data to create a digest for.
  45. * @param data_len Number of bytes at @c data to digest.
  46. * @param digest Output buffer, must be at least @c mi->digest_size in size.
  47. * @return 0 on success, non-zero on error.
  48. */
  49. int crypto_hash (const digest_mech_info_t *mi, const char *data, size_t data_len, uint8_t *digest);
  50. /**
  51. * Wrapper function for performing a one-in-all hashing operation of a file.
  52. * @param mi A mech from @c crypto_digest_mech(). A null pointer @c mi
  53. * is harmless, but will of course result in an error return.
  54. * @param read Pointer to the read function (e.g. fs_read)
  55. * @param readarg Argument to pass to the read function (e.g. file descriptor)
  56. * @param digest Output buffer, must be at least @c mi->digest_size in size.
  57. * @return 0 on success, non-zero on error.
  58. */
  59. int crypto_fhash (const digest_mech_info_t *mi, read_fn read, int readarg, uint8_t *digest);
  60. /**
  61. * Generate a HMAC signature.
  62. * @param mi A mech from @c crypto_digest_mech(). A null pointer @c mi
  63. * is harmless, but will of course result in an error return.
  64. * @param data The data to generate a signature for.
  65. * @param data_len Number of bytes at @c data to process.
  66. * @param key The key to use.
  67. * @param key_len Number of bytes the @c key comprises.
  68. * @param digest Output buffer, must be at least @c mi->digest_size in size.
  69. * @return 0 on success, non-zero on error.
  70. */
  71. int crypto_hmac (const digest_mech_info_t *mi, const char *data, size_t data_len, const char *key, size_t key_len, uint8_t *digest);
  72. /**
  73. * Perform ASCII Hex encoding. Does not null-terminate the buffer.
  74. *
  75. * @param bin The buffer to ascii-hex encode.
  76. * @param bin_len Number of bytes in @c bin to encode.
  77. * @param outbuf Output buffer, must be at least @c bin_len*2 bytes in size.
  78. * Note that in-place encoding is supported, and as such
  79. * bin==outbuf is safe, provided the buffer is large enough.
  80. */
  81. void crypto_encode_asciihex (const char *bin, size_t bin_len, char *outbuf);
  82. /** Text string "0123456789abcdef" */
  83. const char crypto_hexbytes[17];
  84. #endif