xref: /utopia/UTPA2-700.0.x/modules/security/hal/k6lite/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     //Not Implemented!
272 }
273 
HAL_RSA_Read_Hwkey_Switch(void)274 MS_U32 HAL_RSA_Read_Hwkey_Switch(void)
275 {
276     //Not Implemented!
277     return 0;
278 }
279 
HAL_RSA_Rootkey_Sel(MS_U8 u8KeySrc)280 void HAL_RSA_Rootkey_Sel(MS_U8 u8KeySrc)
281 {
282     // RSA_ROOTKEY_SEL_PUB_NO              0x00000020    /// if public key, 0: select 1st key; 1 select 2nd key
283     // RSA_ROOTKEY_SEL_TYPE                0x00000040    /// 1: OTP RSA key is public; 0: OTP RSA is private
284     MS_U32 value = 0;
285 
286     switch (u8KeySrc)
287     {
288     case E_RSA_HW_PRI_KEY:   // 0*
289         {
290             value &= ~RSA_ROOTKEY_SEL_TYPE;
291             break;
292         }
293     case E_RSA_HW_PUB_KEY1:  // 10
294         {
295             value |= RSA_ROOTKEY_SEL_TYPE;
296             value &= ~RSA_ROOTKEY_SEL_PUB_NO;
297             break;
298         }
299     case E_RSA_HW_PUB_KEY2:  // 11
300         {
301             value |= RSA_ROOTKEY_SEL_TYPE;
302             value |= RSA_ROOTKEY_SEL_PUB_NO;
303             break;
304         }
305     default:
306         {
307             break;
308         }
309     }
310 
311     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start) , value);
312 }
313 
HAL_RSA_ExponetialStart(void)314 void HAL_RSA_ExponetialStart(void)
315 {
316     //RSA exp start
317 
318     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start) , _RSA_REG32_R(&_RSACtrl[0].Rsa_Ind32_Start)|(RSA_EXP_START));
319     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start) , _RSA_REG32_R(&_RSACtrl[0].Rsa_Ind32_Start)&(~RSA_EXP_START) );
320 }
321 
HAL_RSA_GetStatus(void)322 MS_U32 HAL_RSA_GetStatus(void)
323 {
324     return _RSA_REG32_R(&_RSACtrl[0].Rsa_Ind32_Addr) & RSA_STATUS_MASK;
325 }
326 
HAL_RSA_FileOutStart(void)327 void HAL_RSA_FileOutStart(void)
328 {
329     //RSA ind32_start
330 
331     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start) , RSA_INDIRECT_START);
332     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Start), 0);
333 }
334 
HAL_RSA_SetFileOutAddr(MS_U32 u32offset)335 void HAL_RSA_SetFileOutAddr(MS_U32 u32offset)
336 {
337     MS_U32 u32Addr = 0;
338     u32Addr = RSA_Z_BASE_ADDR + u32offset;
339 
340     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Addr) ,u32Addr);
341 }
342 
HAL_RSA_FileOut(void)343 MS_U32 HAL_RSA_FileOut(void)
344 {
345     MS_U32 u32out = 0;
346 
347     u32out = _RSA_REG32_R(&_RSACtrl[0].Rsa_Ind32_Data);
348     _RSA_REG32_W((&_RSACtrl[0].Rsa_Ind32_Data) , 0);
349 
350     return u32out;
351 }
352 
HAL_RSA_Get_RSA_IsFinished(void)353 MS_U32 HAL_RSA_Get_RSA_IsFinished(void)  ///  done
354 {
355     //// in fact it is used to detect if rsa is busy .... but the function name seems not correct.
356     ////  return value :TRUE  means busy....
357     ////                FALSE means finished
358     ////  But the API is used in utopia, so I did not refine it now.....
359     if((HAL_RSA_GetStatus()&RSA_STATUS_RSA_BUSY) == RSA_STATUS_RSA_BUSY)
360     {
361         return -1;
362     }
363     else{
364         return 0;
365     }
366 }
367 
368 
HAL_RSA_SetSecureRange(MS_U32 u32addr,MS_U32 u32size)369 MS_BOOL HAL_RSA_SetSecureRange(MS_U32 u32addr, MS_U32 u32size)
370 {
371 	RSA_FLOW_DBG("u32addr=%x, u32size=%x\n", u32addr, u32size);
372 	RSA_FLOW_DBG("_RSACtrl = %x\n", _RSACtrl);
373 
374 	MS_U32 start_addr    = u32addr;
375 
376 
377 	MS_U32 start_unit = 0;
378 	MS_U32 end_unit   = 0;
379 
380 
381 	start_unit =  u32addr >> REG_RSA_SEC_RANGE_SHIFT;
382 	end_unit   = (u32addr + u32size) >> REG_RSA_SEC_RANGE_SHIFT;
383 
384 
385 	//alignment to secure range start
386 	start_addr = u32addr & REG_RSA_SEC_RANGE_MASK;
387 
388 	if(start_addr < u32addr)
389 	{
390 		RSA_FLOW_DBG("Invalid Input Address. Need to alignment...\n");
391 		return FALSE;
392 	}
393 
394 
395 	if((end_unit == start_unit)  || (end_unit < start_unit))
396 	{
397 		RSA_FLOW_DBG("Invalid Input Size\n");
398 		return FALSE;
399 	}
400 	else
401 	{
402 		if(_u32CurrentSecureRangeSet < RSA_MAX_SECURE_RANGE_SIZE)
403 		{
404 
405 		#if 1
406 			_RSA_REG32_W(&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_Start, start_unit);
407 			_RSA_REG32_W(&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_End, end_unit);
408 
409 			RSA_FLOW_DBG("Rsa_Sec_Range_Start=%x, Rsa_Sec_Range_End=%x\n",
410 				&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_Start,
411 				&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_End);
412 
413 			RSA_FLOW_DBG("start_unit = %x, end_unit = %x, _u32CurrentSecureRangeSet  = %x",
414 				         start_unit, end_unit, _u32CurrentSecureRangeSet);
415 
416 			_u32CurrentSecureRangeSet ++;
417 
418 		#endif
419 			return TRUE;
420 		}
421 		else
422 		{
423 			RSA_FLOW_DBG("Secure Range Set is not enough\n");
424 			return FALSE;
425 		}
426 	}
427 }
428 
429 #if 0
430 MS_BOOL HAL_RSA_GetSecureRange(MS_U32 u32addr, MS_U32 u32size, MS_U32* u32getaddr, MS_U32* u32getsize)
431 {
432 	MS_U32 start_addr    = u32addr;
433 	MS_U32 avalible_size = u32size;
434 
435 	MS_U32 start_unit = 0;
436 	MS_U32 end_unit   = 0;
437 
438 	*u32getaddr = NULL;
439 	*u32getsize = 0;
440 
441 	start_unit =  u32addr >> 16;
442 	end_unit   = (u32addr + u32size) >> 16;
443 
444 
445 	//alignment to secure range start
446 	start_addr = start_addr >> 16;
447 	start_addr = start_addr << 16;
448 
449 	if(start_addr < u32addr)
450 	{
451 		start_unit += 1;
452 
453 		avalible_size = u32size - ((start_unit << 16) - u32addr);
454 		end_unit = start_unit +  (avalible_size >> 16);
455 	}
456 
457 
458 	if((end_unit == start_unit)  || (end_unit < start_unit))
459 	{
460 		RSA_FLOW_DBG("Invalid Input Size\n");
461 		return FALSE;
462 	}
463 	else
464 	{
465 		if(_u32CurrentSecureRangeSet < RSA_MAX_SECURE_RANGE_SIZE)
466 		{
467 			_RSA_REG32_W(&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_Start, start_unit);
468 			_RSA_REG32_W(&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_End, end_unit);
469 
470 			_u32CurrentSecureRangeSet ++;
471 
472 			*u32getaddr = start_unit << 16;
473 			*u32getsize = (end_unit - start_unit) << 16;
474 			RSA_FLOW_DBG("Rsa_Sec_Range_Start=%x, Rsa_Sec_Range_End=%x\n",
475 				&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_Start,
476 				&_RSACtrl[0].Rsa_Sec_Range[_u32CurrentSecureRangeSet].Rsa_Sec_Range_End);
477 
478 			RSA_FLOW_DBG("start_unit = %x, end_unit = %x, _u32CurrentSecureRangeSet  = %x",
479 				         start_unit, end_unit, _u32CurrentSecureRangeSet);
480 			return TRUE;
481 		}
482 		else
483 		{
484 			RSA_FLOW_DBG("Secure Range Set is not enough\n");
485 			return FALSE;
486 		}
487 	}
488 }
489 #endif
490 
491