test_crypt.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include <common.h>
  8. #include <test/lib.h>
  9. #include <test/test.h>
  10. #include <test/ut.h>
  11. #include <crypt.h>
  12. /**
  13. * lib_crypt() - unit test for crypt-style password hashing
  14. *
  15. * @uts: unit test state
  16. * Return: 0 = success, 1 = failure
  17. */
  18. static int lib_crypt(struct unit_test_state *uts)
  19. {
  20. int equals = 0;
  21. int err;
  22. err = crypt_compare("", "password", &equals);
  23. ut_assertf(err != 0, "crypt_compare successful but should not\n");
  24. ut_assertf(equals != 1,
  25. "crypt_compare password hash matched but should not\n");
  26. if (IS_ENABLED(CONFIG_CRYPT_PW_SHA256)) {
  27. err = crypt_compare("$5$", "password", &equals);
  28. ut_assertf(err == 0, "crypt-sha256 not successful\n");
  29. ut_assertf(
  30. equals != 1,
  31. "crypt-sha256 password hash matched but should not\n");
  32. err = crypt_compare(
  33. "$5$rounds=640000$TM4lL4zXDG7F4aRX$JM7a9wmvodnA0WasjTztj6mxg.KVuk6doQ/eBhdcapB",
  34. "password", &equals);
  35. ut_assertf(err == 0, "crypt-sha256 failed: %d\n", err);
  36. ut_assertf(equals == 1,
  37. "crypt-sha256 password hash didn't match\n");
  38. }
  39. equals = 0;
  40. if (IS_ENABLED(CONFIG_CRYPT_PW_SHA512)) {
  41. err = crypt_compare("$6$", "password", &equals);
  42. ut_assertf(err == 0, "crypt-sha512 not successful\n");
  43. ut_assertf(
  44. equals != 1,
  45. "crypt-sha512 password hash matched but should not\n");
  46. err = crypt_compare(
  47. "$6$rounds=640000$fCTP1F0N5JLq2eND$z5EzK5KZJA9JnOaj5d1Gg/2v6VqFOQJ3bVekWuCPauabutBt/8qzV1exJnytUyhbq3H0bSBXtodwNbtGEi/Tm/",
  48. "password", &equals);
  49. ut_assertf(err == 0, "crypt-sha512 failed: %d\n", err);
  50. ut_assertf(equals == 1,
  51. "crypt-sha512 password hash didn't match\n");
  52. }
  53. return CMD_RET_SUCCESS;
  54. }
  55. LIB_TEST(lib_crypt, 0);