otp.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 18 Jun 2017 19:50:43 -0400
changeset 57 4974e5368a29
parent 56 a573346e6f7b
permissions -rw-r--r--
Minor type cleanup ("unsigned char" -> "uint8_t").
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"
56
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 55
diff changeset
     6
#include "otp.h"
55
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
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
     9
{
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
    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
    11
    int retval = 0;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
    uint32_t accum = 0;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    13
    int shifter = 0;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    14
    int i;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    15
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    16
    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
    17
        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
    18
        uint8_t val;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    19
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    20
        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
    21
            val = ch - 'A';
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
        } 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
    23
            val = (ch - '2') + 26;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
        } else if (ch == '=') {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
            val = 0;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
        /* 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
    28
        } else if (ch == ' ') {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    29
            continue;  /* skip these. */
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
        } 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
    31
            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
    32
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
        } else {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    34
            return -1;  /* invalid string. */
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
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
        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
    38
        shifter += 5;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
        if (shifter >= 8) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
            if (retval > dstlen) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
                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
    42
            }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
            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
    44
            retval++;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
            shifter -= 8;
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
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    49
    if (shifter > 0) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    50
        if (retval > dstlen) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    51
            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
    52
        }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    53
        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
    54
        retval++;
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
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    57
    return retval;
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
56
a573346e6f7b Added One Time Password support.
Ryan C. Gordon <icculus@icculus.org>
parents: 55
diff changeset
    60
int totp(const char *base32_secret, char *dst, int dstlen)
55
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    61
{
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    62
    uint8_t decoded[64];
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    63
    int decodedlen;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    64
    uint64_t secs;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    65
    uint8_t timebytes[8];
57
4974e5368a29 Minor type cleanup ("unsigned char" -> "uint8_t").
Ryan C. Gordon <icculus@icculus.org>
parents: 56
diff changeset
    66
    uint8_t digest[SHA1_DIGEST_LENGTH];
55
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    67
    uint8_t *bytes;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    68
    uint32_t val;
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    69
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    70
    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
    71
    if (decodedlen == -1) {
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    72
        return -1;
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
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    75
    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
    76
    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
    77
        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
    78
    }
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    79
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    80
    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
    81
57
4974e5368a29 Minor type cleanup ("unsigned char" -> "uint8_t").
Ryan C. Gordon <icculus@icculus.org>
parents: 56
diff changeset
    82
    bytes = digest + (digest[SHA1_DIGEST_LENGTH-1] & 0xF);
55
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    83
    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
    84
          (((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
    85
    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
    86
    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
    87
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    88
    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
    89
    return 0;
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
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    92
/* end of otp.c ... */
0aaf56a96d21 Added initial code for producing time-based One Time Passwords.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93