xref: /freebsd/lib/libmd/sha.3 (revision fd3ced15070885c818b74a44a0fbe45ed8687f44)
1c177a86bSGarrett Wollman.\"
2c177a86bSGarrett Wollman.\" ----------------------------------------------------------------------------
3c177a86bSGarrett Wollman.\" "THE BEER-WARE LICENSE" (Revision 42):
442c159feSPoul-Henning Kamp.\" <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5c177a86bSGarrett Wollman.\" can do whatever you want with this stuff. If we meet some day, and you think
6c177a86bSGarrett Wollman.\" this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7c177a86bSGarrett Wollman.\" ----------------------------------------------------------------------------
8c177a86bSGarrett Wollman.\"
9c177a86bSGarrett Wollman.\" 	From: Id: mdX.3,v 1.14 1999/02/11 20:31:49 wollman Exp
10c177a86bSGarrett Wollman.\"
11*fd3ced15SKyle Evans.Dd February 12, 2023
12d00ed299SGarrett Wollman.Dt SHA 3
13a307d598SRuslan Ermilov.Os
14c177a86bSGarrett Wollman.Sh NAME
15c177a86bSGarrett Wollman.Nm SHA_Init ,
16c177a86bSGarrett Wollman.Nm SHA_Update ,
17c177a86bSGarrett Wollman.Nm SHA_Final ,
18c177a86bSGarrett Wollman.Nm SHA_End ,
19*fd3ced15SKyle Evans.Nm SHA_Fd ,
20*fd3ced15SKyle Evans.Nm SHA_FdChunk ,
21c177a86bSGarrett Wollman.Nm SHA_File ,
228a24546cSPoul-Henning Kamp.Nm SHA_FileChunk ,
23c177a86bSGarrett Wollman.Nm SHA_Data ,
24c177a86bSGarrett Wollman.Nm SHA1_Init ,
25c177a86bSGarrett Wollman.Nm SHA1_Update ,
26c177a86bSGarrett Wollman.Nm SHA1_Final ,
27c177a86bSGarrett Wollman.Nm SHA1_End ,
28*fd3ced15SKyle Evans.Nm SHA1_Fd ,
29*fd3ced15SKyle Evans.Nm SHA1_FdChunk ,
30c177a86bSGarrett Wollman.Nm SHA1_File ,
318a24546cSPoul-Henning Kamp.Nm SHA1_FileChunk ,
32c177a86bSGarrett Wollman.Nm SHA1_Data
33c177a86bSGarrett Wollman.Nd calculate the FIPS 160 and 160-1 ``SHA'' message digests
349452ceebSAlexey Zelkin.Sh LIBRARY
359452ceebSAlexey Zelkin.Lb libmd
36c177a86bSGarrett Wollman.Sh SYNOPSIS
3732eef9aeSRuslan Ermilov.In sys/types.h
3832eef9aeSRuslan Ermilov.In sha.h
39c177a86bSGarrett Wollman.Ft void
40c177a86bSGarrett Wollman.Fn SHA_Init "SHA_CTX *context"
41c177a86bSGarrett Wollman.Ft void
42c7981eaeSColin Percival.Fn SHA_Update "SHA_CTX *context" "const unsigned char *data" "size_t len"
43c177a86bSGarrett Wollman.Ft void
44c177a86bSGarrett Wollman.Fn SHA_Final "unsigned char digest[20]" "SHA_CTX *context"
45c177a86bSGarrett Wollman.Ft "char *"
46c177a86bSGarrett Wollman.Fn SHA_End "SHA_CTX *context" "char *buf"
47c177a86bSGarrett Wollman.Ft "char *"
48*fd3ced15SKyle Evans.Fn SHA_Fd "int fd" "char *buf"
49*fd3ced15SKyle Evans.Ft "char *"
50*fd3ced15SKyle Evans.Fn SHA_FdChunk "int fd" "char *buf" "off_t offset" "off_t length"
51*fd3ced15SKyle Evans.Ft "char *"
52c177a86bSGarrett Wollman.Fn SHA_File "const char *filename" "char *buf"
53c177a86bSGarrett Wollman.Ft "char *"
548a24546cSPoul-Henning Kamp.Fn SHA_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
558a24546cSPoul-Henning Kamp.Ft "char *"
56c177a86bSGarrett Wollman.Fn SHA_Data "const unsigned char *data" "unsigned int len" "char *buf"
57c177a86bSGarrett Wollman.Ft void
58c177a86bSGarrett Wollman.Fn SHA1_Init "SHA_CTX *context"
59c177a86bSGarrett Wollman.Ft void
60c7981eaeSColin Percival.Fn SHA1_Update "SHA_CTX *context" "const unsigned char *data" "size_t len"
61c177a86bSGarrett Wollman.Ft void
62c177a86bSGarrett Wollman.Fn SHA1_Final "unsigned char digest[20]" "SHA_CTX *context"
63c177a86bSGarrett Wollman.Ft "char *"
64c177a86bSGarrett Wollman.Fn SHA1_End "SHA_CTX *context" "char *buf"
65c177a86bSGarrett Wollman.Ft "char *"
66*fd3ced15SKyle Evans.Fn SHA1_Fd "int fd" "char *buf"
67*fd3ced15SKyle Evans.Ft "char *"
68*fd3ced15SKyle Evans.Fn SHA1_FdChunk "int fd" "char *buf" "off_t offset" "off_t length"
69*fd3ced15SKyle Evans.Ft "char *"
70c177a86bSGarrett Wollman.Fn SHA1_File "const char *filename" "char *buf"
71c177a86bSGarrett Wollman.Ft "char *"
728a24546cSPoul-Henning Kamp.Fn SHA1_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
738a24546cSPoul-Henning Kamp.Ft "char *"
74c177a86bSGarrett Wollman.Fn SHA1_Data "const unsigned char *data" "unsigned int len" "char *buf"
75c177a86bSGarrett Wollman.Sh DESCRIPTION
76c177a86bSGarrett WollmanThe
77c177a86bSGarrett Wollman.Li SHA_
78c177a86bSGarrett Wollmanand
79c177a86bSGarrett Wollman.Li SHA1_
80c177a86bSGarrett Wollmanfunctions calculate a 160-bit cryptographic checksum (digest)
81dc2af37dSHiten Pandyafor any number of input bytes.
82dc2af37dSHiten PandyaA cryptographic checksum is a one-way
83c177a86bSGarrett Wollmanhash function; that is, it is computationally impractical to find
84dc2af37dSHiten Pandyathe input corresponding to a particular output.
85dc2af37dSHiten PandyaThis net result is
86dc2af37dSHiten Pandyaa
87dc2af37dSHiten Pandya.Dq fingerprint
880227791bSRuslan Ermilovof the input-data, which does not disclose the actual input.
89c177a86bSGarrett Wollman.Pp
902768d705SDag-Erling SmørgravSHA (or SHA-0) is the original Secure Hash Algorithm specified in FIPS 160.
912768d705SDag-Erling SmørgravIt was quickly proven insecure, and has been superseded by SHA-1.
922768d705SDag-Erling SmørgravSHA-0 is included for compatibility purposes only.
93c177a86bSGarrett Wollman.Pp
94c177a86bSGarrett WollmanThe
95c177a86bSGarrett Wollman.Fn SHA1_Init ,
96c177a86bSGarrett Wollman.Fn SHA1_Update ,
97c177a86bSGarrett Wollmanand
98c177a86bSGarrett Wollman.Fn SHA1_Final
99dc2af37dSHiten Pandyafunctions are the core functions.
100dc2af37dSHiten PandyaAllocate an
101dc2af37dSHiten Pandya.Vt SHA_CTX ,
102dc2af37dSHiten Pandyainitialize it with
103c177a86bSGarrett Wollman.Fn SHA1_Init ,
104c177a86bSGarrett Wollmanrun over the data with
105c177a86bSGarrett Wollman.Fn SHA1_Update ,
106c177a86bSGarrett Wollmanand finally extract the result using
10766bdf50fSXin LI.Fn SHA1_Final ,
10866bdf50fSXin LIwhich will also erase the
10966bdf50fSXin LI.Vt SHA_CTX .
110c177a86bSGarrett Wollman.Pp
111c177a86bSGarrett Wollman.Fn SHA1_End
112c177a86bSGarrett Wollmanis a wrapper for
113c177a86bSGarrett Wollman.Fn SHA1_Final
114c177a86bSGarrett Wollmanwhich converts the return value to a 41-character
115c177a86bSGarrett Wollman(including the terminating '\e0')
1162768d705SDag-Erling SmørgravASCII string which represents the 160 bits in hexadecimal.
117c177a86bSGarrett Wollman.Pp
118c177a86bSGarrett Wollman.Fn SHA1_File
119c177a86bSGarrett Wollmancalculates the digest of a file, and uses
120c177a86bSGarrett Wollman.Fn SHA1_End
121c177a86bSGarrett Wollmanto return the result.
122c177a86bSGarrett WollmanIf the file cannot be opened, a null pointer is returned.
1238a24546cSPoul-Henning Kamp.Fn SHA1_FileChunk
1248a24546cSPoul-Henning Kampis similar to
1258a24546cSPoul-Henning Kamp.Fn SHA1_File ,
1268a24546cSPoul-Henning Kampbut it only calculates the digest over a byte-range of the file specified,
1278a24546cSPoul-Henning Kampstarting at
1287e5d6de0SRuslan Ermilov.Fa offset
1298a24546cSPoul-Henning Kampand spanning
1307e5d6de0SRuslan Ermilov.Fa length
1318a24546cSPoul-Henning Kampbytes.
1328a24546cSPoul-Henning KampIf the
1337e5d6de0SRuslan Ermilov.Fa length
1348a24546cSPoul-Henning Kampparameter is specified as 0, or more than the length of the remaining part
1358a24546cSPoul-Henning Kampof the file,
1368a24546cSPoul-Henning Kamp.Fn SHA1_FileChunk
1378a24546cSPoul-Henning Kampcalculates the digest from
1387e5d6de0SRuslan Ermilov.Fa offset
1398a24546cSPoul-Henning Kampto the end of file.
140c177a86bSGarrett Wollman.Fn SHA1_Data
141c177a86bSGarrett Wollmancalculates the digest of a chunk of data in memory, and uses
142c177a86bSGarrett Wollman.Fn SHA1_End
143c177a86bSGarrett Wollmanto return the result.
144c177a86bSGarrett Wollman.Pp
145*fd3ced15SKyle EvansThe
146*fd3ced15SKyle Evans.Fn SHA1_Fd
147*fd3ced15SKyle Evansand
148*fd3ced15SKyle Evans.Fn SHA1_FdChunk
149*fd3ced15SKyle Evansfunctions are identical to their
150*fd3ced15SKyle Evans.Fn SHA1_File
151*fd3ced15SKyle Evansand
152*fd3ced15SKyle Evans.Fn SHA1_FileChunk
153*fd3ced15SKyle Evanscounterparts, with the exception that the first argument is an
154*fd3ced15SKyle Evans.Fa fd
155*fd3ced15SKyle Evansinstead of a
156*fd3ced15SKyle Evans.Fa filename .
157*fd3ced15SKyle Evans.Pp
158c177a86bSGarrett WollmanWhen using
159c177a86bSGarrett Wollman.Fn SHA1_End ,
160c177a86bSGarrett Wollman.Fn SHA1_File ,
161c177a86bSGarrett Wollmanor
162c177a86bSGarrett Wollman.Fn SHA1_Data ,
163c177a86bSGarrett Wollmanthe
1647e5d6de0SRuslan Ermilov.Fa buf
165c177a86bSGarrett Wollmanargument can be a null pointer, in which case the returned string
166c177a86bSGarrett Wollmanis allocated with
167c177a86bSGarrett Wollman.Xr malloc 3
168c177a86bSGarrett Wollmanand subsequently must be explicitly deallocated using
169c177a86bSGarrett Wollman.Xr free 3
170c177a86bSGarrett Wollmanafter use.
171c177a86bSGarrett WollmanIf the
1727e5d6de0SRuslan Ermilov.Fa buf
173c177a86bSGarrett Wollmanargument is non-null it must point to at least 41 characters of buffer space.
174f26f4cf1SAllan Jude.Sh ERRORS
175f26f4cf1SAllan JudeThe
176f26f4cf1SAllan Jude.Fn SHA1_End
177f26f4cf1SAllan Judefunction called with a null buf argument may fail and return NULL if:
178f26f4cf1SAllan Jude.Bl -tag -width Er
179f26f4cf1SAllan Jude.It Bq Er ENOMEM
180f26f4cf1SAllan JudeInsufficient storage space is available.
181f26f4cf1SAllan Jude.El
182f26f4cf1SAllan Jude.Pp
183f26f4cf1SAllan JudeThe
184f26f4cf1SAllan Jude.Fn SHA1_File
185f26f4cf1SAllan Judeand
186f26f4cf1SAllan Jude.Fn SHA1_FileChunk
187f26f4cf1SAllan Judemay return NULL when underlying
188f26f4cf1SAllan Jude.Xr open 2 ,
189f26f4cf1SAllan Jude.Xr fstat 2 ,
190f26f4cf1SAllan Jude.Xr lseek 2 ,
191f26f4cf1SAllan Judeor
192d491167bSJens Schweikhardt.Xr SHA1_End 3
193f26f4cf1SAllan Judefail.
194c177a86bSGarrett Wollman.Sh SEE ALSO
195c177a86bSGarrett Wollman.Xr md4 3 ,
196d00ed299SGarrett Wollman.Xr md5 3 ,
19748856b00SRuslan Ermilov.Xr ripemd 3 ,
198b468a9ffSAllan Jude.Xr sha256 3 ,
199b468a9ffSAllan Jude.Xr sha512 3 ,
200b468a9ffSAllan Jude.Xr skein 3
20124a0682cSRuslan Ermilov.Sh HISTORY
20224a0682cSRuslan ErmilovThese functions appeared in
20324a0682cSRuslan Ermilov.Fx 4.0 .
204c177a86bSGarrett Wollman.Sh AUTHORS
205c177a86bSGarrett WollmanThe core hash routines were implemented by Eric Young based on the
206c177a86bSGarrett Wollmanpublished
2072768d705SDag-Erling SmørgravFIPS standards.
208c177a86bSGarrett Wollman.Sh BUGS
2092768d705SDag-Erling SmørgravThe SHA1 algorithm has been proven to be vulnerable to practical collision
21034841dd6SAllan Judeattacks and should not be relied upon to produce unique outputs,
21187e34f6cSAllan Jude.Em nor should it be used as part of a new cryptographic signature scheme.
212