os/security/crypto/weakcryptospi/test/tasymmetric/tvectorutils.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tasymmetric/tvectorutils.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,243 @@
     1.4 +/*
     1.5 +* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "tvectorutils.h"
    1.23 +#include "t_input.h"
    1.24 +
    1.25 +//	RSA
    1.26 +_LIT8(KModStart, "<modulus>");
    1.27 +_LIT8(KModEnd, "</modulus>");
    1.28 +_LIT8(KPubExpStart, "<publicExponent>");
    1.29 +_LIT8(KPubExpEnd, "</publicExponent>");
    1.30 +_LIT8(KPrivExpStart, "<privateExponent>");
    1.31 +_LIT8(KPrivExpEnd, "</privateExponent>");
    1.32 +_LIT8(KPStart, "<P>");
    1.33 +_LIT8(KPEnd, "</P>");
    1.34 +_LIT8(KQStart, "<Q>");
    1.35 +_LIT8(KQEnd, "</Q>");
    1.36 +_LIT8(KdPStart, "<dP>");
    1.37 +_LIT8(KdPEnd, "</dP>");
    1.38 +_LIT8(KdQStart, "<dQ>");
    1.39 +_LIT8(KdQEnd, "</dQ>");
    1.40 +_LIT8(KqInvStart, "<qInv>");
    1.41 +_LIT8(KqInvEnd, "</qInv>");
    1.42 +
    1.43 +_LIT8(KTrueVal, "ETrue");
    1.44 +_LIT8(KFalseVal, "EFalse");
    1.45 +
    1.46 +RInteger VectorUtils::ParseIntegerL(const TDesC8& aDes)
    1.47 +    {
    1.48 +	HBufC8* buf = ParseBinaryL(aDes);
    1.49 +	CleanupStack::PushL(buf);
    1.50 +    RInteger result = RInteger::NewL(*buf);
    1.51 +    CleanupStack::PopAndDestroy(buf);
    1.52 +
    1.53 +    return result;
    1.54 +    }
    1.55 +
    1.56 +HBufC8* VectorUtils::ParseBinaryL(const TDesC8& aDes)
    1.57 +{
    1.58 +    __ASSERT_ALWAYS(aDes.Length() % 2 == 0, User::Panic(_L("ParseBinaryL"), KErrArgument));
    1.59 +	int length = aDes.Length() / 2;
    1.60 +	HBufC8* buf = HBufC8::NewL(length);
    1.61 +    TPtr8 ptr = buf->Des();
    1.62 +	ptr.SetLength(length);
    1.63 +
    1.64 +    for (TInt i = 0 ; i < aDes.Length() ; i += 2)
    1.65 +        {
    1.66 +        TUint8 tmp;
    1.67 +        tmp=(TUint8)(aDes[i]-(aDes[i]>'9'?('A'-10):'0'));
    1.68 +        tmp*=16;
    1.69 +        tmp|=(TUint8)(aDes[i+1]-(aDes[i+1]>'9'?('A'-10):'0'));
    1.70 +		ptr[i / 2] = tmp;
    1.71 +        }
    1.72 +
    1.73 +	return buf;
    1.74 +}
    1.75 +
    1.76 +// Print an Integer into hex, only works for positive integers
    1.77 +TDesC* VectorUtils::PrintIntegerL(const TInteger& aInt)
    1.78 +{
    1.79 +	HBufC8* binary = aInt.BufferLC();
    1.80 +	TDesC* result = PrintBinaryL(*binary);
    1.81 +	CleanupStack::PopAndDestroy(binary);
    1.82 +
    1.83 +	return result;
    1.84 +}
    1.85 +
    1.86 +// Print a binary string into hex
    1.87 +TDesC* VectorUtils::PrintBinaryL(const TDesC8& aData)
    1.88 +{
    1.89 +	int length = aData.Length() * 2;
    1.90 +	HBufC* buf = HBufC::NewL(length);
    1.91 +	TPtr ptr = buf->Des();
    1.92 +	ptr.SetLength(length);
    1.93 +
    1.94 +	for (int i = 0 ; i < aData.Length() ; ++i)
    1.95 +		{
    1.96 +		TUint8 val = aData[i];
    1.97 +		TUint8 n = (TUint8) ((val & 0xf0) >> 4);
    1.98 +		ptr[i * 2] = (TUint8) (n < 10 ? ('0' + n) : ('A' + n - 10));
    1.99 +		n = (TUint8) (val & 0x0f);
   1.100 +		ptr[i * 2 + 1] = (TUint8) (n < 10 ? ('0' + n) : ('A' + n - 10));
   1.101 +		}
   1.102 +
   1.103 +	return buf;
   1.104 +}
   1.105 +
   1.106 +TBool VectorUtils::ParseBoolL(const TDesC8& aDes)
   1.107 +    {
   1.108 +    TBool result = EFalse;
   1.109 +
   1.110 +    if (aDes == KTrueVal)
   1.111 +        result = ETrue;
   1.112 +    else if (aDes != KFalseVal)
   1.113 +        User::Leave(KErrArgument);
   1.114 +
   1.115 +    return result;
   1.116 +    }
   1.117 +
   1.118 +CRSAPublicKey* VectorUtils::ReadRSAPublicKeyL(const TDesC8& aData)
   1.119 +    {
   1.120 +	TPtrC8 modIn = Input::ParseElement(aData, KModStart, KModEnd);
   1.121 +    RInteger mod = ParseIntegerL(modIn);
   1.122 +	CleanupStack::PushL(mod);
   1.123 +
   1.124 +	TPtrC8 pubExpIn = Input::ParseElement(aData, KPubExpStart, KPubExpEnd);
   1.125 +	RInteger pubExp = ParseIntegerL(pubExpIn);
   1.126 +	CleanupStack::PushL(pubExp);
   1.127 +
   1.128 +	CRSAPublicKey* result = CRSAPublicKey::NewL(mod, pubExp);	
   1.129 +	CleanupStack::Pop(2, &mod);
   1.130 +	
   1.131 +    return result;
   1.132 +    }
   1.133 +
   1.134 +CRSAPrivateKeyStandard* VectorUtils::ReadRSAPrivateKeyL(const TDesC8& aData)
   1.135 +    {
   1.136 +	TPtrC8 modIn = Input::ParseElement(aData, KModStart, KModEnd);
   1.137 +    RInteger mod = ParseIntegerL(modIn);
   1.138 +	CleanupStack::PushL(mod);
   1.139 +
   1.140 +	TPtrC8 privExpIn = Input::ParseElement(aData, KPrivExpStart, KPrivExpEnd);
   1.141 +	RInteger privExp = ParseIntegerL(privExpIn);
   1.142 +	CleanupStack::PushL(privExp);
   1.143 +
   1.144 +	CRSAPrivateKeyStandard* result  = CRSAPrivateKeyStandard::NewL(mod, privExp);
   1.145 +	CleanupStack::Pop(2, &mod);
   1.146 +	
   1.147 +    return result;
   1.148 +    }
   1.149 +
   1.150 +CRSAPrivateKeyCRT* VectorUtils::ReadRSAPrivateKeyCRTL(const TDesC8& aData)
   1.151 +    {
   1.152 +	TPtrC8 modIn = Input::ParseElement(aData, KModStart, KModEnd);
   1.153 +    RInteger mod = ParseIntegerL(modIn);
   1.154 +	CleanupStack::PushL(mod);
   1.155 +
   1.156 +	TPtrC8 pIn = Input::ParseElement(aData, KPStart, KPEnd);
   1.157 +	RInteger P = ParseIntegerL(pIn);
   1.158 +	CleanupStack::PushL(P);
   1.159 +
   1.160 +	TPtrC8 qIn = Input::ParseElement(aData, KQStart, KQEnd);
   1.161 +	RInteger Q = ParseIntegerL(qIn);
   1.162 +	CleanupStack::PushL(Q);
   1.163 +	
   1.164 +	TPtrC8 dpIn = Input::ParseElement(aData, KdPStart, KdPEnd);
   1.165 +	RInteger dP = ParseIntegerL(dpIn);
   1.166 +	CleanupStack::PushL(dP);
   1.167 +	
   1.168 +	TPtrC8 dqIn = Input::ParseElement(aData, KdQStart, KdQEnd);
   1.169 +	RInteger dQ = ParseIntegerL(dqIn);
   1.170 +	CleanupStack::PushL(dQ);
   1.171 +
   1.172 +	TPtrC8 qInvIn = Input::ParseElement(aData, KqInvStart, KqInvEnd);
   1.173 +	RInteger qInv = ParseIntegerL(qInvIn);
   1.174 +	CleanupStack::PushL(qInv);
   1.175 +	
   1.176 +	CRSAPrivateKeyCRT* privKey  = CRSAPrivateKeyCRT::NewL(mod, P, Q, dP, dQ, qInv);
   1.177 +	CleanupStack::Pop(6, &mod);
   1.178 +	
   1.179 +    return (privKey);
   1.180 +    }
   1.181 +
   1.182 +CDSAPublicKey* VectorUtils::ReadDSAPublicKeyL(const TDesC8& aData)
   1.183 +    {
   1.184 +	TPtrC8 pIn = Input::ParseElement(aData, _L8("<p>"));
   1.185 +    RInteger p = ParseIntegerL(pIn);
   1.186 +	CleanupStack::PushL(p);
   1.187 +
   1.188 +	TPtrC8 qIn = Input::ParseElement(aData, _L8("<q>"));
   1.189 +    RInteger q = ParseIntegerL(qIn);
   1.190 +	CleanupStack::PushL(q);
   1.191 +
   1.192 +	TPtrC8 gIn = Input::ParseElement(aData, _L8("<g>"));
   1.193 +    RInteger g = ParseIntegerL(gIn);
   1.194 +	CleanupStack::PushL(g);
   1.195 +
   1.196 +	TPtrC8 yIn = Input::ParseElement(aData, _L8("<y>"));
   1.197 +    RInteger y = ParseIntegerL(yIn);
   1.198 +	CleanupStack::PushL(y);
   1.199 +
   1.200 +	CDSAPublicKey* result = CDSAPublicKey::NewLC(p, q, g, y);		
   1.201 +	CleanupStack::Pop(5, &p);
   1.202 +
   1.203 +    return result;
   1.204 +    }
   1.205 +
   1.206 +CDSAPrivateKey* VectorUtils::ReadDSAPrivateKeyL(const TDesC8& aData)
   1.207 +    {
   1.208 +	TPtrC8 pIn = Input::ParseElement(aData, _L8("<p>"));
   1.209 +    RInteger p = ParseIntegerL(pIn);
   1.210 +	CleanupStack::PushL(p);
   1.211 +
   1.212 +	TPtrC8 qIn = Input::ParseElement(aData, _L8("<q>"));
   1.213 +    RInteger q = ParseIntegerL(qIn);
   1.214 +	CleanupStack::PushL(q);
   1.215 +
   1.216 +	TPtrC8 gIn = Input::ParseElement(aData, _L8("<g>"));
   1.217 +    RInteger g = ParseIntegerL(gIn);
   1.218 +	CleanupStack::PushL(g);
   1.219 +
   1.220 +	TPtrC8 xIn = Input::ParseElement(aData, _L8("<x>"));
   1.221 +    RInteger x = ParseIntegerL(xIn);
   1.222 +	CleanupStack::PushL(x);
   1.223 +
   1.224 +	CDSAPrivateKey* result = CDSAPrivateKey::NewLC(p, q, g, x);
   1.225 +	CleanupStack::Pop(result);
   1.226 +
   1.227 +	CleanupStack::Pop(4, &p);
   1.228 +
   1.229 +    return result;
   1.230 +    }
   1.231 +
   1.232 +CDSASignature* VectorUtils::ReadDSASignatureL(const TDesC8& aData)
   1.233 +    {
   1.234 +	TPtrC8 rIn = Input::ParseElement(aData, _L8("<r>"));
   1.235 +    RInteger r = ParseIntegerL(rIn);
   1.236 +	CleanupStack::PushL(r);
   1.237 +
   1.238 +	TPtrC8 sIn = Input::ParseElement(aData, _L8("<s>"));
   1.239 +    RInteger s = ParseIntegerL(sIn);
   1.240 +	CleanupStack::PushL(s);
   1.241 +
   1.242 +	CDSASignature* result = CDSASignature::NewLC(r, s);	
   1.243 +	CleanupStack::Pop(3, &r);
   1.244 +
   1.245 +    return result;
   1.246 +    }