Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / security-assistant / cryptograhic_hash_function.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 #include "security-assistant/cryptograhic_hash_function.h"
19 #include <gcrypt.h>
20 #include <gpg-error.h>
21
22 #include "utility/sync_util.h"
23
24 #ifndef SYNC_AGENT_LOG
25 #undef LOG_TAG
26 #define LOG_TAG "AF_SA"
27 #endif
28
29 static unsigned char *_crypt_crytograhic_hash_function(int algo, const char *data, int data_len, const char *key, int key_len, int flags);
30 static unsigned char *_crypt_symmetric_cipher_function(const char *plain, int plain_len, const char *key, int key_len, int flags);
31
32 unsigned char *sa_crypt_des(const char *plain, int plain_len, const char *key, int key_len)
33 {
34         _EXTERN_FUNC_ENTER;
35
36         int flags = 0;
37
38         _EXTERN_FUNC_EXIT;
39
40         return _crypt_symmetric_cipher_function(plain, plain_len, key, key_len, flags);
41 }
42
43 unsigned char *sa_crypt_md5(sync_agent_sa_cryptographic_hash_function_flags_e flags, const char *plain, int plain_len, const char *key, int key_len)
44 {
45         _EXTERN_FUNC_ENTER;
46
47         unsigned char *crypt_data = NULL;
48
49         switch (flags) {
50         case SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUN_HMAC:
51                 {
52                         _DEBUG_INFO("SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUN_HMAC");
53                         crypt_data = _crypt_crytograhic_hash_function(GCRY_MD_MD5, plain, plain_len, key, key_len, GCRY_MD_FLAG_HMAC);
54                 }
55                 break;
56         default:
57                 {
58                         _DEBUG_INFO("default");
59                         crypt_data = _crypt_crytograhic_hash_function(GCRY_MD_MD5, plain, plain_len, 0, 0, 0);
60                 }
61                 break;
62         }
63
64         _EXTERN_FUNC_EXIT;
65
66         return crypt_data;
67 }
68
69 unsigned char *sa_crypt_sha1(sync_agent_sa_cryptographic_hash_function_flags_e flags, const char *plain, int plain_len, const char *key, int key_len)
70 {
71         _EXTERN_FUNC_ENTER;
72
73         unsigned char *crypt_data = NULL;
74
75         switch (flags) {
76         case SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUN_HMAC:
77                 {
78                         _DEBUG_INFO("SYNC_AGENT_SA_CRYPTOGRAHIC_HASH_FUN_HMAC");
79                         crypt_data = _crypt_crytograhic_hash_function(GCRY_MD_SHA1, plain, plain_len, key, key_len, GCRY_MD_FLAG_HMAC);
80                 }
81                 break;
82         default:
83                 {
84                         _DEBUG_INFO("default");
85                         crypt_data = _crypt_crytograhic_hash_function(GCRY_MD_SHA1, plain, plain_len, 0, 0, 0);
86                 }
87                 break;
88         }
89
90         _EXTERN_FUNC_EXIT;
91
92         return crypt_data;
93 }
94
95 static unsigned char *_crypt_crytograhic_hash_function(int algo, const char *data, int data_len, const char *key, int key_len, int flags)
96 {
97         _INNER_FUNC_ENTER;
98
99         gcry_md_hd_t hd, hd2;
100         unsigned char *crypt_data = NULL;
101         unsigned char *p = NULL;
102         int crypt_data_len = 0;
103         int i;
104         gcry_error_t err = 0;
105
106         err = gcry_md_open(&hd, algo, flags);
107         if (err) {
108                 _DEBUG_TRACE("algo %d, grcy_md_open failed: %s\n", algo, gpg_strerror(err));
109                 return NULL;
110         }
111
112         crypt_data_len = gcry_md_get_algo_dlen(algo);
113         if (crypt_data_len < 1 || crypt_data_len > 500) {
114                 _DEBUG_TRACE("algo %d, grcy_md_get_algo_dlen failed: %d\n", algo, crypt_data_len);
115                 return NULL;
116         }
117
118         if (flags != 0) {
119                 gcry_md_setkey(hd, key, key_len);
120                 if (err) {
121                         _DEBUG_TRACE("algo %d, grcy_md_setkey failed: %s\n", algo, gpg_strerror(err));
122                         return NULL;
123                 }
124         }
125
126         if (*data == '!' && !data[1]) { /* hash one million times a "a" */
127                 char aaa[1000];
128
129                 /* Write in odd size chunks so that we test the buffering.  */
130                 memset(aaa, 'a', 1000);
131                 for (i = 0; i < 1000; i++)
132                         gcry_md_write(hd, aaa, 1000);
133         } else {
134                 gcry_md_write(hd, data, data_len);
135         }
136
137         err = gcry_md_copy(&hd2, hd);
138         if (err) {
139                 _DEBUG_TRACE("algo %d, gcry_md_copy failed: %s\n", algo, gpg_strerror(err));
140         }
141
142         gcry_md_close(hd);
143
144         p = gcry_md_read(hd2, algo);
145         if (p != NULL) {
146                 crypt_data = (unsigned char *)strdup((const char *)p);
147         }
148
149         gcry_md_close(hd2);
150
151         _INNER_FUNC_EXIT;
152
153         return crypt_data;
154 }
155
156 static unsigned char *_crypt_symmetric_cipher_function(const char *plain, int plain_len, const char *key, int key_len, int flags)
157 {
158         _INNER_FUNC_ENTER;
159
160         _INNER_FUNC_EXIT;
161
162         return NULL;
163 }