2024-05-31 12:59:58 +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-06 00:05:37 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2024-05-31 12:59:58 +02:00
|
|
|
#include "lilcrypto.h"
|
|
|
|
#include "auth.h"
|
|
|
|
#include "impl_poly1305.h"
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
|
2024-06-07 20:40:14 +02:00
|
|
|
static int
|
2024-06-09 17:09:15 +02:00
|
|
|
poly1305_init(void *arg, void *initparams)
|
2024-05-31 12:59:58 +02:00
|
|
|
{
|
2024-06-09 17:09:15 +02:00
|
|
|
struct lc_poly1305_params *params = initparams;
|
2024-06-15 23:13:31 +02:00
|
|
|
struct poly1305_state *state = arg;
|
2024-06-07 20:40:14 +02:00
|
|
|
size_t i;
|
|
|
|
uint32_t t0, t1, t2, t3;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
state->h0 = 0;
|
|
|
|
state->h1 = 0;
|
|
|
|
state->h2 = 0;
|
|
|
|
state->h3 = 0;
|
|
|
|
state->h4 = 0;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-07 20:40:14 +02:00
|
|
|
t0 = load32le(¶ms->key[0]);
|
|
|
|
t1 = load32le(¶ms->key[4]);
|
|
|
|
t2 = load32le(¶ms->key[8]);
|
|
|
|
t3 = load32le(¶ms->key[12]);
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
state->r0 = t0 & 0x3ffffff;
|
|
|
|
state->r1 = ((t1 << 6) | (t0 >> 26)) & 0x3ffff03;
|
|
|
|
state->r2 = ((t2 << 12) | (t1 >> 20)) & 0x3ffc0ff;
|
|
|
|
state->r3 = ((t3 << 18) | (t2 >> 14)) & 0x3f03fff;
|
|
|
|
state->r4 = (t3 >> 8) & 0xfffff;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
state->x1 = 5 * state->r1;
|
|
|
|
state->x2 = 5 * state->r2;
|
|
|
|
state->x3 = 5 * state->r3;
|
|
|
|
state->x4 = 5 * state->r4;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
state->s0 = load32le(¶ms->key[16]);
|
|
|
|
state->s1 = load32le(¶ms->key[20]);
|
|
|
|
state->s2 = load32le(¶ms->key[24]);
|
|
|
|
state->s3 = load32le(¶ms->key[28]);
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-15 23:22:12 +02:00
|
|
|
state->blen = 0;
|
2024-06-09 16:16:18 +02:00
|
|
|
for (i = 0; i < LC_POLY1305_BLOCKLEN; i++)
|
2024-06-15 23:22:12 +02:00
|
|
|
state->b[i] = 0;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-07 20:40:14 +02:00
|
|
|
static int
|
2024-05-31 12:59:58 +02:00
|
|
|
poly1305_update(void *arg, const uint8_t *in, size_t inlen)
|
|
|
|
{
|
2024-06-15 23:13:31 +02:00
|
|
|
struct poly1305_state *state = arg;
|
2024-05-31 12:59:58 +02:00
|
|
|
size_t i;
|
|
|
|
|
2024-06-15 23:22:12 +02:00
|
|
|
for (i = 0; i + state->blen < LC_POLY1305_BLOCKLEN && i < inlen; i++)
|
|
|
|
state->b[i + state->blen] = in[i];
|
|
|
|
state->blen += i;
|
2024-05-31 12:59:58 +02:00
|
|
|
in += i;
|
|
|
|
inlen -= i;
|
|
|
|
|
2024-06-15 23:22:12 +02:00
|
|
|
if (state->blen == LC_POLY1305_BLOCKLEN) {
|
2024-06-15 23:13:31 +02:00
|
|
|
poly1305_block(state, 1);
|
2024-06-15 23:22:12 +02:00
|
|
|
state->blen = 0;
|
2024-05-31 12:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (inlen == 0)
|
|
|
|
return 1;
|
|
|
|
|
2024-06-09 16:16:18 +02:00
|
|
|
while (inlen >= LC_POLY1305_BLOCKLEN) {
|
|
|
|
for (i = 0; i < LC_POLY1305_BLOCKLEN; i++)
|
2024-06-15 23:22:12 +02:00
|
|
|
state->b[i] = in[i];
|
2024-06-15 23:13:31 +02:00
|
|
|
poly1305_block(state, 1);
|
2024-05-31 12:59:58 +02:00
|
|
|
|
2024-06-09 16:16:18 +02:00
|
|
|
in += LC_POLY1305_BLOCKLEN;
|
|
|
|
inlen -= LC_POLY1305_BLOCKLEN;
|
2024-05-31 12:59:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < inlen; i++)
|
2024-06-15 23:22:12 +02:00
|
|
|
state->b[i] = in[i];
|
|
|
|
state->blen = inlen;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-07 20:40:14 +02:00
|
|
|
static int
|
2024-05-31 12:59:58 +02:00
|
|
|
poly1305_final(void *arg, uint8_t *out, size_t *outlen)
|
|
|
|
{
|
2024-06-15 23:13:31 +02:00
|
|
|
struct poly1305_state *state = arg;
|
2024-05-31 12:59:58 +02:00
|
|
|
uint32_t tag[POLY1305_TAGLEN_WORDS];
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
*outlen = LC_POLY1305_TAGLEN;
|
|
|
|
if (out == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2024-06-15 23:22:12 +02:00
|
|
|
i = state->blen;
|
2024-05-31 12:59:58 +02:00
|
|
|
if (i > 0) {
|
2024-06-09 16:16:18 +02:00
|
|
|
if (i < LC_POLY1305_BLOCKLEN) {
|
2024-06-15 23:22:12 +02:00
|
|
|
state->b[i++] = 1;
|
2024-06-09 16:16:18 +02:00
|
|
|
for (; i < LC_POLY1305_BLOCKLEN; i++)
|
2024-06-15 23:22:12 +02:00
|
|
|
state->b[i] = 0;
|
2024-06-15 23:13:31 +02:00
|
|
|
poly1305_block(state, 0);
|
2024-05-31 12:59:58 +02:00
|
|
|
} else
|
2024-06-15 23:13:31 +02:00
|
|
|
poly1305_block(state, 1);
|
2024-05-31 12:59:58 +02:00
|
|
|
}
|
2024-06-15 23:13:31 +02:00
|
|
|
poly1305_reduce(state, tag);
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
store32le(&out[0], tag[0]);
|
|
|
|
store32le(&out[4], tag[1]);
|
|
|
|
store32le(&out[8], tag[2]);
|
|
|
|
store32le(&out[12], tag[3]);
|
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
lc_scrub(state, sizeof(*state));
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2024-06-09 17:09:15 +02:00
|
|
|
poly1305_auth(uint8_t *out, size_t *outlen, void *initparams,
|
2024-05-31 12:59:58 +02:00
|
|
|
const uint8_t *in, size_t inlen)
|
|
|
|
{
|
2024-06-15 23:13:31 +02:00
|
|
|
struct poly1305_state state;
|
2024-05-31 12:59:58 +02:00
|
|
|
|
|
|
|
if (out == NULL) {
|
|
|
|
*outlen = LC_POLY1305_TAGLEN;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-15 23:13:31 +02:00
|
|
|
return poly1305_init(&state, initparams) &&
|
|
|
|
poly1305_update(&state, in, inlen) &&
|
|
|
|
poly1305_final(&state, out, outlen);
|
2024-05-31 12:59:58 +02:00
|
|
|
}
|
|
|
|
|
2024-06-06 13:45:30 +02:00
|
|
|
|
2024-05-31 12:59:58 +02:00
|
|
|
const struct lc_auth_impl *
|
|
|
|
lc_auth_impl_poly1305(void)
|
|
|
|
{
|
2024-06-16 03:51:00 +02:00
|
|
|
static struct lc_auth_impl poly1305_impl = {
|
|
|
|
.init = &poly1305_init,
|
|
|
|
.update = &poly1305_update,
|
|
|
|
.final = &poly1305_final,
|
|
|
|
.auth = &poly1305_auth,
|
|
|
|
|
|
|
|
.argsz = sizeof(struct poly1305_state),
|
|
|
|
.blocklen = LC_POLY1305_BLOCKLEN,
|
|
|
|
.taglen = LC_POLY1305_TAGLEN,
|
|
|
|
};
|
|
|
|
|
2024-05-31 12:59:58 +02:00
|
|
|
return &poly1305_impl;
|
|
|
|
}
|