1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcrypto/test/thash/thash.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,1152 @@
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 + test.Printf(_L("\r\n"));
1.235 +
1.236 + if (!testSuccess)
1.237 + User::Leave(KErrGeneral);
1.238 + }
1.239 +
1.240 +void VectorTestL(CMessageDigest* aMD,const TDesC& aFilename)
1.241 + {
1.242 + test.Next(_L("Test Vector tests - original API"));
1.243 + TBool finished=EFalse;
1.244 + TBool testSuccess = ETrue;
1.245 + CTestData* data = CTestData::NewL(aFilename);
1.246 + CleanupStack::PushL(data);
1.247 +
1.248 + while (!finished)
1.249 + {
1.250 + switch (data->Type())
1.251 + {
1.252 + case CTestData::EMessage:
1.253 + {
1.254 + break;
1.255 + }
1.256 + //This is added to read large input data from the files.
1.257 + case CTestData::EFileName:
1.258 + {
1.259 + CMessageDigest* md=aMD->ReplicateL();
1.260 + CleanupStack::PushL(md);
1.261 +
1.262 + //get the filename from the .dat file
1.263 + HBufC8* filename = (*data)[1];
1.264 + User::LeaveIfNull(filename);
1.265 + CleanupStack::PushL(filename);
1.266 +
1.267 + HBufC8* output= (*data)[2];
1.268 + User::LeaveIfNull(output);
1.269 + CleanupStack::PushL(output);
1.270 + Hex(*output);
1.271 +
1.272 + HBufC16* inputFileName = CnvUtfConverter::ConvertToUnicodeFromUtf8L(*filename);
1.273 +
1.274 + RFs fs;
1.275 + RFile file;
1.276 + CleanupClosePushL(fs);
1.277 + User::LeaveIfError(fs.Connect());
1.278 + TDriveUnit sysDrive(fs.GetSystemDrive());
1.279 + TBuf<24> filePath (sysDrive.Name());
1.280 + filePath.Append(_L("\\thash\\"));
1.281 + User::LeaveIfError(fs.SetSessionPath(filePath));
1.282 + CleanupClosePushL(file);
1.283 + User::LeaveIfError(file.Open(fs,*inputFileName,EFileShareAny|EFileRead));
1.284 + // read into iFile
1.285 + TInt size=0;
1.286 + file.Size(size);
1.287 + HBufC8* fileContents=HBufC8::NewMaxL(size);
1.288 +
1.289 + TPtr8 ptr=fileContents->Des();
1.290 + User::LeaveIfError(file.Read(ptr));
1.291 + CleanupStack::PopAndDestroy(2, &fs);
1.292 + CleanupStack::PushL(fileContents);
1.293 + delete inputFileName;
1.294 +
1.295 + TPtrC8 digest = md->Hash(*fileContents);
1.296 + if (digest!=(*output))
1.297 + {
1.298 + test.Next(_L("Digest Not Equal"));
1.299 + testSuccess = EFalse;
1.300 + }
1.301 +
1.302 + md->Reset();
1.303 +
1.304 + TPtrC8 digest2 = md->Hash(*fileContents);
1.305 + if (digest2!=(*output))
1.306 + {
1.307 + testSuccess = EFalse;
1.308 + }
1.309 +
1.310 + // Now try this in 2 half sections (just a quick check)
1.311 + md->Reset();
1.312 +
1.313 + TInt inputLen = fileContents->Length();
1.314 + if (inputLen > 1)
1.315 + {
1.316 + TInt leftHandLen = inputLen/2;
1.317 + TPtrC8 left = fileContents->Left(leftHandLen);
1.318 + TPtrC8 right = fileContents->Right(inputLen - leftHandLen);
1.319 +
1.320 + TPtrC8 halfDigest = md->Hash(left);
1.321 + if (halfDigest.Size()==0) // Unnecessary test, but removes
1.322 + User::Leave(KErrAbort); // a warning about not using halfDigest
1.323 +
1.324 + TPtrC8 wholeDigest = md->Hash(right);
1.325 +
1.326 + if (wholeDigest!=(*output))
1.327 + {
1.328 + testSuccess = EFalse;
1.329 + }
1.330 + }
1.331 +
1.332 + md->Reset();
1.333 + if (md->Final(*fileContents)!=(*output))
1.334 + {
1.335 + testSuccess = EFalse;
1.336 + }
1.337 +
1.338 + // Now try this in 2 half sections (just a quick check)
1.339 + md->Reset();
1.340 + inputLen = fileContents->Length();
1.341 + if (inputLen > 1)
1.342 + {
1.343 + TInt leftHandLen = inputLen/2;
1.344 + TPtrC8 left = fileContents->Left(leftHandLen);
1.345 + TPtrC8 right = fileContents->Right(inputLen - leftHandLen);
1.346 +
1.347 + md->Update(left);
1.348 + TPtrC8 wholeDigest = md->Final(right);
1.349 +
1.350 + if (wholeDigest!=(*output))
1.351 + {
1.352 + testSuccess = EFalse;
1.353 + }
1.354 + }
1.355 + CleanupStack::PopAndDestroy(4, md);//md,filename,output,fileContents
1.356 +
1.357 + break;
1.358 + }
1.359 + case CTestData::EData:
1.360 + {
1.361 + CMessageDigest* md=aMD->ReplicateL();
1.362 + CleanupStack::PushL(md);
1.363 +
1.364 + HBufC8* input = (*data)[0];
1.365 + User::LeaveIfNull(input);
1.366 + CleanupStack::PushL(input);
1.367 +
1.368 + HBufC8* output= (*data)[1];
1.369 + User::LeaveIfNull(output);
1.370 + CleanupStack::PushL(output);
1.371 + Hex(*input);
1.372 + Hex(*output);
1.373 +
1.374 + TPtrC8 digest = md->Hash(*input);
1.375 + if (digest!=(*output))
1.376 + {
1.377 + testSuccess = EFalse;
1.378 + }
1.379 +
1.380 + md->Reset();
1.381 +
1.382 + TPtrC8 digest2 = md->Hash(*input);
1.383 + if (digest2!=(*output))
1.384 + {
1.385 + testSuccess = EFalse;
1.386 + }
1.387 +
1.388 + // Now try this in 2 half sections (just a quick check)
1.389 + md->Reset();
1.390 +
1.391 + TInt inputLen = input->Length();
1.392 + if (inputLen > 1)
1.393 + {
1.394 + TInt leftHandLen = inputLen/2;
1.395 + TPtrC8 left = input->Left(leftHandLen);
1.396 + TPtrC8 right = input->Right(inputLen - leftHandLen);
1.397 +
1.398 + TPtrC8 halfDigest = md->Hash(left);
1.399 + if (halfDigest.Size()==0) // Unnecessary test, but removes
1.400 + User::Leave(KErrAbort); // a warning about not using halfDigest
1.401 +
1.402 + TPtrC8 wholeDigest = md->Hash(right);
1.403 +
1.404 + if (wholeDigest!=(*output))
1.405 + {
1.406 + testSuccess = EFalse;
1.407 + }
1.408 + }
1.409 +
1.410 + ///////////////////////////////////////////////////////////////
1.411 + // Now test the new API - Update/Final functions
1.412 + ///////////////////////////////////////////////////////////////
1.413 + md->Reset();
1.414 + if (md->Final(*input)!=(*output))
1.415 + {
1.416 + testSuccess = EFalse;
1.417 + }
1.418 +
1.419 + // Now try this in 2 half sections (just a quick check)
1.420 + md->Reset();
1.421 + inputLen = input->Length();
1.422 + if (inputLen > 1)
1.423 + {
1.424 + TInt leftHandLen = inputLen/2;
1.425 + TPtrC8 left = input->Left(leftHandLen);
1.426 + TPtrC8 right = input->Right(inputLen - leftHandLen);
1.427 +
1.428 + md->Update(left);
1.429 + TPtrC8 wholeDigest = md->Final(right);
1.430 +
1.431 + if (wholeDigest!=(*output))
1.432 + {
1.433 + testSuccess = EFalse;
1.434 + }
1.435 + }
1.436 + CleanupStack::PopAndDestroy(3);
1.437 + break;
1.438 + }
1.439 + case CTestData::EFinished:
1.440 + finished=ETrue;
1.441 + break;
1.442 + default:
1.443 + test.Printf(_L("Error in data file\r\n"));
1.444 + break;
1.445 + }
1.446 + };
1.447 +
1.448 + CleanupStack::PopAndDestroy(data);
1.449 +
1.450 + if (!testSuccess)
1.451 + User::Leave(KErrGeneral);
1.452 + }
1.453 +
1.454 +void OOMTestL(CMessageDigest* aMD)
1.455 + {
1.456 + test.Next(_L("Out of memory test"));
1.457 + TInt err = KErrNoMemory;
1.458 + TInt nextFailure=0;
1.459 + gInOOMTest=ETrue;
1.460 + while (err!=KErrNone)
1.461 + {
1.462 + __UHEAP_MARK;
1.463 + __UHEAP_FAILNEXT(nextFailure);
1.464 + TRAP(err,FunctionalityTestL(aMD));
1.465 + __UHEAP_MARKEND;
1.466 + nextFailure++;
1.467 + }
1.468 + __UHEAP_RESET;
1.469 + gInOOMTest=EFalse;
1.470 + }
1.471 +
1.472 +void HMACVectorTestL(CMessageDigest* aMD,const TDesC& aFilename)
1.473 + {
1.474 + test.Next(_L("HMAC Test Vector tests"));
1.475 + CTestData* data = CTestData::NewL(aFilename);
1.476 + CleanupStack::PushL(data);
1.477 +
1.478 + TBool finished=EFalse;
1.479 + TBool testSuccess = ETrue;
1.480 +
1.481 + while (!finished)
1.482 + {
1.483 + switch (data->Type())
1.484 + {
1.485 + case CTestData::EMessage:
1.486 + {
1.487 + break;
1.488 + }
1.489 + case CTestData::EFileName:
1.490 + {
1.491 + //get the filename from the .dat file
1.492 + HBufC8* filename = (*data)[1];
1.493 + User::LeaveIfNull(filename);
1.494 + CleanupStack::PushL(filename);
1.495 +
1.496 + HBufC8* key= (*data)[2];
1.497 + User::LeaveIfNull(key);
1.498 + CleanupStack::PushL(key);
1.499 +
1.500 + HBufC16* inputFileName = CnvUtfConverter::ConvertToUnicodeFromUtf8L(*filename);
1.501 +
1.502 + HBufC8* output = (*data)[3];
1.503 + User::LeaveIfNull(output);
1.504 + CleanupStack::PushL(output);
1.505 +
1.506 + Hex(*key);
1.507 + Hex(*output);
1.508 +
1.509 + RFs fs;
1.510 + RFile file;
1.511 + CleanupClosePushL(fs);
1.512 + User::LeaveIfError(fs.Connect());
1.513 + TDriveUnit sysDrive(fs.GetSystemDrive());
1.514 + TBuf<24> filePath (sysDrive.Name());
1.515 + filePath.Append(_L("\\thash\\"));
1.516 + User::LeaveIfError(fs.SetSessionPath(filePath));
1.517 + CleanupClosePushL(file);
1.518 + User::LeaveIfError(file.Open(fs,*inputFileName,EFileShareAny|EFileRead));
1.519 + // read into iFile
1.520 + TInt size=0;
1.521 + file.Size(size);
1.522 + HBufC8* fileContents=HBufC8::NewMaxL(size);
1.523 +
1.524 + TPtr8 ptr=fileContents->Des();
1.525 + User::LeaveIfError(file.Read(ptr));
1.526 + CleanupStack::PopAndDestroy(2, &fs);
1.527 + CleanupStack::PushL(fileContents);
1.528 + delete inputFileName;
1.529 +
1.530 + CMessageDigest* temp = aMD->ReplicateL();
1.531 + CleanupStack::PushL(temp);
1.532 + CMessageDigest* md = CHMAC::NewL(*key, temp);
1.533 + CleanupStack::Pop(temp); // Now owned by md
1.534 +
1.535 + TPtrC8 digest = md->Hash(*fileContents);
1.536 + if (digest!=(*output))
1.537 + {
1.538 + testSuccess = EFalse;
1.539 + }
1.540 +
1.541 + // Now try this in 2 half sections (just a quick check)
1.542 + md->Reset();
1.543 +
1.544 + TInt inputLen = fileContents->Length();
1.545 + if (inputLen > 1)
1.546 + {
1.547 + TInt leftHandLen = inputLen/2;
1.548 + TPtrC8 left = fileContents->Left(leftHandLen);
1.549 + TPtrC8 right = fileContents->Right(inputLen - leftHandLen);
1.550 +
1.551 + TPtrC8 halfDigest = md->Hash(left);
1.552 + if (halfDigest.Size()==0) // Unnecessary test, but removes
1.553 + User::Leave(KErrAbort); // a warning about not using halfDigest
1.554 + TPtrC8 wholeDigest = md->Hash(right);
1.555 +
1.556 + if (wholeDigest!=(*output))
1.557 + {
1.558 + testSuccess = EFalse;
1.559 + }
1.560 + }
1.561 +
1.562 + md->Reset();
1.563 + TPtrC8 finalDigest = md->Final(*fileContents);
1.564 + if (finalDigest!=(*output))
1.565 + {
1.566 + testSuccess = EFalse;
1.567 + }
1.568 +
1.569 + // Now try this in 2 half sections (just a quick check)
1.570 + md->Reset();
1.571 +
1.572 + inputLen = fileContents->Length();
1.573 + if (inputLen > 1)
1.574 + {
1.575 + TInt leftHandLen = inputLen/2;
1.576 + TPtrC8 left = fileContents->Left(leftHandLen);
1.577 + TPtrC8 right = fileContents->Right(inputLen - leftHandLen);
1.578 +
1.579 + md->Update(left);
1.580 + TPtrC8 wholeDigest = md->Final(right);
1.581 +
1.582 + if (wholeDigest!=(*output))
1.583 + {
1.584 + testSuccess = EFalse;
1.585 + }
1.586 + }
1.587 + delete md;
1.588 +
1.589 + CleanupStack::PopAndDestroy(4, filename); // filename, key, output,fileContents
1.590 + break;
1.591 + }
1.592 + case CTestData::EData:
1.593 + {
1.594 + HBufC8* input = (*data)[0];
1.595 + User::LeaveIfNull(input);
1.596 + CleanupStack::PushL(input);
1.597 +
1.598 + HBufC8* key = (*data)[1];
1.599 + User::LeaveIfNull(key);
1.600 + CleanupStack::PushL(key);
1.601 +
1.602 + HBufC8* output = (*data)[2];
1.603 + User::LeaveIfNull(output);
1.604 + CleanupStack::PushL(output);
1.605 +
1.606 + Hex(*input);
1.607 + Hex(*key);
1.608 + Hex(*output);
1.609 +
1.610 + CMessageDigest* temp = aMD->ReplicateL();
1.611 + CleanupStack::PushL(temp);
1.612 + CMessageDigest* md = CHMAC::NewL(*key, temp);
1.613 + CleanupStack::Pop(temp); // Now owned by md
1.614 +
1.615 + TPtrC8 digest = md->Hash(*input);
1.616 + if (digest!=(*output))
1.617 + {
1.618 + testSuccess = EFalse;
1.619 + }
1.620 +
1.621 + // Now try this in 2 half sections (just a quick check)
1.622 + md->Reset();
1.623 +
1.624 + TInt inputLen = input->Length();
1.625 + if (inputLen > 1)
1.626 + {
1.627 + TInt leftHandLen = inputLen/2;
1.628 + TPtrC8 left = input->Left(leftHandLen);
1.629 + TPtrC8 right = input->Right(inputLen - leftHandLen);
1.630 +
1.631 + TPtrC8 halfDigest = md->Hash(left);
1.632 + if (halfDigest.Size()==0) // Unnecessary test, but removes
1.633 + User::Leave(KErrAbort); // a warning about not using halfDigest
1.634 + TPtrC8 wholeDigest = md->Hash(right);
1.635 +
1.636 + if (wholeDigest!=(*output))
1.637 + {
1.638 + testSuccess = EFalse;
1.639 + }
1.640 + }
1.641 +
1.642 +///////////////////////////////////////////////////////////////
1.643 +// Now test the new API - Update/Final functions
1.644 +///////////////////////////////////////////////////////////////
1.645 + md->Reset();
1.646 + TPtrC8 finalDigest = md->Final(*input);
1.647 + if (finalDigest!=(*output))
1.648 + {
1.649 + testSuccess = EFalse;
1.650 + }
1.651 +
1.652 + // Now try this in 2 half sections (just a quick check)
1.653 + md->Reset();
1.654 +
1.655 + inputLen = input->Length();
1.656 + if (inputLen > 1)
1.657 + {
1.658 + TInt leftHandLen = inputLen/2;
1.659 + TPtrC8 left = input->Left(leftHandLen);
1.660 + TPtrC8 right = input->Right(inputLen - leftHandLen);
1.661 +
1.662 + md->Update(left);
1.663 + TPtrC8 wholeDigest = md->Final(right);
1.664 +
1.665 + if (wholeDigest!=(*output))
1.666 + {
1.667 + testSuccess = EFalse;
1.668 + }
1.669 + }
1.670 + delete md;
1.671 + CleanupStack::PopAndDestroy(3); // input, key, output
1.672 + break;
1.673 + }
1.674 + case CTestData::EFinished:
1.675 + finished=ETrue;
1.676 + break;
1.677 + default:
1.678 + test.Printf(_L("Error in data file\r\n"));
1.679 + break;
1.680 + }
1.681 + };
1.682 +
1.683 + CleanupStack::PopAndDestroy(data);
1.684 +
1.685 + if (!testSuccess)
1.686 + User::Leave(KErrGeneral);
1.687 + }
1.688 +
1.689 +void HMACTestsL(CMessageDigest* aMD,const TDesC& aFilename, const TDesC& aHashType)
1.690 + {
1.691 + TBuf<0x40> formattable;
1.692 + formattable.Format(_L("HMAC Tests for %S"), &aHashType);
1.693 + test.Next(formattable);
1.694 + CMessageDigest* temp = aMD->ReplicateL();
1.695 + CleanupStack::PushL(temp);
1.696 + CHMAC* hmac=CHMAC::NewL(_L8("aaaaaaaa"), temp);
1.697 + CleanupStack::Pop(temp);
1.698 + CleanupStack::PushL(hmac);
1.699 +
1.700 +// For each of the available digests
1.701 + FunctionalityTestL(hmac); //JCS for now
1.702 + HMACVectorTestL(aMD,aFilename);
1.703 + OOMTestL(hmac);
1.704 +
1.705 + CleanupStack::PopAndDestroy(hmac);
1.706 + }
1.707 +
1.708 +
1.709 +void MD2TestsL()
1.710 + {
1.711 + CMD2* md2;
1.712 + md2=CMD2::NewL();
1.713 + CleanupStack::PushL(md2);
1.714 +
1.715 + FunctionalityTestL(md2);
1.716 +
1.717 + VectorTestL(md2,_L("md2.dat"));
1.718 +
1.719 + OOMTestL(md2);
1.720 +
1.721 + CleanupStack::PopAndDestroy(md2);
1.722 +
1.723 +// Problem reported by Jal Panvel, 17-12-1999.
1.724 +// Report by email, CSHA1::Hash() returning zero length descriptor
1.725 +//
1.726 +// This was caused by failure to set internal hash descriptor length on setup, this
1.727 +// problem was present in all hashes except MD2 which set it up correctly.
1.728 +// Fixed 17-12-1999.
1.729 + test.Next(_L("Fixed bugs Tests"));
1.730 + md2 = CMD2::NewL();
1.731 + CleanupStack::PushL(md2);
1.732 + TPtrC8 data(_L8("The quick brown fox jumped over the lazy dog"));
1.733 + TBuf8<128> hash;
1.734 + hash = md2->Hash(data);
1.735 + test(hash.Length() == md2->HashSize());
1.736 +
1.737 + HMACTestsL(md2,_L("hmacmd2.dat"), _L("md2"));
1.738 + CleanupStack::PopAndDestroy(md2);
1.739 + }
1.740 +
1.741 +void MD5TestsL()
1.742 + {
1.743 + CMD5* md5;
1.744 + md5=CMD5::NewL();
1.745 + CleanupStack::PushL(md5);
1.746 +
1.747 + FunctionalityTestL(md5);
1.748 +
1.749 + VectorTestL(md5,_L("md5.dat"));
1.750 +
1.751 + OOMTestL(md5);
1.752 +
1.753 + CleanupStack::PopAndDestroy(md5);
1.754 + test.Next(_L("Fixed bugs Tests"));
1.755 +// Problem reported by Jal Panvel, 17-12-1999.
1.756 +// Report by email, CSHA1::Hash() returning zero length descriptor
1.757 +//
1.758 +// This was caused by failure to set internal hash descriptor length on setup, this
1.759 +// problem was present in all hashes except MD2 which set it up correctly.
1.760 +// Fixed 17-12-1999.
1.761 + CMD5* md = CMD5::NewL();
1.762 + CleanupStack::PushL(md);
1.763 + TPtrC8 data(_L8("The quick brown fox jumped over the lazy dog"));
1.764 + TBuf8<128> hash;
1.765 + hash = md->Hash(data);
1.766 + test(hash.Length() == md->HashSize());
1.767 +
1.768 + HMACTestsL(md5,_L("hmacmd5.dat"), _L("md5"));
1.769 + CleanupStack::PopAndDestroy(md);
1.770 +
1.771 + // Test for DEF001510 "TLS - Receives Disconnect Indication during hands..."
1.772 + CMD5* testHasher = CMD5::NewL();
1.773 + CleanupStack::PushL(testHasher);
1.774 + TPtrC8 client(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A4"));
1.775 + TPtrC8 server(_L8("3E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
1.776 +
1.777 + HBufC8* clientData = client.AllocLC();
1.778 + HBufC8* serverData = server.AllocLC();
1.779 +
1.780 + Hex(*clientData);
1.781 + Hex(*serverData);
1.782 +
1.783 + testHasher->Hash(*clientData);
1.784 + testHasher->Hash(*serverData);
1.785 +
1.786 + TBuf8<32> md5buf;
1.787 + md5buf.Copy(testHasher->Hash(TPtrC8(0,0)));
1.788 +
1.789 + testHasher->Reset();
1.790 +
1.791 +// Now hash in one chunk
1.792 + TPtrC8 all(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A43E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
1.793 + HBufC8* allData = all.AllocLC();
1.794 + Hex(*allData);
1.795 +
1.796 + TBuf8<32> allbuf;
1.797 + allbuf = testHasher->Hash(*allData);
1.798 + test(allbuf.Compare(md5buf)==0);
1.799 + CleanupStack::PopAndDestroy(4, testHasher);
1.800 + }
1.801 +
1.802 +// Test for the MD4 Message Digest Algorithm API's
1.803 +void MD4TestsL()
1.804 + {
1.805 + CMD4* md4;
1.806 + md4=CMD4::NewL();
1.807 + CleanupStack::PushL(md4);
1.808 +
1.809 + FunctionalityTestL(md4);
1.810 +
1.811 + VectorTestL(md4,_L("md4.dat"));
1.812 +
1.813 + OOMTestL(md4);
1.814 +
1.815 + CleanupStack::PopAndDestroy(md4);
1.816 +
1.817 + //Test to check the Hash Size.
1.818 + CMD4* md = CMD4::NewL();
1.819 + CleanupStack::PushL(md);
1.820 + TPtrC8 data(_L8("The quick brown fox jumped over the lazy dog"));
1.821 + TBuf8<128> hash;
1.822 + hash = md->Hash(data);
1.823 + test(hash.Length() == md->HashSize());
1.824 +
1.825 + HMACTestsL(md,_L("hmacmd4.dat"), _L("md4"));
1.826 + CleanupStack::PopAndDestroy(md);
1.827 +
1.828 + //Tests carried for other Message Digest Algorithms copied(SHA1 and MD5)
1.829 + //Here Input Data is given in two parts and as a whole and the digest generated is compared.
1.830 + CMD4* testHasher = CMD4::NewL();
1.831 + CleanupStack::PushL(testHasher);
1.832 + TPtrC8 client(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A4"));
1.833 + TPtrC8 server(_L8("3E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
1.834 +
1.835 + HBufC8* clientData = client.AllocLC();
1.836 + HBufC8* serverData = server.AllocLC();
1.837 +
1.838 + Hex(*clientData);
1.839 + Hex(*serverData);
1.840 +
1.841 + testHasher->Hash(*clientData);
1.842 + testHasher->Hash(*serverData);
1.843 +
1.844 + TBuf8<32> md4buf;
1.845 + md4buf.Copy(testHasher->Hash(TPtrC8(0,0)));
1.846 +
1.847 + testHasher->Reset();
1.848 +
1.849 +// Now hash in one chunk
1.850 + TPtrC8 all(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A43E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
1.851 + HBufC8* allData = all.AllocLC();
1.852 + Hex(*allData);
1.853 +
1.854 + TBuf8<32> allbuf;
1.855 + allbuf = testHasher->Hash(*allData);
1.856 + test(allbuf.Compare(md4buf)==0);
1.857 + CleanupStack::PopAndDestroy(4, testHasher);
1.858 +
1.859 + //Tests for the Factory Method CMessageDigestFactory
1.860 + CMessageDigest* messageDigest = CMessageDigestFactory::NewDigestLC(CMessageDigest::EMD4);
1.861 + VectorTestL(messageDigest,_L("md4.dat"));
1.862 + CleanupStack::PopAndDestroy(messageDigest);
1.863 + }
1.864 +
1.865 +
1.866 +void SHA1TestsL()
1.867 + {
1.868 + CSHA1* sha;
1.869 + sha=CSHA1::NewL();
1.870 + CleanupStack::PushL(sha);
1.871 +
1.872 + VectorTestL(sha,_L("sha1.dat"));
1.873 +
1.874 + FunctionalityTestL(sha);
1.875 +
1.876 + OOMTestL(sha);
1.877 +
1.878 + CleanupStack::PopAndDestroy(sha);
1.879 +
1.880 + test.Next(_L("Fixed bugs Tests"));
1.881 +// Problem reported by Jal Panvel, 17-12-1999.
1.882 +// Report by email, CSHA1::Hash() returning zero length descriptor
1.883 +//
1.884 +// This was caused by failure to set internal hash descriptor length on setup, this
1.885 +// problem was present in all hashes except MD2 which set it up correctly.
1.886 +// Fixed 17-12-1999.
1.887 +
1.888 + sha=CSHA1::NewL();
1.889 + CleanupStack::PushL(sha);
1.890 + TPtrC8 data(_L8("The quick brown fox jumped over the lazy dog"));
1.891 + TBuf8<128> hash;
1.892 + hash = sha->Hash(data);
1.893 + test(hash.Length() == sha->HashSize());
1.894 + sha->Reset();
1.895 +
1.896 + // Test for DEF001510 "TLS - Receives Disconnect Indication during hands..."
1.897 + CSHA1* testHasher = CSHA1::NewL();
1.898 + CleanupStack::PushL(testHasher);
1.899 + TPtrC8 client(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A4"));
1.900 + TPtrC8 server(_L8("3E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
1.901 +
1.902 + HBufC8* clientData = client.AllocLC();
1.903 + HBufC8* serverData = server.AllocLC();
1.904 +
1.905 + Hex(*clientData);
1.906 + Hex(*serverData);
1.907 +
1.908 +// Hash in 2 portions
1.909 + TBuf8<32> clientbuf;
1.910 + TBuf8<32> serverbuf;
1.911 + clientbuf = testHasher->Hash(*clientData);
1.912 + serverbuf = testHasher->Hash(*serverData);
1.913 +
1.914 + TBuf8<32> shabuf;
1.915 + shabuf.Copy(testHasher->Hash(TPtrC8(0,0)));
1.916 + testHasher->Reset();
1.917 +
1.918 +// Now hash in one chunk
1.919 + TPtrC8 all(_L8("D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A43E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8"));
1.920 + HBufC8* allData = all.AllocLC();
1.921 + Hex(*allData);
1.922 +
1.923 + TBuf8<32> allbuf;
1.924 + allbuf = testHasher->Hash(*allData);
1.925 +
1.926 + test(allbuf.Compare(shabuf)==0);
1.927 + CleanupStack::PopAndDestroy(4, testHasher);
1.928 +
1.929 + // Test hashing non-word aligned data - used to crash on arm
1.930 + TPtrC8 nonAlignedData = data.Mid(1);
1.931 + hash = sha->Final(nonAlignedData);
1.932 + test(hash.Length() == sha->HashSize());
1.933 + sha->Reset();
1.934 +
1.935 +// Test end
1.936 + HMACTestsL(sha,_L("hmacsha1.dat"), _L("sha1"));
1.937 + CleanupStack::PopAndDestroy(); // sha
1.938 + }
1.939 +
1.940 +void ExecuteHashTestsL(CMessageDigest* aMD, const TDesC& aVector, const TDesC& aHMACVector, const TDesC& aHashType)
1.941 + {
1.942 + VectorTestL(aMD, aVector);
1.943 + FunctionalityTestL(aMD);
1.944 + OOMTestL(aMD);
1.945 + aMD->Reset();
1.946 +
1.947 + test.Next(_L("Fixed bugs Tests"));
1.948 +
1.949 + _LIT8(KTest1Data, "The quick brown fox jumped over the lazy dog");
1.950 + TBuf8<KMaxHashSize> hash;
1.951 + hash = aMD->Hash(KTest1Data());
1.952 + test(hash.Length() == aMD->HashSize());
1.953 + aMD->Reset();
1.954 +
1.955 + // Test for DEF001510 "TLS - Receives Disconnect Indication during hands..."
1.956 + _LIT8(KClientData, "D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A4");
1.957 + _LIT8(KServerData, "3E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8");
1.958 +
1.959 + HBufC8* clientData = KClientData().AllocLC();
1.960 + HBufC8* serverData = KServerData().AllocLC();
1.961 +
1.962 + Hex(*clientData);
1.963 + Hex(*serverData);
1.964 +
1.965 +// Hash in 2 portions
1.966 + aMD->Hash(*clientData);
1.967 + aMD->Hash(*serverData);
1.968 +
1.969 + CleanupStack::PopAndDestroy(2, clientData);
1.970 +
1.971 + TBuf8<KMaxHashSize> shabuf;
1.972 + shabuf.Copy(aMD->Hash(KNullDesC8()));
1.973 + aMD->Reset();
1.974 +
1.975 +// Now hash in one chunk
1.976 + _LIT8(KAllData, "D652CA1A6154D8303C16C055E424A5ACF3EBAB94284CD9B05B85C0D0F0B8E7A43E3E56059EFEE4F8C5B05C76128C4C84916DF9E935510C3C063454856FF29FF8");
1.977 + HBufC8* allData = KAllData().AllocLC();
1.978 + Hex(*allData);
1.979 +
1.980 + TBuf8<KMaxHashSize> allbuf;
1.981 + allbuf = aMD->Hash(*allData);
1.982 +
1.983 + test(allbuf.Compare(shabuf)==0);
1.984 + CleanupStack::PopAndDestroy(allData);
1.985 +
1.986 + // Test hashing non-word aligned data - used to crash on arm
1.987 + TPtrC8 nonAlignedData = KTest1Data().Mid(1);
1.988 + hash = aMD->Final(nonAlignedData);
1.989 + test(hash.Length() == aMD->HashSize());
1.990 + aMD->Reset();
1.991 +
1.992 +// Test end
1.993 + HMACTestsL(aMD, aHMACVector, aHashType);
1.994 + }
1.995 +
1.996 +_LIT(K224Algo, "SHA-224.dat");
1.997 +_LIT(K256Algo, "SHA-256.dat");
1.998 +_LIT(K384Algo, "SHA-384.dat");
1.999 +_LIT(K512Algo, "SHA-512.dat");
1.1000 +_LIT(K224Vector, "sha224.dat");
1.1001 +_LIT(K256Vector, "sha256.dat");
1.1002 +_LIT(K384Vector, "sha384.dat");
1.1003 +_LIT(K512Vector, "sha512.dat");
1.1004 +_LIT(K224HmacVector, "hmacsha224.dat");
1.1005 +_LIT(K256HmacVector, "hmacsha256.dat");
1.1006 +_LIT(K384HmacVector, "hmacsha384.dat");
1.1007 +_LIT(K512HmacVector, "hmacsha512.dat");
1.1008 +
1.1009 +const TDesC* gNames[] =
1.1010 + {
1.1011 + &K224Vector(),
1.1012 + &K224HmacVector(),
1.1013 + &K224Algo(),
1.1014 + &K256Vector(),
1.1015 + &K256HmacVector(),
1.1016 + &K256Algo(),
1.1017 + &K384Vector(),
1.1018 + &K384HmacVector(),
1.1019 + &K384Algo(),
1.1020 + &K512Vector(),
1.1021 + &K512HmacVector(),
1.1022 + &K512Algo(),
1.1023 + };
1.1024 +
1.1025 +
1.1026 +void SHA2TestsL(CMessageDigest::THashId aHashId)
1.1027 + {
1.1028 + CMessageDigest* md = CMessageDigestFactory::NewDigestLC(aHashId);
1.1029 + TInt pos = aHashId - CMessageDigest::ESHA224;
1.1030 + pos *= 3;
1.1031 + ExecuteHashTestsL(md, *gNames[pos], *gNames[pos+1], *gNames[pos+2]);
1.1032 + CleanupStack::PopAndDestroy(md);
1.1033 + }
1.1034 +
1.1035 +void HashTests(void)
1.1036 + {
1.1037 + TInt32 testsFailed=0, testCount=0;
1.1038 +
1.1039 + test.Start(_L("SHA1 Tests"));
1.1040 + TRAPD(r, SHA1TestsL());
1.1041 + ++testCount;
1.1042 + if (r!=KErrNone)
1.1043 + {
1.1044 + test.Printf(_L("\r\nSHA1 Tests failed error code = %d\r\n\r\n"),r);
1.1045 + ++testsFailed;
1.1046 + }
1.1047 +
1.1048 + test.Start(_L("SHA-224 Tests"));
1.1049 + TRAP(r, SHA2TestsL(CMessageDigest::ESHA224));
1.1050 + ++testCount;
1.1051 + if (r!=KErrNone)
1.1052 + {
1.1053 + test.Printf(_L("\r\nSHA-224 Tests failed error code = %d\r\n\r\n"),r);
1.1054 + ++testsFailed;
1.1055 + }
1.1056 +
1.1057 + test.Start(_L("SHA-256 Tests"));
1.1058 + TRAP(r, SHA2TestsL(CMessageDigest::ESHA256));
1.1059 + ++testCount;
1.1060 + if (r!=KErrNone)
1.1061 + {
1.1062 + test.Printf(_L("\r\nSHA-256 Tests failed error code = %d\r\n\r\n"),r);
1.1063 + ++testsFailed;
1.1064 + }
1.1065 +
1.1066 + test.Start(_L("SHA-384 Tests"));
1.1067 + TRAP(r, SHA2TestsL(CMessageDigest::ESHA384));
1.1068 + ++testCount;
1.1069 + if (r!=KErrNone)
1.1070 + {
1.1071 + test.Printf(_L("\r\nSHA-384 Tests failed error code = %d\r\n\r\n"),r);
1.1072 + ++testsFailed;
1.1073 + }
1.1074 +
1.1075 + test.Start(_L("SHA-512 Tests"));
1.1076 + TRAP(r, SHA2TestsL(CMessageDigest::ESHA512));
1.1077 + ++testCount;
1.1078 + if (r!=KErrNone)
1.1079 + {
1.1080 + test.Printf(_L("\r\nSHA-512 Tests failed error code = %d\r\n\r\n"),r);
1.1081 + ++testsFailed;
1.1082 + }
1.1083 +
1.1084 + test.Start(_L("MD5 Tests"));
1.1085 + TRAP(r, MD5TestsL());
1.1086 + ++testCount;
1.1087 + if (r!=KErrNone)
1.1088 + {
1.1089 + test.Printf(_L("\r\nMD5 Tests failed error code = %d\r\n\r\n"),r);
1.1090 + ++testsFailed;
1.1091 + }
1.1092 +
1.1093 + test.Start(_L("MD2 Tests"));
1.1094 + TRAP(r, MD2TestsL());
1.1095 + ++testCount;
1.1096 + if (r!=KErrNone)
1.1097 + {
1.1098 + test.Printf(_L("\r\nMD2 Tests failed error code = %d\r\n\r\n"),r);
1.1099 + ++testsFailed;
1.1100 + }
1.1101 +
1.1102 + //MD4 Message Digest Algorithm Tests
1.1103 + test.Start(_L("MD4 Tests"));
1.1104 + TRAP(r, MD4TestsL());
1.1105 + ++testCount;
1.1106 + if (r!=KErrNone)
1.1107 + {
1.1108 + test.Printf(_L("\r\nMD4 Tests failed error code = %d\r\n\r\n"),r);
1.1109 + ++testsFailed;
1.1110 + }
1.1111 +
1.1112 + test.Printf(_L("\r\n%d tests failed out of %d \r\n"),testsFailed,testCount);
1.1113 +
1.1114 +
1.1115 + test(testsFailed==0);
1.1116 + }
1.1117 +
1.1118 +
1.1119 +GLDEF_C TInt E32Main(void)
1.1120 +
1.1121 + {
1.1122 + CTrapCleanup* cleanup;
1.1123 + cleanup=CTrapCleanup::New();
1.1124 +
1.1125 + test.Start(_L(" @SYMTestCaseID:SEC-CRYPTO-HASH-0001 Hash Algorithm Tests "));
1.1126 + CTestConsole* con=NULL;
1.1127 + TRAPD(ret, con=CTestConsole::NewL(test.Console()));
1.1128 + if(ret != KErrNone)
1.1129 + {
1.1130 + return ret;
1.1131 + }
1.1132 + RFs fs;
1.1133 +
1.1134 + fs.Connect();
1.1135 + RFile* file;
1.1136 + file=new (ELeave) RFile;
1.1137 +
1.1138 + TDriveUnit sysDrive (fs.GetSystemDrive());
1.1139 + TDriveName driveName(sysDrive.Name());
1.1140 + TBuf<24> hashLogFile (driveName);
1.1141 + hashLogFile.Append(_L("\\HashLog.txt"));
1.1142 +
1.1143 + file->Replace(fs,hashLogFile,EFileShareAny|EFileWrite);
1.1144 + con->SetLogFile(file);
1.1145 +
1.1146 + test.SetConsole(con);
1.1147 + __UHEAP_MARK;
1.1148 + HashTests();
1.1149 + __UHEAP_MARKEND;
1.1150 +
1.1151 + test.End();
1.1152 + test.Close();
1.1153 + delete cleanup;
1.1154 + return(KErrNone);
1.1155 + }