xref: /openbsd-src/lib/libcrypto/man/HMAC.3 (revision c1a45aed656e7d5627c30c92421893a76f370ccb)
1.\" $OpenBSD: HMAC.3,v 1.20 2022/01/25 17:55:39 tb Exp $
2.\" full merge up to: OpenSSL crypto/hmac a528d4f0 Oct 27 13:40:11 2015 -0400
3.\" selective merge up to: OpenSSL man3/HMAC b3696a55 Sep 2 09:35:50 2017 -0400
4.\"
5.\" This file was written by Ulf Moeller <ulf@openssl.org>,
6.\" Richard Levitte <levitte@openssl.org>, and
7.\" Matt Caswell <matt@openssl.org>.
8.\" Copyright (c) 2000-2002, 2006, 2008, 2009, 2013, 2015, 2016
9.\" The OpenSSL Project.  All rights reserved.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
14.\"
15.\" 1. Redistributions of source code must retain the above copyright
16.\"    notice, this list of conditions and the following disclaimer.
17.\"
18.\" 2. Redistributions in binary form must reproduce the above copyright
19.\"    notice, this list of conditions and the following disclaimer in
20.\"    the documentation and/or other materials provided with the
21.\"    distribution.
22.\"
23.\" 3. All advertising materials mentioning features or use of this
24.\"    software must display the following acknowledgment:
25.\"    "This product includes software developed by the OpenSSL Project
26.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
27.\"
28.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
29.\"    endorse or promote products derived from this software without
30.\"    prior written permission. For written permission, please contact
31.\"    openssl-core@openssl.org.
32.\"
33.\" 5. Products derived from this software may not be called "OpenSSL"
34.\"    nor may "OpenSSL" appear in their names without prior written
35.\"    permission of the OpenSSL Project.
36.\"
37.\" 6. Redistributions of any form whatsoever must retain the following
38.\"    acknowledgment:
39.\"    "This product includes software developed by the OpenSSL Project
40.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
41.\"
42.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
43.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
46.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53.\" OF THE POSSIBILITY OF SUCH DAMAGE.
54.\"
55.Dd $Mdocdate: January 25 2022 $
56.Dt HMAC 3
57.Os
58.Sh NAME
59.Nm HMAC ,
60.Nm HMAC_CTX_new ,
61.Nm HMAC_CTX_reset ,
62.Nm HMAC_CTX_free ,
63.Nm HMAC_Init_ex ,
64.Nm HMAC_Init ,
65.Nm HMAC_Update ,
66.Nm HMAC_Final ,
67.Nm HMAC_CTX_copy ,
68.Nm HMAC_CTX_set_flags ,
69.Nm HMAC_CTX_get_md ,
70.Nm HMAC_size
71.Nd HMAC message authentication code
72.Sh SYNOPSIS
73.In openssl/hmac.h
74.Ft unsigned char *
75.Fo HMAC
76.Fa "const EVP_MD *evp_md"
77.Fa "const void *key"
78.Fa "int key_len"
79.Fa "const unsigned char *d"
80.Fa "size_t n"
81.Fa "unsigned char *md"
82.Fa "unsigned int *md_len"
83.Fc
84.Ft HMAC_CTX *
85.Fn HMAC_CTX_new void
86.Ft int
87.Fo HMAC_CTX_reset
88.Fa "HMAC_CTX *ctx"
89.Fc
90.Ft void
91.Fo HMAC_CTX_free
92.Fa "HMAC_CTX *ctx"
93.Fc
94.Ft int
95.Fo HMAC_Init_ex
96.Fa "HMAC_CTX *ctx"
97.Fa "const void *key"
98.Fa "int key_len"
99.Fa "const EVP_MD *md"
100.Fa "ENGINE *impl"
101.Fc
102.Ft int
103.Fo HMAC_Init
104.Fa "HMAC_CTX *ctx"
105.Fa "const void *key"
106.Fa "int key_len"
107.Fa "const EVP_MD *md"
108.Fc
109.Ft int
110.Fo HMAC_Update
111.Fa "HMAC_CTX *ctx"
112.Fa "const unsigned char *data"
113.Fa "size_t len"
114.Fc
115.Ft int
116.Fo HMAC_Final
117.Fa "HMAC_CTX *ctx"
118.Fa "unsigned char *md"
119.Fa "unsigned int *len"
120.Fc
121.Ft int
122.Fo HMAC_CTX_copy
123.Fa "HMAC_CTX *dctx"
124.Fa "HMAC_CTX *sctx"
125.Fc
126.Ft void
127.Fo HMAC_CTX_set_flags
128.Fa "HMAC_CTX *ctx"
129.Fa "unsigned long flags"
130.Fc
131.Ft const EVP_MD *
132.Fo HMAC_CTX_get_md
133.Fa "const HMAC_CTX *ctx"
134.Fc
135.Ft size_t
136.Fo HMAC_size
137.Fa "const HMAC_CTX *e"
138.Fc
139.Sh DESCRIPTION
140HMAC is a MAC (message authentication code), i.e. a keyed hash
141function used for message authentication, which is based on a hash
142function.
143.Pp
144.Fn HMAC
145computes the message authentication code of the
146.Fa n
147bytes at
148.Fa d
149using the hash function
150.Fa evp_md
151and the key
152.Fa key
153which is
154.Fa key_len
155bytes long.
156.Pp
157It places the result in
158.Fa md ,
159which must have space for the output of the hash function, which is no
160more than
161.Dv EVP_MAX_MD_SIZE
162bytes.
163If
164.Fa md
165is
166.Dv NULL ,
167the digest is placed in a static array, which is not thread safe.
168The size of the output is placed in
169.Fa md_len ,
170unless it is
171.Dv NULL .
172.Pp
173.Fa evp_md
174can be
175.Xr EVP_sha1 3 ,
176.Xr EVP_ripemd160 3 ,
177etc.
178.Pp
179.Fn HMAC_CTX_new
180allocates and initializes a new
181.Vt HMAC_CTX
182object.
183.Pp
184.Fn HMAC_CTX_reset
185zeroes and re-initializes
186.Fa ctx
187and associated resources, making it suitable for new computations
188as if it was deleted with
189.Fn HMAC_CTX_free
190and newly created with
191.Fn HMAC_CTX_new .
192.Pp
193.Fn HMAC_CTX_free
194erases the key and other data from
195.Fa ctx ,
196releases any associated resources, and finally frees
197.Fa ctx
198itself.
199.Pp
200The following functions may be used if the message is not completely
201stored in memory:
202.Pp
203.Fn HMAC_Init_ex
204sets up or reuses
205.Fa ctx
206to use the hash function
207.Fa evp_md
208and the key
209.Fa key .
210Either can be
211.Dv NULL ,
212in which case the existing one is reused.
213The
214.Fa ctx
215must have been created with
216.Fn HMAC_CTX_new
217before the first use in this function.
218If
219.Fn HMAC_Init_ex
220is called with a
221.Dv NULL
222.Fa key
223but
224.Fa evp_md
225is neither
226.Dv NULL
227nor the same as the previous digest used by
228.Fa ctx ,
229then an error is returned because reuse of an existing key with a
230different digest is not supported.
231.Pp
232.Fn HMAC_Init
233is a deprecated wrapper around
234.Fn HMAC_Init_ex
235which performs no longer useful extra initialization in
236some circumstances.
237.Pp
238.Fn HMAC_Update
239can be called repeatedly with chunks of the message to be authenticated
240.Pq Fa len No bytes at Fa data .
241.Pp
242.Fn HMAC_Final
243places the message authentication code in
244.Fa md ,
245which must have space for the hash function output.
246.Pp
247.Fn HMAC_CTX_copy
248copies all of the internal state from
249.Fa sctx
250into
251.Fa dctx .
252.Pp
253.Fn HMAC_CTX_set_flags
254applies the specified flags to the internal
255.Vt EVP_MD_CTX
256objects.
257Possible flag values
258.Dv EVP_MD_CTX_FLAG_*
259are defined in
260.In openssl/evp.h .
261.Pp
262.Fn HMAC_size
263returns the length in bytes of the underlying hash function output.
264It is implemented as a macro.
265.Sh RETURN VALUES
266.Fn HMAC
267returns a pointer to the message authentication code or
268.Dv NULL
269if an error occurred.
270.Pp
271.Fn HMAC_CTX_new
272returns a pointer to the new
273.Vt HMAC_CTX
274object or
275.Dv NULL
276if an error occurred.
277.Pp
278.Fn HMAC_CTX_reset ,
279.Fn HMAC_Init_ex ,
280.Fn HMAC_Update ,
281.Fn HMAC_Final ,
282and
283.Fn HMAC_CTX_copy
284return 1 for success or 0 if an error occurred.
285.Pp
286.Fn HMAC_CTX_get_md
287returns the message digest that was previously set for
288.Fa ctx
289with
290.Fn HMAC_Init_ex ,
291or
292.Dv NULL
293if none was set.
294.Pp
295.Fn HMAC_size
296returns the length in bytes of the underlying hash function output
297or 0 on error.
298.Sh SEE ALSO
299.Xr CMAC_Init 3 ,
300.Xr EVP_DigestInit 3
301.Sh STANDARDS
302RFC 2104
303.Sh HISTORY
304.Fn HMAC ,
305.Fn HMAC_Init ,
306.Fn HMAC_Update ,
307.Fn HMAC_Final ,
308and
309.Fn HMAC_size
310first appeared in SSLeay 0.9.0 and have been available since
311.Ox 2.4 .
312.Pp
313.Fn HMAC_Init_ex
314first appeared in OpenSSL 0.9.7 and have been available since
315.Ox 3.2 .
316.Pp
317.Fn HMAC_CTX_set_flags
318first appeared in OpenSSL 0.9.7f and have been available since
319.Ox 3.8 .
320.Pp
321.Fn HMAC_CTX_copy
322first appeared in OpenSSL 1.0.0 and has been available since
323.Ox 4.9 .
324.Pp
325.Fn HMAC_CTX_new ,
326.Fn HMAC_CTX_reset ,
327.Fn HMAC_CTX_free ,
328and
329.Fn HMAC_CTX_get_md
330first appeared in OpenSSL 1.1.0 and have been available since
331.Ox 6.3 .
332