eap_peap_common.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * EAP-PEAP common routines
  3. * Copyright (c) 2008-2011, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/sha1.h"
  11. #include "eap_peap_common.h"
  12. int peap_prfplus(int version, const u8 *key, size_t key_len,
  13. const char *label, const u8 *seed, size_t seed_len,
  14. u8 *buf, size_t buf_len)
  15. {
  16. unsigned char counter = 0;
  17. size_t pos, plen;
  18. u8 hash[SHA1_MAC_LEN];
  19. size_t label_len = os_strlen(label);
  20. u8 extra[2];
  21. const unsigned char *addr[5];
  22. size_t len[5];
  23. addr[0] = hash;
  24. len[0] = 0;
  25. addr[1] = (unsigned char *) label;
  26. len[1] = label_len;
  27. addr[2] = seed;
  28. len[2] = seed_len;
  29. if (version == 0) {
  30. /*
  31. * PRF+(K, S, LEN) = T1 | T2 | ... | Tn
  32. * T1 = HMAC-SHA1(K, S | 0x01 | 0x00 | 0x00)
  33. * T2 = HMAC-SHA1(K, T1 | S | 0x02 | 0x00 | 0x00)
  34. * ...
  35. * Tn = HMAC-SHA1(K, Tn-1 | S | n | 0x00 | 0x00)
  36. */
  37. extra[0] = 0;
  38. extra[1] = 0;
  39. addr[3] = &counter;
  40. len[3] = 1;
  41. addr[4] = extra;
  42. len[4] = 2;
  43. } else {
  44. /*
  45. * PRF (K,S,LEN) = T1 | T2 | T3 | T4 | ... where:
  46. * T1 = HMAC-SHA1(K, S | LEN | 0x01)
  47. * T2 = HMAC-SHA1 (K, T1 | S | LEN | 0x02)
  48. * T3 = HMAC-SHA1 (K, T2 | S | LEN | 0x03)
  49. * T4 = HMAC-SHA1 (K, T3 | S | LEN | 0x04)
  50. * ...
  51. */
  52. extra[0] = buf_len & 0xff;
  53. addr[3] = extra;
  54. len[3] = 1;
  55. addr[4] = &counter;
  56. len[4] = 1;
  57. }
  58. pos = 0;
  59. while (pos < buf_len) {
  60. counter++;
  61. plen = buf_len - pos;
  62. if (hmac_sha1_vector(key, key_len, 5, addr, len, hash) < 0)
  63. return -1;
  64. if (plen >= SHA1_MAC_LEN) {
  65. os_memcpy(&buf[pos], hash, SHA1_MAC_LEN);
  66. pos += SHA1_MAC_LEN;
  67. } else {
  68. os_memcpy(&buf[pos], hash, plen);
  69. break;
  70. }
  71. len[0] = SHA1_MAC_LEN;
  72. }
  73. return 0;
  74. }