summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2020-03-01 18:02:39 +0100
committerSébastien Helleu <flashcode@flashtux.org>2020-03-01 21:24:27 +0100
commit9a6a27ef58982319549529d43906da60ce199aaf (patch)
tree71693d78f414b8e0cac15c6284df5c4ee6229a81 /src/core
parentc4ef3d6c2e5339d3b6ac2ba255d166a4d8984bce (diff)
downloadweechat-9a6a27ef58982319549529d43906da60ce199aaf.zip
core: move crypto functions to wee-crypto.c, rename API function string_hash to crypto_hash
Diffstat (limited to 'src/core')
-rw-r--r--src/core/CMakeLists.txt1
-rw-r--r--src/core/Makefile.am2
-rw-r--r--src/core/wee-crypto.c402
-rw-r--r--src/core/wee-crypto.h39
-rw-r--r--src/core/wee-secure.c328
-rw-r--r--src/core/wee-secure.h13
-rw-r--r--src/core/wee-string.c64
-rw-r--r--src/core/wee-string.h2
8 files changed, 446 insertions, 405 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index 3bfab22db..578335cdd 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -28,6 +28,7 @@ set(LIB_CORE_SRC
wee-completion.c wee-completion.h
wee-config.c wee-config.h
wee-config-file.c wee-config-file.h
+ wee-crypto.c wee-crypto.h
wee-debug.c wee-debug.h
wee-eval.c wee-eval.h
wee-hashtable.c wee-hashtable.h
diff --git a/src/core/Makefile.am b/src/core/Makefile.am
index de3f628df..09cd709a9 100644
--- a/src/core/Makefile.am
+++ b/src/core/Makefile.am
@@ -37,6 +37,8 @@ lib_weechat_core_a_SOURCES = weechat.c \
wee-config.h \
wee-config-file.c \
wee-config-file.h \
+ wee-crypto.c \
+ wee-crypto.h \
wee-debug.c \
wee-debug.h \
wee-eval.c \
diff --git a/src/core/wee-crypto.c b/src/core/wee-crypto.c
new file mode 100644
index 000000000..248f73c1d
--- /dev/null
+++ b/src/core/wee-crypto.c
@@ -0,0 +1,402 @@
+/*
+ * wee-crypto.c - cryptographic functions
+ *
+ * Copyright (C) 2020 Sébastien Helleu <flashcode@flashtux.org>
+ *
+ * This file is part of WeeChat, the extensible chat client.
+ *
+ * WeeChat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * WeeChat is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with WeeChat. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <time.h>
+#include <math.h>
+#include <gcrypt.h>
+
+#include "weechat.h"
+#include "wee-crypto.h"
+#include "wee-config-file.h"
+#include "wee-hashtable.h"
+#include "wee-string.h"
+#include "../plugins/plugin.h"
+
+char *weecrypto_hash_algo_string[] = {
+ "crc32",
+ "md5",
+ "sha1",
+ "sha224", "sha256", "sha384", "sha512",
+ "sha3-224", "sha3-256", "sha3-384", "sha3-512",
+ NULL,
+};
+int weecrypto_hash_algo[] = {
+ GCRY_MD_CRC32,
+ GCRY_MD_MD5,
+ GCRY_MD_SHA1,
+ GCRY_MD_SHA224, GCRY_MD_SHA256, GCRY_MD_SHA384, GCRY_MD_SHA512,
+ GCRY_MD_SHA3_224, GCRY_MD_SHA3_256, GCRY_MD_SHA3_384, GCRY_MD_SHA3_512,
+};
+
+
+/*
+ * Returns the hash algorithm with the name, or GCRY_MD_NONE if not found.
+ */
+
+int
+weecrypto_get_hash_algo (const char *hash_algo)
+{
+ int i;
+
+ if (!hash_algo)
+ return GCRY_MD_NONE;
+
+ for (i = 0; weecrypto_hash_algo_string[i]; i++)
+ {
+ if (strcmp (weecrypto_hash_algo_string[i], hash_algo) == 0)
+ return weecrypto_hash_algo[i];
+ }
+
+ return GCRY_MD_NONE;
+}
+
+/*
+ * Computes hash of data using the given algorithm.
+ *
+ * The hash size depends on the algorithm, common ones are:
+ *
+ * GCRY_MD_CRC32 32 bits == 4 bytes
+ * GCRY_MD_MD5 128 bits == 16 bytes
+ * GCRY_MD_SHA1 160 bits == 20 bytes
+ * GCRY_MD_SHA224 224 bits == 28 bytes
+ * GCRY_MD_SHA256 256 bits == 32 bytes
+ * GCRY_MD_SHA384 384 bits == 48 bytes
+ * GCRY_MD_SHA512 512 bits == 64 bytes
+ * GCRY_MD_SHA3_224 224 bits == 28 bytes
+ * GCRY_MD_SHA3_256 256 bits == 32 bytes
+ * GCRY_MD_SHA3_384 384 bits == 48 bytes
+ * GCRY_MD_SHA3_512 512 bits == 64 bytes
+ *
+ * The result hash is stored in "hash" (the buffer must be large enough).
+ *
+ * If hash_size is not NULL, the length of hash is stored in *hash_size
+ * (in bytes).
+ *
+ * Returns 1 if OK, 0 if error.
+ */
+
+int
+weecrypto_hash (const void *data, int data_size, int hash_algo,
+ void *hash, int *hash_size)
+{
+ gcry_md_hd_t *hd_md;
+ int rc, hd_md_opened, algo_size;
+ unsigned char *ptr_hash;
+
+ rc = 0;
+ hd_md = NULL;
+ hd_md_opened = 0;
+
+ if (!hash)
+ goto hash_end;
+
+ if (hash_size)
+ *hash_size = 0;
+
+ if (!data || (data_size < 1))
+ goto hash_end;
+
+ hd_md = malloc (sizeof (gcry_md_hd_t));
+ if (!hd_md)
+ goto hash_end;
+
+ if (gcry_md_open (hd_md, hash_algo, 0) != 0)
+ goto hash_end;
+
+ hd_md_opened = 1;
+
+ gcry_md_write (*hd_md, data, data_size);
+ ptr_hash = gcry_md_read (*hd_md, hash_algo);
+ if (!ptr_hash)
+ goto hash_end;
+
+ algo_size = gcry_md_get_algo_dlen (hash_algo);
+ memcpy (hash, ptr_hash, algo_size);
+ if (hash_size)
+ *hash_size = algo_size;
+
+ rc = 1;
+
+hash_end:
+ if (hd_md)
+ {
+ if (hd_md_opened)
+ gcry_md_close (*hd_md);
+ free (hd_md);
+ }
+ return rc;
+}
+
+/*
+ * Computes PKCS#5 Passphrase Based Key Derivation Function number 2 (PBKDF2)
+ * hash of data.
+ *
+ * The hash size depends on the algorithm, common ones are:
+ *
+ * GCRY_MD_SHA1 160 bits == 20 bytes
+ * GCRY_MD_SHA256 256 bits == 32 bytes
+ * GCRY_MD_SHA512 512 bits == 64 bytes
+ *
+ * The result hash is stored in "hash" (the buffer must be large enough).
+ *
+ * If hash_size is not NULL, the length of hash is stored in *hash_size
+ * (in bytes).
+ *
+ * Returns 1 if OK, 0 if error.
+ */
+
+int
+weecrypto_hash_pbkdf2 (const void *data, int data_size, int hash_subalgo,
+ const void *salt, int salt_size, int iterations,
+ void *hash, int *hash_size)
+{
+ int rc, algo_size;
+
+ rc = 0;
+
+ if (!hash)
+ goto hash_pbkdf2_end;
+
+ if (hash_size)
+ *hash_size = 0;
+
+ if (!data || (data_size < 1) || !salt || (salt_size < 1)
+ || (iterations < 1))
+ {
+ goto hash_pbkdf2_end;
+ }
+
+ algo_size = gcry_md_get_algo_dlen (hash_subalgo);
+ if (gcry_kdf_derive (data, data_size, GCRY_KDF_PBKDF2, hash_subalgo,
+ salt, salt_size, iterations,
+ algo_size, hash) != 0)
+ {
+ goto hash_pbkdf2_end;
+ }
+
+ if (hash_size)
+ *hash_size = algo_size;
+
+ rc = 1;
+
+hash_pbkdf2_end:
+ return rc;
+}
+
+/*
+ * Generates a Time-based One-Time Password (TOTP), as described
+ * in the RFC 6238.
+ *
+ * Returns:
+ * 1: OK
+ * 0: error
+ */
+
+int
+weecrypto_totp_generate_internal (const char *secret, int length_secret,
+ uint64_t moving_factor, int digits,
+ char *result)
+{
+ gcry_md_hd_t hd_md;
+ uint64_t moving_factor_swapped;
+ unsigned char *ptr_hash;
+ char hash[20];
+ int offset, length;
+ unsigned long bin_code;
+
+ if (gcry_md_open (&hd_md, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC) != 0)
+ return 0;
+
+ if (gcry_md_setkey (hd_md, secret, length_secret) != 0)
+ {
+ gcry_md_close (hd_md);
+ return 0;
+ }
+
+ moving_factor_swapped = (moving_factor >> 56)
+ | ((moving_factor << 40) & 0x00FF000000000000)
+ | ((moving_factor << 24) & 0x0000FF0000000000)
+ | ((moving_factor << 8) & 0x000000FF00000000)
+ | ((moving_factor >> 8) & 0x00000000FF000000)
+ | ((moving_factor >> 24) & 0x0000000000FF0000)
+ | ((moving_factor >> 40) & 0x000000000000FF00)
+ | (moving_factor << 56);
+
+ gcry_md_write (hd_md,
+ &moving_factor_swapped, sizeof (moving_factor_swapped));
+
+ ptr_hash = gcry_md_read (hd_md, GCRY_MD_SHA1);
+ if (!ptr_hash)
+ {
+ gcry_md_close (hd_md);
+ return 0;
+ }
+
+ memcpy (hash, ptr_hash, sizeof (hash));
+
+ gcry_md_close (hd_md);
+
+ offset = hash[19] & 0xf;
+ bin_code = (hash[offset] & 0x7f) << 24
+ | (hash[offset+1] & 0xff) << 16
+ | (hash[offset+2] & 0xff) << 8
+ | (hash[offset+3] & 0xff);
+
+ bin_code %= (unsigned long)(pow (10, digits));
+
+ length = snprintf (result, digits + 1, "%.*lu", digits, bin_code);
+ if (length != digits)
+ return 0;
+
+ return 1;
+}
+
+/*
+ * Generates a Time-based One-Time Password (TOTP), as described
+ * in the RFC 6238.
+ *
+ * Returns the password as string, NULL if error.
+ *
+ * Note: result must be freed after use.
+ */
+
+char *
+weecrypto_totp_generate (const char *secret_base32, time_t totp_time,
+ int digits)
+{
+ char *result, *secret;
+ int length_secret, rc;
+ uint64_t moving_factor;
+
+ secret = NULL;
+ result = NULL;
+
+ if (!secret_base32 || !secret_base32[0]
+ || (digits < WEECRYPTO_TOTP_MIN_DIGITS)
+ || (digits > WEECRYPTO_TOTP_MAX_DIGITS))
+ {
+ goto error;
+ }
+
+ secret = malloc ((strlen (secret_base32) * 4) + 16 + 1);
+ if (!secret)
+ goto error;
+
+ length_secret = string_base32_decode (secret_base32, secret);
+ if (length_secret < 0)
+ goto error;
+
+ result = malloc (digits + 1);
+ if (!result)
+ goto error;
+
+ if (totp_time == 0)
+ totp_time = time (NULL);
+
+ moving_factor = totp_time / 30;
+
+ rc = weecrypto_totp_generate_internal (secret, length_secret,
+ moving_factor, digits, result);
+ if (!rc)
+ goto error;
+
+ free (secret);
+
+ return result;
+
+error:
+ if (secret)
+ free (secret);
+ if (result)
+ free (result);
+ return NULL;
+}
+
+/*
+ * Validates a Time-based One-Time Password (TOTP).
+ *
+ * Returns:
+ * 1: OTP is OK
+ * 0: OTP is invalid
+ */
+
+int
+weecrypto_totp_validate (const char *secret_base32, time_t totp_time,
+ int window, const char *otp)
+{
+ char *secret, str_otp[16];
+ int length_secret, digits, rc, otp_ok;
+ uint64_t i, moving_factor;
+
+ secret = NULL;
+
+ if (!secret_base32 || !secret_base32[0] || (window < 0) || !otp || !otp[0])
+ goto error;
+
+ digits = strlen (otp);
+ if ((digits < WEECRYPTO_TOTP_MIN_DIGITS)
+ || (digits > WEECRYPTO_TOTP_MAX_DIGITS))
+ {
+ goto error;
+ }
+
+ secret = malloc (strlen (secret_base32) + 1);
+ if (!secret)
+ goto error;
+
+ length_secret = string_base32_decode (secret_base32, secret);
+ if (length_secret < 0)
+ goto error;
+
+ if (totp_time == 0)
+ totp_time = time (NULL);
+
+ moving_factor = totp_time / 30;
+
+ otp_ok = 0;
+
+ for (i = moving_factor - window; i <= moving_factor + window; i++)
+ {
+ rc = weecrypto_totp_generate_internal (secret, length_secret,
+ i, digits, str_otp);
+ if (rc && (strcmp (str_otp, otp) == 0))
+ {
+ otp_ok = 1;
+ break;
+ }
+ }
+
+ free (secret);
+
+ return otp_ok;
+
+error:
+ if (secret)
+ free (secret);
+ return 0;
+}
diff --git a/src/core/wee-crypto.h b/src/core/wee-crypto.h
new file mode 100644
index 000000000..9902042af
--- /dev/null
+++ b/src/core/wee-crypto.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2020 Sébastien Helleu <flashcode@flashtux.org>
+ *
+ * This file is part of WeeChat, the extensible chat client.
+ *
+ * WeeChat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * WeeChat is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with WeeChat. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef WEECHAT_CRYPTO_H
+#define WEECHAT_CRYPTO_H
+
+#define WEECRYPTO_TOTP_MIN_DIGITS 4
+#define WEECRYPTO_TOTP_MAX_DIGITS 10
+
+extern int weecrypto_get_hash_algo (const char *hash_algo);
+extern int weecrypto_hash (const void *data, int data_size, int hash_algo,
+ void *hash, int *hash_size);
+extern int weecrypto_hash_pbkdf2 (const void *data, int data_size,
+ int hash_subalgo,
+ const void *salt, int salt_size,
+ int iterations,
+ void *hash, int *hash_size);
+extern char *weecrypto_totp_generate (const char *secret, time_t totp_time,
+ int digits);
+extern int weecrypto_totp_validate (const char *secret, time_t totp_time,
+ int window, const char *otp);
+
+#endif /* WEECHAT_CRYPTO_H */
diff --git a/src/core/wee-secure.c b/src/core/wee-secure.c
index 542938998..ce700986f 100644
--- a/src/core/wee-secure.c
+++ b/src/core/wee-secure.c
@@ -25,13 +25,11 @@
#include <stdlib.h>
#include <stdio.h>
-#include <stdint.h>
-#include <time.h>
-#include <math.h>
#include <gcrypt.h>
#include "weechat.h"
#include "wee-config-file.h"
+#include "wee-crypto.h"
#include "wee-hashtable.h"
#include "wee-secure.h"
#include "wee-secure-config.h"
@@ -66,139 +64,6 @@ int secure_data_encrypted = 0;
/*
- * Computes hash of data using the given algorithm.
- *
- * The hash size depends on the algorithm, common ones are:
- *
- * GCRY_MD_CRC32 32 bits == 4 bytes
- * GCRY_MD_MD5 128 bits == 16 bytes
- * GCRY_MD_SHA1 160 bits == 20 bytes
- * GCRY_MD_SHA224 224 bits == 28 bytes
- * GCRY_MD_SHA256 256 bits == 32 bytes
- * GCRY_MD_SHA384 384 bits == 48 bytes
- * GCRY_MD_SHA512 512 bits == 64 bytes
- * GCRY_MD_SHA3_224 224 bits == 28 bytes
- * GCRY_MD_SHA3_256 256 bits == 32 bytes
- * GCRY_MD_SHA3_384 384 bits == 48 bytes
- * GCRY_MD_SHA3_512 512 bits == 64 bytes
- *
- * The result hash is stored in "hash" (the buffer must be large enough).
- *
- * If hash_size is not NULL, the length of hash is stored in *hash_size
- * (in bytes).
- *
- * Returns 1 if OK, 0 if error.
- */
-
-int
-secure_hash (const void *data, int data_size, int hash_algo,
- void *hash, int *hash_size)
-{
- gcry_md_hd_t *hd_md;
- int rc, hd_md_opened, algo_size;
- unsigned char *ptr_hash;
-
- rc = 0;
- hd_md = NULL;
- hd_md_opened = 0;
-
- if (!hash)
- goto hash_end;
-
- if (hash_size)
- *hash_size = 0;
-
- if (!data || (data_size < 1))
- goto hash_end;
-
- hd_md = malloc (sizeof (gcry_md_hd_t));
- if (!hd_md)
- goto hash_end;
-
- if (gcry_md_open (hd_md, hash_algo, 0) != 0)
- goto hash_end;
-
- hd_md_opened = 1;
-
- gcry_md_write (*hd_md, data, data_size);
- ptr_hash = gcry_md_read (*hd_md, hash_algo);
- if (!ptr_hash)
- goto hash_end;
-
- algo_size = gcry_md_get_algo_dlen (hash_algo);
- memcpy (hash, ptr_hash, algo_size);
- if (hash_size)
- *hash_size = algo_size;
-
- rc = 1;
-
-hash_end:
- if (hd_md)
- {
- if (hd_md_opened)
- gcry_md_close (*hd_md);
- free (hd_md);
- }
- return rc;
-}
-
-/*
- * Computes PKCS#5 Passphrase Based Key Derivation Function number 2 (PBKDF2)
- * hash of data, as binary buffer.
- *
- * The hash size depends on the algorithm, common ones are:
- *
- * GCRY_MD_SHA1 160 bits == 20 bytes
- * GCRY_MD_SHA256 256 bits == 32 bytes
- * GCRY_MD_SHA512 512 bits == 64 bytes
- *
- * The result hash is stored in "hash" (the buffer must be large enough).
- *
- * If hash_size is not NULL, the length of hash is stored in *hash_size
- * (in bytes).
- *
- * Returns 1 if OK, 0 if error.
- */
-
-int
-secure_hash_pbkdf2 (const void *data, int data_size, int hash_subalgo,
- const void *salt, int salt_size, int iterations,
- void *hash, int *hash_size)
-{
- int rc, algo_size;
-
- rc = 0;
-
- if (!hash)
- goto hash_pbkdf2_end;
-
- if (hash_size)
- *hash_size = 0;
-
- if (!data || (data_size < 1) || !salt || (salt_size < 1)
- || (iterations < 1))
- {
- goto hash_pbkdf2_end;
- }
-
- algo_size = gcry_md_get_algo_dlen (hash_subalgo);
- if (gcry_kdf_derive (data, data_size, GCRY_KDF_PBKDF2, hash_subalgo,
- salt, salt_size, iterations,
- algo_size, hash) != 0)
- {
- goto hash_pbkdf2_end;
- }
-
- if (hash_size)
- *hash_size = algo_size;
-
- rc = 1;
-
-hash_pbkdf2_end:
- return rc;
-}
-
-/*
* Derives a key from salt + passphrase (using a hash).
*
* Returns:
@@ -228,7 +93,7 @@ secure_derive_key (const char *salt, const char *passphrase,
memcpy (buffer + SECURE_SALT_SIZE, passphrase, strlen (passphrase));
/* compute hash of buffer */
- if (!secure_hash (buffer, length, GCRY_MD_SHA512, hash, &length_hash))
+ if (!weecrypto_hash (buffer, length, GCRY_MD_SHA512, hash, &length_hash))
{
free (buffer);
return 0;
@@ -629,195 +494,6 @@ secure_decrypt_data_not_decrypted (const char *passphrase)
}
/*
- * Generates a Time-based One-Time Password (TOTP), as described
- * in the RFC 6238.
- *
- * Returns:
- * 1: OK
- * 0: error
- */
-
-int
-secure_totp_generate_internal (const char *secret, int length_secret,
- uint64_t moving_factor, int digits,
- char *result)
-{
- gcry_md_hd_t hd_md;
- uint64_t moving_factor_swapped;
- unsigned char *ptr_hash;
- char hash[20];
- int offset, length;
- unsigned long bin_code;
-
- if (gcry_md_open (&hd_md, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC) != 0)
- return 0;
-
- if (gcry_md_setkey (hd_md, secret, length_secret) != 0)
- {
- gcry_md_close (hd_md);
- return 0;
- }
-
- moving_factor_swapped = (moving_factor >> 56)
- | ((moving_factor << 40) & 0x00FF000000000000)
- | ((moving_factor << 24) & 0x0000FF0000000000)
- | ((moving_factor << 8) & 0x000000FF00000000)
- | ((moving_factor >> 8) & 0x00000000FF000000)
- | ((moving_factor >> 24) & 0x0000000000FF0000)
- | ((moving_factor >> 40) & 0x000000000000FF00)
- | (moving_factor << 56);
-
- gcry_md_write (hd_md,
- &moving_factor_swapped, sizeof (moving_factor_swapped));
-
- ptr_hash = gcry_md_read (hd_md, GCRY_MD_SHA1);
- if (!ptr_hash)
- {
- gcry_md_close (hd_md);
- return 0;
- }
-
- memcpy (hash, ptr_hash, sizeof (hash));
-
- gcry_md_close (hd_md);
-
- offset = hash[19] & 0xf;
- bin_code = (hash[offset] & 0x7f) << 24
- | (hash[offset+1] & 0xff) << 16
- | (hash[offset+2] & 0xff) << 8
- | (hash[offset+3] & 0xff);
-
- bin_code %= (unsigned long)(pow (10, digits));
-
- length = snprintf (result, digits + 1, "%.*lu", digits, bin_code);
- if (length != digits)
- return 0;
-
- return 1;
-}
-
-/*
- * Generates a Time-based One-Time Password (TOTP), as described
- * in the RFC 6238.
- *
- * Returns the password as string, NULL if error.
- *
- * Note: result must be freed after use.
- */
-
-char *
-secure_totp_generate (const char *secret_base32, time_t totp_time, int digits)
-{
- char *result, *secret;
- int length_secret, rc;
- uint64_t moving_factor;
-
- secret = NULL;
- result = NULL;
-
- if (!secret_base32 || !secret_base32[0]
- || (digits < SECURE_TOTP_MIN_DIGITS)
- || (digits > SECURE_TOTP_MAX_DIGITS))
- {
- goto error;
- }
-
- secret = malloc ((strlen (secret_base32) * 4) + 16 + 1);
- if (!secret)
- goto error;
-
- length_secret = string_base32_decode (secret_base32, secret);
- if (length_secret < 0)
- goto error;
-
- result = malloc (digits + 1);
- if (!result)
- goto error;
-
- if (totp_time == 0)
- totp_time = time (NULL);
-
- moving_factor = totp_time / 30;
-
- rc = secure_totp_generate_internal (secret, length_secret,
- moving_factor, digits, result);
- if (!rc)
- goto error;
-
- free (secret);
-
- return result;
-
-error:
- if (secret)
- free (secret);
- if (result)
- free (result);
- return NULL;
-}
-
-/*
- * Validates a Time-based One-Time Password (TOTP).
- *
- * Returns:
- * 1: OTP is OK
- * 0: OTP is invalid
- */
-
-int
-secure_totp_validate (const char *secret_base32, time_t totp_time, int window,
- const char *otp)
-{
- char *secret, str_otp[16];
- int length_secret, digits, rc, otp_ok;
- uint64_t i, moving_factor;
-
- secret = NULL;
-
- if (!secret_base32 || !secret_base32[0] || (window < 0) || !otp || !otp[0])
- goto error;
-
- digits = strlen (otp);
- if ((digits < SECURE_TOTP_MIN_DIGITS) || (digits > SECURE_TOTP_MAX_DIGITS))
- goto error;
-
- secret = malloc (strlen (secret_base32) + 1);
- if (!secret)
- goto error;
-
- length_secret = string_base32_decode (secret_base32, secret);
- if (length_secret < 0)
- goto error;
-
- if (totp_time == 0)
- totp_time = time (NULL);
-
- moving_factor = totp_time / 30;
-
- otp_ok = 0;
-
- for (i = moving_factor - window; i <= moving_factor + window; i++)
- {
- rc = secure_totp_generate_internal (secret, length_secret,
- i, digits, str_otp);
- if (rc && (strcmp (str_otp, otp) == 0))
- {
- otp_ok = 1;
- break;
- }
- }
-
- free (secret);
-
- return otp_ok;
-
-error:
- if (secret)
- free (secret);
- return 0;
-}
-
-/*
* Initializes secured data.
*
* Returns:
diff --git a/src/core/wee-secure.h b/src/core/wee-secure.h
index fc9bd3a2c..41a142d2d 100644
--- a/src/core/wee-secure.h
+++ b/src/core/wee-secure.h
@@ -27,8 +27,6 @@
#define SECURE_SALT_DEFAULT "WeeChat!"
#define SECURE_DATA_PASSPHRASE_FLAG "__passphrase__"
#define SECURE_SALT_SIZE 8
-#define SECURE_TOTP_MIN_DIGITS 4
-#define SECURE_TOTP_MAX_DIGITS 10
enum t_secure_config_hash_algo
{
@@ -55,13 +53,6 @@ extern int secure_cipher[];
extern int secure_data_encrypted;
extern char *secure_decrypt_error[];
-extern int secure_hash (const void *data, int data_size, int hash_algo,
- void *hash, int *hash_size);
-extern int secure_hash_pbkdf2 (const void *data, int data_size,
- int hash_subalgo,
- const void *salt, int salt_size,
- int iterations,
- void *hash, int *hash_size);
extern int secure_encrypt_data (const char *data, int length_data,
int hash_algo, int cipher,
const char *passphrase, char **encrypted,
@@ -71,10 +62,6 @@ extern int secure_decrypt_data (const char *buffer, int length_buffer,
const char *passphrase, char **decrypted,
int *length_decrypted);
extern int secure_decrypt_data_not_decrypted (const char *passphrase);
-extern char *secure_totp_generate (const char *secret, time_t totp_time,
- int digits);
-extern int secure_totp_validate (const char *secret, time_t totp_time,
- int window, const char *otp);
extern int secure_init ();
extern void secure_end ();
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index 67164870a..783bd63e3 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -53,7 +53,6 @@
#include "wee-config.h"
#include "wee-eval.h"
#include "wee-hashtable.h"
-#include "wee-secure.h"
#include "wee-utf8.h"
#include "../gui/gui-chat.h"
#include "../gui/gui-color.h"
@@ -65,22 +64,6 @@
((c >= 'A') && (c <= 'F')) ? c - 'A' + 10 : \
c - '0')
-char *string_hash_algo_string[] = {
- "crc32",
- "md5",
- "sha1",
- "sha224", "sha256", "sha384", "sha512",
- "sha3-224", "sha3-256", "sha3-384", "sha3-512",
- NULL,
-};
-int string_hash_algo[] = {
- GCRY_MD_CRC32,
- GCRY_MD_MD5,
- GCRY_MD_SHA1,
- GCRY_MD_SHA224, GCRY_MD_SHA256, GCRY_MD_SHA384, GCRY_MD_SHA512,
- GCRY_MD_SHA3_224, GCRY_MD_SHA3_256, GCRY_MD_SHA3_384, GCRY_MD_SHA3_512,
-};
-
struct t_hashtable *string_hashtable_shared = NULL;
@@ -3423,53 +3406,6 @@ end:
}
/*
- * Returns the hash algorithm with the name, or GCRY_MD_NONE if not found.
- */
-
-int
-string_get_hash_algo (const char *hash_algo)
-{
- int i;
-
- if (!hash_algo)
- return GCRY_MD_NONE;
-
- for (i = 0; string_hash_algo_string[i]; i++)
- {
- if (strcmp (string_hash_algo_string[i], hash_algo) == 0)
- return string_hash_algo[i];
- }
-
- return GCRY_MD_NONE;
-}
-
-/*
- * Computes hash data.
- */
-
-int
-string_hash (const void *data, int data_size, const char *hash_algo,
- void *hash, int *hash_size)
-{
- int algo;
-
- if (!hash)
- return 0;
-
- if (hash_size)
- *hash_size = 0;
-
- if (!data || (data_size < 1) || !hash_algo)
- return 0;
-
- algo = string_get_hash_algo (hash_algo);
- if (algo == GCRY_MD_NONE)
- return 0;
-
- return secure_hash (data, data_size, algo, hash, hash_size);
-}
-
-/*
* Checks if a string is a command.
*
* Returns:
diff --git a/src/core/wee-string.h b/src/core/wee-string.h
index 079b9d276..585cb399d 100644
--- a/src/core/wee-string.h
+++ b/src/core/wee-string.h
@@ -118,8 +118,6 @@ extern int string_base64_decode (const char *from, char *to);
extern char *string_hex_dump (const char *data, int data_size,
int bytes_per_line,
const char *prefix, const char *suffix);
-extern int string_hash (const void *data, int data_size,
- const char *hash_algo, void *hash, int *hash_size);
extern int string_is_command_char (const char *string);
extern const char *string_input_for_buffer (const char *string);
extern char *string_replace_with_callback (const char *string,