1ENCRYPT(3)                 Linux Programmer's Manual                ENCRYPT(3)
2
3
4

NAME

6       encrypt, setkey, encrypt_r, setkey_r - encrypt 64-bit messages
7

SYNOPSIS

9       #define _XOPEN_SOURCE       /* See feature_test_macros(7) */
10       #include <unistd.h>
11
12       void encrypt(char block[64], int edflag);
13
14       #define _XOPEN_SOURCE       /* See feature_test_macros(7) */
15       #include <stdlib.h>
16
17       void setkey(const char *key);
18
19       #define _GNU_SOURCE         /* See feature_test_macros(7) */
20       #include <crypt.h>
21
22       void setkey_r(const char *key, struct crypt_data *data);
23       void encrypt_r(char *block, int edflag, struct crypt_data *data);
24
25       Each of these requires linking with -lcrypt.
26

DESCRIPTION

28       These  functions  encrypt  and  decrypt  64-bit messages.  The setkey()
29       function sets the key used by encrypt().  The key argument used here is
30       an  array  of  64 bytes, each of which has numerical value 1 or 0.  The
31       bytes key[n] where n=8*i-1 are  ignored,  so  that  the  effective  key
32       length is 56 bits.
33
34       The  encrypt()  function modifies the passed buffer, encoding if edflag
35       is 0, and decoding if 1 is being passed.  Like the key  argument,  also
36       block  is  a  bit vector representation of the actual value that is en‐
37       coded.  The result is returned in that same vector.
38
39       These two functions are not reentrant, that is, the key data is kept in
40       static storage.  The functions setkey_r() and encrypt_r() are the reen‐
41       trant versions.  They use the following structure to hold the key data:
42
43           struct crypt_data {
44               char keysched[16 * 8];
45               char sb0[32768];
46               char sb1[32768];
47               char sb2[32768];
48               char sb3[32768];
49               char crypt_3_buf[14];
50               char current_salt[2];
51               long current_saltbits;
52               int  direction;
53               int  initialized;
54           };
55
56       Before calling setkey_r() set data->initialized to zero.
57

RETURN VALUE

59       These functions do not return any value.
60

ERRORS

62       Set errno to zero before calling the above functions.  On success,  er‐
63       rno is unchanged.
64
65       ENOSYS The  function  is  not provided.  (For example because of former
66              USA export restrictions.)
67

VERSIONS

69       Because they employ the DES block cipher, which is no longer considered
70       secure,  crypt(),  crypt_r(),  setkey(), and setkey_r() were removed in
71       glibc 2.28.  Applications should switch to a  modern  cryptography  li‐
72       brary, such as libgcrypt.
73

ATTRIBUTES

75       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
76       tributes(7).
77
78       ┌───────────────────────────────┬───────────────┬──────────────────────┐
79Interface                      Attribute     Value                
80       ├───────────────────────────────┼───────────────┼──────────────────────┤
81encrypt(), setkey()            │ Thread safety │ MT-Unsafe race:crypt │
82       ├───────────────────────────────┼───────────────┼──────────────────────┤
83encrypt_r(), setkey_r()        │ Thread safety │ MT-Safe              │
84       └───────────────────────────────┴───────────────┴──────────────────────┘
85

CONFORMING TO

87       encrypt(), setkey(): POSIX.1-2001, POSIX.1-2008, SUS, SVr4.
88
89       The functions encrypt_r() and setkey_r() are GNU extensions.
90

NOTES

92   Availability in glibc
93       See crypt(3).
94
95   Features in glibc
96       In glibc 2.2, these functions use the DES algorithm.
97

EXAMPLES

99       #define _XOPEN_SOURCE
100       #include <stdio.h>
101       #include <stdlib.h>
102       #include <unistd.h>
103       #include <crypt.h>
104
105       int
106       main(void)
107       {
108           char key[64];
109           char orig[9] = "eggplant";
110           char buf[64];
111           char txt[9];
112
113           for (int i = 0; i < 64; i++) {
114               key[i] = rand() & 1;
115           }
116
117           for (int i = 0; i < 8; i++) {
118               for (int j = 0; j < 8; j++) {
119                   buf[i * 8 + j] = orig[i] >> j & 1;
120               }
121               setkey(key);
122           }
123           printf("Before encrypting: %s\n", orig);
124
125           encrypt(buf, 0);
126           for (int i = 0; i < 8; i++) {
127               for (int j = 0, txt[i] = '\0'; j < 8; j++) {
128                   txt[i] |= buf[i * 8 + j] << j;
129               }
130               txt[8] = '\0';
131           }
132           printf("After encrypting:  %s\n", txt);
133
134           encrypt(buf, 1);
135           for (int i = 0; i < 8; i++) {
136               for (int j = 0, txt[i] = '\0'; j < 8; j++) {
137                   txt[i] |= buf[i * 8 + j] << j;
138               }
139               txt[8] = '\0';
140           }
141           printf("After decrypting:  %s\n", txt);
142           exit(EXIT_SUCCESS);
143       }
144

SEE ALSO

146       cbc_crypt(3), crypt(3), ecb_crypt(3),
147

COLOPHON

149       This page is part of release 5.12 of the Linux  man-pages  project.   A
150       description  of  the project, information about reporting bugs, and the
151       latest    version    of    this    page,    can     be     found     at
152       https://www.kernel.org/doc/man-pages/.
153
154
155
156                                  2021-03-22                        ENCRYPT(3)
Impressum