xref: /rk3399_ARM-atf/drivers/st/crypto/stm32_pka.c (revision b0fbc02aea76d31e749444da63b084e6b2bd089b)
1*b0fbc02aSNicolas Toromanoff /*
2*b0fbc02aSNicolas Toromanoff  * Copyright (c) 2022, STMicroelectronics - All Rights Reserved
3*b0fbc02aSNicolas Toromanoff  *
4*b0fbc02aSNicolas Toromanoff  * SPDX-License-Identifier: BSD-3-Clause
5*b0fbc02aSNicolas Toromanoff  */
6*b0fbc02aSNicolas Toromanoff 
7*b0fbc02aSNicolas Toromanoff #include <assert.h>
8*b0fbc02aSNicolas Toromanoff #include <errno.h>
9*b0fbc02aSNicolas Toromanoff #include <stdint.h>
10*b0fbc02aSNicolas Toromanoff 
11*b0fbc02aSNicolas Toromanoff #include <drivers/clk.h>
12*b0fbc02aSNicolas Toromanoff #include <drivers/delay_timer.h>
13*b0fbc02aSNicolas Toromanoff #include <drivers/st/stm32_pka.h>
14*b0fbc02aSNicolas Toromanoff #include <drivers/st/stm32mp_reset.h>
15*b0fbc02aSNicolas Toromanoff #include <lib/mmio.h>
16*b0fbc02aSNicolas Toromanoff #include <lib/utils.h>
17*b0fbc02aSNicolas Toromanoff #include <libfdt.h>
18*b0fbc02aSNicolas Toromanoff #include <plat/common/platform.h>
19*b0fbc02aSNicolas Toromanoff 
20*b0fbc02aSNicolas Toromanoff #include <platform_def.h>
21*b0fbc02aSNicolas Toromanoff 
22*b0fbc02aSNicolas Toromanoff /*
23*b0fbc02aSNicolas Toromanoff  * For our comprehension in this file
24*b0fbc02aSNicolas Toromanoff  *  _len are in BITs
25*b0fbc02aSNicolas Toromanoff  *  _size are in BYTEs
26*b0fbc02aSNicolas Toromanoff  *  _nbw are in number of PKA_word (PKA_word = u64)
27*b0fbc02aSNicolas Toromanoff  */
28*b0fbc02aSNicolas Toromanoff 
29*b0fbc02aSNicolas Toromanoff #define UINT8_LEN			8U
30*b0fbc02aSNicolas Toromanoff #define UINT64_LEN			(UINT8_LEN * sizeof(uint64_t))
31*b0fbc02aSNicolas Toromanoff #define WORD_SIZE			(sizeof(uint64_t))
32*b0fbc02aSNicolas Toromanoff #define OP_NBW_FROM_LEN(len)		(DIV_ROUND_UP_2EVAL((len), UINT64_LEN) + 1)
33*b0fbc02aSNicolas Toromanoff #define OP_NBW_FROM_SIZE(s)		OP_NBW_FROM_LEN((s) * UINT8_LEN)
34*b0fbc02aSNicolas Toromanoff #define OP_SIZE_FROM_SIZE(s)		(OP_NBW_FROM_SIZE(s) * WORD_SIZE)
35*b0fbc02aSNicolas Toromanoff 
36*b0fbc02aSNicolas Toromanoff #define DT_PKA_COMPAT			"st,stm32-pka64"
37*b0fbc02aSNicolas Toromanoff 
38*b0fbc02aSNicolas Toromanoff #define MAX_ECC_SIZE_LEN		640U
39*b0fbc02aSNicolas Toromanoff #define MAX_EO_NBW			OP_NBW_FROM_LEN(MAX_ECC_SIZE_LEN)
40*b0fbc02aSNicolas Toromanoff 
41*b0fbc02aSNicolas Toromanoff /* PKA registers */
42*b0fbc02aSNicolas Toromanoff /* PKA control register */
43*b0fbc02aSNicolas Toromanoff #define _PKA_CR				0x0U
44*b0fbc02aSNicolas Toromanoff /* PKA status register */
45*b0fbc02aSNicolas Toromanoff #define _PKA_SR				0x4U
46*b0fbc02aSNicolas Toromanoff /* PKA clear flag register */
47*b0fbc02aSNicolas Toromanoff #define _PKA_CLRFR			0x8U
48*b0fbc02aSNicolas Toromanoff /* PKA version register */
49*b0fbc02aSNicolas Toromanoff #define _PKA_VERR			0x1FF4U
50*b0fbc02aSNicolas Toromanoff /* PKA identification register */
51*b0fbc02aSNicolas Toromanoff #define _PKA_IPIDR			0x1FF8U
52*b0fbc02aSNicolas Toromanoff 
53*b0fbc02aSNicolas Toromanoff /* PKA control register fields */
54*b0fbc02aSNicolas Toromanoff #define _PKA_CR_MODE_MASK		GENMASK(13, 8)
55*b0fbc02aSNicolas Toromanoff #define _PKA_CR_MODE_SHIFT		8U
56*b0fbc02aSNicolas Toromanoff #define _PKA_CR_MODE_ADD		0x9U
57*b0fbc02aSNicolas Toromanoff #define _PKA_CR_MODE_ECDSA_VERIF	0x26U
58*b0fbc02aSNicolas Toromanoff #define _PKA_CR_START			BIT(1)
59*b0fbc02aSNicolas Toromanoff #define _PKA_CR_EN			BIT(0)
60*b0fbc02aSNicolas Toromanoff 
61*b0fbc02aSNicolas Toromanoff /* PKA status register fields */
62*b0fbc02aSNicolas Toromanoff #define _PKA_SR_BUSY			BIT(16)
63*b0fbc02aSNicolas Toromanoff #define _PKA_SR_LMF			BIT(1)
64*b0fbc02aSNicolas Toromanoff #define _PKA_SR_INITOK			BIT(0)
65*b0fbc02aSNicolas Toromanoff 
66*b0fbc02aSNicolas Toromanoff /* PKA it flag fields (used in CR, SR and CLRFR) */
67*b0fbc02aSNicolas Toromanoff #define _PKA_IT_MASK			(GENMASK(21, 19) | BIT(17))
68*b0fbc02aSNicolas Toromanoff #define _PKA_IT_SHIFT			17U
69*b0fbc02aSNicolas Toromanoff #define _PKA_IT_OPERR			BIT(21)
70*b0fbc02aSNicolas Toromanoff #define _PKA_IT_ADDRERR			BIT(20)
71*b0fbc02aSNicolas Toromanoff #define _PKA_IT_RAMERR			BIT(19)
72*b0fbc02aSNicolas Toromanoff #define _PKA_IT_PROCEND			BIT(17)
73*b0fbc02aSNicolas Toromanoff 
74*b0fbc02aSNicolas Toromanoff /* PKA version register fields */
75*b0fbc02aSNicolas Toromanoff #define _PKA_VERR_MAJREV_MASK		GENMASK(7, 4)
76*b0fbc02aSNicolas Toromanoff #define _PKA_VERR_MAJREV_SHIFT		4U
77*b0fbc02aSNicolas Toromanoff #define _PKA_VERR_MINREV_MASK		GENMASK(3, 0)
78*b0fbc02aSNicolas Toromanoff #define _PKA_VERR_MINREV_SHIFT		0U
79*b0fbc02aSNicolas Toromanoff 
80*b0fbc02aSNicolas Toromanoff /* RAM magic offset */
81*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_START			0x400U
82*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_SIZE			5336U
83*b0fbc02aSNicolas Toromanoff 
84*b0fbc02aSNicolas Toromanoff /* ECDSA verification */
85*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_N_LEN			0x408U /* 64 */
86*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_P_LEN			0x4C8U /* 64 */
87*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_A_SIGN			0x468U /* 64 */
88*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_A			0x470U /* EOS */
89*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_P			0x4D0U /* EOS */
90*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_XG			0x678U /* EOS */
91*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_YG			0x6D0U /* EOS */
92*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_XQ			0x12F8U /* EOS */
93*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_YQ			0x1350U /* EOS */
94*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_SIGN_R			0x10E0U /* EOS */
95*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_SIGN_S			0xC68U /* EOS */
96*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_HASH_Z			0x13A8U /* EOS */
97*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_PRIME_N		0x1088U /* EOS */
98*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_ECDSA_VERIFY		0x5D0U /* 64 */
99*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_ECDSA_VERIFY_VALID	0xD60DULL
100*b0fbc02aSNicolas Toromanoff #define _PKA_RAM_ECDSA_VERIFY_INVALID	0xA3B7ULL
101*b0fbc02aSNicolas Toromanoff 
102*b0fbc02aSNicolas Toromanoff #define PKA_TIMEOUT_US			1000000U
103*b0fbc02aSNicolas Toromanoff #define TIMEOUT_US_1MS			1000U
104*b0fbc02aSNicolas Toromanoff #define PKA_RESET_DELAY			20U
105*b0fbc02aSNicolas Toromanoff 
106*b0fbc02aSNicolas Toromanoff struct curve_parameters {
107*b0fbc02aSNicolas Toromanoff 	uint32_t a_sign;  /* 0 positive, 1 negative */
108*b0fbc02aSNicolas Toromanoff 	uint8_t *a;    /* Curve coefficient |a| */
109*b0fbc02aSNicolas Toromanoff 	size_t a_size;
110*b0fbc02aSNicolas Toromanoff 	uint8_t *p;    /* Curve modulus value */
111*b0fbc02aSNicolas Toromanoff 	uint32_t p_len;
112*b0fbc02aSNicolas Toromanoff 	uint8_t *xg;   /* Curve base point G coordinate x */
113*b0fbc02aSNicolas Toromanoff 	size_t xg_size;
114*b0fbc02aSNicolas Toromanoff 	uint8_t *yg;   /* Curve base point G coordinate y */
115*b0fbc02aSNicolas Toromanoff 	size_t yg_size;
116*b0fbc02aSNicolas Toromanoff 	uint8_t *n;    /* Curve prime order n */
117*b0fbc02aSNicolas Toromanoff 	uint32_t n_len;
118*b0fbc02aSNicolas Toromanoff };
119*b0fbc02aSNicolas Toromanoff 
120*b0fbc02aSNicolas Toromanoff static const struct curve_parameters curve_def[] = {
121*b0fbc02aSNicolas Toromanoff #if PKA_USE_NIST_P256
122*b0fbc02aSNicolas Toromanoff 	[PKA_NIST_P256] = {
123*b0fbc02aSNicolas Toromanoff 		.p_len = 256U,
124*b0fbc02aSNicolas Toromanoff 		.n_len = 256U,
125*b0fbc02aSNicolas Toromanoff 		.p  = (uint8_t[]){0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
126*b0fbc02aSNicolas Toromanoff 				  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127*b0fbc02aSNicolas Toromanoff 				  0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
128*b0fbc02aSNicolas Toromanoff 				  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
129*b0fbc02aSNicolas Toromanoff 		.n  = (uint8_t[]){0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
130*b0fbc02aSNicolas Toromanoff 				  0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
131*b0fbc02aSNicolas Toromanoff 				  0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
132*b0fbc02aSNicolas Toromanoff 				  0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51},
133*b0fbc02aSNicolas Toromanoff 		.a_sign = 1U,
134*b0fbc02aSNicolas Toromanoff 		.a = (uint8_t[]){0x03},
135*b0fbc02aSNicolas Toromanoff 		.a_size = 1U,
136*b0fbc02aSNicolas Toromanoff 		.xg = (uint8_t[]){0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
137*b0fbc02aSNicolas Toromanoff 				  0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
138*b0fbc02aSNicolas Toromanoff 				  0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
139*b0fbc02aSNicolas Toromanoff 				  0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96},
140*b0fbc02aSNicolas Toromanoff 		.xg_size = 32U,
141*b0fbc02aSNicolas Toromanoff 		.yg = (uint8_t[]){0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B,
142*b0fbc02aSNicolas Toromanoff 				  0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16,
143*b0fbc02aSNicolas Toromanoff 				  0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE,
144*b0fbc02aSNicolas Toromanoff 				  0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5},
145*b0fbc02aSNicolas Toromanoff 		.yg_size = 32U,
146*b0fbc02aSNicolas Toromanoff 	},
147*b0fbc02aSNicolas Toromanoff #endif
148*b0fbc02aSNicolas Toromanoff #if PKA_USE_BRAINPOOL_P256R1
149*b0fbc02aSNicolas Toromanoff 	[PKA_BRAINPOOL_P256R1] = {
150*b0fbc02aSNicolas Toromanoff 		.p_len = 256,
151*b0fbc02aSNicolas Toromanoff 		.n_len = 256,
152*b0fbc02aSNicolas Toromanoff 		.p  = (uint8_t[]){0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC,
153*b0fbc02aSNicolas Toromanoff 				  0x3E, 0x66, 0x0A, 0x90, 0x9D, 0x83, 0x8D, 0x72,
154*b0fbc02aSNicolas Toromanoff 				  0x6E, 0x3B, 0xF6, 0x23, 0xD5, 0x26, 0x20, 0x28,
155*b0fbc02aSNicolas Toromanoff 				  0x20, 0x13, 0x48, 0x1D, 0x1F, 0x6E, 0x53, 0x77},
156*b0fbc02aSNicolas Toromanoff 		.n  = (uint8_t[]){0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC,
157*b0fbc02aSNicolas Toromanoff 				  0x3E, 0x66, 0x0A, 0x90, 0x9D, 0x83, 0x8D, 0x71,
158*b0fbc02aSNicolas Toromanoff 				  0x8C, 0x39, 0x7A, 0xA3, 0xB5, 0x61, 0xA6, 0xF7,
159*b0fbc02aSNicolas Toromanoff 				  0x90, 0x1E, 0x0E, 0x82, 0x97, 0x48, 0x56, 0xA7},
160*b0fbc02aSNicolas Toromanoff 		.a  = (uint8_t[]){0x7D, 0x5A, 0x09, 0x75, 0xFC, 0x2C, 0x30, 0x57,
161*b0fbc02aSNicolas Toromanoff 				  0xEE, 0xF6, 0x75, 0x30, 0x41, 0x7A, 0xFF, 0xE7,
162*b0fbc02aSNicolas Toromanoff 				  0xFB, 0x80, 0x55, 0xC1, 0x26, 0xDC, 0x5C, 0x6C,
163*b0fbc02aSNicolas Toromanoff 				  0xE9, 0x4A, 0x4B, 0x44, 0xF3, 0x30, 0xB5, 0xD9},
164*b0fbc02aSNicolas Toromanoff 		.a_size = 32U,
165*b0fbc02aSNicolas Toromanoff 		.xg = (uint8_t[]){0x8B, 0xD2, 0xAE, 0xB9, 0xCB, 0x7E, 0x57, 0xCB,
166*b0fbc02aSNicolas Toromanoff 				  0x2C, 0x4B, 0x48, 0x2F, 0xFC, 0x81, 0xB7, 0xAF,
167*b0fbc02aSNicolas Toromanoff 				  0xB9, 0xDE, 0x27, 0xE1, 0xE3, 0xBD, 0x23, 0xC2,
168*b0fbc02aSNicolas Toromanoff 				  0x3A, 0x44, 0x53, 0xBD, 0x9A, 0xCE, 0x32, 0x62},
169*b0fbc02aSNicolas Toromanoff 		.xg_size = 32U,
170*b0fbc02aSNicolas Toromanoff 		.yg = (uint8_t[]){0x54, 0x7E, 0xF8, 0x35, 0xC3, 0xDA, 0xC4, 0xFD,
171*b0fbc02aSNicolas Toromanoff 				  0x97, 0xF8, 0x46, 0x1A, 0x14, 0x61, 0x1D, 0xC9,
172*b0fbc02aSNicolas Toromanoff 				  0xC2, 0x77, 0x45, 0x13, 0x2D, 0xED, 0x8E, 0x54,
173*b0fbc02aSNicolas Toromanoff 				  0x5C, 0x1D, 0x54, 0xC7, 0x2F, 0x04, 0x69, 0x97},
174*b0fbc02aSNicolas Toromanoff 		.yg_size = 32U,
175*b0fbc02aSNicolas Toromanoff 	},
176*b0fbc02aSNicolas Toromanoff #endif
177*b0fbc02aSNicolas Toromanoff #if PKA_USE_BRAINPOOL_P256T1
178*b0fbc02aSNicolas Toromanoff 	[PKA_BRAINPOOL_P256T1] = {
179*b0fbc02aSNicolas Toromanoff 		.p_len = 256,
180*b0fbc02aSNicolas Toromanoff 		.n_len = 256,
181*b0fbc02aSNicolas Toromanoff 		.p  = (uint8_t[]){0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC,
182*b0fbc02aSNicolas Toromanoff 				  0x3E, 0x66, 0x0A, 0x90, 0x9D, 0x83, 0x8D, 0x72,
183*b0fbc02aSNicolas Toromanoff 				  0x6E, 0x3B, 0xF6, 0x23, 0xD5, 0x26, 0x20, 0x28,
184*b0fbc02aSNicolas Toromanoff 				  0x20, 0x13, 0x48, 0x1D, 0x1F, 0x6E, 0x53, 0x77},
185*b0fbc02aSNicolas Toromanoff 		.n  = (uint8_t[]){0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC,
186*b0fbc02aSNicolas Toromanoff 				  0x3E, 0x66, 0x0A, 0x90, 0x9D, 0x83, 0x8D, 0x71,
187*b0fbc02aSNicolas Toromanoff 				  0x8C, 0x39, 0x7A, 0xA3, 0xB5, 0x61, 0xA6, 0xF7,
188*b0fbc02aSNicolas Toromanoff 				  0x90, 0x1E, 0x0E, 0x82, 0x97, 0x48, 0x56, 0xA7},
189*b0fbc02aSNicolas Toromanoff 		.a  = (uint8_t[]){0xA9, 0xFB, 0x57, 0xDB, 0xA1, 0xEE, 0xA9, 0xBC,
190*b0fbc02aSNicolas Toromanoff 				  0x3E, 0x66, 0x0A, 0x90, 0x9D, 0x83, 0x8D, 0x72,
191*b0fbc02aSNicolas Toromanoff 				  0x6E, 0x3B, 0xF6, 0x23, 0xD5, 0x26, 0x20, 0x28,
192*b0fbc02aSNicolas Toromanoff 				  0x20, 0x13, 0x48, 0x1D, 0x1F, 0x6E, 0x53, 0x74},
193*b0fbc02aSNicolas Toromanoff 		.a_size = 32U,
194*b0fbc02aSNicolas Toromanoff 		.xg = (uint8_t[]){0xA3, 0xE8, 0xEB, 0x3C, 0xC1, 0xCF, 0xE7, 0xB7,
195*b0fbc02aSNicolas Toromanoff 				  0x73, 0x22, 0x13, 0xB2, 0x3A, 0x65, 0x61, 0x49,
196*b0fbc02aSNicolas Toromanoff 				  0xAF, 0xA1, 0x42, 0xC4, 0x7A, 0xAF, 0xBC, 0x2B,
197*b0fbc02aSNicolas Toromanoff 				  0x79, 0xA1, 0x91, 0x56, 0x2E, 0x13, 0x05, 0xF4},
198*b0fbc02aSNicolas Toromanoff 		.xg_size = 32U,
199*b0fbc02aSNicolas Toromanoff 		.yg = (uint8_t[]){0x2D, 0x99, 0x6C, 0x82, 0x34, 0x39, 0xC5, 0x6D,
200*b0fbc02aSNicolas Toromanoff 				  0x7F, 0x7B, 0x22, 0xE1, 0x46, 0x44, 0x41, 0x7E,
201*b0fbc02aSNicolas Toromanoff 				  0x69, 0xBC, 0xB6, 0xDE, 0x39, 0xD0, 0x27, 0x00,
202*b0fbc02aSNicolas Toromanoff 				  0x1D, 0xAB, 0xE8, 0xF3, 0x5B, 0x25, 0xC9, 0xBE},
203*b0fbc02aSNicolas Toromanoff 		.yg_size = 32U,
204*b0fbc02aSNicolas Toromanoff 	},
205*b0fbc02aSNicolas Toromanoff #endif
206*b0fbc02aSNicolas Toromanoff #if PKA_USE_NIST_P521
207*b0fbc02aSNicolas Toromanoff 	[PKA_NIST_P521] = {
208*b0fbc02aSNicolas Toromanoff 		.p_len = 521,
209*b0fbc02aSNicolas Toromanoff 		.n_len = 521,
210*b0fbc02aSNicolas Toromanoff 		.p  = (uint8_t[]){                                    0x01, 0xff,
211*b0fbc02aSNicolas Toromanoff 				  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
212*b0fbc02aSNicolas Toromanoff 				  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
213*b0fbc02aSNicolas Toromanoff 				  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
214*b0fbc02aSNicolas Toromanoff 				  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
215*b0fbc02aSNicolas Toromanoff 				  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
216*b0fbc02aSNicolas Toromanoff 				  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
217*b0fbc02aSNicolas Toromanoff 				  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
218*b0fbc02aSNicolas Toromanoff 				  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
219*b0fbc02aSNicolas Toromanoff 		.n  = (uint8_t[]){                                    0x01, 0xff,
220*b0fbc02aSNicolas Toromanoff 				  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
221*b0fbc02aSNicolas Toromanoff 				  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
222*b0fbc02aSNicolas Toromanoff 				  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
223*b0fbc02aSNicolas Toromanoff 				  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,
224*b0fbc02aSNicolas Toromanoff 				  0x51, 0x86, 0x87, 0x83, 0xbf, 0x2f, 0x96, 0x6b,
225*b0fbc02aSNicolas Toromanoff 				  0x7f, 0xcc, 0x01, 0x48, 0xf7, 0x09, 0xa5, 0xd0,
226*b0fbc02aSNicolas Toromanoff 				  0x3b, 0xb5, 0xc9, 0xb8, 0x89, 0x9c, 0x47, 0xae,
227*b0fbc02aSNicolas Toromanoff 				  0xbb, 0x6f, 0xb7, 0x1e, 0x91, 0x38, 0x64, 0x09},
228*b0fbc02aSNicolas Toromanoff 		.a_sign = 1,
229*b0fbc02aSNicolas Toromanoff 		.a  = (uint8_t[]){0x03},
230*b0fbc02aSNicolas Toromanoff 		.a_size = 1U,
231*b0fbc02aSNicolas Toromanoff 		.xg = (uint8_t[]){                                          0xc6,
232*b0fbc02aSNicolas Toromanoff 				  0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04, 0xe9, 0xcd,
233*b0fbc02aSNicolas Toromanoff 				  0x9e, 0x3e, 0xcb, 0x66, 0x23, 0x95, 0xb4, 0x42,
234*b0fbc02aSNicolas Toromanoff 				  0x9c, 0x64, 0x81, 0x39, 0x05, 0x3f, 0xb5, 0x21,
235*b0fbc02aSNicolas Toromanoff 				  0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d, 0x3d, 0xba,
236*b0fbc02aSNicolas Toromanoff 				  0xa1, 0x4b, 0x5e, 0x77, 0xef, 0xe7, 0x59, 0x28,
237*b0fbc02aSNicolas Toromanoff 				  0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff, 0xa8, 0xde,
238*b0fbc02aSNicolas Toromanoff 				  0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a, 0x42, 0x9b,
239*b0fbc02aSNicolas Toromanoff 				  0xf9, 0x7e, 0x7e, 0x31, 0xc2, 0xe5, 0xbd, 0x66},
240*b0fbc02aSNicolas Toromanoff 		.xg_size = 65U,
241*b0fbc02aSNicolas Toromanoff 		.yg = (uint8_t[]){                                    0x01, 0x18,
242*b0fbc02aSNicolas Toromanoff 				  0x39, 0x29, 0x6a, 0x78, 0x9a, 0x3b, 0xc0, 0x04,
243*b0fbc02aSNicolas Toromanoff 				  0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d, 0x1b, 0xd9,
244*b0fbc02aSNicolas Toromanoff 				  0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b, 0x44, 0x68,
245*b0fbc02aSNicolas Toromanoff 				  0x17, 0xaf, 0xbd, 0x17, 0x27, 0x3e, 0x66, 0x2c,
246*b0fbc02aSNicolas Toromanoff 				  0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4, 0x26, 0x40,
247*b0fbc02aSNicolas Toromanoff 				  0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad, 0x07, 0x61,
248*b0fbc02aSNicolas Toromanoff 				  0x35, 0x3c, 0x70, 0x86, 0xa2, 0x72, 0xc2, 0x40,
249*b0fbc02aSNicolas Toromanoff 				  0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1, 0x66, 0x50},
250*b0fbc02aSNicolas Toromanoff 		.yg_size = 66U,
251*b0fbc02aSNicolas Toromanoff 	},
252*b0fbc02aSNicolas Toromanoff #endif
253*b0fbc02aSNicolas Toromanoff };
254*b0fbc02aSNicolas Toromanoff 
255*b0fbc02aSNicolas Toromanoff static struct stm32_pka_platdata pka_pdata;
256*b0fbc02aSNicolas Toromanoff 
257*b0fbc02aSNicolas Toromanoff #pragma weak stm32_pka_get_platdata
258*b0fbc02aSNicolas Toromanoff 
259*b0fbc02aSNicolas Toromanoff int stm32_pka_get_platdata(struct stm32_pka_platdata *pdata)
260*b0fbc02aSNicolas Toromanoff {
261*b0fbc02aSNicolas Toromanoff 	return -ENODEV;
262*b0fbc02aSNicolas Toromanoff }
263*b0fbc02aSNicolas Toromanoff 
264*b0fbc02aSNicolas Toromanoff static int stm32_pka_parse_fdt(void)
265*b0fbc02aSNicolas Toromanoff {
266*b0fbc02aSNicolas Toromanoff 	int node;
267*b0fbc02aSNicolas Toromanoff 	struct dt_node_info info;
268*b0fbc02aSNicolas Toromanoff 	void *fdt;
269*b0fbc02aSNicolas Toromanoff 
270*b0fbc02aSNicolas Toromanoff 	if (fdt_get_address(&fdt) == 0) {
271*b0fbc02aSNicolas Toromanoff 		return -FDT_ERR_NOTFOUND;
272*b0fbc02aSNicolas Toromanoff 	}
273*b0fbc02aSNicolas Toromanoff 
274*b0fbc02aSNicolas Toromanoff 	node = dt_get_node(&info, -1, DT_PKA_COMPAT);
275*b0fbc02aSNicolas Toromanoff 	if (node < 0) {
276*b0fbc02aSNicolas Toromanoff 		ERROR("No PKA entry in DT\n");
277*b0fbc02aSNicolas Toromanoff 		return -FDT_ERR_NOTFOUND;
278*b0fbc02aSNicolas Toromanoff 	}
279*b0fbc02aSNicolas Toromanoff 
280*b0fbc02aSNicolas Toromanoff 	if (info.status == DT_DISABLED) {
281*b0fbc02aSNicolas Toromanoff 		return -FDT_ERR_NOTFOUND;
282*b0fbc02aSNicolas Toromanoff 	}
283*b0fbc02aSNicolas Toromanoff 
284*b0fbc02aSNicolas Toromanoff 	if ((info.base == 0) || (info.clock < 0) || (info.reset < 0)) {
285*b0fbc02aSNicolas Toromanoff 		return -FDT_ERR_BADVALUE;
286*b0fbc02aSNicolas Toromanoff 	}
287*b0fbc02aSNicolas Toromanoff 
288*b0fbc02aSNicolas Toromanoff 	pka_pdata.base = (uintptr_t)info.base;
289*b0fbc02aSNicolas Toromanoff 	pka_pdata.clock_id = (unsigned long)info.clock;
290*b0fbc02aSNicolas Toromanoff 	pka_pdata.reset_id = (unsigned int)info.reset;
291*b0fbc02aSNicolas Toromanoff 
292*b0fbc02aSNicolas Toromanoff 	return 0;
293*b0fbc02aSNicolas Toromanoff }
294*b0fbc02aSNicolas Toromanoff 
295*b0fbc02aSNicolas Toromanoff static int pka_wait_bit(uintptr_t base, uint32_t bit)
296*b0fbc02aSNicolas Toromanoff {
297*b0fbc02aSNicolas Toromanoff 	uint64_t timeout = timeout_init_us(PKA_TIMEOUT_US);
298*b0fbc02aSNicolas Toromanoff 
299*b0fbc02aSNicolas Toromanoff 	while ((mmio_read_32(base + _PKA_SR) & bit) != bit) {
300*b0fbc02aSNicolas Toromanoff 		if (timeout_elapsed(timeout)) {
301*b0fbc02aSNicolas Toromanoff 			WARN("timeout waiting %x\n", bit);
302*b0fbc02aSNicolas Toromanoff 			return -ETIMEDOUT;
303*b0fbc02aSNicolas Toromanoff 		}
304*b0fbc02aSNicolas Toromanoff 	}
305*b0fbc02aSNicolas Toromanoff 
306*b0fbc02aSNicolas Toromanoff 	return 0;
307*b0fbc02aSNicolas Toromanoff 
308*b0fbc02aSNicolas Toromanoff }
309*b0fbc02aSNicolas Toromanoff 
310*b0fbc02aSNicolas Toromanoff static void pka_disable(uintptr_t base)
311*b0fbc02aSNicolas Toromanoff {
312*b0fbc02aSNicolas Toromanoff 	mmio_clrbits_32(base + _PKA_CR, _PKA_CR_EN);
313*b0fbc02aSNicolas Toromanoff }
314*b0fbc02aSNicolas Toromanoff 
315*b0fbc02aSNicolas Toromanoff static int pka_enable(uintptr_t base, uint32_t mode)
316*b0fbc02aSNicolas Toromanoff {
317*b0fbc02aSNicolas Toromanoff 	/* Set mode and disable interrupts */
318*b0fbc02aSNicolas Toromanoff 	mmio_clrsetbits_32(base + _PKA_CR, _PKA_IT_MASK | _PKA_CR_MODE_MASK,
319*b0fbc02aSNicolas Toromanoff 			   _PKA_CR_MODE_MASK & (mode << _PKA_CR_MODE_SHIFT));
320*b0fbc02aSNicolas Toromanoff 
321*b0fbc02aSNicolas Toromanoff 	mmio_setbits_32(base + _PKA_CR, _PKA_CR_EN);
322*b0fbc02aSNicolas Toromanoff 
323*b0fbc02aSNicolas Toromanoff 	return pka_wait_bit(base, _PKA_SR_INITOK);
324*b0fbc02aSNicolas Toromanoff }
325*b0fbc02aSNicolas Toromanoff 
326*b0fbc02aSNicolas Toromanoff /*
327*b0fbc02aSNicolas Toromanoff  * Data are already loaded in PKA internal RAM
328*b0fbc02aSNicolas Toromanoff  * MODE is set
329*b0fbc02aSNicolas Toromanoff  * We start process, and wait for its end.
330*b0fbc02aSNicolas Toromanoff  */
331*b0fbc02aSNicolas Toromanoff static int stm32_pka_process(uintptr_t base)
332*b0fbc02aSNicolas Toromanoff {
333*b0fbc02aSNicolas Toromanoff 	mmio_setbits_32(base + _PKA_CR, _PKA_CR_START);
334*b0fbc02aSNicolas Toromanoff 
335*b0fbc02aSNicolas Toromanoff 	return pka_wait_bit(base, _PKA_IT_PROCEND);
336*b0fbc02aSNicolas Toromanoff }
337*b0fbc02aSNicolas Toromanoff 
338*b0fbc02aSNicolas Toromanoff /**
339*b0fbc02aSNicolas Toromanoff  * @brief Write ECC operand to PKA RAM.
340*b0fbc02aSNicolas Toromanoff  * @note  PKA expect to write u64 word, each u64 are: the least significant bit is
341*b0fbc02aSNicolas Toromanoff  *        bit 0; the most significant bit is bit 63.
342*b0fbc02aSNicolas Toromanoff  *        We write eo_nbw (ECC operand Size) u64, value that depends of the chosen
343*b0fbc02aSNicolas Toromanoff  *        prime modulus length in bits.
344*b0fbc02aSNicolas Toromanoff  *        First less signicant u64 is written to low address
345*b0fbc02aSNicolas Toromanoff  *        Most significant u64 to higher address.
346*b0fbc02aSNicolas Toromanoff  *        And at last address we write a u64(0x0)
347*b0fbc02aSNicolas Toromanoff  * @note  This function doesn't only manage endianness (as bswap64 do), but also
348*b0fbc02aSNicolas Toromanoff  *        complete most significant incomplete u64 with 0 (if data is not a u64
349*b0fbc02aSNicolas Toromanoff  *        multiple), and fill u64 last address with 0.
350*b0fbc02aSNicolas Toromanoff  * @param addr: PKA_RAM address to write the buffer 'data'
351*b0fbc02aSNicolas Toromanoff  * @param data: is a BYTE list with most significant bytes first
352*b0fbc02aSNicolas Toromanoff  * @param data_size: nb of byte in data
353*b0fbc02aSNicolas Toromanoff  * @param eo_nbw: is ECC Operand size in 64bits word (including the extra 0)
354*b0fbc02aSNicolas Toromanoff  *                (note it depends of the prime modulus length, not the data size)
355*b0fbc02aSNicolas Toromanoff  * @retval 0 if OK.
356*b0fbc02aSNicolas Toromanoff  *         -EINVAL if data_size and eo_nbw are inconsistent, ie data doesn't
357*b0fbc02aSNicolas Toromanoff  *         fit in defined eo_nbw, or eo_nbw bigger than hardware limit.
358*b0fbc02aSNicolas Toromanoff  */
359*b0fbc02aSNicolas Toromanoff static int write_eo_data(uintptr_t addr, uint8_t *data, unsigned int data_size,
360*b0fbc02aSNicolas Toromanoff 			 unsigned int eo_nbw)
361*b0fbc02aSNicolas Toromanoff {
362*b0fbc02aSNicolas Toromanoff 	uint32_t word_index;
363*b0fbc02aSNicolas Toromanoff 	int data_index;
364*b0fbc02aSNicolas Toromanoff 
365*b0fbc02aSNicolas Toromanoff 	if ((eo_nbw < OP_NBW_FROM_SIZE(data_size)) || (eo_nbw > MAX_EO_NBW)) {
366*b0fbc02aSNicolas Toromanoff 		return -EINVAL;
367*b0fbc02aSNicolas Toromanoff 	}
368*b0fbc02aSNicolas Toromanoff 
369*b0fbc02aSNicolas Toromanoff 	/* Fill value */
370*b0fbc02aSNicolas Toromanoff 	data_index = (int)data_size - 1;
371*b0fbc02aSNicolas Toromanoff 	for (word_index = 0U; word_index < eo_nbw; word_index++) {
372*b0fbc02aSNicolas Toromanoff 		uint64_t tmp = 0ULL;
373*b0fbc02aSNicolas Toromanoff 		unsigned int i = 0U;  /* index in the tmp U64 word */
374*b0fbc02aSNicolas Toromanoff 
375*b0fbc02aSNicolas Toromanoff 		/* Stop if end of tmp or end of data */
376*b0fbc02aSNicolas Toromanoff 		while ((i < sizeof(tmp)) && (data_index >= 0)) {
377*b0fbc02aSNicolas Toromanoff 			tmp |= (uint64_t)(data[data_index]) << (UINT8_LEN * i);
378*b0fbc02aSNicolas Toromanoff 			i++; /* Move byte index in current (u64)tmp */
379*b0fbc02aSNicolas Toromanoff 			data_index--; /* Move to just next most significat byte */
380*b0fbc02aSNicolas Toromanoff 		}
381*b0fbc02aSNicolas Toromanoff 
382*b0fbc02aSNicolas Toromanoff 		mmio_write_64(addr + word_index * sizeof(tmp), tmp);
383*b0fbc02aSNicolas Toromanoff 	}
384*b0fbc02aSNicolas Toromanoff 
385*b0fbc02aSNicolas Toromanoff 	return 0;
386*b0fbc02aSNicolas Toromanoff }
387*b0fbc02aSNicolas Toromanoff 
388*b0fbc02aSNicolas Toromanoff static unsigned int get_ecc_op_nbword(enum stm32_pka_ecdsa_curve_id cid)
389*b0fbc02aSNicolas Toromanoff {
390*b0fbc02aSNicolas Toromanoff 	if (cid >= ARRAY_SIZE(curve_def)) {
391*b0fbc02aSNicolas Toromanoff 		ERROR("CID %u is out of boundaries\n", cid);
392*b0fbc02aSNicolas Toromanoff 		panic();
393*b0fbc02aSNicolas Toromanoff 	}
394*b0fbc02aSNicolas Toromanoff 
395*b0fbc02aSNicolas Toromanoff 	return OP_NBW_FROM_LEN(curve_def[cid].n_len);
396*b0fbc02aSNicolas Toromanoff }
397*b0fbc02aSNicolas Toromanoff 
398*b0fbc02aSNicolas Toromanoff static int stm32_pka_ecdsa_verif_configure_curve(uintptr_t base, enum stm32_pka_ecdsa_curve_id cid)
399*b0fbc02aSNicolas Toromanoff {
400*b0fbc02aSNicolas Toromanoff 	int ret;
401*b0fbc02aSNicolas Toromanoff 	unsigned int eo_nbw = get_ecc_op_nbword(cid);
402*b0fbc02aSNicolas Toromanoff 
403*b0fbc02aSNicolas Toromanoff 	mmio_write_64(base + _PKA_RAM_N_LEN, curve_def[cid].n_len);
404*b0fbc02aSNicolas Toromanoff 	mmio_write_64(base + _PKA_RAM_P_LEN, curve_def[cid].p_len);
405*b0fbc02aSNicolas Toromanoff 	mmio_write_64(base + _PKA_RAM_A_SIGN, curve_def[cid].a_sign);
406*b0fbc02aSNicolas Toromanoff 
407*b0fbc02aSNicolas Toromanoff 	ret = write_eo_data(base + _PKA_RAM_A, curve_def[cid].a, curve_def[cid].a_size, eo_nbw);
408*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
409*b0fbc02aSNicolas Toromanoff 		return ret;
410*b0fbc02aSNicolas Toromanoff 	}
411*b0fbc02aSNicolas Toromanoff 
412*b0fbc02aSNicolas Toromanoff 	ret = write_eo_data(base + _PKA_RAM_PRIME_N,
413*b0fbc02aSNicolas Toromanoff 			    curve_def[cid].n, div_round_up(curve_def[cid].n_len, UINT8_LEN),
414*b0fbc02aSNicolas Toromanoff 			    eo_nbw);
415*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
416*b0fbc02aSNicolas Toromanoff 		return ret;
417*b0fbc02aSNicolas Toromanoff 	}
418*b0fbc02aSNicolas Toromanoff 
419*b0fbc02aSNicolas Toromanoff 	ret = write_eo_data(base + _PKA_RAM_P, curve_def[cid].p,
420*b0fbc02aSNicolas Toromanoff 			    div_round_up(curve_def[cid].p_len, UINT8_LEN), eo_nbw);
421*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
422*b0fbc02aSNicolas Toromanoff 		return ret;
423*b0fbc02aSNicolas Toromanoff 	}
424*b0fbc02aSNicolas Toromanoff 
425*b0fbc02aSNicolas Toromanoff 	ret = write_eo_data(base + _PKA_RAM_XG, curve_def[cid].xg, curve_def[cid].xg_size, eo_nbw);
426*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
427*b0fbc02aSNicolas Toromanoff 		return ret;
428*b0fbc02aSNicolas Toromanoff 	}
429*b0fbc02aSNicolas Toromanoff 
430*b0fbc02aSNicolas Toromanoff 	ret = write_eo_data(base + _PKA_RAM_YG, curve_def[cid].yg, curve_def[cid].yg_size, eo_nbw);
431*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
432*b0fbc02aSNicolas Toromanoff 		return ret;
433*b0fbc02aSNicolas Toromanoff 	}
434*b0fbc02aSNicolas Toromanoff 
435*b0fbc02aSNicolas Toromanoff 	return 0;
436*b0fbc02aSNicolas Toromanoff }
437*b0fbc02aSNicolas Toromanoff 
438*b0fbc02aSNicolas Toromanoff static int stm32_pka_ecdsa_verif_check_return(uintptr_t base)
439*b0fbc02aSNicolas Toromanoff {
440*b0fbc02aSNicolas Toromanoff 	uint64_t value;
441*b0fbc02aSNicolas Toromanoff 	uint32_t sr;
442*b0fbc02aSNicolas Toromanoff 
443*b0fbc02aSNicolas Toromanoff 	sr = mmio_read_32(base + _PKA_SR);
444*b0fbc02aSNicolas Toromanoff 	if ((sr & (_PKA_IT_OPERR | _PKA_IT_ADDRERR | _PKA_IT_RAMERR)) != 0) {
445*b0fbc02aSNicolas Toromanoff 		WARN("Detected error(s): %s%s%s\n",
446*b0fbc02aSNicolas Toromanoff 		     (sr & _PKA_IT_OPERR) ? "Operation " : "",
447*b0fbc02aSNicolas Toromanoff 		     (sr & _PKA_IT_ADDRERR) ? "Address " : "",
448*b0fbc02aSNicolas Toromanoff 		     (sr & _PKA_IT_RAMERR) ? "RAM" : "");
449*b0fbc02aSNicolas Toromanoff 		return -EINVAL;
450*b0fbc02aSNicolas Toromanoff 	}
451*b0fbc02aSNicolas Toromanoff 
452*b0fbc02aSNicolas Toromanoff 	value = mmio_read_64(base + _PKA_RAM_ECDSA_VERIFY);
453*b0fbc02aSNicolas Toromanoff 	if (value == _PKA_RAM_ECDSA_VERIFY_VALID) {
454*b0fbc02aSNicolas Toromanoff 		return 0;
455*b0fbc02aSNicolas Toromanoff 	}
456*b0fbc02aSNicolas Toromanoff 
457*b0fbc02aSNicolas Toromanoff 	if (value == _PKA_RAM_ECDSA_VERIFY_INVALID) {
458*b0fbc02aSNicolas Toromanoff 		return -EAUTH;
459*b0fbc02aSNicolas Toromanoff 	}
460*b0fbc02aSNicolas Toromanoff 
461*b0fbc02aSNicolas Toromanoff 	return -EINVAL;
462*b0fbc02aSNicolas Toromanoff }
463*b0fbc02aSNicolas Toromanoff 
464*b0fbc02aSNicolas Toromanoff /**
465*b0fbc02aSNicolas Toromanoff  * @brief Check if BigInt stored in data is 0
466*b0fbc02aSNicolas Toromanoff  *
467*b0fbc02aSNicolas Toromanoff  * @param data: a BYTE array with most significant bytes first
468*b0fbc02aSNicolas Toromanoff  * @param size: data size
469*b0fbc02aSNicolas Toromanoff  *
470*b0fbc02aSNicolas Toromanoff  * @retval: true: if data represents a 0 value (ie all bytes == 0)
471*b0fbc02aSNicolas Toromanoff  *          false: if data represents a non-zero value.
472*b0fbc02aSNicolas Toromanoff  */
473*b0fbc02aSNicolas Toromanoff static bool is_zero(uint8_t *data, unsigned int size)
474*b0fbc02aSNicolas Toromanoff {
475*b0fbc02aSNicolas Toromanoff 	unsigned int i;
476*b0fbc02aSNicolas Toromanoff 
477*b0fbc02aSNicolas Toromanoff 	for (i = 0U; i < size; i++) {
478*b0fbc02aSNicolas Toromanoff 		if (data[i] != 0U) {
479*b0fbc02aSNicolas Toromanoff 			return false;
480*b0fbc02aSNicolas Toromanoff 		}
481*b0fbc02aSNicolas Toromanoff 	}
482*b0fbc02aSNicolas Toromanoff 
483*b0fbc02aSNicolas Toromanoff 	return true;
484*b0fbc02aSNicolas Toromanoff }
485*b0fbc02aSNicolas Toromanoff 
486*b0fbc02aSNicolas Toromanoff /**
487*b0fbc02aSNicolas Toromanoff  * @brief Compare two BigInt:
488*b0fbc02aSNicolas Toromanoff  * @param xdata_a: a BYTE array with most significant bytes first
489*b0fbc02aSNicolas Toromanoff  * @param size_a: nb of Byte of 'a'
490*b0fbc02aSNicolas Toromanoff  * @param data_b: a BYTE array with most significant bytes first
491*b0fbc02aSNicolas Toromanoff  * @param size_b: nb of Byte of 'b'
492*b0fbc02aSNicolas Toromanoff  *
493*b0fbc02aSNicolas Toromanoff  * @retval: true if data_a < data_b
494*b0fbc02aSNicolas Toromanoff  *          false if data_a >= data_b
495*b0fbc02aSNicolas Toromanoff  */
496*b0fbc02aSNicolas Toromanoff static bool is_smaller(uint8_t *data_a, unsigned int size_a,
497*b0fbc02aSNicolas Toromanoff 		       uint8_t *data_b, unsigned int size_b)
498*b0fbc02aSNicolas Toromanoff {
499*b0fbc02aSNicolas Toromanoff 	unsigned int i;
500*b0fbc02aSNicolas Toromanoff 
501*b0fbc02aSNicolas Toromanoff 	i = MAX(size_a, size_b) + 1U;
502*b0fbc02aSNicolas Toromanoff 	do {
503*b0fbc02aSNicolas Toromanoff 		uint8_t a, b;
504*b0fbc02aSNicolas Toromanoff 
505*b0fbc02aSNicolas Toromanoff 		i--;
506*b0fbc02aSNicolas Toromanoff 		if (size_a < i) {
507*b0fbc02aSNicolas Toromanoff 			a = 0U;
508*b0fbc02aSNicolas Toromanoff 		} else {
509*b0fbc02aSNicolas Toromanoff 			a = data_a[size_a - i];
510*b0fbc02aSNicolas Toromanoff 		}
511*b0fbc02aSNicolas Toromanoff 
512*b0fbc02aSNicolas Toromanoff 		if (size_b < i) {
513*b0fbc02aSNicolas Toromanoff 			b = 0U;
514*b0fbc02aSNicolas Toromanoff 		} else {
515*b0fbc02aSNicolas Toromanoff 			b = data_b[size_b - i];
516*b0fbc02aSNicolas Toromanoff 		}
517*b0fbc02aSNicolas Toromanoff 
518*b0fbc02aSNicolas Toromanoff 		if (a < b) {
519*b0fbc02aSNicolas Toromanoff 			return true;
520*b0fbc02aSNicolas Toromanoff 		}
521*b0fbc02aSNicolas Toromanoff 
522*b0fbc02aSNicolas Toromanoff 		if (a > b) {
523*b0fbc02aSNicolas Toromanoff 			return false;
524*b0fbc02aSNicolas Toromanoff 		}
525*b0fbc02aSNicolas Toromanoff 	} while (i != 0U);
526*b0fbc02aSNicolas Toromanoff 
527*b0fbc02aSNicolas Toromanoff 	return false;
528*b0fbc02aSNicolas Toromanoff }
529*b0fbc02aSNicolas Toromanoff 
530*b0fbc02aSNicolas Toromanoff static int stm32_pka_ecdsa_check_param(void *sig_r_ptr, unsigned int sig_r_size,
531*b0fbc02aSNicolas Toromanoff 				       void *sig_s_ptr, unsigned int sig_s_size,
532*b0fbc02aSNicolas Toromanoff 				       void *pk_x_ptr, unsigned int pk_x_size,
533*b0fbc02aSNicolas Toromanoff 				       void *pk_y_ptr, unsigned int pk_y_size,
534*b0fbc02aSNicolas Toromanoff 				       enum stm32_pka_ecdsa_curve_id cid)
535*b0fbc02aSNicolas Toromanoff {
536*b0fbc02aSNicolas Toromanoff 	/* Public Key check */
537*b0fbc02aSNicolas Toromanoff 	/* Check Xq < p */
538*b0fbc02aSNicolas Toromanoff 	if (!is_smaller(pk_x_ptr, pk_x_size,
539*b0fbc02aSNicolas Toromanoff 			curve_def[cid].p, div_round_up(curve_def[cid].p_len, UINT8_LEN))) {
540*b0fbc02aSNicolas Toromanoff 		WARN("%s Xq < p inval\n", __func__);
541*b0fbc02aSNicolas Toromanoff 		return -EINVAL;
542*b0fbc02aSNicolas Toromanoff 	}
543*b0fbc02aSNicolas Toromanoff 
544*b0fbc02aSNicolas Toromanoff 	/* Check Yq < p */
545*b0fbc02aSNicolas Toromanoff 	if (!is_smaller(pk_y_ptr, pk_y_size,
546*b0fbc02aSNicolas Toromanoff 			curve_def[cid].p, div_round_up(curve_def[cid].p_len, UINT8_LEN))) {
547*b0fbc02aSNicolas Toromanoff 		WARN("%s Yq < p inval\n", __func__);
548*b0fbc02aSNicolas Toromanoff 		return -EINVAL;
549*b0fbc02aSNicolas Toromanoff 	}
550*b0fbc02aSNicolas Toromanoff 
551*b0fbc02aSNicolas Toromanoff 	/* Signature check */
552*b0fbc02aSNicolas Toromanoff 	/* Check 0 < r < n */
553*b0fbc02aSNicolas Toromanoff 	if (!is_smaller(sig_r_ptr, sig_r_size,
554*b0fbc02aSNicolas Toromanoff 			curve_def[cid].n, div_round_up(curve_def[cid].n_len, UINT8_LEN)) &&
555*b0fbc02aSNicolas Toromanoff 	    !is_zero(sig_r_ptr, sig_r_size)) {
556*b0fbc02aSNicolas Toromanoff 		WARN("%s 0< r < n inval\n", __func__);
557*b0fbc02aSNicolas Toromanoff 		return -EINVAL;
558*b0fbc02aSNicolas Toromanoff 	}
559*b0fbc02aSNicolas Toromanoff 
560*b0fbc02aSNicolas Toromanoff 	/* Check 0 < s < n */
561*b0fbc02aSNicolas Toromanoff 	if (!is_smaller(sig_s_ptr, sig_s_size,
562*b0fbc02aSNicolas Toromanoff 			curve_def[cid].n, div_round_up(curve_def[cid].n_len, UINT8_LEN)) &&
563*b0fbc02aSNicolas Toromanoff 	    !is_zero(sig_s_ptr, sig_s_size)) {
564*b0fbc02aSNicolas Toromanoff 		WARN("%s 0< s < n inval\n", __func__);
565*b0fbc02aSNicolas Toromanoff 		return -EINVAL;
566*b0fbc02aSNicolas Toromanoff 	}
567*b0fbc02aSNicolas Toromanoff 
568*b0fbc02aSNicolas Toromanoff 	return 0;
569*b0fbc02aSNicolas Toromanoff }
570*b0fbc02aSNicolas Toromanoff 
571*b0fbc02aSNicolas Toromanoff /*
572*b0fbc02aSNicolas Toromanoff  * @brief  Initialize the PKA driver.
573*b0fbc02aSNicolas Toromanoff  * @param  None.
574*b0fbc02aSNicolas Toromanoff  * @retval 0 if OK, negative value else.
575*b0fbc02aSNicolas Toromanoff  */
576*b0fbc02aSNicolas Toromanoff int stm32_pka_init(void)
577*b0fbc02aSNicolas Toromanoff {
578*b0fbc02aSNicolas Toromanoff 	int err;
579*b0fbc02aSNicolas Toromanoff #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
580*b0fbc02aSNicolas Toromanoff 	uint32_t ver;
581*b0fbc02aSNicolas Toromanoff 	uint32_t id;
582*b0fbc02aSNicolas Toromanoff #endif
583*b0fbc02aSNicolas Toromanoff 
584*b0fbc02aSNicolas Toromanoff 	err = stm32_pka_parse_fdt();
585*b0fbc02aSNicolas Toromanoff 	if (err != 0) {
586*b0fbc02aSNicolas Toromanoff 		err = stm32_pka_get_platdata(&pka_pdata);
587*b0fbc02aSNicolas Toromanoff 		if (err != 0) {
588*b0fbc02aSNicolas Toromanoff 			return err;
589*b0fbc02aSNicolas Toromanoff 		}
590*b0fbc02aSNicolas Toromanoff 	}
591*b0fbc02aSNicolas Toromanoff 
592*b0fbc02aSNicolas Toromanoff 	clk_enable(pka_pdata.clock_id);
593*b0fbc02aSNicolas Toromanoff 
594*b0fbc02aSNicolas Toromanoff 	if (stm32mp_reset_assert((unsigned long)pka_pdata.reset_id, TIMEOUT_US_1MS) != 0) {
595*b0fbc02aSNicolas Toromanoff 		panic();
596*b0fbc02aSNicolas Toromanoff 	}
597*b0fbc02aSNicolas Toromanoff 
598*b0fbc02aSNicolas Toromanoff 	udelay(PKA_RESET_DELAY);
599*b0fbc02aSNicolas Toromanoff 	if (stm32mp_reset_deassert((unsigned long)pka_pdata.reset_id, TIMEOUT_US_1MS) != 0) {
600*b0fbc02aSNicolas Toromanoff 		panic();
601*b0fbc02aSNicolas Toromanoff 	}
602*b0fbc02aSNicolas Toromanoff 
603*b0fbc02aSNicolas Toromanoff #if LOG_LEVEL >= LOG_LEVEL_VERBOSE
604*b0fbc02aSNicolas Toromanoff 	id = mmio_read_32(pka_pdata.base + _PKA_IPIDR);
605*b0fbc02aSNicolas Toromanoff 	ver = mmio_read_32(pka_pdata.base + _PKA_VERR);
606*b0fbc02aSNicolas Toromanoff 
607*b0fbc02aSNicolas Toromanoff 	VERBOSE("STM32 PKA[%x] V%u.%u\n", id,
608*b0fbc02aSNicolas Toromanoff 		(ver & _PKA_VERR_MAJREV_MASK) >> _PKA_VERR_MAJREV_SHIFT,
609*b0fbc02aSNicolas Toromanoff 		(ver & _PKA_VERR_MINREV_MASK) >> _PKA_VERR_MINREV_SHIFT);
610*b0fbc02aSNicolas Toromanoff #endif
611*b0fbc02aSNicolas Toromanoff 	return 0;
612*b0fbc02aSNicolas Toromanoff }
613*b0fbc02aSNicolas Toromanoff 
614*b0fbc02aSNicolas Toromanoff int stm32_pka_ecdsa_verif(void *hash, unsigned int hash_size,
615*b0fbc02aSNicolas Toromanoff 			  void *sig_r_ptr, unsigned int sig_r_size,
616*b0fbc02aSNicolas Toromanoff 			  void *sig_s_ptr, unsigned int sig_s_size,
617*b0fbc02aSNicolas Toromanoff 			  void *pk_x_ptr, unsigned int pk_x_size,
618*b0fbc02aSNicolas Toromanoff 			  void *pk_y_ptr, unsigned int pk_y_size,
619*b0fbc02aSNicolas Toromanoff 			  enum stm32_pka_ecdsa_curve_id cid)
620*b0fbc02aSNicolas Toromanoff {
621*b0fbc02aSNicolas Toromanoff 	int ret;
622*b0fbc02aSNicolas Toromanoff 	uintptr_t base = pka_pdata.base;
623*b0fbc02aSNicolas Toromanoff 	unsigned int eo_nbw = get_ecc_op_nbword(cid);
624*b0fbc02aSNicolas Toromanoff 
625*b0fbc02aSNicolas Toromanoff 	if ((hash == NULL) || (sig_r_ptr == NULL) || (sig_s_ptr == NULL) ||
626*b0fbc02aSNicolas Toromanoff 	    (pk_x_ptr == NULL) || (pk_y_ptr == NULL)) {
627*b0fbc02aSNicolas Toromanoff 		INFO("%s invalid input param\n", __func__);
628*b0fbc02aSNicolas Toromanoff 		return -EINVAL;
629*b0fbc02aSNicolas Toromanoff 	}
630*b0fbc02aSNicolas Toromanoff 
631*b0fbc02aSNicolas Toromanoff 	ret = stm32_pka_ecdsa_check_param(sig_r_ptr, sig_r_size,
632*b0fbc02aSNicolas Toromanoff 					  sig_s_ptr, sig_s_size,
633*b0fbc02aSNicolas Toromanoff 					  pk_x_ptr, pk_x_size,
634*b0fbc02aSNicolas Toromanoff 					  pk_y_ptr, pk_y_size,
635*b0fbc02aSNicolas Toromanoff 					  cid);
636*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
637*b0fbc02aSNicolas Toromanoff 		INFO("%s check param error %d\n", __func__, ret);
638*b0fbc02aSNicolas Toromanoff 		goto out;
639*b0fbc02aSNicolas Toromanoff 	}
640*b0fbc02aSNicolas Toromanoff 
641*b0fbc02aSNicolas Toromanoff 	if ((mmio_read_32(base + _PKA_SR) & _PKA_SR_BUSY) == _PKA_SR_BUSY) {
642*b0fbc02aSNicolas Toromanoff 		INFO("%s busy\n", __func__);
643*b0fbc02aSNicolas Toromanoff 		ret = -EBUSY;
644*b0fbc02aSNicolas Toromanoff 		goto out;
645*b0fbc02aSNicolas Toromanoff 	}
646*b0fbc02aSNicolas Toromanoff 
647*b0fbc02aSNicolas Toromanoff 	/* Fill PKA RAM */
648*b0fbc02aSNicolas Toromanoff 	/* With curve id values */
649*b0fbc02aSNicolas Toromanoff 	ret = stm32_pka_ecdsa_verif_configure_curve(base, cid);
650*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
651*b0fbc02aSNicolas Toromanoff 		goto out;
652*b0fbc02aSNicolas Toromanoff 	}
653*b0fbc02aSNicolas Toromanoff 
654*b0fbc02aSNicolas Toromanoff 	/* With pubkey */
655*b0fbc02aSNicolas Toromanoff 	ret = write_eo_data(base + _PKA_RAM_XQ, pk_x_ptr, pk_x_size, eo_nbw);
656*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
657*b0fbc02aSNicolas Toromanoff 		goto out;
658*b0fbc02aSNicolas Toromanoff 	}
659*b0fbc02aSNicolas Toromanoff 
660*b0fbc02aSNicolas Toromanoff 	ret = write_eo_data(base + _PKA_RAM_YQ, pk_y_ptr, pk_y_size, eo_nbw);
661*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
662*b0fbc02aSNicolas Toromanoff 		goto out;
663*b0fbc02aSNicolas Toromanoff 	}
664*b0fbc02aSNicolas Toromanoff 
665*b0fbc02aSNicolas Toromanoff 	/* With hash */
666*b0fbc02aSNicolas Toromanoff 	ret = write_eo_data(base + _PKA_RAM_HASH_Z, hash, hash_size, eo_nbw);
667*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
668*b0fbc02aSNicolas Toromanoff 		goto out;
669*b0fbc02aSNicolas Toromanoff 	}
670*b0fbc02aSNicolas Toromanoff 
671*b0fbc02aSNicolas Toromanoff 	/* With signature */
672*b0fbc02aSNicolas Toromanoff 	ret = write_eo_data(base + _PKA_RAM_SIGN_R, sig_r_ptr, sig_r_size, eo_nbw);
673*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
674*b0fbc02aSNicolas Toromanoff 		goto out;
675*b0fbc02aSNicolas Toromanoff 	}
676*b0fbc02aSNicolas Toromanoff 
677*b0fbc02aSNicolas Toromanoff 	ret = write_eo_data(base + _PKA_RAM_SIGN_S, sig_s_ptr, sig_s_size, eo_nbw);
678*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
679*b0fbc02aSNicolas Toromanoff 		goto out;
680*b0fbc02aSNicolas Toromanoff 	}
681*b0fbc02aSNicolas Toromanoff 
682*b0fbc02aSNicolas Toromanoff 	/* Set mode to ecdsa signature verification */
683*b0fbc02aSNicolas Toromanoff 	ret = pka_enable(base, _PKA_CR_MODE_ECDSA_VERIF);
684*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
685*b0fbc02aSNicolas Toromanoff 		WARN("%s set mode pka error %d\n", __func__, ret);
686*b0fbc02aSNicolas Toromanoff 		goto out;
687*b0fbc02aSNicolas Toromanoff 	}
688*b0fbc02aSNicolas Toromanoff 
689*b0fbc02aSNicolas Toromanoff 	/* Start processing and wait end */
690*b0fbc02aSNicolas Toromanoff 	ret = stm32_pka_process(base);
691*b0fbc02aSNicolas Toromanoff 	if (ret < 0) {
692*b0fbc02aSNicolas Toromanoff 		WARN("%s process error %d\n", __func__, ret);
693*b0fbc02aSNicolas Toromanoff 		goto out;
694*b0fbc02aSNicolas Toromanoff 	}
695*b0fbc02aSNicolas Toromanoff 
696*b0fbc02aSNicolas Toromanoff 	/* Check return status */
697*b0fbc02aSNicolas Toromanoff 	ret = stm32_pka_ecdsa_verif_check_return(base);
698*b0fbc02aSNicolas Toromanoff 
699*b0fbc02aSNicolas Toromanoff 	/* Unset end proc */
700*b0fbc02aSNicolas Toromanoff 	mmio_setbits_32(base + _PKA_CLRFR, _PKA_IT_PROCEND);
701*b0fbc02aSNicolas Toromanoff 
702*b0fbc02aSNicolas Toromanoff out:
703*b0fbc02aSNicolas Toromanoff 	/* Disable PKA (will stop all pending proccess and reset RAM) */
704*b0fbc02aSNicolas Toromanoff 	pka_disable(base);
705*b0fbc02aSNicolas Toromanoff 
706*b0fbc02aSNicolas Toromanoff 	return ret;
707*b0fbc02aSNicolas Toromanoff }
708