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