sl@0: /* sl@0: * Copyright (c) 2003-2010 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: /** sl@0: @file sl@0: @internalComponent sl@0: @released sl@0: */ sl@0: sl@0: #ifndef __RSAFUNCTION_H__ sl@0: #define __RSAFUNCTION_H__ sl@0: sl@0: #include <e32base.h> sl@0: #include <cryptospi/keys.h> sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: class TInteger; sl@0: sl@0: class RSAFunction sl@0: { sl@0: public: sl@0: static inline TBool IsInputValid(const TInteger& aInput, sl@0: const TInteger& aModulus); sl@0: static inline void IsInputValidL(const TInteger& aInput, sl@0: const TInteger& aModulus); sl@0: static void EncryptL(const CKey& aPublicKey, sl@0: const TInteger& aInput, RInteger& aOutput); sl@0: static void DecryptL(const CKey& aPrivateKey, sl@0: const TInteger& aInput, RInteger& aOutput); sl@0: static void SignL(const CKey& aPrivateKey, sl@0: const TInteger& aInput, RInteger& aOutput); sl@0: static void VerifyL(const CKey& aPublicKey, sl@0: const TInteger& aInput, RInteger& aOutput); sl@0: private: sl@0: static void FunctionL(const TInteger& aModulus, const TInteger& aExponent, sl@0: const TInteger& aBase, RInteger& aOutput); sl@0: static void FunctionCRTL(const CKey& aPrivateKey, sl@0: const TInteger& aInput, RInteger& aOutput); sl@0: private: sl@0: RSAFunction(void); sl@0: }; sl@0: sl@0: /** Computes whether a given message representative is within the valid bounds sl@0: * for a given modulus, i.e. whether the message is representative within [0,n-1]. sl@0: * @param aInput The message representative. sl@0: * @param aModulus The modulus. sl@0: * @return TBool representing whether or not the message representative is sl@0: * valid. sl@0: */ sl@0: TBool RSAFunction::IsInputValid(const TInteger& aInput, sl@0: const TInteger& aModulus) sl@0: { sl@0: //See HAC 8.3 1.b sl@0: //Message (input) must be in the interval [0,n-1] (inclusive) sl@0: if( aInput.IsNegative() || aInput >= aModulus ) sl@0: return EFalse; sl@0: else sl@0: return ETrue; sl@0: } sl@0: sl@0: void RSAFunction::IsInputValidL(const TInteger& aInput, sl@0: const TInteger& aModulus) sl@0: { sl@0: if(!IsInputValid(aInput, aModulus)) sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: #endif sl@0: