esign.h
Go to the documentation of this file.
// esign.h - originally written and placed in the public domain by Wei Dai
/// \file esign.h
/// \brief Classes providing ESIGN signature schemes as defined in IEEE P1363a
/// \since Crypto++ 5.0
#ifndef CRYPTOPP_ESIGN_H
#define CRYPTOPP_ESIGN_H
#include "cryptlib.h"
#include "pubkey.h"
#include "integer.h"
#include "asn.h"
#include "misc.h"
NAMESPACE_BEGIN(CryptoPP)
/// \brief ESIGN trapdoor function using the public key
/// \since Crypto++ 5.0
class ESIGNFunction : public TrapdoorFunction, public ASN1CryptoMaterial<PublicKey>
{
typedef ESIGNFunction ThisClass;
public:
/// \brief Initialize a ESIGN public key with {n,e}
/// \param n the modulus
/// \param e the public exponent
void Initialize(const Integer &n, const Integer &e)
{m_n = n; m_e = e;}
// PublicKey
void BERDecode(BufferedTransformation &bt);
void DEREncode(BufferedTransformation &bt) const;
// CryptoMaterial
bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
void AssignFrom(const NameValuePairs &source);
// TrapdoorFunction
Integer ApplyFunction(const Integer &x) const;
Integer PreimageBound() const {return m_n;}
Integer ImageBound() const {return Integer::Power2(GetK());}
// non-derived
const Integer & GetModulus() const {return m_n;}
const Integer & GetPublicExponent() const {return m_e;}
void SetModulus(const Integer &n) {m_n = n;}
void SetPublicExponent(const Integer &e) {m_e = e;}
protected:
// Covertiy finding on overflow. The library allows small values for research purposes.
unsigned int GetK() const {return SaturatingSubtract(m_n.BitCount()/3, 1U);}
Integer m_n, m_e;
};
/// \brief ESIGN trapdoor function using the private key
/// \since Crypto++ 5.0
class InvertibleESIGNFunction : public ESIGNFunction, public RandomizedTrapdoorFunctionInverse, public PrivateKey
{
typedef InvertibleESIGNFunction ThisClass;
public:
/// \brief Initialize a ESIGN private key with {n,e,p,q}
/// \param n modulus
/// \param e public exponent
/// \param p first prime factor
/// \param q second prime factor
/// \details This Initialize() function overload initializes a private key from existing parameters.
void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q)
{m_n = n; m_e = e; m_p = p; m_q = q;}
/// \brief Create a ESIGN private key
/// \param rng a RandomNumberGenerator derived class
/// \param modulusBits the size of the modulud, in bits
/// \details This function overload of Initialize() creates a new private key because it
/// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
/// then use one of the other Initialize() overloads.
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
{GenerateRandomWithKeySize(rng, modulusBits);}
// Squash Visual Studio C4250 warning
void Save(BufferedTransformation &bt) const
{BEREncode(bt);}
// Squash Visual Studio C4250 warning
void Load(BufferedTransformation &bt)
{BERDecode(bt);}
void BERDecode(BufferedTransformation &bt);
void DEREncode(BufferedTransformation &bt) const;
Integer CalculateRandomizedInverse(RandomNumberGenerator &rng, const Integer &x) const;
// GeneratibleCryptoMaterial
bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
void AssignFrom(const NameValuePairs &source);
/*! parameters: (ModulusSize) */
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
const Integer& GetPrime1() const {return m_p;}
const Integer& GetPrime2() const {return m_q;}
void SetPrime1(const Integer &p) {m_p = p;}
void SetPrime2(const Integer &q) {m_q = q;}
protected:
Integer m_p, m_q;
};
/// \brief EMSA5 padding method
/// \tparam T Mask Generation Function
/// \since Crypto++ 5.0
template <class T>
class EMSA5Pad : public PK_DeterministicSignatureMessageEncodingMethod
{
public:
CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "EMSA5";}
void ComputeMessageRepresentative(RandomNumberGenerator &rng,
const byte *recoverableMessage, size_t recoverableMessageLength,
HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
byte *representative, size_t representativeBitLength) const
{
CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(recoverableMessage), CRYPTOPP_UNUSED(recoverableMessageLength);
CRYPTOPP_UNUSED(messageEmpty), CRYPTOPP_UNUSED(hashIdentifier);
SecByteBlock digest(hash.DigestSize());
hash.Final(digest);
size_t representativeByteLength = BitsToBytes(representativeBitLength);
T mgf;
mgf.GenerateAndMask(hash, representative, representativeByteLength, digest, digest.size(), false);
if (representativeBitLength % 8 != 0)
representative[0] = (byte)Crop(representative[0], representativeBitLength % 8);
}
};
/// \brief EMSA5 padding method, for use with ESIGN
/// \since Crypto++ 5.0
struct P1363_EMSA5 : public SignatureStandard
{
typedef EMSA5Pad<P1363_MGF1> SignatureMessageEncodingMethod;
};
/// \brief ESIGN keys
/// \since Crypto++ 5.0
struct ESIGN_Keys
{
CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "ESIGN";}
typedef ESIGNFunction PublicKey;
typedef InvertibleESIGNFunction PrivateKey;
};
/// \brief ESIGN signature scheme, IEEE P1363a
/// \tparam H HashTransformation derived class
/// \tparam STANDARD Signature encoding method
/// \since Crypto++ 5.0
template <class H, class STANDARD = P1363_EMSA5>
struct ESIGN : public TF_SS<ESIGN_Keys, STANDARD, H>
{
};
NAMESPACE_END
#endif
asn.h
Classes and functions for working with ANS.1 objects.
ASN1CryptoMaterial
Encode and decode ASN.1 objects with additional information.
Definition: asn.h:684
ASN1Object::BEREncode
virtual void BEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: cryptlib.h:3302
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1652
EMSA5Pad
EMSA5 padding method.
Definition: esign.h:121
ESIGNFunction
ESIGN trapdoor function using the public key.
Definition: esign.h:21
ESIGNFunction::PreimageBound
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition: esign.h:43
ESIGNFunction::Initialize
void Initialize(const Integer &n, const Integer &e)
Initialize a ESIGN public key with {n,e}.
Definition: esign.h:29
ESIGNFunction::ImageBound
Integer ImageBound() const
Returns the maximum size of a message after the trapdoor function is applied.
Definition: esign.h:44
GeneratableCryptoMaterial::GenerateRandomWithKeySize
void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize)
Generate a random key or crypto parameters.
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1113
HashTransformation::DigestSize
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
HashTransformation::Final
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:1142
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:50
Integer::Power2
static Integer Power2(size_t e)
Exponentiates to a power of 2.
InvertibleESIGNFunction
ESIGN trapdoor function using the private key.
Definition: esign.h:63
InvertibleESIGNFunction::CalculateRandomizedInverse
Integer CalculateRandomizedInverse(RandomNumberGenerator &rng, const Integer &x) const
Applies the inverse of the trapdoor function, using random data if required.
Definition: esign.cpp:153
InvertibleESIGNFunction::Validate
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: esign.cpp:194
InvertibleESIGNFunction::GenerateRandom
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition: esign.cpp:88
InvertibleESIGNFunction::Initialize
void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q)
Initialize a ESIGN private key with {n,e,p,q}.
Definition: esign.h:74
InvertibleESIGNFunction::Initialize
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
Create a ESIGN private key.
Definition: esign.h:83
InvertibleESIGNFunction::Save
void Save(BufferedTransformation &bt) const
Saves a key to a BufferedTransformation.
Definition: esign.h:87
InvertibleESIGNFunction::Load
void Load(BufferedTransformation &bt)
Loads a key from a BufferedTransformation.
Definition: esign.h:91
InvertibleESIGNFunction::DEREncode
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: esign.cpp:143
InvertibleESIGNFunction::BERDecode
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
Definition: esign.cpp:133
InvertibleESIGNFunction::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: esign.cpp:217
InvertibleESIGNFunction::AssignFrom
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: esign.cpp:225
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:322
PK_DeterministicSignatureMessageEncodingMethod
Interface for message encoding method for public key signature schemes.
Definition: pubkey.h:392
PrivateKey
Interface for private keys.
Definition: cryptlib.h:2541
PublicKey
Interface for public keys.
Definition: cryptlib.h:2536
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1435
RandomizedTrapdoorFunctionInverse
Applies the inverse of the trapdoor function, using random data if required.
Definition: pubkey.h:155
SecBlock::size
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:867
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:1226
TF_SS
Trapdoor Function (TF) Signature Scheme.
Definition: pubkey.h:2316
TrapdoorFunction
Applies the trapdoor function.
Definition: pubkey.h:126
byte
unsigned char byte
8-bit unsigned datatype
Definition: config_int.h:56
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
integer.h
Multiple precision integer with arithmetic operations.
misc.h
Utility functions for the Crypto++ library.
SaturatingSubtract
T1 SaturatingSubtract(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 0.
Definition: misc.h:1093
Crop
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
Definition: misc.h:926
BitsToBytes
size_t BitsToBytes(size_t bitCount)
Returns the number of 8-bit bytes or octets required for the specified number of bits.
Definition: misc.h:938
CryptoPP
Crypto++ library namespace.
pubkey.h
This file contains helper classes/functions for implementing public key algorithms.
ESIGN_Keys
ESIGN keys.
Definition: esign.h:152
ESIGN
ESIGN signature scheme, IEEE P1363a.
Definition: esign.h:164
P1363_EMSA5
EMSA5 padding method, for use with ESIGN.
Definition: esign.h:145
SignatureStandard
Base class for public key signature standard classes.
Definition: pubkey.h:2279