1994-05-21 09:45:58 +04:00
|
|
|
/* S/KEY v1.1b (skeysubr.c)
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Neil M. Haller <nmh@thumper.bellcore.com>
|
|
|
|
* Philip R. Karn <karn@chicago.qualcomm.com>
|
|
|
|
* John S. Walden <jsw@thumper.bellcore.com>
|
|
|
|
*
|
|
|
|
* Modifications:
|
|
|
|
* Scott Chasin <chasin@crimelab.com>
|
|
|
|
*
|
|
|
|
* S/KEY misc routines.
|
|
|
|
*
|
1995-06-20 02:47:37 +04:00
|
|
|
* $Id: skeysubr.c,v 1.5 1995/06/19 22:47:37 jtc Exp $
|
1994-05-21 09:45:58 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
1995-06-20 02:47:37 +04:00
|
|
|
#include <termios.h>
|
1994-05-21 09:45:58 +04:00
|
|
|
|
|
|
|
#include "md4.h"
|
|
|
|
#include "skey.h"
|
|
|
|
|
1994-05-24 10:25:27 +04:00
|
|
|
struct termios newtty;
|
|
|
|
struct termios oldtty;
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
static void trapped __ARGS((int sig));
|
|
|
|
static void set_term __ARGS((void));
|
|
|
|
static void unset_term __ARGS((void));
|
|
|
|
static void echo_off __ARGS((void));
|
1994-05-21 09:45:58 +04:00
|
|
|
|
|
|
|
/* Crunch a key:
|
|
|
|
* concatenate the seed and the password, run through MD4 and
|
|
|
|
* collapse to 64 bits. This is defined as the user's starting key.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
keycrunch(result,seed,passwd)
|
|
|
|
char *result; /* 8-byte result */
|
|
|
|
char *seed; /* Seed, any length */
|
|
|
|
char *passwd; /* Password, any length */
|
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
MDstruct md;
|
|
|
|
unsigned int buflen;
|
|
|
|
int i;
|
|
|
|
register long tmp;
|
|
|
|
|
|
|
|
buflen = strlen(seed) + strlen(passwd);
|
|
|
|
if ((buf = (char *)malloc(buflen+1)) == NULL)
|
|
|
|
return -1;
|
|
|
|
strcpy(buf,seed);
|
|
|
|
strcat(buf,passwd);
|
|
|
|
|
|
|
|
/* Crunch the key through MD4 */
|
|
|
|
sevenbit(buf);
|
|
|
|
MDbegin(&md);
|
|
|
|
MDupdate(&md,(unsigned char *)buf,8*buflen);
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
/* Fold result from 128 to 64 bits */
|
|
|
|
md.buffer[0] ^= md.buffer[2];
|
|
|
|
md.buffer[1] ^= md.buffer[3];
|
|
|
|
|
|
|
|
/* Default (but slow) code that will convert to
|
|
|
|
* little-endian byte ordering on any machine
|
|
|
|
*/
|
|
|
|
for (i=0; i<2; i++) {
|
|
|
|
tmp = md.buffer[i];
|
|
|
|
*result++ = tmp;
|
|
|
|
tmp >>= 8;
|
|
|
|
*result++ = tmp;
|
|
|
|
tmp >>= 8;
|
|
|
|
*result++ = tmp;
|
|
|
|
tmp >>= 8;
|
|
|
|
*result++ = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The one-way function f(). Takes 8 bytes and returns 8 bytes in place */
|
1995-06-05 23:48:33 +04:00
|
|
|
void
|
|
|
|
f(x)
|
|
|
|
char *x;
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
|
|
|
MDstruct md;
|
|
|
|
register long tmp;
|
|
|
|
|
|
|
|
MDbegin(&md);
|
|
|
|
MDupdate(&md,(unsigned char *)x,64);
|
|
|
|
|
|
|
|
/* Fold 128 to 64 bits */
|
|
|
|
md.buffer[0] ^= md.buffer[2];
|
|
|
|
md.buffer[1] ^= md.buffer[3];
|
|
|
|
|
|
|
|
/* Default (but slow) code that will convert to
|
|
|
|
* little-endian byte ordering on any machine
|
|
|
|
*/
|
|
|
|
tmp = md.buffer[0];
|
|
|
|
*x++ = tmp;
|
|
|
|
tmp >>= 8;
|
|
|
|
*x++ = tmp;
|
|
|
|
tmp >>= 8;
|
|
|
|
*x++ = tmp;
|
|
|
|
tmp >>= 8;
|
|
|
|
*x++ = tmp;
|
|
|
|
|
|
|
|
tmp = md.buffer[1];
|
|
|
|
*x++ = tmp;
|
|
|
|
tmp >>= 8;
|
|
|
|
*x++ = tmp;
|
|
|
|
tmp >>= 8;
|
|
|
|
*x++ = tmp;
|
|
|
|
tmp >>= 8;
|
|
|
|
*x = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Strip trailing cr/lf from a line of text */
|
1995-06-05 23:48:33 +04:00
|
|
|
void
|
|
|
|
rip(buf)
|
|
|
|
char *buf;
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
|
|
|
char *cp;
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
if ((cp = strchr(buf,'\r')) != NULL)
|
1994-05-21 09:45:58 +04:00
|
|
|
*cp = '\0';
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
if ((cp = strchr(buf,'\n')) != NULL)
|
1994-05-21 09:45:58 +04:00
|
|
|
*cp = '\0';
|
|
|
|
}
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
char *
|
|
|
|
readpass (buf,n)
|
|
|
|
char *buf;
|
|
|
|
int n;
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
1995-06-05 23:48:33 +04:00
|
|
|
set_term();
|
|
|
|
echo_off();
|
1994-05-21 09:45:58 +04:00
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
fgets(buf, n, stdin);
|
1994-05-21 09:45:58 +04:00
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
rip(buf);
|
|
|
|
printf("\n");
|
1994-05-21 09:45:58 +04:00
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
sevenbit(buf);
|
1994-05-21 09:45:58 +04:00
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
unset_term();
|
|
|
|
return buf;
|
1994-05-21 09:45:58 +04:00
|
|
|
}
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
char *
|
|
|
|
readskey(buf, n)
|
|
|
|
char *buf;
|
|
|
|
int n;
|
1995-05-18 00:24:39 +04:00
|
|
|
{
|
1995-06-05 23:48:33 +04:00
|
|
|
fgets (buf, n, stdin);
|
1995-05-18 00:24:39 +04:00
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
rip(buf);
|
|
|
|
printf ("\n");
|
1995-05-18 00:24:39 +04:00
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
sevenbit (buf);
|
1995-05-18 00:24:39 +04:00
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
return buf;
|
1995-05-18 00:24:39 +04:00
|
|
|
}
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
static void
|
|
|
|
set_term()
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
1995-06-05 23:48:33 +04:00
|
|
|
fflush(stdout);
|
|
|
|
tcgetattr(fileno(stdin), &newtty);
|
|
|
|
tcgetattr(fileno(stdin), &oldtty);
|
1994-05-21 09:45:58 +04:00
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
signal (SIGINT, trapped);
|
1994-05-21 09:45:58 +04:00
|
|
|
}
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
static void
|
|
|
|
echo_off()
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
1995-06-05 23:48:33 +04:00
|
|
|
newtty.c_lflag &= ~(ICANON | ECHO | ECHONL);
|
|
|
|
newtty.c_cc[VMIN] = 1;
|
|
|
|
newtty.c_cc[VTIME] = 0;
|
|
|
|
newtty.c_cc[VINTR] = 3;
|
1994-05-21 09:45:58 +04:00
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
tcsetattr(fileno(stdin), TCSADRAIN, &newtty);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
unset_term()
|
|
|
|
{
|
|
|
|
tcsetattr(fileno(stdin), TCSADRAIN, &oldtty);
|
1994-05-21 09:45:58 +04:00
|
|
|
}
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
static void
|
|
|
|
trapped(sig)
|
|
|
|
int sig;
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
1995-06-05 23:48:33 +04:00
|
|
|
signal(SIGINT, trapped);
|
|
|
|
printf("^C\n");
|
|
|
|
unset_term();
|
|
|
|
exit(-1);
|
1994-05-21 09:45:58 +04:00
|
|
|
}
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
/* Convert 8-byte hex-ascii string to binary array
|
|
|
|
* Returns 0 on success, -1 on error
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
atob8(out, in)
|
|
|
|
register char *out, *in;
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register int val;
|
|
|
|
|
|
|
|
if (in == NULL || out == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i=0; i<8; i++) {
|
|
|
|
if ((in = skipspace(in)) == NULL)
|
|
|
|
return -1;
|
|
|
|
if ((val = htoi(*in++)) == -1)
|
|
|
|
return -1;
|
|
|
|
*out = val << 4;
|
|
|
|
|
|
|
|
if ((in = skipspace(in)) == NULL)
|
|
|
|
return -1;
|
|
|
|
if ((val = htoi(*in++)) == -1)
|
|
|
|
return -1;
|
|
|
|
*out++ |= val;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert 8-byte binary array to hex-ascii string */
|
|
|
|
int
|
|
|
|
btoa8(out, in)
|
|
|
|
register char *out, *in;
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
|
|
|
|
if (in == NULL || out == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i=0;i<8;i++) {
|
|
|
|
sprintf(out,"%02x",*in++ & 0xff);
|
|
|
|
out += 2;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Convert hex digit to binary integer */
|
|
|
|
int
|
|
|
|
htoi(c)
|
|
|
|
register char c;
|
|
|
|
{
|
|
|
|
if ('0' <= c && c <= '9')
|
|
|
|
return c - '0';
|
|
|
|
if ('a' <= c && c <= 'f')
|
|
|
|
return 10 + c - 'a';
|
|
|
|
if ('A' <= c && c <= 'F')
|
|
|
|
return 10 + c - 'A';
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
skipspace(cp)
|
|
|
|
register char *cp;
|
|
|
|
{
|
|
|
|
while (*cp == ' ' || *cp == '\t')
|
|
|
|
cp++;
|
|
|
|
|
|
|
|
if (*cp == '\0')
|
|
|
|
return NULL;
|
|
|
|
else
|
|
|
|
return cp;
|
|
|
|
}
|
1994-05-21 09:45:58 +04:00
|
|
|
|
|
|
|
/* removebackspaced over charaters from the string */
|
1995-06-05 23:48:33 +04:00
|
|
|
void
|
1994-05-21 09:45:58 +04:00
|
|
|
backspace(buf)
|
|
|
|
char *buf;
|
|
|
|
{
|
|
|
|
char bs = 0x8;
|
|
|
|
char *cp = buf;
|
|
|
|
char *out = buf;
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
while (*cp) {
|
|
|
|
if (*cp == bs) {
|
|
|
|
if (out == buf) {
|
1994-05-21 09:45:58 +04:00
|
|
|
cp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cp++;
|
|
|
|
out--;
|
|
|
|
}
|
1995-06-05 23:48:33 +04:00
|
|
|
} else {
|
1994-05-21 09:45:58 +04:00
|
|
|
*out++ = *cp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
*out = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* sevenbit ()
|
|
|
|
*
|
|
|
|
* Make sure line is all seven bits.
|
|
|
|
*/
|
|
|
|
|
1995-06-05 23:48:33 +04:00
|
|
|
void
|
|
|
|
sevenbit(s)
|
|
|
|
char *s;
|
1994-05-21 09:45:58 +04:00
|
|
|
{
|
1995-06-05 23:48:33 +04:00
|
|
|
while (*s)
|
|
|
|
*s++ &= 0x7f;
|
1994-05-21 09:45:58 +04:00
|
|
|
}
|