sha1-prf.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * SHA1-based PRF
  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_prf - SHA1-based Pseudo-Random Function (PRF) (IEEE 802.11i, 8.5.1.1)
  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. * @data: Extra data to bind into the key
  18. * @data_len: Length of the data
  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 (e.g., PMK in IEEE 802.11i).
  25. */
  26. int sha1_prf(const u8 *key, size_t key_len, const char *label,
  27. const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
  28. {
  29. u8 counter = 0;
  30. size_t pos, plen;
  31. u8 hash[SHA1_MAC_LEN];
  32. size_t label_len = os_strlen(label) + 1;
  33. const unsigned char *addr[3];
  34. size_t len[3];
  35. addr[0] = (u8 *) label;
  36. len[0] = label_len;
  37. addr[1] = data;
  38. len[1] = data_len;
  39. addr[2] = &counter;
  40. len[2] = 1;
  41. pos = 0;
  42. while (pos < buf_len) {
  43. plen = buf_len - pos;
  44. if (plen >= SHA1_MAC_LEN) {
  45. if (hmac_sha1_vector(key, key_len, 3, addr, len,
  46. &buf[pos]))
  47. return -1;
  48. pos += SHA1_MAC_LEN;
  49. } else {
  50. if (hmac_sha1_vector(key, key_len, 3, addr, len,
  51. hash))
  52. return -1;
  53. os_memcpy(&buf[pos], hash, plen);
  54. break;
  55. }
  56. counter++;
  57. }
  58. return 0;
  59. }