srp.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. *
  10. * Originally written by Christophe Renou and Peter Sylvester,
  11. * for the EdelKey project.
  12. */
  13. #ifndef HEADER_SRP_H
  14. # define HEADER_SRP_H
  15. #include <openssl/opensslconf.h>
  16. #ifndef OPENSSL_NO_SRP
  17. # include <stdio.h>
  18. # include <string.h>
  19. # include <openssl/safestack.h>
  20. # include <openssl/bn.h>
  21. # include <openssl/crypto.h>
  22. # ifdef __cplusplus
  23. extern "C" {
  24. # endif
  25. typedef struct SRP_gN_cache_st {
  26. char *b64_bn;
  27. BIGNUM *bn;
  28. } SRP_gN_cache;
  29. DEFINE_STACK_OF(SRP_gN_cache)
  30. typedef struct SRP_user_pwd_st {
  31. /* Owned by us. */
  32. char *id;
  33. BIGNUM *s;
  34. BIGNUM *v;
  35. /* Not owned by us. */
  36. const BIGNUM *g;
  37. const BIGNUM *N;
  38. /* Owned by us. */
  39. char *info;
  40. } SRP_user_pwd;
  41. SRP_user_pwd *SRP_user_pwd_new(void);
  42. void SRP_user_pwd_free(SRP_user_pwd *user_pwd);
  43. void SRP_user_pwd_set_gN(SRP_user_pwd *user_pwd, const BIGNUM *g, const BIGNUM *N);
  44. int SRP_user_pwd_set1_ids(SRP_user_pwd *user_pwd, const char *id, const char *info);
  45. int SRP_user_pwd_set0_sv(SRP_user_pwd *user_pwd, BIGNUM *s, BIGNUM *v);
  46. DEFINE_STACK_OF(SRP_user_pwd)
  47. typedef struct SRP_VBASE_st {
  48. STACK_OF(SRP_user_pwd) *users_pwd;
  49. STACK_OF(SRP_gN_cache) *gN_cache;
  50. /* to simulate a user */
  51. char *seed_key;
  52. const BIGNUM *default_g;
  53. const BIGNUM *default_N;
  54. } SRP_VBASE;
  55. /*
  56. * Internal structure storing N and g pair
  57. */
  58. typedef struct SRP_gN_st {
  59. char *id;
  60. const BIGNUM *g;
  61. const BIGNUM *N;
  62. } SRP_gN;
  63. DEFINE_STACK_OF(SRP_gN)
  64. SRP_VBASE *SRP_VBASE_new(char *seed_key);
  65. void SRP_VBASE_free(SRP_VBASE *vb);
  66. int SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file);
  67. int SRP_VBASE_add0_user(SRP_VBASE *vb, SRP_user_pwd *user_pwd);
  68. /* This method ignores the configured seed and fails for an unknown user. */
  69. DEPRECATEDIN_1_1_0(SRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username))
  70. /* NOTE: unlike in SRP_VBASE_get_by_user, caller owns the returned pointer.*/
  71. SRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username);
  72. char *SRP_create_verifier(const char *user, const char *pass, char **salt,
  73. char **verifier, const char *N, const char *g);
  74. int SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
  75. BIGNUM **verifier, const BIGNUM *N,
  76. const BIGNUM *g);
  77. # define SRP_NO_ERROR 0
  78. # define SRP_ERR_VBASE_INCOMPLETE_FILE 1
  79. # define SRP_ERR_VBASE_BN_LIB 2
  80. # define SRP_ERR_OPEN_FILE 3
  81. # define SRP_ERR_MEMORY 4
  82. # define DB_srptype 0
  83. # define DB_srpverifier 1
  84. # define DB_srpsalt 2
  85. # define DB_srpid 3
  86. # define DB_srpgN 4
  87. # define DB_srpinfo 5
  88. # undef DB_NUMBER
  89. # define DB_NUMBER 6
  90. # define DB_SRP_INDEX 'I'
  91. # define DB_SRP_VALID 'V'
  92. # define DB_SRP_REVOKED 'R'
  93. # define DB_SRP_MODIF 'v'
  94. /* see srp.c */
  95. char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N);
  96. SRP_gN *SRP_get_default_gN(const char *id);
  97. /* server side .... */
  98. BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
  99. const BIGNUM *b, const BIGNUM *N);
  100. BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
  101. const BIGNUM *v);
  102. int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N);
  103. BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N);
  104. /* client side .... */
  105. BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass);
  106. BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g);
  107. BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
  108. const BIGNUM *x, const BIGNUM *a, const BIGNUM *u);
  109. int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N);
  110. # define SRP_MINIMAL_N 1024
  111. # ifdef __cplusplus
  112. }
  113. # endif
  114. # endif
  115. #endif