otp.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 18 Jun 2017 01:57:23 -0400
changeset 55 0aaf56a96d21
child 56 a573346e6f7b
permissions -rw-r--r--
Added initial code for producing time-based One Time Passwords.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
#include <stdio.h>
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
#include <stdint.h>
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
#include <string.h>
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
#include <time.h>
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
#include "sha1.h"
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
static int base32_decode(const char *src, const int srclen, uint8_t *dst, const int dstlen)
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
{
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     9
    const int len = srclen == -1 ? strlen((const char *) src) : srclen;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
    int retval = 0;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
    uint32_t accum = 0;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
    int shifter = 0;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
    int i;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
    for (i = 0; i < len; i++) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
        const uint8_t ch = (uint8_t) src[i];
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    17
        uint8_t val;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    18
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
        if ((ch >= 'A') && (ch <= 'Z')) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
            val = ch - 'A';
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
        } else if ((ch >= '2') && (ch <= '7')) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
            val = (ch - '2') + 26;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
        } else if (ch == '=') {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
            val = 0;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
        /* these are illegal in base32, but GAuth keys might have them. */
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
        } else if (ch == ' ') {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    28
            continue;  /* skip these. */
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
        } else if ((ch >= 'a') && (ch <= 'z')) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
            val = ch - 'a'; /* treat like uppercase. */
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
        } else {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
            return -1;  /* invalid string. */
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
        }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
        accum = (accum << 5) | ((uint32_t) val);
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
        shifter += 5;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
        if (shifter >= 8) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
            if (retval > dstlen) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
                return -1;  /* dst too small */
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
            }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
            dst[retval] = (uint8_t) ((accum >> (shifter - 8)) & 0xFF);
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
            retval++;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
            shifter -= 8;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
        }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    46
    }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    47
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    48
    if (shifter > 0) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
        if (retval > dstlen) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
            return -1;  /* dst too small */
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
        }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    52
        dst[retval] = (uint8_t) (accum & 0xFF);
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
        retval++;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    54
    }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    55
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    56
    return retval;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
}
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    58
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    59
static int totp(const char *base32_secret, char *dst, int dstlen)
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    60
{
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
    uint8_t decoded[64];
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    int decodedlen;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    uint64_t secs;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    uint8_t timebytes[8];
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    unsigned char digest[SHA1_DIGEST_LENGTH];
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    66
    uint8_t *bytes;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    uint32_t val;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
    decodedlen = base32_decode(base32_secret, -1, decoded, sizeof (decoded));
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    if (decodedlen == -1) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    71
        return -1;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
    }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    73
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    74
    secs = ((uint64_t) time(NULL)) / 30;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    for (int i = 0; i < 8; i++) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    76
        timebytes[i] = (uint8_t) (secs >> ((7-i) * 8));
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    77
    }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    78
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
    SHA1Hmac(decoded, decodedlen, timebytes, 8, digest);
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    81
    bytes = (uint8_t *) (digest + (digest[SHA1_DIGEST_LENGTH-1] & 0xF));
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    82
    val = (((uint32_t) bytes[0]) << 24) | (((uint32_t) bytes[1]) << 16) |
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
          (((uint32_t) bytes[2]) << 8) | (((uint32_t) bytes[3]));
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    84
    val &= 0x7FFFFFFF; /* drop most significant bit. */
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    85
    val %= 1000000;  /* make it six digits long. */
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    86
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    87
    snprintf(dst, dstlen, "%06u", (unsigned int) val);
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    return 0;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    89
}
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    90
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    91
int main(int argc, char **argv)
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
{
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
    char result[16];
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
    int i;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
    for (i = 1; i < argc; i++) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
        printf("%s: ", argv[i]);
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
        if (totp(argv[i], result, sizeof (result)) == -1) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
            printf("[FAILED!]");
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
        } else {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
            printf("%s (valid for %d more seconds)", result, 30 - ((int) (time(NULL) % 30)));
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
        }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
        printf("\n");
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
    }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
    return 0;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
}
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
/* end of otp.c ... */
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108