os/security/crypto/weakcrypto/test/tasymmetric/tdsavector.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "tdsavector.h"
sl@0
    20
#include "tvectorutils.h"
sl@0
    21
#include "t_input.h"
sl@0
    22
#include <bigint.h>
sl@0
    23
#include "tbrokenrandom.h"
sl@0
    24
sl@0
    25
////////////////////////////////////////////////////////////////////////////////
sl@0
    26
// CDSASignVector
sl@0
    27
////////////////////////////////////////////////////////////////////////////////
sl@0
    28
sl@0
    29
CTestAction* CDSASignVector::NewL(RFs& aFs,
sl@0
    30
                                  CConsoleBase& aConsole,
sl@0
    31
                                  Output& aOut, 
sl@0
    32
                                  const TTestActionSpec& aTestActionSpec)
sl@0
    33
	{
sl@0
    34
	CTestAction* self = CDSASignVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
sl@0
    35
	CleanupStack::Pop();
sl@0
    36
	return self;
sl@0
    37
	}
sl@0
    38
sl@0
    39
CTestAction* CDSASignVector::NewLC(RFs& aFs,
sl@0
    40
                                   CConsoleBase& aConsole,
sl@0
    41
                                   Output& aOut, 
sl@0
    42
                                   const TTestActionSpec& aTestActionSpec)
sl@0
    43
	{
sl@0
    44
	CDSASignVector* self = new(ELeave) CDSASignVector(aFs, aConsole, aOut);
sl@0
    45
	CleanupStack::PushL(self);
sl@0
    46
	self->ConstructL(aTestActionSpec);
sl@0
    47
	return self;
sl@0
    48
	}
sl@0
    49
sl@0
    50
CDSASignVector::~CDSASignVector()
sl@0
    51
	{
sl@0
    52
    delete iPrivKey;
sl@0
    53
	delete iSignature;
sl@0
    54
	delete iK;
sl@0
    55
	delete iSigInput;
sl@0
    56
	}
sl@0
    57
sl@0
    58
CDSASignVector::CDSASignVector(RFs& /*aFs*/, 
sl@0
    59
                               CConsoleBase& aConsole,
sl@0
    60
                               Output& aOut)					 
sl@0
    61
    : CVectorTest(aConsole, aOut)
sl@0
    62
	{
sl@0
    63
	}
sl@0
    64
sl@0
    65
void CDSASignVector::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0
    66
	{
sl@0
    67
	CVectorTest::ConstructL(aTestActionSpec);
sl@0
    68
sl@0
    69
    iPrivKey = VectorUtils::ReadDSAPrivateKeyL(aTestActionSpec.iActionBody);
sl@0
    70
sl@0
    71
	iMessage.Set(Input::ParseElement(aTestActionSpec.iActionBody, _L8("<m>")));
sl@0
    72
	iK = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<k>"));
sl@0
    73
sl@0
    74
	iSignature = VectorUtils::ReadDSASignatureL(aTestActionSpec.iActionBody);
sl@0
    75
	iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
sl@0
    76
	iSigInput->Update(iMessage);
sl@0
    77
	}
sl@0
    78
sl@0
    79
void CDSASignVector::DoPerformActionL()
sl@0
    80
	{
sl@0
    81
	__UHEAP_MARK;
sl@0
    82
sl@0
    83
	CRandomSetSource* random = new(ELeave)CRandomSetSource(*iK);
sl@0
    84
	SetThreadRandomLC(random);
sl@0
    85
sl@0
    86
    CDSASigner* signer = CDSASigner::NewLC(*iPrivKey);
sl@0
    87
	const CDSASignature* testSig = signer->SignL(iSigInput->Final());
sl@0
    88
    iResult = (*testSig == *iSignature);
sl@0
    89
sl@0
    90
	delete testSig;
sl@0
    91
    CleanupStack::PopAndDestroy(signer);
sl@0
    92
	CleanupStack::PopAndDestroy(); //SetThreadRandomLC
sl@0
    93
sl@0
    94
	__UHEAP_MARKEND;
sl@0
    95
	}
sl@0
    96
sl@0
    97
void CDSASignVector::DoPerformanceTestActionL()
sl@0
    98
	{
sl@0
    99
	iResult = ETrue;
sl@0
   100
	}
sl@0
   101
sl@0
   102
void CDSASignVector::DoCheckResult(TInt /*aError*/)
sl@0
   103
	{
sl@0
   104
//	If using Keith's fixed up testframework for testing failures, iResult will 
sl@0
   105
//	have already been corrected for a deliberate fail result.
sl@0
   106
//	If not using Keith's testframework iResult is still a fail result at this
sl@0
   107
//	point, so now's the time to adjust for deliberate failure.
sl@0
   108
sl@0
   109
	if (!iResult)
sl@0
   110
		iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
sl@0
   111
	
sl@0
   112
	if( iResult == EFalse )
sl@0
   113
		{
sl@0
   114
		iConsole.Printf(_L("X"));
sl@0
   115
		}
sl@0
   116
	else 
sl@0
   117
		{
sl@0
   118
		iConsole.Printf(_L("."));
sl@0
   119
		}
sl@0
   120
	}
sl@0
   121
sl@0
   122
////////////////////////////////////////////////////////////////////////////////
sl@0
   123
// CDSAVerifyVector
sl@0
   124
////////////////////////////////////////////////////////////////////////////////
sl@0
   125
sl@0
   126
CTestAction* CDSAVerifyVector::NewL(RFs& aFs,
sl@0
   127
                                  CConsoleBase& aConsole,
sl@0
   128
                                  Output& aOut, 
sl@0
   129
                                  const TTestActionSpec& aTestActionSpec)
sl@0
   130
	{
sl@0
   131
	CTestAction* self = CDSAVerifyVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
sl@0
   132
	CleanupStack::Pop();
sl@0
   133
	return self;
sl@0
   134
	}
sl@0
   135
sl@0
   136
CTestAction* CDSAVerifyVector::NewLC(RFs& aFs,
sl@0
   137
                                   CConsoleBase& aConsole,
sl@0
   138
                                   Output& aOut, 
sl@0
   139
                                   const TTestActionSpec& aTestActionSpec)
sl@0
   140
	{
sl@0
   141
	CDSAVerifyVector* self = new(ELeave) CDSAVerifyVector(aFs, aConsole, aOut);
sl@0
   142
	CleanupStack::PushL(self);
sl@0
   143
	self->ConstructL(aTestActionSpec);
sl@0
   144
	return self;
sl@0
   145
	}
sl@0
   146
sl@0
   147
CDSAVerifyVector::~CDSAVerifyVector()
sl@0
   148
	{
sl@0
   149
    delete iPubKey;
sl@0
   150
	delete iSignature;
sl@0
   151
	delete iMessage;
sl@0
   152
	delete iSigInput;
sl@0
   153
	}
sl@0
   154
sl@0
   155
CDSAVerifyVector::CDSAVerifyVector(RFs& /*aFs*/, 
sl@0
   156
                                   CConsoleBase& aConsole,
sl@0
   157
                                   Output& aOut)					 
sl@0
   158
    : CVectorTest(aConsole, aOut)
sl@0
   159
	{
sl@0
   160
	}
sl@0
   161
sl@0
   162
void CDSAVerifyVector::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0
   163
	{
sl@0
   164
	CVectorTest::ConstructL(aTestActionSpec);
sl@0
   165
sl@0
   166
    iPubKey = VectorUtils::ReadDSAPublicKeyL(aTestActionSpec.iActionBody);
sl@0
   167
sl@0
   168
	TPtrC8 message(Input::ParseElement(aTestActionSpec.iActionBody, _L8("<m>")));
sl@0
   169
	if (message.Length()==0)	
sl@0
   170
		iMessage = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<hexm>"));
sl@0
   171
	else
sl@0
   172
		iMessage = message.AllocL();
sl@0
   173
		
sl@0
   174
	iSignature = VectorUtils::ReadDSASignatureL(aTestActionSpec.iActionBody);
sl@0
   175
	iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
sl@0
   176
	iSigInput->Update(*iMessage);
sl@0
   177
	}	
sl@0
   178
sl@0
   179
void CDSAVerifyVector::DoPerformActionL()
sl@0
   180
	{
sl@0
   181
	__UHEAP_MARK;
sl@0
   182
sl@0
   183
    CDSAVerifier* verifier = CDSAVerifier::NewLC(*iPubKey);
sl@0
   184
    iResult = verifier->VerifyL(iSigInput->Final(), *iSignature);
sl@0
   185
sl@0
   186
    CleanupStack::PopAndDestroy(verifier);
sl@0
   187
	__UHEAP_MARKEND;
sl@0
   188
	}
sl@0
   189
sl@0
   190
void CDSAVerifyVector::DoPerformanceTestActionL()
sl@0
   191
	{
sl@0
   192
	iResult = ETrue;
sl@0
   193
	}
sl@0
   194
sl@0
   195
void CDSAVerifyVector::DoCheckResult(TInt /*aError*/)
sl@0
   196
	{
sl@0
   197
//	If using Keith's fixed up testframework for testing failures, iResult will 
sl@0
   198
//	have already been corrected for a deliberate fail result.
sl@0
   199
//	If not using Keith's testframework iResult is still a fail result at this
sl@0
   200
//	point, so now's the time to adjust for deliberate failure.
sl@0
   201
sl@0
   202
	if (!iResult)
sl@0
   203
		iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
sl@0
   204
	
sl@0
   205
	if( iResult == EFalse )
sl@0
   206
		{
sl@0
   207
		iConsole.Printf(_L("X"));
sl@0
   208
		}
sl@0
   209
	else 
sl@0
   210
		{
sl@0
   211
		iConsole.Printf(_L("."));
sl@0
   212
		}
sl@0
   213
	}