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

Incompatible with cryptopp #11

Closed
hcaihao opened this issue May 25, 2017 · 17 comments
Closed

Incompatible with cryptopp #11

hcaihao opened this issue May 25, 2017 · 17 comments

Comments

@hcaihao
Copy link

hcaihao commented May 25, 2017

I use the sample code to get "n e d q p", but cannot decrypt the cipher text by cryptopp.

local rsa = require "resty.nettle.rsa"
local r = rsa.new()
ngx.say("n:"..r.public:n())
ngx.say("e:"..r.public:e())
ngx.say("d:"..r.private:d())
ngx.say("p:"..r.private:p())
ngx.say("q:"..r.private:q())
local hex = require "resty.nettle.base16"
local r = rsa.new()
local gibb = r:encrypt("fish")
ngx.say(gibb)
local clear = r:decrypt(gibb)
ngx.say(clear)

Output:
n:b72e931e33293988ba371a3bc51d8be07878ead61928dd08f7a4f6fa5dbada2cd7e42a4278078e9cade85753df73856e049ebce7526b8ef5c23eb146e11f47d0bece456f46f3ef8ef9cd915b61ce406eb32571933f8a51bdbee60a11b7b603ce355cfdea9fbb65d650b4a4ac9c1d6280016aa64fae54fe93add285487b2f54c97f80ca80e747c06082cecaea07ec12b4ade6bbc7656c91e428c76f9d0b088e41eacdf59c70ba3c4d148cbccc911a7bc196157199ee18c3061bc7816be41208287a8e9056c371bfcbb687de21bcc5149e5080e2d1f0bd08161781598e4ebeecd48101a5b5266eff2711639d1a91ea2f8243560c14d798e2ee205249ce725cd5a133b846cfc960c3c7716d0fc47cf82b1e850777bad0b3c33a7e00f70adde47b4088021ba6b1b97ef8ede84b80d50a20e6e7f33bfc973985c1bcc42244c679300b003095c1c85f5c4aa44134e23465e54701836719857994d1ecbae4b1af5fa7aff76ff66ca546475b7da7cf08733a58b25048d875fa2b0e8d28e057524bef3118b276085cc4795d67ed3308a64b52a920a086d1426834830b7a0171d282383ecb8e70072e0406f690f359fdf32d6ae90198c87f8003ef3c67652865bfae07d83839d13c505208322c0b308eb1ea82cdc3ea9a17a8c46f18edc980007171d37137359f8b701a51dc5f05d33e77a7b9d593f789ae92395a249a80753e70995a4cf3
e:10001
d:33571a649c72316de0c310d72fa7d84c51779259ef3344bffea87d902e34f071211111c70f5d0edb5ae283973e49625c336f4a028b5719fa4736caf8d29fb1c10fb9403cafe62e7e2405397823316b5d9b58233ce1d22296cbaa2da84994f9fc1178beb1c3930237cee6d2408e80addab28af191e38a6ecf75284bb2c4d3bf2fba4c9e75025d59cdb2dbc283553515090eb45c6cc64067a34aca9abda919fcc4fcca3ccc5bcbd69ac3e7ae02a7a88486fd49854d5f2e117ae4ce45b7b7745655020472efde57432a153e7f437564c72297ed87a4cf942b7005a67b33275edb81b3a6828b02fa99089a1711312134419c80c2dd1ffa4d1f4d1e41f1edaa9fda3f99da654fccbf647e66f2b317d26315f7f8f6dd0e355784e5316232bbae40d211463d2a53e79680d8877dc8aba1e3af40ad0dabeeba6443835013f0c526879dc470b42c7eaf6b8c6d8ab7104fa540e4000c015004235eb5c908a93a4e6b4cf8121c4fae56d79b4a3693177b08d02872d0cc759d02798a45c4925a55d3fd958675262990d659561e5d6287f171cd470492229fd3e22e02af16b39735e63e8dde52aeb3f9ecdbfd4fd1c5b5a6c5e87b73aeeae028d041e94d8d3f61e4a36a4ac4c145f10d24614df14829a420601ce4c8be26bbde6c2684eb4b7a9dabb048af7713403be9c45b6e4052b11830cf7b0f05501c99b8dc5f165e7dccab5a08c1b92401
p:c1ae955ce14e79c49043884c11ef020befc02243c674cb8aeb5e91a28d5be88a393108da9ac9d76034566611ebd4c0ccc0fed8889a521172ab3a178decabd27cfcb8e49d3f7d09d15f799bddc4e8c41d7a817b8eda60b0244a9fa36f3b1b8142e51b214b29ac6ee126ee33157b3d3b09f33ffd2bedcd77b9c7dcd0feae004609431f8ea928d29938dd96d5e09824dc4cedaf8b2c1fbcf9792f684573df2ecb18a6e11f76f204edf9014f1e78e37692ae47e102bc0e915fb272c99cb92def6a8d8d8218b6b745b93761d102a6445c68f8beb65e7595f4c2365187bbf1d1fd1b89cd0a5b981b1031f3c72ced7d1ea8bd47b2a9a285a91fcd55b842c15f80acc3ab
q:f21f1cca83f50f2bc05dcb1d5bf16806c77816103489152acd67058c6d2034cd2926eb4fda950023c22dc4be98b75dcfb90a5703f49c8e84508bd062adeabdb8a08f5e5f044811024fdd854cd4f1d9ca4ecb1e4f14c20520b898fbcd147125fe2e676a7292180949c359f843fbc318665dd653d150deefd14241675a7b758d40c3a5fcd321b8b8c31e40eff5e28de2c6aa1ddf7b0d15036b73e1bce829e47a618d5ff4a32426e2eb7dfb16d7bc1c15830eafe8e9b7e8cbe40ab0588fa87bacd4728548c3c5c3ff5667a41722e0f3eaff230d932dc2c173081c69e86df25975c042e872bd2a422bbe6715578fc714a85da2d7b857280ce2232d4f6279126853d9
cipher:60825a64566db004c282188996f74d5d84b9fa7ef971155b220e8ba36eb262e68299480c5eb067d873b531ed077b00c7602c8126485d0bf34a54470865a76441ac39734a0dfd0d0879a5bb1e3034f0c4afc48b1472ea50b449cb5f9aaabb26250423c4bf78300792636d5d6d503d120da9154e0398194a8284ce2643ab266326ea4d629bb738937ed1617df7ee7bf7429942409d48781e1fceb1c2608af60fed42a37caaf0f35616bb41959d151a873e5f4565be8220f8b4058dc975ae77cd221ae2f66260300401a3e8e171ad658100ad7e239a9259f952e7599a1d0cdb0cdb4a792792e9d7a7fdb99ed4ac0fc752f1bc4785218a31642a631b95e402b8b6686a4690aa6e092af88c542bd0e62353c461bbdd956fe3018b17634b10c6f7c8767985dfc2e5e9d395af516ddd710444d2b39d2d03da405cd041a75850f02292a660e541a00f47a6938295ddbad84de448bc78deb767611f991c919da007614032cfa17f1fb40d145f1d5238e33a1de784e3e474ad80325b57598ad35283f4f438bbe67844fb070243ba0dd112fa39305888a44086fd12d7649d80d3d3bfa2b4e574ec177927c7ef3e9e1ec69662be16ca7922e5cb15d2484daad1cf12fde129281219d919b53aaabd8ff9d47852f7b6bfd76e8504eeb07a0fa4ca7b2561cadecbd6df3cd709dbe6bccbd2c74b21ef071093340e28e237e8f21e1ba78192ec8933
plain:fish
	AutoSeededRandomPool rng;
	InvertibleRSAFunction params;

	const CryptoPP::Integer n("b72e931e33293988ba371a3bc51d8be07878ead61928dd08f7a4f6fa5dbada2cd7e42a4278078e9cade85753df73856e049ebce7526b8ef5c23eb146e11f47d0bece456f46f3ef8ef9cd915b61ce406eb32571933f8a51bdbee60a11b7b603ce355cfdea9fbb65d650b4a4ac9c1d6280016aa64fae54fe93add285487b2f54c97f80ca80e747c06082cecaea07ec12b4ade6bbc7656c91e428c76f9d0b088e41eacdf59c70ba3c4d148cbccc911a7bc196157199ee18c3061bc7816be41208287a8e9056c371bfcbb687de21bcc5149e5080e2d1f0bd08161781598e4ebeecd48101a5b5266eff2711639d1a91ea2f8243560c14d798e2ee205249ce725cd5a133b846cfc960c3c7716d0fc47cf82b1e850777bad0b3c33a7e00f70adde47b4088021ba6b1b97ef8ede84b80d50a20e6e7f33bfc973985c1bcc42244c679300b003095c1c85f5c4aa44134e23465e54701836719857994d1ecbae4b1af5fa7aff76ff66ca546475b7da7cf08733a58b25048d875fa2b0e8d28e057524bef3118b276085cc4795d67ed3308a64b52a920a086d1426834830b7a0171d282383ecb8e70072e0406f690f359fdf32d6ae90198c87f8003ef3c67652865bfae07d83839d13c505208322c0b308eb1ea82cdc3ea9a17a8c46f18edc980007171d37137359f8b701a51dc5f05d33e77a7b9d593f789ae92395a249a80753e70995a4cf3h");
	const CryptoPP::Integer p("c1ae955ce14e79c49043884c11ef020befc02243c674cb8aeb5e91a28d5be88a393108da9ac9d76034566611ebd4c0ccc0fed8889a521172ab3a178decabd27cfcb8e49d3f7d09d15f799bddc4e8c41d7a817b8eda60b0244a9fa36f3b1b8142e51b214b29ac6ee126ee33157b3d3b09f33ffd2bedcd77b9c7dcd0feae004609431f8ea928d29938dd96d5e09824dc4cedaf8b2c1fbcf9792f684573df2ecb18a6e11f76f204edf9014f1e78e37692ae47e102bc0e915fb272c99cb92def6a8d8d8218b6b745b93761d102a6445c68f8beb65e7595f4c2365187bbf1d1fd1b89cd0a5b981b1031f3c72ced7d1ea8bd47b2a9a285a91fcd55b842c15f80acc3abh");
	const CryptoPP::Integer q("f21f1cca83f50f2bc05dcb1d5bf16806c77816103489152acd67058c6d2034cd2926eb4fda950023c22dc4be98b75dcfb90a5703f49c8e84508bd062adeabdb8a08f5e5f044811024fdd854cd4f1d9ca4ecb1e4f14c20520b898fbcd147125fe2e676a7292180949c359f843fbc318665dd653d150deefd14241675a7b758d40c3a5fcd321b8b8c31e40eff5e28de2c6aa1ddf7b0d15036b73e1bce829e47a618d5ff4a32426e2eb7dfb16d7bc1c15830eafe8e9b7e8cbe40ab0588fa87bacd4728548c3c5c3ff5667a41722e0f3eaff230d932dc2c173081c69e86df25975c042e872bd2a422bbe6715578fc714a85da2d7b857280ce2232d4f6279126853d9h");
	const CryptoPP::Integer d("33571a649c72316de0c310d72fa7d84c51779259ef3344bffea87d902e34f071211111c70f5d0edb5ae283973e49625c336f4a028b5719fa4736caf8d29fb1c10fb9403cafe62e7e2405397823316b5d9b58233ce1d22296cbaa2da84994f9fc1178beb1c3930237cee6d2408e80addab28af191e38a6ecf75284bb2c4d3bf2fba4c9e75025d59cdb2dbc283553515090eb45c6cc64067a34aca9abda919fcc4fcca3ccc5bcbd69ac3e7ae02a7a88486fd49854d5f2e117ae4ce45b7b7745655020472efde57432a153e7f437564c72297ed87a4cf942b7005a67b33275edb81b3a6828b02fa99089a1711312134419c80c2dd1ffa4d1f4d1e41f1edaa9fda3f99da654fccbf647e66f2b317d26315f7f8f6dd0e355784e5316232bbae40d211463d2a53e79680d8877dc8aba1e3af40ad0dabeeba6443835013f0c526879dc470b42c7eaf6b8c6d8ab7104fa540e4000c015004235eb5c908a93a4e6b4cf8121c4fae56d79b4a3693177b08d02872d0cc759d02798a45c4925a55d3fd958675262990d659561e5d6287f171cd470492229fd3e22e02af16b39735e63e8dde52aeb3f9ecdbfd4fd1c5b5a6c5e87b73aeeae028d041e94d8d3f61e4a36a4ac4c145f10d24614df14829a420601ce4c8be26bbde6c2684eb4b7a9dabb048af7713403be9c45b6e4052b11830cf7b0f05501c99b8dc5f165e7dccab5a08c1b92401h");
	const CryptoPP::Integer e("10001h");
	params.Initialize(n, e, d);

	RSA::PrivateKey privateKey(params);

	std::string recovered;

	   ////////////////////////////////////////////////
	   // Decryption
	CryptoPP::RSAES_OAEP_SHA_Decryptor _d(privateKey);

	StringSource ss2("60825a64566db004c282188996f74d5d84b9fa7ef971155b220e8ba36eb262e68299480c5eb067d873b531ed077b00c7602c8126485d0bf34a54470865a76441ac39734a0dfd0d0879a5bb1e3034f0c4afc48b1472ea50b449cb5f9aaabb26250423c4bf78300792636d5d6d503d120da9154e0398194a8284ce2643ab266326ea4d629bb738937ed1617df7ee7bf7429942409d48781e1fceb1c2608af60fed42a37caaf0f35616bb41959d151a873e5f4565be8220f8b4058dc975ae77cd221ae2f66260300401a3e8e171ad658100ad7e239a9259f952e7599a1d0cdb0cdb4a792792e9d7a7fdb99ed4ac0fc752f1bc4785218a31642a631b95e402b8b6686a4690aa6e092af88c542bd0e62353c461bbdd956fe3018b17634b10c6f7c8767985dfc2e5e9d395af516ddd710444d2b39d2d03da405cd041a75850f02292a660e541a00f47a6938295ddbad84de448bc78deb767611f991c919da007614032cfa17f1fb40d145f1d5238e33a1de784e3e474ad80325b57598ad35283f4f438bbe67844fb070243ba0dd112fa39305888a44086fd12d7649d80d3d3bfa2b4e574ec177927c7ef3e9e1ec69662be16ca7922e5cb15d2484daad1cf12fde129281219d919b53aaabd8ff9d47852f7b6bfd76e8504eeb07a0fa4ca7b2561cadecbd6df3cd709dbe6bccbd2c74b21ef071093340e28e237e8f21e1ba78192ec8933", true,
		new HexDecoder(new CryptoPP::PK_DecryptorFilter(rng, _d,
			new StringSink(recovered)
		)) // PK_DecryptorFilter
	); // StringSource

Got "CryptoPP::InvalidCiphertext" error.

@bungle
Copy link
Owner

bungle commented May 25, 2017

It looks like you are encrypting with Nettle's RSA (v.1.5) and decrypting with RSA OAEP (v.2.0). I welcome contributions for OAEP in lua-resty-nettle. I need to check if Nettle provides any helpers for it.

@bungle
Copy link
Owner

bungle commented May 25, 2017

Does CryptoPP Support v1.5 padding that Nettle is using (OAEP is v2.0)?

@hcaihao
Copy link
Author

hcaihao commented May 25, 2017

Sorry, I'm newer to rsa, but I think cryptopp is support PKCS v1.5 padding.

@hcaihao
Copy link
Author

hcaihao commented May 25, 2017

According to your hint, I change the CryptoPP::RSAES_OAEP_SHA_Decryptor to CryptoPP::RSAES_PKCS1v15_Decryptor, it works!

And now, How to use cryptopp rsa keys in lua-resty-nettle? The fixed values(n = 4096, e=65537) in rsa.lua is unsafe.

	/////////////////////////////////////
	 Pseudo Random Number Generator
	AutoSeededRandomPool rng;

	///////////////////////////////////////
	// Generate Parameters
	InvertibleRSAFunction params;
	params.GenerateRandomWithKeySize(rng, 1024);

	///////////////////////////////////////
	// Generated Parameters
	const CryptoPP::Integer& n = params.GetModulus();
	const CryptoPP::Integer& p = params.GetPrime1();
	const CryptoPP::Integer& q = params.GetPrime2();
	const CryptoPP::Integer& d = params.GetPrivateExponent();
	const CryptoPP::Integer& e = params.GetPublicExponent();

	///////////////////////////////////////
	// Dump
	std::cout << "RSA Parameters:" << std::endl;
	std::cout << " n: " << n << std::endl;
	std::cout << " p: " << p << std::endl;
	std::cout << " q: " << q << std::endl;
	std::cout << " d: " << d << std::endl;
	std::cout << " e: " << e << std::endl;
	std::cout << std::endl;

@bungle
Copy link
Owner

bungle commented May 25, 2017

By default please supply the parameters as base16 encoded bytes as a string. See this:
https:/bungle/lua-resty-nettle/blob/master/lib/resty/nettle/rsa.lua#L104
which calls to this:
https:/bungle/lua-resty-nettle/blob/master/lib/resty/nettle/mpz.lua#L49

Also check this:
https://gmplib.org/manual/Assigning-Integers.html

I think for you it is easiest to std:cout the keys from CryptoPP as base16 (hex encoded string), your dump section, and then just feed those to lua-resty-nettle's keys.

@bungle
Copy link
Owner

bungle commented May 25, 2017

@bungle
Copy link
Owner

bungle commented May 25, 2017

the fixed values:
https:/bungle/lua-resty-nettle/blob/master/lib/resty/nettle/rsa.lua#L184-L185

are not the fixed values for actual n and e. Well for e it is. And it is a safe value. But for n it just tells its size. Not the actual value, so it defaults to 4096 bits, which is safe. But this is just a key generating function. Depending on your needs, you should actually just create

public key:
https:/bungle/lua-resty-nettle/blob/master/lib/resty/nettle/rsa.lua#L66

and / or private key:
https:/bungle/lua-resty-nettle/blob/master/lib/resty/nettle/rsa.lua#L100

Usually you need only one of the keys (e.g. your code verifies signatures, you will only need public key, or you are only decrypting, then you will only need private key).

@bungle
Copy link
Owner

bungle commented May 25, 2017

And yes, I need to update the example, as quite a lot has happened with this library recently.

@bungle
Copy link
Owner

bungle commented May 25, 2017

you can also supply the base attribute which default to 16 to give key parameters in different base. E.g. base 10.

@hcaihao
Copy link
Author

hcaihao commented May 26, 2017

For use customed public & private key, I modify the rsa.lua, from

local public = {}
public.__index = public
...
local private = {}
private.__index = private

to

public = {}
public.__index = public
...
private = {}
private.__index = private

The test code cannot decrypt successfully, However, the cryptopp is ok.

local n = "b55b4f0bcf4f7c581825fbd2b9e226fac644728e1e1a714b081c4f293e3b31a7bed5eeb7c0632503a13fb640efcbe7f0be2e60ecdc8e4cc6cafbee1358c65b6ebfa6cf2d5fb2a5b1b8c0125c012cc796be6f03cdc2b3b6032a05886be48e336bfca1d1d27259017fa6656d9cb18848eddd8e92626d4d59db2cf6570d0889b87b";
local p = "d00a2268dc13e896f651c265ba7d869b89b5dbd8dfa9032f17d14d64686772b914eff1313ee7d8694f296584955c44ac1511ba709e3f7d73daa319cf21ad3f0b";
local q = "df2a6e057f41903c7260c0641a747bf32da8c27d8a124c5a8fc62768ea7513816d41416e231780d61562429618ed6c107618e844ce31768521eaf494cd1e1251";
local d = "8f6fea7312c2713c53cd27012217e6708d83f17ec9fc6c02c6256dced6f386708015403b8124695017a7068511a41a8c643ed656409323d24f1536d5826f503c50b7ab3cf53ca492288ab135115a621fd946749a127a54c26210a1dfd6a7caadc9836dc6bb75ce07148e231293725c79160b08b3b26c9f0068fcf20b1cdb6541";
local e = "10001";
local rsa = require "resty.nettle.rsa"
local pux = public.new(n,e,16)
local pri = private.new(d, p, q, nil, nil, nil, 16)
local r = rsa.new(pux, pri)
local gibb = r:encrypt("fish")
ngx.say(gibb)
local clear = r:decrypt(gibb)
ngx.say(clear)

Outout:
1fabfb19c26772facf3b4257e5253790103e26273227b3d42ddffd6d06a0b763d9237313151521f74ccbe414e2dcf738e0be049d9d69b70c3bcadee9f1b7dd649b8b64215c1919069fc1654dd1f51ddc5128ac33d1b76386bbfa39bb7d21869746f6e4a3a1ba09936099c63ff0bc720e35631299565b2da1b1a094429b5b5e01
nil -- should be fish
const CryptoPP::Integer n("b55b4f0bcf4f7c581825fbd2b9e226fac644728e1e1a714b081c4f293e3b31a7bed5eeb7c0632503a13fb640efcbe7f0be2e60ecdc8e4cc6cafbee1358c65b6ebfa6cf2d5fb2a5b1b8c0125c012cc796be6f03cdc2b3b6032a05886be48e336bfca1d1d27259017fa6656d9cb18848eddd8e92626d4d59db2cf6570d0889b87bh");
const CryptoPP::Integer p("d00a2268dc13e896f651c265ba7d869b89b5dbd8dfa9032f17d14d64686772b914eff1313ee7d8694f296584955c44ac1511ba709e3f7d73daa319cf21ad3f0bh");
const CryptoPP::Integer q("df2a6e057f41903c7260c0641a747bf32da8c27d8a124c5a8fc62768ea7513816d41416e231780d61562429618ed6c107618e844ce31768521eaf494cd1e1251h");
const CryptoPP::Integer d("8f6fea7312c2713c53cd27012217e6708d83f17ec9fc6c02c6256dced6f386708015403b8124695017a7068511a41a8c643ed656409323d24f1536d5826f503c50b7ab3cf53ca492288ab135115a621fd946749a127a54c26210a1dfd6a7caadc9836dc6bb75ce07148e231293725c79160b08b3b26c9f0068fcf20b1cdb6541h");
const CryptoPP::Integer e("10001h");

AutoSeededRandomPool rng;
InvertibleRSAFunction params;

params.Initialize(n, e, d);
RSA::PrivateKey privateKey(params);

std::string plain;

CryptoPP::RSAES_PKCS1v15_Decryptor _d(privateKey);	//RSAES_OAEP_SHA_Decryptor

StringSource ss2("1fabfb19c26772facf3b4257e5253790103e26273227b3d42ddffd6d06a0b763d9237313151521f74ccbe414e2dcf738e0be049d9d69b70c3bcadee9f1b7dd649b8b64215c1919069fc1654dd1f51ddc5128ac33d1b76386bbfa39bb7d21869746f6e4a3a1ba09936099c63ff0bc720e35631299565b2da1b1a094429b5b5e01", true,
	new HexDecoder(new CryptoPP::PK_DecryptorFilter(rng, _d,
		new StringSink(plain)
	)) // PK_DecryptorFilter
); // StringSource

std::cout << plain << std::endl;

Outout:
fish

@bungle
Copy link
Owner

bungle commented May 26, 2017

No, do not modify. It is totally unneeded.

just use, e.g.:

local pub, err = rsa.key.public.new(n, e)

@hcaihao
Copy link
Author

hcaihao commented May 26, 2017

Ok, why decrypt result is nil?

@hcaihao
Copy link
Author

hcaihao commented May 27, 2017

The problem has been found: a,b,c cannot be omitted. It's different from cryptopp.

@hcaihao hcaihao closed this as completed May 27, 2017
@bungle
Copy link
Owner

bungle commented May 27, 2017

Good, I guessed that it might be the case, but did't have time to test.

@hcaihao
Copy link
Author

hcaihao commented May 27, 2017

Maybe you can add some codes in function "private.new(d, p, q, a, b, c, base)" to calc the a,b,c if them are nil.

a = d % (p - 1)
b = d % (q - 1)
c = q.InverseMod(p)

@bungle
Copy link
Owner

bungle commented May 29, 2017

@hcaihao, just to let you know, the latest commit includes auto calculation of a, b, and c in case when they are not provided but the neccessary parameters to count them are:
dd41916

@hcaihao
Copy link
Author

hcaihao commented May 29, 2017

Good work! I'll test it later, If any problem, I'll post here.

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

No branches or pull requests

2 participants