summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortedu <>2014-05-17 13:27:55 +0000
committertedu <>2014-05-17 13:27:55 +0000
commit1751b5188cc8ed3fea8bc02d09fd547d8bfd27e8 (patch)
treef59537ab5078574c6dcacba6ea639db495b7f658 /src
parent03af8b66bc2785f9f64b1e1b21c45684be8e2d38 (diff)
downloadopenbsd-1751b5188cc8ed3fea8bc02d09fd547d8bfd27e8.tar.gz
openbsd-1751b5188cc8ed3fea8bc02d09fd547d8bfd27e8.tar.bz2
openbsd-1751b5188cc8ed3fea8bc02d09fd547d8bfd27e8.zip
refactor some of the des crypt code to use fewer globals.
ok miod
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/crypt/crypt.c75
-rw-r--r--src/lib/libc/crypt/crypt2.c12
2 files changed, 45 insertions, 42 deletions
diff --git a/src/lib/libc/crypt/crypt.c b/src/lib/libc/crypt/crypt.c
index ad8140e302..157784527b 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.21 2014/05/12 19:13:14 tedu Exp $ */ 1/* $OpenBSD: crypt.c,v 1.22 2014/05/17 13:27:55 tedu Exp $ */
2 2
3/* 3/*
4 * FreeSec: libcrypt 4 * FreeSec: libcrypt
@@ -160,8 +160,6 @@ const u_int32_t _des_bits32[32] =
160 160
161const u_char _des_bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; 161const u_char _des_bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
162 162
163static u_int32_t saltbits;
164static int32_t old_salt;
165static const u_int32_t *bits28, *bits24; 163static const u_int32_t *bits28, *bits24;
166static u_char init_perm[64], final_perm[64]; 164static u_char init_perm[64], final_perm[64];
167static u_int32_t en_keysl[16], en_keysr[16]; 165static u_int32_t en_keysl[16], en_keysr[16];
@@ -205,8 +203,6 @@ _des_init(void)
205 u_int32_t *p, *il, *ir, *fl, *fr; 203 u_int32_t *p, *il, *ir, *fl, *fr;
206 204
207 old_rawkey0 = old_rawkey1 = 0; 205 old_rawkey0 = old_rawkey1 = 0;
208 saltbits = 0;
209 old_salt = 0;
210 bits24 = (bits28 = _des_bits32 + 4) + 4; 206 bits24 = (bits28 = _des_bits32 + 4) + 4;
211 207
212 /* 208 /*
@@ -328,16 +324,12 @@ _des_init(void)
328 _des_initialised = 1; 324 _des_initialised = 1;
329} 325}
330 326
331void 327u_int32_t
332_des_setup_salt(int32_t salt) 328_des_setup_salt(int32_t salt)
333{ 329{
334 u_int32_t obit, saltbit; 330 u_int32_t obit, saltbit, saltbits;
335 int i; 331 int i;
336 332
337 if (salt == old_salt)
338 return;
339 old_salt = salt;
340
341 saltbits = 0; 333 saltbits = 0;
342 saltbit = 1; 334 saltbit = 1;
343 obit = 0x800000; 335 obit = 0x800000;
@@ -347,6 +339,7 @@ _des_setup_salt(int32_t salt)
347 saltbit <<= 1; 339 saltbit <<= 1;
348 obit >>= 1; 340 obit >>= 1;
349 } 341 }
342 return saltbits;
350} 343}
351 344
352int 345int
@@ -431,7 +424,7 @@ des_setkey(const char *key)
431 424
432int 425int
433_des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, 426_des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out,
434 int count) 427 int count, u_int32_t saltbits)
435{ 428{
436 /* 429 /*
437 * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. 430 * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format.
@@ -549,19 +542,19 @@ _des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out,
549int 542int
550des_cipher(const char *in, char *out, int32_t salt, int count) 543des_cipher(const char *in, char *out, int32_t salt, int count)
551{ 544{
552 u_int32_t l_out, r_out, rawl, rawr; 545 u_int32_t l_out, r_out, rawl, rawr, saltbits;
553 u_int32_t x[2]; 546 u_int32_t x[2];
554 int retval; 547 int retval;
555 548
556 if (!_des_initialised) 549 if (!_des_initialised)
557 _des_init(); 550 _des_init();
558 551
559 _des_setup_salt(salt); 552 saltbits = _des_setup_salt(salt);
560 553
561 memcpy(x, in, sizeof x); 554 memcpy(x, in, sizeof x);
562 rawl = ntohl(x[0]); 555 rawl = ntohl(x[0]);
563 rawr = ntohl(x[1]); 556 rawr = ntohl(x[1]);
564 retval = _des_do_des(rawl, rawr, &l_out, &r_out, count); 557 retval = _des_do_des(rawl, rawr, &l_out, &r_out, count, saltbits);
565 558
566 x[0] = htonl(l_out); 559 x[0] = htonl(l_out);
567 x[1] = htonl(r_out); 560 x[1] = htonl(r_out);
@@ -569,23 +562,12 @@ des_cipher(const char *in, char *out, int32_t salt, int count)
569 return(retval); 562 return(retval);
570} 563}
571 564
572char * 565static int
573crypt(const char *key, const char *setting) 566crypt_hashpass(const char *key, const char *setting, char *output)
574{ 567{
575 int i; 568 int i;
576 u_int32_t count, salt, l, r0, r1, keybuf[2]; 569 u_int32_t count, salt, l, r0, r1, saltbits, keybuf[2];
577 u_char *p, *q; 570 u_char *p, *q;
578 static u_char output[21];
579 extern char *bcrypt(const char *, const char *);
580
581 if (setting[0] == '$') {
582 switch (setting[1]) {
583 case '2':
584 return bcrypt(key, setting);
585 default:
586 return (NULL);
587 }
588 }
589 571
590 if (!_des_initialised) 572 if (!_des_initialised)
591 _des_init(); 573 _des_init();
@@ -600,7 +582,7 @@ crypt(const char *key, const char *setting)
600 key++; 582 key++;
601 } 583 }
602 if (des_setkey((char *) keybuf)) 584 if (des_setkey((char *) keybuf))
603 return(NULL); 585 return(-1);
604 586
605 if (*setting == _PASSWORD_EFMT1) { 587 if (*setting == _PASSWORD_EFMT1) {
606 /* 588 /*
@@ -619,7 +601,7 @@ crypt(const char *key, const char *setting)
619 * Encrypt the key with itself. 601 * Encrypt the key with itself.
620 */ 602 */
621 if (des_cipher((char *)keybuf, (char *)keybuf, 0, 1)) 603 if (des_cipher((char *)keybuf, (char *)keybuf, 0, 1))
622 return(NULL); 604 return(-1);
623 /* 605 /*
624 * And XOR with the next 8 characters of the key. 606 * And XOR with the next 8 characters of the key.
625 */ 607 */
@@ -629,7 +611,7 @@ crypt(const char *key, const char *setting)
629 *q++ ^= *key++ << 1; 611 *q++ ^= *key++ << 1;
630 612
631 if (des_setkey((char *) keybuf)) 613 if (des_setkey((char *) keybuf))
632 return(NULL); 614 return(-1);
633 } 615 }
634 strlcpy((char *)output, setting, 10); 616 strlcpy((char *)output, setting, 10);
635 617
@@ -663,13 +645,13 @@ crypt(const char *key, const char *setting)
663 645
664 p = output + 2; 646 p = output + 2;
665 } 647 }
666 _des_setup_salt(salt); 648 saltbits = _des_setup_salt(salt);
667 649
668 /* 650 /*
669 * Do it. 651 * Do it.
670 */ 652 */
671 if (_des_do_des(0, 0, &r0, &r1, count)) 653 if (_des_do_des(0, 0, &r0, &r1, count, saltbits))
672 return(NULL); 654 return(-1);
673 /* 655 /*
674 * Now encode the result... 656 * Now encode the result...
675 */ 657 */
@@ -691,5 +673,26 @@ crypt(const char *key, const char *setting)
691 *p++ = ascii64[l & 0x3f]; 673 *p++ = ascii64[l & 0x3f];
692 *p = 0; 674 *p = 0;
693 675
694 return((char *)output); 676 return(0);
677}
678
679char *
680crypt(const char *key, const char *setting)
681{
682 static u_char goutput[21];
683 extern char *bcrypt(const char *, const char *);
684
685 if (setting[0] == '$') {
686 switch (setting[1]) {
687 case '2':
688 return bcrypt(key, setting);
689 default:
690 return (NULL);
691 }
692 }
693
694 memset(goutput, 0, sizeof(goutput));
695 if (crypt_hashpass(key, setting, goutput) != 0)
696 return (NULL);
697 return goutput;
695} 698}
diff --git a/src/lib/libc/crypt/crypt2.c b/src/lib/libc/crypt/crypt2.c
index f31818ae2b..f64721368a 100644
--- a/src/lib/libc/crypt/crypt2.c
+++ b/src/lib/libc/crypt/crypt2.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: crypt2.c,v 1.4 2013/04/17 17:40:35 tedu Exp $ */ 1/* $OpenBSD: crypt2.c,v 1.5 2014/05/17 13:27:55 tedu Exp $ */
2 2
3/* 3/*
4 * FreeSec: libcrypt 4 * FreeSec: libcrypt
@@ -60,8 +60,8 @@ extern const u_char _des_bits8[8];
60extern const u_int32_t _des_bits32[32]; 60extern const u_int32_t _des_bits32[32];
61extern int _des_initialised; 61extern int _des_initialised;
62void _des_init(void); 62void _des_init(void);
63void _des_setup_salt(int32_t salt); 63u_int32_t _des_setup_salt(int32_t salt);
64int _des_do_des(u_int32_t , u_int32_t , u_int32_t *, u_int32_t *, int); 64int _des_do_des(u_int32_t , u_int32_t , u_int32_t *, u_int32_t *, int, u_int32_t);
65 65
66int 66int
67setkey(const char *key) 67setkey(const char *key)
@@ -84,14 +84,14 @@ setkey(const char *key)
84int 84int
85encrypt(char *block, int flag) 85encrypt(char *block, int flag)
86{ 86{
87 u_int32_t io[2]; 87 u_int32_t saltbits, io[2];
88 u_char *p; 88 u_char *p;
89 int i, j, retval; 89 int i, j, retval;
90 90
91 if (!_des_initialised) 91 if (!_des_initialised)
92 _des_init(); 92 _des_init();
93 93
94 _des_setup_salt(0); 94 saltbits = _des_setup_salt(0);
95 p = (u_char *)block; 95 p = (u_char *)block;
96 for (i = 0; i < 2; i++) { 96 for (i = 0; i < 2; i++) {
97 io[i] = 0L; 97 io[i] = 0L;
@@ -99,7 +99,7 @@ encrypt(char *block, int flag)
99 if (*p++ & 1) 99 if (*p++ & 1)
100 io[i] |= _des_bits32[j]; 100 io[i] |= _des_bits32[j];
101 } 101 }
102 retval = _des_do_des(io[0], io[1], io, io + 1, flag ? -1 : 1); 102 retval = _des_do_des(io[0], io[1], io, io + 1, flag ? -1 : 1, saltbits);
103 for (i = 0; i < 2; i++) 103 for (i = 0; i < 2; i++)
104 for (j = 0; j < 32; j++) 104 for (j = 0; j < 32; j++)
105 block[(i << 5) | j] = (io[i] & _des_bits32[j]) ? 1 : 0; 105 block[(i << 5) | j] = (io[i] & _des_bits32[j]) ? 1 : 0;