crypto/weakcrypto/source/asymmetric/rsasigner.cpp
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
*
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
#include <asymmetric.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    20
#include <asymmetrickeys.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    21
#include <bigint.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    22
#include <securityerr.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    23
#include "rsafunction.h"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    24
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    25
/* CRSASigner */
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    26
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    27
EXPORT_C CRSASigner::CRSASigner(void)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    28
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    29
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    30
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    31
/* CRSAPKCS1v15Signer */
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    32
EXPORT_C CRSAPKCS1v15Signer* CRSAPKCS1v15Signer::NewL(
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    33
	const CRSAPrivateKey& aKey)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    34
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    35
	CRSAPKCS1v15Signer* self = NewLC(aKey);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    36
	CleanupStack::Pop(self);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    37
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    38
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    39
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    40
EXPORT_C CRSAPKCS1v15Signer* CRSAPKCS1v15Signer::NewLC(
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    41
	const CRSAPrivateKey& aKey)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    42
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    43
	CRSAPKCS1v15Signer* self = new(ELeave) CRSAPKCS1v15Signer(aKey);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    44
	CleanupStack::PushL(self);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    45
	self->ConstructL();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    46
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    47
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    48
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    49
TInt CRSAPKCS1v15Signer::MaxInputLength(void) const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    50
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    51
	return MaxOutputLength() - iPadding->MinPaddingLength();	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    52
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    53
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    54
TInt CRSAPKCS1v15Signer::MaxOutputLength(void) const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    55
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    56
	return iPrivateKey.N().ByteCount();	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    57
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    58
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    59
CRSASignature* CRSAPKCS1v15Signer::SignL(const TDesC8& aInput) const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    60
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    61
	HBufC8* buf = HBufC8::NewMaxLC(MaxOutputLength());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    62
	TPtr8 ptr = buf->Des();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    63
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    64
	ptr.SetLength(aInput.Length());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    65
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    66
	//The following will panic if aInput is larger than MaxOutputLength() It is
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    67
	//likely that the caller has passed in something that has not been hashed.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    68
	//This is a programming, and likely a security error, in client code, not a
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    69
	//problem here.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    70
	iPadding->PadL(aInput, ptr);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    71
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    72
	RInteger input = RInteger::NewL(ptr);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    73
	CleanupStack::PushL(input);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    74
	RInteger output;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    75
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    76
	RSAFunction::SignL(iPrivateKey, input, output);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    77
	CleanupStack::PushL(output);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    78
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    79
	CRSASignature* signature = CRSASignature::NewL(output);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    80
	CleanupStack::Pop(&output); //output, now owned by CRSASignature
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    81
	CleanupStack::PopAndDestroy(2, buf); //input, buf
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    82
	return signature;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    83
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    84
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    85
CRSAPKCS1v15Signer::CRSAPKCS1v15Signer(const CRSAPrivateKey& aKey)  
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    86
	: iPrivateKey(aKey)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    87
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    88
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    89
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    90
void CRSAPKCS1v15Signer::ConstructL(void)  
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    91
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    92
	iPadding = CPaddingPKCS1Signature::NewL(MaxOutputLength());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    93
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    94
	// Check if MaxInputLength() makes sense, if not the key length must 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    95
	// be too small
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    96
	if(MaxInputLength() <= 0)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    97
		{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    98
		User::Leave(KErrKeySize);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    99
		}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   100
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   101
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   102
CRSAPKCS1v15Signer::~CRSAPKCS1v15Signer(void)  
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   103
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   104
	delete iPadding;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   105
	}