xref: /openbsd/lib/libc/hash/SHA1Init.3 (revision 771fbea0)
1.\"	$OpenBSD: SHA1Init.3,v 1.2 2019/12/05 21:45:05 jmc Exp $
2.\"
3.\" Copyright (c) 1997, 2004 Todd C. Miller <millert@openbsd.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.\" See http://csrc.nist.gov/publications/fips/fips180-1/fip180-1.txt
18.\" for the detailed standard
19.\"
20.Dd $Mdocdate: December 5 2019 $
21.Dt SHA1INIT 3
22.Os
23.Sh NAME
24.Nm SHA1Init ,
25.Nm SHA1Update ,
26.Nm SHA1Pad ,
27.Nm SHA1Final ,
28.Nm SHA1Transform ,
29.Nm SHA1End ,
30.Nm SHA1File ,
31.Nm SHA1FileChunk ,
32.Nm SHA1Data
33.Nd calculate the NIST Secure Hash Algorithm
34.Sh SYNOPSIS
35.In sys/types.h
36.In sha1.h
37.Ft void
38.Fn SHA1Init "SHA1_CTX *context"
39.Ft void
40.Fn SHA1Update "SHA1_CTX *context" "const u_int8_t *data" "size_t len"
41.Ft void
42.Fn SHA1Pad "SHA1_CTX *context"
43.Ft void
44.Fn SHA1Final "u_int8_t digest[SHA1_DIGEST_LENGTH]" "SHA1_CTX *context"
45.Ft void
46.Fn SHA1Transform "u_int32_t state[5]" "const u_int8_t buffer[SHA1_BLOCK_LENGTH]"
47.Ft "char *"
48.Fn SHA1End "SHA1_CTX *context" "char *buf"
49.Ft "char *"
50.Fn SHA1File "const char *filename" "char *buf"
51.Ft "char *"
52.Fn SHA1FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
53.Ft "char *"
54.Fn SHA1Data "const u_int8_t *data" "size_t len" "char *buf"
55.Sh DESCRIPTION
56The SHA1 functions implement the NIST Secure Hash Algorithm (SHA-1),
57FIPS PUB 180-1.
58SHA-1 is used to generate a condensed representation
59of a message called a message digest.
60The algorithm takes a
61message less than 2^64 bits as input and produces a 160-bit digest
62suitable for use as a digital signature.
63.Pp
64SHA-1 has been broken; it should only be used where necessary for
65backward compatibility.
66The attack on SHA-1 is in the nature of finding
67.Dq collisions
68\(em that is, multiple inputs which hash to the same value.
69It is still unlikely for an attacker to be able to determine the exact
70original input given a hash value.
71.Pp
72The
73.Fn SHA1Init
74function initializes a SHA1_CTX
75.Fa context
76for use with
77.Fn SHA1Update ,
78and
79.Fn SHA1Final .
80The
81.Fn SHA1Update
82function adds
83.Fa data
84of length
85.Fa len
86to the SHA1_CTX specified by
87.Fa context .
88.Fn SHA1Final
89is called when all data has been added via
90.Fn SHA1Update
91and stores a message digest in the
92.Fa digest
93parameter.
94.Pp
95The
96.Fn SHA1Pad
97function can be used to apply padding to the message digest as in
98.Fn SHA1Final ,
99but the current context can still be used with
100.Fn SHA1Update .
101.Pp
102The
103.Fn SHA1Transform
104function is used by
105.Fn SHA1Update
106to hash 512-bit blocks and forms the core of the algorithm.
107Most programs should use the interface provided by
108.Fn SHA1Init ,
109.Fn SHA1Update
110and
111.Fn SHA1Final
112instead of calling
113.Fn SHA1Transform
114directly.
115.Pp
116The
117.Fn SHA1End
118function is a front end for
119.Fn SHA1Final
120which converts the digest into an ASCII representation
121of the 160 bit digest in hexadecimal.
122.Pp
123The
124.Fn SHA1File
125function calculates the digest for a file and returns the result via
126.Fn SHA1End .
127If
128.Fn SHA1File
129is unable to open the file a
130.Dv NULL
131pointer is returned.
132.Pp
133.Fn SHA1FileChunk
134behaves like
135.Fn SHA1File
136but calculates the digest only for that portion of the file starting at
137.Fa offset
138and continuing for
139.Fa length
140bytes or until end of file is reached, whichever comes first.
141A zero
142.Fa length
143can be specified to read until end of file.
144A negative
145.Fa length
146or
147.Fa offset
148will be ignored.
149.Pp
150The
151.Fn SHA1Data
152function
153calculates the digest of an arbitrary string and returns the result via
154.Fn SHA1End .
155.Pp
156For each of the
157.Fn SHA1End ,
158.Fn SHA1File ,
159and
160.Fn SHA1Data
161functions the
162.Fa buf
163parameter should either be a string of at least 41 characters in
164size or a
165.Dv NULL
166pointer.
167In the latter case, space will be dynamically allocated via
168.Xr malloc 3
169and should be freed using
170.Xr free 3
171when it is no longer needed.
172.Sh EXAMPLES
173The following code fragment will calculate the digest for
174the string
175.Qq abc
176which is
177.Dq 0xa9993e364706816aba3e25717850c26c9cd0d89d .
178.Bd -literal -offset indent
179SHA1_CTX sha;
180u_int8_t results[SHA1_DIGEST_LENGTH];
181char *buf;
182int n;
183
184buf = "abc";
185n = strlen(buf);
186SHA1Init(&sha);
187SHA1Update(&sha, (u_int8_t *)buf, n);
188SHA1Final(results, &sha);
189
190/* Print the digest as one long hex value */
191printf("0x");
192for (n = 0; n < SHA1_DIGEST_LENGTH; n++)
193	printf("%02x", results[n]);
194putchar('\en');
195.Ed
196.Pp
197Alternately, the helper functions could be used in the following way:
198.Bd -literal -offset indent
199u_int8_t output[SHA1_DIGEST_STRING_LENGTH];
200char *buf = "abc";
201
202printf("0x%s\en", SHA1Data(buf, strlen(buf), output));
203.Ed
204.Sh SEE ALSO
205.Xr cksum 1 ,
206.Xr sha1 1 ,
207.Xr MD5Init 3 ,
208.Xr RMD160Init 3 ,
209.Xr SHA256Init 3
210.Sh STANDARDS
211.Rs
212.%A J. Burrows
213.%R FIPS PUB 180-1
214.%T The Secure Hash Standard
215.Re
216.Pp
217.Rs
218.%A D. Eastlake
219.%A P. Jones
220.%D September 2001
221.%R RFC 3174
222.%T US Secure Hash Algorithm 1 (SHA1)
223.Re
224.Sh HISTORY
225The SHA-1 functions appeared in
226.Ox 2.0 .
227.Sh AUTHORS
228.An -nosplit
229This implementation of SHA-1 was written by
230.An Steve Reid .
231.Pp
232The
233.Fn SHA1End ,
234.Fn SHA1File ,
235.Fn SHA1FileChunk ,
236and
237.Fn SHA1Data
238helper functions are derived from code written by
239.An Poul-Henning Kamp .
240