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

Scrypt producing some weird hash. #225

Closed
bfamzz opened this issue Dec 28, 2019 · 11 comments
Closed

Scrypt producing some weird hash. #225

bfamzz opened this issue Dec 28, 2019 · 11 comments

Comments

@bfamzz
Copy link

bfamzz commented Dec 28, 2019

I am working on a C++ project that requires some hashing.

I included the scrypt header file in my C++ code as follows:

extern "C" { #include "scrypt-kdf.h" }

/* Parameters  controlling memory usage and cpu time --scrypt-kdf */
#define N 16384
#define r 8
#define p 1
#define OUTPUT_BUFLEN 64

I have a function declaration as follows:

int hashPassword(const char* password, const char* salt, uint8_t hashedPasswordOutput[]);

The function definition is as follows:

int hashPassword(const char* password, const char* salt, uint8_t hashedPasswordOutput[]) {
	int exitCode = scrypt_kdf((const uint8_t*)password, strlen(password),
		(const uint8_t*)salt, strlen(salt), N, r, p, hashedPasswordOutput, OUTPUT_BUFLEN);
	
        // Logging
	std::cout << "Password is: " << (const uint8_t*)password << "\n";
	std::cout << "Salt is: " << (const uint8_t*)salt << "\n";
	for (int i = 0; i < OUTPUT_BUFLEN; ++i)
	{
		std::cout << hashedPasswordOutput[i] << "\n";
	}
	std::cout << "Exit code is: " << exitCode << "\n";
	return exitCode;
}

The problem is I am getting some really weird hash. Output is:
HashedPasswordString is: 8���i��/��} Y��GWg�0GNEv��ԥP������ݺLJ5�y\j;ExqZRk�U��������`

See Screenshot below:
image

### More Details:
I am running the project on docker using the GCC:9.1.0 image.

How do I resolve this to get the right hash? Thanks.

@cperciva
Copy link
Member

That probably is the correct hash. An scrypt hash is 64 bytes of binary data in this case. You probably want to base64 encode it?

@bfamzz
Copy link
Author

bfamzz commented Dec 29, 2019

Thanks @cperciva for your response.

When I tried to encode it using base64 (I utilized this online tool: https://www.base64encode.org/) the output is: OO+/ve+/ve+/vWnvv73vv70v77+977+9fSBZ77+977+9R1dn77+9MEdORXbvv73vv73UpVDvv73vv73vv73vv73vv73vv73duseHNe+/vXlcajtFeHFaUmvvv71V77+977+977+977+977+977+977+977+9

The output above looks really different (pattern-wise) from the one below which I got from printing the output of the "sample-libscrypt-kdf.c" in the repo. Here's what I got:
27169184123166136170197227130143013713816221213321348125208142376315117024458552161571942024652220241164142187561941551623916725325342173643344144216482182015271219254152
without encoding in base64.
Note: parameters used in the repo example are (N,r,p, output_buflen) (16384, 8, 1, 64). I changed the output_buflen from 8 to 64.

Here is a screenshot:
image

Encoding the above output in base64 using the aforementioned online tool yields:
MjcxNjkxODQxMjMxNjYxMzYxNzAxOTcyMjcxMzAxNDMwMTM3MTM4MTYyMjEyMTMzMjEzNDgxMjUyMDgxNDIzNzYzMTUxMTcwMjQ0NTg1NTIxNjE1NzE5NDIwMjQ2NTIyMjAyNDExNjQxNDIxODc1NjE5NDE1NTE2MjM5MTY3MjUzMjUzNDIxNzM2NDMzNDQxNDQyMTY0ODIxODIwMTUyNzEyMTkyNTQxNTI=

What are your thoughts please? Thanks.

@gperciva
Copy link
Member

I doubt that copy&pasting binary data from a terminal window into a website will do what you want. I recommend that you do the base64 conversion before printing anything.

I'm not certain how you produced that string from sample-libscrypt-kdf.c, so it may or may not be meaningful.

(Separate note: it's not a great idea to run everything as root.)

@bfamzz
Copy link
Author

bfamzz commented Dec 29, 2019

Thanks @gperciva for the hint. I'll create a user with non-root access.

To produce that string from sample-libscrypt-kdf.c, all I did was include a for loop (with the condition (int i = 0; i < OUTPUT_BUFLEN; ++i) in the if block with the condition exitcode == 0.

@gperciva
Copy link
Member

Given what you've described, consider this output: 123, arising from uint8_t. That could arise from 4 different arrays:

  • output[1] = 123
  • ouput[2] = {12, 3}
  • output[2] = {1, 23}
  • output[3] = {1, 2, 3}

Even if you know that the initial array has two values, there's no way to distinguish between those two cases.

@bfamzz
Copy link
Author

bfamzz commented Dec 29, 2019

Thanks @gperciva . You're right. I appreciate.

I am looking for a suitable base64 library to use in encoding the output before printing it like you rightly advised. Can you kindly recommend a library to use? Thanks.

@gperciva
Copy link
Member

Sorry, I haven't looked into base64 libraries. I see that https://github.com/technion/libscrypt uses a library from ISC, so that might be a decent place to start.

@bfamzz
Copy link
Author

bfamzz commented Dec 30, 2019

Thanks @gperciva. After looking around, I found an implementation from Apple https://opensource.apple.com/source/QuickTimeStreamingServer/QuickTimeStreamingServer-452/CommonUtilitiesLib/base64.c and https://opensource.apple.com/source/QuickTimeStreamingServer/QuickTimeStreamingServer-452/CommonUtilitiesLib/base64.h which I included in my project.

Here is the output of a base64 encoded hash:

Exit code is: 0
Encoded hash in base64 is: GD1aptveY4tE1260KNAYeI95wLjxTgBK45dOZaljX8ziBAre28jDrZsERUw8HIBnUED75ZBYUtOBMx2gJUVR9A==

What are your thoughts please? Thanks.

@gperciva
Copy link
Member

gperciva commented Dec 30, 2019

If you want to check if that hash is correct, I suggest using an alternate implementation and seeing if it produces the same hash. You might try https://github.com/technion/libscrypt, or something in the "Other scrypt software" list at the bottom of http://www.tarsnap.com/scrypt.html.

Alternatively, you could start from the hash, decode it, then use scrypt to decrypt the result. You should end up with the same input that you gave to scrypt in the first place.

@gperciva
Copy link
Member

gperciva commented Dec 30, 2019

If you want to discuss general development in C++, use of libraries, and how to use hashes, I suggest posting on https://stackoverflow.com/

@bfamzz
Copy link
Author

bfamzz commented Dec 31, 2019

Thanks @gperciva , you've been so helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants