summaryrefslogtreecommitdiff
path: root/libc/misc
diff options
context:
space:
mode:
authorRobert de Bath <rdebath@poboxes.com>1998-02-01 11:26:21 +0100
committerLubomir Rintel <lkundrak@v3.sk>2013-10-23 23:40:14 +0200
commit48f0b3eb836162d41622cedc1eb5f5168168fb8e (patch)
treec53156383d4682a0a296f6611575dbc1d64d1881 /libc/misc
parent48798bf2eb93ec3b99720ac2e16093441156653d (diff)
downloaddev86-48f0b3eb836162d41622cedc1eb5f5168168fb8e.tar.gz
Import Dev86src-0.13.5.tar.gzv0.13.5
Diffstat (limited to 'libc/misc')
-rw-r--r--libc/misc/Makefile2
-rw-r--r--libc/misc/crypt.c45
-rw-r--r--libc/misc/tmpnam.c50
3 files changed, 85 insertions, 12 deletions
diff --git a/libc/misc/Makefile b/libc/misc/Makefile
index 50d31b7..6634f18 100644
--- a/libc/misc/Makefile
+++ b/libc/misc/Makefile
@@ -12,7 +12,7 @@ GOBJ=atoi.o atol.o ltoa.o ltostr.o \
ctype.o qsort.o bsearch.o rand.o lsearch.o getopt.o \
itoa.o cputype.o strtol.o crypt.o
-UOBJ=getenv.o putenv.o popen.o system.o setenv.o getcwd.o
+UOBJ=getenv.o putenv.o popen.o system.o setenv.o getcwd.o tmpnam.o
SSRC=syslib.c
SOBJ=time.o abort.o wait.o wait3.o waitpid.o killpg.o setpgrp.o sleep.o \
diff --git a/libc/misc/crypt.c b/libc/misc/crypt.c
index 906dea2..db69325 100644
--- a/libc/misc/crypt.c
+++ b/libc/misc/crypt.c
@@ -1,42 +1,65 @@
-#include <features.h>
-#include <stdlib.h>
/* TEA based crypt(), version 0.0 <ndf@linux.mit.edu>
* It looks like there are problems with key bits carrying through
* to the encryted data, and I want to get rid of that libc call..
* This is just so rob could see it ;) */
+
+/*
+ * I've:
+ * Compared the TEA implementation to a reference source - OK
+ * Noted the cycles count at 64 is twice the suggested value.
+ * Changed the types of 'n' and 'i' for better code with bcc.
+ * Removed a possible overrun of rkey by looping the values, it's now
+ * possible to _choose_ every bit of the 128bit PW with a 32 character word.
+ * Corrected the output transformation, it lost bits between words.
+ * Cleaned out all trace of the uncrypted PW from rkey.
+ *
+ * RDB.
+ */
+
+#include <stdlib.h>
+
char *
crypt(const char * key, const char * salt)
{
- /* n is the number of rounds, delta is a golden # derivative,
- k is the key, v is the data to be encrypted. */
- unsigned long v[2], sum=0, delta=0x9e3779b9, n=64, k[4];
+ /* n is the number of cycles (2 rounds/cycle),
+ delta is a golden # derivative,
+ k is the key, v is the data to be encrypted. */
+
+ unsigned long v[2], sum=0, delta=0x9e3779b9, k[4];
+ int n=64, i, j;
static char rkey[16];
- unsigned char i;
/* Our constant string will be a string of zeros .. */
v[0]=v[1]=k[0]=k[1]=k[2]=k[3]=0;
for(i=0;i<16;i++) rkey[i]=0;
+
rkey[0]=*salt;
rkey[1]=salt[1];
- for (i=0;key[i];i++) rkey[i+1]=key[i];
+ for (j=2,i=0;key[i];i++,j=((j+1)&15))
+ rkey[j]=(rkey[j]<<4)+(rkey[j]>>4)+ key[i];
+
memcpy(k, rkey, 4*sizeof(long));
- while (n-->0) {
+ while (n-->0) {
sum += delta;
v[0] += (v[1]<<4)+k[0] ^ v[1]+sum ^ (v[1]>>5)+k[1];
v[1] += (v[0]<<4)+k[2] ^ v[0]+sum ^ (v[0]>>5)+k[3];
}
+ /* Remove any trace of key */
+ for(i=0;i<16;i++) rkey[i]=0;
*rkey=*salt; rkey[1]=salt[1];
/* Now we need to unpack the bits and map it to "A-Za-z0-9./" for printing
in /etc/passwd */
+ sum=v[0];
for (i=2;i<13;i++)
{
/* This unpacks the 6 bit data, each cluster into its own byte */
- if (i==8) v[0]|=v[1]>>28;
- rkey[i]=v[(i-2)/6]&0x3F;
- v[(i-2)/6]>>=6;
+ rkey[i]=(sum&0x3F);
+ sum>>=6;
+ if(i==0+2) sum |= (v[1]<<26);
+ if(i==5+2) sum |= (v[1]>>4);
/* Now we map to the proper chars */
if (rkey[i]>=0 && rkey[i]<12) rkey[i]+=46;
diff --git a/libc/misc/tmpnam.c b/libc/misc/tmpnam.c
new file mode 100644
index 0000000..5bb72c5
--- /dev/null
+++ b/libc/misc/tmpnam.c
@@ -0,0 +1,50 @@
+/*
+ * (C) Shane Kerr <kerr@wizard.net> under terms of LGPL
+ */
+
+#include <unistd.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#ifndef P_tmpdir
+#define P_tmpdir "/tmp"
+#endif
+
+#ifndef L_tmpnam
+#define L_tmpnam 20
+#endif
+
+char *tmpnam(s)
+char *s;
+{
+ static char ret_val[L_tmpnam];
+ static char c1 = 0;
+ static char c2 = 0;
+ static char c3 = 0;
+ static char uniq_ch[] =
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+ struct stat stbuf;
+
+ do {
+ sprintf(ret_val, "%s/%05d%c%c%c", P_tmpdir, getpid(),
+ uniq_ch[c1], uniq_ch[c2], uniq_ch[c3]);
+ if (++c1 >= 62) {
+ c1 = 0;
+ if (++c2 >= 62) {
+ c2 = 0;
+ if (++c3 >= 62) {
+ errno = EEXIST;
+ return 0;
+ }
+ }
+ }
+ } while (stat(ret_val, &stbuf) == 0);
+
+ if (s != 0) {
+ strcpy(s, ret_val);
+ return s;
+ } else {
+ return ret_val;
+ }
+}
+