blob: bca99238948f14680eb85747d4ce4264ff077c8d [file] [log] [blame]
Martin Willif979e012015-06-01 13:43:58 +02001/*
2 * Poly1305 authenticator algorithm, RFC7539
3 *
4 * Copyright (C) 2015 Martin Willi
5 *
6 * Based on public domain code by Andrew Moon and Daniel J. Bernstein.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <crypto/algapi.h>
15#include <crypto/internal/hash.h>
Martin Willi2546f812015-07-16 19:14:05 +020016#include <crypto/poly1305.h>
Martin Willif979e012015-06-01 13:43:58 +020017#include <linux/crypto.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20
Martin Willif979e012015-06-01 13:43:58 +020021static inline u64 mlt(u64 a, u64 b)
22{
23 return a * b;
24}
25
26static inline u32 sr(u64 v, u_char n)
27{
28 return v >> n;
29}
30
31static inline u32 and(u32 v, u32 mask)
32{
33 return v & mask;
34}
35
36static inline u32 le32_to_cpuvp(const void *p)
37{
38 return le32_to_cpup(p);
39}
40
Martin Willi2546f812015-07-16 19:14:05 +020041int crypto_poly1305_init(struct shash_desc *desc)
Martin Willif979e012015-06-01 13:43:58 +020042{
43 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
44
45 memset(dctx->h, 0, sizeof(dctx->h));
46 dctx->buflen = 0;
Martin Willic2b7b20a2015-06-16 11:34:16 +020047 dctx->rset = false;
48 dctx->sset = false;
Martin Willif979e012015-06-01 13:43:58 +020049
50 return 0;
51}
Martin Willi2546f812015-07-16 19:14:05 +020052EXPORT_SYMBOL_GPL(crypto_poly1305_init);
Martin Willif979e012015-06-01 13:43:58 +020053
Martin Willic2b7b20a2015-06-16 11:34:16 +020054static void poly1305_setrkey(struct poly1305_desc_ctx *dctx, const u8 *key)
55{
Martin Willif979e012015-06-01 13:43:58 +020056 /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
Martin Willic2b7b20a2015-06-16 11:34:16 +020057 dctx->r[0] = (le32_to_cpuvp(key + 0) >> 0) & 0x3ffffff;
58 dctx->r[1] = (le32_to_cpuvp(key + 3) >> 2) & 0x3ffff03;
59 dctx->r[2] = (le32_to_cpuvp(key + 6) >> 4) & 0x3ffc0ff;
60 dctx->r[3] = (le32_to_cpuvp(key + 9) >> 6) & 0x3f03fff;
61 dctx->r[4] = (le32_to_cpuvp(key + 12) >> 8) & 0x00fffff;
62}
Martin Willif979e012015-06-01 13:43:58 +020063
Martin Willic2b7b20a2015-06-16 11:34:16 +020064static void poly1305_setskey(struct poly1305_desc_ctx *dctx, const u8 *key)
65{
66 dctx->s[0] = le32_to_cpuvp(key + 0);
67 dctx->s[1] = le32_to_cpuvp(key + 4);
68 dctx->s[2] = le32_to_cpuvp(key + 8);
69 dctx->s[3] = le32_to_cpuvp(key + 12);
Martin Willif979e012015-06-01 13:43:58 +020070}
71
Eric Biggersb93728342018-01-03 11:16:25 -080072/*
73 * Poly1305 requires a unique key for each tag, which implies that we can't set
74 * it on the tfm that gets accessed by multiple users simultaneously. Instead we
75 * expect the key as the first 32 bytes in the update() call.
76 */
Martin Willi2546f812015-07-16 19:14:05 +020077unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx,
78 const u8 *src, unsigned int srclen)
Martin Willif979e012015-06-01 13:43:58 +020079{
Martin Willi2546f812015-07-16 19:14:05 +020080 if (!dctx->sset) {
Martin Willic2b7b20a2015-06-16 11:34:16 +020081 if (!dctx->rset && srclen >= POLY1305_BLOCK_SIZE) {
82 poly1305_setrkey(dctx, src);
83 src += POLY1305_BLOCK_SIZE;
84 srclen -= POLY1305_BLOCK_SIZE;
85 dctx->rset = true;
86 }
87 if (srclen >= POLY1305_BLOCK_SIZE) {
88 poly1305_setskey(dctx, src);
89 src += POLY1305_BLOCK_SIZE;
90 srclen -= POLY1305_BLOCK_SIZE;
91 dctx->sset = true;
92 }
93 }
Martin Willi2546f812015-07-16 19:14:05 +020094 return srclen;
95}
96EXPORT_SYMBOL_GPL(crypto_poly1305_setdesckey);
97
98static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
99 const u8 *src, unsigned int srclen,
100 u32 hibit)
101{
102 u32 r0, r1, r2, r3, r4;
103 u32 s1, s2, s3, s4;
104 u32 h0, h1, h2, h3, h4;
105 u64 d0, d1, d2, d3, d4;
106 unsigned int datalen;
107
108 if (unlikely(!dctx->sset)) {
109 datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
110 src += srclen - datalen;
111 srclen = datalen;
112 }
Martin Willic2b7b20a2015-06-16 11:34:16 +0200113
114 r0 = dctx->r[0];
115 r1 = dctx->r[1];
116 r2 = dctx->r[2];
117 r3 = dctx->r[3];
118 r4 = dctx->r[4];
Martin Willif979e012015-06-01 13:43:58 +0200119
120 s1 = r1 * 5;
121 s2 = r2 * 5;
122 s3 = r3 * 5;
123 s4 = r4 * 5;
124
125 h0 = dctx->h[0];
126 h1 = dctx->h[1];
127 h2 = dctx->h[2];
128 h3 = dctx->h[3];
129 h4 = dctx->h[4];
130
131 while (likely(srclen >= POLY1305_BLOCK_SIZE)) {
132
133 /* h += m[i] */
134 h0 += (le32_to_cpuvp(src + 0) >> 0) & 0x3ffffff;
135 h1 += (le32_to_cpuvp(src + 3) >> 2) & 0x3ffffff;
136 h2 += (le32_to_cpuvp(src + 6) >> 4) & 0x3ffffff;
137 h3 += (le32_to_cpuvp(src + 9) >> 6) & 0x3ffffff;
138 h4 += (le32_to_cpuvp(src + 12) >> 8) | hibit;
139
140 /* h *= r */
141 d0 = mlt(h0, r0) + mlt(h1, s4) + mlt(h2, s3) +
142 mlt(h3, s2) + mlt(h4, s1);
143 d1 = mlt(h0, r1) + mlt(h1, r0) + mlt(h2, s4) +
144 mlt(h3, s3) + mlt(h4, s2);
145 d2 = mlt(h0, r2) + mlt(h1, r1) + mlt(h2, r0) +
146 mlt(h3, s4) + mlt(h4, s3);
147 d3 = mlt(h0, r3) + mlt(h1, r2) + mlt(h2, r1) +
148 mlt(h3, r0) + mlt(h4, s4);
149 d4 = mlt(h0, r4) + mlt(h1, r3) + mlt(h2, r2) +
150 mlt(h3, r1) + mlt(h4, r0);
151
152 /* (partial) h %= p */
153 d1 += sr(d0, 26); h0 = and(d0, 0x3ffffff);
154 d2 += sr(d1, 26); h1 = and(d1, 0x3ffffff);
155 d3 += sr(d2, 26); h2 = and(d2, 0x3ffffff);
156 d4 += sr(d3, 26); h3 = and(d3, 0x3ffffff);
157 h0 += sr(d4, 26) * 5; h4 = and(d4, 0x3ffffff);
158 h1 += h0 >> 26; h0 = h0 & 0x3ffffff;
159
160 src += POLY1305_BLOCK_SIZE;
161 srclen -= POLY1305_BLOCK_SIZE;
162 }
163
164 dctx->h[0] = h0;
165 dctx->h[1] = h1;
166 dctx->h[2] = h2;
167 dctx->h[3] = h3;
168 dctx->h[4] = h4;
169
170 return srclen;
171}
172
Martin Willi2546f812015-07-16 19:14:05 +0200173int crypto_poly1305_update(struct shash_desc *desc,
Martin Willif979e012015-06-01 13:43:58 +0200174 const u8 *src, unsigned int srclen)
175{
176 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
Martin Willif979e012015-06-01 13:43:58 +0200177 unsigned int bytes;
178
179 if (unlikely(dctx->buflen)) {
180 bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
181 memcpy(dctx->buf + dctx->buflen, src, bytes);
182 src += bytes;
183 srclen -= bytes;
184 dctx->buflen += bytes;
185
186 if (dctx->buflen == POLY1305_BLOCK_SIZE) {
Martin Willic2b7b20a2015-06-16 11:34:16 +0200187 poly1305_blocks(dctx, dctx->buf,
Martin Willif979e012015-06-01 13:43:58 +0200188 POLY1305_BLOCK_SIZE, 1 << 24);
189 dctx->buflen = 0;
190 }
191 }
192
193 if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
Martin Willic2b7b20a2015-06-16 11:34:16 +0200194 bytes = poly1305_blocks(dctx, src, srclen, 1 << 24);
Martin Willif979e012015-06-01 13:43:58 +0200195 src += srclen - bytes;
196 srclen = bytes;
197 }
198
199 if (unlikely(srclen)) {
200 dctx->buflen = srclen;
201 memcpy(dctx->buf, src, srclen);
202 }
203
204 return 0;
205}
Martin Willi2546f812015-07-16 19:14:05 +0200206EXPORT_SYMBOL_GPL(crypto_poly1305_update);
Martin Willif979e012015-06-01 13:43:58 +0200207
Martin Willi2546f812015-07-16 19:14:05 +0200208int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
Martin Willif979e012015-06-01 13:43:58 +0200209{
210 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
Martin Willif979e012015-06-01 13:43:58 +0200211 __le32 *mac = (__le32 *)dst;
212 u32 h0, h1, h2, h3, h4;
213 u32 g0, g1, g2, g3, g4;
214 u32 mask;
215 u64 f = 0;
216
Martin Willic2b7b20a2015-06-16 11:34:16 +0200217 if (unlikely(!dctx->sset))
218 return -ENOKEY;
219
Martin Willif979e012015-06-01 13:43:58 +0200220 if (unlikely(dctx->buflen)) {
221 dctx->buf[dctx->buflen++] = 1;
222 memset(dctx->buf + dctx->buflen, 0,
223 POLY1305_BLOCK_SIZE - dctx->buflen);
Martin Willic2b7b20a2015-06-16 11:34:16 +0200224 poly1305_blocks(dctx, dctx->buf, POLY1305_BLOCK_SIZE, 0);
Martin Willif979e012015-06-01 13:43:58 +0200225 }
226
227 /* fully carry h */
228 h0 = dctx->h[0];
229 h1 = dctx->h[1];
230 h2 = dctx->h[2];
231 h3 = dctx->h[3];
232 h4 = dctx->h[4];
233
234 h2 += (h1 >> 26); h1 = h1 & 0x3ffffff;
235 h3 += (h2 >> 26); h2 = h2 & 0x3ffffff;
236 h4 += (h3 >> 26); h3 = h3 & 0x3ffffff;
237 h0 += (h4 >> 26) * 5; h4 = h4 & 0x3ffffff;
238 h1 += (h0 >> 26); h0 = h0 & 0x3ffffff;
239
240 /* compute h + -p */
241 g0 = h0 + 5;
242 g1 = h1 + (g0 >> 26); g0 &= 0x3ffffff;
243 g2 = h2 + (g1 >> 26); g1 &= 0x3ffffff;
244 g3 = h3 + (g2 >> 26); g2 &= 0x3ffffff;
245 g4 = h4 + (g3 >> 26) - (1 << 26); g3 &= 0x3ffffff;
246
247 /* select h if h < p, or h + -p if h >= p */
248 mask = (g4 >> ((sizeof(u32) * 8) - 1)) - 1;
249 g0 &= mask;
250 g1 &= mask;
251 g2 &= mask;
252 g3 &= mask;
253 g4 &= mask;
254 mask = ~mask;
255 h0 = (h0 & mask) | g0;
256 h1 = (h1 & mask) | g1;
257 h2 = (h2 & mask) | g2;
258 h3 = (h3 & mask) | g3;
259 h4 = (h4 & mask) | g4;
260
261 /* h = h % (2^128) */
262 h0 = (h0 >> 0) | (h1 << 26);
263 h1 = (h1 >> 6) | (h2 << 20);
264 h2 = (h2 >> 12) | (h3 << 14);
265 h3 = (h3 >> 18) | (h4 << 8);
266
267 /* mac = (h + s) % (2^128) */
Martin Willic2b7b20a2015-06-16 11:34:16 +0200268 f = (f >> 32) + h0 + dctx->s[0]; mac[0] = cpu_to_le32(f);
269 f = (f >> 32) + h1 + dctx->s[1]; mac[1] = cpu_to_le32(f);
270 f = (f >> 32) + h2 + dctx->s[2]; mac[2] = cpu_to_le32(f);
271 f = (f >> 32) + h3 + dctx->s[3]; mac[3] = cpu_to_le32(f);
Martin Willif979e012015-06-01 13:43:58 +0200272
273 return 0;
274}
Martin Willi2546f812015-07-16 19:14:05 +0200275EXPORT_SYMBOL_GPL(crypto_poly1305_final);
Martin Willif979e012015-06-01 13:43:58 +0200276
277static struct shash_alg poly1305_alg = {
278 .digestsize = POLY1305_DIGEST_SIZE,
Martin Willi2546f812015-07-16 19:14:05 +0200279 .init = crypto_poly1305_init,
280 .update = crypto_poly1305_update,
281 .final = crypto_poly1305_final,
Martin Willif979e012015-06-01 13:43:58 +0200282 .descsize = sizeof(struct poly1305_desc_ctx),
283 .base = {
284 .cra_name = "poly1305",
285 .cra_driver_name = "poly1305-generic",
286 .cra_priority = 100,
287 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
288 .cra_alignmask = sizeof(u32) - 1,
289 .cra_blocksize = POLY1305_BLOCK_SIZE,
Martin Willif979e012015-06-01 13:43:58 +0200290 .cra_module = THIS_MODULE,
291 },
292};
293
294static int __init poly1305_mod_init(void)
295{
296 return crypto_register_shash(&poly1305_alg);
297}
298
299static void __exit poly1305_mod_exit(void)
300{
301 crypto_unregister_shash(&poly1305_alg);
302}
303
304module_init(poly1305_mod_init);
305module_exit(poly1305_mod_exit);
306
307MODULE_LICENSE("GPL");
308MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
309MODULE_DESCRIPTION("Poly1305 authenticator");
310MODULE_ALIAS_CRYPTO("poly1305");
311MODULE_ALIAS_CRYPTO("poly1305-generic");