1
0

pwdbased.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*!
  2. \ingroup Password
  3. \brief This function implements the Password Based Key Derivation
  4. Function 1 (PBKDF1), converting an input password with a concatenated salt
  5. into a more secure key, which it stores in output. It allows the user to
  6. select between SHA and MD5 as hash functions.
  7. \return 0 Returned on successfully deriving a key from the input password
  8. \return BAD_FUNC_ARG Returned if there is an invalid hash type given
  9. (valid type are: MD5 and SHA), iterations is less than 1, or the key
  10. length (kLen) requested is greater than the hash length of the provided hash
  11. \return MEMORY_E Returned if there is an error allocating memory for a
  12. SHA or MD5 object
  13. \param output pointer to the buffer in which to store the generated key.
  14. Should be at least kLen long
  15. \param passwd pointer to the buffer containing the password to use for
  16. the key derivation
  17. \param pLen length of the password to use for key derivation
  18. \param salt pointer to the buffer containing the salt to use for
  19. key derivation
  20. \param sLen length of the salt
  21. \param iterations number of times to process the hash
  22. \param kLen desired length of the derived key. Should not be longer
  23. than the digest size of the hash chosen
  24. \param hashType the hashing algorithm to use. Valid choices are WC_MD5 and WC_SHA
  25. _Example_
  26. \code
  27. int ret;
  28. byte key[WC_MD5_DIGEST_SIZE];
  29. byte pass[] = { }; // initialize with password
  30. byte salt[] = { }; // initialize with salt
  31. ret = wc_PBKDF1(key, pass, sizeof(pass), salt, sizeof(salt), 1000,
  32. sizeof(key), WC_MD5);
  33. if ( ret != 0 ) {
  34. // error deriving key from password
  35. }
  36. \endcode
  37. \sa wc_PBKDF2
  38. \sa wc_PKCS12_PBKDF
  39. */
  40. int wc_PBKDF1(byte* output, const byte* passwd, int pLen,
  41. const byte* salt, int sLen, int iterations, int kLen,
  42. int typeH);
  43. /*!
  44. \ingroup Password
  45. \brief This function implements the Password Based Key Derivation
  46. Function 2 (PBKDF2), converting an input password with a concatenated
  47. salt into a more secure key, which it stores in output. It allows the user
  48. to select any of the supported HMAC hash functions, including: WC_MD5,
  49. WC_SHA, WC_SHA256, WC_SHA384, WC_SHA512, WC_SHA3_224, WC_SHA3_256,
  50. WC_SHA3_384 or WC_SHA3_512
  51. \return 0 Returned on successfully deriving a key from the input password
  52. \return BAD_FUNC_ARG Returned if there is an invalid hash type given or
  53. iterations is less than 1
  54. \return MEMORY_E Returned if there is an allocating memory for
  55. the HMAC object
  56. \param output pointer to the buffer in which to store the generated key.
  57. Should be kLen long
  58. \param passwd pointer to the buffer containing the password to use for
  59. the key derivation
  60. \param pLen length of the password to use for key derivation
  61. \param salt pointer to the buffer containing the salt to use for
  62. key derivation
  63. \param sLen length of the salt
  64. \param iterations number of times to process the hash
  65. \param kLen desired length of the derived key
  66. \param hashType the hashing algorithm to use. Valid choices are: WC_MD5,
  67. WC_SHA, WC_SHA256, WC_SHA384, WC_SHA512, WC_SHA3_224, WC_SHA3_256,
  68. WC_SHA3_384 or WC_SHA3_512
  69. _Example_
  70. \code
  71. int ret;
  72. byte key[64];
  73. byte pass[] = { }; // initialize with password
  74. byte salt[] = { }; // initialize with salt
  75. ret = wc_PBKDF2(key, pass, sizeof(pass), salt, sizeof(salt), 2048, sizeof(key),
  76. WC_SHA512);
  77. if ( ret != 0 ) {
  78. // error deriving key from password
  79. }
  80. \endcode
  81. \sa wc_PBKDF1
  82. \sa wc_PKCS12_PBKDF
  83. */
  84. int wc_PBKDF2(byte* output, const byte* passwd, int pLen,
  85. const byte* salt, int sLen, int iterations, int kLen,
  86. int typeH);
  87. /*!
  88. \ingroup Password
  89. \brief This function implements the Password Based Key Derivation Function
  90. (PBKDF) described in RFC 7292 Appendix B. This function converts an input
  91. password with a concatenated salt into a more secure key, which it stores
  92. in output. It allows the user to select any of the supported HMAC hash
  93. functions, including: WC_MD5, WC_SHA, WC_SHA256, WC_SHA384, WC_SHA512,
  94. WC_SHA3_224, WC_SHA3_256, WC_SHA3_384 or WC_SHA3_512
  95. \return 0 Returned on successfully deriving a key from the input password
  96. \return BAD_FUNC_ARG Returned if there is an invalid hash type given,
  97. iterations is less than 1, or the key length (kLen) requested is greater
  98. than the hash length of the provided hash
  99. \return MEMORY_E Returned if there is an allocating memory
  100. \return MP_INIT_E may be returned if there is an error during key generation
  101. \return MP_READ_E may be returned if there is an error during key generation
  102. \return MP_CMP_E may be returned if there is an error during key generation
  103. \return MP_INVMOD_E may be returned if there is an error during
  104. key generation
  105. \return MP_EXPTMOD_E may be returned if there is an error during
  106. key generation
  107. \return MP_MOD_E may be returned if there is an error during key generation
  108. \return MP_MUL_E may be returned if there is an error during key generation
  109. \return MP_ADD_E may be returned if there is an error during key generation
  110. \return MP_MULMOD_E may be returned if there is an error during
  111. key generation
  112. \return MP_TO_E may be returned if there is an error during key generation
  113. \return MP_MEM may be returned if there is an error during key generation
  114. \param output pointer to the buffer in which to store the generated key.
  115. Should be kLen long
  116. \param passwd pointer to the buffer containing the password to use for
  117. the key derivation
  118. \param pLen length of the password to use for key derivation
  119. \param salt pointer to the buffer containing the salt to use
  120. for key derivation
  121. \param sLen length of the salt
  122. \param iterations number of times to process the hash
  123. \param kLen desired length of the derived key
  124. \param hashType the hashing algorithm to use. Valid choices are: WC_MD5,
  125. WC_SHA, WC_SHA256, WC_SHA384, WC_SHA512, WC_SHA3_224, WC_SHA3_256,
  126. WC_SHA3_384 or WC_SHA3_512
  127. \param id this is a byte identifier indicating the purpose of key
  128. generation. It is used to diversify the key output, and should be
  129. assigned as follows: ID=1: pseudorandom bits are to be used as key
  130. material for performing encryption or decryption. ID=2: pseudorandom
  131. bits are to be used an IV (Initial Value) for encryption or decryption.
  132. ID=3: pseudorandom bits are to be used as an integrity key for MACing.
  133. _Example_
  134. \code
  135. int ret;
  136. byte key[64];
  137. byte pass[] = { }; // initialize with password
  138. byte salt[] = { }; // initialize with salt
  139. ret = wc_PKCS512_PBKDF(key, pass, sizeof(pass), salt, sizeof(salt), 2048,
  140. sizeof(key), WC_SHA512, 1);
  141. if ( ret != 0 ) {
  142. // error deriving key from password
  143. }
  144. \endcode
  145. \sa wc_PBKDF1
  146. \sa wc_PBKDF2
  147. */
  148. int wc_PKCS12_PBKDF(byte* output, const byte* passwd, int pLen,
  149. const byte* salt, int sLen, int iterations,
  150. int kLen, int typeH, int purpose);