import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / misc / mediatek / masp / mt8127 / mach / hacc_tee.c
1
2 #include <mach/mt_typedefs.h>
3 #include <mach/mt_sec_hal.h>
4 #include <mach/sec_osal.h>
5
6 #include "hacc_mach.h"
7 #include "sec_error.h"
8
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);
13
14 /* To turn on HACC module clock if required */
15 unsigned char masp_hal_secure_algo_init(void)
16 {
17 bool ret = TRUE;
18
19 return ret;
20 }
21
22 /* To turn off HACC module clock if required */
23 unsigned char masp_hal_secure_algo_deinit(void)
24 {
25 bool ret = TRUE;
26
27 return ret;
28 }
29
30 /* This function will not work in TEE case */
31 unsigned int masp_hal_sp_hacc_init (unsigned char *sec_seed, unsigned int size)
32 {
33 /* No implemtation is required in TEE's case */
34 return 0;
35 }
36
37 unsigned int masp_hal_sp_hacc_blk_sz (void)
38 {
39 return AES_BLK_SZ;
40 }
41
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)
44 {
45 unsigned int ret = SEC_OK;
46
47 /* get hacc lock */
48 if(TRUE == bDoLock)
49 {
50 /* If the semaphore is successfully acquired, this function returns 0.*/
51 ret = osal_hacc_lock();
52
53 if(ret)
54 {
55 ret = ERR_SBOOT_HACC_LOCK_FAIL;
56 goto _exit;
57 }
58 }
59 /* turn on clock */
60 masp_hal_secure_algo_init();
61
62
63 if(buf_size != 0)
64 {
65 /* try to open connection to TEE */
66 if(open_sdriver_connection() < 0)
67 {
68 ret = ERR_HACC_OPEN_SECURE_CONNECTION_FAIL;
69 goto _exit;
70 }
71
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)
74 {
75 ret = ERR_HACC_REQUEST_SECURE_SERVICE_FAIL;
76 goto _exit;
77 }
78
79 if(close_sdriver_connection() < 0)
80 {
81 ret = ERR_HACC_CLOSE_SECURE_CONNECTION_FAIL;
82 goto _exit;
83 }
84 }
85 else
86 {
87 printk("[HACC] hacc_secure_request - buffer size is 0, no encryption or decyrption is performed\n");
88 }
89
90
91 _exit:
92 /* turn off clock */
93 masp_hal_secure_algo_deinit();
94 /* release hacc lock */
95 if(TRUE == bDoLock)
96 {
97 osal_hacc_unlock();
98 }
99
100 if(ret)
101 {
102 printk("[HACC] hacc_secure_request fail (0x%x)\n", ret);
103
104 ASSERT(0);
105 }
106
107 return buf;
108 }
109
110 void masp_hal_secure_algo(unsigned char Direction, unsigned int ContentAddr, unsigned int ContentLen, unsigned char *CustomSeed, unsigned char *ResText)
111 {
112 unsigned int err = 0;
113 unsigned char *src, *dst;
114 unsigned int i = 0;
115
116 /* try to get hacc lock */
117 do
118 {
119 /* If the semaphore is successfully acquired, this function returns 0.*/
120 err = osal_hacc_lock();
121 }while( 0 != err );
122
123 /* initialize source and destination address */
124 src = (unsigned char *)ContentAddr;
125 dst = (unsigned char *)ResText;
126
127 /* according to input parameter to encrypt or decrypt */
128 switch (Direction)
129 {
130 case TRUE:
131 dst = hacc_secure_request(HACC_USER3, (unsigned char*)src, ContentLen, TRUE, FALSE, CustomSeed, _CRYPTO_SEED_LEN);//encrypt
132 break;
133
134 case FALSE:
135 dst = hacc_secure_request(HACC_USER3, (unsigned char*)src, ContentLen, FALSE, FALSE, CustomSeed, _CRYPTO_SEED_LEN);//decrypt
136 break;
137
138 default:
139 err = ERR_KER_CRYPTO_INVALID_MODE;
140 goto _wrong_direction;
141 }
142
143 /* copy result */
144 for (i=0; i < ContentLen; i++)
145 {
146 *(ResText+i) = *(dst+i);
147 }
148
149 _wrong_direction:
150 /* try to release hacc lock */
151 osal_hacc_unlock();
152
153 if(err)
154 {
155 printk("[HACC] masp_hal_secure_algo error (0x%x)\n", err);
156 ASSERT(0);
157 }
158 }
159
160 /*
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
165 */
166 unsigned char* masp_hal_sp_hacc_enc(unsigned char *buf, unsigned int size, unsigned char bAC, HACC_USER user, unsigned char bDoLock)
167 {
168 return hacc_secure_request(user, buf, size, TRUE, bDoLock, NULL, 0);
169 }
170
171 unsigned char* masp_hal_sp_hacc_dec(unsigned char *buf, unsigned int size, unsigned char bAC, HACC_USER user, unsigned char bDoLock)
172 {
173 return hacc_secure_request(user, buf, size, FALSE, bDoLock, NULL, 0);
174 }
175