Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use origin input key by default #46

Closed
wants to merge 10 commits into from
Closed

Conversation

haorenfsa
Copy link

this patch is mean to fix issues like #28, #19
and it's now compatite with openssl shell command and php
note it's not related to pull request #35 : it's only about the key, not data.

things might concern :

  1. to compatite with former aes.lua version, one has to explicit write what hash method he/she need when using new() function, which is by default _M.hash.md5;

eg:

aes = require "resty.aes"
aes:new("key", "salt", some_cipher, hash.md5, ....)

  1. readme might need to update

@haorenfsa
Copy link
Author

eh, I'll take a look at test problems later

@agentzh
Copy link
Member

agentzh commented Aug 21, 2017

@haorenfsa This would break backward compatibility, right? Better avoid breaking it even though the current impl is wrong.

@agentzh
Copy link
Member

agentzh commented Aug 21, 2017

@chase Will you please review this PR? @haorenfsa found that you mistakenly use the MD5 digest of the user key instead of the user key itself in the resty.aes module.

@haorenfsa
Copy link
Author

@agentzh okey, I'll check it later

@haorenfsa
Copy link
Author

the CI test seems passed, but still showing pending

@agentzh
Copy link
Member

agentzh commented Aug 25, 2017

@haorenfsa Yeah, seems like a github or travis bug.

if option == options.default then
if type(_hash) == "table" then
if not _hash.iv or #_hash.iv ~= 16 then
return nil, "bad iv"
Copy link
Member

Choose a reason for hiding this comment

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

We should use 4-space indentations consistently. Please fix other similar places, if any.

@@ -293,3 +293,24 @@ failed to new: bad iv
--- no_error_log
[error]

Copy link
Member

Choose a reason for hiding this comment

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

We always use 3 blank lines to separate test blocks. Please use the reindex script to auto-format this .t file. Thank you.

local options = {
default = 0x01,
key_without_gen = 0x02
-- may add other things like padding mode; or use bit 'or/and' operation for combined option
Copy link
Member

Choose a reason for hiding this comment

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

This line is too long, exceeding 80 columns.


local options = {
default = 0x01,
key_without_gen = 0x02
Copy link
Member

Choose a reason for hiding this comment

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

Better add a trailing comma to simplify adding more options in the future.

Copy link
Member

Choose a reason for hiding this comment

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

Also, the option name"key_without_gen" is confusing. What does it mean?

BTW, shall we add some docs to README for this change or feature?

Copy link
Author

Choose a reason for hiding this comment

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

about "key_without_gen" option
By default, new() function in aes.lua uses EVP_BytesToKey in openssl to generate key and iv from input key together with salt and etc by md5. Use generated key can avoid some attacks.
But sometimes people just want to use simple key without iv and generation instead, for compatibility or other reason.
so how should we name it? use_origin_key?

about docs
Of course we should. It takes me quite a while when I crash into this.

Copy link
Member

Choose a reason for hiding this comment

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

@haorenfsa use_raw_key might be a better name? Also, maye we do not need the default option at all?

Copy link
Author

Choose a reason for hiding this comment

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

yeah,it's better. And default can be replaced with nil input

end

-- note it's key padding not data padding, and it use zero padding only;
key = key..string.rep("\0", _cipherLength - #key)
Copy link
Member

Choose a reason for hiding this comment

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

Please avoid this global variable string. We should avoid use of Lua globals on such Lua hot code paths. We should write

local str_rep = string.rep

function blah(...)
    str_rep(...)
end

This way we also save 2 table looks on hot code paths, one is the table lookup of the key "string" in the global environment table, and the other is the lookup of the key "rep" in the "string" table.

@hy05190134
Copy link

hy05190134 commented Sep 7, 2017

LGTM, and I apply the patch for my aes.lua

@wenxzhen
Copy link

which version will include this change?

@chase
Copy link
Contributor

chase commented Nov 28, 2017

If I recall correctly, the use of MD5 was for compatibility of some library or language which I cannot place exactly.

Security-wise, what I committed is quite a big mistake, even if it was for the sake of compatibility. If this matches the valid behavior of the most commonly integrated tools/languages, it should be merged.

That said, I do not have time to help maintain the code I contributed or do code reviews, unfortunately.

@haorenfsa
Copy link
Author

almost forgot about this patch... I'll commit a update for tutorial in this patch later this week, and is there anything else need to be altered? @agentzh

@wenxzhen my team has been using this patch in production environment for several months, and it is doing well.

@wenxzhen
Copy link

wenxzhen commented Nov 28, 2017 via email

add example of using "use_raw_key" option
@haorenfsa
Copy link
Author

@agentzh plz review again when you can spare some time? thanks for the attention

@agentzh
Copy link
Member

agentzh commented Dec 7, 2017

@haorenfsa I'll look into this as soon as I can manage. Sorry for the delay on my side.

@fancy-rabbit
Copy link

fancy-rabbit commented Mar 25, 2020

same problem. I'm trying to decrypt data encrypted by crypto-js(AES-256, ECB) and worked around it with the following code
local cipher = aes:new(key_encryption_key, nil, aes.cipher(256, 'ecb'), {method = function(key)return key end, iv = string.rep(string.char(0), 16)})
it will always call _hash.method (default MD5) on the key, thus a dummy method function may worked well. (although IV is not used in ECB mode but a dummy one should be provided, too)

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.

7 participants