blob: 38c6f9486bf1ca365b4abe69049aea7b420664db [file] [log] [blame]
Ruchika Gupta4345a572014-10-07 15:46:20 +05301/*
2 * Copyright 2014 Freescale Semiconductor, Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 *
6 */
7
8#include <common.h>
9#include <malloc.h>
Raul Cardenasb5a36d82015-02-27 11:22:06 -060010#include <fsl_sec.h>
Masahiro Yamada64e4f7f2016-09-21 11:28:57 +090011#include <linux/errno.h>
Ruchika Gupta4345a572014-10-07 15:46:20 +053012#include "jobdesc.h"
13#include "desc.h"
14#include "jr.h"
15
gaurav ranab64308e2015-02-25 09:37:09 +053016int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
Ruchika Gupta4345a572014-10-07 15:46:20 +053017{
18 int ret, i = 0;
19 u32 *desc;
20
Sumit Gargff9eb052016-07-14 13:27:50 -040021 printf("\nDecapsulating blob to get data\n");
Ruchika Gupta4345a572014-10-07 15:46:20 +053022 desc = malloc(sizeof(int) * MAX_CAAM_DESCSIZE);
23 if (!desc) {
24 debug("Not enough memory for descriptor allocation\n");
25 return -1;
26 }
27
28 inline_cnstr_jobdesc_blob_decap(desc, key_mod, src, dst, len);
29
Sumit Gargff9eb052016-07-14 13:27:50 -040030 debug("Descriptor dump:\n");
Ruchika Gupta4345a572014-10-07 15:46:20 +053031 for (i = 0; i < 14; i++)
Sumit Gargff9eb052016-07-14 13:27:50 -040032 debug("Word[%d]: %08x\n", i, *(desc + i));
Ruchika Gupta4345a572014-10-07 15:46:20 +053033 ret = run_descriptor_jr(desc);
34
35 if (ret)
36 printf("Error in Decapsulation %d\n", ret);
Sumit Gargff9eb052016-07-14 13:27:50 -040037 else
38 printf("Decapsulation Success\n");
Ruchika Gupta4345a572014-10-07 15:46:20 +053039
40 free(desc);
41 return ret;
42}
43
gaurav ranab64308e2015-02-25 09:37:09 +053044int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
Ruchika Gupta4345a572014-10-07 15:46:20 +053045{
46 int ret, i = 0;
47 u32 *desc;
48
49 printf("\nEncapsulating data to form blob\n");
50 desc = malloc(sizeof(int) * MAX_CAAM_DESCSIZE);
51 if (!desc) {
52 debug("Not enough memory for descriptor allocation\n");
53 return -1;
54 }
55
56 inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len);
Sumit Gargff9eb052016-07-14 13:27:50 -040057
58 debug("Descriptor dump:\n");
Ruchika Gupta4345a572014-10-07 15:46:20 +053059 for (i = 0; i < 14; i++)
Sumit Gargff9eb052016-07-14 13:27:50 -040060 debug("Word[%d]: %08x\n", i, *(desc + i));
Ruchika Gupta4345a572014-10-07 15:46:20 +053061 ret = run_descriptor_jr(desc);
62
63 if (ret)
64 printf("Error in Encapsulation %d\n", ret);
Sumit Gargff9eb052016-07-14 13:27:50 -040065 else
66 printf("Encapsulation Success\n");
Ruchika Gupta4345a572014-10-07 15:46:20 +053067
68 free(desc);
69 return ret;
70}
Raul Cardenasb5a36d82015-02-27 11:22:06 -060071
72#ifdef CONFIG_CMD_DEKBLOB
73int blob_dek(const u8 *src, u8 *dst, u8 len)
74{
75 int ret, size, i = 0;
76 u32 *desc;
77
78 int out_sz = WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE;
79
80 puts("\nEncapsulating provided DEK to form blob\n");
81 desc = memalign(ARCH_DMA_MINALIGN,
82 sizeof(uint32_t) * DEK_BLOB_DESCSIZE);
83 if (!desc) {
84 debug("Not enough memory for descriptor allocation\n");
85 return -ENOMEM;
86 }
87
88 ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len);
89 if (ret) {
90 debug("Error in Job Descriptor Construction: %d\n", ret);
91 } else {
92 size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE,
93 ARCH_DMA_MINALIGN);
94 flush_dcache_range((unsigned long)desc,
95 (unsigned long)desc + size);
96 size = roundup(sizeof(uint8_t) * out_sz, ARCH_DMA_MINALIGN);
97 flush_dcache_range((unsigned long)dst,
98 (unsigned long)dst + size);
99
100 ret = run_descriptor_jr(desc);
101 }
102
103 if (ret) {
104 debug("Error in Encapsulation %d\n", ret);
105 goto err;
106 }
107
108 size = roundup(out_sz, ARCH_DMA_MINALIGN);
109 invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size);
110
111 puts("DEK Blob\n");
112 for (i = 0; i < out_sz; i++)
113 printf("%02X", ((uint8_t *)dst)[i]);
114 printf("\n");
115
116err:
117 free(desc);
118 return ret;
119}
120#endif