ocrypto_srp.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /**
  2. * Copyright (c) 2019 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /**@file
  41. * @defgroup nrf_oberon_srp SRP - Secure Remote Password APIs
  42. * @ingroup nrf_oberon
  43. * @{
  44. * @brief Type declarations and APIs for the SRP key agreement protocol.
  45. *
  46. * SRP is an augmented password-authenticated key agreement protocol,
  47. * specifically designed to work around existing patents. SRP allows the use of
  48. * user names and passwords over unencrypted channels and supplies a shared
  49. * secret at the end of the authentication sequence that can be used to generate
  50. * encryption keys.
  51. *
  52. * An eavesdropper or man in the middle cannot obtain enough information to be
  53. * able to brute force guess a password without further interactions with the
  54. * parties for each guess.
  55. *
  56. * The server does not store password-equivalent data. This means that an
  57. * attacker who steals the server data cannot masquerade as the client unless
  58. * they first perform a brute force search for the password.
  59. *
  60. * The specific variant implemented here is SRP-6 3072 bit SHA-512.
  61. *
  62. * @see [RFC 5054 - Using the Secure Remote Password (SRP) Protocol for TLS Authentication](https://tools.ietf.org/html/rfc5054)
  63. * @see [The Stanford SRP Homepage](http://srp.stanford.edu)
  64. *
  65. * **Basic protocol overview**
  66. *
  67. * *Setup*
  68. * 1. Server generates a username / password combination together with a salt.
  69. * 2. Server derives a password verifier (see #ocrypto_srp_verifier).
  70. * 3. The username, salt and verifier are stored and required to open sessions.
  71. * The original password is no longer needed.
  72. *
  73. * *Session opening*
  74. * 1. Client sends a username and the public key of an ephemeral key pair to the
  75. * server.
  76. * 2. Server sends the salt and the public key of another ephemeral key pair to
  77. * the client (see #ocrypto_srp_public_key).
  78. * 3. Client and Server both compute the session key from this information (see
  79. * #ocrypto_srp_scrambling_parameter, #ocrypto_srp_premaster_secret,
  80. * #ocrypto_srp_session_key).
  81. * 4. Client sends proof of the session key to the server.
  82. * 5. Server validates proof (see #ocrypto_srp_proof_m1), then sends proof of the
  83. * session key to the client (see #ocrypto_srp_proof_m2).
  84. * 6. Client validates proof. Both parties know that they share the same private
  85. * session key.
  86. */
  87. #ifndef OCRYPTO_SRP_H
  88. #define OCRYPTO_SRP_H
  89. #include <stddef.h>
  90. #include <stdint.h>
  91. #ifdef __cplusplus
  92. extern "C" {
  93. #endif
  94. /**
  95. * Salt length.
  96. */
  97. #define ocrypto_srp_SALT_BYTES (16)
  98. /**
  99. * Password verifier length.
  100. */
  101. #define ocrypto_srp_VERIFIER_BYTES (384)
  102. /**
  103. * Secret key length.
  104. */
  105. #define ocrypto_srp_SECRET_KEY_BYTES (32)
  106. /**
  107. * Public key length.
  108. */
  109. #define ocrypto_srp_PUBLIC_KEY_BYTES (384)
  110. /**
  111. * Scrambling parameter length.
  112. */
  113. #define ocrypto_srp_SCRAMBLING_PARAMETER_BYTES (64)
  114. /**
  115. * Premaster secret length.
  116. */
  117. #define ocrypto_srp_PREMASTER_SECRET_BYTES (384)
  118. /**
  119. * Session key length.
  120. */
  121. #define ocrypto_srp_SESSION_KEY_BYTES (64)
  122. /**
  123. * Proof length.
  124. */
  125. #define ocrypto_srp_PROOF_BYTES (64)
  126. /**@name SRP-6 Password verifier generation
  127. *
  128. * A password verifier is generated from a user name and a password. The
  129. * password @p pass may be discarded, as only the verifier is used during later
  130. * computations.
  131. */
  132. /**@{*/
  133. /**
  134. * SRP-6 Password Verifier.
  135. *
  136. * The verifier is generated for a given user name @p user, a password @p pass
  137. * and salt @p salt.
  138. *
  139. * @param[out] v Generated password verifier, must be 32-bit aligned.
  140. * @param salt Salt.
  141. * @param user User name.
  142. * @param user_len Length of @p user.
  143. * @param pass Password.
  144. * @param pass_len Length of @p pass.
  145. */
  146. void ocrypto_srp_verifier(
  147. uint8_t v[ocrypto_srp_VERIFIER_BYTES],
  148. const uint8_t salt[ocrypto_srp_SALT_BYTES],
  149. const uint8_t *user, size_t user_len,
  150. const uint8_t *pass, size_t pass_len);
  151. /**@}*/
  152. /**@name SRP-6 Public key generation
  153. *
  154. * An ephemeral keypair can be generated based on the password verifier to be
  155. * used when opening a new session.
  156. */
  157. /**@{*/
  158. /**
  159. * SRP-6 Public Key.
  160. *
  161. * The public key for a given private key @p priv_b is generated using the
  162. * password verifier @p v and put into @p pub_b.
  163. *
  164. * @param[out] pub_b Generated public key, must be 32-bit aligned.
  165. * @param priv_b Private key.
  166. * @param v Password verifier.
  167. */
  168. void ocrypto_srp_public_key(
  169. uint8_t pub_b[ocrypto_srp_PUBLIC_KEY_BYTES],
  170. const uint8_t priv_b[ocrypto_srp_SECRET_KEY_BYTES],
  171. const uint8_t v[ocrypto_srp_VERIFIER_BYTES]);
  172. /**@}*/
  173. /**@name SRP-6 Session key generation
  174. *
  175. * A premaster secret can be derived from both the client's and server's public
  176. * keys, the server's private key and the password verifier. A shared session
  177. * key can be generated from this premaster secret.
  178. */
  179. /**@{*/
  180. /**
  181. * SRP-6 Scrambling Parameter.
  182. *
  183. * The scrambling parameter is computed from both the client's public key
  184. * @p pub_a and the server's public key @p pub_b. The scrambling parameter
  185. * is required to compute the premaster secret.
  186. *
  187. * @param[out] u Generated scrambling parameter.
  188. * @param pub_a Client public key.
  189. * @param pub_b Server public key.
  190. */
  191. void ocrypto_srp_scrambling_parameter(
  192. uint8_t u[ocrypto_srp_SCRAMBLING_PARAMETER_BYTES],
  193. const uint8_t pub_a[ocrypto_srp_PUBLIC_KEY_BYTES],
  194. const uint8_t pub_b[ocrypto_srp_PUBLIC_KEY_BYTES]);
  195. /**
  196. * SRP-6 Premaster Secret.
  197. *
  198. * The premaster secret between the client and the server is computed using the
  199. * client public key @p pub_a, the server private key @p priv_b, the scrambling
  200. * parameter @p u and the password verifier @p v. If the client public key
  201. * @p pub_a is valid, the premaster secret is then put into @p s. The premaster
  202. * secret can be used to generate encryption keys.
  203. *
  204. * @param[out] s Generated premaster secret, must be 32-bit aligned.
  205. * @param pub_a Client public key.
  206. * @param priv_b Server private key.
  207. * @param u Scrambling parameter; generated with @c srp_scrambling_parameter.
  208. * @param v Password verifier.
  209. *
  210. * @retval 0 If @p pub_a is a valid public key.
  211. * @retval 1 Otherwise.
  212. */
  213. int ocrypto_srp_premaster_secret(
  214. uint8_t s[ocrypto_srp_PREMASTER_SECRET_BYTES],
  215. const uint8_t pub_a[ocrypto_srp_PUBLIC_KEY_BYTES],
  216. const uint8_t priv_b[ocrypto_srp_SECRET_KEY_BYTES],
  217. const uint8_t u[ocrypto_srp_SCRAMBLING_PARAMETER_BYTES],
  218. const uint8_t v[ocrypto_srp_VERIFIER_BYTES]);
  219. /**
  220. * SRP-6 SRP Session Key.
  221. *
  222. * Generates the shared SRP session key from the premaster secret @p s and puts
  223. * it into @p k.
  224. *
  225. * @param[out] k Generated SRP session key.
  226. * @param s Premaster secret.
  227. */
  228. void ocrypto_srp_session_key(
  229. uint8_t k[ocrypto_srp_SESSION_KEY_BYTES],
  230. const uint8_t s[ocrypto_srp_PREMASTER_SECRET_BYTES]);
  231. /**@}*/
  232. /**@name SRP-6 Proof exchange
  233. *
  234. * Proofs are exchanged from client to server and vice versa to ensure that both
  235. * parties computed the same shared session key. The proofs only match if the
  236. * correct password is used by the client.
  237. */
  238. /**@{*/
  239. /**
  240. * SRP-6 Proof M1 (client to server).
  241. *
  242. * A proof is generated by the client and sent to the server to assert that the
  243. * client is in possession of the shared session key @p k. The server also
  244. * generates the proof. Only if the proofs match, the process can continue.
  245. * The proof is based on the salt @p salt, the client public key @p pub_a,
  246. * the server public key @p pub_b and the shared session key @p k.
  247. *
  248. * @param[out] m1 Generated proof.
  249. * @param user User name.
  250. * @param user_len Length of @p user.
  251. * @param salt Salt.
  252. * @param pub_a Client public key.
  253. * @param pub_b Server public key.
  254. * @param k Session key.
  255. */
  256. void ocrypto_srp_proof_m1(
  257. uint8_t m1[ocrypto_srp_PROOF_BYTES],
  258. const uint8_t *user, size_t user_len,
  259. const uint8_t salt[ocrypto_srp_SALT_BYTES],
  260. const uint8_t pub_a[ocrypto_srp_PUBLIC_KEY_BYTES],
  261. const uint8_t pub_b[ocrypto_srp_PUBLIC_KEY_BYTES],
  262. const uint8_t k[ocrypto_srp_SESSION_KEY_BYTES]);
  263. /**
  264. * SRP-6 Proof M2 (server to client).
  265. *
  266. * A second proof is generated by the server and sent back to the client to
  267. * assert that the server is in possession of the shared session key @p k. The
  268. * client also generates the proof. If the proofs match, both parties can assume
  269. * that they share the same session key @p k. The second proof is based on the
  270. * client public key @p pub_a, the first proof @p m1 and the session key @p k.
  271. *
  272. * @param[out] m2 Generated proof.
  273. * @param pub_a Client public key.
  274. * @param m1 First proof. Generated with @c srp_proof_m1.
  275. * @param k Session key.
  276. */
  277. void ocrypto_srp_proof_m2(
  278. uint8_t m2[ocrypto_srp_PROOF_BYTES],
  279. const uint8_t pub_a[ocrypto_srp_PUBLIC_KEY_BYTES],
  280. const uint8_t m1[ocrypto_srp_PROOF_BYTES],
  281. const uint8_t k[ocrypto_srp_SESSION_KEY_BYTES]);
  282. /**@}*/
  283. #ifdef __cplusplus
  284. }
  285. #endif
  286. #endif /* #ifndef OCRYPTO_SRP_H */
  287. /** @} */