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