1 /*
2 * Copyright 1998-2021 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /*
11 * DES low level APIs are deprecated for public use, but still ok for internal
12 * use.
13 */
14 #include "internal/deprecated.h"
15
16 /* NOCW */
17 #include <stdio.h>
18 #ifdef _OSD_POSIX
19 #ifndef CHARSET_EBCDIC
20 #define CHARSET_EBCDIC 1
21 #endif
22 #endif
23 #ifdef CHARSET_EBCDIC
24 #include <openssl/ebcdic.h>
25 #endif
26
27 #include <openssl/crypto.h>
28 #include "des_local.h"
29
30 /*
31 * Added more values to handle illegal salt values the way normal crypt()
32 * implementations do.
33 */
34 static const unsigned char con_salt[128] = {
35 0xD2,
36 0xD3,
37 0xD4,
38 0xD5,
39 0xD6,
40 0xD7,
41 0xD8,
42 0xD9,
43 0xDA,
44 0xDB,
45 0xDC,
46 0xDD,
47 0xDE,
48 0xDF,
49 0xE0,
50 0xE1,
51 0xE2,
52 0xE3,
53 0xE4,
54 0xE5,
55 0xE6,
56 0xE7,
57 0xE8,
58 0xE9,
59 0xEA,
60 0xEB,
61 0xEC,
62 0xED,
63 0xEE,
64 0xEF,
65 0xF0,
66 0xF1,
67 0xF2,
68 0xF3,
69 0xF4,
70 0xF5,
71 0xF6,
72 0xF7,
73 0xF8,
74 0xF9,
75 0xFA,
76 0xFB,
77 0xFC,
78 0xFD,
79 0xFE,
80 0xFF,
81 0x00,
82 0x01,
83 0x02,
84 0x03,
85 0x04,
86 0x05,
87 0x06,
88 0x07,
89 0x08,
90 0x09,
91 0x0A,
92 0x0B,
93 0x05,
94 0x06,
95 0x07,
96 0x08,
97 0x09,
98 0x0A,
99 0x0B,
100 0x0C,
101 0x0D,
102 0x0E,
103 0x0F,
104 0x10,
105 0x11,
106 0x12,
107 0x13,
108 0x14,
109 0x15,
110 0x16,
111 0x17,
112 0x18,
113 0x19,
114 0x1A,
115 0x1B,
116 0x1C,
117 0x1D,
118 0x1E,
119 0x1F,
120 0x20,
121 0x21,
122 0x22,
123 0x23,
124 0x24,
125 0x25,
126 0x20,
127 0x21,
128 0x22,
129 0x23,
130 0x24,
131 0x25,
132 0x26,
133 0x27,
134 0x28,
135 0x29,
136 0x2A,
137 0x2B,
138 0x2C,
139 0x2D,
140 0x2E,
141 0x2F,
142 0x30,
143 0x31,
144 0x32,
145 0x33,
146 0x34,
147 0x35,
148 0x36,
149 0x37,
150 0x38,
151 0x39,
152 0x3A,
153 0x3B,
154 0x3C,
155 0x3D,
156 0x3E,
157 0x3F,
158 0x40,
159 0x41,
160 0x42,
161 0x43,
162 0x44,
163 };
164
165 static const unsigned char cov_2char[64] = {
166 0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
167 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44,
168 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C,
169 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54,
170 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62,
171 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
172 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72,
173 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A
174 };
175
DES_crypt(const char * buf,const char * salt)176 char *DES_crypt(const char *buf, const char *salt)
177 {
178 static char buff[14];
179
180 #ifndef CHARSET_EBCDIC
181 return DES_fcrypt(buf, salt, buff);
182 #else
183 char e_salt[2 + 1];
184 char e_buf[32 + 1]; /* replace 32 by 8 ? */
185 char *ret;
186
187 if (salt[0] == '\0' || salt[1] == '\0')
188 return NULL;
189
190 /* Copy salt, convert to ASCII. */
191 e_salt[0] = salt[0];
192 e_salt[1] = salt[1];
193 e_salt[2] = '\0';
194 ebcdic2ascii(e_salt, e_salt, sizeof(e_salt));
195
196 /* Convert password to ASCII. */
197 OPENSSL_strlcpy(e_buf, buf, sizeof(e_buf));
198 ebcdic2ascii(e_buf, e_buf, sizeof(e_buf));
199
200 /* Encrypt it (from/to ASCII); if it worked, convert back. */
201 ret = DES_fcrypt(e_buf, e_salt, buff);
202 if (ret != NULL)
203 ascii2ebcdic(ret, ret, strlen(ret));
204
205 return ret;
206 #endif
207 }
208
DES_fcrypt(const char * buf,const char * salt,char * ret)209 char *DES_fcrypt(const char *buf, const char *salt, char *ret)
210 {
211 unsigned int i, j, x, y;
212 DES_LONG Eswap0, Eswap1;
213 DES_LONG out[2], ll;
214 DES_cblock key;
215 DES_key_schedule ks;
216 unsigned char bb[9];
217 unsigned char *b = bb;
218 unsigned char c, u;
219
220 x = ret[0] = salt[0];
221 if (x == 0 || x >= sizeof(con_salt))
222 return NULL;
223 Eswap0 = con_salt[x] << 2;
224 x = ret[1] = salt[1];
225 if (x == 0 || x >= sizeof(con_salt))
226 return NULL;
227 Eswap1 = con_salt[x] << 6;
228
229 /*
230 * EAY r=strlen(buf); r=(r+7)/8;
231 */
232 for (i = 0; i < 8; i++) {
233 c = *(buf++);
234 if (!c)
235 break;
236 key[i] = (c << 1);
237 }
238 for (; i < 8; i++)
239 key[i] = 0;
240
241 DES_set_key_unchecked(&key, &ks);
242 fcrypt_body(&(out[0]), &ks, Eswap0, Eswap1);
243
244 ll = out[0];
245 l2c(ll, b);
246 ll = out[1];
247 l2c(ll, b);
248 y = 0;
249 u = 0x80;
250 bb[8] = 0;
251 for (i = 2; i < 13; i++) {
252 c = 0;
253 for (j = 0; j < 6; j++) {
254 c <<= 1;
255 if (bb[y] & u)
256 c |= 1;
257 u >>= 1;
258 if (!u) {
259 y++;
260 u = 0x80;
261 }
262 }
263 ret[i] = cov_2char[c];
264 }
265 ret[13] = '\0';
266 return ret;
267 }
268