Skip to content

Commit ea0b398

Browse files
Haren Myneniherbertx
authored andcommitted
crypto: 842 - Add CRC and validation support
This patch adds CRC generation and validation support for nx-842. Add CRC flag so that nx842 coprocessor includes CRC during compression and validates during decompression. Also changes in 842 SW compression to append CRC value at the end of template and checks during decompression. Signed-off-by: Haren Myneni <haren@us.ibm.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent f512843 commit ea0b398

5 files changed

Lines changed: 40 additions & 4 deletions

File tree

‎drivers/crypto/nx/nx-842-powernv.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ static int nx842_powernv_compress(const unsigned char *in, unsigned int inlen,
491491
void *wmem)
492492
{
493493
return nx842_powernv_function(in, inlen, out, outlenp,
494-
wmem, CCW_FC_842_COMP_NOCRC);
494+
wmem, CCW_FC_842_COMP_CRC);
495495
}
496496

497497
/**
@@ -519,7 +519,7 @@ static int nx842_powernv_decompress(const unsigned char *in, unsigned int inlen,
519519
void *wmem)
520520
{
521521
return nx842_powernv_function(in, inlen, out, outlenp,
522-
wmem, CCW_FC_842_DECOMP_NOCRC);
522+
wmem, CCW_FC_842_DECOMP_CRC);
523523
}
524524

525525
static int __init nx842_powernv_probe(struct device_node *dn)

‎drivers/crypto/nx/nx-842-pseries.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ static int nx842_validate_result(struct device *dev,
234234
dev_dbg(dev, "%s: Out of space in output buffer\n",
235235
__func__);
236236
return -ENOSPC;
237+
case 65: /* Calculated CRC doesn't match the passed value */
238+
dev_dbg(dev, "%s: CRC mismatch for decompression\n",
239+
__func__);
240+
return -EINVAL;
237241
case 66: /* Input data contains an illegal template field */
238242
case 67: /* Template indicates data past the end of the input stream */
239243
dev_dbg(dev, "%s: Bad data for decompression (code:%d)\n",
@@ -324,7 +328,7 @@ static int nx842_pseries_compress(const unsigned char *in, unsigned int inlen,
324328
slout.entries = (struct nx842_slentry *)workmem->slout;
325329

326330
/* Init operation */
327-
op.flags = NX842_OP_COMPRESS;
331+
op.flags = NX842_OP_COMPRESS_CRC;
328332
csbcpb = &workmem->csbcpb;
329333
memset(csbcpb, 0, sizeof(*csbcpb));
330334
op.csbcpb = nx842_get_pa(csbcpb);
@@ -457,7 +461,7 @@ static int nx842_pseries_decompress(const unsigned char *in, unsigned int inlen,
457461
slout.entries = (struct nx842_slentry *)workmem->slout;
458462

459463
/* Init operation */
460-
op.flags = NX842_OP_DECOMPRESS;
464+
op.flags = NX842_OP_DECOMPRESS_CRC;
461465
csbcpb = &workmem->csbcpb;
462466
memset(csbcpb, 0, sizeof(*csbcpb));
463467
op.csbcpb = nx842_get_pa(csbcpb);

‎lib/842/842.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include <linux/module.h>
7777
#include <linux/kernel.h>
7878
#include <linux/bitops.h>
79+
#include <linux/crc32.h>
7980
#include <asm/unaligned.h>
8081

8182
#include <linux/sw842.h>
@@ -98,6 +99,7 @@
9899
#define I2_BITS (8)
99100
#define I4_BITS (9)
100101
#define I8_BITS (8)
102+
#define CRC_BITS (32)
101103

102104
#define REPEAT_BITS_MAX (0x3f)
103105
#define SHORT_DATA_BITS_MAX (0x7)

‎lib/842/842_compress.c‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ int sw842_compress(const u8 *in, unsigned int ilen,
490490
int ret;
491491
u64 last, next, pad, total;
492492
u8 repeat_count = 0;
493+
u32 crc;
493494

494495
BUILD_BUG_ON(sizeof(*p) > SW842_MEM_COMPRESS);
495496

@@ -580,6 +581,18 @@ int sw842_compress(const u8 *in, unsigned int ilen,
580581
if (ret)
581582
return ret;
582583

584+
/*
585+
* crc(0:31) is appended to target data starting with the next
586+
* bit after End of stream template.
587+
* nx842 calculates CRC for data in big-endian format. So doing
588+
* same here so that sw842 decompression can be used for both
589+
* compressed data.
590+
*/
591+
crc = crc32_be(0, in, ilen);
592+
ret = add_bits(p, crc, CRC_BITS);
593+
if (ret)
594+
return ret;
595+
583596
if (p->bit) {
584597
p->out++;
585598
p->olen--;

‎lib/842/842_decompress.c‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ int sw842_decompress(const u8 *in, unsigned int ilen,
285285
struct sw842_param p;
286286
int ret;
287287
u64 op, rep, tmp, bytes, total;
288+
u64 crc;
288289

289290
p.in = (u8 *)in;
290291
p.bit = 0;
@@ -375,6 +376,22 @@ int sw842_decompress(const u8 *in, unsigned int ilen,
375376
}
376377
} while (op != OP_END);
377378

379+
/*
380+
* crc(0:31) is saved in compressed data starting with the
381+
* next bit after End of stream template.
382+
*/
383+
ret = next_bits(&p, &crc, CRC_BITS);
384+
if (ret)
385+
return ret;
386+
387+
/*
388+
* Validate CRC saved in compressed data.
389+
*/
390+
if (crc != (u64)crc32_be(0, out, total - p.olen)) {
391+
pr_debug("CRC mismatch for decompression\n");
392+
return -EINVAL;
393+
}
394+
378395
if (unlikely((total - p.olen) > UINT_MAX))
379396
return -ENOSPC;
380397

0 commit comments

Comments
 (0)