summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib/emile/emile_cipher.c9
-rw-r--r--src/lib/emile/emile_cipher.h6
-rw-r--r--src/lib/emile/emile_cipher_gnutls.c57
-rw-r--r--src/lib/emile/emile_cipher_openssl.c58
-rw-r--r--src/lib/emile/emile_main.c60
-rw-r--r--src/lib/emile/emile_private.h9
6 files changed, 101 insertions, 98 deletions
diff --git a/src/lib/emile/emile_cipher.c b/src/lib/emile/emile_cipher.c
index 3c5fd185cc..544f271400 100644
--- a/src/lib/emile/emile_cipher.c
+++ b/src/lib/emile/emile_cipher.c
@@ -13,6 +13,15 @@ Eina_Bool _emile_cipher_init(void)
13 return EINA_FALSE; 13 return EINA_FALSE;
14} 14}
15 15
16EAPI Eina_Bool
17emile_binbuf_sha1(const char *key EINA_UNUSED,
18 int key_len EINA_UNUSED,
19 const Eina_Binbuf *data EINA_UNUSED,
20 unsigned char digest[20])
21{
22 return EINA_FALSE;
23}
24
16EAPI Eina_Binbuf * 25EAPI Eina_Binbuf *
17emile_binbuf_cipher(const Eina_Binbuf *data EINA_UNUSED, 26emile_binbuf_cipher(const Eina_Binbuf *data EINA_UNUSED,
18 const char *key EINA_UNUSED, 27 const char *key EINA_UNUSED,
diff --git a/src/lib/emile/emile_cipher.h b/src/lib/emile/emile_cipher.h
index 80097545ed..69379d8721 100644
--- a/src/lib/emile/emile_cipher.h
+++ b/src/lib/emile/emile_cipher.h
@@ -26,6 +26,12 @@ EAPI Eina_Binbuf *emile_binbuf_cipher(const Eina_Binbuf *in,
26EAPI Eina_Binbuf *emile_binbuf_decipher(const Eina_Binbuf *in, 26EAPI Eina_Binbuf *emile_binbuf_decipher(const Eina_Binbuf *in,
27 const char *key, unsigned int length); 27 const char *key, unsigned int length);
28 28
29EAPI Eina_Bool emile_binbuf_sha1(const char *key,
30 unsigned int key_len,
31 const Eina_Binbuf *data,
32 unsigned char digest[20]);
33
34
29EAPI Emile_SSL *emile_cipher_server_listen(Emile_Cipher_Type t); 35EAPI Emile_SSL *emile_cipher_server_listen(Emile_Cipher_Type t);
30EAPI Emile_SSL *emile_cipher_client_connect(Emile_SSL *server, int fd); 36EAPI Emile_SSL *emile_cipher_client_connect(Emile_SSL *server, int fd);
31EAPI Emile_SSL *emile_cipher_server_connect(Emile_Cipher_Type t); 37EAPI Emile_SSL *emile_cipher_server_connect(Emile_Cipher_Type t);
diff --git a/src/lib/emile/emile_cipher_gnutls.c b/src/lib/emile/emile_cipher_gnutls.c
index 998ce886ca..d51ae8bb9a 100644
--- a/src/lib/emile/emile_cipher_gnutls.c
+++ b/src/lib/emile/emile_cipher_gnutls.c
@@ -170,56 +170,15 @@ emile_hmac_sha1(const void *key,
170 return EINA_TRUE; 170 return EINA_TRUE;
171} 171}
172 172
173static Eina_Bool 173EAPI Eina_Bool
174emile_pbkdf2_sha1(const char *key, 174emile_binbuf_sha1(const char *key,
175 int key_len, 175 unsigned int key_len,
176 const unsigned char *salt, 176 const Eina_Binbuf *data,
177 unsigned int salt_len, 177 unsigned char digest[20])
178 int iter,
179 unsigned char *res,
180 int res_len)
181{ 178{
182 unsigned char digest[20]; 179 return emile_hmac_sha1(key, key_len,
183 unsigned char tab[4]; 180 eina_binbuf_string_get(data), eina_binbuf_length_get(data),
184 unsigned char *p = res; 181 digest);
185 unsigned char *buf;
186 unsigned int i;
187 int digest_len = 20;
188 int len = res_len;
189 int tmp_len;
190 int j, k;
191
192 buf = alloca(salt_len + 4);
193 if (!buf) return EINA_FALSE;
194
195 for (i = 1; len; len -= tmp_len, p += tmp_len, i++)
196 {
197 if (len > digest_len)
198 tmp_len = digest_len;
199 else
200 tmp_len = len;
201
202 tab[0] = (unsigned char)(i & 0xff000000) >> 24;
203 tab[1] = (unsigned char)(i & 0x00ff0000) >> 16;
204 tab[2] = (unsigned char)(i & 0x0000ff00) >> 8;
205 tab[3] = (unsigned char)(i & 0x000000ff) >> 0;
206
207 memcpy(buf, salt, salt_len);
208 memcpy(buf + salt_len, tab, 4);
209 if (!emile_hmac_sha1(key, key_len, buf, salt_len + 4, digest))
210 return EINA_FALSE;
211 memcpy(p, digest, tmp_len);
212
213 for (j = 1; j < iter; j++)
214 {
215 if (!emile_hmac_sha1(key, key_len, digest, 20, digest))
216 return EINA_FALSE;
217 for (k = 0; k < tmp_len; k++)
218 p[k] ^= digest[k];
219 }
220 }
221
222 return EINA_TRUE;
223} 182}
224 183
225EAPI Eina_Binbuf * 184EAPI Eina_Binbuf *
diff --git a/src/lib/emile/emile_cipher_openssl.c b/src/lib/emile/emile_cipher_openssl.c
index 18ce5c7510..46143d0b45 100644
--- a/src/lib/emile/emile_cipher_openssl.c
+++ b/src/lib/emile/emile_cipher_openssl.c
@@ -57,56 +57,16 @@ _emile_cipher_init(void)
57 return EINA_TRUE; 57 return EINA_TRUE;
58} 58}
59 59
60static Eina_Bool 60EAPI Eina_Bool
61emile_pbkdf2_sha1(const char *key, 61emile_binbuf_sha1(const char *key,
62 int key_len, 62 unsigned int key_len,
63 const unsigned char *salt, 63 const Eina_Binbuf *data,
64 unsigned int salt_len, 64 unsigned char digest[20])
65 int iter,
66 unsigned char *res,
67 int res_len)
68{ 65{
69 unsigned char digest[20]; 66 HMAC(EVP_sha1(),
70 unsigned char tab[4]; 67 key, key_len,
71 unsigned char *p = res; 68 eina_binbuf_string_get(data), eina_binbuf_length_get(data),
72 unsigned char *buf; 69 digest, NULL);
73 unsigned int i;
74 int digest_len = 20;
75 int len = res_len;
76 int tmp_len;
77 int j, k;
78 HMAC_CTX hctx;
79
80 buf = alloca(salt_len + 4);
81 if (!buf) return EINA_FALSE;
82
83 for (i = 1; len; len -= tmp_len, p += tmp_len, i++)
84 {
85 if (len > digest_len)
86 tmp_len = digest_len;
87 else
88 tmp_len = len;
89
90 tab[0] = (unsigned char)(i & 0xff000000) >> 24;
91 tab[1] = (unsigned char)(i & 0x00ff0000) >> 16;
92 tab[2] = (unsigned char)(i & 0x0000ff00) >> 8;
93 tab[3] = (unsigned char)(i & 0x000000ff) >> 0;
94
95 HMAC_Init(&hctx, key, key_len, EVP_sha1());
96 HMAC_Update(&hctx, salt, salt_len);
97 HMAC_Update(&hctx, tab, 4);
98 HMAC_Final(&hctx, digest, NULL);
99 memcpy(p, digest, tmp_len);
100
101 for (j = 1; j < iter; j++)
102 {
103 HMAC(EVP_sha1(), key, key_len, digest, 20, digest, NULL);
104 for (k = 0; k < tmp_len; k++)
105 p[k] ^= digest[k];
106 }
107 HMAC_cleanup(&hctx);
108 }
109
110 return EINA_TRUE; 70 return EINA_TRUE;
111} 71}
112 72
diff --git a/src/lib/emile/emile_main.c b/src/lib/emile/emile_main.c
index eb18a61de5..b8712fce45 100644
--- a/src/lib/emile/emile_main.c
+++ b/src/lib/emile/emile_main.c
@@ -104,3 +104,63 @@ emile_shutdown(void)
104 104
105 return _emile_init_count; 105 return _emile_init_count;
106} 106}
107
108/* For the moment, we have just one function shared accross both cipher
109 * backend, so here it is. */
110Eina_Bool
111emile_pbkdf2_sha1(const char *key,
112 unsigned int key_len,
113 const unsigned char *salt,
114 unsigned int salt_len,
115 unsigned int iter,
116 unsigned char *res,
117 unsigned int res_len)
118{
119 Eina_Binbuf *step1, *step2;
120 unsigned char *buf;
121 unsigned char *p = res;
122 unsigned char digest[20];
123 unsigned char tab[4];
124 unsigned int len = res_len;
125 unsigned int tmp_len;
126 unsigned int i, j, k;
127
128 buf = alloca(salt_len + 4);
129 if (!buf) return EINA_FALSE;
130
131 step1 = eina_binbuf_manage_read_only_new_length(buf, salt_len + 4);
132 if (!step1) return EINA_FALSE;
133 step2 = eina_binbuf_manage_read_only_new_length(digest, 20);
134 if (!step2) return EINA_FALSE;
135
136 for (i = 1; len; len -= tmp_len, p += tmp_len, i++)
137 {
138 tmp_len = (len > 20) ? 20 : len;
139
140 tab[0] = (unsigned char)(i & 0xff000000) >> 24;
141 tab[1] = (unsigned char)(i & 0x00ff0000) >> 16;
142 tab[2] = (unsigned char)(i & 0x0000ff00) >> 8;
143 tab[3] = (unsigned char)(i & 0x000000ff) >> 0;
144
145 memcpy(buf, salt, salt_len);
146 memcpy(buf + salt_len, tab, 4);
147
148 if (!emile_binbuf_sha1(key, key_len, step1, digest))
149 return EINA_FALSE;
150
151 memcpy(p, digest, tmp_len);
152
153 for (j = 1; j < iter; j++)
154 {
155 if (!emile_binbuf_sha1(key, key_len, step2, digest))
156 return EINA_FALSE;
157 for (k = 0; k < tmp_len; k++)
158 p[k] ^= digest[k];
159 }
160 }
161
162 eina_binbuf_free(step1);
163 eina_binbuf_free(step2);
164
165 return EINA_TRUE;
166}
diff --git a/src/lib/emile/emile_private.h b/src/lib/emile/emile_private.h
index f6a005d44a..cba9022394 100644
--- a/src/lib/emile/emile_private.h
+++ b/src/lib/emile/emile_private.h
@@ -34,4 +34,13 @@ typedef enum
34 34
35Eina_Bool _emile_cipher_init(void); 35Eina_Bool _emile_cipher_init(void);
36 36
37Eina_Bool
38emile_pbkdf2_sha1(const char *key,
39 unsigned int key_len,
40 const unsigned char *salt,
41 unsigned int salt_len,
42 unsigned int iter,
43 unsigned char *res,
44 unsigned int res_len);
45
37#endif /* EMILE_PRIVATE_H_ */ 46#endif /* EMILE_PRIVATE_H_ */