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