sl@0: /* sl@0: * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "../common/inlines.h" sl@0: #include "../bigint/mont.h" sl@0: #include "dsakeypairshim.h" sl@0: sl@0: const TUint SHASIZE = 20; sl@0: const TUint KMinPrimeLength = 512; sl@0: const TUint KMaxPrimeLength = 1024; sl@0: const TUint KPrimeLengthMultiple = 64; sl@0: sl@0: /* CDSAParameters */ sl@0: sl@0: EXPORT_C const TInteger& CDSAParameters::P(void) const sl@0: { sl@0: return iP; sl@0: } sl@0: sl@0: EXPORT_C const TInteger& CDSAParameters::Q(void) const sl@0: { sl@0: return iQ; sl@0: } sl@0: sl@0: EXPORT_C const TInteger& CDSAParameters::G(void) const sl@0: { sl@0: return iG; sl@0: } sl@0: sl@0: EXPORT_C CDSAParameters::~CDSAParameters(void) sl@0: { sl@0: iP.Close(); sl@0: iQ.Close(); sl@0: iG.Close(); sl@0: } sl@0: sl@0: EXPORT_C CDSAParameters* CDSAParameters::NewL(RInteger& aP, RInteger& aQ, sl@0: RInteger& aG) sl@0: { sl@0: CDSAParameters* me = new (ELeave) CDSAParameters(aP, aQ, aG); sl@0: return (me); sl@0: } sl@0: sl@0: EXPORT_C TBool CDSAParameters::ValidatePrimesL(const CDSAPrimeCertificate& aCert) sl@0: const sl@0: { sl@0: TBool result = EFalse; sl@0: RInteger p; sl@0: RInteger q; sl@0: //Regenerate primes using aCert's seed and counter sl@0: TUint counter = aCert.Counter(); sl@0: if(!CDSAParameters::GeneratePrimesL(aCert.Seed(), counter, p, sl@0: P().BitCount(), q, ETrue)) sl@0: { sl@0: return result; sl@0: } sl@0: //this doesn't leave, no need to push p and q sl@0: if(p == P() && q == Q() && counter == aCert.Counter()) sl@0: { sl@0: result = ETrue; sl@0: } sl@0: p.Close(); sl@0: q.Close(); sl@0: return result; sl@0: } sl@0: sl@0: EXPORT_C TBool CDSAParameters::ValidPrimeLength(TUint aPrimeBits) sl@0: { sl@0: return (aPrimeBits >= KMinPrimeLength && sl@0: aPrimeBits <= KMaxPrimeLength && sl@0: aPrimeBits % KPrimeLengthMultiple == 0); sl@0: } sl@0: sl@0: EXPORT_C CDSAParameters::CDSAParameters(RInteger& aP, RInteger& aQ, sl@0: RInteger& aG) : iP(aP), iQ(aQ), iG(aG) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CDSAParameters::CDSAParameters(void) sl@0: { sl@0: } sl@0: sl@0: TBool CDSAParameters::GeneratePrimesL(const TDesC8& aSeed, TUint& aCounter, sl@0: RInteger& aP, TUint aL, RInteger& aQ, TBool aUseInputCounter) sl@0: { sl@0: //This follows the steps in FIPS 186-2 sl@0: //See DSS Appendix 2.2 sl@0: //Note. Step 1 is performed prior to calling GeneratePrimesL, so that this sl@0: //routine can be used for both generation and validation. sl@0: //Step 1. Choose an arbitrary sequence of at least 160 bits and call it sl@0: //SEED. Let g be the length of SEED in bits. sl@0: sl@0: if(!CDSAParameters::ValidPrimeLength(aL)) sl@0: { sl@0: User::Leave(KErrNotSupported); sl@0: } sl@0: sl@0: CSHA1* sha1 = CSHA1::NewL(); sl@0: CleanupStack::PushL(sha1); sl@0: sl@0: HBufC8* seedBuf = aSeed.AllocLC(); sl@0: TPtr8 seed = seedBuf->Des(); sl@0: TUint gBytes = aSeed.Size(); sl@0: //Note that the DSS's g = BytesToBits(gBytes) ie. the number of random bits sl@0: //in the seed. sl@0: //This function has made the assumption (for ease of computation) that g%8 sl@0: //is 0. Ie the seed is a whole number of random bytes. sl@0: TBuf8 U; sl@0: TBuf8 temp; sl@0: const TUint n = (aL-1)/160; sl@0: const TUint b = (aL-1)%160; sl@0: HBufC8* Wbuf = HBufC8::NewMaxLC((n+1) * SHASIZE); sl@0: TUint8* W = const_cast(Wbuf->Ptr()); sl@0: sl@0: U.Copy(sha1->Final(seed)); sl@0: sl@0: //Step 2. U = SHA-1[SEED] XOR SHA-1[(SEED+1) mod 2^g] sl@0: for(TInt i=gBytes - 1, carry=ETrue; i>=0 && carry; i--) sl@0: { sl@0: //!++(TUint) adds one to the current word which if it overflows to zero sl@0: //sets carry to 1 thus letting the loop continue. It's a poor man's sl@0: //multi-word addition. Swift eh? sl@0: carry = !++(seed[i]); sl@0: } sl@0: sl@0: temp.Copy(sha1->Final(seed)); sl@0: XorBuf(const_cast(U.Ptr()), temp.Ptr(), SHASIZE); sl@0: sl@0: //Step 3. Form q from U by setting the most significant bit (2^159) sl@0: //and the least significant bit to 1. sl@0: U[0] |= 0x80; sl@0: U[SHASIZE-1] |= 1; sl@0: sl@0: aQ = RInteger::NewL(U); sl@0: CleanupStack::PushL(aQ); sl@0: sl@0: //Step 4. Use a robust primality testing algo to test if q is prime sl@0: //The robust part is the calling codes problem. This will use whatever sl@0: //random number generator you set for the thread. To attempt FIPS 186-2 sl@0: //compliance, set a FIPS 186-2 compliant RNG. sl@0: if( !aQ.IsPrimeL() ) sl@0: { sl@0: //Step 5. If not exit and get a new seed sl@0: CleanupStack::PopAndDestroy(&aQ); sl@0: CleanupStack::PopAndDestroy(Wbuf); sl@0: CleanupStack::PopAndDestroy(seedBuf); sl@0: CleanupStack::PopAndDestroy(sha1); sl@0: return EFalse; sl@0: } sl@0: sl@0: TUint counterEnd = aUseInputCounter ? aCounter+1 : 4096; sl@0: sl@0: //Step 6. Let counter = 0 and offset = 2 sl@0: //Note 1. that the DSS speaks of SEED + offset + k because they always sl@0: //refer to a constant SEED. We update our seed as we go so the offset sl@0: //variable has already been added to seed in the previous iterations. sl@0: //Note 2. We've already added 1 to our seed, so the first time through this sl@0: //the offset in DSS speak will be 2. sl@0: for(TUint counter=0; counter < counterEnd; counter++) sl@0: { sl@0: //Step 7. For k=0, ..., n let sl@0: // Vk = SHA-1[(SEED + offset + k) mod 2^g] sl@0: //I'm storing the Vk's inside of a big W buffer. sl@0: for(TUint k=0; k<=n; k++) sl@0: { sl@0: for(TInt i=gBytes-1, carry=ETrue; i>=0 && carry; i--) sl@0: { sl@0: carry = !++(seed[i]); sl@0: } sl@0: if(!aUseInputCounter || counter == aCounter) sl@0: { sl@0: TPtr8 Wptr(W+(n-k)*SHASIZE, gBytes); sl@0: Wptr.Copy(sha1->Final(seed)); sl@0: } sl@0: } sl@0: if(!aUseInputCounter || counter == aCounter) sl@0: { sl@0: //Step 8. Let W be the integer... and let X = W + 2^(L-1) sl@0: const_cast((*Wbuf)[SHASIZE - 1 - b/8]) |= 0x80; sl@0: TPtr8 Wptr(W + SHASIZE - 1 - b/8, aL/8, aL/8); sl@0: RInteger X = RInteger::NewL(Wptr); sl@0: CleanupStack::PushL(X); sl@0: //Step 9. Let c = X mod 2q and set p = X - (c-1) sl@0: RInteger twoQ = aQ.TimesL(TInteger::Two()); sl@0: CleanupStack::PushL(twoQ); sl@0: RInteger c = X.ModuloL(twoQ); sl@0: CleanupStack::PushL(c); sl@0: --c; sl@0: aP = X.MinusL(c); sl@0: CleanupStack::PopAndDestroy(3, &X); //twoQ, c, X sl@0: CleanupStack::PushL(aP); sl@0: sl@0: //Step 10 and 11: if p >= 2^(L-1) and p is prime sl@0: if( aP.Bit(aL-1) && aP.IsPrimeL() ) sl@0: { sl@0: aCounter = counter; sl@0: CleanupStack::Pop(&aP); sl@0: CleanupStack::Pop(&aQ); sl@0: CleanupStack::PopAndDestroy(Wbuf); sl@0: CleanupStack::PopAndDestroy(seedBuf); sl@0: CleanupStack::PopAndDestroy(sha1); sl@0: return ETrue; sl@0: } sl@0: CleanupStack::PopAndDestroy(&aP); sl@0: } sl@0: } sl@0: CleanupStack::PopAndDestroy(&aQ); sl@0: CleanupStack::PopAndDestroy(Wbuf); sl@0: CleanupStack::PopAndDestroy(seedBuf); sl@0: CleanupStack::PopAndDestroy(sha1); sl@0: return EFalse; sl@0: } sl@0: sl@0: /* CDSAPublicKey */ sl@0: sl@0: EXPORT_C CDSAPublicKey* CDSAPublicKey::NewL(RInteger& aP, RInteger& aQ, sl@0: RInteger& aG, RInteger& aY) sl@0: { sl@0: CDSAPublicKey* self = new(ELeave) CDSAPublicKey(aP, aQ, aG, aY); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CDSAPublicKey* CDSAPublicKey::NewLC(RInteger& aP, RInteger& aQ, sl@0: RInteger& aG, RInteger& aY) sl@0: { sl@0: CDSAPublicKey* self = NewL(aP, aQ, aG, aY); sl@0: CleanupStack::PushL(self); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C const TInteger& CDSAPublicKey::Y(void) const sl@0: { sl@0: return iY; sl@0: } sl@0: sl@0: EXPORT_C CDSAPublicKey::CDSAPublicKey(void) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CDSAPublicKey::CDSAPublicKey(RInteger& aP, RInteger& aQ, RInteger& aG, sl@0: RInteger& aY) : CDSAParameters(aP, aQ, aG), iY(aY) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CDSAPublicKey::~CDSAPublicKey(void) sl@0: { sl@0: iY.Close(); sl@0: } sl@0: sl@0: /* CDSAPrivateKey */ sl@0: sl@0: EXPORT_C CDSAPrivateKey* CDSAPrivateKey::NewL(RInteger& aP, RInteger& aQ, sl@0: RInteger& aG, RInteger& aX) sl@0: { sl@0: CDSAPrivateKey* self = new(ELeave) CDSAPrivateKey(aP, aQ, aG, aX); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CDSAPrivateKey* CDSAPrivateKey::NewLC(RInteger& aP, RInteger& aQ, sl@0: RInteger& aG, RInteger& aX) sl@0: { sl@0: CDSAPrivateKey* self = NewL(aP, aQ, aG, aX); sl@0: CleanupStack::PushL(self); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C const TInteger& CDSAPrivateKey::X(void) const sl@0: { sl@0: return iX; sl@0: } sl@0: sl@0: EXPORT_C CDSAPrivateKey::CDSAPrivateKey(RInteger& aP, RInteger& aQ, RInteger& aG, sl@0: RInteger& aX) : CDSAParameters(aP, aQ, aG), iX(aX) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CDSAPrivateKey::~CDSAPrivateKey(void) sl@0: { sl@0: iX.Close(); sl@0: } sl@0: sl@0: /* CDSAKeyPair */ sl@0: sl@0: EXPORT_C CDSAKeyPair* CDSAKeyPair::NewL(TUint aKeyBits) sl@0: { sl@0: CDSAKeyPairShim* self = CDSAKeyPairShim::NewLC(aKeyBits); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CDSAKeyPair* CDSAKeyPair::NewLC(TUint aKeyBits) sl@0: { sl@0: CDSAKeyPairShim* self = CDSAKeyPairShim::NewLC(aKeyBits); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C const CDSAPublicKey& CDSAKeyPair::PublicKey(void) const sl@0: { sl@0: return *iPublic; sl@0: } sl@0: sl@0: EXPORT_C const CDSAPrivateKey& CDSAKeyPair::PrivateKey(void) const sl@0: { sl@0: return *iPrivate; sl@0: } sl@0: sl@0: EXPORT_C CDSAKeyPair::~CDSAKeyPair(void) sl@0: { sl@0: delete iPublic; sl@0: delete iPrivate; sl@0: delete iPrimeCertificate; sl@0: } sl@0: sl@0: EXPORT_C CDSAKeyPair::CDSAKeyPair(void) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C const CDSAPrimeCertificate& CDSAKeyPair::PrimeCertificate(void) const sl@0: { sl@0: return *iPrimeCertificate; sl@0: } sl@0: sl@0: /* CDSAPrimeCertificate */ sl@0: sl@0: EXPORT_C CDSAPrimeCertificate* CDSAPrimeCertificate::NewL(const TDesC8& aSeed, sl@0: TUint aCounter) sl@0: { sl@0: CDSAPrimeCertificate* self = NewLC(aSeed, aCounter); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C CDSAPrimeCertificate* CDSAPrimeCertificate::NewLC(const TDesC8& aSeed, sl@0: TUint aCounter) sl@0: { sl@0: CDSAPrimeCertificate* self = new(ELeave) CDSAPrimeCertificate(aCounter); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aSeed); sl@0: return self; sl@0: } sl@0: sl@0: EXPORT_C const TDesC8& CDSAPrimeCertificate::Seed(void) const sl@0: { sl@0: return *iSeed; sl@0: } sl@0: sl@0: EXPORT_C TUint CDSAPrimeCertificate::Counter(void) const sl@0: { sl@0: return iCounter; sl@0: } sl@0: sl@0: EXPORT_C CDSAPrimeCertificate::~CDSAPrimeCertificate(void) sl@0: { sl@0: delete const_cast(iSeed); sl@0: } sl@0: sl@0: void CDSAPrimeCertificate::ConstructL(const TDesC8& aSeed) sl@0: { sl@0: iSeed = aSeed.AllocL(); sl@0: } sl@0: sl@0: EXPORT_C CDSAPrimeCertificate::CDSAPrimeCertificate(TUint aCounter) sl@0: : iCounter(aCounter) sl@0: { sl@0: } sl@0: sl@0: // Over taken by shim version. so, exclude it from coverage. sl@0: #ifdef _BullseyeCoverage sl@0: #pragma suppress_warnings on sl@0: #pragma BullseyeCoverage off sl@0: #pragma suppress_warnings off sl@0: #endif sl@0: void CDSAKeyPair::ConstructL(TUint /*aPBits*/) sl@0: { sl@0: } sl@0: sl@0: // Unused exported and protected method can be excluded from coverage. sl@0: EXPORT_C CDSAPrimeCertificate::CDSAPrimeCertificate(void) sl@0: { sl@0: } sl@0: sl@0: EXPORT_C CDSAPrivateKey::CDSAPrivateKey(void) sl@0: { sl@0: }