1238384Sjkim/* crypto/srp/srp.h */
2296341Sdelphij/*
3296341Sdelphij * Written by Christophe Renou (christophe.renou@edelweb.fr) with the
4296341Sdelphij * precious help of Peter Sylvester (peter.sylvester@edelweb.fr) for the
5296341Sdelphij * EdelKey project and contributed to the OpenSSL project 2004.
6238384Sjkim */
7238384Sjkim/* ====================================================================
8238384Sjkim * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
9238384Sjkim *
10238384Sjkim * Redistribution and use in source and binary forms, with or without
11238384Sjkim * modification, are permitted provided that the following conditions
12238384Sjkim * are met:
13238384Sjkim *
14238384Sjkim * 1. Redistributions of source code must retain the above copyright
15296341Sdelphij *    notice, this list of conditions and the following disclaimer.
16238384Sjkim *
17238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
18238384Sjkim *    notice, this list of conditions and the following disclaimer in
19238384Sjkim *    the documentation and/or other materials provided with the
20238384Sjkim *    distribution.
21238384Sjkim *
22238384Sjkim * 3. All advertising materials mentioning features or use of this
23238384Sjkim *    software must display the following acknowledgment:
24238384Sjkim *    "This product includes software developed by the OpenSSL Project
25238384Sjkim *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26238384Sjkim *
27238384Sjkim * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28238384Sjkim *    endorse or promote products derived from this software without
29238384Sjkim *    prior written permission. For written permission, please contact
30238384Sjkim *    licensing@OpenSSL.org.
31238384Sjkim *
32238384Sjkim * 5. Products derived from this software may not be called "OpenSSL"
33238384Sjkim *    nor may "OpenSSL" appear in their names without prior written
34238384Sjkim *    permission of the OpenSSL Project.
35238384Sjkim *
36238384Sjkim * 6. Redistributions of any form whatsoever must retain the following
37238384Sjkim *    acknowledgment:
38238384Sjkim *    "This product includes software developed by the OpenSSL Project
39238384Sjkim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40238384Sjkim *
41238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42238384Sjkim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44238384Sjkim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
45238384Sjkim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46238384Sjkim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47238384Sjkim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48238384Sjkim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50238384Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51238384Sjkim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52238384Sjkim * OF THE POSSIBILITY OF SUCH DAMAGE.
53238384Sjkim * ====================================================================
54238384Sjkim *
55238384Sjkim * This product includes cryptographic software written by Eric Young
56238384Sjkim * (eay@cryptsoft.com).  This product includes software written by Tim
57238384Sjkim * Hudson (tjh@cryptsoft.com).
58238384Sjkim *
59238384Sjkim */
60238384Sjkim#ifndef __SRP_H__
61296341Sdelphij# define __SRP_H__
62238384Sjkim
63296341Sdelphij# ifndef OPENSSL_NO_SRP
64238384Sjkim
65296341Sdelphij#  include <stdio.h>
66296341Sdelphij#  include <string.h>
67238384Sjkim
68238384Sjkim#ifdef  __cplusplus
69238384Sjkimextern "C" {
70238384Sjkim#endif
71238384Sjkim
72296341Sdelphij#  include <openssl/safestack.h>
73296341Sdelphij#  include <openssl/bn.h>
74296341Sdelphij#  include <openssl/crypto.h>
75238384Sjkim
76296341Sdelphijtypedef struct SRP_gN_cache_st {
77296341Sdelphij    char *b64_bn;
78296341Sdelphij    BIGNUM *bn;
79296341Sdelphij} SRP_gN_cache;
80238384Sjkim
81238384Sjkim
82238384SjkimDECLARE_STACK_OF(SRP_gN_cache)
83238384Sjkim
84296341Sdelphijtypedef struct SRP_user_pwd_st {
85296341Sdelphij    /* Owned by us. */
86296341Sdelphij    char *id;
87296341Sdelphij    BIGNUM *s;
88296341Sdelphij    BIGNUM *v;
89296341Sdelphij    /* Not owned by us. */
90296341Sdelphij    const BIGNUM *g;
91296341Sdelphij    const BIGNUM *N;
92296341Sdelphij    /* Owned by us. */
93296341Sdelphij    char *info;
94296341Sdelphij} SRP_user_pwd;
95238384Sjkim
96238384SjkimDECLARE_STACK_OF(SRP_user_pwd)
97238384Sjkim
98296341Sdelphijvoid SRP_user_pwd_free(SRP_user_pwd *user_pwd);
99296341Sdelphij
100296341Sdelphijtypedef struct SRP_VBASE_st {
101296341Sdelphij    STACK_OF(SRP_user_pwd) *users_pwd;
102296341Sdelphij    STACK_OF(SRP_gN_cache) *gN_cache;
103238384Sjkim/* to simulate a user */
104296341Sdelphij    char *seed_key;
105296341Sdelphij    BIGNUM *default_g;
106296341Sdelphij    BIGNUM *default_N;
107296341Sdelphij} SRP_VBASE;
108238384Sjkim
109296341Sdelphij/*
110296341Sdelphij * Structure interne pour retenir les couples N et g
111296341Sdelphij */
112296341Sdelphijtypedef struct SRP_gN_st {
113296341Sdelphij    char *id;
114296341Sdelphij    BIGNUM *g;
115296341Sdelphij    BIGNUM *N;
116296341Sdelphij} SRP_gN;
117238384Sjkim
118238384SjkimDECLARE_STACK_OF(SRP_gN)
119238384Sjkim
120238384SjkimSRP_VBASE *SRP_VBASE_new(char *seed_key);
121238384Sjkimint SRP_VBASE_free(SRP_VBASE *vb);
122296341Sdelphijint SRP_VBASE_init(SRP_VBASE *vb, char *verifier_file);
123296341Sdelphij
124296341Sdelphij/* This method ignores the configured seed and fails for an unknown user. */
125238384SjkimSRP_user_pwd *SRP_VBASE_get_by_user(SRP_VBASE *vb, char *username);
126296341Sdelphij/* NOTE: unlike in SRP_VBASE_get_by_user, caller owns the returned pointer.*/
127296341SdelphijSRP_user_pwd *SRP_VBASE_get1_by_user(SRP_VBASE *vb, char *username);
128296341Sdelphij
129238384Sjkimchar *SRP_create_verifier(const char *user, const char *pass, char **salt,
130296341Sdelphij                          char **verifier, const char *N, const char *g);
131296341Sdelphijint SRP_create_verifier_BN(const char *user, const char *pass, BIGNUM **salt,
132296341Sdelphij                           BIGNUM **verifier, BIGNUM *N, BIGNUM *g);
133238384Sjkim
134296341Sdelphij#  define SRP_NO_ERROR 0
135296341Sdelphij#  define SRP_ERR_VBASE_INCOMPLETE_FILE 1
136296341Sdelphij#  define SRP_ERR_VBASE_BN_LIB 2
137296341Sdelphij#  define SRP_ERR_OPEN_FILE 3
138296341Sdelphij#  define SRP_ERR_MEMORY 4
139238384Sjkim
140296341Sdelphij#  define DB_srptype      0
141296341Sdelphij#  define DB_srpverifier  1
142296341Sdelphij#  define DB_srpsalt      2
143296341Sdelphij#  define DB_srpid        3
144296341Sdelphij#  define DB_srpgN        4
145296341Sdelphij#  define DB_srpinfo      5
146296341Sdelphij#  undef  DB_NUMBER
147296341Sdelphij#  define DB_NUMBER       6
148238384Sjkim
149296341Sdelphij#  define DB_SRP_INDEX    'I'
150296341Sdelphij#  define DB_SRP_VALID    'V'
151296341Sdelphij#  define DB_SRP_REVOKED  'R'
152296341Sdelphij#  define DB_SRP_MODIF    'v'
153238384Sjkim
154238384Sjkim/* see srp.c */
155296341Sdelphijchar *SRP_check_known_gN_param(BIGNUM *g, BIGNUM *N);
156296341SdelphijSRP_gN *SRP_get_default_gN(const char *id);
157238384Sjkim
158238384Sjkim/* server side .... */
159296341SdelphijBIGNUM *SRP_Calc_server_key(BIGNUM *A, BIGNUM *v, BIGNUM *u, BIGNUM *b,
160296341Sdelphij                            BIGNUM *N);
161238384SjkimBIGNUM *SRP_Calc_B(BIGNUM *b, BIGNUM *N, BIGNUM *g, BIGNUM *v);
162238384Sjkimint SRP_Verify_A_mod_N(BIGNUM *A, BIGNUM *N);
163296341SdelphijBIGNUM *SRP_Calc_u(BIGNUM *A, BIGNUM *B, BIGNUM *N);
164238384Sjkim
165238384Sjkim/* client side .... */
166238384SjkimBIGNUM *SRP_Calc_x(BIGNUM *s, const char *user, const char *pass);
167238384SjkimBIGNUM *SRP_Calc_A(BIGNUM *a, BIGNUM *N, BIGNUM *g);
168296341SdelphijBIGNUM *SRP_Calc_client_key(BIGNUM *N, BIGNUM *B, BIGNUM *g, BIGNUM *x,
169296341Sdelphij                            BIGNUM *a, BIGNUM *u);
170238384Sjkimint SRP_Verify_B_mod_N(BIGNUM *B, BIGNUM *N);
171238384Sjkim
172296341Sdelphij#  define SRP_MINIMAL_N 1024
173238384Sjkim
174238384Sjkim#ifdef  __cplusplus
175238384Sjkim}
176238384Sjkim#endif
177238384Sjkim
178296341Sdelphij# endif
179238384Sjkim#endif
180