@@ -7,7 +7,7 @@ local C = ffi.C
7
7
require " resty.openssl.include.ecdsa"
8
8
local bn_lib = require " resty.openssl.bn"
9
9
local format_error = require (" resty.openssl.err" ).format_error
10
- local floor = math.floor
10
+ local ceil = math.ceil
11
11
12
12
local _M = {}
13
13
@@ -26,20 +26,25 @@ SEQUENCE {
26
26
The binary form is typically 64 bytes.
27
27
]]
28
28
29
- local function sig_size (ec_key )
30
- local sz = C .ECDSA_size (ec_key )
31
- if sz <= 8 then
32
- error (" failed to get ECDSA signature size " , 2 )
29
+ local function group_size (ec_key )
30
+ local group = C .EC_KEY_get0_group (ec_key )
31
+ if group == nil then
32
+ assert (" failed to get EC group " , 2 )
33
33
end
34
- -- 2 bytes for ASN.1 DER header, 2 bytes for length, 2 bytes for each 2 integers, left 2 integers so /2
35
- return (sz - 8 ) / 2
34
+
35
+ local sz = C .EC_GROUP_order_bits (group )
36
+ if sz <= 0 then
37
+ assert (" failed to get EC group order bits" , 2 )
38
+ end
39
+
40
+ return ceil (sz / 8 )
36
41
end
37
42
38
43
_M .sig_der2raw = function (der , ec_key )
39
44
if ec_key == nil then
40
45
error (" ec_key is required" , 2 )
41
46
end
42
- local psize = sig_size (ec_key )
47
+ local psize = group_size (ec_key )
43
48
44
49
local buf = ffi .new (" const unsigned char*" , der )
45
50
local buf_ptr = ffi .new (" const unsigned char*[1]" , buf )
@@ -82,11 +87,8 @@ _M.sig_raw2der = function(bin, ec_key)
82
87
if ec_key == nil then
83
88
error (" ec_key is required" , 2 )
84
89
end
85
- -- p521 private key x point is 65 bytes and y point is 66 bytes
86
- -- 65+66+8 = 139
87
- -- division by two results in a decimal and hence messes with
88
- -- signature length calculation
89
- local psize = floor (sig_size (ec_key ))
90
+
91
+ local psize = group_size (ec_key )
90
92
91
93
if # bin ~= psize * 2 then
92
94
return nil , " invalid signature length, expect " .. (psize * 2 ) .. " but got " .. # bin
0 commit comments