@@ -15,13 +15,16 @@ #include "config.h" #include -#import "TestsAppDelegate.h" +#import "ObjFW.h" +#import "ObjFWTest.h" -static OFString *const module = @"OFScrypt"; +@interface OFScryptTests: OTTestCase +@end + /* Test vectors form RFC 7914 */ static const unsigned char salsa20Input[64] = { 0x7E, 0x87, 0x9A, 0x21, 0x4F, 0x3E, 0xC9, 0x86, 0x7C, 0xA9, 0x40, 0xE6, 0x41, 0x71, 0x8F, 0x26, 0xBA, 0xEE, 0x55, 0x5B, 0x8C, 0x61, 0xC1, 0xB5, 0x0D, 0xF8, 0x46, 0x11, 0x6D, 0xCD, 0x3B, 0x1D, 0xEE, 0x24, 0xF3, 0x19, @@ -131,35 +134,42 @@ 0x37, 0x30, 0x40, 0x49, 0xE8, 0xA9, 0x52, 0xFB, 0xCB, 0xF4, 0x5C, 0x6F, 0xA7, 0x7A, 0x41, 0xA4 }; #endif -@implementation TestsAppDelegate (OFScryptTests) -- (void)scryptTests +@implementation OFScryptTests +- (void)testSalsa20_8Core { - void *pool = objc_autoreleasePoolPush(); uint32_t salsa20Buffer[16]; + + memcpy(salsa20Buffer, salsa20Input, 64); + OFSalsa20_8Core(salsa20Buffer); + OTAssertEqual(memcmp(salsa20Buffer, salsa20Output, 64), 0); +} + +- (void)testBlockMix +{ uint32_t blockMixBuffer[32]; + + OFScryptBlockMix(blockMixBuffer, blockMixInput.u32, 1); + OTAssertEqual(memcmp(blockMixBuffer, blockMixOutput, 128), 0); +} + +- (void)testROMix +{ uint32_t ROMixBuffer[32], ROMixTmp[17 * 32]; + + memcpy(ROMixBuffer, ROMixInput, 128); + OFScryptROMix(ROMixBuffer, 1, 16, ROMixTmp); + OTAssertEqual(memcmp(ROMixBuffer, ROMixOutput, 128), 0); +} + +- (void)testRFC7941TestVector1 +{ unsigned char output[64]; - TEST(@"Salsa20/8 Core", - R(memcpy(salsa20Buffer, salsa20Input, 64)) && - R(OFSalsa20_8Core(salsa20Buffer)) && - memcmp(salsa20Buffer, salsa20Output, 64) == 0) - - TEST(@"Block mix", - R(OFScryptBlockMix(blockMixBuffer, blockMixInput.u32, 1)) && - memcmp(blockMixBuffer, blockMixOutput, 128) == 0) - - TEST(@"ROMix", - R(memcpy(ROMixBuffer, ROMixInput, 128)) && - R(OFScryptROMix(ROMixBuffer, 1, 16, ROMixTmp)) && - memcmp(ROMixBuffer, ROMixOutput, 128) == 0) - - TEST(@"scrypt test vector #1", - R(OFScrypt((OFScryptParameters){ + OFScrypt((OFScryptParameters){ .blockSize = 1, .costFactor = 16, .parallelization = 1, .salt = (unsigned char *)"", .saltLength = 0, @@ -166,14 +176,20 @@ .password = "", .passwordLength = 0, .key = output, .keyLength = 64, .allowsSwappableMemory = true - })) && memcmp(output, testVector1, 64) == 0) + }); + + OTAssertEqual(memcmp(output, testVector1, 64), 0); +} + +- (void)testRFC7941TestVector2 +{ + unsigned char output[64]; - TEST(@"scrypt test vector #2", - R(OFScrypt((OFScryptParameters){ + OFScrypt((OFScryptParameters){ .blockSize = 8, .costFactor = 1024, .parallelization = 16, .salt = (unsigned char *)"NaCl", .saltLength = 4, @@ -180,16 +196,22 @@ .password = "password", .passwordLength = 8, .key = output, .keyLength = 64, .allowsSwappableMemory = true - })) && memcmp(output, testVector2, 64) == 0) + }); + + OTAssertEqual(memcmp(output, testVector2, 64), 0); +} - /* The third test vector is too expensive for m68k. */ +/* The third test vector is too expensive for m68k. */ #ifndef OF_M68K - TEST(@"scrypt test vector #3", - R(OFScrypt((OFScryptParameters){ +- (void)testRFC7941TestVector3 +{ + unsigned char output[64]; + + OFScrypt((OFScryptParameters){ .blockSize = 8, .costFactor = 16384, .parallelization = 1, .salt = (unsigned char *)"SodiumChloride", .saltLength = 14, @@ -196,17 +218,23 @@ .password = "pleaseletmein", .passwordLength = 13, .key = output, .keyLength = 64, .allowsSwappableMemory = true - })) && memcmp(output, testVector3, 64) == 0) + }); + + OTAssertEqual(memcmp(output, testVector3, 64), 0); +} #endif - /* The forth test vector is too expensive to include it in the tests. */ +/* The forth test vector is too expensive to include it in the tests. */ #if 0 - TEST(@"scrypt test vector #4", - R(OFScrypt((OFScryptParameters){ +- (void)testRFC7941TestVector4 +{ + unsigned char output[64]; + + OFScrypt((OFScryptParameters){ .blockSize = 8, .costFactor = 1048576, .parallelization = 1, .salt = (unsigned char *)"SodiumChloride", .saltLength = 14, @@ -213,11 +241,11 @@ .password = "pleaseletmein", .passwordLength = 13, .key = output, .keyLength = 64, .allowsSwappableMemory = true - })) && memcmp(output, testVector4, 64) == 0) -#endif + }); - objc_autoreleasePoolPop(pool); + OTAssertEqual(memcmp(output, testVector4, 64), 0); } +#endif @end