eap_peap_common.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * EAP-PEAP common routines
  3. * Copyright (c) 2008, 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 "eap_peap_common.h"
  18. void peap_prfplus(int version, const u8 *key, size_t key_len,
  19. const char *label, const u8 *seed, size_t seed_len,
  20. u8 *buf, size_t buf_len)
  21. {
  22. unsigned char counter = 0;
  23. size_t pos, plen;
  24. u8 hash[SHA1_MAC_LEN];
  25. size_t label_len = os_strlen(label);
  26. u8 extra[2];
  27. const unsigned char *addr[5];
  28. size_t len[5];
  29. addr[0] = hash;
  30. len[0] = 0;
  31. addr[1] = (unsigned char *) label;
  32. len[1] = label_len;
  33. addr[2] = seed;
  34. len[2] = seed_len;
  35. if (version == 0) {
  36. /*
  37. * PRF+(K, S, LEN) = T1 | T2 | ... | Tn
  38. * T1 = HMAC-SHA1(K, S | 0x01 | 0x00 | 0x00)
  39. * T2 = HMAC-SHA1(K, T1 | S | 0x02 | 0x00 | 0x00)
  40. * ...
  41. * Tn = HMAC-SHA1(K, Tn-1 | S | n | 0x00 | 0x00)
  42. */
  43. extra[0] = 0;
  44. extra[1] = 0;
  45. addr[3] = &counter;
  46. len[3] = 1;
  47. addr[4] = extra;
  48. len[4] = 2;
  49. } else {
  50. /*
  51. * PRF (K,S,LEN) = T1 | T2 | T3 | T4 | ... where:
  52. * T1 = HMAC-SHA1(K, S | LEN | 0x01)
  53. * T2 = HMAC-SHA1 (K, T1 | S | LEN | 0x02)
  54. * T3 = HMAC-SHA1 (K, T2 | S | LEN | 0x03)
  55. * T4 = HMAC-SHA1 (K, T3 | S | LEN | 0x04)
  56. * ...
  57. */
  58. extra[0] = buf_len & 0xff;
  59. addr[3] = extra;
  60. len[3] = 1;
  61. addr[4] = &counter;
  62. len[4] = 1;
  63. }
  64. pos = 0;
  65. while (pos < buf_len) {
  66. counter++;
  67. plen = buf_len - pos;
  68. hmac_sha1_vector(key, key_len, 5, addr, len, hash);
  69. if (plen >= SHA1_MAC_LEN) {
  70. os_memcpy(&buf[pos], hash, SHA1_MAC_LEN);
  71. pos += SHA1_MAC_LEN;
  72. } else {
  73. os_memcpy(&buf[pos], hash, plen);
  74. break;
  75. }
  76. len[0] = SHA1_MAC_LEN;
  77. }
  78. }