summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2020-03-01 14:26:24 +0100
committerSébastien Helleu <flashcode@flashtux.org>2020-03-01 14:26:24 +0100
commit1ae25914588221ece76da2d39ddece16de0c7712 (patch)
tree4cd093d55316c35dd95c56ba8402e9d9923b15a7 /src/core
parent3472793d24c4e4158d9d274d46eab9fff9e647ea (diff)
downloadweechat-1ae25914588221ece76da2d39ddece16de0c7712.zip
core: add function secure_hash_pbkdf2
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-secure.c54
-rw-r--r--src/core/wee-secure.h5
2 files changed, 59 insertions, 0 deletions
diff --git a/src/core/wee-secure.c b/src/core/wee-secure.c
index 97a007631..4a6b918aa 100644
--- a/src/core/wee-secure.c
+++ b/src/core/wee-secure.c
@@ -164,6 +164,60 @@ hash_end:
}
/*
+ * Computes PKCS#5 Passphrase Based Key Derivation Function number 2 (PBKDF2)
+ * hash of data, as binary buffer.
+ *
+ * Returns 1 if OK, 0 if error.
+ *
+ * Note: if OK, "*hash" must be freed after use.
+ */
+
+int
+secure_hash_pbkdf2 (const char *data, int length_data, int hash_subalgo,
+ const char *salt, int length_salt, int iterations,
+ char **hash, int *length_hash)
+{
+ int rc;
+
+ rc = 0;
+
+ if (!hash || !length_hash)
+ goto hash_pbkdf2_end;
+
+ *hash = NULL;
+ *length_hash = 0;
+
+ if (!data || (length_data < 1) || !salt || (length_salt < 1)
+ || (iterations < 1))
+ {
+ goto hash_pbkdf2_end;
+ }
+
+ *length_hash = gcry_md_get_algo_dlen (hash_subalgo);
+ *hash = malloc (*length_hash);
+ if (!*hash)
+ {
+ *length_hash = 0;
+ goto hash_pbkdf2_end;
+ }
+
+ if (gcry_kdf_derive (data, length_data, GCRY_KDF_PBKDF2, hash_subalgo,
+ salt, length_salt, iterations,
+ *length_hash, *hash) != 0)
+ {
+ free (*hash);
+ *hash = NULL;
+ *length_hash = 0;
+ goto hash_pbkdf2_end;
+ }
+
+ rc = 1;
+
+hash_pbkdf2_end:
+ return rc;
+}
+
+/*
* Derives a key from salt + passphrase (using a hash).
*
* Returns:
diff --git a/src/core/wee-secure.h b/src/core/wee-secure.h
index 303e0c9e5..ff468b3f7 100644
--- a/src/core/wee-secure.h
+++ b/src/core/wee-secure.h
@@ -58,6 +58,11 @@ extern char *secure_decrypt_error[];
extern void secure_hash_binary (const char *data, int length_data,
int hash_algo, char **hash, int *length_hash);
extern char *secure_hash (const char *data, int length_data, int hash_algo);
+extern int secure_hash_pbkdf2 (const char *data, int length_data,
+ int hash_subalgo,
+ const char *salt, int length_salt,
+ int iterations,
+ char **hash, int *length_hash);
extern int secure_encrypt_data (const char *data, int length_data,
int hash_algo, int cipher,
const char *passphrase, char **encrypted,