sha1-pbkdf2.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "common.h"
00019 #include "sha1.h"
00020 #include "md5.h"
00021 #include "crypto.h"
00022 
00023 static int pbkdf2_sha1_f(const char *passphrase, const char *ssid,
00024                          size_t ssid_len, int iterations, unsigned int count,
00025                          u8 *digest)
00026 {
00027         unsigned char tmp[SHA1_MAC_LEN], tmp2[SHA1_MAC_LEN];
00028         int i, j;
00029         unsigned char count_buf[4];
00030         const u8 *addr[2];
00031         size_t len[2];
00032         size_t passphrase_len = os_strlen(passphrase);
00033 
00034         addr[0] = (u8 *) ssid;
00035         len[0] = ssid_len;
00036         addr[1] = count_buf;
00037         len[1] = 4;
00038 
00039         /* F(P, S, c, i) = U1 xor U2 xor ... Uc
00040          * U1 = PRF(P, S || i)
00041          * U2 = PRF(P, U1)
00042          * Uc = PRF(P, Uc-1)
00043          */
00044 
00045         count_buf[0] = (count >> 24) & 0xff;
00046         count_buf[1] = (count >> 16) & 0xff;
00047         count_buf[2] = (count >> 8) & 0xff;
00048         count_buf[3] = count & 0xff;
00049         if (hmac_sha1_vector((u8 *) passphrase, passphrase_len, 2, addr, len,
00050                              tmp))
00051                 return -1;
00052         os_memcpy(digest, tmp, SHA1_MAC_LEN);
00053 
00054         for (i = 1; i < iterations; i++) {
00055                 if (hmac_sha1((u8 *) passphrase, passphrase_len, tmp,
00056                               SHA1_MAC_LEN, tmp2))
00057                         return -1;
00058                 os_memcpy(tmp, tmp2, SHA1_MAC_LEN);
00059                 for (j = 0; j < SHA1_MAC_LEN; j++)
00060                         digest[j] ^= tmp2[j];
00061         }
00062 
00063         return 0;
00064 }
00065 
00066 
00082 int pbkdf2_sha1(const char *passphrase, const char *ssid, size_t ssid_len,
00083                 int iterations, u8 *buf, size_t buflen)
00084 {
00085         unsigned int count = 0;
00086         unsigned char *pos = buf;
00087         size_t left = buflen, plen;
00088         unsigned char digest[SHA1_MAC_LEN];
00089 
00090         while (left > 0) {
00091                 count++;
00092                 if (pbkdf2_sha1_f(passphrase, ssid, ssid_len, iterations,
00093                                   count, digest))
00094                         return -1;
00095                 plen = left > SHA1_MAC_LEN ? SHA1_MAC_LEN : left;
00096                 os_memcpy(pos, digest, plen);
00097                 pos += plen;
00098                 left -= plen;
00099         }
00100 
00101         return 0;
00102 }
00103 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:50 2009 for hostapd by  doxygen 1.6.1