os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rsafunction.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     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 *
    16 */
    17 
    18 
    19 /**
    20  @file
    21  @internalComponent
    22  @released
    23 */
    24 
    25 #ifndef __RSAFUNCTION_H__
    26 #define __RSAFUNCTION_H__
    27 
    28 #include <e32base.h>
    29 #include "keys.h"
    30 
    31 using namespace CryptoSpi;
    32 
    33 class TInteger;
    34 
    35 class RSAFunction
    36 	{
    37 public:
    38 	static inline TBool IsInputValid(const TInteger& aInput, 
    39 		const TInteger& aModulus);
    40 	static inline void IsInputValidL(const TInteger& aInput, 
    41 		const TInteger& aModulus);
    42 	static void EncryptL(const CKey& aPublicKey,
    43 		const TInteger& aInput, RInteger& aOutput);
    44 	static void DecryptL(const CKey& aPrivateKey,
    45 		const TInteger& aInput, RInteger& aOutput);
    46 	static void SignL(const CKey& aPrivateKey,
    47 		const TInteger& aInput, RInteger& aOutput);
    48 	static void VerifyL(const CKey& aPublicKey,
    49 		const TInteger& aInput, RInteger& aOutput);
    50 private:
    51 	static void FunctionL(const TInteger& aModulus, const TInteger& aExponent,
    52 		const TInteger& aBase, RInteger& aOutput);
    53 	static void FunctionCRTL(const CKey& aPrivateKey,
    54 		const TInteger& aInput, RInteger& aOutput);
    55 private:
    56 	RSAFunction(void);
    57 	};
    58 
    59 /** Computes whether a given message representative is within the valid bounds
    60  * for a given modulus, i.e. whether the message is representative within [0,n-1].
    61  * @param aInput The message representative.
    62  * @param aModulus The modulus.
    63  * @return TBool representing whether or not the message representative is
    64  * valid.
    65  */
    66 TBool RSAFunction::IsInputValid(const TInteger& aInput, 
    67 	const TInteger& aModulus)
    68 	{
    69 	//See HAC 8.3 1.b
    70 	//Message (input) must be in the interval [0,n-1] (inclusive)
    71 	if( aInput.IsNegative() || aInput >= aModulus )
    72 		return EFalse;
    73 	else
    74 		return ETrue;
    75 	}
    76 
    77 void RSAFunction::IsInputValidL(const TInteger& aInput,
    78 	const TInteger& aModulus)
    79 	{
    80 	if(!IsInputValid(aInput, aModulus))
    81 		User::Leave(KErrArgument);
    82 	}
    83 
    84 #endif
    85