| 1 | /* gc-pbkdf2-sha1.c --- Password-Based Key Derivation Function a'la PKCS#5 |
| 2 | Copyright (C) 2002-2006, 2009-2012 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 Lesser General Public License as published by |
| 6 | the Free Software Foundation; either version 2.1, 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 Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public License |
| 15 | along with this program; if not, see <http://www.gnu.org/licenses/>. */ |
| 16 | |
| 17 | /* Written by Simon Josefsson. */ |
| 18 | |
| 19 | #include <config.h> |
| 20 | |
| 21 | #include "gc.h" |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | /* Implement PKCS#5 PBKDF2 as per RFC 2898. The PRF to use is hard |
| 27 | coded to be HMAC-SHA1. Inputs are the password P of length PLEN, |
| 28 | the salt S of length SLEN, the iteration counter C (> 0), and the |
| 29 | desired derived output length DKLEN. Output buffer is DK which |
| 30 | must have room for at least DKLEN octets. The output buffer will |
| 31 | be filled with the derived data. */ |
| 32 | Gc_rc |
| 33 | gc_pbkdf2_sha1 (const char *P, size_t Plen, |
| 34 | const char *S, size_t Slen, |
| 35 | unsigned int c, |
| 36 | char *DK, size_t dkLen) |
| 37 | { |
| 38 | unsigned int hLen = 20; |
| 39 | char U[20]; |
| 40 | char T[20]; |
| 41 | unsigned int u; |
| 42 | unsigned int l; |
| 43 | unsigned int r; |
| 44 | unsigned int i; |
| 45 | unsigned int k; |
| 46 | int rc; |
| 47 | char *tmp; |
| 48 | size_t tmplen = Slen + 4; |
| 49 | |
| 50 | if (c == 0) |
| 51 | return GC_PKCS5_INVALID_ITERATION_COUNT; |
| 52 | |
| 53 | if (dkLen == 0) |
| 54 | return GC_PKCS5_INVALID_DERIVED_KEY_LENGTH; |
| 55 | |
| 56 | if (dkLen > 4294967295U) |
| 57 | return GC_PKCS5_DERIVED_KEY_TOO_LONG; |
| 58 | |
| 59 | l = ((dkLen - 1) / hLen) + 1; |
| 60 | r = dkLen - (l - 1) * hLen; |
| 61 | |
| 62 | tmp = malloc (tmplen); |
| 63 | if (tmp == NULL) |
| 64 | return GC_MALLOC_ERROR; |
| 65 | |
| 66 | memcpy (tmp, S, Slen); |
| 67 | |
| 68 | for (i = 1; i <= l; i++) |
| 69 | { |
| 70 | memset (T, 0, hLen); |
| 71 | |
| 72 | for (u = 1; u <= c; u++) |
| 73 | { |
| 74 | if (u == 1) |
| 75 | { |
| 76 | tmp[Slen + 0] = (i & 0xff000000) >> 24; |
| 77 | tmp[Slen + 1] = (i & 0x00ff0000) >> 16; |
| 78 | tmp[Slen + 2] = (i & 0x0000ff00) >> 8; |
| 79 | tmp[Slen + 3] = (i & 0x000000ff) >> 0; |
| 80 | |
| 81 | rc = gc_hmac_sha1 (P, Plen, tmp, tmplen, U); |
| 82 | } |
| 83 | else |
| 84 | rc = gc_hmac_sha1 (P, Plen, U, hLen, U); |
| 85 | |
| 86 | if (rc != GC_OK) |
| 87 | { |
| 88 | free (tmp); |
| 89 | return rc; |
| 90 | } |
| 91 | |
| 92 | for (k = 0; k < hLen; k++) |
| 93 | T[k] ^= U[k]; |
| 94 | } |
| 95 | |
| 96 | memcpy (DK + (i - 1) * hLen, T, i == l ? r : hLen); |
| 97 | } |
| 98 | |
| 99 | free (tmp); |
| 100 | |
| 101 | return GC_OK; |
| 102 | } |
| 103 | |