Skip to content

Commit 79fd31d

Browse files
horiagherbertx
authored andcommitted
crypto: talitos - support for assoc data provided as scatterlist
Generate a link table in case assoc data is a scatterlist. While at it, add support for handling non-contiguous assoc data and iv. Signed-off-by: Horia Geanta <horia.geanta@freescale.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 2a1cfe4 commit 79fd31d

1 file changed

Lines changed: 125 additions & 51 deletions

File tree

‎drivers/crypto/talitos.c‎

Lines changed: 125 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -714,10 +714,13 @@ static int aead_setkey(struct crypto_aead *authenc,
714714

715715
/*
716716
* talitos_edesc - s/w-extended descriptor
717+
* @assoc_nents: number of segments in associated data scatterlist
717718
* @src_nents: number of segments in input scatterlist
718719
* @dst_nents: number of segments in output scatterlist
720+
* @assoc_chained: whether assoc is chained or not
719721
* @src_chained: whether src is chained or not
720722
* @dst_chained: whether dst is chained or not
723+
* @iv_dma: dma address of iv for checking continuity and link table
721724
* @dma_len: length of dma mapped link_tbl space
722725
* @dma_link_tbl: bus physical address of link_tbl
723726
* @desc: h/w descriptor
@@ -728,10 +731,13 @@ static int aead_setkey(struct crypto_aead *authenc,
728731
* of link_tbl data
729732
*/
730733
struct talitos_edesc {
734+
int assoc_nents;
731735
int src_nents;
732736
int dst_nents;
737+
bool assoc_chained;
733738
bool src_chained;
734739
bool dst_chained;
740+
dma_addr_t iv_dma;
735741
int dma_len;
736742
dma_addr_t dma_link_tbl;
737743
struct talitos_desc desc;
@@ -799,7 +805,13 @@ static void ipsec_esp_unmap(struct device *dev,
799805
unmap_single_talitos_ptr(dev, &edesc->desc.ptr[2], DMA_TO_DEVICE);
800806
unmap_single_talitos_ptr(dev, &edesc->desc.ptr[0], DMA_TO_DEVICE);
801807

802-
dma_unmap_sg(dev, areq->assoc, 1, DMA_TO_DEVICE);
808+
if (edesc->assoc_chained)
809+
talitos_unmap_sg_chain(dev, areq->assoc, DMA_TO_DEVICE);
810+
else
811+
/* assoc_nents counts also for IV in non-contiguous cases */
812+
dma_unmap_sg(dev, areq->assoc,
813+
edesc->assoc_nents ? edesc->assoc_nents - 1 : 1,
814+
DMA_TO_DEVICE);
803815

804816
talitos_sg_unmap(dev, edesc, areq->src, areq->dst);
805817

@@ -829,7 +841,8 @@ static void ipsec_esp_encrypt_done(struct device *dev,
829841
/* copy the generated ICV to dst */
830842
if (edesc->dst_nents) {
831843
icvdata = &edesc->link_tbl[edesc->src_nents +
832-
edesc->dst_nents + 2];
844+
edesc->dst_nents + 2 +
845+
edesc->assoc_nents];
833846
sg = sg_last(areq->dst, edesc->dst_nents);
834847
memcpy((char *)sg_virt(sg) + sg->length - ctx->authsize,
835848
icvdata, ctx->authsize);
@@ -859,7 +872,8 @@ static void ipsec_esp_decrypt_swauth_done(struct device *dev,
859872
/* auth check */
860873
if (edesc->dma_len)
861874
icvdata = &edesc->link_tbl[edesc->src_nents +
862-
edesc->dst_nents + 2];
875+
edesc->dst_nents + 2 +
876+
edesc->assoc_nents];
863877
else
864878
icvdata = &edesc->link_tbl[0];
865879

@@ -934,10 +948,9 @@ static int sg_to_link_tbl(struct scatterlist *sg, int sg_count,
934948
* fill in and submit ipsec_esp descriptor
935949
*/
936950
static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
937-
u8 *giv, u64 seq,
938-
void (*callback) (struct device *dev,
939-
struct talitos_desc *desc,
940-
void *context, int error))
951+
u64 seq, void (*callback) (struct device *dev,
952+
struct talitos_desc *desc,
953+
void *context, int error))
941954
{
942955
struct crypto_aead *aead = crypto_aead_reqtfm(areq);
943956
struct talitos_ctx *ctx = crypto_aead_ctx(aead);
@@ -952,12 +965,42 @@ static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
952965
/* hmac key */
953966
map_single_talitos_ptr(dev, &desc->ptr[0], ctx->authkeylen, &ctx->key,
954967
0, DMA_TO_DEVICE);
968+
955969
/* hmac data */
956-
map_single_talitos_ptr(dev, &desc->ptr[1], areq->assoclen + ivsize,
957-
sg_virt(areq->assoc), 0, DMA_TO_DEVICE);
970+
desc->ptr[1].len = cpu_to_be16(areq->assoclen + ivsize);
971+
if (edesc->assoc_nents) {
972+
int tbl_off = edesc->src_nents + edesc->dst_nents + 2;
973+
struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
974+
975+
to_talitos_ptr(&desc->ptr[1], edesc->dma_link_tbl + tbl_off *
976+
sizeof(struct talitos_ptr));
977+
desc->ptr[1].j_extent = DESC_PTR_LNKTBL_JUMP;
978+
979+
/* assoc_nents - 1 entries for assoc, 1 for IV */
980+
sg_count = sg_to_link_tbl(areq->assoc, edesc->assoc_nents - 1,
981+
areq->assoclen, tbl_ptr);
982+
983+
/* add IV to link table */
984+
tbl_ptr += sg_count - 1;
985+
tbl_ptr->j_extent = 0;
986+
tbl_ptr++;
987+
to_talitos_ptr(tbl_ptr, edesc->iv_dma);
988+
tbl_ptr->len = cpu_to_be16(ivsize);
989+
tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
990+
991+
dma_sync_single_for_device(dev, edesc->dma_link_tbl,
992+
edesc->dma_len, DMA_BIDIRECTIONAL);
993+
} else {
994+
to_talitos_ptr(&desc->ptr[1], sg_dma_address(areq->assoc));
995+
desc->ptr[1].j_extent = 0;
996+
}
997+
958998
/* cipher iv */
959-
map_single_talitos_ptr(dev, &desc->ptr[2], ivsize, giv ?: areq->iv, 0,
960-
DMA_TO_DEVICE);
999+
to_talitos_ptr(&desc->ptr[2], edesc->iv_dma);
1000+
desc->ptr[2].len = cpu_to_be16(ivsize);
1001+
desc->ptr[2].j_extent = 0;
1002+
/* Sync needed for the aead_givencrypt case */
1003+
dma_sync_single_for_device(dev, edesc->iv_dma, ivsize, DMA_TO_DEVICE);
9611004

9621005
/* cipher key */
9631006
map_single_talitos_ptr(dev, &desc->ptr[3], ctx->enckeylen,
@@ -1013,26 +1056,25 @@ static int ipsec_esp(struct talitos_edesc *edesc, struct aead_request *areq,
10131056
if (sg_count == 1) {
10141057
to_talitos_ptr(&desc->ptr[5], sg_dma_address(areq->dst));
10151058
} else {
1016-
struct talitos_ptr *link_tbl_ptr =
1017-
&edesc->link_tbl[edesc->src_nents + 1];
1059+
int tbl_off = edesc->src_nents + 1;
1060+
struct talitos_ptr *tbl_ptr = &edesc->link_tbl[tbl_off];
10181061

10191062
to_talitos_ptr(&desc->ptr[5], edesc->dma_link_tbl +
1020-
(edesc->src_nents + 1) *
1021-
sizeof(struct talitos_ptr));
1063+
tbl_off * sizeof(struct talitos_ptr));
10221064
sg_count = sg_to_link_tbl(areq->dst, sg_count, cryptlen,
1023-
link_tbl_ptr);
1065+
tbl_ptr);
10241066

10251067
/* Add an entry to the link table for ICV data */
1026-
link_tbl_ptr += sg_count - 1;
1027-
link_tbl_ptr->j_extent = 0;
1028-
sg_count++;
1029-
link_tbl_ptr++;
1030-
link_tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1031-
link_tbl_ptr->len = cpu_to_be16(authsize);
1068+
tbl_ptr += sg_count - 1;
1069+
tbl_ptr->j_extent = 0;
1070+
tbl_ptr++;
1071+
tbl_ptr->j_extent = DESC_PTR_LNKTBL_RETURN;
1072+
tbl_ptr->len = cpu_to_be16(authsize);
10321073

10331074
/* icv data follows link tables */
1034-
to_talitos_ptr(link_tbl_ptr, edesc->dma_link_tbl +
1035-
(edesc->src_nents + edesc->dst_nents + 2) *
1075+
to_talitos_ptr(tbl_ptr, edesc->dma_link_tbl +
1076+
(tbl_off + edesc->dst_nents + 1 +
1077+
edesc->assoc_nents) *
10361078
sizeof(struct talitos_ptr));
10371079
desc->ptr[5].j_extent |= DESC_PTR_LNKTBL_JUMP;
10381080
dma_sync_single_for_device(ctx->dev, edesc->dma_link_tbl,
@@ -1133,16 +1175,21 @@ static size_t sg_copy_end_to_buffer(struct scatterlist *sgl, unsigned int nents,
11331175
* allocate and map the extended descriptor
11341176
*/
11351177
static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
1178+
struct scatterlist *assoc,
11361179
struct scatterlist *src,
11371180
struct scatterlist *dst,
1181+
u8 *iv,
1182+
unsigned int assoclen,
11381183
unsigned int cryptlen,
11391184
unsigned int authsize,
1185+
unsigned int ivsize,
11401186
int icv_stashing,
11411187
u32 cryptoflags)
11421188
{
11431189
struct talitos_edesc *edesc;
1144-
int src_nents, dst_nents, alloc_len, dma_len;
1145-
bool src_chained, dst_chained = false;
1190+
int assoc_nents = 0, src_nents, dst_nents, alloc_len, dma_len;
1191+
bool assoc_chained = false, src_chained = false, dst_chained = false;
1192+
dma_addr_t iv_dma = 0;
11461193
gfp_t flags = cryptoflags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
11471194
GFP_ATOMIC;
11481195

@@ -1151,6 +1198,25 @@ static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
11511198
return ERR_PTR(-EINVAL);
11521199
}
11531200

1201+
if (iv)
1202+
iv_dma = dma_map_single(dev, iv, ivsize, DMA_TO_DEVICE);
1203+
1204+
if (assoc) {
1205+
/*
1206+
* Currently it is assumed that iv is provided whenever assoc
1207+
* is.
1208+
*/
1209+
BUG_ON(!iv);
1210+
1211+
assoc_nents = sg_count(assoc, assoclen, &assoc_chained);
1212+
talitos_map_sg(dev, assoc, assoc_nents, DMA_TO_DEVICE,
1213+
assoc_chained);
1214+
assoc_nents = (assoc_nents == 1) ? 0 : assoc_nents;
1215+
1216+
if (assoc_nents || sg_dma_address(assoc) + assoclen != iv_dma)
1217+
assoc_nents = assoc_nents ? assoc_nents + 1 : 2;
1218+
}
1219+
11541220
src_nents = sg_count(src, cryptlen + authsize, &src_chained);
11551221
src_nents = (src_nents == 1) ? 0 : src_nents;
11561222

@@ -1172,9 +1238,9 @@ static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
11721238
* and the ICV data itself
11731239
*/
11741240
alloc_len = sizeof(struct talitos_edesc);
1175-
if (src_nents || dst_nents) {
1176-
dma_len = (src_nents + dst_nents + 2) *
1177-
sizeof(struct talitos_ptr) + authsize;
1241+
if (assoc_nents || src_nents || dst_nents) {
1242+
dma_len = (src_nents + dst_nents + 2 + assoc_nents) *
1243+
sizeof(struct talitos_ptr) + authsize;
11781244
alloc_len += dma_len;
11791245
} else {
11801246
dma_len = 0;
@@ -1183,14 +1249,20 @@ static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
11831249

11841250
edesc = kmalloc(alloc_len, GFP_DMA | flags);
11851251
if (!edesc) {
1252+
talitos_unmap_sg_chain(dev, assoc, DMA_TO_DEVICE);
1253+
if (iv_dma)
1254+
dma_unmap_single(dev, iv_dma, ivsize, DMA_TO_DEVICE);
11861255
dev_err(dev, "could not allocate edescriptor\n");
11871256
return ERR_PTR(-ENOMEM);
11881257
}
11891258

1259+
edesc->assoc_nents = assoc_nents;
11901260
edesc->src_nents = src_nents;
11911261
edesc->dst_nents = dst_nents;
1262+
edesc->assoc_chained = assoc_chained;
11921263
edesc->src_chained = src_chained;
11931264
edesc->dst_chained = dst_chained;
1265+
edesc->iv_dma = iv_dma;
11941266
edesc->dma_len = dma_len;
11951267
if (dma_len)
11961268
edesc->dma_link_tbl = dma_map_single(dev, &edesc->link_tbl[0],
@@ -1200,14 +1272,16 @@ static struct talitos_edesc *talitos_edesc_alloc(struct device *dev,
12001272
return edesc;
12011273
}
12021274

1203-
static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq,
1275+
static struct talitos_edesc *aead_edesc_alloc(struct aead_request *areq, u8 *iv,
12041276
int icv_stashing)
12051277
{
12061278
struct crypto_aead *authenc = crypto_aead_reqtfm(areq);
12071279
struct talitos_ctx *ctx = crypto_aead_ctx(authenc);
1280+
unsigned int ivsize = crypto_aead_ivsize(authenc);
12081281

1209-
return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst,
1210-
areq->cryptlen, ctx->authsize, icv_stashing,
1282+
return talitos_edesc_alloc(ctx->dev, areq->assoc, areq->src, areq->dst,
1283+
iv, areq->assoclen, areq->cryptlen,
1284+
ctx->authsize, ivsize, icv_stashing,
12111285
areq->base.flags);
12121286
}
12131287

@@ -1218,14 +1292,14 @@ static int aead_encrypt(struct aead_request *req)
12181292
struct talitos_edesc *edesc;
12191293

12201294
/* allocate extended descriptor */
1221-
edesc = aead_edesc_alloc(req, 0);
1295+
edesc = aead_edesc_alloc(req, req->iv, 0);
12221296
if (IS_ERR(edesc))
12231297
return PTR_ERR(edesc);
12241298

12251299
/* set encrypt */
12261300
edesc->desc.hdr = ctx->desc_hdr_template | DESC_HDR_MODE0_ENCRYPT;
12271301

1228-
return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_encrypt_done);
1302+
return ipsec_esp(edesc, req, 0, ipsec_esp_encrypt_done);
12291303
}
12301304

12311305
static int aead_decrypt(struct aead_request *req)
@@ -1241,7 +1315,7 @@ static int aead_decrypt(struct aead_request *req)
12411315
req->cryptlen -= authsize;
12421316

12431317
/* allocate extended descriptor */
1244-
edesc = aead_edesc_alloc(req, 1);
1318+
edesc = aead_edesc_alloc(req, req->iv, 1);
12451319
if (IS_ERR(edesc))
12461320
return PTR_ERR(edesc);
12471321

@@ -1257,9 +1331,7 @@ static int aead_decrypt(struct aead_request *req)
12571331
/* reset integrity check result bits */
12581332
edesc->desc.hdr_lo = 0;
12591333

1260-
return ipsec_esp(edesc, req, NULL, 0,
1261-
ipsec_esp_decrypt_hwauth_done);
1262-
1334+
return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_hwauth_done);
12631335
}
12641336

12651337
/* Have to check the ICV with software */
@@ -1268,7 +1340,8 @@ static int aead_decrypt(struct aead_request *req)
12681340
/* stash incoming ICV for later cmp with ICV generated by the h/w */
12691341
if (edesc->dma_len)
12701342
icvdata = &edesc->link_tbl[edesc->src_nents +
1271-
edesc->dst_nents + 2];
1343+
edesc->dst_nents + 2 +
1344+
edesc->assoc_nents];
12721345
else
12731346
icvdata = &edesc->link_tbl[0];
12741347

@@ -1277,7 +1350,7 @@ static int aead_decrypt(struct aead_request *req)
12771350
memcpy(icvdata, (char *)sg_virt(sg) + sg->length - ctx->authsize,
12781351
ctx->authsize);
12791352

1280-
return ipsec_esp(edesc, req, NULL, 0, ipsec_esp_decrypt_swauth_done);
1353+
return ipsec_esp(edesc, req, 0, ipsec_esp_decrypt_swauth_done);
12811354
}
12821355

12831356
static int aead_givencrypt(struct aead_givcrypt_request *req)
@@ -1288,7 +1361,7 @@ static int aead_givencrypt(struct aead_givcrypt_request *req)
12881361
struct talitos_edesc *edesc;
12891362

12901363
/* allocate extended descriptor */
1291-
edesc = aead_edesc_alloc(areq, 0);
1364+
edesc = aead_edesc_alloc(areq, req->giv, 0);
12921365
if (IS_ERR(edesc))
12931366
return PTR_ERR(edesc);
12941367

@@ -1299,8 +1372,7 @@ static int aead_givencrypt(struct aead_givcrypt_request *req)
12991372
/* avoid consecutive packets going out with same IV */
13001373
*(__be64 *)req->giv ^= cpu_to_be64(req->seq);
13011374

1302-
return ipsec_esp(edesc, areq, req->giv, req->seq,
1303-
ipsec_esp_encrypt_done);
1375+
return ipsec_esp(edesc, areq, req->seq, ipsec_esp_encrypt_done);
13041376
}
13051377

13061378
static int ablkcipher_setkey(struct crypto_ablkcipher *cipher,
@@ -1356,7 +1428,7 @@ static int common_nonsnoop(struct talitos_edesc *edesc,
13561428
struct device *dev = ctx->dev;
13571429
struct talitos_desc *desc = &edesc->desc;
13581430
unsigned int cryptlen = areq->nbytes;
1359-
unsigned int ivsize;
1431+
unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
13601432
int sg_count, ret;
13611433

13621434
/* first DWORD empty */
@@ -1365,9 +1437,9 @@ static int common_nonsnoop(struct talitos_edesc *edesc,
13651437
desc->ptr[0].j_extent = 0;
13661438

13671439
/* cipher iv */
1368-
ivsize = crypto_ablkcipher_ivsize(cipher);
1369-
map_single_talitos_ptr(dev, &desc->ptr[1], ivsize, areq->info, 0,
1370-
DMA_TO_DEVICE);
1440+
to_talitos_ptr(&desc->ptr[1], edesc->iv_dma);
1441+
desc->ptr[1].len = cpu_to_be16(ivsize);
1442+
desc->ptr[1].j_extent = 0;
13711443

13721444
/* cipher key */
13731445
map_single_talitos_ptr(dev, &desc->ptr[2], ctx->keylen,
@@ -1449,9 +1521,11 @@ static struct talitos_edesc *ablkcipher_edesc_alloc(struct ablkcipher_request *
14491521
{
14501522
struct crypto_ablkcipher *cipher = crypto_ablkcipher_reqtfm(areq);
14511523
struct talitos_ctx *ctx = crypto_ablkcipher_ctx(cipher);
1524+
unsigned int ivsize = crypto_ablkcipher_ivsize(cipher);
14521525

1453-
return talitos_edesc_alloc(ctx->dev, areq->src, areq->dst, areq->nbytes,
1454-
0, 0, areq->base.flags);
1526+
return talitos_edesc_alloc(ctx->dev, NULL, areq->src, areq->dst,
1527+
areq->info, 0, areq->nbytes, 0, ivsize, 0,
1528+
areq->base.flags);
14551529
}
14561530

14571531
static int ablkcipher_encrypt(struct ablkcipher_request *areq)
@@ -1629,8 +1703,8 @@ static struct talitos_edesc *ahash_edesc_alloc(struct ahash_request *areq,
16291703
struct talitos_ctx *ctx = crypto_ahash_ctx(tfm);
16301704
struct talitos_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
16311705

1632-
return talitos_edesc_alloc(ctx->dev, req_ctx->psrc, NULL, nbytes, 0, 0,
1633-
areq->base.flags);
1706+
return talitos_edesc_alloc(ctx->dev, NULL, req_ctx->psrc, NULL, NULL, 0,
1707+
nbytes, 0, 0, 0, areq->base.flags);
16341708
}
16351709

16361710
static int ahash_init(struct ahash_request *areq)

0 commit comments

Comments
 (0)