X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fchacha-poly1305%2Fchacha-poly1305.c;h=d13fec419369ee35ff881e4139bc82f873b097b4;hb=1545909dcb3ac618754486f4ccd4d8f237d64bb7;hp=d4f37237983764ff51c6714b050cce6700c2cd25;hpb=3a316823b971396a428f020f401b9fe41252d98d;p=tinc diff --git a/src/chacha-poly1305/chacha-poly1305.c b/src/chacha-poly1305/chacha-poly1305.c index d4f37237..d13fec41 100644 --- a/src/chacha-poly1305/chacha-poly1305.c +++ b/src/chacha-poly1305/chacha-poly1305.c @@ -1,6 +1,4 @@ #include "../system.h" - -#include "../cipher.h" #include "../xalloc.h" #include "chacha.h" @@ -11,42 +9,40 @@ struct chacha_poly1305_ctx { struct chacha_ctx main_ctx, header_ctx; }; -chacha_poly1305_ctx_t *chacha_poly1305_init(void) -{ +chacha_poly1305_ctx_t *chacha_poly1305_init(void) { chacha_poly1305_ctx_t *ctx = xzalloc(sizeof(*ctx)); return ctx; } -void chacha_poly1305_exit(chacha_poly1305_ctx_t *ctx) -{ +void chacha_poly1305_exit(chacha_poly1305_ctx_t *ctx) { free(ctx); } -bool chacha_poly1305_set_key(chacha_poly1305_ctx_t *ctx, const void *key) -{ +bool chacha_poly1305_set_key(chacha_poly1305_ctx_t *ctx, const void *vkey) { + const uint8_t *key = vkey; chacha_keysetup(&ctx->main_ctx, key, 256); chacha_keysetup(&ctx->header_ctx, key + 32, 256); return true; } -static void put_u64(void *vp, uint64_t v) -{ +static void put_u64(void *vp, uint64_t v) { uint8_t *p = (uint8_t *) vp; - p[0] = (uint8_t) (v >> 56) & 0xff; - p[1] = (uint8_t) (v >> 48) & 0xff; - p[2] = (uint8_t) (v >> 40) & 0xff; - p[3] = (uint8_t) (v >> 32) & 0xff; - p[4] = (uint8_t) (v >> 24) & 0xff; - p[5] = (uint8_t) (v >> 16) & 0xff; - p[6] = (uint8_t) (v >> 8) & 0xff; + p[0] = (uint8_t)(v >> 56) & 0xff; + p[1] = (uint8_t)(v >> 48) & 0xff; + p[2] = (uint8_t)(v >> 40) & 0xff; + p[3] = (uint8_t)(v >> 32) & 0xff; + p[4] = (uint8_t)(v >> 24) & 0xff; + p[5] = (uint8_t)(v >> 16) & 0xff; + p[6] = (uint8_t)(v >> 8) & 0xff; p[7] = (uint8_t) v & 0xff; } -bool chacha_poly1305_encrypt(chacha_poly1305_ctx_t *ctx, uint64_t seqnr, const void *indata, size_t inlen, void *outdata, size_t *outlen) { +bool chacha_poly1305_encrypt(chacha_poly1305_ctx_t *ctx, uint64_t seqnr, const void *indata, size_t inlen, void *voutdata, size_t *outlen) { uint8_t seqbuf[8]; - const uint8_t one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */ + const uint8_t one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */ uint8_t poly_key[POLY1305_KEYLEN]; + uint8_t *outdata = voutdata; /* * Run ChaCha20 once to generate the Poly1305 key. The IV is the @@ -63,16 +59,18 @@ bool chacha_poly1305_encrypt(chacha_poly1305_ctx_t *ctx, uint64_t seqnr, const v chacha_encrypt_bytes(&ctx->main_ctx, indata, outdata, inlen); poly1305_auth(outdata + inlen, outdata, inlen, poly_key); - if (outlen) + if(outlen) { *outlen = inlen + POLY1305_TAGLEN; + } return true; } -bool chacha_poly1305_decrypt(chacha_poly1305_ctx_t *ctx, uint64_t seqnr, const void *indata, size_t inlen, void *outdata, size_t *outlen) { +bool chacha_poly1305_decrypt(chacha_poly1305_ctx_t *ctx, uint64_t seqnr, const void *vindata, size_t inlen, void *outdata, size_t *outlen) { uint8_t seqbuf[8]; - const uint8_t one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */ + const uint8_t one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */ uint8_t expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN]; + const uint8_t *indata = vindata; /* * Run ChaCha20 once to generate the Poly1305 key. The IV is the @@ -91,13 +89,16 @@ bool chacha_poly1305_decrypt(chacha_poly1305_ctx_t *ctx, uint64_t seqnr, const v const uint8_t *tag = indata + inlen; poly1305_auth(expected_tag, indata, inlen, poly_key); - if (memcmp(expected_tag, tag, POLY1305_TAGLEN)) + + if(memcmp(expected_tag, tag, POLY1305_TAGLEN)) { return false; + } chacha_encrypt_bytes(&ctx->main_ctx, indata, outdata, inlen); - if (outlen) + if(outlen) { *outlen = inlen; + } return true; }