[BACK]Return to x99token.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / x99token

Annotation of src/usr.bin/x99token/x99token.c, Revision 1.1

1.1     ! millert     1: /*
        !             2:  * X9.9 calculator
        !             3:  * This software is provided AS IS with no express or implied warranty
        !             4:  * October 1995, Paul Borman <prb@krystal.com>
        !             5:  */
        !             6: #if    defined(KRBDES) && !defined(__unix__)
        !             7: #define        __unix__
        !             8: #endif
        !             9: #ifdef __unix__
        !            10: #ifdef LITTLE_ENDIAN
        !            11: #undef LITTLE_ENDIAN
        !            12: #endif
        !            13: #include <pwd.h>
        !            14: #else
        !            15: #include <dos.h>
        !            16: #endif
        !            17: #include <ctype.h>
        !            18: #include <stdio.h>
        !            19: #include <stdlib.h>
        !            20: #include <string.h>
        !            21: #include <unistd.h>
        !            22: #include <sys/stat.h>
        !            23: extern char *optarg;
        !            24:
        !            25:
        !            26: #ifdef __unix__
        !            27: #define        KEYFILE ".keyfile.des"
        !            28: #define        MPL     1024
        !            29: #else
        !            30: #define        KEYFILE "keyfile.des"
        !            31: #define        MPL     256
        !            32: #endif
        !            33:
        !            34: #define        HEXDIGITS       "0123456789abcdef"
        !            35: #define        DECDIGITS       "0123456789012345"
        !            36:
        !            37: char *digits = HEXDIGITS;
        !            38:
        !            39: #ifdef KRBDES
        !            40: #include <des.h>
        !            41: #define setkey dessetkey
        !            42:
        !            43: void desinit(int i) { ; }
        !            44: void dessetkey(char ks[16][8], char key[8])
        !            45: {
        !            46:        des_key_schedule *k = (des_key_schedule *)ks;
        !            47:        des_fixup_key_parity((des_cblock *)key);
        !            48:        des_key_sched((des_cblock *)key, *k);
        !            49: }
        !            50: void endes(char ks[16][8], char key[8])
        !            51: {
        !            52:        des_cblock cb;
        !            53:        des_key_schedule *k = (des_key_schedule *)ks;
        !            54:
        !            55:        des_ecb_encrypt((des_cblock *)key, &cb, *k, DES_ENCRYPT);
        !            56:        memcpy(key, &cb, 8);
        !            57: }
        !            58: #endif
        !            59:
        !            60: void predict(char ks[16][8], char *chal, int cnt);
        !            61:
        !            62: int
        !            63: main(int ac, char **av)
        !            64: {
        !            65:        int i;
        !            66:        char ks[16][8];
        !            67:        char buf[256];
        !            68:        char key[8];
        !            69:        char _keyfile[MPL];
        !            70:        char *keyfile = 0;
        !            71:        FILE *fp;
        !            72:        int init = 0;
        !            73:        int hex = 1;
        !            74:        int cnt = 1;
        !            75:        unsigned long pin;
        !            76: #ifdef __unix__
        !            77:        struct passwd *pwd;
        !            78: #endif
        !            79:
        !            80:        while ((i = getopt(ac, av, "dk:in:")) != EOF)
        !            81:                switch (i) {
        !            82:                case 'k':
        !            83:                        keyfile = optarg;
        !            84:                        break;
        !            85:                case 'i':
        !            86:                        init = 1;
        !            87:                        break;
        !            88:                case 'd':
        !            89:                        hex = 0;
        !            90:                        break;
        !            91:                case 'n':
        !            92:                        cnt = atoi(optarg);
        !            93:                        if (cnt <= 0) {
        !            94:                                fprintf(stderr, "%s: invalid count\n", optarg);
        !            95:                                exit(1);
        !            96:                        }
        !            97:                        break;
        !            98:                default:
        !            99:                        fprintf(stderr, "Usage: x99token [-n cnt] [-h] [-k keyfile]\n"
        !           100:                                        "       x99token -i [-k keyfile]\n");
        !           101:                        exit(1);
        !           102:                }
        !           103:
        !           104:        desinit(0);
        !           105:
        !           106:        if (!keyfile) {
        !           107: #ifdef __unix__
        !           108:                if ((pwd = getpwuid(getuid())) == 0) {
        !           109:                        fprintf(stderr, "Say, just who are you, anyhow?\n");
        !           110:                        exit(1);
        !           111:                }
        !           112:                sprintf(_keyfile, "%s/%s", pwd->pw_dir, KEYFILE);
        !           113:                keyfile = _keyfile;
        !           114: #else
        !           115:                keyfile = KEYFILE;
        !           116: #endif
        !           117:        }
        !           118:
        !           119:        if (init) {
        !           120: #ifdef __unix__
        !           121:                strcpy(buf, (char *)getpass("Enter Key: "));
        !           122: #else
        !           123:                printf("Enter key: ");
        !           124:                if (fgets(buf, sizeof(buf), stdin) == NULL)
        !           125:                        exit(0);
        !           126: #endif
        !           127:        } else if ((fp = fopen(keyfile, "r")) == NULL) {
        !           128:                fprintf(stderr, "Failed to open %s\n", keyfile);
        !           129:                exit(1);
        !           130:        } else {
        !           131:                if (fgets(buf, sizeof(buf), fp) == NULL) {
        !           132:                        fprintf(stderr, "No key in %s\n", keyfile);
        !           133:                        exit(1);
        !           134:                }
        !           135:                fclose(fp);
        !           136:        }
        !           137:
        !           138:        memset(key, 0, sizeof(key));
        !           139:        if (init && buf[3] == ' ') {
        !           140:                char *b = buf;
        !           141:                /* Assume octal input */
        !           142:                for (i = 0; i < 8; ++i) {
        !           143:                        if (!*b) {
        !           144:                                fprintf(stderr, "%s: invalid key\n", buf);
        !           145:                        }
        !           146:                        while (isdigit(*b))
        !           147:                                key[i] = key[i] << 3 | *b++ - '0';
        !           148:                        while (*b && !isdigit(*b))
        !           149:                                ++b;
        !           150:                }
        !           151:        } else
        !           152:                for (i = 0; i < 16; ++i) {
        !           153:                        int d;
        !           154:
        !           155:                        if (islower(buf[i]))
        !           156:                                buf[i] = toupper(buf[i]);
        !           157:                        if (buf[i] >= '0' && buf[i] <= '9')
        !           158:                                d = buf[i] - '0';
        !           159:                        else if (buf[i] >= 'A' && buf[i] <= 'F')
        !           160:                                d = buf[i] - 'A' + 10;
        !           161:                        else {
        !           162:                                fprintf(stderr, "invalid key: %s\n", buf);
        !           163:                                exit(1);
        !           164:                        }
        !           165:                        key[i>>1] |= d << ((i & 1) ? 0 : 4);
        !           166:                }
        !           167:
        !           168: #ifdef __unix__
        !           169:        strcpy(buf, (char *)getpass("Enter Pin: "));
        !           170: #else
        !           171:        printf("Enter Pin: ");
        !           172:        if (fgets(buf, sizeof(buf), stdin) == NULL)
        !           173:                exit(0);
        !           174: #endif
        !           175:
        !           176:        for (i = 0; buf[i] && buf[i] != '\n'; ++i)
        !           177:                if (isdigit(buf[i]))
        !           178:                        pin = pin * 16 + buf[i] - '0' + 1;
        !           179:
        !           180:        if ((pin & 0xffff0000L) == 0)
        !           181:                pin |= pin << 16;
        !           182:
        !           183:        for (i = 0; i < 8; ++i)
        !           184:                key[0] ^= (pin >> ((i * 7) % 26)) & 0x7f;
        !           185:
        !           186:        if (init) {
        !           187:                if ((fp = fopen(keyfile, "w")) == NULL) {
        !           188:                        fprintf(stderr, "could not open %s for writing\n",
        !           189:                                keyfile);
        !           190:                        exit(1);
        !           191:                }
        !           192:                for (i = 0; i < 8; ++i) {
        !           193:                        fprintf(fp, "%c", digits[(key[i]>>4)&0xf]);
        !           194:                        fprintf(fp, "%c", digits[(key[i]>>0)&0xf]);
        !           195:                }
        !           196:                fprintf(fp, "\n");
        !           197:                fclose(fp);
        !           198: #ifdef __unix__
        !           199:                chmod(keyfile, 0600);
        !           200: #else
        !           201:                dos_setfileattr(keyfile, FA_HIDDEN | FA_SYSTEM);
        !           202: #endif
        !           203:                exit(0);
        !           204:        }
        !           205:
        !           206:        setkey(ks, key);
        !           207:
        !           208:        printf("Enter challange: ");
        !           209:        memset(buf, 0, sizeof(buf));
        !           210:        if (fgets(buf, sizeof(buf), stdin) == NULL)
        !           211:                exit(0);
        !           212:
        !           213:        for (i = 0; i < 8; ++i)
        !           214:                if (buf[i] == '\n')
        !           215:                        buf[i] = '\0';
        !           216:
        !           217:        if (!hex)
        !           218:                digits = DECDIGITS;
        !           219:
        !           220:        predict(ks, buf, cnt);
        !           221:
        !           222:        exit(0);
        !           223: }
        !           224:
        !           225: void
        !           226: predict(char ks[16][8], char *chal, int cnt)
        !           227: {
        !           228:        int i;
        !           229:
        !           230:        while (cnt-- > 0) {
        !           231:                printf("%.8s: ", chal);
        !           232:                endes(ks, chal);
        !           233:                for (i = 0; i < 4; ++i) {
        !           234:                        printf("%c", digits[(chal[i]>>4) & 0xf]);
        !           235:                        printf("%c", digits[(chal[i]>>0) & 0xf]);
        !           236:                }
        !           237:                printf("\n");
        !           238:                for (i = 0; i < 8; ++i) {
        !           239:                        if ((chal[i] &= 0xf) > 9)
        !           240:                                chal[i] -= 10;
        !           241:                        chal[i] |= 0x30;
        !           242:                }
        !           243:        }
        !           244: }