9848de598ee799f9d857e4e3fe4d8f58076b4601
[platform/core/security/drm-service-core-tizen.git] / tappsd / src / util / DTapps2HMAC.cpp
1 /*
2  * Copyright (c) 2000-2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Flora License, Version 1.1 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 /**
19  * @file        DTapps2HMAC.cpp
20  * @brief       This file includes functions relating to HMAC.
21  * @author      Harsha Shekar (h.shekar@samsung.com)
22  */
23 #include <dukgen.h>
24 #include <openssl/evp.h>
25 #include <openssl/hmac.h>
26
27 #include "DTapps2HMAC.h"
28
29 void DTappsCalHMACSHA1(unsigned char* key,int key_len,unsigned char* msg,size_t msglen,unsigned char *md,unsigned int *md_len)
30 {
31         HMAC(EVP_sha1(),(void*)key,key_len,msg,msglen,md,md_len);
32 }
33
34 int DTappsGetDeviceKey(unsigned char **pDevKey,unsigned int *DevKeyLen)
35 {
36         int                             ret = 0;
37         char                    passwd[30] = {0,};
38         unsigned int    LocalKeyLen = 16;
39
40         char* pDuk = GetDeviceUniqueKey(passwd, strlen(passwd), 16);
41
42         if (!pDuk || !pDevKey || !DevKeyLen)
43         {
44                 DRM_TAPPS_EXCEPTION("pDevKey = %p, DevKeyLen = %p",pDevKey,DevKeyLen);
45                 goto DTApps_ERR;
46         }
47
48         DRM_TAPPS_LOG("LocalKeyLen = %u", LocalKeyLen);
49
50         *pDevKey = (unsigned char*)DTAPPS_MALLOC(LocalKeyLen + 1);
51
52         if(NULL == *pDevKey)
53         {
54                 DRM_TAPPS_EXCEPTION("Malloc Failed");
55
56                 goto DTApps_ERR;
57         }
58
59         DTAPPS_MEMSET(*pDevKey, 0x0, LocalKeyLen + 1);
60         DTAPPS_MEMCPY(*pDevKey, pDuk, LocalKeyLen);
61
62         *DevKeyLen = LocalKeyLen;
63         ret = 1;
64
65 DTApps_ERR:
66
67         if (ret == 1)
68                 DRM_TAPPS_LOG("SUCCESS:ret=%d", ret);
69         else
70                 DRM_TAPPS_EXCEPTION("FAILED:ret=%d", ret);
71
72         free(pDuk);
73
74         return ret;
75 }
76
77 void DTappsFreeDeviceKey(unsigned char **pDevKey)
78 {
79         if(pDevKey == NULL)
80         {
81                 DRM_TAPPS_EXCEPTION("pDevKey = %p", pDevKey);
82
83                 return;
84         }
85
86         DTAPPS_FREE(*pDevKey);
87         *pDevKey = NULL;
88 }