ObjFW  Diff

Differences From Artifact [3717bd0eab]:

To Artifact [0924135d9a]:


13
14
15
16
17
18
19
20


21


22

23
24
25
26
27
28
29
13
14
15
16
17
18
19

20
21
22
23
24

25
26
27
28
29
30
31
32







-
+
+

+
+
-
+







 * file.
 */

#include "config.h"

#include <string.h>

#import "TestsAppDelegate.h"
#import "ObjFW.h"
#import "ObjFWTest.h"

@interface OFScryptTests: OTTestCase
@end
static OFString *const module = @"OFScrypt";

/* 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,
	0xDF, 0x9B, 0x3D, 0x85, 0x14, 0x12, 0x1E, 0x4B, 0x5A, 0xC5, 0xAA, 0x32,
	0x76, 0x02, 0x1D, 0x29, 0x09, 0xC7, 0x48, 0x29, 0xED, 0xEB, 0xC6, 0x8D,
129
130
131
132
133
134
135
136
137


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160



























161
162
163
164
165
166
167
168
169
170


171
172
173
174








175
176
177
178
179
180
181
182
183
184


185
186
187




188



189
190


191
192
193
194
195
196
197
198
199
200


201


202
203
204

205



206
207


208
209
210
211
212
213
214
215
216
217
218

219
220
221

222

223
132
133
134
135
136
137
138


139
140
141

142



143
















144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182




183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202



203
204
205
206
207
208
209
210


211
212
213
214
215
216
217
218
219
220
221
222
223
224

225
226
227
228

229
230
231
232
233


234
235
236
237
238
239
240
241
242
243
244
245

246

247

248
249
250
251







-
-
+
+

-

-
-
-

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+










+
+
-
-
-
-
+
+
+
+
+
+
+
+










+
+
-
-
-
+
+
+
+

+
+
+
-
-
+
+










+
+
-
+
+


-
+

+
+
+
-
-
+
+










-
+
-

-
+

+

	0xAB, 0xE5, 0xEE, 0x98, 0x20, 0xAD, 0xAA, 0x47, 0x8E, 0x56, 0xFD, 0x8F,
	0x4B, 0xA5, 0xD0, 0x9F, 0xFA, 0x1C, 0x6D, 0x92, 0x7C, 0x40, 0xF4, 0xC3,
	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];
	uint32_t blockMixBuffer[32];
	uint32_t ROMixBuffer[32], ROMixTmp[17 * 32];
	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){
	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];

	OFScrypt((OFScryptParameters){
		.blockSize             = 1,
		.costFactor            = 16,
		.parallelization       = 1,
		.salt                  = (unsigned char *)"",
		.saltLength            = 0,
		.password              = "",
		.passwordLength        = 0,
		.key                   = output,
		.keyLength             = 64,
		.allowsSwappableMemory = true
	});

	    })) && memcmp(output, testVector1, 64) == 0)

	TEST(@"scrypt test vector #2",
	    R(OFScrypt((OFScryptParameters){
	OTAssertEqual(memcmp(output, testVector1, 64), 0);
}

- (void)testRFC7941TestVector2
{
	unsigned char output[64];

	OFScrypt((OFScryptParameters){
		.blockSize             = 8,
		.costFactor            = 1024,
		.parallelization       = 16,
		.salt                  = (unsigned char *)"NaCl",
		.saltLength            = 4,
		.password              = "password",
		.passwordLength        = 8,
		.key                   = output,
		.keyLength             = 64,
		.allowsSwappableMemory = true
	});

	    })) && memcmp(output, testVector2, 64) == 0)

	/* The third test vector is too expensive for m68k. */
	OTAssertEqual(memcmp(output, testVector2, 64), 0);
}

/* The third test vector is too expensive for m68k. */
#ifndef OF_M68K
- (void)testRFC7941TestVector3
{
	unsigned char output[64];
	TEST(@"scrypt test vector #3",
	    R(OFScrypt((OFScryptParameters){

	OFScrypt((OFScryptParameters){
		.blockSize             = 8,
		.costFactor            = 16384,
		.parallelization       = 1,
		.salt                  = (unsigned char *)"SodiumChloride",
		.saltLength            = 14,
		.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
- (void)testRFC7941TestVector4
{
	unsigned char output[64];
	TEST(@"scrypt test vector #4",
	    R(OFScrypt((OFScryptParameters){

	OFScrypt((OFScryptParameters){
		.blockSize             = 8,
		.costFactor            = 1048576,
		.parallelization       = 1,
		.salt                  = (unsigned char *)"SodiumChloride",
		.saltLength            = 14,
		.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