remove some unused stuff

This commit is contained in:
Glenn Maynard
2004-05-14 00:35:44 +00:00
parent 6e399ab8ab
commit 5705412dc2
7 changed files with 1 additions and 572 deletions
-9
View File
@@ -5,7 +5,6 @@
#include "misc.h"
#include "filters.h"
#include "algparam.h"
#include "fips140.h"
#include "argnames.h"
#include <memory>
@@ -31,14 +30,6 @@ BufferedTransformation & TheBitBucket()
Algorithm::Algorithm(bool checkSelfTestStatus)
{
if (checkSelfTestStatus && FIPS_140_2_ComplianceEnabled())
{
if (GetPowerUpSelfTestStatus() == POWER_UP_SELF_TEST_NOT_DONE && !PowerUpSelfTestInProgressOnThisThread())
throw SelfTestFailure("Cryptographic algorithms are disabled before the power-up self tests are performed.");
if (GetPowerUpSelfTestStatus() == POWER_UP_SELF_TEST_FAILED)
throw SelfTestFailure("Cryptographic algorithms are disabled after power-up a self test failed.");
}
}
void SimpleKeyingInterface::SetKeyWithRounds(const byte *key, unsigned int length, int rounds)
-16
View File
@@ -940,19 +940,6 @@ public:
/*! \note This function can be used to create a public key from a private key. */
virtual void AssignFrom(const NameValuePairs &source) =0;
//! check this object for errors
/*! \param level denotes the level of thoroughness:
0 - using this object won't cause a crash or exception (rng is ignored)
1 - this object will probably function (encrypt, sign, etc.) correctly (but may not check for weak keys and such)
2 - make sure this object will function correctly, and do reasonable security checks
3 - do checks that may take a long time
\return true if the tests pass */
virtual bool Validate(RandomNumberGenerator &rng, unsigned int level) const =0;
//! throws InvalidMaterial if this object fails Validate() test
virtual void ThrowIfInvalid(RandomNumberGenerator &rng, unsigned int level) const
{if (!Validate(rng, level)) throw InvalidMaterial("CryptoMaterial: this object contains invalid values");}
// virtual std::vector<std::string> GetSupportedFormats(bool includeSaveOnly=false, bool includeLoadOnly=false);
//! save key into a BufferedTransformation
@@ -980,9 +967,6 @@ public:
//! save precomputation for later use
virtual void SavePrecomputation(BufferedTransformation &storedPrecomputation) const
{assert(!SupportsPrecomputation()); throw NotImplemented("CryptoMaterial: this object does not support precomputation");}
// for internal library use
void DoQuickSanityCheck() const {ThrowIfInvalid(NullRNG(), 0);}
};
//! interface for generatable crypto material, such as private keys and crypto parameters
-64
View File
@@ -2163,61 +2163,6 @@ void MontgomeryReduce(word *R, word *T, const word *X, const word *M, const word
CopyWords(R, T + (borrow ? N : 0), N);
}
// R[N] --- result = X/(2**(WORD_BITS*N/2)) mod M
// T[2*N] - temporary work space
// X[2*N] - number to be reduced
// M[N] --- modulus
// U[N/2] - multiplicative inverse of M mod 2**(WORD_BITS*N/2)
// V[N] --- 2**(WORD_BITS*3*N/2) mod M
void HalfMontgomeryReduce(word *R, word *T, const word *X, const word *M, const word *U, const word *V, unsigned int N)
{
assert(N%2==0 && N>=4);
#define M0 M
#define M1 (M+N2)
#define V0 V
#define V1 (V+N2)
#define X0 X
#define X1 (X+N2)
#define X2 (X+N)
#define X3 (X+N+N2)
const unsigned int N2 = N/2;
Multiply(T0, T2, V0, X3, N2);
int c2 = Add(T0, T0, X0, N);
MultiplyBottom(T3, T2, T0, U, N2);
MultiplyTop(T2, R, T0, T3, M0, N2);
c2 -= Subtract(T2, T1, T2, N2);
Multiply(T0, R, T3, M1, N2);
c2 -= Subtract(T0, T2, T0, N2);
int c3 = -(int)Subtract(T1, X2, T1, N2);
Multiply(R0, T2, V1, X3, N2);
c3 += Add(R, R, T, N);
if (c2>0)
c3 += Increment(R1, N2);
else if (c2<0)
c3 -= Decrement(R1, N2, -c2);
assert(c3>=-1 && c3<=1);
if (c3>0)
Subtract(R, R, M, N);
else if (c3<0)
Add(R, R, M, N);
#undef M0
#undef M1
#undef V0
#undef V1
#undef X0
#undef X1
#undef X2
#undef X3
}
#undef A0
#undef A1
#undef B0
@@ -2285,15 +2230,6 @@ static inline void AtomicDivide(word *Q, const word *A, const word *B)
T[0] = A[0]; T[1] = A[1]; T[2] = A[2]; T[3] = A[3];
Q[1] = SubatomicDivide(T+1, B[0], B[1]);
Q[0] = SubatomicDivide(T, B[0], B[1]);
#ifndef NDEBUG
// multiply quotient and divisor and add remainder, make sure it equals dividend
assert(!T[2] && !T[3] && (T[1] < B[1] || (T[1]==B[1] && T[0]<B[0])));
word P[4];
LowLevel::Multiply2(P, Q, B);
Add(P, P, T, 4);
assert(memcmp(P, A, 4*WORD_SIZE)==0);
#endif
}
}
-370
View File
@@ -135,15 +135,6 @@ bool SmallDivisorsTest(const Integer &p)
return !TrialDivision(p, primeTable[primeTableSize-1]);
}
bool IsFermatProbablePrime(const Integer &n, const Integer &b)
{
if (n <= 3)
return n==2 || n==3;
assert(n>3 && b>1 && b<n-1);
return a_exp_b_mod_c(b, n-1, n)==1;
}
bool IsStrongProbablePrime(const Integer &n, const Integer &b)
{
if (n <= 3)
@@ -194,32 +185,6 @@ bool RabinMillerTest(RandomNumberGenerator &rng, const Integer &n, unsigned int
return true;
}
bool IsLucasProbablePrime(const Integer &n)
{
if (n <= 1)
return false;
if (n.IsEven())
return n==2;
assert(n>2);
Integer b=3;
unsigned int i=0;
int j;
while ((j=Jacobi(b.Squared()-4, n)) == 1)
{
if (++i==64 && n.IsSquare()) // avoid infinite loop if n is a square
return false;
++b; ++b;
}
if (j==0)
return false;
else
return Lucas(n+1, b, n)==2;
}
bool IsStrongLucasProbablePrime(const Integer &n)
{
@@ -498,87 +463,6 @@ static bool ProvePrime(const Integer &p, const Integer &q)
return false;
}
Integer MihailescuProvablePrime(RandomNumberGenerator &rng, unsigned int pbits)
{
Integer p;
Integer minP = Integer::Power2(pbits-1);
Integer maxP = Integer::Power2(pbits) - 1;
if (maxP <= Integer(lastSmallPrime).Squared())
{
// Randomize() will generate a prime provable by trial division
p.Randomize(rng, minP, maxP, Integer::PRIME);
return p;
}
unsigned int qbits = (pbits+2)/3 + 1 + rng.GenerateWord32(0, pbits/36);
Integer q = MihailescuProvablePrime(rng, qbits);
Integer q2 = q<<1;
while (true)
{
// this initializes the sieve to search in the arithmetic
// progression p = p_0 + \lambda * q2 = p_0 + 2 * \lambda * q,
// with q the recursively generated prime above. We will be able
// to use Lucas tets for proving primality. A trick of Quisquater
// allows taking q > cubic_root(p) rather then square_root: this
// decreases the recursion.
p.Randomize(rng, minP, maxP, Integer::ANY, 1, q2);
PrimeSieve sieve(p, STDMIN(p+PrimeSearchInterval(maxP)*q2, maxP), q2);
while (sieve.NextCandidate(p))
{
if (FastProbablePrimeTest(p) && ProvePrime(p, q))
return p;
}
}
// not reached
return p;
}
Integer MaurerProvablePrime(RandomNumberGenerator &rng, unsigned int bits)
{
const unsigned smallPrimeBound = 29, c_opt=10;
Integer p;
BuildPrimeTable();
if (bits < smallPrimeBound)
{
do
p.Randomize(rng, Integer::Power2(bits-1), Integer::Power2(bits)-1, Integer::ANY, 1, 2);
while (TrialDivision(p, 1 << ((bits+1)/2)));
}
else
{
const unsigned margin = bits > 50 ? 20 : (bits-10)/2;
double relativeSize;
do
relativeSize = pow(2.0, double(rng.GenerateWord32())/0xffffffff - 1);
while (bits * relativeSize >= bits - margin);
Integer a,b;
Integer q = MaurerProvablePrime(rng, unsigned(bits*relativeSize));
Integer I = Integer::Power2(bits-2)/q;
Integer I2 = I << 1;
unsigned int trialDivisorBound = (unsigned int)STDMIN((unsigned long)primeTable[primeTableSize-1], (unsigned long)bits*bits/c_opt);
bool success = false;
while (!success)
{
p.Randomize(rng, I, I2, Integer::ANY);
p *= q; p <<= 1; ++p;
if (!TrialDivision(p, trialDivisorBound))
{
a.Randomize(rng, 2, p-1, Integer::ANY);
b = a_exp_b_mod_c(a, (p-1)/q, p);
success = (GCD(b-1, p) == 1) && (a_exp_b_mod_c(b, q, p) == 1);
}
}
}
return p;
}
Integer CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u)
{
// isn't operator overloading great?
@@ -854,174 +738,6 @@ Integer Lucas(const Integer &e, const Integer &pIn, const Integer &n)
return m.ConvertOut(v);
}
// This is Peter Montgomery's unpublished Lucas sequence evalutation algorithm.
// The total number of multiplies and squares used is less than the binary
// algorithm (see above). Unfortunately I can't get it to run as fast as
// the binary algorithm because of the extra overhead.
/*
Integer Lucas(const Integer &n, const Integer &P, const Integer &modulus)
{
if (!n)
return 2;
#define f(A, B, C) m.Subtract(m.Multiply(A, B), C)
#define X2(A) m.Subtract(m.Square(A), two)
#define X3(A) m.Multiply(A, m.Subtract(m.Square(A), three))
MontgomeryRepresentation m(modulus);
Integer two=m.ConvertIn(2), three=m.ConvertIn(3);
Integer A=m.ConvertIn(P), B, C, p, d=n, e, r, t, T, U;
while (d!=1)
{
p = d;
unsigned int b = WORD_BITS * p.WordCount();
Integer alpha = (Integer(5)<<(2*b-2)).SquareRoot() - Integer::Power2(b-1);
r = (p*alpha)>>b;
e = d-r;
B = A;
C = two;
d = r;
while (d!=e)
{
if (d<e)
{
swap(d, e);
swap(A, B);
}
unsigned int dm2 = d[0], em2 = e[0];
unsigned int dm3 = d%3, em3 = e%3;
// if ((dm6+em6)%3 == 0 && d <= e + (e>>2))
if ((dm3+em3==0 || dm3+em3==3) && (t = e, t >>= 2, t += e, d <= t))
{
// #1
// t = (d+d-e)/3;
// t = d; t += d; t -= e; t /= 3;
// e = (e+e-d)/3;
// e += e; e -= d; e /= 3;
// d = t;
// t = (d+e)/3
t = d; t += e; t /= 3;
e -= t;
d -= t;
T = f(A, B, C);
U = f(T, A, B);
B = f(T, B, A);
A = U;
continue;
}
// if (dm6 == em6 && d <= e + (e>>2))
if (dm3 == em3 && dm2 == em2 && (t = e, t >>= 2, t += e, d <= t))
{
// #2
// d = (d-e)>>1;
d -= e; d >>= 1;
B = f(A, B, C);
A = X2(A);
continue;
}
// if (d <= (e<<2))
if (d <= (t = e, t <<= 2))
{
// #3
d -= e;
C = f(A, B, C);
swap(B, C);
continue;
}
if (dm2 == em2)
{
// #4
// d = (d-e)>>1;
d -= e; d >>= 1;
B = f(A, B, C);
A = X2(A);
continue;
}
if (dm2 == 0)
{
// #5
d >>= 1;
C = f(A, C, B);
A = X2(A);
continue;
}
if (dm3 == 0)
{
// #6
// d = d/3 - e;
d /= 3; d -= e;
T = X2(A);
C = f(T, f(A, B, C), C);
swap(B, C);
A = f(T, A, A);
continue;
}
if (dm3+em3==0 || dm3+em3==3)
{
// #7
// d = (d-e-e)/3;
d -= e; d -= e; d /= 3;
T = f(A, B, C);
B = f(T, A, B);
A = X3(A);
continue;
}
if (dm3 == em3)
{
// #8
// d = (d-e)/3;
d -= e; d /= 3;
T = f(A, B, C);
C = f(A, C, B);
B = T;
A = X3(A);
continue;
}
assert(em2 == 0);
// #9
e >>= 1;
C = f(C, B, A);
B = X2(B);
}
A = f(A, B, C);
}
#undef f
#undef X2
#undef X3
return m.ConvertOut(A);
}
*/
Integer InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q, const Integer &u)
{
Integer d = (m*m-4);
Integer p2 = p-Jacobi(d,p);
Integer q2 = q-Jacobi(d,q);
return CRT(Lucas(EuclideanMultiplicativeInverse(e,p2), m, p), p, Lucas(EuclideanMultiplicativeInverse(e,q2), m, q), q, u);
}
Integer InverseLucas(const Integer &e, const Integer &m, const Integer &p, const Integer &q)
{
return InverseLucas(e, m, p, q, EuclideanMultiplicativeInverse(p, q));
}
unsigned int FactoringWorkFactor(unsigned int n)
{
// extrapolated from the table in Odlyzko's "The Future of Integer Factorization"
@@ -1037,91 +753,5 @@ unsigned int DiscreteLogWorkFactor(unsigned int n)
else return (unsigned int)(2.4 * pow((double)n, 1.0/3.0) * pow(log(double(n)), 2.0/3.0) - 5);
}
// ********************************************************
void PrimeAndGenerator::Generate(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned int qbits)
{
// no prime exists for delta = -1, qbits = 4, and pbits = 5
assert(qbits > 4);
assert(pbits > qbits);
if (qbits+1 == pbits)
{
Integer minP = Integer::Power2(pbits-1);
Integer maxP = Integer::Power2(pbits) - 1;
bool success = false;
while (!success)
{
p.Randomize(rng, minP, maxP, Integer::ANY, 6+5*delta, 12);
PrimeSieve sieve(p, STDMIN(p+PrimeSearchInterval(maxP)*12, maxP), 12, delta);
while (sieve.NextCandidate(p))
{
assert(IsSmallPrime(p) || SmallDivisorsTest(p));
q = (p-delta) >> 1;
assert(IsSmallPrime(q) || SmallDivisorsTest(q));
if (FastProbablePrimeTest(q) && FastProbablePrimeTest(p) && IsPrime(q) && IsPrime(p))
{
success = true;
break;
}
}
}
if (delta == 1)
{
// find g such that g is a quadratic residue mod p, then g has order q
// g=4 always works, but this way we get the smallest quadratic residue (other than 1)
for (g=2; Jacobi(g, p) != 1; ++g) {}
// contributed by Walt Tuvell: g should be the following according to the Law of Quadratic Reciprocity
assert((p%8==1 || p%8==7) ? g==2 : (p%12==1 || p%12==11) ? g==3 : g==4);
}
else
{
assert(delta == -1);
// find g such that g*g-4 is a quadratic non-residue,
// and such that g has order q
for (g=3; ; ++g)
if (Jacobi(g*g-4, p)==-1 && Lucas(q, g, p)==2)
break;
}
}
else
{
Integer minQ = Integer::Power2(qbits-1);
Integer maxQ = Integer::Power2(qbits) - 1;
Integer minP = Integer::Power2(pbits-1);
Integer maxP = Integer::Power2(pbits) - 1;
do
{
q.Randomize(rng, minQ, maxQ, Integer::PRIME);
} while (!p.Randomize(rng, minP, maxP, Integer::PRIME, delta%q, q));
// find a random g of order q
if (delta==1)
{
do
{
Integer h(rng, 2, p-2, Integer::ANY);
g = a_exp_b_mod_c(h, (p-1)/q, p);
} while (g <= 1);
assert(a_exp_b_mod_c(g, q, p)==1);
}
else
{
assert(delta==-1);
do
{
Integer h(rng, 3, p-1, Integer::ANY);
if (Jacobi(h*h-4, p)==1)
continue;
g = Lucas((p+1)/q, h, p);
} while (g <= 2);
assert(Lucas(q, g, p) == 2);
}
}
}
NAMESPACE_END
-35
View File
@@ -19,10 +19,6 @@ void BuildPrimeTable();
// ************ primality testing ****************
// generate a provable prime
Integer MaurerProvablePrime(RandomNumberGenerator &rng, unsigned int bits);
Integer MihailescuProvablePrime(RandomNumberGenerator &rng, unsigned int bits);
bool IsSmallPrime(const Integer &p);
// returns true if p is divisible by some prime less than bound
@@ -32,10 +28,6 @@ bool TrialDivision(const Integer &p, unsigned bound);
// returns true if p is NOT divisible by small primes
bool SmallDivisorsTest(const Integer &p);
// These is no reason to use these two, use the ones below instead
bool IsFermatProbablePrime(const Integer &n, const Integer &b);
bool IsLucasProbablePrime(const Integer &n);
bool IsStrongProbablePrime(const Integer &n, const Integer &b);
bool IsStrongLucasProbablePrime(const Integer &n);
@@ -111,33 +103,6 @@ bool SolveModularQuadraticEquation(Integer &r1, Integer &r2, const Integer &a, c
unsigned int DiscreteLogWorkFactor(unsigned int bitlength);
unsigned int FactoringWorkFactor(unsigned int bitlength);
// ********************************************************
//! generator of prime numbers of special forms
class PrimeAndGenerator
{
public:
PrimeAndGenerator() {}
// generate a random prime p of the form 2*q+delta, where delta is 1 or -1 and q is also prime
// Precondition: pbits > 5
// warning: this is slow, because primes of this form are harder to find
PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits)
{Generate(delta, rng, pbits, pbits-1);}
// generate a random prime p of the form 2*r*q+delta, where q is also prime
// Precondition: qbits > 4 && pbits > qbits
PrimeAndGenerator(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits)
{Generate(delta, rng, pbits, qbits);}
void Generate(signed int delta, RandomNumberGenerator &rng, unsigned int pbits, unsigned qbits);
const Integer& Prime() const {return p;}
const Integer& SubPrime() const {return q;}
const Integer& Generator() const {return g;}
private:
Integer p, q, g;
};
NAMESPACE_END
#endif
-8
View File
@@ -35,7 +35,6 @@
#include "integer.h"
#include "filters.h"
#include "eprecomp.h"
#include "fips140.h"
#include "argnames.h"
#include <memory>
@@ -852,13 +851,6 @@ public:
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &params)
{
BASE::GenerateRandom(rng, params);
if (FIPS_140_2_ComplianceEnabled())
{
typename SIGNATURE_SCHEME::Signer signer(*this);
typename SIGNATURE_SCHEME::Verifier verifier(signer);
SignaturePairwiseConsistencyTest_FIPS_140_Only(signer, verifier);
}
}
};
+1 -70
View File
@@ -8,39 +8,12 @@
#include "nbtheory.h"
#include "sha.h"
#include "algparam.h"
#include "fips140.h"
#ifndef NDEBUG
#include "pssr.h"
#endif
#include "oaep.cpp"
NAMESPACE_BEGIN(CryptoPP)
#ifndef NDEBUG
void RSA_TestInstantiations()
{
RSASS<PKCS1v15, SHA>::Verifier x1(1, 1);
RSASS<PKCS1v15, SHA>::Signer x2(NullRNG(), 1);
RSASS<PKCS1v15, SHA>::Verifier x3(x2);
RSASS<PKCS1v15, SHA>::Verifier x4(x2.GetKey());
RSASS<PSS, SHA>::Verifier x5(x3);
#ifndef __MWERKS__
RSASS<PSSR, SHA>::Signer x6 = x2;
x3 = x2;
x6 = x2;
#endif
RSAES<PKCS1v15>::Encryptor x7(x2);
#ifndef __GNUC__
RSAES<PKCS1v15>::Encryptor x8(x3);
#endif
RSAES<OAEP<SHA> >::Encryptor x9(x2);
x4 = x2.GetKey();
}
#endif
template class OAEP<SHA>;
OID RSAFunction::GetAlgorithmID() const
@@ -66,18 +39,9 @@ void RSAFunction::DEREncodeKey(BufferedTransformation &bt) const
Integer RSAFunction::ApplyFunction(const Integer &x) const
{
DoQuickSanityCheck();
return a_exp_b_mod_c(x, m_e, m_n);
}
bool RSAFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
{
bool pass = true;
pass = pass && m_n > Integer::One() && m_n.IsOdd();
pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n;
return pass;
}
bool RSAFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
return GetValueHelper(this, name, valueType, pValue).Assignable()
@@ -130,17 +94,6 @@ void InvertibleRSAFunction::GenerateRandom(RandomNumberGenerator &rng, const Nam
m_dq = m_d % (m_q-1);
m_n = m_p * m_q;
m_u = m_q.InverseMod(m_p);
if (FIPS_140_2_ComplianceEnabled())
{
RSASS<PKCS1v15, SHA>::Signer signer(*this);
RSASS<PKCS1v15, SHA>::Verifier verifier(signer);
SignaturePairwiseConsistencyTest_FIPS_140_Only(signer, verifier);
RSAES<OAEP<SHA> >::Decryptor decryptor(*this);
RSAES<OAEP<SHA> >::Encryptor encryptor(decryptor);
EncryptionPairwiseConsistencyTest_FIPS_140_Only(encryptor, decryptor);
}
}
void InvertibleRSAFunction::Initialize(RandomNumberGenerator &rng, unsigned int keybits, const Integer &e)
@@ -215,7 +168,6 @@ void InvertibleRSAFunction::DEREncodeKey(BufferedTransformation &bt) const
Integer InvertibleRSAFunction::CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
{
DoQuickSanityCheck();
ModularArithmetic modn(m_n);
Integer r(rng, Integer::One(), m_n - Integer::One());
Integer re = modn.Exponentiate(r, m_e);
@@ -229,27 +181,6 @@ Integer InvertibleRSAFunction::CalculateInverse(RandomNumberGenerator &rng, cons
return y;
}
bool InvertibleRSAFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
{
bool pass = RSAFunction::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_d > Integer::One() && m_d.IsOdd() && m_d < m_n;
pass = pass && m_dp > Integer::One() && m_dp.IsOdd() && m_dp < m_p;
pass = pass && m_dq > Integer::One() && m_dq.IsOdd() && m_dq < m_q;
pass = pass && m_u.IsPositive() && m_u < m_p;
if (level >= 1)
{
pass = pass && m_p * m_q == m_n;
pass = pass && m_e*m_d % LCM(m_p-1, m_q-1) == 1;
pass = pass && m_dp == m_d%(m_p-1) && m_dq == m_d%(m_q-1);
pass = pass && m_u * m_q % m_p == 1;
}
if (level >= 2)
pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
return pass;
}
bool InvertibleRSAFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
{
return GetValueHelper<RSAFunction>(this, name, valueType, pValue).Assignable()