linux-crypto.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stephan Mueller <smueller@chronox.de>
To: linux-crypto@vger.kernel.org
Cc: harsh@chelsio.com, jonathan.cameron@huawei.com
Subject: [RFC] AF_ALG AIO and IV
Date: Fri, 12 Jan 2018 14:21:15 +0100	[thread overview]
Message-ID: <2118226.LQArbCsRu5@tauon.chronox.de> (raw)

Hi,

The kernel crypto API requires the caller to set an IV in the request data 
structure. That request data structure shall define one particular cipher 
operation. During the cipher operation, the IV is read by the cipher 
implementation and eventually the potentially updated IV (e.g. in case of CBC) 
is written back to the memory location the request data structure points to.

AF_ALG allows setting the IV with a sendmsg request, where the IV is stored in 
the AF_ALG context that is unique to one particular AF_ALG socket. Note the 
analogy: an AF_ALG socket is like a TFM where one recvmsg operation uses one 
request with the TFM from the socket.

AF_ALG these days supports AIO operations with multiple IOCBs. I.e. with one 
recvmsg call, multiple IOVECs can be specified. Each individual IOCB (derived 
from one IOVEC) implies that one request data structure is created with the 
data to be processed by the cipher implementation. The IV that was set with 
the sendmsg call is registered with the request data structure before the 
cipher operation.

In case of an AIO operation, the cipher operation invocation returns 
immediately, queuing the request to the hardware. While the AIO request is 
processed by the hardware, recvmsg processes the next IOVEC for which another 
request is created. Again, the IV buffer from the AF_ALG socket context is 
registered with the new request and the cipher operation is invoked.

You may now see that there is a potential race condition regarding the IV 
handling, because there is *no* separate IV buffer for the different requests. 
This is nicely demonstrated with libkcapi using the following command which 
creates an AIO request with two IOCBs each encrypting one AES block in CBC 
mode:

kcapi  -d 2 -x 9  -e -c "cbc(aes)" -k 
8d7dd9b0170ce0b5f2f8e1aa768e01e91da8bfc67fd486d081b28254c99eb423 -i 
7fbc02ebf5b93322329df9bfccb635af -p 48981da18e4bb9ef7e2e3162d16b1910

When the first AIO request finishes before the 2nd AIO request is processed, 
the returned value is:

8b19050f66582cb7f7e4b6c873819b7108afa0eaa7de29bac7d903576b674c32

I.e. two blocks where the IV output from the first request is the IV input to 
the 2nd block.

In case the first AIO request is not completed before the 2nd request 
commences, the result is two identical AES blocks (i.e. both use the same IV):

8b19050f66582cb7f7e4b6c873819b718b19050f66582cb7f7e4b6c873819b71

This inconsistent result may even lead to the conclusion that there can be a 
memory corruption in the IV buffer if both AIO requests write to the IV buffer 
at the same time.

This needs to be solved somehow. I see the following options which I would 
like to have vetted by the community.

1. Require that the cipher implementations serialize any AIO requests that 
have dependencies. I.e. for CBC, requests need to be serialized by the driver. 
For, say, ECB or XTS no serialization is necessary.

2. Change AF_ALG to require a per-request IV. This could be implemented by 
moving the IV submission via CMSG from sendmsg to recvmsg. I.e. the recvmsg 
code path would obtain the IV.

I would tend to favor option 2 as this requires code change at only location. 
If option 2 is considered, I would recommend to still allow setting the IV via 
sendmsg CMSG (to keep the interface stable). If, however, the caller provides 
an IV via recvmsg, this takes precedence.

If there are other options, please allow us to learn about them.

Ciao
Stephan

             reply	other threads:[~2018-01-12 13:21 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-12 13:21 Stephan Mueller [this message]
2018-01-15  9:35 ` [PATCH] crypto: AF_ALG - inline IV support Stephan Mueller
2018-01-21 12:14   ` Stephan Müller
2018-01-23 11:02     ` Harsh Jain
2018-01-30  8:27       ` [PATCH] crypto: AF_ALG AIO - lock context IV Stephan Müller
2018-01-30 14:04         ` Stephan Mueller
2018-01-30 15:51         ` Jonathan Cameron
2018-01-31 12:29           ` Jonathan Cameron
2018-02-01  9:35             ` Gilad Ben-Yossef
2018-02-01  9:46               ` Stephan Mueller
2018-02-01 10:06                 ` Gilad Ben-Yossef
2018-02-01 10:15                   ` Stephan Mueller
2018-02-01 10:04               ` Stephan Mueller
2018-02-01 10:07                 ` Gilad Ben-Yossef
2018-02-01 10:25                   ` Jonathan Cameron
2018-02-01 10:55                     ` Harsh Jain
2018-02-07  7:42                     ` [PATCH v2 0/4] crypto: AF_ALG AIO improvements Stephan Müller
2018-02-07  7:43                       ` [PATCH v2 1/4] crypto: AF_ALG AIO - lock context IV Stephan Müller
2018-02-07  7:43                       ` [PATCH v2 2/4] crypto: AF_ALG - inline IV support Stephan Müller
2018-02-07 13:54                         ` Jonathan Cameron
2018-02-07 14:01                           ` Stephan Müller
2018-02-07  7:43                       ` [PATCH v2 3/4] crypto: AF_ALG - allow driver to serialize IV access Stephan Müller
2018-02-07  7:44                       ` [PATCH v2 4/4] crypto: add CRYPTO_TFM_REQ_PARALLEL flag Stephan Müller
2018-02-07 12:48                         ` Stephan Mueller
2018-02-07 15:39                           ` Jonathan Cameron
2018-02-07 15:43                             ` Stephan Mueller
2018-02-07 16:14                               ` Jonathan Cameron
2018-02-07 16:25                                 ` Stephan Mueller
2018-02-07  8:52                       ` [PATCH v2 0/4] crypto: AF_ALG AIO improvements Harsh Jain
2018-02-07 15:37                       ` Jonathan Cameron
2018-02-09 22:02                       ` [PATCH v3 " Stephan Müller
2018-02-09 22:03                         ` [PATCH v3 1/4] crypto: AF_ALG AIO - lock context IV Stephan Müller
2018-02-14  5:43                           ` Harsh Jain
2018-02-14 12:52                             ` Stephan Mueller
2018-02-15  5:30                               ` Harsh Jain
2018-02-15  6:28                                 ` Stephan Mueller
2018-02-15  7:03                                   ` Harsh Jain
2018-02-15  7:17                                     ` Stephan Mueller
2018-02-15 11:38                                       ` Harsh Jain
2018-02-15 11:45                                         ` Stephan Mueller
2018-02-15 12:45                                           ` Harsh Jain
2018-02-15 13:04                                             ` Stephan Mueller
2018-02-15 13:26                                               ` Jeffrey Walton
2018-02-15 18:09                                               ` Stephan Mueller
2018-02-09 22:03                         ` [PATCH v3 2/4] crypto: AF_ALG - inline IV support Stephan Müller
2018-02-09 22:04                         ` [PATCH v3 3/4] crypto: AF_ALG - allow driver to serialize IV access Stephan Müller
2018-02-09 22:04                         ` [PATCH v3 4/4] crypto: add CRYPTO_TFM_REQ_IV_SERIALIZE flag Stephan Müller
2018-02-14  5:50                           ` Harsh Jain
2018-02-14 12:47                             ` Stephan Mueller
2018-01-22 14:11   ` [PATCH] crypto: AF_ALG - inline IV support Jonathan Cameron
2018-01-22 14:30     ` Stephan Mueller
2018-01-22 14:52       ` Jonathan Cameron
2018-01-15  9:39 ` [RFC] AF_ALG AIO and IV Stephan Mueller
2018-01-15 11:05 ` Jonathan Cameron
2018-01-15 12:07   ` Stephan Mueller
2018-01-15 12:59     ` Jonathan Cameron
2018-01-15 13:15       ` Stephan Mueller
2018-01-15 14:25         ` Jonathan Cameron
2018-01-15 14:31           ` Stephan Mueller
2018-01-15 14:42             ` Jonathan Cameron
2018-01-16  6:28               ` Stephan Mueller
2018-01-16 10:51                 ` Jonathan Cameron
2018-01-15 14:37           ` Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2118226.LQArbCsRu5@tauon.chronox.de \
    --to=smueller@chronox.de \
    --cc=harsh@chelsio.com \
    --cc=jonathan.cameron@huawei.com \
    --cc=linux-crypto@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).