Skip to content

Commit 878afc3

Browse files
ebiggersherbertx
authored andcommitted
crypto: poly1305 - use structures for key and accumulator
In preparation for exposing a low-level Poly1305 API which implements the ε-almost-∆-universal (εA∆U) hash function underlying the Poly1305 MAC and supports block-aligned inputs only, create structures poly1305_key and poly1305_state which hold the limbs of the Poly1305 "r" key and accumulator, respectively. These structures could actually have the same type (e.g. poly1305_val), but different types are preferable, to prevent misuse. Acked-by: Martin Willi <martin@strongswan.org> Signed-off-by: Eric Biggers <ebiggers@google.com> Acked-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent bdb063a commit 878afc3

File tree

3 files changed

+47
-37
lines changed

3 files changed

+47
-37
lines changed

arch/x86/crypto/poly1305_glue.c

+11-9
Original file line numberDiff line numberDiff line change
@@ -83,35 +83,37 @@ static unsigned int poly1305_simd_blocks(struct poly1305_desc_ctx *dctx,
8383
if (poly1305_use_avx2 && srclen >= POLY1305_BLOCK_SIZE * 4) {
8484
if (unlikely(!sctx->wset)) {
8585
if (!sctx->uset) {
86-
memcpy(sctx->u, dctx->r, sizeof(sctx->u));
87-
poly1305_simd_mult(sctx->u, dctx->r);
86+
memcpy(sctx->u, dctx->r.r, sizeof(sctx->u));
87+
poly1305_simd_mult(sctx->u, dctx->r.r);
8888
sctx->uset = true;
8989
}
9090
memcpy(sctx->u + 5, sctx->u, sizeof(sctx->u));
91-
poly1305_simd_mult(sctx->u + 5, dctx->r);
91+
poly1305_simd_mult(sctx->u + 5, dctx->r.r);
9292
memcpy(sctx->u + 10, sctx->u + 5, sizeof(sctx->u));
93-
poly1305_simd_mult(sctx->u + 10, dctx->r);
93+
poly1305_simd_mult(sctx->u + 10, dctx->r.r);
9494
sctx->wset = true;
9595
}
9696
blocks = srclen / (POLY1305_BLOCK_SIZE * 4);
97-
poly1305_4block_avx2(dctx->h, src, dctx->r, blocks, sctx->u);
97+
poly1305_4block_avx2(dctx->h.h, src, dctx->r.r, blocks,
98+
sctx->u);
9899
src += POLY1305_BLOCK_SIZE * 4 * blocks;
99100
srclen -= POLY1305_BLOCK_SIZE * 4 * blocks;
100101
}
101102
#endif
102103
if (likely(srclen >= POLY1305_BLOCK_SIZE * 2)) {
103104
if (unlikely(!sctx->uset)) {
104-
memcpy(sctx->u, dctx->r, sizeof(sctx->u));
105-
poly1305_simd_mult(sctx->u, dctx->r);
105+
memcpy(sctx->u, dctx->r.r, sizeof(sctx->u));
106+
poly1305_simd_mult(sctx->u, dctx->r.r);
106107
sctx->uset = true;
107108
}
108109
blocks = srclen / (POLY1305_BLOCK_SIZE * 2);
109-
poly1305_2block_sse2(dctx->h, src, dctx->r, blocks, sctx->u);
110+
poly1305_2block_sse2(dctx->h.h, src, dctx->r.r, blocks,
111+
sctx->u);
110112
src += POLY1305_BLOCK_SIZE * 2 * blocks;
111113
srclen -= POLY1305_BLOCK_SIZE * 2 * blocks;
112114
}
113115
if (srclen >= POLY1305_BLOCK_SIZE) {
114-
poly1305_block_sse2(dctx->h, src, dctx->r, 1);
116+
poly1305_block_sse2(dctx->h.h, src, dctx->r.r, 1);
115117
srclen -= POLY1305_BLOCK_SIZE;
116118
}
117119
return srclen;

crypto/poly1305_generic.c

+26-26
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int crypto_poly1305_init(struct shash_desc *desc)
3838
{
3939
struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
4040

41-
memset(dctx->h, 0, sizeof(dctx->h));
41+
memset(dctx->h.h, 0, sizeof(dctx->h.h));
4242
dctx->buflen = 0;
4343
dctx->rset = false;
4444
dctx->sset = false;
@@ -50,11 +50,11 @@ EXPORT_SYMBOL_GPL(crypto_poly1305_init);
5050
static void poly1305_setrkey(struct poly1305_desc_ctx *dctx, const u8 *key)
5151
{
5252
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
53-
dctx->r[0] = (get_unaligned_le32(key + 0) >> 0) & 0x3ffffff;
54-
dctx->r[1] = (get_unaligned_le32(key + 3) >> 2) & 0x3ffff03;
55-
dctx->r[2] = (get_unaligned_le32(key + 6) >> 4) & 0x3ffc0ff;
56-
dctx->r[3] = (get_unaligned_le32(key + 9) >> 6) & 0x3f03fff;
57-
dctx->r[4] = (get_unaligned_le32(key + 12) >> 8) & 0x00fffff;
53+
dctx->r.r[0] = (get_unaligned_le32(key + 0) >> 0) & 0x3ffffff;
54+
dctx->r.r[1] = (get_unaligned_le32(key + 3) >> 2) & 0x3ffff03;
55+
dctx->r.r[2] = (get_unaligned_le32(key + 6) >> 4) & 0x3ffc0ff;
56+
dctx->r.r[3] = (get_unaligned_le32(key + 9) >> 6) & 0x3f03fff;
57+
dctx->r.r[4] = (get_unaligned_le32(key + 12) >> 8) & 0x00fffff;
5858
}
5959

6060
static void poly1305_setskey(struct poly1305_desc_ctx *dctx, const u8 *key)
@@ -107,22 +107,22 @@ static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
107107
srclen = datalen;
108108
}
109109

110-
r0 = dctx->r[0];
111-
r1 = dctx->r[1];
112-
r2 = dctx->r[2];
113-
r3 = dctx->r[3];
114-
r4 = dctx->r[4];
110+
r0 = dctx->r.r[0];
111+
r1 = dctx->r.r[1];
112+
r2 = dctx->r.r[2];
113+
r3 = dctx->r.r[3];
114+
r4 = dctx->r.r[4];
115115

116116
s1 = r1 * 5;
117117
s2 = r2 * 5;
118118
s3 = r3 * 5;
119119
s4 = r4 * 5;
120120

121-
h0 = dctx->h[0];
122-
h1 = dctx->h[1];
123-
h2 = dctx->h[2];
124-
h3 = dctx->h[3];
125-
h4 = dctx->h[4];
121+
h0 = dctx->h.h[0];
122+
h1 = dctx->h.h[1];
123+
h2 = dctx->h.h[2];
124+
h3 = dctx->h.h[3];
125+
h4 = dctx->h.h[4];
126126

127127
while (likely(srclen >= POLY1305_BLOCK_SIZE)) {
128128

@@ -157,11 +157,11 @@ static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
157157
srclen -= POLY1305_BLOCK_SIZE;
158158
}
159159

160-
dctx->h[0] = h0;
161-
dctx->h[1] = h1;
162-
dctx->h[2] = h2;
163-
dctx->h[3] = h3;
164-
dctx->h[4] = h4;
160+
dctx->h.h[0] = h0;
161+
dctx->h.h[1] = h1;
162+
dctx->h.h[2] = h2;
163+
dctx->h.h[3] = h3;
164+
dctx->h.h[4] = h4;
165165

166166
return srclen;
167167
}
@@ -220,11 +220,11 @@ int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
220220
}
221221

222222
/* fully carry h */
223-
h0 = dctx->h[0];
224-
h1 = dctx->h[1];
225-
h2 = dctx->h[2];
226-
h3 = dctx->h[3];
227-
h4 = dctx->h[4];
223+
h0 = dctx->h.h[0];
224+
h1 = dctx->h.h[1];
225+
h2 = dctx->h.h[2];
226+
h3 = dctx->h.h[3];
227+
h4 = dctx->h.h[4];
228228

229229
h2 += (h1 >> 26); h1 = h1 & 0x3ffffff;
230230
h3 += (h2 >> 26); h2 = h2 & 0x3ffffff;

include/crypto/poly1305.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,21 @@
1313
#define POLY1305_KEY_SIZE 32
1414
#define POLY1305_DIGEST_SIZE 16
1515

16+
struct poly1305_key {
17+
u32 r[5]; /* key, base 2^26 */
18+
};
19+
20+
struct poly1305_state {
21+
u32 h[5]; /* accumulator, base 2^26 */
22+
};
23+
1624
struct poly1305_desc_ctx {
1725
/* key */
18-
u32 r[5];
26+
struct poly1305_key r;
1927
/* finalize key */
2028
u32 s[4];
2129
/* accumulator */
22-
u32 h[5];
30+
struct poly1305_state h;
2331
/* partial buffer */
2432
u8 buf[POLY1305_BLOCK_SIZE];
2533
/* bytes used in partial buffer */

0 commit comments

Comments
 (0)