Steffen Jaeckel | 229bd51 | 2021-07-08 15:57:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (c) 2021 Steffen Jaeckel |
| 4 | * |
| 5 | * Unit test for crypt-style password hashing |
| 6 | */ |
| 7 | |
Steffen Jaeckel | 229bd51 | 2021-07-08 15:57:33 +0200 | [diff] [blame] | 8 | #include <test/lib.h> |
| 9 | #include <test/test.h> |
| 10 | #include <test/ut.h> |
| 11 | |
| 12 | #include <crypt.h> |
| 13 | |
| 14 | /** |
| 15 | * lib_crypt() - unit test for crypt-style password hashing |
| 16 | * |
| 17 | * @uts: unit test state |
| 18 | * Return: 0 = success, 1 = failure |
| 19 | */ |
| 20 | static int lib_crypt(struct unit_test_state *uts) |
| 21 | { |
| 22 | int equals = 0; |
Steffen Jaeckel | 878c91b | 2021-07-08 15:57:34 +0200 | [diff] [blame] | 23 | int err; |
| 24 | |
| 25 | err = crypt_compare("", "password", &equals); |
| 26 | ut_assertf(err != 0, "crypt_compare successful but should not\n"); |
| 27 | ut_assertf(equals != 1, |
| 28 | "crypt_compare password hash matched but should not\n"); |
Steffen Jaeckel | 229bd51 | 2021-07-08 15:57:33 +0200 | [diff] [blame] | 29 | |
| 30 | if (IS_ENABLED(CONFIG_CRYPT_PW_SHA256)) { |
Steffen Jaeckel | 878c91b | 2021-07-08 15:57:34 +0200 | [diff] [blame] | 31 | err = crypt_compare("$5$", "password", &equals); |
| 32 | ut_assertf(err == 0, "crypt-sha256 not successful\n"); |
| 33 | ut_assertf( |
| 34 | equals != 1, |
| 35 | "crypt-sha256 password hash matched but should not\n"); |
| 36 | |
| 37 | err = crypt_compare( |
Steffen Jaeckel | 229bd51 | 2021-07-08 15:57:33 +0200 | [diff] [blame] | 38 | "$5$rounds=640000$TM4lL4zXDG7F4aRX$JM7a9wmvodnA0WasjTztj6mxg.KVuk6doQ/eBhdcapB", |
| 39 | "password", &equals); |
Steffen Jaeckel | 878c91b | 2021-07-08 15:57:34 +0200 | [diff] [blame] | 40 | ut_assertf(err == 0, "crypt-sha256 failed: %d\n", err); |
Steffen Jaeckel | 229bd51 | 2021-07-08 15:57:33 +0200 | [diff] [blame] | 41 | ut_assertf(equals == 1, |
| 42 | "crypt-sha256 password hash didn't match\n"); |
| 43 | } |
| 44 | equals = 0; |
| 45 | if (IS_ENABLED(CONFIG_CRYPT_PW_SHA512)) { |
Steffen Jaeckel | 878c91b | 2021-07-08 15:57:34 +0200 | [diff] [blame] | 46 | err = crypt_compare("$6$", "password", &equals); |
| 47 | ut_assertf(err == 0, "crypt-sha512 not successful\n"); |
| 48 | ut_assertf( |
| 49 | equals != 1, |
| 50 | "crypt-sha512 password hash matched but should not\n"); |
| 51 | |
| 52 | err = crypt_compare( |
Steffen Jaeckel | 229bd51 | 2021-07-08 15:57:33 +0200 | [diff] [blame] | 53 | "$6$rounds=640000$fCTP1F0N5JLq2eND$z5EzK5KZJA9JnOaj5d1Gg/2v6VqFOQJ3bVekWuCPauabutBt/8qzV1exJnytUyhbq3H0bSBXtodwNbtGEi/Tm/", |
| 54 | "password", &equals); |
Steffen Jaeckel | 878c91b | 2021-07-08 15:57:34 +0200 | [diff] [blame] | 55 | ut_assertf(err == 0, "crypt-sha512 failed: %d\n", err); |
Steffen Jaeckel | 229bd51 | 2021-07-08 15:57:33 +0200 | [diff] [blame] | 56 | ut_assertf(equals == 1, |
| 57 | "crypt-sha512 password hash didn't match\n"); |
| 58 | } |
| 59 | |
| 60 | return CMD_RET_SUCCESS; |
| 61 | } |
Steffen Jaeckel | 229bd51 | 2021-07-08 15:57:33 +0200 | [diff] [blame] | 62 | LIB_TEST(lib_crypt, 0); |