cryptoservices/filebasedcertificateandkeystores/source/keystore/Server/keystreamutils.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "keystreamutils.h"
       
    20 #include "asymmetrickeys.h"
       
    21 
       
    22 void ExternalizeL(const CRSAPublicKey& aKey, RWriteStream& aStream)
       
    23 	{
       
    24 	aStream << aKey.N() << aKey.E();
       
    25 	}
       
    26 
       
    27 void ExternalizeL(const CRSAPrivateKey& aData, RWriteStream& aStream)
       
    28 	{
       
    29 	aStream << aData.N();
       
    30 	
       
    31 	// Check the incoming RSA private key (standard or CRT)	
       
    32 	TRSAPrivateKeyType keyType = aData.PrivateKeyType();
       
    33 	aStream.WriteInt32L((TInt32)keyType);
       
    34 
       
    35 	if (EStandard==keyType)
       
    36 		{
       
    37 		const CRSAPrivateKeyStandard& key = static_cast<const CRSAPrivateKeyStandard&>(aData);
       
    38 		aStream << key.D();
       
    39 		}
       
    40 	else if (EStandardCRT==keyType)
       
    41 		{
       
    42 		const CRSAPrivateKeyCRT& key = static_cast<const CRSAPrivateKeyCRT&>(aData);
       
    43 		aStream << key.P() << key.Q() << key.DP() << key.DQ() << key.QInv();
       
    44 		}
       
    45 	else
       
    46 		{
       
    47 		User::Leave(KErrNotSupported);
       
    48 		}
       
    49 	}
       
    50 
       
    51 void ExternalizeL(const CDSAPublicKey& aKey, RWriteStream& aStream)
       
    52 	{
       
    53 	aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.Y();
       
    54 	}
       
    55 
       
    56 void ExternalizeL(const CDSAPrivateKey& aKey, RWriteStream& aStream)
       
    57 	{
       
    58 	aStream << aKey.P() << aKey.Q() << aKey.G() << aKey.X();
       
    59 	}
       
    60 
       
    61 void CreateL(RReadStream& aStream, CRSAPublicKey*& aOut)
       
    62 	{
       
    63 	RInteger N, keyPublicExp;
       
    64 	CreateLC(aStream, N);
       
    65 	CreateLC(aStream, keyPublicExp);
       
    66 
       
    67 	aOut = CRSAPublicKey::NewL(N, keyPublicExp);
       
    68 
       
    69 	CleanupStack::Pop(2, &N); // keyPublicExp, N
       
    70 	}
       
    71 
       
    72 void CreateL(RReadStream& aStream, CRSAPrivateKey*& aOut)
       
    73 	{
       
    74 	RInteger privateN;
       
    75 	CreateLC(aStream, privateN);
       
    76 
       
    77 	TRSAPrivateKeyType keyType = EStandard;
       
    78 	keyType = (TRSAPrivateKeyType)aStream.ReadInt32L();
       
    79 
       
    80 	if (EStandard==keyType)
       
    81 		{
       
    82 		RInteger D;
       
    83 		CreateLC(aStream, D);
       
    84 	
       
    85 		aOut = CRSAPrivateKeyStandard::NewL(privateN, D);
       
    86 
       
    87 		CleanupStack::Pop(&D);
       
    88 		}
       
    89 	else if (EStandardCRT==keyType)
       
    90 		{
       
    91 		RInteger p, q, dP, dQ, qInv;
       
    92 		CreateLC(aStream, p);
       
    93 		CreateLC(aStream, q);
       
    94 		CreateLC(aStream, dP);
       
    95 		CreateLC(aStream, dQ);
       
    96 		CreateLC(aStream, qInv);
       
    97 				
       
    98 		aOut = CRSAPrivateKeyCRT::NewL(privateN, p, q, dP, dQ, qInv);
       
    99 		
       
   100 		CleanupStack::Pop(5, &p);
       
   101 		}
       
   102 	else
       
   103 		{
       
   104 		User::Leave(KErrNotSupported);
       
   105 		}
       
   106 
       
   107 	CleanupStack::Pop(&privateN);
       
   108 	}
       
   109 
       
   110 void CreateL(RReadStream& aStream, CDSAPublicKey*& aOut)
       
   111 	{
       
   112 	RInteger P, Q, G, Y;
       
   113 	CreateLC(aStream, P);
       
   114 	CreateLC(aStream, Q);
       
   115 	CreateLC(aStream, G);
       
   116 	CreateLC(aStream, Y);
       
   117 
       
   118 	aOut = CDSAPublicKey::NewL(P, Q, G, Y);
       
   119 
       
   120 	CleanupStack::Pop(4, &P);
       
   121 	}
       
   122 
       
   123 void CreateL(RReadStream& aStream, CDSAPrivateKey*& aOut)
       
   124 	{
       
   125 	RInteger P, Q, G, X;
       
   126 	CreateLC(aStream, P);
       
   127 	CreateLC(aStream, Q);
       
   128 	CreateLC(aStream, G);
       
   129 	CreateLC(aStream, X);
       
   130 
       
   131 	aOut = CDSAPrivateKey::NewL(P, Q, G, X);
       
   132 
       
   133 	CleanupStack::Pop(4, &P);
       
   134 	}