1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <crypto/algapi.h>
7 #include <crypto/internal/aead.h>
8 #include <crypto/authenc.h>
9 #include <crypto/gcm.h>
10 #include <linux/rtnetlink.h>
11 #include <crypto/internal/des.h>
12 #include "cc_driver.h"
13 #include "cc_buffer_mgr.h"
15 #include "cc_request_mgr.h"
17 #include "cc_sram_mgr.h"
19 #define template_aead template_u.aead
21 #define MAX_AEAD_SETKEY_SEQ 12
22 #define MAX_AEAD_PROCESS_SEQ 23
24 #define MAX_HMAC_DIGEST_SIZE (SHA256_DIGEST_SIZE)
25 #define MAX_HMAC_BLOCK_SIZE (SHA256_BLOCK_SIZE)
27 #define MAX_NONCE_SIZE CTR_RFC3686_NONCE_SIZE
29 struct cc_aead_handle
{
30 u32 sram_workspace_addr
;
31 struct list_head aead_list
;
36 u8
*ipad_opad
; /* IPAD, OPAD*/
37 dma_addr_t padded_authkey_dma_addr
;
38 dma_addr_t ipad_opad_dma_addr
;
42 u8
*xcbc_keys
; /* K1,K2,K3 */
43 dma_addr_t xcbc_keys_dma_addr
;
47 struct cc_drvdata
*drvdata
;
48 u8 ctr_nonce
[MAX_NONCE_SIZE
]; /* used for ctr3686 iv and aes ccm */
50 dma_addr_t enckey_dma_addr
;
52 struct cc_hmac_s hmac
;
53 struct cc_xcbc_s xcbc
;
55 unsigned int enc_keylen
;
56 unsigned int auth_keylen
;
57 unsigned int authsize
; /* Actual (reduced?) size of the MAC/ICv */
58 unsigned int hash_len
;
59 enum drv_cipher_mode cipher_mode
;
60 enum cc_flow_mode flow_mode
;
61 enum drv_hash_mode auth_mode
;
64 static void cc_aead_exit(struct crypto_aead
*tfm
)
66 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
67 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
69 dev_dbg(dev
, "Clearing context @%p for %s\n", crypto_aead_ctx(tfm
),
70 crypto_tfm_alg_name(&tfm
->base
));
72 /* Unmap enckey buffer */
74 dma_free_coherent(dev
, AES_MAX_KEY_SIZE
, ctx
->enckey
,
75 ctx
->enckey_dma_addr
);
76 dev_dbg(dev
, "Freed enckey DMA buffer enckey_dma_addr=%pad\n",
77 &ctx
->enckey_dma_addr
);
78 ctx
->enckey_dma_addr
= 0;
82 if (ctx
->auth_mode
== DRV_HASH_XCBC_MAC
) { /* XCBC authetication */
83 struct cc_xcbc_s
*xcbc
= &ctx
->auth_state
.xcbc
;
85 if (xcbc
->xcbc_keys
) {
86 dma_free_coherent(dev
, CC_AES_128_BIT_KEY_SIZE
* 3,
88 xcbc
->xcbc_keys_dma_addr
);
90 dev_dbg(dev
, "Freed xcbc_keys DMA buffer xcbc_keys_dma_addr=%pad\n",
91 &xcbc
->xcbc_keys_dma_addr
);
92 xcbc
->xcbc_keys_dma_addr
= 0;
93 xcbc
->xcbc_keys
= NULL
;
94 } else if (ctx
->auth_mode
!= DRV_HASH_NULL
) { /* HMAC auth. */
95 struct cc_hmac_s
*hmac
= &ctx
->auth_state
.hmac
;
97 if (hmac
->ipad_opad
) {
98 dma_free_coherent(dev
, 2 * MAX_HMAC_DIGEST_SIZE
,
100 hmac
->ipad_opad_dma_addr
);
101 dev_dbg(dev
, "Freed ipad_opad DMA buffer ipad_opad_dma_addr=%pad\n",
102 &hmac
->ipad_opad_dma_addr
);
103 hmac
->ipad_opad_dma_addr
= 0;
104 hmac
->ipad_opad
= NULL
;
106 if (hmac
->padded_authkey
) {
107 dma_free_coherent(dev
, MAX_HMAC_BLOCK_SIZE
,
108 hmac
->padded_authkey
,
109 hmac
->padded_authkey_dma_addr
);
110 dev_dbg(dev
, "Freed padded_authkey DMA buffer padded_authkey_dma_addr=%pad\n",
111 &hmac
->padded_authkey_dma_addr
);
112 hmac
->padded_authkey_dma_addr
= 0;
113 hmac
->padded_authkey
= NULL
;
118 static unsigned int cc_get_aead_hash_len(struct crypto_aead
*tfm
)
120 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
122 return cc_get_default_hash_len(ctx
->drvdata
);
125 static int cc_aead_init(struct crypto_aead
*tfm
)
127 struct aead_alg
*alg
= crypto_aead_alg(tfm
);
128 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
129 struct cc_crypto_alg
*cc_alg
=
130 container_of(alg
, struct cc_crypto_alg
, aead_alg
);
131 struct device
*dev
= drvdata_to_dev(cc_alg
->drvdata
);
133 dev_dbg(dev
, "Initializing context @%p for %s\n", ctx
,
134 crypto_tfm_alg_name(&tfm
->base
));
136 /* Initialize modes in instance */
137 ctx
->cipher_mode
= cc_alg
->cipher_mode
;
138 ctx
->flow_mode
= cc_alg
->flow_mode
;
139 ctx
->auth_mode
= cc_alg
->auth_mode
;
140 ctx
->drvdata
= cc_alg
->drvdata
;
141 crypto_aead_set_reqsize_dma(tfm
, sizeof(struct aead_req_ctx
));
143 /* Allocate key buffer, cache line aligned */
144 ctx
->enckey
= dma_alloc_coherent(dev
, AES_MAX_KEY_SIZE
,
145 &ctx
->enckey_dma_addr
, GFP_KERNEL
);
147 dev_err(dev
, "Failed allocating key buffer\n");
150 dev_dbg(dev
, "Allocated enckey buffer in context ctx->enckey=@%p\n",
153 /* Set default authlen value */
155 if (ctx
->auth_mode
== DRV_HASH_XCBC_MAC
) { /* XCBC authetication */
156 struct cc_xcbc_s
*xcbc
= &ctx
->auth_state
.xcbc
;
157 const unsigned int key_size
= CC_AES_128_BIT_KEY_SIZE
* 3;
159 /* Allocate dma-coherent buffer for XCBC's K1+K2+K3 */
160 /* (and temporary for user key - up to 256b) */
161 xcbc
->xcbc_keys
= dma_alloc_coherent(dev
, key_size
,
162 &xcbc
->xcbc_keys_dma_addr
,
164 if (!xcbc
->xcbc_keys
) {
165 dev_err(dev
, "Failed allocating buffer for XCBC keys\n");
168 } else if (ctx
->auth_mode
!= DRV_HASH_NULL
) { /* HMAC authentication */
169 struct cc_hmac_s
*hmac
= &ctx
->auth_state
.hmac
;
170 const unsigned int digest_size
= 2 * MAX_HMAC_DIGEST_SIZE
;
171 dma_addr_t
*pkey_dma
= &hmac
->padded_authkey_dma_addr
;
173 /* Allocate dma-coherent buffer for IPAD + OPAD */
174 hmac
->ipad_opad
= dma_alloc_coherent(dev
, digest_size
,
175 &hmac
->ipad_opad_dma_addr
,
178 if (!hmac
->ipad_opad
) {
179 dev_err(dev
, "Failed allocating IPAD/OPAD buffer\n");
183 dev_dbg(dev
, "Allocated authkey buffer in context ctx->authkey=@%p\n",
186 hmac
->padded_authkey
= dma_alloc_coherent(dev
,
191 if (!hmac
->padded_authkey
) {
192 dev_err(dev
, "failed to allocate padded_authkey\n");
196 ctx
->auth_state
.hmac
.ipad_opad
= NULL
;
197 ctx
->auth_state
.hmac
.padded_authkey
= NULL
;
199 ctx
->hash_len
= cc_get_aead_hash_len(tfm
);
208 static void cc_aead_complete(struct device
*dev
, void *cc_req
, int err
)
210 struct aead_request
*areq
= (struct aead_request
*)cc_req
;
211 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(areq
);
212 struct crypto_aead
*tfm
= crypto_aead_reqtfm(cc_req
);
213 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
215 /* BACKLOG notification */
216 if (err
== -EINPROGRESS
)
219 cc_unmap_aead_request(dev
, areq
);
221 /* Restore ordinary iv pointer */
222 areq
->iv
= areq_ctx
->backup_iv
;
227 if (areq_ctx
->gen_ctx
.op_type
== DRV_CRYPTO_DIRECTION_DECRYPT
) {
228 if (memcmp(areq_ctx
->mac_buf
, areq_ctx
->icv_virt_addr
,
229 ctx
->authsize
) != 0) {
230 dev_dbg(dev
, "Payload authentication failure, (auth-size=%d, cipher=%d)\n",
231 ctx
->authsize
, ctx
->cipher_mode
);
232 /* In case of payload authentication failure, MUST NOT
233 * revealed the decrypted message --> zero its memory.
235 sg_zero_buffer(areq
->dst
, sg_nents(areq
->dst
),
236 areq
->cryptlen
, areq
->assoclen
);
240 } else if (areq_ctx
->is_icv_fragmented
) {
241 u32 skip
= areq
->cryptlen
+ areq_ctx
->dst_offset
;
243 cc_copy_sg_portion(dev
, areq_ctx
->mac_buf
, areq_ctx
->dst_sgl
,
244 skip
, (skip
+ ctx
->authsize
),
248 aead_request_complete(areq
, err
);
251 static unsigned int xcbc_setkey(struct cc_hw_desc
*desc
,
252 struct cc_aead_ctx
*ctx
)
254 /* Load the AES key */
255 hw_desc_init(&desc
[0]);
256 /* We are using for the source/user key the same buffer
257 * as for the output keys, * because after this key loading it
258 * is not needed anymore
260 set_din_type(&desc
[0], DMA_DLLI
,
261 ctx
->auth_state
.xcbc
.xcbc_keys_dma_addr
, ctx
->auth_keylen
,
263 set_cipher_mode(&desc
[0], DRV_CIPHER_ECB
);
264 set_cipher_config0(&desc
[0], DRV_CRYPTO_DIRECTION_ENCRYPT
);
265 set_key_size_aes(&desc
[0], ctx
->auth_keylen
);
266 set_flow_mode(&desc
[0], S_DIN_to_AES
);
267 set_setup_mode(&desc
[0], SETUP_LOAD_KEY0
);
269 hw_desc_init(&desc
[1]);
270 set_din_const(&desc
[1], 0x01010101, CC_AES_128_BIT_KEY_SIZE
);
271 set_flow_mode(&desc
[1], DIN_AES_DOUT
);
272 set_dout_dlli(&desc
[1], ctx
->auth_state
.xcbc
.xcbc_keys_dma_addr
,
273 AES_KEYSIZE_128
, NS_BIT
, 0);
275 hw_desc_init(&desc
[2]);
276 set_din_const(&desc
[2], 0x02020202, CC_AES_128_BIT_KEY_SIZE
);
277 set_flow_mode(&desc
[2], DIN_AES_DOUT
);
278 set_dout_dlli(&desc
[2], (ctx
->auth_state
.xcbc
.xcbc_keys_dma_addr
280 AES_KEYSIZE_128
, NS_BIT
, 0);
282 hw_desc_init(&desc
[3]);
283 set_din_const(&desc
[3], 0x03030303, CC_AES_128_BIT_KEY_SIZE
);
284 set_flow_mode(&desc
[3], DIN_AES_DOUT
);
285 set_dout_dlli(&desc
[3], (ctx
->auth_state
.xcbc
.xcbc_keys_dma_addr
286 + 2 * AES_KEYSIZE_128
),
287 AES_KEYSIZE_128
, NS_BIT
, 0);
292 static unsigned int hmac_setkey(struct cc_hw_desc
*desc
,
293 struct cc_aead_ctx
*ctx
)
295 unsigned int hmac_pad_const
[2] = { HMAC_IPAD_CONST
, HMAC_OPAD_CONST
};
296 unsigned int digest_ofs
= 0;
297 unsigned int hash_mode
= (ctx
->auth_mode
== DRV_HASH_SHA1
) ?
298 DRV_HASH_HW_SHA1
: DRV_HASH_HW_SHA256
;
299 unsigned int digest_size
= (ctx
->auth_mode
== DRV_HASH_SHA1
) ?
300 CC_SHA1_DIGEST_SIZE
: CC_SHA256_DIGEST_SIZE
;
301 struct cc_hmac_s
*hmac
= &ctx
->auth_state
.hmac
;
303 unsigned int idx
= 0;
306 /* calc derived HMAC key */
307 for (i
= 0; i
< 2; i
++) {
308 /* Load hash initial state */
309 hw_desc_init(&desc
[idx
]);
310 set_cipher_mode(&desc
[idx
], hash_mode
);
311 set_din_sram(&desc
[idx
],
312 cc_larval_digest_addr(ctx
->drvdata
,
315 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
316 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE0
);
319 /* Load the hash current length*/
320 hw_desc_init(&desc
[idx
]);
321 set_cipher_mode(&desc
[idx
], hash_mode
);
322 set_din_const(&desc
[idx
], 0, ctx
->hash_len
);
323 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
324 set_setup_mode(&desc
[idx
], SETUP_LOAD_KEY0
);
327 /* Prepare ipad key */
328 hw_desc_init(&desc
[idx
]);
329 set_xor_val(&desc
[idx
], hmac_pad_const
[i
]);
330 set_cipher_mode(&desc
[idx
], hash_mode
);
331 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
332 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE1
);
335 /* Perform HASH update */
336 hw_desc_init(&desc
[idx
]);
337 set_din_type(&desc
[idx
], DMA_DLLI
,
338 hmac
->padded_authkey_dma_addr
,
339 SHA256_BLOCK_SIZE
, NS_BIT
);
340 set_cipher_mode(&desc
[idx
], hash_mode
);
341 set_xor_active(&desc
[idx
]);
342 set_flow_mode(&desc
[idx
], DIN_HASH
);
346 hw_desc_init(&desc
[idx
]);
347 set_cipher_mode(&desc
[idx
], hash_mode
);
348 set_dout_dlli(&desc
[idx
],
349 (hmac
->ipad_opad_dma_addr
+ digest_ofs
),
350 digest_size
, NS_BIT
, 0);
351 set_flow_mode(&desc
[idx
], S_HASH_to_DOUT
);
352 set_setup_mode(&desc
[idx
], SETUP_WRITE_STATE0
);
353 set_cipher_config1(&desc
[idx
], HASH_PADDING_DISABLED
);
356 digest_ofs
+= digest_size
;
362 static int validate_keys_sizes(struct cc_aead_ctx
*ctx
)
364 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
366 dev_dbg(dev
, "enc_keylen=%u authkeylen=%u\n",
367 ctx
->enc_keylen
, ctx
->auth_keylen
);
369 switch (ctx
->auth_mode
) {
371 case DRV_HASH_SHA256
:
373 case DRV_HASH_XCBC_MAC
:
374 if (ctx
->auth_keylen
!= AES_KEYSIZE_128
&&
375 ctx
->auth_keylen
!= AES_KEYSIZE_192
&&
376 ctx
->auth_keylen
!= AES_KEYSIZE_256
)
379 case DRV_HASH_NULL
: /* Not authenc (e.g., CCM) - no auth_key) */
380 if (ctx
->auth_keylen
> 0)
384 dev_dbg(dev
, "Invalid auth_mode=%d\n", ctx
->auth_mode
);
387 /* Check cipher key size */
388 if (ctx
->flow_mode
== S_DIN_to_DES
) {
389 if (ctx
->enc_keylen
!= DES3_EDE_KEY_SIZE
) {
390 dev_dbg(dev
, "Invalid cipher(3DES) key size: %u\n",
394 } else { /* Default assumed to be AES ciphers */
395 if (ctx
->enc_keylen
!= AES_KEYSIZE_128
&&
396 ctx
->enc_keylen
!= AES_KEYSIZE_192
&&
397 ctx
->enc_keylen
!= AES_KEYSIZE_256
) {
398 dev_dbg(dev
, "Invalid cipher(AES) key size: %u\n",
404 return 0; /* All tests of keys sizes passed */
407 /* This function prepers the user key so it can pass to the hmac processing
408 * (copy to intenral buffer or hash in case of key longer than block
410 static int cc_get_plain_hmac_key(struct crypto_aead
*tfm
, const u8
*authkey
,
413 dma_addr_t key_dma_addr
= 0;
414 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
415 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
417 struct cc_crypto_req cc_req
= {};
418 unsigned int blocksize
;
419 unsigned int digestsize
;
420 unsigned int hashmode
;
421 unsigned int idx
= 0;
424 struct cc_hw_desc desc
[MAX_AEAD_SETKEY_SEQ
];
425 dma_addr_t padded_authkey_dma_addr
=
426 ctx
->auth_state
.hmac
.padded_authkey_dma_addr
;
428 switch (ctx
->auth_mode
) { /* auth_key required and >0 */
430 blocksize
= SHA1_BLOCK_SIZE
;
431 digestsize
= SHA1_DIGEST_SIZE
;
432 hashmode
= DRV_HASH_HW_SHA1
;
434 case DRV_HASH_SHA256
:
436 blocksize
= SHA256_BLOCK_SIZE
;
437 digestsize
= SHA256_DIGEST_SIZE
;
438 hashmode
= DRV_HASH_HW_SHA256
;
443 key
= kmemdup(authkey
, keylen
, GFP_KERNEL
);
447 key_dma_addr
= dma_map_single(dev
, key
, keylen
, DMA_TO_DEVICE
);
448 if (dma_mapping_error(dev
, key_dma_addr
)) {
449 dev_err(dev
, "Mapping key va=0x%p len=%u for DMA failed\n",
451 kfree_sensitive(key
);
454 if (keylen
> blocksize
) {
455 /* Load hash initial state */
456 hw_desc_init(&desc
[idx
]);
457 set_cipher_mode(&desc
[idx
], hashmode
);
458 larval_addr
= cc_larval_digest_addr(ctx
->drvdata
,
460 set_din_sram(&desc
[idx
], larval_addr
, digestsize
);
461 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
462 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE0
);
465 /* Load the hash current length*/
466 hw_desc_init(&desc
[idx
]);
467 set_cipher_mode(&desc
[idx
], hashmode
);
468 set_din_const(&desc
[idx
], 0, ctx
->hash_len
);
469 set_cipher_config1(&desc
[idx
], HASH_PADDING_ENABLED
);
470 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
471 set_setup_mode(&desc
[idx
], SETUP_LOAD_KEY0
);
474 hw_desc_init(&desc
[idx
]);
475 set_din_type(&desc
[idx
], DMA_DLLI
,
476 key_dma_addr
, keylen
, NS_BIT
);
477 set_flow_mode(&desc
[idx
], DIN_HASH
);
481 hw_desc_init(&desc
[idx
]);
482 set_cipher_mode(&desc
[idx
], hashmode
);
483 set_dout_dlli(&desc
[idx
], padded_authkey_dma_addr
,
484 digestsize
, NS_BIT
, 0);
485 set_flow_mode(&desc
[idx
], S_HASH_to_DOUT
);
486 set_setup_mode(&desc
[idx
], SETUP_WRITE_STATE0
);
487 set_cipher_config1(&desc
[idx
], HASH_PADDING_DISABLED
);
488 set_cipher_config0(&desc
[idx
],
489 HASH_DIGEST_RESULT_LITTLE_ENDIAN
);
492 hw_desc_init(&desc
[idx
]);
493 set_din_const(&desc
[idx
], 0, (blocksize
- digestsize
));
494 set_flow_mode(&desc
[idx
], BYPASS
);
495 set_dout_dlli(&desc
[idx
], (padded_authkey_dma_addr
+
496 digestsize
), (blocksize
- digestsize
),
500 hw_desc_init(&desc
[idx
]);
501 set_din_type(&desc
[idx
], DMA_DLLI
, key_dma_addr
,
503 set_flow_mode(&desc
[idx
], BYPASS
);
504 set_dout_dlli(&desc
[idx
], padded_authkey_dma_addr
,
508 if ((blocksize
- keylen
) != 0) {
509 hw_desc_init(&desc
[idx
]);
510 set_din_const(&desc
[idx
], 0,
511 (blocksize
- keylen
));
512 set_flow_mode(&desc
[idx
], BYPASS
);
513 set_dout_dlli(&desc
[idx
],
514 (padded_authkey_dma_addr
+
516 (blocksize
- keylen
), NS_BIT
, 0);
521 hw_desc_init(&desc
[idx
]);
522 set_din_const(&desc
[idx
], 0, (blocksize
- keylen
));
523 set_flow_mode(&desc
[idx
], BYPASS
);
524 set_dout_dlli(&desc
[idx
], padded_authkey_dma_addr
,
525 blocksize
, NS_BIT
, 0);
529 rc
= cc_send_sync_request(ctx
->drvdata
, &cc_req
, desc
, idx
);
531 dev_err(dev
, "send_request() failed (rc=%d)\n", rc
);
534 dma_unmap_single(dev
, key_dma_addr
, keylen
, DMA_TO_DEVICE
);
536 kfree_sensitive(key
);
541 static int cc_aead_setkey(struct crypto_aead
*tfm
, const u8
*key
,
544 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
545 struct cc_crypto_req cc_req
= {};
546 struct cc_hw_desc desc
[MAX_AEAD_SETKEY_SEQ
];
547 unsigned int seq_len
= 0;
548 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
549 const u8
*enckey
, *authkey
;
552 dev_dbg(dev
, "Setting key in context @%p for %s. key=%p keylen=%u\n",
553 ctx
, crypto_tfm_alg_name(crypto_aead_tfm(tfm
)), key
, keylen
);
555 /* STAT_PHASE_0: Init and sanity checks */
557 if (ctx
->auth_mode
!= DRV_HASH_NULL
) { /* authenc() alg. */
558 struct crypto_authenc_keys keys
;
560 rc
= crypto_authenc_extractkeys(&keys
, key
, keylen
);
563 enckey
= keys
.enckey
;
564 authkey
= keys
.authkey
;
565 ctx
->enc_keylen
= keys
.enckeylen
;
566 ctx
->auth_keylen
= keys
.authkeylen
;
568 if (ctx
->cipher_mode
== DRV_CIPHER_CTR
) {
569 /* the nonce is stored in bytes at end of key */
570 if (ctx
->enc_keylen
<
571 (AES_MIN_KEY_SIZE
+ CTR_RFC3686_NONCE_SIZE
))
573 /* Copy nonce from last 4 bytes in CTR key to
574 * first 4 bytes in CTR IV
576 memcpy(ctx
->ctr_nonce
, enckey
+ ctx
->enc_keylen
-
577 CTR_RFC3686_NONCE_SIZE
, CTR_RFC3686_NONCE_SIZE
);
578 /* Set CTR key size */
579 ctx
->enc_keylen
-= CTR_RFC3686_NONCE_SIZE
;
581 } else { /* non-authenc - has just one key */
584 ctx
->enc_keylen
= keylen
;
585 ctx
->auth_keylen
= 0;
588 rc
= validate_keys_sizes(ctx
);
592 /* STAT_PHASE_1: Copy key to ctx */
594 /* Get key material */
595 memcpy(ctx
->enckey
, enckey
, ctx
->enc_keylen
);
596 if (ctx
->enc_keylen
== 24)
597 memset(ctx
->enckey
+ 24, 0, CC_AES_KEY_SIZE_MAX
- 24);
598 if (ctx
->auth_mode
== DRV_HASH_XCBC_MAC
) {
599 memcpy(ctx
->auth_state
.xcbc
.xcbc_keys
, authkey
,
601 } else if (ctx
->auth_mode
!= DRV_HASH_NULL
) { /* HMAC */
602 rc
= cc_get_plain_hmac_key(tfm
, authkey
, ctx
->auth_keylen
);
607 /* STAT_PHASE_2: Create sequence */
609 switch (ctx
->auth_mode
) {
611 case DRV_HASH_SHA256
:
612 seq_len
= hmac_setkey(desc
, ctx
);
614 case DRV_HASH_XCBC_MAC
:
615 seq_len
= xcbc_setkey(desc
, ctx
);
617 case DRV_HASH_NULL
: /* non-authenc modes, e.g., CCM */
618 break; /* No auth. key setup */
620 dev_err(dev
, "Unsupported authenc (%d)\n", ctx
->auth_mode
);
624 /* STAT_PHASE_3: Submit sequence to HW */
626 if (seq_len
> 0) { /* For CCM there is no sequence to setup the key */
627 rc
= cc_send_sync_request(ctx
->drvdata
, &cc_req
, desc
, seq_len
);
629 dev_err(dev
, "send_request() failed (rc=%d)\n", rc
);
634 /* Update STAT_PHASE_3 */
638 static int cc_des3_aead_setkey(struct crypto_aead
*aead
, const u8
*key
,
641 struct crypto_authenc_keys keys
;
644 err
= crypto_authenc_extractkeys(&keys
, key
, keylen
);
648 err
= verify_aead_des3_key(aead
, keys
.enckey
, keys
.enckeylen
) ?:
649 cc_aead_setkey(aead
, key
, keylen
);
651 memzero_explicit(&keys
, sizeof(keys
));
655 static int cc_rfc4309_ccm_setkey(struct crypto_aead
*tfm
, const u8
*key
,
658 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
664 memcpy(ctx
->ctr_nonce
, key
+ keylen
, 3);
666 return cc_aead_setkey(tfm
, key
, keylen
);
669 static int cc_aead_setauthsize(struct crypto_aead
*authenc
,
670 unsigned int authsize
)
672 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(authenc
);
673 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
675 /* Unsupported auth. sizes */
677 authsize
> crypto_aead_maxauthsize(authenc
)) {
681 ctx
->authsize
= authsize
;
682 dev_dbg(dev
, "authlen=%d\n", ctx
->authsize
);
687 static int cc_rfc4309_ccm_setauthsize(struct crypto_aead
*authenc
,
688 unsigned int authsize
)
699 return cc_aead_setauthsize(authenc
, authsize
);
702 static int cc_ccm_setauthsize(struct crypto_aead
*authenc
,
703 unsigned int authsize
)
718 return cc_aead_setauthsize(authenc
, authsize
);
721 static void cc_set_assoc_desc(struct aead_request
*areq
, unsigned int flow_mode
,
722 struct cc_hw_desc desc
[], unsigned int *seq_size
)
724 struct crypto_aead
*tfm
= crypto_aead_reqtfm(areq
);
725 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
726 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(areq
);
727 enum cc_req_dma_buf_type assoc_dma_type
= areq_ctx
->assoc_buff_type
;
728 unsigned int idx
= *seq_size
;
729 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
731 switch (assoc_dma_type
) {
732 case CC_DMA_BUF_DLLI
:
733 dev_dbg(dev
, "ASSOC buffer type DLLI\n");
734 hw_desc_init(&desc
[idx
]);
735 set_din_type(&desc
[idx
], DMA_DLLI
, sg_dma_address(areq
->src
),
736 areq_ctx
->assoclen
, NS_BIT
);
737 set_flow_mode(&desc
[idx
], flow_mode
);
738 if (ctx
->auth_mode
== DRV_HASH_XCBC_MAC
&&
739 areq_ctx
->cryptlen
> 0)
740 set_din_not_last_indication(&desc
[idx
]);
742 case CC_DMA_BUF_MLLI
:
743 dev_dbg(dev
, "ASSOC buffer type MLLI\n");
744 hw_desc_init(&desc
[idx
]);
745 set_din_type(&desc
[idx
], DMA_MLLI
, areq_ctx
->assoc
.sram_addr
,
746 areq_ctx
->assoc
.mlli_nents
, NS_BIT
);
747 set_flow_mode(&desc
[idx
], flow_mode
);
748 if (ctx
->auth_mode
== DRV_HASH_XCBC_MAC
&&
749 areq_ctx
->cryptlen
> 0)
750 set_din_not_last_indication(&desc
[idx
]);
752 case CC_DMA_BUF_NULL
:
754 dev_err(dev
, "Invalid ASSOC buffer type\n");
760 static void cc_proc_authen_desc(struct aead_request
*areq
,
761 unsigned int flow_mode
,
762 struct cc_hw_desc desc
[],
763 unsigned int *seq_size
, int direct
)
765 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(areq
);
766 enum cc_req_dma_buf_type data_dma_type
= areq_ctx
->data_buff_type
;
767 unsigned int idx
= *seq_size
;
768 struct crypto_aead
*tfm
= crypto_aead_reqtfm(areq
);
769 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
770 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
772 switch (data_dma_type
) {
773 case CC_DMA_BUF_DLLI
:
775 struct scatterlist
*cipher
=
776 (direct
== DRV_CRYPTO_DIRECTION_ENCRYPT
) ?
777 areq_ctx
->dst_sgl
: areq_ctx
->src_sgl
;
779 unsigned int offset
=
780 (direct
== DRV_CRYPTO_DIRECTION_ENCRYPT
) ?
781 areq_ctx
->dst_offset
: areq_ctx
->src_offset
;
782 dev_dbg(dev
, "AUTHENC: SRC/DST buffer type DLLI\n");
783 hw_desc_init(&desc
[idx
]);
784 set_din_type(&desc
[idx
], DMA_DLLI
,
785 (sg_dma_address(cipher
) + offset
),
786 areq_ctx
->cryptlen
, NS_BIT
);
787 set_flow_mode(&desc
[idx
], flow_mode
);
790 case CC_DMA_BUF_MLLI
:
792 /* DOUBLE-PASS flow (as default)
793 * assoc. + iv + data -compact in one table
794 * if assoclen is ZERO only IV perform
796 u32 mlli_addr
= areq_ctx
->assoc
.sram_addr
;
797 u32 mlli_nents
= areq_ctx
->assoc
.mlli_nents
;
799 if (areq_ctx
->is_single_pass
) {
800 if (direct
== DRV_CRYPTO_DIRECTION_ENCRYPT
) {
801 mlli_addr
= areq_ctx
->dst
.sram_addr
;
802 mlli_nents
= areq_ctx
->dst
.mlli_nents
;
804 mlli_addr
= areq_ctx
->src
.sram_addr
;
805 mlli_nents
= areq_ctx
->src
.mlli_nents
;
809 dev_dbg(dev
, "AUTHENC: SRC/DST buffer type MLLI\n");
810 hw_desc_init(&desc
[idx
]);
811 set_din_type(&desc
[idx
], DMA_MLLI
, mlli_addr
, mlli_nents
,
813 set_flow_mode(&desc
[idx
], flow_mode
);
816 case CC_DMA_BUF_NULL
:
818 dev_err(dev
, "AUTHENC: Invalid SRC/DST buffer type\n");
824 static void cc_proc_cipher_desc(struct aead_request
*areq
,
825 unsigned int flow_mode
,
826 struct cc_hw_desc desc
[],
827 unsigned int *seq_size
)
829 unsigned int idx
= *seq_size
;
830 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(areq
);
831 enum cc_req_dma_buf_type data_dma_type
= areq_ctx
->data_buff_type
;
832 struct crypto_aead
*tfm
= crypto_aead_reqtfm(areq
);
833 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
834 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
836 if (areq_ctx
->cryptlen
== 0)
837 return; /*null processing*/
839 switch (data_dma_type
) {
840 case CC_DMA_BUF_DLLI
:
841 dev_dbg(dev
, "CIPHER: SRC/DST buffer type DLLI\n");
842 hw_desc_init(&desc
[idx
]);
843 set_din_type(&desc
[idx
], DMA_DLLI
,
844 (sg_dma_address(areq_ctx
->src_sgl
) +
845 areq_ctx
->src_offset
), areq_ctx
->cryptlen
,
847 set_dout_dlli(&desc
[idx
],
848 (sg_dma_address(areq_ctx
->dst_sgl
) +
849 areq_ctx
->dst_offset
),
850 areq_ctx
->cryptlen
, NS_BIT
, 0);
851 set_flow_mode(&desc
[idx
], flow_mode
);
853 case CC_DMA_BUF_MLLI
:
854 dev_dbg(dev
, "CIPHER: SRC/DST buffer type MLLI\n");
855 hw_desc_init(&desc
[idx
]);
856 set_din_type(&desc
[idx
], DMA_MLLI
, areq_ctx
->src
.sram_addr
,
857 areq_ctx
->src
.mlli_nents
, NS_BIT
);
858 set_dout_mlli(&desc
[idx
], areq_ctx
->dst
.sram_addr
,
859 areq_ctx
->dst
.mlli_nents
, NS_BIT
, 0);
860 set_flow_mode(&desc
[idx
], flow_mode
);
862 case CC_DMA_BUF_NULL
:
864 dev_err(dev
, "CIPHER: Invalid SRC/DST buffer type\n");
870 static void cc_proc_digest_desc(struct aead_request
*req
,
871 struct cc_hw_desc desc
[],
872 unsigned int *seq_size
)
874 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
875 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
876 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
877 unsigned int idx
= *seq_size
;
878 unsigned int hash_mode
= (ctx
->auth_mode
== DRV_HASH_SHA1
) ?
879 DRV_HASH_HW_SHA1
: DRV_HASH_HW_SHA256
;
880 int direct
= req_ctx
->gen_ctx
.op_type
;
882 /* Get final ICV result */
883 if (direct
== DRV_CRYPTO_DIRECTION_ENCRYPT
) {
884 hw_desc_init(&desc
[idx
]);
885 set_flow_mode(&desc
[idx
], S_HASH_to_DOUT
);
886 set_setup_mode(&desc
[idx
], SETUP_WRITE_STATE0
);
887 set_dout_dlli(&desc
[idx
], req_ctx
->icv_dma_addr
, ctx
->authsize
,
889 set_queue_last_ind(ctx
->drvdata
, &desc
[idx
]);
890 if (ctx
->auth_mode
== DRV_HASH_XCBC_MAC
) {
891 set_aes_not_hash_mode(&desc
[idx
]);
892 set_cipher_mode(&desc
[idx
], DRV_CIPHER_XCBC_MAC
);
894 set_cipher_config0(&desc
[idx
],
895 HASH_DIGEST_RESULT_LITTLE_ENDIAN
);
896 set_cipher_mode(&desc
[idx
], hash_mode
);
899 /* Get ICV out from hardware */
900 hw_desc_init(&desc
[idx
]);
901 set_setup_mode(&desc
[idx
], SETUP_WRITE_STATE0
);
902 set_flow_mode(&desc
[idx
], S_HASH_to_DOUT
);
903 set_dout_dlli(&desc
[idx
], req_ctx
->mac_buf_dma_addr
,
904 ctx
->authsize
, NS_BIT
, 1);
905 set_queue_last_ind(ctx
->drvdata
, &desc
[idx
]);
906 set_cipher_config0(&desc
[idx
],
907 HASH_DIGEST_RESULT_LITTLE_ENDIAN
);
908 set_cipher_config1(&desc
[idx
], HASH_PADDING_DISABLED
);
909 if (ctx
->auth_mode
== DRV_HASH_XCBC_MAC
) {
910 set_cipher_mode(&desc
[idx
], DRV_CIPHER_XCBC_MAC
);
911 set_aes_not_hash_mode(&desc
[idx
]);
913 set_cipher_mode(&desc
[idx
], hash_mode
);
920 static void cc_set_cipher_desc(struct aead_request
*req
,
921 struct cc_hw_desc desc
[],
922 unsigned int *seq_size
)
924 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
925 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
926 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
927 unsigned int hw_iv_size
= req_ctx
->hw_iv_size
;
928 unsigned int idx
= *seq_size
;
929 int direct
= req_ctx
->gen_ctx
.op_type
;
931 /* Setup cipher state */
932 hw_desc_init(&desc
[idx
]);
933 set_cipher_config0(&desc
[idx
], direct
);
934 set_flow_mode(&desc
[idx
], ctx
->flow_mode
);
935 set_din_type(&desc
[idx
], DMA_DLLI
, req_ctx
->gen_ctx
.iv_dma_addr
,
937 if (ctx
->cipher_mode
== DRV_CIPHER_CTR
)
938 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE1
);
940 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE0
);
941 set_cipher_mode(&desc
[idx
], ctx
->cipher_mode
);
945 hw_desc_init(&desc
[idx
]);
946 set_cipher_config0(&desc
[idx
], direct
);
947 set_setup_mode(&desc
[idx
], SETUP_LOAD_KEY0
);
948 set_flow_mode(&desc
[idx
], ctx
->flow_mode
);
949 if (ctx
->flow_mode
== S_DIN_to_AES
) {
950 set_din_type(&desc
[idx
], DMA_DLLI
, ctx
->enckey_dma_addr
,
951 ((ctx
->enc_keylen
== 24) ? CC_AES_KEY_SIZE_MAX
:
952 ctx
->enc_keylen
), NS_BIT
);
953 set_key_size_aes(&desc
[idx
], ctx
->enc_keylen
);
955 set_din_type(&desc
[idx
], DMA_DLLI
, ctx
->enckey_dma_addr
,
956 ctx
->enc_keylen
, NS_BIT
);
957 set_key_size_des(&desc
[idx
], ctx
->enc_keylen
);
959 set_cipher_mode(&desc
[idx
], ctx
->cipher_mode
);
965 static void cc_proc_cipher(struct aead_request
*req
, struct cc_hw_desc desc
[],
966 unsigned int *seq_size
, unsigned int data_flow_mode
)
968 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
969 int direct
= req_ctx
->gen_ctx
.op_type
;
970 unsigned int idx
= *seq_size
;
972 if (req_ctx
->cryptlen
== 0)
973 return; /*null processing*/
975 cc_set_cipher_desc(req
, desc
, &idx
);
976 cc_proc_cipher_desc(req
, data_flow_mode
, desc
, &idx
);
977 if (direct
== DRV_CRYPTO_DIRECTION_ENCRYPT
) {
978 /* We must wait for DMA to write all cipher */
979 hw_desc_init(&desc
[idx
]);
980 set_din_no_dma(&desc
[idx
], 0, 0xfffff0);
981 set_dout_no_dma(&desc
[idx
], 0, 0, 1);
988 static void cc_set_hmac_desc(struct aead_request
*req
, struct cc_hw_desc desc
[],
989 unsigned int *seq_size
)
991 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
992 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
993 unsigned int hash_mode
= (ctx
->auth_mode
== DRV_HASH_SHA1
) ?
994 DRV_HASH_HW_SHA1
: DRV_HASH_HW_SHA256
;
995 unsigned int digest_size
= (ctx
->auth_mode
== DRV_HASH_SHA1
) ?
996 CC_SHA1_DIGEST_SIZE
: CC_SHA256_DIGEST_SIZE
;
997 unsigned int idx
= *seq_size
;
999 /* Loading hash ipad xor key state */
1000 hw_desc_init(&desc
[idx
]);
1001 set_cipher_mode(&desc
[idx
], hash_mode
);
1002 set_din_type(&desc
[idx
], DMA_DLLI
,
1003 ctx
->auth_state
.hmac
.ipad_opad_dma_addr
, digest_size
,
1005 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1006 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE0
);
1009 /* Load init. digest len (64 bytes) */
1010 hw_desc_init(&desc
[idx
]);
1011 set_cipher_mode(&desc
[idx
], hash_mode
);
1012 set_din_sram(&desc
[idx
], cc_digest_len_addr(ctx
->drvdata
, hash_mode
),
1014 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1015 set_setup_mode(&desc
[idx
], SETUP_LOAD_KEY0
);
1021 static void cc_set_xcbc_desc(struct aead_request
*req
, struct cc_hw_desc desc
[],
1022 unsigned int *seq_size
)
1024 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1025 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1026 unsigned int idx
= *seq_size
;
1028 /* Loading MAC state */
1029 hw_desc_init(&desc
[idx
]);
1030 set_din_const(&desc
[idx
], 0, CC_AES_BLOCK_SIZE
);
1031 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE0
);
1032 set_cipher_mode(&desc
[idx
], DRV_CIPHER_XCBC_MAC
);
1033 set_cipher_config0(&desc
[idx
], DESC_DIRECTION_ENCRYPT_ENCRYPT
);
1034 set_key_size_aes(&desc
[idx
], CC_AES_128_BIT_KEY_SIZE
);
1035 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1036 set_aes_not_hash_mode(&desc
[idx
]);
1039 /* Setup XCBC MAC K1 */
1040 hw_desc_init(&desc
[idx
]);
1041 set_din_type(&desc
[idx
], DMA_DLLI
,
1042 ctx
->auth_state
.xcbc
.xcbc_keys_dma_addr
,
1043 AES_KEYSIZE_128
, NS_BIT
);
1044 set_setup_mode(&desc
[idx
], SETUP_LOAD_KEY0
);
1045 set_cipher_mode(&desc
[idx
], DRV_CIPHER_XCBC_MAC
);
1046 set_cipher_config0(&desc
[idx
], DESC_DIRECTION_ENCRYPT_ENCRYPT
);
1047 set_key_size_aes(&desc
[idx
], CC_AES_128_BIT_KEY_SIZE
);
1048 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1049 set_aes_not_hash_mode(&desc
[idx
]);
1052 /* Setup XCBC MAC K2 */
1053 hw_desc_init(&desc
[idx
]);
1054 set_din_type(&desc
[idx
], DMA_DLLI
,
1055 (ctx
->auth_state
.xcbc
.xcbc_keys_dma_addr
+
1056 AES_KEYSIZE_128
), AES_KEYSIZE_128
, NS_BIT
);
1057 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE1
);
1058 set_cipher_mode(&desc
[idx
], DRV_CIPHER_XCBC_MAC
);
1059 set_cipher_config0(&desc
[idx
], DESC_DIRECTION_ENCRYPT_ENCRYPT
);
1060 set_key_size_aes(&desc
[idx
], CC_AES_128_BIT_KEY_SIZE
);
1061 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1062 set_aes_not_hash_mode(&desc
[idx
]);
1065 /* Setup XCBC MAC K3 */
1066 hw_desc_init(&desc
[idx
]);
1067 set_din_type(&desc
[idx
], DMA_DLLI
,
1068 (ctx
->auth_state
.xcbc
.xcbc_keys_dma_addr
+
1069 2 * AES_KEYSIZE_128
), AES_KEYSIZE_128
, NS_BIT
);
1070 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE2
);
1071 set_cipher_mode(&desc
[idx
], DRV_CIPHER_XCBC_MAC
);
1072 set_cipher_config0(&desc
[idx
], DESC_DIRECTION_ENCRYPT_ENCRYPT
);
1073 set_key_size_aes(&desc
[idx
], CC_AES_128_BIT_KEY_SIZE
);
1074 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1075 set_aes_not_hash_mode(&desc
[idx
]);
1081 static void cc_proc_header_desc(struct aead_request
*req
,
1082 struct cc_hw_desc desc
[],
1083 unsigned int *seq_size
)
1085 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
1086 unsigned int idx
= *seq_size
;
1088 /* Hash associated data */
1089 if (areq_ctx
->assoclen
> 0)
1090 cc_set_assoc_desc(req
, DIN_HASH
, desc
, &idx
);
1096 static void cc_proc_scheme_desc(struct aead_request
*req
,
1097 struct cc_hw_desc desc
[],
1098 unsigned int *seq_size
)
1100 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1101 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1102 struct cc_aead_handle
*aead_handle
= ctx
->drvdata
->aead_handle
;
1103 unsigned int hash_mode
= (ctx
->auth_mode
== DRV_HASH_SHA1
) ?
1104 DRV_HASH_HW_SHA1
: DRV_HASH_HW_SHA256
;
1105 unsigned int digest_size
= (ctx
->auth_mode
== DRV_HASH_SHA1
) ?
1106 CC_SHA1_DIGEST_SIZE
: CC_SHA256_DIGEST_SIZE
;
1107 unsigned int idx
= *seq_size
;
1109 hw_desc_init(&desc
[idx
]);
1110 set_cipher_mode(&desc
[idx
], hash_mode
);
1111 set_dout_sram(&desc
[idx
], aead_handle
->sram_workspace_addr
,
1113 set_flow_mode(&desc
[idx
], S_HASH_to_DOUT
);
1114 set_setup_mode(&desc
[idx
], SETUP_WRITE_STATE1
);
1115 set_cipher_do(&desc
[idx
], DO_PAD
);
1118 /* Get final ICV result */
1119 hw_desc_init(&desc
[idx
]);
1120 set_dout_sram(&desc
[idx
], aead_handle
->sram_workspace_addr
,
1122 set_flow_mode(&desc
[idx
], S_HASH_to_DOUT
);
1123 set_setup_mode(&desc
[idx
], SETUP_WRITE_STATE0
);
1124 set_cipher_config0(&desc
[idx
], HASH_DIGEST_RESULT_LITTLE_ENDIAN
);
1125 set_cipher_mode(&desc
[idx
], hash_mode
);
1128 /* Loading hash opad xor key state */
1129 hw_desc_init(&desc
[idx
]);
1130 set_cipher_mode(&desc
[idx
], hash_mode
);
1131 set_din_type(&desc
[idx
], DMA_DLLI
,
1132 (ctx
->auth_state
.hmac
.ipad_opad_dma_addr
+ digest_size
),
1133 digest_size
, NS_BIT
);
1134 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1135 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE0
);
1138 /* Load init. digest len (64 bytes) */
1139 hw_desc_init(&desc
[idx
]);
1140 set_cipher_mode(&desc
[idx
], hash_mode
);
1141 set_din_sram(&desc
[idx
], cc_digest_len_addr(ctx
->drvdata
, hash_mode
),
1143 set_cipher_config1(&desc
[idx
], HASH_PADDING_ENABLED
);
1144 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1145 set_setup_mode(&desc
[idx
], SETUP_LOAD_KEY0
);
1148 /* Perform HASH update */
1149 hw_desc_init(&desc
[idx
]);
1150 set_din_sram(&desc
[idx
], aead_handle
->sram_workspace_addr
,
1152 set_flow_mode(&desc
[idx
], DIN_HASH
);
1158 static void cc_mlli_to_sram(struct aead_request
*req
,
1159 struct cc_hw_desc desc
[], unsigned int *seq_size
)
1161 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
1162 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1163 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1164 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
1166 if ((req_ctx
->assoc_buff_type
== CC_DMA_BUF_MLLI
||
1167 req_ctx
->data_buff_type
== CC_DMA_BUF_MLLI
||
1168 !req_ctx
->is_single_pass
) && req_ctx
->mlli_params
.mlli_len
) {
1169 dev_dbg(dev
, "Copy-to-sram: mlli_dma=%08x, mlli_size=%u\n",
1170 ctx
->drvdata
->mlli_sram_addr
,
1171 req_ctx
->mlli_params
.mlli_len
);
1172 /* Copy MLLI table host-to-sram */
1173 hw_desc_init(&desc
[*seq_size
]);
1174 set_din_type(&desc
[*seq_size
], DMA_DLLI
,
1175 req_ctx
->mlli_params
.mlli_dma_addr
,
1176 req_ctx
->mlli_params
.mlli_len
, NS_BIT
);
1177 set_dout_sram(&desc
[*seq_size
],
1178 ctx
->drvdata
->mlli_sram_addr
,
1179 req_ctx
->mlli_params
.mlli_len
);
1180 set_flow_mode(&desc
[*seq_size
], BYPASS
);
1185 static enum cc_flow_mode
cc_get_data_flow(enum drv_crypto_direction direct
,
1186 enum cc_flow_mode setup_flow_mode
,
1187 bool is_single_pass
)
1189 enum cc_flow_mode data_flow_mode
;
1191 if (direct
== DRV_CRYPTO_DIRECTION_ENCRYPT
) {
1192 if (setup_flow_mode
== S_DIN_to_AES
)
1193 data_flow_mode
= is_single_pass
?
1194 AES_to_HASH_and_DOUT
: DIN_AES_DOUT
;
1196 data_flow_mode
= is_single_pass
?
1197 DES_to_HASH_and_DOUT
: DIN_DES_DOUT
;
1198 } else { /* Decrypt */
1199 if (setup_flow_mode
== S_DIN_to_AES
)
1200 data_flow_mode
= is_single_pass
?
1201 AES_and_HASH
: DIN_AES_DOUT
;
1203 data_flow_mode
= is_single_pass
?
1204 DES_and_HASH
: DIN_DES_DOUT
;
1207 return data_flow_mode
;
1210 static void cc_hmac_authenc(struct aead_request
*req
, struct cc_hw_desc desc
[],
1211 unsigned int *seq_size
)
1213 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1214 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1215 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
1216 int direct
= req_ctx
->gen_ctx
.op_type
;
1217 unsigned int data_flow_mode
=
1218 cc_get_data_flow(direct
, ctx
->flow_mode
,
1219 req_ctx
->is_single_pass
);
1221 if (req_ctx
->is_single_pass
) {
1225 cc_set_hmac_desc(req
, desc
, seq_size
);
1226 cc_set_cipher_desc(req
, desc
, seq_size
);
1227 cc_proc_header_desc(req
, desc
, seq_size
);
1228 cc_proc_cipher_desc(req
, data_flow_mode
, desc
, seq_size
);
1229 cc_proc_scheme_desc(req
, desc
, seq_size
);
1230 cc_proc_digest_desc(req
, desc
, seq_size
);
1236 * Fallback for unsupported single-pass modes,
1237 * i.e. using assoc. data of non-word-multiple
1239 if (direct
== DRV_CRYPTO_DIRECTION_ENCRYPT
) {
1240 /* encrypt first.. */
1241 cc_proc_cipher(req
, desc
, seq_size
, data_flow_mode
);
1242 /* authenc after..*/
1243 cc_set_hmac_desc(req
, desc
, seq_size
);
1244 cc_proc_authen_desc(req
, DIN_HASH
, desc
, seq_size
, direct
);
1245 cc_proc_scheme_desc(req
, desc
, seq_size
);
1246 cc_proc_digest_desc(req
, desc
, seq_size
);
1248 } else { /*DECRYPT*/
1249 /* authenc first..*/
1250 cc_set_hmac_desc(req
, desc
, seq_size
);
1251 cc_proc_authen_desc(req
, DIN_HASH
, desc
, seq_size
, direct
);
1252 cc_proc_scheme_desc(req
, desc
, seq_size
);
1253 /* decrypt after.. */
1254 cc_proc_cipher(req
, desc
, seq_size
, data_flow_mode
);
1255 /* read the digest result with setting the completion bit
1256 * must be after the cipher operation
1258 cc_proc_digest_desc(req
, desc
, seq_size
);
1263 cc_xcbc_authenc(struct aead_request
*req
, struct cc_hw_desc desc
[],
1264 unsigned int *seq_size
)
1266 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1267 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1268 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
1269 int direct
= req_ctx
->gen_ctx
.op_type
;
1270 unsigned int data_flow_mode
=
1271 cc_get_data_flow(direct
, ctx
->flow_mode
,
1272 req_ctx
->is_single_pass
);
1274 if (req_ctx
->is_single_pass
) {
1278 cc_set_xcbc_desc(req
, desc
, seq_size
);
1279 cc_set_cipher_desc(req
, desc
, seq_size
);
1280 cc_proc_header_desc(req
, desc
, seq_size
);
1281 cc_proc_cipher_desc(req
, data_flow_mode
, desc
, seq_size
);
1282 cc_proc_digest_desc(req
, desc
, seq_size
);
1288 * Fallback for unsupported single-pass modes,
1289 * i.e. using assoc. data of non-word-multiple
1291 if (direct
== DRV_CRYPTO_DIRECTION_ENCRYPT
) {
1292 /* encrypt first.. */
1293 cc_proc_cipher(req
, desc
, seq_size
, data_flow_mode
);
1294 /* authenc after.. */
1295 cc_set_xcbc_desc(req
, desc
, seq_size
);
1296 cc_proc_authen_desc(req
, DIN_HASH
, desc
, seq_size
, direct
);
1297 cc_proc_digest_desc(req
, desc
, seq_size
);
1298 } else { /*DECRYPT*/
1299 /* authenc first.. */
1300 cc_set_xcbc_desc(req
, desc
, seq_size
);
1301 cc_proc_authen_desc(req
, DIN_HASH
, desc
, seq_size
, direct
);
1302 /* decrypt after..*/
1303 cc_proc_cipher(req
, desc
, seq_size
, data_flow_mode
);
1304 /* read the digest result with setting the completion bit
1305 * must be after the cipher operation
1307 cc_proc_digest_desc(req
, desc
, seq_size
);
1311 static int validate_data_size(struct cc_aead_ctx
*ctx
,
1312 enum drv_crypto_direction direct
,
1313 struct aead_request
*req
)
1315 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
1316 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
1317 unsigned int assoclen
= areq_ctx
->assoclen
;
1318 unsigned int cipherlen
= (direct
== DRV_CRYPTO_DIRECTION_DECRYPT
) ?
1319 (req
->cryptlen
- ctx
->authsize
) : req
->cryptlen
;
1321 if (direct
== DRV_CRYPTO_DIRECTION_DECRYPT
&&
1322 req
->cryptlen
< ctx
->authsize
)
1325 areq_ctx
->is_single_pass
= true; /*defaulted to fast flow*/
1327 switch (ctx
->flow_mode
) {
1329 if (ctx
->cipher_mode
== DRV_CIPHER_CBC
&&
1330 !IS_ALIGNED(cipherlen
, AES_BLOCK_SIZE
))
1332 if (ctx
->cipher_mode
== DRV_CIPHER_CCM
)
1334 if (ctx
->cipher_mode
== DRV_CIPHER_GCTR
) {
1335 if (areq_ctx
->plaintext_authenticate_only
)
1336 areq_ctx
->is_single_pass
= false;
1340 if (!IS_ALIGNED(assoclen
, sizeof(u32
)))
1341 areq_ctx
->is_single_pass
= false;
1343 if (ctx
->cipher_mode
== DRV_CIPHER_CTR
&&
1344 !IS_ALIGNED(cipherlen
, sizeof(u32
)))
1345 areq_ctx
->is_single_pass
= false;
1349 if (!IS_ALIGNED(cipherlen
, DES_BLOCK_SIZE
))
1351 if (!IS_ALIGNED(assoclen
, DES_BLOCK_SIZE
))
1352 areq_ctx
->is_single_pass
= false;
1355 dev_err(dev
, "Unexpected flow mode (%d)\n", ctx
->flow_mode
);
1365 static unsigned int format_ccm_a0(u8
*pa0_buff
, u32 header_size
)
1367 unsigned int len
= 0;
1369 if (header_size
== 0)
1372 if (header_size
< ((1UL << 16) - (1UL << 8))) {
1375 pa0_buff
[0] = (header_size
>> 8) & 0xFF;
1376 pa0_buff
[1] = header_size
& 0xFF;
1382 pa0_buff
[2] = (header_size
>> 24) & 0xFF;
1383 pa0_buff
[3] = (header_size
>> 16) & 0xFF;
1384 pa0_buff
[4] = (header_size
>> 8) & 0xFF;
1385 pa0_buff
[5] = header_size
& 0xFF;
1391 static int set_msg_len(u8
*block
, unsigned int msglen
, unsigned int csize
)
1395 memset(block
, 0, csize
);
1400 else if (msglen
> (1 << (8 * csize
)))
1403 data
= cpu_to_be32(msglen
);
1404 memcpy(block
- csize
, (u8
*)&data
+ 4 - csize
, csize
);
1409 static int cc_ccm(struct aead_request
*req
, struct cc_hw_desc desc
[],
1410 unsigned int *seq_size
)
1412 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1413 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1414 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
1415 unsigned int idx
= *seq_size
;
1416 unsigned int cipher_flow_mode
;
1417 dma_addr_t mac_result
;
1419 if (req_ctx
->gen_ctx
.op_type
== DRV_CRYPTO_DIRECTION_DECRYPT
) {
1420 cipher_flow_mode
= AES_to_HASH_and_DOUT
;
1421 mac_result
= req_ctx
->mac_buf_dma_addr
;
1422 } else { /* Encrypt */
1423 cipher_flow_mode
= AES_and_HASH
;
1424 mac_result
= req_ctx
->icv_dma_addr
;
1428 hw_desc_init(&desc
[idx
]);
1429 set_cipher_mode(&desc
[idx
], DRV_CIPHER_CTR
);
1430 set_din_type(&desc
[idx
], DMA_DLLI
, ctx
->enckey_dma_addr
,
1431 ((ctx
->enc_keylen
== 24) ? CC_AES_KEY_SIZE_MAX
:
1432 ctx
->enc_keylen
), NS_BIT
);
1433 set_key_size_aes(&desc
[idx
], ctx
->enc_keylen
);
1434 set_setup_mode(&desc
[idx
], SETUP_LOAD_KEY0
);
1435 set_cipher_config0(&desc
[idx
], DESC_DIRECTION_ENCRYPT_ENCRYPT
);
1436 set_flow_mode(&desc
[idx
], S_DIN_to_AES
);
1439 /* load ctr state */
1440 hw_desc_init(&desc
[idx
]);
1441 set_cipher_mode(&desc
[idx
], DRV_CIPHER_CTR
);
1442 set_key_size_aes(&desc
[idx
], ctx
->enc_keylen
);
1443 set_din_type(&desc
[idx
], DMA_DLLI
,
1444 req_ctx
->gen_ctx
.iv_dma_addr
, AES_BLOCK_SIZE
, NS_BIT
);
1445 set_cipher_config0(&desc
[idx
], DESC_DIRECTION_ENCRYPT_ENCRYPT
);
1446 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE1
);
1447 set_flow_mode(&desc
[idx
], S_DIN_to_AES
);
1451 hw_desc_init(&desc
[idx
]);
1452 set_cipher_mode(&desc
[idx
], DRV_CIPHER_CBC_MAC
);
1453 set_din_type(&desc
[idx
], DMA_DLLI
, ctx
->enckey_dma_addr
,
1454 ((ctx
->enc_keylen
== 24) ? CC_AES_KEY_SIZE_MAX
:
1455 ctx
->enc_keylen
), NS_BIT
);
1456 set_key_size_aes(&desc
[idx
], ctx
->enc_keylen
);
1457 set_setup_mode(&desc
[idx
], SETUP_LOAD_KEY0
);
1458 set_cipher_config0(&desc
[idx
], DESC_DIRECTION_ENCRYPT_ENCRYPT
);
1459 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1460 set_aes_not_hash_mode(&desc
[idx
]);
1463 /* load MAC state */
1464 hw_desc_init(&desc
[idx
]);
1465 set_cipher_mode(&desc
[idx
], DRV_CIPHER_CBC_MAC
);
1466 set_key_size_aes(&desc
[idx
], ctx
->enc_keylen
);
1467 set_din_type(&desc
[idx
], DMA_DLLI
, req_ctx
->mac_buf_dma_addr
,
1468 AES_BLOCK_SIZE
, NS_BIT
);
1469 set_cipher_config0(&desc
[idx
], DESC_DIRECTION_ENCRYPT_ENCRYPT
);
1470 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE0
);
1471 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1472 set_aes_not_hash_mode(&desc
[idx
]);
1475 /* process assoc data */
1476 if (req_ctx
->assoclen
> 0) {
1477 cc_set_assoc_desc(req
, DIN_HASH
, desc
, &idx
);
1479 hw_desc_init(&desc
[idx
]);
1480 set_din_type(&desc
[idx
], DMA_DLLI
,
1481 sg_dma_address(&req_ctx
->ccm_adata_sg
),
1482 AES_BLOCK_SIZE
+ req_ctx
->ccm_hdr_size
, NS_BIT
);
1483 set_flow_mode(&desc
[idx
], DIN_HASH
);
1487 /* process the cipher */
1488 if (req_ctx
->cryptlen
)
1489 cc_proc_cipher_desc(req
, cipher_flow_mode
, desc
, &idx
);
1491 /* Read temporal MAC */
1492 hw_desc_init(&desc
[idx
]);
1493 set_cipher_mode(&desc
[idx
], DRV_CIPHER_CBC_MAC
);
1494 set_dout_dlli(&desc
[idx
], req_ctx
->mac_buf_dma_addr
, ctx
->authsize
,
1496 set_setup_mode(&desc
[idx
], SETUP_WRITE_STATE0
);
1497 set_cipher_config0(&desc
[idx
], HASH_DIGEST_RESULT_LITTLE_ENDIAN
);
1498 set_flow_mode(&desc
[idx
], S_HASH_to_DOUT
);
1499 set_aes_not_hash_mode(&desc
[idx
]);
1502 /* load AES-CTR state (for last MAC calculation)*/
1503 hw_desc_init(&desc
[idx
]);
1504 set_cipher_mode(&desc
[idx
], DRV_CIPHER_CTR
);
1505 set_cipher_config0(&desc
[idx
], DRV_CRYPTO_DIRECTION_ENCRYPT
);
1506 set_din_type(&desc
[idx
], DMA_DLLI
, req_ctx
->ccm_iv0_dma_addr
,
1507 AES_BLOCK_SIZE
, NS_BIT
);
1508 set_key_size_aes(&desc
[idx
], ctx
->enc_keylen
);
1509 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE1
);
1510 set_flow_mode(&desc
[idx
], S_DIN_to_AES
);
1513 hw_desc_init(&desc
[idx
]);
1514 set_din_no_dma(&desc
[idx
], 0, 0xfffff0);
1515 set_dout_no_dma(&desc
[idx
], 0, 0, 1);
1518 /* encrypt the "T" value and store MAC in mac_state */
1519 hw_desc_init(&desc
[idx
]);
1520 set_din_type(&desc
[idx
], DMA_DLLI
, req_ctx
->mac_buf_dma_addr
,
1521 ctx
->authsize
, NS_BIT
);
1522 set_dout_dlli(&desc
[idx
], mac_result
, ctx
->authsize
, NS_BIT
, 1);
1523 set_queue_last_ind(ctx
->drvdata
, &desc
[idx
]);
1524 set_flow_mode(&desc
[idx
], DIN_AES_DOUT
);
1531 static int config_ccm_adata(struct aead_request
*req
)
1533 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1534 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1535 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
1536 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
1537 //unsigned int size_of_a = 0, rem_a_size = 0;
1538 unsigned int lp
= req
->iv
[0];
1539 /* Note: The code assume that req->iv[0] already contains the value
1542 unsigned int l
= lp
+ 1; /* This is L' of RFC 3610. */
1543 unsigned int m
= ctx
->authsize
; /* This is M' of RFC 3610. */
1544 u8
*b0
= req_ctx
->ccm_config
+ CCM_B0_OFFSET
;
1545 u8
*a0
= req_ctx
->ccm_config
+ CCM_A0_OFFSET
;
1546 u8
*ctr_count_0
= req_ctx
->ccm_config
+ CCM_CTR_COUNT_0_OFFSET
;
1547 unsigned int cryptlen
= (req_ctx
->gen_ctx
.op_type
==
1548 DRV_CRYPTO_DIRECTION_ENCRYPT
) ?
1550 (req
->cryptlen
- ctx
->authsize
);
1553 memset(req_ctx
->mac_buf
, 0, AES_BLOCK_SIZE
);
1554 memset(req_ctx
->ccm_config
, 0, AES_BLOCK_SIZE
* 3);
1556 /* taken from crypto/ccm.c */
1557 /* 2 <= L <= 8, so 1 <= L' <= 7. */
1558 if (l
< 2 || l
> 8) {
1559 dev_dbg(dev
, "illegal iv value %X\n", req
->iv
[0]);
1562 memcpy(b0
, req
->iv
, AES_BLOCK_SIZE
);
1564 /* format control info per RFC 3610 and
1565 * NIST Special Publication 800-38C
1567 *b0
|= (8 * ((m
- 2) / 2));
1568 if (req_ctx
->assoclen
> 0)
1569 *b0
|= 64; /* Enable bit 6 if Adata exists. */
1571 rc
= set_msg_len(b0
+ 16 - l
, cryptlen
, l
); /* Write L'. */
1573 dev_err(dev
, "message len overflow detected");
1576 /* END of "taken from crypto/ccm.c" */
1578 /* l(a) - size of associated data. */
1579 req_ctx
->ccm_hdr_size
= format_ccm_a0(a0
, req_ctx
->assoclen
);
1581 memset(req
->iv
+ 15 - req
->iv
[0], 0, req
->iv
[0] + 1);
1584 memcpy(ctr_count_0
, req
->iv
, AES_BLOCK_SIZE
);
1585 ctr_count_0
[15] = 0;
1590 static void cc_proc_rfc4309_ccm(struct aead_request
*req
)
1592 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1593 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1594 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
1597 memset(areq_ctx
->ctr_iv
, 0, AES_BLOCK_SIZE
);
1598 /* For RFC 4309, always use 4 bytes for message length
1599 * (at most 2^32-1 bytes).
1601 areq_ctx
->ctr_iv
[0] = 3;
1603 /* In RFC 4309 there is an 11-bytes nonce+IV part,
1604 * that we build here.
1606 memcpy(areq_ctx
->ctr_iv
+ CCM_BLOCK_NONCE_OFFSET
, ctx
->ctr_nonce
,
1607 CCM_BLOCK_NONCE_SIZE
);
1608 memcpy(areq_ctx
->ctr_iv
+ CCM_BLOCK_IV_OFFSET
, req
->iv
,
1610 req
->iv
= areq_ctx
->ctr_iv
;
1613 static void cc_set_ghash_desc(struct aead_request
*req
,
1614 struct cc_hw_desc desc
[], unsigned int *seq_size
)
1616 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1617 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1618 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
1619 unsigned int idx
= *seq_size
;
1621 /* load key to AES*/
1622 hw_desc_init(&desc
[idx
]);
1623 set_cipher_mode(&desc
[idx
], DRV_CIPHER_ECB
);
1624 set_cipher_config0(&desc
[idx
], DRV_CRYPTO_DIRECTION_ENCRYPT
);
1625 set_din_type(&desc
[idx
], DMA_DLLI
, ctx
->enckey_dma_addr
,
1626 ctx
->enc_keylen
, NS_BIT
);
1627 set_key_size_aes(&desc
[idx
], ctx
->enc_keylen
);
1628 set_setup_mode(&desc
[idx
], SETUP_LOAD_KEY0
);
1629 set_flow_mode(&desc
[idx
], S_DIN_to_AES
);
1632 /* process one zero block to generate hkey */
1633 hw_desc_init(&desc
[idx
]);
1634 set_din_const(&desc
[idx
], 0x0, AES_BLOCK_SIZE
);
1635 set_dout_dlli(&desc
[idx
], req_ctx
->hkey_dma_addr
, AES_BLOCK_SIZE
,
1637 set_flow_mode(&desc
[idx
], DIN_AES_DOUT
);
1640 /* Memory Barrier */
1641 hw_desc_init(&desc
[idx
]);
1642 set_din_no_dma(&desc
[idx
], 0, 0xfffff0);
1643 set_dout_no_dma(&desc
[idx
], 0, 0, 1);
1646 /* Load GHASH subkey */
1647 hw_desc_init(&desc
[idx
]);
1648 set_din_type(&desc
[idx
], DMA_DLLI
, req_ctx
->hkey_dma_addr
,
1649 AES_BLOCK_SIZE
, NS_BIT
);
1650 set_dout_no_dma(&desc
[idx
], 0, 0, 1);
1651 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1652 set_aes_not_hash_mode(&desc
[idx
]);
1653 set_cipher_mode(&desc
[idx
], DRV_HASH_HW_GHASH
);
1654 set_cipher_config1(&desc
[idx
], HASH_PADDING_ENABLED
);
1655 set_setup_mode(&desc
[idx
], SETUP_LOAD_KEY0
);
1658 /* Configure Hash Engine to work with GHASH.
1659 * Since it was not possible to extend HASH submodes to add GHASH,
1660 * The following command is necessary in order to
1661 * select GHASH (according to HW designers)
1663 hw_desc_init(&desc
[idx
]);
1664 set_din_no_dma(&desc
[idx
], 0, 0xfffff0);
1665 set_dout_no_dma(&desc
[idx
], 0, 0, 1);
1666 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1667 set_aes_not_hash_mode(&desc
[idx
]);
1668 set_cipher_mode(&desc
[idx
], DRV_HASH_HW_GHASH
);
1669 set_cipher_do(&desc
[idx
], 1); //1=AES_SK RKEK
1670 set_cipher_config0(&desc
[idx
], DRV_CRYPTO_DIRECTION_ENCRYPT
);
1671 set_cipher_config1(&desc
[idx
], HASH_PADDING_ENABLED
);
1672 set_setup_mode(&desc
[idx
], SETUP_LOAD_KEY0
);
1675 /* Load GHASH initial STATE (which is 0). (for any hash there is an
1678 hw_desc_init(&desc
[idx
]);
1679 set_din_const(&desc
[idx
], 0x0, AES_BLOCK_SIZE
);
1680 set_dout_no_dma(&desc
[idx
], 0, 0, 1);
1681 set_flow_mode(&desc
[idx
], S_DIN_to_HASH
);
1682 set_aes_not_hash_mode(&desc
[idx
]);
1683 set_cipher_mode(&desc
[idx
], DRV_HASH_HW_GHASH
);
1684 set_cipher_config1(&desc
[idx
], HASH_PADDING_ENABLED
);
1685 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE0
);
1691 static void cc_set_gctr_desc(struct aead_request
*req
, struct cc_hw_desc desc
[],
1692 unsigned int *seq_size
)
1694 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1695 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1696 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
1697 unsigned int idx
= *seq_size
;
1699 /* load key to AES*/
1700 hw_desc_init(&desc
[idx
]);
1701 set_cipher_mode(&desc
[idx
], DRV_CIPHER_GCTR
);
1702 set_cipher_config0(&desc
[idx
], DRV_CRYPTO_DIRECTION_ENCRYPT
);
1703 set_din_type(&desc
[idx
], DMA_DLLI
, ctx
->enckey_dma_addr
,
1704 ctx
->enc_keylen
, NS_BIT
);
1705 set_key_size_aes(&desc
[idx
], ctx
->enc_keylen
);
1706 set_setup_mode(&desc
[idx
], SETUP_LOAD_KEY0
);
1707 set_flow_mode(&desc
[idx
], S_DIN_to_AES
);
1710 if (req_ctx
->cryptlen
&& !req_ctx
->plaintext_authenticate_only
) {
1711 /* load AES/CTR initial CTR value inc by 2*/
1712 hw_desc_init(&desc
[idx
]);
1713 set_cipher_mode(&desc
[idx
], DRV_CIPHER_GCTR
);
1714 set_key_size_aes(&desc
[idx
], ctx
->enc_keylen
);
1715 set_din_type(&desc
[idx
], DMA_DLLI
,
1716 req_ctx
->gcm_iv_inc2_dma_addr
, AES_BLOCK_SIZE
,
1718 set_cipher_config0(&desc
[idx
], DRV_CRYPTO_DIRECTION_ENCRYPT
);
1719 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE1
);
1720 set_flow_mode(&desc
[idx
], S_DIN_to_AES
);
1727 static void cc_proc_gcm_result(struct aead_request
*req
,
1728 struct cc_hw_desc desc
[],
1729 unsigned int *seq_size
)
1731 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1732 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1733 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
1734 dma_addr_t mac_result
;
1735 unsigned int idx
= *seq_size
;
1737 if (req_ctx
->gen_ctx
.op_type
== DRV_CRYPTO_DIRECTION_DECRYPT
) {
1738 mac_result
= req_ctx
->mac_buf_dma_addr
;
1739 } else { /* Encrypt */
1740 mac_result
= req_ctx
->icv_dma_addr
;
1743 /* process(ghash) gcm_block_len */
1744 hw_desc_init(&desc
[idx
]);
1745 set_din_type(&desc
[idx
], DMA_DLLI
, req_ctx
->gcm_block_len_dma_addr
,
1746 AES_BLOCK_SIZE
, NS_BIT
);
1747 set_flow_mode(&desc
[idx
], DIN_HASH
);
1750 /* Store GHASH state after GHASH(Associated Data + Cipher +LenBlock) */
1751 hw_desc_init(&desc
[idx
]);
1752 set_cipher_mode(&desc
[idx
], DRV_HASH_HW_GHASH
);
1753 set_din_no_dma(&desc
[idx
], 0, 0xfffff0);
1754 set_dout_dlli(&desc
[idx
], req_ctx
->mac_buf_dma_addr
, AES_BLOCK_SIZE
,
1756 set_setup_mode(&desc
[idx
], SETUP_WRITE_STATE0
);
1757 set_flow_mode(&desc
[idx
], S_HASH_to_DOUT
);
1758 set_aes_not_hash_mode(&desc
[idx
]);
1762 /* load AES/CTR initial CTR value inc by 1*/
1763 hw_desc_init(&desc
[idx
]);
1764 set_cipher_mode(&desc
[idx
], DRV_CIPHER_GCTR
);
1765 set_key_size_aes(&desc
[idx
], ctx
->enc_keylen
);
1766 set_din_type(&desc
[idx
], DMA_DLLI
, req_ctx
->gcm_iv_inc1_dma_addr
,
1767 AES_BLOCK_SIZE
, NS_BIT
);
1768 set_cipher_config0(&desc
[idx
], DRV_CRYPTO_DIRECTION_ENCRYPT
);
1769 set_setup_mode(&desc
[idx
], SETUP_LOAD_STATE1
);
1770 set_flow_mode(&desc
[idx
], S_DIN_to_AES
);
1773 /* Memory Barrier */
1774 hw_desc_init(&desc
[idx
]);
1775 set_din_no_dma(&desc
[idx
], 0, 0xfffff0);
1776 set_dout_no_dma(&desc
[idx
], 0, 0, 1);
1779 /* process GCTR on stored GHASH and store MAC in mac_state*/
1780 hw_desc_init(&desc
[idx
]);
1781 set_cipher_mode(&desc
[idx
], DRV_CIPHER_GCTR
);
1782 set_din_type(&desc
[idx
], DMA_DLLI
, req_ctx
->mac_buf_dma_addr
,
1783 AES_BLOCK_SIZE
, NS_BIT
);
1784 set_dout_dlli(&desc
[idx
], mac_result
, ctx
->authsize
, NS_BIT
, 1);
1785 set_queue_last_ind(ctx
->drvdata
, &desc
[idx
]);
1786 set_flow_mode(&desc
[idx
], DIN_AES_DOUT
);
1792 static int cc_gcm(struct aead_request
*req
, struct cc_hw_desc desc
[],
1793 unsigned int *seq_size
)
1795 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
1796 unsigned int cipher_flow_mode
;
1798 //in RFC4543 no data to encrypt. just copy data from src to dest.
1799 if (req_ctx
->plaintext_authenticate_only
) {
1800 cc_proc_cipher_desc(req
, BYPASS
, desc
, seq_size
);
1801 cc_set_ghash_desc(req
, desc
, seq_size
);
1802 /* process(ghash) assoc data */
1803 cc_set_assoc_desc(req
, DIN_HASH
, desc
, seq_size
);
1804 cc_set_gctr_desc(req
, desc
, seq_size
);
1805 cc_proc_gcm_result(req
, desc
, seq_size
);
1809 if (req_ctx
->gen_ctx
.op_type
== DRV_CRYPTO_DIRECTION_DECRYPT
) {
1810 cipher_flow_mode
= AES_and_HASH
;
1811 } else { /* Encrypt */
1812 cipher_flow_mode
= AES_to_HASH_and_DOUT
;
1815 // for gcm and rfc4106.
1816 cc_set_ghash_desc(req
, desc
, seq_size
);
1817 /* process(ghash) assoc data */
1818 if (req_ctx
->assoclen
> 0)
1819 cc_set_assoc_desc(req
, DIN_HASH
, desc
, seq_size
);
1820 cc_set_gctr_desc(req
, desc
, seq_size
);
1821 /* process(gctr+ghash) */
1822 if (req_ctx
->cryptlen
)
1823 cc_proc_cipher_desc(req
, cipher_flow_mode
, desc
, seq_size
);
1824 cc_proc_gcm_result(req
, desc
, seq_size
);
1829 static int config_gcm_context(struct aead_request
*req
)
1831 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1832 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1833 struct aead_req_ctx
*req_ctx
= aead_request_ctx_dma(req
);
1834 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
1836 unsigned int cryptlen
= (req_ctx
->gen_ctx
.op_type
==
1837 DRV_CRYPTO_DIRECTION_ENCRYPT
) ?
1839 (req
->cryptlen
- ctx
->authsize
);
1840 __be32 counter
= cpu_to_be32(2);
1842 dev_dbg(dev
, "%s() cryptlen = %d, req_ctx->assoclen = %d ctx->authsize = %d\n",
1843 __func__
, cryptlen
, req_ctx
->assoclen
, ctx
->authsize
);
1845 memset(req_ctx
->hkey
, 0, AES_BLOCK_SIZE
);
1847 memset(req_ctx
->mac_buf
, 0, AES_BLOCK_SIZE
);
1849 memcpy(req
->iv
+ 12, &counter
, 4);
1850 memcpy(req_ctx
->gcm_iv_inc2
, req
->iv
, 16);
1852 counter
= cpu_to_be32(1);
1853 memcpy(req
->iv
+ 12, &counter
, 4);
1854 memcpy(req_ctx
->gcm_iv_inc1
, req
->iv
, 16);
1856 if (!req_ctx
->plaintext_authenticate_only
) {
1859 temp64
= cpu_to_be64(req_ctx
->assoclen
* 8);
1860 memcpy(&req_ctx
->gcm_len_block
.len_a
, &temp64
, sizeof(temp64
));
1861 temp64
= cpu_to_be64(cryptlen
* 8);
1862 memcpy(&req_ctx
->gcm_len_block
.len_c
, &temp64
, 8);
1864 /* rfc4543=> all data(AAD,IV,Plain) are considered additional
1865 * data that is nothing is encrypted.
1869 temp64
= cpu_to_be64((req_ctx
->assoclen
+ cryptlen
) * 8);
1870 memcpy(&req_ctx
->gcm_len_block
.len_a
, &temp64
, sizeof(temp64
));
1872 memcpy(&req_ctx
->gcm_len_block
.len_c
, &temp64
, 8);
1878 static void cc_proc_rfc4_gcm(struct aead_request
*req
)
1880 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1881 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1882 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
1884 memcpy(areq_ctx
->ctr_iv
+ GCM_BLOCK_RFC4_NONCE_OFFSET
,
1885 ctx
->ctr_nonce
, GCM_BLOCK_RFC4_NONCE_SIZE
);
1886 memcpy(areq_ctx
->ctr_iv
+ GCM_BLOCK_RFC4_IV_OFFSET
, req
->iv
,
1887 GCM_BLOCK_RFC4_IV_SIZE
);
1888 req
->iv
= areq_ctx
->ctr_iv
;
1891 static int cc_proc_aead(struct aead_request
*req
,
1892 enum drv_crypto_direction direct
)
1896 struct cc_hw_desc desc
[MAX_AEAD_PROCESS_SEQ
];
1897 struct crypto_aead
*tfm
= crypto_aead_reqtfm(req
);
1898 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
1899 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
1900 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
1901 struct cc_crypto_req cc_req
= {};
1903 dev_dbg(dev
, "%s context=%p req=%p iv=%p src=%p src_ofs=%d dst=%p dst_ofs=%d cryptolen=%d\n",
1904 ((direct
== DRV_CRYPTO_DIRECTION_ENCRYPT
) ? "Enc" : "Dec"),
1905 ctx
, req
, req
->iv
, sg_virt(req
->src
), req
->src
->offset
,
1906 sg_virt(req
->dst
), req
->dst
->offset
, req
->cryptlen
);
1908 /* STAT_PHASE_0: Init and sanity checks */
1910 /* Check data length according to mode */
1911 if (validate_data_size(ctx
, direct
, req
)) {
1912 dev_err(dev
, "Unsupported crypt/assoc len %d/%d.\n",
1913 req
->cryptlen
, areq_ctx
->assoclen
);
1917 /* Setup request structure */
1918 cc_req
.user_cb
= cc_aead_complete
;
1919 cc_req
.user_arg
= req
;
1921 /* Setup request context */
1922 areq_ctx
->gen_ctx
.op_type
= direct
;
1923 areq_ctx
->req_authsize
= ctx
->authsize
;
1924 areq_ctx
->cipher_mode
= ctx
->cipher_mode
;
1926 /* STAT_PHASE_1: Map buffers */
1928 if (ctx
->cipher_mode
== DRV_CIPHER_CTR
) {
1929 /* Build CTR IV - Copy nonce from last 4 bytes in
1930 * CTR key to first 4 bytes in CTR IV
1932 memcpy(areq_ctx
->ctr_iv
, ctx
->ctr_nonce
,
1933 CTR_RFC3686_NONCE_SIZE
);
1934 memcpy(areq_ctx
->ctr_iv
+ CTR_RFC3686_NONCE_SIZE
, req
->iv
,
1935 CTR_RFC3686_IV_SIZE
);
1936 /* Initialize counter portion of counter block */
1937 *(__be32
*)(areq_ctx
->ctr_iv
+ CTR_RFC3686_NONCE_SIZE
+
1938 CTR_RFC3686_IV_SIZE
) = cpu_to_be32(1);
1940 /* Replace with counter iv */
1941 req
->iv
= areq_ctx
->ctr_iv
;
1942 areq_ctx
->hw_iv_size
= CTR_RFC3686_BLOCK_SIZE
;
1943 } else if ((ctx
->cipher_mode
== DRV_CIPHER_CCM
) ||
1944 (ctx
->cipher_mode
== DRV_CIPHER_GCTR
)) {
1945 areq_ctx
->hw_iv_size
= AES_BLOCK_SIZE
;
1946 if (areq_ctx
->ctr_iv
!= req
->iv
) {
1947 memcpy(areq_ctx
->ctr_iv
, req
->iv
,
1948 crypto_aead_ivsize(tfm
));
1949 req
->iv
= areq_ctx
->ctr_iv
;
1952 areq_ctx
->hw_iv_size
= crypto_aead_ivsize(tfm
);
1955 if (ctx
->cipher_mode
== DRV_CIPHER_CCM
) {
1956 rc
= config_ccm_adata(req
);
1958 dev_dbg(dev
, "config_ccm_adata() returned with a failure %d!",
1963 areq_ctx
->ccm_hdr_size
= ccm_header_size_null
;
1966 if (ctx
->cipher_mode
== DRV_CIPHER_GCTR
) {
1967 rc
= config_gcm_context(req
);
1969 dev_dbg(dev
, "config_gcm_context() returned with a failure %d!",
1975 rc
= cc_map_aead_request(ctx
->drvdata
, req
);
1977 dev_err(dev
, "map_request() failed\n");
1981 /* STAT_PHASE_2: Create sequence */
1983 /* Load MLLI tables to SRAM if necessary */
1984 cc_mlli_to_sram(req
, desc
, &seq_len
);
1986 switch (ctx
->auth_mode
) {
1988 case DRV_HASH_SHA256
:
1989 cc_hmac_authenc(req
, desc
, &seq_len
);
1991 case DRV_HASH_XCBC_MAC
:
1992 cc_xcbc_authenc(req
, desc
, &seq_len
);
1995 if (ctx
->cipher_mode
== DRV_CIPHER_CCM
)
1996 cc_ccm(req
, desc
, &seq_len
);
1997 if (ctx
->cipher_mode
== DRV_CIPHER_GCTR
)
1998 cc_gcm(req
, desc
, &seq_len
);
2001 dev_err(dev
, "Unsupported authenc (%d)\n", ctx
->auth_mode
);
2002 cc_unmap_aead_request(dev
, req
);
2007 /* STAT_PHASE_3: Lock HW and push sequence */
2009 rc
= cc_send_request(ctx
->drvdata
, &cc_req
, desc
, seq_len
, &req
->base
);
2011 if (rc
!= -EINPROGRESS
&& rc
!= -EBUSY
) {
2012 dev_err(dev
, "send_request() failed (rc=%d)\n", rc
);
2013 cc_unmap_aead_request(dev
, req
);
2020 static int cc_aead_encrypt(struct aead_request
*req
)
2022 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
2025 memset(areq_ctx
, 0, sizeof(*areq_ctx
));
2027 /* No generated IV required */
2028 areq_ctx
->backup_iv
= req
->iv
;
2029 areq_ctx
->assoclen
= req
->assoclen
;
2031 rc
= cc_proc_aead(req
, DRV_CRYPTO_DIRECTION_ENCRYPT
);
2032 if (rc
!= -EINPROGRESS
&& rc
!= -EBUSY
)
2033 req
->iv
= areq_ctx
->backup_iv
;
2038 static int cc_rfc4309_ccm_encrypt(struct aead_request
*req
)
2040 /* Very similar to cc_aead_encrypt() above. */
2042 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
2045 rc
= crypto_ipsec_check_assoclen(req
->assoclen
);
2049 memset(areq_ctx
, 0, sizeof(*areq_ctx
));
2051 /* No generated IV required */
2052 areq_ctx
->backup_iv
= req
->iv
;
2053 areq_ctx
->assoclen
= req
->assoclen
- CCM_BLOCK_IV_SIZE
;
2055 cc_proc_rfc4309_ccm(req
);
2057 rc
= cc_proc_aead(req
, DRV_CRYPTO_DIRECTION_ENCRYPT
);
2058 if (rc
!= -EINPROGRESS
&& rc
!= -EBUSY
)
2059 req
->iv
= areq_ctx
->backup_iv
;
2064 static int cc_aead_decrypt(struct aead_request
*req
)
2066 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
2069 memset(areq_ctx
, 0, sizeof(*areq_ctx
));
2071 /* No generated IV required */
2072 areq_ctx
->backup_iv
= req
->iv
;
2073 areq_ctx
->assoclen
= req
->assoclen
;
2075 rc
= cc_proc_aead(req
, DRV_CRYPTO_DIRECTION_DECRYPT
);
2076 if (rc
!= -EINPROGRESS
&& rc
!= -EBUSY
)
2077 req
->iv
= areq_ctx
->backup_iv
;
2082 static int cc_rfc4309_ccm_decrypt(struct aead_request
*req
)
2084 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
2087 rc
= crypto_ipsec_check_assoclen(req
->assoclen
);
2091 memset(areq_ctx
, 0, sizeof(*areq_ctx
));
2093 /* No generated IV required */
2094 areq_ctx
->backup_iv
= req
->iv
;
2095 areq_ctx
->assoclen
= req
->assoclen
- CCM_BLOCK_IV_SIZE
;
2097 cc_proc_rfc4309_ccm(req
);
2099 rc
= cc_proc_aead(req
, DRV_CRYPTO_DIRECTION_DECRYPT
);
2100 if (rc
!= -EINPROGRESS
&& rc
!= -EBUSY
)
2101 req
->iv
= areq_ctx
->backup_iv
;
2107 static int cc_rfc4106_gcm_setkey(struct crypto_aead
*tfm
, const u8
*key
,
2108 unsigned int keylen
)
2110 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
2111 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
2113 dev_dbg(dev
, "%s() keylen %d, key %p\n", __func__
, keylen
, key
);
2119 memcpy(ctx
->ctr_nonce
, key
+ keylen
, 4);
2121 return cc_aead_setkey(tfm
, key
, keylen
);
2124 static int cc_rfc4543_gcm_setkey(struct crypto_aead
*tfm
, const u8
*key
,
2125 unsigned int keylen
)
2127 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(tfm
);
2128 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
2130 dev_dbg(dev
, "%s() keylen %d, key %p\n", __func__
, keylen
, key
);
2136 memcpy(ctx
->ctr_nonce
, key
+ keylen
, 4);
2138 return cc_aead_setkey(tfm
, key
, keylen
);
2141 static int cc_gcm_setauthsize(struct crypto_aead
*authenc
,
2142 unsigned int authsize
)
2157 return cc_aead_setauthsize(authenc
, authsize
);
2160 static int cc_rfc4106_gcm_setauthsize(struct crypto_aead
*authenc
,
2161 unsigned int authsize
)
2163 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(authenc
);
2164 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
2166 dev_dbg(dev
, "authsize %d\n", authsize
);
2177 return cc_aead_setauthsize(authenc
, authsize
);
2180 static int cc_rfc4543_gcm_setauthsize(struct crypto_aead
*authenc
,
2181 unsigned int authsize
)
2183 struct cc_aead_ctx
*ctx
= crypto_aead_ctx(authenc
);
2184 struct device
*dev
= drvdata_to_dev(ctx
->drvdata
);
2186 dev_dbg(dev
, "authsize %d\n", authsize
);
2191 return cc_aead_setauthsize(authenc
, authsize
);
2194 static int cc_rfc4106_gcm_encrypt(struct aead_request
*req
)
2196 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
2199 rc
= crypto_ipsec_check_assoclen(req
->assoclen
);
2203 memset(areq_ctx
, 0, sizeof(*areq_ctx
));
2205 /* No generated IV required */
2206 areq_ctx
->backup_iv
= req
->iv
;
2207 areq_ctx
->assoclen
= req
->assoclen
- GCM_BLOCK_RFC4_IV_SIZE
;
2209 cc_proc_rfc4_gcm(req
);
2211 rc
= cc_proc_aead(req
, DRV_CRYPTO_DIRECTION_ENCRYPT
);
2212 if (rc
!= -EINPROGRESS
&& rc
!= -EBUSY
)
2213 req
->iv
= areq_ctx
->backup_iv
;
2218 static int cc_rfc4543_gcm_encrypt(struct aead_request
*req
)
2220 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
2223 rc
= crypto_ipsec_check_assoclen(req
->assoclen
);
2227 memset(areq_ctx
, 0, sizeof(*areq_ctx
));
2229 //plaintext is not encrypted with rfc4543
2230 areq_ctx
->plaintext_authenticate_only
= true;
2232 /* No generated IV required */
2233 areq_ctx
->backup_iv
= req
->iv
;
2234 areq_ctx
->assoclen
= req
->assoclen
;
2236 cc_proc_rfc4_gcm(req
);
2238 rc
= cc_proc_aead(req
, DRV_CRYPTO_DIRECTION_ENCRYPT
);
2239 if (rc
!= -EINPROGRESS
&& rc
!= -EBUSY
)
2240 req
->iv
= areq_ctx
->backup_iv
;
2245 static int cc_rfc4106_gcm_decrypt(struct aead_request
*req
)
2247 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
2250 rc
= crypto_ipsec_check_assoclen(req
->assoclen
);
2254 memset(areq_ctx
, 0, sizeof(*areq_ctx
));
2256 /* No generated IV required */
2257 areq_ctx
->backup_iv
= req
->iv
;
2258 areq_ctx
->assoclen
= req
->assoclen
- GCM_BLOCK_RFC4_IV_SIZE
;
2260 cc_proc_rfc4_gcm(req
);
2262 rc
= cc_proc_aead(req
, DRV_CRYPTO_DIRECTION_DECRYPT
);
2263 if (rc
!= -EINPROGRESS
&& rc
!= -EBUSY
)
2264 req
->iv
= areq_ctx
->backup_iv
;
2269 static int cc_rfc4543_gcm_decrypt(struct aead_request
*req
)
2271 struct aead_req_ctx
*areq_ctx
= aead_request_ctx_dma(req
);
2274 rc
= crypto_ipsec_check_assoclen(req
->assoclen
);
2278 memset(areq_ctx
, 0, sizeof(*areq_ctx
));
2280 //plaintext is not decrypted with rfc4543
2281 areq_ctx
->plaintext_authenticate_only
= true;
2283 /* No generated IV required */
2284 areq_ctx
->backup_iv
= req
->iv
;
2285 areq_ctx
->assoclen
= req
->assoclen
;
2287 cc_proc_rfc4_gcm(req
);
2289 rc
= cc_proc_aead(req
, DRV_CRYPTO_DIRECTION_DECRYPT
);
2290 if (rc
!= -EINPROGRESS
&& rc
!= -EBUSY
)
2291 req
->iv
= areq_ctx
->backup_iv
;
2297 static struct cc_alg_template aead_algs
[] = {
2299 .name
= "authenc(hmac(sha1),cbc(aes))",
2300 .driver_name
= "authenc-hmac-sha1-cbc-aes-ccree",
2301 .blocksize
= AES_BLOCK_SIZE
,
2303 .setkey
= cc_aead_setkey
,
2304 .setauthsize
= cc_aead_setauthsize
,
2305 .encrypt
= cc_aead_encrypt
,
2306 .decrypt
= cc_aead_decrypt
,
2307 .init
= cc_aead_init
,
2308 .exit
= cc_aead_exit
,
2309 .ivsize
= AES_BLOCK_SIZE
,
2310 .maxauthsize
= SHA1_DIGEST_SIZE
,
2312 .cipher_mode
= DRV_CIPHER_CBC
,
2313 .flow_mode
= S_DIN_to_AES
,
2314 .auth_mode
= DRV_HASH_SHA1
,
2315 .min_hw_rev
= CC_HW_REV_630
,
2316 .std_body
= CC_STD_NIST
,
2319 .name
= "authenc(hmac(sha1),cbc(des3_ede))",
2320 .driver_name
= "authenc-hmac-sha1-cbc-des3-ccree",
2321 .blocksize
= DES3_EDE_BLOCK_SIZE
,
2323 .setkey
= cc_des3_aead_setkey
,
2324 .setauthsize
= cc_aead_setauthsize
,
2325 .encrypt
= cc_aead_encrypt
,
2326 .decrypt
= cc_aead_decrypt
,
2327 .init
= cc_aead_init
,
2328 .exit
= cc_aead_exit
,
2329 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2330 .maxauthsize
= SHA1_DIGEST_SIZE
,
2332 .cipher_mode
= DRV_CIPHER_CBC
,
2333 .flow_mode
= S_DIN_to_DES
,
2334 .auth_mode
= DRV_HASH_SHA1
,
2335 .min_hw_rev
= CC_HW_REV_630
,
2336 .std_body
= CC_STD_NIST
,
2339 .name
= "authenc(hmac(sha256),cbc(aes))",
2340 .driver_name
= "authenc-hmac-sha256-cbc-aes-ccree",
2341 .blocksize
= AES_BLOCK_SIZE
,
2343 .setkey
= cc_aead_setkey
,
2344 .setauthsize
= cc_aead_setauthsize
,
2345 .encrypt
= cc_aead_encrypt
,
2346 .decrypt
= cc_aead_decrypt
,
2347 .init
= cc_aead_init
,
2348 .exit
= cc_aead_exit
,
2349 .ivsize
= AES_BLOCK_SIZE
,
2350 .maxauthsize
= SHA256_DIGEST_SIZE
,
2352 .cipher_mode
= DRV_CIPHER_CBC
,
2353 .flow_mode
= S_DIN_to_AES
,
2354 .auth_mode
= DRV_HASH_SHA256
,
2355 .min_hw_rev
= CC_HW_REV_630
,
2356 .std_body
= CC_STD_NIST
,
2359 .name
= "authenc(hmac(sha256),cbc(des3_ede))",
2360 .driver_name
= "authenc-hmac-sha256-cbc-des3-ccree",
2361 .blocksize
= DES3_EDE_BLOCK_SIZE
,
2363 .setkey
= cc_des3_aead_setkey
,
2364 .setauthsize
= cc_aead_setauthsize
,
2365 .encrypt
= cc_aead_encrypt
,
2366 .decrypt
= cc_aead_decrypt
,
2367 .init
= cc_aead_init
,
2368 .exit
= cc_aead_exit
,
2369 .ivsize
= DES3_EDE_BLOCK_SIZE
,
2370 .maxauthsize
= SHA256_DIGEST_SIZE
,
2372 .cipher_mode
= DRV_CIPHER_CBC
,
2373 .flow_mode
= S_DIN_to_DES
,
2374 .auth_mode
= DRV_HASH_SHA256
,
2375 .min_hw_rev
= CC_HW_REV_630
,
2376 .std_body
= CC_STD_NIST
,
2379 .name
= "authenc(xcbc(aes),cbc(aes))",
2380 .driver_name
= "authenc-xcbc-aes-cbc-aes-ccree",
2381 .blocksize
= AES_BLOCK_SIZE
,
2383 .setkey
= cc_aead_setkey
,
2384 .setauthsize
= cc_aead_setauthsize
,
2385 .encrypt
= cc_aead_encrypt
,
2386 .decrypt
= cc_aead_decrypt
,
2387 .init
= cc_aead_init
,
2388 .exit
= cc_aead_exit
,
2389 .ivsize
= AES_BLOCK_SIZE
,
2390 .maxauthsize
= AES_BLOCK_SIZE
,
2392 .cipher_mode
= DRV_CIPHER_CBC
,
2393 .flow_mode
= S_DIN_to_AES
,
2394 .auth_mode
= DRV_HASH_XCBC_MAC
,
2395 .min_hw_rev
= CC_HW_REV_630
,
2396 .std_body
= CC_STD_NIST
,
2399 .name
= "authenc(hmac(sha1),rfc3686(ctr(aes)))",
2400 .driver_name
= "authenc-hmac-sha1-rfc3686-ctr-aes-ccree",
2403 .setkey
= cc_aead_setkey
,
2404 .setauthsize
= cc_aead_setauthsize
,
2405 .encrypt
= cc_aead_encrypt
,
2406 .decrypt
= cc_aead_decrypt
,
2407 .init
= cc_aead_init
,
2408 .exit
= cc_aead_exit
,
2409 .ivsize
= CTR_RFC3686_IV_SIZE
,
2410 .maxauthsize
= SHA1_DIGEST_SIZE
,
2412 .cipher_mode
= DRV_CIPHER_CTR
,
2413 .flow_mode
= S_DIN_to_AES
,
2414 .auth_mode
= DRV_HASH_SHA1
,
2415 .min_hw_rev
= CC_HW_REV_630
,
2416 .std_body
= CC_STD_NIST
,
2419 .name
= "authenc(hmac(sha256),rfc3686(ctr(aes)))",
2420 .driver_name
= "authenc-hmac-sha256-rfc3686-ctr-aes-ccree",
2423 .setkey
= cc_aead_setkey
,
2424 .setauthsize
= cc_aead_setauthsize
,
2425 .encrypt
= cc_aead_encrypt
,
2426 .decrypt
= cc_aead_decrypt
,
2427 .init
= cc_aead_init
,
2428 .exit
= cc_aead_exit
,
2429 .ivsize
= CTR_RFC3686_IV_SIZE
,
2430 .maxauthsize
= SHA256_DIGEST_SIZE
,
2432 .cipher_mode
= DRV_CIPHER_CTR
,
2433 .flow_mode
= S_DIN_to_AES
,
2434 .auth_mode
= DRV_HASH_SHA256
,
2435 .min_hw_rev
= CC_HW_REV_630
,
2436 .std_body
= CC_STD_NIST
,
2439 .name
= "authenc(xcbc(aes),rfc3686(ctr(aes)))",
2440 .driver_name
= "authenc-xcbc-aes-rfc3686-ctr-aes-ccree",
2443 .setkey
= cc_aead_setkey
,
2444 .setauthsize
= cc_aead_setauthsize
,
2445 .encrypt
= cc_aead_encrypt
,
2446 .decrypt
= cc_aead_decrypt
,
2447 .init
= cc_aead_init
,
2448 .exit
= cc_aead_exit
,
2449 .ivsize
= CTR_RFC3686_IV_SIZE
,
2450 .maxauthsize
= AES_BLOCK_SIZE
,
2452 .cipher_mode
= DRV_CIPHER_CTR
,
2453 .flow_mode
= S_DIN_to_AES
,
2454 .auth_mode
= DRV_HASH_XCBC_MAC
,
2455 .min_hw_rev
= CC_HW_REV_630
,
2456 .std_body
= CC_STD_NIST
,
2460 .driver_name
= "ccm-aes-ccree",
2463 .setkey
= cc_aead_setkey
,
2464 .setauthsize
= cc_ccm_setauthsize
,
2465 .encrypt
= cc_aead_encrypt
,
2466 .decrypt
= cc_aead_decrypt
,
2467 .init
= cc_aead_init
,
2468 .exit
= cc_aead_exit
,
2469 .ivsize
= AES_BLOCK_SIZE
,
2470 .maxauthsize
= AES_BLOCK_SIZE
,
2472 .cipher_mode
= DRV_CIPHER_CCM
,
2473 .flow_mode
= S_DIN_to_AES
,
2474 .auth_mode
= DRV_HASH_NULL
,
2475 .min_hw_rev
= CC_HW_REV_630
,
2476 .std_body
= CC_STD_NIST
,
2479 .name
= "rfc4309(ccm(aes))",
2480 .driver_name
= "rfc4309-ccm-aes-ccree",
2483 .setkey
= cc_rfc4309_ccm_setkey
,
2484 .setauthsize
= cc_rfc4309_ccm_setauthsize
,
2485 .encrypt
= cc_rfc4309_ccm_encrypt
,
2486 .decrypt
= cc_rfc4309_ccm_decrypt
,
2487 .init
= cc_aead_init
,
2488 .exit
= cc_aead_exit
,
2489 .ivsize
= CCM_BLOCK_IV_SIZE
,
2490 .maxauthsize
= AES_BLOCK_SIZE
,
2492 .cipher_mode
= DRV_CIPHER_CCM
,
2493 .flow_mode
= S_DIN_to_AES
,
2494 .auth_mode
= DRV_HASH_NULL
,
2495 .min_hw_rev
= CC_HW_REV_630
,
2496 .std_body
= CC_STD_NIST
,
2500 .driver_name
= "gcm-aes-ccree",
2503 .setkey
= cc_aead_setkey
,
2504 .setauthsize
= cc_gcm_setauthsize
,
2505 .encrypt
= cc_aead_encrypt
,
2506 .decrypt
= cc_aead_decrypt
,
2507 .init
= cc_aead_init
,
2508 .exit
= cc_aead_exit
,
2510 .maxauthsize
= AES_BLOCK_SIZE
,
2512 .cipher_mode
= DRV_CIPHER_GCTR
,
2513 .flow_mode
= S_DIN_to_AES
,
2514 .auth_mode
= DRV_HASH_NULL
,
2515 .min_hw_rev
= CC_HW_REV_630
,
2516 .std_body
= CC_STD_NIST
,
2519 .name
= "rfc4106(gcm(aes))",
2520 .driver_name
= "rfc4106-gcm-aes-ccree",
2523 .setkey
= cc_rfc4106_gcm_setkey
,
2524 .setauthsize
= cc_rfc4106_gcm_setauthsize
,
2525 .encrypt
= cc_rfc4106_gcm_encrypt
,
2526 .decrypt
= cc_rfc4106_gcm_decrypt
,
2527 .init
= cc_aead_init
,
2528 .exit
= cc_aead_exit
,
2529 .ivsize
= GCM_BLOCK_RFC4_IV_SIZE
,
2530 .maxauthsize
= AES_BLOCK_SIZE
,
2532 .cipher_mode
= DRV_CIPHER_GCTR
,
2533 .flow_mode
= S_DIN_to_AES
,
2534 .auth_mode
= DRV_HASH_NULL
,
2535 .min_hw_rev
= CC_HW_REV_630
,
2536 .std_body
= CC_STD_NIST
,
2539 .name
= "rfc4543(gcm(aes))",
2540 .driver_name
= "rfc4543-gcm-aes-ccree",
2543 .setkey
= cc_rfc4543_gcm_setkey
,
2544 .setauthsize
= cc_rfc4543_gcm_setauthsize
,
2545 .encrypt
= cc_rfc4543_gcm_encrypt
,
2546 .decrypt
= cc_rfc4543_gcm_decrypt
,
2547 .init
= cc_aead_init
,
2548 .exit
= cc_aead_exit
,
2549 .ivsize
= GCM_BLOCK_RFC4_IV_SIZE
,
2550 .maxauthsize
= AES_BLOCK_SIZE
,
2552 .cipher_mode
= DRV_CIPHER_GCTR
,
2553 .flow_mode
= S_DIN_to_AES
,
2554 .auth_mode
= DRV_HASH_NULL
,
2555 .min_hw_rev
= CC_HW_REV_630
,
2556 .std_body
= CC_STD_NIST
,
2560 static struct cc_crypto_alg
*cc_create_aead_alg(struct cc_alg_template
*tmpl
,
2563 struct cc_crypto_alg
*t_alg
;
2564 struct aead_alg
*alg
;
2566 t_alg
= devm_kzalloc(dev
, sizeof(*t_alg
), GFP_KERNEL
);
2568 return ERR_PTR(-ENOMEM
);
2570 alg
= &tmpl
->template_aead
;
2572 if (snprintf(alg
->base
.cra_name
, CRYPTO_MAX_ALG_NAME
, "%s",
2573 tmpl
->name
) >= CRYPTO_MAX_ALG_NAME
)
2574 return ERR_PTR(-EINVAL
);
2575 if (snprintf(alg
->base
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
, "%s",
2576 tmpl
->driver_name
) >= CRYPTO_MAX_ALG_NAME
)
2577 return ERR_PTR(-EINVAL
);
2579 alg
->base
.cra_module
= THIS_MODULE
;
2580 alg
->base
.cra_priority
= CC_CRA_PRIO
;
2582 alg
->base
.cra_ctxsize
= sizeof(struct cc_aead_ctx
);
2583 alg
->base
.cra_flags
= CRYPTO_ALG_ASYNC
| CRYPTO_ALG_KERN_DRIVER_ONLY
;
2584 alg
->base
.cra_blocksize
= tmpl
->blocksize
;
2585 alg
->init
= cc_aead_init
;
2586 alg
->exit
= cc_aead_exit
;
2588 t_alg
->aead_alg
= *alg
;
2590 t_alg
->cipher_mode
= tmpl
->cipher_mode
;
2591 t_alg
->flow_mode
= tmpl
->flow_mode
;
2592 t_alg
->auth_mode
= tmpl
->auth_mode
;
2597 int cc_aead_free(struct cc_drvdata
*drvdata
)
2599 struct cc_crypto_alg
*t_alg
, *n
;
2600 struct cc_aead_handle
*aead_handle
= drvdata
->aead_handle
;
2602 /* Remove registered algs */
2603 list_for_each_entry_safe(t_alg
, n
, &aead_handle
->aead_list
, entry
) {
2604 crypto_unregister_aead(&t_alg
->aead_alg
);
2605 list_del(&t_alg
->entry
);
2611 int cc_aead_alloc(struct cc_drvdata
*drvdata
)
2613 struct cc_aead_handle
*aead_handle
;
2614 struct cc_crypto_alg
*t_alg
;
2617 struct device
*dev
= drvdata_to_dev(drvdata
);
2619 aead_handle
= devm_kmalloc(dev
, sizeof(*aead_handle
), GFP_KERNEL
);
2625 INIT_LIST_HEAD(&aead_handle
->aead_list
);
2626 drvdata
->aead_handle
= aead_handle
;
2628 aead_handle
->sram_workspace_addr
= cc_sram_alloc(drvdata
,
2629 MAX_HMAC_DIGEST_SIZE
);
2631 if (aead_handle
->sram_workspace_addr
== NULL_SRAM_ADDR
) {
2637 for (alg
= 0; alg
< ARRAY_SIZE(aead_algs
); alg
++) {
2638 if ((aead_algs
[alg
].min_hw_rev
> drvdata
->hw_rev
) ||
2639 !(drvdata
->std_bodies
& aead_algs
[alg
].std_body
))
2642 t_alg
= cc_create_aead_alg(&aead_algs
[alg
], dev
);
2643 if (IS_ERR(t_alg
)) {
2644 rc
= PTR_ERR(t_alg
);
2645 dev_err(dev
, "%s alg allocation failed\n",
2646 aead_algs
[alg
].driver_name
);
2649 t_alg
->drvdata
= drvdata
;
2650 rc
= crypto_register_aead(&t_alg
->aead_alg
);
2652 dev_err(dev
, "%s alg registration failed\n",
2653 t_alg
->aead_alg
.base
.cra_driver_name
);
2657 list_add_tail(&t_alg
->entry
, &aead_handle
->aead_list
);
2658 dev_dbg(dev
, "Registered %s\n",
2659 t_alg
->aead_alg
.base
.cra_driver_name
);
2665 cc_aead_free(drvdata
);