Welcome to little lamb

Code » ssp » master » tree

[master] / src / ssp / hotp.c

/* This file is part of ssp                             https://lila.oss/ssp
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <limb/hmac.h>
#include "hotp.h"

int
hotp(hasher *hr, const char *secret, size_t slen, u64 c, int digits)
{
    char buf[hr->hlen];
    u64p_be(&c);

    hmac(buf, hr, secret, slen, &c, sizeof(c));

    u64 tenpow[] = { 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000 };
    int offset = buf[hr->hlen - 1] & 0xf;
    int r = (buf[offset + 0] & 0x7f) << 24
        |   (buf[offset + 1] & 0xff) << 16
        |   (buf[offset + 2] & 0xff) <<  8
        |   (buf[offset + 3] & 0xff);
    return r % tenpow[digits - 5];
}