2 typedef long unsigned int size_t;
3 typedef unsigned char __u_char;
4 typedef unsigned short int __u_short;
5 typedef unsigned int __u_int;
6 typedef unsigned long int __u_long;
7 typedef signed char __int8_t;
8 typedef unsigned char __uint8_t;
9 typedef signed short int __int16_t;
10 typedef unsigned short int __uint16_t;
11 typedef signed int __int32_t;
12 typedef unsigned int __uint32_t;
13 typedef signed long int __int64_t;
14 typedef unsigned long int __uint64_t;
15 typedef long int __quad_t;
16 typedef unsigned long int __u_quad_t;
17 typedef unsigned long int __dev_t;
18 typedef unsigned int __uid_t;
19 typedef unsigned int __gid_t;
20 typedef unsigned long int __ino_t;
21 typedef unsigned long int __ino64_t;
22 typedef unsigned int __mode_t;
23 typedef unsigned long int __nlink_t;
24 typedef long int __off_t;
25 typedef long int __off64_t;
27 typedef struct { int __val[2]; } __fsid_t;
28 typedef long int __clock_t;
29 typedef unsigned long int __rlim_t;
30 typedef unsigned long int __rlim64_t;
31 typedef unsigned int __id_t;
32 typedef long int __time_t;
33 typedef unsigned int __useconds_t;
34 typedef long int __suseconds_t;
35 typedef int __daddr_t;
36 typedef long int __swblk_t;
38 typedef int __clockid_t;
39 typedef void * __timer_t;
40 typedef long int __blksize_t;
41 typedef long int __blkcnt_t;
42 typedef long int __blkcnt64_t;
43 typedef unsigned long int __fsblkcnt_t;
44 typedef unsigned long int __fsblkcnt64_t;
45 typedef unsigned long int __fsfilcnt_t;
46 typedef unsigned long int __fsfilcnt64_t;
47 typedef long int __ssize_t;
48 typedef __off64_t __loff_t;
49 typedef __quad_t *__qaddr_t;
50 typedef char *__caddr_t;
51 typedef long int __intptr_t;
52 typedef unsigned int __socklen_t;
53 typedef __u_char u_char;
54 typedef __u_short u_short;
55 typedef __u_int u_int;
56 typedef __u_long u_long;
57 typedef __quad_t quad_t;
58 typedef __u_quad_t u_quad_t;
59 typedef __fsid_t fsid_t;
60 typedef __loff_t loff_t;
61 typedef __ino_t ino_t;
64 typedef __dev_t dev_t;
69 typedef __gid_t gid_t;
74 typedef __mode_t mode_t;
79 typedef __nlink_t nlink_t;
84 typedef __uid_t uid_t;
90 typedef __off_t off_t;
91 typedef __pid_t pid_t;
93 typedef __ssize_t ssize_t;
94 typedef __daddr_t daddr_t;
95 typedef __caddr_t caddr_t;
96 typedef __key_t key_t;
98 typedef __time_t time_t;
101 typedef __clockid_t clockid_t;
107 typedef __timer_t timer_t;
108 typedef unsigned long int ulong;
109 typedef unsigned short int ushort;
110 typedef unsigned int uint;
111 typedef int int8_t __attribute__ ((__mode__ (__QI__)));
112 typedef int int16_t __attribute__ ((__mode__ (__HI__)));
113 typedef int int32_t __attribute__ ((__mode__ (__SI__)));
114 typedef int int64_t __attribute__ ((__mode__ (__DI__)));
115 typedef unsigned int u_int8_t __attribute__ ((__mode__ (__QI__)));
116 typedef unsigned int u_int16_t __attribute__ ((__mode__ (__HI__)));
117 typedef unsigned int u_int32_t __attribute__ ((__mode__ (__SI__)));
118 typedef unsigned int u_int64_t __attribute__ ((__mode__ (__DI__)));
119 typedef int register_t __attribute__ ((__mode__ (__word__)));
120 typedef int __sig_atomic_t;
123 unsigned long int __val[(1024 / (8 * sizeof (unsigned long int)))];
125 typedef __sigset_t sigset_t;
134 __suseconds_t tv_usec;
136 typedef __suseconds_t suseconds_t;
137 typedef long int __fd_mask;
140 __fd_mask __fds_bits[1024 / (8 * sizeof (__fd_mask))];
142 typedef __fd_mask fd_mask;
144 extern int select (int __nfds, fd_set *__restrict __readfds,
145 fd_set *__restrict __writefds,
146 fd_set *__restrict __exceptfds,
147 struct timeval *__restrict __timeout);
148 extern int pselect (int __nfds, fd_set *__restrict __readfds,
149 fd_set *__restrict __writefds,
150 fd_set *__restrict __exceptfds,
151 const struct timespec *__restrict __timeout,
152 const __sigset_t *__restrict __sigmask);
155 extern __inline unsigned int gnu_dev_major (unsigned long long int __dev)
156 __attribute__ ((__nothrow__));
158 extern __inline unsigned int gnu_dev_minor (unsigned long long int __dev)
159 __attribute__ ((__nothrow__));
161 extern __inline unsigned long long int gnu_dev_makedev (unsigned int __major,
162 unsigned int __minor)
163 __attribute__ ((__nothrow__));
164 __extension__ extern __inline unsigned int
165 __attribute__ ((__nothrow__)) gnu_dev_major (unsigned long long int __dev)
167 return ((__dev >> 8) & 0xfff) | ((unsigned int) (__dev >> 32) & ~0xfff);
169 __extension__ extern __inline unsigned int
170 __attribute__ ((__nothrow__)) gnu_dev_minor (unsigned long long int __dev)
172 return (__dev & 0xff) | ((unsigned int) (__dev >> 12) & ~0xff);
174 __extension__ extern __inline unsigned long long int
175 __attribute__ ((__nothrow__)) gnu_dev_makedev (unsigned int __major, unsigned int __minor)
177 return ((__minor & 0xff) | ((__major & 0xfff) << 8)
178 | (((unsigned long long int) (__minor & ~0xff)) << 12)
179 | (((unsigned long long int) (__major & ~0xfff)) << 32));
181 typedef __blkcnt_t blkcnt_t;
182 typedef __fsblkcnt_t fsblkcnt_t;
183 typedef __fsfilcnt_t fsfilcnt_t;
184 typedef unsigned long int pthread_t;
190 typedef struct __pthread_internal_list
192 struct __pthread_internal_list *__prev;
193 struct __pthread_internal_list *__next;
197 struct __pthread_mutex_s
200 unsigned int __count;
202 unsigned int __nusers;
205 __pthread_list_t __list;
214 } pthread_mutexattr_t;
220 unsigned int __futex;
221 __extension__ unsigned long long int __total_seq;
222 __extension__ unsigned long long int __wakeup_seq;
223 __extension__ unsigned long long int __woken_seq;
225 unsigned int __nwaiters;
226 unsigned int __broadcast_seq;
229 __extension__ long long int __align;
235 } pthread_condattr_t;
236 typedef unsigned int pthread_key_t;
237 typedef int pthread_once_t;
243 unsigned int __nr_readers;
244 unsigned int __readers_wakeup;
245 unsigned int __writer_wakeup;
246 unsigned int __nr_readers_queued;
247 unsigned int __nr_writers_queued;
250 unsigned long int __pad2;
251 unsigned long int __pad3;
252 unsigned int __flags;
261 } pthread_rwlockattr_t;
262 typedef volatile int pthread_spinlock_t;
272 } pthread_barrierattr_t;
276 typedef unsigned char uint8_t;
277 typedef unsigned short int uint16_t;
278 typedef unsigned int uint32_t;
279 typedef unsigned long int uint64_t;
280 typedef signed char int_least8_t;
281 typedef short int int_least16_t;
282 typedef int int_least32_t;
283 typedef long int int_least64_t;
284 typedef unsigned char uint_least8_t;
285 typedef unsigned short int uint_least16_t;
286 typedef unsigned int uint_least32_t;
287 typedef unsigned long int uint_least64_t;
288 typedef signed char int_fast8_t;
289 typedef long int int_fast16_t;
290 typedef long int int_fast32_t;
291 typedef long int int_fast64_t;
292 typedef unsigned char uint_fast8_t;
293 typedef unsigned long int uint_fast16_t;
294 typedef unsigned long int uint_fast32_t;
295 typedef unsigned long int uint_fast64_t;
296 typedef long int intptr_t;
297 typedef unsigned long int uintptr_t;
298 typedef long int intmax_t;
299 typedef unsigned long int uintmax_t;
307 extern ssize_t readv (int __fd, __const struct iovec *__iovec, int __count);
308 extern ssize_t writev (int __fd, __const struct iovec *__iovec, int __count);
310 typedef __socklen_t socklen_t;
320 typedef unsigned short int sa_family_t;
323 sa_family_t sa_family;
326 struct sockaddr_storage
328 sa_family_t ss_family;
329 __uint64_t __ss_align;
330 char __ss_padding[(128 - (2 * sizeof (__uint64_t)))];
336 MSG_DONTROUTE = 0x04,
347 MSG_ERRQUEUE = 0x2000,
348 MSG_NOSIGNAL = 0x4000,
354 socklen_t msg_namelen;
355 struct iovec *msg_iov;
358 size_t msg_controllen;
366 __extension__ unsigned char __cmsg_data [];
368 extern struct cmsghdr *__cmsg_nxthdr (struct msghdr *__mhdr,
369 struct cmsghdr *__cmsg) __attribute__ ((__nothrow__));
373 , SCM_CREDENTIALS = 0x02
388 unsigned short int sa_family;
389 unsigned char sa_data[14];
397 extern int socket (int __domain, int __type, int __protocol) __attribute__ ((__nothrow__));
398 extern int socketpair (int __domain, int __type, int __protocol,
399 int __fds[2]) __attribute__ ((__nothrow__));
400 extern int bind (int __fd, __const struct sockaddr * __addr, socklen_t __len)
401 __attribute__ ((__nothrow__));
402 extern int getsockname (int __fd, struct sockaddr *__restrict __addr,
403 socklen_t *__restrict __len) __attribute__ ((__nothrow__));
404 extern int connect (int __fd, __const struct sockaddr * __addr, socklen_t __len);
405 extern int getpeername (int __fd, struct sockaddr *__restrict __addr,
406 socklen_t *__restrict __len) __attribute__ ((__nothrow__));
407 extern ssize_t send (int __fd, __const void *__buf, size_t __n, int __flags);
408 extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);
409 extern ssize_t sendto (int __fd, __const void *__buf, size_t __n,
410 int __flags, __const struct sockaddr * __addr,
411 socklen_t __addr_len);
412 extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n,
413 int __flags, struct sockaddr *__restrict __addr,
414 socklen_t *__restrict __addr_len);
415 extern ssize_t sendmsg (int __fd, __const struct msghdr *__message,
417 extern ssize_t recvmsg (int __fd, struct msghdr *__message, int __flags);
418 extern int getsockopt (int __fd, int __level, int __optname,
419 void *__restrict __optval,
420 socklen_t *__restrict __optlen) __attribute__ ((__nothrow__));
421 extern int setsockopt (int __fd, int __level, int __optname,
422 __const void *__optval, socklen_t __optlen) __attribute__ ((__nothrow__));
423 extern int listen (int __fd, int __n) __attribute__ ((__nothrow__));
424 extern int accept (int __fd, struct sockaddr *__restrict __addr,
425 socklen_t *__restrict __addr_len);
426 extern int shutdown (int __fd, int __how) __attribute__ ((__nothrow__));
427 extern int sockatmark (int __fd) __attribute__ ((__nothrow__));
428 extern int isfdtype (int __fd, int __fdtype) __attribute__ ((__nothrow__));
445 IPPROTO_ROUTING = 43,
446 IPPROTO_FRAGMENT = 44,
453 IPPROTO_DSTOPTS = 60,
462 typedef uint16_t in_port_t;
473 IPPORT_TIMESERVER = 37,
474 IPPORT_NAMESERVER = 42,
482 IPPORT_EXECSERVER = 512,
483 IPPORT_LOGINSERVER = 513,
484 IPPORT_CMDSERVER = 514,
485 IPPORT_EFSSERVER = 520,
486 IPPORT_BIFFUDP = 512,
487 IPPORT_WHOSERVER = 513,
488 IPPORT_ROUTESERVER = 520,
489 IPPORT_RESERVED = 1024,
490 IPPORT_USERRESERVED = 5000
492 typedef uint32_t in_addr_t;
501 uint8_t u6_addr8[16];
502 uint16_t u6_addr16[8];
503 uint32_t u6_addr32[4];
506 extern const struct in6_addr in6addr_any;
507 extern const struct in6_addr in6addr_loopback;
510 sa_family_t sin_family;
512 struct in_addr sin_addr;
513 unsigned char sin_zero[sizeof (struct sockaddr) -
514 (sizeof (unsigned short int)) -
516 sizeof (struct in_addr)];
520 sa_family_t sin6_family;
522 uint32_t sin6_flowinfo;
523 struct in6_addr sin6_addr;
524 uint32_t sin6_scope_id;
528 struct in_addr imr_multiaddr;
529 struct in_addr imr_interface;
531 struct ip_mreq_source
533 struct in_addr imr_multiaddr;
534 struct in_addr imr_interface;
535 struct in_addr imr_sourceaddr;
539 struct in6_addr ipv6mr_multiaddr;
540 unsigned int ipv6mr_interface;
544 uint32_t gr_interface;
545 struct sockaddr_storage gr_group;
547 struct group_source_req
549 uint32_t gsr_interface;
550 struct sockaddr_storage gsr_group;
551 struct sockaddr_storage gsr_source;
555 struct in_addr imsf_multiaddr;
556 struct in_addr imsf_interface;
558 uint32_t imsf_numsrc;
559 struct in_addr imsf_slist[1];
563 uint32_t gf_interface;
564 struct sockaddr_storage gf_group;
567 struct sockaddr_storage gf_slist[1];
571 struct in_addr ip_dst;
576 struct in_addr imr_multiaddr;
577 struct in_addr imr_address;
583 struct in_addr ipi_spec_dst;
584 struct in_addr ipi_addr;
586 extern uint32_t ntohl (uint32_t __netlong) __attribute__ ((__nothrow__)) __attribute__ ((__const__));
587 extern uint16_t ntohs (uint16_t __netshort)
588 __attribute__ ((__nothrow__)) __attribute__ ((__const__));
589 extern uint32_t htonl (uint32_t __hostlong)
590 __attribute__ ((__nothrow__)) __attribute__ ((__const__));
591 extern uint16_t htons (uint16_t __hostshort)
592 __attribute__ ((__nothrow__)) __attribute__ ((__const__));
593 extern int bindresvport (int __sockfd, struct sockaddr_in *__sock_in) __attribute__ ((__nothrow__));
594 extern int bindresvport6 (int __sockfd, struct sockaddr_in6 *__sock_in)
595 __attribute__ ((__nothrow__));
598 struct in6_addr ipi6_addr;
599 unsigned int ipi6_ifindex;
603 struct sockaddr_in6 ip6m_addr;
619 typedef struct _IO_FILE FILE;
622 extern void setpwent (void);
623 extern void endpwent (void);
624 extern struct passwd *getpwent (void);
625 extern struct passwd *fgetpwent (FILE *__stream);
626 extern int putpwent (__const struct passwd *__restrict __p,
627 FILE *__restrict __f);
628 extern struct passwd *getpwuid (__uid_t __uid);
629 extern struct passwd *getpwnam (__const char *__name);
630 extern int getpwent_r (struct passwd *__restrict __resultbuf,
631 char *__restrict __buffer, size_t __buflen,
632 struct passwd **__restrict __result);
633 extern int getpwuid_r (__uid_t __uid,
634 struct passwd *__restrict __resultbuf,
635 char *__restrict __buffer, size_t __buflen,
636 struct passwd **__restrict __result);
637 extern int getpwnam_r (__const char *__restrict __name,
638 struct passwd *__restrict __resultbuf,
639 char *__restrict __buffer, size_t __buflen,
640 struct passwd **__restrict __result);
641 extern int fgetpwent_r (FILE *__restrict __stream,
642 struct passwd *__restrict __resultbuf,
643 char *__restrict __buffer, size_t __buflen,
644 struct passwd **__restrict __result);
648 extern void *memcpy (void *__restrict __dest,
649 __const void *__restrict __src, size_t __n)
650 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1, 2)));
651 extern void *memmove (void *__dest, __const void *__src, size_t __n)
652 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1, 2)));
654 extern void *memccpy (void *__restrict __dest, __const void *__restrict __src,
656 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1, 2)));
658 extern void *memset (void *__s, int __c, size_t __n) __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1)));
659 extern int memcmp (__const void *__s1, __const void *__s2, size_t __n)
660 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
661 extern void *memchr (__const void *__s, int __c, size_t __n)
662 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
665 extern char *strcpy (char *__restrict __dest, __const char *__restrict __src)
666 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1, 2)));
667 extern char *strncpy (char *__restrict __dest,
668 __const char *__restrict __src, size_t __n)
669 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1, 2)));
670 extern char *strcat (char *__restrict __dest, __const char *__restrict __src)
671 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1, 2)));
672 extern char *strncat (char *__restrict __dest, __const char *__restrict __src,
673 size_t __n) __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1, 2)));
674 extern int strcmp (__const char *__s1, __const char *__s2)
675 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
676 extern int strncmp (__const char *__s1, __const char *__s2, size_t __n)
677 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
678 extern int strcoll (__const char *__s1, __const char *__s2)
679 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
680 extern size_t strxfrm (char *__restrict __dest,
681 __const char *__restrict __src, size_t __n)
682 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (2)));
684 extern char *strdup (__const char *__s)
685 __attribute__ ((__nothrow__)) __attribute__ ((__malloc__)) __attribute__ ((__nonnull__ (1)));
687 extern char *strchr (__const char *__s, int __c)
688 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
689 extern char *strrchr (__const char *__s, int __c)
690 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
693 extern size_t strcspn (__const char *__s, __const char *__reject)
694 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
695 extern size_t strspn (__const char *__s, __const char *__accept)
696 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
697 extern char *strpbrk (__const char *__s, __const char *__accept)
698 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
699 extern char *strstr (__const char *__haystack, __const char *__needle)
700 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
701 extern char *strtok (char *__restrict __s, __const char *__restrict __delim)
702 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (2)));
704 extern char *__strtok_r (char *__restrict __s,
705 __const char *__restrict __delim,
706 char **__restrict __save_ptr)
707 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (2, 3)));
708 extern char *strtok_r (char *__restrict __s, __const char *__restrict __delim,
709 char **__restrict __save_ptr)
710 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (2, 3)));
712 extern size_t strlen (__const char *__s)
713 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
716 extern char *strerror (int __errnum) __attribute__ ((__nothrow__));
718 extern int strerror_r (int __errnum, char *__buf, size_t __buflen) __asm__ ("" "__xpg_strerror_r") __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (2)));
719 extern void __bzero (void *__s, size_t __n) __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1)));
720 extern void bcopy (__const void *__src, void *__dest, size_t __n)
721 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1, 2)));
722 extern void bzero (void *__s, size_t __n) __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1)));
723 extern int bcmp (__const void *__s1, __const void *__s2, size_t __n)
724 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
725 extern char *index (__const char *__s, int __c)
726 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
727 extern char *rindex (__const char *__s, int __c)
728 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1)));
729 extern int ffs (int __i) __attribute__ ((__nothrow__)) __attribute__ ((__const__));
730 extern int strcasecmp (__const char *__s1, __const char *__s2)
731 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
732 extern int strncasecmp (__const char *__s1, __const char *__s2, size_t __n)
733 __attribute__ ((__nothrow__)) __attribute__ ((__pure__)) __attribute__ ((__nonnull__ (1, 2)));
734 extern char *strsep (char **__restrict __stringp,
735 __const char *__restrict __delim)
736 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1, 2)));
739 extern char *crypt (__const char *__key, __const char *__salt)
740 __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1, 2)));
741 extern void setkey (__const char *__key) __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1)));
742 extern void encrypt (char *__block, int __edflag) __attribute__ ((__nothrow__)) __attribute__ ((__nonnull__ (1)));
744 extern char *__md5_crypt(const unsigned char *pw, const unsigned char *salt);
745 extern char *__des_crypt(const unsigned char *pw, const unsigned char *salt);
746 static u_char inv_key_perm[64];
747 static u_char inv_comp_perm[56];
748 static u_char u_sbox[8][64];
749 static u_char un_pbox[32];
750 static u_int32_t en_keysl[16], en_keysr[16];
751 static u_int32_t de_keysl[16], de_keysr[16];
752 static u_int32_t ip_maskl[8][256], ip_maskr[8][256];
753 static u_int32_t fp_maskl[8][256], fp_maskr[8][256];
754 static u_int32_t key_perm_maskl[8][128], key_perm_maskr[8][128];
755 static u_int32_t comp_maskl[8][128], comp_maskr[8][128];
756 static u_int32_t saltbits;
757 static u_int32_t old_salt;
758 static u_int32_t old_rawkey0, old_rawkey1;
759 static u_char init_perm[64], final_perm[64];
760 static u_char m_sbox[4][4096];
761 static u_int32_t psbox[4][256];
762 static const u_char ascii64[] =
763 "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
764 static const u_char IP[64] = {
765 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
766 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
767 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
768 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
770 static const u_char key_perm[56] = {
771 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18,
772 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36,
773 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22,
774 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4
776 static const u_char key_shifts[16] = {
777 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
779 static const u_char comp_perm[48] = {
780 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10,
781 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2,
782 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
783 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
785 static const u_char sbox[8][64] = {
787 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
788 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
789 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
790 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13},
792 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
793 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
794 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
795 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9},
797 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
798 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
799 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
800 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12},
802 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
803 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
804 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
805 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14},
807 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
808 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
809 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
810 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3},
812 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
813 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
814 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
815 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13},
817 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
818 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
819 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
820 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12},
822 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
823 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
824 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
825 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}
827 static const u_char pbox[32] = {
828 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10,
829 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25
831 static const u_int32_t bits32[32] = {
832 0x80000000, 0x40000000, 0x20000000, 0x10000000,
833 0x08000000, 0x04000000, 0x02000000, 0x01000000,
834 0x00800000, 0x00400000, 0x00200000, 0x00100000,
835 0x00080000, 0x00040000, 0x00020000, 0x00010000,
836 0x00008000, 0x00004000, 0x00002000, 0x00001000,
837 0x00000800, 0x00000400, 0x00000200, 0x00000100,
838 0x00000080, 0x00000040, 0x00000020, 0x00000010,
839 0x00000008, 0x00000004, 0x00000002, 0x00000001
841 static const u_char bits8[8] =
842 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
843 static const u_int32_t *bits28, *bits24;
844 static int ascii_to_bin(char ch)
849 return (ch - 'a' + 38);
853 return (ch - 'A' + 12);
860 static void des_init(void)
862 int i, j, b, k, inbit, obit;
863 u_int32_t *p, *il, *ir, *fl, *fr;
864 static int des_initialised = 0;
865 if (des_initialised == 1)
867 old_rawkey0 = old_rawkey1 = 0L;
870 bits24 = (bits28 = bits32 + 4) + 4;
871 for (i = 0; i < 8; i++)
872 for (j = 0; j < 64; j++) {
873 b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf);
874 u_sbox[i][j] = sbox[i][b];
876 for (b = 0; b < 4; b++)
877 for (i = 0; i < 64; i++)
878 for (j = 0; j < 64; j++)
879 m_sbox[b][(i << 6) | j] =
880 (u_char) ((u_sbox[(b << 1)][i] << 4) |
881 u_sbox[(b << 1) + 1][j]);
882 for (i = 0; i < 64; i++) {
883 init_perm[final_perm[i] = IP[i] - 1] = (u_char) i;
884 inv_key_perm[i] = 255;
886 for (i = 0; i < 56; i++) {
887 inv_key_perm[key_perm[i] - 1] = (u_char) i;
888 inv_comp_perm[i] = 255;
890 for (i = 0; i < 48; i++) {
891 inv_comp_perm[comp_perm[i] - 1] = (u_char) i;
893 for (k = 0; k < 8; k++) {
894 for (i = 0; i < 256; i++) {
895 *(il = &ip_maskl[k][i]) = 0L;
896 *(ir = &ip_maskr[k][i]) = 0L;
897 *(fl = &fp_maskl[k][i]) = 0L;
898 *(fr = &fp_maskr[k][i]) = 0L;
899 for (j = 0; j < 8; j++) {
902 if ((obit = init_perm[inbit]) < 32)
905 *ir |= bits32[obit - 32];
906 if ((obit = final_perm[inbit]) < 32)
909 *fr |= bits32[obit - 32];
913 for (i = 0; i < 128; i++) {
914 *(il = &key_perm_maskl[k][i]) = 0L;
915 *(ir = &key_perm_maskr[k][i]) = 0L;
916 for (j = 0; j < 7; j++) {
918 if (i & bits8[j + 1]) {
919 if ((obit = inv_key_perm[inbit]) == 255)
924 *ir |= bits28[obit - 28];
927 *(il = &comp_maskl[k][i]) = 0L;
928 *(ir = &comp_maskr[k][i]) = 0L;
929 for (j = 0; j < 7; j++) {
931 if (i & bits8[j + 1]) {
933 inv_comp_perm[inbit]) == 255)
938 *ir |= bits24[obit - 24];
943 for (i = 0; i < 32; i++)
944 un_pbox[pbox[i] - 1] = (u_char) i;
945 for (b = 0; b < 4; b++)
946 for (i = 0; i < 256; i++) {
947 *(p = &psbox[b][i]) = 0L;
948 for (j = 0; j < 8; j++) {
950 *p |= bits32[un_pbox[8 * b + j]];
955 static void setup_salt(u_int32_t salt)
957 u_int32_t obit, saltbit;
959 if (salt == old_salt)
965 for (i = 0; i < 24; i++) {
972 static int des_setkey(const char *key)
974 u_int32_t k0, k1, rawkey0, rawkey1;
977 rawkey0 = ntohl(*(const u_int32_t *)key);
978 rawkey1 = ntohl(*(const u_int32_t *)(key + 4));
979 if ((rawkey0 | rawkey1)
980 && rawkey0 == old_rawkey0 && rawkey1 == old_rawkey1) {
983 old_rawkey0 = rawkey0;
984 old_rawkey1 = rawkey1;
985 k0 = key_perm_maskl[0][rawkey0 >> 25]
986 | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f]
987 | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f]
988 | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f]
989 | key_perm_maskl[4][rawkey1 >> 25]
990 | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f]
991 | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f]
992 | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f];
993 k1 = key_perm_maskr[0][rawkey0 >> 25]
994 | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f]
995 | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f]
996 | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f]
997 | key_perm_maskr[4][rawkey1 >> 25]
998 | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f]
999 | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f]
1000 | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f];
1002 for (round = 0; round < 16; round++) {
1004 shifts += key_shifts[round];
1005 t0 = (k0 << shifts) | (k0 >> (28 - shifts));
1006 t1 = (k1 << shifts) | (k1 >> (28 - shifts));
1007 de_keysl[15 - round] =
1008 en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f]
1009 | comp_maskl[1][(t0 >> 14) & 0x7f]
1010 | comp_maskl[2][(t0 >> 7) & 0x7f]
1011 | comp_maskl[3][t0 & 0x7f]
1012 | comp_maskl[4][(t1 >> 21) & 0x7f]
1013 | comp_maskl[5][(t1 >> 14) & 0x7f]
1014 | comp_maskl[6][(t1 >> 7) & 0x7f]
1015 | comp_maskl[7][t1 & 0x7f];
1016 de_keysr[15 - round] =
1017 en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f]
1018 | comp_maskr[1][(t0 >> 14) & 0x7f]
1019 | comp_maskr[2][(t0 >> 7) & 0x7f]
1020 | comp_maskr[3][t0 & 0x7f]
1021 | comp_maskr[4][(t1 >> 21) & 0x7f]
1022 | comp_maskr[5][(t1 >> 14) & 0x7f]
1023 | comp_maskr[6][(t1 >> 7) & 0x7f]
1024 | comp_maskr[7][t1 & 0x7f];
1029 do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t * l_out, u_int32_t * r_out,
1032 u_int32_t l, r, *kl, *kr, *kl1, *kr1;
1033 u_int32_t f, r48l, r48r;
1037 } else if (count > 0) {
1045 l = ip_maskl[0][l_in >> 24]
1046 | ip_maskl[1][(l_in >> 16) & 0xff]
1047 | ip_maskl[2][(l_in >> 8) & 0xff]
1048 | ip_maskl[3][l_in & 0xff]
1049 | ip_maskl[4][r_in >> 24]
1050 | ip_maskl[5][(r_in >> 16) & 0xff]
1051 | ip_maskl[6][(r_in >> 8) & 0xff]
1052 | ip_maskl[7][r_in & 0xff];
1053 r = ip_maskr[0][l_in >> 24]
1054 | ip_maskr[1][(l_in >> 16) & 0xff]
1055 | ip_maskr[2][(l_in >> 8) & 0xff]
1056 | ip_maskr[3][l_in & 0xff]
1057 | ip_maskr[4][r_in >> 24]
1058 | ip_maskr[5][(r_in >> 16) & 0xff]
1059 | ip_maskr[6][(r_in >> 8) & 0xff]
1060 | ip_maskr[7][r_in & 0xff];
1066 r48l = ((r & 0x00000001) << 23)
1067 | ((r & 0xf8000000) >> 9)
1068 | ((r & 0x1f800000) >> 11)
1069 | ((r & 0x01f80000) >> 13)
1070 | ((r & 0x001f8000) >> 15);
1071 r48r = ((r & 0x0001f800) << 7)
1072 | ((r & 0x00001f80) << 5)
1073 | ((r & 0x000001f8) << 3)
1074 | ((r & 0x0000001f) << 1)
1075 | ((r & 0x80000000) >> 31);
1076 f = (r48l ^ r48r) & saltbits;
1079 f = psbox[0][m_sbox[0][r48l >> 12]]
1080 | psbox[1][m_sbox[1][r48l & 0xfff]]
1081 | psbox[2][m_sbox[2][r48r >> 12]]
1082 | psbox[3][m_sbox[3][r48r & 0xfff]];
1090 *l_out = fp_maskl[0][l >> 24]
1091 | fp_maskl[1][(l >> 16) & 0xff]
1092 | fp_maskl[2][(l >> 8) & 0xff]
1093 | fp_maskl[3][l & 0xff]
1094 | fp_maskl[4][r >> 24]
1095 | fp_maskl[5][(r >> 16) & 0xff]
1096 | fp_maskl[6][(r >> 8) & 0xff]
1097 | fp_maskl[7][r & 0xff];
1098 *r_out = fp_maskr[0][l >> 24]
1099 | fp_maskr[1][(l >> 16) & 0xff]
1100 | fp_maskr[2][(l >> 8) & 0xff]
1101 | fp_maskr[3][l & 0xff]
1102 | fp_maskr[4][r >> 24]
1103 | fp_maskr[5][(r >> 16) & 0xff]
1104 | fp_maskr[6][(r >> 8) & 0xff]
1105 | fp_maskr[7][r & 0xff];
1108 void setkey(const char *key)
1111 u_int32_t packed_keys[2];
1113 p = (u_char *) packed_keys;
1114 for (i = 0; i < 8; i++) {
1116 for (j = 0; j < 8; j++)
1120 des_setkey((char *)p);
1122 void encrypt(char *block, int flag)
1129 p = (u_char *) block;
1130 for (i = 0; i < 2; i++) {
1132 for (j = 0; j < 32; j++)
1136 do_des(io[0], io[1], io, io + 1, flag ? -1 : 1);
1137 for (i = 0; i < 2; i++)
1138 for (j = 0; j < 32; j++)
1139 block[(i << 5) | j] = (io[i] & bits32[j]) ? 1 : 0;
1141 char *__des_crypt(const unsigned char *key, const unsigned char *setting)
1143 u_int32_t count, salt, l, r0, r1, keybuf[2];
1145 static char output[21];
1147 q = (u_char *) keybuf;
1148 while (q - (u_char *) keybuf - 8) {
1153 if (des_setkey((char *)keybuf))
1154 return (((void *)0));
1157 salt = (ascii_to_bin(setting[1]) << 6)
1158 | ascii_to_bin(setting[0]);
1159 output[0] = setting[0];
1160 output[1] = setting[1] ? setting[1] : output[0];
1161 p = (u_char *) output + 2;
1164 if (do_des(0L, 0L, &r0, &r1, (int)count))
1165 return (((void *)0));
1167 *p++ = ascii64[(l >> 18) & 0x3f];
1168 *p++ = ascii64[(l >> 12) & 0x3f];
1169 *p++ = ascii64[(l >> 6) & 0x3f];
1170 *p++ = ascii64[l & 0x3f];
1171 l = (r0 << 16) | ((r1 >> 16) & 0xffff);
1172 *p++ = ascii64[(l >> 18) & 0x3f];
1173 *p++ = ascii64[(l >> 12) & 0x3f];
1174 *p++ = ascii64[(l >> 6) & 0x3f];
1175 *p++ = ascii64[l & 0x3f];
1177 *p++ = ascii64[(l >> 12) & 0x3f];
1178 *p++ = ascii64[(l >> 6) & 0x3f];
1179 *p++ = ascii64[l & 0x3f];