Make the block lengths part of the public interface

This commit is contained in:
Lucas Gabriel Vuotto 2024-06-09 14:16:18 +00:00
parent 06e9c5ec67
commit f1c4ceea84
11 changed files with 64 additions and 61 deletions

View File

@ -67,8 +67,8 @@ chacha20_poly1305_seal(uint8_t *out, size_t *outlen, const void *initparams,
inlen > SIZE_MAX - LC_POLY1305_TAGLEN)
return 0;
/* Counter 0 is used for deriving Poly1305 key. */
if (inlen > SIZE_MAX - (CHACHA20_BLOCKLEN - 1) ||
(inlen + CHACHA20_BLOCKLEN - 1) / CHACHA20_BLOCKLEN >
if (inlen > SIZE_MAX - (LC_CHACHA20_BLOCKLEN - 1) ||
(inlen + LC_CHACHA20_BLOCKLEN - 1) / LC_CHACHA20_BLOCKLEN >
CHACHA20_CTRMAX - 1)
return 0;
@ -156,8 +156,8 @@ xchacha20_poly1305_seal(uint8_t *out, size_t *outlen, const void *initparams,
inlen > SIZE_MAX - LC_POLY1305_TAGLEN)
return 0;
/* Counter 0 is used for deriving Poly1305 key. */
if (inlen > SIZE_MAX - (CHACHA20_BLOCKLEN - 1) ||
(inlen + CHACHA20_BLOCKLEN - 1) / CHACHA20_BLOCKLEN >
if (inlen > SIZE_MAX - (LC_CHACHA20_BLOCKLEN - 1) ||
(inlen + LC_CHACHA20_BLOCKLEN - 1) / LC_CHACHA20_BLOCKLEN >
CHACHA20_CTRMAX - 1)
return 0;
@ -246,8 +246,8 @@ chacha20_poly1305_open(uint8_t *out, size_t *outlen, const void *initparams,
inlen > UINT64_MAX || aadlen > UINT64_MAX)
return 0;
/* Counter 0 is used for deriving Poly1305 key. */
if (inlen > SIZE_MAX - (CHACHA20_BLOCKLEN - 1) ||
(inlen + CHACHA20_BLOCKLEN - 1) / CHACHA20_BLOCKLEN >
if (inlen > SIZE_MAX - (LC_CHACHA20_BLOCKLEN - 1) ||
(inlen + LC_CHACHA20_BLOCKLEN - 1) / LC_CHACHA20_BLOCKLEN >
CHACHA20_CTRMAX - 1) {
return 0;
}
@ -341,8 +341,8 @@ xchacha20_poly1305_open(uint8_t *out, size_t *outlen, const void *initparams,
inlen > UINT64_MAX || aadlen > UINT64_MAX)
return 0;
/* Counter 0 is used for deriving Poly1305 key. */
if (inlen > SIZE_MAX - (CHACHA20_BLOCKLEN - 1) ||
(inlen + CHACHA20_BLOCKLEN - 1) / CHACHA20_BLOCKLEN >
if (inlen > SIZE_MAX - (LC_CHACHA20_BLOCKLEN - 1) ||
(inlen + LC_CHACHA20_BLOCKLEN - 1) / LC_CHACHA20_BLOCKLEN >
CHACHA20_CTRMAX - 1) {
return 0;
}

View File

@ -63,7 +63,7 @@ hmac_sha224_sha256_init(void *arg, const void *initparams)
const struct lc_hmac_params *params = initparams;
struct hmac_ctx *ctx = arg;
ctx->blocksz = SHA256_BLOCKLEN;
ctx->blocksz = LC_SHA256_BLOCKLEN;
return hmac_common_init(ctx, params->key, params->keylen);
}
@ -74,7 +74,7 @@ hmac_sha384_sha512_init(void *arg, const void *initparams)
const struct lc_hmac_params *params = initparams;
struct hmac_ctx *ctx = arg;
ctx->blocksz = SHA512_BLOCKLEN;
ctx->blocksz = LC_SHA512_BLOCKLEN;
return hmac_common_init(ctx, params->key, params->keylen);
}

View File

@ -59,7 +59,7 @@ poly1305_init(void *arg, const void *initparams)
ctx->s3 = load32le(&params->key[28]);
ctx->mlen = 0;
for (i = 0; i < POLY1305_BLOCKLEN; i++)
for (i = 0; i < LC_POLY1305_BLOCKLEN; i++)
ctx->m[i] = 0;
return 1;
@ -71,13 +71,13 @@ poly1305_update(void *arg, const uint8_t *in, size_t inlen)
struct poly1305_ctx *ctx = arg;
size_t i;
for (i = 0; i + ctx->mlen < POLY1305_BLOCKLEN && i < inlen; i++)
for (i = 0; i + ctx->mlen < LC_POLY1305_BLOCKLEN && i < inlen; i++)
ctx->m[i + ctx->mlen] = in[i];
ctx->mlen += i;
in += i;
inlen -= i;
if (ctx->mlen == POLY1305_BLOCKLEN) {
if (ctx->mlen == LC_POLY1305_BLOCKLEN) {
poly1305_block(ctx, 1);
ctx->mlen = 0;
}
@ -85,13 +85,13 @@ poly1305_update(void *arg, const uint8_t *in, size_t inlen)
if (inlen == 0)
return 1;
while (inlen >= POLY1305_BLOCKLEN) {
for (i = 0; i < POLY1305_BLOCKLEN; i++)
while (inlen >= LC_POLY1305_BLOCKLEN) {
for (i = 0; i < LC_POLY1305_BLOCKLEN; i++)
ctx->m[i] = in[i];
poly1305_block(ctx, 1);
in += POLY1305_BLOCKLEN;
inlen -= POLY1305_BLOCKLEN;
in += LC_POLY1305_BLOCKLEN;
inlen -= LC_POLY1305_BLOCKLEN;
}
for (i = 0; i < inlen; i++)
@ -114,9 +114,9 @@ poly1305_final(void *arg, uint8_t *out, size_t *outlen)
i = ctx->mlen;
if (i > 0) {
if (i < POLY1305_BLOCKLEN) {
if (i < LC_POLY1305_BLOCKLEN) {
ctx->m[i++] = 1;
for (; i < POLY1305_BLOCKLEN; i++)
for (; i < LC_POLY1305_BLOCKLEN; i++)
ctx->m[i] = 0;
poly1305_block(ctx, 0);
} else

View File

@ -91,25 +91,25 @@ chacha20_anycrypt_update(void *arg, uint8_t *out, size_t *outlen,
uint32_t h;
*outlen = 0;
if (inlen > SIZE_MAX - (CHACHA20_BLOCKLEN - 1) - ctx->mlen)
if (inlen > SIZE_MAX - (LC_CHACHA20_BLOCKLEN - 1) - ctx->mlen)
return 0;
blocks = (inlen + ctx->mlen + CHACHA20_BLOCKLEN - 1) /
CHACHA20_BLOCKLEN;
blocks = (inlen + ctx->mlen + LC_CHACHA20_BLOCKLEN - 1) /
LC_CHACHA20_BLOCKLEN;
if (blocks + ctx->n[0] > CHACHA20_CTRMAX)
return 0;
*outlen = ctx->mlen + inlen -
((ctx->mlen + inlen) % CHACHA20_BLOCKLEN);
((ctx->mlen + inlen) % LC_CHACHA20_BLOCKLEN);
if (out == NULL)
return 1;
for (i = 0; i + ctx->mlen < CHACHA20_BLOCKLEN && i < inlen; i++)
for (i = 0; i + ctx->mlen < LC_CHACHA20_BLOCKLEN && i < inlen; i++)
ctx->m[i + ctx->mlen] = in[i];
ctx->mlen += i;
in += i;
inlen -= i;
if (ctx->mlen == CHACHA20_BLOCKLEN) {
if (ctx->mlen == LC_CHACHA20_BLOCKLEN) {
chacha20_block(ctx);
ctx->n[0]++;
@ -118,14 +118,14 @@ chacha20_anycrypt_update(void *arg, uint8_t *out, size_t *outlen,
h ^= ctx->s[i];
store32le(&out[i * 4], h);
}
out += CHACHA20_BLOCKLEN;
out += LC_CHACHA20_BLOCKLEN;
ctx->mlen = 0;
}
if (inlen == 0)
return 1;
while (inlen >= CHACHA20_BLOCKLEN) {
while (inlen >= LC_CHACHA20_BLOCKLEN) {
chacha20_block(ctx);
ctx->n[0]++;
@ -134,9 +134,9 @@ chacha20_anycrypt_update(void *arg, uint8_t *out, size_t *outlen,
h ^= ctx->s[i];
store32le(&out[i * 4], h);
}
out += CHACHA20_BLOCKLEN;
in += CHACHA20_BLOCKLEN;
inlen -= CHACHA20_BLOCKLEN;
out += LC_CHACHA20_BLOCKLEN;
in += LC_CHACHA20_BLOCKLEN;
inlen -= LC_CHACHA20_BLOCKLEN;
}
for (i = 0; i < inlen; i++)
@ -189,8 +189,8 @@ chacha20_anycrypt(uint8_t *out, size_t *outlen, const void *initparams,
*outlen = 0;
if (inlen > SIZE_MAX - (CHACHA20_BLOCKLEN - 1) ||
(inlen + CHACHA20_BLOCKLEN - 1) / CHACHA20_BLOCKLEN >
if (inlen > SIZE_MAX - (LC_CHACHA20_BLOCKLEN - 1) ||
(inlen + LC_CHACHA20_BLOCKLEN - 1) / LC_CHACHA20_BLOCKLEN >
CHACHA20_CTRMAX)
return 0;

View File

@ -71,7 +71,7 @@ sha224_init(void *arg)
ctx->sz = 0;
ctx->mlen = 0;
for (i = 0; i < SHA256_BLOCKLEN; i++)
for (i = 0; i < LC_SHA256_BLOCKLEN; i++)
ctx->m[i] = 0;
return 1;
@ -95,7 +95,7 @@ sha256_init(void *arg)
ctx->sz = 0;
ctx->mlen = 0;
for (i = 0; i < SHA256_BLOCKLEN; i++)
for (i = 0; i < LC_SHA256_BLOCKLEN; i++)
ctx->m[i] = 0;
return 1;
@ -111,13 +111,13 @@ sha224_sha256_update(void *arg, const uint8_t *in, size_t inlen)
return 0;
ctx->sz += inlen;
for (i = 0; i + ctx->mlen < SHA256_BLOCKLEN && i < inlen; i++)
for (i = 0; i + ctx->mlen < LC_SHA256_BLOCKLEN && i < inlen; i++)
ctx->m[i + ctx->mlen] = in[i];
ctx->mlen += i;
in += i;
inlen -= i;
if (ctx->mlen == SHA256_BLOCKLEN) {
if (ctx->mlen == LC_SHA256_BLOCKLEN) {
sha256_block(ctx);
ctx->mlen = 0;
}
@ -125,8 +125,8 @@ sha224_sha256_update(void *arg, const uint8_t *in, size_t inlen)
if (inlen == 0)
return 1;
while (inlen >= SHA256_BLOCKLEN) {
for (i = 0; i < SHA256_BLOCKLEN; i++)
while (inlen >= LC_SHA256_BLOCKLEN) {
for (i = 0; i < LC_SHA256_BLOCKLEN; i++)
ctx->m[i] = in[i];
in += i;
inlen -= i;
@ -161,14 +161,14 @@ sha224_sha256_final(struct sha256_ctx *ctx)
mlen = ctx->mlen;
ctx->m[mlen++] = 0x80;
if (mlen >= SHA256_BLOCKLEN - sizeof(uint64_t)) {
for (i = mlen; i < SHA256_BLOCKLEN; i++)
if (mlen >= LC_SHA256_BLOCKLEN - sizeof(uint64_t)) {
for (i = mlen; i < LC_SHA256_BLOCKLEN; i++)
ctx->m[i] = 0;
sha256_block(ctx);
mlen = 0;
}
for (i = mlen; i < SHA256_BLOCKLEN - sizeof(uint64_t); i++)
for (i = mlen; i < LC_SHA256_BLOCKLEN - sizeof(uint64_t); i++)
ctx->m[i] = 0;
store64be(&ctx->m[i], ctx->sz << 3);
sha256_block(ctx);

View File

@ -72,7 +72,7 @@ sha384_init(void *arg)
ctx->szhi = ctx->szlo = 0;
ctx->mlen = 0;
for (i = 0; i < SHA512_BLOCKLEN; i++)
for (i = 0; i < LC_SHA512_BLOCKLEN; i++)
ctx->m[i] = 0;
return 1;
@ -96,7 +96,7 @@ sha512_init(void *arg)
ctx->szhi = ctx->szlo = 0;
ctx->mlen = 0;
for (i = 0; i < SHA512_BLOCKLEN; i++)
for (i = 0; i < LC_SHA512_BLOCKLEN; i++)
ctx->m[i] = 0;
return 1;
@ -116,13 +116,13 @@ sha384_sha512_update(void *arg, const uint8_t *in, size_t inlen)
} else
ctx->szlo += inlen;
for (i = 0; i + ctx->mlen < SHA512_BLOCKLEN && i < inlen; i++)
for (i = 0; i + ctx->mlen < LC_SHA512_BLOCKLEN && i < inlen; i++)
ctx->m[i + ctx->mlen] = in[i];
ctx->mlen += i;
in += i;
inlen -= i;
if (ctx->mlen == SHA512_BLOCKLEN) {
if (ctx->mlen == LC_SHA512_BLOCKLEN) {
sha512_block(ctx);
ctx->mlen = 0;
}
@ -130,8 +130,8 @@ sha384_sha512_update(void *arg, const uint8_t *in, size_t inlen)
if (inlen == 0)
return 1;
while (inlen >= SHA512_BLOCKLEN) {
for (i = 0; i < SHA512_BLOCKLEN; i++)
while (inlen >= LC_SHA512_BLOCKLEN) {
for (i = 0; i < LC_SHA512_BLOCKLEN; i++)
ctx->m[i] = in[i];
in += i;
inlen -= i;
@ -166,14 +166,14 @@ sha384_sha512_final(struct sha512_ctx *ctx)
mlen = ctx->mlen;
ctx->m[mlen++] = 0x80;
if (mlen >= SHA512_BLOCKLEN - 2 * sizeof(uint64_t)) {
for (i = mlen; i < SHA512_BLOCKLEN; i++)
if (mlen >= LC_SHA512_BLOCKLEN - 2 * sizeof(uint64_t)) {
for (i = mlen; i < LC_SHA512_BLOCKLEN; i++)
ctx->m[i] = 0;
sha512_block(ctx);
mlen = 0;
}
for (i = mlen; i < SHA512_BLOCKLEN - 2 * sizeof(uint64_t); i++)
for (i = mlen; i < LC_SHA512_BLOCKLEN - 2 * sizeof(uint64_t); i++)
ctx->m[i] = 0;
store64be(&ctx->m[i], (ctx->szhi << 3) | (ctx->szlo >> 63));
store64be(&ctx->m[i + sizeof(uint64_t)], ctx->szlo << 3);

View File

@ -20,8 +20,7 @@
#include "lilcrypto.h"
#define CHACHA20_BLOCKLEN 64
#define CHACHA20_BLOCKLEN_WORDS (CHACHA20_BLOCKLEN / sizeof(uint32_t))
#define CHACHA20_BLOCKLEN_WORDS (LC_CHACHA20_BLOCKLEN / sizeof(uint32_t))
#define CHACHA20_CTRMAX 4294967295 /* 2^32 - 1 */
#define CHACHA20_KEY_WORDS (LC_CHACHA20_KEYLEN / sizeof(uint32_t))
#define CHACHA20_NONCE_WORDS 4
@ -33,7 +32,7 @@ struct chacha20_ctx {
uint32_t k[CHACHA20_KEY_WORDS];
uint32_t n[CHACHA20_NONCE_WORDS];
size_t mlen;
uint8_t m[CHACHA20_BLOCKLEN];
uint8_t m[LC_CHACHA20_BLOCKLEN];
};

View File

@ -20,7 +20,6 @@
#include "lilcrypto.h"
#define POLY1305_BLOCKLEN 16
#define POLY1305_TAGLEN_WORDS (LC_POLY1305_TAGLEN / sizeof(uint32_t))
@ -30,7 +29,7 @@ struct poly1305_ctx {
uint32_t x1, x2, x3, x4;
uint32_t s0, s1, s2, s3;
size_t mlen;
uint8_t m[POLY1305_BLOCKLEN];
uint8_t m[LC_POLY1305_BLOCKLEN];
};

View File

@ -20,8 +20,7 @@
#include "lilcrypto.h"
#define SHA256_BLOCKLEN 64
#define SHA256_BLOCKLEN_WORDS (SHA256_BLOCKLEN / sizeof(uint32_t))
#define SHA256_BLOCKLEN_WORDS (LC_SHA256_BLOCKLEN / sizeof(uint32_t))
#define SHA256_ROUNDS 64
@ -29,7 +28,7 @@ struct sha256_ctx {
uint32_t h0, h1, h2, h3, h4, h5, h6, h7;
uint64_t sz;
size_t mlen;
uint8_t m[SHA256_BLOCKLEN];
uint8_t m[LC_SHA256_BLOCKLEN];
};

View File

@ -20,8 +20,7 @@
#include "lilcrypto.h"
#define SHA512_BLOCKLEN 128
#define SHA512_BLOCKLEN_WORDS (SHA512_BLOCKLEN / sizeof(uint64_t))
#define SHA512_BLOCKLEN_WORDS (LC_SHA512_BLOCKLEN / sizeof(uint64_t))
#define SHA512_ROUNDS 80
@ -29,7 +28,7 @@ struct sha512_ctx {
uint64_t h0, h1, h2, h3, h4, h5, h6, h7;
uint64_t szhi, szlo;
size_t mlen;
uint8_t m[SHA512_BLOCKLEN];
uint8_t m[LC_SHA512_BLOCKLEN];
};

View File

@ -27,18 +27,25 @@
*/
/* Hashes. */
#define LC_SHA224_BLOCKLEN 64
#define LC_SHA224_HASHLEN 28
#define LC_SHA256_BLOCKLEN 64
#define LC_SHA256_HASHLEN 32
#define LC_SHA384_BLOCKLEN 128
#define LC_SHA384_HASHLEN 48
#define LC_SHA512_BLOCKLEN 128
#define LC_SHA512_HASHLEN 64
/* Authentitcation. */
#define LC_POLY1305_BLOCKLEN 16
#define LC_POLY1305_KEYLEN 32
#define LC_POLY1305_TAGLEN 16
/* Ciphers. */
#define LC_CHACHA20_BLOCKLEN 64
#define LC_CHACHA20_KEYLEN 32
#define LC_CHACHA20_NONCELEN 12
#define LC_XCHACHA20_BLOCKLEN 64
#define LC_XCHACHA20_KEYLEN 32
#define LC_XCHACHA20_NONCELEN 24