otp.c
changeset 57 4974e5368a29
parent 56 a573346e6f7b
equal deleted inserted replaced
56:a573346e6f7b 57:4974e5368a29
    61 {
    61 {
    62     uint8_t decoded[64];
    62     uint8_t decoded[64];
    63     int decodedlen;
    63     int decodedlen;
    64     uint64_t secs;
    64     uint64_t secs;
    65     uint8_t timebytes[8];
    65     uint8_t timebytes[8];
    66     unsigned char digest[SHA1_DIGEST_LENGTH];
    66     uint8_t digest[SHA1_DIGEST_LENGTH];
    67     uint8_t *bytes;
    67     uint8_t *bytes;
    68     uint32_t val;
    68     uint32_t val;
    69 
    69 
    70     decodedlen = base32_decode(base32_secret, -1, decoded, sizeof (decoded));
    70     decodedlen = base32_decode(base32_secret, -1, decoded, sizeof (decoded));
    71     if (decodedlen == -1) {
    71     if (decodedlen == -1) {
    77         timebytes[i] = (uint8_t) (secs >> ((7-i) * 8));
    77         timebytes[i] = (uint8_t) (secs >> ((7-i) * 8));
    78     }
    78     }
    79 
    79 
    80     SHA1Hmac(decoded, decodedlen, timebytes, 8, digest);
    80     SHA1Hmac(decoded, decodedlen, timebytes, 8, digest);
    81 
    81 
    82     bytes = (uint8_t *) (digest + (digest[SHA1_DIGEST_LENGTH-1] & 0xF));
    82     bytes = digest + (digest[SHA1_DIGEST_LENGTH-1] & 0xF);
    83     val = (((uint32_t) bytes[0]) << 24) | (((uint32_t) bytes[1]) << 16) |
    83     val = (((uint32_t) bytes[0]) << 24) | (((uint32_t) bytes[1]) << 16) |
    84           (((uint32_t) bytes[2]) << 8) | (((uint32_t) bytes[3]));
    84           (((uint32_t) bytes[2]) << 8) | (((uint32_t) bytes[3]));
    85     val &= 0x7FFFFFFF; /* drop most significant bit. */
    85     val &= 0x7FFFFFFF; /* drop most significant bit. */
    86     val %= 1000000;  /* make it six digits long. */
    86     val %= 1000000;  /* make it six digits long. */
    87 
    87