Skip to content

feat(crypto) add crypto.memcmp bindings#221

Merged
fffonion merged 4 commits into
fffonion:masterfrom
anvouk:master
Oct 14, 2025
Merged

feat(crypto) add crypto.memcmp bindings#221
fffonion merged 4 commits into
fffonion:masterfrom
anvouk:master

Conversation

@anvouk
Copy link
Copy Markdown
Contributor

@anvouk anvouk commented Oct 11, 2025

@anvouk anvouk marked this pull request as ready for review October 11, 2025 12:46
Comment thread lib/resty/openssl/crypto.lua Outdated
local _M = {}

function _M.memcmp(a, b, len)
if type(a) ~= "string" or type(b) ~= "string" or type(len) ~= "number" or len < 1 then
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution!
However if we are only supporting comparing strings, why don't we have a for loop in lua land to compare it. It can also be done in a way to avoid timing attack right 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, I defaulted to using openssl because it's a battle tested implementation and to avoid writing a potentially unsafe version myself. I should be able to make it more generic and support more types.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, support some specific types of cdata pointer should also be fine.

Copy link
Copy Markdown
Contributor Author

@anvouk anvouk Oct 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Should support all lua types buffer.encode supports. strings and cdata are treated as is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests should be fixed now.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah actually, by cdata i was referring to passing them as-is without encoding it.

the function would accepts two string, or two cdata (treated as opaque pointer) and a third argument size.

user can always encode abitrary data as string as pass in so we shouldn't do the encode on our side.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes It's already doing it that way. strings and cdata are not encoded but passed as-is. Everything else is encoded using the buffer built-in lib. You can also technically compare a cdata with a string, altough I'm not sure It could be useful.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, that's good, i misread sorry.
let's do remove the buffer encode part, and only keep string and cdata, anything else we can return an error.
let's also add in the test case to compare two cdata pointer, we can ffi.new a pair of char* and compare them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@fffonion
Copy link
Copy Markdown
Owner

thank you @anvouk this is good to go ! the CI looks broken, i will fix it and then merge your PR.

@fffonion
Copy link
Copy Markdown
Owner

actually let me merge this first, will fix it on master

@fffonion fffonion merged commit c66d7f2 into fffonion:master Oct 14, 2025
7 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants