crys_ec_mont_api.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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_ECMONT_API_H
  35. #define CRYS_ECMONT_API_H
  36. #include "ssi_pal_types.h"
  37. #include "crys_rnd.h"
  38. #include "crys_pka_defs_hw.h"
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. /*!
  43. @file
  44. @brief This file contains the CRYS APIs used for EC MONT (Montgomery Curve25519) algorithms.
  45. @defgroup crys_ec_mont CryptoCell EC Montgomery APIs
  46. @{
  47. @ingroup cryptocell_ec
  48. \note Implemented algorithms according to Montgomery elliptic curves cryptography,
  49. developed by Daniel J.Bernstein etc.
  50. */
  51. /*! EC Montgomery curve25519 modulus size in bits, words and bytes */
  52. /*! EC Montgomery modulus size in bits. */
  53. #define CRYS_ECMONT_MOD_SIZE_IN_BITS 255U
  54. /*! EC Montgomery modulus size in words. */
  55. #define CRYS_ECMONT_MOD_SIZE_IN_32BIT_WORDS ((CRYS_ECMONT_MOD_SIZE_IN_BITS + SASI_BITS_IN_32BIT_WORD - 1) / SASI_BITS_IN_32BIT_WORD)
  56. /*! EC Montgomery modulus size in bytes. */
  57. #define CRYS_ECMONT_MOD_SIZE_IN_BYTES ((CRYS_ECMONT_MOD_SIZE_IN_BITS + SASI_BITS_IN_32BIT_WORD - 1) / sizeof(uint32_t))
  58. /*! Constant sizes of special EC_MONT buffers and arrays */
  59. /*! EC Montgomery scalar size in bytes. */
  60. #define CRYS_ECMONT_SCALARBYTES (CRYS_ECMONT_MOD_SIZE_IN_32BIT_WORDS * SASI_32BIT_WORD_SIZE)
  61. /*! EC Montgomery scalar multiplication size in bytes. */
  62. #define CRYS_ECMONT_SCALARMULTBYTES (CRYS_ECMONT_MOD_SIZE_IN_32BIT_WORDS * SASI_32BIT_WORD_SIZE)
  63. /*! EC Montgomery scalar seed size in bytes. */
  64. #define CRYS_ECMONT_SEEDBYTES (CRYS_ECMONT_MOD_SIZE_IN_32BIT_WORDS * SASI_32BIT_WORD_SIZE)
  65. /*! EC Montgomery domains ID-s enumerator. */
  66. typedef enum {
  67. CRYS_ECMONT_DOMAIN_CURVE_25519, /*!< EC Curve25519. */
  68. /*! EC Montgomery last domain. */
  69. CRYS_ECMONT_DOMAIN_OFF_MODE,
  70. /*! Reserved. */
  71. CRYS_ECMONT_DOMAIN_LAST = 0x7FFFFFFF
  72. }CRYS_ECMONT_DomainId_t;
  73. /*! EC_MONT scalar mult temp buffer type definition */
  74. typedef struct {
  75. /*! Internal temporary buffer. */
  76. uint32_t ecMontScalarMultTempBuff[CRYS_EC_MONT_TEMP_BUFF_SIZE_IN_32BIT_WORDS]; // set needed
  77. } CRYS_ECMONT_ScalrMultTempBuff_t;
  78. /*! EC_MONT temp buffer type definition */
  79. typedef struct {
  80. /* Don't change sequence order of the buffers */
  81. /*! Internal temporary buffer. */
  82. uint32_t ecMontScalar[CRYS_EC_MONT_EDW_MODULUS_MAX_SIZE_IN_WORDS];
  83. /*! Internal temporary buffer. */
  84. uint32_t ecMontResPoint[CRYS_EC_MONT_EDW_MODULUS_MAX_SIZE_IN_WORDS];
  85. /*! Internal temporary buffer. */
  86. uint32_t ecMontInPoint[CRYS_EC_MONT_EDW_MODULUS_MAX_SIZE_IN_WORDS];
  87. /*! Internal temporary buffer. */
  88. CRYS_ECMONT_ScalrMultTempBuff_t ecMontScalrMultTempBuff; // if needed
  89. } CRYS_ECMONT_TempBuff_t;
  90. /*********************************************************************/
  91. /*!
  92. @brief The function performs EC Montgomery (Curve25519) scalar multiplication:
  93. resPoint = scalar * point.
  94. @return CRYS_OK on success,
  95. @return A non-zero value on failure as defined crys_ec_mont_edw_error.h.
  96. */
  97. CIMPORT_C CRYSError_t CRYS_ECMONT_Scalarmult(
  98. uint8_t *pResPoint, /*!< [out] Pointer to the public (secret) key. */
  99. size_t *pResPointSize, /*!< [in/out] Pointer to the size of the public key in bytes.
  100. In - the size of the buffer. must be at least EC modulus
  101. size (for curve25519 - 32 bytes).
  102. Out - the actual size. */
  103. const uint8_t *pScalar, /*!< [in] Pointer to the secret (private) key. */
  104. size_t scalarSize, /*!< [in] Pointer to the size of the secret key in bytes;
  105. must be equal to EC order size (for curve25519 - 32 bytes). */
  106. const uint8_t *pInPoint, /*!< [in] Pointer to the input point (compressed). */
  107. size_t inPointSize, /*!< [in] Size of the point - must be equal to CRYS_ECMONT_MOD_SIZE_IN_BYTES. */
  108. CRYS_ECMONT_TempBuff_t *pEcMontTempBuff /*!< [in] Pointer to temp buffer, for internal use. */);
  109. /*********************************************************************/
  110. /*!
  111. @brief The function performs EC Montgomery (Curve25519) scalar multiplication of base point:
  112. res = scalar * base_point.
  113. Note: all byte arrays have LE order of bytes, i.e. LS byte is on left most place.
  114. @return CRYS_OK on success,
  115. @return A non-zero value on failure as defined crys_ec_mont_edw_error.h.
  116. */
  117. CIMPORT_C CRYSError_t CRYS_ECMONT_ScalarmultBase(
  118. uint8_t *pResPoint, /*!< [out] Pointer to the public (secret) key. */
  119. size_t *pResPointSize, /*!< [in/out] Pointer to the size of the public key in bytes.
  120. In - the size of buffer must be at least EC modulus size
  121. (for curve25519 - 32 bytes);
  122. Out - the actual size. */
  123. const uint8_t *pScalar, /*!< [in] Pointer to the secret (private) key. */
  124. size_t scalarSize, /*!< [in] Pointer to the size of the scalar in bytes -
  125. must be equal to EC order size (for curve25519 - 32 bytes). */
  126. CRYS_ECMONT_TempBuff_t *pEcMontTempBuff /*!< [in] Pointer to temp buffer, for internal use. */);
  127. /*******************************************************************/
  128. /*!
  129. @brief The function randomly generates private and public keys for Montgomery
  130. Curve25519.
  131. \note <ul id="noteb"><li> All byte arrays are in LE order of bytes, i.e. LS byte is on the left most place.</li>
  132. <li> LS and MS bits of the Secret key are set according to EC Montgomery scalar mult. algorithm:
  133. secrKey[0] &= 248; secrKey[31] &= 127; secrKey[31] |= 64;</li></ul>
  134. @return CRYS_OK on success,
  135. @return A non-zero value on failure as defined crys_ec_mont_edw_error.h or crys_rnd_error.h.
  136. */
  137. CIMPORT_C CRYSError_t CRYS_ECMONT_KeyPair (
  138. uint8_t *pPublKey, /*!< [out] Pointer to the public key. */
  139. size_t *pPublKeySize, /*!< [in/out] Pointer to the size of the public key in bytes.
  140. In - the size of the buffer must be at least EC order size
  141. (for curve25519 - 32 bytes);
  142. Out - the actual size. */
  143. uint8_t *pSecrKey, /*!< [out] Pointer to the secret key, including. */
  144. size_t *pSecrKeySize, /*!< [in/out] Pointer to the size of buffer for the secret key in bytes -
  145. must be at least EC order size (for curve25519 - 32 bytes). */
  146. void *pRndState, /*!< [in/out] Pointer to the RND state structure. */
  147. SaSiRndGenerateVectWorkFunc_t rndGenerateVectFunc, /*!< [in] Pointer to the random vector generation function. */
  148. CRYS_ECMONT_TempBuff_t *pEcMontTempBuff /*!< [in] Pointer to the temp buffer, for internal use. */);
  149. /*******************************************************************/
  150. /*!
  151. @brief The function generates private and public keys for Montgomery algorithms.
  152. The generation performed using given seed.
  153. @return CRYS_OK on success,
  154. @return A non-zero value on failure as defined crys_ec_mont_edw_error.h or crys_hash_error.h.
  155. */
  156. CIMPORT_C CRYSError_t CRYS_ECMONT_SeedKeyPair (
  157. uint8_t *pPublKey, /*!< [out] Pointer to the public (secret) key. */
  158. size_t *pPublKeySize, /*!< [in/out] Pointer to the size of the public key in bytes.
  159. In - the size of buffer must be at least EC order size
  160. (for curve25519 - 32 bytes);
  161. Out - the actual size. */
  162. uint8_t *pSecrKey, /*!< [out] Pointer to the secret (private) key. */
  163. size_t *pSecrKeySize, /*!< [in/out] Pointer to the size of the secret key in bytes
  164. In - the size of buffer must be at least EC order size
  165. (for curve25519 - 32 bytes);
  166. Out - the actual size. */
  167. const uint8_t *pSeed, /*!< [in] Pointer to the given seed - 32 bytes. */
  168. size_t seedSize, /*!< [in/] Size of the seed in bytes (must be equal to CRYS_ECMONT_SEEDBYTES). */
  169. CRYS_ECMONT_TempBuff_t *pEcMontTempBuff /*!< [in] Pointer to a temp buffer, for internal use. */);
  170. #ifdef __cplusplus
  171. }
  172. #endif
  173. /**
  174. @}
  175. */
  176. #endif