sha1-pbkdf2.c 2.5 KB

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