]> git.meshlink.io Git - meshlink/blob - src/chacha-poly1305/chacha-poly1305.c
Add support for encrypted storage.
[meshlink] / src / chacha-poly1305 / chacha-poly1305.c
1 #include "../system.h"
2
3 #include "../xalloc.h"
4
5 #include "chacha.h"
6 #include "chacha-poly1305.h"
7 #include "poly1305.h"
8
9 struct chacha_poly1305_ctx {
10         struct chacha_ctx main_ctx, header_ctx;
11 };
12
13 chacha_poly1305_ctx_t *chacha_poly1305_init(void)
14 {
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 {
21         free(ctx);
22 }
23
24 bool chacha_poly1305_set_key(chacha_poly1305_ctx_t *ctx, const void *key)
25 {
26         chacha_keysetup(&ctx->main_ctx, key, 256);
27         chacha_keysetup(&ctx->header_ctx, (uint8_t *)key + 32, 256);
28         return true;
29 }
30
31 static void put_u64(void *vp, uint64_t v)
32 {
33         uint8_t *p = (uint8_t *) vp;
34
35         p[0] = (uint8_t) (v >> 56) & 0xff;
36         p[1] = (uint8_t) (v >> 48) & 0xff;
37         p[2] = (uint8_t) (v >> 40) & 0xff;
38         p[3] = (uint8_t) (v >> 32) & 0xff;
39         p[4] = (uint8_t) (v >> 24) & 0xff;
40         p[5] = (uint8_t) (v >> 16) & 0xff;
41         p[6] = (uint8_t) (v >> 8) & 0xff;
42         p[7] = (uint8_t) v & 0xff;
43 }
44
45 bool chacha_poly1305_encrypt(chacha_poly1305_ctx_t *ctx, uint64_t seqnr, const void *indata, size_t inlen, void *outdata, size_t *outlen) {
46         uint8_t seqbuf[8];
47         const uint8_t one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 };      /* NB little-endian */
48         uint8_t poly_key[POLY1305_KEYLEN];
49
50         /*
51          * Run ChaCha20 once to generate the Poly1305 key. The IV is the
52          * packet sequence number.
53          */
54         memset(poly_key, 0, sizeof(poly_key));
55         put_u64(seqbuf, seqnr);
56         chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL);
57         chacha_encrypt_bytes(&ctx->main_ctx, poly_key, poly_key, sizeof(poly_key));
58
59         /* Set Chacha's block counter to 1 */
60         chacha_ivsetup(&ctx->main_ctx, seqbuf, one);
61
62         chacha_encrypt_bytes(&ctx->main_ctx, indata, outdata, inlen);
63         poly1305_auth((uint8_t *)outdata + inlen, outdata, inlen, poly_key);
64
65         if (outlen)
66                 *outlen = inlen + POLY1305_TAGLEN;
67
68         return true;
69 }
70
71 bool chacha_poly1305_decrypt(chacha_poly1305_ctx_t *ctx, uint64_t seqnr, const void *indata, size_t inlen, void *outdata, size_t *outlen) {
72         uint8_t seqbuf[8];
73         const uint8_t one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 };      /* NB little-endian */
74         uint8_t expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
75
76         /*
77          * Run ChaCha20 once to generate the Poly1305 key. The IV is the
78          * packet sequence number.
79          */
80         memset(poly_key, 0, sizeof(poly_key));
81         put_u64(seqbuf, seqnr);
82         chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL);
83         chacha_encrypt_bytes(&ctx->main_ctx, poly_key, poly_key, sizeof(poly_key));
84
85         /* Set Chacha's block counter to 1 */
86         chacha_ivsetup(&ctx->main_ctx, seqbuf, one);
87
88         /* Check tag before anything else */
89         inlen -= POLY1305_TAGLEN;
90         const uint8_t *tag = (const uint8_t *)indata + inlen;
91
92         poly1305_auth(expected_tag, indata, inlen, poly_key);
93         if (memcmp(expected_tag, tag, POLY1305_TAGLEN))
94                 return false;
95
96         chacha_encrypt_bytes(&ctx->main_ctx, indata, outdata, inlen);
97
98         if (outlen)
99                 *outlen = inlen;
100
101         return true;
102 }
103
104 bool chacha_poly1305_encrypt_iv96(chacha_poly1305_ctx_t *ctx, const uint8_t *seqbuf, const void *indata, size_t inlen, void *outdata, size_t *outlen) {
105         const uint8_t one[4] = { 1, 0, 0, 0 };  /* NB little-endian */
106         uint8_t poly_key[POLY1305_KEYLEN];
107
108         /*
109          * Run ChaCha20 once to generate the Poly1305 key. The IV is the
110          * packet sequence number.
111          */
112         memset(poly_key, 0, sizeof(poly_key));
113         chacha_ivsetup_96(&ctx->main_ctx, seqbuf, NULL);
114         chacha_encrypt_bytes(&ctx->main_ctx, poly_key, poly_key, sizeof(poly_key));
115
116         /* Set Chacha's block counter to 1 */
117         chacha_ivsetup_96(&ctx->main_ctx, seqbuf, one);
118
119         chacha_encrypt_bytes(&ctx->main_ctx, indata, outdata, inlen);
120         poly1305_auth((uint8_t *)outdata + inlen, outdata, inlen, poly_key);
121
122         if (outlen)
123                 *outlen = inlen + POLY1305_TAGLEN;
124
125         return true;
126 }
127
128 bool chacha_poly1305_decrypt_iv96(chacha_poly1305_ctx_t *ctx, const uint8_t *seqbuf, const void *indata, size_t inlen, void *outdata, size_t *outlen) {
129         const uint8_t one[4] = { 1, 0, 0, 0 };  /* NB little-endian */
130         uint8_t expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
131
132         /*
133          * Run ChaCha20 once to generate the Poly1305 key. The IV is the
134          * packet sequence number.
135          */
136         memset(poly_key, 0, sizeof(poly_key));
137         chacha_ivsetup_96(&ctx->main_ctx, seqbuf, NULL);
138         chacha_encrypt_bytes(&ctx->main_ctx, poly_key, poly_key, sizeof(poly_key));
139
140         /* Set Chacha's block counter to 1 */
141         chacha_ivsetup_96(&ctx->main_ctx, seqbuf, one);
142
143         /* Check tag before anything else */
144         inlen -= POLY1305_TAGLEN;
145         const uint8_t *tag = (const uint8_t *)indata + inlen;
146
147         poly1305_auth(expected_tag, indata, inlen, poly_key);
148         if (memcmp(expected_tag, tag, POLY1305_TAGLEN))
149                 return false;
150
151         chacha_encrypt_bytes(&ctx->main_ctx, indata, outdata, inlen);
152
153         if (outlen)
154                 *outlen = inlen;
155
156         return true;
157 }