digests.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 sint32_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. * Commence calculating a HMAC signature.
  62. *
  63. * Once this fuction returns successfully, the @c ctx may be updated with
  64. * data in multiple passes uses @c mi->update(), before being finalized via
  65. * @c crypto_hmac_finalize().
  66. *
  67. * @param ctx A created context for the given mech @c mi.
  68. * @param mi A mech from @c crypto_digest_mech().
  69. * @param key The key bytes to use.
  70. * @param key_len Number of bytes the @c key comprises.
  71. * @param k_opad Buffer of size @c mi->block_size bytes to store the second
  72. * round of key material in.
  73. * Must be passed to @c crypto_hmac_finalize().
  74. */
  75. void crypto_hmac_begin (void *ctx, const digest_mech_info_t *mi, const char *key, size_t key_len, uint8_t *k_opad);
  76. /**
  77. * Finalizes calculating a HMAC signature.
  78. * @param ctx The created context used with @c crypto_hmac_begin().
  79. * @param mi The mech used with @c crypto_hmac_begin().
  80. * @param k_opad Second round of keying material, obtained
  81. * from @c crypto_hmac_begin().
  82. * @param digest Output buffer, must be at least @c mi->digest_size in size.
  83. */
  84. void crypto_hmac_finalize (void *ctx, const digest_mech_info_t *mi, const uint8_t *k_opad, uint8_t *digest);
  85. /**
  86. * Generate a HMAC signature in one pass.
  87. * Implemented in terms of @c crypto_hmac_begin() / @c crypto_hmac_end().
  88. * @param mi A mech from @c crypto_digest_mech(). A null pointer @c mi
  89. * is harmless, but will of course result in an error return.
  90. * @param data The data to generate a signature for.
  91. * @param data_len Number of bytes at @c data to process.
  92. * @param key The key to use.
  93. * @param key_len Number of bytes the @c key comprises.
  94. * @param digest Output buffer, must be at least @c mi->digest_size in size.
  95. * @return 0 on success, non-zero on error.
  96. */
  97. 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);
  98. /**
  99. * Perform ASCII Hex encoding. Does not null-terminate the buffer.
  100. *
  101. * @param bin The buffer to ascii-hex encode.
  102. * @param bin_len Number of bytes in @c bin to encode.
  103. * @param outbuf Output buffer, must be at least @c bin_len*2 bytes in size.
  104. * Note that in-place encoding is supported, and as such
  105. * bin==outbuf is safe, provided the buffer is large enough.
  106. */
  107. void crypto_encode_asciihex (const char *bin, size_t bin_len, char *outbuf);
  108. /** Text string "0123456789abcdef" */
  109. const char crypto_hexbytes[17];
  110. #endif