From eabb5f43f6260c18466e05746f71be04b3073f25 Mon Sep 17 00:00:00 2001 From: mjewkes Date: Tue, 29 Jul 2014 21:31:57 -0300 Subject: [PATCH] Handshake HMAC Authentication success/failure test. Random uint16 generation variance testing for full CryptoTools test coverage. Removal of stub tests. --- .../network/rtp/zrtp/HandshakePacketTest.m | 41 +++++++++++++++++++ Signal/test/util/CryptoUtilTest.h | 2 +- Signal/test/util/CryptoUtilTest.m | 26 ++++++++---- 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/Signal/test/network/rtp/zrtp/HandshakePacketTest.m b/Signal/test/network/rtp/zrtp/HandshakePacketTest.m index 3f35efb34..4d0be08ba 100644 --- a/Signal/test/network/rtp/zrtp/HandshakePacketTest.m +++ b/Signal/test/network/rtp/zrtp/HandshakePacketTest.m @@ -70,4 +70,45 @@ test([p.hashChainH3 isEqual:h.h3]); test([p.clientId isEqual:[@"RedPhone 019 " encodedAsAscii]]); } +-(void) testHandshakeMacAuthenticationSucceeds{ + NSData* type = [@"0f0f0f0f0f0f0f0f" decodedAsHexString]; + NSData* payload =[@"ff00ff00" decodedAsHexString]; + NSData* untouchedPayload =[@"ff00ff00" decodedAsHexString]; + + NSData* key =[@"11" decodedAsHexString]; + + HandshakePacket* p = [HandshakePacket handshakePacketWithTypeId:type + andPayload:payload]; + HandshakePacket* withHMAC = [p withHmacAppended:key]; + HandshakePacket* strippedOfValidHMAC = [withHMAC withHmacVerifiedAndRemoved:key]; + + test([[p payload] isEqualToData:[strippedOfValidHMAC payload]]); + + test([untouchedPayload isEqualToData:[p payload]]); + test([untouchedPayload isEqualToData:[strippedOfValidHMAC payload]]); +} +-(void) testHandshakeMacAuthenticationFails{ + NSData* type = [@"0f0f0f0f0f0f0f0f" decodedAsHexString]; + NSData* payload =[@"ff00ff00" decodedAsHexString]; + NSData* untouchedPayload =[@"ff00ff00" decodedAsHexString]; + + NSData* key =[@"11" decodedAsHexString]; + + NSData* badkey =[@"10" decodedAsHexString]; + + + HandshakePacket* p = [HandshakePacket handshakePacketWithTypeId:type + andPayload:payload]; + HandshakePacket* withHMAC = [p withHmacAppended:key]; + + testThrows([withHMAC withHmacVerifiedAndRemoved:badkey]); + + HandshakePacket* strippedOfValidHMAC = [withHMAC withHmacVerifiedAndRemoved:key]; + + test([[p payload] isEqualToData:[strippedOfValidHMAC payload]]); + + test([untouchedPayload isEqualToData:[p payload]]); + test([untouchedPayload isEqualToData:[strippedOfValidHMAC payload]]); + +} @end diff --git a/Signal/test/util/CryptoUtilTest.h b/Signal/test/util/CryptoUtilTest.h index cc4b080b2..f6f3e1d85 100644 --- a/Signal/test/util/CryptoUtilTest.h +++ b/Signal/test/util/CryptoUtilTest.h @@ -1,5 +1,5 @@ #import -@interface CryptoUtilTest : XCTestCase +@interface CryptoToolsTest : XCTestCase @end diff --git a/Signal/test/util/CryptoUtilTest.m b/Signal/test/util/CryptoUtilTest.m index 022cd3768..5f990ea0d 100644 --- a/Signal/test/util/CryptoUtilTest.m +++ b/Signal/test/util/CryptoUtilTest.m @@ -1,9 +1,11 @@ -#import "CryptoUtilTest.h" +#import "CryptoToolsTest.h" #import "Util.h" #import "CryptoTools.h" #import "TestUtil.h" -@implementation CryptoUtilTest +//This test class covers CryptoTools.h - the accessor class for the core crypto library. + +@implementation CryptoToolsTest -(void) testIsEqualToData_TimingSafe { test([[NSMutableData dataWithLength:0] isEqualToData_TimingSafe:[NSMutableData dataWithLength:0]]); test([[NSMutableData dataWithLength:1] isEqualToData_TimingSafe:[NSMutableData dataWithLength:1]]); @@ -49,7 +51,7 @@ NSData* actual = [val hashWithSha256]; test([actual isEqualToData:expected]); } --(void) testRandom { +-(void) testRandomForVariance { NSData* d = [CryptoTools generateSecureRandomData:8]; NSData* d2 = [CryptoTools generateSecureRandomData:8]; @@ -59,6 +61,17 @@ // extremely unlikely to fail if any reasonable amount of entropy is going into d and d2 test(![d isEqualToData:d2]); } + +-(void) testRandomUInt16GenerationForVariance{ + uint16_t a = [CryptoTools generateSecureRandomUInt16]; + uint16_t b = [CryptoTools generateSecureRandomUInt16]; + uint16_t c = [CryptoTools generateSecureRandomUInt16]; + uint16_t d = [CryptoTools generateSecureRandomUInt16]; + int n = sizeof(uint32_t); + // extremely unlikely to fail if any reasonable amount of entropy is going into d and d2 + test(!(a==b==c==d)); +} + -(void) testKnownAesCipherFeedback { NSData* iv = [@"000102030405060708090a0b0c0d0e0f" decodedAsHexString]; NSData* plain =[@"6bc1bee22e409f96e93d7e117393172a" decodedAsHexString]; @@ -110,10 +123,5 @@ -(void) testComputeKnownOtp { test([[CryptoTools computeOtpWithPassword:@"password" andCounter:123] isEqualToString:@"SiYZc8Xg6KSmCECSImVSmjnRNfc="]); } --(void) testMacAuthenticationSucceeds{ - -} --(void) testMacAuthenticationFails{ - -} + @end