kdf.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* kdf.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*!
  22. \file wolfssl/wolfcrypt/kdf.h
  23. */
  24. #ifndef NO_KDF
  25. #ifndef WOLF_CRYPT_KDF_H
  26. #define WOLF_CRYPT_KDF_H
  27. #if defined(HAVE_FIPS) && \
  28. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  29. #include <wolfssl/wolfcrypt/fips.h>
  30. #endif
  31. #include <wolfssl/wolfcrypt/hmac.h>
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. enum max_prf {
  36. #ifdef HAVE_FFDHE_8192
  37. MAX_PRF_HALF = 516, /* Maximum half secret len */
  38. #elif defined(HAVE_FFDHE_6144)
  39. MAX_PRF_HALF = 388, /* Maximum half secret len */
  40. #else
  41. MAX_PRF_HALF = 260, /* Maximum half secret len */
  42. #endif
  43. MAX_PRF_LABSEED = 128, /* Maximum label + seed len */
  44. MAX_PRF_DIG = 224 /* Maximum digest len */
  45. };
  46. #ifdef WOLFSSL_HAVE_PRF
  47. WOLFSSL_API int wc_PRF(byte* result, word32 resLen, const byte* secret,
  48. word32 secLen, const byte* seed, word32 seedLen, int hash,
  49. void* heap, int devId);
  50. WOLFSSL_API int wc_PRF_TLSv1(byte* digest, word32 digLen, const byte* secret,
  51. word32 secLen, const byte* label, word32 labLen,
  52. const byte* seed, word32 seedLen, void* heap, int devId);
  53. WOLFSSL_API int wc_PRF_TLS(byte* digest, word32 digLen, const byte* secret,
  54. word32 secLen, const byte* label, word32 labLen,
  55. const byte* seed, word32 seedLen, int useAtLeastSha256,
  56. int hash_type, void* heap, int devId);
  57. #endif /* WOLFSSL_HAVE_PRF */
  58. #ifdef HAVE_HKDF
  59. enum {
  60. /*
  61. MAX_HKDF_LABEL_SZ = OPAQUE16_LEN +
  62. OPAQUE8_LEN + PROTOCOL_LABEL_SZ + MAX_LABEL_SZ +
  63. OPAQUE8_LEN + WC_MAX_DIGEST_SIZE
  64. */
  65. MAX_TLS13_HKDF_LABEL_SZ = 47 + WC_MAX_DIGEST_SIZE
  66. };
  67. WOLFSSL_API int wc_Tls13_HKDF_Extract(byte* prk, const byte* salt,
  68. word32 saltLen, byte* ikm, word32 ikmLen, int digest);
  69. WOLFSSL_API int wc_Tls13_HKDF_Expand_Label(byte* okm, word32 okmLen,
  70. const byte* prk, word32 prkLen,
  71. const byte* protocol, word32 protocolLen,
  72. const byte* label, word32 labelLen,
  73. const byte* info, word32 infoLen,
  74. int digest);
  75. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && \
  76. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  77. WOLFSSL_API int wc_Tls13_HKDF_Expand_Label_Alloc(byte* okm, word32 okmLen,
  78. const byte* prk, word32 prkLen, const byte* protocol, word32 protocolLen,
  79. const byte* label, word32 labelLen, const byte* info, word32 infoLen,
  80. int digest, void* heap);
  81. #endif /* !defined(HAVE_FIPS) || FIPS_VERSION_GE(5,3) */
  82. #endif /* HAVE_HKDF */
  83. #ifdef WOLFSSL_WOLFSSH
  84. WOLFSSL_API int wc_SSH_KDF(byte hashId, byte keyId,
  85. byte* key, word32 keySz,
  86. const byte* k, word32 kSz,
  87. const byte* h, word32 hSz,
  88. const byte* sessionId, word32 sessionIdSz);
  89. #endif /* WOLFSSL_WOLFSSH */
  90. #ifdef WC_SRTP_KDF
  91. /* Label values for purpose. */
  92. #define WC_SRTP_LABEL_ENCRYPTION 0x00
  93. #define WC_SRTP_LABEL_MSG_AUTH 0x01
  94. #define WC_SRTP_LABEL_SALT 0x02
  95. #define WC_SRTCP_LABEL_ENCRYPTION 0x03
  96. #define WC_SRTCP_LABEL_MSG_AUTH 0x04
  97. #define WC_SRTCP_LABEL_SALT 0x05
  98. #define WC_SRTP_LABEL_HDR_ENCRYPTION 0x06
  99. #define WC_SRTP_LABEL_HDR_SALT 0x07
  100. /* Length of index for SRTP KDF. */
  101. #define WC_SRTP_INDEX_LEN 6
  102. /* Length of index for SRTCP KDF. */
  103. #define WC_SRTCP_INDEX_LEN 4
  104. /* Maximum length of salt that can be used with SRTP/SRTCP. */
  105. #define WC_SRTP_MAX_SALT 14
  106. WOLFSSL_API int wc_SRTP_KDF(const byte* key, word32 keySz, const byte* salt,
  107. word32 saltSz, int kdrIdx, const byte* index, byte* key1, word32 key1Sz,
  108. byte* key2, word32 key2Sz, byte* key3, word32 key3Sz);
  109. WOLFSSL_API int wc_SRTCP_KDF(const byte* key, word32 keySz, const byte* salt,
  110. word32 saltSz, int kdrIdx, const byte* index, byte* key1, word32 key1Sz,
  111. byte* key2, word32 key2Sz, byte* key3, word32 key3Sz);
  112. WOLFSSL_API int wc_SRTP_KDF_label(const byte* key, word32 keySz,
  113. const byte* salt, word32 saltSz, int kdrIdx, const byte* index, byte label,
  114. byte* outKey, word32 outKeySz);
  115. WOLFSSL_API int wc_SRTCP_KDF_label(const byte* key, word32 keySz,
  116. const byte* salt, word32 saltSz, int kdrIdx, const byte* index, byte label,
  117. byte* outKey, word32 outKeySz);
  118. WOLFSSL_API int wc_SRTP_KDF_kdr_to_idx(word32 kdr);
  119. #endif /* WC_SRTP_KDF */
  120. #ifdef __cplusplus
  121. } /* extern "C" */
  122. #endif
  123. #endif /* WOLF_CRYPT_KDF_H */
  124. #endif /* NO_KDF */