1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2021 Steffen Jaeckel
5 * Unit test for crypt-style password hashing
10 #include <test/test.h>
16 * lib_crypt() - unit test for crypt-style password hashing
18 * @uts: unit test state
19 * Return: 0 = success, 1 = failure
21 static int lib_crypt(struct unit_test_state *uts)
26 err = crypt_compare("", "password", &equals);
27 ut_assertf(err != 0, "crypt_compare successful but should not\n");
28 ut_assertf(equals != 1,
29 "crypt_compare password hash matched but should not\n");
31 if (IS_ENABLED(CONFIG_CRYPT_PW_SHA256)) {
32 err = crypt_compare("$5$", "password", &equals);
33 ut_assertf(err == 0, "crypt-sha256 not successful\n");
36 "crypt-sha256 password hash matched but should not\n");
39 "$5$rounds=640000$TM4lL4zXDG7F4aRX$JM7a9wmvodnA0WasjTztj6mxg.KVuk6doQ/eBhdcapB",
41 ut_assertf(err == 0, "crypt-sha256 failed: %d\n", err);
42 ut_assertf(equals == 1,
43 "crypt-sha256 password hash didn't match\n");
46 if (IS_ENABLED(CONFIG_CRYPT_PW_SHA512)) {
47 err = crypt_compare("$6$", "password", &equals);
48 ut_assertf(err == 0, "crypt-sha512 not successful\n");
51 "crypt-sha512 password hash matched but should not\n");
54 "$6$rounds=640000$fCTP1F0N5JLq2eND$z5EzK5KZJA9JnOaj5d1Gg/2v6VqFOQJ3bVekWuCPauabutBt/8qzV1exJnytUyhbq3H0bSBXtodwNbtGEi/Tm/",
56 ut_assertf(err == 0, "crypt-sha512 failed: %d\n", err);
57 ut_assertf(equals == 1,
58 "crypt-sha512 password hash didn't match\n");
61 return CMD_RET_SUCCESS;
64 LIB_TEST(lib_crypt, 0);