xref: /netbsd-src/usr.bin/skey/skey.c (revision edfa83365254b6d7c6cdaa0d30b214319daeee7f)
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.2 1994/12/28 23:17:52 cgd 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 		slash = strchr(argv[optind], '/');
65 		if (slash == NULL)
66 			usage(argv[0]);
67 		*slash++ = '\0';
68 		seed = slash;
69 
70 		if ((n = atoi(argv[optind])) < 0) {
71 			fprintf(stderr, "%s not positive\n", argv[optind]);
72 			usage(argv[0]);
73 		}
74 	} else {
75 
76 		if ((n = atoi(argv[optind])) < 0) {
77 			fprintf(stderr, "%s not positive\n", argv[optind]);
78 			usage(argv[0]);
79 		}
80 		seed = argv[++optind];
81 	}
82 
83 	/* Get user's secret password */
84 	if (!pass) {
85 		fprintf(stderr, "Enter secret password: ");
86 		readpass(passwd, sizeof(passwd));
87 	}
88 	rip(passwd);
89 
90 	/* Crunch seed and password into starting key */
91 	if (keycrunch(key, seed, passwd) != 0) {
92 		fprintf(stderr, "%s: key crunch failed\n", argv[0]);
93 		exit(1);
94 	}
95 	if (cnt == 1) {
96 		while (n-- != 0)
97 			f(key);
98 		printf("%s\n", btoe(buf, key));
99 #ifdef	HEXIN
100 		printf("%s\n", put8(buf, key));
101 #endif
102 	} else {
103 		for (i = 0; i <= n - cnt; i++)
104 			f(key);
105 		for (; i <= n; i++) {
106 #ifdef	HEXIN
107 			printf("%d: %-29s  %s\n", i, btoe(buf, key), put8(buf, key));
108 #else
109 			printf("%d: %-29s\n", i, btoe(buf, key));
110 #endif
111 			f(key);
112 		}
113 	}
114 	exit(0);
115 }
116 
117 void
118 usage(s)
119 	char   *s;
120 {
121 
122 	fprintf(stderr,
123 	    "Usage: %s [-n count] [-p password ] sequence# [/] key\n", s);
124 	exit(1);
125 }
126