1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/test/thash/thash.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,1150 @@
1.4 +/*
1.5 +* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +/**
1.23 + @file
1.24 +*/
1.25 +
1.26 +#include "hashtestutils.h"
1.27 +#include <utf.h>
1.28 +
1.29 +TBool gInOOMTest=EFalse;
1.30 +
1.31 +RTest test(_L("Hash Tests"));
1.32 +
1.33 +const TInt KMaxHashSize = 64; // Hash size in bytes
1.34 +
1.35 +void Hex(HBufC8& aString)
1.36 + {
1.37 + TPtr8 ptr=aString.Des();
1.38 + if (aString.Length()%2)
1.39 + {
1.40 + ptr.SetLength(0);
1.41 + return;
1.42 + }
1.43 + TInt i;
1.44 + for (i=0;i<aString.Length();i+=2)
1.45 + {
1.46 + TUint8 tmp;
1.47 + tmp=(TUint8)(aString[i]-(aString[i]>'9'?('A'-10):'0'));
1.48 + tmp*=16;
1.49 + tmp|=(TUint8)(aString[i+1]-(aString[i+1]>'9'?('A'-10):'0'));
1.50 + ptr[i/2]=tmp;
1.51 + }
1.52 + ptr.SetLength(aString.Length()/2);
1.53 + }
1.54 +
1.55 +void Spin()
1.56 +{// Pointless function to print a dot
1.57 + if (gInOOMTest)
1.58 + {
1.59 + static TInt count=0;
1.60 + if (count++==100)
1.61 + {
1.62 + test.Printf(_L("o"));
1.63 + count=0;
1.64 + }
1.65 + return;
1.66 + }
1.67 + test.Printf(_L("."));
1.68 +}
1.69 +
1.70 +void FunctionalityTestL(CMessageDigest* aMD)
1.71 + {
1.72 + if (!gInOOMTest)
1.73 + test.Next(_L("Functionality test - original API"));
1.74 +
1.75 + const TInt maxbuffersize=1024;
1.76 + TInt buffersize;
1.77 + TInt increment;
1.78 + if (gInOOMTest)
1.79 + {
1.80 + buffersize=256;
1.81 + increment=6;
1.82 + }
1.83 + else
1.84 + {
1.85 + buffersize=maxbuffersize;
1.86 + increment=1;
1.87 + }
1.88 +
1.89 + TBool testSuccess = ETrue;
1.90 + TInt i = 0;
1.91 + TBuf8<maxbuffersize> buf(maxbuffersize);
1.92 + for (;i<buffersize;i++)
1.93 + {
1.94 + buf[i]=(TUint8)i;
1.95 + }
1.96 +
1.97 + for (i=0;i<buffersize;i+=increment)
1.98 + {
1.99 + TInt j;
1.100 + Spin();
1.101 + if (!gInOOMTest)
1.102 + {
1.103 + if (i>128)
1.104 + {
1.105 + increment=8;
1.106 + }
1.107 + }
1.108 + else
1.109 + {
1.110 + if (i>24)
1.111 + {
1.112 + increment=32;
1.113 + }
1.114 + }
1.115 +
1.116 + for (j=0;j<i;j+=16)
1.117 + {
1.118 + buf[0]=(TUint8)j;
1.119 + TPtrC8 ptr=buf.Left(i);
1.120 + TPtrC8 ptr2=buf.Left(j);
1.121 + TPtrC8 ptr3=buf.Mid(j,i-j);
1.122 +
1.123 + CMessageDigest* first=aMD->ReplicateL();
1.124 + CleanupStack::PushL(first);
1.125 + TPtrC8 firstFinal = first->Hash(ptr);
1.126 +
1.127 + aMD->Reset();
1.128 + aMD->Update(ptr);
1.129 + TPtrC8 aMDFinal = aMD->Final();
1.130 +
1.131 + CMessageDigest* second=aMD->ReplicateL();
1.132 + CleanupStack::PushL(second);
1.133 + second->Hash(ptr2);
1.134 +
1.135 + CMessageDigest* third=second->CopyL();
1.136 + CleanupStack::PushL(third);
1.137 +
1.138 + TPtrC8 secondFinal = second->Hash(ptr3);
1.139 +
1.140 + if (aMDFinal!=firstFinal)
1.141 + {
1.142 + testSuccess = EFalse;
1.143 + }
1.144 +
1.145 + if (firstFinal!=secondFinal)
1.146 + {
1.147 + testSuccess = EFalse;
1.148 + }
1.149 +
1.150 + TPtrC8 thirdFinal = third->Hash(ptr3);
1.151 + if (firstFinal!=thirdFinal)
1.152 + {
1.153 + testSuccess = EFalse;
1.154 + }
1.155 + CleanupStack::PopAndDestroy(3); // first, second, third
1.156 + }
1.157 + }
1.158 +
1.159 +///////////////////////////////////////////////////////////////
1.160 +// Now test the new API - Update/Final functions
1.161 +///////////////////////////////////////////////////////////////
1.162 + if (!gInOOMTest)
1.163 + test.Next(_L("\rFunctionality test - calls added API functions Final & Update"));
1.164 +
1.165 + for (i=0;i<buffersize;i++)
1.166 + {
1.167 + buf[i]=(TUint8)i;
1.168 + }
1.169 +
1.170 + for (i=0;i<buffersize;i+=increment)
1.171 + {
1.172 + TInt j;
1.173 + Spin();
1.174 + if (!gInOOMTest)
1.175 + {
1.176 + if (i>128)
1.177 + {
1.178 + increment=8;
1.179 + }
1.180 + }
1.181 + else
1.182 + {
1.183 + if (i>24)
1.184 + {
1.185 + increment=32;
1.186 + }
1.187 + }
1.188 + for (j=0;j<i;j+=16)
1.189 + {
1.190 + buf[0]=(TUint8)j;
1.191 + TPtrC8 ptr=buf.Left(i);
1.192 + TPtrC8 ptr2=buf.Left(j);
1.193 + TPtrC8 ptr3=buf.Mid(j,i-j);
1.194 +
1.195 + CMessageDigest* first=aMD->ReplicateL();
1.196 + CleanupStack::PushL(first);
1.197 + first->Update(ptr);
1.198 +
1.199 + aMD->Update(ptr);
1.200 +
1.201 + CMessageDigest* second=aMD->ReplicateL();
1.202 + CleanupStack::PushL(second);
1.203 + second->Update(ptr2);
1.204 +
1.205 + CMessageDigest* third=second->CopyL();
1.206 + CleanupStack::PushL(third);
1.207 + third->Update(ptr3);
1.208 +
1.209 + second->Update(ptr3);
1.210 +
1.211 + TPtrC8 aMDFinal = aMD->Final();
1.212 + TPtrC8 firstFinal = first->Final();
1.213 + TPtrC8 secondFinal = second->Final();
1.214 + TPtrC8 thirdFinal = third->Final();
1.215 +
1.216 + if (aMDFinal!=firstFinal)
1.217 + {
1.218 + testSuccess = EFalse;
1.219 + }
1.220 +
1.221 + if (firstFinal!=secondFinal)
1.222 + {
1.223 + testSuccess = EFalse;
1.224 + }
1.225 +
1.226 + if (firstFinal!=thirdFinal)
1.227 + {
1.228 + testSuccess = EFalse;
1.229 + }
1.230 +
1.231 + CleanupStack::PopAndDestroy(3); // first, second, third
1.232 + }
1.233 + }
1.234 +
1.235 +
1.236 + test.Printf(_L("\r\n"));
1.237 +
1.238 + if (!testSuccess)
1.239 + User::Leave(KErrGeneral);
1.240 + }
1.241 +
1.242 +void VectorTestL(CMessageDigest* aMD,const TDesC& aFilename)
1.243 + {
1.244 + test.Next(_L("Test Vector tests - original API"));
1.245 + TBool finished=EFalse;
1.246 + TBool testSuccess = ETrue;
1.247 + //aFilename is a .dat file which contains the message and the corresponding digest
1.248 + CTestData* data = CTestData::NewL(aFilename);
1.249 + CleanupStack::PushL(data);
1.250 +
1.251 + while (!finished)
1.252 + {
1.253 + switch (data->Type())
1.254 + {
1.255 + case CTestData::EMessage:
1.256 + {
1.257 + break;
1.258 + }
1.259 + //This is added to read large input data from the files.
1.260 + case CTestData::EFileName:
1.261 + {
1.262 + CMessageDigest* md=aMD->ReplicateL();
1.263 + CleanupStack::PushL(md);
1.264 +
1.265 + //get the filename from the .dat file
1.266 + HBufC8* filename = (*data)[1];
1.267 + User::LeaveIfNull(filename);
1.268 + CleanupStack::PushL(filename);
1.269 +
1.270 + HBufC8* output= (*data)[2];
1.271 + User::LeaveIfNull(output);
1.272 + CleanupStack::PushL(output);
1.273 + Hex(*output);
1.274 +
1.275 + HBufC16* inputFileName = CnvUtfConverter::ConvertToUnicodeFromUtf8L(*filename);
1.276 +
1.277 + RFs fs;
1.278 + RFile file;
1.279 + CleanupClosePushL(fs);
1.280 + User::LeaveIfError(fs.Connect());
1.281 + TDriveUnit sysDrive(fs.GetSystemDrive());
1.282 + TBuf<24> filePath (sysDrive.Name());
1.283 + filePath.Append(_L("\\thash\\"));
1.284 + User::LeaveIfError(fs.SetSessionPath(filePath));
1.285 + CleanupClosePushL(file);
1.286 + User::LeaveIfError(file.Open(fs,*inputFileName,EFileShareAny|EFileRead));
1.287 + // read into iFile
1.288 + TInt size=0;
1.289 + file.Size(size);
1.290 +
1.291 + HBufC8* fileContents=HBufC8::NewMaxL(size);
1.292 +
1.293 + TPtr8 ptr=fileContents->Des();
1.294 + User::LeaveIfError(file.Read(ptr));
1.295 + CleanupStack::PopAndDestroy(2, &fs);
1.296 + CleanupStack::PushL(fileContents);
1.297 + delete inputFileName;
1.298 +
1.299 + TPtrC8 digest = md->Hash(*fileContents);
1.300 + if (digest!=(*output))
1.301 + {
1.302 + testSuccess = EFalse;
1.303 + }
1.304 +
1.305 + md->Reset();
1.306 +
1.307 + TPtrC8 digest2 = md->Hash(*fileContents);
1.308 + if (digest2!=(*output))
1.309 + {
1.310 + testSuccess = EFalse;
1.311 + }
1.312 +
1.313 + // Now try this in 2 half sections (just a quick check)
1.314 + md->Reset();
1.315 +
1.316 + TInt inputLen = fileContents->Length();
1.317 + if (inputLen > 1)
1.318 + {
1.319 + TInt leftHandLen = inputLen/2;
1.320 + TPtrC8 left = fileContents->Left(leftHandLen);
1.321 + TPtrC8 right = fileContents->Right(inputLen - leftHandLen);
1.322 +
1.323 + TPtrC8 halfDigest = md->Hash(left);
1.324 + if (halfDigest.Size()==0) // Unnecessary test, but removes
1.325 + User::Leave(KErrAbort); // a warning about not using halfDigest
1.326 +
1.327 + TPtrC8 wholeDigest = md->Hash(right);
1.328 +
1.329 + if (wholeDigest!=(*output))
1.330 + {
1.331 + testSuccess = EFalse;
1.332 + }
1.333 + }
1.334 +
1.335 + md->Reset();
1.336 + if (md->Final(*fileContents)!=(*output))
1.337 + {
1.338 + testSuccess = EFalse;
1.339 + }
1.340 +
1.341 + // Now try this in 2 half sections (just a quick check)
1.342 + md->Reset();
1.343 + inputLen = fileContents->Length();
1.344 + if (inputLen > 1)
1.345 + {
1.346 + TInt leftHandLen = inputLen/2;
1.347 + TPtrC8 left = fileContents->Left(leftHandLen);
1.348 + TPtrC8 right = fileContents->Right(inputLen - leftHandLen);
1.349 +
1.350 + md->Update(left);
1.351 + TPtrC8 wholeDigest = md->Final(right);
1.352 +
1.353 + if (wholeDigest!=(*output))
1.354 + {
1.355 + testSuccess = EFalse;
1.356 + }
1.357 + }
1.358 + CleanupStack::PopAndDestroy(4, md);//md,filename,output,fileContents
1.359 +
1.360 + break;
1.361 + }
1.362 + case CTestData::EData:
1.363 + {
1.364 + CMessageDigest* md=aMD->ReplicateL();
1.365 + CleanupStack::PushL(md);
1.366 +
1.367 + HBufC8* input = (*data)[0];
1.368 + User::LeaveIfNull(input);
1.369 + CleanupStack::PushL(input);
1.370 +
1.371 + HBufC8* output= (*data)[1];
1.372 + User::LeaveIfNull(output);
1.373 + CleanupStack::PushL(output);
1.374 + Hex(*input);
1.375 + Hex(*output);
1.376 +
1.377 + //call the Hash function of the Algorithm.
1.378 + TPtrC8 digest = md->Hash(*input);
1.379 + //Compare the digest generated with the output(digest) expected.
1.380 + if (digest!=(*output))
1.381 + {
1.382 + testSuccess = EFalse;
1.383 + }
1.384 +
1.385 + //check the Reset() function.
1.386 + md->Reset();
1.387 +
1.388 + TPtrC8 digest2 = md->Hash(*input);
1.389 + if (digest2!=(*output))
1.390 + {
1.391 + testSuccess = EFalse;
1.392 + }
1.393 +
1.394 + // Now try this in 2 half sections (just a quick check)
1.395 + md->Reset();
1.396 +
1.397 + TInt inputLen = input->Length();
1.398 + if (inputLen > 1)
1.399 + {
1.400 + TInt leftHandLen = inputLen/2;
1.401 + TPtrC8 left = input->Left(leftHandLen);
1.402 + TPtrC8 right = input->Right(inputLen - leftHandLen);
1.403 +
1.404 + TPtrC8 halfDigest = md->Hash(left);
1.405 + if (halfDigest.Size()==0) // Unnecessary test, but removes
1.406 + User::Leave(KErrAbort); // a warning about not using halfDigest
1.407 +
1.408 + TPtrC8 wholeDigest = md->Hash(right);
1.409 +
1.410 + if (wholeDigest!=(*output))
1.411 + {
1.412 + testSuccess = EFalse;
1.413 + }
1.414 + }
1.415 +
1.416 +///////////////////////////////////////////////////////////////
1.417 +// Now test the new API - Update/Final functions
1.418 +///////////////////////////////////////////////////////////////
1.419 + md->Reset();
1.420 + if (md->Final(*input)!=(*output))
1.421 + {
1.422 + testSuccess = EFalse;
1.423 + }
1.424 +
1.425 + // Now try this in 2 half sections (just a quick check)
1.426 + md->Reset();
1.427 + inputLen = input->Length();
1.428 + if (inputLen > 1)
1.429 + {
1.430 + TInt leftHandLen = inputLen/2;
1.431 + TPtrC8 left = input->Left(leftHandLen);
1.432 + TPtrC8 right = input->Right(inputLen - leftHandLen);
1.433 +
1.434 + md->Update(left);
1.435 + TPtrC8 wholeDigest = md->Final(right);
1.436 +
1.437 + if (wholeDigest!=(*output))
1.438 + {
1.439 + testSuccess = EFalse;
1.440 + }
1.441 + }
1.442 + CleanupStack::PopAndDestroy(3);
1.443 + break;
1.444 + }
1.445 + case CTestData::EFinished:
1.446 + finished=ETrue;
1.447 + break;
1.448 + default:
1.449 + test.Printf(_L("Error in data file\r\n"));
1.450 + break;
1.451 + }
1.452 + };
1.453 +
1.454 + CleanupStack::PopAndDestroy(data);
1.455 +
1.456 + if (!testSuccess)
1.457 + User::Leave(KErrGeneral);
1.458 + }
1.459 +
1.460 +void OOMTestL(CMessageDigest* aMD)
1.461 + {
1.462 + test.Next(_L("Out of memory test"));
1.463 + TInt err = KErrNoMemory;
1.464 + TInt nextFailure=0;
1.465 + gInOOMTest=ETrue;
1.466 + while (err!=KErrNone)
1.467 + {
1.468 + __UHEAP_MARK;
1.469 + __UHEAP_FAILNEXT(nextFailure);
1.470 + TRAP(err,FunctionalityTestL(aMD));
1.471 + __UHEAP_MARKEND;
1.472 + nextFailure++;
1.473 + }
1.474 + __UHEAP_RESET;
1.475 + gInOOMTest=EFalse;
1.476 + }
1.477 +
1.478 +void HMACVectorTestL(CMessageDigest* aMD,const TDesC& aFilename)
1.479 + {
1.480 + test.Next(_L("HMAC Test Vector tests"));
1.481 + CTestData* data = CTestData::NewL(aFilename);
1.482 + CleanupStack::PushL(data);
1.483 +
1.484 + TBool finished=EFalse;
1.485 + TBool testSuccess = ETrue;
1.486 +
1.487 + while (!finished)
1.488 + {
1.489 + switch (data->Type())
1.490 + {
1.491 + case CTestData::EMessage:
1.492 + {
1.493 + break;
1.494 + }
1.495 + case CTestData::EFileName:
1.496 + {
1.497 + //get the filename from the .dat file
1.498 + HBufC8* filename = (*data)[1];
1.499 + User::LeaveIfNull(filename);
1.500 + CleanupStack::PushL(filename);
1.501 +
1.502 + HBufC8* key= (*data)[2];
1.503 + User::LeaveIfNull(key);
1.504 + CleanupStack::PushL(key);
1.505 +
1.506 + HBufC16* inputFileName = CnvUtfConverter::ConvertToUnicodeFromUtf8L(*filename);
1.507 +
1.508 + HBufC8* output = (*data)[3];
1.509 + User::LeaveIfNull(output);
1.510 + CleanupStack::PushL(output);
1.511 +
1.512 + Hex(*key);
1.513 + Hex(*output);
1.514 +
1.515 + RFs fs;
1.516 + RFile file;
1.517 + CleanupClosePushL(fs);
1.518 + User::LeaveIfError(fs.Connect());
1.519 + TDriveUnit sysDrive(fs.GetSystemDrive());
1.520 + TBuf<24> filePath (sysDrive.Name());
1.521 + filePath.Append(_L("\\thash\\"));
1.522 + User::LeaveIfError(fs.SetSessionPath(filePath));
1.523 + CleanupClosePushL(file);
1.524 + User::LeaveIfError(file.Open(fs,*inputFileName,EFileShareAny|EFileRead));
1.525 + // read into iFile
1.526 + TInt size=0;
1.527 + file.Size(size);
1.528 + HBufC8* fileContents=HBufC8::NewMaxL(size);
1.529 +
1.530 + TPtr8 ptr=fileContents->Des();
1.531 + User::LeaveIfError(file.Read(ptr));
1.532 + CleanupStack::PopAndDestroy(2, &fs);
1.533 + CleanupStack::PushL(fileContents);
1.534 + delete inputFileName;
1.535 +
1.536 + CMessageDigest* temp = aMD->ReplicateL();
1.537 + CleanupStack::PushL(temp);
1.538 + CMessageDigest* md = CHMAC::NewL(*key, temp);
1.539 + CleanupStack::Pop(temp); // Now owned by md
1.540 +
1.541 + TPtrC8 digest = md->Hash(*fileContents);
1.542 + if (digest!=(*output))
1.543 + {
1.544 + testSuccess = EFalse;
1.545 + }
1.546 +
1.547 + // Now try this in 2 half sections (just a quick check)
1.548 + md->Reset();
1.549 +
1.550 + TInt inputLen = fileContents->Length();
1.551 + if (inputLen > 1)
1.552 + {
1.553 + TInt leftHandLen = inputLen/2;
1.554 + TPtrC8 left = fileContents->Left(leftHandLen);
1.555 + TPtrC8 right = fileContents->Right(inputLen - leftHandLen);
1.556 +
1.557 + TPtrC8 halfDigest = md->Hash(left);
1.558 + if (halfDigest.Size()==0) // Unnecessary test, but removes
1.559 + User::Leave(KErrAbort); // a warning about not using halfDigest
1.560 + TPtrC8 wholeDigest = md->Hash(right);
1.561 +
1.562 + if (wholeDigest!=(*output))
1.563 + {
1.564 + testSuccess = EFalse;
1.565 + }
1.566 + }
1.567 +
1.568 + md->Reset();
1.569 + TPtrC8 finalDigest = md->Final(*fileContents);
1.570 + if (finalDigest!=(*output))
1.571 + {
1.572 + testSuccess = EFalse;
1.573 + }
1.574 +
1.575 + // Now try this in 2 half sections (just a quick check)
1.576 + md->Reset();
1.577 +
1.578 + inputLen = fileContents->Length();
1.579 + if (inputLen > 1)
1.580 + {
1.581 + TInt leftHandLen = inputLen/2;
1.582 + TPtrC8 left = fileContents->Left(leftHandLen);
1.583 + TPtrC8 right = fileContents->Right(inputLen - leftHandLen);
1.584 +
1.585 + md->Update(left);
1.586 + TPtrC8 wholeDigest = md->Final(right);
1.587 +
1.588 + if (wholeDigest!=(*output))
1.589 + {
1.590 + testSuccess = EFalse;
1.591 + }
1.592 + }
1.593 + delete md;
1.594 + CleanupStack::PopAndDestroy(4, filename); // filename, key, output,fileContents
1.595 + break;
1.596 + }
1.597 + case CTestData::EData:
1.598 + {
1.599 + HBufC8* input = (*data)[0];
1.600 + User::LeaveIfNull(input);
1.601 + CleanupStack::PushL(input);
1.602 +
1.603 + HBufC8* key = (*data)[1];
1.604 + User::LeaveIfNull(key);
1.605 + CleanupStack::PushL(key);
1.606 +
1.607 + HBufC8* output = (*data)[2];
1.608 + User::LeaveIfNull(output);
1.609 + CleanupStack::PushL(output);
1.610 +
1.611 + Hex(*input);
1.612 + Hex(*key);
1.613 + Hex(*output);
1.614 +
1.615 + CMessageDigest* temp = aMD->ReplicateL();
1.616 + CleanupStack::PushL(temp);
1.617 + CMessageDigest* md = CHMAC::NewL(*key, temp);
1.618 + CleanupStack::Pop(temp); // Now owned by md
1.619 +
1.620 + TPtrC8 digest = md->Hash(*input);
1.621 + if (digest!=(*output))
1.622 + {
1.623 + testSuccess = EFalse;
1.624 + }
1.625 +
1.626 + // Now try this in 2 half sections (just a quick check)
1.627 + md->Reset();
1.628 +
1.629 + TInt inputLen = input->Length();
1.630 + if (inputLen > 1)
1.631 + {
1.632 + TInt leftHandLen = inputLen/2;
1.633 + TPtrC8 left = input->Left(leftHandLen);
1.634 + TPtrC8 right = input->Right(inputLen - leftHandLen);
1.635 +
1.636 + TPtrC8 halfDigest = md->Hash(left);
1.637 + if (halfDigest.Size()==0) // Unnecessary test, but removes
1.638 + User::Leave(KErrAbort); // a warning about not using halfDigest
1.639 + TPtrC8 wholeDigest = md->Hash(right);
1.640 +
1.641 + if (wholeDigest!=(*output))
1.642 + {
1.643 + testSuccess = EFalse;
1.644 + }
1.645 + }
1.646 +
1.647 +///////////////////////////////////////////////////////////////
1.648 +// Now test the new API - Update/Final functions
1.649 +///////////////////////////////////////////////////////////////
1.650 + md->Reset();
1.651 + TPtrC8 finalDigest = md->Final(*input);
1.652 + if (finalDigest!=(*output))
1.653 + {
1.654 + testSuccess = EFalse;
1.655 + }
1.656 +
1.657 + // Now try this in 2 half sections (just a quick check)
1.658 + md->Reset();
1.659 +
1.660 + inputLen = input->Length();
1.661 + if (inputLen > 1)
1.662 + {
1.663 + TInt leftHandLen = inputLen/2;
1.664 + TPtrC8 left = input->Left(leftHandLen);
1.665 + TPtrC8 right = input->Right(inputLen - leftHandLen);
1.666 +
1.667 + md->Update(left);
1.668 + TPtrC8 wholeDigest = md->Final(right);
1.669 +
1.670 + if (wholeDigest!=(*output))
1.671 + {
1.672 + testSuccess = EFalse;
1.673 + }
1.674 + }
1.675 + delete md;
1.676 + CleanupStack::PopAndDestroy(3); // input, key, output
1.677 + break;
1.678 + }
1.679 + case CTestData::EFinished:
1.680 + finished=ETrue;
1.681 + break;
1.682 + default:
1.683 + test.Printf(_L("Error in data file\r\n"));
1.684 + break;
1.685 + }
1.686 + };
1.687 +
1.688 + CleanupStack::PopAndDestroy(data);
1.689 +
1.690 + if (!testSuccess)
1.691 + User::Leave(KErrGeneral);
1.692 + }
1.693 +
1.694 +void HMACTestsL(CMessageDigest* aMD,const TDesC& aFilename, const TDesC& aHashType)
1.695 + {
1.696 + TBuf<0x40> formattable;
1.697 + formattable.Format(_L("HMAC Tests for %S"), &aHashType);
1.698 + test.Next(formattable);
1.699 + CMessageDigest* temp = aMD->ReplicateL();
1.700 + CleanupStack::PushL(temp);
1.701 + CHMAC* hmac=CHMAC::NewL(_L8("aaaaaaaa"), temp);
1.702 + CleanupStack::Pop(temp);
1.703 + CleanupStack::PushL(hmac);
1.704 +
1.705 +// For each of the available digests
1.706 + FunctionalityTestL(hmac); //JCS for now
1.707 + HMACVectorTestL(aMD,aFilename);
1.708 + OOMTestL(hmac);
1.709 +
1.710 + CleanupStack::PopAndDestroy(hmac);
1.711 + }
1.712 +
1.713 +void MD2TestsL()
1.714 + {
1.715 + CMD2* md2;
1.716 + md2=CMD2::NewL();
1.717 + CleanupStack::PushL(md2);
1.718 +
1.719 + FunctionalityTestL(md2);
1.720 +
1.721 + VectorTestL(md2,_L("md2.dat"));
1.722 +
1.723 + OOMTestL(md2);
1.724 +
1.725 + CleanupStack::PopAndDestroy(md2);
1.726 +
1.727 +// Problem reported by Jal Panvel, 17-12-1999.
1.728 +// Report by email, CSHA1::Hash() returning zero length descriptor
1.729 +//
1.730 +// This was caused by failure to set internal hash descriptor length on setup, this
1.731 +// problem was present in all hashes except MD2 which set it up correctly.
1.732 +// Fixed 17-12-1999.
1.733 + test.Next(_L("Fixed bugs Tests"));
1.734 + md2 = CMD2::NewL();
1.735 + CleanupStack::PushL(md2);
1.736 + TPtrC8 data(_L8("The quick brown fox jumped over the lazy dog"));
1.737 + TBuf8<128> hash;
1.738 + hash = md2->Hash(data);
1.739 + test(hash.Length() == md2->HashSize());
1.740 +
1.741 + HMACTestsL(md2,_L("hmacmd2.dat"), _L("md2"));
1.742 + CleanupStack::PopAndDestroy(md2);
1.743 + }
1.744 +
1.745 +void MD5TestsL()
1.746 + {
1.747 + CMD5* md5;
1.748 + md5=CMD5::NewL();
1.749 + CleanupStack::PushL(md5);
1.750 +
1.751 + FunctionalityTestL(md5);
1.752 +
1.753 + VectorTestL(md5,_L("md5.dat"));
1.754 +
1.755 + OOMTestL(md5);
1.756 +
1.757 + CleanupStack::PopAndDestroy(md5);
1.758 + test.Next(_L("Fixed bugs Tests"));
1.759 +// Problem reported by Jal Panvel, 17-12-1999.
1.760 +// Report by email, CSHA1::Hash() returning zero length descriptor
1.761 +//
1.762 +// This was caused by failure to set internal hash descriptor length on setup, this
1.763 +// problem was present in all hashes except MD2 which set it up correctly.
1.764 +// Fixed 17-12-1999.
1.765 + CMD5* md = CMD5::NewL();
1.766 + CleanupStack::PushL(md);
1.767 + TPtrC8 data(_L8("The quick brown fox jumped over the lazy dog"));
1.768 + TBuf8<128> hash;
1.769 + hash = md->Hash(data);
1.770 + test(hash.Length() == md->HashSize());
1.771 +
1.772 + HMACTestsL(md,_L("hmacmd5.dat"), _L("md5"));
1.773 + CleanupStack::PopAndDestroy(md);
1.774 +
1.775 + // Test for DEF001510 "TLS - Receives Disconnect Indication during hands..."
1.776 + CMD5* testHasher = CMD5::NewL();
1.777 + CleanupStack::PushL(testHasher);
1.778 + TPtrC8 client(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A4"));
1.779 + TPtrC8 server(_L8("3E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
1.780 +
1.781 + HBufC8* clientData = client.AllocLC();
1.782 + HBufC8* serverData = server.AllocLC();
1.783 +
1.784 + Hex(*clientData);
1.785 + Hex(*serverData);
1.786 +
1.787 + testHasher->Hash(*clientData);
1.788 + testHasher->Hash(*serverData);
1.789 +
1.790 + TBuf8<32> md5buf;
1.791 + md5buf.Copy(testHasher->Hash(TPtrC8(0,0)));
1.792 +
1.793 + testHasher->Reset();
1.794 +
1.795 +// Now hash in one chunk
1.796 + TPtrC8 all(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A43E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
1.797 + HBufC8* allData = all.AllocLC();
1.798 + Hex(*allData);
1.799 +
1.800 + TBuf8<32> allbuf;
1.801 + allbuf = testHasher->Hash(*allData);
1.802 + test(allbuf.Compare(md5buf)==0);
1.803 + CleanupStack::PopAndDestroy(4, testHasher);
1.804 + }
1.805 +
1.806 +
1.807 +void MD4TestsL()
1.808 + {
1.809 + CMD4* md4;
1.810 + md4=CMD4::NewL();
1.811 + CleanupStack::PushL(md4);
1.812 +
1.813 + FunctionalityTestL(md4);
1.814 +
1.815 + VectorTestL(md4,_L("md4.dat"));
1.816 +
1.817 + OOMTestL(md4);
1.818 +
1.819 + CleanupStack::PopAndDestroy(md4);
1.820 +
1.821 + //Copied the tests carried out for other Digest generating algorithms (MD5, SHA1)
1.822 + CMD4* md = CMD4::NewL();
1.823 + CleanupStack::PushL(md);
1.824 + TPtrC8 data(_L8("The quick brown fox jumped over the lazy dog"));
1.825 + TBuf8<128> hash;
1.826 + hash = md->Hash(data);
1.827 + test(hash.Length() == md->HashSize());
1.828 +
1.829 + HMACTestsL(md,_L("hmacmd4.dat"), _L("md4"));
1.830 + CleanupStack::PopAndDestroy(md);
1.831 +
1.832 + CMD4* testHasher = CMD4::NewL();
1.833 + CleanupStack::PushL(testHasher);
1.834 + TPtrC8 client(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A4"));
1.835 + TPtrC8 server(_L8("3E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
1.836 +
1.837 + HBufC8* clientData = client.AllocLC();
1.838 + HBufC8* serverData = server.AllocLC();
1.839 +
1.840 + Hex(*clientData);
1.841 + Hex(*serverData);
1.842 +
1.843 + testHasher->Hash(*clientData);
1.844 + testHasher->Hash(*serverData);
1.845 +
1.846 + TBuf8<32> md4buf;
1.847 + md4buf.Copy(testHasher->Hash(TPtrC8(0,0)));
1.848 +
1.849 + testHasher->Reset();
1.850 +
1.851 +// Now hash in one chunk
1.852 + TPtrC8 all(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A43E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
1.853 + HBufC8* allData = all.AllocLC();
1.854 + Hex(*allData);
1.855 +
1.856 + TBuf8<32> allbuf;
1.857 + allbuf = testHasher->Hash(*allData);
1.858 + test(allbuf.Compare(md4buf)==0);
1.859 + CleanupStack::PopAndDestroy(4, testHasher);
1.860 +
1.861 + //Tests for the Factory Method CMessageDigestFactory
1.862 + CMessageDigest* messageDigest = CMessageDigestFactory::NewDigestLC(CMessageDigest::EMD4);
1.863 + VectorTestL(messageDigest,_L("md4.dat"));
1.864 + CleanupStack::PopAndDestroy(messageDigest);
1.865 +
1.866 +
1.867 + }
1.868 +
1.869 +void SHA1TestsL()
1.870 + {
1.871 + CSHA1* sha;
1.872 + sha=CSHA1::NewL();
1.873 + CleanupStack::PushL(sha);
1.874 +
1.875 + VectorTestL(sha,_L("sha1.dat"));
1.876 +
1.877 + FunctionalityTestL(sha);
1.878 +
1.879 + OOMTestL(sha);
1.880 +
1.881 + CleanupStack::PopAndDestroy(sha);
1.882 +
1.883 + test.Next(_L("Fixed bugs Tests"));
1.884 +// Problem reported by Jal Panvel, 17-12-1999.
1.885 +// Report by email, CSHA1::Hash() returning zero length descriptor
1.886 +//
1.887 +// This was caused by failure to set internal hash descriptor length on setup, this
1.888 +// problem was present in all hashes except MD2 which set it up correctly.
1.889 +// Fixed 17-12-1999.
1.890 +
1.891 + sha=CSHA1::NewL();
1.892 + CleanupStack::PushL(sha);
1.893 + TPtrC8 data(_L8("The quick brown fox jumped over the lazy dog"));
1.894 + TBuf8<128> hash;
1.895 + hash = sha->Hash(data);
1.896 + test(hash.Length() == sha->HashSize());
1.897 + sha->Reset();
1.898 +
1.899 + // Test for DEF001510 "TLS - Receives Disconnect Indication during hands..."
1.900 + CSHA1* testHasher = CSHA1::NewL();
1.901 + CleanupStack::PushL(testHasher);
1.902 + TPtrC8 client(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A4"));
1.903 + TPtrC8 server(_L8("3E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
1.904 +
1.905 + HBufC8* clientData = client.AllocLC();
1.906 + HBufC8* serverData = server.AllocLC();
1.907 +
1.908 + Hex(*clientData);
1.909 + Hex(*serverData);
1.910 +
1.911 +// Hash in 2 portions
1.912 + TBuf8<32> clientbuf;
1.913 + TBuf8<32> serverbuf;
1.914 + clientbuf = testHasher->Hash(*clientData);
1.915 + serverbuf = testHasher->Hash(*serverData);
1.916 +
1.917 + TBuf8<32> shabuf;
1.918 + shabuf.Copy(testHasher->Hash(TPtrC8(0,0)));
1.919 + testHasher->Reset();
1.920 +
1.921 +// Now hash in one chunk
1.922 + TPtrC8 all(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A43E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
1.923 + HBufC8* allData = all.AllocLC();
1.924 + Hex(*allData);
1.925 +
1.926 + TBuf8<32> allbuf;
1.927 + allbuf = testHasher->Hash(*allData);
1.928 +
1.929 + test(allbuf.Compare(shabuf)==0);
1.930 + CleanupStack::PopAndDestroy(4, testHasher);
1.931 +
1.932 + // Test hashing non-word aligned data - used to crash on arm
1.933 + TPtrC8 nonAlignedData = data.Mid(1);
1.934 + hash = sha->Final(nonAlignedData);
1.935 + test(hash.Length() == sha->HashSize());
1.936 + sha->Reset();
1.937 +
1.938 +// Test end
1.939 + HMACTestsL(sha,_L("hmacsha1.dat"), _L("sha1"));
1.940 + CleanupStack::PopAndDestroy(); // sha
1.941 + }
1.942 +
1.943 +void ExecuteSha2TestsL(CMessageDigest* aMD, const TDesC& aVector, const TDesC& aHMACVector, const TDesC& aHashType)
1.944 + {
1.945 + VectorTestL(aMD, aVector);
1.946 + FunctionalityTestL(aMD);
1.947 + OOMTestL(aMD);
1.948 + aMD->Reset();
1.949 +
1.950 + test.Next(_L("Fixed bugs Tests"));
1.951 +
1.952 + _LIT8(KTest1Data, "The quick brown fox jumped over the lazy dog");
1.953 + TBuf8<KMaxHashSize> hash;
1.954 + hash = aMD->Hash(KTest1Data());
1.955 + test(hash.Length() == aMD->HashSize());
1.956 + aMD->Reset();
1.957 +
1.958 + _LIT8(KClientData, "D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A4");
1.959 + _LIT8(KServerData, "3E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8");
1.960 +
1.961 + HBufC8* clientData = KClientData().AllocLC();
1.962 + HBufC8* serverData = KServerData().AllocLC();
1.963 +
1.964 + Hex(*clientData);
1.965 + Hex(*serverData);
1.966 +
1.967 +// Hash in 2 portions
1.968 + aMD->Hash(*clientData);
1.969 + aMD->Hash(*serverData);
1.970 +
1.971 + CleanupStack::PopAndDestroy(2, clientData);
1.972 +
1.973 + TBuf8<KMaxHashSize> shabuf;
1.974 + shabuf.Copy(aMD->Hash(KNullDesC8()));
1.975 + aMD->Reset();
1.976 +
1.977 +// Now hash in one chunk
1.978 + _LIT8(KAllData, "D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A43E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8");
1.979 + HBufC8* allData = KAllData().AllocLC();
1.980 + Hex(*allData);
1.981 +
1.982 + TBuf8<KMaxHashSize> allbuf;
1.983 + allbuf = aMD->Hash(*allData);
1.984 +
1.985 + test(allbuf.Compare(shabuf)==0);
1.986 + CleanupStack::PopAndDestroy(allData);
1.987 +
1.988 + // Test hashing non-word aligned data - used to crash on arm
1.989 + TPtrC8 nonAlignedData = KTest1Data().Mid(1);
1.990 + hash = aMD->Final(nonAlignedData);
1.991 + test(hash.Length() == aMD->HashSize());
1.992 + aMD->Reset();
1.993 +
1.994 +// Test end
1.995 + HMACTestsL(aMD, aHMACVector, aHashType);
1.996 + }
1.997 +
1.998 +_LIT(K224Algo, "SHA-224.dat");
1.999 +_LIT(K256Algo, "SHA-256.dat");
1.1000 +_LIT(K384Algo, "SHA-384.dat");
1.1001 +_LIT(K512Algo, "SHA-512.dat");
1.1002 +_LIT(K224Vector, "sha224.dat");
1.1003 +_LIT(K256Vector, "sha256.dat");
1.1004 +_LIT(K384Vector, "sha384.dat");
1.1005 +_LIT(K512Vector, "sha512.dat");
1.1006 +_LIT(K224HmacVector, "hmacsha224.dat");
1.1007 +_LIT(K256HmacVector, "hmacsha256.dat");
1.1008 +_LIT(K384HmacVector, "hmacsha384.dat");
1.1009 +_LIT(K512HmacVector, "hmacsha512.dat");
1.1010 +
1.1011 +const TDesC* gNames[] =
1.1012 + {
1.1013 + &K224Vector(),
1.1014 + &K224HmacVector(),
1.1015 + &K224Algo(),
1.1016 + &K256Vector(),
1.1017 + &K256HmacVector(),
1.1018 + &K256Algo(),
1.1019 + &K384Vector(),
1.1020 + &K384HmacVector(),
1.1021 + &K384Algo(),
1.1022 + &K512Vector(),
1.1023 + &K512HmacVector(),
1.1024 + &K512Algo(),
1.1025 + };
1.1026 +
1.1027 +
1.1028 +void SHA2TestsL(CMessageDigest::THashId aHashId)
1.1029 + {
1.1030 + CMessageDigest* md = CMessageDigestFactory::NewDigestLC(aHashId);
1.1031 + TInt pos = aHashId - CMessageDigest::ESHA224;
1.1032 + pos *= 3;
1.1033 + ExecuteSha2TestsL (md, *gNames[pos], *gNames[pos+1], *gNames[pos+2]);
1.1034 + CleanupStack::PopAndDestroy(md);
1.1035 + }
1.1036 +
1.1037 +void HashTests(void)
1.1038 + {
1.1039 + TInt32 testsFailed=0, testCount=0;
1.1040 +
1.1041 + test.Start(_L("MD4 Tests"));
1.1042 + TRAPD(r, MD4TestsL());
1.1043 + ++testCount;
1.1044 + if (r!=KErrNone)
1.1045 + {
1.1046 + test.Printf(_L("\r\nMD4 Tests failed error code =%d\r\n\r\n"),r);
1.1047 + ++testsFailed;
1.1048 + }
1.1049 +
1.1050 + test.Start(_L("SHA1 Tests"));
1.1051 + TRAP(r, SHA1TestsL());
1.1052 + ++testCount;
1.1053 + if (r!=KErrNone)
1.1054 + {
1.1055 + test.Printf(_L("\r\nSHA1 Tests failed error code =%d\r\n\r\n"),r);
1.1056 + ++testsFailed;
1.1057 + }
1.1058 +
1.1059 + test.Start(_L("SHA-224 Tests"));
1.1060 + TRAP(r, SHA2TestsL(CMessageDigest::ESHA224));
1.1061 + ++testCount;
1.1062 + if (r!=KErrNone)
1.1063 + {
1.1064 + test.Printf(_L("\r\nSHA-224 Tests failed error code =%d\r\n\r\n"),r);
1.1065 + ++testsFailed;
1.1066 + }
1.1067 +
1.1068 + test.Start(_L("SHA-256 Tests"));
1.1069 + TRAP(r, SHA2TestsL(CMessageDigest::ESHA256));
1.1070 + ++testCount;
1.1071 + if (r!=KErrNone)
1.1072 + {
1.1073 + test.Printf(_L("\r\nSHA-256 Tests failed error code =%d\r\n\r\n"),r);
1.1074 + ++testsFailed;
1.1075 + }
1.1076 +
1.1077 + test.Start(_L("SHA-384 Tests"));
1.1078 + TRAP(r, SHA2TestsL(CMessageDigest::ESHA384));
1.1079 + ++testCount;
1.1080 + if (r!=KErrNone)
1.1081 + {
1.1082 + test.Printf(_L("\r\nSHA-384 Tests failed error code =%d\r\n\r\n"),r);
1.1083 + ++testsFailed;
1.1084 + }
1.1085 +
1.1086 + test.Start(_L("SHA-512 Tests"));
1.1087 + TRAP(r, SHA2TestsL(CMessageDigest::ESHA512));
1.1088 + ++testCount;
1.1089 + if (r!=KErrNone)
1.1090 + {
1.1091 + test.Printf(_L("\r\nSHA-512 Tests failed error code =%d\r\n\r\n"),r);
1.1092 + ++testsFailed;
1.1093 + }
1.1094 +
1.1095 + test.Start(_L("MD5 Tests"));
1.1096 + TRAP(r, MD5TestsL());
1.1097 + ++testCount;
1.1098 + if (r!=KErrNone)
1.1099 + {
1.1100 + test.Printf(_L("\r\nMD5 Tests failed error code =%d\r\n\r\n"),r);
1.1101 + ++testsFailed;
1.1102 + }
1.1103 +
1.1104 + test.Start(_L("MD2 Tests"));
1.1105 + TRAP(r, MD2TestsL());
1.1106 + ++testCount;
1.1107 + if (r!=KErrNone)
1.1108 + {
1.1109 + test.Printf(_L("\r\nMD2 Tests failed error code =%d\r\n\r\n"),r);
1.1110 + ++testsFailed;
1.1111 + }
1.1112 +
1.1113 + test.Printf(_L("\r\n%d tests failed out of %d \r\n"),testsFailed,testCount);
1.1114 + test(testsFailed==0);
1.1115 + }
1.1116 +
1.1117 +
1.1118 +GLDEF_C TInt E32Main(void)
1.1119 +
1.1120 + {
1.1121 + CTrapCleanup* cleanup;
1.1122 + cleanup=CTrapCleanup::New();
1.1123 +
1.1124 + test.Start(_L(" @SYMTestCaseID:SEC-CRYPTOSPI-HASH-0035 Hash Algorithm Tests "));
1.1125 + CTestConsole* con=NULL;
1.1126 + TRAPD(ret, con=CTestConsole::NewL(test.Console()));
1.1127 + if(ret != KErrNone)
1.1128 + {
1.1129 + return ret;
1.1130 + }
1.1131 + RFs fs;
1.1132 +
1.1133 + fs.Connect();
1.1134 + RFile* file;
1.1135 + file=new (ELeave) RFile;
1.1136 +
1.1137 + TDriveUnit sysDrive (fs.GetSystemDrive());
1.1138 + TBuf<24> hashLogFile (sysDrive.Name());
1.1139 + hashLogFile.Append(_L("\\HashLog.txt"));
1.1140 +
1.1141 + file->Replace(fs,hashLogFile,EFileShareAny|EFileWrite);
1.1142 + con->SetLogFile(file);
1.1143 +
1.1144 + test.SetConsole(con);
1.1145 + __UHEAP_MARK;
1.1146 + HashTests();
1.1147 + __UHEAP_MARKEND;
1.1148 +
1.1149 + test.End();
1.1150 + test.Close();
1.1151 + delete cleanup;
1.1152 + return(KErrNone);
1.1153 + }