Debian Codegraph @

upstream/debian-trixie/man3/encrypt.3

raw
1 '\" t
2 .\" Copyright 2000 Nicolás Lichtmaier <[email protected]>
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 .\" <[email protected]>
9 .\"
10 .\" Modified 2003-04-04, aeb
11 .\"
12 .TH encrypt 3 2024-06-15 "Linux man-pages 6.9.1"
13 .SH NAME
14 encrypt, setkey, encrypt_r, setkey_r \- encrypt 64-bit messages
15 .SH LIBRARY
16 Password hashing library
17 .RI ( libcrypt ", " \-lcrypt )
18 .SH SYNOPSIS
19 .nf
20 .BR "#define _XOPEN_SOURCE" " /* See feature_test_macros(7) */"
21 .B #include <unistd.h>
22 .P
23 .BI "[[deprecated]] void encrypt(char " block "[64], int " edflag );
24 .P
25 .BR "#define _XOPEN_SOURCE" " /* See feature_test_macros(7) */"
26 .B #include <stdlib.h>
27 .P
28 .BI "[[deprecated]] void setkey(const char *" key );
29 .P
30 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
31 .B #include <crypt.h>
32 .P
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 .P
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 .P
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 .P
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;
85 };
86 .EE
87 .in
88 .P
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.
103 .TP
104 .B ENOSYS
105 The function is not provided.
106 (For example because of former USA export restrictions.)
107 .SH ATTRIBUTES
108 For an explanation of the terms used in this section, see
109 .BR attributes (7).
110 .TS
111 allbox;
112 lbx lb lb
113 l l l.
114 Interface Attribute Value
115 T{
116 .na
117 .nh
118 .BR encrypt (),
119 .BR setkey ()
120 T} Thread safety MT-Unsafe race:crypt
121 T{
122 .na
123 .nh
124 .BR encrypt_r (),
125 .BR setkey_r ()
126 T} Thread safety MT-Safe
127 .TE
128 .SH STANDARDS
129 .TP
130 .BR encrypt ()
131 .TQ
132 .BR setkey ()
133 POSIX.1-2008.
134 .TP
135 .BR encrypt_r ()
136 .TQ
137 .BR setkey_r ()
138 None.
139 .SH HISTORY
140 Removed in glibc 2.28.
141 .P
142 Because they employ the DES block cipher,
143 which is no longer considered secure,
144 these functions were removed from glibc.
145 Applications should switch to a modern cryptography library, such as
146 .BR libgcrypt .
147 .TP
148 .BR encrypt ()
149 .TQ
150 .BR setkey ()
151 POSIX.1-2001, SUS, SVr4.
152 .SS Availability in glibc
153 See
154 .BR crypt (3).
155 .SS Features in glibc
156 In glibc 2.2, these functions use the DES algorithm.
157 .SH EXAMPLES
158 .\" SRC BEGIN (encrypt.c)
159 .EX
160 #define _XOPEN_SOURCE
161 #include <crypt.h>
162 #include <stdio.h>
163 #include <stdlib.h>
164 #include <unistd.h>
165 \&
166 int
167 main(void)
168 {
169 char key[64];
170 char orig[9] = "eggplant";
171 char buf[64];
172 char txt[9];
173 \&
174 for (size_t i = 0; i < 64; i++) {
175 key[i] = rand() & 1;
176 }
177 \&
178 for (size_t i = 0; i < 8; i++) {
179 for (size_t j = 0; j < 8; j++) {
180 buf[i * 8 + j] = orig[i] >> j & 1;
181 }
182 setkey(key);
183 }
184 printf("Before encrypting: %s\[rs]n", orig);
185 \&
186 encrypt(buf, 0);
187 for (size_t i = 0; i < 8; i++) {
188 for (size_t j = 0, txt[i] = \[aq]\[rs]0\[aq]; j < 8; j++) {
189 txt[i] |= buf[i * 8 + j] << j;
190 }
191 txt[8] = \[aq]\[rs]0\[aq];
192 }
193 printf("After encrypting: %s\[rs]n", txt);
194 \&
195 encrypt(buf, 1);
196 for (size_t i = 0; i < 8; i++) {
197 for (size_t j = 0, txt[i] = \[aq]\[rs]0\[aq]; j < 8; j++) {
198 txt[i] |= buf[i * 8 + j] << j;
199 }
200 txt[8] = \[aq]\[rs]0\[aq];
201 }
202 printf("After decrypting: %s\[rs]n", txt);
203 exit(EXIT_SUCCESS);
204 }
205 .EE
206 .\" SRC END
207 .SH SEE ALSO
208 .BR cbc_crypt (3),
209 .BR crypt (3),
210 .BR ecb_crypt (3)
211 .\" .BR fcrypt (3)