os/security/crypto/weakcryptospi/test/tasymmetric/tdsavector.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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
const TInt KSha1HashLength = 20; 
sl@0
    26
////////////////////////////////////////////////////////////////////////////////
sl@0
    27
// CDSASignVector
sl@0
    28
////////////////////////////////////////////////////////////////////////////////
sl@0
    29
sl@0
    30
CTestAction* CDSASignVector::NewL(RFs& aFs,
sl@0
    31
                                  CConsoleBase& aConsole,
sl@0
    32
                                  Output& aOut, 
sl@0
    33
                                  const TTestActionSpec& aTestActionSpec)
sl@0
    34
	{
sl@0
    35
	CTestAction* self = CDSASignVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
sl@0
    36
	CleanupStack::Pop();
sl@0
    37
	return self;
sl@0
    38
	}
sl@0
    39
sl@0
    40
CTestAction* CDSASignVector::NewLC(RFs& aFs,
sl@0
    41
                                   CConsoleBase& aConsole,
sl@0
    42
                                   Output& aOut, 
sl@0
    43
                                   const TTestActionSpec& aTestActionSpec)
sl@0
    44
	{
sl@0
    45
	CDSASignVector* self = new(ELeave) CDSASignVector(aFs, aConsole, aOut);
sl@0
    46
	CleanupStack::PushL(self);
sl@0
    47
	self->ConstructL(aTestActionSpec);
sl@0
    48
	return self;
sl@0
    49
	}
sl@0
    50
sl@0
    51
CDSASignVector::~CDSASignVector()
sl@0
    52
	{
sl@0
    53
    delete iPrivKey;
sl@0
    54
	delete iSignature;
sl@0
    55
	delete iK;
sl@0
    56
	delete iSigInput;
sl@0
    57
	}
sl@0
    58
sl@0
    59
CDSASignVector::CDSASignVector(RFs& /*aFs*/, 
sl@0
    60
                               CConsoleBase& aConsole,
sl@0
    61
                               Output& aOut)					 
sl@0
    62
    : CVectorTest(aConsole, aOut)
sl@0
    63
	{
sl@0
    64
	}
sl@0
    65
sl@0
    66
void CDSASignVector::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0
    67
	{
sl@0
    68
	CVectorTest::ConstructL(aTestActionSpec);
sl@0
    69
sl@0
    70
    iPrivKey = VectorUtils::ReadDSAPrivateKeyL(aTestActionSpec.iActionBody);
sl@0
    71
sl@0
    72
	iMessage.Set(Input::ParseElement(aTestActionSpec.iActionBody, _L8("<m>")));
sl@0
    73
	iK = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<k>"));
sl@0
    74
sl@0
    75
	iSignature = VectorUtils::ReadDSASignatureL(aTestActionSpec.iActionBody);
sl@0
    76
	iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
sl@0
    77
	iSigInput->Update(iMessage);
sl@0
    78
	}
sl@0
    79
sl@0
    80
void CDSASignVector::DoPerformActionL()
sl@0
    81
	{
sl@0
    82
	__UHEAP_MARK;
sl@0
    83
sl@0
    84
	CRandomSetSource* random = new(ELeave)CRandomSetSource(*iK);
sl@0
    85
	SetThreadRandomLC(random);
sl@0
    86
sl@0
    87
    CDSASigner* signer = CDSASigner::NewLC(*iPrivKey);
sl@0
    88
	const CDSASignature* testSig = signer->SignL(iSigInput->Final());
sl@0
    89
    iResult = (*testSig == *iSignature);
sl@0
    90
	delete testSig;
sl@0
    91
    CleanupStack::PopAndDestroy(signer);
sl@0
    92
sl@0
    93
    CDSASigner* signer1 = CDSASigner::NewL(*iPrivKey);
sl@0
    94
    CleanupStack::PushL(signer1);
sl@0
    95
    if (signer1->MaxInputLength() != KSha1HashLength)
sl@0
    96
    	{
sl@0
    97
        iResult = EFalse;
sl@0
    98
        }
sl@0
    99
    CleanupStack::PopAndDestroy(signer1); 
sl@0
   100
	CleanupStack::PopAndDestroy(); //SetThreadRandomLC
sl@0
   101
sl@0
   102
	__UHEAP_MARKEND;
sl@0
   103
	}
sl@0
   104
sl@0
   105
void CDSASignVector::DoPerformanceTestActionL()
sl@0
   106
	{
sl@0
   107
	iResult = ETrue;
sl@0
   108
	}
sl@0
   109
sl@0
   110
void CDSASignVector::DoCheckResult(TInt /*aError*/)
sl@0
   111
	{
sl@0
   112
//	If using Keith's fixed up testframework for testing failures, iResult will 
sl@0
   113
//	have already been corrected for a deliberate fail result.
sl@0
   114
//	If not using Keith's testframework iResult is still a fail result at this
sl@0
   115
//	point, so now's the time to adjust for deliberate failure.
sl@0
   116
sl@0
   117
	if (!iResult)
sl@0
   118
		iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
sl@0
   119
	
sl@0
   120
	if( iResult == EFalse )
sl@0
   121
		{
sl@0
   122
		iConsole.Printf(_L("X"));
sl@0
   123
		}
sl@0
   124
	else 
sl@0
   125
		{
sl@0
   126
		iConsole.Printf(_L("."));
sl@0
   127
		}
sl@0
   128
	}
sl@0
   129
sl@0
   130
////////////////////////////////////////////////////////////////////////////////
sl@0
   131
// CDSAVerifyVector
sl@0
   132
////////////////////////////////////////////////////////////////////////////////
sl@0
   133
sl@0
   134
CTestAction* CDSAVerifyVector::NewL(RFs& aFs,
sl@0
   135
                                  CConsoleBase& aConsole,
sl@0
   136
                                  Output& aOut, 
sl@0
   137
                                  const TTestActionSpec& aTestActionSpec)
sl@0
   138
	{
sl@0
   139
	CTestAction* self = CDSAVerifyVector::NewLC(aFs, aConsole, aOut, aTestActionSpec);
sl@0
   140
	CleanupStack::Pop();
sl@0
   141
	return self;
sl@0
   142
	}
sl@0
   143
sl@0
   144
CTestAction* CDSAVerifyVector::NewLC(RFs& aFs,
sl@0
   145
                                   CConsoleBase& aConsole,
sl@0
   146
                                   Output& aOut, 
sl@0
   147
                                   const TTestActionSpec& aTestActionSpec)
sl@0
   148
	{
sl@0
   149
	CDSAVerifyVector* self = new(ELeave) CDSAVerifyVector(aFs, aConsole, aOut);
sl@0
   150
	CleanupStack::PushL(self);
sl@0
   151
	self->ConstructL(aTestActionSpec);
sl@0
   152
	return self;
sl@0
   153
	}
sl@0
   154
sl@0
   155
CDSAVerifyVector::~CDSAVerifyVector()
sl@0
   156
	{
sl@0
   157
    delete iPubKey;
sl@0
   158
	delete iSignature;
sl@0
   159
	delete iMessage;
sl@0
   160
	delete iSigInput;
sl@0
   161
	}
sl@0
   162
sl@0
   163
CDSAVerifyVector::CDSAVerifyVector(RFs& /*aFs*/, 
sl@0
   164
                                   CConsoleBase& aConsole,
sl@0
   165
                                   Output& aOut)					 
sl@0
   166
    : CVectorTest(aConsole, aOut)
sl@0
   167
	{
sl@0
   168
	}
sl@0
   169
sl@0
   170
void CDSAVerifyVector::ConstructL(const TTestActionSpec& aTestActionSpec)
sl@0
   171
	{
sl@0
   172
	CVectorTest::ConstructL(aTestActionSpec);
sl@0
   173
sl@0
   174
    iPubKey = VectorUtils::ReadDSAPublicKeyL(aTestActionSpec.iActionBody);
sl@0
   175
sl@0
   176
	TPtrC8 message(Input::ParseElement(aTestActionSpec.iActionBody, _L8("<m>")));
sl@0
   177
	if (message.Length()==0)	
sl@0
   178
		iMessage = Input::ParseElementHexL(aTestActionSpec.iActionBody, _L8("<hexm>"));
sl@0
   179
	else
sl@0
   180
		iMessage = message.AllocL();
sl@0
   181
		
sl@0
   182
	iSignature = VectorUtils::ReadDSASignatureL(aTestActionSpec.iActionBody);
sl@0
   183
	iSigInput = CHashingSignatureInput::NewL(CMessageDigest::ESHA1);
sl@0
   184
	iSigInput->Update(*iMessage);
sl@0
   185
	}	
sl@0
   186
sl@0
   187
void CDSAVerifyVector::DoPerformActionL()
sl@0
   188
	{
sl@0
   189
	__UHEAP_MARK;
sl@0
   190
sl@0
   191
    CDSAVerifier* verifier = CDSAVerifier::NewLC(*iPubKey);
sl@0
   192
    iResult = verifier->VerifyL(iSigInput->Final(), *iSignature);
sl@0
   193
sl@0
   194
    CleanupStack::PopAndDestroy(verifier);
sl@0
   195
	__UHEAP_MARKEND;
sl@0
   196
	}
sl@0
   197
sl@0
   198
void CDSAVerifyVector::DoPerformanceTestActionL()
sl@0
   199
	{
sl@0
   200
	iResult = ETrue;
sl@0
   201
	}
sl@0
   202
sl@0
   203
void CDSAVerifyVector::DoCheckResult(TInt /*aError*/)
sl@0
   204
	{
sl@0
   205
//	If using Keith's fixed up testframework for testing failures, iResult will 
sl@0
   206
//	have already been corrected for a deliberate fail result.
sl@0
   207
//	If not using Keith's testframework iResult is still a fail result at this
sl@0
   208
//	point, so now's the time to adjust for deliberate failure.
sl@0
   209
sl@0
   210
	if (!iResult)
sl@0
   211
		iResult = (iResult && iExpectedResult) || (!iResult && !iExpectedResult);
sl@0
   212
	
sl@0
   213
	if( iResult == EFalse )
sl@0
   214
		{
sl@0
   215
		iConsole.Printf(_L("X"));
sl@0
   216
		}
sl@0
   217
	else 
sl@0
   218
		{
sl@0
   219
		iConsole.Printf(_L("."));
sl@0
   220
		}
sl@0
   221
	}