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