Skip to content

Commit a066474

Browse files
committed
srp: refactor srp module, and document
1 parent 66cd473 commit a066474

File tree

2 files changed

+115
-21
lines changed

2 files changed

+115
-21
lines changed

src/srp.c

+113-18
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,67 @@
1+
/***
2+
srp module to handle secure remote password.
3+
Provide srp_gn as lua object.
4+
5+
@module srp
6+
@usage
7+
srp = require('openssl').srp
8+
*/
19
#include "openssl.h"
210
#include "private.h"
311

412
#ifndef OPENSSL_NO_SRP
513
#include <openssl/srp.h>
614
#include <openssl/bn.h>
715

8-
/* server side */
16+
/***
17+
Gets the default SRP_gN object.
18+
@function get_default_gN
19+
@tparam string id SRP_gN ID
20+
@treturn openssl.srp_gn GN SRP_gN object
21+
*/
22+
static int openssl_srp_get_default_gN(lua_State *L)
23+
{
24+
const char *id = luaL_checkstring(L, 1);
25+
SRP_gN *GN = SRP_get_default_gN(id);
26+
if(GN)
27+
PUSH_OBJECT(GN, "openssl.srp_gn");
28+
else
29+
lua_pushnil(L);
30+
return 1;
31+
}
32+
33+
/***
34+
Calculates the x value.
35+
@function calc_x
36+
@tparam openssl.bn s Salt
37+
@tparam string username Username
38+
@tparam string password Password
39+
@treturn openssl.bn x Value
40+
*/
41+
static int openssl_srp_calc_x(lua_State *L)
42+
{
43+
BIGNUM *s = CHECK_OBJECT(1, BIGNUM, "openssl.bn");
44+
const char *username = luaL_checkstring(L, 2);
45+
const char *password = luaL_checkstring(L, 3);
46+
47+
BIGNUM *x = SRP_Calc_x(s, username, password);
48+
PUSH_OBJECT(x, "openssl.bn");
49+
return 1;
50+
}
51+
52+
/***
53+
openssl.srp_gn class.
54+
@type srp_gn
55+
*/
56+
57+
/***
58+
Creates an SRP verifier.
59+
@function create_verifier
60+
@tparam string username Username
61+
@tparam string servpass Service password
62+
@treturn openssl.bn salt Salt
63+
@treturn openssl.bn verifier Verifier
64+
*/
965
static int openssl_srp_create_verifier(lua_State *L)
1066
{
1167
const SRP_gN *GN = CHECK_OBJECT(1, SRP_gN, "openssl.srp_gn");
@@ -29,6 +85,14 @@ static int openssl_srp_create_verifier(lua_State *L)
2985
#define BN_RAND_BOTTOM_ANY 0
3086
#endif
3187

88+
/***
89+
Calculates the server's B value.
90+
@function calc_b
91+
@tparam openssl.bn v Verifier
92+
@tparam[opt] int bits Number of random bits, default is 256
93+
@treturn openssl.bn Bpub Server public key
94+
@treturn openssl.bn Brnd Server random number
95+
*/
3296
static int openssl_srp_calc_b(lua_State *L)
3397
{
3498
int ret = 0;
@@ -61,6 +125,15 @@ static int openssl_srp_calc_b(lua_State *L)
61125
return ret;
62126
}
63127

128+
/***
129+
Calculates the server's key.
130+
@function calc_server_key
131+
@tparam openssl.bn Apub Client public key
132+
@tparam openssl.bn v Verifier
133+
@tparam openssl.bn u Random number u
134+
@tparam openssl.bn Brnd Server random number
135+
@treturn openssl.bn Kserver Server key
136+
*/
64137
static int openssl_srp_calc_server_key(lua_State *L)
65138
{
66139
const SRP_gN *GN = CHECK_OBJECT(1, SRP_gN, "openssl.srp_gn");
@@ -76,6 +149,13 @@ static int openssl_srp_calc_server_key(lua_State *L)
76149
}
77150

78151
/* client side */
152+
/***
153+
Calculates the client's A value.
154+
@function calc_a
155+
@tparam[opt] int bits Number of random bits, default is 256
156+
@treturn openssl.bn Apub Client public key
157+
@treturn openssl.bn Arnd Client random number
158+
***/
79159
static int openssl_srp_calc_a(lua_State *L)
80160
{
81161
int ret = 0;
@@ -107,17 +187,36 @@ static int openssl_srp_calc_a(lua_State *L)
107187
return ret;
108188
}
109189

110-
static int openssl_srp_calc_x(lua_State *L)
190+
/* close https://github.com/zhaozg/lua-openssl/issues/312 */
191+
/***
192+
Calculates the x value.
193+
@function calc_x
194+
@tparam openssl.bn s Salt
195+
@tparam string username Username
196+
@tparam string password Password
197+
@treturn openssl.bn x Value
198+
*/
199+
static int openssl_srp_calc_X(lua_State *L)
111200
{
112-
BIGNUM *s = CHECK_OBJECT(1, BIGNUM, "openssl.bn");
113-
const char *username = luaL_checkstring(L, 2);
114-
const char *password = luaL_checkstring(L, 3);
201+
const SRP_gN *GN = CHECK_OBJECT(1, SRP_gN, "openssl.srp_gn");
202+
BIGNUM *s = CHECK_OBJECT(2, BIGNUM, "openssl.bn");
203+
const char *username = luaL_checkstring(L, 3);
204+
const char *password = luaL_checkstring(L, 4);
115205

116206
BIGNUM *x = SRP_Calc_x(s, username, password);
117207
PUSH_OBJECT(x, "openssl.bn");
118208
return 1;
119209
}
120210

211+
/***
212+
Calculates the client's key.
213+
@function calc_client_key
214+
@tparam openssl.bn Bpub Server public key
215+
@tparam openssl.bn x x Value
216+
@tparam openssl.bn Arnd Client random number
217+
@tparam openssl.bn u Random number u
218+
@treturn openssl.bn Kclient Client key
219+
*/
121220
static int openssl_srp_calc_client_key(lua_State *L)
122221
{
123222
const SRP_gN *GN = CHECK_OBJECT(1, SRP_gN, "openssl.srp_gn");
@@ -132,18 +231,13 @@ static int openssl_srp_calc_client_key(lua_State *L)
132231
return 1;
133232
}
134233

135-
/* both side */
136-
static int openssl_srp_get_default_gN(lua_State *L)
137-
{
138-
const char *id = luaL_checkstring(L, 1);
139-
SRP_gN *GN = SRP_get_default_gN(id);
140-
if(GN)
141-
PUSH_OBJECT(GN, "openssl.srp_gn");
142-
else
143-
lua_pushnil(L);
144-
return 1;
145-
}
146-
234+
/***
235+
Calculates the u value.
236+
@function calc_u
237+
@tparam openssl.bn Apub Client public key
238+
@tparam openssl.bn Bpub Server public key
239+
@treturn openssl.bn u Value
240+
*/
147241
static int openssl_srp_calc_u(lua_State *L)
148242
{
149243
const SRP_gN *GN = CHECK_OBJECT(1, SRP_gN, "openssl.srp_gn");
@@ -163,7 +257,7 @@ static luaL_Reg srp_funs[] =
163257

164258
/* client side */
165259
{"calc_a", openssl_srp_calc_a},
166-
{"calc_x", openssl_srp_calc_x},
260+
{"calc_x", openssl_srp_calc_X},
167261
{"calc_client_key", openssl_srp_calc_client_key},
168262

169263
/* server side */
@@ -180,6 +274,7 @@ static luaL_Reg srp_funs[] =
180274
static luaL_Reg R[] =
181275
{
182276
{"get_default_gN", openssl_srp_get_default_gN},
277+
{"calc_x", openssl_srp_calc_x},
183278

184279
{NULL, NULL}
185280
};

test/9.srp.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function TestSRP:test_4_Calc_u()
4040
end
4141

4242
function TestSRP:test_5_cli_key()
43-
local x = assert(GN.calc_x(self.salt, self.user, self.pass))
43+
local x = assert(GN:calc_x(self.salt, self.user, self.pass))
4444
self.Kclient = assert(GN:calc_client_key(self.Bpub, x, self.Arnd, self.u))
4545
end
4646

@@ -51,7 +51,7 @@ function TestSRP:test_6_srv_key()
5151
end
5252

5353
function TestSRP:test_7_cli_key()
54-
local x = assert(GN.calc_x(self.salt, self.user, self.pass .. '1'))
54+
local x = assert(srp.calc_x(self.salt, self.user, self.pass .. '1'))
5555
self.Kclient = assert(GN:calc_client_key(self.Bpub, x, self.Arnd, self.u))
5656
end
5757

@@ -60,4 +60,3 @@ function TestSRP:test_8_srv_key()
6060
self.Brnd))
6161
assert(Kserver ~= self.Kclient)
6262
end
63-

0 commit comments

Comments
 (0)