2019-05-27 09:55:01 +03:00
// SPDX-License-Identifier: GPL-2.0-or-later
2015-06-01 14:44:00 +03:00
/*
* ChaCha20 - Poly1305 AEAD , RFC7539
*
* Copyright ( C ) 2015 Martin Willi
*/
# include <crypto/internal/aead.h>
# include <crypto/internal/hash.h>
# include <crypto/internal/skcipher.h>
# include <crypto/scatterwalk.h>
2018-11-17 04:26:21 +03:00
# include <crypto/chacha.h>
2015-07-16 20:14:05 +03:00
# include <crypto/poly1305.h>
2015-06-01 14:44:00 +03:00
# include <linux/err.h>
# include <linux/init.h>
# include <linux/kernel.h>
# include <linux/module.h>
struct chachapoly_instance_ctx {
struct crypto_skcipher_spawn chacha ;
struct crypto_ahash_spawn poly ;
unsigned int saltlen ;
} ;
struct chachapoly_ctx {
2016-07-12 08:17:40 +03:00
struct crypto_skcipher * chacha ;
2015-06-01 14:44:00 +03:00
struct crypto_ahash * poly ;
/* key bytes we use for the ChaCha20 IV */
unsigned int saltlen ;
u8 salt [ ] ;
} ;
struct poly_req {
/* zero byte padding for AD/ciphertext, as needed */
u8 pad [ POLY1305_BLOCK_SIZE ] ;
/* tail data with AD/ciphertext lengths */
struct {
__le64 assoclen ;
__le64 cryptlen ;
} tail ;
struct scatterlist src [ 1 ] ;
struct ahash_request req ; /* must be last member */
} ;
struct chacha_req {
2018-11-17 04:26:21 +03:00
u8 iv [ CHACHA_IV_SIZE ] ;
2015-06-01 14:44:00 +03:00
struct scatterlist src [ 1 ] ;
2016-07-12 08:17:40 +03:00
struct skcipher_request req ; /* must be last member */
2015-06-01 14:44:00 +03:00
} ;
struct chachapoly_req_ctx {
2015-07-16 07:35:08 +03:00
struct scatterlist src [ 2 ] ;
struct scatterlist dst [ 2 ] ;
2015-06-16 12:34:16 +03:00
/* the key we generate for Poly1305 using Chacha20 */
u8 key [ POLY1305_KEY_SIZE ] ;
2015-06-01 14:44:00 +03:00
/* calculated Poly1305 tag */
u8 tag [ POLY1305_DIGEST_SIZE ] ;
/* length of data to en/decrypt, without ICV */
unsigned int cryptlen ;
2015-07-16 07:35:08 +03:00
/* Actual AD, excluding IV */
unsigned int assoclen ;
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
/* request flags, with MAY_SLEEP cleared if needed */
u32 flags ;
2015-06-01 14:44:00 +03:00
union {
struct poly_req poly ;
struct chacha_req chacha ;
} u ;
} ;
static inline void async_done_continue ( struct aead_request * req , int err ,
int ( * cont ) ( struct aead_request * ) )
{
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
if ( ! err ) {
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
rctx - > flags & = ~ CRYPTO_TFM_REQ_MAY_SLEEP ;
2015-06-01 14:44:00 +03:00
err = cont ( req ) ;
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
}
2015-06-01 14:44:00 +03:00
if ( err ! = - EINPROGRESS & & err ! = - EBUSY )
aead_request_complete ( req , err ) ;
}
static void chacha_iv ( u8 * iv , struct aead_request * req , u32 icb )
{
struct chachapoly_ctx * ctx = crypto_aead_ctx ( crypto_aead_reqtfm ( req ) ) ;
__le32 leicb = cpu_to_le32 ( icb ) ;
memcpy ( iv , & leicb , sizeof ( leicb ) ) ;
memcpy ( iv + sizeof ( leicb ) , ctx - > salt , ctx - > saltlen ) ;
memcpy ( iv + sizeof ( leicb ) + ctx - > saltlen , req - > iv ,
2018-11-17 04:26:21 +03:00
CHACHA_IV_SIZE - sizeof ( leicb ) - ctx - > saltlen ) ;
2015-06-01 14:44:00 +03:00
}
static int poly_verify_tag ( struct aead_request * req )
{
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
u8 tag [ sizeof ( rctx - > tag ) ] ;
2015-07-16 07:35:08 +03:00
scatterwalk_map_and_copy ( tag , req - > src ,
req - > assoclen + rctx - > cryptlen ,
sizeof ( tag ) , 0 ) ;
2015-06-01 14:44:00 +03:00
if ( crypto_memneq ( tag , rctx - > tag , sizeof ( tag ) ) )
return - EBADMSG ;
return 0 ;
}
static int poly_copy_tag ( struct aead_request * req )
{
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
2015-07-16 07:35:08 +03:00
scatterwalk_map_and_copy ( rctx - > tag , req - > dst ,
req - > assoclen + rctx - > cryptlen ,
2015-06-01 14:44:00 +03:00
sizeof ( rctx - > tag ) , 1 ) ;
return 0 ;
}
static void chacha_decrypt_done ( struct crypto_async_request * areq , int err )
{
async_done_continue ( areq - > data , err , poly_verify_tag ) ;
}
static int chacha_decrypt ( struct aead_request * req )
{
struct chachapoly_ctx * ctx = crypto_aead_ctx ( crypto_aead_reqtfm ( req ) ) ;
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
struct chacha_req * creq = & rctx - > u . chacha ;
2015-07-16 07:35:08 +03:00
struct scatterlist * src , * dst ;
2015-06-01 14:44:00 +03:00
int err ;
2015-12-06 04:51:38 +03:00
if ( rctx - > cryptlen = = 0 )
goto skip ;
2015-06-01 14:44:00 +03:00
chacha_iv ( creq - > iv , req , 1 ) ;
2015-07-16 07:35:08 +03:00
src = scatterwalk_ffwd ( rctx - > src , req - > src , req - > assoclen ) ;
dst = src ;
2019-06-03 08:46:34 +03:00
if ( req - > src ! = req - > dst )
2015-07-16 07:35:08 +03:00
dst = scatterwalk_ffwd ( rctx - > dst , req - > dst , req - > assoclen ) ;
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
skcipher_request_set_callback ( & creq - > req , rctx - > flags ,
2016-07-12 08:17:40 +03:00
chacha_decrypt_done , req ) ;
skcipher_request_set_tfm ( & creq - > req , ctx - > chacha ) ;
skcipher_request_set_crypt ( & creq - > req , src , dst ,
rctx - > cryptlen , creq - > iv ) ;
err = crypto_skcipher_decrypt ( & creq - > req ) ;
2015-06-01 14:44:00 +03:00
if ( err )
return err ;
2015-12-06 04:51:38 +03:00
skip :
2015-06-01 14:44:00 +03:00
return poly_verify_tag ( req ) ;
}
static int poly_tail_continue ( struct aead_request * req )
{
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
if ( rctx - > cryptlen = = req - > cryptlen ) /* encrypting */
return poly_copy_tag ( req ) ;
return chacha_decrypt ( req ) ;
}
static void poly_tail_done ( struct crypto_async_request * areq , int err )
{
async_done_continue ( areq - > data , err , poly_tail_continue ) ;
}
static int poly_tail ( struct aead_request * req )
{
2015-07-16 07:35:08 +03:00
struct crypto_aead * tfm = crypto_aead_reqtfm ( req ) ;
struct chachapoly_ctx * ctx = crypto_aead_ctx ( tfm ) ;
2015-06-01 14:44:00 +03:00
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
struct poly_req * preq = & rctx - > u . poly ;
int err ;
2019-06-03 08:46:34 +03:00
preq - > tail . assoclen = cpu_to_le64 ( rctx - > assoclen ) ;
preq - > tail . cryptlen = cpu_to_le64 ( rctx - > cryptlen ) ;
sg_init_one ( preq - > src , & preq - > tail , sizeof ( preq - > tail ) ) ;
2015-06-01 14:44:00 +03:00
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
ahash_request_set_callback ( & preq - > req , rctx - > flags ,
2015-06-01 14:44:00 +03:00
poly_tail_done , req ) ;
ahash_request_set_tfm ( & preq - > req , ctx - > poly ) ;
ahash_request_set_crypt ( & preq - > req , preq - > src ,
rctx - > tag , sizeof ( preq - > tail ) ) ;
err = crypto_ahash_finup ( & preq - > req ) ;
if ( err )
return err ;
return poly_tail_continue ( req ) ;
}
static void poly_cipherpad_done ( struct crypto_async_request * areq , int err )
{
async_done_continue ( areq - > data , err , poly_tail ) ;
}
static int poly_cipherpad ( struct aead_request * req )
{
struct chachapoly_ctx * ctx = crypto_aead_ctx ( crypto_aead_reqtfm ( req ) ) ;
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
struct poly_req * preq = & rctx - > u . poly ;
2019-06-03 08:46:34 +03:00
unsigned int padlen ;
2015-06-01 14:44:00 +03:00
int err ;
2019-06-03 08:46:34 +03:00
padlen = - rctx - > cryptlen % POLY1305_BLOCK_SIZE ;
2015-06-01 14:44:00 +03:00
memset ( preq - > pad , 0 , sizeof ( preq - > pad ) ) ;
2019-06-03 08:46:34 +03:00
sg_init_one ( preq - > src , preq - > pad , padlen ) ;
2015-06-01 14:44:00 +03:00
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
ahash_request_set_callback ( & preq - > req , rctx - > flags ,
2015-06-01 14:44:00 +03:00
poly_cipherpad_done , req ) ;
ahash_request_set_tfm ( & preq - > req , ctx - > poly ) ;
ahash_request_set_crypt ( & preq - > req , preq - > src , NULL , padlen ) ;
err = crypto_ahash_update ( & preq - > req ) ;
if ( err )
return err ;
return poly_tail ( req ) ;
}
static void poly_cipher_done ( struct crypto_async_request * areq , int err )
{
async_done_continue ( areq - > data , err , poly_cipherpad ) ;
}
static int poly_cipher ( struct aead_request * req )
{
struct chachapoly_ctx * ctx = crypto_aead_ctx ( crypto_aead_reqtfm ( req ) ) ;
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
struct poly_req * preq = & rctx - > u . poly ;
struct scatterlist * crypt = req - > src ;
int err ;
if ( rctx - > cryptlen = = req - > cryptlen ) /* encrypting */
crypt = req - > dst ;
2015-07-16 07:35:08 +03:00
crypt = scatterwalk_ffwd ( rctx - > src , crypt , req - > assoclen ) ;
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
ahash_request_set_callback ( & preq - > req , rctx - > flags ,
2015-06-01 14:44:00 +03:00
poly_cipher_done , req ) ;
ahash_request_set_tfm ( & preq - > req , ctx - > poly ) ;
ahash_request_set_crypt ( & preq - > req , crypt , NULL , rctx - > cryptlen ) ;
err = crypto_ahash_update ( & preq - > req ) ;
if ( err )
return err ;
return poly_cipherpad ( req ) ;
}
static void poly_adpad_done ( struct crypto_async_request * areq , int err )
{
async_done_continue ( areq - > data , err , poly_cipher ) ;
}
static int poly_adpad ( struct aead_request * req )
{
struct chachapoly_ctx * ctx = crypto_aead_ctx ( crypto_aead_reqtfm ( req ) ) ;
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
struct poly_req * preq = & rctx - > u . poly ;
2019-06-03 08:46:34 +03:00
unsigned int padlen ;
2015-06-01 14:44:00 +03:00
int err ;
2019-06-03 08:46:34 +03:00
padlen = - rctx - > assoclen % POLY1305_BLOCK_SIZE ;
2015-06-01 14:44:00 +03:00
memset ( preq - > pad , 0 , sizeof ( preq - > pad ) ) ;
2019-06-03 08:46:34 +03:00
sg_init_one ( preq - > src , preq - > pad , padlen ) ;
2015-06-01 14:44:00 +03:00
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
ahash_request_set_callback ( & preq - > req , rctx - > flags ,
2015-06-01 14:44:00 +03:00
poly_adpad_done , req ) ;
ahash_request_set_tfm ( & preq - > req , ctx - > poly ) ;
ahash_request_set_crypt ( & preq - > req , preq - > src , NULL , padlen ) ;
err = crypto_ahash_update ( & preq - > req ) ;
if ( err )
return err ;
return poly_cipher ( req ) ;
}
static void poly_ad_done ( struct crypto_async_request * areq , int err )
{
async_done_continue ( areq - > data , err , poly_adpad ) ;
}
static int poly_ad ( struct aead_request * req )
{
struct chachapoly_ctx * ctx = crypto_aead_ctx ( crypto_aead_reqtfm ( req ) ) ;
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
struct poly_req * preq = & rctx - > u . poly ;
int err ;
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
ahash_request_set_callback ( & preq - > req , rctx - > flags ,
2015-06-01 14:44:00 +03:00
poly_ad_done , req ) ;
ahash_request_set_tfm ( & preq - > req , ctx - > poly ) ;
2015-07-16 07:35:08 +03:00
ahash_request_set_crypt ( & preq - > req , req - > src , NULL , rctx - > assoclen ) ;
2015-06-01 14:44:00 +03:00
err = crypto_ahash_update ( & preq - > req ) ;
if ( err )
return err ;
return poly_adpad ( req ) ;
}
2015-06-16 12:34:16 +03:00
static void poly_setkey_done ( struct crypto_async_request * areq , int err )
2015-06-01 14:44:00 +03:00
{
async_done_continue ( areq - > data , err , poly_ad ) ;
}
2015-06-16 12:34:16 +03:00
static int poly_setkey ( struct aead_request * req )
2015-06-01 14:44:00 +03:00
{
struct chachapoly_ctx * ctx = crypto_aead_ctx ( crypto_aead_reqtfm ( req ) ) ;
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
struct poly_req * preq = & rctx - > u . poly ;
int err ;
2019-06-03 08:46:34 +03:00
sg_init_one ( preq - > src , rctx - > key , sizeof ( rctx - > key ) ) ;
2015-06-16 12:34:16 +03:00
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
ahash_request_set_callback ( & preq - > req , rctx - > flags ,
2015-06-16 12:34:16 +03:00
poly_setkey_done , req ) ;
2015-06-01 14:44:00 +03:00
ahash_request_set_tfm ( & preq - > req , ctx - > poly ) ;
2015-06-16 12:34:16 +03:00
ahash_request_set_crypt ( & preq - > req , preq - > src , NULL , sizeof ( rctx - > key ) ) ;
2015-06-01 14:44:00 +03:00
2015-06-16 12:34:16 +03:00
err = crypto_ahash_update ( & preq - > req ) ;
2015-06-01 14:44:00 +03:00
if ( err )
return err ;
return poly_ad ( req ) ;
}
2015-06-16 12:34:16 +03:00
static void poly_init_done ( struct crypto_async_request * areq , int err )
2015-06-01 14:44:00 +03:00
{
2015-06-16 12:34:16 +03:00
async_done_continue ( areq - > data , err , poly_setkey ) ;
}
static int poly_init ( struct aead_request * req )
{
struct chachapoly_ctx * ctx = crypto_aead_ctx ( crypto_aead_reqtfm ( req ) ) ;
2015-06-01 14:44:00 +03:00
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
2015-06-16 12:34:16 +03:00
struct poly_req * preq = & rctx - > u . poly ;
2015-06-01 14:44:00 +03:00
int err ;
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
ahash_request_set_callback ( & preq - > req , rctx - > flags ,
2015-06-16 12:34:16 +03:00
poly_init_done , req ) ;
ahash_request_set_tfm ( & preq - > req , ctx - > poly ) ;
2015-06-01 14:44:00 +03:00
2015-06-16 12:34:16 +03:00
err = crypto_ahash_init ( & preq - > req ) ;
2015-06-01 14:44:00 +03:00
if ( err )
return err ;
2015-06-16 12:34:16 +03:00
return poly_setkey ( req ) ;
2015-06-01 14:44:00 +03:00
}
static void poly_genkey_done ( struct crypto_async_request * areq , int err )
{
2015-06-16 12:34:16 +03:00
async_done_continue ( areq - > data , err , poly_init ) ;
2015-06-01 14:44:00 +03:00
}
static int poly_genkey ( struct aead_request * req )
{
2015-07-16 07:35:08 +03:00
struct crypto_aead * tfm = crypto_aead_reqtfm ( req ) ;
struct chachapoly_ctx * ctx = crypto_aead_ctx ( tfm ) ;
2015-06-01 14:44:00 +03:00
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
struct chacha_req * creq = & rctx - > u . chacha ;
int err ;
2015-07-16 07:35:08 +03:00
rctx - > assoclen = req - > assoclen ;
if ( crypto_aead_ivsize ( tfm ) = = 8 ) {
if ( rctx - > assoclen < 8 )
return - EINVAL ;
rctx - > assoclen - = 8 ;
}
2015-06-16 12:34:16 +03:00
memset ( rctx - > key , 0 , sizeof ( rctx - > key ) ) ;
2019-06-03 08:46:34 +03:00
sg_init_one ( creq - > src , rctx - > key , sizeof ( rctx - > key ) ) ;
2015-06-01 14:44:00 +03:00
chacha_iv ( creq - > iv , req , 0 ) ;
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
skcipher_request_set_callback ( & creq - > req , rctx - > flags ,
2016-07-12 08:17:40 +03:00
poly_genkey_done , req ) ;
skcipher_request_set_tfm ( & creq - > req , ctx - > chacha ) ;
skcipher_request_set_crypt ( & creq - > req , creq - > src , creq - > src ,
POLY1305_KEY_SIZE , creq - > iv ) ;
2015-06-01 14:44:00 +03:00
2016-07-12 08:17:40 +03:00
err = crypto_skcipher_decrypt ( & creq - > req ) ;
2015-06-01 14:44:00 +03:00
if ( err )
return err ;
2015-06-16 12:34:16 +03:00
return poly_init ( req ) ;
2015-06-01 14:44:00 +03:00
}
static void chacha_encrypt_done ( struct crypto_async_request * areq , int err )
{
async_done_continue ( areq - > data , err , poly_genkey ) ;
}
static int chacha_encrypt ( struct aead_request * req )
{
struct chachapoly_ctx * ctx = crypto_aead_ctx ( crypto_aead_reqtfm ( req ) ) ;
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
struct chacha_req * creq = & rctx - > u . chacha ;
2015-07-16 07:35:08 +03:00
struct scatterlist * src , * dst ;
2015-06-01 14:44:00 +03:00
int err ;
2015-12-06 04:51:38 +03:00
if ( req - > cryptlen = = 0 )
goto skip ;
2015-06-01 14:44:00 +03:00
chacha_iv ( creq - > iv , req , 1 ) ;
2015-07-16 07:35:08 +03:00
src = scatterwalk_ffwd ( rctx - > src , req - > src , req - > assoclen ) ;
dst = src ;
2019-06-03 08:46:34 +03:00
if ( req - > src ! = req - > dst )
2015-07-16 07:35:08 +03:00
dst = scatterwalk_ffwd ( rctx - > dst , req - > dst , req - > assoclen ) ;
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
skcipher_request_set_callback ( & creq - > req , rctx - > flags ,
2016-07-12 08:17:40 +03:00
chacha_encrypt_done , req ) ;
skcipher_request_set_tfm ( & creq - > req , ctx - > chacha ) ;
skcipher_request_set_crypt ( & creq - > req , src , dst ,
req - > cryptlen , creq - > iv ) ;
err = crypto_skcipher_encrypt ( & creq - > req ) ;
2015-06-01 14:44:00 +03:00
if ( err )
return err ;
2015-12-06 04:51:38 +03:00
skip :
2015-06-01 14:44:00 +03:00
return poly_genkey ( req ) ;
}
static int chachapoly_encrypt ( struct aead_request * req )
{
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
rctx - > cryptlen = req - > cryptlen ;
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
rctx - > flags = aead_request_flags ( req ) ;
2015-06-01 14:44:00 +03:00
/* encrypt call chain:
* - chacha_encrypt / done ( )
2015-06-16 12:34:16 +03:00
* - poly_genkey / done ( )
2015-06-01 14:44:00 +03:00
* - poly_init / done ( )
2015-06-16 12:34:16 +03:00
* - poly_setkey / done ( )
2015-06-01 14:44:00 +03:00
* - poly_ad / done ( )
* - poly_adpad / done ( )
* - poly_cipher / done ( )
* - poly_cipherpad / done ( )
* - poly_tail / done / continue ( )
* - poly_copy_tag ( )
*/
return chacha_encrypt ( req ) ;
}
static int chachapoly_decrypt ( struct aead_request * req )
{
struct chachapoly_req_ctx * rctx = aead_request_ctx ( req ) ;
rctx - > cryptlen = req - > cryptlen - POLY1305_DIGEST_SIZE ;
crypto: chacha20poly1305 - fix atomic sleep when using async algorithm
Clear the CRYPTO_TFM_REQ_MAY_SLEEP flag when the chacha20poly1305
operation is being continued from an async completion callback, since
sleeping may not be allowed in that context.
This is basically the same bug that was recently fixed in the xts and
lrw templates. But, it's always been broken in chacha20poly1305 too.
This was found using syzkaller in combination with the updated crypto
self-tests which actually test the MAY_SLEEP flag now.
Reproducer:
python -c 'import socket; socket.socket(socket.AF_ALG, 5, 0).bind(
("aead", "rfc7539(cryptd(chacha20-generic),poly1305-generic)"))'
Kernel output:
BUG: sleeping function called from invalid context at include/crypto/algapi.h:426
in_atomic(): 1, irqs_disabled(): 0, pid: 1001, name: kworker/2:2
[...]
CPU: 2 PID: 1001 Comm: kworker/2:2 Not tainted 5.2.0-rc2 #5
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.12.0-20181126_142135-anatol 04/01/2014
Workqueue: crypto cryptd_queue_worker
Call Trace:
__dump_stack lib/dump_stack.c:77 [inline]
dump_stack+0x4d/0x6a lib/dump_stack.c:113
___might_sleep kernel/sched/core.c:6138 [inline]
___might_sleep.cold.19+0x8e/0x9f kernel/sched/core.c:6095
crypto_yield include/crypto/algapi.h:426 [inline]
crypto_hash_walk_done+0xd6/0x100 crypto/ahash.c:113
shash_ahash_update+0x41/0x60 crypto/shash.c:251
shash_async_update+0xd/0x10 crypto/shash.c:260
crypto_ahash_update include/crypto/hash.h:539 [inline]
poly_setkey+0xf6/0x130 crypto/chacha20poly1305.c:337
poly_init+0x51/0x60 crypto/chacha20poly1305.c:364
async_done_continue crypto/chacha20poly1305.c:78 [inline]
poly_genkey_done+0x15/0x30 crypto/chacha20poly1305.c:369
cryptd_skcipher_complete+0x29/0x70 crypto/cryptd.c:279
cryptd_skcipher_decrypt+0xcd/0x110 crypto/cryptd.c:339
cryptd_queue_worker+0x70/0xa0 crypto/cryptd.c:184
process_one_work+0x1ed/0x420 kernel/workqueue.c:2269
worker_thread+0x3e/0x3a0 kernel/workqueue.c:2415
kthread+0x11f/0x140 kernel/kthread.c:255
ret_from_fork+0x1f/0x30 arch/x86/entry/entry_64.S:352
Fixes: 71ebc4d1b27d ("crypto: chacha20poly1305 - Add a ChaCha20-Poly1305 AEAD construction, RFC7539")
Cc: <stable@vger.kernel.org> # v4.2+
Cc: Martin Willi <martin@strongswan.org>
Signed-off-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2019-05-31 21:12:30 +03:00
rctx - > flags = aead_request_flags ( req ) ;
2015-06-01 14:44:00 +03:00
/* decrypt call chain:
2015-06-16 12:34:16 +03:00
* - poly_genkey / done ( )
2015-06-01 14:44:00 +03:00
* - poly_init / done ( )
2015-06-16 12:34:16 +03:00
* - poly_setkey / done ( )
2015-06-01 14:44:00 +03:00
* - poly_ad / done ( )
* - poly_adpad / done ( )
* - poly_cipher / done ( )
* - poly_cipherpad / done ( )
* - poly_tail / done / continue ( )
* - chacha_decrypt / done ( )
* - poly_verify_tag ( )
*/
return poly_genkey ( req ) ;
}
static int chachapoly_setkey ( struct crypto_aead * aead , const u8 * key ,
unsigned int keylen )
{
struct chachapoly_ctx * ctx = crypto_aead_ctx ( aead ) ;
2018-11-17 04:26:21 +03:00
if ( keylen ! = ctx - > saltlen + CHACHA_KEY_SIZE )
2015-06-01 14:44:00 +03:00
return - EINVAL ;
keylen - = ctx - > saltlen ;
memcpy ( ctx - > salt , key + keylen , ctx - > saltlen ) ;
2016-07-12 08:17:40 +03:00
crypto_skcipher_clear_flags ( ctx - > chacha , CRYPTO_TFM_REQ_MASK ) ;
crypto_skcipher_set_flags ( ctx - > chacha , crypto_aead_get_flags ( aead ) &
CRYPTO_TFM_REQ_MASK ) ;
2019-12-31 06:19:38 +03:00
return crypto_skcipher_setkey ( ctx - > chacha , key , keylen ) ;
2015-06-01 14:44:00 +03:00
}
static int chachapoly_setauthsize ( struct crypto_aead * tfm ,
unsigned int authsize )
{
if ( authsize ! = POLY1305_DIGEST_SIZE )
return - EINVAL ;
return 0 ;
}
2015-07-16 07:35:08 +03:00
static int chachapoly_init ( struct crypto_aead * tfm )
2015-06-01 14:44:00 +03:00
{
2015-07-16 07:35:08 +03:00
struct aead_instance * inst = aead_alg_instance ( tfm ) ;
struct chachapoly_instance_ctx * ictx = aead_instance_ctx ( inst ) ;
struct chachapoly_ctx * ctx = crypto_aead_ctx ( tfm ) ;
2016-07-12 08:17:40 +03:00
struct crypto_skcipher * chacha ;
2015-06-01 14:44:00 +03:00
struct crypto_ahash * poly ;
unsigned long align ;
poly = crypto_spawn_ahash ( & ictx - > poly ) ;
if ( IS_ERR ( poly ) )
return PTR_ERR ( poly ) ;
2016-10-28 19:52:19 +03:00
chacha = crypto_spawn_skcipher ( & ictx - > chacha ) ;
2015-06-01 14:44:00 +03:00
if ( IS_ERR ( chacha ) ) {
crypto_free_ahash ( poly ) ;
return PTR_ERR ( chacha ) ;
}
ctx - > chacha = chacha ;
ctx - > poly = poly ;
ctx - > saltlen = ictx - > saltlen ;
2015-07-16 07:35:08 +03:00
align = crypto_aead_alignmask ( tfm ) ;
2015-06-01 14:44:00 +03:00
align & = ~ ( crypto_tfm_ctx_alignment ( ) - 1 ) ;
2015-07-16 07:35:08 +03:00
crypto_aead_set_reqsize (
tfm ,
align + offsetof ( struct chachapoly_req_ctx , u ) +
max ( offsetof ( struct chacha_req , req ) +
2016-07-12 08:17:40 +03:00
sizeof ( struct skcipher_request ) +
crypto_skcipher_reqsize ( chacha ) ,
2015-07-16 07:35:08 +03:00
offsetof ( struct poly_req , req ) +
sizeof ( struct ahash_request ) +
crypto_ahash_reqsize ( poly ) ) ) ;
2015-06-01 14:44:00 +03:00
return 0 ;
}
2015-07-16 07:35:08 +03:00
static void chachapoly_exit ( struct crypto_aead * tfm )
2015-06-01 14:44:00 +03:00
{
2015-07-16 07:35:08 +03:00
struct chachapoly_ctx * ctx = crypto_aead_ctx ( tfm ) ;
2015-06-01 14:44:00 +03:00
crypto_free_ahash ( ctx - > poly ) ;
2016-07-12 08:17:40 +03:00
crypto_free_skcipher ( ctx - > chacha ) ;
2015-06-01 14:44:00 +03:00
}
2015-07-16 07:35:08 +03:00
static void chachapoly_free ( struct aead_instance * inst )
{
struct chachapoly_instance_ctx * ctx = aead_instance_ctx ( inst ) ;
crypto_drop_skcipher ( & ctx - > chacha ) ;
crypto_drop_ahash ( & ctx - > poly ) ;
kfree ( inst ) ;
}
static int chachapoly_create ( struct crypto_template * tmpl , struct rtattr * * tb ,
const char * name , unsigned int ivsize )
2015-06-01 14:44:00 +03:00
{
2020-01-03 06:58:45 +03:00
u32 mask ;
2015-07-16 07:35:08 +03:00
struct aead_instance * inst ;
2015-06-01 14:44:00 +03:00
struct chachapoly_instance_ctx * ctx ;
2020-01-03 06:58:59 +03:00
struct skcipher_alg * chacha ;
struct hash_alg_common * poly ;
2015-06-01 14:44:00 +03:00
int err ;
if ( ivsize > CHACHAPOLY_IV_SIZE )
2015-07-16 07:35:08 +03:00
return - EINVAL ;
2015-06-01 14:44:00 +03:00
2020-07-10 09:20:38 +03:00
err = crypto_check_attr_type ( tb , CRYPTO_ALG_TYPE_AEAD , & mask ) ;
if ( err )
return err ;
2020-01-03 06:58:45 +03:00
2015-06-01 14:44:00 +03:00
inst = kzalloc ( sizeof ( * inst ) + sizeof ( * ctx ) , GFP_KERNEL ) ;
if ( ! inst )
2020-01-03 06:58:59 +03:00
return - ENOMEM ;
2015-07-16 07:35:08 +03:00
ctx = aead_instance_ctx ( inst ) ;
2015-06-01 14:44:00 +03:00
ctx - > saltlen = CHACHAPOLY_IV_SIZE - ivsize ;
2020-01-03 06:58:45 +03:00
err = crypto_grab_skcipher ( & ctx - > chacha , aead_crypto_instance ( inst ) ,
2020-01-03 06:58:59 +03:00
crypto_attr_alg_name ( tb [ 1 ] ) , 0 , mask ) ;
2015-06-01 14:44:00 +03:00
if ( err )
2020-01-03 06:58:59 +03:00
goto err_free_inst ;
2016-07-12 08:17:40 +03:00
chacha = crypto_spawn_skcipher_alg ( & ctx - > chacha ) ;
2015-06-01 14:44:00 +03:00
2020-01-03 06:58:59 +03:00
err = crypto_grab_ahash ( & ctx - > poly , aead_crypto_instance ( inst ) ,
crypto_attr_alg_name ( tb [ 2 ] ) , 0 , mask ) ;
if ( err )
goto err_free_inst ;
poly = crypto_spawn_ahash_alg ( & ctx - > poly ) ;
2015-06-01 14:44:00 +03:00
err = - EINVAL ;
2020-01-03 06:58:59 +03:00
if ( poly - > digestsize ! = POLY1305_DIGEST_SIZE )
goto err_free_inst ;
2015-06-01 14:44:00 +03:00
/* Need 16-byte IV size, including Initial Block Counter value */
2018-11-17 04:26:21 +03:00
if ( crypto_skcipher_alg_ivsize ( chacha ) ! = CHACHA_IV_SIZE )
2020-01-03 06:58:59 +03:00
goto err_free_inst ;
2015-06-01 14:44:00 +03:00
/* Not a stream cipher? */
2016-07-12 08:17:40 +03:00
if ( chacha - > base . cra_blocksize ! = 1 )
2020-01-03 06:58:59 +03:00
goto err_free_inst ;
2015-06-01 14:44:00 +03:00
err = - ENAMETOOLONG ;
2015-07-16 07:35:08 +03:00
if ( snprintf ( inst - > alg . base . cra_name , CRYPTO_MAX_ALG_NAME ,
2019-03-31 23:04:16 +03:00
" %s(%s,%s) " , name , chacha - > base . cra_name ,
2020-01-03 06:58:59 +03:00
poly - > base . cra_name ) > = CRYPTO_MAX_ALG_NAME )
goto err_free_inst ;
2015-07-16 07:35:08 +03:00
if ( snprintf ( inst - > alg . base . cra_driver_name , CRYPTO_MAX_ALG_NAME ,
2016-07-12 08:17:40 +03:00
" %s(%s,%s) " , name , chacha - > base . cra_driver_name ,
2020-01-03 06:58:59 +03:00
poly - > base . cra_driver_name ) > = CRYPTO_MAX_ALG_NAME )
goto err_free_inst ;
2015-06-01 14:44:00 +03:00
2016-07-12 08:17:40 +03:00
inst - > alg . base . cra_priority = ( chacha - > base . cra_priority +
2020-01-03 06:58:59 +03:00
poly - > base . cra_priority ) / 2 ;
2015-07-16 07:35:08 +03:00
inst - > alg . base . cra_blocksize = 1 ;
2016-07-12 08:17:40 +03:00
inst - > alg . base . cra_alignmask = chacha - > base . cra_alignmask |
2020-01-03 06:58:59 +03:00
poly - > base . cra_alignmask ;
2015-07-16 07:35:08 +03:00
inst - > alg . base . cra_ctxsize = sizeof ( struct chachapoly_ctx ) +
ctx - > saltlen ;
inst - > alg . ivsize = ivsize ;
2016-07-12 08:17:40 +03:00
inst - > alg . chunksize = crypto_skcipher_alg_chunksize ( chacha ) ;
2015-07-16 07:35:08 +03:00
inst - > alg . maxauthsize = POLY1305_DIGEST_SIZE ;
inst - > alg . init = chachapoly_init ;
inst - > alg . exit = chachapoly_exit ;
inst - > alg . encrypt = chachapoly_encrypt ;
inst - > alg . decrypt = chachapoly_decrypt ;
inst - > alg . setkey = chachapoly_setkey ;
inst - > alg . setauthsize = chachapoly_setauthsize ;
inst - > free = chachapoly_free ;
err = aead_register_instance ( tmpl , inst ) ;
2020-01-03 06:58:59 +03:00
if ( err ) {
2015-06-01 14:44:00 +03:00
err_free_inst :
2020-01-03 06:58:59 +03:00
chachapoly_free ( inst ) ;
}
return err ;
2015-06-01 14:44:00 +03:00
}
2015-07-16 07:35:08 +03:00
static int rfc7539_create ( struct crypto_template * tmpl , struct rtattr * * tb )
2015-06-01 14:44:02 +03:00
{
2015-07-16 07:35:08 +03:00
return chachapoly_create ( tmpl , tb , " rfc7539 " , 12 ) ;
2015-06-01 14:44:02 +03:00
}
2015-07-16 07:35:08 +03:00
static int rfc7539esp_create ( struct crypto_template * tmpl , struct rtattr * * tb )
2015-06-01 14:44:00 +03:00
{
2015-07-16 07:35:08 +03:00
return chachapoly_create ( tmpl , tb , " rfc7539esp " , 8 ) ;
2015-06-01 14:44:00 +03:00
}
2019-01-18 08:58:15 +03:00
static struct crypto_template rfc7539_tmpls [ ] = {
{
. name = " rfc7539 " ,
. create = rfc7539_create ,
. module = THIS_MODULE ,
} , {
. name = " rfc7539esp " ,
. create = rfc7539esp_create ,
. module = THIS_MODULE ,
} ,
2015-06-01 14:44:02 +03:00
} ;
2015-06-01 14:44:00 +03:00
static int __init chacha20poly1305_module_init ( void )
{
2019-01-18 08:58:15 +03:00
return crypto_register_templates ( rfc7539_tmpls ,
ARRAY_SIZE ( rfc7539_tmpls ) ) ;
2015-06-01 14:44:00 +03:00
}
static void __exit chacha20poly1305_module_exit ( void )
{
2019-01-18 08:58:15 +03:00
crypto_unregister_templates ( rfc7539_tmpls ,
ARRAY_SIZE ( rfc7539_tmpls ) ) ;
2015-06-01 14:44:00 +03:00
}
2019-04-12 07:57:42 +03:00
subsys_initcall ( chacha20poly1305_module_init ) ;
2015-06-01 14:44:00 +03:00
module_exit ( chacha20poly1305_module_exit ) ;
MODULE_LICENSE ( " GPL " ) ;
MODULE_AUTHOR ( " Martin Willi <martin@strongswan.org> " ) ;
MODULE_DESCRIPTION ( " ChaCha20-Poly1305 AEAD " ) ;
MODULE_ALIAS_CRYPTO ( " rfc7539 " ) ;
2015-06-01 14:44:02 +03:00
MODULE_ALIAS_CRYPTO ( " rfc7539esp " ) ;