kernel: 5.4: import wireguard backport
[openwrt/openwrt.git] / target / linux / generic / backport-5.4 / 080-wireguard-0015-crypto-poly1305-expose-init-update-final-library-int.patch
1 From fd966ddf025b8b62aab20d2e4eb242fe51ad5137 Mon Sep 17 00:00:00 2001
2 From: Ard Biesheuvel <ardb@kernel.org>
3 Date: Fri, 8 Nov 2019 13:22:21 +0100
4 Subject: [PATCH 015/124] crypto: poly1305 - expose init/update/final library
5 interface
6
7 commit a1d93064094cc5e24d64e35cf093e7191d0c9344 upstream.
8
9 Expose the existing generic Poly1305 code via a init/update/final
10 library interface so that callers are not required to go through
11 the crypto API's shash abstraction to access it. At the same time,
12 make some preparations so that the library implementation can be
13 superseded by an accelerated arch-specific version in the future.
14
15 Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
16 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
17 Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
18 ---
19 crypto/poly1305_generic.c | 22 +-----------
20 include/crypto/poly1305.h | 38 +++++++++++++++++++-
21 lib/crypto/Kconfig | 26 ++++++++++++++
22 lib/crypto/poly1305.c | 74 +++++++++++++++++++++++++++++++++++++++
23 4 files changed, 138 insertions(+), 22 deletions(-)
24
25 --- a/crypto/poly1305_generic.c
26 +++ b/crypto/poly1305_generic.c
27 @@ -85,31 +85,11 @@ EXPORT_SYMBOL_GPL(crypto_poly1305_update
28 int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
29 {
30 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
31 - __le32 digest[4];
32 - u64 f = 0;
33
34 if (unlikely(!dctx->sset))
35 return -ENOKEY;
36
37 - if (unlikely(dctx->buflen)) {
38 - dctx->buf[dctx->buflen++] = 1;
39 - memset(dctx->buf + dctx->buflen, 0,
40 - POLY1305_BLOCK_SIZE - dctx->buflen);
41 - poly1305_core_blocks(&dctx->h, dctx->r, dctx->buf, 1, 0);
42 - }
43 -
44 - poly1305_core_emit(&dctx->h, digest);
45 -
46 - /* mac = (h + s) % (2^128) */
47 - f = (f >> 32) + le32_to_cpu(digest[0]) + dctx->s[0];
48 - put_unaligned_le32(f, dst + 0);
49 - f = (f >> 32) + le32_to_cpu(digest[1]) + dctx->s[1];
50 - put_unaligned_le32(f, dst + 4);
51 - f = (f >> 32) + le32_to_cpu(digest[2]) + dctx->s[2];
52 - put_unaligned_le32(f, dst + 8);
53 - f = (f >> 32) + le32_to_cpu(digest[3]) + dctx->s[3];
54 - put_unaligned_le32(f, dst + 12);
55 -
56 + poly1305_final_generic(dctx, dst);
57 return 0;
58 }
59 EXPORT_SYMBOL_GPL(crypto_poly1305_final);
60 --- a/include/crypto/poly1305.h
61 +++ b/include/crypto/poly1305.h
62 @@ -35,7 +35,43 @@ struct poly1305_desc_ctx {
63 /* accumulator */
64 struct poly1305_state h;
65 /* key */
66 - struct poly1305_key r[1];
67 + struct poly1305_key r[CONFIG_CRYPTO_LIB_POLY1305_RSIZE];
68 };
69
70 +void poly1305_init_arch(struct poly1305_desc_ctx *desc, const u8 *key);
71 +void poly1305_init_generic(struct poly1305_desc_ctx *desc, const u8 *key);
72 +
73 +static inline void poly1305_init(struct poly1305_desc_ctx *desc, const u8 *key)
74 +{
75 + if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
76 + poly1305_init_arch(desc, key);
77 + else
78 + poly1305_init_generic(desc, key);
79 +}
80 +
81 +void poly1305_update_arch(struct poly1305_desc_ctx *desc, const u8 *src,
82 + unsigned int nbytes);
83 +void poly1305_update_generic(struct poly1305_desc_ctx *desc, const u8 *src,
84 + unsigned int nbytes);
85 +
86 +static inline void poly1305_update(struct poly1305_desc_ctx *desc,
87 + const u8 *src, unsigned int nbytes)
88 +{
89 + if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
90 + poly1305_update_arch(desc, src, nbytes);
91 + else
92 + poly1305_update_generic(desc, src, nbytes);
93 +}
94 +
95 +void poly1305_final_arch(struct poly1305_desc_ctx *desc, u8 *digest);
96 +void poly1305_final_generic(struct poly1305_desc_ctx *desc, u8 *digest);
97 +
98 +static inline void poly1305_final(struct poly1305_desc_ctx *desc, u8 *digest)
99 +{
100 + if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
101 + poly1305_final_arch(desc, digest);
102 + else
103 + poly1305_final_generic(desc, digest);
104 +}
105 +
106 #endif
107 --- a/lib/crypto/Kconfig
108 +++ b/lib/crypto/Kconfig
109 @@ -37,8 +37,34 @@ config CRYPTO_LIB_CHACHA
110 config CRYPTO_LIB_DES
111 tristate
112
113 +config CRYPTO_LIB_POLY1305_RSIZE
114 + int
115 + default 1
116 +
117 +config CRYPTO_ARCH_HAVE_LIB_POLY1305
118 + tristate
119 + help
120 + Declares whether the architecture provides an arch-specific
121 + accelerated implementation of the Poly1305 library interface,
122 + either builtin or as a module.
123 +
124 config CRYPTO_LIB_POLY1305_GENERIC
125 tristate
126 + help
127 + This symbol can be depended upon by arch implementations of the
128 + Poly1305 library interface that require the generic code as a
129 + fallback, e.g., for SIMD implementations. If no arch specific
130 + implementation is enabled, this implementation serves the users
131 + of CRYPTO_LIB_POLY1305.
132 +
133 +config CRYPTO_LIB_POLY1305
134 + tristate "Poly1305 library interface"
135 + depends on CRYPTO_ARCH_HAVE_LIB_POLY1305 || !CRYPTO_ARCH_HAVE_LIB_POLY1305
136 + select CRYPTO_LIB_POLY1305_GENERIC if CRYPTO_ARCH_HAVE_LIB_POLY1305=n
137 + help
138 + Enable the Poly1305 library interface. This interface may be fulfilled
139 + by either the generic implementation or an arch-specific one, if one
140 + is available and enabled.
141
142 config CRYPTO_LIB_SHA256
143 tristate
144 --- a/lib/crypto/poly1305.c
145 +++ b/lib/crypto/poly1305.c
146 @@ -154,5 +154,79 @@ void poly1305_core_emit(const struct pol
147 }
148 EXPORT_SYMBOL_GPL(poly1305_core_emit);
149
150 +void poly1305_init_generic(struct poly1305_desc_ctx *desc, const u8 *key)
151 +{
152 + poly1305_core_setkey(desc->r, key);
153 + desc->s[0] = get_unaligned_le32(key + 16);
154 + desc->s[1] = get_unaligned_le32(key + 20);
155 + desc->s[2] = get_unaligned_le32(key + 24);
156 + desc->s[3] = get_unaligned_le32(key + 28);
157 + poly1305_core_init(&desc->h);
158 + desc->buflen = 0;
159 + desc->sset = true;
160 + desc->rset = 1;
161 +}
162 +EXPORT_SYMBOL_GPL(poly1305_init_generic);
163 +
164 +void poly1305_update_generic(struct poly1305_desc_ctx *desc, const u8 *src,
165 + unsigned int nbytes)
166 +{
167 + unsigned int bytes;
168 +
169 + if (unlikely(desc->buflen)) {
170 + bytes = min(nbytes, POLY1305_BLOCK_SIZE - desc->buflen);
171 + memcpy(desc->buf + desc->buflen, src, bytes);
172 + src += bytes;
173 + nbytes -= bytes;
174 + desc->buflen += bytes;
175 +
176 + if (desc->buflen == POLY1305_BLOCK_SIZE) {
177 + poly1305_core_blocks(&desc->h, desc->r, desc->buf, 1, 1);
178 + desc->buflen = 0;
179 + }
180 + }
181 +
182 + if (likely(nbytes >= POLY1305_BLOCK_SIZE)) {
183 + poly1305_core_blocks(&desc->h, desc->r, src,
184 + nbytes / POLY1305_BLOCK_SIZE, 1);
185 + src += nbytes - (nbytes % POLY1305_BLOCK_SIZE);
186 + nbytes %= POLY1305_BLOCK_SIZE;
187 + }
188 +
189 + if (unlikely(nbytes)) {
190 + desc->buflen = nbytes;
191 + memcpy(desc->buf, src, nbytes);
192 + }
193 +}
194 +EXPORT_SYMBOL_GPL(poly1305_update_generic);
195 +
196 +void poly1305_final_generic(struct poly1305_desc_ctx *desc, u8 *dst)
197 +{
198 + __le32 digest[4];
199 + u64 f = 0;
200 +
201 + if (unlikely(desc->buflen)) {
202 + desc->buf[desc->buflen++] = 1;
203 + memset(desc->buf + desc->buflen, 0,
204 + POLY1305_BLOCK_SIZE - desc->buflen);
205 + poly1305_core_blocks(&desc->h, desc->r, desc->buf, 1, 0);
206 + }
207 +
208 + poly1305_core_emit(&desc->h, digest);
209 +
210 + /* mac = (h + s) % (2^128) */
211 + f = (f >> 32) + le32_to_cpu(digest[0]) + desc->s[0];
212 + put_unaligned_le32(f, dst + 0);
213 + f = (f >> 32) + le32_to_cpu(digest[1]) + desc->s[1];
214 + put_unaligned_le32(f, dst + 4);
215 + f = (f >> 32) + le32_to_cpu(digest[2]) + desc->s[2];
216 + put_unaligned_le32(f, dst + 8);
217 + f = (f >> 32) + le32_to_cpu(digest[3]) + desc->s[3];
218 + put_unaligned_le32(f, dst + 12);
219 +
220 + *desc = (struct poly1305_desc_ctx){};
221 +}
222 +EXPORT_SYMBOL_GPL(poly1305_final_generic);
223 +
224 MODULE_LICENSE("GPL");
225 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");