pbkdf2.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* gc-pbkdf2-sha1.c --- Password-Based Key Derivation Function a'la PKCS#5
  2. Copyright (C) 2002, 2003, 2004, 2005, 2006, 2009 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. /* Written by Simon Josefsson. */
  15. /* Imported from gnulib. */
  16. #include <grub/crypto.h>
  17. #include <grub/mm.h>
  18. #include <grub/misc.h>
  19. #include <grub/dl.h>
  20. GRUB_MOD_LICENSE ("GPLv2+");
  21. /* Implement PKCS#5 PBKDF2 as per RFC 2898. The PRF to use is HMAC variant
  22. of digest supplied by MD. Inputs are the password P of length PLEN,
  23. the salt S of length SLEN, the iteration counter C (> 0), and the
  24. desired derived output length DKLEN. Output buffer is DK which
  25. must have room for at least DKLEN octets. The output buffer will
  26. be filled with the derived data. */
  27. gcry_err_code_t
  28. grub_crypto_pbkdf2 (const struct gcry_md_spec *md,
  29. const grub_uint8_t *P, grub_size_t Plen,
  30. const grub_uint8_t *S, grub_size_t Slen,
  31. unsigned int c,
  32. grub_uint8_t *DK, grub_size_t dkLen)
  33. {
  34. unsigned int hLen = md->mdlen;
  35. grub_uint8_t U[GRUB_CRYPTO_MAX_MDLEN];
  36. grub_uint8_t T[GRUB_CRYPTO_MAX_MDLEN];
  37. unsigned int u;
  38. unsigned int l;
  39. unsigned int r;
  40. unsigned int i;
  41. unsigned int k;
  42. gcry_err_code_t rc;
  43. grub_uint8_t *tmp;
  44. grub_size_t tmplen = Slen + 4;
  45. if (md->mdlen > GRUB_CRYPTO_MAX_MDLEN || md->mdlen == 0)
  46. return GPG_ERR_INV_ARG;
  47. if (c == 0)
  48. return GPG_ERR_INV_ARG;
  49. if (dkLen == 0)
  50. return GPG_ERR_INV_ARG;
  51. if (dkLen > 4294967295U)
  52. return GPG_ERR_INV_ARG;
  53. l = ((dkLen - 1) / hLen) + 1;
  54. r = dkLen - (l - 1) * hLen;
  55. tmp = grub_malloc (tmplen);
  56. if (tmp == NULL)
  57. return GPG_ERR_OUT_OF_MEMORY;
  58. grub_memcpy (tmp, S, Slen);
  59. for (i = 1; i - 1 < l; i++)
  60. {
  61. grub_memset (T, 0, hLen);
  62. for (u = 0; u < c; u++)
  63. {
  64. if (u == 0)
  65. {
  66. tmp[Slen + 0] = (i & 0xff000000) >> 24;
  67. tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
  68. tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
  69. tmp[Slen + 3] = (i & 0x000000ff) >> 0;
  70. rc = grub_crypto_hmac_buffer (md, P, Plen, tmp, tmplen, U);
  71. }
  72. else
  73. rc = grub_crypto_hmac_buffer (md, P, Plen, U, hLen, U);
  74. if (rc != GPG_ERR_NO_ERROR)
  75. {
  76. grub_free (tmp);
  77. return rc;
  78. }
  79. for (k = 0; k < hLen; k++)
  80. T[k] ^= U[k];
  81. }
  82. grub_memcpy (DK + (i - 1) * hLen, T, i == l ? r : hLen);
  83. }
  84. grub_free (tmp);
  85. return GPG_ERR_NO_ERROR;
  86. }