eap_pax_common.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * EAP server/peer: EAP-PAX shared routines
  3. * Copyright (c) 2005, 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_pax_common.h"
  12. /**
  13. * eap_pax_kdf - PAX Key Derivation Function
  14. * @mac_id: MAC ID (EAP_PAX_MAC_*) / currently, only HMAC_SHA1_128 is supported
  15. * @key: Secret key (X)
  16. * @key_len: Length of the secret key in bytes
  17. * @identifier: Public identifier for the key (Y)
  18. * @entropy: Exchanged entropy to seed the KDF (Z)
  19. * @entropy_len: Length of the entropy in bytes
  20. * @output_len: Output len in bytes (W)
  21. * @output: Buffer for the derived key
  22. * Returns: 0 on success, -1 failed
  23. *
  24. * RFC 4746, Section 2.6: PAX-KDF-W(X, Y, Z)
  25. */
  26. int eap_pax_kdf(u8 mac_id, const u8 *key, size_t key_len,
  27. const char *identifier,
  28. const u8 *entropy, size_t entropy_len,
  29. size_t output_len, u8 *output)
  30. {
  31. u8 mac[SHA1_MAC_LEN];
  32. u8 counter, *pos;
  33. const u8 *addr[3];
  34. size_t len[3];
  35. size_t num_blocks, left;
  36. num_blocks = (output_len + EAP_PAX_MAC_LEN - 1) / EAP_PAX_MAC_LEN;
  37. if (identifier == NULL || num_blocks >= 255)
  38. return -1;
  39. /* TODO: add support for EAP_PAX_HMAC_SHA256_128 */
  40. if (mac_id != EAP_PAX_MAC_HMAC_SHA1_128)
  41. return -1;
  42. addr[0] = (const u8 *) identifier;
  43. len[0] = os_strlen(identifier);
  44. addr[1] = entropy;
  45. len[1] = entropy_len;
  46. addr[2] = &counter;
  47. len[2] = 1;
  48. pos = output;
  49. left = output_len;
  50. for (counter = 1; counter <= (u8) num_blocks; counter++) {
  51. size_t clen = left > EAP_PAX_MAC_LEN ? EAP_PAX_MAC_LEN : left;
  52. hmac_sha1_vector(key, key_len, 3, addr, len, mac);
  53. os_memcpy(pos, mac, clen);
  54. pos += clen;
  55. left -= clen;
  56. }
  57. return 0;
  58. }
  59. /**
  60. * eap_pax_mac - EAP-PAX MAC
  61. * @mac_id: MAC ID (EAP_PAX_MAC_*) / currently, only HMAC_SHA1_128 is supported
  62. * @key: Secret key
  63. * @key_len: Length of the secret key in bytes
  64. * @data1: Optional data, first block; %NULL if not used
  65. * @data1_len: Length of data1 in bytes
  66. * @data2: Optional data, second block; %NULL if not used
  67. * @data2_len: Length of data2 in bytes
  68. * @data3: Optional data, third block; %NULL if not used
  69. * @data3_len: Length of data3 in bytes
  70. * @mac: Buffer for the MAC value (EAP_PAX_MAC_LEN = 16 bytes)
  71. * Returns: 0 on success, -1 on failure
  72. *
  73. * Wrapper function to calculate EAP-PAX MAC.
  74. */
  75. int eap_pax_mac(u8 mac_id, const u8 *key, size_t key_len,
  76. const u8 *data1, size_t data1_len,
  77. const u8 *data2, size_t data2_len,
  78. const u8 *data3, size_t data3_len,
  79. u8 *mac)
  80. {
  81. u8 hash[SHA1_MAC_LEN];
  82. const u8 *addr[3];
  83. size_t len[3];
  84. size_t count;
  85. /* TODO: add support for EAP_PAX_HMAC_SHA256_128 */
  86. if (mac_id != EAP_PAX_MAC_HMAC_SHA1_128)
  87. return -1;
  88. addr[0] = data1;
  89. len[0] = data1_len;
  90. addr[1] = data2;
  91. len[1] = data2_len;
  92. addr[2] = data3;
  93. len[2] = data3_len;
  94. count = (data1 ? 1 : 0) + (data2 ? 1 : 0) + (data3 ? 1 : 0);
  95. hmac_sha1_vector(key, key_len, count, addr, len, hash);
  96. os_memcpy(mac, hash, EAP_PAX_MAC_LEN);
  97. return 0;
  98. }
  99. /**
  100. * eap_pax_initial_key_derivation - EAP-PAX initial key derivation
  101. * @mac_id: MAC ID (EAP_PAX_MAC_*) / currently, only HMAC_SHA1_128 is supported
  102. * @ak: Authentication Key
  103. * @e: Entropy
  104. * @mk: Buffer for the derived Master Key
  105. * @ck: Buffer for the derived Confirmation Key
  106. * @ick: Buffer for the derived Integrity Check Key
  107. * @mid: Buffer for the derived Method ID
  108. * Returns: 0 on success, -1 on failure
  109. */
  110. int eap_pax_initial_key_derivation(u8 mac_id, const u8 *ak, const u8 *e,
  111. u8 *mk, u8 *ck, u8 *ick, u8 *mid)
  112. {
  113. wpa_printf(MSG_DEBUG, "EAP-PAX: initial key derivation");
  114. if (eap_pax_kdf(mac_id, ak, EAP_PAX_AK_LEN, "Master Key",
  115. e, 2 * EAP_PAX_RAND_LEN, EAP_PAX_MK_LEN, mk) ||
  116. eap_pax_kdf(mac_id, mk, EAP_PAX_MK_LEN, "Confirmation Key",
  117. e, 2 * EAP_PAX_RAND_LEN, EAP_PAX_CK_LEN, ck) ||
  118. eap_pax_kdf(mac_id, mk, EAP_PAX_MK_LEN, "Integrity Check Key",
  119. e, 2 * EAP_PAX_RAND_LEN, EAP_PAX_ICK_LEN, ick) ||
  120. eap_pax_kdf(mac_id, mk, EAP_PAX_MK_LEN, "Method ID",
  121. e, 2 * EAP_PAX_RAND_LEN, EAP_PAX_MID_LEN, mid))
  122. return -1;
  123. wpa_hexdump_key(MSG_MSGDUMP, "EAP-PAX: AK", ak, EAP_PAX_AK_LEN);
  124. wpa_hexdump_key(MSG_MSGDUMP, "EAP-PAX: MK", mk, EAP_PAX_MK_LEN);
  125. wpa_hexdump_key(MSG_MSGDUMP, "EAP-PAX: CK", ck, EAP_PAX_CK_LEN);
  126. wpa_hexdump_key(MSG_MSGDUMP, "EAP-PAX: ICK", ick, EAP_PAX_ICK_LEN);
  127. wpa_hexdump_key(MSG_MSGDUMP, "EAP-PAX: MID", mid, EAP_PAX_MID_LEN);
  128. return 0;
  129. }