2019-05-29 07:17:58 -07:00
// SPDX-License-Identifier: GPL-2.0-only
2014-06-25 19:28:57 +03:00
/*
* Copyright ( c ) 2010 - 2014 , The Linux Foundation . All rights reserved .
*/
# include <linux/device.h>
2020-08-19 21:58:20 +10:00
# include <linux/dma-mapping.h>
2014-06-25 19:28:57 +03:00
# include <linux/interrupt.h>
crypto: qce - use AES fallback for small requests
Process small blocks using the fallback cipher, as a workaround for an
observed failure (DMA-related, apparently) when computing the GCM ghash
key. This brings a speed gain as well, since it avoids the latency of
using the hardware engine to process small blocks.
Using software for all 16-byte requests would be enough to make GCM
work, but to increase performance, a larger threshold would be better.
Measuring the performance of supported ciphers with openssl speed,
software matches hardware at around 768-1024 bytes.
Considering the 256-bit ciphers, software is 2-3 times faster than qce
at 256-bytes, 30% faster at 512, and about even at 768-bytes. With
128-bit keys, the break-even point would be around 1024-bytes.
This adds the 'aes_sw_max_len' parameter, to set the largest request
length processed by the software fallback. Its default is being set to
512 bytes, a little lower than the break-even point, to balance the cost
in CPU usage.
Signed-off-by: Eneas U de Queiroz <cotequeiroz@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2020-02-07 12:02:26 -03:00
# include <linux/moduleparam.h>
2014-06-25 19:28:57 +03:00
# include <linux/types.h>
2021-02-11 15:01:22 -05:00
# include <linux/errno.h>
2014-06-25 19:28:57 +03:00
# include <crypto/aes.h>
2019-08-15 12:01:01 +03:00
# include <crypto/internal/des.h>
2016-06-29 18:04:04 +08:00
# include <crypto/internal/skcipher.h>
2014-06-25 19:28:57 +03:00
# include "cipher.h"
crypto: qce - use AES fallback for small requests
Process small blocks using the fallback cipher, as a workaround for an
observed failure (DMA-related, apparently) when computing the GCM ghash
key. This brings a speed gain as well, since it avoids the latency of
using the hardware engine to process small blocks.
Using software for all 16-byte requests would be enough to make GCM
work, but to increase performance, a larger threshold would be better.
Measuring the performance of supported ciphers with openssl speed,
software matches hardware at around 768-1024 bytes.
Considering the 256-bit ciphers, software is 2-3 times faster than qce
at 256-bytes, 30% faster at 512, and about even at 768-bytes. With
128-bit keys, the break-even point would be around 1024-bytes.
This adds the 'aes_sw_max_len' parameter, to set the largest request
length processed by the software fallback. Its default is being set to
512 bytes, a little lower than the break-even point, to balance the cost
in CPU usage.
Signed-off-by: Eneas U de Queiroz <cotequeiroz@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2020-02-07 12:02:26 -03:00
static unsigned int aes_sw_max_len = CONFIG_CRYPTO_DEV_QCE_SW_MAX_LEN ;
module_param ( aes_sw_max_len , uint , 0644 ) ;
MODULE_PARM_DESC ( aes_sw_max_len ,
" Only use hardware for AES requests larger than this "
" [0=always use hardware; anything <16 breaks AES-GCM; default= "
2020-03-04 15:24:55 -03:00
__stringify ( CONFIG_CRYPTO_DEV_QCE_SW_MAX_LEN ) " ] " ) ;
crypto: qce - use AES fallback for small requests
Process small blocks using the fallback cipher, as a workaround for an
observed failure (DMA-related, apparently) when computing the GCM ghash
key. This brings a speed gain as well, since it avoids the latency of
using the hardware engine to process small blocks.
Using software for all 16-byte requests would be enough to make GCM
work, but to increase performance, a larger threshold would be better.
Measuring the performance of supported ciphers with openssl speed,
software matches hardware at around 768-1024 bytes.
Considering the 256-bit ciphers, software is 2-3 times faster than qce
at 256-bytes, 30% faster at 512, and about even at 768-bytes. With
128-bit keys, the break-even point would be around 1024-bytes.
This adds the 'aes_sw_max_len' parameter, to set the largest request
length processed by the software fallback. Its default is being set to
512 bytes, a little lower than the break-even point, to balance the cost
in CPU usage.
Signed-off-by: Eneas U de Queiroz <cotequeiroz@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2020-02-07 12:02:26 -03:00
2019-11-09 18:09:45 +01:00
static LIST_HEAD ( skcipher_algs ) ;
2014-06-25 19:28:57 +03:00
2019-11-09 18:09:45 +01:00
static void qce_skcipher_done ( void * data )
2014-06-25 19:28:57 +03:00
{
struct crypto_async_request * async_req = data ;
2019-11-09 18:09:45 +01:00
struct skcipher_request * req = skcipher_request_cast ( async_req ) ;
struct qce_cipher_reqctx * rctx = skcipher_request_ctx ( req ) ;
struct qce_alg_template * tmpl = to_cipher_tmpl ( crypto_skcipher_reqtfm ( req ) ) ;
2014-06-25 19:28:57 +03:00
struct qce_device * qce = tmpl - > qce ;
2019-12-20 16:02:16 -03:00
struct qce_result_dump * result_buf = qce - > dma . result_buf ;
2014-06-25 19:28:57 +03:00
enum dma_data_direction dir_src , dir_dst ;
u32 status ;
int error ;
bool diff_dst ;
diff_dst = ( req - > src ! = req - > dst ) ? true : false ;
dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL ;
dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL ;
error = qce_dma_terminate_all ( & qce - > dma ) ;
if ( error )
2019-11-09 18:09:45 +01:00
dev_dbg ( qce - > dev , " skcipher dma termination error (%d) \n " ,
2014-06-25 19:28:57 +03:00
error ) ;
if ( diff_dst )
2015-10-02 08:01:02 +02:00
dma_unmap_sg ( qce - > dev , rctx - > src_sg , rctx - > src_nents , dir_src ) ;
dma_unmap_sg ( qce - > dev , rctx - > dst_sg , rctx - > dst_nents , dir_dst ) ;
2014-06-25 19:28:57 +03:00
sg_free_table ( & rctx - > dst_tbl ) ;
error = qce_check_status ( qce , & status ) ;
if ( error < 0 )
2019-11-09 18:09:45 +01:00
dev_dbg ( qce - > dev , " skcipher operation error (%x) \n " , status ) ;
2014-06-25 19:28:57 +03:00
2019-12-20 16:02:16 -03:00
memcpy ( rctx - > iv , result_buf - > encr_cntr_iv , rctx - > ivsize ) ;
2014-06-25 19:28:57 +03:00
qce - > async_req_done ( tmpl - > qce , error ) ;
}
static int
2019-11-09 18:09:45 +01:00
qce_skcipher_async_req_handle ( struct crypto_async_request * async_req )
2014-06-25 19:28:57 +03:00
{
2019-11-09 18:09:45 +01:00
struct skcipher_request * req = skcipher_request_cast ( async_req ) ;
struct qce_cipher_reqctx * rctx = skcipher_request_ctx ( req ) ;
struct crypto_skcipher * skcipher = crypto_skcipher_reqtfm ( req ) ;
struct qce_alg_template * tmpl = to_cipher_tmpl ( crypto_skcipher_reqtfm ( req ) ) ;
2014-06-25 19:28:57 +03:00
struct qce_device * qce = tmpl - > qce ;
enum dma_data_direction dir_src , dir_dst ;
struct scatterlist * sg ;
bool diff_dst ;
gfp_t gfp ;
2021-05-20 22:20:23 -04:00
int dst_nents , src_nents , ret ;
2014-06-25 19:28:57 +03:00
2019-11-09 18:09:45 +01:00
rctx - > iv = req - > iv ;
rctx - > ivsize = crypto_skcipher_ivsize ( skcipher ) ;
rctx - > cryptlen = req - > cryptlen ;
2014-06-25 19:28:57 +03:00
diff_dst = ( req - > src ! = req - > dst ) ? true : false ;
dir_src = diff_dst ? DMA_TO_DEVICE : DMA_BIDIRECTIONAL ;
dir_dst = diff_dst ? DMA_FROM_DEVICE : DMA_BIDIRECTIONAL ;
2019-11-09 18:09:45 +01:00
rctx - > src_nents = sg_nents_for_len ( req - > src , req - > cryptlen ) ;
2015-10-02 08:01:02 +02:00
if ( diff_dst )
2019-11-09 18:09:45 +01:00
rctx - > dst_nents = sg_nents_for_len ( req - > dst , req - > cryptlen ) ;
2015-10-02 08:01:02 +02:00
else
2014-06-25 19:28:57 +03:00
rctx - > dst_nents = rctx - > src_nents ;
2015-11-04 21:13:36 +01:00
if ( rctx - > src_nents < 0 ) {
dev_err ( qce - > dev , " Invalid numbers of src SG. \n " ) ;
return rctx - > src_nents ;
}
if ( rctx - > dst_nents < 0 ) {
dev_err ( qce - > dev , " Invalid numbers of dst SG. \n " ) ;
return - rctx - > dst_nents ;
}
2014-06-25 19:28:57 +03:00
rctx - > dst_nents + = 1 ;
gfp = ( req - > base . flags & CRYPTO_TFM_REQ_MAY_SLEEP ) ?
GFP_KERNEL : GFP_ATOMIC ;
ret = sg_alloc_table ( & rctx - > dst_tbl , rctx - > dst_nents , gfp ) ;
if ( ret )
return ret ;
sg_init_one ( & rctx - > result_sg , qce - > dma . result_buf , QCE_RESULT_BUF_SZ ) ;
2020-02-07 12:02:25 -03:00
sg = qce_sgtable_add ( & rctx - > dst_tbl , req - > dst , req - > cryptlen ) ;
2014-06-25 19:28:57 +03:00
if ( IS_ERR ( sg ) ) {
ret = PTR_ERR ( sg ) ;
goto error_free ;
}
2020-02-07 12:02:25 -03:00
sg = qce_sgtable_add ( & rctx - > dst_tbl , & rctx - > result_sg ,
QCE_RESULT_BUF_SZ ) ;
2014-06-25 19:28:57 +03:00
if ( IS_ERR ( sg ) ) {
ret = PTR_ERR ( sg ) ;
goto error_free ;
}
sg_mark_end ( sg ) ;
rctx - > dst_sg = rctx - > dst_tbl . sgl ;
2021-05-20 22:20:23 -04:00
dst_nents = dma_map_sg ( qce - > dev , rctx - > dst_sg , rctx - > dst_nents , dir_dst ) ;
2022-08-19 08:07:51 +02:00
if ( ! dst_nents ) {
ret = - EIO ;
2014-06-25 19:28:57 +03:00
goto error_free ;
2021-06-02 11:36:45 +00:00
}
2014-06-25 19:28:57 +03:00
if ( diff_dst ) {
2021-05-20 22:20:23 -04:00
src_nents = dma_map_sg ( qce - > dev , req - > src , rctx - > src_nents , dir_src ) ;
2022-08-19 08:07:51 +02:00
if ( ! src_nents ) {
ret = - EIO ;
2014-06-25 19:28:57 +03:00
goto error_unmap_dst ;
2021-06-02 11:36:45 +00:00
}
2014-06-25 19:28:57 +03:00
rctx - > src_sg = req - > src ;
} else {
rctx - > src_sg = rctx - > dst_sg ;
2021-05-20 22:20:23 -04:00
src_nents = dst_nents - 1 ;
2014-06-25 19:28:57 +03:00
}
2021-05-20 22:20:23 -04:00
ret = qce_dma_prep_sgs ( & qce - > dma , rctx - > src_sg , src_nents ,
rctx - > dst_sg , dst_nents ,
2019-11-09 18:09:45 +01:00
qce_skcipher_done , async_req ) ;
2014-06-25 19:28:57 +03:00
if ( ret )
goto error_unmap_src ;
qce_dma_issue_pending ( & qce - > dma ) ;
2021-02-11 15:01:28 -05:00
ret = qce_start ( async_req , tmpl - > crypto_alg_type ) ;
2014-06-25 19:28:57 +03:00
if ( ret )
goto error_terminate ;
return 0 ;
error_terminate :
qce_dma_terminate_all ( & qce - > dma ) ;
error_unmap_src :
if ( diff_dst )
2015-10-02 08:01:02 +02:00
dma_unmap_sg ( qce - > dev , req - > src , rctx - > src_nents , dir_src ) ;
2014-06-25 19:28:57 +03:00
error_unmap_dst :
2015-10-02 08:01:02 +02:00
dma_unmap_sg ( qce - > dev , rctx - > dst_sg , rctx - > dst_nents , dir_dst ) ;
2014-06-25 19:28:57 +03:00
error_free :
sg_free_table ( & rctx - > dst_tbl ) ;
return ret ;
}
2019-11-09 18:09:45 +01:00
static int qce_skcipher_setkey ( struct crypto_skcipher * ablk , const u8 * key ,
2014-06-25 19:28:57 +03:00
unsigned int keylen )
{
2019-11-09 18:09:45 +01:00
struct crypto_tfm * tfm = crypto_skcipher_tfm ( ablk ) ;
2014-06-25 19:28:57 +03:00
struct qce_cipher_ctx * ctx = crypto_tfm_ctx ( tfm ) ;
2019-12-20 16:02:14 -03:00
unsigned long flags = to_cipher_tmpl ( ablk ) - > alg_flags ;
2021-02-11 15:01:20 -05:00
unsigned int __keylen ;
2014-06-25 19:28:57 +03:00
int ret ;
if ( ! key | | ! keylen )
return - EINVAL ;
2021-02-11 15:01:20 -05:00
/*
* AES XTS key1 = key2 not supported by crypto engine .
* Revisit to request a fallback cipher in this case .
*/
if ( IS_XTS ( flags ) ) {
__keylen = keylen > > 1 ;
if ( ! memcmp ( key , key + __keylen , __keylen ) )
return - ENOKEY ;
} else {
__keylen = keylen ;
}
switch ( __keylen ) {
2019-08-15 12:01:01 +03:00
case AES_KEYSIZE_128 :
case AES_KEYSIZE_256 :
crypto: qce - use AES fallback for small requests
Process small blocks using the fallback cipher, as a workaround for an
observed failure (DMA-related, apparently) when computing the GCM ghash
key. This brings a speed gain as well, since it avoids the latency of
using the hardware engine to process small blocks.
Using software for all 16-byte requests would be enough to make GCM
work, but to increase performance, a larger threshold would be better.
Measuring the performance of supported ciphers with openssl speed,
software matches hardware at around 768-1024 bytes.
Considering the 256-bit ciphers, software is 2-3 times faster than qce
at 256-bytes, 30% faster at 512, and about even at 768-bytes. With
128-bit keys, the break-even point would be around 1024-bytes.
This adds the 'aes_sw_max_len' parameter, to set the largest request
length processed by the software fallback. Its default is being set to
512 bytes, a little lower than the break-even point, to balance the cost
in CPU usage.
Signed-off-by: Eneas U de Queiroz <cotequeiroz@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2020-02-07 12:02:26 -03:00
memcpy ( ctx - > enc_key , key , keylen ) ;
2019-08-15 12:01:01 +03:00
break ;
2021-02-11 15:01:20 -05:00
case AES_KEYSIZE_192 :
break ;
default :
return - EINVAL ;
2014-06-25 19:28:57 +03:00
}
crypto: qce - permit asynchronous skcipher as fallback
Even though the qce driver implements asynchronous versions of ecb(aes),
cbc(aes)and xts(aes), the fallbacks it allocates are required to be
synchronous. Given that SIMD based software implementations are usually
asynchronous as well, even though they rarely complete asynchronously
(this typically only happens in cases where the request was made from
softirq context, while SIMD was already in use in the task context that
it interrupted), these implementations are disregarded, and either the
generic C version or another table based version implemented in assembler
is selected instead.
Since falling back to synchronous AES is not only a performance issue, but
potentially a security issue as well (due to the fact that table based AES
is not time invariant), let's fix this, by allocating an ordinary skcipher
as the fallback, and invoke it with the completion routine that was given
to the outer request.
While at it, remove the pointless memset() from qce_skcipher_init(), and
remove the call to it qce_skcipher_init_fallback().
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2020-07-07 09:32:01 +03:00
ret = crypto_skcipher_setkey ( ctx - > fallback , key , keylen ) ;
2014-06-25 19:28:57 +03:00
if ( ! ret )
ctx - > enc_keylen = keylen ;
return ret ;
2019-08-15 12:01:01 +03:00
}
2019-11-09 18:09:45 +01:00
static int qce_des_setkey ( struct crypto_skcipher * ablk , const u8 * key ,
2019-08-15 12:01:01 +03:00
unsigned int keylen )
{
2019-11-09 18:09:45 +01:00
struct qce_cipher_ctx * ctx = crypto_skcipher_ctx ( ablk ) ;
2019-08-15 12:01:01 +03:00
int err ;
2019-11-09 18:09:45 +01:00
err = verify_skcipher_des_key ( ablk , key ) ;
2019-08-15 12:01:01 +03:00
if ( err )
return err ;
ctx - > enc_keylen = keylen ;
memcpy ( ctx - > enc_key , key , keylen ) ;
return 0 ;
2014-06-25 19:28:57 +03:00
}
2019-11-09 18:09:45 +01:00
static int qce_des3_setkey ( struct crypto_skcipher * ablk , const u8 * key ,
2019-04-11 16:51:16 +08:00
unsigned int keylen )
{
2019-11-09 18:09:45 +01:00
struct qce_cipher_ctx * ctx = crypto_skcipher_ctx ( ablk ) ;
2021-02-11 15:01:21 -05:00
u32 _key [ 6 ] ;
2019-04-11 16:51:16 +08:00
int err ;
2019-11-09 18:09:45 +01:00
err = verify_skcipher_des3_key ( ablk , key ) ;
2019-08-15 12:01:01 +03:00
if ( err )
2019-04-11 16:51:16 +08:00
return err ;
2021-02-11 15:01:21 -05:00
/*
* The crypto engine does not support any two keys
* being the same for triple des algorithms . The
* verify_skcipher_des3_key does not check for all the
* below conditions . Return - ENOKEY in case any two keys
* are the same . Revisit to see if a fallback cipher
* is needed to handle this condition .
*/
memcpy ( _key , key , DES3_EDE_KEY_SIZE ) ;
if ( ! ( ( _key [ 0 ] ^ _key [ 2 ] ) | ( _key [ 1 ] ^ _key [ 3 ] ) ) | |
! ( ( _key [ 2 ] ^ _key [ 4 ] ) | ( _key [ 3 ] ^ _key [ 5 ] ) ) | |
! ( ( _key [ 0 ] ^ _key [ 4 ] ) | ( _key [ 1 ] ^ _key [ 5 ] ) ) )
return - ENOKEY ;
2019-04-11 16:51:16 +08:00
ctx - > enc_keylen = keylen ;
memcpy ( ctx - > enc_key , key , keylen ) ;
return 0 ;
}
2019-11-09 18:09:45 +01:00
static int qce_skcipher_crypt ( struct skcipher_request * req , int encrypt )
2014-06-25 19:28:57 +03:00
{
2019-11-09 18:09:45 +01:00
struct crypto_skcipher * tfm = crypto_skcipher_reqtfm ( req ) ;
struct qce_cipher_ctx * ctx = crypto_skcipher_ctx ( tfm ) ;
struct qce_cipher_reqctx * rctx = skcipher_request_ctx ( req ) ;
2014-06-25 19:28:57 +03:00
struct qce_alg_template * tmpl = to_cipher_tmpl ( tfm ) ;
2021-02-11 15:01:23 -05:00
unsigned int blocksize = crypto_skcipher_blocksize ( tfm ) ;
2019-12-20 16:02:14 -03:00
int keylen ;
2014-06-25 19:28:57 +03:00
int ret ;
rctx - > flags = tmpl - > alg_flags ;
rctx - > flags | = encrypt ? QCE_ENCRYPT : QCE_DECRYPT ;
2019-12-20 16:02:14 -03:00
keylen = IS_XTS ( rctx - > flags ) ? ctx - > enc_keylen > > 1 : ctx - > enc_keylen ;
2014-06-25 19:28:57 +03:00
2021-02-11 15:01:22 -05:00
/* CE does not handle 0 length messages */
if ( ! req - > cryptlen )
return 0 ;
2021-02-11 15:01:23 -05:00
/*
* ECB and CBC algorithms require message lengths to be
* multiples of block size .
*/
if ( IS_ECB ( rctx - > flags ) | | IS_CBC ( rctx - > flags ) )
if ( ! IS_ALIGNED ( req - > cryptlen , blocksize ) )
return - EINVAL ;
2021-02-11 15:01:25 -05:00
/*
* Conditions for requesting a fallback cipher
* AES - 192 ( not supported by crypto engine ( CE ) )
* AES - XTS request with len < = 512 byte ( not recommended to use CE )
* AES - XTS request with len > QCE_SECTOR_SIZE and
* is not a multiple of it . ( Revisit this condition to check if it is
* needed in all versions of CE )
2020-02-07 12:02:27 -03:00
*/
crypto: qce - use AES fallback for small requests
Process small blocks using the fallback cipher, as a workaround for an
observed failure (DMA-related, apparently) when computing the GCM ghash
key. This brings a speed gain as well, since it avoids the latency of
using the hardware engine to process small blocks.
Using software for all 16-byte requests would be enough to make GCM
work, but to increase performance, a larger threshold would be better.
Measuring the performance of supported ciphers with openssl speed,
software matches hardware at around 768-1024 bytes.
Considering the 256-bit ciphers, software is 2-3 times faster than qce
at 256-bytes, 30% faster at 512, and about even at 768-bytes. With
128-bit keys, the break-even point would be around 1024-bytes.
This adds the 'aes_sw_max_len' parameter, to set the largest request
length processed by the software fallback. Its default is being set to
512 bytes, a little lower than the break-even point, to balance the cost
in CPU usage.
Signed-off-by: Eneas U de Queiroz <cotequeiroz@gmail.com>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2020-02-07 12:02:26 -03:00
if ( IS_AES ( rctx - > flags ) & &
2021-02-11 15:01:25 -05:00
( ( keylen ! = AES_KEYSIZE_128 & & keylen ! = AES_KEYSIZE_256 ) | |
( IS_XTS ( rctx - > flags ) & & ( ( req - > cryptlen < = aes_sw_max_len ) | |
( req - > cryptlen > QCE_SECTOR_SIZE & &
req - > cryptlen % QCE_SECTOR_SIZE ) ) ) ) ) {
crypto: qce - permit asynchronous skcipher as fallback
Even though the qce driver implements asynchronous versions of ecb(aes),
cbc(aes)and xts(aes), the fallbacks it allocates are required to be
synchronous. Given that SIMD based software implementations are usually
asynchronous as well, even though they rarely complete asynchronously
(this typically only happens in cases where the request was made from
softirq context, while SIMD was already in use in the task context that
it interrupted), these implementations are disregarded, and either the
generic C version or another table based version implemented in assembler
is selected instead.
Since falling back to synchronous AES is not only a performance issue, but
potentially a security issue as well (due to the fact that table based AES
is not time invariant), let's fix this, by allocating an ordinary skcipher
as the fallback, and invoke it with the completion routine that was given
to the outer request.
While at it, remove the pointless memset() from qce_skcipher_init(), and
remove the call to it qce_skcipher_init_fallback().
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2020-07-07 09:32:01 +03:00
skcipher_request_set_tfm ( & rctx - > fallback_req , ctx - > fallback ) ;
skcipher_request_set_callback ( & rctx - > fallback_req ,
req - > base . flags ,
req - > base . complete ,
req - > base . data ) ;
skcipher_request_set_crypt ( & rctx - > fallback_req , req - > src ,
req - > dst , req - > cryptlen , req - > iv ) ;
ret = encrypt ? crypto_skcipher_encrypt ( & rctx - > fallback_req ) :
crypto_skcipher_decrypt ( & rctx - > fallback_req ) ;
2014-06-25 19:28:57 +03:00
return ret ;
}
return tmpl - > qce - > async_req_enqueue ( tmpl - > qce , & req - > base ) ;
}
2019-11-09 18:09:45 +01:00
static int qce_skcipher_encrypt ( struct skcipher_request * req )
2014-06-25 19:28:57 +03:00
{
2019-11-09 18:09:45 +01:00
return qce_skcipher_crypt ( req , 1 ) ;
2014-06-25 19:28:57 +03:00
}
2019-11-09 18:09:45 +01:00
static int qce_skcipher_decrypt ( struct skcipher_request * req )
2014-06-25 19:28:57 +03:00
{
2019-11-09 18:09:45 +01:00
return qce_skcipher_crypt ( req , 0 ) ;
2014-06-25 19:28:57 +03:00
}
2019-11-09 18:09:45 +01:00
static int qce_skcipher_init ( struct crypto_skcipher * tfm )
2014-06-25 19:28:57 +03:00
{
crypto: qce - permit asynchronous skcipher as fallback
Even though the qce driver implements asynchronous versions of ecb(aes),
cbc(aes)and xts(aes), the fallbacks it allocates are required to be
synchronous. Given that SIMD based software implementations are usually
asynchronous as well, even though they rarely complete asynchronously
(this typically only happens in cases where the request was made from
softirq context, while SIMD was already in use in the task context that
it interrupted), these implementations are disregarded, and either the
generic C version or another table based version implemented in assembler
is selected instead.
Since falling back to synchronous AES is not only a performance issue, but
potentially a security issue as well (due to the fact that table based AES
is not time invariant), let's fix this, by allocating an ordinary skcipher
as the fallback, and invoke it with the completion routine that was given
to the outer request.
While at it, remove the pointless memset() from qce_skcipher_init(), and
remove the call to it qce_skcipher_init_fallback().
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2020-07-07 09:32:01 +03:00
/* take the size without the fallback skcipher_request at the end */
crypto_skcipher_set_reqsize ( tfm , offsetof ( struct qce_cipher_reqctx ,
fallback_req ) ) ;
2019-12-20 16:02:17 -03:00
return 0 ;
}
2014-06-25 19:28:57 +03:00
2019-12-20 16:02:17 -03:00
static int qce_skcipher_init_fallback ( struct crypto_skcipher * tfm )
{
struct qce_cipher_ctx * ctx = crypto_skcipher_ctx ( tfm ) ;
crypto: qce - permit asynchronous skcipher as fallback
Even though the qce driver implements asynchronous versions of ecb(aes),
cbc(aes)and xts(aes), the fallbacks it allocates are required to be
synchronous. Given that SIMD based software implementations are usually
asynchronous as well, even though they rarely complete asynchronously
(this typically only happens in cases where the request was made from
softirq context, while SIMD was already in use in the task context that
it interrupted), these implementations are disregarded, and either the
generic C version or another table based version implemented in assembler
is selected instead.
Since falling back to synchronous AES is not only a performance issue, but
potentially a security issue as well (due to the fact that table based AES
is not time invariant), let's fix this, by allocating an ordinary skcipher
as the fallback, and invoke it with the completion routine that was given
to the outer request.
While at it, remove the pointless memset() from qce_skcipher_init(), and
remove the call to it qce_skcipher_init_fallback().
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2020-07-07 09:32:01 +03:00
ctx - > fallback = crypto_alloc_skcipher ( crypto_tfm_alg_name ( & tfm - > base ) ,
0 , CRYPTO_ALG_NEED_FALLBACK ) ;
if ( IS_ERR ( ctx - > fallback ) )
return PTR_ERR ( ctx - > fallback ) ;
crypto_skcipher_set_reqsize ( tfm , sizeof ( struct qce_cipher_reqctx ) +
crypto_skcipher_reqsize ( ctx - > fallback ) ) ;
return 0 ;
2014-06-25 19:28:57 +03:00
}
2019-11-09 18:09:45 +01:00
static void qce_skcipher_exit ( struct crypto_skcipher * tfm )
2014-06-25 19:28:57 +03:00
{
2019-11-09 18:09:45 +01:00
struct qce_cipher_ctx * ctx = crypto_skcipher_ctx ( tfm ) ;
2014-06-25 19:28:57 +03:00
crypto: qce - permit asynchronous skcipher as fallback
Even though the qce driver implements asynchronous versions of ecb(aes),
cbc(aes)and xts(aes), the fallbacks it allocates are required to be
synchronous. Given that SIMD based software implementations are usually
asynchronous as well, even though they rarely complete asynchronously
(this typically only happens in cases where the request was made from
softirq context, while SIMD was already in use in the task context that
it interrupted), these implementations are disregarded, and either the
generic C version or another table based version implemented in assembler
is selected instead.
Since falling back to synchronous AES is not only a performance issue, but
potentially a security issue as well (due to the fact that table based AES
is not time invariant), let's fix this, by allocating an ordinary skcipher
as the fallback, and invoke it with the completion routine that was given
to the outer request.
While at it, remove the pointless memset() from qce_skcipher_init(), and
remove the call to it qce_skcipher_init_fallback().
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2020-07-07 09:32:01 +03:00
crypto_free_skcipher ( ctx - > fallback ) ;
2014-06-25 19:28:57 +03:00
}
2019-11-09 18:09:45 +01:00
struct qce_skcipher_def {
2014-06-25 19:28:57 +03:00
unsigned long flags ;
const char * name ;
const char * drv_name ;
unsigned int blocksize ;
2019-12-20 16:02:13 -03:00
unsigned int chunksize ;
2014-06-25 19:28:57 +03:00
unsigned int ivsize ;
unsigned int min_keysize ;
unsigned int max_keysize ;
} ;
2019-11-09 18:09:45 +01:00
static const struct qce_skcipher_def skcipher_def [ ] = {
2014-06-25 19:28:57 +03:00
{
. flags = QCE_ALG_AES | QCE_MODE_ECB ,
. name = " ecb(aes) " ,
. drv_name = " ecb-aes-qce " ,
. blocksize = AES_BLOCK_SIZE ,
2021-02-11 15:01:24 -05:00
. ivsize = 0 ,
2014-06-25 19:28:57 +03:00
. min_keysize = AES_MIN_KEY_SIZE ,
. max_keysize = AES_MAX_KEY_SIZE ,
} ,
{
. flags = QCE_ALG_AES | QCE_MODE_CBC ,
. name = " cbc(aes) " ,
. drv_name = " cbc-aes-qce " ,
. blocksize = AES_BLOCK_SIZE ,
. ivsize = AES_BLOCK_SIZE ,
. min_keysize = AES_MIN_KEY_SIZE ,
. max_keysize = AES_MAX_KEY_SIZE ,
} ,
{
. flags = QCE_ALG_AES | QCE_MODE_CTR ,
. name = " ctr(aes) " ,
. drv_name = " ctr-aes-qce " ,
2019-12-20 16:02:13 -03:00
. blocksize = 1 ,
. chunksize = AES_BLOCK_SIZE ,
2014-06-25 19:28:57 +03:00
. ivsize = AES_BLOCK_SIZE ,
. min_keysize = AES_MIN_KEY_SIZE ,
. max_keysize = AES_MAX_KEY_SIZE ,
} ,
{
. flags = QCE_ALG_AES | QCE_MODE_XTS ,
. name = " xts(aes) " ,
. drv_name = " xts-aes-qce " ,
. blocksize = AES_BLOCK_SIZE ,
. ivsize = AES_BLOCK_SIZE ,
2019-12-20 16:02:14 -03:00
. min_keysize = AES_MIN_KEY_SIZE * 2 ,
. max_keysize = AES_MAX_KEY_SIZE * 2 ,
2014-06-25 19:28:57 +03:00
} ,
{
. flags = QCE_ALG_DES | QCE_MODE_ECB ,
. name = " ecb(des) " ,
. drv_name = " ecb-des-qce " ,
. blocksize = DES_BLOCK_SIZE ,
. ivsize = 0 ,
. min_keysize = DES_KEY_SIZE ,
. max_keysize = DES_KEY_SIZE ,
} ,
{
. flags = QCE_ALG_DES | QCE_MODE_CBC ,
. name = " cbc(des) " ,
. drv_name = " cbc-des-qce " ,
. blocksize = DES_BLOCK_SIZE ,
. ivsize = DES_BLOCK_SIZE ,
. min_keysize = DES_KEY_SIZE ,
. max_keysize = DES_KEY_SIZE ,
} ,
{
. flags = QCE_ALG_3DES | QCE_MODE_ECB ,
. name = " ecb(des3_ede) " ,
. drv_name = " ecb-3des-qce " ,
. blocksize = DES3_EDE_BLOCK_SIZE ,
. ivsize = 0 ,
. min_keysize = DES3_EDE_KEY_SIZE ,
. max_keysize = DES3_EDE_KEY_SIZE ,
} ,
{
. flags = QCE_ALG_3DES | QCE_MODE_CBC ,
. name = " cbc(des3_ede) " ,
. drv_name = " cbc-3des-qce " ,
. blocksize = DES3_EDE_BLOCK_SIZE ,
. ivsize = DES3_EDE_BLOCK_SIZE ,
. min_keysize = DES3_EDE_KEY_SIZE ,
. max_keysize = DES3_EDE_KEY_SIZE ,
} ,
} ;
2019-11-09 18:09:45 +01:00
static int qce_skcipher_register_one ( const struct qce_skcipher_def * def ,
2014-06-25 19:28:57 +03:00
struct qce_device * qce )
{
struct qce_alg_template * tmpl ;
2019-11-09 18:09:45 +01:00
struct skcipher_alg * alg ;
2014-06-25 19:28:57 +03:00
int ret ;
tmpl = kzalloc ( sizeof ( * tmpl ) , GFP_KERNEL ) ;
if ( ! tmpl )
return - ENOMEM ;
2019-11-09 18:09:45 +01:00
alg = & tmpl - > alg . skcipher ;
2014-06-25 19:28:57 +03:00
2019-11-09 18:09:45 +01:00
snprintf ( alg - > base . cra_name , CRYPTO_MAX_ALG_NAME , " %s " , def - > name ) ;
snprintf ( alg - > base . cra_driver_name , CRYPTO_MAX_ALG_NAME , " %s " ,
2014-06-25 19:28:57 +03:00
def - > drv_name ) ;
2019-11-09 18:09:45 +01:00
alg - > base . cra_blocksize = def - > blocksize ;
2019-12-20 16:02:13 -03:00
alg - > chunksize = def - > chunksize ;
2019-11-09 18:09:45 +01:00
alg - > ivsize = def - > ivsize ;
alg - > min_keysize = def - > min_keysize ;
alg - > max_keysize = def - > max_keysize ;
alg - > setkey = IS_3DES ( def - > flags ) ? qce_des3_setkey :
IS_DES ( def - > flags ) ? qce_des_setkey :
qce_skcipher_setkey ;
alg - > encrypt = qce_skcipher_encrypt ;
alg - > decrypt = qce_skcipher_decrypt ;
alg - > base . cra_priority = 300 ;
alg - > base . cra_flags = CRYPTO_ALG_ASYNC |
2020-07-09 23:20:41 -07:00
CRYPTO_ALG_ALLOCATES_MEMORY |
2019-11-09 18:09:45 +01:00
CRYPTO_ALG_KERN_DRIVER_ONLY ;
alg - > base . cra_ctxsize = sizeof ( struct qce_cipher_ctx ) ;
alg - > base . cra_alignmask = 0 ;
alg - > base . cra_module = THIS_MODULE ;
2019-12-20 16:02:17 -03:00
if ( IS_AES ( def - > flags ) ) {
alg - > base . cra_flags | = CRYPTO_ALG_NEED_FALLBACK ;
alg - > init = qce_skcipher_init_fallback ;
alg - > exit = qce_skcipher_exit ;
} else {
alg - > init = qce_skcipher_init ;
}
2014-06-25 19:28:57 +03:00
INIT_LIST_HEAD ( & tmpl - > entry ) ;
2019-11-09 18:09:45 +01:00
tmpl - > crypto_alg_type = CRYPTO_ALG_TYPE_SKCIPHER ;
2014-06-25 19:28:57 +03:00
tmpl - > alg_flags = def - > flags ;
tmpl - > qce = qce ;
2019-11-09 18:09:45 +01:00
ret = crypto_register_skcipher ( alg ) ;
2014-06-25 19:28:57 +03:00
if ( ret ) {
2019-11-09 18:09:45 +01:00
dev_err ( qce - > dev , " %s registration failed \n " , alg - > base . cra_name ) ;
2021-11-04 06:46:42 -07:00
kfree ( tmpl ) ;
2014-06-25 19:28:57 +03:00
return ret ;
}
2019-11-09 18:09:45 +01:00
list_add_tail ( & tmpl - > entry , & skcipher_algs ) ;
dev_dbg ( qce - > dev , " %s is registered \n " , alg - > base . cra_name ) ;
2014-06-25 19:28:57 +03:00
return 0 ;
}
2019-11-09 18:09:45 +01:00
static void qce_skcipher_unregister ( struct qce_device * qce )
2014-06-25 19:28:57 +03:00
{
struct qce_alg_template * tmpl , * n ;
2019-11-09 18:09:45 +01:00
list_for_each_entry_safe ( tmpl , n , & skcipher_algs , entry ) {
crypto_unregister_skcipher ( & tmpl - > alg . skcipher ) ;
2014-06-25 19:28:57 +03:00
list_del ( & tmpl - > entry ) ;
kfree ( tmpl ) ;
}
}
2019-11-09 18:09:45 +01:00
static int qce_skcipher_register ( struct qce_device * qce )
2014-06-25 19:28:57 +03:00
{
int ret , i ;
2019-11-09 18:09:45 +01:00
for ( i = 0 ; i < ARRAY_SIZE ( skcipher_def ) ; i + + ) {
ret = qce_skcipher_register_one ( & skcipher_def [ i ] , qce ) ;
2014-06-25 19:28:57 +03:00
if ( ret )
goto err ;
}
return 0 ;
err :
2019-11-09 18:09:45 +01:00
qce_skcipher_unregister ( qce ) ;
2014-06-25 19:28:57 +03:00
return ret ;
}
2019-11-09 18:09:45 +01:00
const struct qce_algo_ops skcipher_ops = {
. type = CRYPTO_ALG_TYPE_SKCIPHER ,
. register_algs = qce_skcipher_register ,
. unregister_algs = qce_skcipher_unregister ,
. async_req_handle = qce_skcipher_async_req_handle ,
2014-06-25 19:28:57 +03:00
} ;