Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / lib / pbkdf2.c
blobb4c69a17919adabf91c50547839e7a93a233b1b3
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.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Simon Josefsson. */
19 /* Imported from gnulib. */
21 #include <grub/crypto.h>
22 #include <grub/mm.h>
23 #include <grub/misc.h>
24 #include <grub/dl.h>
26 GRUB_MOD_LICENSE ("GPLv2+");
28 /* Implement PKCS#5 PBKDF2 as per RFC 2898. The PRF to use is HMAC variant
29 of digest supplied by MD. Inputs are the password P of length PLEN,
30 the salt S of length SLEN, the iteration counter C (> 0), and the
31 desired derived output length DKLEN. Output buffer is DK which
32 must have room for at least DKLEN octets. The output buffer will
33 be filled with the derived data. */
34 #pragma GCC diagnostic ignored "-Wunreachable-code"
36 gcry_err_code_t
37 grub_crypto_pbkdf2 (const struct gcry_md_spec *md,
38 const grub_uint8_t *P, grub_size_t Plen,
39 const grub_uint8_t *S, grub_size_t Slen,
40 unsigned int c,
41 grub_uint8_t *DK, grub_size_t dkLen)
43 unsigned int hLen = md->mdlen;
44 grub_uint8_t U[md->mdlen];
45 grub_uint8_t T[md->mdlen];
46 unsigned int u;
47 unsigned int l;
48 unsigned int r;
49 unsigned int i;
50 unsigned int k;
51 gcry_err_code_t rc;
52 grub_uint8_t *tmp;
53 grub_size_t tmplen = Slen + 4;
55 if (c == 0)
56 return GPG_ERR_INV_ARG;
58 if (dkLen == 0)
59 return GPG_ERR_INV_ARG;
61 if (dkLen > 4294967295U)
62 return GPG_ERR_INV_ARG;
64 l = ((dkLen - 1) / hLen) + 1;
65 r = dkLen - (l - 1) * hLen;
67 tmp = grub_malloc (tmplen);
68 if (tmp == NULL)
69 return GPG_ERR_OUT_OF_MEMORY;
71 grub_memcpy (tmp, S, Slen);
73 for (i = 1; i - 1 < l; i++)
75 grub_memset (T, 0, hLen);
77 for (u = 0; u < c; u++)
79 if (u == 0)
81 tmp[Slen + 0] = (i & 0xff000000) >> 24;
82 tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
83 tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
84 tmp[Slen + 3] = (i & 0x000000ff) >> 0;
86 rc = grub_crypto_hmac_buffer (md, P, Plen, tmp, tmplen, U);
88 else
89 rc = grub_crypto_hmac_buffer (md, P, Plen, U, hLen, U);
91 if (rc != GPG_ERR_NO_ERROR)
93 grub_free (tmp);
94 return rc;
97 for (k = 0; k < hLen; k++)
98 T[k] ^= U[k];
101 grub_memcpy (DK + (i - 1) * hLen, T, i == l ? r : hLen);
104 grub_free (tmp);
106 return GPG_ERR_NO_ERROR;