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

Implement SHA512/t #120

Open
cfcs opened this issue May 16, 2017 · 0 comments
Open

Implement SHA512/t #120

cfcs opened this issue May 16, 2017 · 0 comments

Comments

@cfcs
Copy link

cfcs commented May 16, 2017

It would be nice to have the /t flavor of SHA512 implemented (I specifically care about SHA512/256, but don't see much reason not to implement the generic function).
SHA512/256 is, for example, used in the salsa20 stream cipher, and for libsodium's crypto_auth primitive.

See: https://en.wikipedia.org/wiki/SHA-2

The C code for this was commented out, apparently due to the use of sprintf() in the default: .. of the switch statement, which carries a dependency on libc: https://github.com/mirleft/ocaml-nocrypto/blob/master/src/native/hash/sha512.c#L236

SHA512/t is roughly similar to SHA512, but the initialization is slightly different, as can be seen in the commented-out C code linked to above (the nc_sha512_init_t() function).

The sprintf (if we care about generic t's - again, I mostly care about SHA512/256) can be replaced with something like

memcpy(buf, "SHA-512/", 8);

i = 0;

memset(buf + 8, 0, 4);
int scale = 100;
for(; scale > 0; scale /= 10)
{
    if (t / scale)
        buf[8 + (i++)] = (t / scale) % 10 | 0x30;
}

The algorithm is described here: http://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf

Test vectors are available here:

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

1 participant