summaryrefslogtreecommitdiffstats
path: root/3rd_party/libsrp6a-sha512/srp.c
diff options
context:
space:
mode:
Diffstat (limited to '3rd_party/libsrp6a-sha512/srp.c')
-rw-r--r--3rd_party/libsrp6a-sha512/srp.c274
1 files changed, 274 insertions, 0 deletions
diff --git a/3rd_party/libsrp6a-sha512/srp.c b/3rd_party/libsrp6a-sha512/srp.c
new file mode 100644
index 0000000..74e1f98
--- /dev/null
+++ b/3rd_party/libsrp6a-sha512/srp.c
@@ -0,0 +1,274 @@
1/*
2 * Copyright (c) 1997-2007 The Stanford SRP Authentication Project
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
18 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
21 * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
22 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
23 * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
24 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 *
26 * Redistributions in source or binary form must retain an intact copy
27 * of this copyright notice.
28 */
29
30#include "t_defines.h"
31#include "srp.h"
32
33static int library_initialized = 0;
34
35_TYPE( SRP_RESULT )
36SRP_initialize_library()
37{
38 if(library_initialized == 0) {
39 BigIntegerInitialize();
40 t_stronginitrand();
41 library_initialized = 1;
42 }
43 return SRP_SUCCESS;
44}
45
46_TYPE( SRP_RESULT )
47SRP_finalize_library()
48{
49 if(library_initialized > 0) {
50 library_initialized = 0;
51 BigIntegerFinalize();
52 }
53 return SRP_SUCCESS;
54}
55
56static int srp_modulus_min_bits = SRP_DEFAULT_MIN_BITS;
57
58_TYPE( SRP_RESULT )
59SRP_set_modulus_min_bits(int minbits)
60{
61 srp_modulus_min_bits = minbits;
62 return SRP_SUCCESS;
63}
64
65_TYPE( int )
66SRP_get_modulus_min_bits()
67{
68 return srp_modulus_min_bits;
69}
70
71static int
72default_secret_bits_cb(int modsize)
73{
74 return 256;
75 /*return modsize;*/ /* Warning: Very Slow */
76}
77
78static SRP_SECRET_BITS_CB srp_sb_cb = default_secret_bits_cb;
79
80_TYPE( SRP_RESULT )
81SRP_set_secret_bits_cb(SRP_SECRET_BITS_CB cb)
82{
83 srp_sb_cb = cb;
84 return SRP_SUCCESS;
85}
86
87_TYPE( int )
88SRP_get_secret_bits(int modsize)
89{
90 return (*srp_sb_cb)(modsize);
91}
92
93_TYPE( SRP * )
94SRP_new(SRP_METHOD * meth)
95{
96 SRP * srp = (SRP *) malloc(sizeof(SRP));
97
98 if(srp == NULL)
99 return NULL;
100
101 srp->flags = 0;
102 srp->username = cstr_new();
103 srp->bctx = BigIntegerCtxNew();
104 srp->modulus = NULL;
105 srp->accel = NULL;
106 srp->generator = NULL;
107 srp->salt = NULL;
108 srp->verifier = NULL;
109 srp->password = NULL;
110 srp->pubkey = NULL;
111 srp->secret = NULL;
112 srp->u = NULL;
113 srp->key = NULL;
114 srp->ex_data = cstr_new();
115 srp->param_cb = NULL;
116 srp->meth = meth;
117 srp->meth_data = NULL;
118 //srp->slu = NULL;
119 if(srp->meth->init == NULL || (*srp->meth->init)(srp) == SRP_SUCCESS)
120 return srp;
121 free(srp);
122 return NULL;
123}
124
125_TYPE( SRP_RESULT )
126SRP_free(SRP * srp)
127{
128 if(srp->meth->finish)
129 (*srp->meth->finish)(srp);
130
131 if(srp->username)
132 cstr_clear_free(srp->username);
133 if(srp->modulus)
134 BigIntegerFree(srp->modulus);
135 if(srp->accel)
136 BigIntegerModAccelFree(srp->accel);
137 if(srp->generator)
138 BigIntegerFree(srp->generator);
139 if(srp->salt)
140 cstr_clear_free(srp->salt);
141 if(srp->verifier)
142 BigIntegerClearFree(srp->verifier);
143 if(srp->password)
144 BigIntegerClearFree(srp->password);
145 if(srp->pubkey)
146 BigIntegerFree(srp->pubkey);
147 if(srp->secret)
148 BigIntegerClearFree(srp->secret);
149 if(srp->u)
150 BigIntegerFree(srp->u);
151 if(srp->key)
152 BigIntegerClearFree(srp->key);
153 if(srp->bctx)
154 BigIntegerCtxFree(srp->bctx);
155 if(srp->ex_data)
156 cstr_clear_free(srp->ex_data);
157 free(srp);
158 return SRP_SUCCESS;
159}
160
161_TYPE( SRP_RESULT )
162SRP_set_client_param_verify_cb(SRP * srp, SRP_CLIENT_PARAM_VERIFY_CB cb)
163{
164 srp->param_cb = cb;
165 return SRP_SUCCESS;
166}
167
168_TYPE( SRP_RESULT )
169SRP_set_username(SRP * srp, const char * username)
170{
171 cstr_set(srp->username, username);
172 return SRP_SUCCESS;
173}
174
175_TYPE( SRP_RESULT )
176SRP_set_user_raw(SRP * srp, const unsigned char * user, int userlen)
177{
178 cstr_setn(srp->username, (const char*)user, userlen);
179 return SRP_SUCCESS;
180}
181
182_TYPE( SRP_RESULT )
183SRP_set_params(SRP * srp, const unsigned char * modulus, int modlen,
184 const unsigned char * generator, int genlen,
185 const unsigned char * salt, int saltlen)
186{
187 SRP_RESULT rc;
188
189 if(modulus == NULL || generator == NULL || salt == NULL)
190 return SRP_ERROR;
191
192 /* Set fields in SRP context */
193 srp->modulus = BigIntegerFromBytes(modulus, modlen);
194 if(srp->flags & SRP_FLAG_MOD_ACCEL)
195 srp->accel = BigIntegerModAccelNew(srp->modulus, srp->bctx);
196 srp->generator = BigIntegerFromBytes(generator, genlen);
197 if(srp->salt == NULL)
198 srp->salt = cstr_new();
199 cstr_setn(srp->salt, (const char*)salt, saltlen);
200
201 /* Now attempt to validate parameters */
202 if(BigIntegerBitLen(srp->modulus) < SRP_get_modulus_min_bits())
203 return SRP_ERROR;
204
205 if(srp->param_cb) {
206 rc = (*srp->param_cb)(srp, modulus, modlen, generator, genlen);
207 if(!SRP_OK(rc))
208 return rc;
209 }
210
211 return (*srp->meth->params)(srp, modulus, modlen, generator, genlen,
212 salt, saltlen);
213}
214
215_TYPE( SRP_RESULT )
216SRP_set_authenticator(SRP * srp, const unsigned char * a, int alen)
217{
218 return (*srp->meth->auth)(srp, a, alen);
219}
220
221_TYPE( SRP_RESULT )
222SRP_set_auth_password(SRP * srp, const char * password)
223{
224 return (*srp->meth->passwd)(srp, (const unsigned char *)password,
225 strlen(password));
226}
227
228_TYPE( SRP_RESULT )
229SRP_set_auth_password_raw(SRP * srp,
230 const unsigned char * password, int passlen)
231{
232 return (*srp->meth->passwd)(srp, password, passlen);
233}
234
235_TYPE( SRP_RESULT )
236SRP_gen_pub(SRP * srp, cstr ** result)
237{
238 return (*srp->meth->genpub)(srp, result);
239}
240
241_TYPE( SRP_RESULT )
242SRP_add_ex_data(SRP * srp, const unsigned char * data, int datalen)
243{
244 cstr_appendn(srp->ex_data, (const char*)data, datalen);
245 return SRP_SUCCESS;
246}
247
248_TYPE( SRP_RESULT )
249SRP_compute_key(SRP * srp, cstr ** result,
250 const unsigned char * pubkey, int pubkeylen)
251{
252 return (*srp->meth->key)(srp, result, pubkey, pubkeylen);
253}
254
255_TYPE( SRP_RESULT )
256SRP_verify(SRP * srp, const unsigned char * proof, int prooflen)
257{
258 return (*srp->meth->verify)(srp, proof, prooflen);
259}
260
261_TYPE( SRP_RESULT )
262SRP_respond(SRP * srp, cstr ** proof)
263{
264 return (*srp->meth->respond)(srp, proof);
265}
266
267_TYPE( SRP_RESULT )
268SRP_use_engine(const char * engine)
269{
270 if(BigIntegerOK(BigIntegerUseEngine(engine)))
271 return SRP_SUCCESS;
272 else
273 return SRP_ERROR;
274}