srp.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Secure Remote Password 6a implementation
  3. * https://github.com/est31/csrp-gmp
  4. *
  5. * The MIT License (MIT)
  6. *
  7. * Copyright (c) 2010, 2013 Tom Cocagne, 2015 est31 <MTest31@outlook.com>
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  10. * this software and associated documentation files (the "Software"), to deal in
  11. * the Software without restriction, including without limitation the rights to
  12. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  13. * of the Software, and to permit persons to whom the Software is furnished to do
  14. * so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in all
  17. * copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  25. * SOFTWARE.
  26. *
  27. */
  28. /*
  29. *
  30. * Purpose: This is a direct implementation of the Secure Remote Password
  31. * Protocol version 6a as described by
  32. * http://srp.stanford.edu/design.html
  33. *
  34. * Author: tom.cocagne@gmail.com (Tom Cocagne)
  35. *
  36. * Dependencies: LibGMP
  37. *
  38. * Usage: Refer to test_srp.c for a demonstration
  39. *
  40. * Notes:
  41. * This library allows multiple combinations of hashing algorithms and
  42. * prime number constants. For authentication to succeed, the hash and
  43. * prime number constants must match between
  44. * srp_create_salted_verification_key(), srp_user_new(),
  45. * and srp_verifier_new(). A recommended approach is to determine the
  46. * desired level of security for an application and globally define the
  47. * hash and prime number constants to the predetermined values.
  48. *
  49. * As one might suspect, more bits means more security. As one might also
  50. * suspect, more bits also means more processing time. The test_srp.c
  51. * program can be easily modified to profile various combinations of
  52. * hash & prime number pairings.
  53. */
  54. #pragma once
  55. struct SRPVerifier;
  56. struct SRPUser;
  57. typedef enum {
  58. SRP_NG_1024,
  59. SRP_NG_2048,
  60. SRP_NG_4096,
  61. SRP_NG_8192,
  62. SRP_NG_CUSTOM
  63. } SRP_NGType;
  64. typedef enum {
  65. /*SRP_SHA1,*/
  66. /*SRP_SHA224,*/
  67. SRP_SHA256,
  68. /*SRP_SHA384,
  69. SRP_SHA512*/
  70. } SRP_HashAlgorithm;
  71. typedef enum {
  72. SRP_ERR,
  73. SRP_OK,
  74. } SRP_Result;
  75. // clang-format off
  76. /* Sets the memory functions used by srp.
  77. * Note: this doesn't set the memory functions used by gmp,
  78. * but it is supported to have different functions for srp and gmp.
  79. * Don't call this after you have already allocated srp structures.
  80. */
  81. void srp_set_memory_functions(
  82. void *(*new_srp_alloc) (size_t),
  83. void *(*new_srp_realloc) (void *, size_t),
  84. void (*new_srp_free) (void *));
  85. /* Out: bytes_v, len_v
  86. *
  87. * The caller is responsible for freeing the memory allocated for bytes_v
  88. *
  89. * The n_hex and g_hex parameters should be 0 unless SRP_NG_CUSTOM is used for ng_type.
  90. * If provided, they must contain ASCII text of the hexidecimal notation.
  91. *
  92. * If bytes_s == NULL, it is filled with random data.
  93. * The caller is responsible for freeing.
  94. *
  95. * Returns SRP_OK on success, and SRP_ERR on error.
  96. * bytes_s might be in this case invalid, don't free it.
  97. */
  98. SRP_Result srp_create_salted_verification_key(SRP_HashAlgorithm alg,
  99. SRP_NGType ng_type, const char *username_for_verifier,
  100. const unsigned char *password, size_t len_password,
  101. unsigned char **bytes_s, size_t *len_s,
  102. unsigned char **bytes_v, size_t *len_v,
  103. const char *n_hex, const char *g_hex);
  104. /* Out: bytes_B, len_B.
  105. *
  106. * On failure, bytes_B will be set to NULL and len_B will be set to 0
  107. *
  108. * The n_hex and g_hex parameters should be 0 unless SRP_NG_CUSTOM is used for ng_type
  109. *
  110. * If bytes_b == NULL, random data is used for b.
  111. *
  112. * Returns pointer to SRPVerifier on success, and NULL on error.
  113. */
  114. struct SRPVerifier* srp_verifier_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
  115. const char *username,
  116. const unsigned char *bytes_s, size_t len_s,
  117. const unsigned char *bytes_v, size_t len_v,
  118. const unsigned char *bytes_A, size_t len_A,
  119. const unsigned char *bytes_b, size_t len_b,
  120. unsigned char** bytes_B, size_t *len_B,
  121. const char* n_hex, const char* g_hex);
  122. // clang-format on
  123. void srp_verifier_delete(struct SRPVerifier *ver);
  124. // srp_verifier_verify_session must have been called before
  125. int srp_verifier_is_authenticated(struct SRPVerifier *ver);
  126. const char *srp_verifier_get_username(struct SRPVerifier *ver);
  127. /* key_length may be null */
  128. const unsigned char *srp_verifier_get_session_key(
  129. struct SRPVerifier *ver, size_t *key_length);
  130. size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver);
  131. /* Verifies session, on success, it writes bytes_HAMK.
  132. * user_M must be exactly srp_verifier_get_session_key_length() bytes in size
  133. */
  134. void srp_verifier_verify_session(
  135. struct SRPVerifier *ver, const unsigned char *user_M, unsigned char **bytes_HAMK);
  136. /*******************************************************************************/
  137. /* The n_hex and g_hex parameters should be 0 unless SRP_NG_CUSTOM is used for ng_type */
  138. struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
  139. const char *username, const char *username_for_verifier,
  140. const unsigned char *bytes_password, size_t len_password, const char *n_hex,
  141. const char *g_hex);
  142. void srp_user_delete(struct SRPUser *usr);
  143. int srp_user_is_authenticated(struct SRPUser *usr);
  144. const char *srp_user_get_username(struct SRPUser *usr);
  145. /* key_length may be null */
  146. const unsigned char *srp_user_get_session_key(struct SRPUser *usr, size_t *key_length);
  147. size_t srp_user_get_session_key_length(struct SRPUser *usr);
  148. // clang-format off
  149. /* Output: username, bytes_A, len_A.
  150. * If you don't want it get written, set username to NULL.
  151. * If bytes_a == NULL, random data is used for a. */
  152. SRP_Result srp_user_start_authentication(struct SRPUser* usr, char **username,
  153. const unsigned char *bytes_a, size_t len_a,
  154. unsigned char **bytes_A, size_t* len_A);
  155. /* Output: bytes_M, len_M (len_M may be null and will always be
  156. * srp_user_get_session_key_length() bytes in size) */
  157. void srp_user_process_challenge(struct SRPUser *usr,
  158. const unsigned char *bytes_s, size_t len_s,
  159. const unsigned char *bytes_B, size_t len_B,
  160. unsigned char **bytes_M, size_t *len_M);
  161. // clang-format on
  162. /* bytes_HAMK must be exactly srp_user_get_session_key_length() bytes in size */
  163. void srp_user_verify_session(struct SRPUser *usr, const unsigned char *bytes_HAMK);