xref: /utopia/UTPA2-700.0.x/modules/security/hal/k7u/rsa/halRSA.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2006-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 
95 ////////////////////////////////////////////////////////////////////////////////////////////////////
96 // file   halAESDMA.c
97 // @brief  AESDMA HAL
98 // @author MStar Semiconductor,Inc.
99 ////////////////////////////////////////////////////////////////////////////////////////////////////
100 
101 #include "MsCommon.h"
102 #include "regRSA.h"
103 #include "halRSA.h"
104 
105 
106 //--------------------------------------------------------------------------------------------------
107 //  Driver Compiler Option
108 //--------------------------------------------------------------------------------------------------
109 
110 #define RSA_FLOW_DBG(fmt, args...)  //{printf("\033[33m [%s]",__func__); printf(fmt, ## args); printf("\033[m");}
111 
112 
113 //--------------------------------------------------------------------------------------------------
114 //  TSP Hardware Abstraction Layer
115 //--------------------------------------------------------------------------------------------------
116 static MS_U32                _u32keylen = 0;
117 static MS_VIRT                _u32RegBase = 0;
118 static REG_RSACtrl          *_RSACtrl = 0;
119 
120 static MS_U32                _u32CurrentSecureRangeSet = 0;
121 
122 
123 //--------------------------------------------------------------------------------------------------
124 //  Macro of bit operations
125 //--------------------------------------------------------------------------------------------------
126 
_RSA_REG32_W(REG32 * reg,MS_U32 value)127 void _RSA_REG32_W(REG32 *reg, MS_U32 value)
128 {
129      (*((volatile MS_U32*)(reg))) = value;
130 }
131 
132 
133 
_RSA_REG32_R(REG32 * reg)134 MS_U32 _RSA_REG32_R(REG32 *reg)
135 {
136     MS_U32     value;
137     value = (*(volatile MS_U32*)(reg));
138     return value;
139 }
140 
141 //--------------------------------------------------------------------------------------------------
142 //  Inline Function
143 //--------------------------------------------------------------------------------------------------
HAL_RSA_SetBank(MS_VIRT u32NonPmBankAddr)144 void HAL_RSA_SetBank(MS_VIRT u32NonPmBankAddr)  // OK
145 {
146     _u32RegBase = u32NonPmBankAddr;
147     _RSACtrl =(REG_RSACtrl        *) (_u32RegBase +REG_RSACTRL_BASE);
148 }
149 
150 
HAL_RSA_ClearInt(void)151 void HAL_RSA_ClearInt(void)  /// OK
152 {
153     //RSA interrupt clear
154     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start) , _RSA_REG32_R(&_RSACtrl[0].Rsa_Ind32_Start)|(RSA_INT_CLR));
155 }
156 
HAL_RSA_Reset(void)157 void HAL_RSA_Reset(void)  //// OK
158 {
159     //RSA Rst
160     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ctrl) , RSA_CTRL_RSA_RST);
161     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ctrl) , 0);
162 
163 
164     //add polling RSA status before load data to SRAM
165     while ((HAL_RSA_GetStatus()&RSA_STATUS_RSA_BUSY) == RSA_STATUS_RSA_BUSY)
166     {
167         ;
168     }
169 }
170 
HAL_RSA_Ind32Ctrl(MS_U8 u8dirction)171 void HAL_RSA_Ind32Ctrl(MS_U8 u8dirction)  ////OK
172 {
173     //[1] reg_ind32_direction 0: Read. 1: Write
174 
175 
176     if(u8dirction==1)
177     {
178         /// Move it in Loadram
179         ////_RSA_REG32_W((&_RSACtrl[0].Rsa_Ctrl) ,(RSA_RAM_DIR)|(RSA_IND32_CTRL_ADDR_AUTO_INC)|(RSA_IND32_CTRL_ACCESS_AUTO_START)|(RSA_RAM_MSB_FIRST));
180 
181     }
182     else
183     {
184         _RSA_REG32_W((&_RSACtrl[0].Rsa_Ctrl) ,(RSA_IND32_CTRL_ADDR_AUTO_INC)|(RSA_IND32_CTRL_ACCESS_AUTO_START)|(RSA_RAM_MSB_FIRST));
185     }
186 }
187 
HAL_RSA_LoadSram(MS_U32 * u32Buf,RSA_IND32Address eMode)188 void HAL_RSA_LoadSram(MS_U32 *u32Buf, RSA_IND32Address eMode) /// Question in K3 demo code
189 {
190     MS_U32 u32Cmd = 0;
191     MS_U32 i = 0;
192     MS_U8  *pu8Data;
193     MS_U32 u32Data = 0;
194 
195     switch (eMode)
196     {
197     case E_RSA_ADDRESS_E:
198         u32Cmd |= (RSA_E_BASE_ADDR);
199         break;
200 
201     case E_RSA_ADDRESS_N:
202         u32Cmd |= (RSA_N_BASE_ADDR);
203         break;
204 
205     case E_RSA_ADDRESS_A:
206         u32Cmd |= (RSA_A_BASE_ADDR);
207         break;
208 
209     default:
210         return;
211     }
212 
213     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Addr) , u32Cmd);
214     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ctrl) ,(RSA_RAM_DIR)|(RSA_IND32_CTRL_ADDR_AUTO_INC)|(RSA_IND32_CTRL_ACCESS_AUTO_START)|(RSA_RAM_MSB_FIRST));
215     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start) , RSA_INDIRECT_START);
216     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start) , 0);
217 
218     for( i = 0; i < 64; i++ )
219     {
220         if(eMode==E_RSA_ADDRESS_E)
221         {
222             pu8Data = (MS_U8 *)(&u32Buf[(64-1)-i]);
223         }
224         else{
225             pu8Data = (MS_U8 *)(&u32Buf[i]);
226         }
227         u32Data = ((MS_U32) pu8Data[0])<<24 |((MS_U32) pu8Data[1])<<16 | ((MS_U32) pu8Data[2])<<8 |(MS_U32) pu8Data[3];
228         _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Data) , u32Data);
229     }
230 }
231 
HAL_RSA_SetKeyLength(MS_U32 u32keylen)232 void HAL_RSA_SetKeyLength(MS_U32 u32keylen) /// Ken Len  unit:Word
233 {
234     //[13:8] n_len_e: key length
235     _u32keylen = u32keylen;
236 }
237 
HAL_RSA_SetKeyType(MS_U8 u8hwkey,MS_U8 u8pubkey)238 void HAL_RSA_SetKeyType(MS_U8 u8hwkey, MS_U8 u8pubkey)
239 {
240     //[1] hw_key_e : 0 : software key, 1: hardware key
241     //[2] e_pub_e : 0: pvivate key, 1: public key
242     MS_U32 value = (_u32keylen<<8);
243 
244     if(u8hwkey==1)
245     {
246         value |= RSA_CTRL_SEL_HW_KEY;
247 
248     }
249     else
250     {
251         value &= ~RSA_CTRL_SEL_HW_KEY;
252 
253     }
254 
255     if(u8pubkey==1)
256     {
257         value |= (RSA_CTRL_SEL_PUBLIC_KEY);
258 
259     }
260     else
261     {
262         value &= ~RSA_CTRL_SEL_PUBLIC_KEY;
263 
264     }
265 
266     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ctrl) , value);
267 }
268 
HAL_RSA_Hwkey_Switch(void)269 void HAL_RSA_Hwkey_Switch(void)
270 {
271     MS_U32 value = 0;
272 
273     value |= RSA_HWKEY_SWITCH;
274 
275     _RSA_REG32_W((&_RSACtrl[0].Rsa_Hwkey_Switch) , value);
276 }
277 
HAL_RSA_Read_Hwkey_Switch(void)278 MS_U32 HAL_RSA_Read_Hwkey_Switch(void)
279 {
280     return _RSA_REG32_R((&_RSACtrl[0].Rsa_Hwkey_Switch)) & RSA_READ_HWKEY_SWITCH;
281 }
282 
HAL_RSA_Rootkey_Sel(MS_U8 u8KeySrc)283 void HAL_RSA_Rootkey_Sel(MS_U8 u8KeySrc)
284 {
285     // RSA_ROOTKEY_SEL_PUB_NO              0x00000020    /// if public key, 0: select 1st key; 1 select 2nd key
286     // RSA_ROOTKEY_SEL_TYPE                0x00000040    /// 1: OTP RSA key is public; 0: OTP RSA is private
287     MS_U32 value = 0;
288 
289     switch (u8KeySrc)
290     {
291     case E_RSA_HW_PRI_KEY:   // 0*
292         {
293             value &= ~RSA_ROOTKEY_SEL_TYPE;
294             break;
295         }
296     case E_RSA_HW_PUB_KEY1:  // 10
297         {
298             value |= RSA_ROOTKEY_SEL_TYPE;
299             value &= ~RSA_ROOTKEY_SEL_PUB_NO;
300             break;
301         }
302     case E_RSA_HW_PUB_KEY2:  // 11
303         {
304             value |= RSA_ROOTKEY_SEL_TYPE;
305             value |= RSA_ROOTKEY_SEL_PUB_NO;
306             break;
307         }
308     default:
309         {
310             break;
311         }
312     }
313 
314     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start) , value);
315 }
316 
HAL_RSA_ExponetialStart(void)317 void HAL_RSA_ExponetialStart(void)
318 {
319     //RSA exp start
320 
321     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start) , _RSA_REG32_R(&_RSACtrl[0].Rsa_Ind32_Start)|(RSA_EXP_START));
322     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start) , _RSA_REG32_R(&_RSACtrl[0].Rsa_Ind32_Start)&(~RSA_EXP_START) );
323 }
324 
HAL_RSA_GetStatus(void)325 MS_U32 HAL_RSA_GetStatus(void)
326 {
327     return _RSA_REG32_R(&_RSACtrl[0].Rsa_Ind32_Addr) & RSA_STATUS_MASK;
328 }
329 
HAL_RSA_FileOutStart(void)330 void HAL_RSA_FileOutStart(void)
331 {
332     //RSA ind32_start
333 
334     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start) , RSA_INDIRECT_START);
335     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start), 0);
336 }
337 
HAL_RSA_SetFileOutAddr(MS_U32 u32offset)338 void HAL_RSA_SetFileOutAddr(MS_U32 u32offset)
339 {
340     MS_U32 u32Addr = 0;
341     u32Addr = RSA_Z_BASE_ADDR + u32offset;
342 
343     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Addr) ,u32Addr);
344 }
345 
HAL_RSA_FileOut(void)346 MS_U32 HAL_RSA_FileOut(void)
347 {
348     MS_U32 u32out = 0;
349 
350     u32out = _RSA_REG32_R(&_RSACtrl[0].Rsa_Ind32_Data);
351     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Data) , 0);
352 
353     return u32out;
354 }
355 
HAL_RSA_Get_RSA_IsFinished(void)356 MS_U32 HAL_RSA_Get_RSA_IsFinished(void)  ///  done
357 {
358     //// in fact it is used to detect if rsa is busy .... but the function name seems not correct.
359     ////  return value :TRUE  means busy....
360     ////                FALSE means finished
361     ////  But the API is used in utopia, so I did not refine it now.....
362     if((HAL_RSA_GetStatus()&RSA_STATUS_RSA_BUSY) == RSA_STATUS_RSA_BUSY)
363     {
364         return -1;
365     }
366     else{
367         return 0;
368     }
369 }
370 
371 
HAL_RSA_SetSecureRange(MS_U32 u32addr,MS_U32 u32size)372 MS_BOOL HAL_RSA_SetSecureRange(MS_U32 u32addr, MS_U32 u32size)
373 {
374 	RSA_FLOW_DBG("u32addr=%x, u32size=%x\n", u32addr, u32size);
375 	RSA_FLOW_DBG("_RSACtrl = %x\n", _RSACtrl);
376 
377 	MS_U32 start_addr    = u32addr;
378 
379 
380 	MS_U32 start_unit = 0;
381 	MS_U32 end_unit   = 0;
382 
383 
384 	start_unit =  u32addr >> REG_RSA_SEC_RANGE_SHIFT;
385 	end_unit   = (u32addr + u32size) >> REG_RSA_SEC_RANGE_SHIFT;
386 
387 
388 	//alignment to secure range start
389 	start_addr = u32addr & REG_RSA_SEC_RANGE_MASK;
390 
391 	if(start_addr < u32addr)
392 	{
393 		RSA_FLOW_DBG("Invalid Input Address. Need to alignment...\n");
394 		return FALSE;
395 	}
396 
397 
398 	if((end_unit == start_unit)  || (end_unit < start_unit))
399 	{
400 		RSA_FLOW_DBG("Invalid Input Size\n");
401 		return FALSE;
402 	}
403 	else
404 	{
405 		if(_u32CurrentSecureRangeSet < RSA_MAX_SECURE_RANGE_SIZE)
406 		{
407 
408 		#if 1
409 			_RSA_REG32_W(&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_Start, start_unit);
410 			_RSA_REG32_W(&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_End, end_unit);
411 
412 			RSA_FLOW_DBG("Rsa_Sec_Range_Start=%x, Rsa_Sec_Range_End=%x\n",
413 				&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_Start,
414 				&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_End);
415 
416 			RSA_FLOW_DBG("start_unit = %x, end_unit = %x, _u32CurrentSecureRangeSet  = %x",
417 				         start_unit, end_unit, _u32CurrentSecureRangeSet);
418 
419 			_u32CurrentSecureRangeSet ++;
420 
421 		#endif
422 			return TRUE;
423 		}
424 		else
425 		{
426 			RSA_FLOW_DBG("Secure Range Set is not enough\n");
427 			return FALSE;
428 		}
429 	}
430 }
431 
432 #if 0
433 MS_BOOL HAL_RSA_GetSecureRange(MS_U32 u32addr, MS_U32 u32size, MS_U32* u32getaddr, MS_U32* u32getsize)
434 {
435 	MS_U32 start_addr    = u32addr;
436 	MS_U32 avalible_size = u32size;
437 
438 	MS_U32 start_unit = 0;
439 	MS_U32 end_unit   = 0;
440 
441 	*u32getaddr = NULL;
442 	*u32getsize = 0;
443 
444 	start_unit =  u32addr >> 16;
445 	end_unit   = (u32addr + u32size) >> 16;
446 
447 
448 	//alignment to secure range start
449 	start_addr = start_addr >> 16;
450 	start_addr = start_addr << 16;
451 
452 	if(start_addr < u32addr)
453 	{
454 		start_unit += 1;
455 
456 		avalible_size = u32size - ((start_unit << 16) - u32addr);
457 		end_unit = start_unit +  (avalible_size >> 16);
458 	}
459 
460 
461 	if((end_unit == start_unit)  || (end_unit < start_unit))
462 	{
463 		RSA_FLOW_DBG("Invalid Input Size\n");
464 		return FALSE;
465 	}
466 	else
467 	{
468 		if(_u32CurrentSecureRangeSet < RSA_MAX_SECURE_RANGE_SIZE)
469 		{
470 			_RSA_REG32_W(&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_Start, start_unit);
471 			_RSA_REG32_W(&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_End, end_unit);
472 
473 			_u32CurrentSecureRangeSet ++;
474 
475 			*u32getaddr = start_unit << 16;
476 			*u32getsize = (end_unit - start_unit) << 16;
477 			RSA_FLOW_DBG("Rsa_Sec_Range_Start=%x, Rsa_Sec_Range_End=%x\n",
478 				&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_Start,
479 				&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_End);
480 
481 			RSA_FLOW_DBG("start_unit = %x, end_unit = %x, _u32CurrentSecureRangeSet  = %x",
482 				         start_unit, end_unit, _u32CurrentSecureRangeSet);
483 			return TRUE;
484 		}
485 		else
486 		{
487 			RSA_FLOW_DBG("Secure Range Set is not enough\n");
488 			return FALSE;
489 		}
490 	}
491 }
492 #endif
493 
494