GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / staging / ccree / ssi_cipher.c
1 /*
2  * Copyright (C) 2012-2017 ARM Limited or its affiliates.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/semaphore.h>
21 #include <crypto/algapi.h>
22 #include <crypto/internal/skcipher.h>
23 #include <crypto/aes.h>
24 #include <crypto/ctr.h>
25 #include <crypto/des.h>
26 #include <crypto/xts.h>
27 #include <crypto/scatterwalk.h>
28
29 #include "ssi_config.h"
30 #include "ssi_driver.h"
31 #include "cc_lli_defs.h"
32 #include "ssi_buffer_mgr.h"
33 #include "ssi_cipher.h"
34 #include "ssi_request_mgr.h"
35 #include "ssi_sysfs.h"
36
37 #define MAX_ABLKCIPHER_SEQ_LEN 6
38
39 #define template_ablkcipher     template_u.ablkcipher
40
41 #define SSI_MIN_AES_XTS_SIZE 0x10
42 #define SSI_MAX_AES_XTS_SIZE 0x2000
43 struct ssi_blkcipher_handle {
44         struct list_head blkcipher_alg_list;
45 };
46
47 struct cc_user_key_info {
48         u8 *key;
49         dma_addr_t key_dma_addr;
50 };
51
52 struct cc_hw_key_info {
53         enum cc_hw_crypto_key key1_slot;
54         enum cc_hw_crypto_key key2_slot;
55 };
56
57 struct ssi_ablkcipher_ctx {
58         struct ssi_drvdata *drvdata;
59         int keylen;
60         int key_round_number;
61         int cipher_mode;
62         int flow_mode;
63         unsigned int flags;
64         struct blkcipher_req_ctx *sync_ctx;
65         struct cc_user_key_info user;
66         struct cc_hw_key_info hw;
67         struct crypto_shash *shash_tfm;
68 };
69
70 static void ssi_ablkcipher_complete(struct device *dev, void *ssi_req, void __iomem *cc_base);
71
72 static int validate_keys_sizes(struct ssi_ablkcipher_ctx *ctx_p, u32 size)
73 {
74         switch (ctx_p->flow_mode) {
75         case S_DIN_to_AES:
76                 switch (size) {
77                 case CC_AES_128_BIT_KEY_SIZE:
78                 case CC_AES_192_BIT_KEY_SIZE:
79                         if (likely((ctx_p->cipher_mode != DRV_CIPHER_XTS) &&
80                                    (ctx_p->cipher_mode != DRV_CIPHER_ESSIV) &&
81                                    (ctx_p->cipher_mode != DRV_CIPHER_BITLOCKER)))
82                                 return 0;
83                         break;
84                 case CC_AES_256_BIT_KEY_SIZE:
85                         return 0;
86                 case (CC_AES_192_BIT_KEY_SIZE * 2):
87                 case (CC_AES_256_BIT_KEY_SIZE * 2):
88                         if (likely((ctx_p->cipher_mode == DRV_CIPHER_XTS) ||
89                                    (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) ||
90                                    (ctx_p->cipher_mode == DRV_CIPHER_BITLOCKER)))
91                                 return 0;
92                         break;
93                 default:
94                         break;
95                 }
96         case S_DIN_to_DES:
97                 if (likely(size == DES3_EDE_KEY_SIZE || size == DES_KEY_SIZE))
98                         return 0;
99                 break;
100 #if SSI_CC_HAS_MULTI2
101         case S_DIN_to_MULTI2:
102                 if (likely(size == CC_MULTI2_SYSTEM_N_DATA_KEY_SIZE))
103                         return 0;
104                 break;
105 #endif
106         default:
107                 break;
108         }
109         return -EINVAL;
110 }
111
112 static int validate_data_size(struct ssi_ablkcipher_ctx *ctx_p, unsigned int size)
113 {
114         switch (ctx_p->flow_mode) {
115         case S_DIN_to_AES:
116                 switch (ctx_p->cipher_mode) {
117                 case DRV_CIPHER_XTS:
118                         if ((size >= SSI_MIN_AES_XTS_SIZE) &&
119                             (size <= SSI_MAX_AES_XTS_SIZE) &&
120                             IS_ALIGNED(size, AES_BLOCK_SIZE))
121                                 return 0;
122                         break;
123                 case DRV_CIPHER_CBC_CTS:
124                         if (likely(size >= AES_BLOCK_SIZE))
125                                 return 0;
126                         break;
127                 case DRV_CIPHER_OFB:
128                 case DRV_CIPHER_CTR:
129                                 return 0;
130                 case DRV_CIPHER_ECB:
131                 case DRV_CIPHER_CBC:
132                 case DRV_CIPHER_ESSIV:
133                 case DRV_CIPHER_BITLOCKER:
134                         if (likely(IS_ALIGNED(size, AES_BLOCK_SIZE)))
135                                 return 0;
136                         break;
137                 default:
138                         break;
139                 }
140                 break;
141         case S_DIN_to_DES:
142                 if (likely(IS_ALIGNED(size, DES_BLOCK_SIZE)))
143                                 return 0;
144                 break;
145 #if SSI_CC_HAS_MULTI2
146         case S_DIN_to_MULTI2:
147                 switch (ctx_p->cipher_mode) {
148                 case DRV_MULTI2_CBC:
149                         if (likely(IS_ALIGNED(size, CC_MULTI2_BLOCK_SIZE)))
150                                 return 0;
151                         break;
152                 case DRV_MULTI2_OFB:
153                         return 0;
154                 default:
155                         break;
156                 }
157                 break;
158 #endif /*SSI_CC_HAS_MULTI2*/
159         default:
160                 break;
161         }
162         return -EINVAL;
163 }
164
165 static unsigned int get_max_keysize(struct crypto_tfm *tfm)
166 {
167         struct ssi_crypto_alg *ssi_alg = container_of(tfm->__crt_alg, struct ssi_crypto_alg, crypto_alg);
168
169         if ((ssi_alg->crypto_alg.cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_ABLKCIPHER)
170                 return ssi_alg->crypto_alg.cra_ablkcipher.max_keysize;
171
172         if ((ssi_alg->crypto_alg.cra_flags & CRYPTO_ALG_TYPE_MASK) == CRYPTO_ALG_TYPE_BLKCIPHER)
173                 return ssi_alg->crypto_alg.cra_blkcipher.max_keysize;
174
175         return 0;
176 }
177
178 static int ssi_blkcipher_init(struct crypto_tfm *tfm)
179 {
180         struct ssi_ablkcipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
181         struct crypto_alg *alg = tfm->__crt_alg;
182         struct ssi_crypto_alg *ssi_alg =
183                         container_of(alg, struct ssi_crypto_alg, crypto_alg);
184         struct device *dev;
185         int rc = 0;
186         unsigned int max_key_buf_size = get_max_keysize(tfm);
187
188         SSI_LOG_DEBUG("Initializing context @%p for %s\n",
189                       ctx_p, crypto_tfm_alg_name(tfm));
190
191         ctx_p->cipher_mode = ssi_alg->cipher_mode;
192         ctx_p->flow_mode = ssi_alg->flow_mode;
193         ctx_p->drvdata = ssi_alg->drvdata;
194         dev = &ctx_p->drvdata->plat_dev->dev;
195
196         /* Allocate key buffer, cache line aligned */
197         ctx_p->user.key = kmalloc(max_key_buf_size, GFP_KERNEL | GFP_DMA);
198         if (!ctx_p->user.key) {
199                 SSI_LOG_ERR("Allocating key buffer in context failed\n");
200                 rc = -ENOMEM;
201         }
202         SSI_LOG_DEBUG("Allocated key buffer in context. key=@%p\n",
203                       ctx_p->user.key);
204
205         /* Map key buffer */
206         ctx_p->user.key_dma_addr = dma_map_single(dev, (void *)ctx_p->user.key,
207                                                   max_key_buf_size,
208                                                   DMA_TO_DEVICE);
209         if (dma_mapping_error(dev, ctx_p->user.key_dma_addr)) {
210                 SSI_LOG_ERR("Mapping Key %u B at va=%pK for DMA failed\n",
211                             max_key_buf_size, ctx_p->user.key);
212                 return -ENOMEM;
213         }
214         SSI_LOG_DEBUG("Mapped key %u B at va=%pK to dma=%pad\n",
215                       max_key_buf_size, ctx_p->user.key,
216                       ctx_p->user.key_dma_addr);
217
218         if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
219                 /* Alloc hash tfm for essiv */
220                 ctx_p->shash_tfm = crypto_alloc_shash("sha256-generic", 0, 0);
221                 if (IS_ERR(ctx_p->shash_tfm)) {
222                         SSI_LOG_ERR("Error allocating hash tfm for ESSIV.\n");
223                         return PTR_ERR(ctx_p->shash_tfm);
224                 }
225         }
226
227         return rc;
228 }
229
230 static void ssi_blkcipher_exit(struct crypto_tfm *tfm)
231 {
232         struct ssi_ablkcipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
233         struct device *dev = &ctx_p->drvdata->plat_dev->dev;
234         unsigned int max_key_buf_size = get_max_keysize(tfm);
235
236         SSI_LOG_DEBUG("Clearing context @%p for %s\n",
237                       crypto_tfm_ctx(tfm), crypto_tfm_alg_name(tfm));
238
239         if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
240                 /* Free hash tfm for essiv */
241                 crypto_free_shash(ctx_p->shash_tfm);
242                 ctx_p->shash_tfm = NULL;
243         }
244
245         /* Unmap key buffer */
246         dma_unmap_single(dev, ctx_p->user.key_dma_addr, max_key_buf_size,
247                          DMA_TO_DEVICE);
248         SSI_LOG_DEBUG("Unmapped key buffer key_dma_addr=%pad\n",
249                       ctx_p->user.key_dma_addr);
250
251         /* Free key buffer in context */
252         kfree(ctx_p->user.key);
253         SSI_LOG_DEBUG("Free key buffer in context. key=@%p\n", ctx_p->user.key);
254 }
255
256 struct tdes_keys {
257         u8      key1[DES_KEY_SIZE];
258         u8      key2[DES_KEY_SIZE];
259         u8      key3[DES_KEY_SIZE];
260 };
261
262 static const u8 zero_buff[] = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
263                                 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
264                                 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
265                                 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
266
267 /* The function verifies that tdes keys are not weak.*/
268 static int ssi_verify_3des_keys(const u8 *key, unsigned int keylen)
269 {
270         struct tdes_keys *tdes_key = (struct tdes_keys *)key;
271
272         /* verify key1 != key2 and key3 != key2*/
273         if (unlikely((memcmp((u8 *)tdes_key->key1, (u8 *)tdes_key->key2, sizeof(tdes_key->key1)) == 0) ||
274                      (memcmp((u8 *)tdes_key->key3, (u8 *)tdes_key->key2, sizeof(tdes_key->key3)) == 0))) {
275                 return -ENOEXEC;
276         }
277
278         return 0;
279 }
280
281 static enum cc_hw_crypto_key hw_key_to_cc_hw_key(int slot_num)
282 {
283         switch (slot_num) {
284         case 0:
285                 return KFDE0_KEY;
286         case 1:
287                 return KFDE1_KEY;
288         case 2:
289                 return KFDE2_KEY;
290         case 3:
291                 return KFDE3_KEY;
292         }
293         return END_OF_KEYS;
294 }
295
296 static int ssi_blkcipher_setkey(struct crypto_tfm *tfm,
297                                 const u8 *key,
298                                 unsigned int keylen)
299 {
300         struct ssi_ablkcipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
301         struct device *dev = &ctx_p->drvdata->plat_dev->dev;
302         u32 tmp[DES_EXPKEY_WORDS];
303         unsigned int max_key_buf_size = get_max_keysize(tfm);
304
305         SSI_LOG_DEBUG("Setting key in context @%p for %s. keylen=%u\n",
306                       ctx_p, crypto_tfm_alg_name(tfm), keylen);
307         dump_byte_array("key", (u8 *)key, keylen);
308
309         SSI_LOG_DEBUG("after FIPS check");
310
311         /* STAT_PHASE_0: Init and sanity checks */
312
313 #if SSI_CC_HAS_MULTI2
314         /*last byte of key buffer is round number and should not be a part of key size*/
315         if (ctx_p->flow_mode == S_DIN_to_MULTI2)
316                 keylen -= 1;
317 #endif /*SSI_CC_HAS_MULTI2*/
318
319         if (unlikely(validate_keys_sizes(ctx_p, keylen) != 0)) {
320                 SSI_LOG_ERR("Unsupported key size %d.\n", keylen);
321                 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
322                 return -EINVAL;
323         }
324
325         if (ssi_is_hw_key(tfm)) {
326                 /* setting HW key slots */
327                 struct arm_hw_key_info *hki = (struct arm_hw_key_info *)key;
328
329                 if (unlikely(ctx_p->flow_mode != S_DIN_to_AES)) {
330                         SSI_LOG_ERR("HW key not supported for non-AES flows\n");
331                         return -EINVAL;
332                 }
333
334                 ctx_p->hw.key1_slot = hw_key_to_cc_hw_key(hki->hw_key1);
335                 if (unlikely(ctx_p->hw.key1_slot == END_OF_KEYS)) {
336                         SSI_LOG_ERR("Unsupported hw key1 number (%d)\n", hki->hw_key1);
337                         return -EINVAL;
338                 }
339
340                 if ((ctx_p->cipher_mode == DRV_CIPHER_XTS) ||
341                     (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) ||
342                     (ctx_p->cipher_mode == DRV_CIPHER_BITLOCKER)) {
343                         if (unlikely(hki->hw_key1 == hki->hw_key2)) {
344                                 SSI_LOG_ERR("Illegal hw key numbers (%d,%d)\n", hki->hw_key1, hki->hw_key2);
345                                 return -EINVAL;
346                         }
347                         ctx_p->hw.key2_slot = hw_key_to_cc_hw_key(hki->hw_key2);
348                         if (unlikely(ctx_p->hw.key2_slot == END_OF_KEYS)) {
349                                 SSI_LOG_ERR("Unsupported hw key2 number (%d)\n", hki->hw_key2);
350                                 return -EINVAL;
351                         }
352                 }
353
354                 ctx_p->keylen = keylen;
355                 SSI_LOG_DEBUG("ssi_is_hw_key ret 0");
356
357                 return 0;
358         }
359
360         // verify weak keys
361         if (ctx_p->flow_mode == S_DIN_to_DES) {
362                 if (unlikely(!des_ekey(tmp, key)) &&
363                     (crypto_tfm_get_flags(tfm) & CRYPTO_TFM_REQ_WEAK_KEY)) {
364                         tfm->crt_flags |= CRYPTO_TFM_RES_WEAK_KEY;
365                         SSI_LOG_DEBUG("weak DES key");
366                         return -EINVAL;
367                 }
368         }
369         if ((ctx_p->cipher_mode == DRV_CIPHER_XTS) &&
370             xts_check_key(tfm, key, keylen) != 0) {
371                 SSI_LOG_DEBUG("weak XTS key");
372                 return -EINVAL;
373         }
374         if ((ctx_p->flow_mode == S_DIN_to_DES) &&
375             (keylen == DES3_EDE_KEY_SIZE) &&
376             ssi_verify_3des_keys(key, keylen) != 0) {
377                 SSI_LOG_DEBUG("weak 3DES key");
378                 return -EINVAL;
379         }
380
381         /* STAT_PHASE_1: Copy key to ctx */
382         dma_sync_single_for_cpu(dev, ctx_p->user.key_dma_addr,
383                                 max_key_buf_size, DMA_TO_DEVICE);
384
385         if (ctx_p->flow_mode == S_DIN_to_MULTI2) {
386 #if SSI_CC_HAS_MULTI2
387                 memcpy(ctx_p->user.key, key, CC_MULTI2_SYSTEM_N_DATA_KEY_SIZE);
388                 ctx_p->key_round_number = key[CC_MULTI2_SYSTEM_N_DATA_KEY_SIZE];
389                 if (ctx_p->key_round_number < CC_MULTI2_MIN_NUM_ROUNDS ||
390                     ctx_p->key_round_number > CC_MULTI2_MAX_NUM_ROUNDS) {
391                         crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
392                         SSI_LOG_DEBUG("SSI_CC_HAS_MULTI2 einval");
393                         return -EINVAL;
394 #endif /*SSI_CC_HAS_MULTI2*/
395         } else {
396                 memcpy(ctx_p->user.key, key, keylen);
397                 if (keylen == 24)
398                         memset(ctx_p->user.key + 24, 0, CC_AES_KEY_SIZE_MAX - 24);
399
400                 if (ctx_p->cipher_mode == DRV_CIPHER_ESSIV) {
401                         /* sha256 for key2 - use sw implementation */
402                         int key_len = keylen >> 1;
403                         int err;
404                         SHASH_DESC_ON_STACK(desc, ctx_p->shash_tfm);
405
406                         desc->tfm = ctx_p->shash_tfm;
407
408                         err = crypto_shash_digest(desc, ctx_p->user.key, key_len, ctx_p->user.key + key_len);
409                         if (err) {
410                                 SSI_LOG_ERR("Failed to hash ESSIV key.\n");
411                                 return err;
412                         }
413                 }
414         }
415         dma_sync_single_for_device(dev, ctx_p->user.key_dma_addr,
416                                    max_key_buf_size, DMA_TO_DEVICE);
417         ctx_p->keylen = keylen;
418
419          SSI_LOG_DEBUG("return safely");
420         return 0;
421 }
422
423 static inline void
424 ssi_blkcipher_create_setup_desc(
425         struct crypto_tfm *tfm,
426         struct blkcipher_req_ctx *req_ctx,
427         unsigned int ivsize,
428         unsigned int nbytes,
429         struct cc_hw_desc desc[],
430         unsigned int *seq_size)
431 {
432         struct ssi_ablkcipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
433         int cipher_mode = ctx_p->cipher_mode;
434         int flow_mode = ctx_p->flow_mode;
435         int direction = req_ctx->gen_ctx.op_type;
436         dma_addr_t key_dma_addr = ctx_p->user.key_dma_addr;
437         unsigned int key_len = ctx_p->keylen;
438         dma_addr_t iv_dma_addr = req_ctx->gen_ctx.iv_dma_addr;
439         unsigned int du_size = nbytes;
440
441         struct ssi_crypto_alg *ssi_alg = container_of(tfm->__crt_alg, struct ssi_crypto_alg, crypto_alg);
442
443         if ((ssi_alg->crypto_alg.cra_flags & CRYPTO_ALG_BULK_MASK) == CRYPTO_ALG_BULK_DU_512)
444                 du_size = 512;
445         if ((ssi_alg->crypto_alg.cra_flags & CRYPTO_ALG_BULK_MASK) == CRYPTO_ALG_BULK_DU_4096)
446                 du_size = 4096;
447
448         switch (cipher_mode) {
449         case DRV_CIPHER_CBC:
450         case DRV_CIPHER_CBC_CTS:
451         case DRV_CIPHER_CTR:
452         case DRV_CIPHER_OFB:
453                 /* Load cipher state */
454                 hw_desc_init(&desc[*seq_size]);
455                 set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr, ivsize,
456                              NS_BIT);
457                 set_cipher_config0(&desc[*seq_size], direction);
458                 set_flow_mode(&desc[*seq_size], flow_mode);
459                 set_cipher_mode(&desc[*seq_size], cipher_mode);
460                 if ((cipher_mode == DRV_CIPHER_CTR) ||
461                     (cipher_mode == DRV_CIPHER_OFB)) {
462                         set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
463                 } else {
464                         set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE0);
465                 }
466                 (*seq_size)++;
467                 /*FALLTHROUGH*/
468         case DRV_CIPHER_ECB:
469                 /* Load key */
470                 hw_desc_init(&desc[*seq_size]);
471                 set_cipher_mode(&desc[*seq_size], cipher_mode);
472                 set_cipher_config0(&desc[*seq_size], direction);
473                 if (flow_mode == S_DIN_to_AES) {
474                         if (ssi_is_hw_key(tfm)) {
475                                 set_hw_crypto_key(&desc[*seq_size],
476                                                   ctx_p->hw.key1_slot);
477                         } else {
478                                 set_din_type(&desc[*seq_size], DMA_DLLI,
479                                              key_dma_addr, ((key_len == 24) ?
480                                                             AES_MAX_KEY_SIZE :
481                                                             key_len), NS_BIT);
482                         }
483                         set_key_size_aes(&desc[*seq_size], key_len);
484                 } else {
485                         /*des*/
486                         set_din_type(&desc[*seq_size], DMA_DLLI, key_dma_addr,
487                                      key_len, NS_BIT);
488                         set_key_size_des(&desc[*seq_size], key_len);
489                 }
490                 set_flow_mode(&desc[*seq_size], flow_mode);
491                 set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
492                 (*seq_size)++;
493                 break;
494         case DRV_CIPHER_XTS:
495         case DRV_CIPHER_ESSIV:
496         case DRV_CIPHER_BITLOCKER:
497                 /* Load AES key */
498                 hw_desc_init(&desc[*seq_size]);
499                 set_cipher_mode(&desc[*seq_size], cipher_mode);
500                 set_cipher_config0(&desc[*seq_size], direction);
501                 if (ssi_is_hw_key(tfm)) {
502                         set_hw_crypto_key(&desc[*seq_size],
503                                           ctx_p->hw.key1_slot);
504                 } else {
505                         set_din_type(&desc[*seq_size], DMA_DLLI, key_dma_addr,
506                                      (key_len / 2), NS_BIT);
507                 }
508                 set_key_size_aes(&desc[*seq_size], (key_len / 2));
509                 set_flow_mode(&desc[*seq_size], flow_mode);
510                 set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
511                 (*seq_size)++;
512
513                 /* load XEX key */
514                 hw_desc_init(&desc[*seq_size]);
515                 set_cipher_mode(&desc[*seq_size], cipher_mode);
516                 set_cipher_config0(&desc[*seq_size], direction);
517                 if (ssi_is_hw_key(tfm)) {
518                         set_hw_crypto_key(&desc[*seq_size],
519                                           ctx_p->hw.key2_slot);
520                 } else {
521                         set_din_type(&desc[*seq_size], DMA_DLLI,
522                                      (key_dma_addr + (key_len / 2)),
523                                      (key_len / 2), NS_BIT);
524                 }
525                 set_xex_data_unit_size(&desc[*seq_size], du_size);
526                 set_flow_mode(&desc[*seq_size], S_DIN_to_AES2);
527                 set_key_size_aes(&desc[*seq_size], (key_len / 2));
528                 set_setup_mode(&desc[*seq_size], SETUP_LOAD_XEX_KEY);
529                 (*seq_size)++;
530
531                 /* Set state */
532                 hw_desc_init(&desc[*seq_size]);
533                 set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
534                 set_cipher_mode(&desc[*seq_size], cipher_mode);
535                 set_cipher_config0(&desc[*seq_size], direction);
536                 set_key_size_aes(&desc[*seq_size], (key_len / 2));
537                 set_flow_mode(&desc[*seq_size], flow_mode);
538                 set_din_type(&desc[*seq_size], DMA_DLLI, iv_dma_addr,
539                              CC_AES_BLOCK_SIZE, NS_BIT);
540                 (*seq_size)++;
541                 break;
542         default:
543                 SSI_LOG_ERR("Unsupported cipher mode (%d)\n", cipher_mode);
544                 BUG();
545         }
546 }
547
548 #if SSI_CC_HAS_MULTI2
549 static inline void ssi_blkcipher_create_multi2_setup_desc(
550         struct crypto_tfm *tfm,
551         struct blkcipher_req_ctx *req_ctx,
552         unsigned int ivsize,
553         struct cc_hw_desc desc[],
554         unsigned int *seq_size)
555 {
556         struct ssi_ablkcipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
557
558         int direction = req_ctx->gen_ctx.op_type;
559         /* Load system key */
560         hw_desc_init(&desc[*seq_size]);
561         set_cipher_mode(&desc[*seq_size], ctx_p->cipher_mode);
562         set_cipher_config0(&desc[*seq_size], direction);
563         set_din_type(&desc[*seq_size], DMA_DLLI, ctx_p->user.key_dma_addr,
564                      CC_MULTI2_SYSTEM_KEY_SIZE, NS_BIT);
565         set_flow_mode(&desc[*seq_size], ctx_p->flow_mode);
566         set_setup_mode(&desc[*seq_size], SETUP_LOAD_KEY0);
567         (*seq_size)++;
568
569         /* load data key */
570         hw_desc_init(&desc[*seq_size]);
571         set_din_type(&desc[*seq_size], DMA_DLLI,
572                      (ctx_p->user.key_dma_addr + CC_MULTI2_SYSTEM_KEY_SIZE),
573                      CC_MULTI2_DATA_KEY_SIZE, NS_BIT);
574         set_multi2_num_rounds(&desc[*seq_size], ctx_p->key_round_number);
575         set_flow_mode(&desc[*seq_size], ctx_p->flow_mode);
576         set_cipher_mode(&desc[*seq_size], ctx_p->cipher_mode);
577         set_cipher_config0(&desc[*seq_size], direction);
578         set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE0);
579         (*seq_size)++;
580
581         /* Set state */
582         hw_desc_init(&desc[*seq_size]);
583         set_din_type(&desc[*seq_size], DMA_DLLI, req_ctx->gen_ctx.iv_dma_addr,
584                      ivsize, NS_BIT);
585         set_cipher_config0(&desc[*seq_size], direction);
586         set_flow_mode(&desc[*seq_size], ctx_p->flow_mode);
587         set_cipher_mode(&desc[*seq_size], ctx_p->cipher_mode);
588         set_setup_mode(&desc[*seq_size], SETUP_LOAD_STATE1);
589         (*seq_size)++;
590 }
591 #endif /*SSI_CC_HAS_MULTI2*/
592
593 static inline void
594 ssi_blkcipher_create_data_desc(
595         struct crypto_tfm *tfm,
596         struct blkcipher_req_ctx *req_ctx,
597         struct scatterlist *dst, struct scatterlist *src,
598         unsigned int nbytes,
599         void *areq,
600         struct cc_hw_desc desc[],
601         unsigned int *seq_size)
602 {
603         struct ssi_ablkcipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
604         unsigned int flow_mode = ctx_p->flow_mode;
605
606         switch (ctx_p->flow_mode) {
607         case S_DIN_to_AES:
608                 flow_mode = DIN_AES_DOUT;
609                 break;
610         case S_DIN_to_DES:
611                 flow_mode = DIN_DES_DOUT;
612                 break;
613 #if SSI_CC_HAS_MULTI2
614         case S_DIN_to_MULTI2:
615                 flow_mode = DIN_MULTI2_DOUT;
616                 break;
617 #endif /*SSI_CC_HAS_MULTI2*/
618         default:
619                 SSI_LOG_ERR("invalid flow mode, flow_mode = %d\n", flow_mode);
620                 return;
621         }
622         /* Process */
623         if (likely(req_ctx->dma_buf_type == SSI_DMA_BUF_DLLI)) {
624                 SSI_LOG_DEBUG(" data params addr %pad length 0x%X\n",
625                               sg_dma_address(src), nbytes);
626                 SSI_LOG_DEBUG(" data params addr %pad length 0x%X\n",
627                               sg_dma_address(dst), nbytes);
628                 hw_desc_init(&desc[*seq_size]);
629                 set_din_type(&desc[*seq_size], DMA_DLLI, sg_dma_address(src),
630                              nbytes, NS_BIT);
631                 set_dout_dlli(&desc[*seq_size], sg_dma_address(dst),
632                               nbytes, NS_BIT, (!areq ? 0 : 1));
633                 if (areq)
634                         set_queue_last_ind(&desc[*seq_size]);
635
636                 set_flow_mode(&desc[*seq_size], flow_mode);
637                 (*seq_size)++;
638         } else {
639                 /* bypass */
640                 SSI_LOG_DEBUG(" bypass params addr %pad "
641                              "length 0x%X addr 0x%08X\n",
642                         req_ctx->mlli_params.mlli_dma_addr,
643                         req_ctx->mlli_params.mlli_len,
644                         (unsigned int)ctx_p->drvdata->mlli_sram_addr);
645                 hw_desc_init(&desc[*seq_size]);
646                 set_din_type(&desc[*seq_size], DMA_DLLI,
647                              req_ctx->mlli_params.mlli_dma_addr,
648                              req_ctx->mlli_params.mlli_len, NS_BIT);
649                 set_dout_sram(&desc[*seq_size],
650                               ctx_p->drvdata->mlli_sram_addr,
651                               req_ctx->mlli_params.mlli_len);
652                 set_flow_mode(&desc[*seq_size], BYPASS);
653                 (*seq_size)++;
654
655                 hw_desc_init(&desc[*seq_size]);
656                 set_din_type(&desc[*seq_size], DMA_MLLI,
657                              ctx_p->drvdata->mlli_sram_addr,
658                              req_ctx->in_mlli_nents, NS_BIT);
659                 if (req_ctx->out_nents == 0) {
660                         SSI_LOG_DEBUG(" din/dout params addr 0x%08X "
661                                      "addr 0x%08X\n",
662                         (unsigned int)ctx_p->drvdata->mlli_sram_addr,
663                         (unsigned int)ctx_p->drvdata->mlli_sram_addr);
664                         set_dout_mlli(&desc[*seq_size],
665                                       ctx_p->drvdata->mlli_sram_addr,
666                                       req_ctx->in_mlli_nents, NS_BIT,
667                                       (!areq ? 0 : 1));
668                 } else {
669                         SSI_LOG_DEBUG(" din/dout params "
670                                      "addr 0x%08X addr 0x%08X\n",
671                                 (unsigned int)ctx_p->drvdata->mlli_sram_addr,
672                                 (unsigned int)ctx_p->drvdata->mlli_sram_addr +
673                                 (u32)LLI_ENTRY_BYTE_SIZE *
674                                                         req_ctx->in_nents);
675                         set_dout_mlli(&desc[*seq_size],
676                                       (ctx_p->drvdata->mlli_sram_addr +
677                                        (LLI_ENTRY_BYTE_SIZE *
678                                         req_ctx->in_mlli_nents)),
679                                       req_ctx->out_mlli_nents, NS_BIT,
680                                       (!areq ? 0 : 1));
681                 }
682                 if (areq)
683                         set_queue_last_ind(&desc[*seq_size]);
684
685                 set_flow_mode(&desc[*seq_size], flow_mode);
686                 (*seq_size)++;
687         }
688 }
689
690 static int ssi_blkcipher_complete(struct device *dev,
691                                   struct ssi_ablkcipher_ctx *ctx_p,
692                                   struct blkcipher_req_ctx *req_ctx,
693                                   struct scatterlist *dst,
694                                   struct scatterlist *src,
695                                   unsigned int ivsize,
696                                   void *areq,
697                                   void __iomem *cc_base)
698 {
699         int completion_error = 0;
700         u32 inflight_counter;
701         struct ablkcipher_request *req = (struct ablkcipher_request *)areq;
702
703         ssi_buffer_mgr_unmap_blkcipher_request(dev, req_ctx, ivsize, src, dst);
704
705         /*Set the inflight couter value to local variable*/
706         inflight_counter =  ctx_p->drvdata->inflight_counter;
707         /*Decrease the inflight counter*/
708         if (ctx_p->flow_mode == BYPASS && ctx_p->drvdata->inflight_counter > 0)
709                 ctx_p->drvdata->inflight_counter--;
710
711         if (areq) {
712                 /*
713                  * The crypto API expects us to set the req->info to the last
714                  * ciphertext block. For encrypt, simply copy from the result.
715                  * For decrypt, we must copy from a saved buffer since this
716                  * could be an in-place decryption operation and the src is
717                  * lost by this point.
718                  */
719                 if (req_ctx->gen_ctx.op_type == DRV_CRYPTO_DIRECTION_DECRYPT)  {
720                         memcpy(req->info, req_ctx->backup_info, ivsize);
721                         kfree(req_ctx->backup_info);
722                 } else {
723                         scatterwalk_map_and_copy(req->info, req->dst,
724                                                  (req->nbytes - ivsize),
725                                                  ivsize, 0);
726                 }
727
728                 ablkcipher_request_complete(areq, completion_error);
729                 return 0;
730         }
731         return completion_error;
732 }
733
734 static int ssi_blkcipher_process(
735         struct crypto_tfm *tfm,
736         struct blkcipher_req_ctx *req_ctx,
737         struct scatterlist *dst, struct scatterlist *src,
738         unsigned int nbytes,
739         void *info, //req info
740         unsigned int ivsize,
741         void *areq,
742         enum drv_crypto_direction direction)
743 {
744         struct ssi_ablkcipher_ctx *ctx_p = crypto_tfm_ctx(tfm);
745         struct device *dev = &ctx_p->drvdata->plat_dev->dev;
746         struct cc_hw_desc desc[MAX_ABLKCIPHER_SEQ_LEN];
747         struct ssi_crypto_req ssi_req = {};
748         int rc, seq_len = 0, cts_restore_flag = 0;
749
750         SSI_LOG_DEBUG("%s areq=%p info=%p nbytes=%d\n",
751                       ((direction == DRV_CRYPTO_DIRECTION_ENCRYPT) ? "Encrypt" : "Decrypt"),
752                       areq, info, nbytes);
753
754         /* STAT_PHASE_0: Init and sanity checks */
755
756         /* TODO: check data length according to mode */
757         if (unlikely(validate_data_size(ctx_p, nbytes))) {
758                 SSI_LOG_ERR("Unsupported data size %d.\n", nbytes);
759                 crypto_tfm_set_flags(tfm, CRYPTO_TFM_RES_BAD_BLOCK_LEN);
760                 rc = -EINVAL;
761                 goto exit_process;
762         }
763         if (nbytes == 0) {
764                 /* No data to process is valid */
765                 rc = 0;
766                 goto exit_process;
767         }
768         /*For CTS in case of data size aligned to 16 use CBC mode*/
769         if (((nbytes % AES_BLOCK_SIZE) == 0) && (ctx_p->cipher_mode == DRV_CIPHER_CBC_CTS)) {
770                 ctx_p->cipher_mode = DRV_CIPHER_CBC;
771                 cts_restore_flag = 1;
772         }
773
774         /* Setup DX request structure */
775         ssi_req.user_cb = (void *)ssi_ablkcipher_complete;
776         ssi_req.user_arg = (void *)areq;
777
778 #ifdef ENABLE_CYCLE_COUNT
779         ssi_req.op_type = (direction == DRV_CRYPTO_DIRECTION_DECRYPT) ?
780                 STAT_OP_TYPE_DECODE : STAT_OP_TYPE_ENCODE;
781
782 #endif
783
784         /* Setup request context */
785         req_ctx->gen_ctx.op_type = direction;
786
787         /* STAT_PHASE_1: Map buffers */
788
789         rc = ssi_buffer_mgr_map_blkcipher_request(ctx_p->drvdata, req_ctx, ivsize, nbytes, info, src, dst);
790         if (unlikely(rc != 0)) {
791                 SSI_LOG_ERR("map_request() failed\n");
792                 goto exit_process;
793         }
794
795         /* STAT_PHASE_2: Create sequence */
796
797         /* Setup processing */
798 #if SSI_CC_HAS_MULTI2
799         if (ctx_p->flow_mode == S_DIN_to_MULTI2)
800                 ssi_blkcipher_create_multi2_setup_desc(tfm, req_ctx, ivsize,
801                                                        desc, &seq_len);
802         else
803 #endif /*SSI_CC_HAS_MULTI2*/
804                 ssi_blkcipher_create_setup_desc(tfm, req_ctx, ivsize, nbytes,
805                                                 desc, &seq_len);
806         /* Data processing */
807         ssi_blkcipher_create_data_desc(tfm, req_ctx, dst, src, nbytes, areq,
808                                        desc, &seq_len);
809
810         /* do we need to generate IV? */
811         if (req_ctx->is_giv) {
812                 ssi_req.ivgen_dma_addr[0] = req_ctx->gen_ctx.iv_dma_addr;
813                 ssi_req.ivgen_dma_addr_len = 1;
814                 /* set the IV size (8/16 B long)*/
815                 ssi_req.ivgen_size = ivsize;
816         }
817
818         /* STAT_PHASE_3: Lock HW and push sequence */
819
820         rc = send_request(ctx_p->drvdata, &ssi_req, desc, seq_len, (!areq) ? 0 : 1);
821         if (areq) {
822                 if (unlikely(rc != -EINPROGRESS)) {
823                         /* Failed to send the request or request completed synchronously */
824                         ssi_buffer_mgr_unmap_blkcipher_request(dev, req_ctx, ivsize, src, dst);
825                 }
826
827         } else {
828                 if (rc != 0) {
829                         ssi_buffer_mgr_unmap_blkcipher_request(dev, req_ctx, ivsize, src, dst);
830                 } else {
831                         rc = ssi_blkcipher_complete(dev, ctx_p, req_ctx, dst,
832                                                     src, ivsize, NULL,
833                                                     ctx_p->drvdata->cc_base);
834                 }
835         }
836
837 exit_process:
838         if (cts_restore_flag != 0)
839                 ctx_p->cipher_mode = DRV_CIPHER_CBC_CTS;
840
841         if (rc != -EINPROGRESS)
842                 kfree(req_ctx->backup_info);
843
844         return rc;
845 }
846
847 static void ssi_ablkcipher_complete(struct device *dev, void *ssi_req, void __iomem *cc_base)
848 {
849         struct ablkcipher_request *areq = (struct ablkcipher_request *)ssi_req;
850         struct blkcipher_req_ctx *req_ctx = ablkcipher_request_ctx(areq);
851         struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(areq);
852         struct ssi_ablkcipher_ctx *ctx_p = crypto_ablkcipher_ctx(tfm);
853         unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
854
855         ssi_blkcipher_complete(dev, ctx_p, req_ctx, areq->dst, areq->src,
856                                ivsize, areq, cc_base);
857 }
858
859 /* Async wrap functions */
860
861 static int ssi_ablkcipher_init(struct crypto_tfm *tfm)
862 {
863         struct ablkcipher_tfm *ablktfm = &tfm->crt_ablkcipher;
864
865         ablktfm->reqsize = sizeof(struct blkcipher_req_ctx);
866
867         return ssi_blkcipher_init(tfm);
868 }
869
870 static int ssi_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
871                                  const u8 *key,
872                                  unsigned int keylen)
873 {
874         return ssi_blkcipher_setkey(crypto_ablkcipher_tfm(tfm), key, keylen);
875 }
876
877 static int ssi_ablkcipher_encrypt(struct ablkcipher_request *req)
878 {
879         struct crypto_ablkcipher *ablk_tfm = crypto_ablkcipher_reqtfm(req);
880         struct crypto_tfm *tfm = crypto_ablkcipher_tfm(ablk_tfm);
881         struct blkcipher_req_ctx *req_ctx = ablkcipher_request_ctx(req);
882         unsigned int ivsize = crypto_ablkcipher_ivsize(ablk_tfm);
883
884         req_ctx->is_giv = false;
885
886         return ssi_blkcipher_process(tfm, req_ctx, req->dst, req->src, req->nbytes, req->info, ivsize, (void *)req, DRV_CRYPTO_DIRECTION_ENCRYPT);
887 }
888
889 static int ssi_ablkcipher_decrypt(struct ablkcipher_request *req)
890 {
891         struct crypto_ablkcipher *ablk_tfm = crypto_ablkcipher_reqtfm(req);
892         struct crypto_tfm *tfm = crypto_ablkcipher_tfm(ablk_tfm);
893         struct blkcipher_req_ctx *req_ctx = ablkcipher_request_ctx(req);
894         unsigned int ivsize = crypto_ablkcipher_ivsize(ablk_tfm);
895
896         /*
897          * Allocate and save the last IV sized bytes of the source, which will
898          * be lost in case of in-place decryption and might be needed for CTS.
899          */
900         req_ctx->backup_info = kmalloc(ivsize, GFP_KERNEL);
901         if (!req_ctx->backup_info)
902                 return -ENOMEM;
903
904         scatterwalk_map_and_copy(req_ctx->backup_info, req->src,
905                                  (req->nbytes - ivsize), ivsize, 0);
906         req_ctx->is_giv = false;
907         req_ctx->backup_info = NULL;
908
909         return ssi_blkcipher_process(tfm, req_ctx, req->dst, req->src, req->nbytes, req->info, ivsize, (void *)req, DRV_CRYPTO_DIRECTION_DECRYPT);
910 }
911
912 /* DX Block cipher alg */
913 static struct ssi_alg_template blkcipher_algs[] = {
914 /* Async template */
915 #if SSI_CC_HAS_AES_XTS
916         {
917                 .name = "xts(aes)",
918                 .driver_name = "xts-aes-dx",
919                 .blocksize = AES_BLOCK_SIZE,
920                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
921                 .template_ablkcipher = {
922                         .setkey = ssi_ablkcipher_setkey,
923                         .encrypt = ssi_ablkcipher_encrypt,
924                         .decrypt = ssi_ablkcipher_decrypt,
925                         .min_keysize = AES_MIN_KEY_SIZE * 2,
926                         .max_keysize = AES_MAX_KEY_SIZE * 2,
927                         .ivsize = AES_BLOCK_SIZE,
928                         .geniv = "eseqiv",
929                         },
930                 .cipher_mode = DRV_CIPHER_XTS,
931                 .flow_mode = S_DIN_to_AES,
932         },
933         {
934                 .name = "xts(aes)",
935                 .driver_name = "xts-aes-du512-dx",
936                 .blocksize = AES_BLOCK_SIZE,
937                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_BULK_DU_512,
938                 .template_ablkcipher = {
939                         .setkey = ssi_ablkcipher_setkey,
940                         .encrypt = ssi_ablkcipher_encrypt,
941                         .decrypt = ssi_ablkcipher_decrypt,
942                         .min_keysize = AES_MIN_KEY_SIZE * 2,
943                         .max_keysize = AES_MAX_KEY_SIZE * 2,
944                         .ivsize = AES_BLOCK_SIZE,
945                         },
946                 .cipher_mode = DRV_CIPHER_XTS,
947                 .flow_mode = S_DIN_to_AES,
948         },
949         {
950                 .name = "xts(aes)",
951                 .driver_name = "xts-aes-du4096-dx",
952                 .blocksize = AES_BLOCK_SIZE,
953                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_BULK_DU_4096,
954                 .template_ablkcipher = {
955                         .setkey = ssi_ablkcipher_setkey,
956                         .encrypt = ssi_ablkcipher_encrypt,
957                         .decrypt = ssi_ablkcipher_decrypt,
958                         .min_keysize = AES_MIN_KEY_SIZE * 2,
959                         .max_keysize = AES_MAX_KEY_SIZE * 2,
960                         .ivsize = AES_BLOCK_SIZE,
961                         },
962                 .cipher_mode = DRV_CIPHER_XTS,
963                 .flow_mode = S_DIN_to_AES,
964         },
965 #endif /*SSI_CC_HAS_AES_XTS*/
966 #if SSI_CC_HAS_AES_ESSIV
967         {
968                 .name = "essiv(aes)",
969                 .driver_name = "essiv-aes-dx",
970                 .blocksize = AES_BLOCK_SIZE,
971                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
972                 .template_ablkcipher = {
973                         .setkey = ssi_ablkcipher_setkey,
974                         .encrypt = ssi_ablkcipher_encrypt,
975                         .decrypt = ssi_ablkcipher_decrypt,
976                         .min_keysize = AES_MIN_KEY_SIZE * 2,
977                         .max_keysize = AES_MAX_KEY_SIZE * 2,
978                         .ivsize = AES_BLOCK_SIZE,
979                         },
980                 .cipher_mode = DRV_CIPHER_ESSIV,
981                 .flow_mode = S_DIN_to_AES,
982         },
983         {
984                 .name = "essiv(aes)",
985                 .driver_name = "essiv-aes-du512-dx",
986                 .blocksize = AES_BLOCK_SIZE,
987                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_BULK_DU_512,
988                 .template_ablkcipher = {
989                         .setkey = ssi_ablkcipher_setkey,
990                         .encrypt = ssi_ablkcipher_encrypt,
991                         .decrypt = ssi_ablkcipher_decrypt,
992                         .min_keysize = AES_MIN_KEY_SIZE * 2,
993                         .max_keysize = AES_MAX_KEY_SIZE * 2,
994                         .ivsize = AES_BLOCK_SIZE,
995                         },
996                 .cipher_mode = DRV_CIPHER_ESSIV,
997                 .flow_mode = S_DIN_to_AES,
998         },
999         {
1000                 .name = "essiv(aes)",
1001                 .driver_name = "essiv-aes-du4096-dx",
1002                 .blocksize = AES_BLOCK_SIZE,
1003                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_BULK_DU_4096,
1004                 .template_ablkcipher = {
1005                         .setkey = ssi_ablkcipher_setkey,
1006                         .encrypt = ssi_ablkcipher_encrypt,
1007                         .decrypt = ssi_ablkcipher_decrypt,
1008                         .min_keysize = AES_MIN_KEY_SIZE * 2,
1009                         .max_keysize = AES_MAX_KEY_SIZE * 2,
1010                         .ivsize = AES_BLOCK_SIZE,
1011                         },
1012                 .cipher_mode = DRV_CIPHER_ESSIV,
1013                 .flow_mode = S_DIN_to_AES,
1014         },
1015 #endif /*SSI_CC_HAS_AES_ESSIV*/
1016 #if SSI_CC_HAS_AES_BITLOCKER
1017         {
1018                 .name = "bitlocker(aes)",
1019                 .driver_name = "bitlocker-aes-dx",
1020                 .blocksize = AES_BLOCK_SIZE,
1021                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1022                 .template_ablkcipher = {
1023                         .setkey = ssi_ablkcipher_setkey,
1024                         .encrypt = ssi_ablkcipher_encrypt,
1025                         .decrypt = ssi_ablkcipher_decrypt,
1026                         .min_keysize = AES_MIN_KEY_SIZE * 2,
1027                         .max_keysize = AES_MAX_KEY_SIZE * 2,
1028                         .ivsize = AES_BLOCK_SIZE,
1029                         },
1030                 .cipher_mode = DRV_CIPHER_BITLOCKER,
1031                 .flow_mode = S_DIN_to_AES,
1032         },
1033         {
1034                 .name = "bitlocker(aes)",
1035                 .driver_name = "bitlocker-aes-du512-dx",
1036                 .blocksize = AES_BLOCK_SIZE,
1037                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_BULK_DU_512,
1038                 .template_ablkcipher = {
1039                         .setkey = ssi_ablkcipher_setkey,
1040                         .encrypt = ssi_ablkcipher_encrypt,
1041                         .decrypt = ssi_ablkcipher_decrypt,
1042                         .min_keysize = AES_MIN_KEY_SIZE * 2,
1043                         .max_keysize = AES_MAX_KEY_SIZE * 2,
1044                         .ivsize = AES_BLOCK_SIZE,
1045                         },
1046                 .cipher_mode = DRV_CIPHER_BITLOCKER,
1047                 .flow_mode = S_DIN_to_AES,
1048         },
1049         {
1050                 .name = "bitlocker(aes)",
1051                 .driver_name = "bitlocker-aes-du4096-dx",
1052                 .blocksize = AES_BLOCK_SIZE,
1053                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_BULK_DU_4096,
1054                 .template_ablkcipher = {
1055                         .setkey = ssi_ablkcipher_setkey,
1056                         .encrypt = ssi_ablkcipher_encrypt,
1057                         .decrypt = ssi_ablkcipher_decrypt,
1058                         .min_keysize = AES_MIN_KEY_SIZE * 2,
1059                         .max_keysize = AES_MAX_KEY_SIZE * 2,
1060                         .ivsize = AES_BLOCK_SIZE,
1061                         },
1062                 .cipher_mode = DRV_CIPHER_BITLOCKER,
1063                 .flow_mode = S_DIN_to_AES,
1064         },
1065 #endif /*SSI_CC_HAS_AES_BITLOCKER*/
1066         {
1067                 .name = "ecb(aes)",
1068                 .driver_name = "ecb-aes-dx",
1069                 .blocksize = AES_BLOCK_SIZE,
1070                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1071                 .template_ablkcipher = {
1072                         .setkey = ssi_ablkcipher_setkey,
1073                         .encrypt = ssi_ablkcipher_encrypt,
1074                         .decrypt = ssi_ablkcipher_decrypt,
1075                         .min_keysize = AES_MIN_KEY_SIZE,
1076                         .max_keysize = AES_MAX_KEY_SIZE,
1077                         .ivsize = 0,
1078                         },
1079                 .cipher_mode = DRV_CIPHER_ECB,
1080                 .flow_mode = S_DIN_to_AES,
1081         },
1082         {
1083                 .name = "cbc(aes)",
1084                 .driver_name = "cbc-aes-dx",
1085                 .blocksize = AES_BLOCK_SIZE,
1086                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1087                 .template_ablkcipher = {
1088                         .setkey = ssi_ablkcipher_setkey,
1089                         .encrypt = ssi_ablkcipher_encrypt,
1090                         .decrypt = ssi_ablkcipher_decrypt,
1091                         .min_keysize = AES_MIN_KEY_SIZE,
1092                         .max_keysize = AES_MAX_KEY_SIZE,
1093                         .ivsize = AES_BLOCK_SIZE,
1094                 },
1095                 .cipher_mode = DRV_CIPHER_CBC,
1096                 .flow_mode = S_DIN_to_AES,
1097         },
1098         {
1099                 .name = "ofb(aes)",
1100                 .driver_name = "ofb-aes-dx",
1101                 .blocksize = AES_BLOCK_SIZE,
1102                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1103                 .template_ablkcipher = {
1104                         .setkey = ssi_ablkcipher_setkey,
1105                         .encrypt = ssi_ablkcipher_encrypt,
1106                         .decrypt = ssi_ablkcipher_decrypt,
1107                         .min_keysize = AES_MIN_KEY_SIZE,
1108                         .max_keysize = AES_MAX_KEY_SIZE,
1109                         .ivsize = AES_BLOCK_SIZE,
1110                         },
1111                 .cipher_mode = DRV_CIPHER_OFB,
1112                 .flow_mode = S_DIN_to_AES,
1113         },
1114 #if SSI_CC_HAS_AES_CTS
1115         {
1116                 .name = "cts1(cbc(aes))",
1117                 .driver_name = "cts1-cbc-aes-dx",
1118                 .blocksize = AES_BLOCK_SIZE,
1119                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1120                 .template_ablkcipher = {
1121                         .setkey = ssi_ablkcipher_setkey,
1122                         .encrypt = ssi_ablkcipher_encrypt,
1123                         .decrypt = ssi_ablkcipher_decrypt,
1124                         .min_keysize = AES_MIN_KEY_SIZE,
1125                         .max_keysize = AES_MAX_KEY_SIZE,
1126                         .ivsize = AES_BLOCK_SIZE,
1127                         },
1128                 .cipher_mode = DRV_CIPHER_CBC_CTS,
1129                 .flow_mode = S_DIN_to_AES,
1130         },
1131 #endif
1132         {
1133                 .name = "ctr(aes)",
1134                 .driver_name = "ctr-aes-dx",
1135                 .blocksize = 1,
1136                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1137                 .template_ablkcipher = {
1138                         .setkey = ssi_ablkcipher_setkey,
1139                         .encrypt = ssi_ablkcipher_encrypt,
1140                         .decrypt = ssi_ablkcipher_decrypt,
1141                         .min_keysize = AES_MIN_KEY_SIZE,
1142                         .max_keysize = AES_MAX_KEY_SIZE,
1143                         .ivsize = AES_BLOCK_SIZE,
1144                         },
1145                 .cipher_mode = DRV_CIPHER_CTR,
1146                 .flow_mode = S_DIN_to_AES,
1147         },
1148         {
1149                 .name = "cbc(des3_ede)",
1150                 .driver_name = "cbc-3des-dx",
1151                 .blocksize = DES3_EDE_BLOCK_SIZE,
1152                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1153                 .template_ablkcipher = {
1154                         .setkey = ssi_ablkcipher_setkey,
1155                         .encrypt = ssi_ablkcipher_encrypt,
1156                         .decrypt = ssi_ablkcipher_decrypt,
1157                         .min_keysize = DES3_EDE_KEY_SIZE,
1158                         .max_keysize = DES3_EDE_KEY_SIZE,
1159                         .ivsize = DES3_EDE_BLOCK_SIZE,
1160                         },
1161                 .cipher_mode = DRV_CIPHER_CBC,
1162                 .flow_mode = S_DIN_to_DES,
1163         },
1164         {
1165                 .name = "ecb(des3_ede)",
1166                 .driver_name = "ecb-3des-dx",
1167                 .blocksize = DES3_EDE_BLOCK_SIZE,
1168                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1169                 .template_ablkcipher = {
1170                         .setkey = ssi_ablkcipher_setkey,
1171                         .encrypt = ssi_ablkcipher_encrypt,
1172                         .decrypt = ssi_ablkcipher_decrypt,
1173                         .min_keysize = DES3_EDE_KEY_SIZE,
1174                         .max_keysize = DES3_EDE_KEY_SIZE,
1175                         .ivsize = 0,
1176                         },
1177                 .cipher_mode = DRV_CIPHER_ECB,
1178                 .flow_mode = S_DIN_to_DES,
1179         },
1180         {
1181                 .name = "cbc(des)",
1182                 .driver_name = "cbc-des-dx",
1183                 .blocksize = DES_BLOCK_SIZE,
1184                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1185                 .template_ablkcipher = {
1186                         .setkey = ssi_ablkcipher_setkey,
1187                         .encrypt = ssi_ablkcipher_encrypt,
1188                         .decrypt = ssi_ablkcipher_decrypt,
1189                         .min_keysize = DES_KEY_SIZE,
1190                         .max_keysize = DES_KEY_SIZE,
1191                         .ivsize = DES_BLOCK_SIZE,
1192                         },
1193                 .cipher_mode = DRV_CIPHER_CBC,
1194                 .flow_mode = S_DIN_to_DES,
1195         },
1196         {
1197                 .name = "ecb(des)",
1198                 .driver_name = "ecb-des-dx",
1199                 .blocksize = DES_BLOCK_SIZE,
1200                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1201                 .template_ablkcipher = {
1202                         .setkey = ssi_ablkcipher_setkey,
1203                         .encrypt = ssi_ablkcipher_encrypt,
1204                         .decrypt = ssi_ablkcipher_decrypt,
1205                         .min_keysize = DES_KEY_SIZE,
1206                         .max_keysize = DES_KEY_SIZE,
1207                         .ivsize = 0,
1208                         },
1209                 .cipher_mode = DRV_CIPHER_ECB,
1210                 .flow_mode = S_DIN_to_DES,
1211         },
1212 #if SSI_CC_HAS_MULTI2
1213         {
1214                 .name = "cbc(multi2)",
1215                 .driver_name = "cbc-multi2-dx",
1216                 .blocksize = CC_MULTI2_BLOCK_SIZE,
1217                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1218                 .template_ablkcipher = {
1219                         .setkey = ssi_ablkcipher_setkey,
1220                         .encrypt = ssi_ablkcipher_encrypt,
1221                         .decrypt = ssi_ablkcipher_decrypt,
1222                         .min_keysize = CC_MULTI2_SYSTEM_N_DATA_KEY_SIZE + 1,
1223                         .max_keysize = CC_MULTI2_SYSTEM_N_DATA_KEY_SIZE + 1,
1224                         .ivsize = CC_MULTI2_IV_SIZE,
1225                         },
1226                 .cipher_mode = DRV_MULTI2_CBC,
1227                 .flow_mode = S_DIN_to_MULTI2,
1228         },
1229         {
1230                 .name = "ofb(multi2)",
1231                 .driver_name = "ofb-multi2-dx",
1232                 .blocksize = 1,
1233                 .type = CRYPTO_ALG_TYPE_ABLKCIPHER,
1234                 .template_ablkcipher = {
1235                         .setkey = ssi_ablkcipher_setkey,
1236                         .encrypt = ssi_ablkcipher_encrypt,
1237                         .decrypt = ssi_ablkcipher_encrypt,
1238                         .min_keysize = CC_MULTI2_SYSTEM_N_DATA_KEY_SIZE + 1,
1239                         .max_keysize = CC_MULTI2_SYSTEM_N_DATA_KEY_SIZE + 1,
1240                         .ivsize = CC_MULTI2_IV_SIZE,
1241                         },
1242                 .cipher_mode = DRV_MULTI2_OFB,
1243                 .flow_mode = S_DIN_to_MULTI2,
1244         },
1245 #endif /*SSI_CC_HAS_MULTI2*/
1246 };
1247
1248 static
1249 struct ssi_crypto_alg *ssi_ablkcipher_create_alg(struct ssi_alg_template *template)
1250 {
1251         struct ssi_crypto_alg *t_alg;
1252         struct crypto_alg *alg;
1253
1254         t_alg = kzalloc(sizeof(*t_alg), GFP_KERNEL);
1255         if (!t_alg) {
1256                 SSI_LOG_ERR("failed to allocate t_alg\n");
1257                 return ERR_PTR(-ENOMEM);
1258         }
1259
1260         alg = &t_alg->crypto_alg;
1261
1262         snprintf(alg->cra_name, CRYPTO_MAX_ALG_NAME, "%s", template->name);
1263         snprintf(alg->cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s",
1264                  template->driver_name);
1265         alg->cra_module = THIS_MODULE;
1266         alg->cra_priority = SSI_CRA_PRIO;
1267         alg->cra_blocksize = template->blocksize;
1268         alg->cra_alignmask = 0;
1269         alg->cra_ctxsize = sizeof(struct ssi_ablkcipher_ctx);
1270
1271         alg->cra_init = ssi_ablkcipher_init;
1272         alg->cra_exit = ssi_blkcipher_exit;
1273         alg->cra_type = &crypto_ablkcipher_type;
1274         alg->cra_ablkcipher = template->template_ablkcipher;
1275         alg->cra_flags = CRYPTO_ALG_ASYNC | CRYPTO_ALG_KERN_DRIVER_ONLY |
1276                                 template->type;
1277
1278         t_alg->cipher_mode = template->cipher_mode;
1279         t_alg->flow_mode = template->flow_mode;
1280
1281         return t_alg;
1282 }
1283
1284 int ssi_ablkcipher_free(struct ssi_drvdata *drvdata)
1285 {
1286         struct ssi_crypto_alg *t_alg, *n;
1287         struct ssi_blkcipher_handle *blkcipher_handle =
1288                                                 drvdata->blkcipher_handle;
1289         struct device *dev;
1290
1291         dev = &drvdata->plat_dev->dev;
1292
1293         if (blkcipher_handle) {
1294                 /* Remove registered algs */
1295                 list_for_each_entry_safe(t_alg, n,
1296                                          &blkcipher_handle->blkcipher_alg_list,
1297                                          entry) {
1298                         crypto_unregister_alg(&t_alg->crypto_alg);
1299                         list_del(&t_alg->entry);
1300                         kfree(t_alg);
1301                 }
1302                 kfree(blkcipher_handle);
1303                 drvdata->blkcipher_handle = NULL;
1304         }
1305         return 0;
1306 }
1307
1308 int ssi_ablkcipher_alloc(struct ssi_drvdata *drvdata)
1309 {
1310         struct ssi_blkcipher_handle *ablkcipher_handle;
1311         struct ssi_crypto_alg *t_alg;
1312         int rc = -ENOMEM;
1313         int alg;
1314
1315         ablkcipher_handle = kmalloc(sizeof(*ablkcipher_handle), GFP_KERNEL);
1316         if (!ablkcipher_handle)
1317                 return -ENOMEM;
1318
1319         drvdata->blkcipher_handle = ablkcipher_handle;
1320
1321         INIT_LIST_HEAD(&ablkcipher_handle->blkcipher_alg_list);
1322
1323         /* Linux crypto */
1324         SSI_LOG_DEBUG("Number of algorithms = %zu\n", ARRAY_SIZE(blkcipher_algs));
1325         for (alg = 0; alg < ARRAY_SIZE(blkcipher_algs); alg++) {
1326                 SSI_LOG_DEBUG("creating %s\n", blkcipher_algs[alg].driver_name);
1327                 t_alg = ssi_ablkcipher_create_alg(&blkcipher_algs[alg]);
1328                 if (IS_ERR(t_alg)) {
1329                         rc = PTR_ERR(t_alg);
1330                         SSI_LOG_ERR("%s alg allocation failed\n",
1331                                     blkcipher_algs[alg].driver_name);
1332                         goto fail0;
1333                 }
1334                 t_alg->drvdata = drvdata;
1335
1336                 SSI_LOG_DEBUG("registering %s\n", blkcipher_algs[alg].driver_name);
1337                 rc = crypto_register_alg(&t_alg->crypto_alg);
1338                 SSI_LOG_DEBUG("%s alg registration rc = %x\n",
1339                               t_alg->crypto_alg.cra_driver_name, rc);
1340                 if (unlikely(rc != 0)) {
1341                         SSI_LOG_ERR("%s alg registration failed\n",
1342                                     t_alg->crypto_alg.cra_driver_name);
1343                         kfree(t_alg);
1344                         goto fail0;
1345                 } else {
1346                         list_add_tail(&t_alg->entry,
1347                                       &ablkcipher_handle->blkcipher_alg_list);
1348                         SSI_LOG_DEBUG("Registered %s\n",
1349                                       t_alg->crypto_alg.cra_driver_name);
1350                 }
1351         }
1352         return 0;
1353
1354 fail0:
1355         ssi_ablkcipher_free(drvdata);
1356         return rc;
1357 }