]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/encrypt.3
crypt.3: srcfix: rewrap source lines
[thirdparty/man-pages.git] / man3 / encrypt.3
1 .\" Copyright 2000 Nicolás Lichtmaier <nick@debian.org>
2 .\" Created 2000-07-22 00:52-0300
3 .\"
4 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
5 .\" This is free documentation; you can redistribute it and/or
6 .\" modify it under the terms of the GNU General Public License as
7 .\" published by the Free Software Foundation; either version 2 of
8 .\" the License, or (at your option) any later version.
9 .\"
10 .\" The GNU General Public License's references to "object code"
11 .\" and "executables" are to be interpreted as the output of any
12 .\" document formatting or typesetting system, including
13 .\" intermediate and printed output.
14 .\"
15 .\" This manual is distributed in the hope that it will be useful,
16 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 .\" GNU General Public License for more details.
19 .\"
20 .\" You should have received a copy of the GNU General Public
21 .\" License along with this manual; if not, see
22 .\" <http://www.gnu.org/licenses/>.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Modified 2002-07-23 19:21:35 CEST 2002 Walter Harms
26 .\" <walter.harms@informatik.uni-oldenburg.de>
27 .\"
28 .\" Modified 2003-04-04, aeb
29 .\"
30 .TH ENCRYPT 3 2017-09-15 "" "Linux Programmer's Manual"
31 .SH NAME
32 encrypt, setkey, encrypt_r, setkey_r \- encrypt 64-bit messages
33 .SH SYNOPSIS
34 .nf
35 .BR "#define _XOPEN_SOURCE" " /* See feature_test_macros(7) */"
36 .B #include <unistd.h>
37 .PP
38 .BI "void encrypt(char " block "[64], int " edflag );
39
40 .BR "#define _XOPEN_SOURCE" " /* See feature_test_macros(7) */"
41 .B #include <stdlib.h>
42 .PP
43 .BI "void setkey(const char *" key );
44
45 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
46 .B "#include <crypt.h>"
47 .PP
48 .BI "void setkey_r(const char *" key ", struct crypt_data *" data );
49 .BI "void encrypt_r(char *" block ", int " edflag \
50 ", struct crypt_data *" data );
51 .fi
52 .PP
53 Each of these requires linking with \fI\-lcrypt\fP.
54 .SH DESCRIPTION
55 These functions encrypt and decrypt 64-bit messages.
56 The
57 .BR setkey ()
58 function sets the key used by
59 .BR encrypt ().
60 The
61 .I key
62 argument used here is an array of 64 bytes, each of which has
63 numerical value 1 or 0.
64 The bytes key[n] where n=8*i-1 are ignored,
65 so that the effective key length is 56 bits.
66 .PP
67 The
68 .BR encrypt ()
69 function modifies the passed buffer, encoding if
70 .I edflag
71 is 0, and decoding if 1 is being passed.
72 Like the
73 .I key
74 argument, also
75 .I block
76 is a bit vector representation of the actual value that is encoded.
77 The result is returned in that same vector.
78 .PP
79 These two functions are not reentrant, that is, the key data is
80 kept in static storage.
81 The functions
82 .BR setkey_r ()
83 and
84 .BR encrypt_r ()
85 are the reentrant versions.
86 They use the following
87 structure to hold the key data:
88 .PP
89 .in +4n
90 .EX
91 struct crypt_data {
92 char keysched[16 * 8];
93 char sb0[32768];
94 char sb1[32768];
95 char sb2[32768];
96 char sb3[32768];
97 char crypt_3_buf[14];
98 char current_salt[2];
99 long int current_saltbits;
100 int direction;
101 int initialized;
102 };
103 .EE
104 .in
105 .PP
106 Before calling
107 .BR setkey_r ()
108 set
109 .I data\->initialized
110 to zero.
111 .SH RETURN VALUE
112 These functions do not return any value.
113 .SH ERRORS
114 Set
115 .I errno
116 to zero before calling the above functions.
117 On success, it is unchanged.
118 .TP
119 .B ENOSYS
120 The function is not provided.
121 (For example because of former USA export restrictions.)
122 .SH ATTRIBUTES
123 For an explanation of the terms used in this section, see
124 .BR attributes (7).
125 .TS
126 allbox;
127 lbw23 lb lb
128 l l l.
129 Interface Attribute Value
130 T{
131 .BR encrypt (),
132 .BR setkey ()
133 T} Thread safety MT-Unsafe race:crypt
134 T{
135 .BR encrypt_r (),
136 .BR setkey_r ()
137 T} Thread safety MT-Safe
138 .TE
139 .SH CONFORMING TO
140 .BR encrypt (),
141 .BR setkey ():
142 POSIX.1-2001, POSIX.1-2008, SUS, SVr4.
143 .PP
144 The functions
145 .BR encrypt_r ()
146 and
147 .BR setkey_r ()
148 are GNU extensions.
149 .SH NOTES
150 .SS Availability in glibc
151 The
152 .BR crypt (),
153 .BR encrypt (),
154 and
155 .BR setkey ()
156 functions are part of the POSIX.1-2008 XSI Options Group for Encryption
157 and are optional. If the interfaces are not available then the symbolic
158 constant
159 .BR _XOPEN_CRYPT
160 is either not defined or defined to -1, and can be checked at runtime with
161 .BR sysconf ().
162 This may be the case if the downstream distribution has switched from glibc
163 crypt to libxcrypt. When recompiling applications in such distributions the
164 user must detect if
165 .BR _XOPEN_CRPYT
166 is not available and include crypt.h for the function prototypes; otherwise
167 libxcrypt is a ABI compatible drop-in replacement.
168 .SS Features in glibc
169 In glibc 2.2, these functions use the DES algorithm.
170 .SH EXAMPLE
171 .EX
172 #define _XOPEN_SOURCE
173 #include <stdio.h>
174 #include <stdlib.h>
175 #include <unistd.h>
176 #include <crypt.h>
177
178 int
179 main(void)
180 {
181 char key[64];
182 char orig[9] = "eggplant";
183 char buf[64];
184 char txt[9];
185 int i, j;
186
187 for (i = 0; i < 64; i++) {
188 key[i] = rand() & 1;
189 }
190
191 for (i = 0; i < 8; i++) {
192 for (j = 0; j < 8; j++) {
193 buf[i * 8 + j] = orig[i] >> j & 1;
194 }
195 setkey(key);
196 }
197 printf("Before encrypting: %s\\n", orig);
198
199 encrypt(buf, 0);
200 for (i = 0; i < 8; i++) {
201 for (j = 0, txt[i] = \(aq\\0\(aq; j < 8; j++) {
202 txt[i] |= buf[i * 8 + j] << j;
203 }
204 txt[8] = \(aq\\0\(aq;
205 }
206 printf("After encrypting: %s\\n", txt);
207
208 encrypt(buf, 1);
209 for (i = 0; i < 8; i++) {
210 for (j = 0, txt[i] = \(aq\\0\(aq; j < 8; j++) {
211 txt[i] |= buf[i * 8 + j] << j;
212 }
213 txt[8] = \(aq\\0\(aq;
214 }
215 printf("After decrypting: %s\\n", txt);
216 exit(EXIT_SUCCESS);
217 }
218 .EE
219 .SH SEE ALSO
220 .BR cbc_crypt (3),
221 .BR crypt (3),
222 .BR ecb_crypt (3),
223 .\" .BR fcrypt (3)