2012-04-19 20:58:29 +02:00
|
|
|
/*
|
|
|
|
* InspIRCd -- Internet Relay Chat Daemon
|
2007-07-16 17:30:04 +00:00
|
|
|
*
|
2020-01-11 22:02:47 +00:00
|
|
|
* Copyright (C) 2014 Daniel Vassdal <shutter@canternet.org>
|
2024-06-07 10:37:56 +01:00
|
|
|
* Copyright (C) 2013, 2021-2023 Sadie Powell <sadie@witchery.services>
|
2020-01-11 22:02:47 +00:00
|
|
|
* Copyright (C) 2012 Robby <robby@chatbelgie.be>
|
|
|
|
* Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
|
|
|
|
* Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
|
|
|
|
* Copyright (C) 2006 Craig Edwards <brain@inspircd.org>
|
2007-07-16 17:30:04 +00:00
|
|
|
*
|
2012-04-19 20:58:29 +02:00
|
|
|
* This file is part of InspIRCd. InspIRCd 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, version 2.
|
2007-07-16 17:30:04 +00:00
|
|
|
*
|
2012-04-19 20:58:29 +02:00
|
|
|
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-07-16 17:30:04 +00:00
|
|
|
*/
|
|
|
|
|
2012-04-19 20:58:29 +02:00
|
|
|
|
2013-04-02 20:12:15 +01:00
|
|
|
#pragma once
|
2007-07-16 17:30:04 +00:00
|
|
|
|
2023-09-03 18:40:03 +01:00
|
|
|
#include "stringutils.h"
|
|
|
|
|
2021-12-20 20:00:03 +00:00
|
|
|
class HashProvider
|
|
|
|
: public DataProvider
|
2007-07-16 17:30:04 +00:00
|
|
|
{
|
2022-01-25 13:59:42 +00:00
|
|
|
public:
|
2010-02-21 17:08:54 +00:00
|
|
|
const unsigned int out_size;
|
|
|
|
const unsigned int block_size;
|
2014-07-02 20:55:33 +02:00
|
|
|
HashProvider(Module* mod, const std::string& Name, unsigned int osiz = 0, unsigned int bsiz = 0)
|
2022-12-01 05:14:58 +00:00
|
|
|
: DataProvider(mod, "hash/" + Name)
|
|
|
|
, out_size(osiz)
|
|
|
|
, block_size(bsiz)
|
2007-07-16 17:30:04 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-07-02 20:55:33 +02:00
|
|
|
virtual std::string GenerateRaw(const std::string& data) = 0;
|
|
|
|
|
|
|
|
virtual std::string ToPrintable(const std::string& raw)
|
|
|
|
{
|
2021-05-10 16:47:35 +01:00
|
|
|
return Hex::Encode(raw);
|
2014-07-02 20:55:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Compare(const std::string& input, const std::string& hash)
|
|
|
|
{
|
|
|
|
return InspIRCd::TimingSafeCompare(Generate(input), hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Generate(const std::string& data)
|
2010-02-21 17:08:45 +00:00
|
|
|
{
|
2014-07-02 20:55:33 +02:00
|
|
|
return ToPrintable(GenerateRaw(data));
|
2010-02-21 17:08:45 +00:00
|
|
|
}
|
|
|
|
|
2010-02-21 17:08:54 +00:00
|
|
|
/** HMAC algorithm, RFC 2104 */
|
|
|
|
std::string hmac(const std::string& key, const std::string& msg)
|
|
|
|
{
|
|
|
|
std::string hmac1, hmac2;
|
2014-07-02 20:55:33 +02:00
|
|
|
std::string kbuf = key.length() > block_size ? GenerateRaw(key) : key;
|
2010-02-21 17:08:54 +00:00
|
|
|
kbuf.resize(block_size);
|
|
|
|
|
|
|
|
for (size_t n = 0; n < block_size; n++)
|
|
|
|
{
|
|
|
|
hmac1.push_back(static_cast<char>(kbuf[n] ^ 0x5C));
|
|
|
|
hmac2.push_back(static_cast<char>(kbuf[n] ^ 0x36));
|
|
|
|
}
|
|
|
|
hmac2.append(msg);
|
2014-07-02 20:55:33 +02:00
|
|
|
hmac1.append(GenerateRaw(hmac2));
|
|
|
|
return GenerateRaw(hmac1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsKDF() const
|
|
|
|
{
|
|
|
|
return (!block_size);
|
2010-02-21 17:08:54 +00:00
|
|
|
}
|
2007-07-16 17:30:04 +00:00
|
|
|
};
|