From b11f59548803413101b72d28f42cbe997d64a144 Mon Sep 17 00:00:00 2001 From: Geoff Keating Date: Sat, 4 Mar 2000 00:47:31 +0000 Subject: * Makeconfig (rpath-dirs): Add crypt. (all-subdirs): Remove md5crypt. * crypt/sysdeps/unix/crypt.c: Move to crypt/. * crypt/sysdeps/unix/crypt-entry.c: Move to crypt/. * sysdeps/generic/crypt-entry.c: Delete. * crypt/sysdeps/unix/crypt.h: Move to crypt/. * sysdeps/generic/crypt.h: Delete. * crypt/sysdeps/unix/crypt-private.h: Move to crypt/. * crypt/sysdeps/unix/crypt_util.c: Move to crypt/. * crypt/sysdeps/unix/des_impl.c: Move to sunrpc/. * sysdeps/generic/des_impl.c: Delete. * crypt/sysdeps/unix/ufc-crypt.h: Move to crypt/. * crypt/sysdeps: Delete. * crypt/crypt_util.c: Don't use `patchlevel.h'. * md5-crypt/Versions: Move to crypt/. * md5-crypt/md5-crypt.c: Move to crypt/. * md5-crypt/md5.c: Move to crypt/. * md5-crypt/md5c-test.c: Move to crypt/. * md5-crypt/md5test.c: Move to crypt/. * md5-crypt: Delete. * crypt/Makefile: Adjust for new structure. 2000-03-03 Geoff Keating * Makeconfig (rpath-dirs): Add crypt. (all-subdirs): Remove md5crypt. * crypt/sysdeps/unix/crypt.c: Move to crypt/. * crypt/sysdeps/unix/crypt-entry.c: Move to crypt/. * sysdeps/generic/crypt-entry.c: Delete. * crypt/sysdeps/unix/crypt.h: Move to crypt/. * sysdeps/generic/crypt.h: Delete. * crypt/sysdeps/unix/crypt-private.h: Move to crypt/. * crypt/sysdeps/unix/crypt_util.c: Move to crypt/. * crypt/sysdeps/unix/des_impl.c: Move to sunrpc/. * sysdeps/generic/des_impl.c: Delete. * crypt/sysdeps/unix/ufc-crypt.h: Move to crypt/. * crypt/sysdeps: Delete. * crypt/crypt_util.c: Don't use `patchlevel.h'. * md5-crypt/Versions: Move to crypt/. * md5-crypt/md5-crypt.c: Move to crypt/. * md5-crypt/md5.c: Move to crypt/. * md5-crypt/md5c-test.c: Move to crypt/. * md5-crypt/md5test.c: Move to crypt/. * md5-crypt: Delete. * crypt/Makefile: Adjust for new structure. --- ChangeLog | 24 + Makeconfig | 4 +- crypt/Makefile | 33 +- crypt/Versions | 5 + crypt/crypt-entry.c | 149 ++++++ crypt/crypt-private.h | 60 +++ crypt/crypt.c | 120 +++++ crypt/crypt.h | 71 +++ crypt/crypt_util.c | 911 ++++++++++++++++++++++++++++++++++++ crypt/md5-crypt.c | 223 +++++++++ crypt/md5.c | 428 +++++++++++++++++ crypt/md5.h | 147 ++++++ crypt/md5c-test.c | 15 + crypt/md5test.c | 45 ++ crypt/sysdeps/mach/hurd/Implies | 2 - crypt/sysdeps/unix/Makefile | 4 - crypt/sysdeps/unix/crypt-entry.c | 149 ------ crypt/sysdeps/unix/crypt-private.h | 60 --- crypt/sysdeps/unix/crypt.c | 120 ----- crypt/sysdeps/unix/crypt.h | 71 --- crypt/sysdeps/unix/crypt_util.c | 914 ------------------------------------- crypt/sysdeps/unix/des_impl.c | 615 ------------------------- crypt/sysdeps/unix/patchlevel.h | 25 - crypt/sysdeps/unix/ufc-crypt.h | 29 -- crypt/ufc-crypt.h | 29 ++ md5-crypt/Makefile | 83 ---- md5-crypt/Versions | 5 - md5-crypt/md5-crypt.c | 223 --------- md5-crypt/md5.c | 428 ----------------- md5-crypt/md5.h | 147 ------ md5-crypt/md5c-test.c | 15 - md5-crypt/md5test.c | 45 -- md5-crypt/onlymd5-entry.c | 1 - sunrpc/des_impl.c | 615 +++++++++++++++++++++++++ sysdeps/generic/crypt-entry.c | 72 --- sysdeps/generic/crypt.h | 59 --- sysdeps/generic/des_impl.c | 30 -- 37 files changed, 2868 insertions(+), 3108 deletions(-) create mode 100644 crypt/Versions create mode 100644 crypt/crypt-entry.c create mode 100644 crypt/crypt-private.h create mode 100644 crypt/crypt.c create mode 100644 crypt/crypt.h create mode 100644 crypt/crypt_util.c create mode 100644 crypt/md5-crypt.c create mode 100644 crypt/md5.c create mode 100644 crypt/md5.h create mode 100644 crypt/md5c-test.c create mode 100644 crypt/md5test.c delete mode 100644 crypt/sysdeps/mach/hurd/Implies delete mode 100644 crypt/sysdeps/unix/Makefile delete mode 100644 crypt/sysdeps/unix/crypt-entry.c delete mode 100644 crypt/sysdeps/unix/crypt-private.h delete mode 100644 crypt/sysdeps/unix/crypt.c delete mode 100644 crypt/sysdeps/unix/crypt.h delete mode 100644 crypt/sysdeps/unix/crypt_util.c delete mode 100644 crypt/sysdeps/unix/des_impl.c delete mode 100644 crypt/sysdeps/unix/patchlevel.h delete mode 100644 crypt/sysdeps/unix/ufc-crypt.h create mode 100644 crypt/ufc-crypt.h delete mode 100644 md5-crypt/Makefile delete mode 100644 md5-crypt/Versions delete mode 100644 md5-crypt/md5-crypt.c delete mode 100644 md5-crypt/md5.c delete mode 100644 md5-crypt/md5.h delete mode 100644 md5-crypt/md5c-test.c delete mode 100644 md5-crypt/md5test.c delete mode 100644 md5-crypt/onlymd5-entry.c create mode 100644 sunrpc/des_impl.c delete mode 100644 sysdeps/generic/crypt-entry.c delete mode 100644 sysdeps/generic/crypt.h delete mode 100644 sysdeps/generic/des_impl.c diff --git a/ChangeLog b/ChangeLog index 7cbabb0..a0d47fb 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,27 @@ +2000-03-03 Geoff Keating + + * Makeconfig (rpath-dirs): Add crypt. + (all-subdirs): Remove md5crypt. + * crypt/sysdeps/unix/crypt.c: Move to crypt/. + * crypt/sysdeps/unix/crypt-entry.c: Move to crypt/. + * sysdeps/generic/crypt-entry.c: Delete. + * crypt/sysdeps/unix/crypt.h: Move to crypt/. + * sysdeps/generic/crypt.h: Delete. + * crypt/sysdeps/unix/crypt-private.h: Move to crypt/. + * crypt/sysdeps/unix/crypt_util.c: Move to crypt/. + * crypt/sysdeps/unix/des_impl.c: Move to sunrpc/. + * sysdeps/generic/des_impl.c: Delete. + * crypt/sysdeps/unix/ufc-crypt.h: Move to crypt/. + * crypt/sysdeps: Delete. + * crypt/crypt_util.c: Don't use `patchlevel.h'. + * md5-crypt/Versions: Move to crypt/. + * md5-crypt/md5-crypt.c: Move to crypt/. + * md5-crypt/md5.c: Move to crypt/. + * md5-crypt/md5c-test.c: Move to crypt/. + * md5-crypt/md5test.c: Move to crypt/. + * md5-crypt: Delete. + * crypt/Makefile: Adjust for new structure. + 2000-03-03 Paul Eggert * posix/regex.c (MB_LEN_MAX): Include to define; diff --git a/Makeconfig b/Makeconfig index 285b747..6d87892 100644 --- a/Makeconfig +++ b/Makeconfig @@ -414,7 +414,7 @@ link-libc = -Wl,-rpath-link=$(rpath-link) \ $(common-objpfx)libc.so$(libc.so-version) \ $(common-objpfx)$(patsubst %,$(libtype.oS),c) $(gnulib) # This is how to find at build-time things that will be installed there. -rpath-dirs = math elf dlfcn nss nis rt resolv +rpath-dirs = math elf dlfcn nss nis rt resolv crypt rpath-link = \ $(common-objdir):$(subst $(empty) ,:,$(patsubst ../$(subdir),.,$(rpath-dirs:%=$(common-objpfx)%))) elfobjdir := $(patsubst ../$(subdir),.,$(common-objpfx)elf) @@ -781,7 +781,7 @@ endif all-subdirs = csu assert ctype locale intl catgets math setjmp signal \ stdlib stdio-common $(stdio) malloc string wcsmbs time dirent \ grp pwd posix io termios resource misc socket sysvipc gmon \ - gnulib iconv iconvdata wctype manual shadow md5-crypt po argp \ + gnulib iconv iconvdata wctype manual shadow po argp \ crypt $(add-ons) nss localedata timezone rt debug \ $(sysdep-subdirs) $(dlfcn) $(binfmt-subdir) all-subdirs := $(filter-out $(sysdep-inhibit-subdirs),$(all-subdirs)) diff --git a/crypt/Makefile b/crypt/Makefile index 1be33be..a9ce7da 100644 --- a/crypt/Makefile +++ b/crypt/Makefile @@ -1,4 +1,4 @@ -# Copyright (C) 1996 Free Software Foundation, Inc. +# Copyright (C) 1996, 2000 Free Software Foundation, Inc. # This file is part of the GNU C Library. # The GNU C Library is free software; you can redistribute it and/or @@ -17,20 +17,35 @@ # Boston, MA 02111-1307, USA. # -# Sub-makefile for DES crypt portion of the library. +# Sub-makefile for crypt() portion of the library. # subdir := crypt -tests = cert +headers := crypt.h + +distribute := md5.h + +extra-libs := libcrypt +extra-libs-others := $(extra-libs) + +libcrypt-routines := crypt-entry md5-crypt md5 crypt crypt_util + +tests = cert md5test md5c-test include ../Makeconfig -rpath-dirs += md5-crypt +ifeq ($(crypt-in-libc),yes) +routines += $(libcrypt-routines) +endif + +$(objpfx)md5test: $(objpfx)md5.o include ../Rules -ifeq ($(build-shared),yes) -$(objpfx)cert: $(common-objpfx)md5-crypt/libcrypt.so$(libcrypt.so-version) -else -$(objpfx)cert: $(common-objpfx)md5-crypt/libcrypt.a -endif +LDLIBS-cert = crypt/libcrypt +LDLIBS-md5c-test = crypt/libcrypt + +# Depend on libc.so so a DT_NEEDED is generated in the shared objects. +# This ensures they will load libc.so for needed symbols if loaded by +# a statically-linked program that hasn't already loaded it. +$(objpfx)libcrypt.so: $(common-objpfx)libc.so diff --git a/crypt/Versions b/crypt/Versions new file mode 100644 index 0000000..389e7d5 --- /dev/null +++ b/crypt/Versions @@ -0,0 +1,5 @@ +libcrypt { + GLIBC_2.0 { + crypt; crypt_r; encrypt; encrypt_r; fcrypt; setkey; setkey_r; + } +} diff --git a/crypt/crypt-entry.c b/crypt/crypt-entry.c new file mode 100644 index 0000000..9d99c53 --- /dev/null +++ b/crypt/crypt-entry.c @@ -0,0 +1,149 @@ +/* + * UFC-crypt: ultra fast crypt(3) implementation + * + * Copyright (C) 1991, 1992, 1993, 1996, 1997 Free Software Foundation, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; see the file COPYING.LIB. If not, + * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * crypt entry points + * + * @(#)crypt-entry.c 1.2 12/20/96 + * + */ + +#ifdef DEBUG +#include +#endif +#include + +#ifndef STATIC +#define STATIC static +#endif + +#ifndef DOS +#include "ufc-crypt.h" +#else +/* + * Thanks to greg%wind@plains.NoDak.edu (Greg W. Wettstein) + * for DOS patches + */ +#include "ufc.h" +#endif +#include "crypt.h" +#include "crypt-private.h" + +/* Prototypes for local functions. */ +#if __STDC__ - 0 +#ifndef __GNU_LIBRARY__ +void _ufc_clearmem (char *start, int cnt); +#else +#define _ufc_clearmem(start, cnt) memset(start, 0, cnt) +#endif +extern char *__md5_crypt_r (const char *key, const char *salt, char *buffer, + int buflen); +extern char *__md5_crypt (const char *key, const char *salt); +#endif + +/* Define our magic string to mark salt for MD5 encryption + replacement. This is meant to be the same as for other MD5 based + encryption implementations. */ +static const char md5_salt_prefix[] = "$1$"; + +/* For use by the old, non-reentrant routines (crypt/encrypt/setkey) */ +extern struct crypt_data _ufc_foobar; + +/* + * UNIX crypt function + */ + +char * +__crypt_r (key, salt, data) + const char *key; + const char *salt; + struct crypt_data * __restrict data; +{ + ufc_long res[4]; + char ktab[9]; + ufc_long xx = 25; /* to cope with GCC long long compiler bugs */ + +#ifdef _LIBC + /* Try to find out whether we have to use MD5 encryption replacement. */ + if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0) + return __md5_crypt_r (key, salt, (char *) data, + sizeof (struct crypt_data)); +#endif + + /* + * Hack DES tables according to salt + */ + _ufc_setup_salt_r (salt, data); + + /* + * Setup key schedule + */ + _ufc_clearmem (ktab, (int) sizeof (ktab)); + (void) strncpy (ktab, key, 8); + _ufc_mk_keytab_r (ktab, data); + + /* + * Go for the 25 DES encryptions + */ + _ufc_clearmem ((char*) res, (int) sizeof (res)); + _ufc_doit_r (xx, data, &res[0]); + + /* + * Do final permutations + */ + _ufc_dofinalperm_r (res, data); + + /* + * And convert back to 6 bit ASCII + */ + _ufc_output_conversion_r (res[0], res[1], salt, data); + return data->crypt_3_buf; +} +weak_alias (__crypt_r, crypt_r) + +char * +crypt (key, salt) + const char *key; + const char *salt; +{ +#ifdef _LIBC + /* Try to find out whether we have to use MD5 encryption replacement. */ + if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0) + return __md5_crypt (key, salt); +#endif + + return __crypt_r (key, salt, &_ufc_foobar); +} + + +/* + * To make fcrypt users happy. + * They don't need to call init_des. + */ +#ifdef _LIBC +weak_alias (crypt, fcrypt) +#else +char * +__fcrypt (key, salt) + const char *key; + const char *salt; +{ + return crypt (key, salt); +} +#endif diff --git a/crypt/crypt-private.h b/crypt/crypt-private.h new file mode 100644 index 0000000..130cccd --- /dev/null +++ b/crypt/crypt-private.h @@ -0,0 +1,60 @@ +/* + * UFC-crypt: ultra fast crypt(3) implementation + * + * Copyright (C) 1991, 92, 93, 96, 97, 98 Free Software Foundation, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; see the file COPYING.LIB. If not, + * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * @(#)crypt-private.h 1.4 12/20/96 + */ + +/* Prototypes for local functions in libcrypt.a. */ + +#ifndef CRYPT_PRIVATE_H +#define CRYPT_PRIVATE_H 1 + +#include + +/* crypt.c */ +extern void _ufc_doit_r (ufc_long itr, struct crypt_data * __restrict __data, + ufc_long *res); + + +/* crypt_util.c */ +extern void __init_des_r (struct crypt_data * __restrict __data); +extern void __init_des (void); + +extern void _ufc_setup_salt_r (__const char *s, + struct crypt_data * __restrict __data); +extern void _ufc_mk_keytab_r (__const char *key, + struct crypt_data * __restrict __data); +extern void _ufc_dofinalperm_r (ufc_long *res, + struct crypt_data * __restrict __data); +extern void _ufc_output_conversion_r (ufc_long v1, ufc_long v2, + __const char *salt, + struct crypt_data * __restrict __data); + +extern void __setkey_r (__const char *__key, + struct crypt_data * __restrict __data); +extern void __encrypt_r (char * __restrict __block, int __edflag, + struct crypt_data * __restrict __data); + +/* crypt-entry.c */ +extern char *__crypt_r (__const char *__key, __const char *__salt, + struct crypt_data * __restrict __data); +extern char *fcrypt (__const char *key, __const char *salt); + +#endif /* crypt-private.h */ diff --git a/crypt/crypt.c b/crypt/crypt.c new file mode 100644 index 0000000..8e2a576 --- /dev/null +++ b/crypt/crypt.c @@ -0,0 +1,120 @@ +/* + * UFC-crypt: ultra fast crypt(3) implementation + * + * Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; see the file COPYING.LIB. If not, + * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * @(#)crypt.c 2.25 12/20/96 + * + * Semiportable C version + * + */ + +#include "ufc-crypt.h" +#include "crypt.h" +#include "crypt-private.h" + +#ifdef _UFC_32_ + +/* + * 32 bit version + */ + +#define SBA(sb, v) (*(long32*)((char*)(sb)+(v))) + +void +_ufc_doit_r(itr, __data, res) + ufc_long itr, *res; + struct crypt_data * __restrict __data; +{ + int i; + long32 s, *k; + long32 *sb01 = (long32*)__data->sb0; + long32 *sb23 = (long32*)__data->sb2; + long32 l1, l2, r1, r2; + + l1 = (long32)res[0]; l2 = (long32)res[1]; + r1 = (long32)res[2]; r2 = (long32)res[3]; + + while(itr--) { + k = (long32*)__data->keysched; + for(i=8; i--; ) { + s = *k++ ^ r1; + l1 ^= SBA(sb01, s & 0xffff); l2 ^= SBA(sb01, (s & 0xffff)+4); + l1 ^= SBA(sb01, s >>= 16 ); l2 ^= SBA(sb01, (s )+4); + s = *k++ ^ r2; + l1 ^= SBA(sb23, s & 0xffff); l2 ^= SBA(sb23, (s & 0xffff)+4); + l1 ^= SBA(sb23, s >>= 16 ); l2 ^= SBA(sb23, (s )+4); + + s = *k++ ^ l1; + r1 ^= SBA(sb01, s & 0xffff); r2 ^= SBA(sb01, (s & 0xffff)+4); + r1 ^= SBA(sb01, s >>= 16 ); r2 ^= SBA(sb01, (s )+4); + s = *k++ ^ l2; + r1 ^= SBA(sb23, s & 0xffff); r2 ^= SBA(sb23, (s & 0xffff)+4); + r1 ^= SBA(sb23, s >>= 16 ); r2 ^= SBA(sb23, (s )+4); + } + s=l1; l1=r1; r1=s; s=l2; l2=r2; r2=s; + } + res[0] = l1; res[1] = l2; res[2] = r1; res[3] = r2; +} + +#endif + +#ifdef _UFC_64_ + +/* + * 64 bit version + */ + +#define SBA(sb, v) (*(long64*)((char*)(sb)+(v))) + +void +_ufc_doit_r(itr, __data, res) + ufc_long itr, *res; + struct crypt_data * __restrict __data; +{ + int i; + long64 l, r, s, *k; + register long64 *sb01 = (long64*)__data->sb0; + register long64 *sb23 = (long64*)__data->sb2; + + l = (((long64)res[0]) << 32) | ((long64)res[1]); + r = (((long64)res[2]) << 32) | ((long64)res[3]); + + while(itr--) { + k = (long64*)__data->keysched; + for(i=8; i--; ) { + s = *k++ ^ r; + l ^= SBA(sb23, (s ) & 0xffff); + l ^= SBA(sb23, (s >>= 16) & 0xffff); + l ^= SBA(sb01, (s >>= 16) & 0xffff); + l ^= SBA(sb01, (s >>= 16) ); + + s = *k++ ^ l; + r ^= SBA(sb23, (s ) & 0xffff); + r ^= SBA(sb23, (s >>= 16) & 0xffff); + r ^= SBA(sb01, (s >>= 16) & 0xffff); + r ^= SBA(sb01, (s >>= 16) ); + } + s=l; l=r; r=s; + } + + res[0] = l >> 32; res[1] = l & 0xffffffff; + res[2] = r >> 32; res[3] = r & 0xffffffff; +} + +#endif diff --git a/crypt/crypt.h b/crypt/crypt.h new file mode 100644 index 0000000..7018cb1 --- /dev/null +++ b/crypt/crypt.h @@ -0,0 +1,71 @@ +/* + * UFC-crypt: ultra fast crypt(3) implementation + * + * Copyright (C) 1991, 92, 93, 96, 97, 98, 2000 Free Software Foundation, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with the GNU C Library; see the file COPYING.LIB. If not, + * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * @(#)crypt.h 1.5 12/20/96 + * + */ + +#ifndef _CRYPT_H +#define _CRYPT_H 1 + +#include + +__BEGIN_DECLS + +/* Encrypt at most 8 characters from KEY using salt to perturb DES. */ +extern char *crypt (__const char *__key, __const char *__salt); + +/* Setup DES tables according KEY. */ +extern void setkey (__const char *__key); + +/* Encrypt data in BLOCK in place if EDFLAG is zero; otherwise decrypt + block in place. */ +extern void encrypt (char *__block, int __edflag); + +#ifdef __USE_GNU +/* Reentrant versions of the functions above. The additional argument + points to a structure where the results are placed in. */ +struct crypt_data + { + char keysched[16 * 8]; + char sb0[32768]; + char sb1[32768]; + char sb2[32768]; + char sb3[32768]; + /* end-of-aligment-critical-data */ + char crypt_3_buf[14]; + char current_salt[2]; + long int current_saltbits; + int direction, initialized; + }; + +extern char *crypt_r (__const char *__key, __const char *__salt, + struct crypt_data * __restrict __data); + +extern void setkey_r (__const char *__key, + struct crypt_data * __restrict __data); + +extern void encrypt_r (char *__block, int __edflag, + struct crypt_data * __restrict __data); +#endif + +__END_DECLS + +#endif /* crypt.h */ diff --git a/crypt/crypt_util.c b/crypt/crypt_util.c new file mode 100644 index 0000000..1eb2d4c --- /dev/null +++ b/crypt/crypt_util.c @@ -0,0 +1,911 @@ +/* + * UFC-crypt: ultra fast crypt(3) implementation + * + * Copyright (C) 1991, 92, 93, 96, 97, 98, 2000 Free Software Foundation, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; see the file COPYING.LIB. If not, + * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + * + * @(#)crypt_util.c 2.56 12/20/96 + * + * Support routines + * + */ + +#ifdef DEBUG +#include +#endif +#include + +#ifndef STATIC +#define STATIC static +#endif + +#ifndef DOS +#include "ufc-crypt.h" +#else +/* + * Thanks to greg%wind@plains.NoDak.edu (Greg W. Wettstein) + * for DOS patches + */ +#include "pl.h" +#include "ufc.h" +#endif +#include "crypt.h" +#include "crypt-private.h" + +/* Prototypes for local functions. */ +#if __STDC__ - 0 +#ifndef __GNU_LIBRARY__ +void _ufc_clearmem (char *start, int cnt); +void _ufc_copymem (char *from, char *to, int cnt); +#endif +#ifdef _UFC_32_ +STATIC void shuffle_sb (long32 *k, ufc_long saltbits); +#else +STATIC void shuffle_sb (long64 *k, ufc_long saltbits); +#endif +#endif + + +/* + * Permutation done once on the 56 bit + * key derived from the original 8 byte ASCII key. + */ +static const int pc1[56] = { + 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, + 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, + 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, + 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 +}; + +/* + * How much to rotate each 28 bit half of the pc1 permutated + * 56 bit key before using pc2 to give the i' key + */ +static const int rots[16] = { + 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 +}; + +/* + * Permutation giving the key + * of the i' DES round + */ +static const int pc2[48] = { + 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, + 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, + 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, + 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 +}; + +/* + * The E expansion table which selects + * bits from the 32 bit intermediate result. + */ +static const int esel[48] = { + 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, + 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, + 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, + 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 +}; + +/* + * Permutation done on the + * result of sbox lookups + */ +static const int perm32[32] = { + 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, + 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 +}; + +/* + * The sboxes + */ +static const int sbox[8][4][16]= { + { { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 }, + { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 }, + { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 }, + { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } + }, + + { { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 }, + { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 }, + { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 }, + { 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 } + }, + + { { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 }, + { 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 }, + { 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 }, + { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } + }, + + { { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 }, + { 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 }, + { 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 }, + { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } + }, + + { { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 }, + { 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 }, + { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 }, + { 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 } + }, + + { { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 }, + { 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 }, + { 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 }, + { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } + }, + + { { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 }, + { 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 }, + { 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 }, + { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } + }, + + { { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 }, + { 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 }, + { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 }, + { 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } + } +}; + +/* + * This is the initial + * permutation matrix + */ +static const int initial_perm[64] = { + 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, + 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, + 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, + 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 +}; + +/* + * This is the final + * permutation matrix + */ +static const int final_perm[64] = { + 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, + 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, + 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, + 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 +}; + +#define ascii_to_bin(c) ((c)>='a'?(c-59):(c)>='A'?((c)-53):(c)-'.') +#define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.') + +static const ufc_long BITMASK[24] = { + 0x40000000, 0x20000000, 0x10000000, 0x08000000, 0x04000000, 0x02000000, + 0x01000000, 0x00800000, 0x00400000, 0x00200000, 0x00100000, 0x00080000, + 0x00004000, 0x00002000, 0x00001000, 0x00000800, 0x00000400, 0x00000200, + 0x00000100, 0x00000080, 0x00000040, 0x00000020, 0x00000010, 0x00000008 +}; + +static const unsigned char bytemask[8] = { + 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 +}; + +static const ufc_long longmask[32] = { + 0x80000000, 0x40000000, 0x20000000, 0x10000000, + 0x08000000, 0x04000000, 0x02000000, 0x01000000, + 0x00800000, 0x00400000, 0x00200000, 0x00100000, + 0x00080000, 0x00040000, 0x00020000, 0x00010000, + 0x00008000, 0x00004000, 0x00002000, 0x00001000, + 0x00000800, 0x00000400, 0x00000200, 0x00000100, + 0x00000080, 0x00000040, 0x00000020, 0x00000010, + 0x00000008, 0x00000004, 0x00000002, 0x00000001 +}; + +/* + * do_pc1: permform pc1 permutation in the key schedule generation. + * + * The first index is the byte number in the 8 byte ASCII key + * - second - - the two 28 bits halfs of the result + * - third - selects the 7 bits actually used of each byte + * + * The result is kept with 28 bit per 32 bit with the 4 most significant + * bits zero. + */ +static ufc_long do_pc1[8][2][128]; + +/* + * do_pc2: permform pc2 permutation in the key schedule generation. + * + * The first index is the septet number in the two 28 bit intermediate values + * - second - - - septet values + * + * Knowledge of the structure of the pc2 permutation is used. + * + * The result is kept with 28 bit per 32 bit with the 4 most significant + * bits zero. + */ +static ufc_long do_pc2[8][128]; + +/* + * eperm32tab: do 32 bit permutation and E selection + * + * The first index is the byte number in the 32 bit value to be permuted + * - second - is the value of this byte + * - third - selects the two 32 bit values + * + * The table is used and generated internally in init_des to speed it up + */ +static ufc_long eperm32tab[4][256][2]; + +/* + * efp: undo an extra e selection and do final + * permutation giving the DES result. + * + * Invoked 6 bit a time on two 48 bit values + * giving two 32 bit longs. + */ +static ufc_long efp[16][64][2]; + +/* + * For use by the old, non-reentrant routines + * (crypt/encrypt/setkey) + */ +struct crypt_data _ufc_foobar; + +#ifdef __GNU_LIBRARY__ +#include + +__libc_lock_define_initialized (static, _ufc_tables_lock) +#endif + +#ifdef DEBUG + +void +_ufc_prbits(a, n) + ufc_long *a; + int n; +{ + ufc_long i, j, t, tmp; + n /= 8; + for(i = 0; i < n; i++) { + tmp=0; + for(j = 0; j < 8; j++) { + t=8*i+j; + tmp|=(a[t/24] & BITMASK[t % 24])?bytemask[j]:0; + } + (void)printf("%02x ",tmp); + } + printf(" "); +} + +static void +_ufc_set_bits(v, b) + ufc_long v; + ufc_long *b; +{ + ufc_long i; + *b = 0; + for(i = 0; i < 24; i++) { + if(v & longmask[8 + i]) + *b |= BITMASK[i]; + } +} + +#endif + +#ifndef __GNU_LIBRARY__ +/* + * Silly rewrites of 'bzero'/'memset'. I do so + * because some machines don't have + * bzero and some don't have memset. + */ + +void +_ufc_clearmem(start, cnt) + char *start; + int cnt; +{ + while(cnt--) + *start++ = '\0'; +} + +void +_ufc_copymem(from, to, cnt) + char *from, *to; + int cnt; +{ + while(cnt--) + *to++ = *from++; +} +#else +#define _ufc_clearmem(start, cnt) memset(start, 0, cnt) +#define _ufc_copymem(from, to, cnt) memcpy(to, from, cnt) +#endif + +/* lookup a 6 bit value in sbox */ + +#define s_lookup(i,s) sbox[(i)][(((s)>>4) & 0x2)|((s) & 0x1)][((s)>>1) & 0xf]; + +/* + * Initialize unit - may be invoked directly + * by fcrypt users. + */ + +void +__init_des_r(__data) + struct crypt_data * __restrict __data; +{ + int comes_from_bit; + int bit, sg; + ufc_long j; + ufc_long mask1, mask2; + int e_inverse[64]; + static volatile int small_tables_initialized = 0; + +#ifdef _UFC_32_ + long32 *sb[4]; + sb[0] = (long32*)__data->sb0; sb[1] = (long32*)__data->sb1; + sb[2] = (long32*)__data->sb2; sb[3] = (long32*)__data->sb3; +#endif +#ifdef _UFC_64_ + long64 *sb[4]; + sb[0] = (long64*)__data->sb0; sb[1] = (long64*)__data->sb1; + sb[2] = (long64*)__data->sb2; sb[3] = (long64*)__data->sb3; +#endif + + if(small_tables_initialized == 0) { +#ifdef __GNU_LIBRARY__ + __libc_lock_lock (_ufc_tables_lock); + if(small_tables_initialized) + goto small_tables_done; +#endif + + /* + * Create the do_pc1 table used + * to affect pc1 permutation + * when generating keys + */ + _ufc_clearmem((char*)do_pc1, (int)sizeof(do_pc1)); + for(bit = 0; bit < 56; bit++) { + comes_from_bit = pc1[bit] - 1; + mask1 = bytemask[comes_from_bit % 8 + 1]; + mask2 = longmask[bit % 28 + 4]; + for(j = 0; j < 128; j++) { + if(j & mask1) + do_pc1[comes_from_bit / 8][bit / 28][j] |= mask2; + } + } + + /* + * Create the do_pc2 table used + * to affect pc2 permutation when + * generating keys + */ + _ufc_clearmem((char*)do_pc2, (int)sizeof(do_pc2)); + for(bit = 0; bit < 48; bit++) { + comes_from_bit = pc2[bit] - 1; + mask1 = bytemask[comes_from_bit % 7 + 1]; + mask2 = BITMASK[bit % 24]; + for(j = 0; j < 128; j++) { + if(j & mask1) + do_pc2[comes_from_bit / 7][j] |= mask2; + } + } + + /* + * Now generate the table used to do combined + * 32 bit permutation and e expansion + * + * We use it because we have to permute 16384 32 bit + * longs into 48 bit in order to initialize sb. + * + * Looping 48 rounds per permutation becomes + * just too slow... + * + */ + + _ufc_clearmem((char*)eperm32tab, (int)sizeof(eperm32tab)); + for(bit = 0; bit < 48; bit++) { + ufc_long mask1,comes_from; + comes_from = perm32[esel[bit]-1]-1; + mask1 = bytemask[comes_from % 8]; + for(j = 256; j--;) { + if(j & mask1) + eperm32tab[comes_from / 8][j][bit / 24] |= BITMASK[bit % 24]; + } + } + + /* + * Create an inverse matrix for esel telling + * where to plug out bits if undoing it + */ + for(bit=48; bit--;) { + e_inverse[esel[bit] - 1 ] = bit; + e_inverse[esel[bit] - 1 + 32] = bit + 48; + } + + /* + * create efp: the matrix used to + * undo the E expansion and effect final permutation + */ + _ufc_clearmem((char*)efp, (int)sizeof efp); + for(bit = 0; bit < 64; bit++) { + int o_bit, o_long; + ufc_long word_value, mask1, mask2; + int comes_from_f_bit, comes_from_e_bit; + int comes_from_word, bit_within_word; + + /* See where bit i belongs in the two 32 bit long's */ + o_long = bit / 32; /* 0..1 */ + o_bit = bit % 32; /* 0..31 */ + + /* + * And find a bit in the e permutated value setting this bit. + * + * Note: the e selection may have selected the same bit several + * times. By the initialization of e_inverse, we only look + * for one specific instance. + */ + comes_from_f_bit = final_perm[bit] - 1; /* 0..63 */ + comes_from_e_bit = e_inverse[comes_from_f_bit]; /* 0..95 */ + comes_from_word = comes_from_e_bit / 6; /* 0..15 */ + bit_within_word = comes_from_e_bit % 6; /* 0..5 */ + + mask1 = longmask[bit_within_word + 26]; + mask2 = longmask[o_bit]; + + for(word_value = 64; word_value--;) { + if(word_value & mask1) + efp[comes_from_word][word_value][o_long] |= mask2; + } + } + small_tables_initialized = 1; +#ifdef __GNU_LIBRARY__ +small_tables_done: + __libc_lock_unlock(_ufc_tables_lock); +#endif + } + + /* + * Create the sb tables: + * + * For each 12 bit segment of an 48 bit intermediate + * result, the sb table precomputes the two 4 bit + * values of the sbox lookups done with the two 6 + * bit halves, shifts them to their proper place, + * sends them through perm32 and finally E expands + * them so that they are ready for the next + * DES round. + * + */ + + _ufc_clearmem((char*)__data->sb0, (int)sizeof(__data->sb0)); + _ufc_clearmem((char*)__data->sb1, (int)sizeof(__data->sb1)); + _ufc_clearmem((char*)__data->sb2, (int)sizeof(__data->sb2)); + _ufc_clearmem((char*)__data->sb3, (int)sizeof(__data->sb3)); + + for(sg = 0; sg < 4; sg++) { + int j1, j2; + int s1, s2; + + for(j1 = 0; j1 < 64; j1++) { + s1 = s_lookup(2 * sg, j1); + for(j2 = 0; j2 < 64; j2++) { + ufc_long to_permute, inx; + + s2 = s_lookup(2 * sg + 1, j2); + to_permute = (((ufc_long)s1 << 4) | + (ufc_long)s2) << (24 - 8 * (ufc_long)sg); + +#ifdef _UFC_32_ + inx = ((j1 << 6) | j2) << 1; + sb[sg][inx ] = eperm32tab[0][(to_permute >> 24) & 0xff][0]; + sb[sg][inx+1] = eperm32tab[0][(to_permute >> 24) & 0xff][1]; + sb[sg][inx ] |= eperm32tab[1][(to_permute >> 16) & 0xff][0]; + sb[sg][inx+1] |= eperm32tab[1][(to_permute >> 16) & 0xff][1]; + sb[sg][inx ] |= eperm32tab[2][(to_permute >> 8) & 0xff][0]; + sb[sg][inx+1] |= eperm32tab[2][(to_permute >> 8) & 0xff][1]; + sb[sg][inx ] |= eperm32tab[3][(to_permute) & 0xff][0]; + sb[sg][inx+1] |= eperm32tab[3][(to_permute) & 0xff][1]; +#endif +#ifdef _UFC_64_ + inx = ((j1 << 6) | j2); + sb[sg][inx] = + ((long64)eperm32tab[0][(to_permute >> 24) & 0xff][0] << 32) | + (long64)eperm32tab[0][(to_permute >> 24) & 0xff][1]; + sb[sg][inx] |= + ((long64)eperm32tab[1][(to_permute >> 16) & 0xff][0] << 32) | + (long64)eperm32tab[1][(to_permute >> 16) & 0xff][1]; + sb[sg][inx] |= + ((long64)eperm32tab[2][(to_permute >> 8) & 0xff][0] << 32) | + (long64)eperm32tab[2][(to_permute >> 8) & 0xff][1]; + sb[sg][inx] |= + ((long64)eperm32tab[3][(to_permute) & 0xff][0] << 32) | + (long64)eperm32tab[3][(to_permute) & 0xff][1]; +#endif + } + } + } + + __data->initialized++; +} + +void +__init_des() +{ + __init_des_r(&_ufc_foobar); +} + +/* + * Process the elements of the sb table permuting the + * bits swapped in the expansion by the current salt. + */ + +#ifdef _UFC_32_ +STATIC void +shuffle_sb(k, saltbits) + long32 *k; + ufc_long saltbits; +{ + ufc_long j; + long32 x; + for(j=4096; j--;) { + x = (k[0] ^ k[1]) & (long32)saltbits; + *k++ ^= x; + *k++ ^= x; + } +} +#endif + +#ifdef _UFC_64_ +STATIC void +shuffle_sb(k, saltbits) + long64 *k; + ufc_long saltbits; +{ + ufc_long j; + long64 x; + for(j=4096; j--;) { + x = ((*k >> 32) ^ *k) & (long64)saltbits; + *k++ ^= (x << 32) | x; + } +} +#endif + +/* + * Setup the unit for a new salt + * Hopefully we'll not see a new salt in each crypt call. + */ + +void +_ufc_setup_salt_r(s, __data) + __const char *s; + struct crypt_data * __restrict __data; +{ + ufc_long i, j, saltbits; + + if(__data->initialized == 0) + __init_des_r(__data); + + if(s[0] == __data->current_salt[0] && s[1] == __data->current_salt[1]) + return; + __data->current_salt[0] = s[0]; __data->current_salt[1] = s[1]; + + /* + * This is the only crypt change to DES: + * entries are swapped in the expansion table + * according to the bits set in the salt. + */ + saltbits = 0; + for(i = 0; i < 2; i++) { + long c=ascii_to_bin(s[i]); + for(j = 0; j < 6; j++) { + if((c >> j) & 0x1) + saltbits |= BITMASK[6 * i + j]; + } + } + + /* + * Permute the sb table values + * to reflect the changed e + * selection table + */ +#ifdef _UFC_32_ +#define LONGG long32* +#endif +#ifdef _UFC_64_ +#define LONGG long64* +#endif + + shuffle_sb((LONGG)__data->sb0, __data->current_saltbits ^ saltbits); + shuffle_sb((LONGG)__data->sb1, __data->current_saltbits ^ saltbits); + shuffle_sb((LONGG)__data->sb2, __data->current_saltbits ^ saltbits); + shuffle_sb((LONGG)__data->sb3, __data->current_saltbits ^ saltbits); + + __data->current_saltbits = saltbits; +} + +void +_ufc_mk_keytab_r(key, __data) + const char *key; + struct crypt_data * __restrict __data; +{ + ufc_long v1, v2, *k1; + int i; +#ifdef _UFC_32_ + long32 v, *k2; + k2 = (long32*)__data->keysched; +#endif +#ifdef _UFC_64_ + long64 v, *k2; + k2 = (long64*)__data->keysched; +#endif + + v1 = v2 = 0; k1 = &do_pc1[0][0][0]; + for(i = 8; i--;) { + v1 |= k1[*key & 0x7f]; k1 += 128; + v2 |= k1[*key++ & 0x7f]; k1 += 128; + } + + for(i = 0; i < 16; i++) { + k1 = &do_pc2[0][0]; + + v1 = (v1 << rots[i]) | (v1 >> (28 - rots[i])); + v = k1[(v1 >> 21) & 0x7f]; k1 += 128; + v |= k1[(v1 >> 14) & 0x7f]; k1 += 128; + v |= k1[(v1 >> 7) & 0x7f]; k1 += 128; + v |= k1[(v1 ) & 0x7f]; k1 += 128; + +#ifdef _UFC_32_ + *k2++ = (v | 0x00008000); + v = 0; +#endif +#ifdef _UFC_64_ + v = (v << 32); +#endif + + v2 = (v2 << rots[i]) | (v2 >> (28 - rots[i])); + v |= k1[(v2 >> 21) & 0x7f]; k1 += 128; + v |= k1[(v2 >> 14) & 0x7f]; k1 += 128; + v |= k1[(v2 >> 7) & 0x7f]; k1 += 128; + v |= k1[(v2 ) & 0x7f]; + +#ifdef _UFC_32_ + *k2++ = (v | 0x00008000); +#endif +#ifdef _UFC_64_ + *k2++ = v | 0x0000800000008000l; +#endif + } + + __data->direction = 0; +} + +/* + * Undo an extra E selection and do final permutations + */ + +void +_ufc_dofinalperm_r(res, __data) + ufc_long *res; + struct crypt_data * __restrict __data; +{ + ufc_long v1, v2, x; + ufc_long l1,l2,r1,r2; + + l1 = res[0]; l2 = res[1]; + r1 = res[2]; r2 = res[3]; + + x = (l1 ^ l2) & __data->current_saltbits; l1 ^= x; l2 ^= x; + x = (r1 ^ r2) & __data->current_saltbits; r1 ^= x; r2 ^= x; + + v1=v2=0; l1 >>= 3; l2 >>= 3; r1 >>= 3; r2 >>= 3; + + v1 |= efp[15][ r2 & 0x3f][0]; v2 |= efp[15][ r2 & 0x3f][1]; + v1 |= efp[14][(r2 >>= 6) & 0x3f][0]; v2 |= efp[14][ r2 & 0x3f][1]; + v1 |= efp[13][(r2 >>= 10) & 0x3f][0]; v2 |= efp[13][ r2 & 0x3f][1]; + v1 |= efp[12][(r2 >>= 6) & 0x3f][0]; v2 |= efp[12][ r2 & 0x3f][1]; + + v1 |= efp[11][ r1 & 0x3f][0]; v2 |= efp[11][ r1 & 0x3f][1]; + v1 |= efp[10][(r1 >>= 6) & 0x3f][0]; v2 |= efp[10][ r1 & 0x3f][1]; + v1 |= efp[ 9][(r1 >>= 10) & 0x3f][0]; v2 |= efp[ 9][ r1 & 0x3f][1]; + v1 |= efp[ 8][(r1 >>= 6) & 0x3f][0]; v2 |= efp[ 8][ r1 & 0x3f][1]; + + v1 |= efp[ 7][ l2 & 0x3f][0]; v2 |= efp[ 7][ l2 & 0x3f][1]; + v1 |= efp[ 6][(l2 >>= 6) & 0x3f][0]; v2 |= efp[ 6][ l2 & 0x3f][1]; + v1 |= efp[ 5][(l2 >>= 10) & 0x3f][0]; v2 |= efp[ 5][ l2 & 0x3f][1]; + v1 |= efp[ 4][(l2 >>= 6) & 0x3f][0]; v2 |= efp[ 4][ l2 & 0x3f][1]; + + v1 |= efp[ 3][ l1 & 0x3f][0]; v2 |= efp[ 3][ l1 & 0x3f][1]; + v1 |= efp[ 2][(l1 >>= 6) & 0x3f][0]; v2 |= efp[ 2][ l1 & 0x3f][1]; + v1 |= efp[ 1][(l1 >>= 10) & 0x3f][0]; v2 |= efp[ 1][ l1 & 0x3f][1]; + v1 |= efp[ 0][(l1 >>= 6) & 0x3f][0]; v2 |= efp[ 0][ l1 & 0x3f][1]; + + res[0] = v1; res[1] = v2; +} + +/* + * crypt only: convert from 64 bit to 11 bit ASCII + * prefixing with the salt + */ + +void +_ufc_output_conversion_r(v1, v2, salt, __data) + ufc_long v1, v2; + __const char *salt; + struct crypt_data * __restrict __data; +{ + int i, s, shf; + + __data->crypt_3_buf[0] = salt[0]; + __data->crypt_3_buf[1] = salt[1] ? salt[1] : salt[0]; + + for(i = 0; i < 5; i++) { + shf = (26 - 6 * i); /* to cope with MSC compiler bug */ + __data->crypt_3_buf[i + 2] = bin_to_ascii((v1 >> shf) & 0x3f); + } + + s = (v2 & 0xf) << 2; + v2 = (v2 >> 2) | ((v1 & 0x3) << 30); + + for(i = 5; i < 10; i++) { + shf = (56 - 6 * i); + __data->crypt_3_buf[i + 2] = bin_to_ascii((v2 >> shf) & 0x3f); + } + + __data->crypt_3_buf[12] = bin_to_ascii(s); + __data->crypt_3_buf[13] = 0; +} + + +/* + * UNIX encrypt function. Takes a bitvector + * represented by one byte per bit and + * encrypt/decrypt according to edflag + */ + +void +__encrypt_r(__block, __edflag, __data) + char *__block; + int __edflag; + struct crypt_data * __restrict __data; +{ + ufc_long l1, l2, r1, r2, res[4]; + int i; +#ifdef _UFC_32_ + long32 *kt; + kt = (long32*)__data->keysched; +#endif +#ifdef _UFC_64_ + long64 *kt; + kt = (long64*)__data->keysched; +#endif + + /* + * Undo any salt changes to E expansion + */ + _ufc_setup_salt_r("..", __data); + + /* + * Reverse key table if + * changing operation (encrypt/decrypt) + */ + if((__edflag == 0) != (__data->direction == 0)) { + for(i = 0; i < 8; i++) { +#ifdef _UFC_32_ + long32 x; + x = kt[2 * (15-i)]; + kt[2 * (15-i)] = kt[2 * i]; + kt[2 * i] = x; + + x = kt[2 * (15-i) + 1]; + kt[2 * (15-i) + 1] = kt[2 * i + 1]; + kt[2 * i + 1] = x; +#endif +#ifdef _UFC_64_ + long64 x; + x = kt[15-i]; + kt[15-i] = kt[i]; + kt[i] = x; +#endif + } + __data->direction = __edflag; + } + + /* + * Do initial permutation + E expansion + */ + i = 0; + for(l1 = 0; i < 24; i++) { + if(__block[initial_perm[esel[i]-1]-1]) + l1 |= BITMASK[i]; + } + for(l2 = 0; i < 48; i++) { + if(__block[initial_perm[esel[i]-1]-1]) + l2 |= BITMASK[i-24]; + } + + i = 0; + for(r1 = 0; i < 24; i++) { + if(__block[initial_perm[esel[i]-1+32]-1]) + r1 |= BITMASK[i]; + } + for(r2 = 0; i < 48; i++) { + if(__block[initial_perm[esel[i]-1+32]-1]) + r2 |= BITMASK[i-24]; + } + + /* + * Do DES inner loops + final conversion + */ + res[0] = l1; res[1] = l2; + res[2] = r1; res[3] = r2; + _ufc_doit_r((ufc_long)1, __data, &res[0]); + + /* + * Do final permutations + */ + _ufc_dofinalperm_r(res, __data); + + /* + * And convert to bit array + */ + l1 = res[0]; r1 = res[1]; + for(i = 0; i < 32; i++) { + *__block++ = (l1 & longmask[i]) != 0; + } + for(i = 0; i < 32; i++) { + *__block++ = (r1 & longmask[i]) != 0; + } +} +weak_alias (__encrypt_r, encrypt_r) + +void +encrypt(__block, __edflag) + char *__block; + int __edflag; +{ + __encrypt_r(__block, __edflag, &_ufc_foobar); +} + + +/* + * UNIX setkey function. Take a 64 bit DES + * key and setup the machinery. + */ + +void +__setkey_r(__key, __data) + __const char *__key; + struct crypt_data * __restrict __data; +{ + int i,j; + unsigned char c; + unsigned char ktab[8]; + + _ufc_setup_salt_r("..", __data); /* be sure we're initialized */ + + for(i = 0; i < 8; i++) { + for(j = 0, c = 0; j < 8; j++) + c = c << 1 | *__key++; + ktab[i] = c >> 1; + } + _ufc_mk_keytab_r(ktab, __data); +} +weak_alias (__setkey_r, setkey_r) + +void +setkey(__key) + __const char *__key; +{ + __setkey_r(__key, &_ufc_foobar); +} diff --git a/crypt/md5-crypt.c b/crypt/md5-crypt.c new file mode 100644 index 0000000..1261035 --- /dev/null +++ b/crypt/md5-crypt.c @@ -0,0 +1,223 @@ +/* One way encryption based on MD5 sum. + Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 1996. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include +#include +#include +#include + +#include "md5.h" + + +/* Define our magic string to mark salt for MD5 "encryption" + replacement. This is meant to be the same as for other MD5 based + encryption implementations. */ +static const char md5_salt_prefix[] = "$1$"; + +/* Table with characters for base64 transformation. */ +static const char b64t[64] = +"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + + +/* Prototypes for local functions. */ +extern char *__md5_crypt_r __P ((const char *key, const char *salt, + char *buffer, int buflen)); +extern char *__md5_crypt __P ((const char *key, const char *salt)); + + +/* This entry point is equivalent to the `crypt' function in Unix + libcs. */ +char * +__md5_crypt_r (key, salt, buffer, buflen) + const char *key; + const char *salt; + char *buffer; + int buflen; +{ + unsigned char alt_result[16]; + struct md5_ctx ctx; + struct md5_ctx alt_ctx; + size_t salt_len; + size_t key_len; + size_t cnt; + char *cp; + + /* Find beginning of salt string. The prefix should normally always + be present. Just in case it is not. */ + if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0) + /* Skip salt prefix. */ + salt += sizeof (md5_salt_prefix) - 1; + + salt_len = MIN (strcspn (salt, "$"), 8); + key_len = strlen (key); + + /* Prepare for the real work. */ + __md5_init_ctx (&ctx); + + /* Add the key string. */ + __md5_process_bytes (key, key_len, &ctx); + + /* Because the SALT argument need not always have the salt prefix we + add it separately. */ + __md5_process_bytes (md5_salt_prefix, sizeof (md5_salt_prefix) - 1, &ctx); + + /* The last part is the salt string. This must be at most 8 + characters and it ends at the first `$' character (for + compatibility which existing solutions). */ + __md5_process_bytes (salt, salt_len, &ctx); + + + /* Compute alternate MD5 sum with input KEY, SALT, and KEY. The + final result will be added to the first context. */ + __md5_init_ctx (&alt_ctx); + + /* Add key. */ + __md5_process_bytes (key, key_len, &alt_ctx); + + /* Add salt. */ + __md5_process_bytes (salt, salt_len, &alt_ctx); + + /* Add key again. */ + __md5_process_bytes (key, key_len, &alt_ctx); + + /* Now get result of this (16 bytes) and add it to the other + context. */ + __md5_finish_ctx (&alt_ctx, alt_result); + + /* Add for any character in the key one byte of the alternate sum. */ + for (cnt = key_len; cnt > 16; cnt -= 16) + __md5_process_bytes (alt_result, 16, &ctx); + __md5_process_bytes (alt_result, cnt, &ctx); + + /* For the following code we need a NUL byte. */ + *alt_result = '\0'; + + /* The original implementation now does something weird: for every 1 + bit in the key the first 0 is added to the buffer, for every 0 + bit the first character of the key. This does not seem to be + what was intended but we have to follow this to be compatible. */ + for (cnt = key_len; cnt > 0; cnt >>= 1) + __md5_process_bytes ((cnt & 1) != 0 ? (const char *) alt_result : key, 1, + &ctx); + + /* Create intermediate result. */ + __md5_finish_ctx (&ctx, alt_result); + + /* Now comes another weirdness. In fear of password crackers here + comes a quite long loop which just processes the output of the + previous round again. We cannot ignore this here. */ + for (cnt = 0; cnt < 1000; ++cnt) + { + /* New context. */ + __md5_init_ctx (&ctx); + + /* Add key or last result. */ + if ((cnt & 1) != 0) + __md5_process_bytes (key, key_len, &ctx); + else + __md5_process_bytes (alt_result, 16, &ctx); + + /* Add salt for numbers not divisible by 3. */ + if (cnt % 3 != 0) + __md5_process_bytes (salt, salt_len, &ctx); + + /* Add key for numbers not divisible by 7. */ + if (cnt % 7 != 0) + __md5_process_bytes (key, key_len, &ctx); + + /* Add key or last result. */ + if ((cnt & 1) != 0) + __md5_process_bytes (alt_result, 16, &ctx); + else + __md5_process_bytes (key, key_len, &ctx); + + /* Create intermediate result. */ + __md5_finish_ctx (&ctx, alt_result); + } + + /* Now we can construct the result string. It consists of three + parts. */ + cp = __stpncpy (buffer, md5_salt_prefix, MAX (0, buflen)); + buflen -= sizeof (md5_salt_prefix); + + cp = __stpncpy (cp, salt, MIN ((size_t) buflen, salt_len)); + buflen -= MIN ((size_t) buflen, salt_len); + + if (buflen > 0) + { + *cp++ = '$'; + --buflen; + } + +#define b64_from_24bit(B2, B1, B0, N) \ + do { \ + unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \ + int n = (N); \ + while (n-- > 0 && buflen > 0) \ + { \ + *cp++ = b64t[w & 0x3f]; \ + --buflen; \ + w >>= 6; \ + } \ + } while (0) + + + b64_from_24bit (alt_result[0], alt_result[6], alt_result[12], 4); + b64_from_24bit (alt_result[1], alt_result[7], alt_result[13], 4); + b64_from_24bit (alt_result[2], alt_result[8], alt_result[14], 4); + b64_from_24bit (alt_result[3], alt_result[9], alt_result[15], 4); + b64_from_24bit (alt_result[4], alt_result[10], alt_result[5], 4); + b64_from_24bit (0, 0, alt_result[11], 2); + if (buflen <= 0) + { + __set_errno (ERANGE); + buffer = NULL; + } + else + *cp = '\0'; /* Terminate the string. */ + + /* Clear the buffer for the intermediate result so that people + attaching to processes or reading core dumps cannot get any + information. */ + memset (alt_result, '\0', sizeof (alt_result)); + + return buffer; +} + + +char * +__md5_crypt (const char *key, const char *salt) +{ + /* We don't want to have an arbitrary limit in the size of the + password. We can compute the size of the result in advance and + so we can prepare the buffer we pass to `md5_crypt_r'. */ + static char *buffer = NULL; + static int buflen = 0; + int needed = 3 + strlen (salt) + 1 + 26 + 1; + + if (buflen < needed) + { + buflen = needed; + if ((buffer = realloc (buffer, buflen)) == NULL) + return NULL; + } + + return __md5_crypt_r (key, salt, buffer, buflen); +} diff --git a/crypt/md5.c b/crypt/md5.c new file mode 100644 index 0000000..478429f --- /dev/null +++ b/crypt/md5.c @@ -0,0 +1,428 @@ +/* md5.c - Functions to compute MD5 message digest of files or memory blocks + according to the definition of MD5 in RFC 1321 from April 1992. + Copyright (C) 1995, 1996, 1997, 1999 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +/* Written by Ulrich Drepper , 1995. */ + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include + +#if STDC_HEADERS || defined _LIBC +# include +# include +#else +# ifndef HAVE_MEMCPY +# define memcpy(d, s, n) bcopy ((s), (d), (n)) +# endif +#endif + +#include "md5.h" + +#ifdef _LIBC +# include +# if __BYTE_ORDER == __BIG_ENDIAN +# define WORDS_BIGENDIAN 1 +# endif +/* We need to keep the namespace clean so define the MD5 function + protected using leading __ . */ +# define md5_init_ctx __md5_init_ctx +# define md5_process_block __md5_process_block +# define md5_process_bytes __md5_process_bytes +# define md5_finish_ctx __md5_finish_ctx +# define md5_read_ctx __md5_read_ctx +# define md5_stream __md5_stream +# define md5_buffer __md5_buffer +#endif + +#ifdef WORDS_BIGENDIAN +# define SWAP(n) \ + (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) +#else +# define SWAP(n) (n) +#endif + + +/* This array contains the bytes used to pad the buffer to the next + 64-byte boundary. (RFC 1321, 3.1: Step 1) */ +static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; + + +/* Initialize structure containing state of computation. + (RFC 1321, 3.3: Step 3) */ +void +md5_init_ctx (ctx) + struct md5_ctx *ctx; +{ + ctx->A = 0x67452301; + ctx->B = 0xefcdab89; + ctx->C = 0x98badcfe; + ctx->D = 0x10325476; + + ctx->total[0] = ctx->total[1] = 0; + ctx->buflen = 0; +} + +/* Put result from CTX in first 16 bytes following RESBUF. The result + must be in little endian byte order. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +void * +md5_read_ctx (ctx, resbuf) + const struct md5_ctx *ctx; + void *resbuf; +{ + ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A); + ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B); + ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C); + ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D); + + return resbuf; +} + +/* Process the remaining bytes in the internal buffer and the usual + prolog according to the standard and write the result to RESBUF. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +void * +md5_finish_ctx (ctx, resbuf) + struct md5_ctx *ctx; + void *resbuf; +{ + /* Take yet unprocessed bytes into account. */ + md5_uint32 bytes = ctx->buflen; + size_t pad; + + /* Now count remaining bytes. */ + ctx->total[0] += bytes; + if (ctx->total[0] < bytes) + ++ctx->total[1]; + + pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; + memcpy (&ctx->buffer[bytes], fillbuf, pad); + + /* Put the 64-bit file length in *bits* at the end of the buffer. */ + *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); + *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | + (ctx->total[0] >> 29)); + + /* Process last bytes. */ + md5_process_block (ctx->buffer, bytes + pad + 8, ctx); + + return md5_read_ctx (ctx, resbuf); +} + +/* Compute MD5 message digest for bytes read from STREAM. The + resulting message digest number will be written into the 16 bytes + beginning at RESBLOCK. */ +int +md5_stream (stream, resblock) + FILE *stream; + void *resblock; +{ + /* Important: BLOCKSIZE must be a multiple of 64. */ +#define BLOCKSIZE 4096 + struct md5_ctx ctx; + char buffer[BLOCKSIZE + 72]; + size_t sum; + + /* Initialize the computation context. */ + md5_init_ctx (&ctx); + + /* Iterate over full file contents. */ + while (1) + { + /* We read the file in blocks of BLOCKSIZE bytes. One call of the + computation function processes the whole buffer so that with the + next round of the loop another block can be read. */ + size_t n; + sum = 0; + + /* Read block. Take care for partial reads. */ + do + { + n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); + + sum += n; + } + while (sum < BLOCKSIZE && n != 0); + if (n == 0 && ferror (stream)) + return 1; + + /* If end of file is reached, end the loop. */ + if (n == 0) + break; + + /* Process buffer with BLOCKSIZE bytes. Note that + BLOCKSIZE % 64 == 0 + */ + md5_process_block (buffer, BLOCKSIZE, &ctx); + } + + /* Add the last bytes if necessary. */ + if (sum > 0) + md5_process_bytes (buffer, sum, &ctx); + + /* Construct result in desired memory. */ + md5_finish_ctx (&ctx, resblock); + return 0; +} + +/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The + result is always in little endian byte order, so that a byte-wise + output yields to the wanted ASCII representation of the message + digest. */ +void * +md5_buffer (buffer, len, resblock) + const char *buffer; + size_t len; + void *resblock; +{ + struct md5_ctx ctx; + + /* Initialize the computation context. */ + md5_init_ctx (&ctx); + + /* Process whole buffer but last len % 64 bytes. */ + md5_process_bytes (buffer, len, &ctx); + + /* Put result in desired memory area. */ + return md5_finish_ctx (&ctx, resblock); +} + + +void +md5_process_bytes (buffer, len, ctx) + const void *buffer; + size_t len; + struct md5_ctx *ctx; +{ + /* When we already have some bits in our internal buffer concatenate + both inputs first. */ + if (ctx->buflen != 0) + { + size_t left_over = ctx->buflen; + size_t add = 128 - left_over > len ? len : 128 - left_over; + + memcpy (&ctx->buffer[left_over], buffer, add); + ctx->buflen += add; + + if (left_over + add > 64) + { + md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx); + /* The regions in the following copy operation cannot overlap. */ + memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], + (left_over + add) & 63); + ctx->buflen = (left_over + add) & 63; + } + + buffer = (const char *) buffer + add; + len -= add; + } + + /* Process available complete blocks. */ + if (len > 64) + { + md5_process_block (buffer, len & ~63, ctx); + buffer = (const char *) buffer + (len & ~63); + len &= 63; + } + + /* Move remaining bytes in internal buffer. */ + if (len > 0) + { + memcpy (ctx->buffer, buffer, len); + ctx->buflen = len; + } +} + + +/* These are the four functions used in the four steps of the MD5 algorithm + and defined in the RFC 1321. The first function is a little bit optimized + (as found in Colin Plumbs public domain implementation). */ +/* #define FF(b, c, d) ((b & c) | (~b & d)) */ +#define FF(b, c, d) (d ^ (b & (c ^ d))) +#define FG(b, c, d) FF (d, b, c) +#define FH(b, c, d) (b ^ c ^ d) +#define FI(b, c, d) (c ^ (b | ~d)) + +/* Process LEN bytes of BUFFER, accumulating context into CTX. + It is assumed that LEN % 64 == 0. */ + +void +md5_process_block (buffer, len, ctx) + const void *buffer; + size_t len; + struct md5_ctx *ctx; +{ + md5_uint32 correct_words[16]; + const md5_uint32 *words = buffer; + size_t nwords = len / sizeof (md5_uint32); + const md5_uint32 *endp = words + nwords; + md5_uint32 A = ctx->A; + md5_uint32 B = ctx->B; + md5_uint32 C = ctx->C; + md5_uint32 D = ctx->D; + + /* First increment the byte count. RFC 1321 specifies the possible + length of the file up to 2^64 bits. Here we only compute the + number of bytes. Do a double word increment. */ + ctx->total[0] += len; + if (ctx->total[0] < len) + ++ctx->total[1]; + + /* Process all bytes in the buffer with 64 bytes in each round of + the loop. */ + while (words < endp) + { + md5_uint32 *cwp = correct_words; + md5_uint32 A_save = A; + md5_uint32 B_save = B; + md5_uint32 C_save = C; + md5_uint32 D_save = D; + + /* First round: using the given function, the context and a constant + the next context is computed. Because the algorithms processing + unit is a 32-bit word and it is determined to work on words in + little endian byte order we perhaps have to change the byte order + before the computation. To reduce the work for the next steps + we store the swapped words in the array CORRECT_WORDS. */ + +#define OP(a, b, c, d, s, T) \ + do \ + { \ + a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \ + ++words; \ + CYCLIC (a, s); \ + a += b; \ + } \ + while (0) + + /* It is unfortunate that C does not provide an operator for + cyclic rotation. Hope the C compiler is smart enough. */ +#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) + + /* Before we start, one word to the strange constants. + They are defined in RFC 1321 as + + T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 + */ + + /* Round 1. */ + OP (A, B, C, D, 7, 0xd76aa478); + OP (D, A, B, C, 12, 0xe8c7b756); + OP (C, D, A, B, 17, 0x242070db); + OP (B, C, D, A, 22, 0xc1bdceee); + OP (A, B, C, D, 7, 0xf57c0faf); + OP (D, A, B, C, 12, 0x4787c62a); + OP (C, D, A, B, 17, 0xa8304613); + OP (B, C, D, A, 22, 0xfd469501); + OP (A, B, C, D, 7, 0x698098d8); + OP (D, A, B, C, 12, 0x8b44f7af); + OP (C, D, A, B, 17, 0xffff5bb1); + OP (B, C, D, A, 22, 0x895cd7be); + OP (A, B, C, D, 7, 0x6b901122); + OP (D, A, B, C, 12, 0xfd987193); + OP (C, D, A, B, 17, 0xa679438e); + OP (B, C, D, A, 22, 0x49b40821); + + /* For the second to fourth round we have the possibly swapped words + in CORRECT_WORDS. Redefine the macro to take an additional first + argument specifying the function to use. */ +#undef OP +#define OP(f, a, b, c, d, k, s, T) \ + do \ + { \ + a += f (b, c, d) + correct_words[k] + T; \ + CYCLIC (a, s); \ + a += b; \ + } \ + while (0) + + /* Round 2. */ + OP (FG, A, B, C, D, 1, 5, 0xf61e2562); + OP (FG, D, A, B, C, 6, 9, 0xc040b340); + OP (FG, C, D, A, B, 11, 14, 0x265e5a51); + OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); + OP (FG, A, B, C, D, 5, 5, 0xd62f105d); + OP (FG, D, A, B, C, 10, 9, 0x02441453); + OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); + OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); + OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); + OP (FG, D, A, B, C, 14, 9, 0xc33707d6); + OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); + OP (FG, B, C, D, A, 8, 20, 0x455a14ed); + OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); + OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); + OP (FG, C, D, A, B, 7, 14, 0x676f02d9); + OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); + + /* Round 3. */ + OP (FH, A, B, C, D, 5, 4, 0xfffa3942); + OP (FH, D, A, B, C, 8, 11, 0x8771f681); + OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); + OP (FH, B, C, D, A, 14, 23, 0xfde5380c); + OP (FH, A, B, C, D, 1, 4, 0xa4beea44); + OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); + OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); + OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); + OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); + OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); + OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); + OP (FH, B, C, D, A, 6, 23, 0x04881d05); + OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); + OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); + OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); + OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); + + /* Round 4. */ + OP (FI, A, B, C, D, 0, 6, 0xf4292244); + OP (FI, D, A, B, C, 7, 10, 0x432aff97); + OP (FI, C, D, A, B, 14, 15, 0xab9423a7); + OP (FI, B, C, D, A, 5, 21, 0xfc93a039); + OP (FI, A, B, C, D, 12, 6, 0x655b59c3); + OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); + OP (FI, C, D, A, B, 10, 15, 0xffeff47d); + OP (FI, B, C, D, A, 1, 21, 0x85845dd1); + OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); + OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); + OP (FI, C, D, A, B, 6, 15, 0xa3014314); + OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); + OP (FI, A, B, C, D, 4, 6, 0xf7537e82); + OP (FI, D, A, B, C, 11, 10, 0xbd3af235); + OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); + OP (FI, B, C, D, A, 9, 21, 0xeb86d391); + + /* Add the starting values of the context. */ + A += A_save; + B += B_save; + C += C_save; + D += D_save; + } + + /* Put checksum in context given as argument. */ + ctx->A = A; + ctx->B = B; + ctx->C = C; + ctx->D = D; +} diff --git a/crypt/md5.h b/crypt/md5.h new file mode 100644 index 0000000..fbc2bdbf --- /dev/null +++ b/crypt/md5.h @@ -0,0 +1,147 @@ +/* Declaration of functions and data types used for MD5 sum computing + library functions. + Copyright (C) 1995, 1996, 1997, 1999 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#ifndef _MD5_H +#define _MD5_H 1 + +#include + +#if defined HAVE_LIMITS_H || _LIBC +# include +#endif + +/* The following contortions are an attempt to use the C preprocessor + to determine an unsigned integral type that is 32 bits wide. An + alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but + doing that would require that the configure script compile and *run* + the resulting executable. Locally running cross-compiled executables + is usually not possible. */ + +#ifdef _LIBC +# include +typedef u_int32_t md5_uint32; +#else +# if defined __STDC__ && __STDC__ +# define UINT_MAX_32_BITS 4294967295U +# else +# define UINT_MAX_32_BITS 0xFFFFFFFF +# endif + +/* If UINT_MAX isn't defined, assume it's a 32-bit type. + This should be valid for all systems GNU cares about because + that doesn't include 16-bit systems, and only modern systems + (that certainly have ) have 64+-bit integral types. */ + +# ifndef UINT_MAX +# define UINT_MAX UINT_MAX_32_BITS +# endif + +# if UINT_MAX == UINT_MAX_32_BITS + typedef unsigned int md5_uint32; +# else +# if USHRT_MAX == UINT_MAX_32_BITS + typedef unsigned short md5_uint32; +# else +# if ULONG_MAX == UINT_MAX_32_BITS + typedef unsigned long md5_uint32; +# else + /* The following line is intended to evoke an error. + Using #error is not portable enough. */ + "Cannot determine unsigned 32-bit data type." +# endif +# endif +# endif +#endif + +#undef __P +#if defined (__STDC__) && __STDC__ +# define __P(x) x +#else +# define __P(x) () +#endif + +/* Structure to save state of computation between the single steps. */ +struct md5_ctx +{ + md5_uint32 A; + md5_uint32 B; + md5_uint32 C; + md5_uint32 D; + + md5_uint32 total[2]; + md5_uint32 buflen; + char buffer[128]; +}; + +/* + * The following three functions are build up the low level used in + * the functions `md5_stream' and `md5_buffer'. + */ + +/* Initialize structure containing state of computation. + (RFC 1321, 3.3: Step 3) */ +extern void __md5_init_ctx __P ((struct md5_ctx *ctx)); + +/* Starting with the result of former calls of this function (or the + initialization function update the context for the next LEN bytes + starting at BUFFER. + It is necessary that LEN is a multiple of 64!!! */ +extern void __md5_process_block __P ((const void *buffer, size_t len, + struct md5_ctx *ctx)); + +/* Starting with the result of former calls of this function (or the + initialization function update the context for the next LEN bytes + starting at BUFFER. + It is NOT required that LEN is a multiple of 64. */ +extern void __md5_process_bytes __P ((const void *buffer, size_t len, + struct md5_ctx *ctx)); + +/* Process the remaining bytes in the buffer and put result from CTX + in first 16 bytes following RESBUF. The result is always in little + endian byte order, so that a byte-wise output yields to the wanted + ASCII representation of the message digest. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +extern void *__md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf)); + + +/* Put result from CTX in first 16 bytes following RESBUF. The result is + always in little endian byte order, so that a byte-wise output yields + to the wanted ASCII representation of the message digest. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +extern void *__md5_read_ctx __P ((const struct md5_ctx *ctx, void *resbuf)); + + +/* Compute MD5 message digest for bytes read from STREAM. The + resulting message digest number will be written into the 16 bytes + beginning at RESBLOCK. */ +extern int __md5_stream __P ((FILE *stream, void *resblock)); + +/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The + result is always in little endian byte order, so that a byte-wise + output yields to the wanted ASCII representation of the message + digest. */ +extern void *__md5_buffer __P ((const char *buffer, size_t len, + void *resblock)); + +#endif /* md5.h */ diff --git a/crypt/md5c-test.c b/crypt/md5c-test.c new file mode 100644 index 0000000..f56d0eb --- /dev/null +++ b/crypt/md5c-test.c @@ -0,0 +1,15 @@ +#include +#include + +int +main (int argc, char *argv[]) +{ + const char salt[] = "$1$saltstring"; + char *cp; + int result = 0; + + cp = crypt ("Hello world!", salt); + result |= strcmp ("$1$saltstri$YMyguxXMBpd2TEZ.vS/3q1", cp); + + return result; +} diff --git a/crypt/md5test.c b/crypt/md5test.c new file mode 100644 index 0000000..cfd9605 --- /dev/null +++ b/crypt/md5test.c @@ -0,0 +1,45 @@ +#include +#include "md5.h" + +static const struct +{ + const char *input; + const char result[16]; +} tests[] = + { + { "", + "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e" }, + { "a", + "\x0c\xc1\x75\xb9\xc0\xf1\xb6\xa8\x31\xc3\x99\xe2\x69\x77\x26\x61" }, + { "abc", + "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f\x72" }, + { "message digest", + "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0" }, + { "abcdefghijklmnopqrstuvwxyz", + "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1\x3b" }, + { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", + "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d\x9f" }, + { "123456789012345678901234567890123456789012345678901234567890" + "12345678901234567890", + "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6\x7a" } + }; + + +int +main (int argc, char *argv[]) +{ + struct md5_ctx ctx; + char sum[16]; + int result = 0; + int cnt; + + for (cnt = 0; cnt < (int) (sizeof (tests) / sizeof (tests[0])); ++cnt) + { + __md5_init_ctx (&ctx); + __md5_process_bytes (tests[cnt].input, strlen (tests[cnt].input), &ctx); + __md5_finish_ctx (&ctx, sum); + result |= memcmp (tests[cnt].result, sum, 16); + } + + return result; +} diff --git a/crypt/sysdeps/mach/hurd/Implies b/crypt/sysdeps/mach/hurd/Implies deleted file mode 100644 index 03a7725..0000000 --- a/crypt/sysdeps/mach/hurd/Implies +++ /dev/null @@ -1,2 +0,0 @@ -# The Hurd uses Unix like DES crypto function. -unix diff --git a/crypt/sysdeps/unix/Makefile b/crypt/sysdeps/unix/Makefile deleted file mode 100644 index 6581048..0000000 --- a/crypt/sysdeps/unix/Makefile +++ /dev/null @@ -1,4 +0,0 @@ -ifeq ($(subdir),md5-crypt) -libcrypt-routines += crypt crypt_util -dont_distribute += crypt.c crypt_util.c -endif diff --git a/crypt/sysdeps/unix/crypt-entry.c b/crypt/sysdeps/unix/crypt-entry.c deleted file mode 100644 index 9d99c53..0000000 --- a/crypt/sysdeps/unix/crypt-entry.c +++ /dev/null @@ -1,149 +0,0 @@ -/* - * UFC-crypt: ultra fast crypt(3) implementation - * - * Copyright (C) 1991, 1992, 1993, 1996, 1997 Free Software Foundation, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; see the file COPYING.LIB. If not, - * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - * - * crypt entry points - * - * @(#)crypt-entry.c 1.2 12/20/96 - * - */ - -#ifdef DEBUG -#include -#endif -#include - -#ifndef STATIC -#define STATIC static -#endif - -#ifndef DOS -#include "ufc-crypt.h" -#else -/* - * Thanks to greg%wind@plains.NoDak.edu (Greg W. Wettstein) - * for DOS patches - */ -#include "ufc.h" -#endif -#include "crypt.h" -#include "crypt-private.h" - -/* Prototypes for local functions. */ -#if __STDC__ - 0 -#ifndef __GNU_LIBRARY__ -void _ufc_clearmem (char *start, int cnt); -#else -#define _ufc_clearmem(start, cnt) memset(start, 0, cnt) -#endif -extern char *__md5_crypt_r (const char *key, const char *salt, char *buffer, - int buflen); -extern char *__md5_crypt (const char *key, const char *salt); -#endif - -/* Define our magic string to mark salt for MD5 encryption - replacement. This is meant to be the same as for other MD5 based - encryption implementations. */ -static const char md5_salt_prefix[] = "$1$"; - -/* For use by the old, non-reentrant routines (crypt/encrypt/setkey) */ -extern struct crypt_data _ufc_foobar; - -/* - * UNIX crypt function - */ - -char * -__crypt_r (key, salt, data) - const char *key; - const char *salt; - struct crypt_data * __restrict data; -{ - ufc_long res[4]; - char ktab[9]; - ufc_long xx = 25; /* to cope with GCC long long compiler bugs */ - -#ifdef _LIBC - /* Try to find out whether we have to use MD5 encryption replacement. */ - if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0) - return __md5_crypt_r (key, salt, (char *) data, - sizeof (struct crypt_data)); -#endif - - /* - * Hack DES tables according to salt - */ - _ufc_setup_salt_r (salt, data); - - /* - * Setup key schedule - */ - _ufc_clearmem (ktab, (int) sizeof (ktab)); - (void) strncpy (ktab, key, 8); - _ufc_mk_keytab_r (ktab, data); - - /* - * Go for the 25 DES encryptions - */ - _ufc_clearmem ((char*) res, (int) sizeof (res)); - _ufc_doit_r (xx, data, &res[0]); - - /* - * Do final permutations - */ - _ufc_dofinalperm_r (res, data); - - /* - * And convert back to 6 bit ASCII - */ - _ufc_output_conversion_r (res[0], res[1], salt, data); - return data->crypt_3_buf; -} -weak_alias (__crypt_r, crypt_r) - -char * -crypt (key, salt) - const char *key; - const char *salt; -{ -#ifdef _LIBC - /* Try to find out whether we have to use MD5 encryption replacement. */ - if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0) - return __md5_crypt (key, salt); -#endif - - return __crypt_r (key, salt, &_ufc_foobar); -} - - -/* - * To make fcrypt users happy. - * They don't need to call init_des. - */ -#ifdef _LIBC -weak_alias (crypt, fcrypt) -#else -char * -__fcrypt (key, salt) - const char *key; - const char *salt; -{ - return crypt (key, salt); -} -#endif diff --git a/crypt/sysdeps/unix/crypt-private.h b/crypt/sysdeps/unix/crypt-private.h deleted file mode 100644 index 130cccd..0000000 --- a/crypt/sysdeps/unix/crypt-private.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * UFC-crypt: ultra fast crypt(3) implementation - * - * Copyright (C) 1991, 92, 93, 96, 97, 98 Free Software Foundation, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; see the file COPYING.LIB. If not, - * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - * - * @(#)crypt-private.h 1.4 12/20/96 - */ - -/* Prototypes for local functions in libcrypt.a. */ - -#ifndef CRYPT_PRIVATE_H -#define CRYPT_PRIVATE_H 1 - -#include - -/* crypt.c */ -extern void _ufc_doit_r (ufc_long itr, struct crypt_data * __restrict __data, - ufc_long *res); - - -/* crypt_util.c */ -extern void __init_des_r (struct crypt_data * __restrict __data); -extern void __init_des (void); - -extern void _ufc_setup_salt_r (__const char *s, - struct crypt_data * __restrict __data); -extern void _ufc_mk_keytab_r (__const char *key, - struct crypt_data * __restrict __data); -extern void _ufc_dofinalperm_r (ufc_long *res, - struct crypt_data * __restrict __data); -extern void _ufc_output_conversion_r (ufc_long v1, ufc_long v2, - __const char *salt, - struct crypt_data * __restrict __data); - -extern void __setkey_r (__const char *__key, - struct crypt_data * __restrict __data); -extern void __encrypt_r (char * __restrict __block, int __edflag, - struct crypt_data * __restrict __data); - -/* crypt-entry.c */ -extern char *__crypt_r (__const char *__key, __const char *__salt, - struct crypt_data * __restrict __data); -extern char *fcrypt (__const char *key, __const char *salt); - -#endif /* crypt-private.h */ diff --git a/crypt/sysdeps/unix/crypt.c b/crypt/sysdeps/unix/crypt.c deleted file mode 100644 index 8e2a576..0000000 --- a/crypt/sysdeps/unix/crypt.c +++ /dev/null @@ -1,120 +0,0 @@ -/* - * UFC-crypt: ultra fast crypt(3) implementation - * - * Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; see the file COPYING.LIB. If not, - * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - * - * @(#)crypt.c 2.25 12/20/96 - * - * Semiportable C version - * - */ - -#include "ufc-crypt.h" -#include "crypt.h" -#include "crypt-private.h" - -#ifdef _UFC_32_ - -/* - * 32 bit version - */ - -#define SBA(sb, v) (*(long32*)((char*)(sb)+(v))) - -void -_ufc_doit_r(itr, __data, res) - ufc_long itr, *res; - struct crypt_data * __restrict __data; -{ - int i; - long32 s, *k; - long32 *sb01 = (long32*)__data->sb0; - long32 *sb23 = (long32*)__data->sb2; - long32 l1, l2, r1, r2; - - l1 = (long32)res[0]; l2 = (long32)res[1]; - r1 = (long32)res[2]; r2 = (long32)res[3]; - - while(itr--) { - k = (long32*)__data->keysched; - for(i=8; i--; ) { - s = *k++ ^ r1; - l1 ^= SBA(sb01, s & 0xffff); l2 ^= SBA(sb01, (s & 0xffff)+4); - l1 ^= SBA(sb01, s >>= 16 ); l2 ^= SBA(sb01, (s )+4); - s = *k++ ^ r2; - l1 ^= SBA(sb23, s & 0xffff); l2 ^= SBA(sb23, (s & 0xffff)+4); - l1 ^= SBA(sb23, s >>= 16 ); l2 ^= SBA(sb23, (s )+4); - - s = *k++ ^ l1; - r1 ^= SBA(sb01, s & 0xffff); r2 ^= SBA(sb01, (s & 0xffff)+4); - r1 ^= SBA(sb01, s >>= 16 ); r2 ^= SBA(sb01, (s )+4); - s = *k++ ^ l2; - r1 ^= SBA(sb23, s & 0xffff); r2 ^= SBA(sb23, (s & 0xffff)+4); - r1 ^= SBA(sb23, s >>= 16 ); r2 ^= SBA(sb23, (s )+4); - } - s=l1; l1=r1; r1=s; s=l2; l2=r2; r2=s; - } - res[0] = l1; res[1] = l2; res[2] = r1; res[3] = r2; -} - -#endif - -#ifdef _UFC_64_ - -/* - * 64 bit version - */ - -#define SBA(sb, v) (*(long64*)((char*)(sb)+(v))) - -void -_ufc_doit_r(itr, __data, res) - ufc_long itr, *res; - struct crypt_data * __restrict __data; -{ - int i; - long64 l, r, s, *k; - register long64 *sb01 = (long64*)__data->sb0; - register long64 *sb23 = (long64*)__data->sb2; - - l = (((long64)res[0]) << 32) | ((long64)res[1]); - r = (((long64)res[2]) << 32) | ((long64)res[3]); - - while(itr--) { - k = (long64*)__data->keysched; - for(i=8; i--; ) { - s = *k++ ^ r; - l ^= SBA(sb23, (s ) & 0xffff); - l ^= SBA(sb23, (s >>= 16) & 0xffff); - l ^= SBA(sb01, (s >>= 16) & 0xffff); - l ^= SBA(sb01, (s >>= 16) ); - - s = *k++ ^ l; - r ^= SBA(sb23, (s ) & 0xffff); - r ^= SBA(sb23, (s >>= 16) & 0xffff); - r ^= SBA(sb01, (s >>= 16) & 0xffff); - r ^= SBA(sb01, (s >>= 16) ); - } - s=l; l=r; r=s; - } - - res[0] = l >> 32; res[1] = l & 0xffffffff; - res[2] = r >> 32; res[3] = r & 0xffffffff; -} - -#endif diff --git a/crypt/sysdeps/unix/crypt.h b/crypt/sysdeps/unix/crypt.h deleted file mode 100644 index 7018cb1..0000000 --- a/crypt/sysdeps/unix/crypt.h +++ /dev/null @@ -1,71 +0,0 @@ -/* - * UFC-crypt: ultra fast crypt(3) implementation - * - * Copyright (C) 1991, 92, 93, 96, 97, 98, 2000 Free Software Foundation, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with the GNU C Library; see the file COPYING.LIB. If not, - * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - * - * @(#)crypt.h 1.5 12/20/96 - * - */ - -#ifndef _CRYPT_H -#define _CRYPT_H 1 - -#include - -__BEGIN_DECLS - -/* Encrypt at most 8 characters from KEY using salt to perturb DES. */ -extern char *crypt (__const char *__key, __const char *__salt); - -/* Setup DES tables according KEY. */ -extern void setkey (__const char *__key); - -/* Encrypt data in BLOCK in place if EDFLAG is zero; otherwise decrypt - block in place. */ -extern void encrypt (char *__block, int __edflag); - -#ifdef __USE_GNU -/* Reentrant versions of the functions above. The additional argument - points to a structure where the results are placed in. */ -struct crypt_data - { - char keysched[16 * 8]; - char sb0[32768]; - char sb1[32768]; - char sb2[32768]; - char sb3[32768]; - /* end-of-aligment-critical-data */ - char crypt_3_buf[14]; - char current_salt[2]; - long int current_saltbits; - int direction, initialized; - }; - -extern char *crypt_r (__const char *__key, __const char *__salt, - struct crypt_data * __restrict __data); - -extern void setkey_r (__const char *__key, - struct crypt_data * __restrict __data); - -extern void encrypt_r (char *__block, int __edflag, - struct crypt_data * __restrict __data); -#endif - -__END_DECLS - -#endif /* crypt.h */ diff --git a/crypt/sysdeps/unix/crypt_util.c b/crypt/sysdeps/unix/crypt_util.c deleted file mode 100644 index 671571c..0000000 --- a/crypt/sysdeps/unix/crypt_util.c +++ /dev/null @@ -1,914 +0,0 @@ -/* - * UFC-crypt: ultra fast crypt(3) implementation - * - * Copyright (C) 1991, 92, 93, 96, 97, 98 Free Software Foundation, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; see the file COPYING.LIB. If not, - * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - * - * @(#)crypt_util.c 2.56 12/20/96 - * - * Support routines - * - */ - -#ifdef DEBUG -#include -#endif -#include - -#ifndef STATIC -#define STATIC static -#endif - -#ifndef DOS -#include "patchlevel.h" -#include "ufc-crypt.h" -#else -/* - * Thanks to greg%wind@plains.NoDak.edu (Greg W. Wettstein) - * for DOS patches - */ -#include "pl.h" -#include "ufc.h" -#endif -#include "crypt.h" -#include "crypt-private.h" - -/* Prototypes for local functions. */ -#if __STDC__ - 0 -#ifndef __GNU_LIBRARY__ -void _ufc_clearmem (char *start, int cnt); -void _ufc_copymem (char *from, char *to, int cnt); -#endif -#ifdef _UFC_32_ -STATIC void shuffle_sb (long32 *k, ufc_long saltbits); -#else -STATIC void shuffle_sb (long64 *k, ufc_long saltbits); -#endif -#endif - - -static const char patchlevel_str[] = PATCHLEVEL; - -/* - * Permutation done once on the 56 bit - * key derived from the original 8 byte ASCII key. - */ -static const int pc1[56] = { - 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, - 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, - 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, - 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 -}; - -/* - * How much to rotate each 28 bit half of the pc1 permutated - * 56 bit key before using pc2 to give the i' key - */ -static const int rots[16] = { - 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 -}; - -/* - * Permutation giving the key - * of the i' DES round - */ -static const int pc2[48] = { - 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, - 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, - 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, - 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 -}; - -/* - * The E expansion table which selects - * bits from the 32 bit intermediate result. - */ -static const int esel[48] = { - 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, - 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, - 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, - 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 -}; - -/* - * Permutation done on the - * result of sbox lookups - */ -static const int perm32[32] = { - 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, - 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 -}; - -/* - * The sboxes - */ -static const int sbox[8][4][16]= { - { { 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 }, - { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 }, - { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 }, - { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } - }, - - { { 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 }, - { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 }, - { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 }, - { 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 } - }, - - { { 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 }, - { 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 }, - { 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 }, - { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } - }, - - { { 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 }, - { 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 }, - { 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 }, - { 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } - }, - - { { 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 }, - { 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 }, - { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 }, - { 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 } - }, - - { { 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 }, - { 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 }, - { 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 }, - { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } - }, - - { { 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 }, - { 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 }, - { 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 }, - { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } - }, - - { { 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 }, - { 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 }, - { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 }, - { 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } - } -}; - -/* - * This is the initial - * permutation matrix - */ -static const int initial_perm[64] = { - 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, - 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, - 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, - 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 -}; - -/* - * This is the final - * permutation matrix - */ -static const int final_perm[64] = { - 40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, - 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, - 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, - 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 -}; - -#define ascii_to_bin(c) ((c)>='a'?(c-59):(c)>='A'?((c)-53):(c)-'.') -#define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.') - -static const ufc_long BITMASK[24] = { - 0x40000000, 0x20000000, 0x10000000, 0x08000000, 0x04000000, 0x02000000, - 0x01000000, 0x00800000, 0x00400000, 0x00200000, 0x00100000, 0x00080000, - 0x00004000, 0x00002000, 0x00001000, 0x00000800, 0x00000400, 0x00000200, - 0x00000100, 0x00000080, 0x00000040, 0x00000020, 0x00000010, 0x00000008 -}; - -static const unsigned char bytemask[8] = { - 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 -}; - -static const ufc_long longmask[32] = { - 0x80000000, 0x40000000, 0x20000000, 0x10000000, - 0x08000000, 0x04000000, 0x02000000, 0x01000000, - 0x00800000, 0x00400000, 0x00200000, 0x00100000, - 0x00080000, 0x00040000, 0x00020000, 0x00010000, - 0x00008000, 0x00004000, 0x00002000, 0x00001000, - 0x00000800, 0x00000400, 0x00000200, 0x00000100, - 0x00000080, 0x00000040, 0x00000020, 0x00000010, - 0x00000008, 0x00000004, 0x00000002, 0x00000001 -}; - -/* - * do_pc1: permform pc1 permutation in the key schedule generation. - * - * The first index is the byte number in the 8 byte ASCII key - * - second - - the two 28 bits halfs of the result - * - third - selects the 7 bits actually used of each byte - * - * The result is kept with 28 bit per 32 bit with the 4 most significant - * bits zero. - */ -static ufc_long do_pc1[8][2][128]; - -/* - * do_pc2: permform pc2 permutation in the key schedule generation. - * - * The first index is the septet number in the two 28 bit intermediate values - * - second - - - septet values - * - * Knowledge of the structure of the pc2 permutation is used. - * - * The result is kept with 28 bit per 32 bit with the 4 most significant - * bits zero. - */ -static ufc_long do_pc2[8][128]; - -/* - * eperm32tab: do 32 bit permutation and E selection - * - * The first index is the byte number in the 32 bit value to be permuted - * - second - is the value of this byte - * - third - selects the two 32 bit values - * - * The table is used and generated internally in init_des to speed it up - */ -static ufc_long eperm32tab[4][256][2]; - -/* - * efp: undo an extra e selection and do final - * permutation giving the DES result. - * - * Invoked 6 bit a time on two 48 bit values - * giving two 32 bit longs. - */ -static ufc_long efp[16][64][2]; - -/* - * For use by the old, non-reentrant routines - * (crypt/encrypt/setkey) - */ -struct crypt_data _ufc_foobar; - -#ifdef __GNU_LIBRARY__ -#include - -__libc_lock_define_initialized (static, _ufc_tables_lock) -#endif - -#ifdef DEBUG - -void -_ufc_prbits(a, n) - ufc_long *a; - int n; -{ - ufc_long i, j, t, tmp; - n /= 8; - for(i = 0; i < n; i++) { - tmp=0; - for(j = 0; j < 8; j++) { - t=8*i+j; - tmp|=(a[t/24] & BITMASK[t % 24])?bytemask[j]:0; - } - (void)printf("%02x ",tmp); - } - printf(" "); -} - -static void -_ufc_set_bits(v, b) - ufc_long v; - ufc_long *b; -{ - ufc_long i; - *b = 0; - for(i = 0; i < 24; i++) { - if(v & longmask[8 + i]) - *b |= BITMASK[i]; - } -} - -#endif - -#ifndef __GNU_LIBRARY__ -/* - * Silly rewrites of 'bzero'/'memset'. I do so - * because some machines don't have - * bzero and some don't have memset. - */ - -void -_ufc_clearmem(start, cnt) - char *start; - int cnt; -{ - while(cnt--) - *start++ = '\0'; -} - -void -_ufc_copymem(from, to, cnt) - char *from, *to; - int cnt; -{ - while(cnt--) - *to++ = *from++; -} -#else -#define _ufc_clearmem(start, cnt) memset(start, 0, cnt) -#define _ufc_copymem(from, to, cnt) memcpy(to, from, cnt) -#endif - -/* lookup a 6 bit value in sbox */ - -#define s_lookup(i,s) sbox[(i)][(((s)>>4) & 0x2)|((s) & 0x1)][((s)>>1) & 0xf]; - -/* - * Initialize unit - may be invoked directly - * by fcrypt users. - */ - -void -__init_des_r(__data) - struct crypt_data * __restrict __data; -{ - int comes_from_bit; - int bit, sg; - ufc_long j; - ufc_long mask1, mask2; - int e_inverse[64]; - static volatile int small_tables_initialized = 0; - -#ifdef _UFC_32_ - long32 *sb[4]; - sb[0] = (long32*)__data->sb0; sb[1] = (long32*)__data->sb1; - sb[2] = (long32*)__data->sb2; sb[3] = (long32*)__data->sb3; -#endif -#ifdef _UFC_64_ - long64 *sb[4]; - sb[0] = (long64*)__data->sb0; sb[1] = (long64*)__data->sb1; - sb[2] = (long64*)__data->sb2; sb[3] = (long64*)__data->sb3; -#endif - - if(small_tables_initialized == 0) { -#ifdef __GNU_LIBRARY__ - __libc_lock_lock (_ufc_tables_lock); - if(small_tables_initialized) - goto small_tables_done; -#endif - - /* - * Create the do_pc1 table used - * to affect pc1 permutation - * when generating keys - */ - _ufc_clearmem((char*)do_pc1, (int)sizeof(do_pc1)); - for(bit = 0; bit < 56; bit++) { - comes_from_bit = pc1[bit] - 1; - mask1 = bytemask[comes_from_bit % 8 + 1]; - mask2 = longmask[bit % 28 + 4]; - for(j = 0; j < 128; j++) { - if(j & mask1) - do_pc1[comes_from_bit / 8][bit / 28][j] |= mask2; - } - } - - /* - * Create the do_pc2 table used - * to affect pc2 permutation when - * generating keys - */ - _ufc_clearmem((char*)do_pc2, (int)sizeof(do_pc2)); - for(bit = 0; bit < 48; bit++) { - comes_from_bit = pc2[bit] - 1; - mask1 = bytemask[comes_from_bit % 7 + 1]; - mask2 = BITMASK[bit % 24]; - for(j = 0; j < 128; j++) { - if(j & mask1) - do_pc2[comes_from_bit / 7][j] |= mask2; - } - } - - /* - * Now generate the table used to do combined - * 32 bit permutation and e expansion - * - * We use it because we have to permute 16384 32 bit - * longs into 48 bit in order to initialize sb. - * - * Looping 48 rounds per permutation becomes - * just too slow... - * - */ - - _ufc_clearmem((char*)eperm32tab, (int)sizeof(eperm32tab)); - for(bit = 0; bit < 48; bit++) { - ufc_long mask1,comes_from; - comes_from = perm32[esel[bit]-1]-1; - mask1 = bytemask[comes_from % 8]; - for(j = 256; j--;) { - if(j & mask1) - eperm32tab[comes_from / 8][j][bit / 24] |= BITMASK[bit % 24]; - } - } - - /* - * Create an inverse matrix for esel telling - * where to plug out bits if undoing it - */ - for(bit=48; bit--;) { - e_inverse[esel[bit] - 1 ] = bit; - e_inverse[esel[bit] - 1 + 32] = bit + 48; - } - - /* - * create efp: the matrix used to - * undo the E expansion and effect final permutation - */ - _ufc_clearmem((char*)efp, (int)sizeof efp); - for(bit = 0; bit < 64; bit++) { - int o_bit, o_long; - ufc_long word_value, mask1, mask2; - int comes_from_f_bit, comes_from_e_bit; - int comes_from_word, bit_within_word; - - /* See where bit i belongs in the two 32 bit long's */ - o_long = bit / 32; /* 0..1 */ - o_bit = bit % 32; /* 0..31 */ - - /* - * And find a bit in the e permutated value setting this bit. - * - * Note: the e selection may have selected the same bit several - * times. By the initialization of e_inverse, we only look - * for one specific instance. - */ - comes_from_f_bit = final_perm[bit] - 1; /* 0..63 */ - comes_from_e_bit = e_inverse[comes_from_f_bit]; /* 0..95 */ - comes_from_word = comes_from_e_bit / 6; /* 0..15 */ - bit_within_word = comes_from_e_bit % 6; /* 0..5 */ - - mask1 = longmask[bit_within_word + 26]; - mask2 = longmask[o_bit]; - - for(word_value = 64; word_value--;) { - if(word_value & mask1) - efp[comes_from_word][word_value][o_long] |= mask2; - } - } - small_tables_initialized = 1; -#ifdef __GNU_LIBRARY__ -small_tables_done: - __libc_lock_unlock(_ufc_tables_lock); -#endif - } - - /* - * Create the sb tables: - * - * For each 12 bit segment of an 48 bit intermediate - * result, the sb table precomputes the two 4 bit - * values of the sbox lookups done with the two 6 - * bit halves, shifts them to their proper place, - * sends them through perm32 and finally E expands - * them so that they are ready for the next - * DES round. - * - */ - - _ufc_clearmem((char*)__data->sb0, (int)sizeof(__data->sb0)); - _ufc_clearmem((char*)__data->sb1, (int)sizeof(__data->sb1)); - _ufc_clearmem((char*)__data->sb2, (int)sizeof(__data->sb2)); - _ufc_clearmem((char*)__data->sb3, (int)sizeof(__data->sb3)); - - for(sg = 0; sg < 4; sg++) { - int j1, j2; - int s1, s2; - - for(j1 = 0; j1 < 64; j1++) { - s1 = s_lookup(2 * sg, j1); - for(j2 = 0; j2 < 64; j2++) { - ufc_long to_permute, inx; - - s2 = s_lookup(2 * sg + 1, j2); - to_permute = (((ufc_long)s1 << 4) | - (ufc_long)s2) << (24 - 8 * (ufc_long)sg); - -#ifdef _UFC_32_ - inx = ((j1 << 6) | j2) << 1; - sb[sg][inx ] = eperm32tab[0][(to_permute >> 24) & 0xff][0]; - sb[sg][inx+1] = eperm32tab[0][(to_permute >> 24) & 0xff][1]; - sb[sg][inx ] |= eperm32tab[1][(to_permute >> 16) & 0xff][0]; - sb[sg][inx+1] |= eperm32tab[1][(to_permute >> 16) & 0xff][1]; - sb[sg][inx ] |= eperm32tab[2][(to_permute >> 8) & 0xff][0]; - sb[sg][inx+1] |= eperm32tab[2][(to_permute >> 8) & 0xff][1]; - sb[sg][inx ] |= eperm32tab[3][(to_permute) & 0xff][0]; - sb[sg][inx+1] |= eperm32tab[3][(to_permute) & 0xff][1]; -#endif -#ifdef _UFC_64_ - inx = ((j1 << 6) | j2); - sb[sg][inx] = - ((long64)eperm32tab[0][(to_permute >> 24) & 0xff][0] << 32) | - (long64)eperm32tab[0][(to_permute >> 24) & 0xff][1]; - sb[sg][inx] |= - ((long64)eperm32tab[1][(to_permute >> 16) & 0xff][0] << 32) | - (long64)eperm32tab[1][(to_permute >> 16) & 0xff][1]; - sb[sg][inx] |= - ((long64)eperm32tab[2][(to_permute >> 8) & 0xff][0] << 32) | - (long64)eperm32tab[2][(to_permute >> 8) & 0xff][1]; - sb[sg][inx] |= - ((long64)eperm32tab[3][(to_permute) & 0xff][0] << 32) | - (long64)eperm32tab[3][(to_permute) & 0xff][1]; -#endif - } - } - } - - __data->initialized++; -} - -void -__init_des() -{ - __init_des_r(&_ufc_foobar); -} - -/* - * Process the elements of the sb table permuting the - * bits swapped in the expansion by the current salt. - */ - -#ifdef _UFC_32_ -STATIC void -shuffle_sb(k, saltbits) - long32 *k; - ufc_long saltbits; -{ - ufc_long j; - long32 x; - for(j=4096; j--;) { - x = (k[0] ^ k[1]) & (long32)saltbits; - *k++ ^= x; - *k++ ^= x; - } -} -#endif - -#ifdef _UFC_64_ -STATIC void -shuffle_sb(k, saltbits) - long64 *k; - ufc_long saltbits; -{ - ufc_long j; - long64 x; - for(j=4096; j--;) { - x = ((*k >> 32) ^ *k) & (long64)saltbits; - *k++ ^= (x << 32) | x; - } -} -#endif - -/* - * Setup the unit for a new salt - * Hopefully we'll not see a new salt in each crypt call. - */ - -void -_ufc_setup_salt_r(s, __data) - __const char *s; - struct crypt_data * __restrict __data; -{ - ufc_long i, j, saltbits; - - if(__data->initialized == 0) - __init_des_r(__data); - - if(s[0] == __data->current_salt[0] && s[1] == __data->current_salt[1]) - return; - __data->current_salt[0] = s[0]; __data->current_salt[1] = s[1]; - - /* - * This is the only crypt change to DES: - * entries are swapped in the expansion table - * according to the bits set in the salt. - */ - saltbits = 0; - for(i = 0; i < 2; i++) { - long c=ascii_to_bin(s[i]); - for(j = 0; j < 6; j++) { - if((c >> j) & 0x1) - saltbits |= BITMASK[6 * i + j]; - } - } - - /* - * Permute the sb table values - * to reflect the changed e - * selection table - */ -#ifdef _UFC_32_ -#define LONGG long32* -#endif -#ifdef _UFC_64_ -#define LONGG long64* -#endif - - shuffle_sb((LONGG)__data->sb0, __data->current_saltbits ^ saltbits); - shuffle_sb((LONGG)__data->sb1, __data->current_saltbits ^ saltbits); - shuffle_sb((LONGG)__data->sb2, __data->current_saltbits ^ saltbits); - shuffle_sb((LONGG)__data->sb3, __data->current_saltbits ^ saltbits); - - __data->current_saltbits = saltbits; -} - -void -_ufc_mk_keytab_r(key, __data) - const char *key; - struct crypt_data * __restrict __data; -{ - ufc_long v1, v2, *k1; - int i; -#ifdef _UFC_32_ - long32 v, *k2; - k2 = (long32*)__data->keysched; -#endif -#ifdef _UFC_64_ - long64 v, *k2; - k2 = (long64*)__data->keysched; -#endif - - v1 = v2 = 0; k1 = &do_pc1[0][0][0]; - for(i = 8; i--;) { - v1 |= k1[*key & 0x7f]; k1 += 128; - v2 |= k1[*key++ & 0x7f]; k1 += 128; - } - - for(i = 0; i < 16; i++) { - k1 = &do_pc2[0][0]; - - v1 = (v1 << rots[i]) | (v1 >> (28 - rots[i])); - v = k1[(v1 >> 21) & 0x7f]; k1 += 128; - v |= k1[(v1 >> 14) & 0x7f]; k1 += 128; - v |= k1[(v1 >> 7) & 0x7f]; k1 += 128; - v |= k1[(v1 ) & 0x7f]; k1 += 128; - -#ifdef _UFC_32_ - *k2++ = (v | 0x00008000); - v = 0; -#endif -#ifdef _UFC_64_ - v = (v << 32); -#endif - - v2 = (v2 << rots[i]) | (v2 >> (28 - rots[i])); - v |= k1[(v2 >> 21) & 0x7f]; k1 += 128; - v |= k1[(v2 >> 14) & 0x7f]; k1 += 128; - v |= k1[(v2 >> 7) & 0x7f]; k1 += 128; - v |= k1[(v2 ) & 0x7f]; - -#ifdef _UFC_32_ - *k2++ = (v | 0x00008000); -#endif -#ifdef _UFC_64_ - *k2++ = v | 0x0000800000008000l; -#endif - } - - __data->direction = 0; -} - -/* - * Undo an extra E selection and do final permutations - */ - -void -_ufc_dofinalperm_r(res, __data) - ufc_long *res; - struct crypt_data * __restrict __data; -{ - ufc_long v1, v2, x; - ufc_long l1,l2,r1,r2; - - l1 = res[0]; l2 = res[1]; - r1 = res[2]; r2 = res[3]; - - x = (l1 ^ l2) & __data->current_saltbits; l1 ^= x; l2 ^= x; - x = (r1 ^ r2) & __data->current_saltbits; r1 ^= x; r2 ^= x; - - v1=v2=0; l1 >>= 3; l2 >>= 3; r1 >>= 3; r2 >>= 3; - - v1 |= efp[15][ r2 & 0x3f][0]; v2 |= efp[15][ r2 & 0x3f][1]; - v1 |= efp[14][(r2 >>= 6) & 0x3f][0]; v2 |= efp[14][ r2 & 0x3f][1]; - v1 |= efp[13][(r2 >>= 10) & 0x3f][0]; v2 |= efp[13][ r2 & 0x3f][1]; - v1 |= efp[12][(r2 >>= 6) & 0x3f][0]; v2 |= efp[12][ r2 & 0x3f][1]; - - v1 |= efp[11][ r1 & 0x3f][0]; v2 |= efp[11][ r1 & 0x3f][1]; - v1 |= efp[10][(r1 >>= 6) & 0x3f][0]; v2 |= efp[10][ r1 & 0x3f][1]; - v1 |= efp[ 9][(r1 >>= 10) & 0x3f][0]; v2 |= efp[ 9][ r1 & 0x3f][1]; - v1 |= efp[ 8][(r1 >>= 6) & 0x3f][0]; v2 |= efp[ 8][ r1 & 0x3f][1]; - - v1 |= efp[ 7][ l2 & 0x3f][0]; v2 |= efp[ 7][ l2 & 0x3f][1]; - v1 |= efp[ 6][(l2 >>= 6) & 0x3f][0]; v2 |= efp[ 6][ l2 & 0x3f][1]; - v1 |= efp[ 5][(l2 >>= 10) & 0x3f][0]; v2 |= efp[ 5][ l2 & 0x3f][1]; - v1 |= efp[ 4][(l2 >>= 6) & 0x3f][0]; v2 |= efp[ 4][ l2 & 0x3f][1]; - - v1 |= efp[ 3][ l1 & 0x3f][0]; v2 |= efp[ 3][ l1 & 0x3f][1]; - v1 |= efp[ 2][(l1 >>= 6) & 0x3f][0]; v2 |= efp[ 2][ l1 & 0x3f][1]; - v1 |= efp[ 1][(l1 >>= 10) & 0x3f][0]; v2 |= efp[ 1][ l1 & 0x3f][1]; - v1 |= efp[ 0][(l1 >>= 6) & 0x3f][0]; v2 |= efp[ 0][ l1 & 0x3f][1]; - - res[0] = v1; res[1] = v2; -} - -/* - * crypt only: convert from 64 bit to 11 bit ASCII - * prefixing with the salt - */ - -void -_ufc_output_conversion_r(v1, v2, salt, __data) - ufc_long v1, v2; - __const char *salt; - struct crypt_data * __restrict __data; -{ - int i, s, shf; - - __data->crypt_3_buf[0] = salt[0]; - __data->crypt_3_buf[1] = salt[1] ? salt[1] : salt[0]; - - for(i = 0; i < 5; i++) { - shf = (26 - 6 * i); /* to cope with MSC compiler bug */ - __data->crypt_3_buf[i + 2] = bin_to_ascii((v1 >> shf) & 0x3f); - } - - s = (v2 & 0xf) << 2; - v2 = (v2 >> 2) | ((v1 & 0x3) << 30); - - for(i = 5; i < 10; i++) { - shf = (56 - 6 * i); - __data->crypt_3_buf[i + 2] = bin_to_ascii((v2 >> shf) & 0x3f); - } - - __data->crypt_3_buf[12] = bin_to_ascii(s); - __data->crypt_3_buf[13] = 0; -} - - -/* - * UNIX encrypt function. Takes a bitvector - * represented by one byte per bit and - * encrypt/decrypt according to edflag - */ - -void -__encrypt_r(__block, __edflag, __data) - char *__block; - int __edflag; - struct crypt_data * __restrict __data; -{ - ufc_long l1, l2, r1, r2, res[4]; - int i; -#ifdef _UFC_32_ - long32 *kt; - kt = (long32*)__data->keysched; -#endif -#ifdef _UFC_64_ - long64 *kt; - kt = (long64*)__data->keysched; -#endif - - /* - * Undo any salt changes to E expansion - */ - _ufc_setup_salt_r("..", __data); - - /* - * Reverse key table if - * changing operation (encrypt/decrypt) - */ - if((__edflag == 0) != (__data->direction == 0)) { - for(i = 0; i < 8; i++) { -#ifdef _UFC_32_ - long32 x; - x = kt[2 * (15-i)]; - kt[2 * (15-i)] = kt[2 * i]; - kt[2 * i] = x; - - x = kt[2 * (15-i) + 1]; - kt[2 * (15-i) + 1] = kt[2 * i + 1]; - kt[2 * i + 1] = x; -#endif -#ifdef _UFC_64_ - long64 x; - x = kt[15-i]; - kt[15-i] = kt[i]; - kt[i] = x; -#endif - } - __data->direction = __edflag; - } - - /* - * Do initial permutation + E expansion - */ - i = 0; - for(l1 = 0; i < 24; i++) { - if(__block[initial_perm[esel[i]-1]-1]) - l1 |= BITMASK[i]; - } - for(l2 = 0; i < 48; i++) { - if(__block[initial_perm[esel[i]-1]-1]) - l2 |= BITMASK[i-24]; - } - - i = 0; - for(r1 = 0; i < 24; i++) { - if(__block[initial_perm[esel[i]-1+32]-1]) - r1 |= BITMASK[i]; - } - for(r2 = 0; i < 48; i++) { - if(__block[initial_perm[esel[i]-1+32]-1]) - r2 |= BITMASK[i-24]; - } - - /* - * Do DES inner loops + final conversion - */ - res[0] = l1; res[1] = l2; - res[2] = r1; res[3] = r2; - _ufc_doit_r((ufc_long)1, __data, &res[0]); - - /* - * Do final permutations - */ - _ufc_dofinalperm_r(res, __data); - - /* - * And convert to bit array - */ - l1 = res[0]; r1 = res[1]; - for(i = 0; i < 32; i++) { - *__block++ = (l1 & longmask[i]) != 0; - } - for(i = 0; i < 32; i++) { - *__block++ = (r1 & longmask[i]) != 0; - } -} -weak_alias (__encrypt_r, encrypt_r) - -void -encrypt(__block, __edflag) - char *__block; - int __edflag; -{ - __encrypt_r(__block, __edflag, &_ufc_foobar); -} - - -/* - * UNIX setkey function. Take a 64 bit DES - * key and setup the machinery. - */ - -void -__setkey_r(__key, __data) - __const char *__key; - struct crypt_data * __restrict __data; -{ - int i,j; - unsigned char c; - unsigned char ktab[8]; - - _ufc_setup_salt_r("..", __data); /* be sure we're initialized */ - - for(i = 0; i < 8; i++) { - for(j = 0, c = 0; j < 8; j++) - c = c << 1 | *__key++; - ktab[i] = c >> 1; - } - _ufc_mk_keytab_r(ktab, __data); -} -weak_alias (__setkey_r, setkey_r) - -void -setkey(__key) - __const char *__key; -{ - __setkey_r(__key, &_ufc_foobar); -} diff --git a/crypt/sysdeps/unix/des_impl.c b/crypt/sysdeps/unix/des_impl.c deleted file mode 100644 index f4fee72..0000000 --- a/crypt/sysdeps/unix/des_impl.c +++ /dev/null @@ -1,615 +0,0 @@ -/* Copyright (C) 1992 Eric Young - see COPYING for more details */ -/* Collected from libdes and modified for SECURE RPC by Martin Kuck 1994 */ -#include -#include "des.h" - - -static const unsigned long des_SPtrans[8][64] = -{ - { /* nibble 0 */ - 0x00820200, 0x00020000, 0x80800000, 0x80820200, - 0x00800000, 0x80020200, 0x80020000, 0x80800000, - 0x80020200, 0x00820200, 0x00820000, 0x80000200, - 0x80800200, 0x00800000, 0x00000000, 0x80020000, - 0x00020000, 0x80000000, 0x00800200, 0x00020200, - 0x80820200, 0x00820000, 0x80000200, 0x00800200, - 0x80000000, 0x00000200, 0x00020200, 0x80820000, - 0x00000200, 0x80800200, 0x80820000, 0x00000000, - 0x00000000, 0x80820200, 0x00800200, 0x80020000, - 0x00820200, 0x00020000, 0x80000200, 0x00800200, - 0x80820000, 0x00000200, 0x00020200, 0x80800000, - 0x80020200, 0x80000000, 0x80800000, 0x00820000, - 0x80820200, 0x00020200, 0x00820000, 0x80800200, - 0x00800000, 0x80000200, 0x80020000, 0x00000000, - 0x00020000, 0x00800000, 0x80800200, 0x00820200, - 0x80000000, 0x80820000, 0x00000200, 0x80020200}, - - { /* nibble 1 */ - 0x10042004, 0x00000000, 0x00042000, 0x10040000, - 0x10000004, 0x00002004, 0x10002000, 0x00042000, - 0x00002000, 0x10040004, 0x00000004, 0x10002000, - 0x00040004, 0x10042000, 0x10040000, 0x00000004, - 0x00040000, 0x10002004, 0x10040004, 0x00002000, - 0x00042004, 0x10000000, 0x00000000, 0x00040004, - 0x10002004, 0x00042004, 0x10042000, 0x10000004, - 0x10000000, 0x00040000, 0x00002004, 0x10042004, - 0x00040004, 0x10042000, 0x10002000, 0x00042004, - 0x10042004, 0x00040004, 0x10000004, 0x00000000, - 0x10000000, 0x00002004, 0x00040000, 0x10040004, - 0x00002000, 0x10000000, 0x00042004, 0x10002004, - 0x10042000, 0x00002000, 0x00000000, 0x10000004, - 0x00000004, 0x10042004, 0x00042000, 0x10040000, - 0x10040004, 0x00040000, 0x00002004, 0x10002000, - 0x10002004, 0x00000004, 0x10040000, 0x00042000}, - - { /* nibble 2 */ - 0x41000000, 0x01010040, 0x00000040, 0x41000040, - 0x40010000, 0x01000000, 0x41000040, 0x00010040, - 0x01000040, 0x00010000, 0x01010000, 0x40000000, - 0x41010040, 0x40000040, 0x40000000, 0x41010000, - 0x00000000, 0x40010000, 0x01010040, 0x00000040, - 0x40000040, 0x41010040, 0x00010000, 0x41000000, - 0x41010000, 0x01000040, 0x40010040, 0x01010000, - 0x00010040, 0x00000000, 0x01000000, 0x40010040, - 0x01010040, 0x00000040, 0x40000000, 0x00010000, - 0x40000040, 0x40010000, 0x01010000, 0x41000040, - 0x00000000, 0x01010040, 0x00010040, 0x41010000, - 0x40010000, 0x01000000, 0x41010040, 0x40000000, - 0x40010040, 0x41000000, 0x01000000, 0x41010040, - 0x00010000, 0x01000040, 0x41000040, 0x00010040, - 0x01000040, 0x00000000, 0x41010000, 0x40000040, - 0x41000000, 0x40010040, 0x00000040, 0x01010000}, - - { /* nibble 3 */ - 0x00100402, 0x04000400, 0x00000002, 0x04100402, - 0x00000000, 0x04100000, 0x04000402, 0x00100002, - 0x04100400, 0x04000002, 0x04000000, 0x00000402, - 0x04000002, 0x00100402, 0x00100000, 0x04000000, - 0x04100002, 0x00100400, 0x00000400, 0x00000002, - 0x00100400, 0x04000402, 0x04100000, 0x00000400, - 0x00000402, 0x00000000, 0x00100002, 0x04100400, - 0x04000400, 0x04100002, 0x04100402, 0x00100000, - 0x04100002, 0x00000402, 0x00100000, 0x04000002, - 0x00100400, 0x04000400, 0x00000002, 0x04100000, - 0x04000402, 0x00000000, 0x00000400, 0x00100002, - 0x00000000, 0x04100002, 0x04100400, 0x00000400, - 0x04000000, 0x04100402, 0x00100402, 0x00100000, - 0x04100402, 0x00000002, 0x04000400, 0x00100402, - 0x00100002, 0x00100400, 0x04100000, 0x04000402, - 0x00000402, 0x04000000, 0x04000002, 0x04100400}, - - { /* nibble 4 */ - 0x02000000, 0x00004000, 0x00000100, 0x02004108, - 0x02004008, 0x02000100, 0x00004108, 0x02004000, - 0x00004000, 0x00000008, 0x02000008, 0x00004100, - 0x02000108, 0x02004008, 0x02004100, 0x00000000, - 0x00004100, 0x02000000, 0x00004008, 0x00000108, - 0x02000100, 0x00004108, 0x00000000, 0x02000008, - 0x00000008, 0x02000108, 0x02004108, 0x00004008, - 0x02004000, 0x00000100, 0x00000108, 0x02004100, - 0x02004100, 0x02000108, 0x00004008, 0x02004000, - 0x00004000, 0x00000008, 0x02000008, 0x02000100, - 0x02000000, 0x00004100, 0x02004108, 0x00000000, - 0x00004108, 0x02000000, 0x00000100, 0x00004008, - 0x02000108, 0x00000100, 0x00000000, 0x02004108, - 0x02004008, 0x02004100, 0x00000108, 0x00004000, - 0x00004100, 0x02004008, 0x02000100, 0x00000108, - 0x00000008, 0x00004108, 0x02004000, 0x02000008}, - - { /* nibble 5 */ - 0x20000010, 0x00080010, 0x00000000, 0x20080800, - 0x00080010, 0x00000800, 0x20000810, 0x00080000, - 0x00000810, 0x20080810, 0x00080800, 0x20000000, - 0x20000800, 0x20000010, 0x20080000, 0x00080810, - 0x00080000, 0x20000810, 0x20080010, 0x00000000, - 0x00000800, 0x00000010, 0x20080800, 0x20080010, - 0x20080810, 0x20080000, 0x20000000, 0x00000810, - 0x00000010, 0x00080800, 0x00080810, 0x20000800, - 0x00000810, 0x20000000, 0x20000800, 0x00080810, - 0x20080800, 0x00080010, 0x00000000, 0x20000800, - 0x20000000, 0x00000800, 0x20080010, 0x00080000, - 0x00080010, 0x20080810, 0x00080800, 0x00000010, - 0x20080810, 0x00080800, 0x00080000, 0x20000810, - 0x20000010, 0x20080000, 0x00080810, 0x00000000, - 0x00000800, 0x20000010, 0x20000810, 0x20080800, - 0x20080000, 0x00000810, 0x00000010, 0x20080010}, - - { /* nibble 6 */ - 0x00001000, 0x00000080, 0x00400080, 0x00400001, - 0x00401081, 0x00001001, 0x00001080, 0x00000000, - 0x00400000, 0x00400081, 0x00000081, 0x00401000, - 0x00000001, 0x00401080, 0x00401000, 0x00000081, - 0x00400081, 0x00001000, 0x00001001, 0x00401081, - 0x00000000, 0x00400080, 0x00400001, 0x00001080, - 0x00401001, 0x00001081, 0x00401080, 0x00000001, - 0x00001081, 0x00401001, 0x00000080, 0x00400000, - 0x00001081, 0x00401000, 0x00401001, 0x00000081, - 0x00001000, 0x00000080, 0x00400000, 0x00401001, - 0x00400081, 0x00001081, 0x00001080, 0x00000000, - 0x00000080, 0x00400001, 0x00000001, 0x00400080, - 0x00000000, 0x00400081, 0x00400080, 0x00001080, - 0x00000081, 0x00001000, 0x00401081, 0x00400000, - 0x00401080, 0x00000001, 0x00001001, 0x00401081, - 0x00400001, 0x00401080, 0x00401000, 0x00001001}, - - { /* nibble 7 */ - 0x08200020, 0x08208000, 0x00008020, 0x00000000, - 0x08008000, 0x00200020, 0x08200000, 0x08208020, - 0x00000020, 0x08000000, 0x00208000, 0x00008020, - 0x00208020, 0x08008020, 0x08000020, 0x08200000, - 0x00008000, 0x00208020, 0x00200020, 0x08008000, - 0x08208020, 0x08000020, 0x00000000, 0x00208000, - 0x08000000, 0x00200000, 0x08008020, 0x08200020, - 0x00200000, 0x00008000, 0x08208000, 0x00000020, - 0x00200000, 0x00008000, 0x08000020, 0x08208020, - 0x00008020, 0x08000000, 0x00000000, 0x00208000, - 0x08200020, 0x08008020, 0x08008000, 0x00200020, - 0x08208000, 0x00000020, 0x00200020, 0x08008000, - 0x08208020, 0x00200000, 0x08200000, 0x08000020, - 0x00208000, 0x00008020, 0x08008020, 0x08200000, - 0x00000020, 0x08208000, 0x00208020, 0x00000000, - 0x08000000, 0x08200020, 0x00008000, 0x00208020}}; - -static const unsigned long des_skb[8][64] = -{ - { /* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ - 0x00000000, 0x00000010, 0x20000000, 0x20000010, - 0x00010000, 0x00010010, 0x20010000, 0x20010010, - 0x00000800, 0x00000810, 0x20000800, 0x20000810, - 0x00010800, 0x00010810, 0x20010800, 0x20010810, - 0x00000020, 0x00000030, 0x20000020, 0x20000030, - 0x00010020, 0x00010030, 0x20010020, 0x20010030, - 0x00000820, 0x00000830, 0x20000820, 0x20000830, - 0x00010820, 0x00010830, 0x20010820, 0x20010830, - 0x00080000, 0x00080010, 0x20080000, 0x20080010, - 0x00090000, 0x00090010, 0x20090000, 0x20090010, - 0x00080800, 0x00080810, 0x20080800, 0x20080810, - 0x00090800, 0x00090810, 0x20090800, 0x20090810, - 0x00080020, 0x00080030, 0x20080020, 0x20080030, - 0x00090020, 0x00090030, 0x20090020, 0x20090030, - 0x00080820, 0x00080830, 0x20080820, 0x20080830, - 0x00090820, 0x00090830, 0x20090820, 0x20090830}, - { /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */ - 0x00000000, 0x02000000, 0x00002000, 0x02002000, - 0x00200000, 0x02200000, 0x00202000, 0x02202000, - 0x00000004, 0x02000004, 0x00002004, 0x02002004, - 0x00200004, 0x02200004, 0x00202004, 0x02202004, - 0x00000400, 0x02000400, 0x00002400, 0x02002400, - 0x00200400, 0x02200400, 0x00202400, 0x02202400, - 0x00000404, 0x02000404, 0x00002404, 0x02002404, - 0x00200404, 0x02200404, 0x00202404, 0x02202404, - 0x10000000, 0x12000000, 0x10002000, 0x12002000, - 0x10200000, 0x12200000, 0x10202000, 0x12202000, - 0x10000004, 0x12000004, 0x10002004, 0x12002004, - 0x10200004, 0x12200004, 0x10202004, 0x12202004, - 0x10000400, 0x12000400, 0x10002400, 0x12002400, - 0x10200400, 0x12200400, 0x10202400, 0x12202400, - 0x10000404, 0x12000404, 0x10002404, 0x12002404, - 0x10200404, 0x12200404, 0x10202404, 0x12202404}, - { /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */ - 0x00000000, 0x00000001, 0x00040000, 0x00040001, - 0x01000000, 0x01000001, 0x01040000, 0x01040001, - 0x00000002, 0x00000003, 0x00040002, 0x00040003, - 0x01000002, 0x01000003, 0x01040002, 0x01040003, - 0x00000200, 0x00000201, 0x00040200, 0x00040201, - 0x01000200, 0x01000201, 0x01040200, 0x01040201, - 0x00000202, 0x00000203, 0x00040202, 0x00040203, - 0x01000202, 0x01000203, 0x01040202, 0x01040203, - 0x08000000, 0x08000001, 0x08040000, 0x08040001, - 0x09000000, 0x09000001, 0x09040000, 0x09040001, - 0x08000002, 0x08000003, 0x08040002, 0x08040003, - 0x09000002, 0x09000003, 0x09040002, 0x09040003, - 0x08000200, 0x08000201, 0x08040200, 0x08040201, - 0x09000200, 0x09000201, 0x09040200, 0x09040201, - 0x08000202, 0x08000203, 0x08040202, 0x08040203, - 0x09000202, 0x09000203, 0x09040202, 0x09040203}, - { /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */ - 0x00000000, 0x00100000, 0x00000100, 0x00100100, - 0x00000008, 0x00100008, 0x00000108, 0x00100108, - 0x00001000, 0x00101000, 0x00001100, 0x00101100, - 0x00001008, 0x00101008, 0x00001108, 0x00101108, - 0x04000000, 0x04100000, 0x04000100, 0x04100100, - 0x04000008, 0x04100008, 0x04000108, 0x04100108, - 0x04001000, 0x04101000, 0x04001100, 0x04101100, - 0x04001008, 0x04101008, 0x04001108, 0x04101108, - 0x00020000, 0x00120000, 0x00020100, 0x00120100, - 0x00020008, 0x00120008, 0x00020108, 0x00120108, - 0x00021000, 0x00121000, 0x00021100, 0x00121100, - 0x00021008, 0x00121008, 0x00021108, 0x00121108, - 0x04020000, 0x04120000, 0x04020100, 0x04120100, - 0x04020008, 0x04120008, 0x04020108, 0x04120108, - 0x04021000, 0x04121000, 0x04021100, 0x04121100, - 0x04021008, 0x04121008, 0x04021108, 0x04121108}, - { /* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ - 0x00000000, 0x10000000, 0x00010000, 0x10010000, - 0x00000004, 0x10000004, 0x00010004, 0x10010004, - 0x20000000, 0x30000000, 0x20010000, 0x30010000, - 0x20000004, 0x30000004, 0x20010004, 0x30010004, - 0x00100000, 0x10100000, 0x00110000, 0x10110000, - 0x00100004, 0x10100004, 0x00110004, 0x10110004, - 0x20100000, 0x30100000, 0x20110000, 0x30110000, - 0x20100004, 0x30100004, 0x20110004, 0x30110004, - 0x00001000, 0x10001000, 0x00011000, 0x10011000, - 0x00001004, 0x10001004, 0x00011004, 0x10011004, - 0x20001000, 0x30001000, 0x20011000, 0x30011000, - 0x20001004, 0x30001004, 0x20011004, 0x30011004, - 0x00101000, 0x10101000, 0x00111000, 0x10111000, - 0x00101004, 0x10101004, 0x00111004, 0x10111004, - 0x20101000, 0x30101000, 0x20111000, 0x30111000, - 0x20101004, 0x30101004, 0x20111004, 0x30111004}, - { /* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */ - 0x00000000, 0x08000000, 0x00000008, 0x08000008, - 0x00000400, 0x08000400, 0x00000408, 0x08000408, - 0x00020000, 0x08020000, 0x00020008, 0x08020008, - 0x00020400, 0x08020400, 0x00020408, 0x08020408, - 0x00000001, 0x08000001, 0x00000009, 0x08000009, - 0x00000401, 0x08000401, 0x00000409, 0x08000409, - 0x00020001, 0x08020001, 0x00020009, 0x08020009, - 0x00020401, 0x08020401, 0x00020409, 0x08020409, - 0x02000000, 0x0A000000, 0x02000008, 0x0A000008, - 0x02000400, 0x0A000400, 0x02000408, 0x0A000408, - 0x02020000, 0x0A020000, 0x02020008, 0x0A020008, - 0x02020400, 0x0A020400, 0x02020408, 0x0A020408, - 0x02000001, 0x0A000001, 0x02000009, 0x0A000009, - 0x02000401, 0x0A000401, 0x02000409, 0x0A000409, - 0x02020001, 0x0A020001, 0x02020009, 0x0A020009, - 0x02020401, 0x0A020401, 0x02020409, 0x0A020409}, - { /* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */ - 0x00000000, 0x00000100, 0x00080000, 0x00080100, - 0x01000000, 0x01000100, 0x01080000, 0x01080100, - 0x00000010, 0x00000110, 0x00080010, 0x00080110, - 0x01000010, 0x01000110, 0x01080010, 0x01080110, - 0x00200000, 0x00200100, 0x00280000, 0x00280100, - 0x01200000, 0x01200100, 0x01280000, 0x01280100, - 0x00200010, 0x00200110, 0x00280010, 0x00280110, - 0x01200010, 0x01200110, 0x01280010, 0x01280110, - 0x00000200, 0x00000300, 0x00080200, 0x00080300, - 0x01000200, 0x01000300, 0x01080200, 0x01080300, - 0x00000210, 0x00000310, 0x00080210, 0x00080310, - 0x01000210, 0x01000310, 0x01080210, 0x01080310, - 0x00200200, 0x00200300, 0x00280200, 0x00280300, - 0x01200200, 0x01200300, 0x01280200, 0x01280300, - 0x00200210, 0x00200310, 0x00280210, 0x00280310, - 0x01200210, 0x01200310, 0x01280210, 0x01280310}, - { /* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */ - 0x00000000, 0x04000000, 0x00040000, 0x04040000, - 0x00000002, 0x04000002, 0x00040002, 0x04040002, - 0x00002000, 0x04002000, 0x00042000, 0x04042000, - 0x00002002, 0x04002002, 0x00042002, 0x04042002, - 0x00000020, 0x04000020, 0x00040020, 0x04040020, - 0x00000022, 0x04000022, 0x00040022, 0x04040022, - 0x00002020, 0x04002020, 0x00042020, 0x04042020, - 0x00002022, 0x04002022, 0x00042022, 0x04042022, - 0x00000800, 0x04000800, 0x00040800, 0x04040800, - 0x00000802, 0x04000802, 0x00040802, 0x04040802, - 0x00002800, 0x04002800, 0x00042800, 0x04042800, - 0x00002802, 0x04002802, 0x00042802, 0x04042802, - 0x00000820, 0x04000820, 0x00040820, 0x04040820, - 0x00000822, 0x04000822, 0x00040822, 0x04040822, - 0x00002820, 0x04002820, 0x00042820, 0x04042820, - 0x00002822, 0x04002822, 0x00042822, 0x04042822}, -}; - -#define c2l(c,l) (l =((unsigned long)(*((c)++))) , \ - l|=((unsigned long)(*((c)++)))<< 8, \ - l|=((unsigned long)(*((c)++)))<<16, \ - l|=((unsigned long)(*((c)++)))<<24) - -#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ - *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ - *((c)++)=(unsigned char)(((l)>>16)&0xff), \ - *((c)++)=(unsigned char)(((l)>>24)&0xff)) - -/* - * IP and FP - * The problem is more of a geometric problem that random bit fiddling. - * 0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6 - * 8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4 - * 16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2 - * 24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0 - * - * 32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7 - * 40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5 - * 48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3 - * 56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1 - * - * The output has been subject to swaps of the form - * 0 1 -> 3 1 but the odd and even bits have been put into - * 2 3 2 0 - * different words. The main trick is to remember that - * t=((l>>size)^r)&(mask); - * r^=t; - * l^=(t<>(n))^(b))&(m)),\ - (b)^=(t),\ - (a)^=((t)<<(n))) - -#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\ - (a)=(a)^(t)^(t>>(16-(n)))) - - -/* The changes to this macro may help or hinder, depending on the - * compiler and the achitecture. gcc2 always seems to do well :-). - * Inspired by Dana How - * DO NOT use the alternative version on machines with 8 byte longs. - */ -#ifdef ALT_ECB -#define D_ENCRYPT(L,R,S) \ - u=((R^s[S ])<<2); \ - t= R^s[S+1]; \ - t=((t>>2)+(t<<30)); \ - L^= \ - *(const unsigned long *)(des_SP+0x0100+((t )&0xfc))+ \ - *(const unsigned long *)(des_SP+0x0300+((t>> 8)&0xfc))+ \ - *(const unsigned long *)(des_SP+0x0500+((t>>16)&0xfc))+ \ - *(const unsigned long *)(des_SP+0x0700+((t>>24)&0xfc))+ \ - *(const unsigned long *)(des_SP+ ((u )&0xfc))+ \ - *(const unsigned long *)(des_SP+0x0200+((u>> 8)&0xfc))+ \ - *(const unsigned long *)(des_SP+0x0400+((u>>16)&0xfc))+ \ - *(const unsigned long *)(des_SP+0x0600+((u>>24)&0xfc)); -#else /* original version */ -#define D_ENCRYPT(L,R,S) \ - u=(R^s[S ]); \ - t=R^s[S+1]; \ - t=((t>>4)+(t<<28)); \ - L^= des_SPtrans[1][(t )&0x3f]| \ - des_SPtrans[3][(t>> 8)&0x3f]| \ - des_SPtrans[5][(t>>16)&0x3f]| \ - des_SPtrans[7][(t>>24)&0x3f]| \ - des_SPtrans[0][(u )&0x3f]| \ - des_SPtrans[2][(u>> 8)&0x3f]| \ - des_SPtrans[4][(u>>16)&0x3f]| \ - des_SPtrans[6][(u>>24)&0x3f]; -#endif - -#define ITERATIONS 16 - -static const char shifts2[16] = -{0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0}; - -static void des_set_key (char *, unsigned long *) internal_function; -static void des_encrypt (unsigned long *, unsigned long *, int) - internal_function; -int _des_crypt (char *, unsigned, struct desparams *); - -static void -internal_function -des_set_key (char *key, unsigned long *schedule) -{ - register unsigned long c, d, t, s; - register unsigned char *in; - register unsigned long *k; - register int i; - - k = (unsigned long *) schedule; - in = (unsigned char *) key; - - c2l (in, c); - c2l (in, d); - - /* I now do it in 47 simple operations :-) - * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov) - * for the inspiration. :-) */ - PERM_OP (d, c, t, 4, 0x0f0f0f0f); - HPERM_OP (c, t, -2, 0xcccc0000); - HPERM_OP (d, t, -2, 0xcccc0000); - PERM_OP (d, c, t, 1, 0x55555555); - PERM_OP (c, d, t, 8, 0x00ff00ff); - PERM_OP (d, c, t, 1, 0x55555555); - d = (((d & 0x000000ff) << 16) | (d & 0x0000ff00) | - ((d & 0x00ff0000) >> 16) | ((c & 0xf0000000) >> 4)); - c &= 0x0fffffff; - - for (i = 0; i < ITERATIONS; i++) - { - if (shifts2[i]) - { - c = ((c >> 2) | (c << 26)); - d = ((d >> 2) | (d << 26)); - } - else - { - c = ((c >> 1) | (c << 27)); - d = ((d >> 1) | (d << 27)); - } - c &= 0x0fffffff; - d &= 0x0fffffff; - /* could be a few less shifts but I am to lazy at this - * point in time to investigate */ - s = des_skb[0][(c) & 0x3f] | - des_skb[1][((c >> 6) & 0x03) | ((c >> 7) & 0x3c)] | - des_skb[2][((c >> 13) & 0x0f) | ((c >> 14) & 0x30)] | - des_skb[3][((c >> 20) & 0x01) | ((c >> 21) & 0x06) | ((c >> 22) & 0x38)]; - t = des_skb[4][(d) & 0x3f] | - des_skb[5][((d >> 7) & 0x03) | ((d >> 8) & 0x3c)] | - des_skb[6][(d >> 15) & 0x3f] | - des_skb[7][((d >> 21) & 0x0f) | ((d >> 22) & 0x30)]; - - /* table contained 0213 4657 */ - *(k++) = ((t << 16) | (s & 0x0000ffff)) & 0xffffffff; - s = ((s >> 16) | (t & 0xffff0000)); - - s = (s << 4) | (s >> 28); - *(k++) = s & 0xffffffff; - } -} - - -static void -internal_function -des_encrypt (unsigned long *buf, unsigned long *schedule, int encrypt) -{ - register unsigned long l, r, t, u; -#ifdef ALT_ECB - register const unsigned char *des_SP = (const unsigned char *) des_SPtrans; -#endif - register int i; - register unsigned long *s; - - l = buf[0]; - r = buf[1]; - - /* do IP */ - PERM_OP (r, l, t, 4, 0x0f0f0f0f); - PERM_OP (l, r, t, 16, 0x0000ffff); - PERM_OP (r, l, t, 2, 0x33333333); - PERM_OP (l, r, t, 8, 0x00ff00ff); - PERM_OP (r, l, t, 1, 0x55555555); - /* r and l are reversed - remember that :-) - fix - * it in the next step */ - - /* Things have been modified so that the initial rotate is - * done outside the loop. This required the - * des_SPtrans values in sp.h to be rotated 1 bit to the right. - * One perl script later and things have a 5% speed up on a sparc2. - * Thanks to Richard Outerbridge <71755.204@CompuServe.COM> - * for pointing this out. */ - t = (r << 1) | (r >> 31); - r = (l << 1) | (l >> 31); - l = t; - - /* clear the top bits on machines with 8byte longs */ - l &= 0xffffffff; - r &= 0xffffffff; - - s = (unsigned long *) schedule; - /* I don't know if it is worth the effort of loop unrolling the - * inner loop */ - if (encrypt) - { - for (i = 0; i < 32; i += 4) - { - D_ENCRYPT (l, r, i + 0); /* 1 */ - D_ENCRYPT (r, l, i + 2); /* 2 */ - } - } - else - { - for (i = 30; i > 0; i -= 4) - { - D_ENCRYPT (l, r, i - 0); /* 16 */ - D_ENCRYPT (r, l, i - 2); /* 15 */ - } - } - l = (l >> 1) | (l << 31); - r = (r >> 1) | (r << 31); - /* clear the top bits on machines with 8byte longs */ - l &= 0xffffffff; - r &= 0xffffffff; - - /* swap l and r - * we will not do the swap so just remember they are - * reversed for the rest of the subroutine - * luckily FP fixes this problem :-) */ - - PERM_OP (r, l, t, 1, 0x55555555); - PERM_OP (l, r, t, 8, 0x00ff00ff); - PERM_OP (r, l, t, 2, 0x33333333); - PERM_OP (l, r, t, 16, 0x0000ffff); - PERM_OP (r, l, t, 4, 0x0f0f0f0f); - - buf[0] = l; - buf[1] = r; - - l = r = t = u = 0; -} - - -int -_des_crypt (char *buf, unsigned len, struct desparams *desp) -{ - unsigned long schedule[32]; - register unsigned long tin0, tin1; - register unsigned long tout0, tout1, xor0, xor1; - register unsigned char *in, *out; - unsigned long tbuf[2]; - unsigned char *iv, *oiv; - int cbc_mode; - - cbc_mode = (desp->des_mode == CBC) ? 1 : 0; - - in = (unsigned char *) buf; - out = (unsigned char *) buf; - oiv = iv = (unsigned char *) desp->des_ivec; - - des_set_key (desp->des_key, schedule); - - tin0 = tin1 = 0; /* For GCC */ - if (desp->des_dir == ENCRYPT) - { - c2l (iv, tout0); - c2l (iv, tout1); - for (; len > 0; len -= 8) - { - c2l (in, tin0); - c2l (in, tin1); - if (cbc_mode) - { - tin0 ^= tout0; - tin1 ^= tout1; - } - tbuf[0] = tin0; - tbuf[1] = tin1; - des_encrypt (tbuf, schedule, 1); - tout0 = tbuf[0]; - tout1 = tbuf[1]; - l2c (tout0, out); - l2c (tout1, out); - } - l2c (tout0, oiv); - l2c (tout1, oiv); - } - else - { - c2l (iv, xor0); - c2l (iv, xor1); - for (; len > 0; len -= 8) - { - c2l (in, tin0); - c2l (in, tin1); - tbuf[0] = tin0; - tbuf[1] = tin1; - des_encrypt (tbuf, schedule, 0); - if (cbc_mode) - { - tout0 = tbuf[0] ^ xor0; - tout1 = tbuf[1] ^ xor1; - xor0 = tin0; - xor1 = tin1; - } - else - { - tout0 = tbuf[0]; - tout1 = tbuf[1]; - } - l2c (tout0, out); - l2c (tout1, out); - } - l2c (tin0, oiv); - l2c (tin1, oiv); - } - tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; - tbuf[0] = tbuf[1] = 0; - __bzero (schedule, sizeof (schedule)); - - return (1); -} diff --git a/crypt/sysdeps/unix/patchlevel.h b/crypt/sysdeps/unix/patchlevel.h deleted file mode 100644 index 450c091..0000000 --- a/crypt/sysdeps/unix/patchlevel.h +++ /dev/null @@ -1,25 +0,0 @@ -/* - * UFC-crypt: ultra fast crypt(3) implementation - * - * Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; see the file COPYING.LIB. If not, - * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - * - * @(#)patchlevel.h 1.13 9/10/96 - * - */ - -#define PATCHLEVEL "UFC-crypt, patchlevel 1e, @(#)patchlevel.h 1.13 9/10/96" diff --git a/crypt/sysdeps/unix/ufc-crypt.h b/crypt/sysdeps/unix/ufc-crypt.h deleted file mode 100644 index 879211a..0000000 --- a/crypt/sysdeps/unix/ufc-crypt.h +++ /dev/null @@ -1,29 +0,0 @@ -/* Types for UFC-crypt. - Copyright (C) 1998 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the GNU C Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ - -#include - -typedef uint_fast32_t ufc_long; -typedef uint64_t long64; -typedef uint32_t long32; -#if UINT_FAST32_MAX == UINT_FAST64_MAX -# define _UFC_64_ -#else -# define _UFC_32_ -#endif diff --git a/crypt/ufc-crypt.h b/crypt/ufc-crypt.h new file mode 100644 index 0000000..879211a --- /dev/null +++ b/crypt/ufc-crypt.h @@ -0,0 +1,29 @@ +/* Types for UFC-crypt. + Copyright (C) 1998 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include + +typedef uint_fast32_t ufc_long; +typedef uint64_t long64; +typedef uint32_t long32; +#if UINT_FAST32_MAX == UINT_FAST64_MAX +# define _UFC_64_ +#else +# define _UFC_32_ +#endif diff --git a/md5-crypt/Makefile b/md5-crypt/Makefile deleted file mode 100644 index 40186ec..0000000 --- a/md5-crypt/Makefile +++ /dev/null @@ -1,83 +0,0 @@ -# Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. -# This file is part of the GNU C Library. - -# The GNU C Library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Library General Public License as -# published by the Free Software Foundation; either version 2 of the -# License, or (at your option) any later version. - -# The GNU C Library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Library General Public License for more details. - -# You should have received a copy of the GNU Library General Public -# License along with the GNU C Library; see the file COPYING.LIB. If not, -# write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, -# Boston, MA 02111-1307, USA. - -# -# Sub-makefile for MD5 crypt portion of the library. -# -subdir := md5-crypt - -headers := crypt.h - -distribute := md5.h - -tests := md5test md5c-test - -extra-libs := libcrypt -extra-libs-others := $(extra-libs) - -md5-routines := crypt-entry md5-crypt md5 -libcrypt-routines := $(md5-routines) - -onlymd5-routines := onlymd5-entry md5-crypt md5 -distribute += onlymd5-entry.c -extra-objs = $(patsubst %,onlymd5-entry%,$(object-suffixes)) - -include ../Makeconfig - -rpath-dirs += md5-crypt - -ifeq ($(crypt-in-libc),yes) -routines += $(libcrypt-routines) -endif - -$(objpfx)md5test: $(objpfx)md5.o - -include ../Rules - -ifeq ($(build-shared),yes) -libdepend = $(common-objpfx)md5-crypt/libcrypt.so$(libcrypt.so-version) -else -libdepend = $(common-objpfx)md5-crypt/libcrypt.a -endif - -$(objpfx)md5test $(objpfx)md5c-test: $(libdepend) - -.PHONY: libmd5crypt -libmd5crypt: $(foreach o,$(object-suffixes), \ - $(addprefix $(objpfx),$(patsubst %,$(libtype$o),md5crypt))) -ifeq ($(build-shared),yes) -libmd5crypt: $(objpfx)libmd5crypt.so -# Use the same soname as the real libcrypt, so that it can be used as a -# drop in replacement. -LDFLAGS-md5crypt.so = -Wl,-soname=lib$(libprefix)crypt.so$(libcrypt.so-version) -# We also use the same version script. -libmd5crypt-map := $(common-objpfx)libcrypt.map -endif - -define o-iterator-doit -$(objpfx)$(patsubst %,$(libtype$o),md5crypt): \ - $(onlymd5-routines:%=$(objpfx)%$o); $$(build-extra-lib) -endef -object-suffixes-left = $(object-suffixes) -include $(patsubst %,$(..)o-iterator.mk,$(object-suffixes)) - -# Depend on libc.so so a DT_NEEDED is generated in the shared objects. -# This ensures they will load libc.so for needed symbols if loaded by -# a statically-linked program that hasn't already loaded it. -$(objpfx)libcrypt.so: $(common-objpfx)libc.so -$(objpfx)libmd5crypt.so: $(common-objpfx)libc.so diff --git a/md5-crypt/Versions b/md5-crypt/Versions deleted file mode 100644 index 389e7d5..0000000 --- a/md5-crypt/Versions +++ /dev/null @@ -1,5 +0,0 @@ -libcrypt { - GLIBC_2.0 { - crypt; crypt_r; encrypt; encrypt_r; fcrypt; setkey; setkey_r; - } -} diff --git a/md5-crypt/md5-crypt.c b/md5-crypt/md5-crypt.c deleted file mode 100644 index 1261035..0000000 --- a/md5-crypt/md5-crypt.c +++ /dev/null @@ -1,223 +0,0 @@ -/* One way encryption based on MD5 sum. - Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 1996. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the GNU C Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ - -#include -#include -#include -#include - -#include "md5.h" - - -/* Define our magic string to mark salt for MD5 "encryption" - replacement. This is meant to be the same as for other MD5 based - encryption implementations. */ -static const char md5_salt_prefix[] = "$1$"; - -/* Table with characters for base64 transformation. */ -static const char b64t[64] = -"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; - - -/* Prototypes for local functions. */ -extern char *__md5_crypt_r __P ((const char *key, const char *salt, - char *buffer, int buflen)); -extern char *__md5_crypt __P ((const char *key, const char *salt)); - - -/* This entry point is equivalent to the `crypt' function in Unix - libcs. */ -char * -__md5_crypt_r (key, salt, buffer, buflen) - const char *key; - const char *salt; - char *buffer; - int buflen; -{ - unsigned char alt_result[16]; - struct md5_ctx ctx; - struct md5_ctx alt_ctx; - size_t salt_len; - size_t key_len; - size_t cnt; - char *cp; - - /* Find beginning of salt string. The prefix should normally always - be present. Just in case it is not. */ - if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0) - /* Skip salt prefix. */ - salt += sizeof (md5_salt_prefix) - 1; - - salt_len = MIN (strcspn (salt, "$"), 8); - key_len = strlen (key); - - /* Prepare for the real work. */ - __md5_init_ctx (&ctx); - - /* Add the key string. */ - __md5_process_bytes (key, key_len, &ctx); - - /* Because the SALT argument need not always have the salt prefix we - add it separately. */ - __md5_process_bytes (md5_salt_prefix, sizeof (md5_salt_prefix) - 1, &ctx); - - /* The last part is the salt string. This must be at most 8 - characters and it ends at the first `$' character (for - compatibility which existing solutions). */ - __md5_process_bytes (salt, salt_len, &ctx); - - - /* Compute alternate MD5 sum with input KEY, SALT, and KEY. The - final result will be added to the first context. */ - __md5_init_ctx (&alt_ctx); - - /* Add key. */ - __md5_process_bytes (key, key_len, &alt_ctx); - - /* Add salt. */ - __md5_process_bytes (salt, salt_len, &alt_ctx); - - /* Add key again. */ - __md5_process_bytes (key, key_len, &alt_ctx); - - /* Now get result of this (16 bytes) and add it to the other - context. */ - __md5_finish_ctx (&alt_ctx, alt_result); - - /* Add for any character in the key one byte of the alternate sum. */ - for (cnt = key_len; cnt > 16; cnt -= 16) - __md5_process_bytes (alt_result, 16, &ctx); - __md5_process_bytes (alt_result, cnt, &ctx); - - /* For the following code we need a NUL byte. */ - *alt_result = '\0'; - - /* The original implementation now does something weird: for every 1 - bit in the key the first 0 is added to the buffer, for every 0 - bit the first character of the key. This does not seem to be - what was intended but we have to follow this to be compatible. */ - for (cnt = key_len; cnt > 0; cnt >>= 1) - __md5_process_bytes ((cnt & 1) != 0 ? (const char *) alt_result : key, 1, - &ctx); - - /* Create intermediate result. */ - __md5_finish_ctx (&ctx, alt_result); - - /* Now comes another weirdness. In fear of password crackers here - comes a quite long loop which just processes the output of the - previous round again. We cannot ignore this here. */ - for (cnt = 0; cnt < 1000; ++cnt) - { - /* New context. */ - __md5_init_ctx (&ctx); - - /* Add key or last result. */ - if ((cnt & 1) != 0) - __md5_process_bytes (key, key_len, &ctx); - else - __md5_process_bytes (alt_result, 16, &ctx); - - /* Add salt for numbers not divisible by 3. */ - if (cnt % 3 != 0) - __md5_process_bytes (salt, salt_len, &ctx); - - /* Add key for numbers not divisible by 7. */ - if (cnt % 7 != 0) - __md5_process_bytes (key, key_len, &ctx); - - /* Add key or last result. */ - if ((cnt & 1) != 0) - __md5_process_bytes (alt_result, 16, &ctx); - else - __md5_process_bytes (key, key_len, &ctx); - - /* Create intermediate result. */ - __md5_finish_ctx (&ctx, alt_result); - } - - /* Now we can construct the result string. It consists of three - parts. */ - cp = __stpncpy (buffer, md5_salt_prefix, MAX (0, buflen)); - buflen -= sizeof (md5_salt_prefix); - - cp = __stpncpy (cp, salt, MIN ((size_t) buflen, salt_len)); - buflen -= MIN ((size_t) buflen, salt_len); - - if (buflen > 0) - { - *cp++ = '$'; - --buflen; - } - -#define b64_from_24bit(B2, B1, B0, N) \ - do { \ - unsigned int w = ((B2) << 16) | ((B1) << 8) | (B0); \ - int n = (N); \ - while (n-- > 0 && buflen > 0) \ - { \ - *cp++ = b64t[w & 0x3f]; \ - --buflen; \ - w >>= 6; \ - } \ - } while (0) - - - b64_from_24bit (alt_result[0], alt_result[6], alt_result[12], 4); - b64_from_24bit (alt_result[1], alt_result[7], alt_result[13], 4); - b64_from_24bit (alt_result[2], alt_result[8], alt_result[14], 4); - b64_from_24bit (alt_result[3], alt_result[9], alt_result[15], 4); - b64_from_24bit (alt_result[4], alt_result[10], alt_result[5], 4); - b64_from_24bit (0, 0, alt_result[11], 2); - if (buflen <= 0) - { - __set_errno (ERANGE); - buffer = NULL; - } - else - *cp = '\0'; /* Terminate the string. */ - - /* Clear the buffer for the intermediate result so that people - attaching to processes or reading core dumps cannot get any - information. */ - memset (alt_result, '\0', sizeof (alt_result)); - - return buffer; -} - - -char * -__md5_crypt (const char *key, const char *salt) -{ - /* We don't want to have an arbitrary limit in the size of the - password. We can compute the size of the result in advance and - so we can prepare the buffer we pass to `md5_crypt_r'. */ - static char *buffer = NULL; - static int buflen = 0; - int needed = 3 + strlen (salt) + 1 + 26 + 1; - - if (buflen < needed) - { - buflen = needed; - if ((buffer = realloc (buffer, buflen)) == NULL) - return NULL; - } - - return __md5_crypt_r (key, salt, buffer, buflen); -} diff --git a/md5-crypt/md5.c b/md5-crypt/md5.c deleted file mode 100644 index 478429f..0000000 --- a/md5-crypt/md5.c +++ /dev/null @@ -1,428 +0,0 @@ -/* md5.c - Functions to compute MD5 message digest of files or memory blocks - according to the definition of MD5 in RFC 1321 from April 1992. - Copyright (C) 1995, 1996, 1997, 1999 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the GNU C Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ - -/* Written by Ulrich Drepper , 1995. */ - -#ifdef HAVE_CONFIG_H -# include -#endif - -#include - -#if STDC_HEADERS || defined _LIBC -# include -# include -#else -# ifndef HAVE_MEMCPY -# define memcpy(d, s, n) bcopy ((s), (d), (n)) -# endif -#endif - -#include "md5.h" - -#ifdef _LIBC -# include -# if __BYTE_ORDER == __BIG_ENDIAN -# define WORDS_BIGENDIAN 1 -# endif -/* We need to keep the namespace clean so define the MD5 function - protected using leading __ . */ -# define md5_init_ctx __md5_init_ctx -# define md5_process_block __md5_process_block -# define md5_process_bytes __md5_process_bytes -# define md5_finish_ctx __md5_finish_ctx -# define md5_read_ctx __md5_read_ctx -# define md5_stream __md5_stream -# define md5_buffer __md5_buffer -#endif - -#ifdef WORDS_BIGENDIAN -# define SWAP(n) \ - (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) -#else -# define SWAP(n) (n) -#endif - - -/* This array contains the bytes used to pad the buffer to the next - 64-byte boundary. (RFC 1321, 3.1: Step 1) */ -static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; - - -/* Initialize structure containing state of computation. - (RFC 1321, 3.3: Step 3) */ -void -md5_init_ctx (ctx) - struct md5_ctx *ctx; -{ - ctx->A = 0x67452301; - ctx->B = 0xefcdab89; - ctx->C = 0x98badcfe; - ctx->D = 0x10325476; - - ctx->total[0] = ctx->total[1] = 0; - ctx->buflen = 0; -} - -/* Put result from CTX in first 16 bytes following RESBUF. The result - must be in little endian byte order. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -void * -md5_read_ctx (ctx, resbuf) - const struct md5_ctx *ctx; - void *resbuf; -{ - ((md5_uint32 *) resbuf)[0] = SWAP (ctx->A); - ((md5_uint32 *) resbuf)[1] = SWAP (ctx->B); - ((md5_uint32 *) resbuf)[2] = SWAP (ctx->C); - ((md5_uint32 *) resbuf)[3] = SWAP (ctx->D); - - return resbuf; -} - -/* Process the remaining bytes in the internal buffer and the usual - prolog according to the standard and write the result to RESBUF. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -void * -md5_finish_ctx (ctx, resbuf) - struct md5_ctx *ctx; - void *resbuf; -{ - /* Take yet unprocessed bytes into account. */ - md5_uint32 bytes = ctx->buflen; - size_t pad; - - /* Now count remaining bytes. */ - ctx->total[0] += bytes; - if (ctx->total[0] < bytes) - ++ctx->total[1]; - - pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes; - memcpy (&ctx->buffer[bytes], fillbuf, pad); - - /* Put the 64-bit file length in *bits* at the end of the buffer. */ - *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); - *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) | - (ctx->total[0] >> 29)); - - /* Process last bytes. */ - md5_process_block (ctx->buffer, bytes + pad + 8, ctx); - - return md5_read_ctx (ctx, resbuf); -} - -/* Compute MD5 message digest for bytes read from STREAM. The - resulting message digest number will be written into the 16 bytes - beginning at RESBLOCK. */ -int -md5_stream (stream, resblock) - FILE *stream; - void *resblock; -{ - /* Important: BLOCKSIZE must be a multiple of 64. */ -#define BLOCKSIZE 4096 - struct md5_ctx ctx; - char buffer[BLOCKSIZE + 72]; - size_t sum; - - /* Initialize the computation context. */ - md5_init_ctx (&ctx); - - /* Iterate over full file contents. */ - while (1) - { - /* We read the file in blocks of BLOCKSIZE bytes. One call of the - computation function processes the whole buffer so that with the - next round of the loop another block can be read. */ - size_t n; - sum = 0; - - /* Read block. Take care for partial reads. */ - do - { - n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); - - sum += n; - } - while (sum < BLOCKSIZE && n != 0); - if (n == 0 && ferror (stream)) - return 1; - - /* If end of file is reached, end the loop. */ - if (n == 0) - break; - - /* Process buffer with BLOCKSIZE bytes. Note that - BLOCKSIZE % 64 == 0 - */ - md5_process_block (buffer, BLOCKSIZE, &ctx); - } - - /* Add the last bytes if necessary. */ - if (sum > 0) - md5_process_bytes (buffer, sum, &ctx); - - /* Construct result in desired memory. */ - md5_finish_ctx (&ctx, resblock); - return 0; -} - -/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The - result is always in little endian byte order, so that a byte-wise - output yields to the wanted ASCII representation of the message - digest. */ -void * -md5_buffer (buffer, len, resblock) - const char *buffer; - size_t len; - void *resblock; -{ - struct md5_ctx ctx; - - /* Initialize the computation context. */ - md5_init_ctx (&ctx); - - /* Process whole buffer but last len % 64 bytes. */ - md5_process_bytes (buffer, len, &ctx); - - /* Put result in desired memory area. */ - return md5_finish_ctx (&ctx, resblock); -} - - -void -md5_process_bytes (buffer, len, ctx) - const void *buffer; - size_t len; - struct md5_ctx *ctx; -{ - /* When we already have some bits in our internal buffer concatenate - both inputs first. */ - if (ctx->buflen != 0) - { - size_t left_over = ctx->buflen; - size_t add = 128 - left_over > len ? len : 128 - left_over; - - memcpy (&ctx->buffer[left_over], buffer, add); - ctx->buflen += add; - - if (left_over + add > 64) - { - md5_process_block (ctx->buffer, (left_over + add) & ~63, ctx); - /* The regions in the following copy operation cannot overlap. */ - memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63], - (left_over + add) & 63); - ctx->buflen = (left_over + add) & 63; - } - - buffer = (const char *) buffer + add; - len -= add; - } - - /* Process available complete blocks. */ - if (len > 64) - { - md5_process_block (buffer, len & ~63, ctx); - buffer = (const char *) buffer + (len & ~63); - len &= 63; - } - - /* Move remaining bytes in internal buffer. */ - if (len > 0) - { - memcpy (ctx->buffer, buffer, len); - ctx->buflen = len; - } -} - - -/* These are the four functions used in the four steps of the MD5 algorithm - and defined in the RFC 1321. The first function is a little bit optimized - (as found in Colin Plumbs public domain implementation). */ -/* #define FF(b, c, d) ((b & c) | (~b & d)) */ -#define FF(b, c, d) (d ^ (b & (c ^ d))) -#define FG(b, c, d) FF (d, b, c) -#define FH(b, c, d) (b ^ c ^ d) -#define FI(b, c, d) (c ^ (b | ~d)) - -/* Process LEN bytes of BUFFER, accumulating context into CTX. - It is assumed that LEN % 64 == 0. */ - -void -md5_process_block (buffer, len, ctx) - const void *buffer; - size_t len; - struct md5_ctx *ctx; -{ - md5_uint32 correct_words[16]; - const md5_uint32 *words = buffer; - size_t nwords = len / sizeof (md5_uint32); - const md5_uint32 *endp = words + nwords; - md5_uint32 A = ctx->A; - md5_uint32 B = ctx->B; - md5_uint32 C = ctx->C; - md5_uint32 D = ctx->D; - - /* First increment the byte count. RFC 1321 specifies the possible - length of the file up to 2^64 bits. Here we only compute the - number of bytes. Do a double word increment. */ - ctx->total[0] += len; - if (ctx->total[0] < len) - ++ctx->total[1]; - - /* Process all bytes in the buffer with 64 bytes in each round of - the loop. */ - while (words < endp) - { - md5_uint32 *cwp = correct_words; - md5_uint32 A_save = A; - md5_uint32 B_save = B; - md5_uint32 C_save = C; - md5_uint32 D_save = D; - - /* First round: using the given function, the context and a constant - the next context is computed. Because the algorithms processing - unit is a 32-bit word and it is determined to work on words in - little endian byte order we perhaps have to change the byte order - before the computation. To reduce the work for the next steps - we store the swapped words in the array CORRECT_WORDS. */ - -#define OP(a, b, c, d, s, T) \ - do \ - { \ - a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \ - ++words; \ - CYCLIC (a, s); \ - a += b; \ - } \ - while (0) - - /* It is unfortunate that C does not provide an operator for - cyclic rotation. Hope the C compiler is smart enough. */ -#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s))) - - /* Before we start, one word to the strange constants. - They are defined in RFC 1321 as - - T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64 - */ - - /* Round 1. */ - OP (A, B, C, D, 7, 0xd76aa478); - OP (D, A, B, C, 12, 0xe8c7b756); - OP (C, D, A, B, 17, 0x242070db); - OP (B, C, D, A, 22, 0xc1bdceee); - OP (A, B, C, D, 7, 0xf57c0faf); - OP (D, A, B, C, 12, 0x4787c62a); - OP (C, D, A, B, 17, 0xa8304613); - OP (B, C, D, A, 22, 0xfd469501); - OP (A, B, C, D, 7, 0x698098d8); - OP (D, A, B, C, 12, 0x8b44f7af); - OP (C, D, A, B, 17, 0xffff5bb1); - OP (B, C, D, A, 22, 0x895cd7be); - OP (A, B, C, D, 7, 0x6b901122); - OP (D, A, B, C, 12, 0xfd987193); - OP (C, D, A, B, 17, 0xa679438e); - OP (B, C, D, A, 22, 0x49b40821); - - /* For the second to fourth round we have the possibly swapped words - in CORRECT_WORDS. Redefine the macro to take an additional first - argument specifying the function to use. */ -#undef OP -#define OP(f, a, b, c, d, k, s, T) \ - do \ - { \ - a += f (b, c, d) + correct_words[k] + T; \ - CYCLIC (a, s); \ - a += b; \ - } \ - while (0) - - /* Round 2. */ - OP (FG, A, B, C, D, 1, 5, 0xf61e2562); - OP (FG, D, A, B, C, 6, 9, 0xc040b340); - OP (FG, C, D, A, B, 11, 14, 0x265e5a51); - OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa); - OP (FG, A, B, C, D, 5, 5, 0xd62f105d); - OP (FG, D, A, B, C, 10, 9, 0x02441453); - OP (FG, C, D, A, B, 15, 14, 0xd8a1e681); - OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8); - OP (FG, A, B, C, D, 9, 5, 0x21e1cde6); - OP (FG, D, A, B, C, 14, 9, 0xc33707d6); - OP (FG, C, D, A, B, 3, 14, 0xf4d50d87); - OP (FG, B, C, D, A, 8, 20, 0x455a14ed); - OP (FG, A, B, C, D, 13, 5, 0xa9e3e905); - OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8); - OP (FG, C, D, A, B, 7, 14, 0x676f02d9); - OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a); - - /* Round 3. */ - OP (FH, A, B, C, D, 5, 4, 0xfffa3942); - OP (FH, D, A, B, C, 8, 11, 0x8771f681); - OP (FH, C, D, A, B, 11, 16, 0x6d9d6122); - OP (FH, B, C, D, A, 14, 23, 0xfde5380c); - OP (FH, A, B, C, D, 1, 4, 0xa4beea44); - OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9); - OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60); - OP (FH, B, C, D, A, 10, 23, 0xbebfbc70); - OP (FH, A, B, C, D, 13, 4, 0x289b7ec6); - OP (FH, D, A, B, C, 0, 11, 0xeaa127fa); - OP (FH, C, D, A, B, 3, 16, 0xd4ef3085); - OP (FH, B, C, D, A, 6, 23, 0x04881d05); - OP (FH, A, B, C, D, 9, 4, 0xd9d4d039); - OP (FH, D, A, B, C, 12, 11, 0xe6db99e5); - OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8); - OP (FH, B, C, D, A, 2, 23, 0xc4ac5665); - - /* Round 4. */ - OP (FI, A, B, C, D, 0, 6, 0xf4292244); - OP (FI, D, A, B, C, 7, 10, 0x432aff97); - OP (FI, C, D, A, B, 14, 15, 0xab9423a7); - OP (FI, B, C, D, A, 5, 21, 0xfc93a039); - OP (FI, A, B, C, D, 12, 6, 0x655b59c3); - OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92); - OP (FI, C, D, A, B, 10, 15, 0xffeff47d); - OP (FI, B, C, D, A, 1, 21, 0x85845dd1); - OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f); - OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0); - OP (FI, C, D, A, B, 6, 15, 0xa3014314); - OP (FI, B, C, D, A, 13, 21, 0x4e0811a1); - OP (FI, A, B, C, D, 4, 6, 0xf7537e82); - OP (FI, D, A, B, C, 11, 10, 0xbd3af235); - OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb); - OP (FI, B, C, D, A, 9, 21, 0xeb86d391); - - /* Add the starting values of the context. */ - A += A_save; - B += B_save; - C += C_save; - D += D_save; - } - - /* Put checksum in context given as argument. */ - ctx->A = A; - ctx->B = B; - ctx->C = C; - ctx->D = D; -} diff --git a/md5-crypt/md5.h b/md5-crypt/md5.h deleted file mode 100644 index fbc2bdbf..0000000 --- a/md5-crypt/md5.h +++ /dev/null @@ -1,147 +0,0 @@ -/* Declaration of functions and data types used for MD5 sum computing - library functions. - Copyright (C) 1995, 1996, 1997, 1999 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the GNU C Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ - -#ifndef _MD5_H -#define _MD5_H 1 - -#include - -#if defined HAVE_LIMITS_H || _LIBC -# include -#endif - -/* The following contortions are an attempt to use the C preprocessor - to determine an unsigned integral type that is 32 bits wide. An - alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but - doing that would require that the configure script compile and *run* - the resulting executable. Locally running cross-compiled executables - is usually not possible. */ - -#ifdef _LIBC -# include -typedef u_int32_t md5_uint32; -#else -# if defined __STDC__ && __STDC__ -# define UINT_MAX_32_BITS 4294967295U -# else -# define UINT_MAX_32_BITS 0xFFFFFFFF -# endif - -/* If UINT_MAX isn't defined, assume it's a 32-bit type. - This should be valid for all systems GNU cares about because - that doesn't include 16-bit systems, and only modern systems - (that certainly have ) have 64+-bit integral types. */ - -# ifndef UINT_MAX -# define UINT_MAX UINT_MAX_32_BITS -# endif - -# if UINT_MAX == UINT_MAX_32_BITS - typedef unsigned int md5_uint32; -# else -# if USHRT_MAX == UINT_MAX_32_BITS - typedef unsigned short md5_uint32; -# else -# if ULONG_MAX == UINT_MAX_32_BITS - typedef unsigned long md5_uint32; -# else - /* The following line is intended to evoke an error. - Using #error is not portable enough. */ - "Cannot determine unsigned 32-bit data type." -# endif -# endif -# endif -#endif - -#undef __P -#if defined (__STDC__) && __STDC__ -# define __P(x) x -#else -# define __P(x) () -#endif - -/* Structure to save state of computation between the single steps. */ -struct md5_ctx -{ - md5_uint32 A; - md5_uint32 B; - md5_uint32 C; - md5_uint32 D; - - md5_uint32 total[2]; - md5_uint32 buflen; - char buffer[128]; -}; - -/* - * The following three functions are build up the low level used in - * the functions `md5_stream' and `md5_buffer'. - */ - -/* Initialize structure containing state of computation. - (RFC 1321, 3.3: Step 3) */ -extern void __md5_init_ctx __P ((struct md5_ctx *ctx)); - -/* Starting with the result of former calls of this function (or the - initialization function update the context for the next LEN bytes - starting at BUFFER. - It is necessary that LEN is a multiple of 64!!! */ -extern void __md5_process_block __P ((const void *buffer, size_t len, - struct md5_ctx *ctx)); - -/* Starting with the result of former calls of this function (or the - initialization function update the context for the next LEN bytes - starting at BUFFER. - It is NOT required that LEN is a multiple of 64. */ -extern void __md5_process_bytes __P ((const void *buffer, size_t len, - struct md5_ctx *ctx)); - -/* Process the remaining bytes in the buffer and put result from CTX - in first 16 bytes following RESBUF. The result is always in little - endian byte order, so that a byte-wise output yields to the wanted - ASCII representation of the message digest. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -extern void *__md5_finish_ctx __P ((struct md5_ctx *ctx, void *resbuf)); - - -/* Put result from CTX in first 16 bytes following RESBUF. The result is - always in little endian byte order, so that a byte-wise output yields - to the wanted ASCII representation of the message digest. - - IMPORTANT: On some systems it is required that RESBUF is correctly - aligned for a 32 bits value. */ -extern void *__md5_read_ctx __P ((const struct md5_ctx *ctx, void *resbuf)); - - -/* Compute MD5 message digest for bytes read from STREAM. The - resulting message digest number will be written into the 16 bytes - beginning at RESBLOCK. */ -extern int __md5_stream __P ((FILE *stream, void *resblock)); - -/* Compute MD5 message digest for LEN bytes beginning at BUFFER. The - result is always in little endian byte order, so that a byte-wise - output yields to the wanted ASCII representation of the message - digest. */ -extern void *__md5_buffer __P ((const char *buffer, size_t len, - void *resblock)); - -#endif /* md5.h */ diff --git a/md5-crypt/md5c-test.c b/md5-crypt/md5c-test.c deleted file mode 100644 index f56d0eb..0000000 --- a/md5-crypt/md5c-test.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -int -main (int argc, char *argv[]) -{ - const char salt[] = "$1$saltstring"; - char *cp; - int result = 0; - - cp = crypt ("Hello world!", salt); - result |= strcmp ("$1$saltstri$YMyguxXMBpd2TEZ.vS/3q1", cp); - - return result; -} diff --git a/md5-crypt/md5test.c b/md5-crypt/md5test.c deleted file mode 100644 index cfd9605..0000000 --- a/md5-crypt/md5test.c +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include "md5.h" - -static const struct -{ - const char *input; - const char result[16]; -} tests[] = - { - { "", - "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e" }, - { "a", - "\x0c\xc1\x75\xb9\xc0\xf1\xb6\xa8\x31\xc3\x99\xe2\x69\x77\x26\x61" }, - { "abc", - "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f\x72" }, - { "message digest", - "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0" }, - { "abcdefghijklmnopqrstuvwxyz", - "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1\x3b" }, - { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", - "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d\x9f" }, - { "123456789012345678901234567890123456789012345678901234567890" - "12345678901234567890", - "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6\x7a" } - }; - - -int -main (int argc, char *argv[]) -{ - struct md5_ctx ctx; - char sum[16]; - int result = 0; - int cnt; - - for (cnt = 0; cnt < (int) (sizeof (tests) / sizeof (tests[0])); ++cnt) - { - __md5_init_ctx (&ctx); - __md5_process_bytes (tests[cnt].input, strlen (tests[cnt].input), &ctx); - __md5_finish_ctx (&ctx, sum); - result |= memcmp (tests[cnt].result, sum, 16); - } - - return result; -} diff --git a/md5-crypt/onlymd5-entry.c b/md5-crypt/onlymd5-entry.c deleted file mode 100644 index d03fae9..0000000 --- a/md5-crypt/onlymd5-entry.c +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/sunrpc/des_impl.c b/sunrpc/des_impl.c new file mode 100644 index 0000000..f4fee72 --- /dev/null +++ b/sunrpc/des_impl.c @@ -0,0 +1,615 @@ +/* Copyright (C) 1992 Eric Young - see COPYING for more details */ +/* Collected from libdes and modified for SECURE RPC by Martin Kuck 1994 */ +#include +#include "des.h" + + +static const unsigned long des_SPtrans[8][64] = +{ + { /* nibble 0 */ + 0x00820200, 0x00020000, 0x80800000, 0x80820200, + 0x00800000, 0x80020200, 0x80020000, 0x80800000, + 0x80020200, 0x00820200, 0x00820000, 0x80000200, + 0x80800200, 0x00800000, 0x00000000, 0x80020000, + 0x00020000, 0x80000000, 0x00800200, 0x00020200, + 0x80820200, 0x00820000, 0x80000200, 0x00800200, + 0x80000000, 0x00000200, 0x00020200, 0x80820000, + 0x00000200, 0x80800200, 0x80820000, 0x00000000, + 0x00000000, 0x80820200, 0x00800200, 0x80020000, + 0x00820200, 0x00020000, 0x80000200, 0x00800200, + 0x80820000, 0x00000200, 0x00020200, 0x80800000, + 0x80020200, 0x80000000, 0x80800000, 0x00820000, + 0x80820200, 0x00020200, 0x00820000, 0x80800200, + 0x00800000, 0x80000200, 0x80020000, 0x00000000, + 0x00020000, 0x00800000, 0x80800200, 0x00820200, + 0x80000000, 0x80820000, 0x00000200, 0x80020200}, + + { /* nibble 1 */ + 0x10042004, 0x00000000, 0x00042000, 0x10040000, + 0x10000004, 0x00002004, 0x10002000, 0x00042000, + 0x00002000, 0x10040004, 0x00000004, 0x10002000, + 0x00040004, 0x10042000, 0x10040000, 0x00000004, + 0x00040000, 0x10002004, 0x10040004, 0x00002000, + 0x00042004, 0x10000000, 0x00000000, 0x00040004, + 0x10002004, 0x00042004, 0x10042000, 0x10000004, + 0x10000000, 0x00040000, 0x00002004, 0x10042004, + 0x00040004, 0x10042000, 0x10002000, 0x00042004, + 0x10042004, 0x00040004, 0x10000004, 0x00000000, + 0x10000000, 0x00002004, 0x00040000, 0x10040004, + 0x00002000, 0x10000000, 0x00042004, 0x10002004, + 0x10042000, 0x00002000, 0x00000000, 0x10000004, + 0x00000004, 0x10042004, 0x00042000, 0x10040000, + 0x10040004, 0x00040000, 0x00002004, 0x10002000, + 0x10002004, 0x00000004, 0x10040000, 0x00042000}, + + { /* nibble 2 */ + 0x41000000, 0x01010040, 0x00000040, 0x41000040, + 0x40010000, 0x01000000, 0x41000040, 0x00010040, + 0x01000040, 0x00010000, 0x01010000, 0x40000000, + 0x41010040, 0x40000040, 0x40000000, 0x41010000, + 0x00000000, 0x40010000, 0x01010040, 0x00000040, + 0x40000040, 0x41010040, 0x00010000, 0x41000000, + 0x41010000, 0x01000040, 0x40010040, 0x01010000, + 0x00010040, 0x00000000, 0x01000000, 0x40010040, + 0x01010040, 0x00000040, 0x40000000, 0x00010000, + 0x40000040, 0x40010000, 0x01010000, 0x41000040, + 0x00000000, 0x01010040, 0x00010040, 0x41010000, + 0x40010000, 0x01000000, 0x41010040, 0x40000000, + 0x40010040, 0x41000000, 0x01000000, 0x41010040, + 0x00010000, 0x01000040, 0x41000040, 0x00010040, + 0x01000040, 0x00000000, 0x41010000, 0x40000040, + 0x41000000, 0x40010040, 0x00000040, 0x01010000}, + + { /* nibble 3 */ + 0x00100402, 0x04000400, 0x00000002, 0x04100402, + 0x00000000, 0x04100000, 0x04000402, 0x00100002, + 0x04100400, 0x04000002, 0x04000000, 0x00000402, + 0x04000002, 0x00100402, 0x00100000, 0x04000000, + 0x04100002, 0x00100400, 0x00000400, 0x00000002, + 0x00100400, 0x04000402, 0x04100000, 0x00000400, + 0x00000402, 0x00000000, 0x00100002, 0x04100400, + 0x04000400, 0x04100002, 0x04100402, 0x00100000, + 0x04100002, 0x00000402, 0x00100000, 0x04000002, + 0x00100400, 0x04000400, 0x00000002, 0x04100000, + 0x04000402, 0x00000000, 0x00000400, 0x00100002, + 0x00000000, 0x04100002, 0x04100400, 0x00000400, + 0x04000000, 0x04100402, 0x00100402, 0x00100000, + 0x04100402, 0x00000002, 0x04000400, 0x00100402, + 0x00100002, 0x00100400, 0x04100000, 0x04000402, + 0x00000402, 0x04000000, 0x04000002, 0x04100400}, + + { /* nibble 4 */ + 0x02000000, 0x00004000, 0x00000100, 0x02004108, + 0x02004008, 0x02000100, 0x00004108, 0x02004000, + 0x00004000, 0x00000008, 0x02000008, 0x00004100, + 0x02000108, 0x02004008, 0x02004100, 0x00000000, + 0x00004100, 0x02000000, 0x00004008, 0x00000108, + 0x02000100, 0x00004108, 0x00000000, 0x02000008, + 0x00000008, 0x02000108, 0x02004108, 0x00004008, + 0x02004000, 0x00000100, 0x00000108, 0x02004100, + 0x02004100, 0x02000108, 0x00004008, 0x02004000, + 0x00004000, 0x00000008, 0x02000008, 0x02000100, + 0x02000000, 0x00004100, 0x02004108, 0x00000000, + 0x00004108, 0x02000000, 0x00000100, 0x00004008, + 0x02000108, 0x00000100, 0x00000000, 0x02004108, + 0x02004008, 0x02004100, 0x00000108, 0x00004000, + 0x00004100, 0x02004008, 0x02000100, 0x00000108, + 0x00000008, 0x00004108, 0x02004000, 0x02000008}, + + { /* nibble 5 */ + 0x20000010, 0x00080010, 0x00000000, 0x20080800, + 0x00080010, 0x00000800, 0x20000810, 0x00080000, + 0x00000810, 0x20080810, 0x00080800, 0x20000000, + 0x20000800, 0x20000010, 0x20080000, 0x00080810, + 0x00080000, 0x20000810, 0x20080010, 0x00000000, + 0x00000800, 0x00000010, 0x20080800, 0x20080010, + 0x20080810, 0x20080000, 0x20000000, 0x00000810, + 0x00000010, 0x00080800, 0x00080810, 0x20000800, + 0x00000810, 0x20000000, 0x20000800, 0x00080810, + 0x20080800, 0x00080010, 0x00000000, 0x20000800, + 0x20000000, 0x00000800, 0x20080010, 0x00080000, + 0x00080010, 0x20080810, 0x00080800, 0x00000010, + 0x20080810, 0x00080800, 0x00080000, 0x20000810, + 0x20000010, 0x20080000, 0x00080810, 0x00000000, + 0x00000800, 0x20000010, 0x20000810, 0x20080800, + 0x20080000, 0x00000810, 0x00000010, 0x20080010}, + + { /* nibble 6 */ + 0x00001000, 0x00000080, 0x00400080, 0x00400001, + 0x00401081, 0x00001001, 0x00001080, 0x00000000, + 0x00400000, 0x00400081, 0x00000081, 0x00401000, + 0x00000001, 0x00401080, 0x00401000, 0x00000081, + 0x00400081, 0x00001000, 0x00001001, 0x00401081, + 0x00000000, 0x00400080, 0x00400001, 0x00001080, + 0x00401001, 0x00001081, 0x00401080, 0x00000001, + 0x00001081, 0x00401001, 0x00000080, 0x00400000, + 0x00001081, 0x00401000, 0x00401001, 0x00000081, + 0x00001000, 0x00000080, 0x00400000, 0x00401001, + 0x00400081, 0x00001081, 0x00001080, 0x00000000, + 0x00000080, 0x00400001, 0x00000001, 0x00400080, + 0x00000000, 0x00400081, 0x00400080, 0x00001080, + 0x00000081, 0x00001000, 0x00401081, 0x00400000, + 0x00401080, 0x00000001, 0x00001001, 0x00401081, + 0x00400001, 0x00401080, 0x00401000, 0x00001001}, + + { /* nibble 7 */ + 0x08200020, 0x08208000, 0x00008020, 0x00000000, + 0x08008000, 0x00200020, 0x08200000, 0x08208020, + 0x00000020, 0x08000000, 0x00208000, 0x00008020, + 0x00208020, 0x08008020, 0x08000020, 0x08200000, + 0x00008000, 0x00208020, 0x00200020, 0x08008000, + 0x08208020, 0x08000020, 0x00000000, 0x00208000, + 0x08000000, 0x00200000, 0x08008020, 0x08200020, + 0x00200000, 0x00008000, 0x08208000, 0x00000020, + 0x00200000, 0x00008000, 0x08000020, 0x08208020, + 0x00008020, 0x08000000, 0x00000000, 0x00208000, + 0x08200020, 0x08008020, 0x08008000, 0x00200020, + 0x08208000, 0x00000020, 0x00200020, 0x08008000, + 0x08208020, 0x00200000, 0x08200000, 0x08000020, + 0x00208000, 0x00008020, 0x08008020, 0x08200000, + 0x00000020, 0x08208000, 0x00208020, 0x00000000, + 0x08000000, 0x08200020, 0x00008000, 0x00208020}}; + +static const unsigned long des_skb[8][64] = +{ + { /* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ + 0x00000000, 0x00000010, 0x20000000, 0x20000010, + 0x00010000, 0x00010010, 0x20010000, 0x20010010, + 0x00000800, 0x00000810, 0x20000800, 0x20000810, + 0x00010800, 0x00010810, 0x20010800, 0x20010810, + 0x00000020, 0x00000030, 0x20000020, 0x20000030, + 0x00010020, 0x00010030, 0x20010020, 0x20010030, + 0x00000820, 0x00000830, 0x20000820, 0x20000830, + 0x00010820, 0x00010830, 0x20010820, 0x20010830, + 0x00080000, 0x00080010, 0x20080000, 0x20080010, + 0x00090000, 0x00090010, 0x20090000, 0x20090010, + 0x00080800, 0x00080810, 0x20080800, 0x20080810, + 0x00090800, 0x00090810, 0x20090800, 0x20090810, + 0x00080020, 0x00080030, 0x20080020, 0x20080030, + 0x00090020, 0x00090030, 0x20090020, 0x20090030, + 0x00080820, 0x00080830, 0x20080820, 0x20080830, + 0x00090820, 0x00090830, 0x20090820, 0x20090830}, + { /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */ + 0x00000000, 0x02000000, 0x00002000, 0x02002000, + 0x00200000, 0x02200000, 0x00202000, 0x02202000, + 0x00000004, 0x02000004, 0x00002004, 0x02002004, + 0x00200004, 0x02200004, 0x00202004, 0x02202004, + 0x00000400, 0x02000400, 0x00002400, 0x02002400, + 0x00200400, 0x02200400, 0x00202400, 0x02202400, + 0x00000404, 0x02000404, 0x00002404, 0x02002404, + 0x00200404, 0x02200404, 0x00202404, 0x02202404, + 0x10000000, 0x12000000, 0x10002000, 0x12002000, + 0x10200000, 0x12200000, 0x10202000, 0x12202000, + 0x10000004, 0x12000004, 0x10002004, 0x12002004, + 0x10200004, 0x12200004, 0x10202004, 0x12202004, + 0x10000400, 0x12000400, 0x10002400, 0x12002400, + 0x10200400, 0x12200400, 0x10202400, 0x12202400, + 0x10000404, 0x12000404, 0x10002404, 0x12002404, + 0x10200404, 0x12200404, 0x10202404, 0x12202404}, + { /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */ + 0x00000000, 0x00000001, 0x00040000, 0x00040001, + 0x01000000, 0x01000001, 0x01040000, 0x01040001, + 0x00000002, 0x00000003, 0x00040002, 0x00040003, + 0x01000002, 0x01000003, 0x01040002, 0x01040003, + 0x00000200, 0x00000201, 0x00040200, 0x00040201, + 0x01000200, 0x01000201, 0x01040200, 0x01040201, + 0x00000202, 0x00000203, 0x00040202, 0x00040203, + 0x01000202, 0x01000203, 0x01040202, 0x01040203, + 0x08000000, 0x08000001, 0x08040000, 0x08040001, + 0x09000000, 0x09000001, 0x09040000, 0x09040001, + 0x08000002, 0x08000003, 0x08040002, 0x08040003, + 0x09000002, 0x09000003, 0x09040002, 0x09040003, + 0x08000200, 0x08000201, 0x08040200, 0x08040201, + 0x09000200, 0x09000201, 0x09040200, 0x09040201, + 0x08000202, 0x08000203, 0x08040202, 0x08040203, + 0x09000202, 0x09000203, 0x09040202, 0x09040203}, + { /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */ + 0x00000000, 0x00100000, 0x00000100, 0x00100100, + 0x00000008, 0x00100008, 0x00000108, 0x00100108, + 0x00001000, 0x00101000, 0x00001100, 0x00101100, + 0x00001008, 0x00101008, 0x00001108, 0x00101108, + 0x04000000, 0x04100000, 0x04000100, 0x04100100, + 0x04000008, 0x04100008, 0x04000108, 0x04100108, + 0x04001000, 0x04101000, 0x04001100, 0x04101100, + 0x04001008, 0x04101008, 0x04001108, 0x04101108, + 0x00020000, 0x00120000, 0x00020100, 0x00120100, + 0x00020008, 0x00120008, 0x00020108, 0x00120108, + 0x00021000, 0x00121000, 0x00021100, 0x00121100, + 0x00021008, 0x00121008, 0x00021108, 0x00121108, + 0x04020000, 0x04120000, 0x04020100, 0x04120100, + 0x04020008, 0x04120008, 0x04020108, 0x04120108, + 0x04021000, 0x04121000, 0x04021100, 0x04121100, + 0x04021008, 0x04121008, 0x04021108, 0x04121108}, + { /* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */ + 0x00000000, 0x10000000, 0x00010000, 0x10010000, + 0x00000004, 0x10000004, 0x00010004, 0x10010004, + 0x20000000, 0x30000000, 0x20010000, 0x30010000, + 0x20000004, 0x30000004, 0x20010004, 0x30010004, + 0x00100000, 0x10100000, 0x00110000, 0x10110000, + 0x00100004, 0x10100004, 0x00110004, 0x10110004, + 0x20100000, 0x30100000, 0x20110000, 0x30110000, + 0x20100004, 0x30100004, 0x20110004, 0x30110004, + 0x00001000, 0x10001000, 0x00011000, 0x10011000, + 0x00001004, 0x10001004, 0x00011004, 0x10011004, + 0x20001000, 0x30001000, 0x20011000, 0x30011000, + 0x20001004, 0x30001004, 0x20011004, 0x30011004, + 0x00101000, 0x10101000, 0x00111000, 0x10111000, + 0x00101004, 0x10101004, 0x00111004, 0x10111004, + 0x20101000, 0x30101000, 0x20111000, 0x30111000, + 0x20101004, 0x30101004, 0x20111004, 0x30111004}, + { /* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */ + 0x00000000, 0x08000000, 0x00000008, 0x08000008, + 0x00000400, 0x08000400, 0x00000408, 0x08000408, + 0x00020000, 0x08020000, 0x00020008, 0x08020008, + 0x00020400, 0x08020400, 0x00020408, 0x08020408, + 0x00000001, 0x08000001, 0x00000009, 0x08000009, + 0x00000401, 0x08000401, 0x00000409, 0x08000409, + 0x00020001, 0x08020001, 0x00020009, 0x08020009, + 0x00020401, 0x08020401, 0x00020409, 0x08020409, + 0x02000000, 0x0A000000, 0x02000008, 0x0A000008, + 0x02000400, 0x0A000400, 0x02000408, 0x0A000408, + 0x02020000, 0x0A020000, 0x02020008, 0x0A020008, + 0x02020400, 0x0A020400, 0x02020408, 0x0A020408, + 0x02000001, 0x0A000001, 0x02000009, 0x0A000009, + 0x02000401, 0x0A000401, 0x02000409, 0x0A000409, + 0x02020001, 0x0A020001, 0x02020009, 0x0A020009, + 0x02020401, 0x0A020401, 0x02020409, 0x0A020409}, + { /* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */ + 0x00000000, 0x00000100, 0x00080000, 0x00080100, + 0x01000000, 0x01000100, 0x01080000, 0x01080100, + 0x00000010, 0x00000110, 0x00080010, 0x00080110, + 0x01000010, 0x01000110, 0x01080010, 0x01080110, + 0x00200000, 0x00200100, 0x00280000, 0x00280100, + 0x01200000, 0x01200100, 0x01280000, 0x01280100, + 0x00200010, 0x00200110, 0x00280010, 0x00280110, + 0x01200010, 0x01200110, 0x01280010, 0x01280110, + 0x00000200, 0x00000300, 0x00080200, 0x00080300, + 0x01000200, 0x01000300, 0x01080200, 0x01080300, + 0x00000210, 0x00000310, 0x00080210, 0x00080310, + 0x01000210, 0x01000310, 0x01080210, 0x01080310, + 0x00200200, 0x00200300, 0x00280200, 0x00280300, + 0x01200200, 0x01200300, 0x01280200, 0x01280300, + 0x00200210, 0x00200310, 0x00280210, 0x00280310, + 0x01200210, 0x01200310, 0x01280210, 0x01280310}, + { /* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */ + 0x00000000, 0x04000000, 0x00040000, 0x04040000, + 0x00000002, 0x04000002, 0x00040002, 0x04040002, + 0x00002000, 0x04002000, 0x00042000, 0x04042000, + 0x00002002, 0x04002002, 0x00042002, 0x04042002, + 0x00000020, 0x04000020, 0x00040020, 0x04040020, + 0x00000022, 0x04000022, 0x00040022, 0x04040022, + 0x00002020, 0x04002020, 0x00042020, 0x04042020, + 0x00002022, 0x04002022, 0x00042022, 0x04042022, + 0x00000800, 0x04000800, 0x00040800, 0x04040800, + 0x00000802, 0x04000802, 0x00040802, 0x04040802, + 0x00002800, 0x04002800, 0x00042800, 0x04042800, + 0x00002802, 0x04002802, 0x00042802, 0x04042802, + 0x00000820, 0x04000820, 0x00040820, 0x04040820, + 0x00000822, 0x04000822, 0x00040822, 0x04040822, + 0x00002820, 0x04002820, 0x00042820, 0x04042820, + 0x00002822, 0x04002822, 0x00042822, 0x04042822}, +}; + +#define c2l(c,l) (l =((unsigned long)(*((c)++))) , \ + l|=((unsigned long)(*((c)++)))<< 8, \ + l|=((unsigned long)(*((c)++)))<<16, \ + l|=((unsigned long)(*((c)++)))<<24) + +#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ + *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ + *((c)++)=(unsigned char)(((l)>>16)&0xff), \ + *((c)++)=(unsigned char)(((l)>>24)&0xff)) + +/* + * IP and FP + * The problem is more of a geometric problem that random bit fiddling. + * 0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6 + * 8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4 + * 16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2 + * 24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0 + * + * 32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7 + * 40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5 + * 48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3 + * 56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1 + * + * The output has been subject to swaps of the form + * 0 1 -> 3 1 but the odd and even bits have been put into + * 2 3 2 0 + * different words. The main trick is to remember that + * t=((l>>size)^r)&(mask); + * r^=t; + * l^=(t<>(n))^(b))&(m)),\ + (b)^=(t),\ + (a)^=((t)<<(n))) + +#define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\ + (a)=(a)^(t)^(t>>(16-(n)))) + + +/* The changes to this macro may help or hinder, depending on the + * compiler and the achitecture. gcc2 always seems to do well :-). + * Inspired by Dana How + * DO NOT use the alternative version on machines with 8 byte longs. + */ +#ifdef ALT_ECB +#define D_ENCRYPT(L,R,S) \ + u=((R^s[S ])<<2); \ + t= R^s[S+1]; \ + t=((t>>2)+(t<<30)); \ + L^= \ + *(const unsigned long *)(des_SP+0x0100+((t )&0xfc))+ \ + *(const unsigned long *)(des_SP+0x0300+((t>> 8)&0xfc))+ \ + *(const unsigned long *)(des_SP+0x0500+((t>>16)&0xfc))+ \ + *(const unsigned long *)(des_SP+0x0700+((t>>24)&0xfc))+ \ + *(const unsigned long *)(des_SP+ ((u )&0xfc))+ \ + *(const unsigned long *)(des_SP+0x0200+((u>> 8)&0xfc))+ \ + *(const unsigned long *)(des_SP+0x0400+((u>>16)&0xfc))+ \ + *(const unsigned long *)(des_SP+0x0600+((u>>24)&0xfc)); +#else /* original version */ +#define D_ENCRYPT(L,R,S) \ + u=(R^s[S ]); \ + t=R^s[S+1]; \ + t=((t>>4)+(t<<28)); \ + L^= des_SPtrans[1][(t )&0x3f]| \ + des_SPtrans[3][(t>> 8)&0x3f]| \ + des_SPtrans[5][(t>>16)&0x3f]| \ + des_SPtrans[7][(t>>24)&0x3f]| \ + des_SPtrans[0][(u )&0x3f]| \ + des_SPtrans[2][(u>> 8)&0x3f]| \ + des_SPtrans[4][(u>>16)&0x3f]| \ + des_SPtrans[6][(u>>24)&0x3f]; +#endif + +#define ITERATIONS 16 + +static const char shifts2[16] = +{0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0}; + +static void des_set_key (char *, unsigned long *) internal_function; +static void des_encrypt (unsigned long *, unsigned long *, int) + internal_function; +int _des_crypt (char *, unsigned, struct desparams *); + +static void +internal_function +des_set_key (char *key, unsigned long *schedule) +{ + register unsigned long c, d, t, s; + register unsigned char *in; + register unsigned long *k; + register int i; + + k = (unsigned long *) schedule; + in = (unsigned char *) key; + + c2l (in, c); + c2l (in, d); + + /* I now do it in 47 simple operations :-) + * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov) + * for the inspiration. :-) */ + PERM_OP (d, c, t, 4, 0x0f0f0f0f); + HPERM_OP (c, t, -2, 0xcccc0000); + HPERM_OP (d, t, -2, 0xcccc0000); + PERM_OP (d, c, t, 1, 0x55555555); + PERM_OP (c, d, t, 8, 0x00ff00ff); + PERM_OP (d, c, t, 1, 0x55555555); + d = (((d & 0x000000ff) << 16) | (d & 0x0000ff00) | + ((d & 0x00ff0000) >> 16) | ((c & 0xf0000000) >> 4)); + c &= 0x0fffffff; + + for (i = 0; i < ITERATIONS; i++) + { + if (shifts2[i]) + { + c = ((c >> 2) | (c << 26)); + d = ((d >> 2) | (d << 26)); + } + else + { + c = ((c >> 1) | (c << 27)); + d = ((d >> 1) | (d << 27)); + } + c &= 0x0fffffff; + d &= 0x0fffffff; + /* could be a few less shifts but I am to lazy at this + * point in time to investigate */ + s = des_skb[0][(c) & 0x3f] | + des_skb[1][((c >> 6) & 0x03) | ((c >> 7) & 0x3c)] | + des_skb[2][((c >> 13) & 0x0f) | ((c >> 14) & 0x30)] | + des_skb[3][((c >> 20) & 0x01) | ((c >> 21) & 0x06) | ((c >> 22) & 0x38)]; + t = des_skb[4][(d) & 0x3f] | + des_skb[5][((d >> 7) & 0x03) | ((d >> 8) & 0x3c)] | + des_skb[6][(d >> 15) & 0x3f] | + des_skb[7][((d >> 21) & 0x0f) | ((d >> 22) & 0x30)]; + + /* table contained 0213 4657 */ + *(k++) = ((t << 16) | (s & 0x0000ffff)) & 0xffffffff; + s = ((s >> 16) | (t & 0xffff0000)); + + s = (s << 4) | (s >> 28); + *(k++) = s & 0xffffffff; + } +} + + +static void +internal_function +des_encrypt (unsigned long *buf, unsigned long *schedule, int encrypt) +{ + register unsigned long l, r, t, u; +#ifdef ALT_ECB + register const unsigned char *des_SP = (const unsigned char *) des_SPtrans; +#endif + register int i; + register unsigned long *s; + + l = buf[0]; + r = buf[1]; + + /* do IP */ + PERM_OP (r, l, t, 4, 0x0f0f0f0f); + PERM_OP (l, r, t, 16, 0x0000ffff); + PERM_OP (r, l, t, 2, 0x33333333); + PERM_OP (l, r, t, 8, 0x00ff00ff); + PERM_OP (r, l, t, 1, 0x55555555); + /* r and l are reversed - remember that :-) - fix + * it in the next step */ + + /* Things have been modified so that the initial rotate is + * done outside the loop. This required the + * des_SPtrans values in sp.h to be rotated 1 bit to the right. + * One perl script later and things have a 5% speed up on a sparc2. + * Thanks to Richard Outerbridge <71755.204@CompuServe.COM> + * for pointing this out. */ + t = (r << 1) | (r >> 31); + r = (l << 1) | (l >> 31); + l = t; + + /* clear the top bits on machines with 8byte longs */ + l &= 0xffffffff; + r &= 0xffffffff; + + s = (unsigned long *) schedule; + /* I don't know if it is worth the effort of loop unrolling the + * inner loop */ + if (encrypt) + { + for (i = 0; i < 32; i += 4) + { + D_ENCRYPT (l, r, i + 0); /* 1 */ + D_ENCRYPT (r, l, i + 2); /* 2 */ + } + } + else + { + for (i = 30; i > 0; i -= 4) + { + D_ENCRYPT (l, r, i - 0); /* 16 */ + D_ENCRYPT (r, l, i - 2); /* 15 */ + } + } + l = (l >> 1) | (l << 31); + r = (r >> 1) | (r << 31); + /* clear the top bits on machines with 8byte longs */ + l &= 0xffffffff; + r &= 0xffffffff; + + /* swap l and r + * we will not do the swap so just remember they are + * reversed for the rest of the subroutine + * luckily FP fixes this problem :-) */ + + PERM_OP (r, l, t, 1, 0x55555555); + PERM_OP (l, r, t, 8, 0x00ff00ff); + PERM_OP (r, l, t, 2, 0x33333333); + PERM_OP (l, r, t, 16, 0x0000ffff); + PERM_OP (r, l, t, 4, 0x0f0f0f0f); + + buf[0] = l; + buf[1] = r; + + l = r = t = u = 0; +} + + +int +_des_crypt (char *buf, unsigned len, struct desparams *desp) +{ + unsigned long schedule[32]; + register unsigned long tin0, tin1; + register unsigned long tout0, tout1, xor0, xor1; + register unsigned char *in, *out; + unsigned long tbuf[2]; + unsigned char *iv, *oiv; + int cbc_mode; + + cbc_mode = (desp->des_mode == CBC) ? 1 : 0; + + in = (unsigned char *) buf; + out = (unsigned char *) buf; + oiv = iv = (unsigned char *) desp->des_ivec; + + des_set_key (desp->des_key, schedule); + + tin0 = tin1 = 0; /* For GCC */ + if (desp->des_dir == ENCRYPT) + { + c2l (iv, tout0); + c2l (iv, tout1); + for (; len > 0; len -= 8) + { + c2l (in, tin0); + c2l (in, tin1); + if (cbc_mode) + { + tin0 ^= tout0; + tin1 ^= tout1; + } + tbuf[0] = tin0; + tbuf[1] = tin1; + des_encrypt (tbuf, schedule, 1); + tout0 = tbuf[0]; + tout1 = tbuf[1]; + l2c (tout0, out); + l2c (tout1, out); + } + l2c (tout0, oiv); + l2c (tout1, oiv); + } + else + { + c2l (iv, xor0); + c2l (iv, xor1); + for (; len > 0; len -= 8) + { + c2l (in, tin0); + c2l (in, tin1); + tbuf[0] = tin0; + tbuf[1] = tin1; + des_encrypt (tbuf, schedule, 0); + if (cbc_mode) + { + tout0 = tbuf[0] ^ xor0; + tout1 = tbuf[1] ^ xor1; + xor0 = tin0; + xor1 = tin1; + } + else + { + tout0 = tbuf[0]; + tout1 = tbuf[1]; + } + l2c (tout0, out); + l2c (tout1, out); + } + l2c (tin0, oiv); + l2c (tin1, oiv); + } + tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; + tbuf[0] = tbuf[1] = 0; + __bzero (schedule, sizeof (schedule)); + + return (1); +} diff --git a/sysdeps/generic/crypt-entry.c b/sysdeps/generic/crypt-entry.c deleted file mode 100644 index 8248ed9..0000000 --- a/sysdeps/generic/crypt-entry.c +++ /dev/null @@ -1,72 +0,0 @@ -/* Wrapper around MD5 sum replacement for crypt function. - Copyright (C) 1996, 1997, 1998 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Ulrich Drepper , 1996. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the GNU C Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ - -#include -#include -#include - - -/* Define our magic string to mark salt for MD5 encryption - replacement. This is meant to be the same as for other MD5 based - encryption implementations. */ -static const char md5_salt_prefix[] = "$1$"; - - -/* Prototypes for the MD5 encryption replacement functions. */ -extern char *__md5_crypt_r (const char *key, const char *salt, char *buffer, - int buflen); -extern char *__md5_crypt (const char *key, const char *salt); - -extern char *__crypt_r (const char *key, const char *salt, - struct crypt_data *__restrict data); - -/* We recognize an intended call of the MD5 crypt replacement function - by the first 3 characters of the salt string. If they match the - MD5 magic string we want MD5 encryption replacement. */ -char * -__crypt_r (key, salt, data) - const char *key; - const char *salt; - struct crypt_data *__restrict data; -{ - if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0) - return __md5_crypt_r (key, salt, (char *) data, - sizeof (struct crypt_data)); - - /* We don't have DES encryption. */ - __set_errno (EOPNOTSUPP); - return NULL; -} -weak_alias (__crypt_r, crypt_r) - - -/* The same here, only we call the non-reentrant version. */ -char * -crypt (key, salt) - const char *key; - const char *salt; -{ - if (strncmp (md5_salt_prefix, salt, sizeof (md5_salt_prefix) - 1) == 0) - return __md5_crypt (key, salt); - - /* We don't have DES encryption. */ - __set_errno (EOPNOTSUPP); - return NULL; -} diff --git a/sysdeps/generic/crypt.h b/sysdeps/generic/crypt.h deleted file mode 100644 index 0713e95..0000000 --- a/sysdeps/generic/crypt.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * UFC-crypt: ultra fast crypt(3) implementation - * - * Copyright (C) 1991, 92, 93, 96, 97, 98 Free Software Foundation, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with the GNU C Library; see the file COPYING.LIB. If not, - * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - * Boston, MA 02111-1307, USA. - * - * @(#)crypt.h 1.3 16 Sep 1996 - * - */ - -#ifndef _CRYPT_H - -#define _CRYPT_H 1 -#include - -__BEGIN_DECLS - -/* Encrypt at most 8 characters from KEY using salt to perturb DES. */ -extern char *crypt __P ((__const char *__key, __const char *__salt)); - - -#ifdef __USE_GNU -/* Reentrant versions of the functions above. The additional argument - points to a structure where the results are placed in. */ -struct crypt_data - { - char keysched[16 * 8]; - char sb0[32768]; - char sb1[32768]; - char sb2[32768]; - char sb3[32768]; - /* end-of-alignment-critical-data */ - char crypt_3_buf[14]; - char current_salt[2]; - long int current_saltbits; - int direction, initialized; - }; - -extern char *crypt_r __P ((__const char *__key, __const char *__salt, - struct crypt_data *__restrict __data)); -#endif - -__END_DECLS - -#endif /* crypt.h */ diff --git a/sysdeps/generic/des_impl.c b/sysdeps/generic/des_impl.c deleted file mode 100644 index f801a84..0000000 --- a/sysdeps/generic/des_impl.c +++ /dev/null @@ -1,30 +0,0 @@ -/* Copyright (C) 1997 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Library General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Library General Public License for more details. - - You should have received a copy of the GNU Library General Public - License along with the GNU C Library; see the file COPYING.LIB. If not, - write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ - -/* We must not distribute the DES implementation as part of the glibc. - So we have to provide a dummy version here. */ -#include - -int -_des_crypt (char *buf, unsigned len, struct desparams *desp) -{ - return 0; -} - -stub_warning (_des_crypt) -#include -- cgit v1.1