diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2001-08-01 22:20:11 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2001-08-01 22:20:11 +0000 |
commit | 5e71be6b58dcee1cc6afa33ae5cdea487cf6b5fc (patch) | |
tree | 66b96ebd83101505e005c0359feba3f8d6fb5bbe /usr.bin/x99token/x99token.c | |
parent | 2029817856760688a5bb321b2c222330b1b896ca (diff) |
Cleanup and remove DOS #ifdefs
Diffstat (limited to 'usr.bin/x99token/x99token.c')
-rw-r--r-- | usr.bin/x99token/x99token.c | 172 |
1 files changed, 59 insertions, 113 deletions
diff --git a/usr.bin/x99token/x99token.c b/usr.bin/x99token/x99token.c index ed205909366..c513056560a 100644 --- a/usr.bin/x99token/x99token.c +++ b/usr.bin/x99token/x99token.c @@ -3,81 +3,45 @@ * This software is provided AS IS with no express or implied warranty * October 1995, Paul Borman <prb@krystal.com> */ -#if defined(KRBDES) && !defined(__unix__) -#define __unix__ -#endif -#ifdef __unix__ -#ifdef LITTLE_ENDIAN -#undef LITTLE_ENDIAN -#endif -#include <pwd.h> -#else -#include <dos.h> -#endif +#include <sys/param.h> +#include <sys/stat.h> + #include <ctype.h> +#include <err.h> +#include <pwd.h> +#include <readpassphrase.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> -#include <sys/stat.h> -extern char *optarg; - - -#ifdef __unix__ -#define KEYFILE ".keyfile.des" -#define MPL 1024 -#else -#define KEYFILE "keyfile.des" -#define MPL 256 -#endif +#include <des.h> +#define KEYFILE ".keyfile.des" #define HEXDIGITS "0123456789abcdef" #define DECDIGITS "0123456789012345" -char *digits = HEXDIGITS; - -#ifdef KRBDES -#include <des.h> -#define setkey dessetkey - -void desinit(int i) { ; } -void dessetkey(char ks[16][8], char key[8]) -{ - des_key_schedule *k = (des_key_schedule *)ks; - des_fixup_key_parity((des_cblock *)key); - des_key_sched((des_cblock *)key, *k); -} -void endes(char ks[16][8], char key[8]) -{ - des_cblock cb; - des_key_schedule *k = (des_key_schedule *)ks; - - des_ecb_encrypt((des_cblock *)key, &cb, *k, DES_ENCRYPT); - memcpy(key, &cb, 8); -} -#endif +void predict(des_key_schedule, char *, int); -void predict(char ks[16][8], char *chal, int cnt); +char *digits = HEXDIGITS; +extern char *__progname; int -main(int ac, char **av) +main(int argc, char **argv) { int i; - char ks[16][8]; char buf[256]; - char key[8]; - char _keyfile[MPL]; + des_key_schedule ks; + des_cblock key; + char _keyfile[MAXPATHLEN]; char *keyfile = 0; FILE *fp; int init = 0; int hex = 1; int cnt = 1; - unsigned long pin; -#ifdef __unix__ + unsigned int pin; struct passwd *pwd; -#endif - while ((i = getopt(ac, av, "dk:in:")) != EOF) + while ((i = getopt(argc, argv, "dk:in:")) != -1) { switch (i) { case 'k': keyfile = optarg; @@ -90,44 +54,32 @@ main(int ac, char **av) break; case 'n': cnt = atoi(optarg); - if (cnt <= 0) { - fprintf(stderr, "%s: invalid count\n", optarg); - exit(1); - } + if (cnt <= 0) + err(1, "invalid count: %s", optarg); break; default: - fprintf(stderr, "Usage: x99token [-n cnt] [-h] [-k keyfile]\n" - " x99token -i [-k keyfile]\n"); + fprintf(stderr, "usage: %s [-n cnt] [-h] [-k keyfile]\n" + " %s -i [-k keyfile]\n", __progname, + __progname); exit(1); } - - desinit(0); + } if (!keyfile) { -#ifdef __unix__ - if ((pwd = getpwuid(getuid())) == 0) { + if ((pwd = getpwuid(getuid())) == NULL) { fprintf(stderr, "Say, just who are you, anyhow?\n"); exit(1); } - sprintf(_keyfile, "%s/%s", pwd->pw_dir, KEYFILE); + snprintf(_keyfile, sizeof(_keyfile), "%s/%s", pwd->pw_dir, + KEYFILE); keyfile = _keyfile; -#else - keyfile = KEYFILE; -#endif } - if (init) { -#ifdef __unix__ - strcpy(buf, (char *)getpass("Enter Key: ")); -#else - printf("Enter key: "); - if (fgets(buf, sizeof(buf), stdin) == NULL) - exit(0); -#endif - } else if ((fp = fopen(keyfile, "r")) == NULL) { - fprintf(stderr, "Failed to open %s\n", keyfile); - exit(1); - } else { + if (init) + readpassphrase("Enter Key: ", buf, sizeof(buf), 0); + else if ((fp = fopen(keyfile, "r")) == NULL) + err(1, "unable to open %s", keyfile); + else { if (fgets(buf, sizeof(buf), fp) == NULL) { fprintf(stderr, "No key in %s\n", keyfile); exit(1); @@ -148,7 +100,7 @@ main(int ac, char **av) while (*b && !isdigit(*b)) ++b; } - } else + } else { for (i = 0; i < 16; ++i) { int d; @@ -164,50 +116,39 @@ main(int ac, char **av) } key[i>>1] |= d << ((i & 1) ? 0 : 4); } + } -#ifdef __unix__ - strcpy(buf, (char *)getpass("Enter Pin: ")); -#else - printf("Enter Pin: "); - if (fgets(buf, sizeof(buf), stdin) == NULL) - exit(0); -#endif - - for (i = 0; buf[i] && buf[i] != '\n'; ++i) + /* XXX - should warn on non-space or non-digit */ + readpassphrase("Enter Pin: ", buf, sizeof(buf), 0); + for (i = 0, pin = 0; buf[i] && buf[i] != '\n'; ++i) if (isdigit(buf[i])) pin = pin * 16 + buf[i] - '0' + 1; - if ((pin & 0xffff0000L) == 0) + if ((pin & 0xffff0000) == 0) pin |= pin << 16; for (i = 0; i < 8; ++i) key[0] ^= (pin >> ((i * 7) % 26)) & 0x7f; if (init) { - if ((fp = fopen(keyfile, "w")) == NULL) { - fprintf(stderr, "could not open %s for writing\n", - keyfile); - exit(1); - } + if ((fp = fopen(keyfile, "w")) == NULL) + err(1, "could not open %s for writing", keyfile); + fchmod(fileno(fp), 0600); for (i = 0; i < 8; ++i) { fprintf(fp, "%c", digits[(key[i]>>4)&0xf]); fprintf(fp, "%c", digits[(key[i]>>0)&0xf]); } - fprintf(fp, "\n"); + fputc('\n', fp); fclose(fp); -#ifdef __unix__ - chmod(keyfile, 0600); -#else - dos_setfileattr(keyfile, FA_HIDDEN | FA_SYSTEM); -#endif exit(0); } - setkey(ks, key); + des_fixup_key_parity(&key); + des_key_sched(&key, ks); - printf("Enter challange: "); - memset(buf, 0, sizeof(buf)); - if (fgets(buf, sizeof(buf), stdin) == NULL) + buf[0] = '\0'; + readpassphrase("Enter challange: ", buf, sizeof(buf), RPP_ECHO_ON); + if (buf[0] == '\0') exit(0); for (i = 0; i < 8; ++i) @@ -219,26 +160,31 @@ main(int ac, char **av) predict(ks, buf, cnt); + memset(&ks, 0, sizeof(ks)); + memset(buf, 0, sizeof(buf)); + exit(0); } void -predict(char ks[16][8], char *chal, int cnt) +predict(des_key_schedule ks, char *chal, int cnt) { int i; + des_cblock cb; while (cnt-- > 0) { printf("%.8s: ", chal); - endes(ks, chal); + des_ecb_encrypt((des_cblock *)chal, &cb, ks, DES_ENCRYPT); for (i = 0; i < 4; ++i) { - printf("%c", digits[(chal[i]>>4) & 0xf]); - printf("%c", digits[(chal[i]>>0) & 0xf]); + printf("%c", digits[(cb[i]>>4) & 0xf]); + printf("%c", digits[(cb[i]>>0) & 0xf]); } - printf("\n"); + putchar('\n'); for (i = 0; i < 8; ++i) { - if ((chal[i] &= 0xf) > 9) - chal[i] -= 10; - chal[i] |= 0x30; + if ((cb[i] &= 0xf) > 9) + cb[i] -= 10; + cb[i] |= 0x30; } } + memset(&cb, 0, sizeof(cb)); } |