xref: /netbsd-src/usr.bin/skey/skey.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*
2  * S/KEY v1.1b (skey.c)
3  *
4  * Authors:
5  *          Neil M. Haller <nmh@thumper.bellcore.com>
6  *          Philip R. Karn <karn@chicago.qualcomm.com>
7  *          John S. Walden <jsw@thumper.bellcore.com>
8  *          Scott Chasin <chasin@crimelab.com>
9  *
10  *
11  * Stand-alone program for computing responses to S/Key challenges.
12  * Takes the iteration count and seed as command line args, prompts
13  * for the user's key, and produces both word and hex format responses.
14  *
15  * Usage example:
16  *	>skey 88 ka9q2
17  *	Enter password:
18  *	OMEN US HORN OMIT BACK AHOY
19  *	>
20  *
21  * $Id: skey.c,v 1.1 1994/05/24 06:48:02 deraadt Exp $
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <sgtty.h>
29 #include "md4.h"
30 #include "skey.h"
31 
32 char   *readpass();
33 void    usage();
34 
35 main(argc, argv)
36 	int	argc;
37 	char	*argv[];
38 {
39 	int     n, cnt, i, pass = 0;
40 	char    passwd[256], key[8], buf[33], *seed, *slash;
41 	extern int optind;
42 	extern char *optarg;
43 
44 	cnt = 1;
45 
46 	while ((i = getopt(argc, argv, "n:p:")) != EOF) {
47 		switch (i) {
48 		case 'n':
49 			cnt = atoi(optarg);
50 			break;
51 		case 'p':
52 			strcpy(passwd, optarg);
53 			pass = 1;
54 			break;
55 		}
56 	}
57 
58 	/* could be in the form <number>/<seed> */
59 
60 	if (argc <= optind + 1) {
61 		/* look for / in it */
62 		if (argc <= optind) {
63 			usage(argv[0]);
64 			exit(1);
65 		}
66 		slash = strchr(argv[optind], '/');
67 		if (slash == NULL) {
68 			usage(argv[0]);
69 			exit(1);
70 		}
71 		*slash++ = '\0';
72 		seed = slash;
73 
74 		if ((n = atoi(argv[optind])) < 0) {
75 			printf("%s not positive\n", argv[optind]);
76 			usage(argv[0]);
77 			exit(1);
78 		}
79 	} else {
80 
81 		if ((n = atoi(argv[optind])) < 0) {
82 			printf("%s not positive\n", argv[optind]);
83 			usage(argv[0]);
84 			exit(1);
85 		}
86 		seed = argv[++optind];
87 	}
88 
89 	/* Get user's secret password */
90 	if (!pass) {
91 		printf("Enter secret password: ");
92 		readpass(passwd, sizeof(passwd));
93 	}
94 	rip(passwd);
95 
96 	/* Crunch seed and password into starting key */
97 	if (keycrunch(key, seed, passwd) != 0) {
98 		fprintf(stderr, "%s: key crunch failed\n", argv[0]);
99 		exit(1);
100 	}
101 	if (cnt == 1) {
102 		while (n-- != 0)
103 			f(key);
104 		printf("%s\n", btoe(buf, key));
105 #ifdef	HEXIN
106 		printf("%s\n", put8(buf, key));
107 #endif
108 	} else {
109 		for (i = 0; i <= n - cnt; i++)
110 			f(key);
111 		for (; i <= n; i++) {
112 #ifdef	HEXIN
113 			printf("%d: %-29s  %s\n", i, btoe(buf, key), put8(buf, key));
114 #else
115 			printf("%d: %-29s\n", i, btoe(buf, key));
116 #endif
117 			f(key);
118 		}
119 	}
120 	exit(0);
121 }
122 
123 void
124 usage(s)
125 	char   *s;
126 {
127 	printf("Usage: %s [-n count] [-p password ] sequence# [/] key\n", s);
128 }
129