Debian Codegraph @

upstream/debian-unstable/man3/encrypt.3

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