aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPascal van Leeuwen <pascalvanl@gmail.com>2019-08-30 03:43:01 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2019-09-05 00:37:01 -0400
commitd2d9e6fd6d1cd9bd0654c9f7ed2317a7fd220aef (patch)
treeda8f0c7581e430b424c4ed29ed6686e2a7336c7a
parent493e289ca8aab8f9408376d6388ac8f819de6b73 (diff)
crypto: inside-secure - Minor code cleanup and optimizations
Some minor cleanup changing e.g. "if (!x) A else B" to "if (x) B else A", merging some back-to-back if's with the same condition, collapsing some back-to-back assignments to the same variable and replacing some weird assignments with proper symbolics. Signed-off-by: Pascal van Leeuwen <pvanleeuwen@verimatrix.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--drivers/crypto/inside-secure/safexcel_cipher.c86
1 files changed, 47 insertions, 39 deletions
diff --git a/drivers/crypto/inside-secure/safexcel_cipher.c b/drivers/crypto/inside-secure/safexcel_cipher.c
index 18115dac9d01..3c2b1f759dad 100644
--- a/drivers/crypto/inside-secure/safexcel_cipher.c
+++ b/drivers/crypto/inside-secure/safexcel_cipher.c
@@ -126,9 +126,6 @@ static void safexcel_aead_token(struct safexcel_cipher_ctx *ctx, u8 *iv,
126 126
127 safexcel_cipher_token(ctx, iv, cdesc); 127 safexcel_cipher_token(ctx, iv, cdesc);
128 128
129 if (direction == SAFEXCEL_DECRYPT)
130 cryptlen -= digestsize;
131
132 if (direction == SAFEXCEL_ENCRYPT) { 129 if (direction == SAFEXCEL_ENCRYPT) {
133 /* align end of instruction sequence to end of token */ 130 /* align end of instruction sequence to end of token */
134 token = (struct safexcel_token *)(cdesc->control_data.token + 131 token = (struct safexcel_token *)(cdesc->control_data.token +
@@ -141,6 +138,8 @@ static void safexcel_aead_token(struct safexcel_cipher_ctx *ctx, u8 *iv,
141 token[2].instructions = EIP197_TOKEN_INS_TYPE_OUTPUT | 138 token[2].instructions = EIP197_TOKEN_INS_TYPE_OUTPUT |
142 EIP197_TOKEN_INS_INSERT_HASH_DIGEST; 139 EIP197_TOKEN_INS_INSERT_HASH_DIGEST;
143 } else { 140 } else {
141 cryptlen -= digestsize;
142
144 /* align end of instruction sequence to end of token */ 143 /* align end of instruction sequence to end of token */
145 token = (struct safexcel_token *)(cdesc->control_data.token + 144 token = (struct safexcel_token *)(cdesc->control_data.token +
146 EIP197_MAX_TOKENS - 4); 145 EIP197_MAX_TOKENS - 4);
@@ -159,13 +158,7 @@ static void safexcel_aead_token(struct safexcel_cipher_ctx *ctx, u8 *iv,
159 token[3].instructions = EIP197_TOKEN_INS_TYPE_OUTPUT; 158 token[3].instructions = EIP197_TOKEN_INS_TYPE_OUTPUT;
160 } 159 }
161 160
162 if (unlikely(!cryptlen)) { 161 if (likely(cryptlen)) {
163 token[1].opcode = EIP197_TOKEN_OPCODE_DIRECTION;
164 token[1].packet_length = assoclen;
165 token[1].stat = EIP197_TOKEN_STAT_LAST_HASH;
166 token[1].instructions = EIP197_TOKEN_INS_LAST |
167 EIP197_TOKEN_INS_TYPE_HASH;
168 } else {
169 if (likely(assoclen)) { 162 if (likely(assoclen)) {
170 token[0].opcode = EIP197_TOKEN_OPCODE_DIRECTION; 163 token[0].opcode = EIP197_TOKEN_OPCODE_DIRECTION;
171 token[0].packet_length = assoclen; 164 token[0].packet_length = assoclen;
@@ -179,6 +172,12 @@ static void safexcel_aead_token(struct safexcel_cipher_ctx *ctx, u8 *iv,
179 EIP197_TOKEN_INS_TYPE_CRYPTO | 172 EIP197_TOKEN_INS_TYPE_CRYPTO |
180 EIP197_TOKEN_INS_TYPE_HASH | 173 EIP197_TOKEN_INS_TYPE_HASH |
181 EIP197_TOKEN_INS_TYPE_OUTPUT; 174 EIP197_TOKEN_INS_TYPE_OUTPUT;
175 } else {
176 token[1].opcode = EIP197_TOKEN_OPCODE_DIRECTION;
177 token[1].packet_length = assoclen;
178 token[1].stat = EIP197_TOKEN_STAT_LAST_HASH;
179 token[1].instructions = EIP197_TOKEN_INS_LAST |
180 EIP197_TOKEN_INS_TYPE_HASH;
182 } 181 }
183} 182}
184 183
@@ -325,45 +324,60 @@ static int safexcel_context_control(struct safexcel_cipher_ctx *ctx,
325 struct safexcel_command_desc *cdesc) 324 struct safexcel_command_desc *cdesc)
326{ 325{
327 struct safexcel_crypto_priv *priv = ctx->priv; 326 struct safexcel_crypto_priv *priv = ctx->priv;
328 int ctrl_size; 327 int ctrl_size = ctx->key_len / sizeof(u32);
328
329 cdesc->control_data.control1 = ctx->mode;
329 330
330 if (ctx->aead) { 331 if (ctx->aead) {
332 /* Take in account the ipad+opad digests */
333 ctrl_size += ctx->state_sz / sizeof(u32) * 2;
334
331 if (sreq->direction == SAFEXCEL_ENCRYPT) 335 if (sreq->direction == SAFEXCEL_ENCRYPT)
332 cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_ENCRYPT_HASH_OUT; 336 cdesc->control_data.control0 =
337 CONTEXT_CONTROL_TYPE_ENCRYPT_HASH_OUT |
338 CONTEXT_CONTROL_DIGEST_HMAC |
339 CONTEXT_CONTROL_KEY_EN |
340 ctx->hash_alg |
341 CONTEXT_CONTROL_SIZE(ctrl_size);
333 else 342 else
334 cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_HASH_DECRYPT_IN; 343 cdesc->control_data.control0 =
344 CONTEXT_CONTROL_TYPE_HASH_DECRYPT_IN |
345 CONTEXT_CONTROL_DIGEST_HMAC |
346 CONTEXT_CONTROL_KEY_EN |
347 ctx->hash_alg |
348 CONTEXT_CONTROL_SIZE(ctrl_size);
335 } else { 349 } else {
336 cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_CRYPTO_OUT; 350 if (sreq->direction == SAFEXCEL_ENCRYPT)
337 351 cdesc->control_data.control0 =
338 /* The decryption control type is a combination of the 352 CONTEXT_CONTROL_TYPE_CRYPTO_OUT |
339 * encryption type and CONTEXT_CONTROL_TYPE_NULL_IN, for all 353 CONTEXT_CONTROL_KEY_EN |
340 * types. 354 CONTEXT_CONTROL_SIZE(ctrl_size);
341 */ 355 else
342 if (sreq->direction == SAFEXCEL_DECRYPT) 356 cdesc->control_data.control0 =
343 cdesc->control_data.control0 |= CONTEXT_CONTROL_TYPE_NULL_IN; 357 CONTEXT_CONTROL_TYPE_CRYPTO_IN |
358 CONTEXT_CONTROL_KEY_EN |
359 CONTEXT_CONTROL_SIZE(ctrl_size);
344 } 360 }
345 361
346 cdesc->control_data.control0 |= CONTEXT_CONTROL_KEY_EN;
347 cdesc->control_data.control1 |= ctx->mode;
348
349 if (ctx->aead)
350 cdesc->control_data.control0 |= CONTEXT_CONTROL_DIGEST_HMAC |
351 ctx->hash_alg;
352
353 if (ctx->alg == SAFEXCEL_DES) { 362 if (ctx->alg == SAFEXCEL_DES) {
354 cdesc->control_data.control0 |= CONTEXT_CONTROL_CRYPTO_ALG_DES; 363 cdesc->control_data.control0 |=
364 CONTEXT_CONTROL_CRYPTO_ALG_DES;
355 } else if (ctx->alg == SAFEXCEL_3DES) { 365 } else if (ctx->alg == SAFEXCEL_3DES) {
356 cdesc->control_data.control0 |= CONTEXT_CONTROL_CRYPTO_ALG_3DES; 366 cdesc->control_data.control0 |=
367 CONTEXT_CONTROL_CRYPTO_ALG_3DES;
357 } else if (ctx->alg == SAFEXCEL_AES) { 368 } else if (ctx->alg == SAFEXCEL_AES) {
358 switch (ctx->key_len >> ctx->xts) { 369 switch (ctx->key_len >> ctx->xts) {
359 case AES_KEYSIZE_128: 370 case AES_KEYSIZE_128:
360 cdesc->control_data.control0 |= CONTEXT_CONTROL_CRYPTO_ALG_AES128; 371 cdesc->control_data.control0 |=
372 CONTEXT_CONTROL_CRYPTO_ALG_AES128;
361 break; 373 break;
362 case AES_KEYSIZE_192: 374 case AES_KEYSIZE_192:
363 cdesc->control_data.control0 |= CONTEXT_CONTROL_CRYPTO_ALG_AES192; 375 cdesc->control_data.control0 |=
376 CONTEXT_CONTROL_CRYPTO_ALG_AES192;
364 break; 377 break;
365 case AES_KEYSIZE_256: 378 case AES_KEYSIZE_256:
366 cdesc->control_data.control0 |= CONTEXT_CONTROL_CRYPTO_ALG_AES256; 379 cdesc->control_data.control0 |=
380 CONTEXT_CONTROL_CRYPTO_ALG_AES256;
367 break; 381 break;
368 default: 382 default:
369 dev_err(priv->dev, "aes keysize not supported: %u\n", 383 dev_err(priv->dev, "aes keysize not supported: %u\n",
@@ -372,12 +386,6 @@ static int safexcel_context_control(struct safexcel_cipher_ctx *ctx,
372 } 386 }
373 } 387 }
374 388
375 ctrl_size = ctx->key_len / sizeof(u32);
376 if (ctx->aead)
377 /* Take in account the ipad+opad digests */
378 ctrl_size += ctx->state_sz / sizeof(u32) * 2;
379 cdesc->control_data.control0 |= CONTEXT_CONTROL_SIZE(ctrl_size);
380
381 return 0; 389 return 0;
382} 390}
383 391