/* SPDX-License-Identifier: GPL-2.0 */1#ifndef _CRYPTO_MD5_H2#define _CRYPTO_MD5_H34#include <crypto/hash.h>5#include <linux/types.h>67#define MD5_DIGEST_SIZE 168#define MD5_HMAC_BLOCK_SIZE 649#define MD5_BLOCK_SIZE 6410#define MD5_BLOCK_WORDS 1611#define MD5_HASH_WORDS 412#define MD5_STATE_SIZE 241314#define MD5_H0 0x67452301UL15#define MD5_H1 0xefcdab89UL16#define MD5_H2 0x98badcfeUL17#define MD5_H3 0x10325476UL1819#define CRYPTO_MD5_STATESIZE \20CRYPTO_HASH_STATESIZE(MD5_STATE_SIZE, MD5_HMAC_BLOCK_SIZE)2122extern const u8 md5_zero_message_hash[MD5_DIGEST_SIZE];2324struct md5_state {25u32 hash[MD5_HASH_WORDS];26u64 byte_count;27u32 block[MD5_BLOCK_WORDS];28};2930/* State for the MD5 compression function */31struct md5_block_state {32u32 h[MD5_HASH_WORDS];33};3435/**36* struct md5_ctx - Context for hashing a message with MD537* @state: the compression function state38* @bytecount: number of bytes processed so far39* @buf: partial block buffer; bytecount % MD5_BLOCK_SIZE bytes are valid40*/41struct md5_ctx {42struct md5_block_state state;43u64 bytecount;44u8 buf[MD5_BLOCK_SIZE] __aligned(__alignof__(__le64));45};4647/**48* md5_init() - Initialize an MD5 context for a new message49* @ctx: the context to initialize50*51* If you don't need incremental computation, consider md5() instead.52*53* Context: Any context.54*/55void md5_init(struct md5_ctx *ctx);5657/**58* md5_update() - Update an MD5 context with message data59* @ctx: the context to update; must have been initialized60* @data: the message data61* @len: the data length in bytes62*63* This can be called any number of times.64*65* Context: Any context.66*/67void md5_update(struct md5_ctx *ctx, const u8 *data, size_t len);6869/**70* md5_final() - Finish computing an MD5 message digest71* @ctx: the context to finalize; must have been initialized72* @out: (output) the resulting MD5 message digest73*74* After finishing, this zeroizes @ctx. So the caller does not need to do it.75*76* Context: Any context.77*/78void md5_final(struct md5_ctx *ctx, u8 out[MD5_DIGEST_SIZE]);7980/**81* md5() - Compute MD5 message digest in one shot82* @data: the message data83* @len: the data length in bytes84* @out: (output) the resulting MD5 message digest85*86* Context: Any context.87*/88void md5(const u8 *data, size_t len, u8 out[MD5_DIGEST_SIZE]);8990/**91* struct hmac_md5_key - Prepared key for HMAC-MD592* @istate: private93* @ostate: private94*/95struct hmac_md5_key {96struct md5_block_state istate;97struct md5_block_state ostate;98};99100/**101* struct hmac_md5_ctx - Context for computing HMAC-MD5 of a message102* @hash_ctx: private103* @ostate: private104*/105struct hmac_md5_ctx {106struct md5_ctx hash_ctx;107struct md5_block_state ostate;108};109110/**111* hmac_md5_preparekey() - Prepare a key for HMAC-MD5112* @key: (output) the key structure to initialize113* @raw_key: the raw HMAC-MD5 key114* @raw_key_len: the key length in bytes. All key lengths are supported.115*116* Note: the caller is responsible for zeroizing both the struct hmac_md5_key117* and the raw key once they are no longer needed.118*119* Context: Any context.120*/121void hmac_md5_preparekey(struct hmac_md5_key *key,122const u8 *raw_key, size_t raw_key_len);123124/**125* hmac_md5_init() - Initialize an HMAC-MD5 context for a new message126* @ctx: (output) the HMAC context to initialize127* @key: the prepared HMAC key128*129* If you don't need incremental computation, consider hmac_md5() instead.130*131* Context: Any context.132*/133void hmac_md5_init(struct hmac_md5_ctx *ctx, const struct hmac_md5_key *key);134135/**136* hmac_md5_init_usingrawkey() - Initialize an HMAC-MD5 context for a new137* message, using a raw key138* @ctx: (output) the HMAC context to initialize139* @raw_key: the raw HMAC-MD5 key140* @raw_key_len: the key length in bytes. All key lengths are supported.141*142* If you don't need incremental computation, consider hmac_md5_usingrawkey()143* instead.144*145* Context: Any context.146*/147void hmac_md5_init_usingrawkey(struct hmac_md5_ctx *ctx,148const u8 *raw_key, size_t raw_key_len);149150/**151* hmac_md5_update() - Update an HMAC-MD5 context with message data152* @ctx: the HMAC context to update; must have been initialized153* @data: the message data154* @data_len: the data length in bytes155*156* This can be called any number of times.157*158* Context: Any context.159*/160static inline void hmac_md5_update(struct hmac_md5_ctx *ctx,161const u8 *data, size_t data_len)162{163md5_update(&ctx->hash_ctx, data, data_len);164}165166/**167* hmac_md5_final() - Finish computing an HMAC-MD5 value168* @ctx: the HMAC context to finalize; must have been initialized169* @out: (output) the resulting HMAC-MD5 value170*171* After finishing, this zeroizes @ctx. So the caller does not need to do it.172*173* Context: Any context.174*/175void hmac_md5_final(struct hmac_md5_ctx *ctx, u8 out[MD5_DIGEST_SIZE]);176177/**178* hmac_md5() - Compute HMAC-MD5 in one shot, using a prepared key179* @key: the prepared HMAC key180* @data: the message data181* @data_len: the data length in bytes182* @out: (output) the resulting HMAC-MD5 value183*184* If you're using the key only once, consider using hmac_md5_usingrawkey().185*186* Context: Any context.187*/188void hmac_md5(const struct hmac_md5_key *key,189const u8 *data, size_t data_len, u8 out[MD5_DIGEST_SIZE]);190191/**192* hmac_md5_usingrawkey() - Compute HMAC-MD5 in one shot, using a raw key193* @raw_key: the raw HMAC-MD5 key194* @raw_key_len: the key length in bytes. All key lengths are supported.195* @data: the message data196* @data_len: the data length in bytes197* @out: (output) the resulting HMAC-MD5 value198*199* If you're using the key multiple times, prefer to use hmac_md5_preparekey()200* followed by multiple calls to hmac_md5() instead.201*202* Context: Any context.203*/204void hmac_md5_usingrawkey(const u8 *raw_key, size_t raw_key_len,205const u8 *data, size_t data_len,206u8 out[MD5_DIGEST_SIZE]);207208#endif /* _CRYPTO_MD5_H */209210211