crys_kdf.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**************************************************************************************
  2. * Copyright (c) 2016-2017, ARM Limited or its affiliates. All rights reserved *
  3. * *
  4. * This file and the related binary are licensed under the following license: *
  5. * *
  6. * ARM Object Code and Header Files License, v1.0 Redistribution. *
  7. * *
  8. * Redistribution and use of object code, header files, and documentation, without *
  9. * modification, are permitted provided that the following conditions are met: *
  10. * *
  11. * 1) Redistributions must reproduce the above copyright notice and the *
  12. * following disclaimer in the documentation and/or other materials *
  13. * provided with the distribution. *
  14. * *
  15. * 2) Unless to the extent explicitly permitted by law, no reverse *
  16. * engineering, decompilation, or disassembly of is permitted. *
  17. * *
  18. * 3) Redistribution and use is permitted solely for the purpose of *
  19. * developing or executing applications that are targeted for use *
  20. * on an ARM-based product. *
  21. * *
  22. * DISCLAIMER. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND *
  23. * CONTRIBUTORS "AS IS." ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT *
  24. * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, NON-INFRINGEMENT, *
  25. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
  26. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED *
  28. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
  29. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
  30. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
  33. **************************************************************************************/
  34. #ifndef CRYS_KDF_H
  35. #define CRYS_KDF_H
  36. #include "crys_hash.h"
  37. #ifdef __cplusplus
  38. extern "C"
  39. {
  40. #endif
  41. /*!
  42. @file
  43. @brief This module defines the API that supports Key derivation function in modes
  44. as defined in PKCS#3, ANSI X9.42-2001, and ANSI X9.63-1999.
  45. @defgroup crys_kdf CryptoCell Key Derivation APIs
  46. @{
  47. @ingroup cryptocell_api
  48. */
  49. #include "crys_hash.h"
  50. /************************ Defines ******************************/
  51. /*! Shared secret value max size in bytes */
  52. #define CRYS_KDF_MAX_SIZE_OF_SHARED_SECRET_VALUE 1024
  53. /* Count and max. sizeof OtherInfo entries (pointers to data buffers) */
  54. /*! Number of other info entries. */
  55. #define CRYS_KDF_COUNT_OF_OTHER_INFO_ENTRIES 5
  56. /*! Maximal size of other info entry. */
  57. #define CRYS_KDF_MAX_SIZE_OF_OTHER_INFO_ENTRY 64 /*!< Size is in bytes*/
  58. /*! Maximal size of keying data in bytes. */
  59. #define CRYS_KDF_MAX_SIZE_OF_KEYING_DATA 2048
  60. /************************ Enums ********************************/
  61. /*! HASH operation modes */
  62. typedef enum
  63. {
  64. /*! SHA1 mode.*/
  65. CRYS_KDF_HASH_SHA1_mode = 0,
  66. /*! SHA224 mode.*/
  67. CRYS_KDF_HASH_SHA224_mode = 1,
  68. /*! SHA256 mode.*/
  69. CRYS_KDF_HASH_SHA256_mode = 2,
  70. /*! SHA384 mode.*/
  71. CRYS_KDF_HASH_SHA384_mode = 3,
  72. /*! SHA512 mode.*/
  73. CRYS_KDF_HASH_SHA512_mode = 4,
  74. /*! Maximal number of HASH modes. */
  75. CRYS_KDF_HASH_NumOfModes,
  76. /*! Reserved.*/
  77. CRYS_KDF_HASH_OpModeLast = 0x7FFFFFFF,
  78. }CRYS_KDF_HASH_OpMode_t;
  79. /*! Key derivation modes. */
  80. typedef enum
  81. {
  82. /*! ASN1 key derivation mode.*/
  83. CRYS_KDF_ASN1_DerivMode = 0,
  84. /*! Concatination key derivation mode.*/
  85. CRYS_KDF_ConcatDerivMode = 1,
  86. /*! X963 key derivation mode.*/
  87. CRYS_KDF_X963_DerivMode = CRYS_KDF_ConcatDerivMode,
  88. /*! ISO 18033 KDF1 key derivation mode.*/
  89. CRYS_KDF_ISO18033_KDF1_DerivMode = 3,
  90. /*! ISO 18033 KDF2 key derivation mode.*/
  91. CRYS_KDF_ISO18033_KDF2_DerivMode = 4,
  92. /*! Maximal number of key derivation modes. */
  93. CRYS_KDF_DerivFunc_NumOfModes = 5,
  94. /*! Reserved.*/
  95. CRYS_KDF_DerivFuncModeLast= 0x7FFFFFFF,
  96. }CRYS_KDF_DerivFuncMode_t;
  97. /************************ Typedefs ****************************/
  98. /*! Structure, containing the optional data (other info) for KDF,
  99. if any data is not needed, then the pointer value and
  100. the size must be set to NULL */
  101. typedef struct
  102. {
  103. /*! A unique object identifier (OID), indicating algorithm(s)
  104. for which the keying data is used. */
  105. uint8_t AlgorithmID[CRYS_KDF_MAX_SIZE_OF_OTHER_INFO_ENTRY];
  106. uint32_t SizeOfAlgorithmID; /*!< Size of algorithm ID.*/
  107. /*! Public information contributed by the initiator. */
  108. uint8_t PartyUInfo[CRYS_KDF_MAX_SIZE_OF_OTHER_INFO_ENTRY];
  109. uint32_t SizeOfPartyUInfo; /*!< Size of the Public information contributed by the initiator. */
  110. /*! Public information contributed by the responder. */
  111. uint8_t PartyVInfo[CRYS_KDF_MAX_SIZE_OF_OTHER_INFO_ENTRY];
  112. uint32_t SizeOfPartyVInfo; /*!< Size of the responder's public information. */
  113. /*! Mutually-known private information, e.g. shared information
  114. communicated throgh a separate channel. */
  115. uint8_t SuppPrivInfo[CRYS_KDF_MAX_SIZE_OF_OTHER_INFO_ENTRY];
  116. uint32_t SizeOfSuppPrivInfo; /*!< Size of the private information. */
  117. /*! Mutually-known public information, */
  118. uint8_t SuppPubInfo[CRYS_KDF_MAX_SIZE_OF_OTHER_INFO_ENTRY];
  119. uint32_t SizeOfSuppPubInfo; /*!< Size of the public information. */
  120. }CRYS_KDF_OtherInfo_t;
  121. /************************ Structs ******************************/
  122. /************************ Public Variables **********************/
  123. /************************ Public Functions **********************/
  124. /****************************************************************/
  125. /*********************************************************************************************************/
  126. /*!
  127. @brief CRYS_KDF_KeyDerivFunc performs key derivation according to one of the modes defined in standards:
  128. ANS X9.42-2001, ANS X9.63, ISO/IEC 18033-2.
  129. The present implementation of the function allows the following operation modes:
  130. <ul><li> CRYS_KDF_ASN1_DerivMode - mode based on ASN.1 DER encoding; </li>
  131. <li> CRYS_KDF_ConcatDerivMode - mode based on concatenation;</li>
  132. <li> CRYS_KDF_X963_DerivMode = CRYS_KDF_ConcatDerivMode;</li>
  133. <li> CRYS_KDF_ISO18033_KDF1_DerivMode - specific mode according to ECIES-KEM algorithm (ISO/IEC 18033-2).</li></ul>
  134. The purpose of this function is to derive a keying data from the shared secret value and some
  135. other optional shared information (SharedInfo).
  136. \note
  137. <ul id="noteb"><li> The length in Bytes of the hash result buffer is denoted by "hashlen".</li>
  138. <li> All buffers arguments are represented in Big-Endian format.</li>
  139. @return CRYS_OK on success.
  140. @return A non-zero value on failure as defined crys_kdf_error.h or crys_hash_error.h.
  141. */
  142. CIMPORT_C CRYSError_t CRYS_KDF_KeyDerivFunc(
  143. uint8_t *ZZSecret_ptr, /*!< [in] A pointer to shared secret value octet string. */
  144. uint32_t ZZSecretSize, /*!< [in] The size of the shared secret value in bytes.
  145. The maximal size is defined as: CRYS_KDF_MAX_SIZE_OF_SHARED_SECRET_VALUE. */
  146. CRYS_KDF_OtherInfo_t *OtherInfo_ptr, /*!< [in] The pointer to structure, containing the data, shared by two entities of
  147. agreement and the data sizes. This argument may be optional in several modes
  148. (if it is not needed - set NULL).
  149. On two ISO/IEC 18033-2 modes - set NULL.
  150. On KDF ASN1 mode the OtherInfo and its AlgorithmID entry are mandatory. */
  151. CRYS_KDF_HASH_OpMode_t KDFhashMode, /*!< [in] The KDF identifier of hash function to be used. The hash function output
  152. must be at least 160 bits. */
  153. CRYS_KDF_DerivFuncMode_t derivation_mode, /*!< [in] Specifies one of above described derivation modes. */
  154. uint8_t *KeyingData_ptr, /*!< [out] A pointer to the buffer for derived keying data. */
  155. uint32_t KeyingDataSizeBytes /*!< [in] The size in bytes of the keying data to be derived.
  156. The maximal size is defined as: CRYS_KDF_MAX_SIZE_OF_KEYING_DATA. */
  157. );
  158. /*********************************************************************************************************/
  159. /*!
  160. CRYS_KDF_ASN1_KeyDerivFunc is A MACRO that performs key derivation according to ASN1 DER encoding method defined
  161. in standard ANS X9.42-2001, 7.2.1. For a description of the parameters see ::CRYS_KDF_KeyDerivFunc.
  162. */
  163. #define CRYS_KDF_ASN1_KeyDerivFunc(ZZSecret_ptr,ZZSecretSize,OtherInfo_ptr,KDFhashMode,KeyingData_ptr,KeyLenInBytes)\
  164. CRYS_KDF_KeyDerivFunc((ZZSecret_ptr),(ZZSecretSize),(OtherInfo_ptr),(KDFhashMode),CRYS_KDF_ASN1_DerivMode,(KeyingData_ptr),(KeyLenInBytes))
  165. /*********************************************************************************************************/
  166. /*!
  167. CRYS_KDF_ConcatKeyDerivFunc is a MACRO that performs key derivation according to concatenation mode defined
  168. in standard ANS X9.42-2001, 7.2.2. For a description of the parameters see
  169. ::CRYS_KDF_KeyDerivFunc.
  170. */
  171. #define CRYS_KDF_ConcatKeyDerivFunc(ZZSecret_ptr,ZZSecretSize,OtherInfo_ptr,KDFhashMode,KeyingData_ptr,KeyLenInBytes)\
  172. CRYS_KDF_KeyDerivFunc((ZZSecret_ptr),(ZZSecretSize),(OtherInfo_ptr),(KDFhashMode),CRYS_KDF_ConcatDerivMode,(KeyingData_ptr),(KeyLenInBytes))
  173. #ifdef __cplusplus
  174. }
  175. #endif
  176. /**
  177. @}
  178. */
  179. #endif