crys_ecpki_build.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_ECPKI_BUILD_H
  35. #define CRYS_ECPKI_BUILD_H
  36. /*!
  37. @defgroup cryptocell_ecpki CryptoCell ECC APIs
  38. @{
  39. @ingroup cryptocell_api
  40. @brief This group is the cryptocell ECC root group
  41. @}
  42. @file
  43. @brief This module defines functions for building key structures used in Elliptic Curves Cryptography (ECC).
  44. @defgroup crys_ecpki_build CryptoCell key build for ECC APIs
  45. @{
  46. @ingroup cryptocell_ecpki
  47. */
  48. #include "crys_error.h"
  49. #include "crys_ecpki_types.h"
  50. #ifdef __cplusplus
  51. extern "C"
  52. {
  53. #endif
  54. /**********************************************************************************
  55. * CRYS_ECPKI_BuildPrivKey function *
  56. **********************************************************************************/
  57. /*!
  58. @brief Builds (imports) the user private key structure from an existing private key so
  59. that this structure can be used by other EC primitives.
  60. This function should be called before using of the private key. Input
  61. domain structure must be initialized by EC parameters and auxiliary
  62. values, using CRYS_ECPKI_GetDomain or CRYS_ECPKI_SetDomain functions.
  63. @return CRYS_OK on success.
  64. @return A non-zero value on failure as defined crys_ecpki_error.h.
  65. */
  66. CIMPORT_C CRYSError_t CRYS_ECPKI_BuildPrivKey(
  67. const CRYS_ECPKI_Domain_t *pDomain, /*!< [in] The EC domain (curve). */
  68. const uint8_t *pPrivKeyIn, /*!< [in] Pointer to private key data. */
  69. uint32_t PrivKeySizeInBytes, /*!< [in] Size of private key data (in bytes). */
  70. CRYS_ECPKI_UserPrivKey_t *pUserPrivKey /*!< [out] Pointer to the private key structure.
  71. This structure is used as input to the ECPKI cryptographic primitives. */
  72. );
  73. /**********************************************************************************
  74. * CRYS_ECPKI_ExportPrivKey function *
  75. **********************************************************************************/
  76. /*!
  77. @brief Converts an existing private key from internal representation to Big-Endian export representation.
  78. \note Size of output is equal to ModSizeInBytes.
  79. @return CRYS_OK on success.
  80. @return A non-zero value on failure as defined crys_ecpki_error.h.
  81. */
  82. CIMPORT_C CRYSError_t CRYS_ECPKI_ExportPrivKey(
  83. CRYS_ECPKI_UserPrivKey_t *pUserPrivKey, /*!< [in] */
  84. uint8_t *pExportPrivKey, /*!< [out] */
  85. uint32_t *pPrivKeySizeBytes /*!< [in/out] */
  86. );
  87. /**********************************************************************************
  88. * _DX_ECPKI_BuildPublKey function *
  89. **********************************************************************************/
  90. /*!
  91. @brief Builds a user public key structure from an imported public key,
  92. so it can be used by other EC primitives.
  93. When operating the EC cryptographic algorithms with imported EC public
  94. key, this function should be called before using of the public key.
  95. \note The Incoming public key PublKeyIn structure is big endian bytes array, containing
  96. concatenation of PC||X||Y, where:
  97. <ul id="noteb"><li> PC - point control single byte, defining the type of point: 0x4 - uncompressed,
  98. 06,07 - hybrid, 2,3 - compressed. </li>
  99. <li>X,Y - EC point coordinates of public key (y is omitted in compressed form),
  100. size of X and Y must be equal to size of EC modulus. </li></ul>
  101. The user may call this function by appropriate macros, according to necessary validation level [SEC1. ECC standard: 3.2, ANS X9.62]:
  102. <ul><li>Checking the input pointers and sizes only - ::CRYS_ECPKI_BuildPublKey.</li>
  103. <li>Partially checking of public key - ::CRYS_ECPKI_BuildPublKeyPartlyCheck. </li>
  104. <li>Full checking of public key - ::CRYS_ECPKI_BuildPublKeyFullCheck. </li></ul>
  105. \note Full check mode takes long time and should be used when it is actually needed.
  106. @return CRYS_OK on success.
  107. @return A non-zero value on failure as defined crys_ecpki_error.h.
  108. */
  109. /*
  110. The function performs the following operations:
  111. - Checks validity of incoming variables and pointers;
  112. - Converts incoming key data from big endian into little endian;
  113. - If public key is given in compressed form (i.e. byte[0] = 2 or 3 and
  114. coordinate Y is omitted), then the function uncompress it;
  115. - Performs checking of input key according to CheckMode parameter.
  116. - Initializes variables and structures.
  117. */
  118. CIMPORT_C CRYSError_t _DX_ECPKI_BuildPublKey(
  119. const CRYS_ECPKI_Domain_t *pDomain, /*!< [in] The EC domain (curve). */
  120. uint8_t *PublKeyIn_ptr, /*!< [in] Pointer to the input public key data, in compressed or
  121. uncompressed or hybrid form:
  122. [PC||X||Y] Big-Endian representation, structured according to
  123. [IEEE1363], where:
  124. <ul><li>X and Y are the public key's EC point coordinates.
  125. In compressed form, Y is omitted.</li>
  126. <li> The sizes of X and Y are equal to the size of the EC modulus.</li>
  127. <li> PC is a one-byte point control that defines the type of point
  128. compression. </li></ul>*/
  129. uint32_t PublKeySizeInBytes, /*!< [in] The size of public key data (in bytes). */
  130. EC_PublKeyCheckMode_t CheckMode, /*!< [in] The required level of public key verification
  131. (higher verification level means longer verification time):
  132. <ul><li> 0 = preliminary validation. </li>
  133. <li> 1 = partial validation. </li>
  134. <li> 2 = full validation. </li></ul>*/
  135. CRYS_ECPKI_UserPublKey_t *pUserPublKey, /*!< [out] Pointer to the output public key structure.
  136. This structure is used as input to the ECPKI cryptographic primitives. */
  137. CRYS_ECPKI_BUILD_TempData_t *pTempBuff /*!< [in] Pointer for a temporary buffer required for the build function. */
  138. );
  139. /**********************************************************************************
  140. * CRYS_ECPKI_BuildPublKey macro *
  141. **********************************************************************************/
  142. /*!
  143. @brief This macro calls _DX_ECPKI_BuildPublKey function for building the public key
  144. while checking input pointers and sizes. For a description of the parameters see ::_DX_ECPKI_BuildPublKey.
  145. */
  146. #define CRYS_ECPKI_BuildPublKey(pDomain, PublKeyIn_ptr, PublKeySizeInBytes, pUserPublKey) \
  147. _DX_ECPKI_BuildPublKey((pDomain), (PublKeyIn_ptr), (PublKeySizeInBytes), CheckPointersAndSizesOnly, (pUserPublKey), NULL)
  148. /**********************************************************************************
  149. * CRYS_ECPKI_BuildPublKeyPartlyCheck macro *
  150. **********************************************************************************/
  151. /*!
  152. @brief This macro calls _DX_ECPKI_BuildPublKey function for building the public key with partial validation of the key [SEC1] - 3.2.3.
  153. For a description of the parameters see ::_DX_ECPKI_BuildPublKey.
  154. */
  155. #define CRYS_ECPKI_BuildPublKeyPartlyCheck(pDomain, PublKeyIn_ptr, PublKeySizeInBytes, pUserPublKey, pTempBuff) \
  156. _DX_ECPKI_BuildPublKey((pDomain), (PublKeyIn_ptr), (PublKeySizeInBytes), ECpublKeyPartlyCheck, (pUserPublKey), (pTempBuff))
  157. /**********************************************************************************
  158. * CRYS_ECPKI_BuildPublKeyFullCheck macro *
  159. **********************************************************************************/
  160. /*!
  161. @brief This macro calls _DX_ECPKI_BuildPublKey function for building the public key with full validation of the key [SEC1] - 3.2.2.
  162. For a description of the parameters and return values see _DX_ECPKI_BuildPublKey.
  163. */
  164. #define CRYS_ECPKI_BuildPublKeyFullCheck(pDomain, PublKeyIn_ptr, PublKeySizeInBytes, pUserPublKey, pTempBuff) \
  165. _DX_ECPKI_BuildPublKey((pDomain), (PublKeyIn_ptr), (PublKeySizeInBytes), (ECpublKeyFullCheck), (pUserPublKey), (pTempBuff))
  166. /***********************************************************************************
  167. * CRYS_ECPKI_ExportPublKey function *
  168. ***********************************************************************************/
  169. /*!
  170. @brief Converts an existing public key from internal representation to Big-Endian export representation.
  171. The function converts the X,Y coordinates of public key EC point to big endianness,
  172. and sets the public key as follows:
  173. <ul><li>In case "Uncompressed" point: PubKey = PC||X||Y, PC = 0x4 - single byte;</li>
  174. <li>In case of "Hybrid" key PC = 0x6.</li>
  175. <li>In case of "Compressed" key PC = 0x2.</li></ul>
  176. \note Size of output X and Y coordinates is equal to ModSizeInBytes.
  177. @return CRYS_OK on success.
  178. @return A non-zero value on failure as defined crys_ecpki_error.h.
  179. */
  180. CIMPORT_C CRYSError_t CRYS_ECPKI_ExportPublKey(
  181. CRYS_ECPKI_UserPublKey_t *pUserPublKey, /*!< [in] Pointer to the input public key structure (in Little-Endian form). */
  182. CRYS_ECPKI_PointCompression_t compression, /*!< [in] Compression mode: Compressed, Uncompressed or Hybrid. */
  183. uint8_t *pExternPublKey, /*!< [out] Pointer to the exported public key array, in compressed or uncompressed
  184. or hybrid form:
  185. [PC||X||Y] Big-Endian representation, structured according to [IEEE1363].
  186. In compressed form, Y is omitted. */
  187. uint32_t *pPublKeySizeBytes /*!< [in/out] Pointer used for the input of the user public key buffer size
  188. (in bytes), and the output of the size of the converted public key in bytes. */
  189. );
  190. #ifdef __cplusplus
  191. }
  192. #endif
  193. /**
  194. @}
  195. */
  196. #endif