]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/encrypt.3
crypt.3: srcfix: rewrap source lines
[thirdparty/man-pages.git] / man3 / encrypt.3
CommitLineData
e00c3a07 1.\" Copyright 2000 Nicolás Lichtmaier <nick@debian.org>
fea681da
MK
2.\" Created 2000-07-22 00:52-0300
3.\"
1dd72f9c 4.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
fea681da
MK
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.\"
faede11f
MK
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/>.
6a8d8745 23.\" %%%LICENSE_END
faede11f 24.\"
fea681da
MK
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.\"
4b8c67d9 30.TH ENCRYPT 3 2017-09-15 "" "Linux Programmer's Manual"
fea681da
MK
31.SH NAME
32encrypt, setkey, encrypt_r, setkey_r \- encrypt 64-bit messages
33.SH SYNOPSIS
3f89e93d 34.nf
b80f966b 35.BR "#define _XOPEN_SOURCE" " /* See feature_test_macros(7) */"
fea681da 36.B #include <unistd.h>
68e4db0a 37.PP
fea681da 38.BI "void encrypt(char " block "[64], int " edflag );
f90f031e 39
b80f966b 40.BR "#define _XOPEN_SOURCE" " /* See feature_test_macros(7) */"
fea681da 41.B #include <stdlib.h>
68e4db0a 42.PP
fea681da 43.BI "void setkey(const char *" key );
f90f031e 44
b80f966b 45.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
0daa9e92 46.B "#include <crypt.h>"
68e4db0a 47.PP
b9f02710 48.BI "void setkey_r(const char *" key ", struct crypt_data *" data );
b9f02710
MK
49.BI "void encrypt_r(char *" block ", int " edflag \
50", struct crypt_data *" data );
3f89e93d 51.fi
68e4db0a 52.PP
e9329f6d 53Each of these requires linking with \fI\-lcrypt\fP.
fea681da 54.SH DESCRIPTION
c13182ef
MK
55These functions encrypt and decrypt 64-bit messages.
56The
63aa9df0 57.BR setkey ()
fea681da 58function sets the key used by
63aa9df0 59.BR encrypt ().
fea681da
MK
60The
61.I key
c4bb193f 62argument used here is an array of 64 bytes, each of which has
c13182ef
MK
63numerical value 1 or 0.
64The bytes key[n] where n=8*i-1 are ignored,
fea681da
MK
65so that the effective key length is 56 bits.
66.PP
c13182ef
MK
67The
68.BR encrypt ()
b5cc2ffb 69function modifies the passed buffer, encoding if
fea681da 70.I edflag
c13182ef 71is 0, and decoding if 1 is being passed.
c4bb193f
MK
72Like the
73.I key
74argument, also
fea681da
MK
75.I block
76is a bit vector representation of the actual value that is encoded.
77The result is returned in that same vector.
78.PP
79These two functions are not reentrant, that is, the key data is
c13182ef
MK
80kept in static storage.
81The functions
63aa9df0 82.BR setkey_r ()
fea681da 83and
63aa9df0 84.BR encrypt_r ()
c13182ef
MK
85are the reentrant versions.
86They use the following
fea681da 87structure to hold the key data:
e646a1ba 88.PP
bd191423 89.in +4n
e646a1ba 90.EX
fea681da 91struct crypt_data {
42ca79eb
MK
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;
fea681da 102};
b8302363 103.EE
bd191423 104.in
e5056894 105.PP
fea681da 106Before calling
63aa9df0 107.BR setkey_r ()
fea681da 108set
94e9d9fe 109.I data\->initialized
fea681da 110to zero.
47297adb 111.SH RETURN VALUE
fea681da
MK
112These functions do not return any value.
113.SH ERRORS
114Set
115.I errno
116to zero before calling the above functions.
117On success, it is unchanged.
118.TP
0daa9e92 119.B ENOSYS
fea681da
MK
120The function is not provided.
121(For example because of former USA export restrictions.)
4fc2f647 122.SH ATTRIBUTES
38688339
MK
123For an explanation of the terms used in this section, see
124.BR attributes (7).
125.TS
126allbox;
127lbw23 lb lb
128l l l.
129Interface Attribute Value
130T{
131.BR encrypt (),
4fc2f647 132.BR setkey ()
89456d09 133T} Thread safety MT-Unsafe race:crypt
38688339
MK
134T{
135.BR encrypt_r (),
4fc2f647 136.BR setkey_r ()
38688339
MK
137T} Thread safety MT-Safe
138.TE
47297adb 139.SH CONFORMING TO
d0aab8a9
MK
140.BR encrypt (),
141.BR setkey ():
142POSIX.1-2001, POSIX.1-2008, SUS, SVr4.
847e0d88 143.PP
2b2581ee
MK
144The functions
145.BR encrypt_r ()
146and
147.BR setkey_r ()
148are GNU extensions.
149.SH NOTES
629a86ec
CD
150.SS Availability in glibc
151The
152.BR crypt (),
153.BR encrypt (),
154and
155.BR setkey ()
156functions are part of the POSIX.1-2008 XSI Options Group for Encryption
157and are optional. If the interfaces are not available then the symbolic
158constant
159.BR _XOPEN_CRYPT
160is either not defined or defined to -1, and can be checked at runtime with
161.BR sysconf ().
162This may be the case if the downstream distribution has switched from glibc
163crypt to libxcrypt. When recompiling applications in such distributions the
164user must detect if
165.BR _XOPEN_CRPYT
166is not available and include crypt.h for the function prototypes; otherwise
167libxcrypt is a ABI compatible drop-in replacement.
168.SS Features in glibc
a53a30b7 169In glibc 2.2, these functions use the DES algorithm.
fea681da 170.SH EXAMPLE
207050fa 171.EX
5aa145bd 172#define _XOPEN_SOURCE
ec5308ca 173#include <stdio.h>
5aa145bd 174#include <stdlib.h>
ec5308ca
RS
175#include <unistd.h>
176#include <crypt.h>
fea681da 177
cf0a9ace 178int
c13182ef 179main(void)
cf0a9ace 180{
ec5308ca
RS
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);
cf0a9ace 207
ec5308ca
RS
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);
fea681da 217}
207050fa 218.EE
47297adb 219.SH SEE ALSO
fea681da
MK
220.BR cbc_crypt (3),
221.BR crypt (3),
222.BR ecb_crypt (3),
0a4f8b7b 223.\" .BR fcrypt (3)