rabin.cpp
// rabin.cpp - originally written and placed in the public domain by Wei Dai
#include "pch.h"
#include "rabin.h"
#include "integer.h"
#include "nbtheory.h"
#include "modarith.h"
#include "asn.h"
#include "sha.h"
NAMESPACE_BEGIN(CryptoPP)
void RabinFunction::BERDecode(BufferedTransformation &bt)
{
BERSequenceDecoder seq(bt);
m_n.BERDecode(seq);
m_r.BERDecode(seq);
m_s.BERDecode(seq);
seq.MessageEnd();
}
void RabinFunction::DEREncode(BufferedTransformation &bt) const
{
DERSequenceEncoder seq(bt);
m_n.DEREncode(seq);
m_r.DEREncode(seq);
m_s.DEREncode(seq);
seq.MessageEnd();
}
Integer RabinFunction::ApplyFunction(const Integer &in) const
{
DoQuickSanityCheck();
Integer out = in.Squared()%m_n;
if (in.IsOdd())
out = out*m_r%m_n;
if (Jacobi(in, m_n)==-1)
out = out*m_s%m_n;
return out;
}
bool RabinFunction::Validate(RandomNumberGenerator& /*rng*/, unsigned int level) const
{
bool pass = true;
pass = pass && m_n > Integer::One() && m_n%4 == 1;
CRYPTOPP_ASSERT(pass);
pass = pass && m_r > Integer::One() && m_r < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_s > Integer::One() && m_s < m_n;
CRYPTOPP_ASSERT(pass);
if (level >= 1)
{
pass = pass && Jacobi(m_r, m_n) == -1 && Jacobi(m_s, m_n) == -1;
CRYPTOPP_ASSERT(pass);
}
return pass;
}
bool RabinFunction::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(QuadraticResidueModPrime1)
CRYPTOPP_GET_FUNCTION_ENTRY(QuadraticResidueModPrime2)
;
}
void RabinFunction::AssignFrom(const NameValuePairs &source)
{
AssignFromHelper(this, source)
CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
CRYPTOPP_SET_FUNCTION_ENTRY(QuadraticResidueModPrime1)
CRYPTOPP_SET_FUNCTION_ENTRY(QuadraticResidueModPrime2)
;
}
// *****************************************************************************
// private key operations:
// generate a random private key
void InvertibleRabinFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
{
int modulusSize = 2048;
alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize);
if (modulusSize < 16)
throw InvalidArgument("InvertibleRabinFunction: specified modulus size is too small");
// VC70 workaround: putting these after primeParam causes overlapped stack allocation
bool rFound=false, sFound=false;
Integer t=2;
AlgorithmParameters primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
("EquivalentTo", 3)("Mod", 4);
m_p.GenerateRandom(rng, primeParam);
m_q.GenerateRandom(rng, primeParam);
while (!(rFound && sFound))
{
int jp = Jacobi(t, m_p);
int jq = Jacobi(t, m_q);
if (!rFound && jp==1 && jq==-1)
{
m_r = t;
rFound = true;
}
if (!sFound && jp==-1 && jq==1)
{
m_s = t;
sFound = true;
}
++t;
}
m_n = m_p * m_q;
m_u = m_q.InverseMod(m_p);
}
void InvertibleRabinFunction::BERDecode(BufferedTransformation &bt)
{
BERSequenceDecoder seq(bt);
m_n.BERDecode(seq);
m_r.BERDecode(seq);
m_s.BERDecode(seq);
m_p.BERDecode(seq);
m_q.BERDecode(seq);
m_u.BERDecode(seq);
seq.MessageEnd();
}
void InvertibleRabinFunction::DEREncode(BufferedTransformation &bt) const
{
DERSequenceEncoder seq(bt);
m_n.DEREncode(seq);
m_r.DEREncode(seq);
m_s.DEREncode(seq);
m_p.DEREncode(seq);
m_q.DEREncode(seq);
m_u.DEREncode(seq);
seq.MessageEnd();
}
Integer InvertibleRabinFunction::CalculateInverse(RandomNumberGenerator &rng, const Integer &in) const
{
DoQuickSanityCheck();
ModularArithmetic modn(m_n);
Integer r(rng, Integer::One(), m_n - Integer::One());
r = modn.Square(r);
Integer r2 = modn.Square(r);
Integer c = modn.Multiply(in, r2); // blind
Integer cp=c%m_p, cq=c%m_q;
int jp = Jacobi(cp, m_p);
int jq = Jacobi(cq, m_q);
if (jq==-1)
{
cp = cp*EuclideanMultiplicativeInverse(m_r, m_p)%m_p;
cq = cq*EuclideanMultiplicativeInverse(m_r, m_q)%m_q;
}
if (jp==-1)
{
cp = cp*EuclideanMultiplicativeInverse(m_s, m_p)%m_p;
cq = cq*EuclideanMultiplicativeInverse(m_s, m_q)%m_q;
}
cp = ModularSquareRoot(cp, m_p);
cq = ModularSquareRoot(cq, m_q);
if (jp==-1)
cp = m_p-cp;
Integer out = CRT(cq, m_q, cp, m_p, m_u);
out = modn.Divide(out, r); // unblind
if ((jq==-1 && out.IsEven()) || (jq==1 && out.IsOdd()))
out = m_n-out;
return out;
}
bool InvertibleRabinFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
{
bool pass = RabinFunction::Validate(rng, level);
CRYPTOPP_ASSERT(pass);
pass = pass && m_p > Integer::One() && m_p%4 == 3 && m_p < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_q > Integer::One() && m_q%4 == 3 && m_q < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_u.IsPositive() && m_u < m_p;
CRYPTOPP_ASSERT(pass);
if (level >= 1)
{
pass = pass && m_p * m_q == m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_u * m_q % m_p == 1;
CRYPTOPP_ASSERT(pass);
pass = pass && Jacobi(m_r, m_p) == 1;
CRYPTOPP_ASSERT(pass);
pass = pass && Jacobi(m_r, m_q) == -1;
CRYPTOPP_ASSERT(pass);
pass = pass && Jacobi(m_s, m_p) == -1;
CRYPTOPP_ASSERT(pass);
pass = pass && Jacobi(m_s, m_q) == 1;
CRYPTOPP_ASSERT(pass);
}
if (level >= 2)
{
pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
CRYPTOPP_ASSERT(pass);
}
return pass;
}
bool InvertibleRabinFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
return GetValueHelper<RabinFunction>(this, name, valueType, pValue).Assignable()
CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
;
}
void InvertibleRabinFunction::AssignFrom(const NameValuePairs &source)
{
AssignFromHelper<RabinFunction>(this, source)
CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
;
}
NAMESPACE_END
asn.h
Classes and functions for working with ANS.1 objects.
AlgorithmParameters
An object that implements NameValuePairs.
Definition: algparam.h:426
BERSequenceDecoder
BER Sequence Decoder.
Definition: asn.h:525
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1652
CryptoMaterial::DoQuickSanityCheck
void DoQuickSanityCheck() const
Perform a quick sanity check.
Definition: cryptlib.h:2493
DERSequenceEncoder
DER Sequence Encoder.
Definition: asn.h:557
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:50
Integer::DEREncode
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
Integer::GenerateRandom
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &params=g_nullNameValuePairs)
Generate a random number.
Definition: integer.h:506
Integer::IsPositive
bool IsPositive() const
Determines if the Integer is positive.
Definition: integer.h:345
Integer::Squared
Integer Squared() const
Multiply this integer by itself.
Definition: integer.h:631
Integer::BERDecode
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
Integer::One
static const Integer & One()
Integer representing 1.
Integer::IsOdd
bool IsOdd() const
Determines if the Integer is odd parity.
Definition: integer.h:354
Integer::InverseMod
Integer InverseMod(const Integer &n) const
Calculate multiplicative inverse.
Integer::IsEven
bool IsEven() const
Determines if the Integer is even parity.
Definition: integer.h:351
InvalidArgument
An invalid argument was detected.
Definition: cryptlib.h:203
InvertibleRabinFunction::CalculateInverse
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
Definition: rabin.cpp:147
InvertibleRabinFunction::GenerateRandom
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition: rabin.cpp:82
InvertibleRabinFunction::AssignFrom
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: rabin.cpp:232
InvertibleRabinFunction::Validate
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: rabin.cpp:190
InvertibleRabinFunction::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: rabin.cpp:223
ModularArithmetic
Ring of congruence classes modulo n.
Definition: modarith.h:44
ModularArithmetic::Multiply
const Integer & Multiply(const Integer &a, const Integer &b) const
Multiplies elements in the ring.
Definition: modarith.h:190
ModularArithmetic::Divide
const Integer & Divide(const Integer &a, const Integer &b) const
Divides elements in the ring.
Definition: modarith.h:218
ModularArithmetic::Square
const Integer & Square(const Integer &a) const
Square an element in the ring.
Definition: modarith.h:197
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:322
NameValuePairs::GetIntValue
CRYPTOPP_DLL bool GetIntValue(const char *name, int &value) const
Get a named value with type int.
Definition: cryptlib.h:415
RabinFunction::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: rabin.cpp:60
RabinFunction::Validate
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: rabin.cpp:43
RabinFunction::AssignFrom
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: rabin.cpp:69
RabinFunction::ApplyFunction
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
Definition: rabin.cpp:31
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1435
integer.h
Multiple precision integer with arithmetic operations.
modarith.h
Class file for performing modular arithmetic.
CryptoPP
Crypto++ library namespace.
Name::Prime1
const char * Prime1()
Integer.
Definition: argnames.h:43
Name::Modulus
const char * Modulus()
Integer.
Definition: argnames.h:33
Name::QuadraticResidueModPrime1
const char * QuadraticResidueModPrime1()
Integer.
Definition: argnames.h:48
Name::MultiplicativeInverseOfPrime2ModPrime1
const char * MultiplicativeInverseOfPrime2ModPrime1()
Integer.
Definition: argnames.h:47
Name::QuadraticResidueModPrime2
const char * QuadraticResidueModPrime2()
Integer.
Definition: argnames.h:49
Name::Prime2
const char * Prime2()
Integer.
Definition: argnames.h:44
nbtheory.h
Classes and functions for number theoretic operations.
Jacobi
CRYPTOPP_DLL int Jacobi(const Integer &a, const Integer &b)
Calculate the Jacobi symbol.
ModularSquareRoot
CRYPTOPP_DLL Integer ModularSquareRoot(const Integer &a, const Integer &p)
Extract a modular square root.
VerifyPrime
CRYPTOPP_DLL bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level=1)
Verifies a number is probably prime.
EuclideanMultiplicativeInverse
Integer EuclideanMultiplicativeInverse(const Integer &a, const Integer &b)
Calculate multiplicative inverse.
Definition: nbtheory.h:166
CRT
CRYPTOPP_DLL Integer CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u)
Chinese Remainder Theorem.
pch.h
Precompiled header file.
rabin.h
Classes for Rabin encryption and signature schemes.
sha.h
Classes for SHA-1 and SHA-2 family of message digests.
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68