visibility attribute move to function definition
[platform/core/security/libcryptsvc.git] / srcs / SecCryptoSvc.c
1 /*
2  * libcryptsvc - device unique key
3  *
4  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <string.h>
23
24 #include <openssl/evp.h>
25 #include <openssl/err.h>
26 #include <openssl/rand.h>
27 #include <openssl/sha.h>
28
29 #include <dlog.h>
30
31 #include "SecCryptoSvc.h"
32
33 #define CS_API __attribute__((visibility("default")))
34
35 #define SYS_SECBOOT_DEV_ID_LEN  16
36 #define NAND_CID_NAME   "/sys/block/mmcblk0/device/cid"
37 #define NAND_CID_SIZE   32
38
39
40 #ifdef CRYPTOSVC_TARGET
41 static int __AsciiToHex(const char AsciiHexUpper,const char AsciiHexLower)
42 {
43         char hexReturn=0;
44
45         //First convert upper hex ascii value
46         if (AsciiHexUpper >= '0' && AsciiHexUpper <= '9')
47                 hexReturn = (AsciiHexUpper - '0') * 16;
48         else if (AsciiHexUpper >= 'A' && AsciiHexUpper <= 'F')
49                 hexReturn = ((AsciiHexUpper - 'A') + 10) * 16;
50         else if (AsciiHexUpper >= 'a' && AsciiHexUpper <= 'f')
51                 hexReturn = ((AsciiHexUpper - 'a') + 10) * 16;
52
53         //Convert lower hex ascii value
54         if (AsciiHexLower >= '0' && AsciiHexLower <= '9')
55                 hexReturn = hexReturn + (AsciiHexLower - '0');
56         else if (AsciiHexLower >= 'A' && AsciiHexLower <= 'F')
57                 hexReturn = hexReturn + (AsciiHexLower - 'A') + 10;
58         else if (AsciiHexLower >= 'a' && AsciiHexLower <= 'f')
59                 hexReturn = hexReturn + (AsciiHexLower - 'a') + 10;
60
61         return hexReturn;
62 }
63
64 static bool OemNandInfoUID(unsigned char *pUID, int nBufferSize)
65 {
66         int fd = 0;
67         char szCID[NAND_CID_SIZE + 1] = {0,};
68
69         memset(pUID, 0x00, nBufferSize);
70
71         fd = open(NAND_CID_NAME, O_RDONLY);
72         if (fd < 0) {
73                 SLOGE("cid open error!");
74                 return false;
75         }
76
77         if (read(fd, szCID, NAND_CID_SIZE) == -1) {
78                 SLOGE("cid read fail!!");
79                 close(fd);
80                 return false;
81         }
82
83         //manufacturer_id
84         pUID[0] =  __AsciiToHex(szCID[0], szCID[1]);
85         //oem_id
86         pUID[4] =  __AsciiToHex(szCID[4], szCID[5]);
87         //prod_rev
88         pUID[8] =  __AsciiToHex(szCID[18], szCID[19]);
89         //serial
90         pUID[15] = __AsciiToHex(szCID[20], szCID[21]);
91         pUID[14] =  __AsciiToHex(szCID[22], szCID[23]);
92         pUID[13] = __AsciiToHex(szCID[24], szCID[25]);
93         pUID[12] =  __AsciiToHex(szCID[26], szCID[27]);
94
95         // random permutation
96         pUID[1] = __AsciiToHex(szCID[2], szCID[3]);
97         pUID[2] = __AsciiToHex(szCID[6], szCID[7]);
98         pUID[3] = __AsciiToHex(szCID[8], szCID[9]);
99
100         pUID[5] = __AsciiToHex(szCID[10], szCID[11]);
101         pUID[6] = __AsciiToHex(szCID[12], szCID[13]);
102         pUID[7] = __AsciiToHex(szCID[14], szCID[15]);
103
104         pUID[9] = __AsciiToHex(szCID[16], szCID[17]);
105         pUID[10] = __AsciiToHex(szCID[28], szCID[29]);
106         pUID[11] = __AsciiToHex(szCID[30], szCID[31]);
107
108         SLOGD(" UID : %8X %8X %8X %8X",
109                 *(int *)pUID,
110                 *(int *)(pUID + 4),
111                 *(int *)(pUID + 8),
112                 *(int *)(pUID + 12));
113
114         close(fd);
115         return true;
116 }
117
118 static void SysSecBootGetDeviceUniqueKey(unsigned char *pUniquekey)
119 {
120         if (!OemNandInfoUID(pUniquekey, SYS_SECBOOT_DEV_ID_LEN))
121                 memset(pUniquekey, 0x00, SYS_SECBOOT_DEV_ID_LEN);
122 }
123 #endif
124
125
126 CS_API
127 bool SecFrameGeneratePlatformUniqueKey(unsigned int uLen, unsigned char *pCek)
128 {
129         bool bResult = true;
130         unsigned int i = 0;
131         unsigned char Key[73] = {0};
132         unsigned char hashedValue[HASH_LEN] = {0};
133         int nTempLen = SEC_DUK_SIZE;
134         int nHashLen = 0;
135         int remain = 0;
136         unsigned char *result = NULL;
137
138 #ifdef CRYPTOSVC_TARGET
139         SysSecBootGetDeviceUniqueKey(Key);
140 #else
141         memset(Key, 0xFF, nTempLen);
142 #endif
143
144         memcpy(Key+nTempLen, SEC_FRAME_OSP_KEY, 9);
145         nTempLen += 9;
146
147         remain = uLen;
148
149         for (i = 0; i < uLen; i += HASH_LEN) {
150                 result = SHA1(Key, nTempLen, hashedValue);
151                 nHashLen = HASH_LEN;
152
153                 if (!result) {
154                         SLOGE("SecCryptoHash fail");
155                         bResult = false;
156                         goto ERR;
157                 }
158
159                 nTempLen = nHashLen;
160
161                 if (remain < HASH_LEN)
162                         memcpy(pCek + i, hashedValue, remain);
163                 else
164                         memcpy(pCek + i, hashedValue, nHashLen);
165
166                 remain -= HASH_LEN;
167                 memset(Key, 0, sizeof(Key));
168                 memcpy(Key, hashedValue, nHashLen);
169         }
170
171         SLOGD("SecFrameGeneratePlatformUniqueKey Success.");
172
173 ERR:
174         return bResult;
175 }
176
177 CS_API
178 char *Base64Encoding(const char *data, int size)
179 {
180         char *pEncodedBuf = NULL;
181         const char *pPointer = NULL;
182         const char *pLength = data + size - 1;
183         unsigned char pInput[3] = {0,0,0};
184         unsigned char poutput[4] = {0,0,0,0};
185         int index = 0;
186         int loopCnt = 0;
187         int stringCnt = 0;
188         int sizeEncodedString = (4 * (size / 3)) + (size % 3 ? 4 : 0) + 1;
189
190         if (!(pEncodedBuf = (char *)calloc(sizeEncodedString, sizeof(char)))) {
191                 SLOGE("Failed to allocate memory");
192                 return NULL;
193         }
194
195         for (loopCnt = 0, pPointer = data; pPointer <= pLength; loopCnt++, pPointer++) {
196                 index = loopCnt % 3;
197                 pInput[index] = *pPointer;
198
199                 if (index == 2 || pPointer == pLength) {
200                         poutput[0] = ((pInput[0] & 0xFC) >> 2);
201                         poutput[1] = ((pInput[0] & 0x3) << 4) | ((pInput[1] & 0xF0) >> 4);
202                         poutput[2] = ((pInput[1] & 0xF) << 2) | ((pInput[2] & 0xC0) >> 6);
203                         poutput[3] = (pInput[2] & 0x3F);
204
205                         pEncodedBuf[stringCnt++] = Base64EncodingTable[poutput[0]];
206                         pEncodedBuf[stringCnt++] = Base64EncodingTable[poutput[1]];
207                         pEncodedBuf[stringCnt++] = index == 0 ? '=' : Base64EncodingTable[poutput[2]];
208                         pEncodedBuf[stringCnt++] = index < 2 ? '=' : Base64EncodingTable[poutput[3]];
209
210                         pInput[0] = pInput[1] = pInput[2] = 0;
211                 }
212         }
213
214         pEncodedBuf[stringCnt] = '\0';
215
216         return pEncodedBuf;
217 }
218
219 CS_API
220 char *GetDuid(int idSize)
221 {
222         const char *version = "1.0#";
223         char info[] = {0xca, 0xfe, 0xbe, 0xbe, 0x78, 0x07, 0x02, 0x03};
224
225         unsigned char *pKey = NULL;
226         unsigned char *pDuid = NULL;
227         char *pId = NULL;
228         char *pKeyVersion = NULL;
229
230         if (idSize <= 0) {
231                 SLOGE("Invalid Input [%d]", idSize);
232                 return NULL;
233         }
234
235         if (!(pKey = (unsigned char *)malloc(idSize))) {
236                 SLOGE("Failed to allocate memory for key");
237                 return NULL;
238         }
239
240         if (!SecFrameGeneratePlatformUniqueKey(idSize, pKey)) {
241                 SLOGE("Failed to get duid");
242                 goto exit;
243         }
244
245         if (!(pDuid = (unsigned char *)malloc(idSize))) {
246                 SLOGE("Failed to allocate memory");
247                 goto exit;
248         }
249
250         PKCS5_PBKDF2_HMAC_SHA1(info, 8, pKey, idSize, 1, idSize, pDuid);
251
252         if (!(pId = Base64Encoding((char *)pDuid, idSize))) {
253                 SLOGE("Failed to convert to base64 string");
254                 goto exit;
255         }
256
257         if (!(pKeyVersion = (char *)calloc(strlen(pId) + strlen(version) + 1, sizeof(char)))) {
258                 SLOGE("Failed to allocate memory");
259                 goto exit;
260         }
261         strncpy(pKeyVersion, version, strlen(version));
262         strncat(pKeyVersion, pId, strlen(pId));
263
264 exit:
265         free(pKey);
266         free(pDuid);
267         free(pId);
268
269         return pKeyVersion;
270 }
271