summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDongsun Lee <ds73.lee@samsung.com>2015-11-04 17:49:48 +0900
committerDongsun Lee <ds73.lee@samsung.com>2015-11-06 10:08:28 +0900
commit56d5d7edb8e612b79e448ddda7361ac30d88488e (patch)
tree33fedecf26294e577c45fe04b7460157e68ba001
parenta59f1b1f2853ba347705bbdd97f587ebe946bb80 (diff)
downloadlibcryptsvc-56d5d7edb8e612b79e448ddda7361ac30d88488e.tar.gz
libcryptsvc-56d5d7edb8e612b79e448ddda7361ac30d88488e.tar.bz2
libcryptsvc-56d5d7edb8e612b79e448ddda7361ac30d88488e.zip
Change-Id: I719e3e4bcb123cd7fe5d6ac5c217e032399ea51a Signed-off-by: Dongsun Lee <ds73.lee@samsung.com>
-rw-r--r--CMakeLists.txt44
-rw-r--r--gadget/CMakeLists.txt28
-rw-r--r--gadget/duid-gadget.c73
-rw-r--r--include/device_info.h63
-rwxr-xr-xpackaging/libcryptsvc.spec11
-rwxr-xr-xsrcs/SecCryptoSvc.c241
-rw-r--r--srcs/device_info.c66
7 files changed, 406 insertions, 120 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e531784..2cb8477 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,7 +7,7 @@ SET(INCLUDEDIR "\${prefix}/include")
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/include)
-SET(pc_requires " openssl dlog libcrypto ")
+SET(pc_requires " openssl dlog libcrypto capi-system-info")
INCLUDE(FindPkgConfig)
pkg_check_modules(pkgs REQUIRED ${pc_requires})
@@ -55,31 +55,31 @@ SET_TARGET_PROPERTIES(
##################################################################################################################
##################################################################################################################
-# for libtzsvc.so
-#SET(SRCS_TZ
-# ${source_dir}/SecTzSvc.c
-#)
+# for libdevice_info.so
+SET(SRCS_DEVICE_INFO
+ ${source_dir}/device_info.c
+)
-#SET(libtzsvc_LDFLAGS "${pkgs_LDFLAGS}")
-#SET(libtzsvc_CFLAGS " ${CFLAGS} -fvisibility=hidden -g -fPIC -I${CMAKE_CURRENT_SOURCE_DIR}/include ")
-#SET(libtzsvc_CPPFLAGS " -DPIC ")
+SET(libdevice_info_LDFLAGS "${pkgs_LDFLAGS}")
+SET(libdevice_info_CFLAGS " ${CFLAGS} ${CMAKE_C_FLAGS} -fvisibility=hidden -g -fPIC -I${CMAKE_CURRENT_SOURCE_DIR}/include ")
+SET(libdevice_info_CPPFLAGS " -DPIC ")
-#SET(LIBTZ_SO "tzsvc")
-#ADD_LIBRARY(${LIBTZ_SO} SHARED ${SRCS_TZ})
+SET(LIBDEVICE_INFO_SO "device_info")
+ADD_LIBRARY(${LIBDEVICE_INFO_SO} SHARED ${SRCS_DEVICE_INFO})
-#TARGET_LINK_LIBRARIES(${LIBTZ_SO} ${libtzsvc_LDFLAGS})
+TARGET_LINK_LIBRARIES(${LIBDEVICE_INFO_SO} ${libdevice_info_LDFLAGS} cryptsvc)
-#IF("${ARCH}" MATCHES "arm")
-#TARGET_LINK_LIBRARIES(${LIBTZ_SO})
-#ENDIF("${ARCH}" MATCHES "arm")
+IF("${ARCH}" MATCHES "arm")
+TARGET_LINK_LIBRARIES(${LIBDEVICE_INFO_SO})
+ENDIF("${ARCH}" MATCHES "arm")
-#SET_TARGET_PROPERTIES(
-# ${LIBTZ_SO}
-# PROPERTIES
-# VERSION ${FULLVER}
-# SOVERSION ${MAJORVER}
-#)
+SET_TARGET_PROPERTIES(
+ ${LIBDEVICE_INFO_SO}
+ PROPERTIES
+ VERSION ${FULLVER}
+ SOVERSION ${MAJORVER}
+)
##################################################################################################################
@@ -115,7 +115,9 @@ CONFIGURE_FILE(${PROJECT_NAME}.pc.in ${PROJECT_NAME}.pc @ONLY)
INSTALL(FILES ${PROJECT_NAME}.pc DESTINATION ${LIB_INSTALL_DIR}/pkgconfig)
INSTALL(TARGETS ${PROJECT_NAME} DESTINATION ${LIB_INSTALL_DIR})
-INSTALL(TARGETS ${LIBTZ_SO} DESTINATION ${LIB_INSTALL_DIR})
+INSTALL(TARGETS ${LIBDEVICE_INFO_SO} DESTINATION ${LIB_INSTALL_DIR})
INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ DESTINATION ${INCLUDEDIR})
ADD_DEFINITIONS(-D_bool_cryptsvc)
+
+ADD_SUBDIRECTORY(gadget)
diff --git a/gadget/CMakeLists.txt b/gadget/CMakeLists.txt
new file mode 100644
index 0000000..dcb40c9
--- /dev/null
+++ b/gadget/CMakeLists.txt
@@ -0,0 +1,28 @@
+SET(GADGET duid-gadget)
+SET(GADGET_SRCS
+ duid-gadget.c
+ ../srcs/SecCryptoSvc.c
+)
+
+SET(GADGET_VENDOR samsung)
+SET(GADGET_DIR ${TZ_SYS_ETC})
+
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
+
+INCLUDE(FindPkgConfig)
+PKG_CHECK_MODULES(GADGET_PKGS REQUIRED dlog openssl)
+
+FOREACH(flag ${GADGET_PKGS_CFLAGS})
+ SET(GADGET_CFLAGS "${GADGET_CFLAGS} ${flag} -fPIE")
+ENDFOREACH(flag)
+
+SET(GADGET_CFLAGS, "${GADGET_CFLAGS} -DPREFIX=\"${CMAKE_INSTALL_PREFIX}\"")
+SET(GADGET_CFLAGS, "${GADGET_CFLAGS} -DVENDOR=\"${GADGET_VENDOR}\"")
+SET(GADGET_CFLAGS, "${GADGET_CFLAGS} -DAPP_NAME=\"${GADGET}\"")
+SET(GADGET_CFLAGS, "${GADGET_CFLAGS} -DAPP_DIR=\"${GADGET_DIR}\"")
+
+ADD_EXECUTABLE(${GADGET} ${GADGET_SRCS})
+SET_TARGET_PROPERTIES(${GADGET} PROPERTIES COMPILE_FLAGS "${GADGET_CFLAGS}")
+TARGET_LINK_LIBRARIES(${GADGET} ${GADGET_PKGS_LDFLAGS} -pie)
+
+INSTALL(TARGETS ${GADGET} DESTINATION ${GADGET_DIR} )
diff --git a/gadget/duid-gadget.c b/gadget/duid-gadget.c
new file mode 100644
index 0000000..48ff6f6
--- /dev/null
+++ b/gadget/duid-gadget.c
@@ -0,0 +1,73 @@
+/*
+ *
+ * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <openssl/evp.h>
+#include <openssl/rand.h>
+
+#include <SecCryptoSvc.h>
+
+int main()
+{
+ const char *version = "1.0#";
+ char info[] = {0xca, 0xfe, 0xbe, 0xbe, 0x78, 0x07, 0x02, 0x03};
+
+ int ret = 0;
+ int keyLen = 20;
+ unsigned char *pKey = NULL;
+ unsigned char *pDuid = NULL;
+ char *pId = NULL;
+ char *pKeyVersion = NULL;
+
+ if (!(pKey = (unsigned char *)malloc(keyLen)))
+ goto exit;
+
+ if (!SecFrameGeneratePlatformUniqueKey(keyLen, pKey)) {
+ fprintf(stderr, "Failed to get duid\n");
+ goto exit;
+ }
+
+ if (!(pDuid = (unsigned char *)malloc(keyLen)))
+ goto exit;
+
+ PKCS5_PBKDF2_HMAC_SHA1(info, 8, pKey, keyLen, 1, keyLen, pDuid);
+
+ if (!(pId = Base64Encoding((char *)pDuid, keyLen)))
+ goto exit;
+
+ if (!(pKeyVersion = (char *)calloc(strlen(pId) + strlen(version) + 1, sizeof(char))))
+ goto exit;
+
+ strncpy(pKeyVersion, version, strlen(version));
+ strncat(pKeyVersion, pId, strlen(pId));
+ printf("%s", pKeyVersion);
+
+ ret = 1;
+
+exit:
+ free(pKey);
+ free(pDuid);
+ free(pId);
+ free(pKeyVersion);
+
+ return ret;
+}
+
diff --git a/include/device_info.h b/include/device_info.h
new file mode 100644
index 0000000..d4c0e24
--- /dev/null
+++ b/include/device_info.h
@@ -0,0 +1,63 @@
+/*
+ * device info
+ *
+ * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef __DEVICE_INFO__
+#define __DEVICE_INFO__
+
+#include <tizen.h>
+
+/**
+ * @addtogroup CAPI_DEVICE_INFO_MODULE
+ * @{
+ */
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * @brief Get device ID
+ * @since_tizen 2.3
+ * @return device unique ID that null terminated string
+ * @see get_device_id()
+ * @code
+ * #include <device_info.h>
+ *
+ * ...
+ *
+ * const char* device_id = NULL;
+ *
+ * device_id = get_device_id();
+ * if(device_id == NULL)
+ * {
+ * // Error handling
+ * }
+ *
+ * ...
+ * @endcode
+ *
+ */
+const char* get_device_id(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/packaging/libcryptsvc.spec b/packaging/libcryptsvc.spec
index 1073249..4955106 100755
--- a/packaging/libcryptsvc.spec
+++ b/packaging/libcryptsvc.spec
@@ -9,6 +9,9 @@ Source1001: libcryptsvc.manifest
BuildRequires: cmake
BuildRequires: pkgconfig(dlog)
BuildRequires: pkgconfig(openssl)
+BuildRequires: pkgconfig(capi-system-info)
+BuildRequires: pkgconfig(libtzplatform-config)
+Requires: pkgconfig(libtzplatform-config)
%description
Crypto Service Library.
@@ -28,10 +31,13 @@ cp %{SOURCE1001} .
%build
MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
%ifarch %ix86 x86_64
-%cmake . -DARCH=x86 -DFULLVER=%{version} -DMAJORVER=${MAJORVER} -DDESCRIPTION="%{summary}"
+%cmake . -DARCH=x86 \
%else
-%cmake . -DARCH=arm -DFULLVER=%{version} -DMAJORVER=${MAJORVER} -DDESCRIPTION="%{summary}"
+%cmake . -DARCH=arm \
%endif
+ -DFULLVER=%{version} -DMAJORVER=${MAJORVER} -DDESCRIPTION="%{summary}" \
+ -DTZ_SYS_ETC=%TZ_SYS_ETC
+
make %{?jobs:-j%jobs}
%install
@@ -46,6 +52,7 @@ make %{?jobs:-j%jobs}
%manifest %{name}.manifest
%license LICENSE.APLv2
%{_libdir}/*.so*
+%attr(755,root,root) %{TZ_SYS_ETC}/duid-gadget
%files devel
%manifest %{name}.manifest
diff --git a/srcs/SecCryptoSvc.c b/srcs/SecCryptoSvc.c
index 8d03381..44d5ce0 100755
--- a/srcs/SecCryptoSvc.c
+++ b/srcs/SecCryptoSvc.c
@@ -17,184 +17,178 @@
*
*/
-#include "SecCryptoSvc.h"
-//#include "SecKmBase64.h"
-//#include "CryptoSvc-debug.h"
+#include <fcntl.h>
+#include <unistd.h>
#include <string.h>
-#include <stdio.h>
+
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
+
#include <dlog.h>
-#include <fcntl.h>
-#include <unistd.h>
+
+#include "SecCryptoSvc.h"
#define SYS_SECBOOT_DEV_ID_LEN 16
#define NAND_CID_NAME "/sys/block/mmcblk0/device/cid"
#define NAND_CID_SIZE 32
+#ifdef CRYPTOSVC_TARGET
static int __AsciiToHex(const char AsciiHexUpper,const char AsciiHexLower)
{
- char hexReturn=0;
-
- //First convert upper hex ascii value
- if(AsciiHexUpper >= '0' && AsciiHexUpper <= '9')
- hexReturn= (AsciiHexUpper - '0')*16;
- else if(AsciiHexUpper >= 'A' && AsciiHexUpper <= 'F')
- hexReturn= ((AsciiHexUpper - 'A')+10)*16;
- else if(AsciiHexUpper >= 'a' && AsciiHexUpper <= 'f')
- hexReturn= ((AsciiHexUpper - 'a')+10)*16;
-
- //Convert lower hex ascii value
- if(AsciiHexLower >= '0' && AsciiHexLower <= '9')
- hexReturn= hexReturn + (AsciiHexLower - '0');
- else if(AsciiHexLower >= 'A' && AsciiHexLower <= 'F')
- hexReturn= hexReturn + (AsciiHexLower - 'A')+10;
- else if(AsciiHexLower >= 'a' && AsciiHexLower <= 'f')
- hexReturn= hexReturn + (AsciiHexLower - 'a')+10;
-
- return hexReturn;
+ char hexReturn=0;
+
+ //First convert upper hex ascii value
+ if (AsciiHexUpper >= '0' && AsciiHexUpper <= '9')
+ hexReturn = (AsciiHexUpper - '0') * 16;
+ else if (AsciiHexUpper >= 'A' && AsciiHexUpper <= 'F')
+ hexReturn = ((AsciiHexUpper - 'A') + 10) * 16;
+ else if (AsciiHexUpper >= 'a' && AsciiHexUpper <= 'f')
+ hexReturn = ((AsciiHexUpper - 'a') + 10) * 16;
+
+ //Convert lower hex ascii value
+ if (AsciiHexLower >= '0' && AsciiHexLower <= '9')
+ hexReturn = hexReturn + (AsciiHexLower - '0');
+ else if (AsciiHexLower >= 'A' && AsciiHexLower <= 'F')
+ hexReturn = hexReturn + (AsciiHexLower - 'A') + 10;
+ else if (AsciiHexLower >= 'a' && AsciiHexLower <= 'f')
+ hexReturn = hexReturn + (AsciiHexLower - 'a') + 10;
+
+ return hexReturn;
}
-bool
-OemNandInfoUID(unsigned char* pUID, int nBufferSize)
+bool OemNandInfoUID(unsigned char *pUID, int nBufferSize)
{
int fd = 0;
- char szCID[NAND_CID_SIZE+1] = {0,};
+ char szCID[NAND_CID_SIZE + 1] = {0,};
- memset(pUID, 0x0, nBufferSize);
+ memset(pUID, 0x00, nBufferSize);
fd = open(NAND_CID_NAME, O_RDONLY);
- if (fd < 0)
- {
- printf("cid open error!\n");
+ if (fd < 0) {
+ SLOGE("cid open error!");
return false;
}
- if(read(fd, szCID, NAND_CID_SIZE) == -1)
- {
- printf("cid read fail!!\n");
+ if (read(fd, szCID, NAND_CID_SIZE) == -1) {
+ SLOGE("cid read fail!!");
close(fd);
return false;
}
//manufacturer_id
- pUID[0] = __AsciiToHex((const char)szCID[0],(const char)szCID[1]);
+ pUID[0] = __AsciiToHex((const char)szCID[0], (const char)szCID[1]);
//oem_id
- pUID[4] = __AsciiToHex((const char)szCID[4],(const char)szCID[5]);
+ pUID[4] = __AsciiToHex((const char)szCID[4], (const char)szCID[5]);
//prod_rev
- pUID[8] = __AsciiToHex((const char)szCID[18],(const char)szCID[19]);
+ pUID[8] = __AsciiToHex((const char)szCID[18], (const char)szCID[19]);
//serial
- pUID[15] = __AsciiToHex((const char)szCID[20],(const char)szCID[21]);
- pUID[14] = __AsciiToHex((const char)szCID[22],(const char)szCID[23]);
- pUID[13] = __AsciiToHex((const char)szCID[24],(const char)szCID[25]);
- pUID[12] = __AsciiToHex((const char)szCID[26],(const char)szCID[27]);
+ pUID[15] = __AsciiToHex((const char)szCID[20], (const char)szCID[21]);
+ pUID[14] = __AsciiToHex((const char)szCID[22], (const char)szCID[23]);
+ pUID[13] = __AsciiToHex((const char)szCID[24], (const char)szCID[25]);
+ pUID[12] = __AsciiToHex((const char)szCID[26], (const char)szCID[27]);
// random permutation
- pUID[1] = __AsciiToHex((const char)szCID[2],(const char)szCID[3]);
- pUID[2] = __AsciiToHex((const char)szCID[6],(const char)szCID[7]);
- pUID[3] = __AsciiToHex((const char)szCID[8],(const char)szCID[9]);
+ pUID[1] = __AsciiToHex((const char)szCID[2], (const char)szCID[3]);
+ pUID[2] = __AsciiToHex((const char)szCID[6], (const char)szCID[7]);
+ pUID[3] = __AsciiToHex((const char)szCID[8], (const char)szCID[9]);
+
+ pUID[5] = __AsciiToHex((const char)szCID[10], (const char)szCID[11]);
+ pUID[6] = __AsciiToHex((const char)szCID[12], (const char)szCID[13]);
+ pUID[7] = __AsciiToHex((const char)szCID[14], (const char)szCID[15]);
- pUID[5] = __AsciiToHex((const char)szCID[10],(const char)szCID[11]);
- pUID[6] = __AsciiToHex((const char)szCID[12],(const char)szCID[13]);
- pUID[7] = __AsciiToHex((const char)szCID[14],(const char)szCID[15]);
+ pUID[9] = __AsciiToHex((const char)szCID[16], (const char)szCID[17]);
+ pUID[10] = __AsciiToHex((const char)szCID[28], (const char)szCID[29]);
+ pUID[11] = __AsciiToHex((const char)szCID[30], (const char)szCID[31]);
- pUID[9] = __AsciiToHex((const char)szCID[16],(const char)szCID[17]);
- pUID[10] = __AsciiToHex((const char)szCID[28],(const char)szCID[29]);
- pUID[11] = __AsciiToHex((const char)szCID[30],(const char)szCID[31]);
- //printf(" UID : %8X %8X %8X %8X\n", *(int*)pUID, *(int*)(pUID+4), *(int*)(pUID+8), *(int*)(pUID+12));
+ SLOGD(" UID : %8X %8X %8X %8X",
+ *(int *)pUID,
+ *(int *)(pUID + 4),
+ *(int *)(pUID + 8),
+ *(int *)(pUID + 12));
close(fd);
return true;
}
-void SysSecBootGetDeviceUniqueKey(unsigned char* pUniquekey)
+void SysSecBootGetDeviceUniqueKey(unsigned char *pUniquekey)
{
- bool result = OemNandInfoUID(pUniquekey, SYS_SECBOOT_DEV_ID_LEN);
- if(result != true){
- printf("SysSecBootGetDeviceUniqueKey is failed");
- memcpy(pUniquekey, 0x00, SYS_SECBOOT_DEV_ID_LEN);
- }
+ if (!OemNandInfoUID(pUniquekey, SYS_SECBOOT_DEV_ID_LEN))
+ memset(pUniquekey, 0x00, SYS_SECBOOT_DEV_ID_LEN);
}
+#endif
+
-bool SecFrameGeneratePlatformUniqueKey(IN UINT32 uLen, IN OUT UINT8 *pCek)
+bool SecFrameGeneratePlatformUniqueKey(unsigned int uLen, unsigned char *pCek)
{
- bool bResult = true;
- unsigned int i = 0;
- unsigned char Key[73] = {0};
- unsigned char hashedValue[HASH_LEN] = {0};
- int nTempLen = SEC_DUK_SIZE;
- int nHashLen = 0;
- int remain = 0;
- unsigned char *result = NULL;
-
- SLOGD("[LOG][%s:L%d] Enter \n", __func__,__LINE__);
+ bool bResult = true;
+ unsigned int i = 0;
+ unsigned char Key[73] = {0};
+ unsigned char hashedValue[HASH_LEN] = {0};
+ int nTempLen = SEC_DUK_SIZE;
+ int nHashLen = 0;
+ int remain = 0;
+ unsigned char *result = NULL;
+
#ifdef CRYPTOSVC_TARGET
SysSecBootGetDeviceUniqueKey(Key);
#else
memset(Key, 0xFF, nTempLen);
#endif
- /* for debugging */
- SLOGD("Device Unique Key Information \n");
-
memcpy(Key+nTempLen, SEC_FRAME_OSP_KEY, 9);
nTempLen += 9;
remain = uLen;
- for( i = 0 ; i < uLen ; i += HASH_LEN )
- {
+ for (i = 0; i < uLen; i += HASH_LEN) {
result = SHA1(Key, nTempLen, hashedValue);
nHashLen = HASH_LEN;
- if( result == NULL)
- {
- SLOGE("SecCryptoHash fail \n");
+ if (!result) {
+ SLOGE("SecCryptoHash fail");
bResult = false;
goto ERR;
}
nTempLen = nHashLen;
- if( remain < HASH_LEN )
- {
- memcpy(pCek+i, hashedValue, remain);
- }
+ if (remain < HASH_LEN)
+ memcpy(pCek + i, hashedValue, remain);
else
- {
- memcpy(pCek+i, hashedValue, nHashLen);
- }
+ memcpy(pCek + i, hashedValue, nHashLen);
remain -= HASH_LEN;
memset(Key, 0, sizeof(Key));
memcpy(Key, hashedValue, nHashLen);
}
- SLOGD("[LOG][%s:L%d] End \n", __func__,__LINE__);
+
+ SLOGD("SecFrameGeneratePlatformUniqueKey Success.");
+
ERR:
- SLOGD("[LOG][%s:L%d] End with ERROR \n", __func__,__LINE__);
return bResult;
}
-char* Base64Encoding(char* pData, int size)
+char *Base64Encoding(char *pData, int size)
{
- char* pEncodedBuf = NULL;
- char* pPointer = NULL;
- char* pLength = NULL;
+ char *pEncodedBuf = NULL;
+ char *pPointer = NULL;
+ char *pLength = pData + size - 1;
unsigned char pInput[3] = {0,0,0};
unsigned char poutput[4] = {0,0,0,0};
int index = 0;
int loopCnt = 0;
int stringCnt = 0;
- int sizeEncodedString = 0;
+ int sizeEncodedString = (4 * (size / 3)) + (size % 3 ? 4 : 0) + 1;
- pLength = pData + size - 1;
- sizeEncodedString = (4 * (size / 3)) + (size % 3? 4 : 0) + 1;
- pEncodedBuf = (char*)calloc(sizeEncodedString, sizeof(char));
+ if (!(pEncodedBuf = (char *)calloc(sizeEncodedString, sizeof(char)))) {
+ SLOGE("Failed to allocate memory");
+ return NULL;
+ }
- for (loopCnt = 0, pPointer = pData; pPointer <= pLength; loopCnt++, pPointer++) {
+ for (loopCnt = 0, pPointer = pData; pPointer <= pLength; loopCnt++, pPointer++) {
index = loopCnt % 3;
pInput[index] = *pPointer;
@@ -206,8 +200,8 @@ char* Base64Encoding(char* pData, int size)
pEncodedBuf[stringCnt++] = Base64EncodingTable[poutput[0]];
pEncodedBuf[stringCnt++] = Base64EncodingTable[poutput[1]];
- pEncodedBuf[stringCnt++] = index == 0? '=' : Base64EncodingTable[poutput[2]];
- pEncodedBuf[stringCnt++] = index < 2? '=' : Base64EncodingTable[poutput[3]];
+ pEncodedBuf[stringCnt++] = index == 0 ? '=' : Base64EncodingTable[poutput[2]];
+ pEncodedBuf[stringCnt++] = index < 2 ? '=' : Base64EncodingTable[poutput[3]];
pInput[0] = pInput[1] = pInput[2] = 0;
}
@@ -217,3 +211,56 @@ char* Base64Encoding(char* pData, int size)
return pEncodedBuf;
}
+
+char *GetDuid(int idSize)
+{
+ const char *version = "1.0#";
+ char info[] = {0xca, 0xfe, 0xbe, 0xbe, 0x78, 0x07, 0x02, 0x03};
+
+ unsigned char *pKey = NULL;
+ unsigned char *pDuid = NULL;
+ char *pId = NULL;
+ char *pKeyVersion = NULL;
+
+ if (idSize <= 0) {
+ SLOGE("Invalid Input [%d]", idSize);
+ return NULL;
+ }
+
+ if (!(pKey = (unsigned char *)malloc(idSize))) {
+ SLOGE("Failed to allocate memory for key");
+ return NULL;
+ }
+
+ if (!SecFrameGeneratePlatformUniqueKey(idSize, pKey)) {
+ SLOGE("Failed to get duid");
+ goto exit;
+ }
+
+ if (!(pDuid = (unsigned char *)malloc(idSize))) {
+ SLOGE("Failed to allocate memory");
+ goto exit;
+ }
+
+ PKCS5_PBKDF2_HMAC_SHA1(info, 8, pKey, idSize, 1, idSize, pDuid);
+
+ if (!(pId = Base64Encoding((char *)pDuid, idSize))) {
+ SLOGE("Failed to convert to base64 string");
+ goto exit;
+ }
+
+ if (!(pKeyVersion = (char *)calloc(strlen(pId) + strlen(version) + 1, sizeof(char)))) {
+ SLOGE("Failed to allocate memory");
+ goto exit;
+ }
+ strncpy(pKeyVersion, version, strlen(version));
+ strncat(pKeyVersion, pId, strlen(pId));
+
+exit:
+ free(pKey);
+ free(pDuid);
+ free(pId);
+
+ return pKeyVersion;
+}
+
diff --git a/srcs/device_info.c b/srcs/device_info.c
new file mode 100644
index 0000000..b15ef5a
--- /dev/null
+++ b/srcs/device_info.c
@@ -0,0 +1,66 @@
+/*
+ * device info
+ *
+ * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <string.h>
+#include <pthread.h>
+
+#include <dlog.h>
+#include <system_info.h>
+
+#include <device_info.h>
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#define LOG_TAG "LIBCRYPTSVC"
+#endif
+
+#define TIZENID_STRING "http://tizen.org/system/tizenid"
+
+char* _device_id = NULL;
+int _is_loaded = false;
+pthread_once_t _load_once_block = PTHREAD_ONCE_INIT;
+
+void load_device_id(void)
+{
+ if (!_device_id
+ && system_info_get_platform_string(TIZENID_STRING, &_device_id) != SYSTEM_INFO_ERROR_NONE) {
+ SECURE_LOGE("Failed to generate DUID.");
+ return;
+ }
+
+ _is_loaded = true;
+}
+
+
+__attribute__((visibility("default")))
+const char *get_device_id(void)
+{
+ if (!_is_loaded) {
+ pthread_once(&_load_once_block, load_device_id);
+ if (!_is_loaded
+ || !_device_id
+ || !strlen(_device_id)) {
+ LOGE("failed to get device id");
+ _load_once_block = PTHREAD_ONCE_INIT;
+ return NULL;
+ }
+ }
+
+ return _device_id;
+}