Reformat all code using astyle.
[tinc] / src / chacha-poly1305 / chacha-poly1305.c
1 #include "../system.h"
2
3 #include "../cipher.h"
4 #include "../xalloc.h"
5
6 #include "chacha.h"
7 #include "chacha-poly1305.h"
8 #include "poly1305.h"
9
10 struct chacha_poly1305_ctx {
11         struct chacha_ctx main_ctx, header_ctx;
12 };
13
14 chacha_poly1305_ctx_t *chacha_poly1305_init(void) {
15         chacha_poly1305_ctx_t *ctx = xzalloc(sizeof(*ctx));
16         return ctx;
17 }
18
19 void chacha_poly1305_exit(chacha_poly1305_ctx_t *ctx) {
20         free(ctx);
21 }
22
23 bool chacha_poly1305_set_key(chacha_poly1305_ctx_t *ctx, const void *key) {
24         chacha_keysetup(&ctx->main_ctx, key, 256);
25         chacha_keysetup(&ctx->header_ctx, key + 32, 256);
26         return true;
27 }
28
29 static void put_u64(void *vp, uint64_t v) {
30         uint8_t *p = (uint8_t *) vp;
31
32         p[0] = (uint8_t)(v >> 56) & 0xff;
33         p[1] = (uint8_t)(v >> 48) & 0xff;
34         p[2] = (uint8_t)(v >> 40) & 0xff;
35         p[3] = (uint8_t)(v >> 32) & 0xff;
36         p[4] = (uint8_t)(v >> 24) & 0xff;
37         p[5] = (uint8_t)(v >> 16) & 0xff;
38         p[6] = (uint8_t)(v >> 8) & 0xff;
39         p[7] = (uint8_t) v & 0xff;
40 }
41
42 bool chacha_poly1305_encrypt(chacha_poly1305_ctx_t *ctx, uint64_t seqnr, const void *indata, size_t inlen, void *outdata, size_t *outlen) {
43         uint8_t seqbuf[8];
44         const uint8_t one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 };      /* NB little-endian */
45         uint8_t poly_key[POLY1305_KEYLEN];
46
47         /*
48          * Run ChaCha20 once to generate the Poly1305 key. The IV is the
49          * packet sequence number.
50          */
51         memset(poly_key, 0, sizeof(poly_key));
52         put_u64(seqbuf, seqnr);
53         chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL);
54         chacha_encrypt_bytes(&ctx->main_ctx, poly_key, poly_key, sizeof(poly_key));
55
56         /* Set Chacha's block counter to 1 */
57         chacha_ivsetup(&ctx->main_ctx, seqbuf, one);
58
59         chacha_encrypt_bytes(&ctx->main_ctx, indata, outdata, inlen);
60         poly1305_auth(outdata + inlen, outdata, inlen, poly_key);
61
62         if(outlen) {
63                 *outlen = inlen + POLY1305_TAGLEN;
64         }
65
66         return true;
67 }
68
69 bool chacha_poly1305_decrypt(chacha_poly1305_ctx_t *ctx, uint64_t seqnr, const void *indata, size_t inlen, void *outdata, size_t *outlen) {
70         uint8_t seqbuf[8];
71         const uint8_t one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 };      /* NB little-endian */
72         uint8_t expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
73
74         /*
75          * Run ChaCha20 once to generate the Poly1305 key. The IV is the
76          * packet sequence number.
77          */
78         memset(poly_key, 0, sizeof(poly_key));
79         put_u64(seqbuf, seqnr);
80         chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL);
81         chacha_encrypt_bytes(&ctx->main_ctx, poly_key, poly_key, sizeof(poly_key));
82
83         /* Set Chacha's block counter to 1 */
84         chacha_ivsetup(&ctx->main_ctx, seqbuf, one);
85
86         /* Check tag before anything else */
87         inlen -= POLY1305_TAGLEN;
88         const uint8_t *tag = indata + inlen;
89
90         poly1305_auth(expected_tag, indata, inlen, poly_key);
91
92         if(memcmp(expected_tag, tag, POLY1305_TAGLEN)) {
93                 return false;
94         }
95
96         chacha_encrypt_bytes(&ctx->main_ctx, indata, outdata, inlen);
97
98         if(outlen) {
99                 *outlen = inlen;
100         }
101
102         return true;
103 }