RDMA/srp: Rework SCSI device reset handling
[linux/fpc-iii.git] / crypto / poly1305_generic.c
blobbca99238948f14680eb85747d4ce4264ff077c8d
1 /*
2 * Poly1305 authenticator algorithm, RFC7539
4 * Copyright (C) 2015 Martin Willi
6 * Based on public domain code by Andrew Moon and Daniel J. Bernstein.
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.
14 #include <crypto/algapi.h>
15 #include <crypto/internal/hash.h>
16 #include <crypto/poly1305.h>
17 #include <linux/crypto.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
21 static inline u64 mlt(u64 a, u64 b)
23 return a * b;
26 static inline u32 sr(u64 v, u_char n)
28 return v >> n;
31 static inline u32 and(u32 v, u32 mask)
33 return v & mask;
36 static inline u32 le32_to_cpuvp(const void *p)
38 return le32_to_cpup(p);
41 int crypto_poly1305_init(struct shash_desc *desc)
43 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
45 memset(dctx->h, 0, sizeof(dctx->h));
46 dctx->buflen = 0;
47 dctx->rset = false;
48 dctx->sset = false;
50 return 0;
52 EXPORT_SYMBOL_GPL(crypto_poly1305_init);
54 static void poly1305_setrkey(struct poly1305_desc_ctx *dctx, const u8 *key)
56 /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
57 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;
64 static void poly1305_setskey(struct poly1305_desc_ctx *dctx, const u8 *key)
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);
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.
77 unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx,
78 const u8 *src, unsigned int srclen)
80 if (!dctx->sset) {
81 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;
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;
94 return srclen;
96 EXPORT_SYMBOL_GPL(crypto_poly1305_setdesckey);
98 static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
99 const u8 *src, unsigned int srclen,
100 u32 hibit)
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;
108 if (unlikely(!dctx->sset)) {
109 datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
110 src += srclen - datalen;
111 srclen = datalen;
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];
120 s1 = r1 * 5;
121 s2 = r2 * 5;
122 s3 = r3 * 5;
123 s4 = r4 * 5;
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];
131 while (likely(srclen >= POLY1305_BLOCK_SIZE)) {
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;
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);
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;
160 src += POLY1305_BLOCK_SIZE;
161 srclen -= POLY1305_BLOCK_SIZE;
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;
170 return srclen;
173 int crypto_poly1305_update(struct shash_desc *desc,
174 const u8 *src, unsigned int srclen)
176 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
177 unsigned int bytes;
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;
186 if (dctx->buflen == POLY1305_BLOCK_SIZE) {
187 poly1305_blocks(dctx, dctx->buf,
188 POLY1305_BLOCK_SIZE, 1 << 24);
189 dctx->buflen = 0;
193 if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
194 bytes = poly1305_blocks(dctx, src, srclen, 1 << 24);
195 src += srclen - bytes;
196 srclen = bytes;
199 if (unlikely(srclen)) {
200 dctx->buflen = srclen;
201 memcpy(dctx->buf, src, srclen);
204 return 0;
206 EXPORT_SYMBOL_GPL(crypto_poly1305_update);
208 int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
210 struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
211 __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;
217 if (unlikely(!dctx->sset))
218 return -ENOKEY;
220 if (unlikely(dctx->buflen)) {
221 dctx->buf[dctx->buflen++] = 1;
222 memset(dctx->buf + dctx->buflen, 0,
223 POLY1305_BLOCK_SIZE - dctx->buflen);
224 poly1305_blocks(dctx, dctx->buf, POLY1305_BLOCK_SIZE, 0);
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];
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;
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;
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;
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);
267 /* mac = (h + s) % (2^128) */
268 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);
273 return 0;
275 EXPORT_SYMBOL_GPL(crypto_poly1305_final);
277 static struct shash_alg poly1305_alg = {
278 .digestsize = POLY1305_DIGEST_SIZE,
279 .init = crypto_poly1305_init,
280 .update = crypto_poly1305_update,
281 .final = crypto_poly1305_final,
282 .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,
290 .cra_module = THIS_MODULE,
294 static int __init poly1305_mod_init(void)
296 return crypto_register_shash(&poly1305_alg);
299 static void __exit poly1305_mod_exit(void)
301 crypto_unregister_shash(&poly1305_alg);
304 module_init(poly1305_mod_init);
305 module_exit(poly1305_mod_exit);
307 MODULE_LICENSE("GPL");
308 MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
309 MODULE_DESCRIPTION("Poly1305 authenticator");
310 MODULE_ALIAS_CRYPTO("poly1305");
311 MODULE_ALIAS_CRYPTO("poly1305-generic");