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
7 commit a1d93064094cc5e24d64e35cf093e7191d0c9344 upstream.
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.
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>
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(-)
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)
30 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
34 if (unlikely(!dctx->sset))
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);
44 - poly1305_core_emit(&dctx->h, digest);
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);
56 + poly1305_final_generic(dctx, dst);
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 {
64 struct poly1305_state h;
66 - struct poly1305_key r[1];
67 + struct poly1305_key r[CONFIG_CRYPTO_LIB_POLY1305_RSIZE];
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);
73 +static inline void poly1305_init(struct poly1305_desc_ctx *desc, const u8 *key)
75 + if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
76 + poly1305_init_arch(desc, key);
78 + poly1305_init_generic(desc, key);
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);
86 +static inline void poly1305_update(struct poly1305_desc_ctx *desc,
87 + const u8 *src, unsigned int nbytes)
89 + if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
90 + poly1305_update_arch(desc, src, nbytes);
92 + poly1305_update_generic(desc, src, nbytes);
95 +void poly1305_final_arch(struct poly1305_desc_ctx *desc, u8 *digest);
96 +void poly1305_final_generic(struct poly1305_desc_ctx *desc, u8 *digest);
98 +static inline void poly1305_final(struct poly1305_desc_ctx *desc, u8 *digest)
100 + if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
101 + poly1305_final_arch(desc, digest);
103 + poly1305_final_generic(desc, digest);
107 --- a/lib/crypto/Kconfig
108 +++ b/lib/crypto/Kconfig
109 @@ -37,8 +37,34 @@ config CRYPTO_LIB_CHACHA
110 config CRYPTO_LIB_DES
113 +config CRYPTO_LIB_POLY1305_RSIZE
117 +config CRYPTO_ARCH_HAVE_LIB_POLY1305
120 + Declares whether the architecture provides an arch-specific
121 + accelerated implementation of the Poly1305 library interface,
122 + either builtin or as a module.
124 config CRYPTO_LIB_POLY1305_GENERIC
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.
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
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.
142 config CRYPTO_LIB_SHA256
144 --- a/lib/crypto/poly1305.c
145 +++ b/lib/crypto/poly1305.c
146 @@ -154,5 +154,79 @@ void poly1305_core_emit(const struct pol
148 EXPORT_SYMBOL_GPL(poly1305_core_emit);
150 +void poly1305_init_generic(struct poly1305_desc_ctx *desc, const u8 *key)
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);
162 +EXPORT_SYMBOL_GPL(poly1305_init_generic);
164 +void poly1305_update_generic(struct poly1305_desc_ctx *desc, const u8 *src,
165 + unsigned int nbytes)
167 + unsigned int bytes;
169 + if (unlikely(desc->buflen)) {
170 + bytes = min(nbytes, POLY1305_BLOCK_SIZE - desc->buflen);
171 + memcpy(desc->buf + desc->buflen, src, bytes);
174 + desc->buflen += bytes;
176 + if (desc->buflen == POLY1305_BLOCK_SIZE) {
177 + poly1305_core_blocks(&desc->h, desc->r, desc->buf, 1, 1);
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;
189 + if (unlikely(nbytes)) {
190 + desc->buflen = nbytes;
191 + memcpy(desc->buf, src, nbytes);
194 +EXPORT_SYMBOL_GPL(poly1305_update_generic);
196 +void poly1305_final_generic(struct poly1305_desc_ctx *desc, u8 *dst)
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);
208 + poly1305_core_emit(&desc->h, digest);
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);
220 + *desc = (struct poly1305_desc_ctx){};
222 +EXPORT_SYMBOL_GPL(poly1305_final_generic);
224 MODULE_LICENSE("GPL");
225 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");