sha1-tprf.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * SHA1 T-PRF for EAP-FAST
  3. * Copyright (c) 2003-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 "sha1.h"
  11. #include "crypto.h"
  12. /**
  13. * sha1_t_prf - EAP-FAST Pseudo-Random Function (T-PRF)
  14. * @key: Key for PRF
  15. * @key_len: Length of the key in bytes
  16. * @label: A unique label for each purpose of the PRF
  17. * @seed: Seed value to bind into the key
  18. * @seed_len: Length of the seed
  19. * @buf: Buffer for the generated pseudo-random key
  20. * @buf_len: Number of bytes of key to generate
  21. * Returns: 0 on success, -1 of failure
  22. *
  23. * This function is used to derive new, cryptographically separate keys from a
  24. * given key for EAP-FAST. T-PRF is defined in RFC 4851, Section 5.5.
  25. */
  26. int sha1_t_prf(const u8 *key, size_t key_len, const char *label,
  27. const u8 *seed, size_t seed_len, u8 *buf, size_t buf_len)
  28. {
  29. unsigned char counter = 0;
  30. size_t pos, plen;
  31. u8 hash[SHA1_MAC_LEN];
  32. size_t label_len = os_strlen(label);
  33. u8 output_len[2];
  34. const unsigned char *addr[5];
  35. size_t len[5];
  36. addr[0] = hash;
  37. len[0] = 0;
  38. addr[1] = (unsigned char *) label;
  39. len[1] = label_len + 1;
  40. addr[2] = seed;
  41. len[2] = seed_len;
  42. addr[3] = output_len;
  43. len[3] = 2;
  44. addr[4] = &counter;
  45. len[4] = 1;
  46. output_len[0] = (buf_len >> 8) & 0xff;
  47. output_len[1] = buf_len & 0xff;
  48. pos = 0;
  49. while (pos < buf_len) {
  50. counter++;
  51. plen = buf_len - pos;
  52. if (hmac_sha1_vector(key, key_len, 5, addr, len, hash))
  53. return -1;
  54. if (plen >= SHA1_MAC_LEN) {
  55. os_memcpy(&buf[pos], hash, SHA1_MAC_LEN);
  56. pos += SHA1_MAC_LEN;
  57. } else {
  58. os_memcpy(&buf[pos], hash, plen);
  59. break;
  60. }
  61. len[0] = SHA1_MAC_LEN;
  62. }
  63. os_memset(hash, 0, SHA1_MAC_LEN);
  64. return 0;
  65. }