home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Utilities / Unix / skey / srcmd5 / skeysubr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-14  |  5.0 KB  |  309 lines

  1. /* S/KEY v1.1b (skeysubr.c)
  2.  *
  3.  * Authors:
  4.  *          Neil M. Haller <nmh@thumper.bellcore.com>
  5.  *          Philip R. Karn <karn@chicago.qualcomm.com>
  6.  *          John S. Walden <jsw@thumper.bellcore.com>
  7.  *
  8.  * Modifications: 
  9.  *          Scott Chasin <chasin@crimelab.com>
  10.  *
  11.  * S/KEY misc routines.
  12.  */
  13.  
  14. #include <stdio.h>
  15.  
  16. #ifdef HAS_STD_LIB
  17. #include <stdlib.h>
  18. #else
  19. #include <sys/types.h>
  20. #endif
  21.  
  22. #include <string.h>
  23. #include <signal.h>
  24.  
  25. #ifdef    __MSDOS__
  26. #include <dos.h>
  27. #endif
  28.  
  29. #ifdef stty
  30. # undef stty
  31. #endif
  32.  
  33. #ifdef gtty
  34. # undef gtty
  35. #endif
  36.  
  37. #ifndef SYSV
  38. # include <sgtty.h>
  39. # define TTYSTRUCT sgttyb
  40. # define stty(fd,buf) ioctl((fd),TIOCSETN,(buf))
  41. # define gtty(fd,buf) ioctl((fd),TIOCGETP,(buf))
  42. #else
  43. # include <termio.h>
  44. # define TTYSTRUCT termio
  45. # define stty(fd,buf) ioctl((fd),TCSETA,(buf))
  46. # define gtty(fd,buf) ioctl((fd),TCGETA,(buf))
  47. #endif
  48.  
  49. #ifdef SYSV
  50.     struct termio newtty;
  51.     struct termio oldtty;
  52. #else
  53.     struct sgttyb newtty;
  54.     struct sgttyb oldtty;
  55.     struct tchars chars;
  56. #endif
  57.  
  58. #ifdef SIGVOID
  59. #define SIGTYPE void
  60. #else
  61. #define SIGTYPE void
  62. #endif
  63.  
  64. SIGTYPE trapped();
  65.  
  66. #include "mdx.h"
  67. #include "skey.h"
  68.  
  69. #if (defined(__MSDOS__) || defined(MPU8086) || defined(MPU8080) \
  70.     || defined(vax) || defined (MIPSEL))
  71. #define    LITTLE_ENDIAN
  72. #endif
  73.  
  74. /* Crunch a key:
  75.  * concatenate the seed and the password, run through MD4 and
  76.  * collapse to 64 bits. This is defined as the user's starting key.
  77.  */
  78. int
  79. keycrunch(result,seed,passwd)
  80. char *result;    /* 8-byte result */
  81. char *seed;    /* Seed, any length */
  82. char *passwd;    /* Password, any length */
  83. {
  84.     char *buf;
  85.     MDX_CTX md;
  86.         UINT4 results[4];
  87.     unsigned int buflen;
  88. #ifndef    LITTLE_ENDIAN
  89.     int i;
  90.     register long tmp;
  91. #endif
  92.     
  93.     buflen = strlen(seed) + strlen(passwd);
  94.     if ((buf = (char *)malloc(buflen+1)) == NULL)
  95.         return -1;
  96.     strcpy(buf,seed);
  97.     strcat(buf,passwd);
  98.  
  99.     /* Crunch the key through MD4 */
  100.     sevenbit(buf);
  101.     MDXInit(&md);
  102.     MDXUpdate(&md,(unsigned char *)buf,buflen);
  103.         MDXFinal((unsigned char *)results,&md);
  104.     free(buf);
  105.  
  106.     /* Fold result from 128 to 64 bits  
  107.     md.buffer[0] ^= md.buffer[2];
  108.     md.buffer[1] ^= md.buffer[3];  */
  109.  
  110.         results[0] ^= results[2];
  111.         results[1] ^= results[3];
  112.  
  113.         memcpy(result,(char *)results,8);
  114.  
  115.         return 0;
  116.  
  117. #ifdef    LITTLE_ENDIAN
  118.     /* Only works on byte-addressed little-endian machines!! */
  119.     memcpy(result,(char *)md.buffer,8);
  120. #else
  121.     /* Default (but slow) code that will convert to
  122.      * little-endian byte ordering on any machine
  123.      */
  124.     for (i=0; i<2; i++) {
  125.         tmp = md.buffer[i];
  126.         *result++ = tmp;
  127.         tmp >>= 8;
  128.         *result++ = tmp;
  129.         tmp >>= 8;
  130.         *result++ = tmp;
  131.         tmp >>= 8;
  132.         *result++ = tmp;
  133.     }
  134. #endif
  135.  
  136.     return 0;
  137. }
  138.  
  139. /* The one-way function f(). Takes 8 bytes and returns 8 bytes in place */
  140. void f (x)
  141. char *x;
  142. {
  143.     MDX_CTX md;
  144.         UINT4 results[4];
  145. #ifndef    LITTLE_ENDIAN
  146.     register long tmp;
  147. #endif
  148.  
  149.     MDXInit(&md);
  150.     MDXUpdate(&md,(unsigned char *)x,64);
  151.         MDXFinal((unsigned char *)results,&md);
  152.     /* Fold 128 to 64 bits 
  153.     md.buffer[0] ^= md.buffer[2];
  154.     md.buffer[1] ^= md.buffer[3]; */
  155.  
  156.         /* Fold 128 to 64 bits */
  157.         results[0] ^= results[2];
  158.         results[1] ^= results[3];
  159.  
  160.         /* Only works on byte-addressed little-endian machines!! */
  161.         memcpy(x,(char *)results,8);
  162. }
  163.  
  164. /* Strip trailing cr/lf from a line of text */
  165. void rip (buf)
  166. char *buf;
  167. {
  168.     char *cp;
  169.  
  170.     if((cp = strchr(buf,'\r')) != NULL)
  171.         *cp = '\0';
  172.  
  173.     if((cp = strchr(buf,'\n')) != NULL)
  174.         *cp = '\0';
  175. }
  176.  
  177. #ifdef    __MSDOS__
  178. char *readpass(buf,n)
  179. char *buf;
  180. int n;
  181. {
  182.   int i;
  183.   char *cp;
  184.  
  185.   for (cp=buf,i = 0; i < n ; i++)
  186.        if ((*cp++ = bdos(7,0,0)) == '\r')
  187.           break;
  188.    *cp = '\0';
  189.    putchar('\n');
  190.    rip(buf);
  191.    return buf;
  192. }
  193. #else
  194.  
  195. char *readpass (buf,n)
  196. char *buf;
  197. int n;
  198. {
  199.  
  200. #ifndef USE_ECHO
  201.     set_term ();
  202.     echo_off ();
  203. #endif
  204.  
  205.     fgets (buf, n, stdin);
  206.  
  207.     rip (buf);
  208.  
  209.     printf ("\n\n");
  210.     sevenbit (buf);
  211.  
  212. #ifndef USE_ECHO
  213.     unset_term ();
  214. #endif
  215.     return buf;
  216. }
  217.  
  218. set_term () 
  219. {
  220.     gtty (fileno(stdin), &newtty);
  221.     gtty (fileno(stdin), &oldtty);
  222.  
  223.     signal (SIGINT, trapped);
  224. }
  225.  
  226. echo_off ()
  227. {
  228.  
  229. #ifdef SYSV
  230.     newtty.c_lflag &= ~(ICANON | ECHO | ECHONL);
  231. #else
  232.     newtty.sg_flags |= CBREAK;
  233.     newtty.sg_flags &= ~ECHO;
  234. #endif
  235.  
  236. #ifdef SYSV
  237.     newtty.c_cc[VMIN] = 1;
  238.     newtty.c_cc[VTIME] = 0;
  239.     newtty.c_cc[VINTR] = 3;
  240. #else
  241.     ioctl(fileno(stdin), TIOCGETC, &chars);
  242.     chars.t_intrc = 3;
  243.     ioctl(fileno(stdin), TIOCSETC, &chars);
  244. #endif
  245.  
  246.     stty (fileno (stdin), &newtty);
  247. }
  248.  
  249. unset_term ()
  250. {
  251.     stty (fileno (stdin), &oldtty);
  252.  
  253. #ifndef SYSV
  254.     ioctl(fileno(stdin), TIOCSETC, &chars);
  255. #endif
  256. }
  257.  
  258. void trapped()
  259.  {
  260.   signal (SIGINT, trapped);
  261.   printf ("^C\n");
  262.   unset_term ();
  263.   exit (-1);
  264.  }
  265.  
  266. #endif
  267.  
  268. /* removebackspaced over charaters from the string */
  269. backspace(buf)
  270. char *buf;
  271. {
  272.     char bs = 0x8;
  273.     char *cp = buf;
  274.     char *out = buf;
  275.  
  276.     while(*cp){
  277.         if( *cp == bs ) {
  278.             if(out == buf){
  279.                 cp++;
  280.                 continue;
  281.             }
  282.             else {
  283.               cp++;
  284.               out--;
  285.             }
  286.         }
  287.         else {
  288.             *out++ = *cp++;
  289.         }
  290.  
  291.     }
  292.     *out = '\0';
  293.     
  294. }
  295.  
  296. /* sevenbit ()
  297.  *
  298.  * Make sure line is all seven bits.
  299.  */
  300.  
  301. sevenbit (s)
  302. char *s;
  303. {
  304.    while (*s) {
  305.      *s = 0x7f & ( *s);
  306.      s++;
  307.    }
  308. }
  309.