source: grub-pc/trunk/fuentes/.pc/remove-unreachable-code-pragmas.patch/grub-core/lib/pbkdf2.c @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

File size: 3.0 KB
Line 
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
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.
8
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.
13
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.  */
17
18/* Written by Simon Josefsson.  */
19/* Imported from gnulib.  */
20
21#include <grub/crypto.h>
22#include <grub/mm.h>
23#include <grub/misc.h>
24#include <grub/dl.h>
25
26GRUB_MOD_LICENSE ("GPLv2+");
27
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"
35
36gcry_err_code_t
37grub_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)
42{
43  unsigned int hLen = md->mdlen;
44  grub_uint8_t U[GRUB_CRYPTO_MAX_MDLEN];
45  grub_uint8_t T[GRUB_CRYPTO_MAX_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;
54
55  if (md->mdlen > GRUB_CRYPTO_MAX_MDLEN)
56    return GPG_ERR_INV_ARG;
57
58  if (c == 0)
59    return GPG_ERR_INV_ARG;
60
61  if (dkLen == 0)
62    return GPG_ERR_INV_ARG;
63
64  if (dkLen > 4294967295U)
65    return GPG_ERR_INV_ARG;
66
67  l = ((dkLen - 1) / hLen) + 1;
68  r = dkLen - (l - 1) * hLen;
69
70  tmp = grub_malloc (tmplen);
71  if (tmp == NULL)
72    return GPG_ERR_OUT_OF_MEMORY;
73
74  grub_memcpy (tmp, S, Slen);
75
76  for (i = 1; i - 1 < l; i++)
77    {
78      grub_memset (T, 0, hLen);
79
80      for (u = 0; u < c; u++)
81        {
82          if (u == 0)
83            {
84              tmp[Slen + 0] = (i & 0xff000000) >> 24;
85              tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
86              tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
87              tmp[Slen + 3] = (i & 0x000000ff) >> 0;
88
89              rc = grub_crypto_hmac_buffer (md, P, Plen, tmp, tmplen, U);
90            }
91          else
92            rc = grub_crypto_hmac_buffer (md, P, Plen, U, hLen, U);
93
94          if (rc != GPG_ERR_NO_ERROR)
95            {
96              grub_free (tmp);
97              return rc;
98            }
99
100          for (k = 0; k < hLen; k++)
101            T[k] ^= U[k];
102        }
103
104      grub_memcpy (DK + (i - 1) * hLen, T, i == l ? r : hLen);
105    }
106
107  grub_free (tmp);
108
109  return GPG_ERR_NO_ERROR;
110}
Note: See TracBrowser for help on using the repository browser.