Skip to content

Commit 3d59a58

Browse files
committed
Merge branch 'chcr-next'
Devulapally Shiva Krishna says: ==================== Crypto/chcr: Fix issues regarding algorithm implementation in driver The following series of patches fixes the issues which came during self-tests with CONFIG_CRYPTO_MANAGER_EXTRA_TESTS enabled. Patch 1: Fixes gcm(aes) hang issue and rfc4106-gcm encryption issue. Patch 2: Fixes ctr, cbc, xts and rfc3686-ctr extra test failures. Patch 3: Fixes ccm(aes) extra test failures. Patch 4: Added support for 48 byte-key_len in aes_xts. Patch 5: fix for hmac(sha) extra test failure. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 33395f4 + 02f58e5 commit 3d59a58

2 files changed

Lines changed: 68 additions & 22 deletions

File tree

‎drivers/crypto/chelsio/chcr_algo.c‎

Lines changed: 67 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,8 +1054,8 @@ static unsigned int adjust_ctr_overflow(u8 *iv, u32 bytes)
10541054
u32 temp = be32_to_cpu(*--b);
10551055

10561056
temp = ~temp;
1057-
c = (u64)temp + 1; // No of block can processed withou overflow
1058-
if ((bytes / AES_BLOCK_SIZE) > c)
1057+
c = (u64)temp + 1; // No of block can processed without overflow
1058+
if ((bytes / AES_BLOCK_SIZE) >= c)
10591059
bytes = c * AES_BLOCK_SIZE;
10601060
return bytes;
10611061
}
@@ -1077,7 +1077,14 @@ static int chcr_update_tweak(struct skcipher_request *req, u8 *iv,
10771077

10781078
keylen = ablkctx->enckey_len / 2;
10791079
key = ablkctx->key + keylen;
1080-
ret = aes_expandkey(&aes, key, keylen);
1080+
/* For a 192 bit key remove the padded zeroes which was
1081+
* added in chcr_xts_setkey
1082+
*/
1083+
if (KEY_CONTEXT_CK_SIZE_G(ntohl(ablkctx->key_ctx_hdr))
1084+
== CHCR_KEYCTX_CIPHER_KEY_SIZE_192)
1085+
ret = aes_expandkey(&aes, key, keylen - 8);
1086+
else
1087+
ret = aes_expandkey(&aes, key, keylen);
10811088
if (ret)
10821089
return ret;
10831090
aes_encrypt(&aes, iv, iv);
@@ -1158,15 +1165,16 @@ static int chcr_final_cipher_iv(struct skcipher_request *req,
11581165
static int chcr_handle_cipher_resp(struct skcipher_request *req,
11591166
unsigned char *input, int err)
11601167
{
1168+
struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
11611169
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1162-
struct chcr_context *ctx = c_ctx(tfm);
1163-
struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
1164-
struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
1165-
struct sk_buff *skb;
11661170
struct cpl_fw6_pld *fw6_pld = (struct cpl_fw6_pld *)input;
1167-
struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
1168-
struct cipher_wr_param wrparam;
1171+
struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
1172+
struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
11691173
struct chcr_dev *dev = c_ctx(tfm)->dev;
1174+
struct chcr_context *ctx = c_ctx(tfm);
1175+
struct adapter *adap = padap(ctx->dev);
1176+
struct cipher_wr_param wrparam;
1177+
struct sk_buff *skb;
11701178
int bytes;
11711179

11721180
if (err)
@@ -1197,6 +1205,8 @@ static int chcr_handle_cipher_resp(struct skcipher_request *req,
11971205
if (unlikely(bytes == 0)) {
11981206
chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
11991207
req);
1208+
memcpy(req->iv, reqctx->init_iv, IV);
1209+
atomic_inc(&adap->chcr_stats.fallback);
12001210
err = chcr_cipher_fallback(ablkctx->sw_cipher,
12011211
req->base.flags,
12021212
req->src,
@@ -1248,20 +1258,28 @@ static int process_cipher(struct skcipher_request *req,
12481258
struct sk_buff **skb,
12491259
unsigned short op_type)
12501260
{
1261+
struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
12511262
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
12521263
unsigned int ivsize = crypto_skcipher_ivsize(tfm);
1253-
struct chcr_skcipher_req_ctx *reqctx = skcipher_request_ctx(req);
12541264
struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
1265+
struct adapter *adap = padap(c_ctx(tfm)->dev);
12551266
struct cipher_wr_param wrparam;
12561267
int bytes, err = -EINVAL;
1268+
int subtype;
12571269

12581270
reqctx->processed = 0;
12591271
reqctx->partial_req = 0;
12601272
if (!req->iv)
12611273
goto error;
1274+
subtype = get_cryptoalg_subtype(tfm);
12621275
if ((ablkctx->enckey_len == 0) || (ivsize > AES_BLOCK_SIZE) ||
12631276
(req->cryptlen == 0) ||
12641277
(req->cryptlen % crypto_skcipher_blocksize(tfm))) {
1278+
if (req->cryptlen == 0 && subtype != CRYPTO_ALG_SUB_TYPE_XTS)
1279+
goto fallback;
1280+
else if (req->cryptlen % crypto_skcipher_blocksize(tfm) &&
1281+
subtype == CRYPTO_ALG_SUB_TYPE_XTS)
1282+
goto fallback;
12651283
pr_err("AES: Invalid value of Key Len %d nbytes %d IV Len %d\n",
12661284
ablkctx->enckey_len, req->cryptlen, ivsize);
12671285
goto error;
@@ -1302,33 +1320,36 @@ static int process_cipher(struct skcipher_request *req,
13021320
} else {
13031321
bytes = req->cryptlen;
13041322
}
1305-
if (get_cryptoalg_subtype(tfm) ==
1306-
CRYPTO_ALG_SUB_TYPE_CTR) {
1323+
if (subtype == CRYPTO_ALG_SUB_TYPE_CTR) {
13071324
bytes = adjust_ctr_overflow(req->iv, bytes);
13081325
}
1309-
if (get_cryptoalg_subtype(tfm) ==
1310-
CRYPTO_ALG_SUB_TYPE_CTR_RFC3686) {
1326+
if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_RFC3686) {
13111327
memcpy(reqctx->iv, ablkctx->nonce, CTR_RFC3686_NONCE_SIZE);
13121328
memcpy(reqctx->iv + CTR_RFC3686_NONCE_SIZE, req->iv,
13131329
CTR_RFC3686_IV_SIZE);
13141330

13151331
/* initialize counter portion of counter block */
13161332
*(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE +
13171333
CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
1334+
memcpy(reqctx->init_iv, reqctx->iv, IV);
13181335

13191336
} else {
13201337

13211338
memcpy(reqctx->iv, req->iv, IV);
1339+
memcpy(reqctx->init_iv, req->iv, IV);
13221340
}
13231341
if (unlikely(bytes == 0)) {
13241342
chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
13251343
req);
1344+
fallback: atomic_inc(&adap->chcr_stats.fallback);
13261345
err = chcr_cipher_fallback(ablkctx->sw_cipher,
13271346
req->base.flags,
13281347
req->src,
13291348
req->dst,
13301349
req->cryptlen,
1331-
reqctx->iv,
1350+
subtype ==
1351+
CRYPTO_ALG_SUB_TYPE_CTR_RFC3686 ?
1352+
reqctx->iv : req->iv,
13321353
op_type);
13331354
goto error;
13341355
}
@@ -1984,7 +2005,7 @@ static int chcr_ahash_digest(struct ahash_request *req)
19842005
req_ctx->data_len += params.bfr_len + params.sg_len;
19852006

19862007
if (req->nbytes == 0) {
1987-
create_last_hash_block(req_ctx->reqbfr, bs, 0);
2008+
create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
19882009
params.more = 1;
19892010
params.bfr_len = bs;
19902011
}
@@ -2250,12 +2271,28 @@ static int chcr_aes_xts_setkey(struct crypto_skcipher *cipher, const u8 *key,
22502271
ablkctx->enckey_len = key_len;
22512272
get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, key_len << 2);
22522273
context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len) >> 4;
2253-
ablkctx->key_ctx_hdr =
2274+
/* Both keys for xts must be aligned to 16 byte boundary
2275+
* by padding with zeros. So for 24 byte keys padding 8 zeroes.
2276+
*/
2277+
if (key_len == 48) {
2278+
context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len
2279+
+ 16) >> 4;
2280+
memmove(ablkctx->key + 32, ablkctx->key + 24, 24);
2281+
memset(ablkctx->key + 24, 0, 8);
2282+
memset(ablkctx->key + 56, 0, 8);
2283+
ablkctx->enckey_len = 64;
2284+
ablkctx->key_ctx_hdr =
2285+
FILL_KEY_CTX_HDR(CHCR_KEYCTX_CIPHER_KEY_SIZE_192,
2286+
CHCR_KEYCTX_NO_KEY, 1,
2287+
0, context_size);
2288+
} else {
2289+
ablkctx->key_ctx_hdr =
22542290
FILL_KEY_CTX_HDR((key_len == AES_KEYSIZE_256) ?
22552291
CHCR_KEYCTX_CIPHER_KEY_SIZE_128 :
22562292
CHCR_KEYCTX_CIPHER_KEY_SIZE_256,
22572293
CHCR_KEYCTX_NO_KEY, 1,
22582294
0, context_size);
2295+
}
22592296
ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_XTS;
22602297
return 0;
22612298
badkey_err:
@@ -2556,7 +2593,7 @@ int chcr_aead_dma_map(struct device *dev,
25562593
int dst_size;
25572594

25582595
dst_size = req->assoclen + req->cryptlen + (op_type ?
2559-
-authsize : authsize);
2596+
0 : authsize);
25602597
if (!req->cryptlen || !dst_size)
25612598
return 0;
25622599
reqctx->iv_dma = dma_map_single(dev, reqctx->iv, (IV + reqctx->b0_len),
@@ -2603,15 +2640,16 @@ void chcr_aead_dma_unmap(struct device *dev,
26032640
int dst_size;
26042641

26052642
dst_size = req->assoclen + req->cryptlen + (op_type ?
2606-
-authsize : authsize);
2643+
0 : authsize);
26072644
if (!req->cryptlen || !dst_size)
26082645
return;
26092646

26102647
dma_unmap_single(dev, reqctx->iv_dma, (IV + reqctx->b0_len),
26112648
DMA_BIDIRECTIONAL);
26122649
if (req->src == req->dst) {
2613-
dma_unmap_sg(dev, req->src, sg_nents(req->src),
2614-
DMA_BIDIRECTIONAL);
2650+
dma_unmap_sg(dev, req->src,
2651+
sg_nents_for_len(req->src, dst_size),
2652+
DMA_BIDIRECTIONAL);
26152653
} else {
26162654
dma_unmap_sg(dev, req->src, sg_nents(req->src),
26172655
DMA_TO_DEVICE);
@@ -2910,7 +2948,7 @@ static void fill_sec_cpl_for_aead(struct cpl_tx_sec_pdu *sec_cpl,
29102948
unsigned int mac_mode = CHCR_SCMD_AUTH_MODE_CBCMAC;
29112949
unsigned int rx_channel_id = reqctx->rxqidx / ctx->rxq_perchan;
29122950
unsigned int ccm_xtra;
2913-
unsigned char tag_offset = 0, auth_offset = 0;
2951+
unsigned int tag_offset = 0, auth_offset = 0;
29142952
unsigned int assoclen;
29152953

29162954
if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
@@ -3702,6 +3740,13 @@ static int chcr_aead_op(struct aead_request *req,
37023740
return -ENOSPC;
37033741
}
37043742

3743+
if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 &&
3744+
crypto_ipsec_check_assoclen(req->assoclen) != 0) {
3745+
pr_err("RFC4106: Invalid value of assoclen %d\n",
3746+
req->assoclen);
3747+
return -EINVAL;
3748+
}
3749+
37053750
/* Form a WR from req */
37063751
skb = create_wr_fn(req, u_ctx->lldi.rxq_ids[reqctx->rxqidx], size);
37073752

‎drivers/crypto/chelsio/chcr_crypto.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ struct chcr_skcipher_req_ctx {
302302
unsigned int op;
303303
u16 imm;
304304
u8 iv[CHCR_MAX_CRYPTO_IV_LEN];
305+
u8 init_iv[CHCR_MAX_CRYPTO_IV_LEN];
305306
u16 txqidx;
306307
u16 rxqidx;
307308
};

0 commit comments

Comments
 (0)