xref: /openbmc/u-boot/drivers/crypto/fsl/fsl_blob.c (revision 9ee16897)
1 /*
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>
10 #include <fsl_sec.h>
11 #include <asm-generic/errno.h>
12 #include "jobdesc.h"
13 #include "desc.h"
14 #include "jr.h"
15 
16 int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
17 {
18 	int ret, i = 0;
19 	u32 *desc;
20 
21 	printf("\nDecapsulating data to form blob\n");
22 	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 
30 	for (i = 0; i < 14; i++)
31 		printf("%x\n", *(desc + i));
32 	ret = run_descriptor_jr(desc);
33 
34 	if (ret)
35 		printf("Error in Decapsulation %d\n", ret);
36 
37 	free(desc);
38 	return ret;
39 }
40 
41 int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
42 {
43 	int ret, i = 0;
44 	u32 *desc;
45 
46 	printf("\nEncapsulating data to form blob\n");
47 	desc = malloc(sizeof(int) * MAX_CAAM_DESCSIZE);
48 	if (!desc) {
49 		debug("Not enough memory for descriptor allocation\n");
50 		return -1;
51 	}
52 
53 	inline_cnstr_jobdesc_blob_encap(desc, key_mod, src, dst, len);
54 	for (i = 0; i < 14; i++)
55 		printf("%x\n", *(desc + i));
56 	ret = run_descriptor_jr(desc);
57 
58 	if (ret)
59 		printf("Error in Encapsulation %d\n", ret);
60 
61 	free(desc);
62 	return ret;
63 }
64 
65 #ifdef CONFIG_CMD_DEKBLOB
66 int blob_dek(const u8 *src, u8 *dst, u8 len)
67 {
68 	int ret, size, i = 0;
69 	u32 *desc;
70 
71 	int out_sz =  WRP_HDR_SIZE + len + KEY_BLOB_SIZE + MAC_SIZE;
72 
73 	puts("\nEncapsulating provided DEK to form blob\n");
74 	desc = memalign(ARCH_DMA_MINALIGN,
75 			sizeof(uint32_t) * DEK_BLOB_DESCSIZE);
76 	if (!desc) {
77 		debug("Not enough memory for descriptor allocation\n");
78 		return -ENOMEM;
79 	}
80 
81 	ret = inline_cnstr_jobdesc_blob_dek(desc, src, dst, len);
82 	if (ret) {
83 		debug("Error in Job Descriptor Construction:  %d\n", ret);
84 	} else {
85 		size = roundup(sizeof(uint32_t) * DEK_BLOB_DESCSIZE,
86 			      ARCH_DMA_MINALIGN);
87 		flush_dcache_range((unsigned long)desc,
88 				   (unsigned long)desc + size);
89 		size = roundup(sizeof(uint8_t) * out_sz, ARCH_DMA_MINALIGN);
90 		flush_dcache_range((unsigned long)dst,
91 				   (unsigned long)dst + size);
92 
93 		ret = run_descriptor_jr(desc);
94 	}
95 
96 	if (ret) {
97 		debug("Error in Encapsulation %d\n", ret);
98 	   goto err;
99 	}
100 
101 	size = roundup(out_sz, ARCH_DMA_MINALIGN);
102 	invalidate_dcache_range((unsigned long)dst, (unsigned long)dst+size);
103 
104 	puts("DEK Blob\n");
105 	for (i = 0; i < out_sz; i++)
106 		printf("%02X", ((uint8_t *)dst)[i]);
107 	printf("\n");
108 
109 err:
110 	free(desc);
111 	return ret;
112 }
113 #endif
114