eap_psk_common.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * EAP server/peer: EAP-PSK shared routines
  3. * Copyright (c) 2004-2006, 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 "crypto/aes_wrap.h"
  17. #include "eap_defs.h"
  18. #include "eap_psk_common.h"
  19. #define aes_block_size 16
  20. int eap_psk_key_setup(const u8 *psk, u8 *ak, u8 *kdk)
  21. {
  22. os_memset(ak, 0, aes_block_size);
  23. if (aes_128_encrypt_block(psk, ak, ak))
  24. return -1;
  25. os_memcpy(kdk, ak, aes_block_size);
  26. ak[aes_block_size - 1] ^= 0x01;
  27. kdk[aes_block_size - 1] ^= 0x02;
  28. if (aes_128_encrypt_block(psk, ak, ak) ||
  29. aes_128_encrypt_block(psk, kdk, kdk))
  30. return -1;
  31. return 0;
  32. }
  33. int eap_psk_derive_keys(const u8 *kdk, const u8 *rand_p, u8 *tek, u8 *msk,
  34. u8 *emsk)
  35. {
  36. u8 hash[aes_block_size];
  37. u8 counter = 1;
  38. int i;
  39. if (aes_128_encrypt_block(kdk, rand_p, hash))
  40. return -1;
  41. hash[aes_block_size - 1] ^= counter;
  42. if (aes_128_encrypt_block(kdk, hash, tek))
  43. return -1;
  44. hash[aes_block_size - 1] ^= counter;
  45. counter++;
  46. for (i = 0; i < EAP_MSK_LEN / aes_block_size; i++) {
  47. hash[aes_block_size - 1] ^= counter;
  48. if (aes_128_encrypt_block(kdk, hash, &msk[i * aes_block_size]))
  49. return -1;
  50. hash[aes_block_size - 1] ^= counter;
  51. counter++;
  52. }
  53. for (i = 0; i < EAP_EMSK_LEN / aes_block_size; i++) {
  54. hash[aes_block_size - 1] ^= counter;
  55. if (aes_128_encrypt_block(kdk, hash,
  56. &emsk[i * aes_block_size]))
  57. return -1;
  58. hash[aes_block_size - 1] ^= counter;
  59. counter++;
  60. }
  61. return 0;
  62. }