-
Notifications
You must be signed in to change notification settings - Fork 5
/
pguecc--1.0.sql
135 lines (109 loc) · 3.56 KB
/
pguecc--1.0.sql
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
\echo Use "CREATE EXTENSION pgcrypto" to load this file. \quit
--
-- ecdsa_sign
--
CREATE FUNCTION ecdsa_sign_raw(bytea, bytea, text)
RETURNS bytea
AS 'MODULE_PATHNAME', 'pg_ecdsa_sign_raw'
LANGUAGE C STRICT;
CREATE FUNCTION ecdsa_sign(private_key_hex text, input_data bytea, hash_func text, curve_name text) RETURNS text AS $$
SELECT encode(ecdsa_sign_raw(
decode(private_key_hex, 'hex'),
digest(input_data, hash_func),
curve_name
), 'hex');
$$ LANGUAGE SQL STRICT;
CREATE FUNCTION ecdsa_sign(private_key bytea, input_data bytea, hash_func text, curve_name text) RETURNS text AS $$
SELECT encode(ecdsa_sign_raw(
private_key,
digest(input_data, hash_func),
curve_name
), 'hex');
$$ LANGUAGE SQL STRICT;
--
-- ecdsa_verify
--
CREATE FUNCTION ecdsa_verify_raw(bytea, bytea, bytea, text)
RETURNS boolean
AS 'MODULE_PATHNAME', 'pg_ecdsa_verify_raw'
LANGUAGE C STRICT;
CREATE FUNCTION ecdsa_verify(public_key_hex text, input_data bytea, signature_hex text, hash_func text, curve_name text) RETURNS boolean AS $$
SELECT ecdsa_verify_raw(
decode(public_key_hex, 'hex'),
digest(input_data, hash_func),
decode(signature_hex, 'hex'),
curve_name
);
$$ LANGUAGE SQL STRICT;
CREATE FUNCTION ecdsa_verify(public_key_hex text, input_data bytea, signature bytea, hash_func text, curve_name text) RETURNS boolean AS $$
SELECT ecdsa_verify_raw(
decode(public_key_hex, 'hex'),
digest(input_data, hash_func),
signature,
curve_name
);
$$ LANGUAGE SQL STRICT;
CREATE FUNCTION ecdsa_verify(public_key bytea, input_data bytea, signature_hex text, hash_func text, curve_name text) RETURNS boolean AS $$
SELECT ecdsa_verify_raw(
public_key,
digest(input_data, hash_func),
decode(signature_hex, 'hex'),
curve_name
);
$$ LANGUAGE SQL STRICT;
CREATE FUNCTION ecdsa_verify(public_key bytea, input_data bytea, signature bytea, hash_func text, curve_name text) RETURNS boolean AS $$
SELECT ecdsa_verify_raw(
public_key,
digest(input_data, hash_func),
signature,
curve_name
);
$$ LANGUAGE SQL STRICT;
--
-- ecdsa_is_valid_public_key
--
CREATE FUNCTION ecdsa_is_valid_public_key(bytea, text)
RETURNS boolean
AS 'MODULE_PATHNAME', 'pg_ecdsa_is_valid_public_key'
LANGUAGE C STRICT;
CREATE FUNCTION ecdsa_is_valid_public_key(public_key_hex text, curve_name text) RETURNS boolean AS $$
SELECT ecdsa_is_valid_public_key(
decode(public_key_hex, 'hex'),
curve_name
);
$$ LANGUAGE SQL STRICT;
--
-- ecdsa_is_valid_private_key
--
CREATE FUNCTION ecdsa_is_valid_private_key(bytea, text)
RETURNS boolean
AS 'MODULE_PATHNAME', 'pg_ecdsa_is_valid_private_key'
LANGUAGE C STRICT;
CREATE FUNCTION ecdsa_is_valid_private_key(private_key_hex text, curve_name text) RETURNS boolean AS $$
SELECT ecdsa_is_valid_private_key(
decode(private_key_hex, 'hex'),
curve_name
);
$$ LANGUAGE SQL STRICT;
--
-- ecdsa_is_valid_public_key
--
CREATE FUNCTION ecdsa_is_valid_curve(text)
RETURNS boolean
AS 'MODULE_PATHNAME', 'pg_ecdsa_is_valid_curve'
LANGUAGE C STRICT;
--
-- ecdsa_make_key
--
CREATE FUNCTION ecdsa_make_key_raw(text)
RETURNS bytea[]
AS 'MODULE_PATHNAME', 'pg_ecdsa_make_key_raw'
LANGUAGE C STRICT;
CREATE FUNCTION ecdsa_make_key(curve text, OUT public_key text, OUT private_key text) AS $$
DECLARE
key_raw bytea[] := ecdsa_make_key_raw(curve);
BEGIN
public_key := encode(key_raw[1], 'hex');
private_key := encode(key_raw[2], 'hex');
END
$$ LANGUAGE plpgsql STRICT;