2022-05-22 00:23:10 +02:00
|
|
|
#ifndef GCRYPT_UTIL_H
|
|
|
|
|
#define GCRYPT_UTIL_H
|
|
|
|
|
|
2022-05-13 11:37:08 +02:00
|
|
|
#include <bitset>
|
|
|
|
|
#include <sstream>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <cstring>
|
2022-05-16 22:35:28 +02:00
|
|
|
#include "GCrypt/SecureBitset.h"
|
|
|
|
|
#include "GCrypt/Block.h"
|
|
|
|
|
#include "GCrypt/Flexblock.h"
|
|
|
|
|
#include "GCrypt/Config.h"
|
2022-05-22 12:51:58 +02:00
|
|
|
#include "GCrypt/GCipher.h"
|
2022-05-16 22:35:28 +02:00
|
|
|
#include "GCrypt/InitializationVector.h"
|
2022-05-13 11:37:08 +02:00
|
|
|
|
2022-05-16 22:15:34 +02:00
|
|
|
namespace Leonetienne::GCrypt {
|
2022-05-13 11:37:08 +02:00
|
|
|
//! Mod-operator that works with negative values
|
2022-05-16 22:01:52 +02:00
|
|
|
inline int Mod(const int numerator, const int denominator) {
|
2022-05-13 11:37:08 +02:00
|
|
|
return (denominator + (numerator % denominator)) % denominator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! Will perform a wrapping left-bitshift on a bitset
|
|
|
|
|
template <std::size_t T>
|
2022-05-16 22:01:52 +02:00
|
|
|
inline SecureBitset<T> Shiftl(const SecureBitset<T>& bits, const std::size_t amount) {
|
2022-05-13 11:37:08 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
|
const std::string bitss = bits.to_string();
|
|
|
|
|
|
2022-05-16 22:01:52 +02:00
|
|
|
for (std::size_t i = 0; i < bitss.size(); i++) {
|
2022-05-13 11:37:08 +02:00
|
|
|
ss << bitss[Mod((int)(i + amount), (int)bitss.size())];
|
2022-05-16 22:01:52 +02:00
|
|
|
}
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
return SecureBitset<T>(ss.str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! Will perform a wrapping right-bitshift on a bitset
|
|
|
|
|
template <std::size_t T>
|
2022-05-16 22:01:52 +02:00
|
|
|
inline SecureBitset<T> Shiftr(const SecureBitset<T>& bits, const std::size_t amount) {
|
2022-05-13 11:37:08 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
|
const std::string bitss = bits.to_string();
|
|
|
|
|
|
2022-05-16 22:01:52 +02:00
|
|
|
for (std::size_t i = 0; i < bitss.size(); i++) {
|
2022-05-13 11:37:08 +02:00
|
|
|
ss << bitss[Mod((i - amount), bitss.size())];
|
2022-05-16 22:01:52 +02:00
|
|
|
}
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
return SecureBitset<T>(ss.str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//! Will pad a string to a set length with a certain character
|
2022-05-22 14:13:10 +02:00
|
|
|
std::string PadStringToLength(const std::string& str, const std::size_t len, const char pad, const bool padLeft = true);
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
//! Will convert a string to a fixed-size data block
|
2022-05-22 13:54:03 +02:00
|
|
|
//! @s: The string to pad
|
|
|
|
|
//! padLeft: should padding be added to the left? If not, to the right.
|
2022-05-22 14:13:10 +02:00
|
|
|
Block StringToBitblock(const std::string& s, bool padLeft = true);
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
//! Will convert a string to a flexible data block
|
2022-05-22 14:13:10 +02:00
|
|
|
Flexblock StringToBits(const std::string& s);
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
//! Will convert a fixed-size data block to a bytestring
|
2022-05-22 14:13:10 +02:00
|
|
|
std::string BitblockToBytes(const Block& bits);
|
2022-05-16 22:01:52 +02:00
|
|
|
|
2022-05-13 11:37:08 +02:00
|
|
|
//! Will convert a fixed-size data block to a string
|
|
|
|
|
//! The difference to BitblockToBytes() is, that it strips excess nullbytes
|
2022-05-22 14:13:10 +02:00
|
|
|
std::string BitblockToString(const Block& bits);
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
//! Will convert a flexible data block to a bytestring
|
2022-05-22 14:13:10 +02:00
|
|
|
std::string BitsToBytes(const Flexblock& bits);
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
//! Will convert a flexible data block to a string
|
2022-05-16 22:01:52 +02:00
|
|
|
//! The difference to BitsToBytes() is, that it strips excess nullbytes
|
2022-05-22 14:13:10 +02:00
|
|
|
std::string BitsToString(const Flexblock& bits);
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
//! Turns a fixed-size data block into a hex-string
|
2022-05-22 14:13:10 +02:00
|
|
|
std::string BitblockToHexstring(const Block& b);
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
//! Turns a flexible data block into a hex-string
|
2022-05-22 14:13:10 +02:00
|
|
|
std::string BitsToHexstring(const Flexblock& b);
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
//! Turns a hex string into a fixed-size data block
|
2022-05-22 14:13:10 +02:00
|
|
|
Block HexstringToBitblock(const std::string& hexstring);
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
//! Turns a hex string into a flexible data block
|
2022-05-22 14:13:10 +02:00
|
|
|
Flexblock HexstringToBits(const std::string& hexstring);
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
//! Will read a file into a flexblock
|
2022-05-22 14:13:10 +02:00
|
|
|
Flexblock ReadFileToBits(const std::string& filepath);
|
2022-05-13 11:37:08 +02:00
|
|
|
|
|
|
|
|
//! Will save bits to a binary file
|
2022-05-22 14:13:10 +02:00
|
|
|
void WriteBitsToFile(const std::string& filepath, const Flexblock& bits);
|
2022-05-13 11:37:08 +02:00
|
|
|
}
|
2022-05-16 22:01:52 +02:00
|
|
|
|
2022-05-22 00:23:10 +02:00
|
|
|
#endif
|
|
|
|
|
|