File: test_low_level.py

package info (click to toggle)
python-argon2 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 360 kB
  • sloc: python: 1,251; makefile: 156
file content (332 lines) | stat: -rw-r--r-- 8,818 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# SPDX-License-Identifier: MIT

import binascii
import os

import pytest

from hypothesis import assume, given, settings
from hypothesis import strategies as st

from argon2.exceptions import (
    HashingError,
    VerificationError,
    VerifyMismatchError,
)
from argon2.low_level import (
    ARGON2_VERSION,
    Type,
    core,
    ffi,
    hash_secret,
    hash_secret_raw,
    lib,
    verify_secret,
)


# Example data obtained using the official Argon2 CLI client:
#
# $ echo -n "password" | ./argon2 somesalt -t 2 -m 16 -p 4
# Type:		Argon2i
# Iterations:	2
# Memory:		65536 KiB
# Parallelism:	4
# Hash:		20c8adf6a90550b08c03f5628b32f9edc9d32ce6b90e254cf5e330a40bcfc2be
# Encoded:	$argon2i$v=19$m=65536,t=2,p=4$
#           c29tZXNhbHQ$IMit9qkFULCMA/ViizL57cnTLOa5DiVM9eMwpAvPwr4
# 0.120 seconds
# Verification ok
#
# $ echo -n "password" | ./argon2 somesalt -t 2 -m 16 -p 4 -d
# Type:		Argon2d
# Iterations:	2
# Memory:		65536 KiB
# Parallelism:	4
# Hash:		7199f977eac587e65fb91866da21941a072b5b960b78ceaaecbdef06c766140d
# Encoded:	$argon2d$v=19$m=65536,t=2,p=4$
#           c29tZXNhbHQ$cZn5d+rFh+ZfuRhm2iGUGgcrW5YLeM6q7L3vBsdmFA0
# 0.119 seconds
# Verification ok
#
# Type:		Argon2id
# Iterations:	2
# Memory:		65536 KiB
# Parallelism:	4
# Hash:		1a9677b0afe81fda7b548895e7a1bfeb8668ffc19a530e37e088a668fab1c02a
# Encoded:	$argon2id$v=19$m=65536,t=2,p=4$
#           c29tZXNhbHQ$GpZ3sK/oH9p7VIiV56G/64Zo/8GaUw434IimaPqxwCo
# 0.154 seconds
# Verification ok

TEST_HASH_I_OLD = (
    b"$argon2i$m=65536,t=2,p=4"
    b"$c29tZXNhbHQAAAAAAAAAAA"
    b"$QWLzI4TY9HkL2ZTLc8g6SinwdhZewYrzz9zxCo0bkGY"
)  # a v1.2 hash without a version tag
TEST_HASH_I = (
    b"$argon2i$v=19$m=65536,t=2,p=4$"
    b"c29tZXNhbHQ$IMit9qkFULCMA/ViizL57cnTLOa5DiVM9eMwpAvPwr4"
)
TEST_HASH_D = (
    b"$argon2d$v=19$m=65536,t=2,p=4$"
    b"c29tZXNhbHQ$cZn5d+rFh+ZfuRhm2iGUGgcrW5YLeM6q7L3vBsdmFA0"
)
TEST_HASH_ID = (
    b"$argon2id$v=19$m=65536,t=2,p=4$"
    b"c29tZXNhbHQ$GpZ3sK/oH9p7VIiV56G/64Zo/8GaUw434IimaPqxwCo"
)
TEST_RAW_I = binascii.unhexlify(
    b"20c8adf6a90550b08c03f5628b32f9edc9d32ce6b90e254cf5e330a40bcfc2be"
)
TEST_RAW_D = binascii.unhexlify(
    b"7199f977eac587e65fb91866da21941a072b5b960b78ceaaecbdef06c766140d"
)
TEST_RAW_ID = binascii.unhexlify(
    b"1a9677b0afe81fda7b548895e7a1bfeb8668ffc19a530e37e088a668fab1c02a"
)

TEST_PASSWORD = b"password"
TEST_SALT_LEN = 16
TEST_SALT = b"somesalt"
TEST_TIME = 2
TEST_MEMORY = 65536
TEST_PARALLELISM = 4
TEST_HASH_LEN = 32

i_and_d_encoded = pytest.mark.parametrize(
    ("type", "hash"),
    [(Type.I, TEST_HASH_I), (Type.D, TEST_HASH_D), (Type.ID, TEST_HASH_ID)],
)
i_and_d_raw = pytest.mark.parametrize(
    ("type", "hash"),
    [(Type.I, TEST_RAW_I), (Type.D, TEST_RAW_D), (Type.ID, TEST_RAW_ID)],
)

both_hash_funcs = pytest.mark.parametrize(
    "func", [hash_secret, hash_secret_raw]
)


class TestHash:
    @i_and_d_encoded
    def test_hash_secret(self, type, hash):
        """
        Creates the same encoded hash as the Argon2 CLI client.
        """
        rv = hash_secret(
            TEST_PASSWORD,
            TEST_SALT,
            TEST_TIME,
            TEST_MEMORY,
            TEST_PARALLELISM,
            TEST_HASH_LEN,
            type,
        )

        assert hash == rv
        assert isinstance(rv, bytes)

    @i_and_d_raw
    def test_hash_secret_raw(self, type, hash):
        """
        Creates the same raw hash as the Argon2 CLI client.
        """
        rv = hash_secret_raw(
            TEST_PASSWORD,
            TEST_SALT,
            TEST_TIME,
            TEST_MEMORY,
            TEST_PARALLELISM,
            TEST_HASH_LEN,
            type,
        )

        assert hash == rv
        assert isinstance(rv, bytes)

    def test_hash_nul_bytes(self):
        """
        Hashing secrets with NUL bytes works as expected.
        """
        params = (
            TEST_SALT,
            TEST_TIME,
            TEST_MEMORY,
            TEST_PARALLELISM,
            TEST_HASH_LEN,
            Type.I,
        )
        rv = hash_secret_raw(b"abc\x00", *params)

        assert rv != hash_secret_raw(b"abc", *params)

    @both_hash_funcs
    def test_hash_wrong_arg_type(self, func):
        """
        Passing an argument of wrong type raises TypeError.
        """
        with pytest.raises(TypeError):
            func("oh no, unicode!")

    @both_hash_funcs
    def test_illegal_argon2_parameter(self, func):
        """
        Raises HashingError if hashing fails.
        """
        with pytest.raises(HashingError):
            func(
                TEST_PASSWORD,
                TEST_SALT,
                TEST_TIME,
                1,
                TEST_PARALLELISM,
                TEST_HASH_LEN,
                Type.I,
            )

    @given(
        st.sampled_from((hash_secret, hash_secret_raw)),
        st.binary(max_size=128),
    )
    def test_hash_fast(self, func, secret):
        """
        Hash various secrets as cheaply as possible.
        """
        func(
            secret,
            salt=b"12345678",
            time_cost=1,
            memory_cost=8,
            parallelism=1,
            hash_len=8,
            type=Type.I,
        )


class TestVerify:
    @i_and_d_encoded
    def test_success(self, type, hash):
        """
        Given a valid hash and secret and correct type, we succeed.
        """
        assert True is verify_secret(hash, TEST_PASSWORD, type)

    @i_and_d_encoded
    def test_fail(self, type, hash):
        """
        Wrong password fails.
        """
        with pytest.raises(VerifyMismatchError):
            verify_secret(hash, bytes(reversed(TEST_PASSWORD)), type)

    def test_fail_wrong_argon2_type(self):
        """
        Given a valid hash and secret and wrong type, we fail.
        """
        verify_secret(TEST_HASH_D, TEST_PASSWORD, Type.D)
        verify_secret(TEST_HASH_I, TEST_PASSWORD, Type.I)
        with pytest.raises(VerificationError):
            verify_secret(TEST_HASH_I, TEST_PASSWORD, Type.D)

    def test_wrong_arg_type(self):
        """
        Passing an argument of wrong type raises TypeError.
        """
        with pytest.raises(TypeError) as e:
            verify_secret(TEST_HASH_I, TEST_PASSWORD.decode("ascii"), Type.I)

        assert e.value.args[0].startswith(
            "initializer for ctype 'uint8_t[]' must be a"
        )

    def test_old_hash(self):
        """
        Hashes without a version tag are recognized and verified correctly.
        """
        assert True is verify_secret(TEST_HASH_I_OLD, TEST_PASSWORD, Type.I)


@given(
    password=st.binary(min_size=lib.ARGON2_MIN_PWD_LENGTH, max_size=65),
    time_cost=st.integers(lib.ARGON2_MIN_TIME, 3),
    parallelism=st.integers(lib.ARGON2_MIN_LANES, 5),
    memory_cost=st.integers(0, 1025),
    hash_len=st.integers(lib.ARGON2_MIN_OUTLEN, 513),
    salt_len=st.integers(lib.ARGON2_MIN_SALT_LENGTH, 513),
)
@settings(deadline=None)
def test_argument_ranges(
    password, time_cost, parallelism, memory_cost, hash_len, salt_len
):
    """
    Ensure that both hashing and verifying works for most combinations of legal
    values.

    Limits are intentionally chosen to be *not* on 2^x boundaries.

    This test is rather slow.
    """
    assume(parallelism * 8 <= memory_cost)
    hash = hash_secret(
        secret=password,
        salt=os.urandom(salt_len),
        time_cost=time_cost,
        parallelism=parallelism,
        memory_cost=memory_cost,
        hash_len=hash_len,
        type=Type.I,
    )
    assert verify_secret(hash, password, Type.I)


def test_core():
    """
    If called with equal parameters, core() will return the same as
    hash_secret().
    """
    pwd = b"secret"
    salt = b"12345678"
    hash_len = 8

    # Keep FFI objects alive throughout the function.
    cout = ffi.new("uint8_t[]", hash_len)
    cpwd = ffi.new("uint8_t[]", pwd)
    csalt = ffi.new("uint8_t[]", salt)

    ctx = ffi.new(
        "argon2_context *",
        {
            "out": cout,
            "outlen": hash_len,
            "version": ARGON2_VERSION,
            "pwd": cpwd,
            "pwdlen": len(pwd),
            "salt": csalt,
            "saltlen": len(salt),
            "secret": ffi.NULL,
            "secretlen": 0,
            "ad": ffi.NULL,
            "adlen": 0,
            "t_cost": 1,
            "m_cost": 8,
            "lanes": 1,
            "threads": 1,
            "allocate_cbk": ffi.NULL,
            "free_cbk": ffi.NULL,
            "flags": lib.ARGON2_DEFAULT_FLAGS,
        },
    )

    rv = core(ctx, Type.D.value)

    assert 0 == rv
    assert hash_secret_raw(
        pwd,
        salt=salt,
        time_cost=1,
        memory_cost=8,
        parallelism=1,
        hash_len=hash_len,
        type=Type.D,
    ) == bytes(ffi.buffer(ctx.out, ctx.outlen))