2 * libcryptsvc - device unique key
4 * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <openssl/evp.h>
25 #include <openssl/err.h>
26 #include <openssl/rand.h>
27 #include <openssl/sha.h>
31 #include "SecCryptoSvc.h"
33 #define SYS_SECBOOT_DEV_ID_LEN 16
34 #define NAND_CID_NAME "/sys/block/mmcblk0/device/cid"
35 #define NAND_CID_SIZE 32
38 #ifdef CRYPTOSVC_TARGET
39 static int __AsciiToHex(const char AsciiHexUpper,const char AsciiHexLower)
43 //First convert upper hex ascii value
44 if (AsciiHexUpper >= '0' && AsciiHexUpper <= '9')
45 hexReturn = (AsciiHexUpper - '0') * 16;
46 else if (AsciiHexUpper >= 'A' && AsciiHexUpper <= 'F')
47 hexReturn = ((AsciiHexUpper - 'A') + 10) * 16;
48 else if (AsciiHexUpper >= 'a' && AsciiHexUpper <= 'f')
49 hexReturn = ((AsciiHexUpper - 'a') + 10) * 16;
51 //Convert lower hex ascii value
52 if (AsciiHexLower >= '0' && AsciiHexLower <= '9')
53 hexReturn = hexReturn + (AsciiHexLower - '0');
54 else if (AsciiHexLower >= 'A' && AsciiHexLower <= 'F')
55 hexReturn = hexReturn + (AsciiHexLower - 'A') + 10;
56 else if (AsciiHexLower >= 'a' && AsciiHexLower <= 'f')
57 hexReturn = hexReturn + (AsciiHexLower - 'a') + 10;
62 bool OemNandInfoUID(unsigned char *pUID, int nBufferSize)
65 char szCID[NAND_CID_SIZE + 1] = {0,};
67 memset(pUID, 0x00, nBufferSize);
69 fd = open(NAND_CID_NAME, O_RDONLY);
71 SLOGE("cid open error!");
75 if (read(fd, szCID, NAND_CID_SIZE) == -1) {
76 SLOGE("cid read fail!!");
82 pUID[0] = __AsciiToHex((const char)szCID[0], (const char)szCID[1]);
84 pUID[4] = __AsciiToHex((const char)szCID[4], (const char)szCID[5]);
86 pUID[8] = __AsciiToHex((const char)szCID[18], (const char)szCID[19]);
88 pUID[15] = __AsciiToHex((const char)szCID[20], (const char)szCID[21]);
89 pUID[14] = __AsciiToHex((const char)szCID[22], (const char)szCID[23]);
90 pUID[13] = __AsciiToHex((const char)szCID[24], (const char)szCID[25]);
91 pUID[12] = __AsciiToHex((const char)szCID[26], (const char)szCID[27]);
94 pUID[1] = __AsciiToHex((const char)szCID[2], (const char)szCID[3]);
95 pUID[2] = __AsciiToHex((const char)szCID[6], (const char)szCID[7]);
96 pUID[3] = __AsciiToHex((const char)szCID[8], (const char)szCID[9]);
98 pUID[5] = __AsciiToHex((const char)szCID[10], (const char)szCID[11]);
99 pUID[6] = __AsciiToHex((const char)szCID[12], (const char)szCID[13]);
100 pUID[7] = __AsciiToHex((const char)szCID[14], (const char)szCID[15]);
102 pUID[9] = __AsciiToHex((const char)szCID[16], (const char)szCID[17]);
103 pUID[10] = __AsciiToHex((const char)szCID[28], (const char)szCID[29]);
104 pUID[11] = __AsciiToHex((const char)szCID[30], (const char)szCID[31]);
106 SLOGD(" UID : %8X %8X %8X %8X",
110 *(int *)(pUID + 12));
116 void SysSecBootGetDeviceUniqueKey(unsigned char *pUniquekey)
118 if (!OemNandInfoUID(pUniquekey, SYS_SECBOOT_DEV_ID_LEN))
119 memset(pUniquekey, 0x00, SYS_SECBOOT_DEV_ID_LEN);
124 bool SecFrameGeneratePlatformUniqueKey(unsigned int uLen, unsigned char *pCek)
128 unsigned char Key[73] = {0};
129 unsigned char hashedValue[HASH_LEN] = {0};
130 int nTempLen = SEC_DUK_SIZE;
133 unsigned char *result = NULL;
135 #ifdef CRYPTOSVC_TARGET
136 SysSecBootGetDeviceUniqueKey(Key);
138 memset(Key, 0xFF, nTempLen);
141 memcpy(Key+nTempLen, SEC_FRAME_OSP_KEY, 9);
146 for (i = 0; i < uLen; i += HASH_LEN) {
147 result = SHA1(Key, nTempLen, hashedValue);
151 SLOGE("SecCryptoHash fail");
158 if (remain < HASH_LEN)
159 memcpy(pCek + i, hashedValue, remain);
161 memcpy(pCek + i, hashedValue, nHashLen);
164 memset(Key, 0, sizeof(Key));
165 memcpy(Key, hashedValue, nHashLen);
168 SLOGD("SecFrameGeneratePlatformUniqueKey Success.");
174 char *Base64Encoding(char *pData, int size)
176 char *pEncodedBuf = NULL;
177 char *pPointer = NULL;
178 char *pLength = pData + size - 1;
179 unsigned char pInput[3] = {0,0,0};
180 unsigned char poutput[4] = {0,0,0,0};
184 int sizeEncodedString = (4 * (size / 3)) + (size % 3 ? 4 : 0) + 1;
186 if (!(pEncodedBuf = (char *)calloc(sizeEncodedString, sizeof(char)))) {
187 SLOGE("Failed to allocate memory");
191 for (loopCnt = 0, pPointer = pData; pPointer <= pLength; loopCnt++, pPointer++) {
193 pInput[index] = *pPointer;
195 if (index == 2 || pPointer == pLength) {
196 poutput[0] = ((pInput[0] & 0xFC) >> 2);
197 poutput[1] = ((pInput[0] & 0x3) << 4) | ((pInput[1] & 0xF0) >> 4);
198 poutput[2] = ((pInput[1] & 0xF) << 2) | ((pInput[2] & 0xC0) >> 6);
199 poutput[3] = (pInput[2] & 0x3F);
201 pEncodedBuf[stringCnt++] = Base64EncodingTable[poutput[0]];
202 pEncodedBuf[stringCnt++] = Base64EncodingTable[poutput[1]];
203 pEncodedBuf[stringCnt++] = index == 0 ? '=' : Base64EncodingTable[poutput[2]];
204 pEncodedBuf[stringCnt++] = index < 2 ? '=' : Base64EncodingTable[poutput[3]];
206 pInput[0] = pInput[1] = pInput[2] = 0;
210 pEncodedBuf[stringCnt] = '\0';
215 char *GetDuid(int idSize)
217 const char *version = "1.0#";
218 char info[] = {0xca, 0xfe, 0xbe, 0xbe, 0x78, 0x07, 0x02, 0x03};
220 unsigned char *pKey = NULL;
221 unsigned char *pDuid = NULL;
223 char *pKeyVersion = NULL;
226 SLOGE("Invalid Input [%d]", idSize);
230 if (!(pKey = (unsigned char *)malloc(idSize))) {
231 SLOGE("Failed to allocate memory for key");
235 if (!SecFrameGeneratePlatformUniqueKey(idSize, pKey)) {
236 SLOGE("Failed to get duid");
240 if (!(pDuid = (unsigned char *)malloc(idSize))) {
241 SLOGE("Failed to allocate memory");
245 PKCS5_PBKDF2_HMAC_SHA1(info, 8, pKey, idSize, 1, idSize, pDuid);
247 if (!(pId = Base64Encoding((char *)pDuid, idSize))) {
248 SLOGE("Failed to convert to base64 string");
252 if (!(pKeyVersion = (char *)calloc(strlen(pId) + strlen(version) + 1, sizeof(char)))) {
253 SLOGE("Failed to allocate memory");
256 strncpy(pKeyVersion, version, strlen(version));
257 strncat(pKeyVersion, pId, strlen(pId));