1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
5 * This is an implementation of the ChaCha20Poly1305 AEAD construction.
7 * Information: https://tools.ietf.org/html/rfc8439
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"
16 static const u8 pad0[CHACHA20_BLOCK_SIZE] = { 0 };
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)
24 struct poly1305_ctx poly1305_state;
25 struct chacha20_ctx chacha20_state;
27 u8 block0[POLY1305_KEY_SIZE];
31 chacha20_init(&chacha20_state, key, nonce);
32 chacha20(&chacha20_state, b.block0, b.block0, sizeof(b.block0),
34 poly1305_init(&poly1305_state, b.block0);
36 poly1305_update(&poly1305_state, ad, ad_len, simd_context);
37 poly1305_update(&poly1305_state, pad0, (0x10 - ad_len) & 0xf,
40 chacha20(&chacha20_state, dst, src, src_len, simd_context);
42 poly1305_update(&poly1305_state, dst, src_len, simd_context);
43 poly1305_update(&poly1305_state, pad0, (0x10 - src_len) & 0xf,
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),
51 poly1305_final(&poly1305_state, dst + src_len, simd_context);
53 memzero_explicit(&chacha20_state, sizeof(chacha20_state));
54 memzero_explicit(&b, sizeof(b));
57 void chacha20poly1305_encrypt(u8 *dst, const u8 *src, const size_t src_len,
58 const u8 *ad, const size_t ad_len,
60 const u8 key[CHACHA20POLY1305_KEY_SIZE])
62 simd_context_t simd_context;
64 simd_get(&simd_context);
65 __chacha20poly1305_encrypt(dst, src, src_len, ad, ad_len, nonce, key,
67 simd_put(&simd_context);
69 EXPORT_SYMBOL(chacha20poly1305_encrypt);
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)
76 struct poly1305_ctx poly1305_state;
77 struct chacha20_ctx chacha20_state;
81 u8 block0[POLY1305_KEY_SIZE];
82 u8 mac[POLY1305_MAC_SIZE];
86 if (unlikely(src_len < POLY1305_MAC_SIZE)) {
87 printf("src_len too short\n");
91 chacha20_init(&chacha20_state, key, nonce);
92 chacha20(&chacha20_state, b.block0, b.block0, sizeof(b.block0),
94 poly1305_init(&poly1305_state, b.block0);
96 poly1305_update(&poly1305_state, ad, ad_len, simd_context);
97 poly1305_update(&poly1305_state, pad0, (0x10 - ad_len) & 0xf,
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,
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),
110 poly1305_final(&poly1305_state, b.mac, simd_context);
112 ret = crypto_memneq(b.mac, src + dst_len, POLY1305_MAC_SIZE);
114 chacha20(&chacha20_state, dst, src, dst_len, simd_context);
116 printf("calculated: %16D\n", b.mac, "");
117 printf("sent : %16D\n", src + dst_len, "");
119 memzero_explicit(&chacha20_state, sizeof(chacha20_state));
120 memzero_explicit(&b, sizeof(b));
125 bool chacha20poly1305_decrypt(u8 *dst, const u8 *src, const size_t src_len,
126 const u8 *ad, const size_t ad_len,
128 const u8 key[CHACHA20POLY1305_KEY_SIZE])
130 simd_context_t simd_context;
133 simd_get(&simd_context);
134 ret = __chacha20poly1305_decrypt(dst, src, src_len, ad, ad_len, nonce,
136 simd_put(&simd_context);
139 EXPORT_SYMBOL(chacha20poly1305_decrypt);
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])
146 simd_context_t simd_context;
147 u32 derived_key[CHACHA20_KEY_WORDS] __aligned(16);
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);
158 EXPORT_SYMBOL(xchacha20poly1305_encrypt);
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])
166 simd_context_t simd_context;
167 u32 derived_key[CHACHA20_KEY_WORDS] __aligned(16);
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);
179 EXPORT_SYMBOL(xchacha20poly1305_decrypt);
181 #include "selftest/chacha20poly1305.c"
183 static int __init mod_init(void)
185 if (!selftest_run("chacha20poly1305", chacha20poly1305_selftest,
187 return -ENOTRECOVERABLE;
191 static void __exit mod_exit(void)
195 module_init(mod_init);
196 module_exit(mod_exit);