blob: fb21edf9748467f3e58cbfc3a151779294a820b2 [file] [log] [blame]
Steffen Jaeckel229bd512021-07-08 15:57:33 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2021 Steffen Jaeckel
4 *
5 * Unit test for crypt-style password hashing
6 */
7
8#include <common.h>
9#include <test/lib.h>
10#include <test/test.h>
11#include <test/ut.h>
12
13#include <crypt.h>
14
15/**
16 * lib_crypt() - unit test for crypt-style password hashing
17 *
18 * @uts: unit test state
19 * Return: 0 = success, 1 = failure
20 */
21static int lib_crypt(struct unit_test_state *uts)
22{
23 int equals = 0;
Steffen Jaeckel878c91b2021-07-08 15:57:34 +020024 int err;
25
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");
Steffen Jaeckel229bd512021-07-08 15:57:33 +020030
31 if (IS_ENABLED(CONFIG_CRYPT_PW_SHA256)) {
Steffen Jaeckel878c91b2021-07-08 15:57:34 +020032 err = crypt_compare("$5$", "password", &equals);
33 ut_assertf(err == 0, "crypt-sha256 not successful\n");
34 ut_assertf(
35 equals != 1,
36 "crypt-sha256 password hash matched but should not\n");
37
38 err = crypt_compare(
Steffen Jaeckel229bd512021-07-08 15:57:33 +020039 "$5$rounds=640000$TM4lL4zXDG7F4aRX$JM7a9wmvodnA0WasjTztj6mxg.KVuk6doQ/eBhdcapB",
40 "password", &equals);
Steffen Jaeckel878c91b2021-07-08 15:57:34 +020041 ut_assertf(err == 0, "crypt-sha256 failed: %d\n", err);
Steffen Jaeckel229bd512021-07-08 15:57:33 +020042 ut_assertf(equals == 1,
43 "crypt-sha256 password hash didn't match\n");
44 }
45 equals = 0;
46 if (IS_ENABLED(CONFIG_CRYPT_PW_SHA512)) {
Steffen Jaeckel878c91b2021-07-08 15:57:34 +020047 err = crypt_compare("$6$", "password", &equals);
48 ut_assertf(err == 0, "crypt-sha512 not successful\n");
49 ut_assertf(
50 equals != 1,
51 "crypt-sha512 password hash matched but should not\n");
52
53 err = crypt_compare(
Steffen Jaeckel229bd512021-07-08 15:57:33 +020054 "$6$rounds=640000$fCTP1F0N5JLq2eND$z5EzK5KZJA9JnOaj5d1Gg/2v6VqFOQJ3bVekWuCPauabutBt/8qzV1exJnytUyhbq3H0bSBXtodwNbtGEi/Tm/",
55 "password", &equals);
Steffen Jaeckel878c91b2021-07-08 15:57:34 +020056 ut_assertf(err == 0, "crypt-sha512 failed: %d\n", err);
Steffen Jaeckel229bd512021-07-08 15:57:33 +020057 ut_assertf(equals == 1,
58 "crypt-sha512 password hash didn't match\n");
59 }
60
61 return CMD_RET_SUCCESS;
62}
63
64LIB_TEST(lib_crypt, 0);