Skip to content

Commit c2b7b20

Browse files
martinwilliherbertx
authored andcommitted
crypto: poly1305 - Pass key as first two message blocks to each desc_ctx
The Poly1305 authenticator requires a unique key for each generated tag. This implies that we can't set the key per tfm, as multiple users set individual keys. Instead we pass a desc specific key as the first two blocks of the message to authenticate in update(). Signed-off-by: Martin Willi <martin@strongswan.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 8f69b76 commit c2b7b20

3 files changed

Lines changed: 134 additions & 116 deletions

File tree

‎crypto/chacha20poly1305.c‎

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ struct poly_req {
5454
};
5555

5656
struct chacha_req {
57-
/* the key we generate for Poly1305 using Chacha20 */
58-
u8 key[POLY1305_KEY_SIZE];
5957
u8 iv[CHACHA20_IV_SIZE];
6058
struct scatterlist src[1];
6159
struct ablkcipher_request req; /* must be last member */
6260
};
6361

6462
struct chachapoly_req_ctx {
63+
/* the key we generate for Poly1305 using Chacha20 */
64+
u8 key[POLY1305_KEY_SIZE];
6565
/* calculated Poly1305 tag */
6666
u8 tag[POLY1305_DIGEST_SIZE];
6767
/* length of data to en/decrypt, without ICV */
@@ -294,53 +294,59 @@ static int poly_ad(struct aead_request *req)
294294
return poly_adpad(req);
295295
}
296296

297-
static void poly_init_done(struct crypto_async_request *areq, int err)
297+
static void poly_setkey_done(struct crypto_async_request *areq, int err)
298298
{
299299
async_done_continue(areq->data, err, poly_ad);
300300
}
301301

302-
static int poly_init(struct aead_request *req)
302+
static int poly_setkey(struct aead_request *req)
303303
{
304304
struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
305305
struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
306306
struct poly_req *preq = &rctx->u.poly;
307307
int err;
308308

309+
sg_init_table(preq->src, 1);
310+
sg_set_buf(preq->src, rctx->key, sizeof(rctx->key));
311+
309312
ahash_request_set_callback(&preq->req, aead_request_flags(req),
310-
poly_init_done, req);
313+
poly_setkey_done, req);
311314
ahash_request_set_tfm(&preq->req, ctx->poly);
315+
ahash_request_set_crypt(&preq->req, preq->src, NULL, sizeof(rctx->key));
312316

313-
err = crypto_ahash_init(&preq->req);
317+
err = crypto_ahash_update(&preq->req);
314318
if (err)
315319
return err;
316320

317321
return poly_ad(req);
318322
}
319323

320-
static int poly_genkey_continue(struct aead_request *req)
324+
static void poly_init_done(struct crypto_async_request *areq, int err)
321325
{
322-
struct crypto_aead *aead = crypto_aead_reqtfm(req);
323-
struct chachapoly_ctx *ctx = crypto_aead_ctx(aead);
326+
async_done_continue(areq->data, err, poly_setkey);
327+
}
328+
329+
static int poly_init(struct aead_request *req)
330+
{
331+
struct chachapoly_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
324332
struct chachapoly_req_ctx *rctx = aead_request_ctx(req);
325-
struct chacha_req *creq = &rctx->u.chacha;
333+
struct poly_req *preq = &rctx->u.poly;
326334
int err;
327335

328-
crypto_ahash_clear_flags(ctx->poly, CRYPTO_TFM_REQ_MASK);
329-
crypto_ahash_set_flags(ctx->poly, crypto_aead_get_flags(aead) &
330-
CRYPTO_TFM_REQ_MASK);
336+
ahash_request_set_callback(&preq->req, aead_request_flags(req),
337+
poly_init_done, req);
338+
ahash_request_set_tfm(&preq->req, ctx->poly);
331339

332-
err = crypto_ahash_setkey(ctx->poly, creq->key, sizeof(creq->key));
333-
crypto_aead_set_flags(aead, crypto_ahash_get_flags(ctx->poly) &
334-
CRYPTO_TFM_RES_MASK);
340+
err = crypto_ahash_init(&preq->req);
335341
if (err)
336342
return err;
337343

338-
return poly_init(req);
344+
return poly_setkey(req);
339345
}
340346

341347
static void poly_genkey_done(struct crypto_async_request *areq, int err)
342348
{
343-
async_done_continue(areq->data, err, poly_genkey_continue);
349+
async_done_continue(areq->data, err, poly_init);
344350
}
345351

346352
static int poly_genkey(struct aead_request *req)
@@ -351,8 +357,8 @@ static int poly_genkey(struct aead_request *req)
351357
int err;
352358

353359
sg_init_table(creq->src, 1);
354-
memset(creq->key, 0, sizeof(creq->key));
355-
sg_set_buf(creq->src, creq->key, sizeof(creq->key));
360+
memset(rctx->key, 0, sizeof(rctx->key));
361+
sg_set_buf(creq->src, rctx->key, sizeof(rctx->key));
356362

357363
chacha_iv(creq->iv, req, 0);
358364

@@ -366,7 +372,7 @@ static int poly_genkey(struct aead_request *req)
366372
if (err)
367373
return err;
368374

369-
return poly_genkey_continue(req);
375+
return poly_init(req);
370376
}
371377

372378
static void chacha_encrypt_done(struct crypto_async_request *areq, int err)
@@ -403,8 +409,9 @@ static int chachapoly_encrypt(struct aead_request *req)
403409

404410
/* encrypt call chain:
405411
* - chacha_encrypt/done()
406-
* - poly_genkey/done/continue()
412+
* - poly_genkey/done()
407413
* - poly_init/done()
414+
* - poly_setkey/done()
408415
* - poly_ad/done()
409416
* - poly_adpad/done()
410417
* - poly_cipher/done()
@@ -424,8 +431,9 @@ static int chachapoly_decrypt(struct aead_request *req)
424431
rctx->cryptlen = req->cryptlen - POLY1305_DIGEST_SIZE;
425432

426433
/* decrypt call chain:
427-
* - poly_genkey/done/continue()
434+
* - poly_genkey/done()
428435
* - poly_init/done()
436+
* - poly_setkey/done()
429437
* - poly_ad/done()
430438
* - poly_adpad/done()
431439
* - poly_cipher/done()

‎crypto/poly1305_generic.c‎

Lines changed: 59 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,21 @@
2121
#define POLY1305_KEY_SIZE 32
2222
#define POLY1305_DIGEST_SIZE 16
2323

24-
struct poly1305_ctx {
24+
struct poly1305_desc_ctx {
2525
/* key */
2626
u32 r[5];
2727
/* finalize key */
2828
u32 s[4];
29-
};
30-
31-
struct poly1305_desc_ctx {
3229
/* accumulator */
3330
u32 h[5];
3431
/* partial buffer */
3532
u8 buf[POLY1305_BLOCK_SIZE];
3633
/* bytes used in partial buffer */
3734
unsigned int buflen;
35+
/* r key has been set */
36+
bool rset;
37+
/* s key has been set */
38+
bool sset;
3839
};
3940

4041
static inline u64 mlt(u64 a, u64 b)
@@ -63,49 +64,69 @@ static int poly1305_init(struct shash_desc *desc)
6364

6465
memset(dctx->h, 0, sizeof(dctx->h));
6566
dctx->buflen = 0;
67+
dctx->rset = false;
68+
dctx->sset = false;
6669

6770
return 0;
6871
}
6972

7073
static int poly1305_setkey(struct crypto_shash *tfm,
7174
const u8 *key, unsigned int keylen)
7275
{
73-
struct poly1305_ctx *ctx = crypto_shash_ctx(tfm);
74-
75-
if (keylen != POLY1305_KEY_SIZE) {
76-
crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
77-
return -EINVAL;
78-
}
76+
/* Poly1305 requires a unique key for each tag, which implies that
77+
* we can't set it on the tfm that gets accessed by multiple users
78+
* simultaneously. Instead we expect the key as the first 32 bytes in
79+
* the update() call. */
80+
return -ENOTSUPP;
81+
}
7982

83+
static void poly1305_setrkey(struct poly1305_desc_ctx *dctx, const u8 *key)
84+
{
8085
/* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
81-
ctx->r[0] = (le32_to_cpuvp(key + 0) >> 0) & 0x3ffffff;
82-
ctx->r[1] = (le32_to_cpuvp(key + 3) >> 2) & 0x3ffff03;
83-
ctx->r[2] = (le32_to_cpuvp(key + 6) >> 4) & 0x3ffc0ff;
84-
ctx->r[3] = (le32_to_cpuvp(key + 9) >> 6) & 0x3f03fff;
85-
ctx->r[4] = (le32_to_cpuvp(key + 12) >> 8) & 0x00fffff;
86-
87-
ctx->s[0] = le32_to_cpuvp(key + 16);
88-
ctx->s[1] = le32_to_cpuvp(key + 20);
89-
ctx->s[2] = le32_to_cpuvp(key + 24);
90-
ctx->s[3] = le32_to_cpuvp(key + 28);
86+
dctx->r[0] = (le32_to_cpuvp(key + 0) >> 0) & 0x3ffffff;
87+
dctx->r[1] = (le32_to_cpuvp(key + 3) >> 2) & 0x3ffff03;
88+
dctx->r[2] = (le32_to_cpuvp(key + 6) >> 4) & 0x3ffc0ff;
89+
dctx->r[3] = (le32_to_cpuvp(key + 9) >> 6) & 0x3f03fff;
90+
dctx->r[4] = (le32_to_cpuvp(key + 12) >> 8) & 0x00fffff;
91+
}
9192

92-
return 0;
93+
static void poly1305_setskey(struct poly1305_desc_ctx *dctx, const u8 *key)
94+
{
95+
dctx->s[0] = le32_to_cpuvp(key + 0);
96+
dctx->s[1] = le32_to_cpuvp(key + 4);
97+
dctx->s[2] = le32_to_cpuvp(key + 8);
98+
dctx->s[3] = le32_to_cpuvp(key + 12);
9399
}
94100

95101
static unsigned int poly1305_blocks(struct poly1305_desc_ctx *dctx,
96-
struct poly1305_ctx *ctx, const u8 *src,
97-
unsigned int srclen, u32 hibit)
102+
const u8 *src, unsigned int srclen,
103+
u32 hibit)
98104
{
99105
u32 r0, r1, r2, r3, r4;
100106
u32 s1, s2, s3, s4;
101107
u32 h0, h1, h2, h3, h4;
102108
u64 d0, d1, d2, d3, d4;
103109

104-
r0 = ctx->r[0];
105-
r1 = ctx->r[1];
106-
r2 = ctx->r[2];
107-
r3 = ctx->r[3];
108-
r4 = ctx->r[4];
110+
if (unlikely(!dctx->sset)) {
111+
if (!dctx->rset && srclen >= POLY1305_BLOCK_SIZE) {
112+
poly1305_setrkey(dctx, src);
113+
src += POLY1305_BLOCK_SIZE;
114+
srclen -= POLY1305_BLOCK_SIZE;
115+
dctx->rset = true;
116+
}
117+
if (srclen >= POLY1305_BLOCK_SIZE) {
118+
poly1305_setskey(dctx, src);
119+
src += POLY1305_BLOCK_SIZE;
120+
srclen -= POLY1305_BLOCK_SIZE;
121+
dctx->sset = true;
122+
}
123+
}
124+
125+
r0 = dctx->r[0];
126+
r1 = dctx->r[1];
127+
r2 = dctx->r[2];
128+
r3 = dctx->r[3];
129+
r4 = dctx->r[4];
109130

110131
s1 = r1 * 5;
111132
s2 = r2 * 5;
@@ -164,7 +185,6 @@ static int poly1305_update(struct shash_desc *desc,
164185
const u8 *src, unsigned int srclen)
165186
{
166187
struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
167-
struct poly1305_ctx *ctx = crypto_shash_ctx(desc->tfm);
168188
unsigned int bytes;
169189

170190
if (unlikely(dctx->buflen)) {
@@ -175,14 +195,14 @@ static int poly1305_update(struct shash_desc *desc,
175195
dctx->buflen += bytes;
176196

177197
if (dctx->buflen == POLY1305_BLOCK_SIZE) {
178-
poly1305_blocks(dctx, ctx, dctx->buf,
198+
poly1305_blocks(dctx, dctx->buf,
179199
POLY1305_BLOCK_SIZE, 1 << 24);
180200
dctx->buflen = 0;
181201
}
182202
}
183203

184204
if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
185-
bytes = poly1305_blocks(dctx, ctx, src, srclen, 1 << 24);
205+
bytes = poly1305_blocks(dctx, src, srclen, 1 << 24);
186206
src += srclen - bytes;
187207
srclen = bytes;
188208
}
@@ -198,18 +218,20 @@ static int poly1305_update(struct shash_desc *desc,
198218
static int poly1305_final(struct shash_desc *desc, u8 *dst)
199219
{
200220
struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
201-
struct poly1305_ctx *ctx = crypto_shash_ctx(desc->tfm);
202221
__le32 *mac = (__le32 *)dst;
203222
u32 h0, h1, h2, h3, h4;
204223
u32 g0, g1, g2, g3, g4;
205224
u32 mask;
206225
u64 f = 0;
207226

227+
if (unlikely(!dctx->sset))
228+
return -ENOKEY;
229+
208230
if (unlikely(dctx->buflen)) {
209231
dctx->buf[dctx->buflen++] = 1;
210232
memset(dctx->buf + dctx->buflen, 0,
211233
POLY1305_BLOCK_SIZE - dctx->buflen);
212-
poly1305_blocks(dctx, ctx, dctx->buf, POLY1305_BLOCK_SIZE, 0);
234+
poly1305_blocks(dctx, dctx->buf, POLY1305_BLOCK_SIZE, 0);
213235
}
214236

215237
/* fully carry h */
@@ -253,10 +275,10 @@ static int poly1305_final(struct shash_desc *desc, u8 *dst)
253275
h3 = (h3 >> 18) | (h4 << 8);
254276

255277
/* mac = (h + s) % (2^128) */
256-
f = (f >> 32) + h0 + ctx->s[0]; mac[0] = cpu_to_le32(f);
257-
f = (f >> 32) + h1 + ctx->s[1]; mac[1] = cpu_to_le32(f);
258-
f = (f >> 32) + h2 + ctx->s[2]; mac[2] = cpu_to_le32(f);
259-
f = (f >> 32) + h3 + ctx->s[3]; mac[3] = cpu_to_le32(f);
278+
f = (f >> 32) + h0 + dctx->s[0]; mac[0] = cpu_to_le32(f);
279+
f = (f >> 32) + h1 + dctx->s[1]; mac[1] = cpu_to_le32(f);
280+
f = (f >> 32) + h2 + dctx->s[2]; mac[2] = cpu_to_le32(f);
281+
f = (f >> 32) + h3 + dctx->s[3]; mac[3] = cpu_to_le32(f);
260282

261283
return 0;
262284
}
@@ -275,7 +297,6 @@ static struct shash_alg poly1305_alg = {
275297
.cra_flags = CRYPTO_ALG_TYPE_SHASH,
276298
.cra_alignmask = sizeof(u32) - 1,
277299
.cra_blocksize = POLY1305_BLOCK_SIZE,
278-
.cra_ctxsize = sizeof(struct poly1305_ctx),
279300
.cra_module = THIS_MODULE,
280301
},
281302
};

0 commit comments

Comments
 (0)