bitwiseshiftleft / sjcl

Stanford Javascript Crypto Library

Home Page:http://bitwiseshiftleft.github.com/sjcl/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

scrypt wrong result with certain parameters

guidovranken opened this issue · comments

    var password = sjcl.codec.hex.toBits("70617373776F7264"); /* "password" */
    var salt = sjcl.codec.hex.toBits("73616C74"); /* "salt" */

    var N = 2;
    var r = 7;
    var p = 1;
    var keySize = 32;
    var derivedKey = sjcl.misc.scrypt(password, salt, N, r, p, keySize * 8);
    console.log(sjcl.codec.hex.fromBits(derivedKey));

This prints:

27272e9e07a3143ed35f946a73c575200059562bdcc24e7b1a18d65b3599575a

But it should print:

728b28339ff809588c6c25fa06299b4f7e557b1527876015f0aef7c8581936f2

You can confirm with this Botan program:

#include <botan/scrypt.h>
#include <string>
#include <stdlib.h>

#define CF_CHECK_NE(expr, res) if ( (expr) == (res) ) { goto end; }

int main(int argc, char** argv)
{
    const size_t N = 2;
    const size_t r = 7;
    const size_t p = 1;
    const size_t size = 32;

    unsigned char* out = (uint8_t*)malloc(size);
    const std::string password = "password";
    const std::string salt = "salt";

    try {
        std::unique_ptr<::Botan::PasswordHashFamily> pwdhash_fam = nullptr;
        std::unique_ptr<::Botan::PasswordHash> pwdhash = nullptr;

        /* Initialize */
        {
            CF_CHECK_NE(pwdhash_fam = ::Botan::PasswordHashFamily::create("Scrypt"), nullptr);
            CF_CHECK_NE(pwdhash = pwdhash_fam->from_params(N, r, p), nullptr);

        }

        /* Process */
        {
            pwdhash->derive_key(
                    out,
                    size,
                    password.data(),
                    password.size(),
                    (const uint8_t*)salt.data(),
                    salt.size());
        }

        for (size_t i = 0; i < size; i++) {
            printf("%02X ", out[i]);
        }
        printf("\n");
    } catch ( ... ) { }

end:
    free(out);
    return 0;
}