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