Tizen 2.0 Release
[external/libgnutls26.git] / lib / x509 / pbkdf2-sha1.c
1 /* gc-pbkdf2-sha1.c --- Password-Based Key Derivation Function a'la PKCS#5
2    Copyright (C) 2002, 2003, 2004, 2005, 2006, 2008, 2010 Free Software
3    Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU Lesser General Public License as published by
7    the Free Software Foundation; either version 2.1, or (at your option)
8    any later version.
9
10    This program 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 Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 /* Written by Simon Josefsson.  The comments in this file are taken
20    from RFC 2898.  */
21
22 #include <gnutls_int.h>
23 #include <gnutls_datum.h>
24 #include <gnutls_errors.h>
25 #include <gnutls_hash_int.h>
26 #include <pbkdf2-sha1.h>
27
28 /*
29  * 5.2 PBKDF2
30  *
31  *  PBKDF2 applies a pseudorandom function (see Appendix B.1 for an
32  *  example) to derive keys. The length of the derived key is essentially
33  *  unbounded. (However, the maximum effective search space for the
34  *  derived key may be limited by the structure of the underlying
35  *  pseudorandom function. See Appendix B.1 for further discussion.)
36  *  PBKDF2 is recommended for new applications.
37  *
38  *  PBKDF2 (P, S, c, dkLen)
39  *
40  *  Options:        PRF        underlying pseudorandom function (hLen
41  *                             denotes the length in octets of the
42  *                             pseudorandom function output)
43  *
44  *  Input:          P          password, an octet string (ASCII or UTF-8)
45  *                  S          salt, an octet string
46  *                  c          iteration count, a positive integer
47  *                  dkLen      intended length in octets of the derived
48  *                             key, a positive integer, at most
49  *                             (2^32 - 1) * hLen
50  *
51  *  Output:         DK         derived key, a dkLen-octet string
52  */
53
54 int
55 _gnutls_pbkdf2_sha1 (const char *P, size_t Plen,
56                      const char *S, size_t Slen,
57                      unsigned int c, char *DK, size_t dkLen)
58 {
59   unsigned int hLen = 20;
60   char U[20];
61   char T[20];
62   unsigned int u;
63   unsigned int l;
64   unsigned int r;
65   unsigned int i;
66   unsigned int k;
67   int rc;
68   char *tmp;
69   size_t tmplen = Slen + 4;
70
71   if (c == 0)
72     {
73       gnutls_assert ();
74       return GNUTLS_E_INVALID_REQUEST;
75     }
76
77   if (dkLen == 0)
78     {
79       gnutls_assert ();
80       return GNUTLS_E_INVALID_REQUEST;
81     }
82   /*
83    *
84    *  Steps:
85    *
86    *     1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and
87    *        stop.
88    */
89
90   if (dkLen > 4294967295U)
91     {
92       gnutls_assert ();
93       return GNUTLS_E_INVALID_REQUEST;
94     }
95
96   /*
97    *     2. Let l be the number of hLen-octet blocks in the derived key,
98    *        rounding up, and let r be the number of octets in the last
99    *        block:
100    *
101    *                  l = CEIL (dkLen / hLen) ,
102    *                  r = dkLen - (l - 1) * hLen .
103    *
104    *        Here, CEIL (x) is the "ceiling" function, i.e. the smallest
105    *        integer greater than, or equal to, x.
106    */
107
108   l = ((dkLen - 1) / hLen) + 1;
109   r = dkLen - (l - 1) * hLen;
110
111   /*
112    *     3. For each block of the derived key apply the function F defined
113    *        below to the password P, the salt S, the iteration count c, and
114    *        the block index to compute the block:
115    *
116    *                  T_1 = F (P, S, c, 1) ,
117    *                  T_2 = F (P, S, c, 2) ,
118    *                  ...
119    *                  T_l = F (P, S, c, l) ,
120    *
121    *        where the function F is defined as the exclusive-or sum of the
122    *        first c iterates of the underlying pseudorandom function PRF
123    *        applied to the password P and the concatenation of the salt S
124    *        and the block index i:
125    *
126    *                  F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
127    *
128    *        where
129    *
130    *                  U_1 = PRF (P, S || INT (i)) ,
131    *                  U_2 = PRF (P, U_1) ,
132    *                  ...
133    *                  U_c = PRF (P, U_{c-1}) .
134    *
135    *        Here, INT (i) is a four-octet encoding of the integer i, most
136    *        significant octet first.
137    *
138    *     4. Concatenate the blocks and extract the first dkLen octets to
139    *        produce a derived key DK:
140    *
141    *                  DK = T_1 || T_2 ||  ...  || T_l<0..r-1>
142    *
143    *     5. Output the derived key DK.
144    *
145    *  Note. The construction of the function F follows a "belt-and-
146    *  suspenders" approach. The iterates U_i are computed recursively to
147    *  remove a degree of parallelism from an opponent; they are exclusive-
148    *  ored together to reduce concerns about the recursion degenerating
149    *  into a small set of values.
150    *
151    */
152
153   tmp = gnutls_malloc (tmplen);
154   if (tmp == NULL)
155     {
156       gnutls_assert ();
157       return GNUTLS_E_MEMORY_ERROR;
158     }
159
160   memcpy (tmp, S, Slen);
161
162   for (i = 1; i <= l; i++)
163     {
164       memset (T, 0, hLen);
165
166       for (u = 1; u <= c; u++)
167         {
168           if (u == 1)
169             {
170               tmp[Slen + 0] = (i & 0xff000000) >> 24;
171               tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
172               tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
173               tmp[Slen + 3] = (i & 0x000000ff) >> 0;
174
175               rc =
176                 _gnutls_hmac_fast (GNUTLS_MAC_SHA1, P, Plen, tmp, tmplen, U);
177             }
178           else
179             rc = _gnutls_hmac_fast (GNUTLS_MAC_SHA1, P, Plen, U, hLen, U);
180
181           if (rc < 0)
182             {
183               gnutls_free (tmp);
184               return rc;
185             }
186
187           for (k = 0; k < hLen; k++)
188             T[k] ^= U[k];
189         }
190
191       memcpy (DK + (i - 1) * hLen, T, i == l ? r : hLen);
192     }
193
194   gnutls_free (tmp);
195
196   return 0;
197 }