tzfile.5, tzselect.8: sync from tzdb upstream
[man-pages.git] / man3 / encrypt.3
blob2557a5f259d43320632cf1b46bfbdb7869a4df00
1 '\" t
2 .\" Copyright 2000 Nicolás Lichtmaier <nick@debian.org>
3 .\" Created 2000-07-22 00:52-0300
4 .\"
5 .\" SPDX-License-Identifier: GPL-2.0-or-later
6 .\"
7 .\" Modified 2002-07-23 19:21:35 CEST 2002 Walter Harms
8 .\" <walter.harms@informatik.uni-oldenburg.de>
9 .\"
10 .\" Modified 2003-04-04, aeb
11 .\"
12 .TH encrypt 3 (date) "Linux man-pages (unreleased)"
13 .SH NAME
14 encrypt, setkey, encrypt_r, setkey_r \- encrypt 64-bit messages
15 .SH LIBRARY
16 Encryption and decryption library
17 .RI ( libcrypto ", " \-lcrypto )
18 .SH SYNOPSIS
19 .nf
20 .BR "#define _XOPEN_SOURCE" "       /* See feature_test_macros(7) */"
21 .B #include <unistd.h>
22 .PP
23 .BI "[[deprecated]] void encrypt(char " block "[64], int " edflag );
24 .PP
25 .BR "#define _XOPEN_SOURCE" "       /* See feature_test_macros(7) */"
26 .B #include <stdlib.h>
27 .PP
28 .BI "[[deprecated]] void setkey(const char *" key );
29 .PP
30 .BR "#define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
31 .B #include <crypt.h>
32 .PP
33 .BI "[[deprecated]] void setkey_r(const char *" key ", struct crypt_data *" data );
34 .BI "[[deprecated]] void encrypt_r(char *" block ", int " edflag ,
35 .BI "                              struct crypt_data *" data );
36 .fi
37 .SH DESCRIPTION
38 These functions encrypt and decrypt 64-bit messages.
39 The
40 .BR setkey ()
41 function sets the key used by
42 .BR encrypt ().
43 The
44 .I key
45 argument used here is an array of 64 bytes, each of which has
46 numerical value 1 or 0.
47 The bytes key[n] where n=8*i-1 are ignored,
48 so that the effective key length is 56 bits.
49 .PP
50 The
51 .BR encrypt ()
52 function modifies the passed buffer, encoding if
53 .I edflag
54 is 0, and decoding if 1 is being passed.
55 Like the
56 .I key
57 argument, also
58 .I block
59 is a bit vector representation of the actual value that is encoded.
60 The result is returned in that same vector.
61 .PP
62 These two functions are not reentrant, that is, the key data is
63 kept in static storage.
64 The functions
65 .BR setkey_r ()
66 and
67 .BR encrypt_r ()
68 are the reentrant versions.
69 They use the following
70 structure to hold the key data:
71 .PP
72 .in +4n
73 .EX
74 struct crypt_data {
75     char keysched[16 * 8];
76     char sb0[32768];
77     char sb1[32768];
78     char sb2[32768];
79     char sb3[32768];
80     char crypt_3_buf[14];
81     char current_salt[2];
82     long current_saltbits;
83     int  direction;
84     int  initialized;
86 .EE
87 .in
88 .PP
89 Before calling
90 .BR setkey_r ()
91 set
92 .I data\->initialized
93 to zero.
94 .SH RETURN VALUE
95 These functions do not return any value.
96 .SH ERRORS
97 Set
98 .I errno
99 to zero before calling the above functions.
100 On success,
101 .I errno
102 is unchanged.
104 .B ENOSYS
105 The function is not provided.
106 (For example because of former USA export restrictions.)
107 .SH VERSIONS
108 Because they employ the DES block cipher,
109 which is no longer considered secure,
110 .BR encrypt (),
111 .BR encrypt_r (),
112 .BR setkey (),
114 .BR setkey_r ()
115 were removed in glibc 2.28.
116 Applications should switch to a modern cryptography library, such as
117 .BR libgcrypt .
118 .SH ATTRIBUTES
119 For an explanation of the terms used in this section, see
120 .BR attributes (7).
121 .ad l
124 allbox;
125 lbx lb lb
126 l l l.
127 Interface       Attribute       Value
129 .BR encrypt (),
130 .BR setkey ()
131 T}      Thread safety   MT-Unsafe race:crypt
133 .BR encrypt_r (),
134 .BR setkey_r ()
135 T}      Thread safety   MT-Safe
139 .sp 1
140 .SH STANDARDS
141 .BR encrypt (),
142 .BR setkey ():
143 POSIX.1-2001, POSIX.1-2008, SUS, SVr4.
145 The functions
146 .BR encrypt_r ()
148 .BR setkey_r ()
149 are GNU extensions.
150 .SH NOTES
151 .SS Availability in glibc
153 .BR crypt (3).
154 .SS Features in glibc
155 In glibc 2.2, these functions use the DES algorithm.
156 .SH EXAMPLES
157 .\" [[deprecated]] SRC BEGIN (encrypt.c)
159 #define _XOPEN_SOURCE
160 #include <crypt.h>
161 #include <stdio.h>
162 #include <stdlib.h>
163 #include <unistd.h>
166 main(void)
168     char key[64];
169     char orig[9] = "eggplant";
170     char buf[64];
171     char txt[9];
173     for (size_t i = 0; i < 64; i++) {
174         key[i] = rand() & 1;
175     }
177     for (size_t i = 0; i < 8; i++) {
178         for (size_t j = 0; j < 8; j++) {
179             buf[i * 8 + j] = orig[i] >> j & 1;
180         }
181         setkey(key);
182     }
183     printf("Before encrypting: %s\en", orig);
185     encrypt(buf, 0);
186     for (size_t i = 0; i < 8; i++) {
187         for (size_t j = 0, txt[i] = \[aq]\e0\[aq]; j < 8; j++) {
188             txt[i] |= buf[i * 8 + j] << j;
189         }
190         txt[8] = \[aq]\e0\[aq];
191     }
192     printf("After encrypting:  %s\en", txt);
194     encrypt(buf, 1);
195     for (size_t i = 0; i < 8; i++) {
196         for (size_t j = 0, txt[i] = \[aq]\e0\[aq]; j < 8; j++) {
197             txt[i] |= buf[i * 8 + j] << j;
198         }
199         txt[8] = \[aq]\e0\[aq];
200     }
201     printf("After decrypting:  %s\en", txt);
202     exit(EXIT_SUCCESS);
205 .\" SRC END
206 .SH SEE ALSO
207 .BR cbc_crypt (3),
208 .BR crypt (3),
209 .BR ecb_crypt (3)
210 .\" .BR fcrypt (3)