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