md5.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _CRYPTO_MD5_H
  3. #define _CRYPTO_MD5_H
  4. #include <crypto/hash.h>
  5. #include <linux/types.h>
  6. #define MD5_DIGEST_SIZE 16
  7. #define MD5_HMAC_BLOCK_SIZE 64
  8. #define MD5_BLOCK_SIZE 64
  9. #define MD5_BLOCK_WORDS 16
  10. #define MD5_HASH_WORDS 4
  11. #define MD5_STATE_SIZE 24
  12. #define MD5_H0 0x67452301UL
  13. #define MD5_H1 0xefcdab89UL
  14. #define MD5_H2 0x98badcfeUL
  15. #define MD5_H3 0x10325476UL
  16. #define CRYPTO_MD5_STATESIZE \
  17. CRYPTO_HASH_STATESIZE(MD5_STATE_SIZE, MD5_HMAC_BLOCK_SIZE)
  18. extern const u8 md5_zero_message_hash[MD5_DIGEST_SIZE];
  19. struct md5_state {
  20. u32 hash[MD5_HASH_WORDS];
  21. u64 byte_count;
  22. u32 block[MD5_BLOCK_WORDS];
  23. };
  24. /* State for the MD5 compression function */
  25. struct md5_block_state {
  26. u32 h[MD5_HASH_WORDS];
  27. };
  28. /**
  29. * struct md5_ctx - Context for hashing a message with MD5
  30. * @state: the compression function state
  31. * @bytecount: number of bytes processed so far
  32. * @buf: partial block buffer; bytecount % MD5_BLOCK_SIZE bytes are valid
  33. */
  34. struct md5_ctx {
  35. struct md5_block_state state;
  36. u64 bytecount;
  37. u8 buf[MD5_BLOCK_SIZE] __aligned(__alignof__(__le64));
  38. };
  39. /**
  40. * md5_init() - Initialize an MD5 context for a new message
  41. * @ctx: the context to initialize
  42. *
  43. * If you don't need incremental computation, consider md5() instead.
  44. *
  45. * Context: Any context.
  46. */
  47. void md5_init(struct md5_ctx *ctx);
  48. /**
  49. * md5_update() - Update an MD5 context with message data
  50. * @ctx: the context to update; must have been initialized
  51. * @data: the message data
  52. * @len: the data length in bytes
  53. *
  54. * This can be called any number of times.
  55. *
  56. * Context: Any context.
  57. */
  58. void md5_update(struct md5_ctx *ctx, const u8 *data, size_t len);
  59. /**
  60. * md5_final() - Finish computing an MD5 message digest
  61. * @ctx: the context to finalize; must have been initialized
  62. * @out: (output) the resulting MD5 message digest
  63. *
  64. * After finishing, this zeroizes @ctx. So the caller does not need to do it.
  65. *
  66. * Context: Any context.
  67. */
  68. void md5_final(struct md5_ctx *ctx, u8 out[at_least MD5_DIGEST_SIZE]);
  69. /**
  70. * md5() - Compute MD5 message digest in one shot
  71. * @data: the message data
  72. * @len: the data length in bytes
  73. * @out: (output) the resulting MD5 message digest
  74. *
  75. * Context: Any context.
  76. */
  77. void md5(const u8 *data, size_t len, u8 out[at_least MD5_DIGEST_SIZE]);
  78. /**
  79. * struct hmac_md5_key - Prepared key for HMAC-MD5
  80. * @istate: private
  81. * @ostate: private
  82. */
  83. struct hmac_md5_key {
  84. struct md5_block_state istate;
  85. struct md5_block_state ostate;
  86. };
  87. /**
  88. * struct hmac_md5_ctx - Context for computing HMAC-MD5 of a message
  89. * @hash_ctx: private
  90. * @ostate: private
  91. */
  92. struct hmac_md5_ctx {
  93. struct md5_ctx hash_ctx;
  94. struct md5_block_state ostate;
  95. };
  96. /**
  97. * hmac_md5_preparekey() - Prepare a key for HMAC-MD5
  98. * @key: (output) the key structure to initialize
  99. * @raw_key: the raw HMAC-MD5 key
  100. * @raw_key_len: the key length in bytes. All key lengths are supported.
  101. *
  102. * Note: the caller is responsible for zeroizing both the struct hmac_md5_key
  103. * and the raw key once they are no longer needed.
  104. *
  105. * Context: Any context.
  106. */
  107. void hmac_md5_preparekey(struct hmac_md5_key *key,
  108. const u8 *raw_key, size_t raw_key_len);
  109. /**
  110. * hmac_md5_init() - Initialize an HMAC-MD5 context for a new message
  111. * @ctx: (output) the HMAC context to initialize
  112. * @key: the prepared HMAC key
  113. *
  114. * If you don't need incremental computation, consider hmac_md5() instead.
  115. *
  116. * Context: Any context.
  117. */
  118. void hmac_md5_init(struct hmac_md5_ctx *ctx, const struct hmac_md5_key *key);
  119. /**
  120. * hmac_md5_init_usingrawkey() - Initialize an HMAC-MD5 context for a new
  121. * message, using a raw key
  122. * @ctx: (output) the HMAC context to initialize
  123. * @raw_key: the raw HMAC-MD5 key
  124. * @raw_key_len: the key length in bytes. All key lengths are supported.
  125. *
  126. * If you don't need incremental computation, consider hmac_md5_usingrawkey()
  127. * instead.
  128. *
  129. * Context: Any context.
  130. */
  131. void hmac_md5_init_usingrawkey(struct hmac_md5_ctx *ctx,
  132. const u8 *raw_key, size_t raw_key_len);
  133. /**
  134. * hmac_md5_update() - Update an HMAC-MD5 context with message data
  135. * @ctx: the HMAC context to update; must have been initialized
  136. * @data: the message data
  137. * @data_len: the data length in bytes
  138. *
  139. * This can be called any number of times.
  140. *
  141. * Context: Any context.
  142. */
  143. static inline void hmac_md5_update(struct hmac_md5_ctx *ctx,
  144. const u8 *data, size_t data_len)
  145. {
  146. md5_update(&ctx->hash_ctx, data, data_len);
  147. }
  148. /**
  149. * hmac_md5_final() - Finish computing an HMAC-MD5 value
  150. * @ctx: the HMAC context to finalize; must have been initialized
  151. * @out: (output) the resulting HMAC-MD5 value
  152. *
  153. * After finishing, this zeroizes @ctx. So the caller does not need to do it.
  154. *
  155. * Context: Any context.
  156. */
  157. void hmac_md5_final(struct hmac_md5_ctx *ctx, u8 out[at_least MD5_DIGEST_SIZE]);
  158. /**
  159. * hmac_md5() - Compute HMAC-MD5 in one shot, using a prepared key
  160. * @key: the prepared HMAC key
  161. * @data: the message data
  162. * @data_len: the data length in bytes
  163. * @out: (output) the resulting HMAC-MD5 value
  164. *
  165. * If you're using the key only once, consider using hmac_md5_usingrawkey().
  166. *
  167. * Context: Any context.
  168. */
  169. void hmac_md5(const struct hmac_md5_key *key,
  170. const u8 *data, size_t data_len,
  171. u8 out[at_least MD5_DIGEST_SIZE]);
  172. /**
  173. * hmac_md5_usingrawkey() - Compute HMAC-MD5 in one shot, using a raw key
  174. * @raw_key: the raw HMAC-MD5 key
  175. * @raw_key_len: the key length in bytes. All key lengths are supported.
  176. * @data: the message data
  177. * @data_len: the data length in bytes
  178. * @out: (output) the resulting HMAC-MD5 value
  179. *
  180. * If you're using the key multiple times, prefer to use hmac_md5_preparekey()
  181. * followed by multiple calls to hmac_md5() instead.
  182. *
  183. * Context: Any context.
  184. */
  185. void hmac_md5_usingrawkey(const u8 *raw_key, size_t raw_key_len,
  186. const u8 *data, size_t data_len,
  187. u8 out[at_least MD5_DIGEST_SIZE]);
  188. #endif /* _CRYPTO_MD5_H */