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