2024-06-06 14:41:44 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2024 Lucas Gabriel Vuotto <lucas@lgv5.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2024-06-17 23:52:07 +02:00
|
|
|
#include "internal.h"
|
2024-06-06 14:41:44 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define HMAC_IPAD UINT8_C(0x36)
|
|
|
|
#define HMAC_OPAD UINT8_C(0x5c)
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2024-06-10 19:29:29 +02:00
|
|
|
hmac_init(void *arg, void *initparams)
|
2024-06-06 14:41:44 +02:00
|
|
|
{
|
2024-06-15 23:13:31 +02:00
|
|
|
struct hmac_state *state = arg;
|
2024-06-10 19:29:29 +02:00
|
|
|
struct lc_hmac_params *params = initparams;
|
|
|
|
uint8_t ikeypad[HMAC_BLOCKLEN_MAX];
|
|
|
|
size_t i, olen, blen, keylen;
|
2024-06-06 14:41:44 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
state->hash = params->hash;
|
2024-06-10 19:29:29 +02:00
|
|
|
keylen = params->keylen;
|
|
|
|
blen = params->hash->impl->blocklen;
|
|
|
|
|
|
|
|
if (keylen > blen) {
|
2024-06-15 23:13:31 +02:00
|
|
|
if (!lc_hash(state->hash->impl, state->key, &olen, params->key,
|
2024-06-10 19:29:29 +02:00
|
|
|
keylen))
|
2024-06-06 14:41:44 +02:00
|
|
|
return 0;
|
2024-06-06 19:14:30 +02:00
|
|
|
keylen = olen;
|
|
|
|
} else
|
2024-06-06 14:41:44 +02:00
|
|
|
for (i = 0; i < keylen; i++)
|
2024-06-15 23:13:31 +02:00
|
|
|
state->key[i] = params->key[i];
|
2024-06-06 19:14:30 +02:00
|
|
|
|
2024-06-10 19:29:29 +02:00
|
|
|
for (i = keylen; i < blen; i++)
|
2024-06-15 23:13:31 +02:00
|
|
|
state->key[i] = 0;
|
2024-06-06 14:41:44 +02:00
|
|
|
|
2024-06-10 19:29:29 +02:00
|
|
|
for (i = 0; i < blen; i++)
|
2024-06-15 23:13:31 +02:00
|
|
|
ikeypad[i] = state->key[i] ^ HMAC_IPAD;
|
2024-06-06 14:41:44 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
return lc_hash_init(state->hash) &&
|
|
|
|
lc_hash_update(state->hash, ikeypad, blen);
|
2024-06-06 14:41:44 +02:00
|
|
|
}
|
|
|
|
|
2024-06-07 20:40:14 +02:00
|
|
|
static int
|
2024-06-06 14:41:44 +02:00
|
|
|
hmac_update(void *arg, const uint8_t *in, size_t inlen)
|
|
|
|
{
|
2024-06-15 23:13:31 +02:00
|
|
|
struct hmac_state *state = arg;
|
2024-06-06 14:41:44 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
return lc_hash_update(state->hash, in, inlen);
|
2024-06-06 14:41:44 +02:00
|
|
|
}
|
|
|
|
|
2024-06-07 20:40:14 +02:00
|
|
|
static int
|
2024-06-06 14:41:44 +02:00
|
|
|
hmac_final(void *arg, uint8_t *out, size_t *outlen)
|
|
|
|
{
|
2024-06-15 23:13:31 +02:00
|
|
|
struct hmac_state *state = arg;
|
|
|
|
uint8_t m[HMAC_BLOCKLEN_MAX],
|
|
|
|
okeypad[HMAC_BLOCKLEN_MAX];
|
|
|
|
size_t i, olen, blen;
|
|
|
|
int rc;
|
2024-06-06 14:41:44 +02:00
|
|
|
|
|
|
|
if (out == NULL) {
|
2024-06-15 23:13:31 +02:00
|
|
|
*outlen = state->hash->impl->hashlen;
|
2024-06-06 14:41:44 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*outlen = 0;
|
2024-06-15 23:13:31 +02:00
|
|
|
blen = state->hash->impl->blocklen;
|
2024-06-10 19:29:29 +02:00
|
|
|
|
|
|
|
for (i = 0; i < blen; i++)
|
2024-06-15 23:13:31 +02:00
|
|
|
okeypad[i] = state->key[i] ^ HMAC_OPAD;
|
2024-06-06 14:41:44 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
rc = lc_hash_final(state->hash, m, &olen) &&
|
|
|
|
lc_hash_init(state->hash) &&
|
|
|
|
lc_hash_update(state->hash, okeypad, blen) &&
|
|
|
|
lc_hash_update(state->hash, m, olen) &&
|
|
|
|
lc_hash_final(state->hash, out, outlen);
|
2024-06-06 14:41:44 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
lc_scrub(state, sizeof(*state));
|
2024-06-06 14:41:44 +02:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2024-06-10 19:29:29 +02:00
|
|
|
static int
|
|
|
|
hmac_auth(uint8_t *out, size_t *outlen, void *initparams, const uint8_t *in,
|
|
|
|
size_t inlen)
|
2024-06-07 05:14:21 +02:00
|
|
|
{
|
2024-06-10 19:29:29 +02:00
|
|
|
struct lc_hmac_params *params = initparams;
|
2024-06-15 23:13:31 +02:00
|
|
|
struct hmac_state state;
|
2024-06-07 05:14:21 +02:00
|
|
|
|
2024-06-10 19:29:29 +02:00
|
|
|
if (out == NULL) {
|
|
|
|
*outlen = params->hash->impl->hashlen;
|
|
|
|
return 1;
|
|
|
|
}
|
2024-06-07 05:14:21 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
return hmac_init(&state, initparams) &&
|
|
|
|
hmac_update(&state, in, inlen) &&
|
|
|
|
hmac_final(&state, out, outlen);
|
2024-06-06 14:41:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-07 05:14:21 +02:00
|
|
|
const struct lc_auth_impl *
|
2024-06-10 19:29:29 +02:00
|
|
|
lc_auth_impl_hmac(void)
|
2024-06-06 14:41:44 +02:00
|
|
|
{
|
2024-06-16 03:51:00 +02:00
|
|
|
static struct lc_auth_impl hmac_impl = {
|
|
|
|
.init = &hmac_init,
|
|
|
|
.update = &hmac_update,
|
|
|
|
.final = &hmac_final,
|
|
|
|
.auth = &hmac_auth,
|
|
|
|
|
|
|
|
.argsz = sizeof(struct hmac_state),
|
|
|
|
.blocklen = 0,
|
|
|
|
.taglen = 0,
|
|
|
|
};
|
|
|
|
|
2024-06-10 19:29:29 +02:00
|
|
|
return &hmac_impl;
|
2024-06-06 14:41:44 +02:00
|
|
|
}
|