sha512-prf.c 2.8 KB

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