Skip to content

Commit 2546f81

Browse files
martinwilliherbertx
authored andcommitted
crypto: poly1305 - Export common Poly1305 helpers
As architecture specific drivers need a software fallback, export Poly1305 init/update/final functions together with some helpers in a header file. Signed-off-by: Martin Willi <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 6692cbc commit 2546f81

File tree

3 files changed

+77
-41
lines changed

3 files changed

+77
-41
lines changed

crypto/chacha20poly1305.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,14 @@
1414
#include <crypto/internal/skcipher.h>
1515
#include <crypto/scatterwalk.h>
1616
#include <crypto/chacha20.h>
17+
#include <crypto/poly1305.h>
1718
#include <linux/err.h>
1819
#include <linux/init.h>
1920
#include <linux/kernel.h>
2021
#include <linux/module.h>
2122

2223
#include "internal.h"
2324

24-
#define POLY1305_BLOCK_SIZE 16
25-
#define POLY1305_DIGEST_SIZE 16
26-
#define POLY1305_KEY_SIZE 32
2725
#define CHACHAPOLY_IV_SIZE 12
2826

2927
struct chachapoly_instance_ctx {

crypto/poly1305_generic.c

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,11 @@
1313

1414
#include <crypto/algapi.h>
1515
#include <crypto/internal/hash.h>
16+
#include <crypto/poly1305.h>
1617
#include <linux/crypto.h>
1718
#include <linux/kernel.h>
1819
#include <linux/module.h>
1920

20-
#define POLY1305_BLOCK_SIZE 16
21-
#define POLY1305_KEY_SIZE 32
22-
#define POLY1305_DIGEST_SIZE 16
23-
24-
struct poly1305_desc_ctx {
25-
/* key */
26-
u32 r[5];
27-
/* finalize key */
28-
u32 s[4];
29-
/* accumulator */
30-
u32 h[5];
31-
/* partial buffer */
32-
u8 buf[POLY1305_BLOCK_SIZE];
33-
/* bytes used in partial buffer */
34-
unsigned int buflen;
35-
/* r key has been set */
36-
bool rset;
37-
/* s key has been set */
38-
bool sset;
39-
};
40-
4121
static inline u64 mlt(u64 a, u64 b)
4222
{
4323
return a * b;
@@ -58,7 +38,7 @@ static inline u32 le32_to_cpuvp(const void *p)
5838
return le32_to_cpup(p);
5939
}
6040

61-
static int poly1305_init(struct shash_desc *desc)
41+
int crypto_poly1305_init(struct shash_desc *desc)
6242
{
6343
struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
6444

@@ -69,8 +49,9 @@ static int poly1305_init(struct shash_desc *desc)
6949

7050
return 0;
7151
}
52+
EXPORT_SYMBOL_GPL(crypto_poly1305_init);
7253

73-
static int poly1305_setkey(struct crypto_shash *tfm,
54+
int crypto_poly1305_setkey(struct crypto_shash *tfm,
7455
const u8 *key, unsigned int keylen)
7556
{
7657
/* Poly1305 requires a unique key for each tag, which implies that
@@ -79,6 +60,7 @@ static int poly1305_setkey(struct crypto_shash *tfm,
7960
* the update() call. */
8061
return -ENOTSUPP;
8162
}
63+
EXPORT_SYMBOL_GPL(crypto_poly1305_setkey);
8264

8365
static void poly1305_setrkey(struct poly1305_desc_ctx *dctx, const u8 *key)
8466
{
@@ -98,16 +80,10 @@ static void poly1305_setskey(struct poly1305_desc_ctx *dctx, const u8 *key)
9880
dctx->s[3] = le32_to_cpuvp(key + 12);
9981
}
10082

101-
static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
102-
const u8 *src, unsigned int srclen,
103-
u32 hibit)
83+
unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx,
84+
const u8 *src, unsigned int srclen)
10485
{
105-
u32 r0, r1, r2, r3, r4;
106-
u32 s1, s2, s3, s4;
107-
u32 h0, h1, h2, h3, h4;
108-
u64 d0, d1, d2, d3, d4;
109-
110-
if (unlikely(!dctx->sset)) {
86+
if (!dctx->sset) {
11187
if (!dctx->rset && srclen >= POLY1305_BLOCK_SIZE) {
11288
poly1305_setrkey(dctx, src);
11389
src += POLY1305_BLOCK_SIZE;
@@ -121,6 +97,25 @@ static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
12197
dctx->sset = true;
12298
}
12399
}
100+
return srclen;
101+
}
102+
EXPORT_SYMBOL_GPL(crypto_poly1305_setdesckey);
103+
104+
static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
105+
const u8 *src, unsigned int srclen,
106+
u32 hibit)
107+
{
108+
u32 r0, r1, r2, r3, r4;
109+
u32 s1, s2, s3, s4;
110+
u32 h0, h1, h2, h3, h4;
111+
u64 d0, d1, d2, d3, d4;
112+
unsigned int datalen;
113+
114+
if (unlikely(!dctx->sset)) {
115+
datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
116+
src += srclen - datalen;
117+
srclen = datalen;
118+
}
124119

125120
r0 = dctx->r[0];
126121
r1 = dctx->r[1];
@@ -181,7 +176,7 @@ static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
181176
return srclen;
182177
}
183178

184-
static int poly1305_update(struct shash_desc *desc,
179+
int crypto_poly1305_update(struct shash_desc *desc,
185180
const u8 *src, unsigned int srclen)
186181
{
187182
struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
@@ -214,8 +209,9 @@ static int poly1305_update(struct shash_desc *desc,
214209

215210
return 0;
216211
}
212+
EXPORT_SYMBOL_GPL(crypto_poly1305_update);
217213

218-
static int poly1305_final(struct shash_desc *desc, u8 *dst)
214+
int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
219215
{
220216
struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
221217
__le32 *mac = (__le32 *)dst;
@@ -282,13 +278,14 @@ static int poly1305_final(struct shash_desc *desc, u8 *dst)
282278

283279
return 0;
284280
}
281+
EXPORT_SYMBOL_GPL(crypto_poly1305_final);
285282

286283
static struct shash_alg poly1305_alg = {
287284
.digestsize = POLY1305_DIGEST_SIZE,
288-
.init = poly1305_init,
289-
.update = poly1305_update,
290-
.final = poly1305_final,
291-
.setkey = poly1305_setkey,
285+
.init = crypto_poly1305_init,
286+
.update = crypto_poly1305_update,
287+
.final = crypto_poly1305_final,
288+
.setkey = crypto_poly1305_setkey,
292289
.descsize = sizeof(struct poly1305_desc_ctx),
293290
.base = {
294291
.cra_name = "poly1305",

include/crypto/poly1305.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Common values for the Poly1305 algorithm
3+
*/
4+
5+
#ifndef _CRYPTO_POLY1305_H
6+
#define _CRYPTO_POLY1305_H
7+
8+
#include <linux/types.h>
9+
#include <linux/crypto.h>
10+
11+
#define POLY1305_BLOCK_SIZE 16
12+
#define POLY1305_KEY_SIZE 32
13+
#define POLY1305_DIGEST_SIZE 16
14+
15+
struct poly1305_desc_ctx {
16+
/* key */
17+
u32 r[5];
18+
/* finalize key */
19+
u32 s[4];
20+
/* accumulator */
21+
u32 h[5];
22+
/* partial buffer */
23+
u8 buf[POLY1305_BLOCK_SIZE];
24+
/* bytes used in partial buffer */
25+
unsigned int buflen;
26+
/* r key has been set */
27+
bool rset;
28+
/* s key has been set */
29+
bool sset;
30+
};
31+
32+
int crypto_poly1305_init(struct shash_desc *desc);
33+
int crypto_poly1305_setkey(struct crypto_shash *tfm,
34+
const u8 *key, unsigned int keylen);
35+
unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx,
36+
const u8 *src, unsigned int srclen);
37+
int crypto_poly1305_update(struct shash_desc *desc,
38+
const u8 *src, unsigned int srclen);
39+
int crypto_poly1305_final(struct shash_desc *desc, u8 *dst);
40+
41+
#endif

0 commit comments

Comments
 (0)