]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libmd/sha256.3
Remove $FreeBSD$: one-line sh pattern
[FreeBSD/FreeBSD.git] / lib / libmd / sha256.3
1 .\"
2 .\" ----------------------------------------------------------------------------
3 .\" "THE BEER-WARE LICENSE" (Revision 42):
4 .\" <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5 .\" can do whatever you want with this stuff. If we meet some day, and you think
6 .\" this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7 .\" ----------------------------------------------------------------------------
8 .\"
9 .\"     From: Id: mdX.3,v 1.14 1999/02/11 20:31:49 wollman Exp
10 .\" $FreeBSD$
11 .\"
12 .Dd July 20, 2018
13 .Dt SHA256 3
14 .Os
15 .Sh NAME
16 .Nm SHA224_Init ,
17 .Nm SHA224_Update ,
18 .Nm SHA224_Final ,
19 .Nm SHA224_End ,
20 .Nm SHA224_File ,
21 .Nm SHA224_FileChunk ,
22 .Nm SHA224_Data ,
23 .Nm SHA256_Init ,
24 .Nm SHA256_Update ,
25 .Nm SHA256_Final ,
26 .Nm SHA256_End ,
27 .Nm SHA256_File ,
28 .Nm SHA256_FileChunk ,
29 .Nm SHA256_Data
30 .Nd calculate the FIPS 180-2 ``SHA-256'' (or SHA-224) message digest
31 .Sh LIBRARY
32 .Lb libmd
33 .Sh SYNOPSIS
34 .In sys/types.h
35 .In sha224.h
36 .Ft void
37 .Fn SHA224_Init "SHA224_CTX *context"
38 .Ft void
39 .Fn SHA224_Update "SHA224_CTX *context" "const unsigned char *data" "size_t len"
40 .Ft void
41 .Fn SHA224_Final "unsigned char digest[32]" "SHA224_CTX *context"
42 .Ft "char *"
43 .Fn SHA224_End "SHA224_CTX *context" "char *buf"
44 .Ft "char *"
45 .Fn SHA224_File "const char *filename" "char *buf"
46 .Ft "char *"
47 .Fn SHA224_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
48 .Ft "char *"
49 .Fn SHA224_Data "const unsigned char *data" "unsigned int len" "char *buf"
50 .In sha256.h
51 .Ft void
52 .Fn SHA256_Init "SHA256_CTX *context"
53 .Ft void
54 .Fn SHA256_Update "SHA256_CTX *context" "const unsigned char *data" "size_t len"
55 .Ft void
56 .Fn SHA256_Final "unsigned char digest[32]" "SHA256_CTX *context"
57 .Ft "char *"
58 .Fn SHA256_End "SHA256_CTX *context" "char *buf"
59 .Ft "char *"
60 .Fn SHA256_File "const char *filename" "char *buf"
61 .Ft "char *"
62 .Fn SHA256_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
63 .Ft "char *"
64 .Fn SHA256_Data "const unsigned char *data" "unsigned int len" "char *buf"
65 .Sh DESCRIPTION
66 The
67 .Li SHA256_
68 functions calculate a 256-bit cryptographic checksum (digest)
69 for any number of input bytes.
70 A cryptographic checksum is a one-way
71 hash function; that is, it is computationally impractical to find
72 the input corresponding to a particular output.
73 This net result is
74 a
75 .Dq fingerprint
76 of the input-data, which does not disclose the actual input.
77 .Pp
78 The
79 .Fn SHA256_Init ,
80 .Fn SHA256_Update ,
81 and
82 .Fn SHA256_Final
83 functions are the core functions.
84 Allocate an
85 .Vt SHA256_CTX ,
86 initialize it with
87 .Fn SHA256_Init ,
88 run over the data with
89 .Fn SHA256_Update ,
90 and finally extract the result using
91 .Fn SHA256_Final ,
92 which will also erase the
93 .Vt SHA256_CTX .
94 .Pp
95 .Fn SHA256_End
96 is a wrapper for
97 .Fn SHA256_Final
98 which converts the return value to a 65-character
99 (including the terminating '\e0')
100 ASCII string which represents the 256 bits in hexadecimal.
101 .Pp
102 .Fn SHA256_File
103 calculates the digest of a file, and uses
104 .Fn SHA256_End
105 to return the result.
106 If the file cannot be opened, a null pointer is returned.
107 .Fn SHA256_FileChunk
108 is similar to
109 .Fn SHA256_File ,
110 but it only calculates the digest over a byte-range of the file specified,
111 starting at
112 .Fa offset
113 and spanning
114 .Fa length
115 bytes.
116 If the
117 .Fa length
118 parameter is specified as 0, or more than the length of the remaining part
119 of the file,
120 .Fn SHA256_FileChunk
121 calculates the digest from
122 .Fa offset
123 to the end of file.
124 .Fn SHA256_Data
125 calculates the digest of a chunk of data in memory, and uses
126 .Fn SHA256_End
127 to return the result.
128 .Pp
129 When using
130 .Fn SHA256_End ,
131 .Fn SHA256_File ,
132 or
133 .Fn SHA256_Data ,
134 the
135 .Fa buf
136 argument can be a null pointer, in which case the returned string
137 is allocated with
138 .Xr malloc 3
139 and subsequently must be explicitly deallocated using
140 .Xr free 3
141 after use.
142 If the
143 .Fa buf
144 argument is non-null it must point to at least 65 characters of buffer space.
145 .Pp
146 SHA224 is identical SHA256, except it has slightly different initialization
147 vectors, and is truncated to a shorter digest.
148 .Sh ERRORS
149 The
150 .Fn SHA256_End
151 function called with a null buf argument may fail and return NULL if:
152 .Bl -tag -width Er
153 .It Bq Er ENOMEM
154 Insufficient storage space is available.
155 .El
156 .Pp
157 The
158 .Fn SHA256_File
159 and
160 .Fn SHA256_FileChunk
161 may return NULL when underlying
162 .Xr open 2 ,
163 .Xr fstat 2 ,
164 .Xr lseek 2 ,
165 or
166 .Xr SHA256_End 3
167 fail.
168 .Sh SEE ALSO
169 .Xr md4 3 ,
170 .Xr md5 3 ,
171 .Xr ripemd 3 ,
172 .Xr sha 3 ,
173 .Xr sha512 3 ,
174 .Xr skein 3
175 .Sh HISTORY
176 These functions appeared in
177 .Fx 6.0 .
178 .Sh AUTHORS
179 The core hash routines were implemented by Colin Percival based on
180 the published FIPS 180-2 standard.
181 .Sh BUGS
182 No method is known to exist which finds two files having the same hash value,
183 nor to find a file with a specific hash value.
184 There is on the other hand no guarantee that such a method does not exist.