xref: /freebsd/lib/libmd/sha256.3 (revision fd3ced15070885c818b74a44a0fbe45ed8687f44)
1186c183cSColin Percival.\"
2186c183cSColin Percival.\" ----------------------------------------------------------------------------
3186c183cSColin Percival.\" "THE BEER-WARE LICENSE" (Revision 42):
4186c183cSColin Percival.\" <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5186c183cSColin Percival.\" can do whatever you want with this stuff. If we meet some day, and you think
6186c183cSColin Percival.\" this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7186c183cSColin Percival.\" ----------------------------------------------------------------------------
8186c183cSColin Percival.\"
9186c183cSColin Percival.\" 	From: Id: mdX.3,v 1.14 1999/02/11 20:31:49 wollman Exp
10186c183cSColin Percival.\"
11*fd3ced15SKyle Evans.Dd March 8, 2022
12186c183cSColin Percival.Dt SHA256 3
13186c183cSColin Percival.Os
14186c183cSColin Percival.Sh NAME
158ff3cdd1SConrad Meyer.Nm SHA224_Init ,
168ff3cdd1SConrad Meyer.Nm SHA224_Update ,
178ff3cdd1SConrad Meyer.Nm SHA224_Final ,
188ff3cdd1SConrad Meyer.Nm SHA224_End ,
19*fd3ced15SKyle Evans.Nm SHA224_Fd ,
20*fd3ced15SKyle Evans.Nm SHA224_FdChunk ,
218ff3cdd1SConrad Meyer.Nm SHA224_File ,
228ff3cdd1SConrad Meyer.Nm SHA224_FileChunk ,
238ff3cdd1SConrad Meyer.Nm SHA224_Data ,
24186c183cSColin Percival.Nm SHA256_Init ,
25186c183cSColin Percival.Nm SHA256_Update ,
26186c183cSColin Percival.Nm SHA256_Final ,
27186c183cSColin Percival.Nm SHA256_End ,
28*fd3ced15SKyle Evans.Nm SHA256_Fd ,
29*fd3ced15SKyle Evans.Nm SHA256_FdChunk ,
30186c183cSColin Percival.Nm SHA256_File ,
31186c183cSColin Percival.Nm SHA256_FileChunk ,
32186c183cSColin Percival.Nm SHA256_Data
338ff3cdd1SConrad Meyer.Nd calculate the FIPS 180-2 ``SHA-256'' (or SHA-224) message digest
34186c183cSColin Percival.Sh LIBRARY
35186c183cSColin Percival.Lb libmd
36186c183cSColin Percival.Sh SYNOPSIS
37186c183cSColin Percival.In sys/types.h
388ff3cdd1SConrad Meyer.In sha224.h
398ff3cdd1SConrad Meyer.Ft void
408ff3cdd1SConrad Meyer.Fn SHA224_Init "SHA224_CTX *context"
418ff3cdd1SConrad Meyer.Ft void
428ff3cdd1SConrad Meyer.Fn SHA224_Update "SHA224_CTX *context" "const unsigned char *data" "size_t len"
438ff3cdd1SConrad Meyer.Ft void
448ff3cdd1SConrad Meyer.Fn SHA224_Final "unsigned char digest[32]" "SHA224_CTX *context"
458ff3cdd1SConrad Meyer.Ft "char *"
468ff3cdd1SConrad Meyer.Fn SHA224_End "SHA224_CTX *context" "char *buf"
478ff3cdd1SConrad Meyer.Ft "char *"
48*fd3ced15SKyle Evans.Fn SHA224_Fd "int fd" "char *buf"
49*fd3ced15SKyle Evans.Ft "char *"
50*fd3ced15SKyle Evans.Fn SHA224_FdChunk "int fd" "char *buf" "off_t offset" "off_t length"
51*fd3ced15SKyle Evans.Ft "char *"
528ff3cdd1SConrad Meyer.Fn SHA224_File "const char *filename" "char *buf"
538ff3cdd1SConrad Meyer.Ft "char *"
548ff3cdd1SConrad Meyer.Fn SHA224_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
558ff3cdd1SConrad Meyer.Ft "char *"
568ff3cdd1SConrad Meyer.Fn SHA224_Data "const unsigned char *data" "unsigned int len" "char *buf"
57186c183cSColin Percival.In sha256.h
58186c183cSColin Percival.Ft void
59febd1f4dSPawel Jakub Dawidek.Fn SHA256_Init "SHA256_CTX *context"
60186c183cSColin Percival.Ft void
61febd1f4dSPawel Jakub Dawidek.Fn SHA256_Update "SHA256_CTX *context" "const unsigned char *data" "size_t len"
62186c183cSColin Percival.Ft void
63febd1f4dSPawel Jakub Dawidek.Fn SHA256_Final "unsigned char digest[32]" "SHA256_CTX *context"
64186c183cSColin Percival.Ft "char *"
65febd1f4dSPawel Jakub Dawidek.Fn SHA256_End "SHA256_CTX *context" "char *buf"
66186c183cSColin Percival.Ft "char *"
67*fd3ced15SKyle Evans.Fn SHA256_Fd "int fd" "char *buf"
68*fd3ced15SKyle Evans.Ft "char *"
69*fd3ced15SKyle Evans.Fn SHA256_FdChunk "int fd" "char *buf" "off_t offset" "off_t length"
70*fd3ced15SKyle Evans.Ft "char *"
71186c183cSColin Percival.Fn SHA256_File "const char *filename" "char *buf"
72186c183cSColin Percival.Ft "char *"
73186c183cSColin Percival.Fn SHA256_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
74186c183cSColin Percival.Ft "char *"
75186c183cSColin Percival.Fn SHA256_Data "const unsigned char *data" "unsigned int len" "char *buf"
76186c183cSColin Percival.Sh DESCRIPTION
77186c183cSColin PercivalThe
78186c183cSColin Percival.Li SHA256_
79186c183cSColin Percivalfunctions calculate a 256-bit cryptographic checksum (digest)
80186c183cSColin Percivalfor any number of input bytes.
81186c183cSColin PercivalA cryptographic checksum is a one-way
82186c183cSColin Percivalhash function; that is, it is computationally impractical to find
83186c183cSColin Percivalthe input corresponding to a particular output.
84186c183cSColin PercivalThis net result is
85186c183cSColin Percivala
86186c183cSColin Percival.Dq fingerprint
87186c183cSColin Percivalof the input-data, which does not disclose the actual input.
88186c183cSColin Percival.Pp
89186c183cSColin PercivalThe
90186c183cSColin Percival.Fn SHA256_Init ,
91186c183cSColin Percival.Fn SHA256_Update ,
92186c183cSColin Percivaland
93186c183cSColin Percival.Fn SHA256_Final
94186c183cSColin Percivalfunctions are the core functions.
95186c183cSColin PercivalAllocate an
96186c183cSColin Percival.Vt SHA256_CTX ,
97186c183cSColin Percivalinitialize it with
98186c183cSColin Percival.Fn SHA256_Init ,
99186c183cSColin Percivalrun over the data with
100186c183cSColin Percival.Fn SHA256_Update ,
101186c183cSColin Percivaland finally extract the result using
10266bdf50fSXin LI.Fn SHA256_Final ,
10366bdf50fSXin LIwhich will also erase the
10466bdf50fSXin LI.Vt SHA256_CTX .
105186c183cSColin Percival.Pp
106186c183cSColin Percival.Fn SHA256_End
107186c183cSColin Percivalis a wrapper for
108186c183cSColin Percival.Fn SHA256_Final
109186c183cSColin Percivalwhich converts the return value to a 65-character
110186c183cSColin Percival(including the terminating '\e0')
1112768d705SDag-Erling SmørgravASCII string which represents the 256 bits in hexadecimal.
112186c183cSColin Percival.Pp
113186c183cSColin Percival.Fn SHA256_File
114186c183cSColin Percivalcalculates the digest of a file, and uses
115186c183cSColin Percival.Fn SHA256_End
116186c183cSColin Percivalto return the result.
117186c183cSColin PercivalIf the file cannot be opened, a null pointer is returned.
118186c183cSColin Percival.Fn SHA256_FileChunk
119186c183cSColin Percivalis similar to
120186c183cSColin Percival.Fn SHA256_File ,
121186c183cSColin Percivalbut it only calculates the digest over a byte-range of the file specified,
122186c183cSColin Percivalstarting at
123186c183cSColin Percival.Fa offset
124186c183cSColin Percivaland spanning
125186c183cSColin Percival.Fa length
126186c183cSColin Percivalbytes.
127186c183cSColin PercivalIf the
128186c183cSColin Percival.Fa length
129186c183cSColin Percivalparameter is specified as 0, or more than the length of the remaining part
130186c183cSColin Percivalof the file,
131186c183cSColin Percival.Fn SHA256_FileChunk
132186c183cSColin Percivalcalculates the digest from
133186c183cSColin Percival.Fa offset
134186c183cSColin Percivalto the end of file.
135186c183cSColin Percival.Fn SHA256_Data
136186c183cSColin Percivalcalculates the digest of a chunk of data in memory, and uses
137186c183cSColin Percival.Fn SHA256_End
138186c183cSColin Percivalto return the result.
139186c183cSColin Percival.Pp
140*fd3ced15SKyle EvansThe
141*fd3ced15SKyle Evans.Fn SHA256_Fd
142*fd3ced15SKyle Evansand
143*fd3ced15SKyle Evans.Fn SHA256_FdChunk
144*fd3ced15SKyle Evansfunctions are identical to their
145*fd3ced15SKyle Evans.Fn SHA256_File
146*fd3ced15SKyle Evansand
147*fd3ced15SKyle Evans.Fn SHA256_FileChunk
148*fd3ced15SKyle Evanscounterparts, with the exception that the first argument is an
149*fd3ced15SKyle Evans.Fa fd
150*fd3ced15SKyle Evansinstead of a
151*fd3ced15SKyle Evans.Fa filename .
152*fd3ced15SKyle Evans.Pp
153186c183cSColin PercivalWhen using
154186c183cSColin Percival.Fn SHA256_End ,
155186c183cSColin Percival.Fn SHA256_File ,
156186c183cSColin Percivalor
157186c183cSColin Percival.Fn SHA256_Data ,
158186c183cSColin Percivalthe
159186c183cSColin Percival.Fa buf
160186c183cSColin Percivalargument can be a null pointer, in which case the returned string
161186c183cSColin Percivalis allocated with
162186c183cSColin Percival.Xr malloc 3
163186c183cSColin Percivaland subsequently must be explicitly deallocated using
164186c183cSColin Percival.Xr free 3
165186c183cSColin Percivalafter use.
166186c183cSColin PercivalIf the
167186c183cSColin Percival.Fa buf
168186c183cSColin Percivalargument is non-null it must point to at least 65 characters of buffer space.
1698ff3cdd1SConrad Meyer.Pp
1708ff3cdd1SConrad MeyerSHA224 is identical SHA256, except it has slightly different initialization
1718ff3cdd1SConrad Meyervectors, and is truncated to a shorter digest.
172f26f4cf1SAllan Jude.Sh ERRORS
173f26f4cf1SAllan JudeThe
174f26f4cf1SAllan Jude.Fn SHA256_End
175f26f4cf1SAllan Judefunction called with a null buf argument may fail and return NULL if:
176f26f4cf1SAllan Jude.Bl -tag -width Er
177f26f4cf1SAllan Jude.It Bq Er ENOMEM
178f26f4cf1SAllan JudeInsufficient storage space is available.
179f26f4cf1SAllan Jude.El
180f26f4cf1SAllan Jude.Pp
181f26f4cf1SAllan JudeThe
182f26f4cf1SAllan Jude.Fn SHA256_File
183f26f4cf1SAllan Judeand
184f26f4cf1SAllan Jude.Fn SHA256_FileChunk
185f26f4cf1SAllan Judemay return NULL when underlying
186f26f4cf1SAllan Jude.Xr open 2 ,
187f26f4cf1SAllan Jude.Xr fstat 2 ,
188f26f4cf1SAllan Jude.Xr lseek 2 ,
189f26f4cf1SAllan Judeor
19030f62691SJens Schweikhardt.Xr SHA256_End 3
191f26f4cf1SAllan Judefail.
192186c183cSColin Percival.Sh SEE ALSO
193186c183cSColin Percival.Xr md4 3 ,
194186c183cSColin Percival.Xr md5 3 ,
195186c183cSColin Percival.Xr ripemd 3 ,
196b468a9ffSAllan Jude.Xr sha 3 ,
197b468a9ffSAllan Jude.Xr sha512 3 ,
198b468a9ffSAllan Jude.Xr skein 3
199186c183cSColin Percival.Sh HISTORY
200186c183cSColin PercivalThese functions appeared in
20102d98cd4SColin Percival.Fx 6.0 .
202186c183cSColin Percival.Sh AUTHORS
203186c183cSColin PercivalThe core hash routines were implemented by Colin Percival based on
2042768d705SDag-Erling Smørgravthe published FIPS 180-2 standard.
205186c183cSColin Percival.Sh BUGS
206186c183cSColin PercivalNo method is known to exist which finds two files having the same hash value,
207186c183cSColin Percivalnor to find a file with a specific hash value.
208186c183cSColin PercivalThere is on the other hand no guarantee that such a method does not exist.
209