xref: /openbsd/lib/libc/hash/SHA1Init.3 (revision 274d7c50)
1.\"	$OpenBSD: SHA1Init.3,v 1.1 2019/08/30 22:20:43 deraadt 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: August 30 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
121.Tn ASCII
122representation of the 160 bit digest in hexadecimal.
123.Pp
124The
125.Fn SHA1File
126function calculates the digest for a file and returns the result via
127.Fn SHA1End .
128If
129.Fn SHA1File
130is unable to open the file a
131.Dv NULL
132pointer is returned.
133.Pp
134.Fn SHA1FileChunk
135behaves like
136.Fn SHA1File
137but calculates the digest only for that portion of the file starting at
138.Fa offset
139and continuing for
140.Fa length
141bytes or until end of file is reached, whichever comes first.
142A zero
143.Fa length
144can be specified to read until end of file.
145A negative
146.Fa length
147or
148.Fa offset
149will be ignored.
150.Pp
151The
152.Fn SHA1Data
153function
154calculates the digest of an arbitrary string and returns the result via
155.Fn SHA1End .
156.Pp
157For each of the
158.Fn SHA1End ,
159.Fn SHA1File ,
160and
161.Fn SHA1Data
162functions the
163.Fa buf
164parameter should either be a string of at least 41 characters in
165size or a
166.Dv NULL
167pointer.
168In the latter case, space will be dynamically allocated via
169.Xr malloc 3
170and should be freed using
171.Xr free 3
172when it is no longer needed.
173.Sh EXAMPLES
174The following code fragment will calculate the digest for
175the string
176.Qq abc
177which is
178.Dq 0xa9993e364706816aba3e25717850c26c9cd0d89d .
179.Bd -literal -offset indent
180SHA1_CTX sha;
181u_int8_t results[SHA1_DIGEST_LENGTH];
182char *buf;
183int n;
184
185buf = "abc";
186n = strlen(buf);
187SHA1Init(&sha);
188SHA1Update(&sha, (u_int8_t *)buf, n);
189SHA1Final(results, &sha);
190
191/* Print the digest as one long hex value */
192printf("0x");
193for (n = 0; n < SHA1_DIGEST_LENGTH; n++)
194	printf("%02x", results[n]);
195putchar('\en');
196.Ed
197.Pp
198Alternately, the helper functions could be used in the following way:
199.Bd -literal -offset indent
200u_int8_t output[SHA1_DIGEST_STRING_LENGTH];
201char *buf = "abc";
202
203printf("0x%s\en", SHA1Data(buf, strlen(buf), output));
204.Ed
205.Sh SEE ALSO
206.Xr cksum 1 ,
207.Xr sha1 1 ,
208.Xr MD5Init 3 ,
209.Xr RMD160Init 3 ,
210.Xr SHA256INIT 3
211.Sh STANDARDS
212.Rs
213.%A J. Burrows
214.%R FIPS PUB 180-1
215.%T The Secure Hash Standard
216.Re
217.Pp
218.Rs
219.%A D. Eastlake
220.%A P. Jones
221.%D September 2001
222.%R RFC 3174
223.%T US Secure Hash Algorithm 1 (SHA1)
224.Re
225.Sh HISTORY
226The SHA-1 functions appeared in
227.Ox 2.0 .
228.Sh AUTHORS
229.An -nosplit
230This implementation of SHA-1 was written by
231.An Steve Reid .
232.Pp
233The
234.Fn SHA1End ,
235.Fn SHA1File ,
236.Fn SHA1FileChunk ,
237and
238.Fn SHA1Data
239helper functions are derived from code written by
240.An Poul-Henning Kamp .
241