summaryrefslogtreecommitdiff
path: root/src/lib/emile
diff options
context:
space:
mode:
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>2016-08-29 15:59:43 -0300
committerGustavo Sverzut Barbieri <barbieri@profusion.mobi>2016-08-30 00:35:17 -0300
commitd8caade348b0aa5c43d048934c13565da9519a6c (patch)
tree9072a4603e2a4408aa87667af0bd22cb22607b40 /src/lib/emile
parent5b8315aeffb6a568d0b70441a2d05f6de2bf8fe1 (diff)
emile: API break, rename sha1 to hmac_sha1, introduce real sha1.
emile_binbuf_sha1() was actually doing HMAC version using the given key. This doesn't work when all you need is just the SHA1 of the input data. Then rename emile_binbuf_sha1() to emile_binbuf_hmac_sha1() and introduce a new version without key/keylen. This API was marked as BETA and no real users in the codebase, then it shouldn't cause us problems.
Diffstat (limited to 'src/lib/emile')
-rw-r--r--src/lib/emile/emile_cipher.c14
-rw-r--r--src/lib/emile/emile_cipher.h4
-rw-r--r--src/lib/emile/emile_cipher_gnutls.c45
-rw-r--r--src/lib/emile/emile_cipher_openssl.c25
-rw-r--r--src/lib/emile/emile_main.c4
5 files changed, 77 insertions, 15 deletions
diff --git a/src/lib/emile/emile_cipher.c b/src/lib/emile/emile_cipher.c
index 4562c016b2..0f09afef71 100644
--- a/src/lib/emile/emile_cipher.c
+++ b/src/lib/emile/emile_cipher.c
@@ -14,10 +14,16 @@ Eina_Bool _emile_cipher_init(void)
14} 14}
15 15
16EAPI Eina_Bool 16EAPI Eina_Bool
17emile_binbuf_sha1(const char *key EINA_UNUSED, 17emile_binbuf_hmac_sha1(const char *key EINA_UNUSED,
18 unsigned int key_len EINA_UNUSED, 18 unsigned int key_len EINA_UNUSED,
19 const Eina_Binbuf *data EINA_UNUSED, 19 const Eina_Binbuf *data EINA_UNUSED,
20 unsigned char digest[20] EINA_UNUSED) 20 unsigned char digest[20] EINA_UNUSED)
21{
22 return EINA_FALSE;
23}
24
25EAPI Eina_Bool
26emile_binbuf_sha1(const Eina_Binbuf * data, unsigned char digest[20])
21{ 27{
22 return EINA_FALSE; 28 return EINA_FALSE;
23} 29}
diff --git a/src/lib/emile/emile_cipher.h b/src/lib/emile/emile_cipher.h
index 9d82d168bd..3849c549c9 100644
--- a/src/lib/emile/emile_cipher.h
+++ b/src/lib/emile/emile_cipher.h
@@ -102,7 +102,9 @@ typedef enum
102 EMILE_WANT_WRITE = 3 102 EMILE_WANT_WRITE = 3
103} Emile_Want_Type; 103} Emile_Want_Type;
104 104
105EAPI Eina_Bool emile_binbuf_sha1(const char *key, unsigned int key_len, const Eina_Binbuf * data, unsigned char digest[20]); 105EAPI Eina_Bool emile_binbuf_hmac_sha1(const char *key, unsigned int key_len, const Eina_Binbuf * data, unsigned char digest[20]);
106
107EAPI Eina_Bool emile_binbuf_sha1(const Eina_Binbuf * data, unsigned char digest[20]);
106 108
107 109
108EAPI Emile_SSL *emile_cipher_server_listen(Emile_Cipher_Type t); 110EAPI Emile_SSL *emile_cipher_server_listen(Emile_Cipher_Type t);
diff --git a/src/lib/emile/emile_cipher_gnutls.c b/src/lib/emile/emile_cipher_gnutls.c
index e703793a09..cb6492a215 100644
--- a/src/lib/emile/emile_cipher_gnutls.c
+++ b/src/lib/emile/emile_cipher_gnutls.c
@@ -125,16 +125,53 @@ emile_hmac_sha1(const void *key,
125} 125}
126 126
127EAPI Eina_Bool 127EAPI Eina_Bool
128emile_binbuf_sha1(const char *key, 128emile_binbuf_hmac_sha1(const char *key,
129 unsigned int key_len, 129 unsigned int key_len,
130 const Eina_Binbuf *data, 130 const Eina_Binbuf *data,
131 unsigned char digest[20]) 131 unsigned char digest[20])
132{ 132{
133 return emile_hmac_sha1(key, key_len, 133 return emile_hmac_sha1(key, key_len,
134 eina_binbuf_string_get(data), eina_binbuf_length_get(data), 134 eina_binbuf_string_get(data), eina_binbuf_length_get(data),
135 digest); 135 digest);
136} 136}
137 137
138static inline Eina_Bool
139emile_sha1(const void *data,
140 size_t data_len,
141 unsigned char *res)
142{
143 size_t hlen = gcry_md_get_algo_dlen(GCRY_MD_SHA1);
144 gcry_md_hd_t mdh;
145 unsigned char *hash;
146 gpg_error_t err;
147
148 err = gcry_md_open(&mdh, GCRY_MD_SHA1, 0);
149 if (err != GPG_ERR_NO_ERROR)
150 return EINA_FALSE;
151
152 gcry_md_write(mdh, data, data_len);
153
154 hash = gcry_md_read(mdh, GCRY_MD_SHA1);
155 if (!hash)
156 {
157 gcry_md_close(mdh);
158 return EINA_FALSE;
159 }
160
161 memcpy(res, hash, hlen);
162
163 gcry_md_close(mdh);
164
165 return EINA_TRUE;
166}
167
168EAPI Eina_Bool
169emile_binbuf_sha1(const Eina_Binbuf * data, unsigned char digest[20])
170{
171 Eina_Slice slice = eina_binbuf_slice_get(data);
172 return emile_sha1(data.mem, data.len, digest);
173}
174
138EAPI Eina_Binbuf * 175EAPI Eina_Binbuf *
139emile_binbuf_cipher(Emile_Cipher_Algorithm algo, 176emile_binbuf_cipher(Emile_Cipher_Algorithm algo,
140 const Eina_Binbuf *data, 177 const Eina_Binbuf *data,
diff --git a/src/lib/emile/emile_cipher_openssl.c b/src/lib/emile/emile_cipher_openssl.c
index 2bbe83fb50..84048ce13e 100644
--- a/src/lib/emile/emile_cipher_openssl.c
+++ b/src/lib/emile/emile_cipher_openssl.c
@@ -58,10 +58,10 @@ _emile_cipher_init(void)
58} 58}
59 59
60EAPI Eina_Bool 60EAPI Eina_Bool
61emile_binbuf_sha1(const char *key, 61emile_binbuf_hmac_sha1(const char *key,
62 unsigned int key_len, 62 unsigned int key_len,
63 const Eina_Binbuf *data, 63 const Eina_Binbuf *data,
64 unsigned char digest[20]) 64 unsigned char digest[20])
65{ 65{
66 HMAC(EVP_sha1(), 66 HMAC(EVP_sha1(),
67 key, key_len, 67 key, key_len,
@@ -70,6 +70,23 @@ emile_binbuf_sha1(const char *key,
70 return EINA_TRUE; 70 return EINA_TRUE;
71} 71}
72 72
73EAPI Eina_Bool
74emile_binbuf_sha1(const Eina_Binbuf * data, unsigned char digest[20])
75{
76 const EVP_MD *md = EVP_sha1();
77 Eina_Slice slice = eina_binbuf_slice_get(data);
78 EVP_MD_CTX ctx;
79
80 EVP_MD_CTX_init(&ctx);
81 EVP_DigestInit_ex(&ctx, md, NULL);
82
83 EVP_DigestUpdate(&ctx, slice.mem, slice.len);
84 EVP_DigestFinal_ex(&ctx, digest, NULL);
85
86 EVP_MD_CTX_cleanup(&ctx);
87 return EINA_TRUE;
88}
89
73EAPI Eina_Binbuf * 90EAPI Eina_Binbuf *
74emile_binbuf_cipher(Emile_Cipher_Algorithm algo, 91emile_binbuf_cipher(Emile_Cipher_Algorithm algo,
75 const Eina_Binbuf *data, 92 const Eina_Binbuf *data,
diff --git a/src/lib/emile/emile_main.c b/src/lib/emile/emile_main.c
index d55782e17a..c466776cc3 100644
--- a/src/lib/emile/emile_main.c
+++ b/src/lib/emile/emile_main.c
@@ -154,14 +154,14 @@ emile_pbkdf2_sha1(const char *key, unsigned int key_len, const unsigned char *sa
154 memcpy(buf, salt, salt_len); 154 memcpy(buf, salt, salt_len);
155 memcpy(buf + salt_len, tab, 4); 155 memcpy(buf + salt_len, tab, 4);
156 156
157 if (!emile_binbuf_sha1(key, key_len, step1, digest)) 157 if (!emile_binbuf_hmac_sha1(key, key_len, step1, digest))
158 return EINA_FALSE; 158 return EINA_FALSE;
159 159
160 memcpy(p, digest, tmp_len); 160 memcpy(p, digest, tmp_len);
161 161
162 for (j = 1; j < iter; j++) 162 for (j = 1; j < iter; j++)
163 { 163 {
164 if (!emile_binbuf_sha1(key, key_len, step2, digest)) 164 if (!emile_binbuf_hmac_sha1(key, key_len, step2, digest))
165 return EINA_FALSE; 165 return EINA_FALSE;
166 for (k = 0; k < tmp_len; k++) 166 for (k = 0; k < tmp_len; k++)
167 p[k] ^= digest[k]; 167 p[k] ^= digest[k];