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