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 February 25, 1999 13.Dt SHA 3 14.Os 15.Sh NAME 16.Nm SHA_Init , 17.Nm SHA_Update , 18.Nm SHA_Final , 19.Nm SHA_End , 20.Nm SHA_File , 21.Nm SHA_FileChunk , 22.Nm SHA_Data , 23.Nm SHA1_Init , 24.Nm SHA1_Update , 25.Nm SHA1_Final , 26.Nm SHA1_End , 27.Nm SHA1_File , 28.Nm SHA1_FileChunk , 29.Nm SHA1_Data 30.Nd calculate the FIPS 160 and 160-1 ``SHA'' message digests 31.Sh LIBRARY 32.Lb libmd 33.Sh SYNOPSIS 34.In sys/types.h 35.In sha.h 36.Ft void 37.Fn SHA_Init "SHA_CTX *context" 38.Ft void 39.Fn SHA_Update "SHA_CTX *context" "const unsigned char *data" "unsigned int len" 40.Ft void 41.Fn SHA_Final "unsigned char digest[20]" "SHA_CTX *context" 42.Ft "char *" 43.Fn SHA_End "SHA_CTX *context" "char *buf" 44.Ft "char *" 45.Fn SHA_File "const char *filename" "char *buf" 46.Ft "char *" 47.Fn SHA_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length" 48.Ft "char *" 49.Fn SHA_Data "const unsigned char *data" "unsigned int len" "char *buf" 50.Ft void 51.Fn SHA1_Init "SHA_CTX *context" 52.Ft void 53.Fn SHA1_Update "SHA_CTX *context" "const unsigned char *data" "unsigned int len" 54.Ft void 55.Fn SHA1_Final "unsigned char digest[20]" "SHA_CTX *context" 56.Ft "char *" 57.Fn SHA1_End "SHA_CTX *context" "char *buf" 58.Ft "char *" 59.Fn SHA1_File "const char *filename" "char *buf" 60.Ft "char *" 61.Fn SHA1_FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length" 62.Ft "char *" 63.Fn SHA1_Data "const unsigned char *data" "unsigned int len" "char *buf" 64.Sh DESCRIPTION 65The 66.Li SHA_ 67and 68.Li SHA1_ 69functions calculate a 160-bit cryptographic checksum (digest) 70for any number of input bytes. A cryptographic checksum is a one-way 71hash function; that is, it is computationally impractical to find 72the input corresponding to a particular output. This net result is 73a ``fingerprint'' of the input-data, which doesn't disclose the actual 74input. 75.Pp 76.Tn SHA 77(or 78.Tn SHA-0 ) 79is the original Secure Hash Algorithm specified in 80.Tn FIPS 81160. It was quickly proven insecure, and has been superseded by 82.Tn SHA-1 . 83.Tn SHA-0 84is included for compatibility purposes only. 85.Pp 86The 87.Fn SHA1_Init , 88.Fn SHA1_Update , 89and 90.Fn SHA1_Final 91functions are the core functions. Allocate an SHA_CTX, initialize it with 92.Fn SHA1_Init , 93run over the data with 94.Fn SHA1_Update , 95and finally extract the result using 96.Fn SHA1_Final . 97.Pp 98.Fn SHA1_End 99is a wrapper for 100.Fn SHA1_Final 101which converts the return value to a 41-character 102(including the terminating '\e0') 103.Tn ASCII 104string which represents the 160 bits in hexadecimal. 105.Pp 106.Fn SHA1_File 107calculates the digest of a file, and uses 108.Fn SHA1_End 109to return the result. 110If the file cannot be opened, a null pointer is returned. 111.Fn SHA1_FileChunk 112is similar to 113.Fn SHA1_File , 114but it only calculates the digest over a byte-range of the file specified, 115starting at 116.Fa offset 117and spanning 118.Fa length 119bytes. 120If the 121.Fa length 122parameter is specified as 0, or more than the length of the remaining part 123of the file, 124.Fn SHA1_FileChunk 125calculates the digest from 126.Fa offset 127to the end of file. 128.Fn SHA1_Data 129calculates the digest of a chunk of data in memory, and uses 130.Fn SHA1_End 131to return the result. 132.Pp 133When using 134.Fn SHA1_End , 135.Fn SHA1_File , 136or 137.Fn SHA1_Data , 138the 139.Fa buf 140argument can be a null pointer, in which case the returned string 141is allocated with 142.Xr malloc 3 143and subsequently must be explicitly deallocated using 144.Xr free 3 145after use. 146If the 147.Fa buf 148argument is non-null it must point to at least 41 characters of buffer space. 149.Sh SEE ALSO 150.Xr md2 3 , 151.Xr md4 3 , 152.Xr md5 3 , 153.Xr ripemd 3 154.Sh AUTHORS 155The core hash routines were implemented by Eric Young based on the 156published 157.Tn FIPS 158standards. 159.Sh HISTORY 160These functions appeared in 161.Fx 4.0 . 162.Sh BUGS 163No method is known to exist which finds two files having the same hash value, 164nor to find a file with a specific hash value. 165There is on the other hand no guarantee that such a method doesn't exist. 166.Pp 167The 168.Tn IA32 169(Intel) implementation of 170.Tn SHA-1 171makes heavy use of the 172.Ql bswapl 173instruction, which is not present on the original 80386. Attempts 174to use 175.Tn SHA-1 176on those processors will cause an illegal instruction trap. 177(Arguably, the kernel should simply emulate this instruction.) 178