Skip to content

Commit b6cd695

Browse files
committed
[Review OP-TEE#2] drivers: caam: implement NXP CAAM driver - Cipher
Minors fixes. Signed-off-by: Clement Faure <clement.faure@nxp.com>
1 parent 9a5de6b commit b6cd695

File tree

4 files changed

+42
-42
lines changed

4 files changed

+42
-42
lines changed

core/drivers/crypto/caam/cipher/caam_cipher.c

+34-37
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818

1919
#include "local.h"
2020

21+
/*
22+
* Maximum job ring descriptor entries
23+
*/
24+
#ifdef CFG_CAAM_64BIT
25+
#define MAX_DESC_ENTRIES 22
26+
#else
27+
#define MAX_DESC_ENTRIES 16
28+
#endif
29+
2130
/*
2231
* Max Cipher Buffer to encrypt/decrypt at each operation
2332
*/
@@ -165,10 +174,8 @@ static enum caam_status copy_ctx_data(struct caambuf *dst,
165174
static enum caam_status do_check_keysize(const struct caamdefkey *def,
166175
size_t size)
167176
{
168-
if (size >= def->min && size <= def->max) {
169-
if (!(size % def->mod))
170-
return CAAM_NO_ERROR;
171-
}
177+
if (size >= def->min && size <= def->max && !(size % def->mod))
178+
return CAAM_NO_ERROR;
172179

173180
return CAAM_BAD_PARAM;
174181
}
@@ -263,9 +270,9 @@ enum caam_status caam_cipher_block(struct cipherdata *ctx, bool savectx,
263270
src_sgt.length);
264271
}
265272

266-
/* No output data to do - just create/update operation context */
273+
/* No output data - just create/update operation context */
267274
if (!outdata)
268-
goto desc_no_outdata;
275+
goto handle_context;
269276

270277
/*
271278
* Output data storage.
@@ -310,7 +317,7 @@ enum caam_status caam_cipher_block(struct cipherdata *ctx, bool savectx,
310317
dst_sgt.length);
311318
}
312319

313-
desc_no_outdata:
320+
handle_context:
314321
if (ctx->ctx.length && ctx->alg->size_ctx) {
315322
if (savectx) {
316323
/* Store the context */
@@ -383,31 +390,24 @@ static const struct cipheralg *get_cipheralgo(uint32_t algo)
383390
break;
384391
}
385392

386-
if (ca)
387-
if (ca->type)
388-
return ca;
393+
if (ca && ca->type)
394+
return ca;
389395

390396
return NULL;
391397
}
392398

393399
/*
394400
* Allocate the SW cipher data context
395401
*
396-
* @algo Algorithm ID of the context
397402
* @ctx [out] Caller context variable
403+
* @algo Algorithm ID of the context
398404
*/
399405
static TEE_Result do_allocate(void **ctx, uint32_t algo)
400406
{
401407
TEE_Result ret = TEE_ERROR_NOT_IMPLEMENTED;
402408
struct cipherdata *cipherdata = NULL;
403409
const struct cipheralg *alg = NULL;
404410

405-
#ifdef CFG_CAAM_64BIT
406-
#define MAX_DESC_ENTRIES 22
407-
#else
408-
#define MAX_DESC_ENTRIES 16
409-
#endif
410-
411411
CIPHER_TRACE("Allocate Algo 0x%" PRIX32 " Context (%p)", algo, ctx);
412412

413413
alg = get_cipheralgo(algo);
@@ -450,7 +450,7 @@ static TEE_Result do_allocate(void **ctx, uint32_t algo)
450450
/*
451451
* Free the internal cipher data context
452452
*
453-
* @ctx Caller context variable
453+
* @ctx Caller context variable or NULL
454454
*/
455455
static void do_free_intern(struct cipherdata *ctx)
456456
{
@@ -480,7 +480,7 @@ static void do_free_intern(struct cipherdata *ctx)
480480
/*
481481
* Free the SW Cipher data context
482482
*
483-
* @ctx Caller context variable
483+
* @ctx Caller context variable or NULL
484484
*/
485485
static void do_free(void *ctx)
486486
{
@@ -661,7 +661,7 @@ static TEE_Result do_init(struct drvcrypt_cipher_init *dinit)
661661
ret = caam_calloc_align_buf(&cipherdata->tweak,
662662
alg->size_block);
663663
if (ret != CAAM_NO_ERROR)
664-
return ret;
664+
goto out;
665665
} else {
666666
/* Fill it with 0's */
667667
memset(cipherdata->tweak.data, 0,
@@ -805,13 +805,14 @@ static TEE_Result do_update_streaming(struct drvcrypt_cipher_update *dupdate)
805805
}
806806

807807
if (size_topost) {
808-
CIPHER_TRACE("Save input data %zu bytes (done %zu)",
809-
size_topost, size_indone);
810808
struct caambuf cpysrc = {
811809
.data = dupdate->src.data,
812810
.length = dupdate->src.length
813811
};
814812

813+
CIPHER_TRACE("Save input data %zu bytes (done %zu)",
814+
size_topost, size_indone);
815+
815816
retstatus = caam_cpy_block_src(&ctx->blockbuf, &cpysrc,
816817
size_indone);
817818
if (retstatus != CAAM_NO_ERROR) {
@@ -944,27 +945,26 @@ static TEE_Result do_update_cipher(struct drvcrypt_cipher_update *dupdate)
944945
srcbuf.length = MAX_CIPHER_BUFFER;
945946
dstbuf.length = MAX_CIPHER_BUFFER;
946947

947-
for (; nb_buf; nb_buf--) {
948+
while (nb_buf--) {
948949
srcbuf.data += offset;
949950
srcbuf.paddr += offset;
950951

951-
CIPHER_TRACE("Do nb_buf=%" PRId32 ", offset %zu", nb_buf,
952-
offset);
952+
CIPHER_TRACE("Do nb_buf=%u, offset %zu", nb_buf, offset);
953+
953954
retstatus =
954955
caam_cipher_block(ctx, true, NEED_KEY1, ctx->encrypt,
955956
&srcbuf, &dstbuf, false);
956957

957-
if (retstatus == CAAM_NO_ERROR) {
958-
cache_operation(TEE_CACHEINVALIDATE, dstbuf.data,
959-
dstbuf.length);
960-
961-
memcpy(dupdate->dst.data + offset, dstbuf.data,
962-
dstbuf.length);
963-
} else {
958+
if (retstatus != CAAM_NO_ERROR) {
964959
ret = TEE_ERROR_GENERIC;
965960
goto out;
966961
}
967962

963+
cache_operation(TEE_CACHEINVALIDATE, dstbuf.data,
964+
dstbuf.length);
965+
966+
memcpy(dupdate->dst.data + offset, dstbuf.data, dstbuf.length);
967+
968968
offset += MAX_CIPHER_BUFFER;
969969
}
970970

@@ -1017,18 +1017,15 @@ static TEE_Result do_update_cipher(struct drvcrypt_cipher_update *dupdate)
10171017
*/
10181018
static TEE_Result do_update(struct drvcrypt_cipher_update *dupdate)
10191019
{
1020-
TEE_Result ret = TEE_ERROR_GENERIC;
10211020
struct cipherdata *cipherdata = dupdate->ctx;
10221021

1023-
ret = cipherdata->alg->update(dupdate);
1024-
1025-
return ret;
1022+
return cipherdata->alg->update(dupdate);
10261023
}
10271024

10281025
/*
10291026
* Finalize of the cipher operation
10301027
*
1031-
* @ctx Caller context variable
1028+
* @ctx Caller context variable or NULL
10321029
*/
10331030
static void do_final(void *ctx __unused)
10341031
{

core/drivers/crypto/caam/cipher/caam_cipher_xts.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ static void do_galois_mult(struct caambuf *buf)
2424

2525
for (idx = 0; idx < buf->length; idx++) {
2626
tmptmp = buf->data[idx] >> 7;
27-
buf->data[idx] = ((buf->data[idx] << 1) | tmp) & UINT8_MAX;
27+
buf->data[idx] = (buf->data[idx] << 1) | tmp;
2828
tmp = tmptmp;
2929
}
3030

@@ -36,7 +36,7 @@ static void do_galois_mult(struct caambuf *buf)
3636
* Tweak a cipher block (XTS mode)
3737
*
3838
* @ctx Cipher context
39-
* @enc_tweak [in/out] Encrypted tweak (galois multiplication)
39+
* @enc_tweak [in/out] Encrypted tweak (Galois multiplication)
4040
* @srcbuf Source data to encrypt/decrypt
4141
* @dstbuf [out] Destination data encrypted/decrypted
4242
* @tmp Temporary data buffer
@@ -138,8 +138,10 @@ TEE_Result caam_cipher_update_xts(struct drvcrypt_cipher_update *dupdate)
138138
fullsize -= lastblk;
139139

140140
/* One full block is needed */
141-
if (!fullsize)
142-
return TEE_ERROR_BAD_PARAMETERS;
141+
if (!fullsize) {
142+
ret = TEE_ERROR_BAD_PARAMETERS;
143+
goto out;
144+
}
143145

144146
if (lastblk)
145147
fullsize -= ctx->alg->size_block;

core/drivers/crypto/caam/cipher/local.h

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ struct cipherdata {
5656
* @encrypt Encrypt or decrypt direction
5757
* @src Source data to encrypt/decrypt
5858
* @dst [out] Destination data encrypted/decrypted
59+
* @blockbuf Saved block during previous streaming update
5960
*/
6061
enum caam_status caam_cipher_block(struct cipherdata *ctx, bool savectx,
6162
uint8_t keyid, bool encrypt,

core/drivers/crypto/caam/include/caam_trace.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#define DBG_TRACE_RNG BIT32(6) /* RNG trace */
3535
#define DBG_TRACE_HASH BIT32(7) /* Hash trace */
3636
#define DBG_TRACE_RSA BIT32(8) /* RSA trace */
37-
#define DBG_TRACE_CIPHER BIT32(9) /* Cipher dump Buffer */
37+
#define DBG_TRACE_CIPHER BIT32(9) /* Cipher dump Buffer */
3838

3939
/* HAL */
4040
#if CAAM_DBG_TRACE(HAL)

0 commit comments

Comments
 (0)