xref: /minix3/lib/libc/hash/sha2/sha2.3 (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras.\" $NetBSD: sha2.3,v 1.5 2009/05/26 08:04:12 joerg Exp $
2*2fe8fb19SBen Gras.\"	$OpenBSD: sha2.3,v 1.11 2004/06/22 01:57:29 jfb Exp $
3*2fe8fb19SBen Gras.\"
4*2fe8fb19SBen Gras.\" Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
5*2fe8fb19SBen Gras.\"
6*2fe8fb19SBen Gras.\" Permission to use, copy, modify, and distribute this software for any
7*2fe8fb19SBen Gras.\" purpose with or without fee is hereby granted, provided that the above
8*2fe8fb19SBen Gras.\" copyright notice and this permission notice appear in all copies.
9*2fe8fb19SBen Gras.\"
10*2fe8fb19SBen Gras.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*2fe8fb19SBen Gras.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*2fe8fb19SBen Gras.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*2fe8fb19SBen Gras.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*2fe8fb19SBen Gras.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*2fe8fb19SBen Gras.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*2fe8fb19SBen Gras.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*2fe8fb19SBen Gras.\"
18*2fe8fb19SBen Gras.\" Sponsored in part by the Defense Advanced Research Projects
19*2fe8fb19SBen Gras.\" Agency (DARPA) and Air Force Research Laboratory, Air Force
20*2fe8fb19SBen Gras.\" Materiel Command, USAF, under agreement number F39502-99-1-0512.
21*2fe8fb19SBen Gras.\"
22*2fe8fb19SBen Gras.\" See http://www.nist.gov/sha/ for the detailed standard
23*2fe8fb19SBen Gras.\"
24*2fe8fb19SBen Gras.Dd May 20, 2009
25*2fe8fb19SBen Gras.Dt SHA2 3
26*2fe8fb19SBen Gras.Os
27*2fe8fb19SBen Gras.Sh NAME
28*2fe8fb19SBen Gras.Nm SHA256_Init ,
29*2fe8fb19SBen Gras.Nm SHA256_Update ,
30*2fe8fb19SBen Gras.Nm SHA256_Pad ,
31*2fe8fb19SBen Gras.Nm SHA256_Final ,
32*2fe8fb19SBen Gras.Nm SHA256_Transform ,
33*2fe8fb19SBen Gras.Nm SHA256_End ,
34*2fe8fb19SBen Gras.Nm SHA256_File ,
35*2fe8fb19SBen Gras.Nm SHA256_FileChunk ,
36*2fe8fb19SBen Gras.Nm SHA256_Data
37*2fe8fb19SBen Gras.Nd calculate the NIST Secure Hash Standard (version 2)
38*2fe8fb19SBen Gras.Sh SYNOPSIS
39*2fe8fb19SBen Gras.In sys/types.h
40*2fe8fb19SBen Gras.In sha2.h
41*2fe8fb19SBen Gras.Ft void
42*2fe8fb19SBen Gras.Fn SHA224_Init "SHA224_CTX *context"
43*2fe8fb19SBen Gras.Ft void
44*2fe8fb19SBen Gras.Fn SHA224_Update "SHA224_CTX *context" "const uint8_t *data" "size_t len"
45*2fe8fb19SBen Gras.Ft void
46*2fe8fb19SBen Gras.Fn SHA224_Pad "SHA224_CTX *context"
47*2fe8fb19SBen Gras.Ft void
48*2fe8fb19SBen Gras.Fn SHA224_Final "uint8_t digest[SHA224_DIGEST_LENGTH]" "SHA224_CTX *context"
49*2fe8fb19SBen Gras.Ft void
50*2fe8fb19SBen Gras.Fn SHA224_Transform "uint32_t state[8]" "const uint8_t buffer[SHA224_BLOCK_LENGTH]"
51*2fe8fb19SBen Gras.Ft "char *"
52*2fe8fb19SBen Gras.Fn SHA224_End "SHA224_CTX *context" "char *buf"
53*2fe8fb19SBen Gras.Ft "char *"
54*2fe8fb19SBen Gras.Fn SHA224_File "const char *filename" "char *buf"
55*2fe8fb19SBen Gras.Ft "char *"
56*2fe8fb19SBen Gras.Fn SHA224_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
57*2fe8fb19SBen Gras.Ft "char *"
58*2fe8fb19SBen Gras.Fn SHA224_Data "uint8_t *data" "size_t len" "char *buf"
59*2fe8fb19SBen Gras.Ft void
60*2fe8fb19SBen Gras.Fn SHA256_Init "SHA256_CTX *context"
61*2fe8fb19SBen Gras.Ft void
62*2fe8fb19SBen Gras.Fn SHA256_Update "SHA256_CTX *context" "const uint8_t *data" "size_t len"
63*2fe8fb19SBen Gras.Ft void
64*2fe8fb19SBen Gras.Fn SHA256_Pad "SHA256_CTX *context"
65*2fe8fb19SBen Gras.Ft void
66*2fe8fb19SBen Gras.Fn SHA256_Final "uint8_t digest[SHA256_DIGEST_LENGTH]" "SHA256_CTX *context"
67*2fe8fb19SBen Gras.Ft void
68*2fe8fb19SBen Gras.Fn SHA256_Transform "uint32_t state[8]" "const uint8_t buffer[SHA256_BLOCK_LENGTH]"
69*2fe8fb19SBen Gras.Ft "char *"
70*2fe8fb19SBen Gras.Fn SHA256_End "SHA256_CTX *context" "char *buf"
71*2fe8fb19SBen Gras.Ft "char *"
72*2fe8fb19SBen Gras.Fn SHA256_File "const char *filename" "char *buf"
73*2fe8fb19SBen Gras.Ft "char *"
74*2fe8fb19SBen Gras.Fn SHA256_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
75*2fe8fb19SBen Gras.Ft "char *"
76*2fe8fb19SBen Gras.Fn SHA256_Data "uint8_t *data" "size_t len" "char *buf"
77*2fe8fb19SBen Gras.Ft void
78*2fe8fb19SBen Gras.Fn SHA384_Init "SHA384_CTX *context"
79*2fe8fb19SBen Gras.Ft void
80*2fe8fb19SBen Gras.Fn SHA384_Update "SHA384_CTX *context" "const uint8_t *data" "size_t len"
81*2fe8fb19SBen Gras.Ft void
82*2fe8fb19SBen Gras.Fn SHA384_Pad "SHA384_CTX *context"
83*2fe8fb19SBen Gras.Ft void
84*2fe8fb19SBen Gras.Fn SHA384_Final "uint8_t digest[SHA384_DIGEST_LENGTH]" "SHA384_CTX *context"
85*2fe8fb19SBen Gras.Ft void
86*2fe8fb19SBen Gras.Fn SHA384_Transform "uint64_t state[8]" "const uint8_t buffer[SHA384_BLOCK_LENGTH]"
87*2fe8fb19SBen Gras.Ft "char *"
88*2fe8fb19SBen Gras.Fn SHA384_End "SHA384_CTX *context" "char *buf"
89*2fe8fb19SBen Gras.Ft "char *"
90*2fe8fb19SBen Gras.Fn SHA384_File "char *filename" "char *buf"
91*2fe8fb19SBen Gras.Ft "char *"
92*2fe8fb19SBen Gras.Fn SHA384_FileChunk "char *filename" "char *buf" "off_t offset" "off_t length"
93*2fe8fb19SBen Gras.Ft "char *"
94*2fe8fb19SBen Gras.Fn SHA384_Data "uint8_t *data" "size_t len" "char *buf"
95*2fe8fb19SBen Gras.Ft void
96*2fe8fb19SBen Gras.Fn SHA512_Init "SHA512_CTX *context"
97*2fe8fb19SBen Gras.Ft void
98*2fe8fb19SBen Gras.Fn SHA512_Update "SHA512_CTX *context" "const uint8_t *data" "size_t len"
99*2fe8fb19SBen Gras.Ft void
100*2fe8fb19SBen Gras.Fn SHA512_Pad "SHA512_CTX *context"
101*2fe8fb19SBen Gras.Ft void
102*2fe8fb19SBen Gras.Fn SHA512_Final "uint8_t digest[SHA512_DIGEST_LENGTH]" "SHA512_CTX *context"
103*2fe8fb19SBen Gras.Ft void
104*2fe8fb19SBen Gras.Fn SHA512_Transform "uint64_t state[8]" "const uint8_t buffer[SHA512_BLOCK_LENGTH]"
105*2fe8fb19SBen Gras.Ft "char *"
106*2fe8fb19SBen Gras.Fn SHA512_End "SHA512_CTX *context" "char *buf"
107*2fe8fb19SBen Gras.Ft "char *"
108*2fe8fb19SBen Gras.Fn SHA512_File "char *filename" "char *buf"
109*2fe8fb19SBen Gras.Ft "char *"
110*2fe8fb19SBen Gras.Fn SHA512_FileChunk "char *filename" "char *buf" "off_t offset" "off_t length"
111*2fe8fb19SBen Gras.Ft "char *"
112*2fe8fb19SBen Gras.Fn SHA512_Data "uint8_t *data" "size_t len" "char *buf"
113*2fe8fb19SBen Gras.Sh DESCRIPTION
114*2fe8fb19SBen GrasThe SHA2 functions implement the NIST Secure Hash Standard,
115*2fe8fb19SBen GrasFIPS PUB 180-2.
116*2fe8fb19SBen GrasThe SHA2 functions are used to generate a condensed representation of a
117*2fe8fb19SBen Grasmessage called a message digest, suitable for use as a digital signature.
118*2fe8fb19SBen GrasThere are four families of functions, with names corresponding to
119*2fe8fb19SBen Grasthe number of bits in the resulting message digest.
120*2fe8fb19SBen GrasThe SHA-224 and SHA-256 functions are limited to processing a message of less
121*2fe8fb19SBen Grasthan 2^64 bits as input.
122*2fe8fb19SBen GrasThe SHA-384 and SHA-512 functions can process a message of at most 2^128 - 1
123*2fe8fb19SBen Grasbits as input.
124*2fe8fb19SBen Gras.Pp
125*2fe8fb19SBen GrasThe SHA2 functions are considered to be more secure than the
126*2fe8fb19SBen Gras.Xr sha1 3
127*2fe8fb19SBen Grasfunctions with which they share a similar interface.
128*2fe8fb19SBen GrasThe 224, 256, 384, and 512-bit versions of SHA2 share the same interface.
129*2fe8fb19SBen GrasFor brevity, only the 256-bit variants are described below.
130*2fe8fb19SBen Gras.Pp
131*2fe8fb19SBen GrasThe
132*2fe8fb19SBen Gras.Fn SHA256_Init
133*2fe8fb19SBen Grasfunction initializes a SHA256_CTX
134*2fe8fb19SBen Gras.Ar context
135*2fe8fb19SBen Grasfor use with
136*2fe8fb19SBen Gras.Fn SHA256_Update ,
137*2fe8fb19SBen Grasand
138*2fe8fb19SBen Gras.Fn SHA256_Final .
139*2fe8fb19SBen GrasThe
140*2fe8fb19SBen Gras.Fn SHA256_Update
141*2fe8fb19SBen Grasfunction adds
142*2fe8fb19SBen Gras.Ar data
143*2fe8fb19SBen Grasof length
144*2fe8fb19SBen Gras.Ar len
145*2fe8fb19SBen Grasto the SHA256_CTX specified by
146*2fe8fb19SBen Gras.Ar context .
147*2fe8fb19SBen Gras.Fn SHA256_Final
148*2fe8fb19SBen Grasis called when all data has been added via
149*2fe8fb19SBen Gras.Fn SHA256_Update
150*2fe8fb19SBen Grasand stores a message digest in the
151*2fe8fb19SBen Gras.Ar digest
152*2fe8fb19SBen Grasparameter.
153*2fe8fb19SBen Gras.Pp
154*2fe8fb19SBen GrasThe
155*2fe8fb19SBen Gras.Fn SHA256_Pad
156*2fe8fb19SBen Grasfunction can be used to apply padding to the message digest as in
157*2fe8fb19SBen Gras.Fn SHA256_Final ,
158*2fe8fb19SBen Grasbut the current context can still be used with
159*2fe8fb19SBen Gras.Fn SHA256_Update .
160*2fe8fb19SBen Gras.Pp
161*2fe8fb19SBen GrasThe
162*2fe8fb19SBen Gras.Fn SHA256_Transform
163*2fe8fb19SBen Grasfunction is used by
164*2fe8fb19SBen Gras.Fn SHA256_Update
165*2fe8fb19SBen Grasto hash 512-bit blocks and forms the core of the algorithm.
166*2fe8fb19SBen GrasMost programs should use the interface provided by
167*2fe8fb19SBen Gras.Fn SHA256_Init ,
168*2fe8fb19SBen Gras.Fn SHA256_Update ,
169*2fe8fb19SBen Grasand
170*2fe8fb19SBen Gras.Fn SHA256_Final
171*2fe8fb19SBen Grasinstead of calling
172*2fe8fb19SBen Gras.Fn SHA256_Transform
173*2fe8fb19SBen Grasdirectly.
174*2fe8fb19SBen Gras.Pp
175*2fe8fb19SBen GrasThe
176*2fe8fb19SBen Gras.Fn SHA256_End
177*2fe8fb19SBen Grasfunction is a front end for
178*2fe8fb19SBen Gras.Fn SHA256_Final
179*2fe8fb19SBen Graswhich converts the digest into an
180*2fe8fb19SBen Gras.Tn ASCII
181*2fe8fb19SBen Grasrepresentation of the digest in hexadecimal.
182*2fe8fb19SBen Gras.Pp
183*2fe8fb19SBen GrasThe
184*2fe8fb19SBen Gras.Fn SHA256_File
185*2fe8fb19SBen Grasfunction calculates the digest for a file and returns the result via
186*2fe8fb19SBen Gras.Fn SHA256_End .
187*2fe8fb19SBen GrasIf
188*2fe8fb19SBen Gras.Fn SHA256_File
189*2fe8fb19SBen Grasis unable to open the file, a
190*2fe8fb19SBen Gras.Dv NULL
191*2fe8fb19SBen Graspointer is returned.
192*2fe8fb19SBen Gras.Pp
193*2fe8fb19SBen Gras.Fn SHA256_FileChunk
194*2fe8fb19SBen Grasbehaves like
195*2fe8fb19SBen Gras.Fn SHA256_File
196*2fe8fb19SBen Grasbut calculates the digest only for that portion of the file starting at
197*2fe8fb19SBen Gras.Fa offset
198*2fe8fb19SBen Grasand continuing for
199*2fe8fb19SBen Gras.Fa length
200*2fe8fb19SBen Grasbytes or until end of file is reached, whichever comes first.
201*2fe8fb19SBen GrasA zero
202*2fe8fb19SBen Gras.Fa length
203*2fe8fb19SBen Grascan be specified to read until end of file.
204*2fe8fb19SBen GrasA negative
205*2fe8fb19SBen Gras.Fa length
206*2fe8fb19SBen Grasor
207*2fe8fb19SBen Gras.Fa offset
208*2fe8fb19SBen Graswill be ignored.
209*2fe8fb19SBen Gras.Pp
210*2fe8fb19SBen GrasThe
211*2fe8fb19SBen Gras.Fn SHA256_Data
212*2fe8fb19SBen Grasfunction
213*2fe8fb19SBen Grascalculates the digest of an arbitrary string and returns the result via
214*2fe8fb19SBen Gras.Fn SHA256_End .
215*2fe8fb19SBen Gras.Pp
216*2fe8fb19SBen GrasFor each of the
217*2fe8fb19SBen Gras.Fn SHA256_End ,
218*2fe8fb19SBen Gras.Fn SHA256_File ,
219*2fe8fb19SBen Gras.Fn SHA256_FileChunk ,
220*2fe8fb19SBen Grasand
221*2fe8fb19SBen Gras.Fn SHA256_Data
222*2fe8fb19SBen Grasfunctions the
223*2fe8fb19SBen Gras.Ar buf
224*2fe8fb19SBen Grasparameter should either be a string large enough to hold the resulting digest
225*2fe8fb19SBen Gras(e.g.,
226*2fe8fb19SBen Gras.Ev SHA224_DIGEST_STRING_LENGTH ,
227*2fe8fb19SBen Gras.Ev SHA256_DIGEST_STRING_LENGTH ,
228*2fe8fb19SBen Gras.Ev SHA384_DIGEST_STRING_LENGTH ,
229*2fe8fb19SBen Grasor
230*2fe8fb19SBen Gras.Ev SHA512_DIGEST_STRING_LENGTH ,
231*2fe8fb19SBen Grasdepending on the function being used)
232*2fe8fb19SBen Grasor a
233*2fe8fb19SBen Gras.Dv NULL
234*2fe8fb19SBen Graspointer.
235*2fe8fb19SBen GrasIn the latter case, space will be dynamically allocated via
236*2fe8fb19SBen Gras.Xr malloc 3
237*2fe8fb19SBen Grasand should be freed using
238*2fe8fb19SBen Gras.Xr free 3
239*2fe8fb19SBen Graswhen it is no longer needed.
240*2fe8fb19SBen Gras.Sh EXAMPLES
241*2fe8fb19SBen GrasThe following code fragment will calculate the SHA-256 digest for the string
242*2fe8fb19SBen Gras.Qq abc ,
243*2fe8fb19SBen Graswhich is
244*2fe8fb19SBen Gras.Dq 0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad .
245*2fe8fb19SBen Gras.Bd -literal -offset indent
246*2fe8fb19SBen GrasSHA256_CTX ctx;
247*2fe8fb19SBen Grasuint8_t results[SHA256_DIGEST_LENGTH];
248*2fe8fb19SBen Graschar *buf;
249*2fe8fb19SBen Grasint n;
250*2fe8fb19SBen Gras
251*2fe8fb19SBen Grasbuf = "abc";
252*2fe8fb19SBen Grasn = strlen(buf);
253*2fe8fb19SBen GrasSHA256_Init(\*[Am]ctx);
254*2fe8fb19SBen GrasSHA256_Update(\*[Am]ctx, (uint8_t *)buf, n);
255*2fe8fb19SBen GrasSHA256_Final(results, \*[Am]ctx);
256*2fe8fb19SBen Gras
257*2fe8fb19SBen Gras/* Print the digest as one long hex value */
258*2fe8fb19SBen Grasprintf("0x");
259*2fe8fb19SBen Grasfor (n = 0; n \*[Lt] SHA256_DIGEST_LENGTH; n++)
260*2fe8fb19SBen Gras	printf("%02x", results[n]);
261*2fe8fb19SBen Grasputchar('\en');
262*2fe8fb19SBen Gras.Ed
263*2fe8fb19SBen Gras.Pp
264*2fe8fb19SBen GrasAlternately, the helper functions could be used in the following way:
265*2fe8fb19SBen Gras.Bd -literal -offset indent
266*2fe8fb19SBen GrasSHA256_CTX ctx;
267*2fe8fb19SBen Grasuint8_t output[SHA256_DIGEST_STRING_LENGTH];
268*2fe8fb19SBen Graschar *buf = "abc";
269*2fe8fb19SBen Gras
270*2fe8fb19SBen Grasprintf("0x%s\en", SHA256_Data(buf, strlen(buf), output));
271*2fe8fb19SBen Gras.Ed
272*2fe8fb19SBen Gras.Sh SEE ALSO
273*2fe8fb19SBen Gras.Xr cksum 1 ,
274*2fe8fb19SBen Gras.Xr md4 3 ,
275*2fe8fb19SBen Gras.Xr md5 3 ,
276*2fe8fb19SBen Gras.Xr rmd160 3 ,
277*2fe8fb19SBen Gras.Xr sha1 3
278*2fe8fb19SBen Gras.Rs
279*2fe8fb19SBen Gras.%T Secure Hash Standard
280*2fe8fb19SBen Gras.%O FIPS PUB 180-2
281*2fe8fb19SBen Gras.Re
282*2fe8fb19SBen Gras.Sh HISTORY
283*2fe8fb19SBen GrasThe SHA2 functions appeared in
284*2fe8fb19SBen Gras.Ox 3.4
285*2fe8fb19SBen Grasand
286*2fe8fb19SBen Gras.Nx 3.0 .
287*2fe8fb19SBen Gras.Sh AUTHORS
288*2fe8fb19SBen GrasThis implementation of the SHA functions was written by Aaron D. Gifford.
289*2fe8fb19SBen Gras.Pp
290*2fe8fb19SBen GrasThe
291*2fe8fb19SBen Gras.Fn SHA256_End ,
292*2fe8fb19SBen Gras.Fn SHA256_File ,
293*2fe8fb19SBen Gras.Fn SHA256_FileChunk ,
294*2fe8fb19SBen Grasand
295*2fe8fb19SBen Gras.Fn SHA256_Data
296*2fe8fb19SBen Grashelper functions are derived from code written by Poul-Henning Kamp.
297*2fe8fb19SBen Gras.Sh CAVEATS
298*2fe8fb19SBen GrasThis implementation of the Secure Hash Standard has not been validated by
299*2fe8fb19SBen GrasNIST and as such is not in official compliance with the standard.
300*2fe8fb19SBen Gras.Pp
301*2fe8fb19SBen GrasIf a message digest is to be copied to a multi-byte type (i.e.:
302*2fe8fb19SBen Grasan array of five 32-bit integers) it will be necessary to
303*2fe8fb19SBen Grasperform byte swapping on little endian machines such as the i386, alpha,
304*2fe8fb19SBen Grasand vax.
305