forked from zhaozg/lua-openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint.c
More file actions
115 lines (92 loc) · 2.93 KB
/
point.c
File metadata and controls
115 lines (92 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/***
EC_POINT module for Lua OpenSSL binding.
This module provides a complete wrapper for OpenSSL's EC_POINT operations,
enabling elliptic curve point mathematical operations.
@module ec.point
@usage
point = require('openssl').ec.point
*/
/* This file is included in ec.c */
#define MYTYPE_POINT "openssl.ec_point"
#define MYVERSION_POINT MYTYPE_POINT " library for " LUA_VERSION " / Nov 2024"
/***
Create a new EC point on a given group.
@function new
@tparam openssl.ec_group group the EC group
@treturn openssl.ec_point new elliptic curve point (at infinity)
@usage
group = require('openssl').group
point = require('openssl').point
g = group.new('prime256v1')
p = point.new(g)
*/
/***
Copy one EC point to another.
@function copy
@tparam openssl.ec_point dest destination point
@tparam openssl.ec_point src source point
@treturn openssl.ec_point destination point (self)
*/
int openssl_point_copy(lua_State *L)
{
EC_POINT *dest = CHECK_OBJECT(1, EC_POINT, MYTYPE_POINT);
const EC_POINT *src = CHECK_OBJECT(2, EC_POINT, MYTYPE_POINT);
if (EC_POINT_copy(dest, src)) {
lua_pushvalue(L, 1);
return 1;
}
return 0;
}
int openssl_point_free(lua_State *L)
{
EC_POINT *point = CHECK_OBJECT(1, EC_POINT, MYTYPE_POINT);
EC_POINT_free(point);
return 0;
}
/***
Convert EC point to string (internal, called by __tostring).
@function tostring
@treturn string string representation
*/
static int openssl_point_tostring(lua_State *L)
{
lua_pushfstring(L, "openssl.ec_point: %p", lua_touserdata(L, 1));
return 1;
}
/* Method table */
static luaL_Reg point_methods[] = {
/* Object methods */
{"copy", openssl_point_copy},
/* Metamethods */
{"__gc", openssl_point_free},
{"__tostring", auxiliar_tostring},
{NULL, NULL}
};
/* Module functions */
static luaL_Reg point_functions[] = {
{"new", openssl_group_point_new},
{"dup", openssl_group_point_dup},
{"equal", openssl_group_point_equal},
{"add", openssl_point_add},
{"dbl", openssl_point_dbl},
{"invert", openssl_point_invert},
{"mul", openssl_point_mul},
{"is_at_infinity", openssl_point_is_at_infinity},
{"is_on_curve", openssl_point_is_on_curve},
{"point2oct", openssl_group_point2oct},
{"oct2point", openssl_group_oct2point},
{"point2bn", openssl_group_point2bn},
{"bn2point", openssl_group_bn2point},
{"point2hex", openssl_group_point2hex},
{"hex2point", openssl_group_hex2point},
{"affine_coordinates", openssl_group_affine_coordinates},
{"set_to_infinity", openssl_point_set_to_infinity},
{NULL, NULL}
};
int
luaopen_ec_point(lua_State *L) {
auxiliar_newclass(L, MYTYPE_POINT, point_methods);
lua_newtable(L);
luaL_setfuncs(L, point_functions, 0);
return 1;
}