Hi,
I'm trying to implement chacha20-poly1305 as used by SSH (https://datatracker.ietf.org/doc/html/draft-ietf-sshm-chacha20-poly1305-04), which differs from RFC 8439's ChaCha-Poly1035 as implemented by Nettle.
While Nettle internally has the necessary building blocks, it lacks a public API to directly use poly1305. I think this is fairly straightforward to add one though. Please see below for my initial attempt. If you think this is the right approach, I'd be happy to work on a proper patch including tests and documentation.
#define POLY1305_KEY_SIZE 32 #define POLY1305_DIGEST_SIZE 16
struct poly1305_mac_ctx { struct poly1305_ctx pctx; union nettle_block16 s; uint8_t block[POLY1305_BLOCK_SIZE]; unsigned index; };
void poly1305_set_key(struct poly1305_mac_ctx* ctx, const uint8_t *key) { _nettle_poly1305_set_key(&ctx->pctx, key); memcpy(ctx->s.b, key + 16, 16); ctx->index = 0; }
void poly1305_update(struct poly1305_mac_ctx* ctx, size_t length, const uint8_t *data) { ctx->index = _nettle_poly1305_update(&ctx->pctx, ctx->block, ctx->index, length, data); }
/* After calling this function, context must be re-keyed before calling poly1305_update/poly1305_digest again. */ void poly1305_digest(struct poly1305_mac_ctx* ctx, uint8_t* digest) { if (ctx->index > 0) { ctx->block[ctx->index] = 1; memset (ctx->block + ctx->index + 1, 0, POLY1305_BLOCK_SIZE - 1 - ctx->index);
_nettle_poly1305_block (&ctx->pctx, ctx->block, 0); } _nettle_poly1305_digest(&ctx->pctx, &ctx->s); memcpy(digest, ctx->s.b, POLY1305_DIGEST_SIZE); }
Regards, Tim
Tim Kosse tim.kosse@filezilla-project.org writes:
I'm trying to implement chacha20-poly1305 as used by SSH (https://datatracker.ietf.org/doc/html/draft-ietf-sshm-chacha20-poly1305-04), which differs from RFC 8439's ChaCha-Poly1035 as implemented by Nettle.
I agree Nettle should support some way of doing this, thanks for looking into that. Not sure what API should look like, though. It's rather different from other mac:s, since one can't set a key once and authenticate multiple messages.
One option could be to instead promote some of the low-level functions.
If we promote _nettle_poly1305_set_key, _nettle_poly1305_update, and _nettle_poly1305_digest, then what's missing is a utility for the digest-time padding of a partial block. We would need something like either
poly1305_digest (struct poly1305_ctx *ctx, union nettle_block16 *s, uint8_t *buffer, unsigned index);
or current _nettle_poly1305_digest and something like
poly1305_pad (struct poly1305_ctx *ctx, uint8_t *buffer, unsigned index);
I think it's desirable that the new poly1305 functions are defined in such a way that current poly1305_aes and chacha_poly1305 api:s could build on top of it (even if that might be unfeasible at the moment since refactoring the corresponding context structs might break the abi).
Another option is to add an implementation of the specific openssh construction, if we don't foresee any need for additional variants building on the poly1305 primitive.
Some more detailed comments below:
struct poly1305_mac_ctx { struct poly1305_ctx pctx; union nettle_block16 s; uint8_t block[POLY1305_BLOCK_SIZE]; unsigned index; };
Having s in this struct implies some impedance mismatch if we try to use it in the implementation of poly1305_aes (there, s is aes_k(nonce), and the nonce is kept in the context struct to support autoincrement). I think I'd prefer passing s as an argument at digest time.
/* After calling this function, context must be re-keyed before calling poly1305_update/poly1305_digest again. */ void poly1305_digest(struct poly1305_mac_ctx* ctx, uint8_t* digest)
The requirement for rekeying after each use stands out, if we try to otherwise stick to nettle's conventions for MAC functions.
Regards, /Niels
On Thu, Aug 13, 2026 at 10:05 AM Niels Möller nisse@lysator.liu.se wrote:
Tim Kosse tim.kosse@filezilla-project.org writes:
I'm trying to implement chacha20-poly1305 as used by SSH (https://datatracker.ietf.org/doc/html/draft-ietf-sshm-chacha20-poly1305-04), which differs from RFC 8439's ChaCha-Poly1035 as implemented by Nettle.
I agree Nettle should support some way of doing this, thanks for looking into that. Not sure what API should look like, though. It's rather different from other mac:s, since one can't set a key once and authenticate multiple messages.
One option could be to instead promote some of the low-level functions.
If we promote _nettle_poly1305_set_key, _nettle_poly1305_update, and _nettle_poly1305_digest, then what's missing is a utility for the digest-time padding of a partial block. We would need something like either
poly1305_digest (struct poly1305_ctx *ctx, union nettle_block16 *s, uint8_t *buffer, unsigned index);
or current _nettle_poly1305_digest and something like
poly1305_pad (struct poly1305_ctx *ctx, uint8_t *buffer, unsigned index);
Why not fold the padding into digest, your first variant. The pad is always immediately followed by the digest, so a separate poly1305_pad mostly adds a way to get it wrong, and digest with index == 0 is just the current _nettle_poly1305_digest, which is what an RFC8439-style caller wants after aligning to a block boundary.
Also, should digest return the tag in *s, like the internal function, or take separate (length, digest) outputs like nettle's other MACs? I have no strong opinion though.
I think it's desirable that the new poly1305 functions are defined in such a way that current poly1305_aes and chacha_poly1305 api:s could build on top of it (even if that might be unfeasible at the moment since refactoring the corresponding context structs might break the abi).
Another option is to add an implementation of the specific openssh construction, if we don't foresee any need for additional variants building on the poly1305 primitive.
Some more detailed comments below:
struct poly1305_mac_ctx { struct poly1305_ctx pctx; union nettle_block16 s; uint8_t block[POLY1305_BLOCK_SIZE]; unsigned index; };
Having s in this struct implies some impedance mismatch if we try to use it in the implementation of poly1305_aes (there, s is aes_k(nonce), and the nonce is kept in the context struct to support autoincrement). I think I'd prefer passing s as an argument at digest time.
/* After calling this function, context must be re-keyed before calling poly1305_update/poly1305_digest again. */ void poly1305_digest(struct poly1305_mac_ctx* ctx, uint8_t* digest)
The requirement for rekeying after each use stands out, if we try to otherwise stick to nettle's conventions for MAC functions.
Regards, /Niels
-- Niels Möller. PGP key CB4962D070D77D7FCB8BA36271D8F1FF368C6677. Internet email is subject to wholesale government surveillance. _______________________________________________ nettle-bugs mailing list -- nettle-bugs@lists.lysator.liu.se To unsubscribe send an email to nettle-bugs-leave@lists.lysator.liu.se
On 2026-08-13 Thu 10:05 AM, Niels Möller wrote:
I think it's desirable that the new poly1305 functions are defined in such a way that current poly1305_aes and chacha_poly1305 api:s could build on top of it
struct poly1305_mac_ctx { struct poly1305_ctx pctx; union nettle_block16 s; uint8_t block[POLY1305_BLOCK_SIZE]; unsigned index; };
Having s in this struct implies some impedance mismatch if we try to use it in the implementation of poly1305_aes (there, s is aes_k(nonce), and the nonce is kept in the context struct to support autoincrement). I think I'd prefer passing s as an argument at digest time.
That makes sense, that way the caller can change s for each invocation.
I'd also change poly1306_set_key in that case though to only accept the first half of the key and change the define:
/* There are two key halves r and s */ #define POLY1305_HALFKEY_SIZE 16
void poly1305_set_key(struct poly1305_mac_ctx* ctx, const uint8_t *r);
void poly1305_digest(struct poly1305_mac_ctx* ctx, uint8_t* digest, const uint8_t *s);
If we promote [...] _nettle_poly1305_update, and _nettle_poly1305_digest, then what's missing is a utility for the digest-time padding of a partial block.
I would not promote the current functions. The caller shouldn't have to worry about handling partials blocks and the correct padding of the last block, the update/digest function itself should take care of it, just like with other mac types in Nettle.
Another option is to add an implementation of the specific openssh construction, if we don't foresee any need for additional variants building on the poly1305 primitive.
I think the API requirements for the SSH-specific construction are a bit more complex. In any case I would want to use wrappers around _nettle_poly1305_update and _nettle_poly1305_digest to make the code easier to understand.
Regards, Tim
nettle-bugs@lists.lysator.liu.se