crypto/weakcrypto/source/asymmetric/rsafunction.h
changeset 0 2c201484c85f
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2003-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 * RSAFunction class implementation
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 /**
       
    23  @file 
       
    24  @internalTechnology
       
    25 */
       
    26  
       
    27 #ifndef __RSAFUNCTION_H__
       
    28 #define __RSAFUNCTION_H__
       
    29 
       
    30 #include <e32base.h>
       
    31 
       
    32 class TInteger;
       
    33 
       
    34 class CRSAPublicKey;
       
    35 class CRSAPrivateKey;
       
    36 class CRSAPrivateKeyCRT;
       
    37 
       
    38 class RSAFunction
       
    39 	{
       
    40 public:
       
    41 	static inline TBool IsInputValid(const TInteger& aInput, 
       
    42 		const TInteger& aModulus);
       
    43 	static inline void IsInputValidL(const TInteger& aInput, 
       
    44 		const TInteger& aModulus);
       
    45 	static void EncryptL(const CRSAPublicKey& aPublicKey,
       
    46 		const TInteger& aInput, RInteger& aOutput);
       
    47 	static void DecryptL(const CRSAPrivateKey& aPrivateKey,
       
    48 		const TInteger& aInput, RInteger& aOutput);
       
    49 	static void SignL(const CRSAPrivateKey& aPrivateKey,
       
    50 		const TInteger& aInput, RInteger& aOutput);
       
    51 	static void VerifyL(const CRSAPublicKey& aPublicKey,
       
    52 		const TInteger& aInput, RInteger& aOutput);
       
    53 private:
       
    54 	static void FunctionL(const TInteger& aModulus, const TInteger& aExponent,
       
    55 		const TInteger& aBase, RInteger& aOutput);
       
    56 	static void FunctionCRTL(const CRSAPrivateKeyCRT& aPrivateKey,
       
    57 		const TInteger& aInput, RInteger& aOutput);
       
    58 private:
       
    59 	RSAFunction(void);
       
    60 	};
       
    61 
       
    62 /** Computes whether a given message representative is within the valid bounds
       
    63  * for a given modulus, i.e. whether the message is representative within [0,n-1].
       
    64  * @param aInput The message representative.
       
    65  * @param aModulus The modulus.
       
    66  * @return TBool representing whether or not the message representative is
       
    67  * valid.
       
    68  */
       
    69 TBool RSAFunction::IsInputValid(const TInteger& aInput, 
       
    70 	const TInteger& aModulus)
       
    71 	{
       
    72 	//See HAC 8.3 1.b
       
    73 	//Message (input) must be in the interval [0,n-1] (inclusive)
       
    74 	if( aInput.IsNegative() || aInput >= aModulus )
       
    75 		return EFalse;
       
    76 	else
       
    77 		return ETrue;
       
    78 	}
       
    79 
       
    80 void RSAFunction::IsInputValidL(const TInteger& aInput,
       
    81 	const TInteger& aModulus)
       
    82 	{
       
    83 	if(!IsInputValid(aInput, aModulus))
       
    84 		User::Leave(KErrArgument);
       
    85 	}
       
    86 
       
    87 #endif