[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.9

1.9     ! deraadt     1: /*     $OpenBSD: x99token.c,v 1.8 2010/10/15 10:18:42 jsg Exp $        */
1.6       millert     2:
1.1       millert     3: /*
                      4:  * X9.9 calculator
                      5:  * This software is provided AS IS with no express or implied warranty
                      6:  * October 1995, Paul Borman <prb@krystal.com>
1.5       millert     7:  *
                      8:  * Donated to the Public Domain by Paul Borman
1.1       millert     9:  */
1.2       millert    10: #include <sys/param.h>
                     11: #include <sys/stat.h>
                     12:
                     13: #include <ctype.h>
                     14: #include <err.h>
1.1       millert    15: #include <pwd.h>
1.2       millert    16: #include <readpassphrase.h>
1.1       millert    17: #include <stdio.h>
                     18: #include <stdlib.h>
                     19: #include <string.h>
                     20: #include <unistd.h>
1.8       jsg        21: #include <openssl/des.h>
1.1       millert    22:
1.2       millert    23: #define        KEYFILE         ".keyfile.des"
1.1       millert    24: #define        HEXDIGITS       "0123456789abcdef"
                     25: #define        DECDIGITS       "0123456789012345"
                     26:
1.8       jsg        27: void predict(DES_key_schedule, const char *, int);
1.2       millert    28:
1.1       millert    29: char *digits = HEXDIGITS;
1.2       millert    30: extern char *__progname;
1.1       millert    31:
                     32: int
1.2       millert    33: main(int argc, char **argv)
1.1       millert    34: {
                     35:        int i;
                     36:        char buf[256];
1.8       jsg        37:        DES_key_schedule ks;
                     38:        DES_cblock key;
1.2       millert    39:        char _keyfile[MAXPATHLEN];
1.1       millert    40:        char *keyfile = 0;
                     41:        FILE *fp;
                     42:        int init = 0;
                     43:        int hex = 1;
                     44:        int cnt = 1;
1.2       millert    45:        unsigned int pin;
1.1       millert    46:        struct passwd *pwd;
                     47:
1.2       millert    48:        while ((i = getopt(argc, argv, "dk:in:")) != -1) {
1.1       millert    49:                switch (i) {
                     50:                case 'k':
                     51:                        keyfile = optarg;
                     52:                        break;
                     53:                case 'i':
                     54:                        init = 1;
                     55:                        break;
                     56:                case 'd':
                     57:                        hex = 0;
                     58:                        break;
                     59:                case 'n':
                     60:                        cnt = atoi(optarg);
1.2       millert    61:                        if (cnt <= 0)
                     62:                                err(1, "invalid count: %s", optarg);
1.1       millert    63:                        break;
                     64:                default:
1.7       jmc        65:                        fprintf(stderr,
                     66:                            "usage: %s [-d] [-k keyfile] [-n count]\n"
                     67:                            "       %s -i [-k keyfile]\n",
                     68:                            __progname, __progname);
1.1       millert    69:                        exit(1);
                     70:                }
1.2       millert    71:        }
1.1       millert    72:
                     73:        if (!keyfile) {
1.2       millert    74:                if ((pwd = getpwuid(getuid())) == NULL) {
1.1       millert    75:                        fprintf(stderr, "Say, just who are you, anyhow?\n");
                     76:                        exit(1);
                     77:                }
1.2       millert    78:                snprintf(_keyfile, sizeof(_keyfile), "%s/%s", pwd->pw_dir,
                     79:                    KEYFILE);
1.1       millert    80:                keyfile = _keyfile;
                     81:        }
                     82:
1.2       millert    83:        if (init)
                     84:                readpassphrase("Enter Key: ", buf, sizeof(buf), 0);
                     85:        else if ((fp = fopen(keyfile, "r")) == NULL)
                     86:                err(1, "unable to open %s", keyfile);
                     87:        else {
1.1       millert    88:                if (fgets(buf, sizeof(buf), fp) == NULL) {
                     89:                        fprintf(stderr, "No key in %s\n", keyfile);
                     90:                        exit(1);
                     91:                }
                     92:                fclose(fp);
                     93:        }
                     94:
                     95:        memset(key, 0, sizeof(key));
                     96:        if (init && buf[3] == ' ') {
                     97:                char *b = buf;
                     98:                /* Assume octal input */
                     99:                for (i = 0; i < 8; ++i) {
1.4       millert   100:                        if (!*b)
1.1       millert   101:                                fprintf(stderr, "%s: invalid key\n", buf);
1.9     ! deraadt   102:                        while (isdigit((unsigned char)*b))
1.4       millert   103:                                key[i] = key[i] << 3 | (*b++ - '0');
1.9     ! deraadt   104:                        while (*b && !isdigit((unsigned char)*b))
1.1       millert   105:                                ++b;
                    106:                }
1.2       millert   107:        } else {
1.1       millert   108:                for (i = 0; i < 16; ++i) {
                    109:                        int d;
                    110:
1.9     ! deraadt   111:                        if (islower((unsigned char)buf[i]))
        !           112:                                buf[i] = toupper((unsigned char)buf[i]);
1.1       millert   113:                        if (buf[i] >= '0' && buf[i] <= '9')
                    114:                                d = buf[i] - '0';
                    115:                        else if (buf[i] >= 'A' && buf[i] <= 'F')
                    116:                                d = buf[i] - 'A' + 10;
                    117:                        else {
                    118:                                fprintf(stderr, "invalid key: %s\n", buf);
                    119:                                exit(1);
                    120:                        }
                    121:                        key[i>>1] |= d << ((i & 1) ? 0 : 4);
                    122:                }
1.2       millert   123:        }
1.1       millert   124:
1.2       millert   125:        /* XXX - should warn on non-space or non-digit */
                    126:        readpassphrase("Enter Pin: ", buf, sizeof(buf), 0);
                    127:        for (i = 0, pin = 0; buf[i] && buf[i] != '\n'; ++i)
1.9     ! deraadt   128:                if (isdigit((unsigned char)buf[i]))
1.1       millert   129:                        pin = pin * 16 + buf[i] - '0' + 1;
                    130:
1.2       millert   131:        if ((pin & 0xffff0000) == 0)
1.1       millert   132:                pin |= pin << 16;
                    133:
                    134:        for (i = 0; i < 8; ++i)
                    135:                key[0] ^= (pin >> ((i * 7) % 26)) & 0x7f;
                    136:
                    137:        if (init) {
1.2       millert   138:                if ((fp = fopen(keyfile, "w")) == NULL)
                    139:                        err(1, "could not open %s for writing", keyfile);
                    140:                fchmod(fileno(fp), 0600);
1.1       millert   141:                for (i = 0; i < 8; ++i) {
                    142:                        fprintf(fp, "%c", digits[(key[i]>>4)&0xf]);
                    143:                        fprintf(fp, "%c", digits[(key[i]>>0)&0xf]);
                    144:                }
1.2       millert   145:                fputc('\n', fp);
1.1       millert   146:                fclose(fp);
                    147:                exit(0);
                    148:        }
                    149:
1.8       jsg       150:        DES_fixup_key_parity(&key);
                    151:        DES_key_sched(&key, &ks);
1.1       millert   152:
1.2       millert   153:        buf[0] = '\0';
1.3       deraadt   154:        readpassphrase("Enter challenge: ", buf, sizeof(buf), RPP_ECHO_ON);
1.2       millert   155:        if (buf[0] == '\0')
1.1       millert   156:                exit(0);
                    157:
                    158:        for (i = 0; i < 8; ++i)
                    159:                if (buf[i] == '\n')
                    160:                        buf[i] = '\0';
                    161:
                    162:        if (!hex)
                    163:                digits = DECDIGITS;
                    164:
                    165:        predict(ks, buf, cnt);
                    166:
1.2       millert   167:        memset(&ks, 0, sizeof(ks));
                    168:        memset(buf, 0, sizeof(buf));
                    169:
1.1       millert   170:        exit(0);
                    171: }
                    172:
                    173: void
1.8       jsg       174: predict(DES_key_schedule ks, const char *chal, int cnt)
1.1       millert   175: {
                    176:        int i;
1.8       jsg       177:        DES_cblock cb;
1.1       millert   178:
1.4       millert   179:        memcpy(&cb, chal, sizeof(cb));
1.1       millert   180:        while (cnt-- > 0) {
1.4       millert   181:                printf("%.8s: ", (char *)cb);
1.8       jsg       182:                DES_ecb_encrypt(&cb, &cb, &ks, DES_ENCRYPT);
1.1       millert   183:                for (i = 0; i < 4; ++i) {
1.2       millert   184:                        printf("%c", digits[(cb[i]>>4) & 0xf]);
                    185:                        printf("%c", digits[(cb[i]>>0) & 0xf]);
1.1       millert   186:                }
1.2       millert   187:                putchar('\n');
1.1       millert   188:                for (i = 0; i < 8; ++i) {
1.2       millert   189:                        if ((cb[i] &= 0xf) > 9)
                    190:                                cb[i] -= 10;
                    191:                        cb[i] |= 0x30;
1.1       millert   192:                }
                    193:        }
1.2       millert   194:        memset(&cb, 0, sizeof(cb));
1.1       millert   195: }