]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/if_wg/module/crypto/zinc/chacha20poly1305.c
service(8): use an environment more consistent with init(8)
[FreeBSD/FreeBSD.git] / sys / dev / if_wg / module / crypto / zinc / chacha20poly1305.c
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  *
5  * This is an implementation of the ChaCha20Poly1305 AEAD construction.
6  *
7  * Information: https://tools.ietf.org/html/rfc8439
8  */
9
10 #include <sys/support.h>
11 #include <zinc/chacha20poly1305.h>
12 #include <zinc/chacha20.h>
13 #include <zinc/poly1305.h>
14 #include "selftest/run.h"
15
16 static const u8 pad0[CHACHA20_BLOCK_SIZE] = { 0 };
17
18 static inline void
19 __chacha20poly1305_encrypt(u8 *dst, const u8 *src, const size_t src_len,
20                            const u8 *ad, const size_t ad_len, const u64 nonce,
21                            const u8 key[CHACHA20POLY1305_KEY_SIZE],
22                            simd_context_t *simd_context)
23 {
24         struct poly1305_ctx poly1305_state;
25         struct chacha20_ctx chacha20_state;
26         union {
27                 u8 block0[POLY1305_KEY_SIZE];
28                 __le64 lens[2];
29         } b = { { 0 } };
30
31         chacha20_init(&chacha20_state, key, nonce);
32         chacha20(&chacha20_state, b.block0, b.block0, sizeof(b.block0),
33                  simd_context);
34         poly1305_init(&poly1305_state, b.block0);
35
36         poly1305_update(&poly1305_state, ad, ad_len, simd_context);
37         poly1305_update(&poly1305_state, pad0, (0x10 - ad_len) & 0xf,
38                         simd_context);
39
40         chacha20(&chacha20_state, dst, src, src_len, simd_context);
41
42         poly1305_update(&poly1305_state, dst, src_len, simd_context);
43         poly1305_update(&poly1305_state, pad0, (0x10 - src_len) & 0xf,
44                         simd_context);
45
46         b.lens[0] = cpu_to_le64(ad_len);
47         b.lens[1] = cpu_to_le64(src_len);
48         poly1305_update(&poly1305_state, (u8 *)b.lens, sizeof(b.lens),
49                         simd_context);
50
51         poly1305_final(&poly1305_state, dst + src_len, simd_context);
52
53         memzero_explicit(&chacha20_state, sizeof(chacha20_state));
54         memzero_explicit(&b, sizeof(b));
55 }
56
57 void chacha20poly1305_encrypt(u8 *dst, const u8 *src, const size_t src_len,
58                               const u8 *ad, const size_t ad_len,
59                               const u64 nonce,
60                               const u8 key[CHACHA20POLY1305_KEY_SIZE])
61 {
62         simd_context_t simd_context;
63
64         simd_get(&simd_context);
65         __chacha20poly1305_encrypt(dst, src, src_len, ad, ad_len, nonce, key,
66                                    &simd_context);
67         simd_put(&simd_context);
68 }
69 EXPORT_SYMBOL(chacha20poly1305_encrypt);
70 static inline bool
71 __chacha20poly1305_decrypt(u8 *dst, const u8 *src, const size_t src_len,
72                            const u8 *ad, const size_t ad_len, const u64 nonce,
73                            const u8 key[CHACHA20POLY1305_KEY_SIZE],
74                            simd_context_t *simd_context)
75 {
76         struct poly1305_ctx poly1305_state;
77         struct chacha20_ctx chacha20_state;
78         int ret;
79         size_t dst_len;
80         union {
81                 u8 block0[POLY1305_KEY_SIZE];
82                 u8 mac[POLY1305_MAC_SIZE];
83                 __le64 lens[2];
84         } b = { { 0 } };
85
86         if (unlikely(src_len < POLY1305_MAC_SIZE)) {
87                 printf("src_len too short\n");
88                 return false;
89         }
90
91         chacha20_init(&chacha20_state, key, nonce);
92         chacha20(&chacha20_state, b.block0, b.block0, sizeof(b.block0),
93                  simd_context);
94         poly1305_init(&poly1305_state, b.block0);
95
96         poly1305_update(&poly1305_state, ad, ad_len, simd_context);
97         poly1305_update(&poly1305_state, pad0, (0x10 - ad_len) & 0xf,
98                         simd_context);
99
100         dst_len = src_len - POLY1305_MAC_SIZE;
101         poly1305_update(&poly1305_state, src, dst_len, simd_context);
102         poly1305_update(&poly1305_state, pad0, (0x10 - dst_len) & 0xf,
103                         simd_context);
104
105         b.lens[0] = cpu_to_le64(ad_len);
106         b.lens[1] = cpu_to_le64(dst_len);
107         poly1305_update(&poly1305_state, (u8 *)b.lens, sizeof(b.lens),
108                         simd_context);
109
110         poly1305_final(&poly1305_state, b.mac, simd_context);
111
112         ret = crypto_memneq(b.mac, src + dst_len, POLY1305_MAC_SIZE);
113         if (likely(!ret))
114                 chacha20(&chacha20_state, dst, src, dst_len, simd_context);
115         else {
116                 printf("calculated: %16D\n", b.mac, "");
117                 printf("sent      : %16D\n", src + dst_len, "");
118         }
119         memzero_explicit(&chacha20_state, sizeof(chacha20_state));
120         memzero_explicit(&b, sizeof(b));
121
122         return !ret;
123 }
124
125 bool chacha20poly1305_decrypt(u8 *dst, const u8 *src, const size_t src_len,
126                               const u8 *ad, const size_t ad_len,
127                               const u64 nonce,
128                               const u8 key[CHACHA20POLY1305_KEY_SIZE])
129 {
130         simd_context_t simd_context;
131         bool ret;
132
133         simd_get(&simd_context);
134         ret = __chacha20poly1305_decrypt(dst, src, src_len, ad, ad_len, nonce,
135                                          key, &simd_context);
136         simd_put(&simd_context);
137         return ret;
138 }
139 EXPORT_SYMBOL(chacha20poly1305_decrypt);
140
141 void xchacha20poly1305_encrypt(u8 *dst, const u8 *src, const size_t src_len,
142                                const u8 *ad, const size_t ad_len,
143                                const u8 nonce[XCHACHA20POLY1305_NONCE_SIZE],
144                                const u8 key[CHACHA20POLY1305_KEY_SIZE])
145 {
146         simd_context_t simd_context;
147         u32 derived_key[CHACHA20_KEY_WORDS] __aligned(16);
148
149         simd_get(&simd_context);
150         hchacha20(derived_key, nonce, key, &simd_context);
151         cpu_to_le32_array(derived_key, ARRAY_SIZE(derived_key));
152         __chacha20poly1305_encrypt(dst, src, src_len, ad, ad_len,
153                                    get_unaligned_le64(nonce + 16),
154                                    (u8 *)derived_key, &simd_context);
155         memzero_explicit(derived_key, CHACHA20POLY1305_KEY_SIZE);
156         simd_put(&simd_context);
157 }
158 EXPORT_SYMBOL(xchacha20poly1305_encrypt);
159
160 bool xchacha20poly1305_decrypt(u8 *dst, const u8 *src, const size_t src_len,
161                                const u8 *ad, const size_t ad_len,
162                                const u8 nonce[XCHACHA20POLY1305_NONCE_SIZE],
163                                const u8 key[CHACHA20POLY1305_KEY_SIZE])
164 {
165         bool ret;
166         simd_context_t simd_context;
167         u32 derived_key[CHACHA20_KEY_WORDS] __aligned(16);
168
169         simd_get(&simd_context);
170         hchacha20(derived_key, nonce, key, &simd_context);
171         cpu_to_le32_array(derived_key, ARRAY_SIZE(derived_key));
172         ret = __chacha20poly1305_decrypt(dst, src, src_len, ad, ad_len,
173                                          get_unaligned_le64(nonce + 16),
174                                          (u8 *)derived_key, &simd_context);
175         memzero_explicit(derived_key, CHACHA20POLY1305_KEY_SIZE);
176         simd_put(&simd_context);
177         return ret;
178 }
179 EXPORT_SYMBOL(xchacha20poly1305_decrypt);
180
181 #include "selftest/chacha20poly1305.c"
182
183 static int __init mod_init(void)
184 {
185         if (!selftest_run("chacha20poly1305", chacha20poly1305_selftest,
186                           NULL, 0))
187                 return -ENOTRECOVERABLE;
188         return 0;
189 }
190
191 static void __exit mod_exit(void)
192 {
193 }
194
195 module_init(mod_init);
196 module_exit(mod_exit);