merge with master
[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 "SecCryptoSvc.h"
21 //#include "SecKmBase64.h"
22 //#include "CryptoSvc-debug.h"
23 #include <string.h>
24 #include <stdio.h>
25 #include <openssl/evp.h>
26 #include <openssl/err.h>
27 #include <openssl/rand.h>
28 #include <openssl/sha.h>
29 #include <dlog.h>
30
31 bool SecFrameGeneratePlatformUniqueKey(IN UINT32  uLen, IN OUT UINT8  *pCek)
32 {
33         bool            bResult = true;
34         unsigned int    i = 0;
35         unsigned char   Key[73] = {0};
36         unsigned char   hashedValue[HASH_LEN] = {0};
37         int             nTempLen = SEC_DUK_SIZE;
38         int             nHashLen = 0;
39         int             remain = 0;
40         unsigned char   *result = NULL;
41
42         SLOGD("[LOG][%s:L%d] Enter \n", __func__,__LINE__);
43 #ifdef CRYPTOSVC_TARGET
44         SysSecBootGetDeviceUniqueKey(Key);
45 #else
46         memset(Key, 0xFF, nTempLen);
47 #endif
48
49         /* for debugging */
50         SLOGD("Device Unique Key Information \n");
51
52         memcpy(Key+nTempLen, SEC_FRAME_OSP_KEY, 9);
53         nTempLen += 9;
54
55         remain = uLen;
56
57         for( i = 0 ; i < uLen ; i += HASH_LEN )
58         {
59                 result = SHA1(Key, nTempLen, hashedValue);
60                 nHashLen = HASH_LEN;
61
62                 if( result == NULL)
63                 {
64                         SLOGE("SecCryptoHash fail \n");
65                         bResult = false;
66                         goto ERR;
67                 }
68
69                 nTempLen = nHashLen;
70
71                 if( remain < HASH_LEN )
72                 {
73                         memcpy(pCek+i, hashedValue, remain);
74                 }
75                 else
76                 {
77                         memcpy(pCek+i, hashedValue, nHashLen);
78                 }
79
80                 remain -= HASH_LEN;
81                 memset(Key, 0, sizeof(Key));
82                 memcpy(Key, hashedValue, nHashLen);
83         }
84         SLOGD("[LOG][%s:L%d] End \n", __func__,__LINE__);
85 ERR:
86         SLOGD("[LOG][%s:L%d] End with ERROR \n", __func__,__LINE__);
87         return bResult;
88 }
89
90