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