projects/linux·blob master · refs

228 lines · 6.3 KiB

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