48ff6f6f3931d8c4b187ed7f4bcd9528a08f22d2
[platform/core/security/libcryptsvc.git] / gadget / duid-gadget.c
1 /*
2  *
3  * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <openssl/evp.h>
24 #include <openssl/rand.h>
25
26 #include <SecCryptoSvc.h>
27
28 int main()
29 {
30         const char *version = "1.0#";
31         char info[] = {0xca, 0xfe, 0xbe, 0xbe, 0x78, 0x07, 0x02, 0x03};
32
33         int ret = 0;
34         int keyLen = 20;
35         unsigned char *pKey = NULL;
36         unsigned char *pDuid = NULL;
37         char *pId = NULL;
38         char *pKeyVersion = NULL;
39
40         if (!(pKey = (unsigned char *)malloc(keyLen)))
41                 goto exit;
42
43         if (!SecFrameGeneratePlatformUniqueKey(keyLen, pKey)) {
44                 fprintf(stderr, "Failed to get duid\n");
45                 goto exit;
46         }
47
48         if (!(pDuid = (unsigned char *)malloc(keyLen)))
49                 goto exit;
50
51         PKCS5_PBKDF2_HMAC_SHA1(info, 8, pKey, keyLen, 1, keyLen, pDuid);
52
53         if (!(pId = Base64Encoding((char *)pDuid, keyLen)))
54                 goto exit;
55
56         if (!(pKeyVersion = (char *)calloc(strlen(pId) + strlen(version) + 1, sizeof(char))))
57                 goto exit;
58
59         strncpy(pKeyVersion, version, strlen(version));
60         strncat(pKeyVersion, pId, strlen(pId));
61         printf("%s", pKeyVersion);
62
63         ret = 1;
64
65 exit:
66         free(pKey);
67         free(pDuid);
68         free(pId);
69         free(pKeyVersion);
70
71         return ret;
72 }
73