Skip to content
This repository was archived by the owner on Jan 6, 2020. It is now read-only.

Crypto Utils

justinemclevy edited this page Jan 17, 2014 · 8 revisions

Back

Overview

Maidsafe utilises CryptoPP as a basic encryption library. The header file crypto.h includes cryptographic type definitions, constants and utility functions used by various other MaidSafe libraries.

Reference

Exposed CryptoPP atomic type

typedef CryptoPP::SHA1 SHA1;
typedef CryptoPP::SHA256 SHA256;
typedef CryptoPP::SHA384 SHA384;
typedef CryptoPP::SHA512 SHA512;
typedef CryptoPP::Tiger Tiger;
typedef CryptoPP::Integer BigInt;

Parameters

enum { AES256_KeySize = 32 };  // size in bytes.
enum { AES256_IVSize = 16 };  // size in bytes.
extern const uint16_t kMaxCompressionLevel;
extern const std::string kMaidSafeVersionLabel1;
extern const std::string kMaidSafeVersionLabel;

Named Type

typedef NonEmptyString SecurePassword, Salt, PlainText, CipherText, CompressedText, UncompressedText;

Tagged value

typedef detail::BoundedString<AES256_KeySize> AES256Key;
typedef detail::BoundedString<AES256_IVSize> AES256InitialisationVector;
typedef detail::BoundedString<SHA1::DIGESTSIZE, SHA1::DIGESTSIZE> SHA1Hash;
typedef detail::BoundedString<SHA256::DIGESTSIZE, SHA256::DIGESTSIZE> SHA256Hash;
typedef detail::BoundedString<SHA384::DIGESTSIZE, SHA384::DIGESTSIZE> SHA384Hash;
typedef detail::BoundedString<SHA512::DIGESTSIZE, SHA512::DIGESTSIZE> SHA512Hash;
typedef detail::BoundedString<Tiger::DIGESTSIZE, Tiger::DIGESTSIZE> TigerHash;

XOR helper function

// Performs a bitwise XOR on each char of first with the corresponding char of second.  If size is 0, an empty string is returned.
template<size_t size>
detail::BoundedString<size, size> XOR(const detail::BoundedString<size, size>& first,
                                      const detail::BoundedString<size, size>& second);
std::string XOR(const std::string& first, const std::string& second);

Hash wrapper

// Hash function operating on a string.
template <typename HashType>
detail::BoundedString<HashType::DIGESTSIZE, HashType::DIGESTSIZE> Hash(const std::string& input);
// Hash function operating on a BoundedString.
template <typename HashType, size_t min, size_t max>
detail::BoundedString<HashType::DIGESTSIZE, HashType::DIGESTSIZE> Hash(const detail::BoundedString<min, max>& input);
// Hash function operating on an arbitrary string type.
template <typename HashType, typename StringType>
detail::BoundedString<HashType::DIGESTSIZE, HashType::DIGESTSIZE, StringType> Hash(const StringType& input);
// Hash function operating on a file.
template <typename HashType>
detail::BoundedString<HashType::DIGESTSIZE, HashType::DIGESTSIZE> HashFile(const boost::filesystem::path& file_path);

Secure password wrapper

// Creates a secure password using the Password-Based Key Derivation Function (PBKDF) version 2
// algorithm.  The number of iterations is derived from "pin".  "label" is additional data to
// provide distinct input data to PBKDF.  The function will throw a std::exception if invalid
// parameters are passed.
template<typename PasswordType>
SecurePassword CreateSecurePassword(const PasswordType& password,
                                    const Salt& salt,
                                    const uint32_t& pin,
                                    const std::string& label = kMaidSafeVersionLabel);

Symmetric encryption

// Performs symmetric encrytion using AES256. It throws a std::exception if the
// key size < AES256_KeySize or if initialisation_vector size < AES256_IVSize.
CipherText SymmEncrypt(const PlainText& input,
                       const AES256Key& key,
                       const AES256InitialisationVector& initialisation_vector);
// Performs symmetric decrytion using AES256. It throws a std::exception if the
// key size < AES256_KeySize or if initialisation_vector size < AES256_IVSize.
PlainText SymmDecrypt(const CipherText& input,
                      const AES256Key& key,
                      const AES256InitialisationVector& initialisation_vector);

N+P key sharing (based on Shamir's algorithm)

std::vector<std::string> SecretShareData(const int32_t& threshold,
                                         const int32_t& number_of_shares,
                                         const std::string& data);
std::string SecretRecoverData(const int32_t& threshold, const std::vector<std::string>& in_strings);

Compress / Uncompress

// Compress a string using gzip.  Compression level must be between 0 and 9
// inclusive or function throws a std::exception.
CompressedText Compress(const UncompressedText& input, const uint16_t& compression_level);
// Uncompress a string using gzip.  Will throw a std::exception if uncompression fails.
UncompressedText Uncompress(const CompressedText& input);

Back

Clone this wiki locally