crypto/weakcrypto/source/asymmetric/rsafunction.h
author hgs
Thu, 12 Aug 2010 21:07:10 +0530
changeset 90 8c545fea2798
parent 72 de46a57f75fb
permissions -rw-r--r--
201031_01
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     1
/*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     2
* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     3
* All rights reserved.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     8
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     9
* Initial Contributors:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    11
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    12
* Contributors:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    13
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    14
* Description: 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    15
* RSAFunction class implementation
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    16
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    17
*/
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    18
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    19
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    20
/**
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    21
 @file 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    22
 @internalTechnology
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    23
*/
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    24
 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    25
#ifndef __RSAFUNCTION_H__
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    26
#define __RSAFUNCTION_H__
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    27
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    28
#include <e32base.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    29
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    30
class TInteger;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    31
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    32
class CRSAPublicKey;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    33
class CRSAPrivateKey;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    34
class CRSAPrivateKeyCRT;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    35
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    36
class RSAFunction
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    37
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    38
public:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    39
	static inline TBool IsInputValid(const TInteger& aInput, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    40
		const TInteger& aModulus);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    41
	static inline void IsInputValidL(const TInteger& aInput, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    42
		const TInteger& aModulus);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    43
	static void EncryptL(const CRSAPublicKey& aPublicKey,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    44
		const TInteger& aInput, RInteger& aOutput);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    45
	static void DecryptL(const CRSAPrivateKey& aPrivateKey,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    46
		const TInteger& aInput, RInteger& aOutput);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    47
	static void SignL(const CRSAPrivateKey& aPrivateKey,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    48
		const TInteger& aInput, RInteger& aOutput);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    49
	static void VerifyL(const CRSAPublicKey& aPublicKey,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    50
		const TInteger& aInput, RInteger& aOutput);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    51
private:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    52
	static void FunctionL(const TInteger& aModulus, const TInteger& aExponent,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    53
		const TInteger& aBase, RInteger& aOutput);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    54
	static void FunctionCRTL(const CRSAPrivateKeyCRT& aPrivateKey,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    55
		const TInteger& aInput, RInteger& aOutput);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    56
private:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    57
	RSAFunction(void);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    58
	};
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    59
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    60
/** Computes whether a given message representative is within the valid bounds
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    61
 * for a given modulus, i.e. whether the message is representative within [0,n-1].
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    62
 * @param aInput The message representative.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    63
 * @param aModulus The modulus.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    64
 * @return TBool representing whether or not the message representative is
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    65
 * valid.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    66
 */
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    67
TBool RSAFunction::IsInputValid(const TInteger& aInput, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    68
	const TInteger& aModulus)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    69
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    70
	//See HAC 8.3 1.b
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    71
	//Message (input) must be in the interval [0,n-1] (inclusive)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    72
	if( aInput.IsNegative() || aInput >= aModulus )
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    73
		return EFalse;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    74
	else
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    75
		return ETrue;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    76
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    77
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    78
void RSAFunction::IsInputValidL(const TInteger& aInput,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    79
	const TInteger& aModulus)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    80
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    81
	if(!IsInputValid(aInput, aModulus))
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    82
		User::Leave(KErrArgument);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    83
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    84
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    85
#endif
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    86