1 /* pkcs5.c Implementation of Password-Based Cryptography as per PKCS#5
2 * Copyright (C) 2002, 2003 Simon Josefsson
4 * This file is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This file 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this file; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * PBKDF2 applies a pseudorandom function (see Appendix B.1 for an
31 * example) to derive keys. The length of the derived key is essentially
32 * unbounded. (However, the maximum effective search space for the
33 * derived key may be limited by the structure of the underlying
34 * pseudorandom function. See Appendix B.1 for further discussion.)
35 * PBKDF2 is recommended for new applications.
37 * PBKDF2 (P, S, c, dkLen)
39 * Options: PRF underlying pseudorandom function (hLen
40 * denotes the length in octets of the
41 * pseudorandom function output)
43 * Input: P password, an octet string
44 * S salt, an octet string
45 * c iteration count, a positive integer
46 * dkLen intended length in octets of the derived
47 * key, a positive integer, at most
50 * Output: DK derived key, a dkLen-octet string
53 #define MAX_PRF_BLOCK_LEN 80
56 shishi_pbkdf2_sha1 (const char *P
,
65 int PRF
= GCRY_MD_SHA1
;
67 unsigned int hLen
= gcry_md_get_algo_dlen (PRF
);
69 struct hmac_sha1_ctx hmac
;
70 unsigned int hLen
= SHA1_DIGEST_SIZE
;
72 char U
[MAX_PRF_BLOCK_LEN
];
73 char T
[MAX_PRF_BLOCK_LEN
];
82 if (hLen
== 0 || hLen
> MAX_PRF_BLOCK_LEN
)
83 return SHISHI_PKCS5_INVALID_PRF
;
86 return SHISHI_PKCS5_INVALID_ITERATION_COUNT
;
89 return SHISHI_PKCS5_INVALID_DERIVED_KEY_LENGTH
;
95 * 1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and
99 if (dkLen
> 4294967295U)
100 return SHISHI_PKCS5_DERIVED_KEY_TOO_LONG
;
103 * 2. Let l be the number of hLen-octet blocks in the derived key,
104 * rounding up, and let r be the number of octets in the last
107 * l = CEIL (dkLen / hLen) ,
108 * r = dkLen - (l - 1) * hLen .
110 * Here, CEIL (x) is the "ceiling" function, i.e. the smallest
111 * integer greater than, or equal to, x.
117 r
= dkLen
- (l
- 1) * hLen
;
120 * 3. For each block of the derived key apply the function F defined
121 * below to the password P, the salt S, the iteration count c, and
122 * the block index to compute the block:
124 * T_1 = F (P, S, c, 1) ,
125 * T_2 = F (P, S, c, 2) ,
127 * T_l = F (P, S, c, l) ,
129 * where the function F is defined as the exclusive-or sum of the
130 * first c iterates of the underlying pseudorandom function PRF
131 * applied to the password P and the concatenation of the salt S
132 * and the block index i:
134 * F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
138 * U_1 = PRF (P, S || INT (i)) ,
139 * U_2 = PRF (P, U_1) ,
141 * U_c = PRF (P, U_{c-1}) .
143 * Here, INT (i) is a four-octet encoding of the integer i, most
144 * significant octet first.
146 * 4. Concatenate the blocks and extract the first dkLen octets to
147 * produce a derived key DK:
149 * DK = T_1 || T_2 || ... || T_l<0..r-1>
151 * 5. Output the derived key DK.
153 * Note. The construction of the function F follows a "belt-and-
154 * suspenders" approach. The iterates U_i are computed recursively to
155 * remove a degree of parallelism from an opponent; they are exclusive-
156 * ored together to reduce concerns about the recursion degenerating
157 * into a small set of values.
162 gcry_md_open (&prf
, PRF
, GCRY_MD_FLAG_HMAC
);
164 return SHISHI_PKCS5_INVALID_PRF
;
167 for (i
= 1; i
<= l
; i
++)
171 for (u
= 1; u
<= c
; u
++)
178 rc
= gcry_md_setkey (prf
, P
, Plen
);
179 if (rc
!= GPG_ERR_NO_ERROR
)
180 return SHISHI_PKCS5_INVALID_PRF
;
182 hmac_sha1_set_key (&hmac
, Plen
, P
);
189 tmp
[0] = (i
& 0xff000000) >> 24;
190 tmp
[1] = (i
& 0x00ff0000) >> 16;
191 tmp
[2] = (i
& 0x0000ff00) >> 8;
192 tmp
[3] = (i
& 0x000000ff) >> 0;
193 memcpy (U
+ Slen
, tmp
, 4);
200 gcry_md_write (prf
, U
, Ulen
);
202 p
= gcry_md_read (prf
, PRF
);
204 return SHISHI_PKCS5_INVALID_PRF
;
208 hmac_sha1_update (&hmac
, Ulen
, U
);
209 hmac_sha1_digest (&hmac
, hLen
, U
);
213 for (k
= 0; k
< hLen
; k
++)
217 memcpy (DK
+ (i
- 1) * hLen
, T
, i
== l
? r
: hLen
);