Skip to content

Commit

Permalink
fixes and tests to p256 zencode
Browse files Browse the repository at this point in the history
typo branching for k

test file

check number of args before using lua stack
  • Loading branch information
albertolerda authored and jaromil committed Nov 21, 2023
1 parent 74e16df commit a553d2a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/zen_p256.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ static int p256_pubcheck(lua_State *L)

static int p256_sign(lua_State *L)
{
int n_args = lua_gettop(L);

BEGIN();
Z(L);
hash256 sha256;
Expand Down Expand Up @@ -143,7 +145,7 @@ static int p256_sign(lua_State *L)
}
sig->len = SIG_SIZE;

if(lua_isnoneornil(L, 3)) {
if(n_args > 2) {
k = o_arg(L, 3);
if(k == NULL) {
failed_msg = "Could not allocate ephemeral key";
Expand Down
1 change: 1 addition & 0 deletions test/lua/crypto.bats
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load ../bats_setup
Z big_arithmetics.lua
Z hash.lua
Z ecdh.lua
Z ecdsa_p256.lua
Z dh_session.lua
Z ecp_generic.lua
Z elgamal.lua
Expand Down
30 changes: 30 additions & 0 deletions test/lua/ecdsa_p256.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
P256 = require('p256')

alice_sk = P256.keygen()
I.spy(alice_sk)
alice_pk = P256.pubgen(alice_sk)
I.spy(alice_pk)

print(' DSA SIGN/VERIFY')

local m = O.from_str([[
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.]])
print 'iterate at least 100 tests of sign/verify'
print 'and at least 1 tests with r or s length lower than 32 bytes'
local tot = 0

while (tot<100) do
sig = P256.sign(alice_sk, m)
assert(P256.verify(alice_pk, sig, m), "ecdh verify failed")
assert(not P256.verify(alice_pk, sig, sha256(m)), "ecdh verify failed")
tot = tot+1
end

print "OK"

1 change: 1 addition & 0 deletions test/vectors/check_ecdsa_p256_sign.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ for line in newline_iter(DATA) do
end
if curr_fields == 7 then
assert(test.qx .. test.qy == P256.pubgen(test.d))
assert(P256.verify(test.qx .. test.qy, test.r .. test.s, test.msg))
assert(test.r .. test.s == P256.sign(test.d, test.msg, test.k))
curr_fields = 0
test = {}
Expand Down

0 comments on commit a553d2a

Please sign in to comment.