Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/include/openssl/hmac.h

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #ifndef OPENSSL_HEADER_HMAC_H
5 #define OPENSSL_HEADER_HMAC_H
6
7 #include <openssl/base.h>
8
9 #include <openssl/digest.h>
10 #include <openssl/sha.h>
11 #include <openssl/md5.h>
12
13 #if defined(__cplusplus)
14 extern "C" {
15 #endif
16
17
18 // HMAC contains functions for constructing PRFs from Merkle–Damgård hash
19 // functions using HMAC.
20
21
22 // One-shot operation.
23
24 // HMAC calculates the HMAC of |data_len| bytes of |data|, using the given key
25 // and hash function, and writes the result to |out|. On entry, |out| must
26 // contain at least |EVP_MD_size| bytes of space. The actual length of the
27 // result is written to |*out_len|. An output size of |EVP_MAX_MD_SIZE| will
28 // always be large enough. It returns |out| or NULL on error.
29 OPENSSL_EXPORT uint8_t *HMAC(const EVP_MD *evp_md, const void *key,
30 size_t key_len, const uint8_t *data,
31 size_t data_len, uint8_t *out,
32 unsigned int *out_len);
33
34
35 // Incremental operation.
36
37 // HMAC_CTX_init initialises |ctx| for use in an HMAC operation. It's assumed
38 // that HMAC_CTX objects will be allocated on the stack thus no allocation
39 // function is provided.
40 OPENSSL_EXPORT void HMAC_CTX_init(HMAC_CTX *ctx);
41
42 // HMAC_CTX_new allocates and initialises a new |HMAC_CTX| and returns it, or
43 // NULL on allocation failure. The caller must use |HMAC_CTX_free| to release
44 // the resulting object.
45 OPENSSL_EXPORT HMAC_CTX *HMAC_CTX_new(void);
46
47 // HMAC_CTX_cleanup zeroises |ctx| since it's allocated on the stack.
48 // This brings the context to its initial state.
49 OPENSSL_EXPORT void HMAC_CTX_cleanup(HMAC_CTX *ctx);
50
51 // HMAC_CTX_cleanse calls |HMAC_CTX_cleanup|.
52 OPENSSL_EXPORT void HMAC_CTX_cleanse(HMAC_CTX *ctx);
53
54 // HMAC_CTX_free calls |HMAC_CTX_cleanup| and then frees |ctx| itself.
55 OPENSSL_EXPORT void HMAC_CTX_free(HMAC_CTX *ctx);
56
57 // HMAC_Init_ex sets up an initialised |HMAC_CTX| to use |md| as the hash
58 // function and |key| as the key. This function resets |HMAC_CTX| to a
59 // fresh state, even if |HMAC_Update| or |HMAC_Final| have been called
60 // previously. For a non-initial call, |md| may be NULL, in which case the
61 // previous hash function will be used. If the hash function has not changed and
62 // |key| is NULL, |ctx| reuses the previous key and resets to a clean state
63 // ready for new data. It returns one on success or zero on allocation failure.
64 //
65 // WARNING: NULL and empty keys are ambiguous on non-initial calls. Passing NULL
66 // |key| but repeating the previous |md| reuses the previous key rather than the
67 // empty key.
68 OPENSSL_EXPORT int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, size_t key_len,
69 const EVP_MD *md, ENGINE *impl);
70
71 // HMAC_Update hashes |data_len| bytes from |data| into the current HMAC
72 // operation in |ctx|. It returns one.
73 OPENSSL_EXPORT int HMAC_Update(HMAC_CTX *ctx, const uint8_t *data,
74 size_t data_len);
75
76 // HMAC_Final completes the HMAC operation in |ctx| and writes the result to
77 // |out| and then sets |*out_len| to the length of the result. On entry, |out|
78 // must contain at least |HMAC_size| bytes of space. An output size of
79 // |EVP_MAX_MD_SIZE| will always be large enough. It returns one on success or
80 // zero on allocation failure.
81 OPENSSL_EXPORT int HMAC_Final(HMAC_CTX *ctx, uint8_t *out,
82 unsigned int *out_len);
83
84
85 // Utility functions.
86
87 // HMAC_size returns the size, in bytes, of the HMAC that will be produced by
88 // |ctx|. On entry, |ctx| must have been setup with |HMAC_Init_ex|.
89 OPENSSL_EXPORT size_t HMAC_size(const HMAC_CTX *ctx);
90
91 // HMAC_CTX_get_md returns |ctx|'s hash function.
92 OPENSSL_EXPORT const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx);
93
94 // HMAC_CTX_copy_ex sets |dest| equal to |src|. On entry, |dest| must have been
95 // initialised by calling |HMAC_CTX_init|. It returns one on success and zero
96 // on error.
97 OPENSSL_EXPORT int HMAC_CTX_copy_ex(HMAC_CTX *dest, const HMAC_CTX *src);
98
99 // HMAC_CTX_reset calls |HMAC_CTX_cleanup| followed by |HMAC_CTX_init|.
100 OPENSSL_EXPORT void HMAC_CTX_reset(HMAC_CTX *ctx);
101
102
103 // Precomputed key functions
104
105 // HMAC_MD5_PRECOMPUTED_KEY_SIZE is the precomputed key size for MD5, in bytes
106 #define HMAC_MD5_PRECOMPUTED_KEY_SIZE 32
107 // HMAC_SHA1_PRECOMPUTED_KEY_SIZE is the precomputed key size for SHA1, in bytes
108 #define HMAC_SHA1_PRECOMPUTED_KEY_SIZE 40
109 // HMAC_SHA224_PRECOMPUTED_KEY_SIZE is the precomputed key size for SHA224, in bytes
110 #define HMAC_SHA224_PRECOMPUTED_KEY_SIZE 64
111 // HMAC_SHA256_PRECOMPUTED_KEY_SIZE is the precomputed key size for SHA256, in bytes
112 #define HMAC_SHA256_PRECOMPUTED_KEY_SIZE 64
113 // HMAC_SHA384_PRECOMPUTED_KEY_SIZE is the precomputed key size for SHA384, in bytes
114 #define HMAC_SHA384_PRECOMPUTED_KEY_SIZE 128
115 // HMAC_SHA512_PRECOMPUTED_KEY_SIZE is the precomputed key size for SHA512, in bytes
116 #define HMAC_SHA512_PRECOMPUTED_KEY_SIZE 128
117 // HMAC_SHA512_224_PRECOMPUTED_KEY_SIZE is the precomputed key size for SHA512_224, in bytes
118 #define HMAC_SHA512_224_PRECOMPUTED_KEY_SIZE 128
119 // HMAC_SHA512_256_PRECOMPUTED_KEY_SIZE is the precomputed key size for SHA512_256, in bytes
120 #define HMAC_SHA512_256_PRECOMPUTED_KEY_SIZE 128
121
122 // HMAC_MAX_PRECOMPUTED_KEY_SIZE is the largest precomputed key size, in bytes.
123 #define HMAC_MAX_PRECOMPUTED_KEY_SIZE (2 * (EVP_MAX_MD_CHAINING_LENGTH))
124
125 // HMAC_set_precomputed_key_export sets the context |ctx| to allow export of the
126 // precomputed key using HMAC_get_precomputed_key. On entry, HMAC_CTX must have
127 // been initialized via HMAC_Init_*, and neither HMAC_Update nor HMAC_Final
128 // must have been called after the last HMAC_Init_ex. It returns one on success
129 // and zero on error.
130 // After a successful call to HMAC_set_precomputed_key_export, HMAC_Update and
131 // HMAC_Final will fail.
132 //
133 // Note: The main reason for this function is to satisfy FIPS assertion AS09.16,
134 // since HMAC_get_precomputed_key returns key material (i.e., a CSP in NIST
135 // terminology).
136 OPENSSL_EXPORT int HMAC_set_precomputed_key_export(HMAC_CTX *ctx);
137
138 // HMAC_get_precomputed_key exports the precomputed key. If |out| is NULL,
139 // |out_len| is set to the size of the precomputed key. After such a call,
140 // |HMAC_get_precomputed_key| can directly be called again with a non-null
141 // |out|. But |HMAC_Update| and |HMAC_Final| will still fail.
142 //
143 // If |out| is not NULL, |*out_len| must contain the number of bytes of space
144 // available at |out|. If sufficient, the precomputed key will be written in
145 // |out| and |out_len| will be updated with the true length (which is
146 // |HMAC_xxx_PRECOMPUTED_KEY_SIZE| for hash function xxx). An output size of
147 // |HMAC_MAX_PRECOMPUTED_KEY_SIZE| will always be large enough. After a
148 // successful call to |HMAC_get_precomputed_key| with a non-NULL |out|, the
149 // context can be directly used for computing an HMAC using |HMAC_Update| and
150 // |HMAC_Final|.
151 //
152 // The function returns one on success and zero on error.
153 //
154 // The precomputed key is the concatenation:
155 // precomputed_key = key_ipad || key_opad
156 // where:
157 // key_ipad = Hash_Compression_Function(key' xor ipad)
158 // key_opad = Hash_Compression_Function(key' xor opad)
159 // key' = padding of key with 0 on the right to be of the block length
160 // if length of key is at most the block length
161 // or Hash(key)
162 // otherwise
163 //
164 // Knowledge of precomputed_key is sufficient to compute HMAC. Use of the
165 // precomputed key instead of the key reduces by 2 the number of hash
166 // compression function calls (or more if key is larger than the block length)
167 OPENSSL_EXPORT int HMAC_get_precomputed_key(HMAC_CTX *ctx, uint8_t *out,
168 size_t *out_len);
169
170 // HMAC_Init_from_precomputed_key sets up an initialised |HMAC_CTX| to use
171 // |md| as the hash function and |precomputed_key| as the precomputed key
172 // (see |HMAC_get_precomputed_key|).
173 // For a non-initial call, |md| may be NULL, in which case the previous hash
174 // function is used. If the hash function has not changed and |precomputed_key|
175 // is NULL, the previous key is used. This non-initial call is interchangeable
176 // with calling |HMAC_Init_ex| with the same parameters. It returns one on
177 // success or zero on failure.
178 //
179 // Note: Contrary to input keys to |HMAC_Init_ex|, which can be the empty key,
180 // an input precomputed key cannot be empty in an initial call to
181 // |HMAC_Init_from_precomputed_key|. Otherwise, the call fails and returns zero.
182 OPENSSL_EXPORT int HMAC_Init_from_precomputed_key(HMAC_CTX *ctx,
183 const uint8_t *precomputed_key,
184 size_t precompute_key_len,
185 const EVP_MD *md);
186
187
188 // Deprecated functions.
189
190 OPENSSL_EXPORT int HMAC_Init(HMAC_CTX *ctx, const void *key, int key_len,
191 const EVP_MD *md);
192
193 // HMAC_CTX_copy calls |HMAC_CTX_init| on |dest| and then sets it equal to
194 // |src|. On entry, |dest| must /not/ be initialised for an operation with
195 // |HMAC_Init_ex|. It returns one on success and zero on error.
196 OPENSSL_EXPORT int HMAC_CTX_copy(HMAC_CTX *dest, const HMAC_CTX *src);
197
198
199 // Private functions
200 typedef struct hmac_methods_st HmacMethods;
201
202 // We use a union to ensure that enough space is allocated and never actually
203 // bother with the named members. We do not externalize SHA3 ctx definition,
204 // so hard-code ctx size below and use a compile-time assertion where that ctx
205 // is defined to ensure it does not exceed size bounded by |md_ctx_union|. This
206 // is OK because union members are never referenced, they're only used for sizing.
207 union md_ctx_union {
208 MD5_CTX md5;
209 SHA_CTX sha1;
210 SHA256_CTX sha256;
211 SHA512_CTX sha512;
212 uint8_t sha3[400];
213 };
214
215 struct hmac_ctx_st {
216 const EVP_MD *md;
217 const HmacMethods *methods;
218 union md_ctx_union md_ctx;
219 union md_ctx_union i_ctx;
220 union md_ctx_union o_ctx;
221 int8_t state;
222 } /* HMAC_CTX */;
223
224
225 #if defined(__cplusplus)
226 } // extern C
227
228 #if !defined(BORINGSSL_NO_CXX)
229 extern "C++" {
230
231 BSSL_NAMESPACE_BEGIN
232
233 BORINGSSL_MAKE_DELETER(HMAC_CTX, HMAC_CTX_free)
234
235 using ScopedHMAC_CTX =
236 internal::StackAllocated<HMAC_CTX, void, HMAC_CTX_init, HMAC_CTX_cleanup>;
237
238 BSSL_NAMESPACE_END
239
240 } // extern C++
241 #endif
242
243 #endif
244
245
246 // Errors
247
248 #define HMAC_R_MISSING_PARAMETERS 100
249 #define HMAC_R_BUFFER_TOO_SMALL 102
250 #define HMAC_R_SET_PRECOMPUTED_KEY_EXPORT_NOT_CALLED 103
251 #define HMAC_R_NOT_CALLED_JUST_AFTER_INIT 104
252 #define HMAC_R_PRECOMPUTED_KEY_NOT_SUPPORTED_FOR_DIGEST 105
253 #define HMAC_R_UNSUPPORTED_DIGEST 106
254
255 #endif // OPENSSL_HEADER_HMAC_H