crys_srp.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. /*!
  35. @file
  36. @brief This file contains all of the enums and definitions that are used for the
  37. CRYS SRP APIs, as well as the APIs themselves.
  38. @defgroup crys_srp CryptoCell SRP APIs
  39. @{
  40. @ingroup cryptocell_api
  41. */
  42. #ifndef CRYS_SRP_H
  43. #define CRYS_SRP_H
  44. #include "ssi_pal_types.h"
  45. #include "crys_error.h"
  46. #include "crys_pka_defs_hw.h"
  47. #include "crys_hash.h"
  48. #include "crys_rnd.h"
  49. #ifdef __cplusplus
  50. extern "C"
  51. {
  52. #endif
  53. /*!\internal The following describes the SRP APIs usage for the Device and the Accessory :*
  54. Device (User) Accessory (Host)
  55. * -------------- -----------------
  56. 1. CRYS_SRP_HK_INIT(CRYS_SRP_USER, .......) CRYS_SRP_HK_INIT(CRYS_SRP_HOST, .....)
  57. 2. CRYS_SRP_PwdVerCreate(..)
  58. 3. CRYS_SRP_UserPubKeyCreate(..) CRYS_SRP_HostPubKeyCreate(..)
  59. 4. CRYS_SRP_UserProofCalc(..)
  60. 5. CRYS_SRP_HostProofVerifyAndCalc(..)
  61. 6. CRYS_SRP_UserProofVerify(..)
  62. 7. CRYS_SRP_Clear(..) CRYS_SRP_Clear(..)
  63. */
  64. /************************ Defines ******************************/
  65. /*! The SRP modulus sizes. */
  66. /*! 1024 bits modulus size. */
  67. #define CRYS_SRP_MODULUS_SIZE_1024_BITS 1024
  68. /*! 1536 bits modulus size. */
  69. #define CRYS_SRP_MODULUS_SIZE_1536_BITS 1536
  70. /*! 2048 bits modulus size. */
  71. #define CRYS_SRP_MODULUS_SIZE_2048_BITS 2048
  72. /*! 3072 bits modulus size. */
  73. #define CRYS_SRP_MODULUS_SIZE_3072_BITS 3072
  74. /*! Maximal modulus size in bits. */
  75. #define CRYS_SRP_MAX_MODULUS_IN_BITS CRYS_SRP_MODULUS_SIZE_3072_BITS
  76. /*! Maximal modulus size in bytes. */
  77. #define CRYS_SRP_MAX_MODULUS (CRYS_SRP_MAX_MODULUS_IN_BITS/SASI_BITS_IN_BYTE)
  78. /*! Maximal modulus size in words. */
  79. #define CRYS_SRP_MAX_MODULUS_IN_WORDS (CRYS_SRP_MAX_MODULUS_IN_BITS/SASI_BITS_IN_32BIT_WORD)
  80. /*! The SRP private number size range. */
  81. /*! Minimal private number size in bits. */
  82. #define CRYS_SRP_PRIV_NUM_MIN_SIZE_IN_BITS (256)
  83. /*! Minimal private number size in bytes. */
  84. #define CRYS_SRP_PRIV_NUM_MIN_SIZE (CRYS_SRP_PRIV_NUM_MIN_SIZE_IN_BITS/SASI_BITS_IN_BYTE)
  85. /*! Minimal private number size in words. */
  86. #define CRYS_SRP_PRIV_NUM_MIN_SIZE_IN_WORDS (CRYS_SRP_PRIV_NUM_MIN_SIZE_IN_BITS/SASI_BITS_IN_32BIT_WORD)
  87. /*! Maximal private number size in bits. */
  88. #define CRYS_SRP_PRIV_NUM_MAX_SIZE_IN_BITS (CRYS_SRP_MAX_MODULUS_IN_BITS)
  89. /*! Maximal private number size in bytes. */
  90. #define CRYS_SRP_PRIV_NUM_MAX_SIZE (CRYS_SRP_PRIV_NUM_MAX_SIZE_IN_BITS/SASI_BITS_IN_BYTE)
  91. /*! Maximal private number size in words. */
  92. #define CRYS_SRP_PRIV_NUM_MAX_SIZE_IN_WORDS (CRYS_SRP_PRIV_NUM_MAX_SIZE_IN_BITS/SASI_BITS_IN_32BIT_WORD)
  93. /*! Maximal SRP HASH digest size in words. */
  94. #define CRYS_SRP_MAX_DIGEST_IN_WORDS CRYS_HASH_RESULT_SIZE_IN_WORDS
  95. /*! Maximal SRP HASH digest size in bytes. */
  96. #define CRYS_SRP_MAX_DIGEST (CRYS_SRP_MAX_DIGEST_IN_WORDS*SASI_32BIT_WORD_SIZE)
  97. /*! Minimal salt size in bytes. */
  98. #define CRYS_SRP_MIN_SALT_SIZE (8)
  99. /*! Minimal salt size in words. */
  100. #define CRYS_SRP_MIN_SALT_SIZE_IN_WORDS (CRYS_SRP_MIN_SALT_SIZE/SASI_32BIT_WORD_SIZE)
  101. /*! Maximal salt size in bytes. */
  102. #define CRYS_SRP_MAX_SALT_SIZE (64)
  103. /*! Maximal salt size in words. */
  104. #define CRYS_SRP_MAX_SALT_SIZE_IN_WORDS (CRYS_SRP_MAX_SALT_SIZE/SASI_32BIT_WORD_SIZE)
  105. /************************ Typedefs ****************************/
  106. /*! SRP modulus buffer definition. */
  107. typedef uint8_t CRYS_SRP_Modulus_t[CRYS_SRP_MAX_MODULUS];
  108. /*! SRP digest buffer definition. */
  109. typedef uint8_t CRYS_SRP_Digest_t[CRYS_SRP_MAX_DIGEST];
  110. /*! SRP secret buffer definition. */
  111. typedef uint8_t CRYS_SRP_Secret_t[2*CRYS_SRP_MAX_DIGEST];
  112. /************************ Enums ********************************/
  113. /*!
  114. SRP supported versions
  115. */
  116. typedef enum {
  117. CRYS_SRP_VER_3 = 0, /*!< VER 3. */
  118. CRYS_SRP_VER_6 = 1, /*!< VER 6. */
  119. CRYS_SRP_VER_6A = 2, /*!< VER 6A. */
  120. CRYS_SRP_VER_HK = 3, /*!< VER 6A. */
  121. /*! Maximal number of supported versions.*/
  122. CRYS_SRP_NumOfVersions,
  123. /*! Reserved.*/
  124. CRYS_SRP_VersionLast= 0x7FFFFFFF,
  125. }CRYS_SRP_Version_t;
  126. /*!
  127. SRP entity type
  128. */
  129. typedef enum {
  130. CRYS_SRP_HOST = 1, /*!< Host entity, called also server, verifier, or Accessory in Home-Kit */
  131. CRYS_SRP_USER = 2, /*!< User entity, called also client, or Device in Home-Kit */
  132. /*! Maximal number of entities types.*/
  133. CRYS_SRP_NumOfEntityType,
  134. /*! Reserved.*/
  135. CRYS_SRP_EntityLast= 0x7FFFFFFF,
  136. }CRYS_SRP_Entity_t;
  137. /************************ Structs ******************************/
  138. /*! Group parameters for the SRP - defines the modulus and the generator used */
  139. typedef struct CRYS_SRP_GroupParam_t {
  140. /*! SRP modulus.*/
  141. CRYS_SRP_Modulus_t modulus;
  142. /*! SRP generator.*/
  143. uint8_t gen;
  144. /*! SRP modulus size in bits .*/
  145. size_t modSizeInBits;
  146. /*! SRP valid Np.*/
  147. uint32_t validNp;
  148. /*! SRP Np buffer.*/
  149. uint32_t Np[CRYS_PKA_BARRETT_MOD_TAG_BUFF_SIZE_IN_WORDS];
  150. }CRYS_SRP_GroupParam_t;
  151. /************************ context Structs ******************************/
  152. /*! The SRP context prototype */
  153. typedef struct CRYS_SRP_Context_t {
  154. /*! SRP entitiy type.*/
  155. CRYS_SRP_Entity_t srpType;
  156. /*! SRP version.*/
  157. CRYS_SRP_Version_t srpVer;
  158. /*! Group parameter including the modulus information.*/
  159. CRYS_SRP_GroupParam_t groupParam; // N, g, Np
  160. /*! Hash mode.*/
  161. CRYS_HASH_OperationMode_t hashMode;
  162. /*! Hash digest size.*/
  163. size_t hashDigestSize;
  164. /*! Pointer to RND state.*/
  165. CRYS_RND_State_t *pRndState;
  166. /*! Pointer to random vector generation function.*/
  167. SaSiRndGenerateVectWorkFunc_t rndGenerateVectFunc;
  168. /*! Modulus.*/
  169. CRYS_SRP_Modulus_t ephemPriv; // a or b
  170. /*! Modulus size.*/
  171. size_t ephemPrivSize;
  172. /*! User name digest.*/
  173. CRYS_SRP_Digest_t userNameDigest; // M
  174. /*! Cred digest.*/
  175. CRYS_SRP_Digest_t credDigest; // p
  176. /*! SRP K multiplier. */ //
  177. CRYS_SRP_Digest_t kMult; // k multiplier
  178. }CRYS_SRP_Context_t;
  179. /************************ SRP common Functions **********************/
  180. /****************************************************************************************************/
  181. /*!
  182. @brief This function initiates the SRP context.
  183. @return CRYS_OK on success.
  184. @return A non-zero value on failure as defined crys_srp_error.h or crys_hash_error.h.
  185. */
  186. CIMPORT_C CRYSError_t CRYS_SRP_Init(
  187. CRYS_SRP_Entity_t srpType, /*!< [in] SRP entity type. */
  188. CRYS_SRP_Version_t srpVer, /*!< [in] SRP version. */
  189. CRYS_SRP_Modulus_t srpModulus, /*!< [in] A pointer to the SRP modulus, BE byte buffer. */
  190. uint8_t srpGen, /*!< [in] The SRP generator param. */
  191. size_t modSizeInBits, /*!< [in] The SRP modulus size in bits:1024, 1536, 2048 & 3072 */
  192. CRYS_HASH_OperationMode_t hashMode, /*!< [in] Enumerator defining the HASH mode. */
  193. uint8_t *pUserName, /*!< [in] A Pointer to user name. */
  194. size_t userNameSize, /*!< [in] The user name buffer size > 0. */
  195. uint8_t *pPwd, /*!< [in] A Pointer to user password. */
  196. size_t pwdSize, /*!< [in] The user password buffer size > 0 if pPwd is valid. */
  197. void *pRndState, /*!< [in] A Pointer to RND context.*/
  198. SaSiRndGenerateVectWorkFunc_t rndGenerateVectFunc, /*!< [in] Pointer to random vector generation function.*/
  199. CRYS_SRP_Context_t *pCtx /*!< [out] A Pointer to the SRP host context.*/
  200. );
  201. /*! MACRO definition for a specific SRP initialization function.*/
  202. #define CRYS_SRP_HK_INIT(srpType, srpModulus, srpGen, modSizeInBits, pUserName, userNameSize, pPwd, pwdSize, pRndState, rndGenerateVectFunc, pCtx) \
  203. CRYS_SRP_Init(srpType, CRYS_SRP_VER_HK, srpModulus, srpGen, modSizeInBits, CRYS_HASH_SHA512_mode, pUserName, userNameSize, pPwd, pwdSize, pRndState, rndGenerateVectFunc, pCtx)
  204. /****************************************************************************************************/
  205. /*!
  206. @brief This function calculates pSalt & password verifier
  207. @return CRYS_OK on success.
  208. @return A non-zero value on failure as defined crys_srp_error.h, crys_rnd_error.h or crys_hash_error.h.
  209. */
  210. CIMPORT_C CRYSError_t CRYS_SRP_PwdVerCreate(
  211. size_t saltSize, /*!< [in] The size of the random salt to generate,
  212. The range is between CRYS_SRP_MIN_SALT_SIZE
  213. to CRYS_SRP_MAX_SALT_SIZE. */
  214. uint8_t *pSalt, /*!< [out] A Pointer to the pSalt number (s).*/
  215. CRYS_SRP_Modulus_t pwdVerifier, /*!< [out] A Pointer to the password verifier (v). */
  216. CRYS_SRP_Context_t *pCtx /*!< [out] A Pointer to the SRP context.*/
  217. );
  218. /****************************************************************************************************/
  219. /*!
  220. @brief Clears the SRP context.
  221. @return CRYS_OK on success.
  222. @return A non-zero value on failure as defined crys_srp_error.h.
  223. */
  224. CIMPORT_C CRYSError_t CRYS_SRP_Clear(
  225. CRYS_SRP_Context_t *pCtx /*!< [in/out] A Pointer to the SRP context.*/
  226. );
  227. /************************ SRP Host Functions **********************/
  228. /****************************************************************************************************/
  229. /*!
  230. @brief This function generates host public & private ephemeral key, known as B & b in RFC
  231. @return CRYS_OK on success.
  232. @return A non-zero value on failure as defined crys_srp_error.h or crys_rnd_error.h.
  233. */
  234. CIMPORT_C CRYSError_t CRYS_SRP_HostPubKeyCreate(
  235. size_t ephemPrivSize, /*!< [in] The size of the generated ephemeral private key (b).
  236. The range is between CRYS_SRP_PRIV_NUM_MIN_SIZE to
  237. CRYS_SRP_PRIV_NUM_MAX_SIZE */
  238. CRYS_SRP_Modulus_t pwdVerifier, /*!< [in] A Pointer to the verifier (v). */
  239. CRYS_SRP_Modulus_t hostPubKeyB, /*!< [out] A Pointer to the host ephemeral public key (B). */
  240. CRYS_SRP_Context_t *pCtx /*!< [in/out] A Pointer to the SRP context.*/
  241. );
  242. /*!
  243. @brief Verifies the user Proof and calculates the Host message proof.
  244. @return CRYS_OK on success.
  245. @return A non-zero value on failure as defined crys_srp_error.h or crys_hash_error.h.
  246. */
  247. CIMPORT_C CRYSError_t CRYS_SRP_HostProofVerifyAndCalc(
  248. size_t saltSize, /*!< [in] The size of the random salt,
  249. The range is between CRYS_SRP_MIN_SALT_SIZE
  250. to CRYS_SRP_MAX_SALT_SIZE. */
  251. uint8_t *pSalt, /*!< [in] A Pointer to the pSalt number.*/
  252. CRYS_SRP_Modulus_t pwdVerifier, /*!< [in] A Pointer to the password verifier (v). */
  253. CRYS_SRP_Modulus_t userPubKeyA, /*!< [in] A Pointer to the user ephemeral public key (A). */
  254. CRYS_SRP_Modulus_t hostPubKeyB, /*!< [in] A Pointer to the host ephemeral public key (B). */
  255. CRYS_SRP_Digest_t userProof, /*!< [in] A Pointer to the SRP user proof buffer (M1).*/
  256. CRYS_SRP_Digest_t hostProof, /*!< [out] A Pointer to the SRP host proof buffer (M2).*/
  257. CRYS_SRP_Secret_t sharedSecret, /*!< [out] A Pointer to the SRP shared secret (K).*/
  258. CRYS_SRP_Context_t *pCtx /*!< [in] A Pointer to the SRP context.*/
  259. );
  260. /************************ SRP User Functions **********************/
  261. /****************************************************************************************************/
  262. /*!
  263. @brief This function generates user public & private ephemeral key, known as A & a in RFC
  264. @return CRYS_OK on success.
  265. @return A non-zero value on failure as defined crys_srp_error.h or crys_rnd_error.h.
  266. */
  267. CIMPORT_C CRYSError_t CRYS_SRP_UserPubKeyCreate(
  268. size_t ephemPrivSize, /*!< [in] The size of the generated ephemeral private key (a).
  269. The range is between CRYS_SRP_PRIV_NUM_MIN_SIZE to
  270. CRYS_SRP_PRIV_NUM_MAX_SIZE */
  271. CRYS_SRP_Modulus_t userPubKeyA, /*!< [out] A Pointer to the user ephemeral public key (A). */
  272. CRYS_SRP_Context_t *pCtx /*!< [in/out] A Pointer to the SRP context.*/
  273. );
  274. /****************************************************************************************************/
  275. /*!
  276. @brief This function calculates the user proof.
  277. @return CRYS_OK on success.
  278. @return A non-zero value on failure as defined crys_srp_error.h or crys_hash_error.h.
  279. */
  280. CIMPORT_C CRYSError_t CRYS_SRP_UserProofCalc(
  281. size_t saltSize, /*!< [in] The size of the random salt,
  282. The range is between CRYS_SRP_MIN_SALT_SIZE
  283. to CRYS_SRP_MAX_SALT_SIZE. */
  284. uint8_t *pSalt, /*!< [in] A Pointer to the pSalt number.*/
  285. CRYS_SRP_Modulus_t userPubKeyA, /*!< [in] A Pointer to the user public ephmeral key (A).*/
  286. CRYS_SRP_Modulus_t hostPubKeyB, /*!< [in] A Pointer to the host public ephmeral key (B).*/
  287. CRYS_SRP_Digest_t userProof, /*!< [out] A Pointer to the SRP user proof buffer (M1).*/
  288. CRYS_SRP_Secret_t sharedSecret, /*!< [out] A Pointer to the SRP shared secret (K).*/
  289. CRYS_SRP_Context_t *pCtx /*!< [out] A Pointer to the SRP context.*/
  290. );
  291. /****************************************************************************************************/
  292. /*!
  293. @brief This function verifies the host proof
  294. @return CRYS_OK on success.
  295. @return A non-zero value on failure as defined crys_srp_error.h or crys_hash_error.h.
  296. */
  297. CIMPORT_C CRYSError_t CRYS_SRP_UserProofVerify(
  298. CRYS_SRP_Secret_t sharedSecret, /*!< [in] A Pointer to the SRP shared secret (K).*/
  299. CRYS_SRP_Modulus_t userPubKeyA, /*!< [in] A Pointer to the user public ephmeral key (A).*/
  300. CRYS_SRP_Digest_t userProof, /*!< [in] A Pointer to the SRP user proof buffer (M1).*/
  301. CRYS_SRP_Digest_t hostProof, /*!< [in] A Pointer to the SRP host proof buffer (M2).*/
  302. CRYS_SRP_Context_t *pCtx /*!< [out] A Pointer to the SRP user context.*/
  303. );
  304. #ifdef __cplusplus
  305. }
  306. #endif
  307. /**
  308. @}
  309. */
  310. #endif /* #ifndef CRYS_SRP_H */