2 #include <mach/mt_typedefs.h>
3 #include <mach/mt_sec_hal.h>
4 #include <mach/sec_osal.h>
9 extern int open_sdriver_connection(void);
10 extern int tee_secure_request(unsigned int user
, unsigned char *data
, unsigned int data_size
,
11 unsigned int direction
, unsigned char *seed
, unsigned int seed_size
);
12 extern int close_sdriver_connection(void);
14 /* To turn on HACC module clock if required */
15 unsigned char masp_hal_secure_algo_init(void)
22 /* To turn off HACC module clock if required */
23 unsigned char masp_hal_secure_algo_deinit(void)
30 /* This function will not work in TEE case */
31 unsigned int masp_hal_sp_hacc_init (unsigned char *sec_seed
, unsigned int size
)
33 /* No implemtation is required in TEE's case */
37 unsigned int masp_hal_sp_hacc_blk_sz (void)
42 static char* hacc_secure_request(HACC_USER user
, unsigned char *buf
, unsigned int buf_size
,
43 BOOL bEncrypt
, BOOL bDoLock
, unsigned char *sec_seed
, unsigned int seed_size
)
45 unsigned int ret
= SEC_OK
;
50 /* If the semaphore is successfully acquired, this function returns 0.*/
51 ret
= osal_hacc_lock();
55 ret
= ERR_SBOOT_HACC_LOCK_FAIL
;
60 masp_hal_secure_algo_init();
65 /* try to open connection to TEE */
66 if(open_sdriver_connection() < 0)
68 ret
= ERR_HACC_OPEN_SECURE_CONNECTION_FAIL
;
72 /* send request to TEE */
73 if( (ret
= tee_secure_request((unsigned int)user
, buf
, buf_size
, (unsigned int)bEncrypt
, sec_seed
, seed_size
)) != SEC_OK
)
75 ret
= ERR_HACC_REQUEST_SECURE_SERVICE_FAIL
;
79 if(close_sdriver_connection() < 0)
81 ret
= ERR_HACC_CLOSE_SECURE_CONNECTION_FAIL
;
87 printk("[HACC] hacc_secure_request - buffer size is 0, no encryption or decyrption is performed\n");
93 masp_hal_secure_algo_deinit();
94 /* release hacc lock */
102 printk("[HACC] hacc_secure_request fail (0x%x)\n", ret
);
110 void masp_hal_secure_algo(unsigned char Direction
, unsigned int ContentAddr
, unsigned int ContentLen
, unsigned char *CustomSeed
, unsigned char *ResText
)
112 unsigned int err
= 0;
113 unsigned char *src
, *dst
;
116 /* try to get hacc lock */
119 /* If the semaphore is successfully acquired, this function returns 0.*/
120 err
= osal_hacc_lock();
123 /* initialize source and destination address */
124 src
= (unsigned char *)ContentAddr
;
125 dst
= (unsigned char *)ResText
;
127 /* according to input parameter to encrypt or decrypt */
131 dst
= hacc_secure_request(HACC_USER3
, (unsigned char*)src
, ContentLen
, TRUE
, FALSE
, CustomSeed
, _CRYPTO_SEED_LEN
);//encrypt
135 dst
= hacc_secure_request(HACC_USER3
, (unsigned char*)src
, ContentLen
, FALSE
, FALSE
, CustomSeed
, _CRYPTO_SEED_LEN
);//decrypt
139 err
= ERR_KER_CRYPTO_INVALID_MODE
;
140 goto _wrong_direction
;
144 for (i
=0; i
< ContentLen
; i
++)
146 *(ResText
+i
) = *(dst
+i
);
150 /* try to release hacc lock */
155 printk("[HACC] masp_hal_secure_algo error (0x%x)\n", err
);
161 * For SECRO (user1), this function will help to get hacc lock
162 * For SECCFG (user1-sbchk), it should get hacc lock via ioctl command before using this function
163 * For MD NVRAM (user3), it should get hacc lock before using this function
164 * For AP NVRAM (user2), it should get hacc lock via ioctl command before using this function
166 unsigned char* masp_hal_sp_hacc_enc(unsigned char *buf
, unsigned int size
, unsigned char bAC
, HACC_USER user
, unsigned char bDoLock
)
168 return hacc_secure_request(user
, buf
, size
, TRUE
, bDoLock
, NULL
, 0);
171 unsigned char* masp_hal_sp_hacc_dec(unsigned char *buf
, unsigned int size
, unsigned char bAC
, HACC_USER user
, unsigned char bDoLock
)
173 return hacc_secure_request(user
, buf
, size
, FALSE
, bDoLock
, NULL
, 0);