xref: /freebsd/crypto/openssl/doc/man3/HMAC.pod (revision a7148ab39c03abd4d1a84997c70bf96f15dd2a09)
1e71b7053SJung-uk Kim=pod
2e71b7053SJung-uk Kim
3e71b7053SJung-uk Kim=head1 NAME
4e71b7053SJung-uk Kim
5e71b7053SJung-uk KimHMAC,
6e71b7053SJung-uk KimHMAC_CTX_new,
7e71b7053SJung-uk KimHMAC_CTX_reset,
8e71b7053SJung-uk KimHMAC_CTX_free,
9e71b7053SJung-uk KimHMAC_Init,
10e71b7053SJung-uk KimHMAC_Init_ex,
11e71b7053SJung-uk KimHMAC_Update,
12e71b7053SJung-uk KimHMAC_Final,
13e71b7053SJung-uk KimHMAC_CTX_copy,
14e71b7053SJung-uk KimHMAC_CTX_set_flags,
15e71b7053SJung-uk KimHMAC_CTX_get_md,
16e71b7053SJung-uk KimHMAC_size
17e71b7053SJung-uk Kim- HMAC message authentication code
18e71b7053SJung-uk Kim
19e71b7053SJung-uk Kim=head1 SYNOPSIS
20e71b7053SJung-uk Kim
21e71b7053SJung-uk Kim #include <openssl/hmac.h>
22e71b7053SJung-uk Kim
23b077aed3SPierre Pronchery unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,
24b077aed3SPierre Pronchery                     const unsigned char *data, size_t data_len,
25e71b7053SJung-uk Kim                     unsigned char *md, unsigned int *md_len);
26e71b7053SJung-uk Kim
27b077aed3SPierre ProncheryThe following functions have been deprecated since OpenSSL 3.0, and can be
28b077aed3SPierre Proncheryhidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
29b077aed3SPierre Proncherysee L<openssl_user_macros(7)>:
30b077aed3SPierre Pronchery
31e71b7053SJung-uk Kim HMAC_CTX *HMAC_CTX_new(void);
32e71b7053SJung-uk Kim int HMAC_CTX_reset(HMAC_CTX *ctx);
33e71b7053SJung-uk Kim
34e71b7053SJung-uk Kim int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int key_len,
35e71b7053SJung-uk Kim                  const EVP_MD *md, ENGINE *impl);
3658f35182SJung-uk Kim int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, size_t len);
37e71b7053SJung-uk Kim int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len);
38e71b7053SJung-uk Kim
39e71b7053SJung-uk Kim void HMAC_CTX_free(HMAC_CTX *ctx);
40e71b7053SJung-uk Kim
41e71b7053SJung-uk Kim int HMAC_CTX_copy(HMAC_CTX *dctx, HMAC_CTX *sctx);
42e71b7053SJung-uk Kim void HMAC_CTX_set_flags(HMAC_CTX *ctx, unsigned long flags);
43e71b7053SJung-uk Kim const EVP_MD *HMAC_CTX_get_md(const HMAC_CTX *ctx);
44e71b7053SJung-uk Kim
45e71b7053SJung-uk Kim size_t HMAC_size(const HMAC_CTX *e);
46e71b7053SJung-uk Kim
47b077aed3SPierre ProncheryThe following function has been deprecated since OpenSSL 1.1.0, and can be
48b077aed3SPierre Proncheryhidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
49b077aed3SPierre Proncherysee L<openssl_user_macros(7)>:
50e71b7053SJung-uk Kim
51e71b7053SJung-uk Kim int HMAC_Init(HMAC_CTX *ctx, const void *key, int key_len,
52e71b7053SJung-uk Kim               const EVP_MD *md);
53e71b7053SJung-uk Kim
54e71b7053SJung-uk Kim=head1 DESCRIPTION
55e71b7053SJung-uk Kim
56e71b7053SJung-uk KimHMAC is a MAC (message authentication code), i.e. a keyed hash
57e71b7053SJung-uk Kimfunction used for message authentication, which is based on a hash
58e71b7053SJung-uk Kimfunction.
59e71b7053SJung-uk Kim
60b077aed3SPierre ProncheryHMAC() computes the message authentication code of the I<data_len> bytes at
61b077aed3SPierre ProncheryI<data> using the hash function I<evp_md> and the key I<key> which is
62b077aed3SPierre ProncheryI<key_len> bytes long. The I<key> may also be NULL with I<key_len> being 0.
63e71b7053SJung-uk Kim
64b077aed3SPierre ProncheryIt places the result in I<md> (which must have space for the output of
65e71b7053SJung-uk Kimthe hash function, which is no more than B<EVP_MAX_MD_SIZE> bytes).
66b077aed3SPierre ProncheryIf I<md> is NULL, the digest is placed in a static array.  The size of
67b077aed3SPierre Proncherythe output is placed in I<md_len>, unless it is NULL. Note: passing a NULL
68b077aed3SPierre Proncheryvalue for I<md> to use the static array is not thread safe.
69e71b7053SJung-uk Kim
70b077aed3SPierre ProncheryI<evp_md> is a message digest such as EVP_sha1(), EVP_ripemd160() etc.
71b077aed3SPierre ProncheryHMAC does not support variable output length digests such as EVP_shake128() and
72610a21fdSJung-uk KimEVP_shake256().
73e71b7053SJung-uk Kim
74b077aed3SPierre ProncheryHMAC() uses the default B<OSSL_LIB_CTX>.
75b077aed3SPierre ProncheryUse L<EVP_Q_mac(3)> instead if a library context is required.
76b077aed3SPierre Pronchery
77b077aed3SPierre ProncheryAll of the functions described below are deprecated.
78b077aed3SPierre ProncheryApplications should instead use L<EVP_MAC_CTX_new(3)>, L<EVP_MAC_CTX_free(3)>,
79b077aed3SPierre ProncheryL<EVP_MAC_init(3)>, L<EVP_MAC_update(3)> and L<EVP_MAC_final(3)>
80b077aed3SPierre Proncheryor the 'quick' single-shot MAC function L<EVP_Q_mac(3)>.
81b077aed3SPierre Pronchery
82e71b7053SJung-uk KimHMAC_CTX_new() creates a new HMAC_CTX in heap memory.
83e71b7053SJung-uk Kim
84b077aed3SPierre ProncheryHMAC_CTX_reset() clears an existing B<HMAC_CTX> and associated
85e71b7053SJung-uk Kimresources, making it suitable for new computations as if it was newly
86e71b7053SJung-uk Kimcreated with HMAC_CTX_new().
87e71b7053SJung-uk Kim
88e71b7053SJung-uk KimHMAC_CTX_free() erases the key and other data from the B<HMAC_CTX>,
89e71b7053SJung-uk Kimreleases any associated resources and finally frees the B<HMAC_CTX>
90*a7148ab3SEnji Cooperitself. If the argument is NULL, nothing is done.
91e71b7053SJung-uk Kim
92e71b7053SJung-uk KimThe following functions may be used if the message is not completely
93e71b7053SJung-uk Kimstored in memory:
94e71b7053SJung-uk Kim
95e71b7053SJung-uk KimHMAC_Init_ex() initializes or reuses a B<HMAC_CTX> structure to use the hash
96b077aed3SPierre Proncheryfunction I<evp_md> and key I<key>. If both are NULL, or if I<key> is NULL
97b077aed3SPierre Proncheryand I<evp_md> is the same as the previous call, then the
98e71b7053SJung-uk Kimexisting key is
99b077aed3SPierre Proncheryreused. I<ctx> must have been created with HMAC_CTX_new() before the first use
100e71b7053SJung-uk Kimof an B<HMAC_CTX> in this function.
101e71b7053SJung-uk Kim
102b077aed3SPierre ProncheryIf HMAC_Init_ex() is called with I<key> NULL and I<evp_md> is not the
103b077aed3SPierre Proncherysame as the previous digest used by I<ctx> then an error is returned
104e71b7053SJung-uk Kimbecause reuse of an existing key with a different digest is not supported.
105e71b7053SJung-uk Kim
106e71b7053SJung-uk KimHMAC_Init() initializes a B<HMAC_CTX> structure to use the hash
107b077aed3SPierre Proncheryfunction I<evp_md> and the key I<key> which is I<key_len> bytes
108e71b7053SJung-uk Kimlong.
109e71b7053SJung-uk Kim
110e71b7053SJung-uk KimHMAC_Update() can be called repeatedly with chunks of the message to
111b077aed3SPierre Proncherybe authenticated (I<len> bytes at I<data>).
112e71b7053SJung-uk Kim
113b077aed3SPierre ProncheryHMAC_Final() places the message authentication code in I<md>, which
114e71b7053SJung-uk Kimmust have space for the hash function output.
115e71b7053SJung-uk Kim
116b077aed3SPierre ProncheryHMAC_CTX_copy() copies all of the internal state from I<sctx> into I<dctx>.
117e71b7053SJung-uk Kim
118e71b7053SJung-uk KimHMAC_CTX_set_flags() applies the specified flags to the internal EVP_MD_CTXs.
119e71b7053SJung-uk KimThese flags have the same meaning as for L<EVP_MD_CTX_set_flags(3)>.
120e71b7053SJung-uk Kim
121e71b7053SJung-uk KimHMAC_CTX_get_md() returns the EVP_MD that has previously been set for the
122e71b7053SJung-uk Kimsupplied HMAC_CTX.
123e71b7053SJung-uk Kim
124e71b7053SJung-uk KimHMAC_size() returns the length in bytes of the underlying hash function output.
125e71b7053SJung-uk Kim
126e71b7053SJung-uk Kim=head1 RETURN VALUES
127e71b7053SJung-uk Kim
128e71b7053SJung-uk KimHMAC() returns a pointer to the message authentication code or NULL if
129e71b7053SJung-uk Kiman error occurred.
130e71b7053SJung-uk Kim
131e71b7053SJung-uk KimHMAC_CTX_new() returns a pointer to a new B<HMAC_CTX> on success or
132b077aed3SPierre ProncheryNULL if an error occurred.
133e71b7053SJung-uk Kim
134e71b7053SJung-uk KimHMAC_CTX_reset(), HMAC_Init_ex(), HMAC_Update(), HMAC_Final() and
135e71b7053SJung-uk KimHMAC_CTX_copy() return 1 for success or 0 if an error occurred.
136e71b7053SJung-uk Kim
137e71b7053SJung-uk KimHMAC_CTX_get_md() return the EVP_MD previously set for the supplied HMAC_CTX or
138e71b7053SJung-uk KimNULL if no EVP_MD has been set.
139e71b7053SJung-uk Kim
140e71b7053SJung-uk KimHMAC_size() returns the length in bytes of the underlying hash function output
141e71b7053SJung-uk Kimor zero on error.
142e71b7053SJung-uk Kim
143e71b7053SJung-uk Kim=head1 CONFORMING TO
144e71b7053SJung-uk Kim
145e71b7053SJung-uk KimRFC 2104
146e71b7053SJung-uk Kim
147e71b7053SJung-uk Kim=head1 SEE ALSO
148e71b7053SJung-uk Kim
149b077aed3SPierre ProncheryL<SHA1(3)>, EVP_Q_mac(3), L<evp(7)>
150e71b7053SJung-uk Kim
151e71b7053SJung-uk Kim=head1 HISTORY
152e71b7053SJung-uk Kim
153b077aed3SPierre ProncheryAll functions except for HMAC() were deprecated in OpenSSL 3.0.
154b077aed3SPierre Pronchery
155e71b7053SJung-uk KimHMAC_CTX_init() was replaced with HMAC_CTX_reset() in OpenSSL 1.1.0.
156e71b7053SJung-uk Kim
157e71b7053SJung-uk KimHMAC_CTX_cleanup() existed in OpenSSL before version 1.1.0.
158e71b7053SJung-uk Kim
159e71b7053SJung-uk KimHMAC_CTX_new(), HMAC_CTX_free() and HMAC_CTX_get_md() are new in OpenSSL 1.1.0.
160e71b7053SJung-uk Kim
161e71b7053SJung-uk KimHMAC_Init_ex(), HMAC_Update() and HMAC_Final() did not return values in
162e71b7053SJung-uk KimOpenSSL before version 1.0.0.
163e71b7053SJung-uk Kim
164e71b7053SJung-uk Kim=head1 COPYRIGHT
165e71b7053SJung-uk Kim
166*a7148ab3SEnji CooperCopyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.
167e71b7053SJung-uk Kim
168b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License").  You may not use
169e71b7053SJung-uk Kimthis file except in compliance with the License.  You can obtain a copy
170e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at
171e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>.
172e71b7053SJung-uk Kim
173e71b7053SJung-uk Kim=cut
174