NetBSD/lib/libskey/skeysubr.c

290 lines
4.4 KiB
C
Raw Normal View History

2000-01-23 05:11:02 +03:00
/* $NetBSD: skeysubr.c,v 1.15 2000/01/23 02:11:02 mycroft Exp $ */
1996-09-19 23:39:36 +04:00
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>
*
1999-07-02 19:37:33 +04:00
* Modifications:
1994-05-21 09:45:58 +04:00
* Scott Chasin <chasin@crimelab.com>
*
* S/KEY misc routines.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
1995-06-20 02:47:37 +04:00
#include <termios.h>
#include <md4.h>
1994-05-21 09:45:58 +04:00
#include "skey.h"
1994-05-24 10:25:27 +04:00
struct termios newtty;
struct termios oldtty;
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 */
1998-07-27 01:58:46 +04:00
const char *seed; /* Seed, any length */
const char *passwd; /* Password, any length */
1994-05-21 09:45:58 +04:00
{
char *buf;
MD4_CTX md;
1994-05-21 09:45:58 +04:00
unsigned int buflen;
u_int32_t hash[4];
1999-07-02 19:37:33 +04:00
1994-05-21 09:45:58 +04:00
buflen = strlen(seed) + strlen(passwd);
if ((buf = (char *)malloc(buflen+1)) == NULL)
return -1;
strcpy(buf, seed); /* XXX strcpy is safe */
strcat(buf, passwd); /* XXX strcat is safe */
1994-05-21 09:45:58 +04:00
/* Crunch the key through MD4 */
sevenbit(buf);
MD4Init(&md);
MD4Update(&md, (unsigned char *) buf, buflen);
2000-01-23 05:11:02 +03:00
MD4Final((unsigned char *)(void *) hash, &md);
1994-05-21 09:45:58 +04:00
free(buf);
/* Fold result from 128 to 64 bits */
hash[0] ^= hash[2];
hash[1] ^= hash[3];
1994-05-21 09:45:58 +04:00
(void)memcpy(result, hash, 8);
1994-05-21 09:45:58 +04:00
return 0;
}
/* The one-way function f(). Takes 8 bytes and returns 8 bytes in place */
void
f(x)
char *x;
1994-05-21 09:45:58 +04:00
{
MD4_CTX md;
u_int32_t hash[4];
1994-05-21 09:45:58 +04:00
MD4Init(&md);
MD4Update(&md, (unsigned char *) x, 8);
2000-01-23 05:11:02 +03:00
MD4Final((unsigned char *)(void *) hash, &md);
1994-05-21 09:45:58 +04:00
/* Fold 128 to 64 bits */
hash[0] ^= hash[2];
hash[1] ^= hash[3];
1994-05-21 09:45:58 +04:00
(void)memcpy(x, hash, 8);
1994-05-21 09:45:58 +04:00
}
/* Strip trailing cr/lf from a line of text */
void
rip(buf)
char *buf;
1994-05-21 09:45:58 +04:00
{
char *cp;
if ((cp = strchr(buf,'\r')) != NULL)
1994-05-21 09:45:58 +04:00
*cp = '\0';
if ((cp = strchr(buf,'\n')) != NULL)
1994-05-21 09:45:58 +04:00
*cp = '\0';
}
char *
readpass (buf,n)
char *buf;
int n;
1994-05-21 09:45:58 +04:00
{
set_term();
echo_off();
1994-05-21 09:45:58 +04:00
fgets(buf, n, stdin);
1994-05-21 09:45:58 +04:00
rip(buf);
printf("\n");
1994-05-21 09:45:58 +04:00
sevenbit(buf);
1994-05-21 09:45:58 +04:00
unset_term();
return buf;
1994-05-21 09:45:58 +04:00
}
char *
readskey(buf, n)
char *buf;
int n;
{
fgets (buf, n, stdin);
rip(buf);
printf ("\n");
sevenbit (buf);
return buf;
}
static void
1999-07-02 19:37:33 +04:00
set_term()
1994-05-21 09:45:58 +04:00
{
fflush(stdout);
tcgetattr(fileno(stdin), &newtty);
tcgetattr(fileno(stdin), &oldtty);
1999-07-02 19:37:33 +04:00
signal (SIGINT, trapped);
1994-05-21 09:45:58 +04:00
}
static void
echo_off()
1994-05-21 09:45:58 +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
tcsetattr(fileno(stdin), TCSADRAIN, &newtty);
}
static void
unset_term()
{
tcsetattr(fileno(stdin), TCSADRAIN, &oldtty);
1994-05-21 09:45:58 +04:00
}
2000-01-23 05:11:02 +03:00
/*ARGSUSED*/
static void
trapped(sig)
int sig;
1994-05-21 09:45:58 +04:00
{
signal(SIGINT, trapped);
printf("^C\n");
unset_term();
exit(-1);
1994-05-21 09:45:58 +04:00
}
/* Convert 8-byte hex-ascii string to binary array
* Returns 0 on success, -1 on error
*/
int
atob8(out, in)
1998-07-27 01:58:46 +04:00
char *out;
const char *in;
{
1998-02-03 22:12:13 +03:00
int i;
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)
1998-07-27 01:58:46 +04:00
char *out;
const char *in;
{
1998-02-03 22:12:13 +03:00
int i;
if (in == NULL || out == NULL)
return -1;
for (i=0;i<8;i++) {
sprintf(out,"%02x",*in++ & 0xff); /* XXX: sprintf() (btoa8() appears to be unused */
out += 2;
}
return 0;
}
/* Convert hex digit to binary integer */
int
htoi(c)
2000-01-23 05:11:02 +03:00
int 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;
}
1998-07-27 01:58:46 +04:00
const char *
skipspace(cp)
1998-07-27 01:58:46 +04:00
const 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 */
void
1994-05-21 09:45:58 +04:00
backspace(buf)
char *buf;
{
char bs = 0x8;
char *cp = buf;
char *out = buf;
while (*cp) {
if (*cp == bs) {
if (out == buf) {
1994-05-21 09:45:58 +04:00
cp++;
continue;
}
else {
cp++;
out--;
}
} else {
1994-05-21 09:45:58 +04:00
*out++ = *cp++;
}
}
*out = '\0';
}
/* sevenbit ()
*
* Make sure line is all seven bits.
*/
1999-07-02 19:37:33 +04:00
void
sevenbit(s)
char *s;
1994-05-21 09:45:58 +04:00
{
while (*s)
*s++ &= 0x7f;
1994-05-21 09:45:58 +04:00
}