pbkdf2_test.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2013 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/test.h>
  19. #include <grub/dl.h>
  20. #include <grub/misc.h>
  21. #include <grub/crypto.h>
  22. GRUB_MOD_LICENSE ("GPLv3+");
  23. static struct
  24. {
  25. const char *P;
  26. grub_size_t Plen;
  27. const char *S;
  28. grub_size_t Slen;
  29. unsigned int c;
  30. grub_size_t dkLen;
  31. const char *DK;
  32. } vectors[] = {
  33. /* RFC6070. */
  34. {
  35. "password", 8,
  36. "salt", 4,
  37. 1, 20,
  38. "\x0c\x60\xc8\x0f\x96\x1f\x0e\x71\xf3\xa9\xb5\x24\xaf\x60\x12"
  39. "\x06\x2f\xe0\x37\xa6"
  40. },
  41. {
  42. "password", 8,
  43. "salt", 4,
  44. 2, 20,
  45. "\xea\x6c\x01\x4d\xc7\x2d\x6f\x8c"
  46. "\xcd\x1e\xd9\x2a\xce\x1d\x41\xf0"
  47. "\xd8\xde\x89\x57"
  48. },
  49. {
  50. "password", 8,
  51. "salt", 4,
  52. 4096, 20,
  53. "\x4b\x00\x79\x01\xb7\x65\x48\x9a\xbe\xad\x49\xd9\x26\xf7"
  54. "\x21\xd0\x65\xa4\x29\xc1"
  55. },
  56. {
  57. "passwordPASSWORDpassword", 24,
  58. "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36,
  59. 4096, 25,
  60. "\x3d\x2e\xec\x4f\xe4\x1c\x84\x9b\x80\xc8\xd8\x36\x62\xc0"
  61. "\xe4\x4a\x8b\x29\x1a\x96\x4c\xf2\xf0\x70\x38"
  62. },
  63. {
  64. "pass\0word", 9,
  65. "sa\0lt", 5,
  66. 4096, 16,
  67. "\x56\xfa\x6a\xa7\x55\x48\x09\x9d\xcc\x37\xd7\xf0\x34\x25\xe0\xc3"
  68. }
  69. };
  70. static void
  71. pbkdf2_test (void)
  72. {
  73. grub_size_t i;
  74. for (i = 0; i < ARRAY_SIZE (vectors); i++)
  75. {
  76. gcry_err_code_t err;
  77. grub_uint8_t DK[32];
  78. err = grub_crypto_pbkdf2 (GRUB_MD_SHA1,
  79. (const grub_uint8_t *) vectors[i].P,
  80. vectors[i].Plen,
  81. (const grub_uint8_t *) vectors[i].S,
  82. vectors[i].Slen,
  83. vectors[i].c,
  84. DK, vectors[i].dkLen);
  85. grub_test_assert (err == 0, "gcry error %d", err);
  86. grub_test_assert (grub_memcmp (DK, vectors[i].DK, vectors[i].dkLen) == 0,
  87. "PBKDF2 mismatch");
  88. }
  89. }
  90. /* Register example_test method as a functional test. */
  91. GRUB_FUNCTIONAL_TEST (pbkdf2_test, pbkdf2_test);