hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

+test.ha (5478B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use bytes;
      5 use encoding::hex;
      6 use os;
      7 use strings;
      8 use test;
      9 
     10 @test fn mode_d_one_pass() void = {
     11 	let pass: [32]u8 = [1...];
     12 	let salt: [16]u8 = [2...];
     13 	let secret: [8]u8 = [3...];
     14 	let data: [12]u8 = [4...];
     15 	let result: [32]u8 = [0...];
     16 
     17 	let expected: [_]u8 = [
     18 		0xfa, 0x17, 0x75, 0xca, 0x80, 0x90, 0x64, 0x66, 0x18, 0xbe,
     19 		0x70, 0xeb, 0x0f, 0xc9, 0xde, 0x43, 0x67, 0x58, 0xed, 0x0c,
     20 		0xa5, 0x36, 0x83, 0x1a, 0xe9, 0xe1, 0x03, 0x48, 0x93, 0x81,
     21 		0xc1, 0x79,
     22 	];
     23 
     24 	let cfg = conf {
     25 		secret = secret,
     26 		data = data,
     27 		passes = 1,
     28 		parallel = 4,
     29 		version = 0x13,
     30 		mem = 32,
     31 		...
     32 	};
     33 
     34 	argon2d(result[..], pass, salt, &cfg)!;
     35 
     36 	assert(bytes::equal(result, expected));
     37 };
     38 
     39 @test fn rfc_d_test_vector() void = {
     40 	let pass: [32]u8 = [1...];
     41 	let salt: [16]u8 = [2...];
     42 	let secret: [8]u8 = [3...];
     43 	let data: [12]u8 = [4...];
     44 	let result: [32]u8 = [0...];
     45 
     46 	let mem: []u64 = alloc([0...], 32z * BLOCKSZ);
     47 	defer free(mem);
     48 
     49 	let expected: [_]u8 = [
     50 		0x51, 0x2b, 0x39, 0x1b, 0x6f, 0x11, 0x62, 0x97, 0x53, 0x71,
     51 		0xd3, 0x09, 0x19, 0x73, 0x42, 0x94, 0xf8, 0x68, 0xe3, 0xbe,
     52 		0x39, 0x84, 0xf3, 0xc1, 0xa1, 0x3a, 0x4d, 0xb9, 0xfa, 0xbe,
     53 		0x4a, 0xcb,
     54 	];
     55 
     56 	let cfg = conf {
     57 		secret = secret,
     58 		data = data,
     59 		passes = 3,
     60 		parallel = 4,
     61 		version = 0x13,
     62 		mem = mem[..],
     63 		...
     64 	};
     65 
     66 	argon2d(result[..], pass, salt, &cfg)!;
     67 
     68 	assert(bytes::equal(result, expected));
     69 };
     70 
     71 
     72 @test fn rfc_i_test_vector() void = {
     73 	let pass: [32]u8 = [1...];
     74 	let salt: [16]u8 = [2...];
     75 	let secret: [8]u8 = [3...];
     76 	let data: [12]u8 = [4...];
     77 	let result: [32]u8 = [0...];
     78 
     79 	let mem: []u64 = alloc([0...], 32z * BLOCKSZ);
     80 	defer free(mem);
     81 
     82 	let expected: [_]u8 = [
     83 		0xc8, 0x14, 0xd9, 0xd1, 0xdc, 0x7f, 0x37, 0xaa, 0x13, 0xf0,
     84 		0xd7, 0x7f, 0x24, 0x94, 0xbd, 0xa1, 0xc8, 0xde, 0x6b, 0x01,
     85 		0x6d, 0xd3, 0x88, 0xd2, 0x99, 0x52, 0xa4, 0xc4, 0x67, 0x2b,
     86 		0x6c, 0xe8,
     87 	];
     88 
     89 	let cfg = conf {
     90 		secret = secret,
     91 		data = data,
     92 		passes = 3,
     93 		parallel = 4,
     94 		version = 0x13,
     95 		mem = mem[..],
     96 		...
     97 	};
     98 
     99 	argon2i(result[..], pass, salt, &cfg)!;
    100 
    101 	assert(bytes::equal(result, expected));
    102 };
    103 
    104 @test fn rfc_id_test_vector() void = {
    105 	let pass: [32]u8 = [1...];
    106 	let salt: [16]u8 = [2...];
    107 	let secret: [8]u8 = [3...];
    108 	let data: [12]u8 = [4...];
    109 	let result: [32]u8 = [0...];
    110 
    111 	let mem: []u64 = alloc([0...], 32z * BLOCKSZ);
    112 	defer free(mem);
    113 
    114 	let expected: [_]u8 = [
    115 		0x0d, 0x64, 0x0d, 0xf5, 0x8d, 0x78, 0x76, 0x6c, 0x08, 0xc0,
    116 		0x37, 0xa3, 0x4a, 0x8b, 0x53, 0xc9, 0xd0, 0x1e, 0xf0, 0x45,
    117 		0x2d, 0x75, 0xb6, 0x5e, 0xb5, 0x25, 0x20, 0xe9, 0x6b, 0x01,
    118 		0xe6, 0x59,
    119 	];
    120 
    121 	let cfg = conf {
    122 		secret = secret,
    123 		data = data,
    124 		passes = 3,
    125 		parallel = 4,
    126 		version = 0x13,
    127 		mem = mem[..],
    128 		...
    129 	};
    130 
    131 	argon2id(result[..], pass, salt, &cfg)!;
    132 
    133 	assert(bytes::equal(result, expected));
    134 };
    135 
    136 type tcase = struct {
    137 	c: conf,
    138 	m: mode,
    139 	h: str,
    140 };
    141 
    142 @test fn samples() void = {
    143 	const pass = strings::toutf8("trustno1");
    144 	const salt = strings::toutf8("abcdefgh");
    145 
    146 	const tests: [_]tcase = [
    147 		tcase {
    148 			c = conf {
    149 				passes = 1,
    150 				parallel = 3,
    151 				version = 0x13,
    152 				mem = 64,
    153 				...
    154 			},
    155 			m = mode::ID,
    156 			h = "c7ada5ba3222fa45a3802249b509dcfb10e68a50e3faad2a6377eeca8395ab47",
    157 		},
    158 		tcase {
    159 			c = conf {
    160 				passes = 1,
    161 				parallel = 4,
    162 				version = 0x13,
    163 				mem = 64,
    164 				...
    165 			},
    166 			m = mode::ID,
    167 			h = "21543b2017ede3f865ea5cb88295628ba25eb3be53a8c4aeb0ac1a264be0110a",
    168 		},
    169 		tcase {
    170 			c = conf {
    171 				passes = 1,
    172 				parallel = 4,
    173 				version = 0x13,
    174 				mem = 64,
    175 				...
    176 			},
    177 			m = mode::I,
    178 			h = "5c3124ce5f3556e5e25f06b5108718f2cd72afee98a3249656eb85ecc0e5b314",
    179 		},
    180 		tcase {
    181 			c = conf {
    182 				passes = 1,
    183 				parallel = 4,
    184 				version = 0x13,
    185 				mem = 64,
    186 				...
    187 			},
    188 			m = mode::D,
    189 			h = "d75524ad0b899363ce77f2d1e1040763dc01cfc725db635391bba163001f08cb",
    190 		},
    191 		tcase {
    192 			c = conf {
    193 				passes = 3,
    194 				parallel = 3,
    195 				version = 0x13,
    196 				mem = 64,
    197 				...
    198 			},
    199 			m = mode::ID,
    200 			h = "226c3ca6caba42b102035d332a11b350f1e19675fccb6e24aa33ca8c31d588c1",
    201 		},
    202 		tcase {
    203 			c = conf {
    204 				passes = 1,
    205 				parallel = 8,
    206 				version = 0x13,
    207 				mem = 64,
    208 				...
    209 			},
    210 			m = mode::ID,
    211 			h = "fadf598b70708f4d91b0e98f038fd25a73950f1f85d57fb250740d817f95e9a9",
    212 		},
    213 		tcase {
    214 			c = conf {
    215 				passes = 1,
    216 				parallel = 4,
    217 				version = 0x13,
    218 				mem = 96,
    219 				...
    220 			},
    221 			m = mode::ID,
    222 			h = "c99aa41cb53cc4919d336c19d38b30d8633c71faa9475293f3fbe0aa6ccd65b2",
    223 		},
    224 	];
    225 
    226 	for (let i = 0z; i < len(tests); i += 1) {
    227 		const t = tests[i];
    228 		const expected = hex::decodestr(t.h)!;
    229 		defer free(expected);
    230 		let dest: []u8 = alloc([0...], len(expected));
    231 		defer free(dest);
    232 
    233 		argon2(dest, pass, salt, &t.c, t.m)!;
    234 		assert(bytes::equal(expected, dest));
    235 	};
    236 };
    237 
    238 @test fn samples_slow() void = {
    239 	test::require("slow");
    240 
    241 	const pass = strings::toutf8("trustno1");
    242 	const salt = strings::toutf8("abcdefgh");
    243 
    244 	const tests: [_]tcase = [
    245 		tcase {
    246 			c = low_mem_conf,
    247 			m = mode::ID,
    248 			h = "8974537c53677aae532b319af700bb4232a0d74eee7d57296b2a3f8303a6bafe",
    249 		},
    250 		tcase {
    251 			c = default_conf,
    252 			m = mode::ID,
    253 			h = "3b282cbf435b0e022f7041549583ddc802e519109f1da8f12d2054910913d660",
    254 		},
    255 	];
    256 
    257 	for (let i = 0z; i < len(tests); i += 1) {
    258 		const t = tests[i];
    259 		const expected = hex::decodestr(t.h)!;
    260 		defer free(expected);
    261 		let dest: []u8 = alloc([0...], len(expected));
    262 		defer free(dest);
    263 
    264 		argon2(dest, pass, salt, &t.c, t.m)!;
    265 		assert(bytes::equal(expected, dest));
    266 	};
    267 };