crys_dh.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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_DH_H
  35. #define _CRYS_DH_H
  36. #include "crys_rsa_types.h"
  37. #include "crys_kdf.h"
  38. #include "crys_rnd.h"
  39. #ifdef __cplusplus
  40. extern "C"
  41. {
  42. #endif
  43. /*!
  44. @file
  45. @brief This module defines the API that supports Diffie-Hellman key exchange, as defined in [PKCS3] and in [X9.42] (key lengths 1024 and 2048 bits).
  46. @defgroup crys_dh CryptoCell DH APIs
  47. @{
  48. @ingroup cryptocell_api
  49. */
  50. /************************ Defines ******************************/
  51. /*! Defintion for DH public key.*/
  52. #define CRYS_DHPubKey_t CRYSRSAPubKey_t
  53. /*! Maximal valid key size in bits.*/
  54. #define CRYS_DH_MAX_VALID_KEY_SIZE_VALUE_IN_BITS CRYS_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BITS /*!< \internal RL restrict to 2048 */
  55. /*! Minimal valid key size in bits.*/
  56. #define CRYS_DH_MIN_VALID_KEY_SIZE_VALUE_IN_BITS 1024 /*!< Size limitation according to ANSI standard */
  57. /*! Maximal modulus size in bytes.*/
  58. #define CRYS_DH_MAX_MOD_SIZE_IN_BYTES (CRYS_DH_MAX_VALID_KEY_SIZE_VALUE_IN_BITS / SASI_BITS_IN_BYTE)
  59. /*! Maximal modulus size in words.*/
  60. #define CRYS_DH_MAX_MOD_SIZE_IN_WORDS (CRYS_DH_MAX_MOD_SIZE_IN_BYTES/sizeof(uint32_t))
  61. /*! Modulus buffer size in words.*/
  62. #define CRYS_DH_MAX_MOD_BUFFER_SIZE_IN_WORDS (CRYS_DH_MAX_MOD_SIZE_IN_WORDS + 2)
  63. /*! Maximal domain generation size in bits.*/
  64. #define CRYS_DH_DOMAIN_GENERATION_MAX_SIZE_BITS CRYS_RSA_MAX_KEY_GENERATION_SIZE_BITS /*!< \internal RL restrict to 2048 */
  65. /*! Defintion for DH primitives data.*/
  66. #define CRYS_DHPrimeData_t CRYS_RSAPrimeData_t
  67. /*! Defintion for DH public key.*/
  68. #define CRYS_DHUserPubKey_t CRYS_RSAUserPubKey_t
  69. /*! Defintion for DH other info.*/
  70. #define CRYS_DH_OtherInfo_t CRYS_KDF_OtherInfo_t
  71. /*! Keying data size is in bytes*/
  72. #define CRYS_DH_MAX_SIZE_OF_KEYING_DATA CRYS_KDF_MAX_SIZE_OF_KEYING_DATA
  73. /************************ Enums ********************************/
  74. /*! DH operations mode */
  75. typedef enum
  76. {
  77. /*! PKCS3 operation mode. */
  78. CRYS_DH_PKCS3_mode = 0,
  79. /*! ANSI X942 operation mode. */
  80. CRYS_DH_ANSI_X942_mode = 1,
  81. /*! Total number of operation modes. */
  82. CRYS_DH_NumOfModes,
  83. /*! Reserved. */
  84. CRYS_DH_OpModeLast = 0x7FFFFFFF,
  85. }CRYS_DH_OpMode_t;
  86. /*! HASH operation modes */
  87. typedef enum
  88. {
  89. /*! SHA1 operation mode. */
  90. CRYS_DH_HASH_SHA1_mode = CRYS_HASH_SHA1_mode,
  91. /*! SHA224 operation mode. */
  92. CRYS_DH_HASH_SHA224_mode = CRYS_HASH_SHA224_mode,
  93. /*! SHA256 operation mode. */
  94. CRYS_DH_HASH_SHA256_mode = CRYS_HASH_SHA256_mode,
  95. /*! SHA384 operation mode. */
  96. CRYS_DH_HASH_SHA384_mode = CRYS_HASH_SHA384_mode,
  97. /*! SHA512 operation mode. */
  98. CRYS_DH_HASH_SHA512_mode = CRYS_HASH_SHA512_mode,
  99. /*! MD5 operation mode (not used in DH). */
  100. CRYS_DH_HASH_MD5_mode = CRYS_HASH_MD5_mode, /*!< \internal not used in DH */
  101. /*! Total number of HASH modes. */
  102. CRYS_DH_HASH_NumOfModes = CRYS_HASH_MD5_mode,
  103. /*! Reserved. */
  104. CRYS_DH_HASH_OperationModeLast = 0x7FFFFFFF,
  105. }CRYS_DH_HASH_OpMode_t;
  106. /*! Key derivation modes. */
  107. typedef enum
  108. {
  109. /*! ASN1 derivation mode.*/
  110. CRYS_DH_ASN1_Der_mode = CRYS_KDF_ASN1_DerivMode,
  111. /*! Concatination derivation mode.*/
  112. CRYS_DH_Concat_Der_mode = CRYS_KDF_ConcatDerivMode,
  113. /*! X963 derivation mode.*/
  114. CRYS_DH_X963_DerMode = CRYS_KDF_ConcatDerivMode,
  115. /*! Reserved. */
  116. CRYS_DH_DerivationFunc_ModeLast= 0x7FFFFFFF,
  117. }CRYS_DH_DerivationFunc_Mode;
  118. /************************ Typedefs *************************************/
  119. /*! Temporary buffer structure for internal usage.*/
  120. typedef struct
  121. {
  122. /*! Temporary primitives data */
  123. CRYS_DHPrimeData_t PrimeData;
  124. /*! Public key. */
  125. CRYS_DHPubKey_t PubKey;
  126. /*! Temporary buffer for internal usage. */
  127. uint32_t TempBuff[CRYS_DH_MAX_MOD_BUFFER_SIZE_IN_WORDS];
  128. } CRYS_DH_ExpTemp_t;
  129. /*! Temporary buffer structure for internal usage. */
  130. typedef struct
  131. {
  132. /*! Temporary primitives data */
  133. CRYS_DHPrimeData_t PrimeData;
  134. /*! User's public key. */
  135. CRYS_DHUserPubKey_t UserPubKey;
  136. /*! Temporary buffer for internal usage. */
  137. uint32_t TempBuff[CRYS_DH_MAX_MOD_BUFFER_SIZE_IN_WORDS];
  138. } CRYS_DH_Temp_t;
  139. /*! Temporary buffer structure for internal usage. */
  140. typedef struct
  141. {
  142. /*! Temporary primitives data */
  143. CRYS_DHPrimeData_t PrimeData;
  144. /*! User's public key. */
  145. CRYS_DHUserPubKey_t UserPubKey;
  146. /*! Temporary buffer for internal usage. */
  147. uint32_t TempBuff[2*CRYS_DH_MAX_MOD_BUFFER_SIZE_IN_WORDS];
  148. } CRYS_DH_HybrTemp_t;
  149. /*! Defintion of buffer used for FIPS Known Answer Tests. */
  150. typedef struct
  151. {
  152. /*! Public key. */
  153. CRYS_DHUserPubKey_t pubKey;
  154. /*! Temporary primitives data */
  155. CRYS_DHPrimeData_t primeData;
  156. /*! Buffer for the secret value.*/
  157. uint8_t secretBuff[CRYS_DH_MIN_VALID_KEY_SIZE_VALUE_IN_BITS/SASI_BITS_IN_BYTE]; // KAT tests uses 1024 bit key
  158. } CRYS_DH_FipsKat_t;
  159. /************************ Structs **************************************/
  160. /************************ Public Variables ******************************/
  161. /************************ Public Functions ******************************/
  162. /*******************************************************************************************/
  163. /*!
  164. @brief This function has two purposes:
  165. <ol><li> Randomly generate the client private key according to the choosen version [PKCS3] or [ANSI X9.42].</li>
  166. <li> Computes the client public key as follows: ClientPub = Generator^Prv mod Prime, where '^' is the symbol of exponentiation.</li></ol>
  167. This function should not be called directly. Instead, use the macros ::CRYS_DH_PKCS3_GeneratePubPrv and ::CRYS_DH_ANSI_X942_GeneratePubPrv.
  168. \note
  169. All buffer parameters should be in Big-Endian form.
  170. @return CRYS_OK on success.
  171. @return A non-zero value on failure as defined crys_dh_error.h, crys_rnd_error.h or crys_rsa_error.h.
  172. */
  173. CIMPORT_C CRYSError_t _DX_DH_GeneratePubPrv(
  174. void *rndState_ptr, /*!< [in/out] Pointer to the RND state structure. */
  175. SaSiRndGenerateVectWorkFunc_t rndGenerateVectFunc, /*!< [in] Pointer to the random vector generation function. */
  176. uint8_t *Generator_ptr, /*!< [in] Pointer to the Generator octet string. */
  177. uint16_t GeneratorSize, /*!< [in] The size of the Generator string (in bytes). */
  178. uint8_t *Prime_ptr, /*!< [in] Pointer to the Prime octet string P (used as modulus in the algorithm). */
  179. uint16_t PrimeSize, /*!< [in] The size of the Prime string in bytes. */
  180. uint16_t L, /*!< [in] Exact size in bits of the Prime to be generated (relevant only for [PKCS3]):
  181. <ul><li> If L!=0, force the private key to be [2^(L-1) ? Prv < 2^L], where '^'
  182. indicates exponentiation.</li>
  183. <li> If L = 0 then [0 < Prv < P-1].</li></ul> */
  184. uint8_t *Q_ptr, /*!< [in] Relevant only for [ANSI X9.42] - Pointer to the Q octet string in the range:
  185. 1 <= Prv <= Q-1 or 1 < Prv < Q-1. */
  186. uint16_t QSize, /*!< [in] Relevant only for [ANSI X9.42] - Size of the Q string (in bytes). */
  187. CRYS_DH_OpMode_t DH_mode, /*!< [in] An enumerator declaring whether this is [PKCS3] or [ANSI X9.42] mode. */
  188. CRYS_DHUserPubKey_t *tmpPubKey_ptr, /*!< [in] Pointer to a temporary buffer for public key structure. Used for the
  189. exponentiation function. */
  190. CRYS_DHPrimeData_t *tmpPrimeData_ptr, /*!< [in] Pointer to a structure holding internal temporary buffers. */
  191. uint8_t *ClientPrvKey_ptr, /*!< [out] Pointer to the Private key Prv. This buffer should be at least the following
  192. size (in bytes):
  193. <ul><li> If L is provided: (L+7)/8.</li>
  194. <li> If L is NULL: \p PrimeSize. </li></ul> */
  195. uint16_t *ClientPrvKeySize_ptr, /*!< [in/out] Pointer to the Private key size:
  196. <ul><li> Input - size of the given buffer.</li>
  197. <li> Output - actual size of the generated private key.</li></ul> */
  198. uint8_t *ClientPub1_ptr, /*!< [out] Pointer to the Public key. This buffer should be at least \p PrimeSize bytes. */
  199. uint16_t *ClientPubSize_ptr /*!< [in/out] Pointer to the Public key size:
  200. <ul><li> Input - size of the given buffer.</li>
  201. <li> Output - actual size of the generated public key.</li></ul> */
  202. );
  203. /* macro for calling the GeneratePubPrv function on PKCS#3 mode: Q is irrelevant */
  204. /*--------------------------------------------------------------------------------*/
  205. /*!
  206. This macro is used to generate the public and private DH keys according to [PKCS3]. For a description of the parameters see ::_DX_DH_GeneratePubPrv.
  207. */
  208. #define CRYS_DH_PKCS3_GeneratePubPrv(rndState_ptr, rndGenerateVectFunc, Generator_ptr,GeneratorSize,\
  209. Prime_ptr,PrimeSize,\
  210. L,\
  211. tmpPubKey_ptr,tmpPrimeData_ptr,\
  212. ClientPrvKey_ptr,ClientPrvKeySize_ptr,\
  213. ClientPub_ptr,ClientPubSize_ptr)\
  214. _DX_DH_GeneratePubPrv(rndState_ptr, rndGenerateVectFunc, (Generator_ptr),(GeneratorSize),\
  215. (Prime_ptr),(PrimeSize),\
  216. (L),\
  217. (uint8_t *)NULL,(uint16_t)0,\
  218. CRYS_DH_PKCS3_mode,\
  219. (tmpPubKey_ptr),(tmpPrimeData_ptr),\
  220. (ClientPrvKey_ptr),(ClientPrvKeySize_ptr),\
  221. (ClientPub_ptr),(ClientPubSize_ptr))
  222. /*!
  223. This macro is used to generate the public and private DH keys according to [ANSI X9.42]. For a description of the parameters see ::_DX_DH_GeneratePubPrv.
  224. */
  225. #define CRYS_DH_ANSI_X942_GeneratePubPrv(rndState_ptr, rndGenerateVectFunc, Generator_ptr,GeneratorSize,Prime_ptr,PrimeSize,\
  226. Q_ptr,QSize,\
  227. tmpPubKey_ptr,tmpPrimeData_ptr,\
  228. ClientPrvKey_ptr,ClientPrvKeySize_ptr,\
  229. ClientPub_ptr,ClientPubSize_ptr)\
  230. _DX_DH_GeneratePubPrv(rndState_ptr, rndGenerateVectFunc, (Generator_ptr),(GeneratorSize),\
  231. (Prime_ptr),(PrimeSize),\
  232. (uint16_t)0,\
  233. (Q_ptr),(QSize),\
  234. CRYS_DH_ANSI_X942_mode,\
  235. (tmpPubKey_ptr),(tmpPrimeData_ptr),\
  236. (ClientPrvKey_ptr),(ClientPrvKeySize_ptr),\
  237. (ClientPub_ptr),(ClientPubSize_ptr))
  238. /*******************************************************************************************/
  239. /*!
  240. @brief This function computes the shared secret key (value) accordng to [ANSI X9.42], 7.5.1:
  241. SecretKey = ServerPubKey ^ ClientPrvKey mod Prime.
  242. \note <ul id="noteb"><li> All buffer parameters should be in Big-Endian form.</li>
  243. <li>The user must obtain assurance of validity of the public key, using one of methods,
  244. described in [ANSI X9.42] paragraph 7.4.</li>
  245. <li>The actual size of the private key (in bits) must be not less than 2 and not greater than the actual
  246. size of the Prime (modulus in bits).</li></ul>
  247. @return CRYS_OK on success.
  248. @return A non-zero value on failure as defined in crys_dh_error.h or crys_rsa_error.h.
  249. */
  250. CIMPORT_C CRYSError_t CRYS_DH_GetSecretKey(
  251. uint8_t *ClientPrvKey_ptr, /*!< [in] Pointer to the Private key octet string Prv < Prime. */
  252. uint16_t ClientPrvKeySize, /*!< [in] The Private key Size (in bytes). */
  253. uint8_t *ServerPubKey_ptr, /*!< [in] Pointer to the Server public key octet string. */
  254. uint16_t ServerPubKeySize, /*!< [in] The Server Public key Size (in bytes). */
  255. uint8_t *Prime_ptr, /*!< [in] Pointer to the Prime octet string. */
  256. uint16_t PrimeSize, /*!< [in] The size of the Prime string. */
  257. CRYS_DHUserPubKey_t *tmpPubKey_ptr, /*!< [in] Pointer to the public key structure. Used for the exponentiation
  258. operation function. Need not be initialized. */
  259. CRYS_DHPrimeData_t *tmpPrimeData_ptr, /*!< [in] Pointer to a structure containing internal temp buffers. */
  260. uint8_t *SecretKey_ptr, /*!< [out] Pointer to the secret key octet string. This buffer should be at
  261. least PrimeSize bytes. */
  262. uint16_t *SecretKeySize_ptr /*!< [in/out] Pointer to the secret key Buffer Size. This buffer should be at
  263. least of PrimeSize bytes:
  264. <ul><li> Input - size of the given buffer.</li>
  265. <li> Output - actual size. </li></ul>*/
  266. );
  267. /******************************************************************************************/
  268. /*!
  269. @brief This function extracts the shared secret keying data from the shared secret value. It should be called by using
  270. macros ::CRYS_DH_X942_GetSecretDataAsn1 and ::CRYS_DH_X942_GetSecretDataConcat.
  271. \note
  272. <ul id="noteb"><li> The "other info" argument and its AlgorithmID entry are mandatory only for ASN1 key derivation, and optional for
  273. the other derivation modes. </li>
  274. <li>If used, all entries of the structure should be initialized with relevant data and size, prior to calling this function
  275. (entry size of empty fields must be set to 0).</li>
  276. <li>All buffers arguments are represented in Big-Endian form.</li></ul>
  277. @return CRYS_OK on success.
  278. @return A non-zero value on failure as defined in crys_dh_error.h, crys_rsa_error.h, crys_kdf_error.h or crys_hash_error.h.
  279. */
  280. CIMPORT_C CRYSError_t CRYS_DH_X942_GetSecretData(
  281. uint8_t *ClientPrvKey_ptr, /*!< [in] Pointer to the Private key octet string. */
  282. uint16_t ClientPrvKeySize, /*!< [in] The Private key size (in bytes). */
  283. uint8_t *ServerPubKey_ptr, /*!< [in] Pointer to the Server public key octet string. */
  284. uint16_t ServerPubKeySize, /*!< [in] The Server Public key size (in bytes). */
  285. uint8_t *Prime_ptr, /*!< [in] Pointer to the Prime octet string. */
  286. uint16_t PrimeSize, /*!< [in] The size of the Prime string. */
  287. CRYS_DH_OtherInfo_t *otherInfo_ptr, /*!< [in] Pointer to structure containing other data, shared by two entities
  288. sharing the secret keying data.
  289. The Maximal size of each data entry of "other info" is limited - see crys_kdf.h
  290. for the defined value. */
  291. CRYS_DH_HASH_OpMode_t hashMode, /*!< [in] One of the supported SHA-x HASH modes. The supported modes are according to the supported
  292. HASH modes for the product (and MD5 is not supported). */
  293. CRYS_DH_DerivationFunc_Mode DerivFunc_mode, /*!< [in] The enumerator ID of key derivation function mode. ASN1 or Concatenation
  294. modes are supported. */
  295. CRYS_DH_Temp_t *tmpBuff_ptr, /*!< [in] A pointer to the DH temp buffer structure. Not initialized. */
  296. uint8_t *SecretKeyingData_ptr, /*!< [out] Pointer to the secret key octet string. This buffer should be at least
  297. PrimeSize bytes. */
  298. uint16_t SecretKeyingDataSize /*!< [in] The required Secret Keying data size (in bytes). Must be larger than 0,
  299. and smaller than the maximal - CRYS_DH_MAX_SIZE_OF_KEYING_DATA. */
  300. );
  301. /****************************************************************/
  302. /*!
  303. This macro implements the DH [ANSI X9.42] standard. It derives a secret key using the Derivation function based on ASN.1. For a
  304. description of the parameters see ::CRYS_DH_X942_GetSecretData.*/
  305. #define CRYS_DH_X942_GetSecretDataAsn1(ClientPrvKey_ptr,ClientPrvKeySize,ServerPubKey_ptr,ServerPubKeySize,Prime_ptr,PrimeSize,otherInfo_ptr,hashMode,tmpBuff_ptr,SecretKeyingData_ptr,SecretKeyingDataSize)\
  306. CRYS_DH_X942_GetSecretData((ClientPrvKey_ptr),(ClientPrvKeySize),(ServerPubKey_ptr),(ServerPubKeySize),(Prime_ptr),(PrimeSize),(otherInfo_ptr),(hashMode),(CRYS_DH_ASN1_Der_mode),(tmpBuff_ptr),(SecretKeyingData_ptr),(SecretKeyingDataSize))
  307. /*!
  308. This macro implements the DH [ANSI X9.42] standard. It derives a secret key using the Derivation function based on concatenation of HASHed data.
  309. For a description of the parameters see ::CRYS_DH_X942_GetSecretData.*/
  310. #define CRYS_DH_X942_GetSecretDataConcat(ClientPrvKey_ptr,ClientPrvKeySize,ServerPubKey_ptr,ServerPubKeySize,Prime_ptr,PrimeSize,otherInfo_ptr,hashMode,tmpBuff_ptr,SecretKeyingData_ptr,SecretKeyingDataSize)\
  311. CRYS_DH_X942_GetSecretData((ClientPrvKey_ptr),(ClientPrvKeySize),(ServerPubKey_ptr),(ServerPubKeySize),(Prime_ptr),(PrimeSize),(otherInfo_ptr),(hashMode),(CRYS_DH_Concat_Der_mode),(tmpBuff_ptr),(SecretKeyingData_ptr),(SecretKeyingDataSize))
  312. /****************************************************************/
  313. /*!
  314. @brief The function computes shared secret data using two pairs of public and private keys:
  315. <ul><li> SecretKey1 = ServerPubKey1^ClientPrvKey1 mod Prime. </li>
  316. <li> SecretKey2 = ServerPubKey2^ClientPrvKey2 mod Prime. </li></ul>
  317. It uses the Derivation function to derive secret keying data from the two secret keys (values).
  318. This function may be called directly, or by using macros ::CRYS_DH_X942_HybridGetSecretDataAsn1 and ::CRYS_DH_X942_HybridGetSecretDataConcat
  319. described above.
  320. \note
  321. <ul id="noteb"><li> The "other info" argument and its AlgorithmID entry are mandatory only for ASN1 key derivation, and optional for the other derivation modes.</li>
  322. If used, all entries of the structure should be initialized with relevant data and size, prior to calling this function
  323. (entry size of empty fields must be set to 0).
  324. <li> All buffers arguments are represented in Big-Endian form. </li></ul>
  325. @return CRYS_OK on success.
  326. @return A non-zero value on failure as defined in crys_dh_error.h, crys_rsa_error.h or crys_hash_error.h.
  327. */
  328. CIMPORT_C CRYSError_t CRYS_DH_X942_HybridGetSecretData(
  329. uint8_t *ClientPrvKey_ptr1, /*!< [in] Pointer to the First Private key octet string number. */
  330. uint16_t ClientPrvKeySize1, /*!< [in] The First Private key Size (in bytes). */
  331. uint8_t *ClientPrvKey_ptr2, /*!< [in] Pointer to the Second Private key octet string. */
  332. uint16_t ClientPrvKeySize2, /*!< [in] The Second Private key Size (in bytes). */
  333. uint8_t *ServerPubKey_ptr1, /*!< [in] Pointer to the First Server public key octet string. */
  334. uint16_t ServerPubKeySize1, /*!< [in] The First Server Public key Size (in bytes). */
  335. uint8_t *ServerPubKey_ptr2, /*!< [in] Pointer to the Second Server public key octet string. */
  336. uint16_t ServerPubKeySize2, /*!< [in] The Second Server Public key Size (in bytes). */
  337. uint8_t *Prime_ptr, /*!< [in] Pointer to the Prime octet string. */
  338. uint16_t PrimeSize, /*!< [in] The size of the Prime string. */
  339. CRYS_DH_OtherInfo_t *otherInfo_ptr, /*!< [in] Pointer to structure containing optional other data, shared by two entities
  340. sharing the secret keying data. */
  341. CRYS_DH_HASH_OpMode_t hashMode, /*!< [in] One of the supported SHA-x HASH modes. The supported modes are according to the supported
  342. HASH modes for the product (and MD5 is not supported). */
  343. CRYS_DH_DerivationFunc_Mode DerivFunc_mode, /*!< [in] The type of function to use to derive the secret key to the key data.
  344. ASN.1 or Concatenation modes are supported. */
  345. CRYS_DH_HybrTemp_t *tmpDhHybr_ptr, /*!< [in] Pointer to a CRYS_DH_Temp_t structure that contains temp buffers for
  346. internal operations. */
  347. uint8_t *SecretKeyingData_ptr, /*!< [out] Pointer to the secret key octet string. This buffer should be at least
  348. of size PrimeSize bytes. */
  349. uint16_t SecretKeyingDataSize /*!< [in] The required Secret Keying data size (in bytes). Must be larger than 0,
  350. and smaller than CRYS_DH_MAX_SIZE_OF_KEYING_DATA. */
  351. );
  352. /****************************************************************/
  353. /*!
  354. This macro implements the DH [X9.42] standard deriving a hybrid secret key from two public-private pair of keys using the Derivation function based on ASN.1.
  355. For a description of the parameters see ::CRYS_DH_X942_HybridGetSecretData.*/
  356. #define CRYS_DH_X942_HybridGetSecretDataAsn1(ClientPrvKey_ptr1,ClientPrvKeySize1,ClientPrvKey_ptr2,ClientPrvKeySize2,ServerPubKey_ptr1,ServerPubKeySize1,ServerPubKey_ptr2,ServerPubKeySize2,Prime_ptr,PrimeSize,otherInfo_ptr,hashFunc,tmpDhHybr_ptr,SecretKeyingData_ptr,SecretKeyingDataSize)\
  357. CRYS_DH_X942_HybridGetSecretData((ClientPrvKey_ptr1),(ClientPrvKeySize1),(ClientPrvKey_ptr2),(ClientPrvKeySize2),(ServerPubKey_ptr1),(ServerPubKeySize1),(ServerPubKey_ptr2),(ServerPubKeySize2),(Prime_ptr),(PrimeSize),(otherInfo_ptr),(hashFunc),CRYS_DH_ASN1_Der_mode,(tmpDhHybr_ptr),(SecretKeyingData_ptr),(SecretKeyingDataSize))
  358. /*!
  359. This macro implements the DH [X9.42] standard, deriving a hybrid secret key from two pairs of public-private keys, using the Derivation
  360. function based on concatenation using SHA-x HASH. For a description of the parameters see ::CRYS_DH_X942_HybridGetSecretData.
  361. */
  362. #define CRYS_DH_X942_HybridGetSecretDataConcat(ClientPrvKey_ptr1,ClientPrvKeySize1,ClientPrvKey_ptr2,ClientPrvKeySize2,ServerPubKey_ptr1,ServerPubKeySize1,ServerPubKey_ptr2,ServerPubKeySize2,Prime_ptr,PrimeSize,otherInfo_ptr,hashFunc,tmpDhHybr_ptr,SecretKeyingData_ptr,SecretKeyingDataSize)\
  363. CRYS_DH_X942_HybridGetSecretData((ClientPrvKey_ptr1),(ClientPrvKeySize1),(ClientPrvKey_ptr2),(ClientPrvKeySize2),(ServerPubKey_ptr1),(ServerPubKeySize1),(ServerPubKey_ptr2),(ServerPubKeySize2),(Prime_ptr),(PrimeSize),(otherInfo_ptr),(hashFunc),CRYS_DH_Concat_Der_mode,(tmpDhHybr_ptr),(SecretKeyingData_ptr),(SecretKeyingDataSize))
  364. /******************************************************************************************/
  365. /*!
  366. @brief The function checks the obtained DH public key according to its domain parameters [ANSI X9.42-2001]
  367. \note
  368. Assuming: The DH domain parameters are valid.
  369. @return CRYS_OK on success.
  370. @return A non-zero value on failure as defined in crys_dh_error.h.
  371. */
  372. CIMPORT_C CRYSError_t CRYS_DH_CheckPubKey(
  373. uint8_t *modP_ptr, /*!< [in] The pointer to the modulus (prime) P. */
  374. uint32_t modPsizeBytes, /*!< [in] The modulus size in bytes. */
  375. uint8_t *orderQ_ptr, /*!< [in] The pointer to the prime order Q of generator. */
  376. uint32_t orderQsizeBytes, /*!< [in] The size of order of generator in bytes. */
  377. uint8_t *pubKey_ptr, /*!< [in] The pointer to the public key to be validated. */
  378. uint32_t pubKeySizeBytes, /*!< [in] The public key size in bytes. */
  379. CRYS_DH_Temp_t *tempBuff_ptr /*!< [in] The temp buffer for internal calculations. */
  380. );
  381. #ifdef __cplusplus
  382. }
  383. #endif
  384. /**
  385. @}
  386. */
  387. #endif