summaryrefslogtreecommitdiff
path: root/src/lib/libc/crypt/crypt.c
diff options
context:
space:
mode:
authorderaadt <>2003-08-12 01:22:17 +0000
committerderaadt <>2003-08-12 01:22:17 +0000
commit89141738fc05289d3121bdc1e4284d6ff1d96cf6 (patch)
treeb5671f7009cdecd6b7f145cb4c3f83c9bd9788f9 /src/lib/libc/crypt/crypt.c
parent000616b610cc504d9eb89b4eebc110ba3667f648 (diff)
downloadopenbsd-89141738fc05289d3121bdc1e4284d6ff1d96cf6.tar.gz
openbsd-89141738fc05289d3121bdc1e4284d6ff1d96cf6.tar.bz2
openbsd-89141738fc05289d3121bdc1e4284d6ff1d96cf6.zip
Split crypto routine sup again, but differently. This should make the
mail floppy parts a fair bit smaller.
Diffstat (limited to 'src/lib/libc/crypt/crypt.c')
-rw-r--r--src/lib/libc/crypt/crypt.c121
1 files changed, 39 insertions, 82 deletions
diff --git a/src/lib/libc/crypt/crypt.c b/src/lib/libc/crypt/crypt.c
index 7f07f4e7ee..e0f5e8364a 100644
--- a/src/lib/libc/crypt/crypt.c
+++ b/src/lib/libc/crypt/crypt.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypt.c,v 1.17 2003/08/07 00:32:12 deraadt Exp $ */ 1/* $OpenBSD: crypt.c,v 1.18 2003/08/12 01:22:17 deraadt Exp $ */
2 2
3/* 3/*
4 * FreeSec: libcrypt 4 * FreeSec: libcrypt
@@ -47,7 +47,7 @@
47 */ 47 */
48 48
49#if defined(LIBC_SCCS) && !defined(lint) 49#if defined(LIBC_SCCS) && !defined(lint)
50static char rcsid[] = "$OpenBSD: crypt.c,v 1.17 2003/08/07 00:32:12 deraadt Exp $"; 50static char rcsid[] = "$OpenBSD: crypt.c,v 1.18 2003/08/12 01:22:17 deraadt Exp $";
51#endif /* LIBC_SCCS and not lint */ 51#endif /* LIBC_SCCS and not lint */
52 52
53#include <sys/types.h> 53#include <sys/types.h>
@@ -60,7 +60,7 @@ static char rcsid[] = "$OpenBSD: crypt.c,v 1.17 2003/08/07 00:32:12 deraadt Exp
60# include <stdio.h> 60# include <stdio.h>
61#endif 61#endif
62 62
63static u_char IP[64] = { 63static const u_char IP[64] = {
64 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 64 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
65 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 65 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
66 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 66 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
@@ -69,19 +69,19 @@ static u_char IP[64] = {
69 69
70static u_char inv_key_perm[64]; 70static u_char inv_key_perm[64];
71static u_char u_key_perm[56]; 71static u_char u_key_perm[56];
72static u_char key_perm[56] = { 72static u_char const key_perm[56] = {
73 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 73 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
74 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 74 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
75 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 75 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
76 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 76 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
77}; 77};
78 78
79static u_char key_shifts[16] = { 79static const u_char key_shifts[16] = {
80 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 80 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
81}; 81};
82 82
83static u_char inv_comp_perm[56]; 83static u_char inv_comp_perm[56];
84static u_char comp_perm[48] = { 84static const u_char comp_perm[48] = {
85 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 85 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
86 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 86 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
87 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 87 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
@@ -93,7 +93,7 @@ static u_char comp_perm[48] = {
93 */ 93 */
94 94
95static u_char u_sbox[8][64]; 95static u_char u_sbox[8][64];
96static u_char sbox[8][64] = { 96static const u_char sbox[8][64] = {
97 { 97 {
98 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 98 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
99 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 99 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
@@ -145,12 +145,12 @@ static u_char sbox[8][64] = {
145}; 145};
146 146
147static u_char un_pbox[32]; 147static u_char un_pbox[32];
148static u_char pbox[32] = { 148static const u_char pbox[32] = {
149 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 149 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
150 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 150 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
151}; 151};
152 152
153static u_int32_t bits32[32] = 153const u_int32_t _des_bits32[32] =
154{ 154{
155 0x80000000, 0x40000000, 0x20000000, 0x10000000, 155 0x80000000, 0x40000000, 0x20000000, 0x10000000,
156 0x08000000, 0x04000000, 0x02000000, 0x01000000, 156 0x08000000, 0x04000000, 0x02000000, 0x01000000,
@@ -162,15 +162,15 @@ static u_int32_t bits32[32] =
162 0x00000008, 0x00000004, 0x00000002, 0x00000001 162 0x00000008, 0x00000004, 0x00000002, 0x00000001
163}; 163};
164 164
165static u_char bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; 165const u_char _des_bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
166 166
167static u_int32_t saltbits; 167static u_int32_t saltbits;
168static int32_t old_salt; 168static int32_t old_salt;
169static u_int32_t *bits28, *bits24; 169static const u_int32_t *bits28, *bits24;
170static u_char init_perm[64], final_perm[64]; 170static u_char init_perm[64], final_perm[64];
171static u_int32_t en_keysl[16], en_keysr[16]; 171static u_int32_t en_keysl[16], en_keysr[16];
172static u_int32_t de_keysl[16], de_keysr[16]; 172static u_int32_t de_keysl[16], de_keysr[16];
173static int des_initialised = 0; 173int _des_initialised = 0;
174static u_char m_sbox[4][4096]; 174static u_char m_sbox[4][4096];
175static u_int32_t psbox[4][256]; 175static u_int32_t psbox[4][256];
176static u_int32_t ip_maskl[8][256], ip_maskr[8][256]; 176static u_int32_t ip_maskl[8][256], ip_maskr[8][256];
@@ -202,8 +202,8 @@ ascii_to_bin(char ch)
202 return(0); 202 return(0);
203} 203}
204 204
205static void 205void
206des_init(void) 206_des_init(void)
207{ 207{
208 int i, j, b, k, inbit, obit; 208 int i, j, b, k, inbit, obit;
209 u_int32_t *p, *il, *ir, *fl, *fr; 209 u_int32_t *p, *il, *ir, *fl, *fr;
@@ -211,7 +211,7 @@ des_init(void)
211 old_rawkey0 = old_rawkey1 = 0; 211 old_rawkey0 = old_rawkey1 = 0;
212 saltbits = 0; 212 saltbits = 0;
213 old_salt = 0; 213 old_salt = 0;
214 bits24 = (bits28 = bits32 + 4) + 4; 214 bits24 = (bits28 = _des_bits32 + 4) + 4;
215 215
216 /* 216 /*
217 * Invert the S-boxes, reordering the input bits. 217 * Invert the S-boxes, reordering the input bits.
@@ -271,15 +271,15 @@ des_init(void)
271 *(fr = &fp_maskr[k][i]) = 0; 271 *(fr = &fp_maskr[k][i]) = 0;
272 for (j = 0; j < 8; j++) { 272 for (j = 0; j < 8; j++) {
273 inbit = 8 * k + j; 273 inbit = 8 * k + j;
274 if (i & bits8[j]) { 274 if (i & _des_bits8[j]) {
275 if ((obit = init_perm[inbit]) < 32) 275 if ((obit = init_perm[inbit]) < 32)
276 *il |= bits32[obit]; 276 *il |= _des_bits32[obit];
277 else 277 else
278 *ir |= bits32[obit-32]; 278 *ir |= _des_bits32[obit-32];
279 if ((obit = final_perm[inbit]) < 32) 279 if ((obit = final_perm[inbit]) < 32)
280 *fl |= bits32[obit]; 280 *fl |= _des_bits32[obit];
281 else 281 else
282 *fr |= bits32[obit - 32]; 282 *fr |= _des_bits32[obit - 32];
283 } 283 }
284 } 284 }
285 } 285 }
@@ -288,7 +288,7 @@ des_init(void)
288 *(ir = &key_perm_maskr[k][i]) = 0; 288 *(ir = &key_perm_maskr[k][i]) = 0;
289 for (j = 0; j < 7; j++) { 289 for (j = 0; j < 7; j++) {
290 inbit = 8 * k + j; 290 inbit = 8 * k + j;
291 if (i & bits8[j + 1]) { 291 if (i & _des_bits8[j + 1]) {
292 if ((obit = inv_key_perm[inbit]) == 255) 292 if ((obit = inv_key_perm[inbit]) == 255)
293 continue; 293 continue;
294 if (obit < 28) 294 if (obit < 28)
@@ -301,7 +301,7 @@ des_init(void)
301 *(ir = &comp_maskr[k][i]) = 0; 301 *(ir = &comp_maskr[k][i]) = 0;
302 for (j = 0; j < 7; j++) { 302 for (j = 0; j < 7; j++) {
303 inbit = 7 * k + j; 303 inbit = 7 * k + j;
304 if (i & bits8[j + 1]) { 304 if (i & _des_bits8[j + 1]) {
305 if ((obit=inv_comp_perm[inbit]) == 255) 305 if ((obit=inv_comp_perm[inbit]) == 255)
306 continue; 306 continue;
307 if (obit < 24) 307 if (obit < 24)
@@ -324,16 +324,16 @@ des_init(void)
324 for (i = 0; i < 256; i++) { 324 for (i = 0; i < 256; i++) {
325 *(p = &psbox[b][i]) = 0; 325 *(p = &psbox[b][i]) = 0;
326 for (j = 0; j < 8; j++) { 326 for (j = 0; j < 8; j++) {
327 if (i & bits8[j]) 327 if (i & _des_bits8[j])
328 *p |= bits32[un_pbox[8 * b + j]]; 328 *p |= _des_bits32[un_pbox[8 * b + j]];
329 } 329 }
330 } 330 }
331 331
332 des_initialised = 1; 332 _des_initialised = 1;
333} 333}
334 334
335static void 335void
336setup_salt(int32_t salt) 336_des_setup_salt(int32_t salt)
337{ 337{
338 u_int32_t obit, saltbit; 338 u_int32_t obit, saltbit;
339 int i; 339 int i;
@@ -359,8 +359,8 @@ des_setkey(const char *key)
359 u_int32_t k0, k1, rawkey0, rawkey1; 359 u_int32_t k0, k1, rawkey0, rawkey1;
360 int shifts, round; 360 int shifts, round;
361 361
362 if (!des_initialised) 362 if (!_des_initialised)
363 des_init(); 363 _des_init();
364 364
365 rawkey0 = ntohl(*(u_int32_t *) key); 365 rawkey0 = ntohl(*(u_int32_t *) key);
366 rawkey1 = ntohl(*(u_int32_t *) (key + 4)); 366 rawkey1 = ntohl(*(u_int32_t *) (key + 4));
@@ -433,8 +433,8 @@ des_setkey(const char *key)
433 return(0); 433 return(0);
434} 434}
435 435
436static int 436int
437do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, 437_des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out,
438 int count) 438 int count)
439{ 439{
440 /* 440 /*
@@ -557,15 +557,15 @@ des_cipher(const char *in, char *out, int32_t salt, int count)
557 u_int32_t x[2]; 557 u_int32_t x[2];
558 int retval; 558 int retval;
559 559
560 if (!des_initialised) 560 if (!_des_initialised)
561 des_init(); 561 _des_init();
562 562
563 setup_salt(salt); 563 _des_setup_salt(salt);
564 564
565 memcpy(x, in, sizeof x); 565 memcpy(x, in, sizeof x);
566 rawl = ntohl(x[0]); 566 rawl = ntohl(x[0]);
567 rawr = ntohl(x[1]); 567 rawr = ntohl(x[1]);
568 retval = do_des(rawl, rawr, &l_out, &r_out, count); 568 retval = _des_do_des(rawl, rawr, &l_out, &r_out, count);
569 569
570 x[0] = htonl(l_out); 570 x[0] = htonl(l_out);
571 x[1] = htonl(r_out); 571 x[1] = htonl(r_out);
@@ -592,8 +592,8 @@ crypt(const char *key, const char *setting)
592 } 592 }
593 } 593 }
594 594
595 if (!des_initialised) 595 if (!_des_initialised)
596 des_init(); 596 _des_init();
597 597
598 /* 598 /*
599 * Copy the key, shifting each character up by one bit 599 * Copy the key, shifting each character up by one bit
@@ -668,12 +668,12 @@ crypt(const char *key, const char *setting)
668 668
669 p = output + 2; 669 p = output + 2;
670 } 670 }
671 setup_salt(salt); 671 _des_setup_salt(salt);
672 672
673 /* 673 /*
674 * Do it. 674 * Do it.
675 */ 675 */
676 if (do_des(0, 0, &r0, &r1, count)) 676 if (_des_do_des(0, 0, &r0, &r1, count))
677 return(NULL); 677 return(NULL);
678 /* 678 /*
679 * Now encode the result... 679 * Now encode the result...
@@ -698,46 +698,3 @@ crypt(const char *key, const char *setting)
698 698
699 return((char *)output); 699 return((char *)output);
700} 700}
701
702int
703setkey(const char *key)
704{
705 int i, j;
706 u_int32_t packed_keys[2];
707 u_char *p;
708
709 p = (u_char *) packed_keys;
710
711 for (i = 0; i < 8; i++) {
712 p[i] = 0;
713 for (j = 0; j < 8; j++)
714 if (*key++ & 1)
715 p[i] |= bits8[j];
716 }
717 return(des_setkey(p));
718}
719
720int
721encrypt(char *block, int flag)
722{
723 u_int32_t io[2];
724 u_char *p;
725 int i, j, retval;
726
727 if (!des_initialised)
728 des_init();
729
730 setup_salt(0);
731 p = (u_char *)block;
732 for (i = 0; i < 2; i++) {
733 io[i] = 0L;
734 for (j = 0; j < 32; j++)
735 if (*p++ & 1)
736 io[i] |= bits32[j];
737 }
738 retval = do_des(io[0], io[1], io, io + 1, flag ? -1 : 1);
739 for (i = 0; i < 2; i++)
740 for (j = 0; j < 32; j++)
741 block[(i << 5) | j] = (io[i] & bits32[j]) ? 1 : 0;
742 return(retval);
743}