blob: 7c22f8e012b839b0778a619506294974f03baba4 [file] [log] [blame]
Breno Limac89287e2021-03-25 17:30:02 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2014-2016 Freescale Semiconductor, Inc.
4 * Copyright 2017 NXP
5 */
6
Breno Limac89287e2021-03-25 17:30:02 +08007#include <errno.h>
8#include <fsl_sec.h>
9#include <memalign.h>
10#include "desc.h"
11#include "desc_constr.h"
12#include "jobdesc.h"
13#include "jr.h"
14
15/* Size of MFG descriptor */
16#define MFG_PUBK_DSC_WORDS 4
17#define MFG_SIGN_DSC_WORDS 8
18
19static void mfg_build_sign_dsc(u32 *dsc_ptr, const u8 *m, int size,
20 u8 *dgst, u8 *c, u8 *d)
21{
22 u32 *dsc = dsc_ptr;
23 struct pdb_mp_sign *pdb;
24
25 init_job_desc_pdb(dsc, 0, sizeof(struct pdb_mp_sign));
26
27 pdb = (struct pdb_mp_sign *)desc_pdb(dsc);
28
29 /* Curve */
30 pdb->pdb_hdr = (PDB_MP_CSEL_P256);
31
32 /* Message Pointer */
33 pdb_add_ptr(&pdb->dma_addr_msg, virt_to_phys((void *)m));
34
35 /* mes-resp Pointer */
36 pdb_add_ptr(&pdb->dma_addr_hash, virt_to_phys((void *)dgst));
37
38 /* C Pointer */
39 pdb_add_ptr(&pdb->dma_addr_c_sig, virt_to_phys((void *)c));
40
41 /* d Pointer */
42 pdb_add_ptr(&pdb->dma_addr_d_sig, virt_to_phys((void *)d));
43
44 /* Message Size */
45 pdb->img_size = size;
46
47 /* MP PubK generate key command */
48 append_cmd(dsc, (CMD_OPERATION | OP_TYPE_DECAP_PROTOCOL |
49 OP_PCLID_MP_SIGN));
50}
51
52static void mfg_build_pubk_dsc(u32 *dsc_ptr, u8 *dst)
53{
54 u32 *dsc = dsc_ptr;
55 struct pdb_mp_pub_k *pdb;
56
57 init_job_desc_pdb(dsc, 0, sizeof(struct pdb_mp_pub_k));
58
59 pdb = (struct pdb_mp_pub_k *)desc_pdb(dsc);
60
61 /* Curve */
62 pdb->pdb_hdr = (PDB_MP_CSEL_P256);
63
64 /* Message Pointer */
65 pdb_add_ptr(&pdb->dma_pkey, virt_to_phys((void *)dst));
66
67 /* MP Sign key command */
68 append_cmd(dsc, (CMD_OPERATION | OP_TYPE_DECAP_PROTOCOL |
69 OP_PCLID_MP_PUB_KEY));
70}
71
72int gen_mppubk(u8 *dst)
73{
74 int size, ret;
75 u32 *dsc;
76
77 /* Job Descriptor initialization */
78 dsc = memalign(ARCH_DMA_MINALIGN,
79 sizeof(uint32_t) * MFG_PUBK_DSC_WORDS);
80 if (!dsc) {
81 debug("Not enough memory for descriptor allocation\n");
82 return -ENOMEM;
83 }
84
85 mfg_build_pubk_dsc(dsc, dst);
86
87 size = roundup(sizeof(uint32_t) * MFG_PUBK_DSC_WORDS,
88 ARCH_DMA_MINALIGN);
89 flush_dcache_range((unsigned long)dsc, (unsigned long)dsc + size);
90
91 size = roundup(FSL_CAAM_MP_PUBK_BYTES, ARCH_DMA_MINALIGN);
92 flush_dcache_range((unsigned long)dst, (unsigned long)dst + size);
93
94 /* Execute Job Descriptor */
95 puts("\nGenerating Manufacturing Protection Public Key\n");
96
97 ret = run_descriptor_jr(dsc);
98 if (ret) {
99 debug("Error in public key generation %d\n", ret);
100 goto err;
101 }
102
103 size = roundup(FSL_CAAM_MP_PUBK_BYTES, ARCH_DMA_MINALIGN);
104 invalidate_dcache_range((unsigned long)dst, (unsigned long)dst + size);
105err:
106 free(dsc);
107 return ret;
108}
109
110int sign_mppubk(const u8 *m, int data_size, u8 *dgst, u8 *c, u8 *d)
111{
112 int size, ret;
113 u32 *dsc;
114
115 /* Job Descriptor initialization */
116 dsc = memalign(ARCH_DMA_MINALIGN,
117 sizeof(uint32_t) * MFG_SIGN_DSC_WORDS);
118 if (!dsc) {
119 debug("Not enough memory for descriptor allocation\n");
120 return -ENOMEM;
121 }
122
123 mfg_build_sign_dsc(dsc, m, data_size, dgst, c, d);
124
125 size = roundup(sizeof(uint32_t) * MFG_SIGN_DSC_WORDS,
126 ARCH_DMA_MINALIGN);
127 flush_dcache_range((unsigned long)dsc, (unsigned long)dsc + size);
128
129 size = roundup(data_size, ARCH_DMA_MINALIGN);
130 flush_dcache_range((unsigned long)m, (unsigned long)m + size);
131
132 size = roundup(FSL_CAAM_MP_MES_DGST_BYTES, ARCH_DMA_MINALIGN);
133 flush_dcache_range((unsigned long)dgst, (unsigned long)dgst + size);
134
135 size = roundup(FSL_CAAM_MP_PRVK_BYTES, ARCH_DMA_MINALIGN);
136 flush_dcache_range((unsigned long)c, (unsigned long)c + size);
137 flush_dcache_range((unsigned long)d, (unsigned long)d + size);
138
139 /* Execute Job Descriptor */
Breno Lima372956c2021-03-25 17:30:35 +0800140 puts("\nSigning message with Manufacturing Protection Private Key\n");
Breno Limac89287e2021-03-25 17:30:02 +0800141
142 ret = run_descriptor_jr(dsc);
143 if (ret) {
144 debug("Error in public key generation %d\n", ret);
145 goto err;
146 }
147
148 size = roundup(FSL_CAAM_MP_MES_DGST_BYTES, ARCH_DMA_MINALIGN);
149 invalidate_dcache_range((unsigned long)dgst,
150 (unsigned long)dgst + size);
151
152 size = roundup(FSL_CAAM_MP_PRVK_BYTES, ARCH_DMA_MINALIGN);
153 invalidate_dcache_range((unsigned long)c, (unsigned long)c + size);
154 invalidate_dcache_range((unsigned long)d, (unsigned long)d + size);
155
156err:
157 free(dsc);
158 return ret;
159}