esign.cpp
// esign.cpp - written and placed in the public domain by Wei Dai
#include "pch.h"
#include "config.h"
// TODO: fix the C4589 warnings
#if CRYPTOPP_MSC_VERSION
# pragma warning(disable: 4589)
#endif
#include "esign.h"
#include "modarith.h"
#include "integer.h"
#include "nbtheory.h"
#include "algparam.h"
#include "sha.h"
#include "asn.h"
NAMESPACE_BEGIN(CryptoPP)
#if !defined(NDEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
void ESIGN_TestInstantiations()
{
ESIGN<SHA>::Verifier x1(1, 1);
ESIGN<SHA>::Signer x2(NullRNG(), 1);
ESIGN<SHA>::Verifier x3(x2);
ESIGN<SHA>::Verifier x4(x2.GetKey());
ESIGN<SHA>::Verifier x5(x3);
ESIGN<SHA>::Signer x6 = x2;
x6 = x2;
x3 = ESIGN<SHA>::Verifier(x2);
x4 = x2.GetKey();
}
#endif
void ESIGNFunction::BERDecode(BufferedTransformation &bt)
{
BERSequenceDecoder seq(bt);
m_n.BERDecode(seq);
m_e.BERDecode(seq);
seq.MessageEnd();
}
void ESIGNFunction::DEREncode(BufferedTransformation &bt) const
{
DERSequenceEncoder seq(bt);
m_n.DEREncode(seq);
m_e.DEREncode(seq);
seq.MessageEnd();
}
Integer ESIGNFunction::ApplyFunction(const Integer &x) const
{
DoQuickSanityCheck();
return STDMIN(a_exp_b_mod_c(x, m_e, m_n) >> (2*GetK()+2), MaxImage());
}
bool ESIGNFunction::Validate(RandomNumberGenerator& rng, unsigned int level) const
{
CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level);
bool pass = true;
pass = pass && m_n > Integer::One() && m_n.IsOdd();
pass = pass && m_e >= 8 && m_e < m_n;
return pass;
}
bool ESIGNFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
return GetValueHelper(this, name, valueType, pValue).Assignable()
CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
CRYPTOPP_GET_FUNCTION_ENTRY(PublicExponent)
;
}
void ESIGNFunction::AssignFrom(const NameValuePairs &source)
{
AssignFromHelper(this, source)
CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
CRYPTOPP_SET_FUNCTION_ENTRY(PublicExponent)
;
}
// *****************************************************************************
void InvertibleESIGNFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &param)
{
int modulusSize = 1023*2;
param.GetIntValue("ModulusSize", modulusSize) || param.GetIntValue("KeySize", modulusSize);
if (modulusSize < 24)
throw InvalidArgument("InvertibleESIGNFunction: specified modulus size is too small");
if (modulusSize % 3 != 0)
throw InvalidArgument("InvertibleESIGNFunction: modulus size must be divisible by 3");
m_e = param.GetValueWithDefault("PublicExponent", Integer(32));
if (m_e < 8)
throw InvalidArgument("InvertibleESIGNFunction: public exponents less than 8 may not be secure");
// VC70 workaround: putting these after primeParam causes overlapped stack allocation
ConstByteArrayParameter seedParam;
SecByteBlock seed;
const Integer minP = Integer(204) << (modulusSize/3-8);
const Integer maxP = Integer::Power2(modulusSize/3)-1;
AlgorithmParameters primeParam = MakeParameters("Min", minP)("Max", maxP)("RandomNumberType", Integer::PRIME);
if (param.GetValue("Seed", seedParam))
{
seed.resize(seedParam.size() + 4);
memcpy(seed + 4, seedParam.begin(), seedParam.size());
PutWord(false, BIG_ENDIAN_ORDER, seed, (word32)0);
m_p.GenerateRandom(rng, CombinedNameValuePairs(primeParam, MakeParameters("Seed", ConstByteArrayParameter(seed))));
PutWord(false, BIG_ENDIAN_ORDER, seed, (word32)1);
m_q.GenerateRandom(rng, CombinedNameValuePairs(primeParam, MakeParameters("Seed", ConstByteArrayParameter(seed))));
}
else
{
m_p.GenerateRandom(rng, primeParam);
m_q.GenerateRandom(rng, primeParam);
}
m_n = m_p * m_p * m_q;
assert(m_n.BitCount() == (unsigned int)modulusSize);
}
void InvertibleESIGNFunction::BERDecode(BufferedTransformation &bt)
{
BERSequenceDecoder privateKey(bt);
m_n.BERDecode(privateKey);
m_e.BERDecode(privateKey);
m_p.BERDecode(privateKey);
m_q.BERDecode(privateKey);
privateKey.MessageEnd();
}
void InvertibleESIGNFunction::DEREncode(BufferedTransformation &bt) const
{
DERSequenceEncoder privateKey(bt);
m_n.DEREncode(privateKey);
m_e.DEREncode(privateKey);
m_p.DEREncode(privateKey);
m_q.DEREncode(privateKey);
privateKey.MessageEnd();
}
Integer InvertibleESIGNFunction::CalculateRandomizedInverse(RandomNumberGenerator &rng, const Integer &x) const
{
DoQuickSanityCheck();
Integer pq = m_p * m_q;
Integer p2 = m_p * m_p;
Integer r, z, re, a, w0, w1;
do
{
r.Randomize(rng, Integer::Zero(), pq);
z = x << (2*GetK()+2);
re = a_exp_b_mod_c(r, m_e, m_n);
a = (z - re) % m_n;
Integer::Divide(w1, w0, a, pq);
if (w1.NotZero())
{
++w0;
w1 = pq - w1;
}
}
while ((w1 >> (2*GetK()+1)).IsPositive());
ModularArithmetic modp(m_p);
Integer t = modp.Divide(w0 * r % m_p, m_e * re % m_p);
Integer s = r + t*pq;
assert(s < m_n);
#if 0
using namespace std;
cout << "f = " << x << endl;
cout << "r = " << r << endl;
cout << "z = " << z << endl;
cout << "a = " << a << endl;
cout << "w0 = " << w0 << endl;
cout << "w1 = " << w1 << endl;
cout << "t = " << t << endl;
cout << "s = " << s << endl;
#endif
return s;
}
bool InvertibleESIGNFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
{
bool pass = ESIGNFunction::Validate(rng, level);
pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
pass = pass && m_p.BitCount() == m_q.BitCount();
if (level >= 1)
pass = pass && m_p * m_p * m_q == m_n;
if (level >= 2)
pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
return pass;
}
bool InvertibleESIGNFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
return GetValueHelper<ESIGNFunction>(this, name, valueType, pValue).Assignable()
CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
;
}
void InvertibleESIGNFunction::AssignFrom(const NameValuePairs &source)
{
AssignFromHelper<ESIGNFunction>(this, source)
CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
;
}
NAMESPACE_END
AlgorithmParameters
An object that implements NameValuePairs.
Definition: algparam.h:489
ConstByteArrayParameter::size
size_t size() const
Length of the memory block.
Definition: algparam.h:93
NameValuePairs::GetValueWithDefault
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:348
MakeParameters
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:554
nbtheory.h
Classes and functions for number theoretic operations.
Integer::DEREncode
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
Definition: integer.cpp:3372
Integer::NotZero
bool NotZero() const
Determines if the Integer is non-0.
Definition: integer.h:333
BIG_ENDIAN_ORDER
@ BIG_ENDIAN_ORDER
byte order is big-endian
Definition: cryptlib.h:127
DERSequenceEncoder
DER Sequence Encoder.
Definition: asn.h:305
Integer::One
static const Integer & One()
Integer representing 1.
Definition: integer.cpp:3016
InvertibleESIGNFunction::DEREncode
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: esign.cpp:141
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1352
TrapdoorFunctionBounds::MaxImage
virtual Integer MaxImage() const
Returns the maximum size of a message after the trapdoor function is applied bound to a public key.
Definition: pubkey.h:90
ModularArithmetic
Ring of congruence classes modulo n.
Definition: modarith.h:34
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:731
esign.h
InvertibleESIGNFunction::Validate
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: esign.cpp:192
ESIGNFunction::Validate
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: esign.cpp:59
modarith.h
Class file for performing modular arithmetic.
Integer::Power2
static Integer Power2(size_t e)
Exponentiates to a power of 2.
Definition: integer.cpp:2995
CryptoMaterial::DoQuickSanityCheck
void DoQuickSanityCheck() const
Perform a quick sanity check.
Definition: cryptlib.h:2148
ESIGNFunction::ApplyFunction
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
Definition: esign.cpp:53
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1186
SecBlock::resize
void resize(size_type newSize)
Change size and preserve contents.
Definition: secblock.h:705
sha.h
Classes for SHA-1 and SHA-2 family of message digests.
NullRNG
RandomNumberGenerator & NullRNG()
Random Number Generator that does not produce random numbers.
Definition: cryptlib.cpp:406
STDMIN
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:467
InvertibleESIGNFunction::GenerateRandom
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition: esign.cpp:86
Integer::IsOdd
bool IsOdd() const
Determines if the Integer is odd parity.
Definition: integer.h:351
InvertibleESIGNFunction::BERDecode
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
Definition: esign.cpp:131
ESIGNFunction::DEREncode
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: esign.cpp:45
VerifyPrime
bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level=1)
Verifies a prime number.
Definition: nbtheory.cpp:249
asn.h
Classes and functions for working with ANS.1 objects.
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:151
Integer::Randomize
void Randomize(RandomNumberGenerator &rng, size_t bitCount)
Set this Integer to random integer.
Definition: integer.cpp:3439
Integer::BitCount
unsigned int BitCount() const
Determines the number of bits required to represent the Integer.
Definition: integer.cpp:3289
Integer::BERDecode
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
Definition: integer.cpp:3379
ESIGNFunction::BERDecode
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
Definition: esign.cpp:37
InvalidArgument
An invalid argument was detected.
Definition: cryptlib.h:182
Integer::Divide
static void Divide(Integer &r, Integer &q, const Integer &a, const Integer &d)
calculate r and q such that (a == d*q + r) && (0 <= r < abs(d))
Definition: integer.cpp:3983
InvertibleESIGNFunction::AssignFrom
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: esign.cpp:213
ESIGNFunction::AssignFrom
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: esign.cpp:76
ConstByteArrayParameter::begin
const byte * begin() const
Pointer to the first byte in the memory block.
Definition: algparam.h:89
Integer::Zero
static const Integer & Zero()
Integer representing 0.
Definition: integer.cpp:3011
CryptoPP
Crypto++ library namespace.
NameValuePairs::GetValue
bool GetValue(const char *name, T &value) const
Get a named value.
Definition: cryptlib.h:335
config.h
Library configuration file.
CombinedNameValuePairs
Combines two sets of NameValuePairs.
Definition: algparam.h:135
NameValuePairs::GetIntValue
bool GetIntValue(const char *name, int &value) const
Get a named value with type int.
Definition: cryptlib.h:371
TF_SS< P1363_EMSA5, H, ESIGN_Keys >::Verifier
PK_FinalTemplate< TF_VerifierImpl< SchemeOptions > > Verifier
implements PK_Verifier interface
Definition: pubkey.h:2206
InvertibleESIGNFunction::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: esign.cpp:205
Integer::PRIME
@ PRIME
a number which is probabilistically prime
Definition: integer.h:91
PK_FinalTemplate
Template implementing constructors for public key algorithm classes.
Definition: pubkey.h:2040
ModularArithmetic::Divide
const Integer & Divide(const Integer &a, const Integer &b) const
Divides elements in the ring.
Definition: modarith.h:200
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:277
ConstByteArrayParameter
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:29
algparam.h
Classes for working with NameValuePairs.
integer.h
Multiple precision integer with arithmetic operations.
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:45
BERSequenceDecoder
BER Sequence Decoder.
Definition: asn.h:295
ESIGNFunction::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: esign.cpp:68