1e28a4053SRui Paulo /*
2e28a4053SRui Paulo * SHA1-based key derivation function (PBKDF2) for IEEE 802.11i
3e28a4053SRui Paulo * Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
4e28a4053SRui Paulo *
5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo * See README for more details.
7e28a4053SRui Paulo */
8e28a4053SRui Paulo
9e28a4053SRui Paulo #include "includes.h"
10e28a4053SRui Paulo
11e28a4053SRui Paulo #include "common.h"
12e28a4053SRui Paulo #include "sha1.h"
13e28a4053SRui Paulo
pbkdf2_sha1_f(const char * passphrase,const u8 * ssid,size_t ssid_len,int iterations,unsigned int count,u8 * digest)14f05cddf9SRui Paulo static int pbkdf2_sha1_f(const char *passphrase, const u8 *ssid,
15e28a4053SRui Paulo size_t ssid_len, int iterations, unsigned int count,
16e28a4053SRui Paulo u8 *digest)
17e28a4053SRui Paulo {
18e28a4053SRui Paulo unsigned char tmp[SHA1_MAC_LEN], tmp2[SHA1_MAC_LEN];
19e28a4053SRui Paulo int i, j;
20e28a4053SRui Paulo unsigned char count_buf[4];
21e28a4053SRui Paulo const u8 *addr[2];
22e28a4053SRui Paulo size_t len[2];
23e28a4053SRui Paulo size_t passphrase_len = os_strlen(passphrase);
24e28a4053SRui Paulo
25f05cddf9SRui Paulo addr[0] = ssid;
26e28a4053SRui Paulo len[0] = ssid_len;
27e28a4053SRui Paulo addr[1] = count_buf;
28e28a4053SRui Paulo len[1] = 4;
29e28a4053SRui Paulo
30e28a4053SRui Paulo /* F(P, S, c, i) = U1 xor U2 xor ... Uc
31e28a4053SRui Paulo * U1 = PRF(P, S || i)
32e28a4053SRui Paulo * U2 = PRF(P, U1)
33e28a4053SRui Paulo * Uc = PRF(P, Uc-1)
34e28a4053SRui Paulo */
35e28a4053SRui Paulo
36e28a4053SRui Paulo count_buf[0] = (count >> 24) & 0xff;
37e28a4053SRui Paulo count_buf[1] = (count >> 16) & 0xff;
38e28a4053SRui Paulo count_buf[2] = (count >> 8) & 0xff;
39e28a4053SRui Paulo count_buf[3] = count & 0xff;
40e28a4053SRui Paulo if (hmac_sha1_vector((u8 *) passphrase, passphrase_len, 2, addr, len,
41e28a4053SRui Paulo tmp))
42e28a4053SRui Paulo return -1;
43e28a4053SRui Paulo os_memcpy(digest, tmp, SHA1_MAC_LEN);
44e28a4053SRui Paulo
45e28a4053SRui Paulo for (i = 1; i < iterations; i++) {
46e28a4053SRui Paulo if (hmac_sha1((u8 *) passphrase, passphrase_len, tmp,
47e28a4053SRui Paulo SHA1_MAC_LEN, tmp2))
48e28a4053SRui Paulo return -1;
49e28a4053SRui Paulo os_memcpy(tmp, tmp2, SHA1_MAC_LEN);
50e28a4053SRui Paulo for (j = 0; j < SHA1_MAC_LEN; j++)
51e28a4053SRui Paulo digest[j] ^= tmp2[j];
52e28a4053SRui Paulo }
53*a90b9d01SCy Schubert forced_memzero(tmp, SHA1_MAC_LEN);
54*a90b9d01SCy Schubert forced_memzero(tmp2, SHA1_MAC_LEN);
55e28a4053SRui Paulo
56e28a4053SRui Paulo return 0;
57e28a4053SRui Paulo }
58e28a4053SRui Paulo
59e28a4053SRui Paulo
60e28a4053SRui Paulo /**
61e28a4053SRui Paulo * pbkdf2_sha1 - SHA1-based key derivation function (PBKDF2) for IEEE 802.11i
62e28a4053SRui Paulo * @passphrase: ASCII passphrase
63e28a4053SRui Paulo * @ssid: SSID
64e28a4053SRui Paulo * @ssid_len: SSID length in bytes
65e28a4053SRui Paulo * @iterations: Number of iterations to run
66e28a4053SRui Paulo * @buf: Buffer for the generated key
67e28a4053SRui Paulo * @buflen: Length of the buffer in bytes
68e28a4053SRui Paulo * Returns: 0 on success, -1 of failure
69e28a4053SRui Paulo *
70e28a4053SRui Paulo * This function is used to derive PSK for WPA-PSK. For this protocol,
71e28a4053SRui Paulo * iterations is set to 4096 and buflen to 32. This function is described in
72e28a4053SRui Paulo * IEEE Std 802.11-2004, Clause H.4. The main construction is from PKCS#5 v2.0.
73e28a4053SRui Paulo */
pbkdf2_sha1(const char * passphrase,const u8 * ssid,size_t ssid_len,int iterations,u8 * buf,size_t buflen)74f05cddf9SRui Paulo int pbkdf2_sha1(const char *passphrase, const u8 *ssid, size_t ssid_len,
75e28a4053SRui Paulo int iterations, u8 *buf, size_t buflen)
76e28a4053SRui Paulo {
77e28a4053SRui Paulo unsigned int count = 0;
78e28a4053SRui Paulo unsigned char *pos = buf;
79e28a4053SRui Paulo size_t left = buflen, plen;
80e28a4053SRui Paulo unsigned char digest[SHA1_MAC_LEN];
81e28a4053SRui Paulo
82e28a4053SRui Paulo while (left > 0) {
83e28a4053SRui Paulo count++;
84e28a4053SRui Paulo if (pbkdf2_sha1_f(passphrase, ssid, ssid_len, iterations,
85e28a4053SRui Paulo count, digest))
86e28a4053SRui Paulo return -1;
87e28a4053SRui Paulo plen = left > SHA1_MAC_LEN ? SHA1_MAC_LEN : left;
88e28a4053SRui Paulo os_memcpy(pos, digest, plen);
89e28a4053SRui Paulo pos += plen;
90e28a4053SRui Paulo left -= plen;
91e28a4053SRui Paulo }
92*a90b9d01SCy Schubert forced_memzero(digest, SHA1_MAC_LEN);
93e28a4053SRui Paulo
94e28a4053SRui Paulo return 0;
95e28a4053SRui Paulo }
96