sha384-prf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * SHA384-based KDF (IEEE 802.11ac)
  3. * Copyright (c) 2003-2015, 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 "sha384.h"
  11. #include "crypto.h"
  12. /**
  13. * sha384_prf - SHA384-based Key derivation function (IEEE 802.11ac, 11.6.1.7.2)
  14. * @key: Key for KDF
  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. *
  22. * This function is used to derive new, cryptographically separate keys from a
  23. * given key.
  24. */
  25. void sha384_prf(const u8 *key, size_t key_len, const char *label,
  26. const u8 *data, size_t data_len, u8 *buf, size_t buf_len)
  27. {
  28. sha384_prf_bits(key, key_len, label, data, data_len, buf, buf_len * 8);
  29. }
  30. /**
  31. * sha384_prf_bits - IEEE Std 802.11ac-2013, 11.6.1.7.2 Key derivation function
  32. * @key: Key for KDF
  33. * @key_len: Length of the key in bytes
  34. * @label: A unique label for each purpose of the PRF
  35. * @data: Extra data to bind into the key
  36. * @data_len: Length of the data
  37. * @buf: Buffer for the generated pseudo-random key
  38. * @buf_len: Number of bits of key to generate
  39. *
  40. * This function is used to derive new, cryptographically separate keys from a
  41. * given key. If the requested buf_len is not divisible by eight, the least
  42. * significant 1-7 bits of the last octet in the output are not part of the
  43. * requested output.
  44. */
  45. void sha384_prf_bits(const u8 *key, size_t key_len, const char *label,
  46. const u8 *data, size_t data_len, u8 *buf,
  47. size_t buf_len_bits)
  48. {
  49. u16 counter = 1;
  50. size_t pos, plen;
  51. u8 hash[SHA384_MAC_LEN];
  52. const u8 *addr[4];
  53. size_t len[4];
  54. u8 counter_le[2], length_le[2];
  55. size_t buf_len = (buf_len_bits + 7) / 8;
  56. addr[0] = counter_le;
  57. len[0] = 2;
  58. addr[1] = (u8 *) label;
  59. len[1] = os_strlen(label);
  60. addr[2] = data;
  61. len[2] = data_len;
  62. addr[3] = length_le;
  63. len[3] = sizeof(length_le);
  64. WPA_PUT_LE16(length_le, buf_len_bits);
  65. pos = 0;
  66. while (pos < buf_len) {
  67. plen = buf_len - pos;
  68. WPA_PUT_LE16(counter_le, counter);
  69. if (plen >= SHA384_MAC_LEN) {
  70. hmac_sha384_vector(key, key_len, 4, addr, len,
  71. &buf[pos]);
  72. pos += SHA384_MAC_LEN;
  73. } else {
  74. hmac_sha384_vector(key, key_len, 4, addr, len, hash);
  75. os_memcpy(&buf[pos], hash, plen);
  76. pos += plen;
  77. break;
  78. }
  79. counter++;
  80. }
  81. /*
  82. * Mask out unused bits in the last octet if it does not use all the
  83. * bits.
  84. */
  85. if (buf_len_bits % 8) {
  86. u8 mask = 0xff << (8 - buf_len_bits % 8);
  87. buf[pos - 1] &= mask;
  88. }
  89. os_memset(hash, 0, sizeof(hash));
  90. }